ctl.c revision 275694
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 275694 2014-12-11 00:25:26Z 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 68#include <cam/cam.h> 69#include <cam/scsi/scsi_all.h> 70#include <cam/scsi/scsi_da.h> 71#include <cam/ctl/ctl_io.h> 72#include <cam/ctl/ctl.h> 73#include <cam/ctl/ctl_frontend.h> 74#include <cam/ctl/ctl_frontend_internal.h> 75#include <cam/ctl/ctl_util.h> 76#include <cam/ctl/ctl_backend.h> 77#include <cam/ctl/ctl_ioctl.h> 78#include <cam/ctl/ctl_ha.h> 79#include <cam/ctl/ctl_private.h> 80#include <cam/ctl/ctl_debug.h> 81#include <cam/ctl/ctl_scsi_all.h> 82#include <cam/ctl/ctl_error.h> 83 84struct ctl_softc *control_softc = NULL; 85 86/* 87 * Size and alignment macros needed for Copan-specific HA hardware. These 88 * can go away when the HA code is re-written, and uses busdma for any 89 * hardware. 90 */ 91#define CTL_ALIGN_8B(target, source, type) \ 92 if (((uint32_t)source & 0x7) != 0) \ 93 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 94 else \ 95 target = (type)source; 96 97#define CTL_SIZE_8B(target, size) \ 98 if ((size & 0x7) != 0) \ 99 target = size + (0x8 - (size & 0x7)); \ 100 else \ 101 target = size; 102 103#define CTL_ALIGN_8B_MARGIN 16 104 105/* 106 * Template mode pages. 107 */ 108 109/* 110 * Note that these are default values only. The actual values will be 111 * filled in when the user does a mode sense. 112 */ 113static struct copan_debugconf_subpage debugconf_page_default = { 114 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 115 DBGCNF_SUBPAGE_CODE, /* subpage */ 116 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 117 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 118 DBGCNF_VERSION, /* page_version */ 119 {CTL_TIME_IO_DEFAULT_SECS>>8, 120 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 121}; 122 123static struct copan_debugconf_subpage debugconf_page_changeable = { 124 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 125 DBGCNF_SUBPAGE_CODE, /* subpage */ 126 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 127 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 128 0, /* page_version */ 129 {0xff,0xff}, /* ctl_time_io_secs */ 130}; 131 132static struct scsi_da_rw_recovery_page rw_er_page_default = { 133 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 134 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 135 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 136 /*read_retry_count*/0, 137 /*correction_span*/0, 138 /*head_offset_count*/0, 139 /*data_strobe_offset_cnt*/0, 140 /*byte8*/SMS_RWER_LBPERE, 141 /*write_retry_count*/0, 142 /*reserved2*/0, 143 /*recovery_time_limit*/{0, 0}, 144}; 145 146static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 147 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 148 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 149 /*byte3*/0, 150 /*read_retry_count*/0, 151 /*correction_span*/0, 152 /*head_offset_count*/0, 153 /*data_strobe_offset_cnt*/0, 154 /*byte8*/0, 155 /*write_retry_count*/0, 156 /*reserved2*/0, 157 /*recovery_time_limit*/{0, 0}, 158}; 159 160static struct scsi_format_page format_page_default = { 161 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 162 /*page_length*/sizeof(struct scsi_format_page) - 2, 163 /*tracks_per_zone*/ {0, 0}, 164 /*alt_sectors_per_zone*/ {0, 0}, 165 /*alt_tracks_per_zone*/ {0, 0}, 166 /*alt_tracks_per_lun*/ {0, 0}, 167 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 168 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 169 /*bytes_per_sector*/ {0, 0}, 170 /*interleave*/ {0, 0}, 171 /*track_skew*/ {0, 0}, 172 /*cylinder_skew*/ {0, 0}, 173 /*flags*/ SFP_HSEC, 174 /*reserved*/ {0, 0, 0} 175}; 176 177static struct scsi_format_page format_page_changeable = { 178 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 179 /*page_length*/sizeof(struct scsi_format_page) - 2, 180 /*tracks_per_zone*/ {0, 0}, 181 /*alt_sectors_per_zone*/ {0, 0}, 182 /*alt_tracks_per_zone*/ {0, 0}, 183 /*alt_tracks_per_lun*/ {0, 0}, 184 /*sectors_per_track*/ {0, 0}, 185 /*bytes_per_sector*/ {0, 0}, 186 /*interleave*/ {0, 0}, 187 /*track_skew*/ {0, 0}, 188 /*cylinder_skew*/ {0, 0}, 189 /*flags*/ 0, 190 /*reserved*/ {0, 0, 0} 191}; 192 193static struct scsi_rigid_disk_page rigid_disk_page_default = { 194 /*page_code*/SMS_RIGID_DISK_PAGE, 195 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 196 /*cylinders*/ {0, 0, 0}, 197 /*heads*/ CTL_DEFAULT_HEADS, 198 /*start_write_precomp*/ {0, 0, 0}, 199 /*start_reduced_current*/ {0, 0, 0}, 200 /*step_rate*/ {0, 0}, 201 /*landing_zone_cylinder*/ {0, 0, 0}, 202 /*rpl*/ SRDP_RPL_DISABLED, 203 /*rotational_offset*/ 0, 204 /*reserved1*/ 0, 205 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 206 CTL_DEFAULT_ROTATION_RATE & 0xff}, 207 /*reserved2*/ {0, 0} 208}; 209 210static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 211 /*page_code*/SMS_RIGID_DISK_PAGE, 212 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 213 /*cylinders*/ {0, 0, 0}, 214 /*heads*/ 0, 215 /*start_write_precomp*/ {0, 0, 0}, 216 /*start_reduced_current*/ {0, 0, 0}, 217 /*step_rate*/ {0, 0}, 218 /*landing_zone_cylinder*/ {0, 0, 0}, 219 /*rpl*/ 0, 220 /*rotational_offset*/ 0, 221 /*reserved1*/ 0, 222 /*rotation_rate*/ {0, 0}, 223 /*reserved2*/ {0, 0} 224}; 225 226static struct scsi_caching_page caching_page_default = { 227 /*page_code*/SMS_CACHING_PAGE, 228 /*page_length*/sizeof(struct scsi_caching_page) - 2, 229 /*flags1*/ SCP_DISC | SCP_WCE, 230 /*ret_priority*/ 0, 231 /*disable_pf_transfer_len*/ {0xff, 0xff}, 232 /*min_prefetch*/ {0, 0}, 233 /*max_prefetch*/ {0xff, 0xff}, 234 /*max_pf_ceiling*/ {0xff, 0xff}, 235 /*flags2*/ 0, 236 /*cache_segments*/ 0, 237 /*cache_seg_size*/ {0, 0}, 238 /*reserved*/ 0, 239 /*non_cache_seg_size*/ {0, 0, 0} 240}; 241 242static struct scsi_caching_page caching_page_changeable = { 243 /*page_code*/SMS_CACHING_PAGE, 244 /*page_length*/sizeof(struct scsi_caching_page) - 2, 245 /*flags1*/ SCP_WCE | SCP_RCD, 246 /*ret_priority*/ 0, 247 /*disable_pf_transfer_len*/ {0, 0}, 248 /*min_prefetch*/ {0, 0}, 249 /*max_prefetch*/ {0, 0}, 250 /*max_pf_ceiling*/ {0, 0}, 251 /*flags2*/ 0, 252 /*cache_segments*/ 0, 253 /*cache_seg_size*/ {0, 0}, 254 /*reserved*/ 0, 255 /*non_cache_seg_size*/ {0, 0, 0} 256}; 257 258static struct scsi_control_page control_page_default = { 259 /*page_code*/SMS_CONTROL_MODE_PAGE, 260 /*page_length*/sizeof(struct scsi_control_page) - 2, 261 /*rlec*/0, 262 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 263 /*eca_and_aen*/0, 264 /*flags4*/SCP_TAS, 265 /*aen_holdoff_period*/{0, 0}, 266 /*busy_timeout_period*/{0, 0}, 267 /*extended_selftest_completion_time*/{0, 0} 268}; 269 270static struct scsi_control_page control_page_changeable = { 271 /*page_code*/SMS_CONTROL_MODE_PAGE, 272 /*page_length*/sizeof(struct scsi_control_page) - 2, 273 /*rlec*/SCP_DSENSE, 274 /*queue_flags*/SCP_QUEUE_ALG_MASK, 275 /*eca_and_aen*/SCP_SWP, 276 /*flags4*/0, 277 /*aen_holdoff_period*/{0, 0}, 278 /*busy_timeout_period*/{0, 0}, 279 /*extended_selftest_completion_time*/{0, 0} 280}; 281 282static struct scsi_info_exceptions_page ie_page_default = { 283 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 284 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 285 /*info_flags*/SIEP_FLAGS_DEXCPT, 286 /*mrie*/0, 287 /*interval_timer*/{0, 0, 0, 0}, 288 /*report_count*/{0, 0, 0, 0} 289}; 290 291static struct scsi_info_exceptions_page ie_page_changeable = { 292 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 293 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 294 /*info_flags*/0, 295 /*mrie*/0, 296 /*interval_timer*/{0, 0, 0, 0}, 297 /*report_count*/{0, 0, 0, 0} 298}; 299 300#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 301 302static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 303 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 304 /*subpage_code*/0x02, 305 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 306 /*flags*/0, 307 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 308 /*descr*/{}}, 309 {{/*flags*/0, 310 /*resource*/0x01, 311 /*reserved*/{0, 0}, 312 /*count*/{0, 0, 0, 0}}, 313 {/*flags*/0, 314 /*resource*/0x02, 315 /*reserved*/{0, 0}, 316 /*count*/{0, 0, 0, 0}}, 317 {/*flags*/0, 318 /*resource*/0xf1, 319 /*reserved*/{0, 0}, 320 /*count*/{0, 0, 0, 0}}, 321 {/*flags*/0, 322 /*resource*/0xf2, 323 /*reserved*/{0, 0}, 324 /*count*/{0, 0, 0, 0}} 325 } 326}; 327 328static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 329 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 330 /*subpage_code*/0x02, 331 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 332 /*flags*/0, 333 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 334 /*descr*/{}}, 335 {{/*flags*/0, 336 /*resource*/0, 337 /*reserved*/{0, 0}, 338 /*count*/{0, 0, 0, 0}}, 339 {/*flags*/0, 340 /*resource*/0, 341 /*reserved*/{0, 0}, 342 /*count*/{0, 0, 0, 0}}, 343 {/*flags*/0, 344 /*resource*/0, 345 /*reserved*/{0, 0}, 346 /*count*/{0, 0, 0, 0}}, 347 {/*flags*/0, 348 /*resource*/0, 349 /*reserved*/{0, 0}, 350 /*count*/{0, 0, 0, 0}} 351 } 352}; 353 354/* 355 * XXX KDM move these into the softc. 356 */ 357static int rcv_sync_msg; 358static int persis_offset; 359static uint8_t ctl_pause_rtr; 360 361SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 362static int worker_threads = -1; 363TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 364SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 365 &worker_threads, 1, "Number of worker threads"); 366static int ctl_debug = CTL_DEBUG_NONE; 367TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug); 368SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 369 &ctl_debug, 0, "Enabled debug flags"); 370 371/* 372 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 373 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 374 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 375 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 376 */ 377#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 378 379static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 380 int param); 381static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 382static int ctl_init(void); 383void ctl_shutdown(void); 384static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 385static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 386static void ctl_ioctl_online(void *arg); 387static void ctl_ioctl_offline(void *arg); 388static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 389static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 390static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 391static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 392static int ctl_ioctl_submit_wait(union ctl_io *io); 393static void ctl_ioctl_datamove(union ctl_io *io); 394static void ctl_ioctl_done(union ctl_io *io); 395static void ctl_ioctl_hard_startstop_callback(void *arg, 396 struct cfi_metatask *metatask); 397static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 398static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 399 struct ctl_ooa *ooa_hdr, 400 struct ctl_ooa_entry *kern_entries); 401static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 402 struct thread *td); 403static uint32_t ctl_map_lun(int port_num, uint32_t lun); 404static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 405#ifdef unused 406static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 407 uint32_t targ_target, uint32_t targ_lun, 408 int can_wait); 409static void ctl_kfree_io(union ctl_io *io); 410#endif /* unused */ 411static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 412 struct ctl_be_lun *be_lun, struct ctl_id target_id); 413static int ctl_free_lun(struct ctl_lun *lun); 414static void ctl_create_lun(struct ctl_be_lun *be_lun); 415/** 416static void ctl_failover_change_pages(struct ctl_softc *softc, 417 struct ctl_scsiio *ctsio, int master); 418**/ 419 420static int ctl_do_mode_select(union ctl_io *io); 421static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 422 uint64_t res_key, uint64_t sa_res_key, 423 uint8_t type, uint32_t residx, 424 struct ctl_scsiio *ctsio, 425 struct scsi_per_res_out *cdb, 426 struct scsi_per_res_out_parms* param); 427static void ctl_pro_preempt_other(struct ctl_lun *lun, 428 union ctl_ha_msg *msg); 429static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 430static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 431static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 432static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 433static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 434static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 435static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 436 int alloc_len); 437static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 438 int alloc_len); 439static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 440static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 441static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 442static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 443static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 444static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 445static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 446 union ctl_io *pending_io, union ctl_io *ooa_io); 447static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 448 union ctl_io *starting_io); 449static int ctl_check_blocked(struct ctl_lun *lun); 450static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 451 struct ctl_lun *lun, 452 const struct ctl_cmd_entry *entry, 453 struct ctl_scsiio *ctsio); 454//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 455static void ctl_failover(void); 456static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 457 struct ctl_scsiio *ctsio); 458static int ctl_scsiio(struct ctl_scsiio *ctsio); 459 460static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 461static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 462 ctl_ua_type ua_type); 463static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 464 ctl_ua_type ua_type); 465static int ctl_abort_task(union ctl_io *io); 466static int ctl_abort_task_set(union ctl_io *io); 467static int ctl_i_t_nexus_reset(union ctl_io *io); 468static void ctl_run_task(union ctl_io *io); 469#ifdef CTL_IO_DELAY 470static void ctl_datamove_timer_wakeup(void *arg); 471static void ctl_done_timer_wakeup(void *arg); 472#endif /* CTL_IO_DELAY */ 473 474static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 475static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 476static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 477static void ctl_datamove_remote_write(union ctl_io *io); 478static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 479static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 480static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 481static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 482 ctl_ha_dt_cb callback); 483static void ctl_datamove_remote_read(union ctl_io *io); 484static void ctl_datamove_remote(union ctl_io *io); 485static int ctl_process_done(union ctl_io *io); 486static void ctl_lun_thread(void *arg); 487static void ctl_thresh_thread(void *arg); 488static void ctl_work_thread(void *arg); 489static void ctl_enqueue_incoming(union ctl_io *io); 490static void ctl_enqueue_rtr(union ctl_io *io); 491static void ctl_enqueue_done(union ctl_io *io); 492static void ctl_enqueue_isc(union ctl_io *io); 493static const struct ctl_cmd_entry * 494 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 495static const struct ctl_cmd_entry * 496 ctl_validate_command(struct ctl_scsiio *ctsio); 497static int ctl_cmd_applicable(uint8_t lun_type, 498 const struct ctl_cmd_entry *entry); 499 500/* 501 * Load the serialization table. This isn't very pretty, but is probably 502 * the easiest way to do it. 503 */ 504#include "ctl_ser_table.c" 505 506/* 507 * We only need to define open, close and ioctl routines for this driver. 508 */ 509static struct cdevsw ctl_cdevsw = { 510 .d_version = D_VERSION, 511 .d_flags = 0, 512 .d_open = ctl_open, 513 .d_close = ctl_close, 514 .d_ioctl = ctl_ioctl, 515 .d_name = "ctl", 516}; 517 518 519MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 520MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 521 522static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 523 524static moduledata_t ctl_moduledata = { 525 "ctl", 526 ctl_module_event_handler, 527 NULL 528}; 529 530DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 531MODULE_VERSION(ctl, 1); 532 533static struct ctl_frontend ioctl_frontend = 534{ 535 .name = "ioctl", 536}; 537 538static void 539ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 540 union ctl_ha_msg *msg_info) 541{ 542 struct ctl_scsiio *ctsio; 543 544 if (msg_info->hdr.original_sc == NULL) { 545 printf("%s: original_sc == NULL!\n", __func__); 546 /* XXX KDM now what? */ 547 return; 548 } 549 550 ctsio = &msg_info->hdr.original_sc->scsiio; 551 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 552 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 553 ctsio->io_hdr.status = msg_info->hdr.status; 554 ctsio->scsi_status = msg_info->scsi.scsi_status; 555 ctsio->sense_len = msg_info->scsi.sense_len; 556 ctsio->sense_residual = msg_info->scsi.sense_residual; 557 ctsio->residual = msg_info->scsi.residual; 558 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 559 sizeof(ctsio->sense_data)); 560 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 561 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 562 ctl_enqueue_isc((union ctl_io *)ctsio); 563} 564 565static void 566ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 567 union ctl_ha_msg *msg_info) 568{ 569 struct ctl_scsiio *ctsio; 570 571 if (msg_info->hdr.serializing_sc == NULL) { 572 printf("%s: serializing_sc == NULL!\n", __func__); 573 /* XXX KDM now what? */ 574 return; 575 } 576 577 ctsio = &msg_info->hdr.serializing_sc->scsiio; 578#if 0 579 /* 580 * Attempt to catch the situation where an I/O has 581 * been freed, and we're using it again. 582 */ 583 if (ctsio->io_hdr.io_type == 0xff) { 584 union ctl_io *tmp_io; 585 tmp_io = (union ctl_io *)ctsio; 586 printf("%s: %p use after free!\n", __func__, 587 ctsio); 588 printf("%s: type %d msg %d cdb %x iptl: " 589 "%d:%d:%d:%d tag 0x%04x " 590 "flag %#x status %x\n", 591 __func__, 592 tmp_io->io_hdr.io_type, 593 tmp_io->io_hdr.msg_type, 594 tmp_io->scsiio.cdb[0], 595 tmp_io->io_hdr.nexus.initid.id, 596 tmp_io->io_hdr.nexus.targ_port, 597 tmp_io->io_hdr.nexus.targ_target.id, 598 tmp_io->io_hdr.nexus.targ_lun, 599 (tmp_io->io_hdr.io_type == 600 CTL_IO_TASK) ? 601 tmp_io->taskio.tag_num : 602 tmp_io->scsiio.tag_num, 603 tmp_io->io_hdr.flags, 604 tmp_io->io_hdr.status); 605 } 606#endif 607 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 608 ctl_enqueue_isc((union ctl_io *)ctsio); 609} 610 611/* 612 * ISC (Inter Shelf Communication) event handler. Events from the HA 613 * subsystem come in here. 614 */ 615static void 616ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 617{ 618 struct ctl_softc *ctl_softc; 619 union ctl_io *io; 620 struct ctl_prio *presio; 621 ctl_ha_status isc_status; 622 623 ctl_softc = control_softc; 624 io = NULL; 625 626 627#if 0 628 printf("CTL: Isc Msg event %d\n", event); 629#endif 630 if (event == CTL_HA_EVT_MSG_RECV) { 631 union ctl_ha_msg msg_info; 632 633 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 634 sizeof(msg_info), /*wait*/ 0); 635#if 0 636 printf("CTL: msg_type %d\n", msg_info.msg_type); 637#endif 638 if (isc_status != 0) { 639 printf("Error receiving message, status = %d\n", 640 isc_status); 641 return; 642 } 643 644 switch (msg_info.hdr.msg_type) { 645 case CTL_MSG_SERIALIZE: 646#if 0 647 printf("Serialize\n"); 648#endif 649 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 650 if (io == NULL) { 651 printf("ctl_isc_event_handler: can't allocate " 652 "ctl_io!\n"); 653 /* Bad Juju */ 654 /* Need to set busy and send msg back */ 655 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 656 msg_info.hdr.status = CTL_SCSI_ERROR; 657 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 658 msg_info.scsi.sense_len = 0; 659 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 660 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 661 } 662 goto bailout; 663 } 664 ctl_zero_io(io); 665 // populate ctsio from msg_info 666 io->io_hdr.io_type = CTL_IO_SCSI; 667 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 668 io->io_hdr.original_sc = msg_info.hdr.original_sc; 669#if 0 670 printf("pOrig %x\n", (int)msg_info.original_sc); 671#endif 672 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 673 CTL_FLAG_IO_ACTIVE; 674 /* 675 * If we're in serialization-only mode, we don't 676 * want to go through full done processing. Thus 677 * the COPY flag. 678 * 679 * XXX KDM add another flag that is more specific. 680 */ 681 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 682 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 683 io->io_hdr.nexus = msg_info.hdr.nexus; 684#if 0 685 printf("targ %d, port %d, iid %d, lun %d\n", 686 io->io_hdr.nexus.targ_target.id, 687 io->io_hdr.nexus.targ_port, 688 io->io_hdr.nexus.initid.id, 689 io->io_hdr.nexus.targ_lun); 690#endif 691 io->scsiio.tag_num = msg_info.scsi.tag_num; 692 io->scsiio.tag_type = msg_info.scsi.tag_type; 693 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 694 CTL_MAX_CDBLEN); 695 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 696 const struct ctl_cmd_entry *entry; 697 698 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 699 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 700 io->io_hdr.flags |= 701 entry->flags & CTL_FLAG_DATA_MASK; 702 } 703 ctl_enqueue_isc(io); 704 break; 705 706 /* Performed on the Originating SC, XFER mode only */ 707 case CTL_MSG_DATAMOVE: { 708 struct ctl_sg_entry *sgl; 709 int i, j; 710 711 io = msg_info.hdr.original_sc; 712 if (io == NULL) { 713 printf("%s: original_sc == NULL!\n", __func__); 714 /* XXX KDM do something here */ 715 break; 716 } 717 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 718 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 719 /* 720 * Keep track of this, we need to send it back over 721 * when the datamove is complete. 722 */ 723 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 724 725 if (msg_info.dt.sg_sequence == 0) { 726 /* 727 * XXX KDM we use the preallocated S/G list 728 * here, but we'll need to change this to 729 * dynamic allocation if we need larger S/G 730 * lists. 731 */ 732 if (msg_info.dt.kern_sg_entries > 733 sizeof(io->io_hdr.remote_sglist) / 734 sizeof(io->io_hdr.remote_sglist[0])) { 735 printf("%s: number of S/G entries " 736 "needed %u > allocated num %zd\n", 737 __func__, 738 msg_info.dt.kern_sg_entries, 739 sizeof(io->io_hdr.remote_sglist)/ 740 sizeof(io->io_hdr.remote_sglist[0])); 741 742 /* 743 * XXX KDM send a message back to 744 * the other side to shut down the 745 * DMA. The error will come back 746 * through via the normal channel. 747 */ 748 break; 749 } 750 sgl = io->io_hdr.remote_sglist; 751 memset(sgl, 0, 752 sizeof(io->io_hdr.remote_sglist)); 753 754 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 755 756 io->scsiio.kern_sg_entries = 757 msg_info.dt.kern_sg_entries; 758 io->scsiio.rem_sg_entries = 759 msg_info.dt.kern_sg_entries; 760 io->scsiio.kern_data_len = 761 msg_info.dt.kern_data_len; 762 io->scsiio.kern_total_len = 763 msg_info.dt.kern_total_len; 764 io->scsiio.kern_data_resid = 765 msg_info.dt.kern_data_resid; 766 io->scsiio.kern_rel_offset = 767 msg_info.dt.kern_rel_offset; 768 /* 769 * Clear out per-DMA flags. 770 */ 771 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 772 /* 773 * Add per-DMA flags that are set for this 774 * particular DMA request. 775 */ 776 io->io_hdr.flags |= msg_info.dt.flags & 777 CTL_FLAG_RDMA_MASK; 778 } else 779 sgl = (struct ctl_sg_entry *) 780 io->scsiio.kern_data_ptr; 781 782 for (i = msg_info.dt.sent_sg_entries, j = 0; 783 i < (msg_info.dt.sent_sg_entries + 784 msg_info.dt.cur_sg_entries); i++, j++) { 785 sgl[i].addr = msg_info.dt.sg_list[j].addr; 786 sgl[i].len = msg_info.dt.sg_list[j].len; 787 788#if 0 789 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 790 __func__, 791 msg_info.dt.sg_list[j].addr, 792 msg_info.dt.sg_list[j].len, 793 sgl[i].addr, sgl[i].len, j, i); 794#endif 795 } 796#if 0 797 memcpy(&sgl[msg_info.dt.sent_sg_entries], 798 msg_info.dt.sg_list, 799 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 800#endif 801 802 /* 803 * If this is the last piece of the I/O, we've got 804 * the full S/G list. Queue processing in the thread. 805 * Otherwise wait for the next piece. 806 */ 807 if (msg_info.dt.sg_last != 0) 808 ctl_enqueue_isc(io); 809 break; 810 } 811 /* Performed on the Serializing (primary) SC, XFER mode only */ 812 case CTL_MSG_DATAMOVE_DONE: { 813 if (msg_info.hdr.serializing_sc == NULL) { 814 printf("%s: serializing_sc == NULL!\n", 815 __func__); 816 /* XXX KDM now what? */ 817 break; 818 } 819 /* 820 * We grab the sense information here in case 821 * there was a failure, so we can return status 822 * back to the initiator. 823 */ 824 io = msg_info.hdr.serializing_sc; 825 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 826 io->io_hdr.status = msg_info.hdr.status; 827 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 828 io->scsiio.sense_len = msg_info.scsi.sense_len; 829 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 830 io->io_hdr.port_status = msg_info.scsi.fetd_status; 831 io->scsiio.residual = msg_info.scsi.residual; 832 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 833 sizeof(io->scsiio.sense_data)); 834 ctl_enqueue_isc(io); 835 break; 836 } 837 838 /* Preformed on Originating SC, SER_ONLY mode */ 839 case CTL_MSG_R2R: 840 io = msg_info.hdr.original_sc; 841 if (io == NULL) { 842 printf("%s: Major Bummer\n", __func__); 843 return; 844 } else { 845#if 0 846 printf("pOrig %x\n",(int) ctsio); 847#endif 848 } 849 io->io_hdr.msg_type = CTL_MSG_R2R; 850 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 851 ctl_enqueue_isc(io); 852 break; 853 854 /* 855 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 856 * mode. 857 * Performed on the Originating (i.e. secondary) SC in XFER 858 * mode 859 */ 860 case CTL_MSG_FINISH_IO: 861 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 862 ctl_isc_handler_finish_xfer(ctl_softc, 863 &msg_info); 864 else 865 ctl_isc_handler_finish_ser_only(ctl_softc, 866 &msg_info); 867 break; 868 869 /* Preformed on Originating SC */ 870 case CTL_MSG_BAD_JUJU: 871 io = msg_info.hdr.original_sc; 872 if (io == NULL) { 873 printf("%s: Bad JUJU!, original_sc is NULL!\n", 874 __func__); 875 break; 876 } 877 ctl_copy_sense_data(&msg_info, io); 878 /* 879 * IO should have already been cleaned up on other 880 * SC so clear this flag so we won't send a message 881 * back to finish the IO there. 882 */ 883 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 884 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 885 886 /* io = msg_info.hdr.serializing_sc; */ 887 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 888 ctl_enqueue_isc(io); 889 break; 890 891 /* Handle resets sent from the other side */ 892 case CTL_MSG_MANAGE_TASKS: { 893 struct ctl_taskio *taskio; 894 taskio = (struct ctl_taskio *)ctl_alloc_io( 895 (void *)ctl_softc->othersc_pool); 896 if (taskio == NULL) { 897 printf("ctl_isc_event_handler: can't allocate " 898 "ctl_io!\n"); 899 /* Bad Juju */ 900 /* should I just call the proper reset func 901 here??? */ 902 goto bailout; 903 } 904 ctl_zero_io((union ctl_io *)taskio); 905 taskio->io_hdr.io_type = CTL_IO_TASK; 906 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 907 taskio->io_hdr.nexus = msg_info.hdr.nexus; 908 taskio->task_action = msg_info.task.task_action; 909 taskio->tag_num = msg_info.task.tag_num; 910 taskio->tag_type = msg_info.task.tag_type; 911#ifdef CTL_TIME_IO 912 taskio->io_hdr.start_time = time_uptime; 913 getbintime(&taskio->io_hdr.start_bt); 914#if 0 915 cs_prof_gettime(&taskio->io_hdr.start_ticks); 916#endif 917#endif /* CTL_TIME_IO */ 918 ctl_run_task((union ctl_io *)taskio); 919 break; 920 } 921 /* Persistent Reserve action which needs attention */ 922 case CTL_MSG_PERS_ACTION: 923 presio = (struct ctl_prio *)ctl_alloc_io( 924 (void *)ctl_softc->othersc_pool); 925 if (presio == NULL) { 926 printf("ctl_isc_event_handler: can't allocate " 927 "ctl_io!\n"); 928 /* Bad Juju */ 929 /* Need to set busy and send msg back */ 930 goto bailout; 931 } 932 ctl_zero_io((union ctl_io *)presio); 933 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 934 presio->pr_msg = msg_info.pr; 935 ctl_enqueue_isc((union ctl_io *)presio); 936 break; 937 case CTL_MSG_SYNC_FE: 938 rcv_sync_msg = 1; 939 break; 940 default: 941 printf("How did I get here?\n"); 942 } 943 } else if (event == CTL_HA_EVT_MSG_SENT) { 944 if (param != CTL_HA_STATUS_SUCCESS) { 945 printf("Bad status from ctl_ha_msg_send status %d\n", 946 param); 947 } 948 return; 949 } else if (event == CTL_HA_EVT_DISCONNECT) { 950 printf("CTL: Got a disconnect from Isc\n"); 951 return; 952 } else { 953 printf("ctl_isc_event_handler: Unknown event %d\n", event); 954 return; 955 } 956 957bailout: 958 return; 959} 960 961static void 962ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 963{ 964 struct scsi_sense_data *sense; 965 966 sense = &dest->scsiio.sense_data; 967 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 968 dest->scsiio.scsi_status = src->scsi.scsi_status; 969 dest->scsiio.sense_len = src->scsi.sense_len; 970 dest->io_hdr.status = src->hdr.status; 971} 972 973static int 974ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS) 975{ 976 struct ctl_softc *softc = (struct ctl_softc *)arg1; 977 struct ctl_lun *lun; 978 int error, value, i; 979 980 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) 981 value = 0; 982 else 983 value = 1; 984 985 error = sysctl_handle_int(oidp, &value, 0, req); 986 if ((error != 0) || (req->newptr == NULL)) 987 return (error); 988 989 mtx_lock(&softc->ctl_lock); 990 if (value == 0) 991 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 992 else 993 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 994 STAILQ_FOREACH(lun, &softc->lun_list, links) { 995 mtx_lock(&lun->lun_lock); 996 for (i = 0; i < CTL_MAX_INITIATORS; i++) 997 lun->pending_ua[i] |= CTL_UA_ASYM_ACC_CHANGE; 998 mtx_unlock(&lun->lun_lock); 999 } 1000 mtx_unlock(&softc->ctl_lock); 1001 return (0); 1002} 1003 1004static int 1005ctl_init(void) 1006{ 1007 struct ctl_softc *softc; 1008 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 1009 struct ctl_port *port; 1010 int i, error, retval; 1011 //int isc_retval; 1012 1013 retval = 0; 1014 ctl_pause_rtr = 0; 1015 rcv_sync_msg = 0; 1016 1017 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1018 M_WAITOK | M_ZERO); 1019 softc = control_softc; 1020 1021 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1022 "cam/ctl"); 1023 1024 softc->dev->si_drv1 = softc; 1025 1026 /* 1027 * By default, return a "bad LUN" peripheral qualifier for unknown 1028 * LUNs. The user can override this default using the tunable or 1029 * sysctl. See the comment in ctl_inquiry_std() for more details. 1030 */ 1031 softc->inquiry_pq_no_lun = 1; 1032 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 1033 &softc->inquiry_pq_no_lun); 1034 sysctl_ctx_init(&softc->sysctl_ctx); 1035 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1036 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1037 CTLFLAG_RD, 0, "CAM Target Layer"); 1038 1039 if (softc->sysctl_tree == NULL) { 1040 printf("%s: unable to allocate sysctl tree\n", __func__); 1041 destroy_dev(softc->dev); 1042 free(control_softc, M_DEVBUF); 1043 control_softc = NULL; 1044 return (ENOMEM); 1045 } 1046 1047 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1048 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1049 "inquiry_pq_no_lun", CTLFLAG_RW, 1050 &softc->inquiry_pq_no_lun, 0, 1051 "Report no lun possible for invalid LUNs"); 1052 1053 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1054 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 1055 softc->open_count = 0; 1056 1057 /* 1058 * Default to actually sending a SYNCHRONIZE CACHE command down to 1059 * the drive. 1060 */ 1061 softc->flags = CTL_FLAG_REAL_SYNC; 1062 1063 /* 1064 * In Copan's HA scheme, the "master" and "slave" roles are 1065 * figured out through the slot the controller is in. Although it 1066 * is an active/active system, someone has to be in charge. 1067 */ 1068 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1069 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1070 "HA head ID (0 - no HA)"); 1071 if (softc->ha_id == 0) { 1072 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1073 softc->is_single = 1; 1074 softc->port_offset = 0; 1075 } else 1076 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS; 1077 persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT; 1078 1079 /* 1080 * XXX KDM need to figure out where we want to get our target ID 1081 * and WWID. Is it different on each port? 1082 */ 1083 softc->target.id = 0; 1084 softc->target.wwid[0] = 0x12345678; 1085 softc->target.wwid[1] = 0x87654321; 1086 STAILQ_INIT(&softc->lun_list); 1087 STAILQ_INIT(&softc->pending_lun_queue); 1088 STAILQ_INIT(&softc->fe_list); 1089 STAILQ_INIT(&softc->port_list); 1090 STAILQ_INIT(&softc->be_list); 1091 STAILQ_INIT(&softc->io_pools); 1092 ctl_tpc_init(softc); 1093 1094 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1095 &internal_pool)!= 0){ 1096 printf("ctl: can't allocate %d entry internal pool, " 1097 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1098 return (ENOMEM); 1099 } 1100 1101 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1102 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1103 printf("ctl: can't allocate %d entry emergency pool, " 1104 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1105 ctl_pool_free(internal_pool); 1106 return (ENOMEM); 1107 } 1108 1109 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1110 &other_pool) != 0) 1111 { 1112 printf("ctl: can't allocate %d entry other SC pool, " 1113 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1114 ctl_pool_free(internal_pool); 1115 ctl_pool_free(emergency_pool); 1116 return (ENOMEM); 1117 } 1118 1119 softc->internal_pool = internal_pool; 1120 softc->emergency_pool = emergency_pool; 1121 softc->othersc_pool = other_pool; 1122 1123 if (worker_threads <= 0) 1124 worker_threads = max(1, mp_ncpus / 4); 1125 if (worker_threads > CTL_MAX_THREADS) 1126 worker_threads = CTL_MAX_THREADS; 1127 1128 for (i = 0; i < worker_threads; i++) { 1129 struct ctl_thread *thr = &softc->threads[i]; 1130 1131 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1132 thr->ctl_softc = softc; 1133 STAILQ_INIT(&thr->incoming_queue); 1134 STAILQ_INIT(&thr->rtr_queue); 1135 STAILQ_INIT(&thr->done_queue); 1136 STAILQ_INIT(&thr->isc_queue); 1137 1138 error = kproc_kthread_add(ctl_work_thread, thr, 1139 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1140 if (error != 0) { 1141 printf("error creating CTL work thread!\n"); 1142 ctl_pool_free(internal_pool); 1143 ctl_pool_free(emergency_pool); 1144 ctl_pool_free(other_pool); 1145 return (error); 1146 } 1147 } 1148 error = kproc_kthread_add(ctl_lun_thread, softc, 1149 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1150 if (error != 0) { 1151 printf("error creating CTL lun thread!\n"); 1152 ctl_pool_free(internal_pool); 1153 ctl_pool_free(emergency_pool); 1154 ctl_pool_free(other_pool); 1155 return (error); 1156 } 1157 error = kproc_kthread_add(ctl_thresh_thread, softc, 1158 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1159 if (error != 0) { 1160 printf("error creating CTL threshold thread!\n"); 1161 ctl_pool_free(internal_pool); 1162 ctl_pool_free(emergency_pool); 1163 ctl_pool_free(other_pool); 1164 return (error); 1165 } 1166 if (bootverbose) 1167 printf("ctl: CAM Target Layer loaded\n"); 1168 1169 /* 1170 * Initialize the ioctl front end. 1171 */ 1172 ctl_frontend_register(&ioctl_frontend); 1173 port = &softc->ioctl_info.port; 1174 port->frontend = &ioctl_frontend; 1175 sprintf(softc->ioctl_info.port_name, "ioctl"); 1176 port->port_type = CTL_PORT_IOCTL; 1177 port->num_requested_ctl_io = 100; 1178 port->port_name = softc->ioctl_info.port_name; 1179 port->port_online = ctl_ioctl_online; 1180 port->port_offline = ctl_ioctl_offline; 1181 port->onoff_arg = &softc->ioctl_info; 1182 port->lun_enable = ctl_ioctl_lun_enable; 1183 port->lun_disable = ctl_ioctl_lun_disable; 1184 port->targ_lun_arg = &softc->ioctl_info; 1185 port->fe_datamove = ctl_ioctl_datamove; 1186 port->fe_done = ctl_ioctl_done; 1187 port->max_targets = 15; 1188 port->max_target_id = 15; 1189 1190 if (ctl_port_register(&softc->ioctl_info.port) != 0) { 1191 printf("ctl: ioctl front end registration failed, will " 1192 "continue anyway\n"); 1193 } 1194 1195 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1196 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN, 1197 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head"); 1198 1199#ifdef CTL_IO_DELAY 1200 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1201 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1202 sizeof(struct callout), CTL_TIMER_BYTES); 1203 return (EINVAL); 1204 } 1205#endif /* CTL_IO_DELAY */ 1206 1207 return (0); 1208} 1209 1210void 1211ctl_shutdown(void) 1212{ 1213 struct ctl_softc *softc; 1214 struct ctl_lun *lun, *next_lun; 1215 struct ctl_io_pool *pool; 1216 1217 softc = (struct ctl_softc *)control_softc; 1218 1219 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1220 printf("ctl: ioctl front end deregistration failed\n"); 1221 1222 mtx_lock(&softc->ctl_lock); 1223 1224 /* 1225 * Free up each LUN. 1226 */ 1227 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1228 next_lun = STAILQ_NEXT(lun, links); 1229 ctl_free_lun(lun); 1230 } 1231 1232 mtx_unlock(&softc->ctl_lock); 1233 1234 ctl_frontend_deregister(&ioctl_frontend); 1235 1236 /* 1237 * This will rip the rug out from under any FETDs or anyone else 1238 * that has a pool allocated. Since we increment our module 1239 * refcount any time someone outside the main CTL module allocates 1240 * a pool, we shouldn't have any problems here. The user won't be 1241 * able to unload the CTL module until client modules have 1242 * successfully unloaded. 1243 */ 1244 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1245 ctl_pool_free(pool); 1246 1247#if 0 1248 ctl_shutdown_thread(softc->work_thread); 1249 mtx_destroy(&softc->queue_lock); 1250#endif 1251 1252 ctl_tpc_shutdown(softc); 1253 mtx_destroy(&softc->pool_lock); 1254 mtx_destroy(&softc->ctl_lock); 1255 1256 destroy_dev(softc->dev); 1257 1258 sysctl_ctx_free(&softc->sysctl_ctx); 1259 1260 free(control_softc, M_DEVBUF); 1261 control_softc = NULL; 1262 1263 if (bootverbose) 1264 printf("ctl: CAM Target Layer unloaded\n"); 1265} 1266 1267static int 1268ctl_module_event_handler(module_t mod, int what, void *arg) 1269{ 1270 1271 switch (what) { 1272 case MOD_LOAD: 1273 return (ctl_init()); 1274 case MOD_UNLOAD: 1275 return (EBUSY); 1276 default: 1277 return (EOPNOTSUPP); 1278 } 1279} 1280 1281/* 1282 * XXX KDM should we do some access checks here? Bump a reference count to 1283 * prevent a CTL module from being unloaded while someone has it open? 1284 */ 1285static int 1286ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1287{ 1288 return (0); 1289} 1290 1291static int 1292ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1293{ 1294 return (0); 1295} 1296 1297int 1298ctl_port_enable(ctl_port_type port_type) 1299{ 1300 struct ctl_softc *softc = control_softc; 1301 struct ctl_port *port; 1302 1303 if (softc->is_single == 0) { 1304 union ctl_ha_msg msg_info; 1305 int isc_retval; 1306 1307#if 0 1308 printf("%s: HA mode, synchronizing frontend enable\n", 1309 __func__); 1310#endif 1311 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1312 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1313 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1314 printf("Sync msg send error retval %d\n", isc_retval); 1315 } 1316 if (!rcv_sync_msg) { 1317 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1318 sizeof(msg_info), 1); 1319 } 1320#if 0 1321 printf("CTL:Frontend Enable\n"); 1322 } else { 1323 printf("%s: single mode, skipping frontend synchronization\n", 1324 __func__); 1325#endif 1326 } 1327 1328 STAILQ_FOREACH(port, &softc->port_list, links) { 1329 if (port_type & port->port_type) 1330 { 1331#if 0 1332 printf("port %d\n", port->targ_port); 1333#endif 1334 ctl_port_online(port); 1335 } 1336 } 1337 1338 return (0); 1339} 1340 1341int 1342ctl_port_disable(ctl_port_type port_type) 1343{ 1344 struct ctl_softc *softc; 1345 struct ctl_port *port; 1346 1347 softc = control_softc; 1348 1349 STAILQ_FOREACH(port, &softc->port_list, links) { 1350 if (port_type & port->port_type) 1351 ctl_port_offline(port); 1352 } 1353 1354 return (0); 1355} 1356 1357/* 1358 * Returns 0 for success, 1 for failure. 1359 * Currently the only failure mode is if there aren't enough entries 1360 * allocated. So, in case of a failure, look at num_entries_dropped, 1361 * reallocate and try again. 1362 */ 1363int 1364ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1365 int *num_entries_filled, int *num_entries_dropped, 1366 ctl_port_type port_type, int no_virtual) 1367{ 1368 struct ctl_softc *softc; 1369 struct ctl_port *port; 1370 int entries_dropped, entries_filled; 1371 int retval; 1372 int i; 1373 1374 softc = control_softc; 1375 1376 retval = 0; 1377 entries_filled = 0; 1378 entries_dropped = 0; 1379 1380 i = 0; 1381 mtx_lock(&softc->ctl_lock); 1382 STAILQ_FOREACH(port, &softc->port_list, links) { 1383 struct ctl_port_entry *entry; 1384 1385 if ((port->port_type & port_type) == 0) 1386 continue; 1387 1388 if ((no_virtual != 0) 1389 && (port->virtual_port != 0)) 1390 continue; 1391 1392 if (entries_filled >= num_entries_alloced) { 1393 entries_dropped++; 1394 continue; 1395 } 1396 entry = &entries[i]; 1397 1398 entry->port_type = port->port_type; 1399 strlcpy(entry->port_name, port->port_name, 1400 sizeof(entry->port_name)); 1401 entry->physical_port = port->physical_port; 1402 entry->virtual_port = port->virtual_port; 1403 entry->wwnn = port->wwnn; 1404 entry->wwpn = port->wwpn; 1405 1406 i++; 1407 entries_filled++; 1408 } 1409 1410 mtx_unlock(&softc->ctl_lock); 1411 1412 if (entries_dropped > 0) 1413 retval = 1; 1414 1415 *num_entries_dropped = entries_dropped; 1416 *num_entries_filled = entries_filled; 1417 1418 return (retval); 1419} 1420 1421static void 1422ctl_ioctl_online(void *arg) 1423{ 1424 struct ctl_ioctl_info *ioctl_info; 1425 1426 ioctl_info = (struct ctl_ioctl_info *)arg; 1427 1428 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1429} 1430 1431static void 1432ctl_ioctl_offline(void *arg) 1433{ 1434 struct ctl_ioctl_info *ioctl_info; 1435 1436 ioctl_info = (struct ctl_ioctl_info *)arg; 1437 1438 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1439} 1440 1441/* 1442 * Remove an initiator by port number and initiator ID. 1443 * Returns 0 for success, -1 for failure. 1444 */ 1445int 1446ctl_remove_initiator(struct ctl_port *port, int iid) 1447{ 1448 struct ctl_softc *softc = control_softc; 1449 1450 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1451 1452 if (iid > CTL_MAX_INIT_PER_PORT) { 1453 printf("%s: initiator ID %u > maximun %u!\n", 1454 __func__, iid, CTL_MAX_INIT_PER_PORT); 1455 return (-1); 1456 } 1457 1458 mtx_lock(&softc->ctl_lock); 1459 port->wwpn_iid[iid].in_use--; 1460 port->wwpn_iid[iid].last_use = time_uptime; 1461 mtx_unlock(&softc->ctl_lock); 1462 1463 return (0); 1464} 1465 1466/* 1467 * Add an initiator to the initiator map. 1468 * Returns iid for success, < 0 for failure. 1469 */ 1470int 1471ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1472{ 1473 struct ctl_softc *softc = control_softc; 1474 time_t best_time; 1475 int i, best; 1476 1477 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1478 1479 if (iid >= CTL_MAX_INIT_PER_PORT) { 1480 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1481 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1482 free(name, M_CTL); 1483 return (-1); 1484 } 1485 1486 mtx_lock(&softc->ctl_lock); 1487 1488 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1489 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1490 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1491 iid = i; 1492 break; 1493 } 1494 if (name != NULL && port->wwpn_iid[i].name != NULL && 1495 strcmp(name, port->wwpn_iid[i].name) == 0) { 1496 iid = i; 1497 break; 1498 } 1499 } 1500 } 1501 1502 if (iid < 0) { 1503 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1504 if (port->wwpn_iid[i].in_use == 0 && 1505 port->wwpn_iid[i].wwpn == 0 && 1506 port->wwpn_iid[i].name == NULL) { 1507 iid = i; 1508 break; 1509 } 1510 } 1511 } 1512 1513 if (iid < 0) { 1514 best = -1; 1515 best_time = INT32_MAX; 1516 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1517 if (port->wwpn_iid[i].in_use == 0) { 1518 if (port->wwpn_iid[i].last_use < best_time) { 1519 best = i; 1520 best_time = port->wwpn_iid[i].last_use; 1521 } 1522 } 1523 } 1524 iid = best; 1525 } 1526 1527 if (iid < 0) { 1528 mtx_unlock(&softc->ctl_lock); 1529 free(name, M_CTL); 1530 return (-2); 1531 } 1532 1533 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1534 /* 1535 * This is not an error yet. 1536 */ 1537 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1538#if 0 1539 printf("%s: port %d iid %u WWPN %#jx arrived" 1540 " again\n", __func__, port->targ_port, 1541 iid, (uintmax_t)wwpn); 1542#endif 1543 goto take; 1544 } 1545 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1546 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1547#if 0 1548 printf("%s: port %d iid %u name '%s' arrived" 1549 " again\n", __func__, port->targ_port, 1550 iid, name); 1551#endif 1552 goto take; 1553 } 1554 1555 /* 1556 * This is an error, but what do we do about it? The 1557 * driver is telling us we have a new WWPN for this 1558 * initiator ID, so we pretty much need to use it. 1559 */ 1560 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1561 " but WWPN %#jx '%s' is still at that address\n", 1562 __func__, port->targ_port, iid, wwpn, name, 1563 (uintmax_t)port->wwpn_iid[iid].wwpn, 1564 port->wwpn_iid[iid].name); 1565 1566 /* 1567 * XXX KDM clear have_ca and ua_pending on each LUN for 1568 * this initiator. 1569 */ 1570 } 1571take: 1572 free(port->wwpn_iid[iid].name, M_CTL); 1573 port->wwpn_iid[iid].name = name; 1574 port->wwpn_iid[iid].wwpn = wwpn; 1575 port->wwpn_iid[iid].in_use++; 1576 mtx_unlock(&softc->ctl_lock); 1577 1578 return (iid); 1579} 1580 1581static int 1582ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1583{ 1584 int len; 1585 1586 switch (port->port_type) { 1587 case CTL_PORT_FC: 1588 { 1589 struct scsi_transportid_fcp *id = 1590 (struct scsi_transportid_fcp *)buf; 1591 if (port->wwpn_iid[iid].wwpn == 0) 1592 return (0); 1593 memset(id, 0, sizeof(*id)); 1594 id->format_protocol = SCSI_PROTO_FC; 1595 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1596 return (sizeof(*id)); 1597 } 1598 case CTL_PORT_ISCSI: 1599 { 1600 struct scsi_transportid_iscsi_port *id = 1601 (struct scsi_transportid_iscsi_port *)buf; 1602 if (port->wwpn_iid[iid].name == NULL) 1603 return (0); 1604 memset(id, 0, 256); 1605 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1606 SCSI_PROTO_ISCSI; 1607 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1608 len = roundup2(min(len, 252), 4); 1609 scsi_ulto2b(len, id->additional_length); 1610 return (sizeof(*id) + len); 1611 } 1612 case CTL_PORT_SAS: 1613 { 1614 struct scsi_transportid_sas *id = 1615 (struct scsi_transportid_sas *)buf; 1616 if (port->wwpn_iid[iid].wwpn == 0) 1617 return (0); 1618 memset(id, 0, sizeof(*id)); 1619 id->format_protocol = SCSI_PROTO_SAS; 1620 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1621 return (sizeof(*id)); 1622 } 1623 default: 1624 { 1625 struct scsi_transportid_spi *id = 1626 (struct scsi_transportid_spi *)buf; 1627 memset(id, 0, sizeof(*id)); 1628 id->format_protocol = SCSI_PROTO_SPI; 1629 scsi_ulto2b(iid, id->scsi_addr); 1630 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1631 return (sizeof(*id)); 1632 } 1633 } 1634} 1635 1636static int 1637ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1638{ 1639 return (0); 1640} 1641 1642static int 1643ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1644{ 1645 return (0); 1646} 1647 1648/* 1649 * Data movement routine for the CTL ioctl frontend port. 1650 */ 1651static int 1652ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1653{ 1654 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1655 struct ctl_sg_entry ext_entry, kern_entry; 1656 int ext_sglen, ext_sg_entries, kern_sg_entries; 1657 int ext_sg_start, ext_offset; 1658 int len_to_copy, len_copied; 1659 int kern_watermark, ext_watermark; 1660 int ext_sglist_malloced; 1661 int i, j; 1662 1663 ext_sglist_malloced = 0; 1664 ext_sg_start = 0; 1665 ext_offset = 0; 1666 1667 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1668 1669 /* 1670 * If this flag is set, fake the data transfer. 1671 */ 1672 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1673 ctsio->ext_data_filled = ctsio->ext_data_len; 1674 goto bailout; 1675 } 1676 1677 /* 1678 * To simplify things here, if we have a single buffer, stick it in 1679 * a S/G entry and just make it a single entry S/G list. 1680 */ 1681 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1682 int len_seen; 1683 1684 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1685 1686 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1687 M_WAITOK); 1688 ext_sglist_malloced = 1; 1689 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1690 ext_sglen) != 0) { 1691 ctl_set_internal_failure(ctsio, 1692 /*sks_valid*/ 0, 1693 /*retry_count*/ 0); 1694 goto bailout; 1695 } 1696 ext_sg_entries = ctsio->ext_sg_entries; 1697 len_seen = 0; 1698 for (i = 0; i < ext_sg_entries; i++) { 1699 if ((len_seen + ext_sglist[i].len) >= 1700 ctsio->ext_data_filled) { 1701 ext_sg_start = i; 1702 ext_offset = ctsio->ext_data_filled - len_seen; 1703 break; 1704 } 1705 len_seen += ext_sglist[i].len; 1706 } 1707 } else { 1708 ext_sglist = &ext_entry; 1709 ext_sglist->addr = ctsio->ext_data_ptr; 1710 ext_sglist->len = ctsio->ext_data_len; 1711 ext_sg_entries = 1; 1712 ext_sg_start = 0; 1713 ext_offset = ctsio->ext_data_filled; 1714 } 1715 1716 if (ctsio->kern_sg_entries > 0) { 1717 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1718 kern_sg_entries = ctsio->kern_sg_entries; 1719 } else { 1720 kern_sglist = &kern_entry; 1721 kern_sglist->addr = ctsio->kern_data_ptr; 1722 kern_sglist->len = ctsio->kern_data_len; 1723 kern_sg_entries = 1; 1724 } 1725 1726 1727 kern_watermark = 0; 1728 ext_watermark = ext_offset; 1729 len_copied = 0; 1730 for (i = ext_sg_start, j = 0; 1731 i < ext_sg_entries && j < kern_sg_entries;) { 1732 uint8_t *ext_ptr, *kern_ptr; 1733 1734 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1735 kern_sglist[j].len - kern_watermark); 1736 1737 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1738 ext_ptr = ext_ptr + ext_watermark; 1739 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1740 /* 1741 * XXX KDM fix this! 1742 */ 1743 panic("need to implement bus address support"); 1744#if 0 1745 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1746#endif 1747 } else 1748 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1749 kern_ptr = kern_ptr + kern_watermark; 1750 1751 kern_watermark += len_to_copy; 1752 ext_watermark += len_to_copy; 1753 1754 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1755 CTL_FLAG_DATA_IN) { 1756 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1757 "bytes to user\n", len_to_copy)); 1758 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1759 "to %p\n", kern_ptr, ext_ptr)); 1760 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1761 ctl_set_internal_failure(ctsio, 1762 /*sks_valid*/ 0, 1763 /*retry_count*/ 0); 1764 goto bailout; 1765 } 1766 } else { 1767 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1768 "bytes from user\n", len_to_copy)); 1769 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1770 "to %p\n", ext_ptr, kern_ptr)); 1771 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1772 ctl_set_internal_failure(ctsio, 1773 /*sks_valid*/ 0, 1774 /*retry_count*/0); 1775 goto bailout; 1776 } 1777 } 1778 1779 len_copied += len_to_copy; 1780 1781 if (ext_sglist[i].len == ext_watermark) { 1782 i++; 1783 ext_watermark = 0; 1784 } 1785 1786 if (kern_sglist[j].len == kern_watermark) { 1787 j++; 1788 kern_watermark = 0; 1789 } 1790 } 1791 1792 ctsio->ext_data_filled += len_copied; 1793 1794 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1795 "kern_sg_entries: %d\n", ext_sg_entries, 1796 kern_sg_entries)); 1797 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1798 "kern_data_len = %d\n", ctsio->ext_data_len, 1799 ctsio->kern_data_len)); 1800 1801 1802 /* XXX KDM set residual?? */ 1803bailout: 1804 1805 if (ext_sglist_malloced != 0) 1806 free(ext_sglist, M_CTL); 1807 1808 return (CTL_RETVAL_COMPLETE); 1809} 1810 1811/* 1812 * Serialize a command that went down the "wrong" side, and so was sent to 1813 * this controller for execution. The logic is a little different than the 1814 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1815 * sent back to the other side, but in the success case, we execute the 1816 * command on this side (XFER mode) or tell the other side to execute it 1817 * (SER_ONLY mode). 1818 */ 1819static int 1820ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1821{ 1822 struct ctl_softc *ctl_softc; 1823 union ctl_ha_msg msg_info; 1824 struct ctl_lun *lun; 1825 int retval = 0; 1826 uint32_t targ_lun; 1827 1828 ctl_softc = control_softc; 1829 1830 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1831 lun = ctl_softc->ctl_luns[targ_lun]; 1832 if (lun==NULL) 1833 { 1834 /* 1835 * Why isn't LUN defined? The other side wouldn't 1836 * send a cmd if the LUN is undefined. 1837 */ 1838 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1839 1840 /* "Logical unit not supported" */ 1841 ctl_set_sense_data(&msg_info.scsi.sense_data, 1842 lun, 1843 /*sense_format*/SSD_TYPE_NONE, 1844 /*current_error*/ 1, 1845 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1846 /*asc*/ 0x25, 1847 /*ascq*/ 0x00, 1848 SSD_ELEM_NONE); 1849 1850 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1851 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1852 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1853 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1854 msg_info.hdr.serializing_sc = NULL; 1855 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1856 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1857 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1858 } 1859 return(1); 1860 1861 } 1862 1863 mtx_lock(&lun->lun_lock); 1864 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1865 1866 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1867 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1868 ooa_links))) { 1869 case CTL_ACTION_BLOCK: 1870 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1871 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1872 blocked_links); 1873 break; 1874 case CTL_ACTION_PASS: 1875 case CTL_ACTION_SKIP: 1876 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1877 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1878 ctl_enqueue_rtr((union ctl_io *)ctsio); 1879 } else { 1880 1881 /* send msg back to other side */ 1882 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1883 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1884 msg_info.hdr.msg_type = CTL_MSG_R2R; 1885#if 0 1886 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1887#endif 1888 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1889 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1890 } 1891 } 1892 break; 1893 case CTL_ACTION_OVERLAP: 1894 /* OVERLAPPED COMMANDS ATTEMPTED */ 1895 ctl_set_sense_data(&msg_info.scsi.sense_data, 1896 lun, 1897 /*sense_format*/SSD_TYPE_NONE, 1898 /*current_error*/ 1, 1899 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1900 /*asc*/ 0x4E, 1901 /*ascq*/ 0x00, 1902 SSD_ELEM_NONE); 1903 1904 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1905 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1906 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1907 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1908 msg_info.hdr.serializing_sc = NULL; 1909 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1910#if 0 1911 printf("BAD JUJU:Major Bummer Overlap\n"); 1912#endif 1913 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1914 retval = 1; 1915 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1916 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1917 } 1918 break; 1919 case CTL_ACTION_OVERLAP_TAG: 1920 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1921 ctl_set_sense_data(&msg_info.scsi.sense_data, 1922 lun, 1923 /*sense_format*/SSD_TYPE_NONE, 1924 /*current_error*/ 1, 1925 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1926 /*asc*/ 0x4D, 1927 /*ascq*/ ctsio->tag_num & 0xff, 1928 SSD_ELEM_NONE); 1929 1930 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1931 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1932 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1933 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1934 msg_info.hdr.serializing_sc = NULL; 1935 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1936#if 0 1937 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1938#endif 1939 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1940 retval = 1; 1941 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1942 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1943 } 1944 break; 1945 case CTL_ACTION_ERROR: 1946 default: 1947 /* "Internal target failure" */ 1948 ctl_set_sense_data(&msg_info.scsi.sense_data, 1949 lun, 1950 /*sense_format*/SSD_TYPE_NONE, 1951 /*current_error*/ 1, 1952 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1953 /*asc*/ 0x44, 1954 /*ascq*/ 0x00, 1955 SSD_ELEM_NONE); 1956 1957 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1958 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1959 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1960 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1961 msg_info.hdr.serializing_sc = NULL; 1962 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1963#if 0 1964 printf("BAD JUJU:Major Bummer HW Error\n"); 1965#endif 1966 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1967 retval = 1; 1968 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1969 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1970 } 1971 break; 1972 } 1973 mtx_unlock(&lun->lun_lock); 1974 return (retval); 1975} 1976 1977static int 1978ctl_ioctl_submit_wait(union ctl_io *io) 1979{ 1980 struct ctl_fe_ioctl_params params; 1981 ctl_fe_ioctl_state last_state; 1982 int done, retval; 1983 1984 retval = 0; 1985 1986 bzero(¶ms, sizeof(params)); 1987 1988 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1989 cv_init(¶ms.sem, "ctlioccv"); 1990 params.state = CTL_IOCTL_INPROG; 1991 last_state = params.state; 1992 1993 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1994 1995 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1996 1997 /* This shouldn't happen */ 1998 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1999 return (retval); 2000 2001 done = 0; 2002 2003 do { 2004 mtx_lock(¶ms.ioctl_mtx); 2005 /* 2006 * Check the state here, and don't sleep if the state has 2007 * already changed (i.e. wakeup has already occured, but we 2008 * weren't waiting yet). 2009 */ 2010 if (params.state == last_state) { 2011 /* XXX KDM cv_wait_sig instead? */ 2012 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 2013 } 2014 last_state = params.state; 2015 2016 switch (params.state) { 2017 case CTL_IOCTL_INPROG: 2018 /* Why did we wake up? */ 2019 /* XXX KDM error here? */ 2020 mtx_unlock(¶ms.ioctl_mtx); 2021 break; 2022 case CTL_IOCTL_DATAMOVE: 2023 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 2024 2025 /* 2026 * change last_state back to INPROG to avoid 2027 * deadlock on subsequent data moves. 2028 */ 2029 params.state = last_state = CTL_IOCTL_INPROG; 2030 2031 mtx_unlock(¶ms.ioctl_mtx); 2032 ctl_ioctl_do_datamove(&io->scsiio); 2033 /* 2034 * Note that in some cases, most notably writes, 2035 * this will queue the I/O and call us back later. 2036 * In other cases, generally reads, this routine 2037 * will immediately call back and wake us up, 2038 * probably using our own context. 2039 */ 2040 io->scsiio.be_move_done(io); 2041 break; 2042 case CTL_IOCTL_DONE: 2043 mtx_unlock(¶ms.ioctl_mtx); 2044 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 2045 done = 1; 2046 break; 2047 default: 2048 mtx_unlock(¶ms.ioctl_mtx); 2049 /* XXX KDM error here? */ 2050 break; 2051 } 2052 } while (done == 0); 2053 2054 mtx_destroy(¶ms.ioctl_mtx); 2055 cv_destroy(¶ms.sem); 2056 2057 return (CTL_RETVAL_COMPLETE); 2058} 2059 2060static void 2061ctl_ioctl_datamove(union ctl_io *io) 2062{ 2063 struct ctl_fe_ioctl_params *params; 2064 2065 params = (struct ctl_fe_ioctl_params *) 2066 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2067 2068 mtx_lock(¶ms->ioctl_mtx); 2069 params->state = CTL_IOCTL_DATAMOVE; 2070 cv_broadcast(¶ms->sem); 2071 mtx_unlock(¶ms->ioctl_mtx); 2072} 2073 2074static void 2075ctl_ioctl_done(union ctl_io *io) 2076{ 2077 struct ctl_fe_ioctl_params *params; 2078 2079 params = (struct ctl_fe_ioctl_params *) 2080 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2081 2082 mtx_lock(¶ms->ioctl_mtx); 2083 params->state = CTL_IOCTL_DONE; 2084 cv_broadcast(¶ms->sem); 2085 mtx_unlock(¶ms->ioctl_mtx); 2086} 2087 2088static void 2089ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2090{ 2091 struct ctl_fe_ioctl_startstop_info *sd_info; 2092 2093 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2094 2095 sd_info->hs_info.status = metatask->status; 2096 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2097 sd_info->hs_info.luns_complete = 2098 metatask->taskinfo.startstop.luns_complete; 2099 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2100 2101 cv_broadcast(&sd_info->sem); 2102} 2103 2104static void 2105ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2106{ 2107 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2108 2109 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2110 2111 mtx_lock(fe_bbr_info->lock); 2112 fe_bbr_info->bbr_info->status = metatask->status; 2113 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2114 fe_bbr_info->wakeup_done = 1; 2115 mtx_unlock(fe_bbr_info->lock); 2116 2117 cv_broadcast(&fe_bbr_info->sem); 2118} 2119 2120/* 2121 * Returns 0 for success, errno for failure. 2122 */ 2123static int 2124ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2125 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2126{ 2127 union ctl_io *io; 2128 int retval; 2129 2130 retval = 0; 2131 2132 mtx_lock(&lun->lun_lock); 2133 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2134 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2135 ooa_links)) { 2136 struct ctl_ooa_entry *entry; 2137 2138 /* 2139 * If we've got more than we can fit, just count the 2140 * remaining entries. 2141 */ 2142 if (*cur_fill_num >= ooa_hdr->alloc_num) 2143 continue; 2144 2145 entry = &kern_entries[*cur_fill_num]; 2146 2147 entry->tag_num = io->scsiio.tag_num; 2148 entry->lun_num = lun->lun; 2149#ifdef CTL_TIME_IO 2150 entry->start_bt = io->io_hdr.start_bt; 2151#endif 2152 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2153 entry->cdb_len = io->scsiio.cdb_len; 2154 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2155 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2156 2157 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2158 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2159 2160 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2161 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2162 2163 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2164 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2165 2166 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2167 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2168 } 2169 mtx_unlock(&lun->lun_lock); 2170 2171 return (retval); 2172} 2173 2174static void * 2175ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2176 size_t error_str_len) 2177{ 2178 void *kptr; 2179 2180 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2181 2182 if (copyin(user_addr, kptr, len) != 0) { 2183 snprintf(error_str, error_str_len, "Error copying %d bytes " 2184 "from user address %p to kernel address %p", len, 2185 user_addr, kptr); 2186 free(kptr, M_CTL); 2187 return (NULL); 2188 } 2189 2190 return (kptr); 2191} 2192 2193static void 2194ctl_free_args(int num_args, struct ctl_be_arg *args) 2195{ 2196 int i; 2197 2198 if (args == NULL) 2199 return; 2200 2201 for (i = 0; i < num_args; i++) { 2202 free(args[i].kname, M_CTL); 2203 free(args[i].kvalue, M_CTL); 2204 } 2205 2206 free(args, M_CTL); 2207} 2208 2209static struct ctl_be_arg * 2210ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2211 char *error_str, size_t error_str_len) 2212{ 2213 struct ctl_be_arg *args; 2214 int i; 2215 2216 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2217 error_str, error_str_len); 2218 2219 if (args == NULL) 2220 goto bailout; 2221 2222 for (i = 0; i < num_args; i++) { 2223 args[i].kname = NULL; 2224 args[i].kvalue = NULL; 2225 } 2226 2227 for (i = 0; i < num_args; i++) { 2228 uint8_t *tmpptr; 2229 2230 args[i].kname = ctl_copyin_alloc(args[i].name, 2231 args[i].namelen, error_str, error_str_len); 2232 if (args[i].kname == NULL) 2233 goto bailout; 2234 2235 if (args[i].kname[args[i].namelen - 1] != '\0') { 2236 snprintf(error_str, error_str_len, "Argument %d " 2237 "name is not NUL-terminated", i); 2238 goto bailout; 2239 } 2240 2241 if (args[i].flags & CTL_BEARG_RD) { 2242 tmpptr = ctl_copyin_alloc(args[i].value, 2243 args[i].vallen, error_str, error_str_len); 2244 if (tmpptr == NULL) 2245 goto bailout; 2246 if ((args[i].flags & CTL_BEARG_ASCII) 2247 && (tmpptr[args[i].vallen - 1] != '\0')) { 2248 snprintf(error_str, error_str_len, "Argument " 2249 "%d value is not NUL-terminated", i); 2250 goto bailout; 2251 } 2252 args[i].kvalue = tmpptr; 2253 } else { 2254 args[i].kvalue = malloc(args[i].vallen, 2255 M_CTL, M_WAITOK | M_ZERO); 2256 } 2257 } 2258 2259 return (args); 2260bailout: 2261 2262 ctl_free_args(num_args, args); 2263 2264 return (NULL); 2265} 2266 2267static void 2268ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2269{ 2270 int i; 2271 2272 for (i = 0; i < num_args; i++) { 2273 if (args[i].flags & CTL_BEARG_WR) 2274 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2275 } 2276} 2277 2278/* 2279 * Escape characters that are illegal or not recommended in XML. 2280 */ 2281int 2282ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2283{ 2284 char *end = str + size; 2285 int retval; 2286 2287 retval = 0; 2288 2289 for (; *str && str < end; str++) { 2290 switch (*str) { 2291 case '&': 2292 retval = sbuf_printf(sb, "&"); 2293 break; 2294 case '>': 2295 retval = sbuf_printf(sb, ">"); 2296 break; 2297 case '<': 2298 retval = sbuf_printf(sb, "<"); 2299 break; 2300 default: 2301 retval = sbuf_putc(sb, *str); 2302 break; 2303 } 2304 2305 if (retval != 0) 2306 break; 2307 2308 } 2309 2310 return (retval); 2311} 2312 2313static void 2314ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2315{ 2316 struct scsi_vpd_id_descriptor *desc; 2317 int i; 2318 2319 if (id == NULL || id->len < 4) 2320 return; 2321 desc = (struct scsi_vpd_id_descriptor *)id->data; 2322 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2323 case SVPD_ID_TYPE_T10: 2324 sbuf_printf(sb, "t10."); 2325 break; 2326 case SVPD_ID_TYPE_EUI64: 2327 sbuf_printf(sb, "eui."); 2328 break; 2329 case SVPD_ID_TYPE_NAA: 2330 sbuf_printf(sb, "naa."); 2331 break; 2332 case SVPD_ID_TYPE_SCSI_NAME: 2333 break; 2334 } 2335 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2336 case SVPD_ID_CODESET_BINARY: 2337 for (i = 0; i < desc->length; i++) 2338 sbuf_printf(sb, "%02x", desc->identifier[i]); 2339 break; 2340 case SVPD_ID_CODESET_ASCII: 2341 sbuf_printf(sb, "%.*s", (int)desc->length, 2342 (char *)desc->identifier); 2343 break; 2344 case SVPD_ID_CODESET_UTF8: 2345 sbuf_printf(sb, "%s", (char *)desc->identifier); 2346 break; 2347 } 2348} 2349 2350static int 2351ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2352 struct thread *td) 2353{ 2354 struct ctl_softc *softc; 2355 int retval; 2356 2357 softc = control_softc; 2358 2359 retval = 0; 2360 2361 switch (cmd) { 2362 case CTL_IO: { 2363 union ctl_io *io; 2364 void *pool_tmp; 2365 2366 /* 2367 * If we haven't been "enabled", don't allow any SCSI I/O 2368 * to this FETD. 2369 */ 2370 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2371 retval = EPERM; 2372 break; 2373 } 2374 2375 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2376 if (io == NULL) { 2377 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2378 retval = ENOSPC; 2379 break; 2380 } 2381 2382 /* 2383 * Need to save the pool reference so it doesn't get 2384 * spammed by the user's ctl_io. 2385 */ 2386 pool_tmp = io->io_hdr.pool; 2387 2388 memcpy(io, (void *)addr, sizeof(*io)); 2389 2390 io->io_hdr.pool = pool_tmp; 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 struct ctl_lun *lun; 2475 2476 STAILQ_FOREACH(lun, &softc->lun_list, 2477 links) { 2478 port->lun_enable(port->targ_lun_arg, 2479 lun->target, 2480 lun->lun); 2481 } 2482 2483 ctl_port_online(port); 2484 } else if (cmd == CTL_DISABLE_PORT) { 2485 struct ctl_lun *lun; 2486 2487 ctl_port_offline(port); 2488 2489 STAILQ_FOREACH(lun, &softc->lun_list, 2490 links) { 2491 port->lun_disable( 2492 port->targ_lun_arg, 2493 lun->target, 2494 lun->lun); 2495 } 2496 } 2497 2498 mtx_lock(&softc->ctl_lock); 2499 2500 if (cmd == CTL_SET_PORT_WWNS) 2501 ctl_port_set_wwns(port, 2502 (entry->flags & CTL_PORT_WWNN_VALID) ? 2503 1 : 0, entry->wwnn, 2504 (entry->flags & CTL_PORT_WWPN_VALID) ? 2505 1 : 0, entry->wwpn); 2506 } 2507 if (done != 0) 2508 break; 2509 } 2510 mtx_unlock(&softc->ctl_lock); 2511 break; 2512 } 2513 case CTL_GET_PORT_LIST: { 2514 struct ctl_port *port; 2515 struct ctl_port_list *list; 2516 int i; 2517 2518 list = (struct ctl_port_list *)addr; 2519 2520 if (list->alloc_len != (list->alloc_num * 2521 sizeof(struct ctl_port_entry))) { 2522 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2523 "alloc_num %u * sizeof(struct ctl_port_entry) " 2524 "%zu\n", __func__, list->alloc_len, 2525 list->alloc_num, sizeof(struct ctl_port_entry)); 2526 retval = EINVAL; 2527 break; 2528 } 2529 list->fill_len = 0; 2530 list->fill_num = 0; 2531 list->dropped_num = 0; 2532 i = 0; 2533 mtx_lock(&softc->ctl_lock); 2534 STAILQ_FOREACH(port, &softc->port_list, links) { 2535 struct ctl_port_entry entry, *list_entry; 2536 2537 if (list->fill_num >= list->alloc_num) { 2538 list->dropped_num++; 2539 continue; 2540 } 2541 2542 entry.port_type = port->port_type; 2543 strlcpy(entry.port_name, port->port_name, 2544 sizeof(entry.port_name)); 2545 entry.targ_port = port->targ_port; 2546 entry.physical_port = port->physical_port; 2547 entry.virtual_port = port->virtual_port; 2548 entry.wwnn = port->wwnn; 2549 entry.wwpn = port->wwpn; 2550 if (port->status & CTL_PORT_STATUS_ONLINE) 2551 entry.online = 1; 2552 else 2553 entry.online = 0; 2554 2555 list_entry = &list->entries[i]; 2556 2557 retval = copyout(&entry, list_entry, sizeof(entry)); 2558 if (retval != 0) { 2559 printf("%s: CTL_GET_PORT_LIST: copyout " 2560 "returned %d\n", __func__, retval); 2561 break; 2562 } 2563 i++; 2564 list->fill_num++; 2565 list->fill_len += sizeof(entry); 2566 } 2567 mtx_unlock(&softc->ctl_lock); 2568 2569 /* 2570 * If this is non-zero, we had a copyout fault, so there's 2571 * probably no point in attempting to set the status inside 2572 * the structure. 2573 */ 2574 if (retval != 0) 2575 break; 2576 2577 if (list->dropped_num > 0) 2578 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2579 else 2580 list->status = CTL_PORT_LIST_OK; 2581 break; 2582 } 2583 case CTL_DUMP_OOA: { 2584 struct ctl_lun *lun; 2585 union ctl_io *io; 2586 char printbuf[128]; 2587 struct sbuf sb; 2588 2589 mtx_lock(&softc->ctl_lock); 2590 printf("Dumping OOA queues:\n"); 2591 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2592 mtx_lock(&lun->lun_lock); 2593 for (io = (union ctl_io *)TAILQ_FIRST( 2594 &lun->ooa_queue); io != NULL; 2595 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2596 ooa_links)) { 2597 sbuf_new(&sb, printbuf, sizeof(printbuf), 2598 SBUF_FIXEDLEN); 2599 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2600 (intmax_t)lun->lun, 2601 io->scsiio.tag_num, 2602 (io->io_hdr.flags & 2603 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2604 (io->io_hdr.flags & 2605 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2606 (io->io_hdr.flags & 2607 CTL_FLAG_ABORT) ? " ABORT" : "", 2608 (io->io_hdr.flags & 2609 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2610 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2611 sbuf_finish(&sb); 2612 printf("%s\n", sbuf_data(&sb)); 2613 } 2614 mtx_unlock(&lun->lun_lock); 2615 } 2616 printf("OOA queues dump done\n"); 2617 mtx_unlock(&softc->ctl_lock); 2618 break; 2619 } 2620 case CTL_GET_OOA: { 2621 struct ctl_lun *lun; 2622 struct ctl_ooa *ooa_hdr; 2623 struct ctl_ooa_entry *entries; 2624 uint32_t cur_fill_num; 2625 2626 ooa_hdr = (struct ctl_ooa *)addr; 2627 2628 if ((ooa_hdr->alloc_len == 0) 2629 || (ooa_hdr->alloc_num == 0)) { 2630 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2631 "must be non-zero\n", __func__, 2632 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2633 retval = EINVAL; 2634 break; 2635 } 2636 2637 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2638 sizeof(struct ctl_ooa_entry))) { 2639 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2640 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2641 __func__, ooa_hdr->alloc_len, 2642 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2643 retval = EINVAL; 2644 break; 2645 } 2646 2647 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2648 if (entries == NULL) { 2649 printf("%s: could not allocate %d bytes for OOA " 2650 "dump\n", __func__, ooa_hdr->alloc_len); 2651 retval = ENOMEM; 2652 break; 2653 } 2654 2655 mtx_lock(&softc->ctl_lock); 2656 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2657 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2658 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2659 mtx_unlock(&softc->ctl_lock); 2660 free(entries, M_CTL); 2661 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2662 __func__, (uintmax_t)ooa_hdr->lun_num); 2663 retval = EINVAL; 2664 break; 2665 } 2666 2667 cur_fill_num = 0; 2668 2669 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2670 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2671 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2672 ooa_hdr, entries); 2673 if (retval != 0) 2674 break; 2675 } 2676 if (retval != 0) { 2677 mtx_unlock(&softc->ctl_lock); 2678 free(entries, M_CTL); 2679 break; 2680 } 2681 } else { 2682 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2683 2684 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2685 entries); 2686 } 2687 mtx_unlock(&softc->ctl_lock); 2688 2689 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2690 ooa_hdr->fill_len = ooa_hdr->fill_num * 2691 sizeof(struct ctl_ooa_entry); 2692 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2693 if (retval != 0) { 2694 printf("%s: error copying out %d bytes for OOA dump\n", 2695 __func__, ooa_hdr->fill_len); 2696 } 2697 2698 getbintime(&ooa_hdr->cur_bt); 2699 2700 if (cur_fill_num > ooa_hdr->alloc_num) { 2701 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2702 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2703 } else { 2704 ooa_hdr->dropped_num = 0; 2705 ooa_hdr->status = CTL_OOA_OK; 2706 } 2707 2708 free(entries, M_CTL); 2709 break; 2710 } 2711 case CTL_CHECK_OOA: { 2712 union ctl_io *io; 2713 struct ctl_lun *lun; 2714 struct ctl_ooa_info *ooa_info; 2715 2716 2717 ooa_info = (struct ctl_ooa_info *)addr; 2718 2719 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2720 ooa_info->status = CTL_OOA_INVALID_LUN; 2721 break; 2722 } 2723 mtx_lock(&softc->ctl_lock); 2724 lun = softc->ctl_luns[ooa_info->lun_id]; 2725 if (lun == NULL) { 2726 mtx_unlock(&softc->ctl_lock); 2727 ooa_info->status = CTL_OOA_INVALID_LUN; 2728 break; 2729 } 2730 mtx_lock(&lun->lun_lock); 2731 mtx_unlock(&softc->ctl_lock); 2732 ooa_info->num_entries = 0; 2733 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2734 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2735 &io->io_hdr, ooa_links)) { 2736 ooa_info->num_entries++; 2737 } 2738 mtx_unlock(&lun->lun_lock); 2739 2740 ooa_info->status = CTL_OOA_SUCCESS; 2741 2742 break; 2743 } 2744 case CTL_HARD_START: 2745 case CTL_HARD_STOP: { 2746 struct ctl_fe_ioctl_startstop_info ss_info; 2747 struct cfi_metatask *metatask; 2748 struct mtx hs_mtx; 2749 2750 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2751 2752 cv_init(&ss_info.sem, "hard start/stop cv" ); 2753 2754 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2755 if (metatask == NULL) { 2756 retval = ENOMEM; 2757 mtx_destroy(&hs_mtx); 2758 break; 2759 } 2760 2761 if (cmd == CTL_HARD_START) 2762 metatask->tasktype = CFI_TASK_STARTUP; 2763 else 2764 metatask->tasktype = CFI_TASK_SHUTDOWN; 2765 2766 metatask->callback = ctl_ioctl_hard_startstop_callback; 2767 metatask->callback_arg = &ss_info; 2768 2769 cfi_action(metatask); 2770 2771 /* Wait for the callback */ 2772 mtx_lock(&hs_mtx); 2773 cv_wait_sig(&ss_info.sem, &hs_mtx); 2774 mtx_unlock(&hs_mtx); 2775 2776 /* 2777 * All information has been copied from the metatask by the 2778 * time cv_broadcast() is called, so we free the metatask here. 2779 */ 2780 cfi_free_metatask(metatask); 2781 2782 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2783 2784 mtx_destroy(&hs_mtx); 2785 break; 2786 } 2787 case CTL_BBRREAD: { 2788 struct ctl_bbrread_info *bbr_info; 2789 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2790 struct mtx bbr_mtx; 2791 struct cfi_metatask *metatask; 2792 2793 bbr_info = (struct ctl_bbrread_info *)addr; 2794 2795 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2796 2797 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2798 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2799 2800 fe_bbr_info.bbr_info = bbr_info; 2801 fe_bbr_info.lock = &bbr_mtx; 2802 2803 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2804 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2805 2806 if (metatask == NULL) { 2807 mtx_destroy(&bbr_mtx); 2808 cv_destroy(&fe_bbr_info.sem); 2809 retval = ENOMEM; 2810 break; 2811 } 2812 metatask->tasktype = CFI_TASK_BBRREAD; 2813 metatask->callback = ctl_ioctl_bbrread_callback; 2814 metatask->callback_arg = &fe_bbr_info; 2815 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2816 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2817 metatask->taskinfo.bbrread.len = bbr_info->len; 2818 2819 cfi_action(metatask); 2820 2821 mtx_lock(&bbr_mtx); 2822 while (fe_bbr_info.wakeup_done == 0) 2823 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2824 mtx_unlock(&bbr_mtx); 2825 2826 bbr_info->status = metatask->status; 2827 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2828 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2829 memcpy(&bbr_info->sense_data, 2830 &metatask->taskinfo.bbrread.sense_data, 2831 ctl_min(sizeof(bbr_info->sense_data), 2832 sizeof(metatask->taskinfo.bbrread.sense_data))); 2833 2834 cfi_free_metatask(metatask); 2835 2836 mtx_destroy(&bbr_mtx); 2837 cv_destroy(&fe_bbr_info.sem); 2838 2839 break; 2840 } 2841 case CTL_DELAY_IO: { 2842 struct ctl_io_delay_info *delay_info; 2843#ifdef CTL_IO_DELAY 2844 struct ctl_lun *lun; 2845#endif /* CTL_IO_DELAY */ 2846 2847 delay_info = (struct ctl_io_delay_info *)addr; 2848 2849#ifdef CTL_IO_DELAY 2850 mtx_lock(&softc->ctl_lock); 2851 2852 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2853 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2854 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2855 } else { 2856 lun = softc->ctl_luns[delay_info->lun_id]; 2857 mtx_lock(&lun->lun_lock); 2858 2859 delay_info->status = CTL_DELAY_STATUS_OK; 2860 2861 switch (delay_info->delay_type) { 2862 case CTL_DELAY_TYPE_CONT: 2863 break; 2864 case CTL_DELAY_TYPE_ONESHOT: 2865 break; 2866 default: 2867 delay_info->status = 2868 CTL_DELAY_STATUS_INVALID_TYPE; 2869 break; 2870 } 2871 2872 switch (delay_info->delay_loc) { 2873 case CTL_DELAY_LOC_DATAMOVE: 2874 lun->delay_info.datamove_type = 2875 delay_info->delay_type; 2876 lun->delay_info.datamove_delay = 2877 delay_info->delay_secs; 2878 break; 2879 case CTL_DELAY_LOC_DONE: 2880 lun->delay_info.done_type = 2881 delay_info->delay_type; 2882 lun->delay_info.done_delay = 2883 delay_info->delay_secs; 2884 break; 2885 default: 2886 delay_info->status = 2887 CTL_DELAY_STATUS_INVALID_LOC; 2888 break; 2889 } 2890 mtx_unlock(&lun->lun_lock); 2891 } 2892 2893 mtx_unlock(&softc->ctl_lock); 2894#else 2895 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2896#endif /* CTL_IO_DELAY */ 2897 break; 2898 } 2899 case CTL_REALSYNC_SET: { 2900 int *syncstate; 2901 2902 syncstate = (int *)addr; 2903 2904 mtx_lock(&softc->ctl_lock); 2905 switch (*syncstate) { 2906 case 0: 2907 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2908 break; 2909 case 1: 2910 softc->flags |= CTL_FLAG_REAL_SYNC; 2911 break; 2912 default: 2913 retval = EINVAL; 2914 break; 2915 } 2916 mtx_unlock(&softc->ctl_lock); 2917 break; 2918 } 2919 case CTL_REALSYNC_GET: { 2920 int *syncstate; 2921 2922 syncstate = (int*)addr; 2923 2924 mtx_lock(&softc->ctl_lock); 2925 if (softc->flags & CTL_FLAG_REAL_SYNC) 2926 *syncstate = 1; 2927 else 2928 *syncstate = 0; 2929 mtx_unlock(&softc->ctl_lock); 2930 2931 break; 2932 } 2933 case CTL_SETSYNC: 2934 case CTL_GETSYNC: { 2935 struct ctl_sync_info *sync_info; 2936 struct ctl_lun *lun; 2937 2938 sync_info = (struct ctl_sync_info *)addr; 2939 2940 mtx_lock(&softc->ctl_lock); 2941 lun = softc->ctl_luns[sync_info->lun_id]; 2942 if (lun == NULL) { 2943 mtx_unlock(&softc->ctl_lock); 2944 sync_info->status = CTL_GS_SYNC_NO_LUN; 2945 } 2946 /* 2947 * Get or set the sync interval. We're not bounds checking 2948 * in the set case, hopefully the user won't do something 2949 * silly. 2950 */ 2951 mtx_lock(&lun->lun_lock); 2952 mtx_unlock(&softc->ctl_lock); 2953 if (cmd == CTL_GETSYNC) 2954 sync_info->sync_interval = lun->sync_interval; 2955 else 2956 lun->sync_interval = sync_info->sync_interval; 2957 mtx_unlock(&lun->lun_lock); 2958 2959 sync_info->status = CTL_GS_SYNC_OK; 2960 2961 break; 2962 } 2963 case CTL_GETSTATS: { 2964 struct ctl_stats *stats; 2965 struct ctl_lun *lun; 2966 int i; 2967 2968 stats = (struct ctl_stats *)addr; 2969 2970 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2971 stats->alloc_len) { 2972 stats->status = CTL_SS_NEED_MORE_SPACE; 2973 stats->num_luns = softc->num_luns; 2974 break; 2975 } 2976 /* 2977 * XXX KDM no locking here. If the LUN list changes, 2978 * things can blow up. 2979 */ 2980 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2981 i++, lun = STAILQ_NEXT(lun, links)) { 2982 retval = copyout(&lun->stats, &stats->lun_stats[i], 2983 sizeof(lun->stats)); 2984 if (retval != 0) 2985 break; 2986 } 2987 stats->num_luns = softc->num_luns; 2988 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2989 softc->num_luns; 2990 stats->status = CTL_SS_OK; 2991#ifdef CTL_TIME_IO 2992 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2993#else 2994 stats->flags = CTL_STATS_FLAG_NONE; 2995#endif 2996 getnanouptime(&stats->timestamp); 2997 break; 2998 } 2999 case CTL_ERROR_INJECT: { 3000 struct ctl_error_desc *err_desc, *new_err_desc; 3001 struct ctl_lun *lun; 3002 3003 err_desc = (struct ctl_error_desc *)addr; 3004 3005 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 3006 M_WAITOK | M_ZERO); 3007 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 3008 3009 mtx_lock(&softc->ctl_lock); 3010 lun = softc->ctl_luns[err_desc->lun_id]; 3011 if (lun == NULL) { 3012 mtx_unlock(&softc->ctl_lock); 3013 free(new_err_desc, M_CTL); 3014 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 3015 __func__, (uintmax_t)err_desc->lun_id); 3016 retval = EINVAL; 3017 break; 3018 } 3019 mtx_lock(&lun->lun_lock); 3020 mtx_unlock(&softc->ctl_lock); 3021 3022 /* 3023 * We could do some checking here to verify the validity 3024 * of the request, but given the complexity of error 3025 * injection requests, the checking logic would be fairly 3026 * complex. 3027 * 3028 * For now, if the request is invalid, it just won't get 3029 * executed and might get deleted. 3030 */ 3031 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 3032 3033 /* 3034 * XXX KDM check to make sure the serial number is unique, 3035 * in case we somehow manage to wrap. That shouldn't 3036 * happen for a very long time, but it's the right thing to 3037 * do. 3038 */ 3039 new_err_desc->serial = lun->error_serial; 3040 err_desc->serial = lun->error_serial; 3041 lun->error_serial++; 3042 3043 mtx_unlock(&lun->lun_lock); 3044 break; 3045 } 3046 case CTL_ERROR_INJECT_DELETE: { 3047 struct ctl_error_desc *delete_desc, *desc, *desc2; 3048 struct ctl_lun *lun; 3049 int delete_done; 3050 3051 delete_desc = (struct ctl_error_desc *)addr; 3052 delete_done = 0; 3053 3054 mtx_lock(&softc->ctl_lock); 3055 lun = softc->ctl_luns[delete_desc->lun_id]; 3056 if (lun == NULL) { 3057 mtx_unlock(&softc->ctl_lock); 3058 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 3059 __func__, (uintmax_t)delete_desc->lun_id); 3060 retval = EINVAL; 3061 break; 3062 } 3063 mtx_lock(&lun->lun_lock); 3064 mtx_unlock(&softc->ctl_lock); 3065 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 3066 if (desc->serial != delete_desc->serial) 3067 continue; 3068 3069 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 3070 links); 3071 free(desc, M_CTL); 3072 delete_done = 1; 3073 } 3074 mtx_unlock(&lun->lun_lock); 3075 if (delete_done == 0) { 3076 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 3077 "error serial %ju on LUN %u\n", __func__, 3078 delete_desc->serial, delete_desc->lun_id); 3079 retval = EINVAL; 3080 break; 3081 } 3082 break; 3083 } 3084 case CTL_DUMP_STRUCTS: { 3085 int i, j, k, idx; 3086 struct ctl_port *port; 3087 struct ctl_frontend *fe; 3088 3089 mtx_lock(&softc->ctl_lock); 3090 printf("CTL Persistent Reservation information start:\n"); 3091 for (i = 0; i < CTL_MAX_LUNS; i++) { 3092 struct ctl_lun *lun; 3093 3094 lun = softc->ctl_luns[i]; 3095 3096 if ((lun == NULL) 3097 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3098 continue; 3099 3100 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3101 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3102 idx = j * CTL_MAX_INIT_PER_PORT + k; 3103 if (lun->pr_keys[idx] == 0) 3104 continue; 3105 printf(" LUN %d port %d iid %d key " 3106 "%#jx\n", i, j, k, 3107 (uintmax_t)lun->pr_keys[idx]); 3108 } 3109 } 3110 } 3111 printf("CTL Persistent Reservation information end\n"); 3112 printf("CTL Ports:\n"); 3113 STAILQ_FOREACH(port, &softc->port_list, links) { 3114 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3115 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3116 port->frontend->name, port->port_type, 3117 port->physical_port, port->virtual_port, 3118 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3119 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3120 if (port->wwpn_iid[j].in_use == 0 && 3121 port->wwpn_iid[j].wwpn == 0 && 3122 port->wwpn_iid[j].name == NULL) 3123 continue; 3124 3125 printf(" iid %u use %d WWPN %#jx '%s'\n", 3126 j, port->wwpn_iid[j].in_use, 3127 (uintmax_t)port->wwpn_iid[j].wwpn, 3128 port->wwpn_iid[j].name); 3129 } 3130 } 3131 printf("CTL Port information end\n"); 3132 mtx_unlock(&softc->ctl_lock); 3133 /* 3134 * XXX KDM calling this without a lock. We'd likely want 3135 * to drop the lock before calling the frontend's dump 3136 * routine anyway. 3137 */ 3138 printf("CTL Frontends:\n"); 3139 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3140 printf(" Frontend '%s'\n", fe->name); 3141 if (fe->fe_dump != NULL) 3142 fe->fe_dump(); 3143 } 3144 printf("CTL Frontend information end\n"); 3145 break; 3146 } 3147 case CTL_LUN_REQ: { 3148 struct ctl_lun_req *lun_req; 3149 struct ctl_backend_driver *backend; 3150 3151 lun_req = (struct ctl_lun_req *)addr; 3152 3153 backend = ctl_backend_find(lun_req->backend); 3154 if (backend == NULL) { 3155 lun_req->status = CTL_LUN_ERROR; 3156 snprintf(lun_req->error_str, 3157 sizeof(lun_req->error_str), 3158 "Backend \"%s\" not found.", 3159 lun_req->backend); 3160 break; 3161 } 3162 if (lun_req->num_be_args > 0) { 3163 lun_req->kern_be_args = ctl_copyin_args( 3164 lun_req->num_be_args, 3165 lun_req->be_args, 3166 lun_req->error_str, 3167 sizeof(lun_req->error_str)); 3168 if (lun_req->kern_be_args == NULL) { 3169 lun_req->status = CTL_LUN_ERROR; 3170 break; 3171 } 3172 } 3173 3174 retval = backend->ioctl(dev, cmd, addr, flag, td); 3175 3176 if (lun_req->num_be_args > 0) { 3177 ctl_copyout_args(lun_req->num_be_args, 3178 lun_req->kern_be_args); 3179 ctl_free_args(lun_req->num_be_args, 3180 lun_req->kern_be_args); 3181 } 3182 break; 3183 } 3184 case CTL_LUN_LIST: { 3185 struct sbuf *sb; 3186 struct ctl_lun *lun; 3187 struct ctl_lun_list *list; 3188 struct ctl_option *opt; 3189 3190 list = (struct ctl_lun_list *)addr; 3191 3192 /* 3193 * Allocate a fixed length sbuf here, based on the length 3194 * of the user's buffer. We could allocate an auto-extending 3195 * buffer, and then tell the user how much larger our 3196 * amount of data is than his buffer, but that presents 3197 * some problems: 3198 * 3199 * 1. The sbuf(9) routines use a blocking malloc, and so 3200 * we can't hold a lock while calling them with an 3201 * auto-extending buffer. 3202 * 3203 * 2. There is not currently a LUN reference counting 3204 * mechanism, outside of outstanding transactions on 3205 * the LUN's OOA queue. So a LUN could go away on us 3206 * while we're getting the LUN number, backend-specific 3207 * information, etc. Thus, given the way things 3208 * currently work, we need to hold the CTL lock while 3209 * grabbing LUN information. 3210 * 3211 * So, from the user's standpoint, the best thing to do is 3212 * allocate what he thinks is a reasonable buffer length, 3213 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3214 * double the buffer length and try again. (And repeat 3215 * that until he succeeds.) 3216 */ 3217 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3218 if (sb == NULL) { 3219 list->status = CTL_LUN_LIST_ERROR; 3220 snprintf(list->error_str, sizeof(list->error_str), 3221 "Unable to allocate %d bytes for LUN list", 3222 list->alloc_len); 3223 break; 3224 } 3225 3226 sbuf_printf(sb, "<ctllunlist>\n"); 3227 3228 mtx_lock(&softc->ctl_lock); 3229 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3230 mtx_lock(&lun->lun_lock); 3231 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3232 (uintmax_t)lun->lun); 3233 3234 /* 3235 * Bail out as soon as we see that we've overfilled 3236 * the buffer. 3237 */ 3238 if (retval != 0) 3239 break; 3240 3241 retval = sbuf_printf(sb, "\t<backend_type>%s" 3242 "</backend_type>\n", 3243 (lun->backend == NULL) ? "none" : 3244 lun->backend->name); 3245 3246 if (retval != 0) 3247 break; 3248 3249 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3250 lun->be_lun->lun_type); 3251 3252 if (retval != 0) 3253 break; 3254 3255 if (lun->backend == NULL) { 3256 retval = sbuf_printf(sb, "</lun>\n"); 3257 if (retval != 0) 3258 break; 3259 continue; 3260 } 3261 3262 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3263 (lun->be_lun->maxlba > 0) ? 3264 lun->be_lun->maxlba + 1 : 0); 3265 3266 if (retval != 0) 3267 break; 3268 3269 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3270 lun->be_lun->blocksize); 3271 3272 if (retval != 0) 3273 break; 3274 3275 retval = sbuf_printf(sb, "\t<serial_number>"); 3276 3277 if (retval != 0) 3278 break; 3279 3280 retval = ctl_sbuf_printf_esc(sb, 3281 lun->be_lun->serial_num, 3282 sizeof(lun->be_lun->serial_num)); 3283 3284 if (retval != 0) 3285 break; 3286 3287 retval = sbuf_printf(sb, "</serial_number>\n"); 3288 3289 if (retval != 0) 3290 break; 3291 3292 retval = sbuf_printf(sb, "\t<device_id>"); 3293 3294 if (retval != 0) 3295 break; 3296 3297 retval = ctl_sbuf_printf_esc(sb, 3298 lun->be_lun->device_id, 3299 sizeof(lun->be_lun->device_id)); 3300 3301 if (retval != 0) 3302 break; 3303 3304 retval = sbuf_printf(sb, "</device_id>\n"); 3305 3306 if (retval != 0) 3307 break; 3308 3309 if (lun->backend->lun_info != NULL) { 3310 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3311 if (retval != 0) 3312 break; 3313 } 3314 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3315 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3316 opt->name, opt->value, opt->name); 3317 if (retval != 0) 3318 break; 3319 } 3320 3321 retval = sbuf_printf(sb, "</lun>\n"); 3322 3323 if (retval != 0) 3324 break; 3325 mtx_unlock(&lun->lun_lock); 3326 } 3327 if (lun != NULL) 3328 mtx_unlock(&lun->lun_lock); 3329 mtx_unlock(&softc->ctl_lock); 3330 3331 if ((retval != 0) 3332 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3333 retval = 0; 3334 sbuf_delete(sb); 3335 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3336 snprintf(list->error_str, sizeof(list->error_str), 3337 "Out of space, %d bytes is too small", 3338 list->alloc_len); 3339 break; 3340 } 3341 3342 sbuf_finish(sb); 3343 3344 retval = copyout(sbuf_data(sb), list->lun_xml, 3345 sbuf_len(sb) + 1); 3346 3347 list->fill_len = sbuf_len(sb) + 1; 3348 list->status = CTL_LUN_LIST_OK; 3349 sbuf_delete(sb); 3350 break; 3351 } 3352 case CTL_ISCSI: { 3353 struct ctl_iscsi *ci; 3354 struct ctl_frontend *fe; 3355 3356 ci = (struct ctl_iscsi *)addr; 3357 3358 fe = ctl_frontend_find("iscsi"); 3359 if (fe == NULL) { 3360 ci->status = CTL_ISCSI_ERROR; 3361 snprintf(ci->error_str, sizeof(ci->error_str), 3362 "Frontend \"iscsi\" not found."); 3363 break; 3364 } 3365 3366 retval = fe->ioctl(dev, cmd, addr, flag, td); 3367 break; 3368 } 3369 case CTL_PORT_REQ: { 3370 struct ctl_req *req; 3371 struct ctl_frontend *fe; 3372 3373 req = (struct ctl_req *)addr; 3374 3375 fe = ctl_frontend_find(req->driver); 3376 if (fe == NULL) { 3377 req->status = CTL_LUN_ERROR; 3378 snprintf(req->error_str, sizeof(req->error_str), 3379 "Frontend \"%s\" not found.", req->driver); 3380 break; 3381 } 3382 if (req->num_args > 0) { 3383 req->kern_args = ctl_copyin_args(req->num_args, 3384 req->args, req->error_str, sizeof(req->error_str)); 3385 if (req->kern_args == NULL) { 3386 req->status = CTL_LUN_ERROR; 3387 break; 3388 } 3389 } 3390 3391 retval = fe->ioctl(dev, cmd, addr, flag, td); 3392 3393 if (req->num_args > 0) { 3394 ctl_copyout_args(req->num_args, req->kern_args); 3395 ctl_free_args(req->num_args, req->kern_args); 3396 } 3397 break; 3398 } 3399 case CTL_PORT_LIST: { 3400 struct sbuf *sb; 3401 struct ctl_port *port; 3402 struct ctl_lun_list *list; 3403 struct ctl_option *opt; 3404 int j; 3405 3406 list = (struct ctl_lun_list *)addr; 3407 3408 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3409 if (sb == NULL) { 3410 list->status = CTL_LUN_LIST_ERROR; 3411 snprintf(list->error_str, sizeof(list->error_str), 3412 "Unable to allocate %d bytes for LUN list", 3413 list->alloc_len); 3414 break; 3415 } 3416 3417 sbuf_printf(sb, "<ctlportlist>\n"); 3418 3419 mtx_lock(&softc->ctl_lock); 3420 STAILQ_FOREACH(port, &softc->port_list, links) { 3421 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3422 (uintmax_t)port->targ_port); 3423 3424 /* 3425 * Bail out as soon as we see that we've overfilled 3426 * the buffer. 3427 */ 3428 if (retval != 0) 3429 break; 3430 3431 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3432 "</frontend_type>\n", port->frontend->name); 3433 if (retval != 0) 3434 break; 3435 3436 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3437 port->port_type); 3438 if (retval != 0) 3439 break; 3440 3441 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3442 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3443 if (retval != 0) 3444 break; 3445 3446 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3447 port->port_name); 3448 if (retval != 0) 3449 break; 3450 3451 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3452 port->physical_port); 3453 if (retval != 0) 3454 break; 3455 3456 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3457 port->virtual_port); 3458 if (retval != 0) 3459 break; 3460 3461 if (port->target_devid != NULL) { 3462 sbuf_printf(sb, "\t<target>"); 3463 ctl_id_sbuf(port->target_devid, sb); 3464 sbuf_printf(sb, "</target>\n"); 3465 } 3466 3467 if (port->port_devid != NULL) { 3468 sbuf_printf(sb, "\t<port>"); 3469 ctl_id_sbuf(port->port_devid, sb); 3470 sbuf_printf(sb, "</port>\n"); 3471 } 3472 3473 if (port->port_info != NULL) { 3474 retval = port->port_info(port->onoff_arg, sb); 3475 if (retval != 0) 3476 break; 3477 } 3478 STAILQ_FOREACH(opt, &port->options, links) { 3479 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3480 opt->name, opt->value, opt->name); 3481 if (retval != 0) 3482 break; 3483 } 3484 3485 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3486 if (port->wwpn_iid[j].in_use == 0 || 3487 (port->wwpn_iid[j].wwpn == 0 && 3488 port->wwpn_iid[j].name == NULL)) 3489 continue; 3490 3491 if (port->wwpn_iid[j].name != NULL) 3492 retval = sbuf_printf(sb, 3493 "\t<initiator>%u %s</initiator>\n", 3494 j, port->wwpn_iid[j].name); 3495 else 3496 retval = sbuf_printf(sb, 3497 "\t<initiator>%u naa.%08jx</initiator>\n", 3498 j, port->wwpn_iid[j].wwpn); 3499 if (retval != 0) 3500 break; 3501 } 3502 if (retval != 0) 3503 break; 3504 3505 retval = sbuf_printf(sb, "</targ_port>\n"); 3506 if (retval != 0) 3507 break; 3508 } 3509 mtx_unlock(&softc->ctl_lock); 3510 3511 if ((retval != 0) 3512 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3513 retval = 0; 3514 sbuf_delete(sb); 3515 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3516 snprintf(list->error_str, sizeof(list->error_str), 3517 "Out of space, %d bytes is too small", 3518 list->alloc_len); 3519 break; 3520 } 3521 3522 sbuf_finish(sb); 3523 3524 retval = copyout(sbuf_data(sb), list->lun_xml, 3525 sbuf_len(sb) + 1); 3526 3527 list->fill_len = sbuf_len(sb) + 1; 3528 list->status = CTL_LUN_LIST_OK; 3529 sbuf_delete(sb); 3530 break; 3531 } 3532 default: { 3533 /* XXX KDM should we fix this? */ 3534#if 0 3535 struct ctl_backend_driver *backend; 3536 unsigned int type; 3537 int found; 3538 3539 found = 0; 3540 3541 /* 3542 * We encode the backend type as the ioctl type for backend 3543 * ioctls. So parse it out here, and then search for a 3544 * backend of this type. 3545 */ 3546 type = _IOC_TYPE(cmd); 3547 3548 STAILQ_FOREACH(backend, &softc->be_list, links) { 3549 if (backend->type == type) { 3550 found = 1; 3551 break; 3552 } 3553 } 3554 if (found == 0) { 3555 printf("ctl: unknown ioctl command %#lx or backend " 3556 "%d\n", cmd, type); 3557 retval = EINVAL; 3558 break; 3559 } 3560 retval = backend->ioctl(dev, cmd, addr, flag, td); 3561#endif 3562 retval = ENOTTY; 3563 break; 3564 } 3565 } 3566 return (retval); 3567} 3568 3569uint32_t 3570ctl_get_initindex(struct ctl_nexus *nexus) 3571{ 3572 if (nexus->targ_port < CTL_MAX_PORTS) 3573 return (nexus->initid.id + 3574 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3575 else 3576 return (nexus->initid.id + 3577 ((nexus->targ_port - CTL_MAX_PORTS) * 3578 CTL_MAX_INIT_PER_PORT)); 3579} 3580 3581uint32_t 3582ctl_get_resindex(struct ctl_nexus *nexus) 3583{ 3584 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3585} 3586 3587uint32_t 3588ctl_port_idx(int port_num) 3589{ 3590 if (port_num < CTL_MAX_PORTS) 3591 return(port_num); 3592 else 3593 return(port_num - CTL_MAX_PORTS); 3594} 3595 3596static uint32_t 3597ctl_map_lun(int port_num, uint32_t lun_id) 3598{ 3599 struct ctl_port *port; 3600 3601 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3602 if (port == NULL) 3603 return (UINT32_MAX); 3604 if (port->lun_map == NULL) 3605 return (lun_id); 3606 return (port->lun_map(port->targ_lun_arg, lun_id)); 3607} 3608 3609static uint32_t 3610ctl_map_lun_back(int port_num, uint32_t lun_id) 3611{ 3612 struct ctl_port *port; 3613 uint32_t i; 3614 3615 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3616 if (port->lun_map == NULL) 3617 return (lun_id); 3618 for (i = 0; i < CTL_MAX_LUNS; i++) { 3619 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3620 return (i); 3621 } 3622 return (UINT32_MAX); 3623} 3624 3625/* 3626 * Note: This only works for bitmask sizes that are at least 32 bits, and 3627 * that are a power of 2. 3628 */ 3629int 3630ctl_ffz(uint32_t *mask, uint32_t size) 3631{ 3632 uint32_t num_chunks, num_pieces; 3633 int i, j; 3634 3635 num_chunks = (size >> 5); 3636 if (num_chunks == 0) 3637 num_chunks++; 3638 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3639 3640 for (i = 0; i < num_chunks; i++) { 3641 for (j = 0; j < num_pieces; j++) { 3642 if ((mask[i] & (1 << j)) == 0) 3643 return ((i << 5) + j); 3644 } 3645 } 3646 3647 return (-1); 3648} 3649 3650int 3651ctl_set_mask(uint32_t *mask, uint32_t bit) 3652{ 3653 uint32_t chunk, piece; 3654 3655 chunk = bit >> 5; 3656 piece = bit % (sizeof(uint32_t) * 8); 3657 3658 if ((mask[chunk] & (1 << piece)) != 0) 3659 return (-1); 3660 else 3661 mask[chunk] |= (1 << piece); 3662 3663 return (0); 3664} 3665 3666int 3667ctl_clear_mask(uint32_t *mask, uint32_t bit) 3668{ 3669 uint32_t chunk, piece; 3670 3671 chunk = bit >> 5; 3672 piece = bit % (sizeof(uint32_t) * 8); 3673 3674 if ((mask[chunk] & (1 << piece)) == 0) 3675 return (-1); 3676 else 3677 mask[chunk] &= ~(1 << piece); 3678 3679 return (0); 3680} 3681 3682int 3683ctl_is_set(uint32_t *mask, uint32_t bit) 3684{ 3685 uint32_t chunk, piece; 3686 3687 chunk = bit >> 5; 3688 piece = bit % (sizeof(uint32_t) * 8); 3689 3690 if ((mask[chunk] & (1 << piece)) == 0) 3691 return (0); 3692 else 3693 return (1); 3694} 3695 3696#ifdef unused 3697/* 3698 * The bus, target and lun are optional, they can be filled in later. 3699 * can_wait is used to determine whether we can wait on the malloc or not. 3700 */ 3701union ctl_io* 3702ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3703 uint32_t targ_lun, int can_wait) 3704{ 3705 union ctl_io *io; 3706 3707 if (can_wait) 3708 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3709 else 3710 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3711 3712 if (io != NULL) { 3713 io->io_hdr.io_type = io_type; 3714 io->io_hdr.targ_port = targ_port; 3715 /* 3716 * XXX KDM this needs to change/go away. We need to move 3717 * to a preallocated pool of ctl_scsiio structures. 3718 */ 3719 io->io_hdr.nexus.targ_target.id = targ_target; 3720 io->io_hdr.nexus.targ_lun = targ_lun; 3721 } 3722 3723 return (io); 3724} 3725 3726void 3727ctl_kfree_io(union ctl_io *io) 3728{ 3729 free(io, M_CTL); 3730} 3731#endif /* unused */ 3732 3733/* 3734 * ctl_softc, pool_type, total_ctl_io are passed in. 3735 * npool is passed out. 3736 */ 3737int 3738ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3739 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3740{ 3741 uint32_t i; 3742 union ctl_io *cur_io, *next_io; 3743 struct ctl_io_pool *pool; 3744 int retval; 3745 3746 retval = 0; 3747 3748 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3749 M_NOWAIT | M_ZERO); 3750 if (pool == NULL) { 3751 retval = ENOMEM; 3752 goto bailout; 3753 } 3754 3755 pool->type = pool_type; 3756 pool->ctl_softc = ctl_softc; 3757 3758 mtx_lock(&ctl_softc->pool_lock); 3759 pool->id = ctl_softc->cur_pool_id++; 3760 mtx_unlock(&ctl_softc->pool_lock); 3761 3762 pool->flags = CTL_POOL_FLAG_NONE; 3763 pool->refcount = 1; /* Reference for validity. */ 3764 STAILQ_INIT(&pool->free_queue); 3765 3766 /* 3767 * XXX KDM other options here: 3768 * - allocate a page at a time 3769 * - allocate one big chunk of memory. 3770 * Page allocation might work well, but would take a little more 3771 * tracking. 3772 */ 3773 for (i = 0; i < total_ctl_io; i++) { 3774 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3775 M_NOWAIT); 3776 if (cur_io == NULL) { 3777 retval = ENOMEM; 3778 break; 3779 } 3780 cur_io->io_hdr.pool = pool; 3781 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3782 pool->total_ctl_io++; 3783 pool->free_ctl_io++; 3784 } 3785 3786 if (retval != 0) { 3787 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3788 cur_io != NULL; cur_io = next_io) { 3789 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3790 links); 3791 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3792 ctl_io_hdr, links); 3793 free(cur_io, M_CTLIO); 3794 } 3795 3796 free(pool, M_CTL); 3797 goto bailout; 3798 } 3799 mtx_lock(&ctl_softc->pool_lock); 3800 ctl_softc->num_pools++; 3801 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3802 /* 3803 * Increment our usage count if this is an external consumer, so we 3804 * can't get unloaded until the external consumer (most likely a 3805 * FETD) unloads and frees his pool. 3806 * 3807 * XXX KDM will this increment the caller's module use count, or 3808 * mine? 3809 */ 3810#if 0 3811 if ((pool_type != CTL_POOL_EMERGENCY) 3812 && (pool_type != CTL_POOL_INTERNAL) 3813 && (pool_type != CTL_POOL_4OTHERSC)) 3814 MOD_INC_USE_COUNT; 3815#endif 3816 3817 mtx_unlock(&ctl_softc->pool_lock); 3818 3819 *npool = pool; 3820 3821bailout: 3822 3823 return (retval); 3824} 3825 3826static int 3827ctl_pool_acquire(struct ctl_io_pool *pool) 3828{ 3829 3830 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3831 3832 if (pool->flags & CTL_POOL_FLAG_INVALID) 3833 return (EINVAL); 3834 3835 pool->refcount++; 3836 3837 return (0); 3838} 3839 3840static void 3841ctl_pool_release(struct ctl_io_pool *pool) 3842{ 3843 struct ctl_softc *ctl_softc = pool->ctl_softc; 3844 union ctl_io *io; 3845 3846 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3847 3848 if (--pool->refcount != 0) 3849 return; 3850 3851 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3852 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3853 links); 3854 free(io, M_CTLIO); 3855 } 3856 3857 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3858 ctl_softc->num_pools--; 3859 3860 /* 3861 * XXX KDM will this decrement the caller's usage count or mine? 3862 */ 3863#if 0 3864 if ((pool->type != CTL_POOL_EMERGENCY) 3865 && (pool->type != CTL_POOL_INTERNAL) 3866 && (pool->type != CTL_POOL_4OTHERSC)) 3867 MOD_DEC_USE_COUNT; 3868#endif 3869 3870 free(pool, M_CTL); 3871} 3872 3873void 3874ctl_pool_free(struct ctl_io_pool *pool) 3875{ 3876 struct ctl_softc *ctl_softc; 3877 3878 if (pool == NULL) 3879 return; 3880 3881 ctl_softc = pool->ctl_softc; 3882 mtx_lock(&ctl_softc->pool_lock); 3883 pool->flags |= CTL_POOL_FLAG_INVALID; 3884 ctl_pool_release(pool); 3885 mtx_unlock(&ctl_softc->pool_lock); 3886} 3887 3888/* 3889 * This routine does not block (except for spinlocks of course). 3890 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3891 * possible. 3892 */ 3893union ctl_io * 3894ctl_alloc_io(void *pool_ref) 3895{ 3896 union ctl_io *io; 3897 struct ctl_softc *ctl_softc; 3898 struct ctl_io_pool *pool, *npool; 3899 struct ctl_io_pool *emergency_pool; 3900 3901 pool = (struct ctl_io_pool *)pool_ref; 3902 3903 if (pool == NULL) { 3904 printf("%s: pool is NULL\n", __func__); 3905 return (NULL); 3906 } 3907 3908 emergency_pool = NULL; 3909 3910 ctl_softc = pool->ctl_softc; 3911 3912 mtx_lock(&ctl_softc->pool_lock); 3913 /* 3914 * First, try to get the io structure from the user's pool. 3915 */ 3916 if (ctl_pool_acquire(pool) == 0) { 3917 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3918 if (io != NULL) { 3919 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3920 pool->total_allocated++; 3921 pool->free_ctl_io--; 3922 mtx_unlock(&ctl_softc->pool_lock); 3923 return (io); 3924 } else 3925 ctl_pool_release(pool); 3926 } 3927 /* 3928 * If he doesn't have any io structures left, search for an 3929 * emergency pool and grab one from there. 3930 */ 3931 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3932 if (npool->type != CTL_POOL_EMERGENCY) 3933 continue; 3934 3935 if (ctl_pool_acquire(npool) != 0) 3936 continue; 3937 3938 emergency_pool = npool; 3939 3940 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3941 if (io != NULL) { 3942 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3943 npool->total_allocated++; 3944 npool->free_ctl_io--; 3945 mtx_unlock(&ctl_softc->pool_lock); 3946 return (io); 3947 } else 3948 ctl_pool_release(npool); 3949 } 3950 3951 /* Drop the spinlock before we malloc */ 3952 mtx_unlock(&ctl_softc->pool_lock); 3953 3954 /* 3955 * The emergency pool (if it exists) didn't have one, so try an 3956 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3957 */ 3958 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3959 if (io != NULL) { 3960 /* 3961 * If the emergency pool exists but is empty, add this 3962 * ctl_io to its list when it gets freed. 3963 */ 3964 if (emergency_pool != NULL) { 3965 mtx_lock(&ctl_softc->pool_lock); 3966 if (ctl_pool_acquire(emergency_pool) == 0) { 3967 io->io_hdr.pool = emergency_pool; 3968 emergency_pool->total_ctl_io++; 3969 /* 3970 * Need to bump this, otherwise 3971 * total_allocated and total_freed won't 3972 * match when we no longer have anything 3973 * outstanding. 3974 */ 3975 emergency_pool->total_allocated++; 3976 } 3977 mtx_unlock(&ctl_softc->pool_lock); 3978 } else 3979 io->io_hdr.pool = NULL; 3980 } 3981 3982 return (io); 3983} 3984 3985void 3986ctl_free_io(union ctl_io *io) 3987{ 3988 if (io == NULL) 3989 return; 3990 3991 /* 3992 * If this ctl_io has a pool, return it to that pool. 3993 */ 3994 if (io->io_hdr.pool != NULL) { 3995 struct ctl_io_pool *pool; 3996 3997 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3998 mtx_lock(&pool->ctl_softc->pool_lock); 3999 io->io_hdr.io_type = 0xff; 4000 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 4001 pool->total_freed++; 4002 pool->free_ctl_io++; 4003 ctl_pool_release(pool); 4004 mtx_unlock(&pool->ctl_softc->pool_lock); 4005 } else { 4006 /* 4007 * Otherwise, just free it. We probably malloced it and 4008 * the emergency pool wasn't available. 4009 */ 4010 free(io, M_CTLIO); 4011 } 4012 4013} 4014 4015void 4016ctl_zero_io(union ctl_io *io) 4017{ 4018 void *pool_ref; 4019 4020 if (io == NULL) 4021 return; 4022 4023 /* 4024 * May need to preserve linked list pointers at some point too. 4025 */ 4026 pool_ref = io->io_hdr.pool; 4027 4028 memset(io, 0, sizeof(*io)); 4029 4030 io->io_hdr.pool = pool_ref; 4031} 4032 4033/* 4034 * This routine is currently used for internal copies of ctl_ios that need 4035 * to persist for some reason after we've already returned status to the 4036 * FETD. (Thus the flag set.) 4037 * 4038 * XXX XXX 4039 * Note that this makes a blind copy of all fields in the ctl_io, except 4040 * for the pool reference. This includes any memory that has been 4041 * allocated! That memory will no longer be valid after done has been 4042 * called, so this would be VERY DANGEROUS for command that actually does 4043 * any reads or writes. Right now (11/7/2005), this is only used for immediate 4044 * start and stop commands, which don't transfer any data, so this is not a 4045 * problem. If it is used for anything else, the caller would also need to 4046 * allocate data buffer space and this routine would need to be modified to 4047 * copy the data buffer(s) as well. 4048 */ 4049void 4050ctl_copy_io(union ctl_io *src, union ctl_io *dest) 4051{ 4052 void *pool_ref; 4053 4054 if ((src == NULL) 4055 || (dest == NULL)) 4056 return; 4057 4058 /* 4059 * May need to preserve linked list pointers at some point too. 4060 */ 4061 pool_ref = dest->io_hdr.pool; 4062 4063 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 4064 4065 dest->io_hdr.pool = pool_ref; 4066 /* 4067 * We need to know that this is an internal copy, and doesn't need 4068 * to get passed back to the FETD that allocated it. 4069 */ 4070 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 4071} 4072 4073static int 4074ctl_expand_number(const char *buf, uint64_t *num) 4075{ 4076 char *endptr; 4077 uint64_t number; 4078 unsigned shift; 4079 4080 number = strtoq(buf, &endptr, 0); 4081 4082 switch (tolower((unsigned char)*endptr)) { 4083 case 'e': 4084 shift = 60; 4085 break; 4086 case 'p': 4087 shift = 50; 4088 break; 4089 case 't': 4090 shift = 40; 4091 break; 4092 case 'g': 4093 shift = 30; 4094 break; 4095 case 'm': 4096 shift = 20; 4097 break; 4098 case 'k': 4099 shift = 10; 4100 break; 4101 case 'b': 4102 case '\0': /* No unit. */ 4103 *num = number; 4104 return (0); 4105 default: 4106 /* Unrecognized unit. */ 4107 return (-1); 4108 } 4109 4110 if ((number << shift) >> shift != number) { 4111 /* Overflow */ 4112 return (-1); 4113 } 4114 *num = number << shift; 4115 return (0); 4116} 4117 4118 4119/* 4120 * This routine could be used in the future to load default and/or saved 4121 * mode page parameters for a particuar lun. 4122 */ 4123static int 4124ctl_init_page_index(struct ctl_lun *lun) 4125{ 4126 int i; 4127 struct ctl_page_index *page_index; 4128 const char *value; 4129 uint64_t ival; 4130 4131 memcpy(&lun->mode_pages.index, page_index_template, 4132 sizeof(page_index_template)); 4133 4134 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4135 4136 page_index = &lun->mode_pages.index[i]; 4137 /* 4138 * If this is a disk-only mode page, there's no point in 4139 * setting it up. For some pages, we have to have some 4140 * basic information about the disk in order to calculate the 4141 * mode page data. 4142 */ 4143 if ((lun->be_lun->lun_type != T_DIRECT) 4144 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4145 continue; 4146 4147 switch (page_index->page_code & SMPH_PC_MASK) { 4148 case SMS_RW_ERROR_RECOVERY_PAGE: { 4149 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4150 panic("subpage is incorrect!"); 4151 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4152 &rw_er_page_default, 4153 sizeof(rw_er_page_default)); 4154 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4155 &rw_er_page_changeable, 4156 sizeof(rw_er_page_changeable)); 4157 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4158 &rw_er_page_default, 4159 sizeof(rw_er_page_default)); 4160 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4161 &rw_er_page_default, 4162 sizeof(rw_er_page_default)); 4163 page_index->page_data = 4164 (uint8_t *)lun->mode_pages.rw_er_page; 4165 break; 4166 } 4167 case SMS_FORMAT_DEVICE_PAGE: { 4168 struct scsi_format_page *format_page; 4169 4170 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4171 panic("subpage is incorrect!"); 4172 4173 /* 4174 * Sectors per track are set above. Bytes per 4175 * sector need to be set here on a per-LUN basis. 4176 */ 4177 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4178 &format_page_default, 4179 sizeof(format_page_default)); 4180 memcpy(&lun->mode_pages.format_page[ 4181 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4182 sizeof(format_page_changeable)); 4183 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4184 &format_page_default, 4185 sizeof(format_page_default)); 4186 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4187 &format_page_default, 4188 sizeof(format_page_default)); 4189 4190 format_page = &lun->mode_pages.format_page[ 4191 CTL_PAGE_CURRENT]; 4192 scsi_ulto2b(lun->be_lun->blocksize, 4193 format_page->bytes_per_sector); 4194 4195 format_page = &lun->mode_pages.format_page[ 4196 CTL_PAGE_DEFAULT]; 4197 scsi_ulto2b(lun->be_lun->blocksize, 4198 format_page->bytes_per_sector); 4199 4200 format_page = &lun->mode_pages.format_page[ 4201 CTL_PAGE_SAVED]; 4202 scsi_ulto2b(lun->be_lun->blocksize, 4203 format_page->bytes_per_sector); 4204 4205 page_index->page_data = 4206 (uint8_t *)lun->mode_pages.format_page; 4207 break; 4208 } 4209 case SMS_RIGID_DISK_PAGE: { 4210 struct scsi_rigid_disk_page *rigid_disk_page; 4211 uint32_t sectors_per_cylinder; 4212 uint64_t cylinders; 4213#ifndef __XSCALE__ 4214 int shift; 4215#endif /* !__XSCALE__ */ 4216 4217 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4218 panic("invalid subpage value %d", 4219 page_index->subpage); 4220 4221 /* 4222 * Rotation rate and sectors per track are set 4223 * above. We calculate the cylinders here based on 4224 * capacity. Due to the number of heads and 4225 * sectors per track we're using, smaller arrays 4226 * may turn out to have 0 cylinders. Linux and 4227 * FreeBSD don't pay attention to these mode pages 4228 * to figure out capacity, but Solaris does. It 4229 * seems to deal with 0 cylinders just fine, and 4230 * works out a fake geometry based on the capacity. 4231 */ 4232 memcpy(&lun->mode_pages.rigid_disk_page[ 4233 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4234 sizeof(rigid_disk_page_default)); 4235 memcpy(&lun->mode_pages.rigid_disk_page[ 4236 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4237 sizeof(rigid_disk_page_changeable)); 4238 4239 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4240 CTL_DEFAULT_HEADS; 4241 4242 /* 4243 * The divide method here will be more accurate, 4244 * probably, but results in floating point being 4245 * used in the kernel on i386 (__udivdi3()). On the 4246 * XScale, though, __udivdi3() is implemented in 4247 * software. 4248 * 4249 * The shift method for cylinder calculation is 4250 * accurate if sectors_per_cylinder is a power of 4251 * 2. Otherwise it might be slightly off -- you 4252 * might have a bit of a truncation problem. 4253 */ 4254#ifdef __XSCALE__ 4255 cylinders = (lun->be_lun->maxlba + 1) / 4256 sectors_per_cylinder; 4257#else 4258 for (shift = 31; shift > 0; shift--) { 4259 if (sectors_per_cylinder & (1 << shift)) 4260 break; 4261 } 4262 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4263#endif 4264 4265 /* 4266 * We've basically got 3 bytes, or 24 bits for the 4267 * cylinder size in the mode page. If we're over, 4268 * just round down to 2^24. 4269 */ 4270 if (cylinders > 0xffffff) 4271 cylinders = 0xffffff; 4272 4273 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4274 CTL_PAGE_DEFAULT]; 4275 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4276 4277 if ((value = ctl_get_opt(&lun->be_lun->options, 4278 "rpm")) != NULL) { 4279 scsi_ulto2b(strtol(value, NULL, 0), 4280 rigid_disk_page->rotation_rate); 4281 } 4282 4283 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4284 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4285 sizeof(rigid_disk_page_default)); 4286 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4287 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4288 sizeof(rigid_disk_page_default)); 4289 4290 page_index->page_data = 4291 (uint8_t *)lun->mode_pages.rigid_disk_page; 4292 break; 4293 } 4294 case SMS_CACHING_PAGE: { 4295 struct scsi_caching_page *caching_page; 4296 4297 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4298 panic("invalid subpage value %d", 4299 page_index->subpage); 4300 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4301 &caching_page_default, 4302 sizeof(caching_page_default)); 4303 memcpy(&lun->mode_pages.caching_page[ 4304 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4305 sizeof(caching_page_changeable)); 4306 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4307 &caching_page_default, 4308 sizeof(caching_page_default)); 4309 caching_page = &lun->mode_pages.caching_page[ 4310 CTL_PAGE_SAVED]; 4311 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4312 if (value != NULL && strcmp(value, "off") == 0) 4313 caching_page->flags1 &= ~SCP_WCE; 4314 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4315 if (value != NULL && strcmp(value, "off") == 0) 4316 caching_page->flags1 |= SCP_RCD; 4317 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4318 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4319 sizeof(caching_page_default)); 4320 page_index->page_data = 4321 (uint8_t *)lun->mode_pages.caching_page; 4322 break; 4323 } 4324 case SMS_CONTROL_MODE_PAGE: { 4325 struct scsi_control_page *control_page; 4326 4327 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4328 panic("invalid subpage value %d", 4329 page_index->subpage); 4330 4331 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4332 &control_page_default, 4333 sizeof(control_page_default)); 4334 memcpy(&lun->mode_pages.control_page[ 4335 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4336 sizeof(control_page_changeable)); 4337 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4338 &control_page_default, 4339 sizeof(control_page_default)); 4340 control_page = &lun->mode_pages.control_page[ 4341 CTL_PAGE_SAVED]; 4342 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4343 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4344 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4345 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4346 } 4347 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4348 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4349 sizeof(control_page_default)); 4350 page_index->page_data = 4351 (uint8_t *)lun->mode_pages.control_page; 4352 break; 4353 4354 } 4355 case SMS_INFO_EXCEPTIONS_PAGE: { 4356 switch (page_index->subpage) { 4357 case SMS_SUBPAGE_PAGE_0: 4358 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4359 &ie_page_default, 4360 sizeof(ie_page_default)); 4361 memcpy(&lun->mode_pages.ie_page[ 4362 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4363 sizeof(ie_page_changeable)); 4364 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4365 &ie_page_default, 4366 sizeof(ie_page_default)); 4367 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4368 &ie_page_default, 4369 sizeof(ie_page_default)); 4370 page_index->page_data = 4371 (uint8_t *)lun->mode_pages.ie_page; 4372 break; 4373 case 0x02: { 4374 struct ctl_logical_block_provisioning_page *page; 4375 4376 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4377 &lbp_page_default, 4378 sizeof(lbp_page_default)); 4379 memcpy(&lun->mode_pages.lbp_page[ 4380 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4381 sizeof(lbp_page_changeable)); 4382 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4383 &lbp_page_default, 4384 sizeof(lbp_page_default)); 4385 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4386 value = ctl_get_opt(&lun->be_lun->options, 4387 "avail-threshold"); 4388 if (value != NULL && 4389 ctl_expand_number(value, &ival) == 0) { 4390 page->descr[0].flags |= SLBPPD_ENABLED | 4391 SLBPPD_ARMING_DEC; 4392 if (lun->be_lun->blocksize) 4393 ival /= lun->be_lun->blocksize; 4394 else 4395 ival /= 512; 4396 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4397 page->descr[0].count); 4398 } 4399 value = ctl_get_opt(&lun->be_lun->options, 4400 "used-threshold"); 4401 if (value != NULL && 4402 ctl_expand_number(value, &ival) == 0) { 4403 page->descr[1].flags |= SLBPPD_ENABLED | 4404 SLBPPD_ARMING_INC; 4405 if (lun->be_lun->blocksize) 4406 ival /= lun->be_lun->blocksize; 4407 else 4408 ival /= 512; 4409 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4410 page->descr[1].count); 4411 } 4412 value = ctl_get_opt(&lun->be_lun->options, 4413 "pool-avail-threshold"); 4414 if (value != NULL && 4415 ctl_expand_number(value, &ival) == 0) { 4416 page->descr[2].flags |= SLBPPD_ENABLED | 4417 SLBPPD_ARMING_DEC; 4418 if (lun->be_lun->blocksize) 4419 ival /= lun->be_lun->blocksize; 4420 else 4421 ival /= 512; 4422 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4423 page->descr[2].count); 4424 } 4425 value = ctl_get_opt(&lun->be_lun->options, 4426 "pool-used-threshold"); 4427 if (value != NULL && 4428 ctl_expand_number(value, &ival) == 0) { 4429 page->descr[3].flags |= SLBPPD_ENABLED | 4430 SLBPPD_ARMING_INC; 4431 if (lun->be_lun->blocksize) 4432 ival /= lun->be_lun->blocksize; 4433 else 4434 ival /= 512; 4435 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4436 page->descr[3].count); 4437 } 4438 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4439 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4440 sizeof(lbp_page_default)); 4441 page_index->page_data = 4442 (uint8_t *)lun->mode_pages.lbp_page; 4443 }} 4444 break; 4445 } 4446 case SMS_VENDOR_SPECIFIC_PAGE:{ 4447 switch (page_index->subpage) { 4448 case DBGCNF_SUBPAGE_CODE: { 4449 struct copan_debugconf_subpage *current_page, 4450 *saved_page; 4451 4452 memcpy(&lun->mode_pages.debugconf_subpage[ 4453 CTL_PAGE_CURRENT], 4454 &debugconf_page_default, 4455 sizeof(debugconf_page_default)); 4456 memcpy(&lun->mode_pages.debugconf_subpage[ 4457 CTL_PAGE_CHANGEABLE], 4458 &debugconf_page_changeable, 4459 sizeof(debugconf_page_changeable)); 4460 memcpy(&lun->mode_pages.debugconf_subpage[ 4461 CTL_PAGE_DEFAULT], 4462 &debugconf_page_default, 4463 sizeof(debugconf_page_default)); 4464 memcpy(&lun->mode_pages.debugconf_subpage[ 4465 CTL_PAGE_SAVED], 4466 &debugconf_page_default, 4467 sizeof(debugconf_page_default)); 4468 page_index->page_data = 4469 (uint8_t *)lun->mode_pages.debugconf_subpage; 4470 4471 current_page = (struct copan_debugconf_subpage *) 4472 (page_index->page_data + 4473 (page_index->page_len * 4474 CTL_PAGE_CURRENT)); 4475 saved_page = (struct copan_debugconf_subpage *) 4476 (page_index->page_data + 4477 (page_index->page_len * 4478 CTL_PAGE_SAVED)); 4479 break; 4480 } 4481 default: 4482 panic("invalid subpage value %d", 4483 page_index->subpage); 4484 break; 4485 } 4486 break; 4487 } 4488 default: 4489 panic("invalid page value %d", 4490 page_index->page_code & SMPH_PC_MASK); 4491 break; 4492 } 4493 } 4494 4495 return (CTL_RETVAL_COMPLETE); 4496} 4497 4498static int 4499ctl_init_log_page_index(struct ctl_lun *lun) 4500{ 4501 struct ctl_page_index *page_index; 4502 int i, j, k, prev; 4503 4504 memcpy(&lun->log_pages.index, log_page_index_template, 4505 sizeof(log_page_index_template)); 4506 4507 prev = -1; 4508 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4509 4510 page_index = &lun->log_pages.index[i]; 4511 /* 4512 * If this is a disk-only mode page, there's no point in 4513 * setting it up. For some pages, we have to have some 4514 * basic information about the disk in order to calculate the 4515 * mode page data. 4516 */ 4517 if ((lun->be_lun->lun_type != T_DIRECT) 4518 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4519 continue; 4520 4521 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4522 ((lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 4523 lun->backend->lun_attr == NULL)) 4524 continue; 4525 4526 if (page_index->page_code != prev) { 4527 lun->log_pages.pages_page[j] = page_index->page_code; 4528 prev = page_index->page_code; 4529 j++; 4530 } 4531 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4532 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4533 k++; 4534 } 4535 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4536 lun->log_pages.index[0].page_len = j; 4537 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4538 lun->log_pages.index[1].page_len = k * 2; 4539 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4540 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4541 4542 return (CTL_RETVAL_COMPLETE); 4543} 4544 4545static int 4546hex2bin(const char *str, uint8_t *buf, int buf_size) 4547{ 4548 int i; 4549 u_char c; 4550 4551 memset(buf, 0, buf_size); 4552 while (isspace(str[0])) 4553 str++; 4554 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4555 str += 2; 4556 buf_size *= 2; 4557 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4558 c = str[i]; 4559 if (isdigit(c)) 4560 c -= '0'; 4561 else if (isalpha(c)) 4562 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4563 else 4564 break; 4565 if (c >= 16) 4566 break; 4567 if ((i & 1) == 0) 4568 buf[i / 2] |= (c << 4); 4569 else 4570 buf[i / 2] |= c; 4571 } 4572 return ((i + 1) / 2); 4573} 4574 4575/* 4576 * LUN allocation. 4577 * 4578 * Requirements: 4579 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4580 * wants us to allocate the LUN and he can block. 4581 * - ctl_softc is always set 4582 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4583 * 4584 * Returns 0 for success, non-zero (errno) for failure. 4585 */ 4586static int 4587ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4588 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4589{ 4590 struct ctl_lun *nlun, *lun; 4591 struct ctl_port *port; 4592 struct scsi_vpd_id_descriptor *desc; 4593 struct scsi_vpd_id_t10 *t10id; 4594 const char *eui, *naa, *scsiname, *vendor, *value; 4595 int lun_number, i, lun_malloced; 4596 int devidlen, idlen1, idlen2 = 0, len; 4597 4598 if (be_lun == NULL) 4599 return (EINVAL); 4600 4601 /* 4602 * We currently only support Direct Access or Processor LUN types. 4603 */ 4604 switch (be_lun->lun_type) { 4605 case T_DIRECT: 4606 break; 4607 case T_PROCESSOR: 4608 break; 4609 case T_SEQUENTIAL: 4610 case T_CHANGER: 4611 default: 4612 be_lun->lun_config_status(be_lun->be_lun, 4613 CTL_LUN_CONFIG_FAILURE); 4614 break; 4615 } 4616 if (ctl_lun == NULL) { 4617 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4618 lun_malloced = 1; 4619 } else { 4620 lun_malloced = 0; 4621 lun = ctl_lun; 4622 } 4623 4624 memset(lun, 0, sizeof(*lun)); 4625 if (lun_malloced) 4626 lun->flags = CTL_LUN_MALLOCED; 4627 4628 /* Generate LUN ID. */ 4629 devidlen = max(CTL_DEVID_MIN_LEN, 4630 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4631 idlen1 = sizeof(*t10id) + devidlen; 4632 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4633 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4634 if (scsiname != NULL) { 4635 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4636 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4637 } 4638 eui = ctl_get_opt(&be_lun->options, "eui"); 4639 if (eui != NULL) { 4640 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4641 } 4642 naa = ctl_get_opt(&be_lun->options, "naa"); 4643 if (naa != NULL) { 4644 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4645 } 4646 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4647 M_CTL, M_WAITOK | M_ZERO); 4648 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4649 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4650 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4651 desc->length = idlen1; 4652 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4653 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4654 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4655 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4656 } else { 4657 strncpy(t10id->vendor, vendor, 4658 min(sizeof(t10id->vendor), strlen(vendor))); 4659 } 4660 strncpy((char *)t10id->vendor_spec_id, 4661 (char *)be_lun->device_id, devidlen); 4662 if (scsiname != NULL) { 4663 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4664 desc->length); 4665 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4666 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4667 SVPD_ID_TYPE_SCSI_NAME; 4668 desc->length = idlen2; 4669 strlcpy(desc->identifier, scsiname, idlen2); 4670 } 4671 if (eui != NULL) { 4672 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4673 desc->length); 4674 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4675 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4676 SVPD_ID_TYPE_EUI64; 4677 desc->length = hex2bin(eui, desc->identifier, 16); 4678 desc->length = desc->length > 12 ? 16 : 4679 (desc->length > 8 ? 12 : 8); 4680 len -= 16 - desc->length; 4681 } 4682 if (naa != NULL) { 4683 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4684 desc->length); 4685 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4686 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4687 SVPD_ID_TYPE_NAA; 4688 desc->length = hex2bin(naa, desc->identifier, 16); 4689 desc->length = desc->length > 8 ? 16 : 8; 4690 len -= 16 - desc->length; 4691 } 4692 lun->lun_devid->len = len; 4693 4694 mtx_lock(&ctl_softc->ctl_lock); 4695 /* 4696 * See if the caller requested a particular LUN number. If so, see 4697 * if it is available. Otherwise, allocate the first available LUN. 4698 */ 4699 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4700 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4701 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4702 mtx_unlock(&ctl_softc->ctl_lock); 4703 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4704 printf("ctl: requested LUN ID %d is higher " 4705 "than CTL_MAX_LUNS - 1 (%d)\n", 4706 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4707 } else { 4708 /* 4709 * XXX KDM return an error, or just assign 4710 * another LUN ID in this case?? 4711 */ 4712 printf("ctl: requested LUN ID %d is already " 4713 "in use\n", be_lun->req_lun_id); 4714 } 4715 if (lun->flags & CTL_LUN_MALLOCED) 4716 free(lun, M_CTL); 4717 be_lun->lun_config_status(be_lun->be_lun, 4718 CTL_LUN_CONFIG_FAILURE); 4719 return (ENOSPC); 4720 } 4721 lun_number = be_lun->req_lun_id; 4722 } else { 4723 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4724 if (lun_number == -1) { 4725 mtx_unlock(&ctl_softc->ctl_lock); 4726 printf("ctl: can't allocate LUN on target %ju, out of " 4727 "LUNs\n", (uintmax_t)target_id.id); 4728 if (lun->flags & CTL_LUN_MALLOCED) 4729 free(lun, M_CTL); 4730 be_lun->lun_config_status(be_lun->be_lun, 4731 CTL_LUN_CONFIG_FAILURE); 4732 return (ENOSPC); 4733 } 4734 } 4735 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4736 4737 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4738 lun->target = target_id; 4739 lun->lun = lun_number; 4740 lun->be_lun = be_lun; 4741 /* 4742 * The processor LUN is always enabled. Disk LUNs come on line 4743 * disabled, and must be enabled by the backend. 4744 */ 4745 lun->flags |= CTL_LUN_DISABLED; 4746 lun->backend = be_lun->be; 4747 be_lun->ctl_lun = lun; 4748 be_lun->lun_id = lun_number; 4749 atomic_add_int(&be_lun->be->num_luns, 1); 4750 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4751 lun->flags |= CTL_LUN_OFFLINE; 4752 4753 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4754 lun->flags |= CTL_LUN_STOPPED; 4755 4756 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4757 lun->flags |= CTL_LUN_INOPERABLE; 4758 4759 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4760 lun->flags |= CTL_LUN_PRIMARY_SC; 4761 4762 value = ctl_get_opt(&be_lun->options, "readonly"); 4763 if (value != NULL && strcmp(value, "on") == 0) 4764 lun->flags |= CTL_LUN_READONLY; 4765 4766 lun->ctl_softc = ctl_softc; 4767 TAILQ_INIT(&lun->ooa_queue); 4768 TAILQ_INIT(&lun->blocked_queue); 4769 STAILQ_INIT(&lun->error_list); 4770 ctl_tpc_lun_init(lun); 4771 4772 /* 4773 * Initialize the mode and log page index. 4774 */ 4775 ctl_init_page_index(lun); 4776 ctl_init_log_page_index(lun); 4777 4778 /* 4779 * Set the poweron UA for all initiators on this LUN only. 4780 */ 4781 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4782 lun->pending_ua[i] = CTL_UA_POWERON; 4783 4784 /* 4785 * Now, before we insert this lun on the lun list, set the lun 4786 * inventory changed UA for all other luns. 4787 */ 4788 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4789 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4790 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4791 } 4792 } 4793 4794 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4795 4796 ctl_softc->ctl_luns[lun_number] = lun; 4797 4798 ctl_softc->num_luns++; 4799 4800 /* Setup statistics gathering */ 4801 lun->stats.device_type = be_lun->lun_type; 4802 lun->stats.lun_number = lun_number; 4803 if (lun->stats.device_type == T_DIRECT) 4804 lun->stats.blocksize = be_lun->blocksize; 4805 else 4806 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4807 for (i = 0;i < CTL_MAX_PORTS;i++) 4808 lun->stats.ports[i].targ_port = i; 4809 4810 mtx_unlock(&ctl_softc->ctl_lock); 4811 4812 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4813 4814 /* 4815 * Run through each registered FETD and bring it online if it isn't 4816 * already. Enable the target ID if it hasn't been enabled, and 4817 * enable this particular LUN. 4818 */ 4819 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4820 int retval; 4821 4822 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4823 if (retval != 0) { 4824 printf("ctl_alloc_lun: FETD %s port %d returned error " 4825 "%d for lun_enable on target %ju lun %d\n", 4826 port->port_name, port->targ_port, retval, 4827 (uintmax_t)target_id.id, lun_number); 4828 } else 4829 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4830 } 4831 return (0); 4832} 4833 4834/* 4835 * Delete a LUN. 4836 * Assumptions: 4837 * - LUN has already been marked invalid and any pending I/O has been taken 4838 * care of. 4839 */ 4840static int 4841ctl_free_lun(struct ctl_lun *lun) 4842{ 4843 struct ctl_softc *softc; 4844#if 0 4845 struct ctl_port *port; 4846#endif 4847 struct ctl_lun *nlun; 4848 int i; 4849 4850 softc = lun->ctl_softc; 4851 4852 mtx_assert(&softc->ctl_lock, MA_OWNED); 4853 4854 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4855 4856 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4857 4858 softc->ctl_luns[lun->lun] = NULL; 4859 4860 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4861 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4862 4863 softc->num_luns--; 4864 4865 /* 4866 * XXX KDM this scheme only works for a single target/multiple LUN 4867 * setup. It needs to be revamped for a multiple target scheme. 4868 * 4869 * XXX KDM this results in port->lun_disable() getting called twice, 4870 * once when ctl_disable_lun() is called, and a second time here. 4871 * We really need to re-think the LUN disable semantics. There 4872 * should probably be several steps/levels to LUN removal: 4873 * - disable 4874 * - invalidate 4875 * - free 4876 * 4877 * Right now we only have a disable method when communicating to 4878 * the front end ports, at least for individual LUNs. 4879 */ 4880#if 0 4881 STAILQ_FOREACH(port, &softc->port_list, links) { 4882 int retval; 4883 4884 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4885 lun->lun); 4886 if (retval != 0) { 4887 printf("ctl_free_lun: FETD %s port %d returned error " 4888 "%d for lun_disable on target %ju lun %jd\n", 4889 port->port_name, port->targ_port, retval, 4890 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4891 } 4892 4893 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4894 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4895 4896 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4897 if (retval != 0) { 4898 printf("ctl_free_lun: FETD %s port %d " 4899 "returned error %d for targ_disable on " 4900 "target %ju\n", port->port_name, 4901 port->targ_port, retval, 4902 (uintmax_t)lun->target.id); 4903 } else 4904 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4905 4906 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4907 continue; 4908 4909#if 0 4910 port->port_offline(port->onoff_arg); 4911 port->status &= ~CTL_PORT_STATUS_ONLINE; 4912#endif 4913 } 4914 } 4915#endif 4916 4917 /* 4918 * Tell the backend to free resources, if this LUN has a backend. 4919 */ 4920 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4921 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4922 4923 ctl_tpc_lun_shutdown(lun); 4924 mtx_destroy(&lun->lun_lock); 4925 free(lun->lun_devid, M_CTL); 4926 free(lun->write_buffer, M_CTL); 4927 if (lun->flags & CTL_LUN_MALLOCED) 4928 free(lun, M_CTL); 4929 4930 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4931 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4932 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4933 } 4934 } 4935 4936 return (0); 4937} 4938 4939static void 4940ctl_create_lun(struct ctl_be_lun *be_lun) 4941{ 4942 struct ctl_softc *ctl_softc; 4943 4944 ctl_softc = control_softc; 4945 4946 /* 4947 * ctl_alloc_lun() should handle all potential failure cases. 4948 */ 4949 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4950} 4951 4952int 4953ctl_add_lun(struct ctl_be_lun *be_lun) 4954{ 4955 struct ctl_softc *ctl_softc = control_softc; 4956 4957 mtx_lock(&ctl_softc->ctl_lock); 4958 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4959 mtx_unlock(&ctl_softc->ctl_lock); 4960 wakeup(&ctl_softc->pending_lun_queue); 4961 4962 return (0); 4963} 4964 4965int 4966ctl_enable_lun(struct ctl_be_lun *be_lun) 4967{ 4968 struct ctl_softc *ctl_softc; 4969 struct ctl_port *port, *nport; 4970 struct ctl_lun *lun; 4971 int retval; 4972 4973 ctl_softc = control_softc; 4974 4975 lun = (struct ctl_lun *)be_lun->ctl_lun; 4976 4977 mtx_lock(&ctl_softc->ctl_lock); 4978 mtx_lock(&lun->lun_lock); 4979 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4980 /* 4981 * eh? Why did we get called if the LUN is already 4982 * enabled? 4983 */ 4984 mtx_unlock(&lun->lun_lock); 4985 mtx_unlock(&ctl_softc->ctl_lock); 4986 return (0); 4987 } 4988 lun->flags &= ~CTL_LUN_DISABLED; 4989 mtx_unlock(&lun->lun_lock); 4990 4991 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4992 nport = STAILQ_NEXT(port, links); 4993 4994 /* 4995 * Drop the lock while we call the FETD's enable routine. 4996 * This can lead to a callback into CTL (at least in the 4997 * case of the internal initiator frontend. 4998 */ 4999 mtx_unlock(&ctl_softc->ctl_lock); 5000 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 5001 mtx_lock(&ctl_softc->ctl_lock); 5002 if (retval != 0) { 5003 printf("%s: FETD %s port %d returned error " 5004 "%d for lun_enable on target %ju lun %jd\n", 5005 __func__, port->port_name, port->targ_port, retval, 5006 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 5007 } 5008#if 0 5009 else { 5010 /* NOTE: TODO: why does lun enable affect port status? */ 5011 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 5012 } 5013#endif 5014 } 5015 5016 mtx_unlock(&ctl_softc->ctl_lock); 5017 5018 return (0); 5019} 5020 5021int 5022ctl_disable_lun(struct ctl_be_lun *be_lun) 5023{ 5024 struct ctl_softc *ctl_softc; 5025 struct ctl_port *port; 5026 struct ctl_lun *lun; 5027 int retval; 5028 5029 ctl_softc = control_softc; 5030 5031 lun = (struct ctl_lun *)be_lun->ctl_lun; 5032 5033 mtx_lock(&ctl_softc->ctl_lock); 5034 mtx_lock(&lun->lun_lock); 5035 if (lun->flags & CTL_LUN_DISABLED) { 5036 mtx_unlock(&lun->lun_lock); 5037 mtx_unlock(&ctl_softc->ctl_lock); 5038 return (0); 5039 } 5040 lun->flags |= CTL_LUN_DISABLED; 5041 mtx_unlock(&lun->lun_lock); 5042 5043 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 5044 mtx_unlock(&ctl_softc->ctl_lock); 5045 /* 5046 * Drop the lock before we call the frontend's disable 5047 * routine, to avoid lock order reversals. 5048 * 5049 * XXX KDM what happens if the frontend list changes while 5050 * we're traversing it? It's unlikely, but should be handled. 5051 */ 5052 retval = port->lun_disable(port->targ_lun_arg, lun->target, 5053 lun->lun); 5054 mtx_lock(&ctl_softc->ctl_lock); 5055 if (retval != 0) { 5056 printf("ctl_alloc_lun: FETD %s port %d returned error " 5057 "%d for lun_disable on target %ju lun %jd\n", 5058 port->port_name, port->targ_port, retval, 5059 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 5060 } 5061 } 5062 5063 mtx_unlock(&ctl_softc->ctl_lock); 5064 5065 return (0); 5066} 5067 5068int 5069ctl_start_lun(struct ctl_be_lun *be_lun) 5070{ 5071 struct ctl_softc *ctl_softc; 5072 struct ctl_lun *lun; 5073 5074 ctl_softc = control_softc; 5075 5076 lun = (struct ctl_lun *)be_lun->ctl_lun; 5077 5078 mtx_lock(&lun->lun_lock); 5079 lun->flags &= ~CTL_LUN_STOPPED; 5080 mtx_unlock(&lun->lun_lock); 5081 5082 return (0); 5083} 5084 5085int 5086ctl_stop_lun(struct ctl_be_lun *be_lun) 5087{ 5088 struct ctl_softc *ctl_softc; 5089 struct ctl_lun *lun; 5090 5091 ctl_softc = control_softc; 5092 5093 lun = (struct ctl_lun *)be_lun->ctl_lun; 5094 5095 mtx_lock(&lun->lun_lock); 5096 lun->flags |= CTL_LUN_STOPPED; 5097 mtx_unlock(&lun->lun_lock); 5098 5099 return (0); 5100} 5101 5102int 5103ctl_lun_offline(struct ctl_be_lun *be_lun) 5104{ 5105 struct ctl_softc *ctl_softc; 5106 struct ctl_lun *lun; 5107 5108 ctl_softc = control_softc; 5109 5110 lun = (struct ctl_lun *)be_lun->ctl_lun; 5111 5112 mtx_lock(&lun->lun_lock); 5113 lun->flags |= CTL_LUN_OFFLINE; 5114 mtx_unlock(&lun->lun_lock); 5115 5116 return (0); 5117} 5118 5119int 5120ctl_lun_online(struct ctl_be_lun *be_lun) 5121{ 5122 struct ctl_softc *ctl_softc; 5123 struct ctl_lun *lun; 5124 5125 ctl_softc = control_softc; 5126 5127 lun = (struct ctl_lun *)be_lun->ctl_lun; 5128 5129 mtx_lock(&lun->lun_lock); 5130 lun->flags &= ~CTL_LUN_OFFLINE; 5131 mtx_unlock(&lun->lun_lock); 5132 5133 return (0); 5134} 5135 5136int 5137ctl_invalidate_lun(struct ctl_be_lun *be_lun) 5138{ 5139 struct ctl_softc *ctl_softc; 5140 struct ctl_lun *lun; 5141 5142 ctl_softc = control_softc; 5143 5144 lun = (struct ctl_lun *)be_lun->ctl_lun; 5145 5146 mtx_lock(&lun->lun_lock); 5147 5148 /* 5149 * The LUN needs to be disabled before it can be marked invalid. 5150 */ 5151 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 5152 mtx_unlock(&lun->lun_lock); 5153 return (-1); 5154 } 5155 /* 5156 * Mark the LUN invalid. 5157 */ 5158 lun->flags |= CTL_LUN_INVALID; 5159 5160 /* 5161 * If there is nothing in the OOA queue, go ahead and free the LUN. 5162 * If we have something in the OOA queue, we'll free it when the 5163 * last I/O completes. 5164 */ 5165 if (TAILQ_EMPTY(&lun->ooa_queue)) { 5166 mtx_unlock(&lun->lun_lock); 5167 mtx_lock(&ctl_softc->ctl_lock); 5168 ctl_free_lun(lun); 5169 mtx_unlock(&ctl_softc->ctl_lock); 5170 } else 5171 mtx_unlock(&lun->lun_lock); 5172 5173 return (0); 5174} 5175 5176int 5177ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5178{ 5179 struct ctl_softc *ctl_softc; 5180 struct ctl_lun *lun; 5181 5182 ctl_softc = control_softc; 5183 lun = (struct ctl_lun *)be_lun->ctl_lun; 5184 5185 mtx_lock(&lun->lun_lock); 5186 lun->flags |= CTL_LUN_INOPERABLE; 5187 mtx_unlock(&lun->lun_lock); 5188 5189 return (0); 5190} 5191 5192int 5193ctl_lun_operable(struct ctl_be_lun *be_lun) 5194{ 5195 struct ctl_softc *ctl_softc; 5196 struct ctl_lun *lun; 5197 5198 ctl_softc = control_softc; 5199 lun = (struct ctl_lun *)be_lun->ctl_lun; 5200 5201 mtx_lock(&lun->lun_lock); 5202 lun->flags &= ~CTL_LUN_INOPERABLE; 5203 mtx_unlock(&lun->lun_lock); 5204 5205 return (0); 5206} 5207 5208void 5209ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5210{ 5211 struct ctl_lun *lun; 5212 struct ctl_softc *softc; 5213 int i; 5214 5215 softc = control_softc; 5216 5217 lun = (struct ctl_lun *)be_lun->ctl_lun; 5218 5219 mtx_lock(&lun->lun_lock); 5220 5221 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5222 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5223 5224 mtx_unlock(&lun->lun_lock); 5225} 5226 5227/* 5228 * Backend "memory move is complete" callback for requests that never 5229 * make it down to say RAIDCore's configuration code. 5230 */ 5231int 5232ctl_config_move_done(union ctl_io *io) 5233{ 5234 int retval; 5235 5236 retval = CTL_RETVAL_COMPLETE; 5237 5238 5239 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5240 /* 5241 * XXX KDM this shouldn't happen, but what if it does? 5242 */ 5243 if (io->io_hdr.io_type != CTL_IO_SCSI) 5244 panic("I/O type isn't CTL_IO_SCSI!"); 5245 5246 if ((io->io_hdr.port_status == 0) 5247 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5248 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5249 io->io_hdr.status = CTL_SUCCESS; 5250 else if ((io->io_hdr.port_status != 0) 5251 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5252 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5253 /* 5254 * For hardware error sense keys, the sense key 5255 * specific value is defined to be a retry count, 5256 * but we use it to pass back an internal FETD 5257 * error code. XXX KDM Hopefully the FETD is only 5258 * using 16 bits for an error code, since that's 5259 * all the space we have in the sks field. 5260 */ 5261 ctl_set_internal_failure(&io->scsiio, 5262 /*sks_valid*/ 1, 5263 /*retry_count*/ 5264 io->io_hdr.port_status); 5265 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5266 free(io->scsiio.kern_data_ptr, M_CTL); 5267 ctl_done(io); 5268 goto bailout; 5269 } 5270 5271 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5272 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5273 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5274 /* 5275 * XXX KDM just assuming a single pointer here, and not a 5276 * S/G list. If we start using S/G lists for config data, 5277 * we'll need to know how to clean them up here as well. 5278 */ 5279 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5280 free(io->scsiio.kern_data_ptr, M_CTL); 5281 /* Hopefully the user has already set the status... */ 5282 ctl_done(io); 5283 } else { 5284 /* 5285 * XXX KDM now we need to continue data movement. Some 5286 * options: 5287 * - call ctl_scsiio() again? We don't do this for data 5288 * writes, because for those at least we know ahead of 5289 * time where the write will go and how long it is. For 5290 * config writes, though, that information is largely 5291 * contained within the write itself, thus we need to 5292 * parse out the data again. 5293 * 5294 * - Call some other function once the data is in? 5295 */ 5296 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5297 ctl_data_print(io); 5298 5299 /* 5300 * XXX KDM call ctl_scsiio() again for now, and check flag 5301 * bits to see whether we're allocated or not. 5302 */ 5303 retval = ctl_scsiio(&io->scsiio); 5304 } 5305bailout: 5306 return (retval); 5307} 5308 5309/* 5310 * This gets called by a backend driver when it is done with a 5311 * data_submit method. 5312 */ 5313void 5314ctl_data_submit_done(union ctl_io *io) 5315{ 5316 /* 5317 * If the IO_CONT flag is set, we need to call the supplied 5318 * function to continue processing the I/O, instead of completing 5319 * the I/O just yet. 5320 * 5321 * If there is an error, though, we don't want to keep processing. 5322 * Instead, just send status back to the initiator. 5323 */ 5324 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5325 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5326 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5327 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5328 io->scsiio.io_cont(io); 5329 return; 5330 } 5331 ctl_done(io); 5332} 5333 5334/* 5335 * This gets called by a backend driver when it is done with a 5336 * configuration write. 5337 */ 5338void 5339ctl_config_write_done(union ctl_io *io) 5340{ 5341 uint8_t *buf; 5342 5343 /* 5344 * If the IO_CONT flag is set, we need to call the supplied 5345 * function to continue processing the I/O, instead of completing 5346 * the I/O just yet. 5347 * 5348 * If there is an error, though, we don't want to keep processing. 5349 * Instead, just send status back to the initiator. 5350 */ 5351 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5352 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5353 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5354 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5355 io->scsiio.io_cont(io); 5356 return; 5357 } 5358 /* 5359 * Since a configuration write can be done for commands that actually 5360 * have data allocated, like write buffer, and commands that have 5361 * no data, like start/stop unit, we need to check here. 5362 */ 5363 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5364 buf = io->scsiio.kern_data_ptr; 5365 else 5366 buf = NULL; 5367 ctl_done(io); 5368 if (buf) 5369 free(buf, M_CTL); 5370} 5371 5372/* 5373 * SCSI release command. 5374 */ 5375int 5376ctl_scsi_release(struct ctl_scsiio *ctsio) 5377{ 5378 int length, longid, thirdparty_id, resv_id; 5379 struct ctl_softc *ctl_softc; 5380 struct ctl_lun *lun; 5381 uint32_t residx; 5382 5383 length = 0; 5384 resv_id = 0; 5385 5386 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5387 5388 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5389 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5390 ctl_softc = control_softc; 5391 5392 switch (ctsio->cdb[0]) { 5393 case RELEASE_10: { 5394 struct scsi_release_10 *cdb; 5395 5396 cdb = (struct scsi_release_10 *)ctsio->cdb; 5397 5398 if (cdb->byte2 & SR10_LONGID) 5399 longid = 1; 5400 else 5401 thirdparty_id = cdb->thirdparty_id; 5402 5403 resv_id = cdb->resv_id; 5404 length = scsi_2btoul(cdb->length); 5405 break; 5406 } 5407 } 5408 5409 5410 /* 5411 * XXX KDM right now, we only support LUN reservation. We don't 5412 * support 3rd party reservations, or extent reservations, which 5413 * might actually need the parameter list. If we've gotten this 5414 * far, we've got a LUN reservation. Anything else got kicked out 5415 * above. So, according to SPC, ignore the length. 5416 */ 5417 length = 0; 5418 5419 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5420 && (length > 0)) { 5421 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5422 ctsio->kern_data_len = length; 5423 ctsio->kern_total_len = length; 5424 ctsio->kern_data_resid = 0; 5425 ctsio->kern_rel_offset = 0; 5426 ctsio->kern_sg_entries = 0; 5427 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5428 ctsio->be_move_done = ctl_config_move_done; 5429 ctl_datamove((union ctl_io *)ctsio); 5430 5431 return (CTL_RETVAL_COMPLETE); 5432 } 5433 5434 if (length > 0) 5435 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5436 5437 mtx_lock(&lun->lun_lock); 5438 5439 /* 5440 * According to SPC, it is not an error for an intiator to attempt 5441 * to release a reservation on a LUN that isn't reserved, or that 5442 * is reserved by another initiator. The reservation can only be 5443 * released, though, by the initiator who made it or by one of 5444 * several reset type events. 5445 */ 5446 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5447 lun->flags &= ~CTL_LUN_RESERVED; 5448 5449 mtx_unlock(&lun->lun_lock); 5450 5451 ctsio->scsi_status = SCSI_STATUS_OK; 5452 ctsio->io_hdr.status = CTL_SUCCESS; 5453 5454 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5455 free(ctsio->kern_data_ptr, M_CTL); 5456 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5457 } 5458 5459 ctl_done((union ctl_io *)ctsio); 5460 return (CTL_RETVAL_COMPLETE); 5461} 5462 5463int 5464ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5465{ 5466 int extent, thirdparty, longid; 5467 int resv_id, length; 5468 uint64_t thirdparty_id; 5469 struct ctl_softc *ctl_softc; 5470 struct ctl_lun *lun; 5471 uint32_t residx; 5472 5473 extent = 0; 5474 thirdparty = 0; 5475 longid = 0; 5476 resv_id = 0; 5477 length = 0; 5478 thirdparty_id = 0; 5479 5480 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5481 5482 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5483 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5484 ctl_softc = control_softc; 5485 5486 switch (ctsio->cdb[0]) { 5487 case RESERVE_10: { 5488 struct scsi_reserve_10 *cdb; 5489 5490 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5491 5492 if (cdb->byte2 & SR10_LONGID) 5493 longid = 1; 5494 else 5495 thirdparty_id = cdb->thirdparty_id; 5496 5497 resv_id = cdb->resv_id; 5498 length = scsi_2btoul(cdb->length); 5499 break; 5500 } 5501 } 5502 5503 /* 5504 * XXX KDM right now, we only support LUN reservation. We don't 5505 * support 3rd party reservations, or extent reservations, which 5506 * might actually need the parameter list. If we've gotten this 5507 * far, we've got a LUN reservation. Anything else got kicked out 5508 * above. So, according to SPC, ignore the length. 5509 */ 5510 length = 0; 5511 5512 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5513 && (length > 0)) { 5514 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5515 ctsio->kern_data_len = length; 5516 ctsio->kern_total_len = length; 5517 ctsio->kern_data_resid = 0; 5518 ctsio->kern_rel_offset = 0; 5519 ctsio->kern_sg_entries = 0; 5520 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5521 ctsio->be_move_done = ctl_config_move_done; 5522 ctl_datamove((union ctl_io *)ctsio); 5523 5524 return (CTL_RETVAL_COMPLETE); 5525 } 5526 5527 if (length > 0) 5528 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5529 5530 mtx_lock(&lun->lun_lock); 5531 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5532 ctl_set_reservation_conflict(ctsio); 5533 goto bailout; 5534 } 5535 5536 lun->flags |= CTL_LUN_RESERVED; 5537 lun->res_idx = residx; 5538 5539 ctsio->scsi_status = SCSI_STATUS_OK; 5540 ctsio->io_hdr.status = CTL_SUCCESS; 5541 5542bailout: 5543 mtx_unlock(&lun->lun_lock); 5544 5545 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5546 free(ctsio->kern_data_ptr, M_CTL); 5547 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5548 } 5549 5550 ctl_done((union ctl_io *)ctsio); 5551 return (CTL_RETVAL_COMPLETE); 5552} 5553 5554int 5555ctl_start_stop(struct ctl_scsiio *ctsio) 5556{ 5557 struct scsi_start_stop_unit *cdb; 5558 struct ctl_lun *lun; 5559 struct ctl_softc *ctl_softc; 5560 int retval; 5561 5562 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5563 5564 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5565 ctl_softc = control_softc; 5566 retval = 0; 5567 5568 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5569 5570 /* 5571 * XXX KDM 5572 * We don't support the immediate bit on a stop unit. In order to 5573 * do that, we would need to code up a way to know that a stop is 5574 * pending, and hold off any new commands until it completes, one 5575 * way or another. Then we could accept or reject those commands 5576 * depending on its status. We would almost need to do the reverse 5577 * of what we do below for an immediate start -- return the copy of 5578 * the ctl_io to the FETD with status to send to the host (and to 5579 * free the copy!) and then free the original I/O once the stop 5580 * actually completes. That way, the OOA queue mechanism can work 5581 * to block commands that shouldn't proceed. Another alternative 5582 * would be to put the copy in the queue in place of the original, 5583 * and return the original back to the caller. That could be 5584 * slightly safer.. 5585 */ 5586 if ((cdb->byte2 & SSS_IMMED) 5587 && ((cdb->how & SSS_START) == 0)) { 5588 ctl_set_invalid_field(ctsio, 5589 /*sks_valid*/ 1, 5590 /*command*/ 1, 5591 /*field*/ 1, 5592 /*bit_valid*/ 1, 5593 /*bit*/ 0); 5594 ctl_done((union ctl_io *)ctsio); 5595 return (CTL_RETVAL_COMPLETE); 5596 } 5597 5598 if ((lun->flags & CTL_LUN_PR_RESERVED) 5599 && ((cdb->how & SSS_START)==0)) { 5600 uint32_t residx; 5601 5602 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5603 if (lun->pr_keys[residx] == 0 5604 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5605 5606 ctl_set_reservation_conflict(ctsio); 5607 ctl_done((union ctl_io *)ctsio); 5608 return (CTL_RETVAL_COMPLETE); 5609 } 5610 } 5611 5612 /* 5613 * If there is no backend on this device, we can't start or stop 5614 * it. In theory we shouldn't get any start/stop commands in the 5615 * first place at this level if the LUN doesn't have a backend. 5616 * That should get stopped by the command decode code. 5617 */ 5618 if (lun->backend == NULL) { 5619 ctl_set_invalid_opcode(ctsio); 5620 ctl_done((union ctl_io *)ctsio); 5621 return (CTL_RETVAL_COMPLETE); 5622 } 5623 5624 /* 5625 * XXX KDM Copan-specific offline behavior. 5626 * Figure out a reasonable way to port this? 5627 */ 5628#ifdef NEEDTOPORT 5629 mtx_lock(&lun->lun_lock); 5630 5631 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5632 && (lun->flags & CTL_LUN_OFFLINE)) { 5633 /* 5634 * If the LUN is offline, and the on/offline bit isn't set, 5635 * reject the start or stop. Otherwise, let it through. 5636 */ 5637 mtx_unlock(&lun->lun_lock); 5638 ctl_set_lun_not_ready(ctsio); 5639 ctl_done((union ctl_io *)ctsio); 5640 } else { 5641 mtx_unlock(&lun->lun_lock); 5642#endif /* NEEDTOPORT */ 5643 /* 5644 * This could be a start or a stop when we're online, 5645 * or a stop/offline or start/online. A start or stop when 5646 * we're offline is covered in the case above. 5647 */ 5648 /* 5649 * In the non-immediate case, we send the request to 5650 * the backend and return status to the user when 5651 * it is done. 5652 * 5653 * In the immediate case, we allocate a new ctl_io 5654 * to hold a copy of the request, and send that to 5655 * the backend. We then set good status on the 5656 * user's request and return it immediately. 5657 */ 5658 if (cdb->byte2 & SSS_IMMED) { 5659 union ctl_io *new_io; 5660 5661 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5662 if (new_io == NULL) { 5663 ctl_set_busy(ctsio); 5664 ctl_done((union ctl_io *)ctsio); 5665 } else { 5666 ctl_copy_io((union ctl_io *)ctsio, 5667 new_io); 5668 retval = lun->backend->config_write(new_io); 5669 ctl_set_success(ctsio); 5670 ctl_done((union ctl_io *)ctsio); 5671 } 5672 } else { 5673 retval = lun->backend->config_write( 5674 (union ctl_io *)ctsio); 5675 } 5676#ifdef NEEDTOPORT 5677 } 5678#endif 5679 return (retval); 5680} 5681 5682/* 5683 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5684 * we don't really do anything with the LBA and length fields if the user 5685 * passes them in. Instead we'll just flush out the cache for the entire 5686 * LUN. 5687 */ 5688int 5689ctl_sync_cache(struct ctl_scsiio *ctsio) 5690{ 5691 struct ctl_lun *lun; 5692 struct ctl_softc *ctl_softc; 5693 uint64_t starting_lba; 5694 uint32_t block_count; 5695 int retval; 5696 5697 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5698 5699 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5700 ctl_softc = control_softc; 5701 retval = 0; 5702 5703 switch (ctsio->cdb[0]) { 5704 case SYNCHRONIZE_CACHE: { 5705 struct scsi_sync_cache *cdb; 5706 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5707 5708 starting_lba = scsi_4btoul(cdb->begin_lba); 5709 block_count = scsi_2btoul(cdb->lb_count); 5710 break; 5711 } 5712 case SYNCHRONIZE_CACHE_16: { 5713 struct scsi_sync_cache_16 *cdb; 5714 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5715 5716 starting_lba = scsi_8btou64(cdb->begin_lba); 5717 block_count = scsi_4btoul(cdb->lb_count); 5718 break; 5719 } 5720 default: 5721 ctl_set_invalid_opcode(ctsio); 5722 ctl_done((union ctl_io *)ctsio); 5723 goto bailout; 5724 break; /* NOTREACHED */ 5725 } 5726 5727 /* 5728 * We check the LBA and length, but don't do anything with them. 5729 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5730 * get flushed. This check will just help satisfy anyone who wants 5731 * to see an error for an out of range LBA. 5732 */ 5733 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5734 ctl_set_lba_out_of_range(ctsio); 5735 ctl_done((union ctl_io *)ctsio); 5736 goto bailout; 5737 } 5738 5739 /* 5740 * If this LUN has no backend, we can't flush the cache anyway. 5741 */ 5742 if (lun->backend == NULL) { 5743 ctl_set_invalid_opcode(ctsio); 5744 ctl_done((union ctl_io *)ctsio); 5745 goto bailout; 5746 } 5747 5748 /* 5749 * Check to see whether we're configured to send the SYNCHRONIZE 5750 * CACHE command directly to the back end. 5751 */ 5752 mtx_lock(&lun->lun_lock); 5753 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5754 && (++(lun->sync_count) >= lun->sync_interval)) { 5755 lun->sync_count = 0; 5756 mtx_unlock(&lun->lun_lock); 5757 retval = lun->backend->config_write((union ctl_io *)ctsio); 5758 } else { 5759 mtx_unlock(&lun->lun_lock); 5760 ctl_set_success(ctsio); 5761 ctl_done((union ctl_io *)ctsio); 5762 } 5763 5764bailout: 5765 5766 return (retval); 5767} 5768 5769int 5770ctl_format(struct ctl_scsiio *ctsio) 5771{ 5772 struct scsi_format *cdb; 5773 struct ctl_lun *lun; 5774 struct ctl_softc *ctl_softc; 5775 int length, defect_list_len; 5776 5777 CTL_DEBUG_PRINT(("ctl_format\n")); 5778 5779 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5780 ctl_softc = control_softc; 5781 5782 cdb = (struct scsi_format *)ctsio->cdb; 5783 5784 length = 0; 5785 if (cdb->byte2 & SF_FMTDATA) { 5786 if (cdb->byte2 & SF_LONGLIST) 5787 length = sizeof(struct scsi_format_header_long); 5788 else 5789 length = sizeof(struct scsi_format_header_short); 5790 } 5791 5792 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5793 && (length > 0)) { 5794 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5795 ctsio->kern_data_len = length; 5796 ctsio->kern_total_len = length; 5797 ctsio->kern_data_resid = 0; 5798 ctsio->kern_rel_offset = 0; 5799 ctsio->kern_sg_entries = 0; 5800 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5801 ctsio->be_move_done = ctl_config_move_done; 5802 ctl_datamove((union ctl_io *)ctsio); 5803 5804 return (CTL_RETVAL_COMPLETE); 5805 } 5806 5807 defect_list_len = 0; 5808 5809 if (cdb->byte2 & SF_FMTDATA) { 5810 if (cdb->byte2 & SF_LONGLIST) { 5811 struct scsi_format_header_long *header; 5812 5813 header = (struct scsi_format_header_long *) 5814 ctsio->kern_data_ptr; 5815 5816 defect_list_len = scsi_4btoul(header->defect_list_len); 5817 if (defect_list_len != 0) { 5818 ctl_set_invalid_field(ctsio, 5819 /*sks_valid*/ 1, 5820 /*command*/ 0, 5821 /*field*/ 2, 5822 /*bit_valid*/ 0, 5823 /*bit*/ 0); 5824 goto bailout; 5825 } 5826 } else { 5827 struct scsi_format_header_short *header; 5828 5829 header = (struct scsi_format_header_short *) 5830 ctsio->kern_data_ptr; 5831 5832 defect_list_len = scsi_2btoul(header->defect_list_len); 5833 if (defect_list_len != 0) { 5834 ctl_set_invalid_field(ctsio, 5835 /*sks_valid*/ 1, 5836 /*command*/ 0, 5837 /*field*/ 2, 5838 /*bit_valid*/ 0, 5839 /*bit*/ 0); 5840 goto bailout; 5841 } 5842 } 5843 } 5844 5845 /* 5846 * The format command will clear out the "Medium format corrupted" 5847 * status if set by the configuration code. That status is really 5848 * just a way to notify the host that we have lost the media, and 5849 * get them to issue a command that will basically make them think 5850 * they're blowing away the media. 5851 */ 5852 mtx_lock(&lun->lun_lock); 5853 lun->flags &= ~CTL_LUN_INOPERABLE; 5854 mtx_unlock(&lun->lun_lock); 5855 5856 ctsio->scsi_status = SCSI_STATUS_OK; 5857 ctsio->io_hdr.status = CTL_SUCCESS; 5858bailout: 5859 5860 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5861 free(ctsio->kern_data_ptr, M_CTL); 5862 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5863 } 5864 5865 ctl_done((union ctl_io *)ctsio); 5866 return (CTL_RETVAL_COMPLETE); 5867} 5868 5869int 5870ctl_read_buffer(struct ctl_scsiio *ctsio) 5871{ 5872 struct scsi_read_buffer *cdb; 5873 struct ctl_lun *lun; 5874 int buffer_offset, len; 5875 static uint8_t descr[4]; 5876 static uint8_t echo_descr[4] = { 0 }; 5877 5878 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5879 5880 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5881 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5882 5883 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5884 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5885 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5886 ctl_set_invalid_field(ctsio, 5887 /*sks_valid*/ 1, 5888 /*command*/ 1, 5889 /*field*/ 1, 5890 /*bit_valid*/ 1, 5891 /*bit*/ 4); 5892 ctl_done((union ctl_io *)ctsio); 5893 return (CTL_RETVAL_COMPLETE); 5894 } 5895 5896 len = scsi_3btoul(cdb->length); 5897 buffer_offset = scsi_3btoul(cdb->offset); 5898 5899 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5900 ctl_set_invalid_field(ctsio, 5901 /*sks_valid*/ 1, 5902 /*command*/ 1, 5903 /*field*/ 6, 5904 /*bit_valid*/ 0, 5905 /*bit*/ 0); 5906 ctl_done((union ctl_io *)ctsio); 5907 return (CTL_RETVAL_COMPLETE); 5908 } 5909 5910 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5911 descr[0] = 0; 5912 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5913 ctsio->kern_data_ptr = descr; 5914 len = min(len, sizeof(descr)); 5915 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5916 ctsio->kern_data_ptr = echo_descr; 5917 len = min(len, sizeof(echo_descr)); 5918 } else { 5919 if (lun->write_buffer == NULL) { 5920 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5921 M_CTL, M_WAITOK); 5922 } 5923 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5924 } 5925 ctsio->kern_data_len = len; 5926 ctsio->kern_total_len = len; 5927 ctsio->kern_data_resid = 0; 5928 ctsio->kern_rel_offset = 0; 5929 ctsio->kern_sg_entries = 0; 5930 ctsio->be_move_done = ctl_config_move_done; 5931 ctl_datamove((union ctl_io *)ctsio); 5932 5933 return (CTL_RETVAL_COMPLETE); 5934} 5935 5936int 5937ctl_write_buffer(struct ctl_scsiio *ctsio) 5938{ 5939 struct scsi_write_buffer *cdb; 5940 struct ctl_lun *lun; 5941 int buffer_offset, len; 5942 5943 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5944 5945 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5946 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5947 5948 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5949 ctl_set_invalid_field(ctsio, 5950 /*sks_valid*/ 1, 5951 /*command*/ 1, 5952 /*field*/ 1, 5953 /*bit_valid*/ 1, 5954 /*bit*/ 4); 5955 ctl_done((union ctl_io *)ctsio); 5956 return (CTL_RETVAL_COMPLETE); 5957 } 5958 5959 len = scsi_3btoul(cdb->length); 5960 buffer_offset = scsi_3btoul(cdb->offset); 5961 5962 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5963 ctl_set_invalid_field(ctsio, 5964 /*sks_valid*/ 1, 5965 /*command*/ 1, 5966 /*field*/ 6, 5967 /*bit_valid*/ 0, 5968 /*bit*/ 0); 5969 ctl_done((union ctl_io *)ctsio); 5970 return (CTL_RETVAL_COMPLETE); 5971 } 5972 5973 /* 5974 * If we've got a kernel request that hasn't been malloced yet, 5975 * malloc it and tell the caller the data buffer is here. 5976 */ 5977 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5978 if (lun->write_buffer == NULL) { 5979 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5980 M_CTL, M_WAITOK); 5981 } 5982 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5983 ctsio->kern_data_len = len; 5984 ctsio->kern_total_len = len; 5985 ctsio->kern_data_resid = 0; 5986 ctsio->kern_rel_offset = 0; 5987 ctsio->kern_sg_entries = 0; 5988 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5989 ctsio->be_move_done = ctl_config_move_done; 5990 ctl_datamove((union ctl_io *)ctsio); 5991 5992 return (CTL_RETVAL_COMPLETE); 5993 } 5994 5995 ctl_done((union ctl_io *)ctsio); 5996 5997 return (CTL_RETVAL_COMPLETE); 5998} 5999 6000int 6001ctl_write_same(struct ctl_scsiio *ctsio) 6002{ 6003 struct ctl_lun *lun; 6004 struct ctl_lba_len_flags *lbalen; 6005 uint64_t lba; 6006 uint32_t num_blocks; 6007 int len, retval; 6008 uint8_t byte2; 6009 6010 retval = CTL_RETVAL_COMPLETE; 6011 6012 CTL_DEBUG_PRINT(("ctl_write_same\n")); 6013 6014 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6015 6016 switch (ctsio->cdb[0]) { 6017 case WRITE_SAME_10: { 6018 struct scsi_write_same_10 *cdb; 6019 6020 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 6021 6022 lba = scsi_4btoul(cdb->addr); 6023 num_blocks = scsi_2btoul(cdb->length); 6024 byte2 = cdb->byte2; 6025 break; 6026 } 6027 case WRITE_SAME_16: { 6028 struct scsi_write_same_16 *cdb; 6029 6030 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 6031 6032 lba = scsi_8btou64(cdb->addr); 6033 num_blocks = scsi_4btoul(cdb->length); 6034 byte2 = cdb->byte2; 6035 break; 6036 } 6037 default: 6038 /* 6039 * We got a command we don't support. This shouldn't 6040 * happen, commands should be filtered out above us. 6041 */ 6042 ctl_set_invalid_opcode(ctsio); 6043 ctl_done((union ctl_io *)ctsio); 6044 6045 return (CTL_RETVAL_COMPLETE); 6046 break; /* NOTREACHED */ 6047 } 6048 6049 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 6050 if ((byte2 & SWS_UNMAP) == 0 && 6051 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 6052 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 6053 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 6054 ctl_done((union ctl_io *)ctsio); 6055 return (CTL_RETVAL_COMPLETE); 6056 } 6057 6058 /* 6059 * The first check is to make sure we're in bounds, the second 6060 * check is to catch wrap-around problems. If the lba + num blocks 6061 * is less than the lba, then we've wrapped around and the block 6062 * range is invalid anyway. 6063 */ 6064 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6065 || ((lba + num_blocks) < lba)) { 6066 ctl_set_lba_out_of_range(ctsio); 6067 ctl_done((union ctl_io *)ctsio); 6068 return (CTL_RETVAL_COMPLETE); 6069 } 6070 6071 /* Zero number of blocks means "to the last logical block" */ 6072 if (num_blocks == 0) { 6073 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 6074 ctl_set_invalid_field(ctsio, 6075 /*sks_valid*/ 0, 6076 /*command*/ 1, 6077 /*field*/ 0, 6078 /*bit_valid*/ 0, 6079 /*bit*/ 0); 6080 ctl_done((union ctl_io *)ctsio); 6081 return (CTL_RETVAL_COMPLETE); 6082 } 6083 num_blocks = (lun->be_lun->maxlba + 1) - lba; 6084 } 6085 6086 len = lun->be_lun->blocksize; 6087 6088 /* 6089 * If we've got a kernel request that hasn't been malloced yet, 6090 * malloc it and tell the caller the data buffer is here. 6091 */ 6092 if ((byte2 & SWS_NDOB) == 0 && 6093 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6094 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6095 ctsio->kern_data_len = len; 6096 ctsio->kern_total_len = len; 6097 ctsio->kern_data_resid = 0; 6098 ctsio->kern_rel_offset = 0; 6099 ctsio->kern_sg_entries = 0; 6100 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6101 ctsio->be_move_done = ctl_config_move_done; 6102 ctl_datamove((union ctl_io *)ctsio); 6103 6104 return (CTL_RETVAL_COMPLETE); 6105 } 6106 6107 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6108 lbalen->lba = lba; 6109 lbalen->len = num_blocks; 6110 lbalen->flags = byte2; 6111 retval = lun->backend->config_write((union ctl_io *)ctsio); 6112 6113 return (retval); 6114} 6115 6116int 6117ctl_unmap(struct ctl_scsiio *ctsio) 6118{ 6119 struct ctl_lun *lun; 6120 struct scsi_unmap *cdb; 6121 struct ctl_ptr_len_flags *ptrlen; 6122 struct scsi_unmap_header *hdr; 6123 struct scsi_unmap_desc *buf, *end, *endnz, *range; 6124 uint64_t lba; 6125 uint32_t num_blocks; 6126 int len, retval; 6127 uint8_t byte2; 6128 6129 retval = CTL_RETVAL_COMPLETE; 6130 6131 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6132 6133 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6134 cdb = (struct scsi_unmap *)ctsio->cdb; 6135 6136 len = scsi_2btoul(cdb->length); 6137 byte2 = cdb->byte2; 6138 6139 /* 6140 * If we've got a kernel request that hasn't been malloced yet, 6141 * malloc it and tell the caller the data buffer is here. 6142 */ 6143 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6144 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6145 ctsio->kern_data_len = len; 6146 ctsio->kern_total_len = len; 6147 ctsio->kern_data_resid = 0; 6148 ctsio->kern_rel_offset = 0; 6149 ctsio->kern_sg_entries = 0; 6150 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6151 ctsio->be_move_done = ctl_config_move_done; 6152 ctl_datamove((union ctl_io *)ctsio); 6153 6154 return (CTL_RETVAL_COMPLETE); 6155 } 6156 6157 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6158 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6159 if (len < sizeof (*hdr) || 6160 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6161 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6162 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6163 ctl_set_invalid_field(ctsio, 6164 /*sks_valid*/ 0, 6165 /*command*/ 0, 6166 /*field*/ 0, 6167 /*bit_valid*/ 0, 6168 /*bit*/ 0); 6169 goto done; 6170 } 6171 len = scsi_2btoul(hdr->desc_length); 6172 buf = (struct scsi_unmap_desc *)(hdr + 1); 6173 end = buf + len / sizeof(*buf); 6174 6175 endnz = buf; 6176 for (range = buf; range < end; range++) { 6177 lba = scsi_8btou64(range->lba); 6178 num_blocks = scsi_4btoul(range->length); 6179 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6180 || ((lba + num_blocks) < lba)) { 6181 ctl_set_lba_out_of_range(ctsio); 6182 ctl_done((union ctl_io *)ctsio); 6183 return (CTL_RETVAL_COMPLETE); 6184 } 6185 if (num_blocks != 0) 6186 endnz = range + 1; 6187 } 6188 6189 /* 6190 * Block backend can not handle zero last range. 6191 * Filter it out and return if there is nothing left. 6192 */ 6193 len = (uint8_t *)endnz - (uint8_t *)buf; 6194 if (len == 0) { 6195 ctl_set_success(ctsio); 6196 goto done; 6197 } 6198 6199 mtx_lock(&lun->lun_lock); 6200 ptrlen = (struct ctl_ptr_len_flags *) 6201 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6202 ptrlen->ptr = (void *)buf; 6203 ptrlen->len = len; 6204 ptrlen->flags = byte2; 6205 ctl_check_blocked(lun); 6206 mtx_unlock(&lun->lun_lock); 6207 6208 retval = lun->backend->config_write((union ctl_io *)ctsio); 6209 return (retval); 6210 6211done: 6212 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 6213 free(ctsio->kern_data_ptr, M_CTL); 6214 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 6215 } 6216 ctl_done((union ctl_io *)ctsio); 6217 return (CTL_RETVAL_COMPLETE); 6218} 6219 6220/* 6221 * Note that this function currently doesn't actually do anything inside 6222 * CTL to enforce things if the DQue bit is turned on. 6223 * 6224 * Also note that this function can't be used in the default case, because 6225 * the DQue bit isn't set in the changeable mask for the control mode page 6226 * anyway. This is just here as an example for how to implement a page 6227 * handler, and a placeholder in case we want to allow the user to turn 6228 * tagged queueing on and off. 6229 * 6230 * The D_SENSE bit handling is functional, however, and will turn 6231 * descriptor sense on and off for a given LUN. 6232 */ 6233int 6234ctl_control_page_handler(struct ctl_scsiio *ctsio, 6235 struct ctl_page_index *page_index, uint8_t *page_ptr) 6236{ 6237 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6238 struct ctl_lun *lun; 6239 struct ctl_softc *softc; 6240 int set_ua; 6241 uint32_t initidx; 6242 6243 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6244 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6245 set_ua = 0; 6246 6247 user_cp = (struct scsi_control_page *)page_ptr; 6248 current_cp = (struct scsi_control_page *) 6249 (page_index->page_data + (page_index->page_len * 6250 CTL_PAGE_CURRENT)); 6251 saved_cp = (struct scsi_control_page *) 6252 (page_index->page_data + (page_index->page_len * 6253 CTL_PAGE_SAVED)); 6254 6255 softc = control_softc; 6256 6257 mtx_lock(&lun->lun_lock); 6258 if (((current_cp->rlec & SCP_DSENSE) == 0) 6259 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6260 /* 6261 * Descriptor sense is currently turned off and the user 6262 * wants to turn it on. 6263 */ 6264 current_cp->rlec |= SCP_DSENSE; 6265 saved_cp->rlec |= SCP_DSENSE; 6266 lun->flags |= CTL_LUN_SENSE_DESC; 6267 set_ua = 1; 6268 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6269 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6270 /* 6271 * Descriptor sense is currently turned on, and the user 6272 * wants to turn it off. 6273 */ 6274 current_cp->rlec &= ~SCP_DSENSE; 6275 saved_cp->rlec &= ~SCP_DSENSE; 6276 lun->flags &= ~CTL_LUN_SENSE_DESC; 6277 set_ua = 1; 6278 } 6279 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6280 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6281 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6282 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6283 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6284 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6285 set_ua = 1; 6286 } 6287 if ((current_cp->eca_and_aen & SCP_SWP) != 6288 (user_cp->eca_and_aen & SCP_SWP)) { 6289 current_cp->eca_and_aen &= ~SCP_SWP; 6290 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6291 saved_cp->eca_and_aen &= ~SCP_SWP; 6292 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6293 set_ua = 1; 6294 } 6295 if (set_ua != 0) { 6296 int i; 6297 /* 6298 * Let other initiators know that the mode 6299 * parameters for this LUN have changed. 6300 */ 6301 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6302 if (i == initidx) 6303 continue; 6304 6305 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6306 } 6307 } 6308 mtx_unlock(&lun->lun_lock); 6309 6310 return (0); 6311} 6312 6313int 6314ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6315 struct ctl_page_index *page_index, uint8_t *page_ptr) 6316{ 6317 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6318 struct ctl_lun *lun; 6319 int set_ua; 6320 uint32_t initidx; 6321 6322 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6323 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6324 set_ua = 0; 6325 6326 user_cp = (struct scsi_caching_page *)page_ptr; 6327 current_cp = (struct scsi_caching_page *) 6328 (page_index->page_data + (page_index->page_len * 6329 CTL_PAGE_CURRENT)); 6330 saved_cp = (struct scsi_caching_page *) 6331 (page_index->page_data + (page_index->page_len * 6332 CTL_PAGE_SAVED)); 6333 6334 mtx_lock(&lun->lun_lock); 6335 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6336 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6337 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6338 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6339 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6340 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6341 set_ua = 1; 6342 } 6343 if (set_ua != 0) { 6344 int i; 6345 /* 6346 * Let other initiators know that the mode 6347 * parameters for this LUN have changed. 6348 */ 6349 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6350 if (i == initidx) 6351 continue; 6352 6353 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6354 } 6355 } 6356 mtx_unlock(&lun->lun_lock); 6357 6358 return (0); 6359} 6360 6361int 6362ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6363 struct ctl_page_index *page_index, 6364 uint8_t *page_ptr) 6365{ 6366 uint8_t *c; 6367 int i; 6368 6369 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6370 ctl_time_io_secs = 6371 (c[0] << 8) | 6372 (c[1] << 0) | 6373 0; 6374 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6375 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6376 printf("page data:"); 6377 for (i=0; i<8; i++) 6378 printf(" %.2x",page_ptr[i]); 6379 printf("\n"); 6380 return (0); 6381} 6382 6383int 6384ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6385 struct ctl_page_index *page_index, 6386 int pc) 6387{ 6388 struct copan_debugconf_subpage *page; 6389 6390 page = (struct copan_debugconf_subpage *)page_index->page_data + 6391 (page_index->page_len * pc); 6392 6393 switch (pc) { 6394 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6395 case SMS_PAGE_CTRL_DEFAULT >> 6: 6396 case SMS_PAGE_CTRL_SAVED >> 6: 6397 /* 6398 * We don't update the changable or default bits for this page. 6399 */ 6400 break; 6401 case SMS_PAGE_CTRL_CURRENT >> 6: 6402 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6403 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6404 break; 6405 default: 6406#ifdef NEEDTOPORT 6407 EPRINT(0, "Invalid PC %d!!", pc); 6408#endif /* NEEDTOPORT */ 6409 break; 6410 } 6411 return (0); 6412} 6413 6414 6415static int 6416ctl_do_mode_select(union ctl_io *io) 6417{ 6418 struct scsi_mode_page_header *page_header; 6419 struct ctl_page_index *page_index; 6420 struct ctl_scsiio *ctsio; 6421 int control_dev, page_len; 6422 int page_len_offset, page_len_size; 6423 union ctl_modepage_info *modepage_info; 6424 struct ctl_lun *lun; 6425 int *len_left, *len_used; 6426 int retval, i; 6427 6428 ctsio = &io->scsiio; 6429 page_index = NULL; 6430 page_len = 0; 6431 retval = CTL_RETVAL_COMPLETE; 6432 6433 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6434 6435 if (lun->be_lun->lun_type != T_DIRECT) 6436 control_dev = 1; 6437 else 6438 control_dev = 0; 6439 6440 modepage_info = (union ctl_modepage_info *) 6441 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6442 len_left = &modepage_info->header.len_left; 6443 len_used = &modepage_info->header.len_used; 6444 6445do_next_page: 6446 6447 page_header = (struct scsi_mode_page_header *) 6448 (ctsio->kern_data_ptr + *len_used); 6449 6450 if (*len_left == 0) { 6451 free(ctsio->kern_data_ptr, M_CTL); 6452 ctl_set_success(ctsio); 6453 ctl_done((union ctl_io *)ctsio); 6454 return (CTL_RETVAL_COMPLETE); 6455 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6456 6457 free(ctsio->kern_data_ptr, M_CTL); 6458 ctl_set_param_len_error(ctsio); 6459 ctl_done((union ctl_io *)ctsio); 6460 return (CTL_RETVAL_COMPLETE); 6461 6462 } else if ((page_header->page_code & SMPH_SPF) 6463 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6464 6465 free(ctsio->kern_data_ptr, M_CTL); 6466 ctl_set_param_len_error(ctsio); 6467 ctl_done((union ctl_io *)ctsio); 6468 return (CTL_RETVAL_COMPLETE); 6469 } 6470 6471 6472 /* 6473 * XXX KDM should we do something with the block descriptor? 6474 */ 6475 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6476 6477 if ((control_dev != 0) 6478 && (lun->mode_pages.index[i].page_flags & 6479 CTL_PAGE_FLAG_DISK_ONLY)) 6480 continue; 6481 6482 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6483 (page_header->page_code & SMPH_PC_MASK)) 6484 continue; 6485 6486 /* 6487 * If neither page has a subpage code, then we've got a 6488 * match. 6489 */ 6490 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6491 && ((page_header->page_code & SMPH_SPF) == 0)) { 6492 page_index = &lun->mode_pages.index[i]; 6493 page_len = page_header->page_length; 6494 break; 6495 } 6496 6497 /* 6498 * If both pages have subpages, then the subpage numbers 6499 * have to match. 6500 */ 6501 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6502 && (page_header->page_code & SMPH_SPF)) { 6503 struct scsi_mode_page_header_sp *sph; 6504 6505 sph = (struct scsi_mode_page_header_sp *)page_header; 6506 6507 if (lun->mode_pages.index[i].subpage == 6508 sph->subpage) { 6509 page_index = &lun->mode_pages.index[i]; 6510 page_len = scsi_2btoul(sph->page_length); 6511 break; 6512 } 6513 } 6514 } 6515 6516 /* 6517 * If we couldn't find the page, or if we don't have a mode select 6518 * handler for it, send back an error to the user. 6519 */ 6520 if ((page_index == NULL) 6521 || (page_index->select_handler == NULL)) { 6522 ctl_set_invalid_field(ctsio, 6523 /*sks_valid*/ 1, 6524 /*command*/ 0, 6525 /*field*/ *len_used, 6526 /*bit_valid*/ 0, 6527 /*bit*/ 0); 6528 free(ctsio->kern_data_ptr, M_CTL); 6529 ctl_done((union ctl_io *)ctsio); 6530 return (CTL_RETVAL_COMPLETE); 6531 } 6532 6533 if (page_index->page_code & SMPH_SPF) { 6534 page_len_offset = 2; 6535 page_len_size = 2; 6536 } else { 6537 page_len_size = 1; 6538 page_len_offset = 1; 6539 } 6540 6541 /* 6542 * If the length the initiator gives us isn't the one we specify in 6543 * the mode page header, or if they didn't specify enough data in 6544 * the CDB to avoid truncating this page, kick out the request. 6545 */ 6546 if ((page_len != (page_index->page_len - page_len_offset - 6547 page_len_size)) 6548 || (*len_left < page_index->page_len)) { 6549 6550 6551 ctl_set_invalid_field(ctsio, 6552 /*sks_valid*/ 1, 6553 /*command*/ 0, 6554 /*field*/ *len_used + page_len_offset, 6555 /*bit_valid*/ 0, 6556 /*bit*/ 0); 6557 free(ctsio->kern_data_ptr, M_CTL); 6558 ctl_done((union ctl_io *)ctsio); 6559 return (CTL_RETVAL_COMPLETE); 6560 } 6561 6562 /* 6563 * Run through the mode page, checking to make sure that the bits 6564 * the user changed are actually legal for him to change. 6565 */ 6566 for (i = 0; i < page_index->page_len; i++) { 6567 uint8_t *user_byte, *change_mask, *current_byte; 6568 int bad_bit; 6569 int j; 6570 6571 user_byte = (uint8_t *)page_header + i; 6572 change_mask = page_index->page_data + 6573 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6574 current_byte = page_index->page_data + 6575 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6576 6577 /* 6578 * Check to see whether the user set any bits in this byte 6579 * that he is not allowed to set. 6580 */ 6581 if ((*user_byte & ~(*change_mask)) == 6582 (*current_byte & ~(*change_mask))) 6583 continue; 6584 6585 /* 6586 * Go through bit by bit to determine which one is illegal. 6587 */ 6588 bad_bit = 0; 6589 for (j = 7; j >= 0; j--) { 6590 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6591 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6592 bad_bit = i; 6593 break; 6594 } 6595 } 6596 ctl_set_invalid_field(ctsio, 6597 /*sks_valid*/ 1, 6598 /*command*/ 0, 6599 /*field*/ *len_used + i, 6600 /*bit_valid*/ 1, 6601 /*bit*/ bad_bit); 6602 free(ctsio->kern_data_ptr, M_CTL); 6603 ctl_done((union ctl_io *)ctsio); 6604 return (CTL_RETVAL_COMPLETE); 6605 } 6606 6607 /* 6608 * Decrement these before we call the page handler, since we may 6609 * end up getting called back one way or another before the handler 6610 * returns to this context. 6611 */ 6612 *len_left -= page_index->page_len; 6613 *len_used += page_index->page_len; 6614 6615 retval = page_index->select_handler(ctsio, page_index, 6616 (uint8_t *)page_header); 6617 6618 /* 6619 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6620 * wait until this queued command completes to finish processing 6621 * the mode page. If it returns anything other than 6622 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6623 * already set the sense information, freed the data pointer, and 6624 * completed the io for us. 6625 */ 6626 if (retval != CTL_RETVAL_COMPLETE) 6627 goto bailout_no_done; 6628 6629 /* 6630 * If the initiator sent us more than one page, parse the next one. 6631 */ 6632 if (*len_left > 0) 6633 goto do_next_page; 6634 6635 ctl_set_success(ctsio); 6636 free(ctsio->kern_data_ptr, M_CTL); 6637 ctl_done((union ctl_io *)ctsio); 6638 6639bailout_no_done: 6640 6641 return (CTL_RETVAL_COMPLETE); 6642 6643} 6644 6645int 6646ctl_mode_select(struct ctl_scsiio *ctsio) 6647{ 6648 int param_len, pf, sp; 6649 int header_size, bd_len; 6650 int len_left, len_used; 6651 struct ctl_page_index *page_index; 6652 struct ctl_lun *lun; 6653 int control_dev, page_len; 6654 union ctl_modepage_info *modepage_info; 6655 int retval; 6656 6657 pf = 0; 6658 sp = 0; 6659 page_len = 0; 6660 len_used = 0; 6661 len_left = 0; 6662 retval = 0; 6663 bd_len = 0; 6664 page_index = NULL; 6665 6666 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6667 6668 if (lun->be_lun->lun_type != T_DIRECT) 6669 control_dev = 1; 6670 else 6671 control_dev = 0; 6672 6673 switch (ctsio->cdb[0]) { 6674 case MODE_SELECT_6: { 6675 struct scsi_mode_select_6 *cdb; 6676 6677 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6678 6679 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6680 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6681 6682 param_len = cdb->length; 6683 header_size = sizeof(struct scsi_mode_header_6); 6684 break; 6685 } 6686 case MODE_SELECT_10: { 6687 struct scsi_mode_select_10 *cdb; 6688 6689 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6690 6691 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6692 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6693 6694 param_len = scsi_2btoul(cdb->length); 6695 header_size = sizeof(struct scsi_mode_header_10); 6696 break; 6697 } 6698 default: 6699 ctl_set_invalid_opcode(ctsio); 6700 ctl_done((union ctl_io *)ctsio); 6701 return (CTL_RETVAL_COMPLETE); 6702 break; /* NOTREACHED */ 6703 } 6704 6705 /* 6706 * From SPC-3: 6707 * "A parameter list length of zero indicates that the Data-Out Buffer 6708 * shall be empty. This condition shall not be considered as an error." 6709 */ 6710 if (param_len == 0) { 6711 ctl_set_success(ctsio); 6712 ctl_done((union ctl_io *)ctsio); 6713 return (CTL_RETVAL_COMPLETE); 6714 } 6715 6716 /* 6717 * Since we'll hit this the first time through, prior to 6718 * allocation, we don't need to free a data buffer here. 6719 */ 6720 if (param_len < header_size) { 6721 ctl_set_param_len_error(ctsio); 6722 ctl_done((union ctl_io *)ctsio); 6723 return (CTL_RETVAL_COMPLETE); 6724 } 6725 6726 /* 6727 * Allocate the data buffer and grab the user's data. In theory, 6728 * we shouldn't have to sanity check the parameter list length here 6729 * because the maximum size is 64K. We should be able to malloc 6730 * that much without too many problems. 6731 */ 6732 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6733 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6734 ctsio->kern_data_len = param_len; 6735 ctsio->kern_total_len = param_len; 6736 ctsio->kern_data_resid = 0; 6737 ctsio->kern_rel_offset = 0; 6738 ctsio->kern_sg_entries = 0; 6739 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6740 ctsio->be_move_done = ctl_config_move_done; 6741 ctl_datamove((union ctl_io *)ctsio); 6742 6743 return (CTL_RETVAL_COMPLETE); 6744 } 6745 6746 switch (ctsio->cdb[0]) { 6747 case MODE_SELECT_6: { 6748 struct scsi_mode_header_6 *mh6; 6749 6750 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6751 bd_len = mh6->blk_desc_len; 6752 break; 6753 } 6754 case MODE_SELECT_10: { 6755 struct scsi_mode_header_10 *mh10; 6756 6757 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6758 bd_len = scsi_2btoul(mh10->blk_desc_len); 6759 break; 6760 } 6761 default: 6762 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6763 break; 6764 } 6765 6766 if (param_len < (header_size + bd_len)) { 6767 free(ctsio->kern_data_ptr, M_CTL); 6768 ctl_set_param_len_error(ctsio); 6769 ctl_done((union ctl_io *)ctsio); 6770 return (CTL_RETVAL_COMPLETE); 6771 } 6772 6773 /* 6774 * Set the IO_CONT flag, so that if this I/O gets passed to 6775 * ctl_config_write_done(), it'll get passed back to 6776 * ctl_do_mode_select() for further processing, or completion if 6777 * we're all done. 6778 */ 6779 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6780 ctsio->io_cont = ctl_do_mode_select; 6781 6782 modepage_info = (union ctl_modepage_info *) 6783 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6784 6785 memset(modepage_info, 0, sizeof(*modepage_info)); 6786 6787 len_left = param_len - header_size - bd_len; 6788 len_used = header_size + bd_len; 6789 6790 modepage_info->header.len_left = len_left; 6791 modepage_info->header.len_used = len_used; 6792 6793 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6794} 6795 6796int 6797ctl_mode_sense(struct ctl_scsiio *ctsio) 6798{ 6799 struct ctl_lun *lun; 6800 int pc, page_code, dbd, llba, subpage; 6801 int alloc_len, page_len, header_len, total_len; 6802 struct scsi_mode_block_descr *block_desc; 6803 struct ctl_page_index *page_index; 6804 int control_dev; 6805 6806 dbd = 0; 6807 llba = 0; 6808 block_desc = NULL; 6809 page_index = NULL; 6810 6811 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6812 6813 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6814 6815 if (lun->be_lun->lun_type != T_DIRECT) 6816 control_dev = 1; 6817 else 6818 control_dev = 0; 6819 6820 switch (ctsio->cdb[0]) { 6821 case MODE_SENSE_6: { 6822 struct scsi_mode_sense_6 *cdb; 6823 6824 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6825 6826 header_len = sizeof(struct scsi_mode_hdr_6); 6827 if (cdb->byte2 & SMS_DBD) 6828 dbd = 1; 6829 else 6830 header_len += sizeof(struct scsi_mode_block_descr); 6831 6832 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6833 page_code = cdb->page & SMS_PAGE_CODE; 6834 subpage = cdb->subpage; 6835 alloc_len = cdb->length; 6836 break; 6837 } 6838 case MODE_SENSE_10: { 6839 struct scsi_mode_sense_10 *cdb; 6840 6841 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6842 6843 header_len = sizeof(struct scsi_mode_hdr_10); 6844 6845 if (cdb->byte2 & SMS_DBD) 6846 dbd = 1; 6847 else 6848 header_len += sizeof(struct scsi_mode_block_descr); 6849 if (cdb->byte2 & SMS10_LLBAA) 6850 llba = 1; 6851 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6852 page_code = cdb->page & SMS_PAGE_CODE; 6853 subpage = cdb->subpage; 6854 alloc_len = scsi_2btoul(cdb->length); 6855 break; 6856 } 6857 default: 6858 ctl_set_invalid_opcode(ctsio); 6859 ctl_done((union ctl_io *)ctsio); 6860 return (CTL_RETVAL_COMPLETE); 6861 break; /* NOTREACHED */ 6862 } 6863 6864 /* 6865 * We have to make a first pass through to calculate the size of 6866 * the pages that match the user's query. Then we allocate enough 6867 * memory to hold it, and actually copy the data into the buffer. 6868 */ 6869 switch (page_code) { 6870 case SMS_ALL_PAGES_PAGE: { 6871 int i; 6872 6873 page_len = 0; 6874 6875 /* 6876 * At the moment, values other than 0 and 0xff here are 6877 * reserved according to SPC-3. 6878 */ 6879 if ((subpage != SMS_SUBPAGE_PAGE_0) 6880 && (subpage != SMS_SUBPAGE_ALL)) { 6881 ctl_set_invalid_field(ctsio, 6882 /*sks_valid*/ 1, 6883 /*command*/ 1, 6884 /*field*/ 3, 6885 /*bit_valid*/ 0, 6886 /*bit*/ 0); 6887 ctl_done((union ctl_io *)ctsio); 6888 return (CTL_RETVAL_COMPLETE); 6889 } 6890 6891 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6892 if ((control_dev != 0) 6893 && (lun->mode_pages.index[i].page_flags & 6894 CTL_PAGE_FLAG_DISK_ONLY)) 6895 continue; 6896 6897 /* 6898 * We don't use this subpage if the user didn't 6899 * request all subpages. 6900 */ 6901 if ((lun->mode_pages.index[i].subpage != 0) 6902 && (subpage == SMS_SUBPAGE_PAGE_0)) 6903 continue; 6904 6905#if 0 6906 printf("found page %#x len %d\n", 6907 lun->mode_pages.index[i].page_code & 6908 SMPH_PC_MASK, 6909 lun->mode_pages.index[i].page_len); 6910#endif 6911 page_len += lun->mode_pages.index[i].page_len; 6912 } 6913 break; 6914 } 6915 default: { 6916 int i; 6917 6918 page_len = 0; 6919 6920 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6921 /* Look for the right page code */ 6922 if ((lun->mode_pages.index[i].page_code & 6923 SMPH_PC_MASK) != page_code) 6924 continue; 6925 6926 /* Look for the right subpage or the subpage wildcard*/ 6927 if ((lun->mode_pages.index[i].subpage != subpage) 6928 && (subpage != SMS_SUBPAGE_ALL)) 6929 continue; 6930 6931 /* Make sure the page is supported for this dev type */ 6932 if ((control_dev != 0) 6933 && (lun->mode_pages.index[i].page_flags & 6934 CTL_PAGE_FLAG_DISK_ONLY)) 6935 continue; 6936 6937#if 0 6938 printf("found page %#x len %d\n", 6939 lun->mode_pages.index[i].page_code & 6940 SMPH_PC_MASK, 6941 lun->mode_pages.index[i].page_len); 6942#endif 6943 6944 page_len += lun->mode_pages.index[i].page_len; 6945 } 6946 6947 if (page_len == 0) { 6948 ctl_set_invalid_field(ctsio, 6949 /*sks_valid*/ 1, 6950 /*command*/ 1, 6951 /*field*/ 2, 6952 /*bit_valid*/ 1, 6953 /*bit*/ 5); 6954 ctl_done((union ctl_io *)ctsio); 6955 return (CTL_RETVAL_COMPLETE); 6956 } 6957 break; 6958 } 6959 } 6960 6961 total_len = header_len + page_len; 6962#if 0 6963 printf("header_len = %d, page_len = %d, total_len = %d\n", 6964 header_len, page_len, total_len); 6965#endif 6966 6967 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6968 ctsio->kern_sg_entries = 0; 6969 ctsio->kern_data_resid = 0; 6970 ctsio->kern_rel_offset = 0; 6971 if (total_len < alloc_len) { 6972 ctsio->residual = alloc_len - total_len; 6973 ctsio->kern_data_len = total_len; 6974 ctsio->kern_total_len = total_len; 6975 } else { 6976 ctsio->residual = 0; 6977 ctsio->kern_data_len = alloc_len; 6978 ctsio->kern_total_len = alloc_len; 6979 } 6980 6981 switch (ctsio->cdb[0]) { 6982 case MODE_SENSE_6: { 6983 struct scsi_mode_hdr_6 *header; 6984 6985 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6986 6987 header->datalen = ctl_min(total_len - 1, 254); 6988 if (control_dev == 0) { 6989 header->dev_specific = 0x10; /* DPOFUA */ 6990 if ((lun->flags & CTL_LUN_READONLY) || 6991 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6992 .eca_and_aen & SCP_SWP) != 0) 6993 header->dev_specific |= 0x80; /* WP */ 6994 } 6995 if (dbd) 6996 header->block_descr_len = 0; 6997 else 6998 header->block_descr_len = 6999 sizeof(struct scsi_mode_block_descr); 7000 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7001 break; 7002 } 7003 case MODE_SENSE_10: { 7004 struct scsi_mode_hdr_10 *header; 7005 int datalen; 7006 7007 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 7008 7009 datalen = ctl_min(total_len - 2, 65533); 7010 scsi_ulto2b(datalen, header->datalen); 7011 if (control_dev == 0) { 7012 header->dev_specific = 0x10; /* DPOFUA */ 7013 if ((lun->flags & CTL_LUN_READONLY) || 7014 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 7015 .eca_and_aen & SCP_SWP) != 0) 7016 header->dev_specific |= 0x80; /* WP */ 7017 } 7018 if (dbd) 7019 scsi_ulto2b(0, header->block_descr_len); 7020 else 7021 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 7022 header->block_descr_len); 7023 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7024 break; 7025 } 7026 default: 7027 panic("invalid CDB type %#x", ctsio->cdb[0]); 7028 break; /* NOTREACHED */ 7029 } 7030 7031 /* 7032 * If we've got a disk, use its blocksize in the block 7033 * descriptor. Otherwise, just set it to 0. 7034 */ 7035 if (dbd == 0) { 7036 if (control_dev == 0) 7037 scsi_ulto3b(lun->be_lun->blocksize, 7038 block_desc->block_len); 7039 else 7040 scsi_ulto3b(0, block_desc->block_len); 7041 } 7042 7043 switch (page_code) { 7044 case SMS_ALL_PAGES_PAGE: { 7045 int i, data_used; 7046 7047 data_used = header_len; 7048 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7049 struct ctl_page_index *page_index; 7050 7051 page_index = &lun->mode_pages.index[i]; 7052 7053 if ((control_dev != 0) 7054 && (page_index->page_flags & 7055 CTL_PAGE_FLAG_DISK_ONLY)) 7056 continue; 7057 7058 /* 7059 * We don't use this subpage if the user didn't 7060 * request all subpages. We already checked (above) 7061 * to make sure the user only specified a subpage 7062 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7063 */ 7064 if ((page_index->subpage != 0) 7065 && (subpage == SMS_SUBPAGE_PAGE_0)) 7066 continue; 7067 7068 /* 7069 * Call the handler, if it exists, to update the 7070 * page to the latest values. 7071 */ 7072 if (page_index->sense_handler != NULL) 7073 page_index->sense_handler(ctsio, page_index,pc); 7074 7075 memcpy(ctsio->kern_data_ptr + data_used, 7076 page_index->page_data + 7077 (page_index->page_len * pc), 7078 page_index->page_len); 7079 data_used += page_index->page_len; 7080 } 7081 break; 7082 } 7083 default: { 7084 int i, data_used; 7085 7086 data_used = header_len; 7087 7088 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7089 struct ctl_page_index *page_index; 7090 7091 page_index = &lun->mode_pages.index[i]; 7092 7093 /* Look for the right page code */ 7094 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7095 continue; 7096 7097 /* Look for the right subpage or the subpage wildcard*/ 7098 if ((page_index->subpage != subpage) 7099 && (subpage != SMS_SUBPAGE_ALL)) 7100 continue; 7101 7102 /* Make sure the page is supported for this dev type */ 7103 if ((control_dev != 0) 7104 && (page_index->page_flags & 7105 CTL_PAGE_FLAG_DISK_ONLY)) 7106 continue; 7107 7108 /* 7109 * Call the handler, if it exists, to update the 7110 * page to the latest values. 7111 */ 7112 if (page_index->sense_handler != NULL) 7113 page_index->sense_handler(ctsio, page_index,pc); 7114 7115 memcpy(ctsio->kern_data_ptr + data_used, 7116 page_index->page_data + 7117 (page_index->page_len * pc), 7118 page_index->page_len); 7119 data_used += page_index->page_len; 7120 } 7121 break; 7122 } 7123 } 7124 7125 ctsio->scsi_status = SCSI_STATUS_OK; 7126 7127 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7128 ctsio->be_move_done = ctl_config_move_done; 7129 ctl_datamove((union ctl_io *)ctsio); 7130 7131 return (CTL_RETVAL_COMPLETE); 7132} 7133 7134int 7135ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 7136 struct ctl_page_index *page_index, 7137 int pc) 7138{ 7139 struct ctl_lun *lun; 7140 struct scsi_log_param_header *phdr; 7141 uint8_t *data; 7142 uint64_t val; 7143 7144 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7145 data = page_index->page_data; 7146 7147 if (lun->backend->lun_attr != NULL && 7148 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 7149 != UINT64_MAX) { 7150 phdr = (struct scsi_log_param_header *)data; 7151 scsi_ulto2b(0x0001, phdr->param_code); 7152 phdr->param_control = SLP_LBIN | SLP_LP; 7153 phdr->param_len = 8; 7154 data = (uint8_t *)(phdr + 1); 7155 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 7156 data[4] = 0x02; /* per-pool */ 7157 data += phdr->param_len; 7158 } 7159 7160 if (lun->backend->lun_attr != NULL && 7161 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 7162 != UINT64_MAX) { 7163 phdr = (struct scsi_log_param_header *)data; 7164 scsi_ulto2b(0x0002, phdr->param_code); 7165 phdr->param_control = SLP_LBIN | SLP_LP; 7166 phdr->param_len = 8; 7167 data = (uint8_t *)(phdr + 1); 7168 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 7169 data[4] = 0x01; /* per-LUN */ 7170 data += phdr->param_len; 7171 } 7172 7173 if (lun->backend->lun_attr != NULL && 7174 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 7175 != UINT64_MAX) { 7176 phdr = (struct scsi_log_param_header *)data; 7177 scsi_ulto2b(0x00f1, phdr->param_code); 7178 phdr->param_control = SLP_LBIN | SLP_LP; 7179 phdr->param_len = 8; 7180 data = (uint8_t *)(phdr + 1); 7181 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 7182 data[4] = 0x02; /* per-pool */ 7183 data += phdr->param_len; 7184 } 7185 7186 if (lun->backend->lun_attr != NULL && 7187 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 7188 != UINT64_MAX) { 7189 phdr = (struct scsi_log_param_header *)data; 7190 scsi_ulto2b(0x00f2, phdr->param_code); 7191 phdr->param_control = SLP_LBIN | SLP_LP; 7192 phdr->param_len = 8; 7193 data = (uint8_t *)(phdr + 1); 7194 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 7195 data[4] = 0x02; /* per-pool */ 7196 data += phdr->param_len; 7197 } 7198 7199 page_index->page_len = data - page_index->page_data; 7200 return (0); 7201} 7202 7203int 7204ctl_log_sense(struct ctl_scsiio *ctsio) 7205{ 7206 struct ctl_lun *lun; 7207 int i, pc, page_code, subpage; 7208 int alloc_len, total_len; 7209 struct ctl_page_index *page_index; 7210 struct scsi_log_sense *cdb; 7211 struct scsi_log_header *header; 7212 7213 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 7214 7215 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7216 cdb = (struct scsi_log_sense *)ctsio->cdb; 7217 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 7218 page_code = cdb->page & SLS_PAGE_CODE; 7219 subpage = cdb->subpage; 7220 alloc_len = scsi_2btoul(cdb->length); 7221 7222 page_index = NULL; 7223 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 7224 page_index = &lun->log_pages.index[i]; 7225 7226 /* Look for the right page code */ 7227 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 7228 continue; 7229 7230 /* Look for the right subpage or the subpage wildcard*/ 7231 if (page_index->subpage != subpage) 7232 continue; 7233 7234 break; 7235 } 7236 if (i >= CTL_NUM_LOG_PAGES) { 7237 ctl_set_invalid_field(ctsio, 7238 /*sks_valid*/ 1, 7239 /*command*/ 1, 7240 /*field*/ 2, 7241 /*bit_valid*/ 0, 7242 /*bit*/ 0); 7243 ctl_done((union ctl_io *)ctsio); 7244 return (CTL_RETVAL_COMPLETE); 7245 } 7246 7247 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 7248 7249 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7250 ctsio->kern_sg_entries = 0; 7251 ctsio->kern_data_resid = 0; 7252 ctsio->kern_rel_offset = 0; 7253 if (total_len < alloc_len) { 7254 ctsio->residual = alloc_len - total_len; 7255 ctsio->kern_data_len = total_len; 7256 ctsio->kern_total_len = total_len; 7257 } else { 7258 ctsio->residual = 0; 7259 ctsio->kern_data_len = alloc_len; 7260 ctsio->kern_total_len = alloc_len; 7261 } 7262 7263 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7264 header->page = page_index->page_code; 7265 if (page_index->subpage) { 7266 header->page |= SL_SPF; 7267 header->subpage = page_index->subpage; 7268 } 7269 scsi_ulto2b(page_index->page_len, header->datalen); 7270 7271 /* 7272 * Call the handler, if it exists, to update the 7273 * page to the latest values. 7274 */ 7275 if (page_index->sense_handler != NULL) 7276 page_index->sense_handler(ctsio, page_index, pc); 7277 7278 memcpy(header + 1, page_index->page_data, page_index->page_len); 7279 7280 ctsio->scsi_status = SCSI_STATUS_OK; 7281 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7282 ctsio->be_move_done = ctl_config_move_done; 7283 ctl_datamove((union ctl_io *)ctsio); 7284 7285 return (CTL_RETVAL_COMPLETE); 7286} 7287 7288int 7289ctl_read_capacity(struct ctl_scsiio *ctsio) 7290{ 7291 struct scsi_read_capacity *cdb; 7292 struct scsi_read_capacity_data *data; 7293 struct ctl_lun *lun; 7294 uint32_t lba; 7295 7296 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7297 7298 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7299 7300 lba = scsi_4btoul(cdb->addr); 7301 if (((cdb->pmi & SRC_PMI) == 0) 7302 && (lba != 0)) { 7303 ctl_set_invalid_field(/*ctsio*/ ctsio, 7304 /*sks_valid*/ 1, 7305 /*command*/ 1, 7306 /*field*/ 2, 7307 /*bit_valid*/ 0, 7308 /*bit*/ 0); 7309 ctl_done((union ctl_io *)ctsio); 7310 return (CTL_RETVAL_COMPLETE); 7311 } 7312 7313 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7314 7315 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7316 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7317 ctsio->residual = 0; 7318 ctsio->kern_data_len = sizeof(*data); 7319 ctsio->kern_total_len = sizeof(*data); 7320 ctsio->kern_data_resid = 0; 7321 ctsio->kern_rel_offset = 0; 7322 ctsio->kern_sg_entries = 0; 7323 7324 /* 7325 * If the maximum LBA is greater than 0xfffffffe, the user must 7326 * issue a SERVICE ACTION IN (16) command, with the read capacity 7327 * serivce action set. 7328 */ 7329 if (lun->be_lun->maxlba > 0xfffffffe) 7330 scsi_ulto4b(0xffffffff, data->addr); 7331 else 7332 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7333 7334 /* 7335 * XXX KDM this may not be 512 bytes... 7336 */ 7337 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7338 7339 ctsio->scsi_status = SCSI_STATUS_OK; 7340 7341 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7342 ctsio->be_move_done = ctl_config_move_done; 7343 ctl_datamove((union ctl_io *)ctsio); 7344 7345 return (CTL_RETVAL_COMPLETE); 7346} 7347 7348int 7349ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7350{ 7351 struct scsi_read_capacity_16 *cdb; 7352 struct scsi_read_capacity_data_long *data; 7353 struct ctl_lun *lun; 7354 uint64_t lba; 7355 uint32_t alloc_len; 7356 7357 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7358 7359 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7360 7361 alloc_len = scsi_4btoul(cdb->alloc_len); 7362 lba = scsi_8btou64(cdb->addr); 7363 7364 if ((cdb->reladr & SRC16_PMI) 7365 && (lba != 0)) { 7366 ctl_set_invalid_field(/*ctsio*/ ctsio, 7367 /*sks_valid*/ 1, 7368 /*command*/ 1, 7369 /*field*/ 2, 7370 /*bit_valid*/ 0, 7371 /*bit*/ 0); 7372 ctl_done((union ctl_io *)ctsio); 7373 return (CTL_RETVAL_COMPLETE); 7374 } 7375 7376 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7377 7378 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7379 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7380 7381 if (sizeof(*data) < alloc_len) { 7382 ctsio->residual = alloc_len - sizeof(*data); 7383 ctsio->kern_data_len = sizeof(*data); 7384 ctsio->kern_total_len = sizeof(*data); 7385 } else { 7386 ctsio->residual = 0; 7387 ctsio->kern_data_len = alloc_len; 7388 ctsio->kern_total_len = alloc_len; 7389 } 7390 ctsio->kern_data_resid = 0; 7391 ctsio->kern_rel_offset = 0; 7392 ctsio->kern_sg_entries = 0; 7393 7394 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7395 /* XXX KDM this may not be 512 bytes... */ 7396 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7397 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7398 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7399 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7400 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7401 7402 ctsio->scsi_status = SCSI_STATUS_OK; 7403 7404 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7405 ctsio->be_move_done = ctl_config_move_done; 7406 ctl_datamove((union ctl_io *)ctsio); 7407 7408 return (CTL_RETVAL_COMPLETE); 7409} 7410 7411int 7412ctl_read_defect(struct ctl_scsiio *ctsio) 7413{ 7414 struct scsi_read_defect_data_10 *ccb10; 7415 struct scsi_read_defect_data_12 *ccb12; 7416 struct scsi_read_defect_data_hdr_10 *data10; 7417 struct scsi_read_defect_data_hdr_12 *data12; 7418 uint32_t alloc_len, data_len; 7419 uint8_t format; 7420 7421 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7422 7423 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7424 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7425 format = ccb10->format; 7426 alloc_len = scsi_2btoul(ccb10->alloc_length); 7427 data_len = sizeof(*data10); 7428 } else { 7429 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7430 format = ccb12->format; 7431 alloc_len = scsi_4btoul(ccb12->alloc_length); 7432 data_len = sizeof(*data12); 7433 } 7434 if (alloc_len == 0) { 7435 ctl_set_success(ctsio); 7436 ctl_done((union ctl_io *)ctsio); 7437 return (CTL_RETVAL_COMPLETE); 7438 } 7439 7440 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7441 if (data_len < alloc_len) { 7442 ctsio->residual = alloc_len - data_len; 7443 ctsio->kern_data_len = data_len; 7444 ctsio->kern_total_len = data_len; 7445 } else { 7446 ctsio->residual = 0; 7447 ctsio->kern_data_len = alloc_len; 7448 ctsio->kern_total_len = alloc_len; 7449 } 7450 ctsio->kern_data_resid = 0; 7451 ctsio->kern_rel_offset = 0; 7452 ctsio->kern_sg_entries = 0; 7453 7454 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7455 data10 = (struct scsi_read_defect_data_hdr_10 *) 7456 ctsio->kern_data_ptr; 7457 data10->format = format; 7458 scsi_ulto2b(0, data10->length); 7459 } else { 7460 data12 = (struct scsi_read_defect_data_hdr_12 *) 7461 ctsio->kern_data_ptr; 7462 data12->format = format; 7463 scsi_ulto2b(0, data12->generation); 7464 scsi_ulto4b(0, data12->length); 7465 } 7466 7467 ctsio->scsi_status = SCSI_STATUS_OK; 7468 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7469 ctsio->be_move_done = ctl_config_move_done; 7470 ctl_datamove((union ctl_io *)ctsio); 7471 return (CTL_RETVAL_COMPLETE); 7472} 7473 7474int 7475ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7476{ 7477 struct scsi_maintenance_in *cdb; 7478 int retval; 7479 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 7480 int num_target_port_groups, num_target_ports; 7481 struct ctl_lun *lun; 7482 struct ctl_softc *softc; 7483 struct ctl_port *port; 7484 struct scsi_target_group_data *rtg_ptr; 7485 struct scsi_target_group_data_extended *rtg_ext_ptr; 7486 struct scsi_target_port_group_descriptor *tpg_desc; 7487 7488 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7489 7490 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7491 softc = control_softc; 7492 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7493 7494 retval = CTL_RETVAL_COMPLETE; 7495 7496 switch (cdb->byte2 & STG_PDF_MASK) { 7497 case STG_PDF_LENGTH: 7498 ext = 0; 7499 break; 7500 case STG_PDF_EXTENDED: 7501 ext = 1; 7502 break; 7503 default: 7504 ctl_set_invalid_field(/*ctsio*/ ctsio, 7505 /*sks_valid*/ 1, 7506 /*command*/ 1, 7507 /*field*/ 2, 7508 /*bit_valid*/ 1, 7509 /*bit*/ 5); 7510 ctl_done((union ctl_io *)ctsio); 7511 return(retval); 7512 } 7513 7514 if (softc->is_single) 7515 num_target_port_groups = 1; 7516 else 7517 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7518 num_target_ports = 0; 7519 mtx_lock(&softc->ctl_lock); 7520 STAILQ_FOREACH(port, &softc->port_list, links) { 7521 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7522 continue; 7523 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7524 continue; 7525 num_target_ports++; 7526 } 7527 mtx_unlock(&softc->ctl_lock); 7528 7529 if (ext) 7530 total_len = sizeof(struct scsi_target_group_data_extended); 7531 else 7532 total_len = sizeof(struct scsi_target_group_data); 7533 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7534 num_target_port_groups + 7535 sizeof(struct scsi_target_port_descriptor) * 7536 num_target_ports * num_target_port_groups; 7537 7538 alloc_len = scsi_4btoul(cdb->length); 7539 7540 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7541 7542 ctsio->kern_sg_entries = 0; 7543 7544 if (total_len < alloc_len) { 7545 ctsio->residual = alloc_len - total_len; 7546 ctsio->kern_data_len = total_len; 7547 ctsio->kern_total_len = total_len; 7548 } else { 7549 ctsio->residual = 0; 7550 ctsio->kern_data_len = alloc_len; 7551 ctsio->kern_total_len = alloc_len; 7552 } 7553 ctsio->kern_data_resid = 0; 7554 ctsio->kern_rel_offset = 0; 7555 7556 if (ext) { 7557 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7558 ctsio->kern_data_ptr; 7559 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7560 rtg_ext_ptr->format_type = 0x10; 7561 rtg_ext_ptr->implicit_transition_time = 0; 7562 tpg_desc = &rtg_ext_ptr->groups[0]; 7563 } else { 7564 rtg_ptr = (struct scsi_target_group_data *) 7565 ctsio->kern_data_ptr; 7566 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7567 tpg_desc = &rtg_ptr->groups[0]; 7568 } 7569 7570 mtx_lock(&softc->ctl_lock); 7571 pg = softc->port_offset / CTL_MAX_PORTS; 7572 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 7573 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 7574 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7575 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7576 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 7577 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7578 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7579 } else { 7580 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7581 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7582 } 7583 } else { 7584 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7585 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7586 } 7587 for (g = 0; g < num_target_port_groups; g++) { 7588 tpg_desc->pref_state = (g == pg) ? gs : os; 7589 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 7590 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7591 tpg_desc->status = TPG_IMPLICIT; 7592 pc = 0; 7593 STAILQ_FOREACH(port, &softc->port_list, links) { 7594 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7595 continue; 7596 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7597 CTL_MAX_LUNS) 7598 continue; 7599 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7600 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7601 relative_target_port_identifier); 7602 pc++; 7603 } 7604 tpg_desc->target_port_count = pc; 7605 tpg_desc = (struct scsi_target_port_group_descriptor *) 7606 &tpg_desc->descriptors[pc]; 7607 } 7608 mtx_unlock(&softc->ctl_lock); 7609 7610 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7611 ctsio->be_move_done = ctl_config_move_done; 7612 7613 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7614 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7615 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7616 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7617 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7618 7619 ctl_datamove((union ctl_io *)ctsio); 7620 return(retval); 7621} 7622 7623int 7624ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7625{ 7626 struct ctl_lun *lun; 7627 struct scsi_report_supported_opcodes *cdb; 7628 const struct ctl_cmd_entry *entry, *sentry; 7629 struct scsi_report_supported_opcodes_all *all; 7630 struct scsi_report_supported_opcodes_descr *descr; 7631 struct scsi_report_supported_opcodes_one *one; 7632 int retval; 7633 int alloc_len, total_len; 7634 int opcode, service_action, i, j, num; 7635 7636 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7637 7638 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7639 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7640 7641 retval = CTL_RETVAL_COMPLETE; 7642 7643 opcode = cdb->requested_opcode; 7644 service_action = scsi_2btoul(cdb->requested_service_action); 7645 switch (cdb->options & RSO_OPTIONS_MASK) { 7646 case RSO_OPTIONS_ALL: 7647 num = 0; 7648 for (i = 0; i < 256; i++) { 7649 entry = &ctl_cmd_table[i]; 7650 if (entry->flags & CTL_CMD_FLAG_SA5) { 7651 for (j = 0; j < 32; j++) { 7652 sentry = &((const struct ctl_cmd_entry *) 7653 entry->execute)[j]; 7654 if (ctl_cmd_applicable( 7655 lun->be_lun->lun_type, sentry)) 7656 num++; 7657 } 7658 } else { 7659 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7660 entry)) 7661 num++; 7662 } 7663 } 7664 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7665 num * sizeof(struct scsi_report_supported_opcodes_descr); 7666 break; 7667 case RSO_OPTIONS_OC: 7668 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7669 ctl_set_invalid_field(/*ctsio*/ ctsio, 7670 /*sks_valid*/ 1, 7671 /*command*/ 1, 7672 /*field*/ 2, 7673 /*bit_valid*/ 1, 7674 /*bit*/ 2); 7675 ctl_done((union ctl_io *)ctsio); 7676 return (CTL_RETVAL_COMPLETE); 7677 } 7678 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7679 break; 7680 case RSO_OPTIONS_OC_SA: 7681 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7682 service_action >= 32) { 7683 ctl_set_invalid_field(/*ctsio*/ ctsio, 7684 /*sks_valid*/ 1, 7685 /*command*/ 1, 7686 /*field*/ 2, 7687 /*bit_valid*/ 1, 7688 /*bit*/ 2); 7689 ctl_done((union ctl_io *)ctsio); 7690 return (CTL_RETVAL_COMPLETE); 7691 } 7692 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7693 break; 7694 default: 7695 ctl_set_invalid_field(/*ctsio*/ ctsio, 7696 /*sks_valid*/ 1, 7697 /*command*/ 1, 7698 /*field*/ 2, 7699 /*bit_valid*/ 1, 7700 /*bit*/ 2); 7701 ctl_done((union ctl_io *)ctsio); 7702 return (CTL_RETVAL_COMPLETE); 7703 } 7704 7705 alloc_len = scsi_4btoul(cdb->length); 7706 7707 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7708 7709 ctsio->kern_sg_entries = 0; 7710 7711 if (total_len < alloc_len) { 7712 ctsio->residual = alloc_len - total_len; 7713 ctsio->kern_data_len = total_len; 7714 ctsio->kern_total_len = total_len; 7715 } else { 7716 ctsio->residual = 0; 7717 ctsio->kern_data_len = alloc_len; 7718 ctsio->kern_total_len = alloc_len; 7719 } 7720 ctsio->kern_data_resid = 0; 7721 ctsio->kern_rel_offset = 0; 7722 7723 switch (cdb->options & RSO_OPTIONS_MASK) { 7724 case RSO_OPTIONS_ALL: 7725 all = (struct scsi_report_supported_opcodes_all *) 7726 ctsio->kern_data_ptr; 7727 num = 0; 7728 for (i = 0; i < 256; i++) { 7729 entry = &ctl_cmd_table[i]; 7730 if (entry->flags & CTL_CMD_FLAG_SA5) { 7731 for (j = 0; j < 32; j++) { 7732 sentry = &((const struct ctl_cmd_entry *) 7733 entry->execute)[j]; 7734 if (!ctl_cmd_applicable( 7735 lun->be_lun->lun_type, sentry)) 7736 continue; 7737 descr = &all->descr[num++]; 7738 descr->opcode = i; 7739 scsi_ulto2b(j, descr->service_action); 7740 descr->flags = RSO_SERVACTV; 7741 scsi_ulto2b(sentry->length, 7742 descr->cdb_length); 7743 } 7744 } else { 7745 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7746 entry)) 7747 continue; 7748 descr = &all->descr[num++]; 7749 descr->opcode = i; 7750 scsi_ulto2b(0, descr->service_action); 7751 descr->flags = 0; 7752 scsi_ulto2b(entry->length, descr->cdb_length); 7753 } 7754 } 7755 scsi_ulto4b( 7756 num * sizeof(struct scsi_report_supported_opcodes_descr), 7757 all->length); 7758 break; 7759 case RSO_OPTIONS_OC: 7760 one = (struct scsi_report_supported_opcodes_one *) 7761 ctsio->kern_data_ptr; 7762 entry = &ctl_cmd_table[opcode]; 7763 goto fill_one; 7764 case RSO_OPTIONS_OC_SA: 7765 one = (struct scsi_report_supported_opcodes_one *) 7766 ctsio->kern_data_ptr; 7767 entry = &ctl_cmd_table[opcode]; 7768 entry = &((const struct ctl_cmd_entry *) 7769 entry->execute)[service_action]; 7770fill_one: 7771 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7772 one->support = 3; 7773 scsi_ulto2b(entry->length, one->cdb_length); 7774 one->cdb_usage[0] = opcode; 7775 memcpy(&one->cdb_usage[1], entry->usage, 7776 entry->length - 1); 7777 } else 7778 one->support = 1; 7779 break; 7780 } 7781 7782 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7783 ctsio->be_move_done = ctl_config_move_done; 7784 7785 ctl_datamove((union ctl_io *)ctsio); 7786 return(retval); 7787} 7788 7789int 7790ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7791{ 7792 struct scsi_report_supported_tmf *cdb; 7793 struct scsi_report_supported_tmf_data *data; 7794 int retval; 7795 int alloc_len, total_len; 7796 7797 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7798 7799 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7800 7801 retval = CTL_RETVAL_COMPLETE; 7802 7803 total_len = sizeof(struct scsi_report_supported_tmf_data); 7804 alloc_len = scsi_4btoul(cdb->length); 7805 7806 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7807 7808 ctsio->kern_sg_entries = 0; 7809 7810 if (total_len < alloc_len) { 7811 ctsio->residual = alloc_len - total_len; 7812 ctsio->kern_data_len = total_len; 7813 ctsio->kern_total_len = total_len; 7814 } else { 7815 ctsio->residual = 0; 7816 ctsio->kern_data_len = alloc_len; 7817 ctsio->kern_total_len = alloc_len; 7818 } 7819 ctsio->kern_data_resid = 0; 7820 ctsio->kern_rel_offset = 0; 7821 7822 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7823 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7824 data->byte2 |= RST_ITNRS; 7825 7826 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7827 ctsio->be_move_done = ctl_config_move_done; 7828 7829 ctl_datamove((union ctl_io *)ctsio); 7830 return (retval); 7831} 7832 7833int 7834ctl_report_timestamp(struct ctl_scsiio *ctsio) 7835{ 7836 struct scsi_report_timestamp *cdb; 7837 struct scsi_report_timestamp_data *data; 7838 struct timeval tv; 7839 int64_t timestamp; 7840 int retval; 7841 int alloc_len, total_len; 7842 7843 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7844 7845 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7846 7847 retval = CTL_RETVAL_COMPLETE; 7848 7849 total_len = sizeof(struct scsi_report_timestamp_data); 7850 alloc_len = scsi_4btoul(cdb->length); 7851 7852 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7853 7854 ctsio->kern_sg_entries = 0; 7855 7856 if (total_len < alloc_len) { 7857 ctsio->residual = alloc_len - total_len; 7858 ctsio->kern_data_len = total_len; 7859 ctsio->kern_total_len = total_len; 7860 } else { 7861 ctsio->residual = 0; 7862 ctsio->kern_data_len = alloc_len; 7863 ctsio->kern_total_len = alloc_len; 7864 } 7865 ctsio->kern_data_resid = 0; 7866 ctsio->kern_rel_offset = 0; 7867 7868 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7869 scsi_ulto2b(sizeof(*data) - 2, data->length); 7870 data->origin = RTS_ORIG_OUTSIDE; 7871 getmicrotime(&tv); 7872 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7873 scsi_ulto4b(timestamp >> 16, data->timestamp); 7874 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7875 7876 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7877 ctsio->be_move_done = ctl_config_move_done; 7878 7879 ctl_datamove((union ctl_io *)ctsio); 7880 return (retval); 7881} 7882 7883int 7884ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7885{ 7886 struct scsi_per_res_in *cdb; 7887 int alloc_len, total_len = 0; 7888 /* struct scsi_per_res_in_rsrv in_data; */ 7889 struct ctl_lun *lun; 7890 struct ctl_softc *softc; 7891 7892 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7893 7894 softc = control_softc; 7895 7896 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7897 7898 alloc_len = scsi_2btoul(cdb->length); 7899 7900 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7901 7902retry: 7903 mtx_lock(&lun->lun_lock); 7904 switch (cdb->action) { 7905 case SPRI_RK: /* read keys */ 7906 total_len = sizeof(struct scsi_per_res_in_keys) + 7907 lun->pr_key_count * 7908 sizeof(struct scsi_per_res_key); 7909 break; 7910 case SPRI_RR: /* read reservation */ 7911 if (lun->flags & CTL_LUN_PR_RESERVED) 7912 total_len = sizeof(struct scsi_per_res_in_rsrv); 7913 else 7914 total_len = sizeof(struct scsi_per_res_in_header); 7915 break; 7916 case SPRI_RC: /* report capabilities */ 7917 total_len = sizeof(struct scsi_per_res_cap); 7918 break; 7919 case SPRI_RS: /* read full status */ 7920 total_len = sizeof(struct scsi_per_res_in_header) + 7921 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7922 lun->pr_key_count; 7923 break; 7924 default: 7925 panic("Invalid PR type %x", cdb->action); 7926 } 7927 mtx_unlock(&lun->lun_lock); 7928 7929 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7930 7931 if (total_len < alloc_len) { 7932 ctsio->residual = alloc_len - total_len; 7933 ctsio->kern_data_len = total_len; 7934 ctsio->kern_total_len = total_len; 7935 } else { 7936 ctsio->residual = 0; 7937 ctsio->kern_data_len = alloc_len; 7938 ctsio->kern_total_len = alloc_len; 7939 } 7940 7941 ctsio->kern_data_resid = 0; 7942 ctsio->kern_rel_offset = 0; 7943 ctsio->kern_sg_entries = 0; 7944 7945 mtx_lock(&lun->lun_lock); 7946 switch (cdb->action) { 7947 case SPRI_RK: { // read keys 7948 struct scsi_per_res_in_keys *res_keys; 7949 int i, key_count; 7950 7951 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7952 7953 /* 7954 * We had to drop the lock to allocate our buffer, which 7955 * leaves time for someone to come in with another 7956 * persistent reservation. (That is unlikely, though, 7957 * since this should be the only persistent reservation 7958 * command active right now.) 7959 */ 7960 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7961 (lun->pr_key_count * 7962 sizeof(struct scsi_per_res_key)))){ 7963 mtx_unlock(&lun->lun_lock); 7964 free(ctsio->kern_data_ptr, M_CTL); 7965 printf("%s: reservation length changed, retrying\n", 7966 __func__); 7967 goto retry; 7968 } 7969 7970 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7971 7972 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7973 lun->pr_key_count, res_keys->header.length); 7974 7975 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7976 if (lun->pr_keys[i] == 0) 7977 continue; 7978 7979 /* 7980 * We used lun->pr_key_count to calculate the 7981 * size to allocate. If it turns out the number of 7982 * initiators with the registered flag set is 7983 * larger than that (i.e. they haven't been kept in 7984 * sync), we've got a problem. 7985 */ 7986 if (key_count >= lun->pr_key_count) { 7987#ifdef NEEDTOPORT 7988 csevent_log(CSC_CTL | CSC_SHELF_SW | 7989 CTL_PR_ERROR, 7990 csevent_LogType_Fault, 7991 csevent_AlertLevel_Yellow, 7992 csevent_FRU_ShelfController, 7993 csevent_FRU_Firmware, 7994 csevent_FRU_Unknown, 7995 "registered keys %d >= key " 7996 "count %d", key_count, 7997 lun->pr_key_count); 7998#endif 7999 key_count++; 8000 continue; 8001 } 8002 scsi_u64to8b(lun->pr_keys[i], 8003 res_keys->keys[key_count].key); 8004 key_count++; 8005 } 8006 break; 8007 } 8008 case SPRI_RR: { // read reservation 8009 struct scsi_per_res_in_rsrv *res; 8010 int tmp_len, header_only; 8011 8012 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 8013 8014 scsi_ulto4b(lun->PRGeneration, res->header.generation); 8015 8016 if (lun->flags & CTL_LUN_PR_RESERVED) 8017 { 8018 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 8019 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 8020 res->header.length); 8021 header_only = 0; 8022 } else { 8023 tmp_len = sizeof(struct scsi_per_res_in_header); 8024 scsi_ulto4b(0, res->header.length); 8025 header_only = 1; 8026 } 8027 8028 /* 8029 * We had to drop the lock to allocate our buffer, which 8030 * leaves time for someone to come in with another 8031 * persistent reservation. (That is unlikely, though, 8032 * since this should be the only persistent reservation 8033 * command active right now.) 8034 */ 8035 if (tmp_len != total_len) { 8036 mtx_unlock(&lun->lun_lock); 8037 free(ctsio->kern_data_ptr, M_CTL); 8038 printf("%s: reservation status changed, retrying\n", 8039 __func__); 8040 goto retry; 8041 } 8042 8043 /* 8044 * No reservation held, so we're done. 8045 */ 8046 if (header_only != 0) 8047 break; 8048 8049 /* 8050 * If the registration is an All Registrants type, the key 8051 * is 0, since it doesn't really matter. 8052 */ 8053 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8054 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx], 8055 res->data.reservation); 8056 } 8057 res->data.scopetype = lun->res_type; 8058 break; 8059 } 8060 case SPRI_RC: //report capabilities 8061 { 8062 struct scsi_per_res_cap *res_cap; 8063 uint16_t type_mask; 8064 8065 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 8066 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 8067 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 8068 type_mask = SPRI_TM_WR_EX_AR | 8069 SPRI_TM_EX_AC_RO | 8070 SPRI_TM_WR_EX_RO | 8071 SPRI_TM_EX_AC | 8072 SPRI_TM_WR_EX | 8073 SPRI_TM_EX_AC_AR; 8074 scsi_ulto2b(type_mask, res_cap->type_mask); 8075 break; 8076 } 8077 case SPRI_RS: { // read full status 8078 struct scsi_per_res_in_full *res_status; 8079 struct scsi_per_res_in_full_desc *res_desc; 8080 struct ctl_port *port; 8081 int i, len; 8082 8083 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 8084 8085 /* 8086 * We had to drop the lock to allocate our buffer, which 8087 * leaves time for someone to come in with another 8088 * persistent reservation. (That is unlikely, though, 8089 * since this should be the only persistent reservation 8090 * command active right now.) 8091 */ 8092 if (total_len < (sizeof(struct scsi_per_res_in_header) + 8093 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 8094 lun->pr_key_count)){ 8095 mtx_unlock(&lun->lun_lock); 8096 free(ctsio->kern_data_ptr, M_CTL); 8097 printf("%s: reservation length changed, retrying\n", 8098 __func__); 8099 goto retry; 8100 } 8101 8102 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 8103 8104 res_desc = &res_status->desc[0]; 8105 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 8106 if (lun->pr_keys[i] == 0) 8107 continue; 8108 8109 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key); 8110 if ((lun->flags & CTL_LUN_PR_RESERVED) && 8111 (lun->pr_res_idx == i || 8112 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 8113 res_desc->flags = SPRI_FULL_R_HOLDER; 8114 res_desc->scopetype = lun->res_type; 8115 } 8116 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 8117 res_desc->rel_trgt_port_id); 8118 len = 0; 8119 port = softc->ctl_ports[ 8120 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 8121 if (port != NULL) 8122 len = ctl_create_iid(port, 8123 i % CTL_MAX_INIT_PER_PORT, 8124 res_desc->transport_id); 8125 scsi_ulto4b(len, res_desc->additional_length); 8126 res_desc = (struct scsi_per_res_in_full_desc *) 8127 &res_desc->transport_id[len]; 8128 } 8129 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 8130 res_status->header.length); 8131 break; 8132 } 8133 default: 8134 /* 8135 * This is a bug, because we just checked for this above, 8136 * and should have returned an error. 8137 */ 8138 panic("Invalid PR type %x", cdb->action); 8139 break; /* NOTREACHED */ 8140 } 8141 mtx_unlock(&lun->lun_lock); 8142 8143 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8144 ctsio->be_move_done = ctl_config_move_done; 8145 8146 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 8147 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 8148 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 8149 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 8150 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 8151 8152 ctl_datamove((union ctl_io *)ctsio); 8153 8154 return (CTL_RETVAL_COMPLETE); 8155} 8156 8157/* 8158 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 8159 * it should return. 8160 */ 8161static int 8162ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 8163 uint64_t sa_res_key, uint8_t type, uint32_t residx, 8164 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 8165 struct scsi_per_res_out_parms* param) 8166{ 8167 union ctl_ha_msg persis_io; 8168 int retval, i; 8169 int isc_retval; 8170 8171 retval = 0; 8172 8173 mtx_lock(&lun->lun_lock); 8174 if (sa_res_key == 0) { 8175 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8176 /* validate scope and type */ 8177 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8178 SPR_LU_SCOPE) { 8179 mtx_unlock(&lun->lun_lock); 8180 ctl_set_invalid_field(/*ctsio*/ ctsio, 8181 /*sks_valid*/ 1, 8182 /*command*/ 1, 8183 /*field*/ 2, 8184 /*bit_valid*/ 1, 8185 /*bit*/ 4); 8186 ctl_done((union ctl_io *)ctsio); 8187 return (1); 8188 } 8189 8190 if (type>8 || type==2 || type==4 || type==0) { 8191 mtx_unlock(&lun->lun_lock); 8192 ctl_set_invalid_field(/*ctsio*/ ctsio, 8193 /*sks_valid*/ 1, 8194 /*command*/ 1, 8195 /*field*/ 2, 8196 /*bit_valid*/ 1, 8197 /*bit*/ 0); 8198 ctl_done((union ctl_io *)ctsio); 8199 return (1); 8200 } 8201 8202 /* 8203 * Unregister everybody else and build UA for 8204 * them 8205 */ 8206 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8207 if (i == residx || lun->pr_keys[i] == 0) 8208 continue; 8209 8210 if (!persis_offset 8211 && i <CTL_MAX_INITIATORS) 8212 lun->pending_ua[i] |= 8213 CTL_UA_REG_PREEMPT; 8214 else if (persis_offset 8215 && i >= persis_offset) 8216 lun->pending_ua[i-persis_offset] |= 8217 CTL_UA_REG_PREEMPT; 8218 lun->pr_keys[i] = 0; 8219 } 8220 lun->pr_key_count = 1; 8221 lun->res_type = type; 8222 if (lun->res_type != SPR_TYPE_WR_EX_AR 8223 && lun->res_type != SPR_TYPE_EX_AC_AR) 8224 lun->pr_res_idx = residx; 8225 8226 /* send msg to other side */ 8227 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8228 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8229 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8230 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8231 persis_io.pr.pr_info.res_type = type; 8232 memcpy(persis_io.pr.pr_info.sa_res_key, 8233 param->serv_act_res_key, 8234 sizeof(param->serv_act_res_key)); 8235 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8236 &persis_io, sizeof(persis_io), 0)) > 8237 CTL_HA_STATUS_SUCCESS) { 8238 printf("CTL:Persis Out error returned " 8239 "from ctl_ha_msg_send %d\n", 8240 isc_retval); 8241 } 8242 } else { 8243 /* not all registrants */ 8244 mtx_unlock(&lun->lun_lock); 8245 free(ctsio->kern_data_ptr, M_CTL); 8246 ctl_set_invalid_field(ctsio, 8247 /*sks_valid*/ 1, 8248 /*command*/ 0, 8249 /*field*/ 8, 8250 /*bit_valid*/ 0, 8251 /*bit*/ 0); 8252 ctl_done((union ctl_io *)ctsio); 8253 return (1); 8254 } 8255 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8256 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8257 int found = 0; 8258 8259 if (res_key == sa_res_key) { 8260 /* special case */ 8261 /* 8262 * The spec implies this is not good but doesn't 8263 * say what to do. There are two choices either 8264 * generate a res conflict or check condition 8265 * with illegal field in parameter data. Since 8266 * that is what is done when the sa_res_key is 8267 * zero I'll take that approach since this has 8268 * to do with the sa_res_key. 8269 */ 8270 mtx_unlock(&lun->lun_lock); 8271 free(ctsio->kern_data_ptr, M_CTL); 8272 ctl_set_invalid_field(ctsio, 8273 /*sks_valid*/ 1, 8274 /*command*/ 0, 8275 /*field*/ 8, 8276 /*bit_valid*/ 0, 8277 /*bit*/ 0); 8278 ctl_done((union ctl_io *)ctsio); 8279 return (1); 8280 } 8281 8282 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8283 if (lun->pr_keys[i] != sa_res_key) 8284 continue; 8285 8286 found = 1; 8287 lun->pr_keys[i] = 0; 8288 lun->pr_key_count--; 8289 8290 if (!persis_offset && i < CTL_MAX_INITIATORS) 8291 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8292 else if (persis_offset && i >= persis_offset) 8293 lun->pending_ua[i-persis_offset] |= 8294 CTL_UA_REG_PREEMPT; 8295 } 8296 if (!found) { 8297 mtx_unlock(&lun->lun_lock); 8298 free(ctsio->kern_data_ptr, M_CTL); 8299 ctl_set_reservation_conflict(ctsio); 8300 ctl_done((union ctl_io *)ctsio); 8301 return (CTL_RETVAL_COMPLETE); 8302 } 8303 /* send msg to other side */ 8304 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8305 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8306 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8307 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8308 persis_io.pr.pr_info.res_type = type; 8309 memcpy(persis_io.pr.pr_info.sa_res_key, 8310 param->serv_act_res_key, 8311 sizeof(param->serv_act_res_key)); 8312 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8313 &persis_io, sizeof(persis_io), 0)) > 8314 CTL_HA_STATUS_SUCCESS) { 8315 printf("CTL:Persis Out error returned from " 8316 "ctl_ha_msg_send %d\n", isc_retval); 8317 } 8318 } else { 8319 /* Reserved but not all registrants */ 8320 /* sa_res_key is res holder */ 8321 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) { 8322 /* validate scope and type */ 8323 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8324 SPR_LU_SCOPE) { 8325 mtx_unlock(&lun->lun_lock); 8326 ctl_set_invalid_field(/*ctsio*/ ctsio, 8327 /*sks_valid*/ 1, 8328 /*command*/ 1, 8329 /*field*/ 2, 8330 /*bit_valid*/ 1, 8331 /*bit*/ 4); 8332 ctl_done((union ctl_io *)ctsio); 8333 return (1); 8334 } 8335 8336 if (type>8 || type==2 || type==4 || type==0) { 8337 mtx_unlock(&lun->lun_lock); 8338 ctl_set_invalid_field(/*ctsio*/ ctsio, 8339 /*sks_valid*/ 1, 8340 /*command*/ 1, 8341 /*field*/ 2, 8342 /*bit_valid*/ 1, 8343 /*bit*/ 0); 8344 ctl_done((union ctl_io *)ctsio); 8345 return (1); 8346 } 8347 8348 /* 8349 * Do the following: 8350 * if sa_res_key != res_key remove all 8351 * registrants w/sa_res_key and generate UA 8352 * for these registrants(Registrations 8353 * Preempted) if it wasn't an exclusive 8354 * reservation generate UA(Reservations 8355 * Preempted) for all other registered nexuses 8356 * if the type has changed. Establish the new 8357 * reservation and holder. If res_key and 8358 * sa_res_key are the same do the above 8359 * except don't unregister the res holder. 8360 */ 8361 8362 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8363 if (i == residx || lun->pr_keys[i] == 0) 8364 continue; 8365 8366 if (sa_res_key == lun->pr_keys[i]) { 8367 lun->pr_keys[i] = 0; 8368 lun->pr_key_count--; 8369 8370 if (!persis_offset 8371 && i < CTL_MAX_INITIATORS) 8372 lun->pending_ua[i] |= 8373 CTL_UA_REG_PREEMPT; 8374 else if (persis_offset 8375 && i >= persis_offset) 8376 lun->pending_ua[i-persis_offset] |= 8377 CTL_UA_REG_PREEMPT; 8378 } else if (type != lun->res_type 8379 && (lun->res_type == SPR_TYPE_WR_EX_RO 8380 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8381 if (!persis_offset 8382 && i < CTL_MAX_INITIATORS) 8383 lun->pending_ua[i] |= 8384 CTL_UA_RES_RELEASE; 8385 else if (persis_offset 8386 && i >= persis_offset) 8387 lun->pending_ua[ 8388 i-persis_offset] |= 8389 CTL_UA_RES_RELEASE; 8390 } 8391 } 8392 lun->res_type = type; 8393 if (lun->res_type != SPR_TYPE_WR_EX_AR 8394 && lun->res_type != SPR_TYPE_EX_AC_AR) 8395 lun->pr_res_idx = residx; 8396 else 8397 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8398 8399 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8400 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8401 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8402 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8403 persis_io.pr.pr_info.res_type = type; 8404 memcpy(persis_io.pr.pr_info.sa_res_key, 8405 param->serv_act_res_key, 8406 sizeof(param->serv_act_res_key)); 8407 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8408 &persis_io, sizeof(persis_io), 0)) > 8409 CTL_HA_STATUS_SUCCESS) { 8410 printf("CTL:Persis Out error returned " 8411 "from ctl_ha_msg_send %d\n", 8412 isc_retval); 8413 } 8414 } else { 8415 /* 8416 * sa_res_key is not the res holder just 8417 * remove registrants 8418 */ 8419 int found=0; 8420 8421 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8422 if (sa_res_key != lun->pr_keys[i]) 8423 continue; 8424 8425 found = 1; 8426 lun->pr_keys[i] = 0; 8427 lun->pr_key_count--; 8428 8429 if (!persis_offset 8430 && i < CTL_MAX_INITIATORS) 8431 lun->pending_ua[i] |= 8432 CTL_UA_REG_PREEMPT; 8433 else if (persis_offset 8434 && i >= persis_offset) 8435 lun->pending_ua[i-persis_offset] |= 8436 CTL_UA_REG_PREEMPT; 8437 } 8438 8439 if (!found) { 8440 mtx_unlock(&lun->lun_lock); 8441 free(ctsio->kern_data_ptr, M_CTL); 8442 ctl_set_reservation_conflict(ctsio); 8443 ctl_done((union ctl_io *)ctsio); 8444 return (1); 8445 } 8446 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8447 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8448 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8449 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8450 persis_io.pr.pr_info.res_type = type; 8451 memcpy(persis_io.pr.pr_info.sa_res_key, 8452 param->serv_act_res_key, 8453 sizeof(param->serv_act_res_key)); 8454 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8455 &persis_io, sizeof(persis_io), 0)) > 8456 CTL_HA_STATUS_SUCCESS) { 8457 printf("CTL:Persis Out error returned " 8458 "from ctl_ha_msg_send %d\n", 8459 isc_retval); 8460 } 8461 } 8462 } 8463 8464 lun->PRGeneration++; 8465 mtx_unlock(&lun->lun_lock); 8466 8467 return (retval); 8468} 8469 8470static void 8471ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8472{ 8473 uint64_t sa_res_key; 8474 int i; 8475 8476 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8477 8478 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8479 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8480 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) { 8481 if (sa_res_key == 0) { 8482 /* 8483 * Unregister everybody else and build UA for 8484 * them 8485 */ 8486 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8487 if (i == msg->pr.pr_info.residx || 8488 lun->pr_keys[i] == 0) 8489 continue; 8490 8491 if (!persis_offset 8492 && i < CTL_MAX_INITIATORS) 8493 lun->pending_ua[i] |= 8494 CTL_UA_REG_PREEMPT; 8495 else if (persis_offset && i >= persis_offset) 8496 lun->pending_ua[i - persis_offset] |= 8497 CTL_UA_REG_PREEMPT; 8498 lun->pr_keys[i] = 0; 8499 } 8500 8501 lun->pr_key_count = 1; 8502 lun->res_type = msg->pr.pr_info.res_type; 8503 if (lun->res_type != SPR_TYPE_WR_EX_AR 8504 && lun->res_type != SPR_TYPE_EX_AC_AR) 8505 lun->pr_res_idx = msg->pr.pr_info.residx; 8506 } else { 8507 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8508 if (sa_res_key == lun->pr_keys[i]) 8509 continue; 8510 8511 lun->pr_keys[i] = 0; 8512 lun->pr_key_count--; 8513 8514 if (!persis_offset 8515 && i < persis_offset) 8516 lun->pending_ua[i] |= 8517 CTL_UA_REG_PREEMPT; 8518 else if (persis_offset 8519 && i >= persis_offset) 8520 lun->pending_ua[i - persis_offset] |= 8521 CTL_UA_REG_PREEMPT; 8522 } 8523 } 8524 } else { 8525 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8526 if (i == msg->pr.pr_info.residx || 8527 lun->pr_keys[i] == 0) 8528 continue; 8529 8530 if (sa_res_key == lun->pr_keys[i]) { 8531 lun->pr_keys[i] = 0; 8532 lun->pr_key_count--; 8533 if (!persis_offset 8534 && i < CTL_MAX_INITIATORS) 8535 lun->pending_ua[i] |= 8536 CTL_UA_REG_PREEMPT; 8537 else if (persis_offset 8538 && i >= persis_offset) 8539 lun->pending_ua[i - persis_offset] |= 8540 CTL_UA_REG_PREEMPT; 8541 } else if (msg->pr.pr_info.res_type != lun->res_type 8542 && (lun->res_type == SPR_TYPE_WR_EX_RO 8543 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8544 if (!persis_offset 8545 && i < persis_offset) 8546 lun->pending_ua[i] |= 8547 CTL_UA_RES_RELEASE; 8548 else if (persis_offset 8549 && i >= persis_offset) 8550 lun->pending_ua[i - persis_offset] |= 8551 CTL_UA_RES_RELEASE; 8552 } 8553 } 8554 lun->res_type = msg->pr.pr_info.res_type; 8555 if (lun->res_type != SPR_TYPE_WR_EX_AR 8556 && lun->res_type != SPR_TYPE_EX_AC_AR) 8557 lun->pr_res_idx = msg->pr.pr_info.residx; 8558 else 8559 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8560 } 8561 lun->PRGeneration++; 8562 8563} 8564 8565 8566int 8567ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8568{ 8569 int retval; 8570 int isc_retval; 8571 u_int32_t param_len; 8572 struct scsi_per_res_out *cdb; 8573 struct ctl_lun *lun; 8574 struct scsi_per_res_out_parms* param; 8575 struct ctl_softc *softc; 8576 uint32_t residx; 8577 uint64_t res_key, sa_res_key; 8578 uint8_t type; 8579 union ctl_ha_msg persis_io; 8580 int i; 8581 8582 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8583 8584 retval = CTL_RETVAL_COMPLETE; 8585 8586 softc = control_softc; 8587 8588 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8589 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8590 8591 /* 8592 * We only support whole-LUN scope. The scope & type are ignored for 8593 * register, register and ignore existing key and clear. 8594 * We sometimes ignore scope and type on preempts too!! 8595 * Verify reservation type here as well. 8596 */ 8597 type = cdb->scope_type & SPR_TYPE_MASK; 8598 if ((cdb->action == SPRO_RESERVE) 8599 || (cdb->action == SPRO_RELEASE)) { 8600 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8601 ctl_set_invalid_field(/*ctsio*/ ctsio, 8602 /*sks_valid*/ 1, 8603 /*command*/ 1, 8604 /*field*/ 2, 8605 /*bit_valid*/ 1, 8606 /*bit*/ 4); 8607 ctl_done((union ctl_io *)ctsio); 8608 return (CTL_RETVAL_COMPLETE); 8609 } 8610 8611 if (type>8 || type==2 || type==4 || type==0) { 8612 ctl_set_invalid_field(/*ctsio*/ ctsio, 8613 /*sks_valid*/ 1, 8614 /*command*/ 1, 8615 /*field*/ 2, 8616 /*bit_valid*/ 1, 8617 /*bit*/ 0); 8618 ctl_done((union ctl_io *)ctsio); 8619 return (CTL_RETVAL_COMPLETE); 8620 } 8621 } 8622 8623 param_len = scsi_4btoul(cdb->length); 8624 8625 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8626 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8627 ctsio->kern_data_len = param_len; 8628 ctsio->kern_total_len = param_len; 8629 ctsio->kern_data_resid = 0; 8630 ctsio->kern_rel_offset = 0; 8631 ctsio->kern_sg_entries = 0; 8632 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8633 ctsio->be_move_done = ctl_config_move_done; 8634 ctl_datamove((union ctl_io *)ctsio); 8635 8636 return (CTL_RETVAL_COMPLETE); 8637 } 8638 8639 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8640 8641 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8642 res_key = scsi_8btou64(param->res_key.key); 8643 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8644 8645 /* 8646 * Validate the reservation key here except for SPRO_REG_IGNO 8647 * This must be done for all other service actions 8648 */ 8649 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8650 mtx_lock(&lun->lun_lock); 8651 if (lun->pr_keys[residx] != 0) { 8652 if (res_key != lun->pr_keys[residx]) { 8653 /* 8654 * The current key passed in doesn't match 8655 * the one the initiator previously 8656 * registered. 8657 */ 8658 mtx_unlock(&lun->lun_lock); 8659 free(ctsio->kern_data_ptr, M_CTL); 8660 ctl_set_reservation_conflict(ctsio); 8661 ctl_done((union ctl_io *)ctsio); 8662 return (CTL_RETVAL_COMPLETE); 8663 } 8664 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8665 /* 8666 * We are not registered 8667 */ 8668 mtx_unlock(&lun->lun_lock); 8669 free(ctsio->kern_data_ptr, M_CTL); 8670 ctl_set_reservation_conflict(ctsio); 8671 ctl_done((union ctl_io *)ctsio); 8672 return (CTL_RETVAL_COMPLETE); 8673 } else if (res_key != 0) { 8674 /* 8675 * We are not registered and trying to register but 8676 * the register key isn't zero. 8677 */ 8678 mtx_unlock(&lun->lun_lock); 8679 free(ctsio->kern_data_ptr, M_CTL); 8680 ctl_set_reservation_conflict(ctsio); 8681 ctl_done((union ctl_io *)ctsio); 8682 return (CTL_RETVAL_COMPLETE); 8683 } 8684 mtx_unlock(&lun->lun_lock); 8685 } 8686 8687 switch (cdb->action & SPRO_ACTION_MASK) { 8688 case SPRO_REGISTER: 8689 case SPRO_REG_IGNO: { 8690 8691#if 0 8692 printf("Registration received\n"); 8693#endif 8694 8695 /* 8696 * We don't support any of these options, as we report in 8697 * the read capabilities request (see 8698 * ctl_persistent_reserve_in(), above). 8699 */ 8700 if ((param->flags & SPR_SPEC_I_PT) 8701 || (param->flags & SPR_ALL_TG_PT) 8702 || (param->flags & SPR_APTPL)) { 8703 int bit_ptr; 8704 8705 if (param->flags & SPR_APTPL) 8706 bit_ptr = 0; 8707 else if (param->flags & SPR_ALL_TG_PT) 8708 bit_ptr = 2; 8709 else /* SPR_SPEC_I_PT */ 8710 bit_ptr = 3; 8711 8712 free(ctsio->kern_data_ptr, M_CTL); 8713 ctl_set_invalid_field(ctsio, 8714 /*sks_valid*/ 1, 8715 /*command*/ 0, 8716 /*field*/ 20, 8717 /*bit_valid*/ 1, 8718 /*bit*/ bit_ptr); 8719 ctl_done((union ctl_io *)ctsio); 8720 return (CTL_RETVAL_COMPLETE); 8721 } 8722 8723 mtx_lock(&lun->lun_lock); 8724 8725 /* 8726 * The initiator wants to clear the 8727 * key/unregister. 8728 */ 8729 if (sa_res_key == 0) { 8730 if ((res_key == 0 8731 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8732 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8733 && lun->pr_keys[residx] == 0)) { 8734 mtx_unlock(&lun->lun_lock); 8735 goto done; 8736 } 8737 8738 lun->pr_keys[residx] = 0; 8739 lun->pr_key_count--; 8740 8741 if (residx == lun->pr_res_idx) { 8742 lun->flags &= ~CTL_LUN_PR_RESERVED; 8743 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8744 8745 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8746 || lun->res_type == SPR_TYPE_EX_AC_RO) 8747 && lun->pr_key_count) { 8748 /* 8749 * If the reservation is a registrants 8750 * only type we need to generate a UA 8751 * for other registered inits. The 8752 * sense code should be RESERVATIONS 8753 * RELEASED 8754 */ 8755 8756 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8757 if (lun->pr_keys[ 8758 i + persis_offset] == 0) 8759 continue; 8760 lun->pending_ua[i] |= 8761 CTL_UA_RES_RELEASE; 8762 } 8763 } 8764 lun->res_type = 0; 8765 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8766 if (lun->pr_key_count==0) { 8767 lun->flags &= ~CTL_LUN_PR_RESERVED; 8768 lun->res_type = 0; 8769 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8770 } 8771 } 8772 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8773 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8774 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8775 persis_io.pr.pr_info.residx = residx; 8776 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8777 &persis_io, sizeof(persis_io), 0 )) > 8778 CTL_HA_STATUS_SUCCESS) { 8779 printf("CTL:Persis Out error returned from " 8780 "ctl_ha_msg_send %d\n", isc_retval); 8781 } 8782 } else /* sa_res_key != 0 */ { 8783 8784 /* 8785 * If we aren't registered currently then increment 8786 * the key count and set the registered flag. 8787 */ 8788 if (lun->pr_keys[residx] == 0) 8789 lun->pr_key_count++; 8790 lun->pr_keys[residx] = sa_res_key; 8791 8792 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8793 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8794 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8795 persis_io.pr.pr_info.residx = residx; 8796 memcpy(persis_io.pr.pr_info.sa_res_key, 8797 param->serv_act_res_key, 8798 sizeof(param->serv_act_res_key)); 8799 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8800 &persis_io, sizeof(persis_io), 0)) > 8801 CTL_HA_STATUS_SUCCESS) { 8802 printf("CTL:Persis Out error returned from " 8803 "ctl_ha_msg_send %d\n", isc_retval); 8804 } 8805 } 8806 lun->PRGeneration++; 8807 mtx_unlock(&lun->lun_lock); 8808 8809 break; 8810 } 8811 case SPRO_RESERVE: 8812#if 0 8813 printf("Reserve executed type %d\n", type); 8814#endif 8815 mtx_lock(&lun->lun_lock); 8816 if (lun->flags & CTL_LUN_PR_RESERVED) { 8817 /* 8818 * if this isn't the reservation holder and it's 8819 * not a "all registrants" type or if the type is 8820 * different then we have a conflict 8821 */ 8822 if ((lun->pr_res_idx != residx 8823 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8824 || lun->res_type != type) { 8825 mtx_unlock(&lun->lun_lock); 8826 free(ctsio->kern_data_ptr, M_CTL); 8827 ctl_set_reservation_conflict(ctsio); 8828 ctl_done((union ctl_io *)ctsio); 8829 return (CTL_RETVAL_COMPLETE); 8830 } 8831 mtx_unlock(&lun->lun_lock); 8832 } else /* create a reservation */ { 8833 /* 8834 * If it's not an "all registrants" type record 8835 * reservation holder 8836 */ 8837 if (type != SPR_TYPE_WR_EX_AR 8838 && type != SPR_TYPE_EX_AC_AR) 8839 lun->pr_res_idx = residx; /* Res holder */ 8840 else 8841 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8842 8843 lun->flags |= CTL_LUN_PR_RESERVED; 8844 lun->res_type = type; 8845 8846 mtx_unlock(&lun->lun_lock); 8847 8848 /* send msg to other side */ 8849 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8850 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8851 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8852 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8853 persis_io.pr.pr_info.res_type = type; 8854 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8855 &persis_io, sizeof(persis_io), 0)) > 8856 CTL_HA_STATUS_SUCCESS) { 8857 printf("CTL:Persis Out error returned from " 8858 "ctl_ha_msg_send %d\n", isc_retval); 8859 } 8860 } 8861 break; 8862 8863 case SPRO_RELEASE: 8864 mtx_lock(&lun->lun_lock); 8865 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8866 /* No reservation exists return good status */ 8867 mtx_unlock(&lun->lun_lock); 8868 goto done; 8869 } 8870 /* 8871 * Is this nexus a reservation holder? 8872 */ 8873 if (lun->pr_res_idx != residx 8874 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8875 /* 8876 * not a res holder return good status but 8877 * do nothing 8878 */ 8879 mtx_unlock(&lun->lun_lock); 8880 goto done; 8881 } 8882 8883 if (lun->res_type != type) { 8884 mtx_unlock(&lun->lun_lock); 8885 free(ctsio->kern_data_ptr, M_CTL); 8886 ctl_set_illegal_pr_release(ctsio); 8887 ctl_done((union ctl_io *)ctsio); 8888 return (CTL_RETVAL_COMPLETE); 8889 } 8890 8891 /* okay to release */ 8892 lun->flags &= ~CTL_LUN_PR_RESERVED; 8893 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8894 lun->res_type = 0; 8895 8896 /* 8897 * if this isn't an exclusive access 8898 * res generate UA for all other 8899 * registrants. 8900 */ 8901 if (type != SPR_TYPE_EX_AC 8902 && type != SPR_TYPE_WR_EX) { 8903 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8904 if (i == residx || 8905 lun->pr_keys[i + persis_offset] == 0) 8906 continue; 8907 lun->pending_ua[i] |= CTL_UA_RES_RELEASE; 8908 } 8909 } 8910 mtx_unlock(&lun->lun_lock); 8911 /* Send msg to other side */ 8912 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8913 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8914 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8915 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8916 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8917 printf("CTL:Persis Out error returned from " 8918 "ctl_ha_msg_send %d\n", isc_retval); 8919 } 8920 break; 8921 8922 case SPRO_CLEAR: 8923 /* send msg to other side */ 8924 8925 mtx_lock(&lun->lun_lock); 8926 lun->flags &= ~CTL_LUN_PR_RESERVED; 8927 lun->res_type = 0; 8928 lun->pr_key_count = 0; 8929 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8930 8931 lun->pr_keys[residx] = 0; 8932 8933 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8934 if (lun->pr_keys[i] != 0) { 8935 if (!persis_offset && i < CTL_MAX_INITIATORS) 8936 lun->pending_ua[i] |= 8937 CTL_UA_RES_PREEMPT; 8938 else if (persis_offset && i >= persis_offset) 8939 lun->pending_ua[i-persis_offset] |= 8940 CTL_UA_RES_PREEMPT; 8941 8942 lun->pr_keys[i] = 0; 8943 } 8944 lun->PRGeneration++; 8945 mtx_unlock(&lun->lun_lock); 8946 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8947 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8948 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8949 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8950 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8951 printf("CTL:Persis Out error returned from " 8952 "ctl_ha_msg_send %d\n", isc_retval); 8953 } 8954 break; 8955 8956 case SPRO_PREEMPT: 8957 case SPRO_PRE_ABO: { 8958 int nretval; 8959 8960 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8961 residx, ctsio, cdb, param); 8962 if (nretval != 0) 8963 return (CTL_RETVAL_COMPLETE); 8964 break; 8965 } 8966 default: 8967 panic("Invalid PR type %x", cdb->action); 8968 } 8969 8970done: 8971 free(ctsio->kern_data_ptr, M_CTL); 8972 ctl_set_success(ctsio); 8973 ctl_done((union ctl_io *)ctsio); 8974 8975 return (retval); 8976} 8977 8978/* 8979 * This routine is for handling a message from the other SC pertaining to 8980 * persistent reserve out. All the error checking will have been done 8981 * so only perorming the action need be done here to keep the two 8982 * in sync. 8983 */ 8984static void 8985ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8986{ 8987 struct ctl_lun *lun; 8988 struct ctl_softc *softc; 8989 int i; 8990 uint32_t targ_lun; 8991 8992 softc = control_softc; 8993 8994 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8995 lun = softc->ctl_luns[targ_lun]; 8996 mtx_lock(&lun->lun_lock); 8997 switch(msg->pr.pr_info.action) { 8998 case CTL_PR_REG_KEY: 8999 if (lun->pr_keys[msg->pr.pr_info.residx] == 0) 9000 lun->pr_key_count++; 9001 lun->pr_keys[msg->pr.pr_info.residx] = 9002 scsi_8btou64(msg->pr.pr_info.sa_res_key); 9003 lun->PRGeneration++; 9004 break; 9005 9006 case CTL_PR_UNREG_KEY: 9007 lun->pr_keys[msg->pr.pr_info.residx] = 0; 9008 lun->pr_key_count--; 9009 9010 /* XXX Need to see if the reservation has been released */ 9011 /* if so do we need to generate UA? */ 9012 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 9013 lun->flags &= ~CTL_LUN_PR_RESERVED; 9014 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 9015 9016 if ((lun->res_type == SPR_TYPE_WR_EX_RO 9017 || lun->res_type == SPR_TYPE_EX_AC_RO) 9018 && lun->pr_key_count) { 9019 /* 9020 * If the reservation is a registrants 9021 * only type we need to generate a UA 9022 * for other registered inits. The 9023 * sense code should be RESERVATIONS 9024 * RELEASED 9025 */ 9026 9027 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 9028 if (lun->pr_keys[i+ 9029 persis_offset] == 0) 9030 continue; 9031 9032 lun->pending_ua[i] |= 9033 CTL_UA_RES_RELEASE; 9034 } 9035 } 9036 lun->res_type = 0; 9037 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 9038 if (lun->pr_key_count==0) { 9039 lun->flags &= ~CTL_LUN_PR_RESERVED; 9040 lun->res_type = 0; 9041 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 9042 } 9043 } 9044 lun->PRGeneration++; 9045 break; 9046 9047 case CTL_PR_RESERVE: 9048 lun->flags |= CTL_LUN_PR_RESERVED; 9049 lun->res_type = msg->pr.pr_info.res_type; 9050 lun->pr_res_idx = msg->pr.pr_info.residx; 9051 9052 break; 9053 9054 case CTL_PR_RELEASE: 9055 /* 9056 * if this isn't an exclusive access res generate UA for all 9057 * other registrants. 9058 */ 9059 if (lun->res_type != SPR_TYPE_EX_AC 9060 && lun->res_type != SPR_TYPE_WR_EX) { 9061 for (i = 0; i < CTL_MAX_INITIATORS; i++) 9062 if (lun->pr_keys[i+persis_offset] != 0) 9063 lun->pending_ua[i] |= 9064 CTL_UA_RES_RELEASE; 9065 } 9066 9067 lun->flags &= ~CTL_LUN_PR_RESERVED; 9068 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 9069 lun->res_type = 0; 9070 break; 9071 9072 case CTL_PR_PREEMPT: 9073 ctl_pro_preempt_other(lun, msg); 9074 break; 9075 case CTL_PR_CLEAR: 9076 lun->flags &= ~CTL_LUN_PR_RESERVED; 9077 lun->res_type = 0; 9078 lun->pr_key_count = 0; 9079 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 9080 9081 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 9082 if (lun->pr_keys[i] == 0) 9083 continue; 9084 if (!persis_offset 9085 && i < CTL_MAX_INITIATORS) 9086 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 9087 else if (persis_offset 9088 && i >= persis_offset) 9089 lun->pending_ua[i-persis_offset] |= 9090 CTL_UA_RES_PREEMPT; 9091 lun->pr_keys[i] = 0; 9092 } 9093 lun->PRGeneration++; 9094 break; 9095 } 9096 9097 mtx_unlock(&lun->lun_lock); 9098} 9099 9100int 9101ctl_read_write(struct ctl_scsiio *ctsio) 9102{ 9103 struct ctl_lun *lun; 9104 struct ctl_lba_len_flags *lbalen; 9105 uint64_t lba; 9106 uint32_t num_blocks; 9107 int flags, retval; 9108 int isread; 9109 9110 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9111 9112 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 9113 9114 flags = 0; 9115 retval = CTL_RETVAL_COMPLETE; 9116 9117 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 9118 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 9119 switch (ctsio->cdb[0]) { 9120 case READ_6: 9121 case WRITE_6: { 9122 struct scsi_rw_6 *cdb; 9123 9124 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9125 9126 lba = scsi_3btoul(cdb->addr); 9127 /* only 5 bits are valid in the most significant address byte */ 9128 lba &= 0x1fffff; 9129 num_blocks = cdb->length; 9130 /* 9131 * This is correct according to SBC-2. 9132 */ 9133 if (num_blocks == 0) 9134 num_blocks = 256; 9135 break; 9136 } 9137 case READ_10: 9138 case WRITE_10: { 9139 struct scsi_rw_10 *cdb; 9140 9141 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9142 if (cdb->byte2 & SRW10_FUA) 9143 flags |= CTL_LLF_FUA; 9144 if (cdb->byte2 & SRW10_DPO) 9145 flags |= CTL_LLF_DPO; 9146 lba = scsi_4btoul(cdb->addr); 9147 num_blocks = scsi_2btoul(cdb->length); 9148 break; 9149 } 9150 case WRITE_VERIFY_10: { 9151 struct scsi_write_verify_10 *cdb; 9152 9153 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9154 flags |= CTL_LLF_FUA; 9155 if (cdb->byte2 & SWV_DPO) 9156 flags |= CTL_LLF_DPO; 9157 lba = scsi_4btoul(cdb->addr); 9158 num_blocks = scsi_2btoul(cdb->length); 9159 break; 9160 } 9161 case READ_12: 9162 case WRITE_12: { 9163 struct scsi_rw_12 *cdb; 9164 9165 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9166 if (cdb->byte2 & SRW12_FUA) 9167 flags |= CTL_LLF_FUA; 9168 if (cdb->byte2 & SRW12_DPO) 9169 flags |= CTL_LLF_DPO; 9170 lba = scsi_4btoul(cdb->addr); 9171 num_blocks = scsi_4btoul(cdb->length); 9172 break; 9173 } 9174 case WRITE_VERIFY_12: { 9175 struct scsi_write_verify_12 *cdb; 9176 9177 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9178 flags |= CTL_LLF_FUA; 9179 if (cdb->byte2 & SWV_DPO) 9180 flags |= CTL_LLF_DPO; 9181 lba = scsi_4btoul(cdb->addr); 9182 num_blocks = scsi_4btoul(cdb->length); 9183 break; 9184 } 9185 case READ_16: 9186 case WRITE_16: { 9187 struct scsi_rw_16 *cdb; 9188 9189 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9190 if (cdb->byte2 & SRW12_FUA) 9191 flags |= CTL_LLF_FUA; 9192 if (cdb->byte2 & SRW12_DPO) 9193 flags |= CTL_LLF_DPO; 9194 lba = scsi_8btou64(cdb->addr); 9195 num_blocks = scsi_4btoul(cdb->length); 9196 break; 9197 } 9198 case WRITE_ATOMIC_16: { 9199 struct scsi_rw_16 *cdb; 9200 9201 if (lun->be_lun->atomicblock == 0) { 9202 ctl_set_invalid_opcode(ctsio); 9203 ctl_done((union ctl_io *)ctsio); 9204 return (CTL_RETVAL_COMPLETE); 9205 } 9206 9207 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9208 if (cdb->byte2 & SRW12_FUA) 9209 flags |= CTL_LLF_FUA; 9210 if (cdb->byte2 & SRW12_DPO) 9211 flags |= CTL_LLF_DPO; 9212 lba = scsi_8btou64(cdb->addr); 9213 num_blocks = scsi_4btoul(cdb->length); 9214 if (num_blocks > lun->be_lun->atomicblock) { 9215 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 9216 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 9217 /*bit*/ 0); 9218 ctl_done((union ctl_io *)ctsio); 9219 return (CTL_RETVAL_COMPLETE); 9220 } 9221 break; 9222 } 9223 case WRITE_VERIFY_16: { 9224 struct scsi_write_verify_16 *cdb; 9225 9226 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9227 flags |= CTL_LLF_FUA; 9228 if (cdb->byte2 & SWV_DPO) 9229 flags |= CTL_LLF_DPO; 9230 lba = scsi_8btou64(cdb->addr); 9231 num_blocks = scsi_4btoul(cdb->length); 9232 break; 9233 } 9234 default: 9235 /* 9236 * We got a command we don't support. This shouldn't 9237 * happen, commands should be filtered out above us. 9238 */ 9239 ctl_set_invalid_opcode(ctsio); 9240 ctl_done((union ctl_io *)ctsio); 9241 9242 return (CTL_RETVAL_COMPLETE); 9243 break; /* NOTREACHED */ 9244 } 9245 9246 /* 9247 * The first check is to make sure we're in bounds, the second 9248 * check is to catch wrap-around problems. If the lba + num blocks 9249 * is less than the lba, then we've wrapped around and the block 9250 * range is invalid anyway. 9251 */ 9252 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9253 || ((lba + num_blocks) < lba)) { 9254 ctl_set_lba_out_of_range(ctsio); 9255 ctl_done((union ctl_io *)ctsio); 9256 return (CTL_RETVAL_COMPLETE); 9257 } 9258 9259 /* 9260 * According to SBC-3, a transfer length of 0 is not an error. 9261 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9262 * translates to 256 blocks for those commands. 9263 */ 9264 if (num_blocks == 0) { 9265 ctl_set_success(ctsio); 9266 ctl_done((union ctl_io *)ctsio); 9267 return (CTL_RETVAL_COMPLETE); 9268 } 9269 9270 /* Set FUA and/or DPO if caches are disabled. */ 9271 if (isread) { 9272 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9273 SCP_RCD) != 0) 9274 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9275 } else { 9276 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9277 SCP_WCE) == 0) 9278 flags |= CTL_LLF_FUA; 9279 } 9280 9281 lbalen = (struct ctl_lba_len_flags *) 9282 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9283 lbalen->lba = lba; 9284 lbalen->len = num_blocks; 9285 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9286 9287 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9288 ctsio->kern_rel_offset = 0; 9289 9290 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9291 9292 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9293 9294 return (retval); 9295} 9296 9297static int 9298ctl_cnw_cont(union ctl_io *io) 9299{ 9300 struct ctl_scsiio *ctsio; 9301 struct ctl_lun *lun; 9302 struct ctl_lba_len_flags *lbalen; 9303 int retval; 9304 9305 ctsio = &io->scsiio; 9306 ctsio->io_hdr.status = CTL_STATUS_NONE; 9307 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9308 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9309 lbalen = (struct ctl_lba_len_flags *) 9310 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9311 lbalen->flags &= ~CTL_LLF_COMPARE; 9312 lbalen->flags |= CTL_LLF_WRITE; 9313 9314 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9315 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9316 return (retval); 9317} 9318 9319int 9320ctl_cnw(struct ctl_scsiio *ctsio) 9321{ 9322 struct ctl_lun *lun; 9323 struct ctl_lba_len_flags *lbalen; 9324 uint64_t lba; 9325 uint32_t num_blocks; 9326 int flags, retval; 9327 9328 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9329 9330 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9331 9332 flags = 0; 9333 retval = CTL_RETVAL_COMPLETE; 9334 9335 switch (ctsio->cdb[0]) { 9336 case COMPARE_AND_WRITE: { 9337 struct scsi_compare_and_write *cdb; 9338 9339 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9340 if (cdb->byte2 & SRW10_FUA) 9341 flags |= CTL_LLF_FUA; 9342 if (cdb->byte2 & SRW10_DPO) 9343 flags |= CTL_LLF_DPO; 9344 lba = scsi_8btou64(cdb->addr); 9345 num_blocks = cdb->length; 9346 break; 9347 } 9348 default: 9349 /* 9350 * We got a command we don't support. This shouldn't 9351 * happen, commands should be filtered out above us. 9352 */ 9353 ctl_set_invalid_opcode(ctsio); 9354 ctl_done((union ctl_io *)ctsio); 9355 9356 return (CTL_RETVAL_COMPLETE); 9357 break; /* NOTREACHED */ 9358 } 9359 9360 /* 9361 * The first check is to make sure we're in bounds, the second 9362 * check is to catch wrap-around problems. If the lba + num blocks 9363 * is less than the lba, then we've wrapped around and the block 9364 * range is invalid anyway. 9365 */ 9366 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9367 || ((lba + num_blocks) < lba)) { 9368 ctl_set_lba_out_of_range(ctsio); 9369 ctl_done((union ctl_io *)ctsio); 9370 return (CTL_RETVAL_COMPLETE); 9371 } 9372 9373 /* 9374 * According to SBC-3, a transfer length of 0 is not an error. 9375 */ 9376 if (num_blocks == 0) { 9377 ctl_set_success(ctsio); 9378 ctl_done((union ctl_io *)ctsio); 9379 return (CTL_RETVAL_COMPLETE); 9380 } 9381 9382 /* Set FUA if write cache is disabled. */ 9383 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9384 SCP_WCE) == 0) 9385 flags |= CTL_LLF_FUA; 9386 9387 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9388 ctsio->kern_rel_offset = 0; 9389 9390 /* 9391 * Set the IO_CONT flag, so that if this I/O gets passed to 9392 * ctl_data_submit_done(), it'll get passed back to 9393 * ctl_ctl_cnw_cont() for further processing. 9394 */ 9395 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9396 ctsio->io_cont = ctl_cnw_cont; 9397 9398 lbalen = (struct ctl_lba_len_flags *) 9399 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9400 lbalen->lba = lba; 9401 lbalen->len = num_blocks; 9402 lbalen->flags = CTL_LLF_COMPARE | flags; 9403 9404 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9405 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9406 return (retval); 9407} 9408 9409int 9410ctl_verify(struct ctl_scsiio *ctsio) 9411{ 9412 struct ctl_lun *lun; 9413 struct ctl_lba_len_flags *lbalen; 9414 uint64_t lba; 9415 uint32_t num_blocks; 9416 int bytchk, flags; 9417 int retval; 9418 9419 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9420 9421 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9422 9423 bytchk = 0; 9424 flags = CTL_LLF_FUA; 9425 retval = CTL_RETVAL_COMPLETE; 9426 9427 switch (ctsio->cdb[0]) { 9428 case VERIFY_10: { 9429 struct scsi_verify_10 *cdb; 9430 9431 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9432 if (cdb->byte2 & SVFY_BYTCHK) 9433 bytchk = 1; 9434 if (cdb->byte2 & SVFY_DPO) 9435 flags |= CTL_LLF_DPO; 9436 lba = scsi_4btoul(cdb->addr); 9437 num_blocks = scsi_2btoul(cdb->length); 9438 break; 9439 } 9440 case VERIFY_12: { 9441 struct scsi_verify_12 *cdb; 9442 9443 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9444 if (cdb->byte2 & SVFY_BYTCHK) 9445 bytchk = 1; 9446 if (cdb->byte2 & SVFY_DPO) 9447 flags |= CTL_LLF_DPO; 9448 lba = scsi_4btoul(cdb->addr); 9449 num_blocks = scsi_4btoul(cdb->length); 9450 break; 9451 } 9452 case VERIFY_16: { 9453 struct scsi_rw_16 *cdb; 9454 9455 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9456 if (cdb->byte2 & SVFY_BYTCHK) 9457 bytchk = 1; 9458 if (cdb->byte2 & SVFY_DPO) 9459 flags |= CTL_LLF_DPO; 9460 lba = scsi_8btou64(cdb->addr); 9461 num_blocks = scsi_4btoul(cdb->length); 9462 break; 9463 } 9464 default: 9465 /* 9466 * We got a command we don't support. This shouldn't 9467 * happen, commands should be filtered out above us. 9468 */ 9469 ctl_set_invalid_opcode(ctsio); 9470 ctl_done((union ctl_io *)ctsio); 9471 return (CTL_RETVAL_COMPLETE); 9472 } 9473 9474 /* 9475 * The first check is to make sure we're in bounds, the second 9476 * check is to catch wrap-around problems. If the lba + num blocks 9477 * is less than the lba, then we've wrapped around and the block 9478 * range is invalid anyway. 9479 */ 9480 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9481 || ((lba + num_blocks) < lba)) { 9482 ctl_set_lba_out_of_range(ctsio); 9483 ctl_done((union ctl_io *)ctsio); 9484 return (CTL_RETVAL_COMPLETE); 9485 } 9486 9487 /* 9488 * According to SBC-3, a transfer length of 0 is not an error. 9489 */ 9490 if (num_blocks == 0) { 9491 ctl_set_success(ctsio); 9492 ctl_done((union ctl_io *)ctsio); 9493 return (CTL_RETVAL_COMPLETE); 9494 } 9495 9496 lbalen = (struct ctl_lba_len_flags *) 9497 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9498 lbalen->lba = lba; 9499 lbalen->len = num_blocks; 9500 if (bytchk) { 9501 lbalen->flags = CTL_LLF_COMPARE | flags; 9502 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9503 } else { 9504 lbalen->flags = CTL_LLF_VERIFY | flags; 9505 ctsio->kern_total_len = 0; 9506 } 9507 ctsio->kern_rel_offset = 0; 9508 9509 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9510 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9511 return (retval); 9512} 9513 9514int 9515ctl_report_luns(struct ctl_scsiio *ctsio) 9516{ 9517 struct scsi_report_luns *cdb; 9518 struct scsi_report_luns_data *lun_data; 9519 struct ctl_lun *lun, *request_lun; 9520 int num_luns, retval; 9521 uint32_t alloc_len, lun_datalen; 9522 int num_filled, well_known; 9523 uint32_t initidx, targ_lun_id, lun_id; 9524 9525 retval = CTL_RETVAL_COMPLETE; 9526 well_known = 0; 9527 9528 cdb = (struct scsi_report_luns *)ctsio->cdb; 9529 9530 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9531 9532 mtx_lock(&control_softc->ctl_lock); 9533 num_luns = control_softc->num_luns; 9534 mtx_unlock(&control_softc->ctl_lock); 9535 9536 switch (cdb->select_report) { 9537 case RPL_REPORT_DEFAULT: 9538 case RPL_REPORT_ALL: 9539 break; 9540 case RPL_REPORT_WELLKNOWN: 9541 well_known = 1; 9542 num_luns = 0; 9543 break; 9544 default: 9545 ctl_set_invalid_field(ctsio, 9546 /*sks_valid*/ 1, 9547 /*command*/ 1, 9548 /*field*/ 2, 9549 /*bit_valid*/ 0, 9550 /*bit*/ 0); 9551 ctl_done((union ctl_io *)ctsio); 9552 return (retval); 9553 break; /* NOTREACHED */ 9554 } 9555 9556 alloc_len = scsi_4btoul(cdb->length); 9557 /* 9558 * The initiator has to allocate at least 16 bytes for this request, 9559 * so he can at least get the header and the first LUN. Otherwise 9560 * we reject the request (per SPC-3 rev 14, section 6.21). 9561 */ 9562 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9563 sizeof(struct scsi_report_luns_lundata))) { 9564 ctl_set_invalid_field(ctsio, 9565 /*sks_valid*/ 1, 9566 /*command*/ 1, 9567 /*field*/ 6, 9568 /*bit_valid*/ 0, 9569 /*bit*/ 0); 9570 ctl_done((union ctl_io *)ctsio); 9571 return (retval); 9572 } 9573 9574 request_lun = (struct ctl_lun *) 9575 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9576 9577 lun_datalen = sizeof(*lun_data) + 9578 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9579 9580 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9581 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9582 ctsio->kern_sg_entries = 0; 9583 9584 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9585 9586 mtx_lock(&control_softc->ctl_lock); 9587 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9588 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9589 if (lun_id >= CTL_MAX_LUNS) 9590 continue; 9591 lun = control_softc->ctl_luns[lun_id]; 9592 if (lun == NULL) 9593 continue; 9594 9595 if (targ_lun_id <= 0xff) { 9596 /* 9597 * Peripheral addressing method, bus number 0. 9598 */ 9599 lun_data->luns[num_filled].lundata[0] = 9600 RPL_LUNDATA_ATYP_PERIPH; 9601 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9602 num_filled++; 9603 } else if (targ_lun_id <= 0x3fff) { 9604 /* 9605 * Flat addressing method. 9606 */ 9607 lun_data->luns[num_filled].lundata[0] = 9608 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9609 lun_data->luns[num_filled].lundata[1] = 9610 (targ_lun_id & 0xff); 9611 num_filled++; 9612 } else if (targ_lun_id <= 0xffffff) { 9613 /* 9614 * Extended flat addressing method. 9615 */ 9616 lun_data->luns[num_filled].lundata[0] = 9617 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9618 scsi_ulto3b(targ_lun_id, 9619 &lun_data->luns[num_filled].lundata[1]); 9620 num_filled++; 9621 } else { 9622 printf("ctl_report_luns: bogus LUN number %jd, " 9623 "skipping\n", (intmax_t)targ_lun_id); 9624 } 9625 /* 9626 * According to SPC-3, rev 14 section 6.21: 9627 * 9628 * "The execution of a REPORT LUNS command to any valid and 9629 * installed logical unit shall clear the REPORTED LUNS DATA 9630 * HAS CHANGED unit attention condition for all logical 9631 * units of that target with respect to the requesting 9632 * initiator. A valid and installed logical unit is one 9633 * having a PERIPHERAL QUALIFIER of 000b in the standard 9634 * INQUIRY data (see 6.4.2)." 9635 * 9636 * If request_lun is NULL, the LUN this report luns command 9637 * was issued to is either disabled or doesn't exist. In that 9638 * case, we shouldn't clear any pending lun change unit 9639 * attention. 9640 */ 9641 if (request_lun != NULL) { 9642 mtx_lock(&lun->lun_lock); 9643 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9644 mtx_unlock(&lun->lun_lock); 9645 } 9646 } 9647 mtx_unlock(&control_softc->ctl_lock); 9648 9649 /* 9650 * It's quite possible that we've returned fewer LUNs than we allocated 9651 * space for. Trim it. 9652 */ 9653 lun_datalen = sizeof(*lun_data) + 9654 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9655 9656 if (lun_datalen < alloc_len) { 9657 ctsio->residual = alloc_len - lun_datalen; 9658 ctsio->kern_data_len = lun_datalen; 9659 ctsio->kern_total_len = lun_datalen; 9660 } else { 9661 ctsio->residual = 0; 9662 ctsio->kern_data_len = alloc_len; 9663 ctsio->kern_total_len = alloc_len; 9664 } 9665 ctsio->kern_data_resid = 0; 9666 ctsio->kern_rel_offset = 0; 9667 ctsio->kern_sg_entries = 0; 9668 9669 /* 9670 * We set this to the actual data length, regardless of how much 9671 * space we actually have to return results. If the user looks at 9672 * this value, he'll know whether or not he allocated enough space 9673 * and reissue the command if necessary. We don't support well 9674 * known logical units, so if the user asks for that, return none. 9675 */ 9676 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9677 9678 /* 9679 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9680 * this request. 9681 */ 9682 ctsio->scsi_status = SCSI_STATUS_OK; 9683 9684 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9685 ctsio->be_move_done = ctl_config_move_done; 9686 ctl_datamove((union ctl_io *)ctsio); 9687 9688 return (retval); 9689} 9690 9691int 9692ctl_request_sense(struct ctl_scsiio *ctsio) 9693{ 9694 struct scsi_request_sense *cdb; 9695 struct scsi_sense_data *sense_ptr; 9696 struct ctl_lun *lun; 9697 uint32_t initidx; 9698 int have_error; 9699 scsi_sense_data_type sense_format; 9700 9701 cdb = (struct scsi_request_sense *)ctsio->cdb; 9702 9703 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9704 9705 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9706 9707 /* 9708 * Determine which sense format the user wants. 9709 */ 9710 if (cdb->byte2 & SRS_DESC) 9711 sense_format = SSD_TYPE_DESC; 9712 else 9713 sense_format = SSD_TYPE_FIXED; 9714 9715 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9716 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9717 ctsio->kern_sg_entries = 0; 9718 9719 /* 9720 * struct scsi_sense_data, which is currently set to 256 bytes, is 9721 * larger than the largest allowed value for the length field in the 9722 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9723 */ 9724 ctsio->residual = 0; 9725 ctsio->kern_data_len = cdb->length; 9726 ctsio->kern_total_len = cdb->length; 9727 9728 ctsio->kern_data_resid = 0; 9729 ctsio->kern_rel_offset = 0; 9730 ctsio->kern_sg_entries = 0; 9731 9732 /* 9733 * If we don't have a LUN, we don't have any pending sense. 9734 */ 9735 if (lun == NULL) 9736 goto no_sense; 9737 9738 have_error = 0; 9739 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9740 /* 9741 * Check for pending sense, and then for pending unit attentions. 9742 * Pending sense gets returned first, then pending unit attentions. 9743 */ 9744 mtx_lock(&lun->lun_lock); 9745#ifdef CTL_WITH_CA 9746 if (ctl_is_set(lun->have_ca, initidx)) { 9747 scsi_sense_data_type stored_format; 9748 9749 /* 9750 * Check to see which sense format was used for the stored 9751 * sense data. 9752 */ 9753 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9754 9755 /* 9756 * If the user requested a different sense format than the 9757 * one we stored, then we need to convert it to the other 9758 * format. If we're going from descriptor to fixed format 9759 * sense data, we may lose things in translation, depending 9760 * on what options were used. 9761 * 9762 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9763 * for some reason we'll just copy it out as-is. 9764 */ 9765 if ((stored_format == SSD_TYPE_FIXED) 9766 && (sense_format == SSD_TYPE_DESC)) 9767 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9768 &lun->pending_sense[initidx], 9769 (struct scsi_sense_data_desc *)sense_ptr); 9770 else if ((stored_format == SSD_TYPE_DESC) 9771 && (sense_format == SSD_TYPE_FIXED)) 9772 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9773 &lun->pending_sense[initidx], 9774 (struct scsi_sense_data_fixed *)sense_ptr); 9775 else 9776 memcpy(sense_ptr, &lun->pending_sense[initidx], 9777 ctl_min(sizeof(*sense_ptr), 9778 sizeof(lun->pending_sense[initidx]))); 9779 9780 ctl_clear_mask(lun->have_ca, initidx); 9781 have_error = 1; 9782 } else 9783#endif 9784 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9785 ctl_ua_type ua_type; 9786 9787 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9788 sense_ptr, sense_format); 9789 if (ua_type != CTL_UA_NONE) 9790 have_error = 1; 9791 } 9792 mtx_unlock(&lun->lun_lock); 9793 9794 /* 9795 * We already have a pending error, return it. 9796 */ 9797 if (have_error != 0) { 9798 /* 9799 * We report the SCSI status as OK, since the status of the 9800 * request sense command itself is OK. 9801 */ 9802 ctsio->scsi_status = SCSI_STATUS_OK; 9803 9804 /* 9805 * We report 0 for the sense length, because we aren't doing 9806 * autosense in this case. We're reporting sense as 9807 * parameter data. 9808 */ 9809 ctsio->sense_len = 0; 9810 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9811 ctsio->be_move_done = ctl_config_move_done; 9812 ctl_datamove((union ctl_io *)ctsio); 9813 9814 return (CTL_RETVAL_COMPLETE); 9815 } 9816 9817no_sense: 9818 9819 /* 9820 * No sense information to report, so we report that everything is 9821 * okay. 9822 */ 9823 ctl_set_sense_data(sense_ptr, 9824 lun, 9825 sense_format, 9826 /*current_error*/ 1, 9827 /*sense_key*/ SSD_KEY_NO_SENSE, 9828 /*asc*/ 0x00, 9829 /*ascq*/ 0x00, 9830 SSD_ELEM_NONE); 9831 9832 ctsio->scsi_status = SCSI_STATUS_OK; 9833 9834 /* 9835 * We report 0 for the sense length, because we aren't doing 9836 * autosense in this case. We're reporting sense as parameter data. 9837 */ 9838 ctsio->sense_len = 0; 9839 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9840 ctsio->be_move_done = ctl_config_move_done; 9841 ctl_datamove((union ctl_io *)ctsio); 9842 9843 return (CTL_RETVAL_COMPLETE); 9844} 9845 9846int 9847ctl_tur(struct ctl_scsiio *ctsio) 9848{ 9849 9850 CTL_DEBUG_PRINT(("ctl_tur\n")); 9851 9852 ctsio->scsi_status = SCSI_STATUS_OK; 9853 ctsio->io_hdr.status = CTL_SUCCESS; 9854 9855 ctl_done((union ctl_io *)ctsio); 9856 9857 return (CTL_RETVAL_COMPLETE); 9858} 9859 9860#ifdef notyet 9861static int 9862ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9863{ 9864 9865} 9866#endif 9867 9868static int 9869ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9870{ 9871 struct scsi_vpd_supported_pages *pages; 9872 int sup_page_size; 9873 struct ctl_lun *lun; 9874 9875 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9876 9877 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9878 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9879 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9880 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9881 ctsio->kern_sg_entries = 0; 9882 9883 if (sup_page_size < alloc_len) { 9884 ctsio->residual = alloc_len - sup_page_size; 9885 ctsio->kern_data_len = sup_page_size; 9886 ctsio->kern_total_len = sup_page_size; 9887 } else { 9888 ctsio->residual = 0; 9889 ctsio->kern_data_len = alloc_len; 9890 ctsio->kern_total_len = alloc_len; 9891 } 9892 ctsio->kern_data_resid = 0; 9893 ctsio->kern_rel_offset = 0; 9894 ctsio->kern_sg_entries = 0; 9895 9896 /* 9897 * The control device is always connected. The disk device, on the 9898 * other hand, may not be online all the time. Need to change this 9899 * to figure out whether the disk device is actually online or not. 9900 */ 9901 if (lun != NULL) 9902 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9903 lun->be_lun->lun_type; 9904 else 9905 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9906 9907 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9908 /* Supported VPD pages */ 9909 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9910 /* Serial Number */ 9911 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9912 /* Device Identification */ 9913 pages->page_list[2] = SVPD_DEVICE_ID; 9914 /* Extended INQUIRY Data */ 9915 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9916 /* Mode Page Policy */ 9917 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9918 /* SCSI Ports */ 9919 pages->page_list[5] = SVPD_SCSI_PORTS; 9920 /* Third-party Copy */ 9921 pages->page_list[6] = SVPD_SCSI_TPC; 9922 /* Block limits */ 9923 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9924 /* Block Device Characteristics */ 9925 pages->page_list[8] = SVPD_BDC; 9926 /* Logical Block Provisioning */ 9927 pages->page_list[9] = SVPD_LBP; 9928 9929 ctsio->scsi_status = SCSI_STATUS_OK; 9930 9931 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9932 ctsio->be_move_done = ctl_config_move_done; 9933 ctl_datamove((union ctl_io *)ctsio); 9934 9935 return (CTL_RETVAL_COMPLETE); 9936} 9937 9938static int 9939ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9940{ 9941 struct scsi_vpd_unit_serial_number *sn_ptr; 9942 struct ctl_lun *lun; 9943 int data_len; 9944 9945 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9946 9947 data_len = 4 + CTL_SN_LEN; 9948 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9949 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9950 if (data_len < alloc_len) { 9951 ctsio->residual = alloc_len - data_len; 9952 ctsio->kern_data_len = data_len; 9953 ctsio->kern_total_len = data_len; 9954 } else { 9955 ctsio->residual = 0; 9956 ctsio->kern_data_len = alloc_len; 9957 ctsio->kern_total_len = alloc_len; 9958 } 9959 ctsio->kern_data_resid = 0; 9960 ctsio->kern_rel_offset = 0; 9961 ctsio->kern_sg_entries = 0; 9962 9963 /* 9964 * The control device is always connected. The disk device, on the 9965 * other hand, may not be online all the time. Need to change this 9966 * to figure out whether the disk device is actually online or not. 9967 */ 9968 if (lun != NULL) 9969 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9970 lun->be_lun->lun_type; 9971 else 9972 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9973 9974 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9975 sn_ptr->length = CTL_SN_LEN; 9976 /* 9977 * If we don't have a LUN, we just leave the serial number as 9978 * all spaces. 9979 */ 9980 if (lun != NULL) { 9981 strncpy((char *)sn_ptr->serial_num, 9982 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9983 } else 9984 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9985 ctsio->scsi_status = SCSI_STATUS_OK; 9986 9987 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9988 ctsio->be_move_done = ctl_config_move_done; 9989 ctl_datamove((union ctl_io *)ctsio); 9990 9991 return (CTL_RETVAL_COMPLETE); 9992} 9993 9994 9995static int 9996ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9997{ 9998 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9999 struct ctl_lun *lun; 10000 int data_len; 10001 10002 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10003 10004 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 10005 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10006 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 10007 ctsio->kern_sg_entries = 0; 10008 10009 if (data_len < alloc_len) { 10010 ctsio->residual = alloc_len - data_len; 10011 ctsio->kern_data_len = data_len; 10012 ctsio->kern_total_len = data_len; 10013 } else { 10014 ctsio->residual = 0; 10015 ctsio->kern_data_len = alloc_len; 10016 ctsio->kern_total_len = alloc_len; 10017 } 10018 ctsio->kern_data_resid = 0; 10019 ctsio->kern_rel_offset = 0; 10020 ctsio->kern_sg_entries = 0; 10021 10022 /* 10023 * The control device is always connected. The disk device, on the 10024 * other hand, may not be online all the time. 10025 */ 10026 if (lun != NULL) 10027 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10028 lun->be_lun->lun_type; 10029 else 10030 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10031 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 10032 eid_ptr->page_length = data_len - 4; 10033 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 10034 eid_ptr->flags3 = SVPD_EID_V_SUP; 10035 10036 ctsio->scsi_status = SCSI_STATUS_OK; 10037 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10038 ctsio->be_move_done = ctl_config_move_done; 10039 ctl_datamove((union ctl_io *)ctsio); 10040 10041 return (CTL_RETVAL_COMPLETE); 10042} 10043 10044static int 10045ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 10046{ 10047 struct scsi_vpd_mode_page_policy *mpp_ptr; 10048 struct ctl_lun *lun; 10049 int data_len; 10050 10051 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10052 10053 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 10054 sizeof(struct scsi_vpd_mode_page_policy_descr); 10055 10056 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10057 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 10058 ctsio->kern_sg_entries = 0; 10059 10060 if (data_len < alloc_len) { 10061 ctsio->residual = alloc_len - data_len; 10062 ctsio->kern_data_len = data_len; 10063 ctsio->kern_total_len = data_len; 10064 } else { 10065 ctsio->residual = 0; 10066 ctsio->kern_data_len = alloc_len; 10067 ctsio->kern_total_len = alloc_len; 10068 } 10069 ctsio->kern_data_resid = 0; 10070 ctsio->kern_rel_offset = 0; 10071 ctsio->kern_sg_entries = 0; 10072 10073 /* 10074 * The control device is always connected. The disk device, on the 10075 * other hand, may not be online all the time. 10076 */ 10077 if (lun != NULL) 10078 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10079 lun->be_lun->lun_type; 10080 else 10081 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10082 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 10083 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 10084 mpp_ptr->descr[0].page_code = 0x3f; 10085 mpp_ptr->descr[0].subpage_code = 0xff; 10086 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 10087 10088 ctsio->scsi_status = SCSI_STATUS_OK; 10089 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10090 ctsio->be_move_done = ctl_config_move_done; 10091 ctl_datamove((union ctl_io *)ctsio); 10092 10093 return (CTL_RETVAL_COMPLETE); 10094} 10095 10096static int 10097ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 10098{ 10099 struct scsi_vpd_device_id *devid_ptr; 10100 struct scsi_vpd_id_descriptor *desc; 10101 struct ctl_softc *ctl_softc; 10102 struct ctl_lun *lun; 10103 struct ctl_port *port; 10104 int data_len; 10105 uint8_t proto; 10106 10107 ctl_softc = control_softc; 10108 10109 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 10110 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10111 10112 data_len = sizeof(struct scsi_vpd_device_id) + 10113 sizeof(struct scsi_vpd_id_descriptor) + 10114 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 10115 sizeof(struct scsi_vpd_id_descriptor) + 10116 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 10117 if (lun && lun->lun_devid) 10118 data_len += lun->lun_devid->len; 10119 if (port->port_devid) 10120 data_len += port->port_devid->len; 10121 if (port->target_devid) 10122 data_len += port->target_devid->len; 10123 10124 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10125 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 10126 ctsio->kern_sg_entries = 0; 10127 10128 if (data_len < alloc_len) { 10129 ctsio->residual = alloc_len - data_len; 10130 ctsio->kern_data_len = data_len; 10131 ctsio->kern_total_len = data_len; 10132 } else { 10133 ctsio->residual = 0; 10134 ctsio->kern_data_len = alloc_len; 10135 ctsio->kern_total_len = alloc_len; 10136 } 10137 ctsio->kern_data_resid = 0; 10138 ctsio->kern_rel_offset = 0; 10139 ctsio->kern_sg_entries = 0; 10140 10141 /* 10142 * The control device is always connected. The disk device, on the 10143 * other hand, may not be online all the time. 10144 */ 10145 if (lun != NULL) 10146 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10147 lun->be_lun->lun_type; 10148 else 10149 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10150 devid_ptr->page_code = SVPD_DEVICE_ID; 10151 scsi_ulto2b(data_len - 4, devid_ptr->length); 10152 10153 if (port->port_type == CTL_PORT_FC) 10154 proto = SCSI_PROTO_FC << 4; 10155 else if (port->port_type == CTL_PORT_ISCSI) 10156 proto = SCSI_PROTO_ISCSI << 4; 10157 else 10158 proto = SCSI_PROTO_SPI << 4; 10159 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 10160 10161 /* 10162 * We're using a LUN association here. i.e., this device ID is a 10163 * per-LUN identifier. 10164 */ 10165 if (lun && lun->lun_devid) { 10166 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 10167 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10168 lun->lun_devid->len); 10169 } 10170 10171 /* 10172 * This is for the WWPN which is a port association. 10173 */ 10174 if (port->port_devid) { 10175 memcpy(desc, port->port_devid->data, port->port_devid->len); 10176 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10177 port->port_devid->len); 10178 } 10179 10180 /* 10181 * This is for the Relative Target Port(type 4h) identifier 10182 */ 10183 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10184 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10185 SVPD_ID_TYPE_RELTARG; 10186 desc->length = 4; 10187 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 10188 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10189 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 10190 10191 /* 10192 * This is for the Target Port Group(type 5h) identifier 10193 */ 10194 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10195 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10196 SVPD_ID_TYPE_TPORTGRP; 10197 desc->length = 4; 10198 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10199 &desc->identifier[2]); 10200 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10201 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10202 10203 /* 10204 * This is for the Target identifier 10205 */ 10206 if (port->target_devid) { 10207 memcpy(desc, port->target_devid->data, port->target_devid->len); 10208 } 10209 10210 ctsio->scsi_status = SCSI_STATUS_OK; 10211 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10212 ctsio->be_move_done = ctl_config_move_done; 10213 ctl_datamove((union ctl_io *)ctsio); 10214 10215 return (CTL_RETVAL_COMPLETE); 10216} 10217 10218static int 10219ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10220{ 10221 struct ctl_softc *softc = control_softc; 10222 struct scsi_vpd_scsi_ports *sp; 10223 struct scsi_vpd_port_designation *pd; 10224 struct scsi_vpd_port_designation_cont *pdc; 10225 struct ctl_lun *lun; 10226 struct ctl_port *port; 10227 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10228 int num_target_port_groups; 10229 10230 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10231 10232 if (softc->is_single) 10233 num_target_port_groups = 1; 10234 else 10235 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10236 num_target_ports = 0; 10237 iid_len = 0; 10238 id_len = 0; 10239 mtx_lock(&softc->ctl_lock); 10240 STAILQ_FOREACH(port, &softc->port_list, links) { 10241 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10242 continue; 10243 if (lun != NULL && 10244 ctl_map_lun_back(port->targ_port, lun->lun) >= 10245 CTL_MAX_LUNS) 10246 continue; 10247 num_target_ports++; 10248 if (port->init_devid) 10249 iid_len += port->init_devid->len; 10250 if (port->port_devid) 10251 id_len += port->port_devid->len; 10252 } 10253 mtx_unlock(&softc->ctl_lock); 10254 10255 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10256 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10257 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10258 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10259 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10260 ctsio->kern_sg_entries = 0; 10261 10262 if (data_len < alloc_len) { 10263 ctsio->residual = alloc_len - data_len; 10264 ctsio->kern_data_len = data_len; 10265 ctsio->kern_total_len = data_len; 10266 } else { 10267 ctsio->residual = 0; 10268 ctsio->kern_data_len = alloc_len; 10269 ctsio->kern_total_len = alloc_len; 10270 } 10271 ctsio->kern_data_resid = 0; 10272 ctsio->kern_rel_offset = 0; 10273 ctsio->kern_sg_entries = 0; 10274 10275 /* 10276 * The control device is always connected. The disk device, on the 10277 * other hand, may not be online all the time. Need to change this 10278 * to figure out whether the disk device is actually online or not. 10279 */ 10280 if (lun != NULL) 10281 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10282 lun->be_lun->lun_type; 10283 else 10284 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10285 10286 sp->page_code = SVPD_SCSI_PORTS; 10287 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10288 sp->page_length); 10289 pd = &sp->design[0]; 10290 10291 mtx_lock(&softc->ctl_lock); 10292 pg = softc->port_offset / CTL_MAX_PORTS; 10293 for (g = 0; g < num_target_port_groups; g++) { 10294 STAILQ_FOREACH(port, &softc->port_list, links) { 10295 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10296 continue; 10297 if (lun != NULL && 10298 ctl_map_lun_back(port->targ_port, lun->lun) >= 10299 CTL_MAX_LUNS) 10300 continue; 10301 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10302 scsi_ulto2b(p, pd->relative_port_id); 10303 if (port->init_devid && g == pg) { 10304 iid_len = port->init_devid->len; 10305 memcpy(pd->initiator_transportid, 10306 port->init_devid->data, port->init_devid->len); 10307 } else 10308 iid_len = 0; 10309 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10310 pdc = (struct scsi_vpd_port_designation_cont *) 10311 (&pd->initiator_transportid[iid_len]); 10312 if (port->port_devid && g == pg) { 10313 id_len = port->port_devid->len; 10314 memcpy(pdc->target_port_descriptors, 10315 port->port_devid->data, port->port_devid->len); 10316 } else 10317 id_len = 0; 10318 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10319 pd = (struct scsi_vpd_port_designation *) 10320 ((uint8_t *)pdc->target_port_descriptors + id_len); 10321 } 10322 } 10323 mtx_unlock(&softc->ctl_lock); 10324 10325 ctsio->scsi_status = SCSI_STATUS_OK; 10326 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10327 ctsio->be_move_done = ctl_config_move_done; 10328 ctl_datamove((union ctl_io *)ctsio); 10329 10330 return (CTL_RETVAL_COMPLETE); 10331} 10332 10333static int 10334ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10335{ 10336 struct scsi_vpd_block_limits *bl_ptr; 10337 struct ctl_lun *lun; 10338 int bs; 10339 10340 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10341 10342 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10343 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10344 ctsio->kern_sg_entries = 0; 10345 10346 if (sizeof(*bl_ptr) < alloc_len) { 10347 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10348 ctsio->kern_data_len = sizeof(*bl_ptr); 10349 ctsio->kern_total_len = sizeof(*bl_ptr); 10350 } else { 10351 ctsio->residual = 0; 10352 ctsio->kern_data_len = alloc_len; 10353 ctsio->kern_total_len = alloc_len; 10354 } 10355 ctsio->kern_data_resid = 0; 10356 ctsio->kern_rel_offset = 0; 10357 ctsio->kern_sg_entries = 0; 10358 10359 /* 10360 * The control device is always connected. The disk device, on the 10361 * other hand, may not be online all the time. Need to change this 10362 * to figure out whether the disk device is actually online or not. 10363 */ 10364 if (lun != NULL) 10365 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10366 lun->be_lun->lun_type; 10367 else 10368 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10369 10370 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10371 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10372 bl_ptr->max_cmp_write_len = 0xff; 10373 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10374 if (lun != NULL) { 10375 bs = lun->be_lun->blocksize; 10376 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10377 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10378 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10379 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10380 if (lun->be_lun->pblockexp != 0) { 10381 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10382 bl_ptr->opt_unmap_grain); 10383 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10384 bl_ptr->unmap_grain_align); 10385 } 10386 } 10387 scsi_ulto4b(lun->be_lun->atomicblock, 10388 bl_ptr->max_atomic_transfer_length); 10389 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10390 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10391 } 10392 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10393 10394 ctsio->scsi_status = SCSI_STATUS_OK; 10395 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10396 ctsio->be_move_done = ctl_config_move_done; 10397 ctl_datamove((union ctl_io *)ctsio); 10398 10399 return (CTL_RETVAL_COMPLETE); 10400} 10401 10402static int 10403ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10404{ 10405 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10406 struct ctl_lun *lun; 10407 const char *value; 10408 u_int i; 10409 10410 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10411 10412 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10413 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10414 ctsio->kern_sg_entries = 0; 10415 10416 if (sizeof(*bdc_ptr) < alloc_len) { 10417 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10418 ctsio->kern_data_len = sizeof(*bdc_ptr); 10419 ctsio->kern_total_len = sizeof(*bdc_ptr); 10420 } else { 10421 ctsio->residual = 0; 10422 ctsio->kern_data_len = alloc_len; 10423 ctsio->kern_total_len = alloc_len; 10424 } 10425 ctsio->kern_data_resid = 0; 10426 ctsio->kern_rel_offset = 0; 10427 ctsio->kern_sg_entries = 0; 10428 10429 /* 10430 * The control device is always connected. The disk device, on the 10431 * other hand, may not be online all the time. Need to change this 10432 * to figure out whether the disk device is actually online or not. 10433 */ 10434 if (lun != NULL) 10435 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10436 lun->be_lun->lun_type; 10437 else 10438 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10439 bdc_ptr->page_code = SVPD_BDC; 10440 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10441 if (lun != NULL && 10442 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10443 i = strtol(value, NULL, 0); 10444 else 10445 i = CTL_DEFAULT_ROTATION_RATE; 10446 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10447 if (lun != NULL && 10448 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10449 i = strtol(value, NULL, 0); 10450 else 10451 i = 0; 10452 bdc_ptr->wab_wac_ff = (i & 0x0f); 10453 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10454 10455 ctsio->scsi_status = SCSI_STATUS_OK; 10456 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10457 ctsio->be_move_done = ctl_config_move_done; 10458 ctl_datamove((union ctl_io *)ctsio); 10459 10460 return (CTL_RETVAL_COMPLETE); 10461} 10462 10463static int 10464ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10465{ 10466 struct scsi_vpd_logical_block_prov *lbp_ptr; 10467 struct ctl_lun *lun; 10468 10469 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10470 10471 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10472 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10473 ctsio->kern_sg_entries = 0; 10474 10475 if (sizeof(*lbp_ptr) < alloc_len) { 10476 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10477 ctsio->kern_data_len = sizeof(*lbp_ptr); 10478 ctsio->kern_total_len = sizeof(*lbp_ptr); 10479 } else { 10480 ctsio->residual = 0; 10481 ctsio->kern_data_len = alloc_len; 10482 ctsio->kern_total_len = alloc_len; 10483 } 10484 ctsio->kern_data_resid = 0; 10485 ctsio->kern_rel_offset = 0; 10486 ctsio->kern_sg_entries = 0; 10487 10488 /* 10489 * The control device is always connected. The disk device, on the 10490 * other hand, may not be online all the time. Need to change this 10491 * to figure out whether the disk device is actually online or not. 10492 */ 10493 if (lun != NULL) 10494 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10495 lun->be_lun->lun_type; 10496 else 10497 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10498 10499 lbp_ptr->page_code = SVPD_LBP; 10500 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10501 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10502 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10503 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10504 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10505 lbp_ptr->prov_type = SVPD_LBP_THIN; 10506 } 10507 10508 ctsio->scsi_status = SCSI_STATUS_OK; 10509 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10510 ctsio->be_move_done = ctl_config_move_done; 10511 ctl_datamove((union ctl_io *)ctsio); 10512 10513 return (CTL_RETVAL_COMPLETE); 10514} 10515 10516static int 10517ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10518{ 10519 struct scsi_inquiry *cdb; 10520 int alloc_len, retval; 10521 10522 cdb = (struct scsi_inquiry *)ctsio->cdb; 10523 10524 retval = CTL_RETVAL_COMPLETE; 10525 10526 alloc_len = scsi_2btoul(cdb->length); 10527 10528 switch (cdb->page_code) { 10529 case SVPD_SUPPORTED_PAGES: 10530 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10531 break; 10532 case SVPD_UNIT_SERIAL_NUMBER: 10533 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10534 break; 10535 case SVPD_DEVICE_ID: 10536 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10537 break; 10538 case SVPD_EXTENDED_INQUIRY_DATA: 10539 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10540 break; 10541 case SVPD_MODE_PAGE_POLICY: 10542 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10543 break; 10544 case SVPD_SCSI_PORTS: 10545 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10546 break; 10547 case SVPD_SCSI_TPC: 10548 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10549 break; 10550 case SVPD_BLOCK_LIMITS: 10551 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10552 break; 10553 case SVPD_BDC: 10554 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10555 break; 10556 case SVPD_LBP: 10557 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10558 break; 10559 default: 10560 ctl_set_invalid_field(ctsio, 10561 /*sks_valid*/ 1, 10562 /*command*/ 1, 10563 /*field*/ 2, 10564 /*bit_valid*/ 0, 10565 /*bit*/ 0); 10566 ctl_done((union ctl_io *)ctsio); 10567 retval = CTL_RETVAL_COMPLETE; 10568 break; 10569 } 10570 10571 return (retval); 10572} 10573 10574static int 10575ctl_inquiry_std(struct ctl_scsiio *ctsio) 10576{ 10577 struct scsi_inquiry_data *inq_ptr; 10578 struct scsi_inquiry *cdb; 10579 struct ctl_softc *ctl_softc; 10580 struct ctl_lun *lun; 10581 char *val; 10582 uint32_t alloc_len, data_len; 10583 ctl_port_type port_type; 10584 10585 ctl_softc = control_softc; 10586 10587 /* 10588 * Figure out whether we're talking to a Fibre Channel port or not. 10589 * We treat the ioctl front end, and any SCSI adapters, as packetized 10590 * SCSI front ends. 10591 */ 10592 port_type = ctl_softc->ctl_ports[ 10593 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10594 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10595 port_type = CTL_PORT_SCSI; 10596 10597 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10598 cdb = (struct scsi_inquiry *)ctsio->cdb; 10599 alloc_len = scsi_2btoul(cdb->length); 10600 10601 /* 10602 * We malloc the full inquiry data size here and fill it 10603 * in. If the user only asks for less, we'll give him 10604 * that much. 10605 */ 10606 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10607 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10608 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10609 ctsio->kern_sg_entries = 0; 10610 ctsio->kern_data_resid = 0; 10611 ctsio->kern_rel_offset = 0; 10612 10613 if (data_len < alloc_len) { 10614 ctsio->residual = alloc_len - data_len; 10615 ctsio->kern_data_len = data_len; 10616 ctsio->kern_total_len = data_len; 10617 } else { 10618 ctsio->residual = 0; 10619 ctsio->kern_data_len = alloc_len; 10620 ctsio->kern_total_len = alloc_len; 10621 } 10622 10623 /* 10624 * If we have a LUN configured, report it as connected. Otherwise, 10625 * report that it is offline or no device is supported, depending 10626 * on the value of inquiry_pq_no_lun. 10627 * 10628 * According to the spec (SPC-4 r34), the peripheral qualifier 10629 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10630 * 10631 * "A peripheral device having the specified peripheral device type 10632 * is not connected to this logical unit. However, the device 10633 * server is capable of supporting the specified peripheral device 10634 * type on this logical unit." 10635 * 10636 * According to the same spec, the peripheral qualifier 10637 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10638 * 10639 * "The device server is not capable of supporting a peripheral 10640 * device on this logical unit. For this peripheral qualifier the 10641 * peripheral device type shall be set to 1Fh. All other peripheral 10642 * device type values are reserved for this peripheral qualifier." 10643 * 10644 * Given the text, it would seem that we probably want to report that 10645 * the LUN is offline here. There is no LUN connected, but we can 10646 * support a LUN at the given LUN number. 10647 * 10648 * In the real world, though, it sounds like things are a little 10649 * different: 10650 * 10651 * - Linux, when presented with a LUN with the offline peripheral 10652 * qualifier, will create an sg driver instance for it. So when 10653 * you attach it to CTL, you wind up with a ton of sg driver 10654 * instances. (One for every LUN that Linux bothered to probe.) 10655 * Linux does this despite the fact that it issues a REPORT LUNs 10656 * to LUN 0 to get the inventory of supported LUNs. 10657 * 10658 * - There is other anecdotal evidence (from Emulex folks) about 10659 * arrays that use the offline peripheral qualifier for LUNs that 10660 * are on the "passive" path in an active/passive array. 10661 * 10662 * So the solution is provide a hopefully reasonable default 10663 * (return bad/no LUN) and allow the user to change the behavior 10664 * with a tunable/sysctl variable. 10665 */ 10666 if (lun != NULL) 10667 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10668 lun->be_lun->lun_type; 10669 else if (ctl_softc->inquiry_pq_no_lun == 0) 10670 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10671 else 10672 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10673 10674 /* RMB in byte 2 is 0 */ 10675 inq_ptr->version = SCSI_REV_SPC4; 10676 10677 /* 10678 * According to SAM-3, even if a device only supports a single 10679 * level of LUN addressing, it should still set the HISUP bit: 10680 * 10681 * 4.9.1 Logical unit numbers overview 10682 * 10683 * All logical unit number formats described in this standard are 10684 * hierarchical in structure even when only a single level in that 10685 * hierarchy is used. The HISUP bit shall be set to one in the 10686 * standard INQUIRY data (see SPC-2) when any logical unit number 10687 * format described in this standard is used. Non-hierarchical 10688 * formats are outside the scope of this standard. 10689 * 10690 * Therefore we set the HiSup bit here. 10691 * 10692 * The reponse format is 2, per SPC-3. 10693 */ 10694 inq_ptr->response_format = SID_HiSup | 2; 10695 10696 inq_ptr->additional_length = data_len - 10697 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10698 CTL_DEBUG_PRINT(("additional_length = %d\n", 10699 inq_ptr->additional_length)); 10700 10701 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10702 /* 16 bit addressing */ 10703 if (port_type == CTL_PORT_SCSI) 10704 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10705 /* XXX set the SID_MultiP bit here if we're actually going to 10706 respond on multiple ports */ 10707 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10708 10709 /* 16 bit data bus, synchronous transfers */ 10710 if (port_type == CTL_PORT_SCSI) 10711 inq_ptr->flags = SID_WBus16 | SID_Sync; 10712 /* 10713 * XXX KDM do we want to support tagged queueing on the control 10714 * device at all? 10715 */ 10716 if ((lun == NULL) 10717 || (lun->be_lun->lun_type != T_PROCESSOR)) 10718 inq_ptr->flags |= SID_CmdQue; 10719 /* 10720 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10721 * We have 8 bytes for the vendor name, and 16 bytes for the device 10722 * name and 4 bytes for the revision. 10723 */ 10724 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10725 "vendor")) == NULL) { 10726 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10727 } else { 10728 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10729 strncpy(inq_ptr->vendor, val, 10730 min(sizeof(inq_ptr->vendor), strlen(val))); 10731 } 10732 if (lun == NULL) { 10733 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10734 sizeof(inq_ptr->product)); 10735 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10736 switch (lun->be_lun->lun_type) { 10737 case T_DIRECT: 10738 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10739 sizeof(inq_ptr->product)); 10740 break; 10741 case T_PROCESSOR: 10742 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10743 sizeof(inq_ptr->product)); 10744 break; 10745 default: 10746 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10747 sizeof(inq_ptr->product)); 10748 break; 10749 } 10750 } else { 10751 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10752 strncpy(inq_ptr->product, val, 10753 min(sizeof(inq_ptr->product), strlen(val))); 10754 } 10755 10756 /* 10757 * XXX make this a macro somewhere so it automatically gets 10758 * incremented when we make changes. 10759 */ 10760 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10761 "revision")) == NULL) { 10762 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10763 } else { 10764 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10765 strncpy(inq_ptr->revision, val, 10766 min(sizeof(inq_ptr->revision), strlen(val))); 10767 } 10768 10769 /* 10770 * For parallel SCSI, we support double transition and single 10771 * transition clocking. We also support QAS (Quick Arbitration 10772 * and Selection) and Information Unit transfers on both the 10773 * control and array devices. 10774 */ 10775 if (port_type == CTL_PORT_SCSI) 10776 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10777 SID_SPI_IUS; 10778 10779 /* SAM-5 (no version claimed) */ 10780 scsi_ulto2b(0x00A0, inq_ptr->version1); 10781 /* SPC-4 (no version claimed) */ 10782 scsi_ulto2b(0x0460, inq_ptr->version2); 10783 if (port_type == CTL_PORT_FC) { 10784 /* FCP-2 ANSI INCITS.350:2003 */ 10785 scsi_ulto2b(0x0917, inq_ptr->version3); 10786 } else if (port_type == CTL_PORT_SCSI) { 10787 /* SPI-4 ANSI INCITS.362:200x */ 10788 scsi_ulto2b(0x0B56, inq_ptr->version3); 10789 } else if (port_type == CTL_PORT_ISCSI) { 10790 /* iSCSI (no version claimed) */ 10791 scsi_ulto2b(0x0960, inq_ptr->version3); 10792 } else if (port_type == CTL_PORT_SAS) { 10793 /* SAS (no version claimed) */ 10794 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10795 } 10796 10797 if (lun == NULL) { 10798 /* SBC-4 (no version claimed) */ 10799 scsi_ulto2b(0x0600, inq_ptr->version4); 10800 } else { 10801 switch (lun->be_lun->lun_type) { 10802 case T_DIRECT: 10803 /* SBC-4 (no version claimed) */ 10804 scsi_ulto2b(0x0600, inq_ptr->version4); 10805 break; 10806 case T_PROCESSOR: 10807 default: 10808 break; 10809 } 10810 } 10811 10812 ctsio->scsi_status = SCSI_STATUS_OK; 10813 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10814 ctsio->be_move_done = ctl_config_move_done; 10815 ctl_datamove((union ctl_io *)ctsio); 10816 return (CTL_RETVAL_COMPLETE); 10817} 10818 10819int 10820ctl_inquiry(struct ctl_scsiio *ctsio) 10821{ 10822 struct scsi_inquiry *cdb; 10823 int retval; 10824 10825 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10826 10827 cdb = (struct scsi_inquiry *)ctsio->cdb; 10828 if (cdb->byte2 & SI_EVPD) 10829 retval = ctl_inquiry_evpd(ctsio); 10830 else if (cdb->page_code == 0) 10831 retval = ctl_inquiry_std(ctsio); 10832 else { 10833 ctl_set_invalid_field(ctsio, 10834 /*sks_valid*/ 1, 10835 /*command*/ 1, 10836 /*field*/ 2, 10837 /*bit_valid*/ 0, 10838 /*bit*/ 0); 10839 ctl_done((union ctl_io *)ctsio); 10840 return (CTL_RETVAL_COMPLETE); 10841 } 10842 10843 return (retval); 10844} 10845 10846/* 10847 * For known CDB types, parse the LBA and length. 10848 */ 10849static int 10850ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10851{ 10852 if (io->io_hdr.io_type != CTL_IO_SCSI) 10853 return (1); 10854 10855 switch (io->scsiio.cdb[0]) { 10856 case COMPARE_AND_WRITE: { 10857 struct scsi_compare_and_write *cdb; 10858 10859 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10860 10861 *lba = scsi_8btou64(cdb->addr); 10862 *len = cdb->length; 10863 break; 10864 } 10865 case READ_6: 10866 case WRITE_6: { 10867 struct scsi_rw_6 *cdb; 10868 10869 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10870 10871 *lba = scsi_3btoul(cdb->addr); 10872 /* only 5 bits are valid in the most significant address byte */ 10873 *lba &= 0x1fffff; 10874 *len = cdb->length; 10875 break; 10876 } 10877 case READ_10: 10878 case WRITE_10: { 10879 struct scsi_rw_10 *cdb; 10880 10881 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10882 10883 *lba = scsi_4btoul(cdb->addr); 10884 *len = scsi_2btoul(cdb->length); 10885 break; 10886 } 10887 case WRITE_VERIFY_10: { 10888 struct scsi_write_verify_10 *cdb; 10889 10890 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10891 10892 *lba = scsi_4btoul(cdb->addr); 10893 *len = scsi_2btoul(cdb->length); 10894 break; 10895 } 10896 case READ_12: 10897 case WRITE_12: { 10898 struct scsi_rw_12 *cdb; 10899 10900 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10901 10902 *lba = scsi_4btoul(cdb->addr); 10903 *len = scsi_4btoul(cdb->length); 10904 break; 10905 } 10906 case WRITE_VERIFY_12: { 10907 struct scsi_write_verify_12 *cdb; 10908 10909 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10910 10911 *lba = scsi_4btoul(cdb->addr); 10912 *len = scsi_4btoul(cdb->length); 10913 break; 10914 } 10915 case READ_16: 10916 case WRITE_16: 10917 case WRITE_ATOMIC_16: { 10918 struct scsi_rw_16 *cdb; 10919 10920 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10921 10922 *lba = scsi_8btou64(cdb->addr); 10923 *len = scsi_4btoul(cdb->length); 10924 break; 10925 } 10926 case WRITE_VERIFY_16: { 10927 struct scsi_write_verify_16 *cdb; 10928 10929 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10930 10931 *lba = scsi_8btou64(cdb->addr); 10932 *len = scsi_4btoul(cdb->length); 10933 break; 10934 } 10935 case WRITE_SAME_10: { 10936 struct scsi_write_same_10 *cdb; 10937 10938 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10939 10940 *lba = scsi_4btoul(cdb->addr); 10941 *len = scsi_2btoul(cdb->length); 10942 break; 10943 } 10944 case WRITE_SAME_16: { 10945 struct scsi_write_same_16 *cdb; 10946 10947 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10948 10949 *lba = scsi_8btou64(cdb->addr); 10950 *len = scsi_4btoul(cdb->length); 10951 break; 10952 } 10953 case VERIFY_10: { 10954 struct scsi_verify_10 *cdb; 10955 10956 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10957 10958 *lba = scsi_4btoul(cdb->addr); 10959 *len = scsi_2btoul(cdb->length); 10960 break; 10961 } 10962 case VERIFY_12: { 10963 struct scsi_verify_12 *cdb; 10964 10965 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10966 10967 *lba = scsi_4btoul(cdb->addr); 10968 *len = scsi_4btoul(cdb->length); 10969 break; 10970 } 10971 case VERIFY_16: { 10972 struct scsi_verify_16 *cdb; 10973 10974 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10975 10976 *lba = scsi_8btou64(cdb->addr); 10977 *len = scsi_4btoul(cdb->length); 10978 break; 10979 } 10980 case UNMAP: { 10981 *lba = 0; 10982 *len = UINT64_MAX; 10983 break; 10984 } 10985 default: 10986 return (1); 10987 break; /* NOTREACHED */ 10988 } 10989 10990 return (0); 10991} 10992 10993static ctl_action 10994ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10995{ 10996 uint64_t endlba1, endlba2; 10997 10998 endlba1 = lba1 + len1 - 1; 10999 endlba2 = lba2 + len2 - 1; 11000 11001 if ((endlba1 < lba2) 11002 || (endlba2 < lba1)) 11003 return (CTL_ACTION_PASS); 11004 else 11005 return (CTL_ACTION_BLOCK); 11006} 11007 11008static int 11009ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 11010{ 11011 struct ctl_ptr_len_flags *ptrlen; 11012 struct scsi_unmap_desc *buf, *end, *range; 11013 uint64_t lba; 11014 uint32_t len; 11015 11016 /* If not UNMAP -- go other way. */ 11017 if (io->io_hdr.io_type != CTL_IO_SCSI || 11018 io->scsiio.cdb[0] != UNMAP) 11019 return (CTL_ACTION_ERROR); 11020 11021 /* If UNMAP without data -- block and wait for data. */ 11022 ptrlen = (struct ctl_ptr_len_flags *) 11023 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 11024 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 11025 ptrlen->ptr == NULL) 11026 return (CTL_ACTION_BLOCK); 11027 11028 /* UNMAP with data -- check for collision. */ 11029 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 11030 end = buf + ptrlen->len / sizeof(*buf); 11031 for (range = buf; range < end; range++) { 11032 lba = scsi_8btou64(range->lba); 11033 len = scsi_4btoul(range->length); 11034 if ((lba < lba2 + len2) && (lba + len > lba2)) 11035 return (CTL_ACTION_BLOCK); 11036 } 11037 return (CTL_ACTION_PASS); 11038} 11039 11040static ctl_action 11041ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 11042{ 11043 uint64_t lba1, lba2; 11044 uint64_t len1, len2; 11045 int retval; 11046 11047 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 11048 return (CTL_ACTION_ERROR); 11049 11050 retval = ctl_extent_check_unmap(io2, lba1, len1); 11051 if (retval != CTL_ACTION_ERROR) 11052 return (retval); 11053 11054 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 11055 return (CTL_ACTION_ERROR); 11056 11057 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 11058} 11059 11060static ctl_action 11061ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 11062 union ctl_io *ooa_io) 11063{ 11064 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 11065 ctl_serialize_action *serialize_row; 11066 11067 /* 11068 * The initiator attempted multiple untagged commands at the same 11069 * time. Can't do that. 11070 */ 11071 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11072 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11073 && ((pending_io->io_hdr.nexus.targ_port == 11074 ooa_io->io_hdr.nexus.targ_port) 11075 && (pending_io->io_hdr.nexus.initid.id == 11076 ooa_io->io_hdr.nexus.initid.id)) 11077 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 11078 return (CTL_ACTION_OVERLAP); 11079 11080 /* 11081 * The initiator attempted to send multiple tagged commands with 11082 * the same ID. (It's fine if different initiators have the same 11083 * tag ID.) 11084 * 11085 * Even if all of those conditions are true, we don't kill the I/O 11086 * if the command ahead of us has been aborted. We won't end up 11087 * sending it to the FETD, and it's perfectly legal to resend a 11088 * command with the same tag number as long as the previous 11089 * instance of this tag number has been aborted somehow. 11090 */ 11091 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11092 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11093 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 11094 && ((pending_io->io_hdr.nexus.targ_port == 11095 ooa_io->io_hdr.nexus.targ_port) 11096 && (pending_io->io_hdr.nexus.initid.id == 11097 ooa_io->io_hdr.nexus.initid.id)) 11098 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 11099 return (CTL_ACTION_OVERLAP_TAG); 11100 11101 /* 11102 * If we get a head of queue tag, SAM-3 says that we should 11103 * immediately execute it. 11104 * 11105 * What happens if this command would normally block for some other 11106 * reason? e.g. a request sense with a head of queue tag 11107 * immediately after a write. Normally that would block, but this 11108 * will result in its getting executed immediately... 11109 * 11110 * We currently return "pass" instead of "skip", so we'll end up 11111 * going through the rest of the queue to check for overlapped tags. 11112 * 11113 * XXX KDM check for other types of blockage first?? 11114 */ 11115 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11116 return (CTL_ACTION_PASS); 11117 11118 /* 11119 * Ordered tags have to block until all items ahead of them 11120 * have completed. If we get called with an ordered tag, we always 11121 * block, if something else is ahead of us in the queue. 11122 */ 11123 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11124 return (CTL_ACTION_BLOCK); 11125 11126 /* 11127 * Simple tags get blocked until all head of queue and ordered tags 11128 * ahead of them have completed. I'm lumping untagged commands in 11129 * with simple tags here. XXX KDM is that the right thing to do? 11130 */ 11131 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11132 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11133 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11134 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11135 return (CTL_ACTION_BLOCK); 11136 11137 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11138 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11139 11140 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11141 11142 switch (serialize_row[pending_entry->seridx]) { 11143 case CTL_SER_BLOCK: 11144 return (CTL_ACTION_BLOCK); 11145 case CTL_SER_EXTENT: 11146 return (ctl_extent_check(pending_io, ooa_io)); 11147 case CTL_SER_EXTENTOPT: 11148 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11149 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11150 return (ctl_extent_check(pending_io, ooa_io)); 11151 /* FALLTHROUGH */ 11152 case CTL_SER_PASS: 11153 return (CTL_ACTION_PASS); 11154 case CTL_SER_BLOCKOPT: 11155 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11156 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11157 return (CTL_ACTION_BLOCK); 11158 return (CTL_ACTION_PASS); 11159 case CTL_SER_SKIP: 11160 return (CTL_ACTION_SKIP); 11161 default: 11162 panic("invalid serialization value %d", 11163 serialize_row[pending_entry->seridx]); 11164 } 11165 11166 return (CTL_ACTION_ERROR); 11167} 11168 11169/* 11170 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11171 * Assumptions: 11172 * - pending_io is generally either incoming, or on the blocked queue 11173 * - starting I/O is the I/O we want to start the check with. 11174 */ 11175static ctl_action 11176ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11177 union ctl_io *starting_io) 11178{ 11179 union ctl_io *ooa_io; 11180 ctl_action action; 11181 11182 mtx_assert(&lun->lun_lock, MA_OWNED); 11183 11184 /* 11185 * Run back along the OOA queue, starting with the current 11186 * blocked I/O and going through every I/O before it on the 11187 * queue. If starting_io is NULL, we'll just end up returning 11188 * CTL_ACTION_PASS. 11189 */ 11190 for (ooa_io = starting_io; ooa_io != NULL; 11191 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11192 ooa_links)){ 11193 11194 /* 11195 * This routine just checks to see whether 11196 * cur_blocked is blocked by ooa_io, which is ahead 11197 * of it in the queue. It doesn't queue/dequeue 11198 * cur_blocked. 11199 */ 11200 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11201 switch (action) { 11202 case CTL_ACTION_BLOCK: 11203 case CTL_ACTION_OVERLAP: 11204 case CTL_ACTION_OVERLAP_TAG: 11205 case CTL_ACTION_SKIP: 11206 case CTL_ACTION_ERROR: 11207 return (action); 11208 break; /* NOTREACHED */ 11209 case CTL_ACTION_PASS: 11210 break; 11211 default: 11212 panic("invalid action %d", action); 11213 break; /* NOTREACHED */ 11214 } 11215 } 11216 11217 return (CTL_ACTION_PASS); 11218} 11219 11220/* 11221 * Assumptions: 11222 * - An I/O has just completed, and has been removed from the per-LUN OOA 11223 * queue, so some items on the blocked queue may now be unblocked. 11224 */ 11225static int 11226ctl_check_blocked(struct ctl_lun *lun) 11227{ 11228 union ctl_io *cur_blocked, *next_blocked; 11229 11230 mtx_assert(&lun->lun_lock, MA_OWNED); 11231 11232 /* 11233 * Run forward from the head of the blocked queue, checking each 11234 * entry against the I/Os prior to it on the OOA queue to see if 11235 * there is still any blockage. 11236 * 11237 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11238 * with our removing a variable on it while it is traversing the 11239 * list. 11240 */ 11241 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11242 cur_blocked != NULL; cur_blocked = next_blocked) { 11243 union ctl_io *prev_ooa; 11244 ctl_action action; 11245 11246 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11247 blocked_links); 11248 11249 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11250 ctl_ooaq, ooa_links); 11251 11252 /* 11253 * If cur_blocked happens to be the first item in the OOA 11254 * queue now, prev_ooa will be NULL, and the action 11255 * returned will just be CTL_ACTION_PASS. 11256 */ 11257 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11258 11259 switch (action) { 11260 case CTL_ACTION_BLOCK: 11261 /* Nothing to do here, still blocked */ 11262 break; 11263 case CTL_ACTION_OVERLAP: 11264 case CTL_ACTION_OVERLAP_TAG: 11265 /* 11266 * This shouldn't happen! In theory we've already 11267 * checked this command for overlap... 11268 */ 11269 break; 11270 case CTL_ACTION_PASS: 11271 case CTL_ACTION_SKIP: { 11272 struct ctl_softc *softc; 11273 const struct ctl_cmd_entry *entry; 11274 uint32_t initidx; 11275 int isc_retval; 11276 11277 /* 11278 * The skip case shouldn't happen, this transaction 11279 * should have never made it onto the blocked queue. 11280 */ 11281 /* 11282 * This I/O is no longer blocked, we can remove it 11283 * from the blocked queue. Since this is a TAILQ 11284 * (doubly linked list), we can do O(1) removals 11285 * from any place on the list. 11286 */ 11287 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11288 blocked_links); 11289 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11290 11291 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11292 /* 11293 * Need to send IO back to original side to 11294 * run 11295 */ 11296 union ctl_ha_msg msg_info; 11297 11298 msg_info.hdr.original_sc = 11299 cur_blocked->io_hdr.original_sc; 11300 msg_info.hdr.serializing_sc = cur_blocked; 11301 msg_info.hdr.msg_type = CTL_MSG_R2R; 11302 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11303 &msg_info, sizeof(msg_info), 0)) > 11304 CTL_HA_STATUS_SUCCESS) { 11305 printf("CTL:Check Blocked error from " 11306 "ctl_ha_msg_send %d\n", 11307 isc_retval); 11308 } 11309 break; 11310 } 11311 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11312 softc = control_softc; 11313 11314 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11315 11316 /* 11317 * Check this I/O for LUN state changes that may 11318 * have happened while this command was blocked. 11319 * The LUN state may have been changed by a command 11320 * ahead of us in the queue, so we need to re-check 11321 * for any states that can be caused by SCSI 11322 * commands. 11323 */ 11324 if (ctl_scsiio_lun_check(softc, lun, entry, 11325 &cur_blocked->scsiio) == 0) { 11326 cur_blocked->io_hdr.flags |= 11327 CTL_FLAG_IS_WAS_ON_RTR; 11328 ctl_enqueue_rtr(cur_blocked); 11329 } else 11330 ctl_done(cur_blocked); 11331 break; 11332 } 11333 default: 11334 /* 11335 * This probably shouldn't happen -- we shouldn't 11336 * get CTL_ACTION_ERROR, or anything else. 11337 */ 11338 break; 11339 } 11340 } 11341 11342 return (CTL_RETVAL_COMPLETE); 11343} 11344 11345/* 11346 * This routine (with one exception) checks LUN flags that can be set by 11347 * commands ahead of us in the OOA queue. These flags have to be checked 11348 * when a command initially comes in, and when we pull a command off the 11349 * blocked queue and are preparing to execute it. The reason we have to 11350 * check these flags for commands on the blocked queue is that the LUN 11351 * state may have been changed by a command ahead of us while we're on the 11352 * blocked queue. 11353 * 11354 * Ordering is somewhat important with these checks, so please pay 11355 * careful attention to the placement of any new checks. 11356 */ 11357static int 11358ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11359 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11360{ 11361 int retval; 11362 uint32_t residx; 11363 11364 retval = 0; 11365 11366 mtx_assert(&lun->lun_lock, MA_OWNED); 11367 11368 /* 11369 * If this shelf is a secondary shelf controller, we have to reject 11370 * any media access commands. 11371 */ 11372 if ((ctl_softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 11373 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 11374 ctl_set_lun_standby(ctsio); 11375 retval = 1; 11376 goto bailout; 11377 } 11378 11379 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11380 if (lun->flags & CTL_LUN_READONLY) { 11381 ctl_set_sense(ctsio, /*current_error*/ 1, 11382 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11383 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11384 retval = 1; 11385 goto bailout; 11386 } 11387 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11388 .eca_and_aen & SCP_SWP) != 0) { 11389 ctl_set_sense(ctsio, /*current_error*/ 1, 11390 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11391 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11392 retval = 1; 11393 goto bailout; 11394 } 11395 } 11396 11397 /* 11398 * Check for a reservation conflict. If this command isn't allowed 11399 * even on reserved LUNs, and if this initiator isn't the one who 11400 * reserved us, reject the command with a reservation conflict. 11401 */ 11402 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11403 if ((lun->flags & CTL_LUN_RESERVED) 11404 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11405 if (lun->res_idx != residx) { 11406 ctl_set_reservation_conflict(ctsio); 11407 retval = 1; 11408 goto bailout; 11409 } 11410 } 11411 11412 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11413 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11414 /* No reservation or command is allowed. */; 11415 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11416 (lun->res_type == SPR_TYPE_WR_EX || 11417 lun->res_type == SPR_TYPE_WR_EX_RO || 11418 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11419 /* The command is allowed for Write Exclusive resv. */; 11420 } else { 11421 /* 11422 * if we aren't registered or it's a res holder type 11423 * reservation and this isn't the res holder then set a 11424 * conflict. 11425 */ 11426 if (lun->pr_keys[residx] == 0 11427 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11428 ctl_set_reservation_conflict(ctsio); 11429 retval = 1; 11430 goto bailout; 11431 } 11432 11433 } 11434 11435 if ((lun->flags & CTL_LUN_OFFLINE) 11436 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11437 ctl_set_lun_not_ready(ctsio); 11438 retval = 1; 11439 goto bailout; 11440 } 11441 11442 /* 11443 * If the LUN is stopped, see if this particular command is allowed 11444 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11445 */ 11446 if ((lun->flags & CTL_LUN_STOPPED) 11447 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11448 /* "Logical unit not ready, initializing cmd. required" */ 11449 ctl_set_lun_stopped(ctsio); 11450 retval = 1; 11451 goto bailout; 11452 } 11453 11454 if ((lun->flags & CTL_LUN_INOPERABLE) 11455 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11456 /* "Medium format corrupted" */ 11457 ctl_set_medium_format_corrupted(ctsio); 11458 retval = 1; 11459 goto bailout; 11460 } 11461 11462bailout: 11463 return (retval); 11464 11465} 11466 11467static void 11468ctl_failover_io(union ctl_io *io, int have_lock) 11469{ 11470 ctl_set_busy(&io->scsiio); 11471 ctl_done(io); 11472} 11473 11474static void 11475ctl_failover(void) 11476{ 11477 struct ctl_lun *lun; 11478 struct ctl_softc *ctl_softc; 11479 union ctl_io *next_io, *pending_io; 11480 union ctl_io *io; 11481 int lun_idx; 11482 int i; 11483 11484 ctl_softc = control_softc; 11485 11486 mtx_lock(&ctl_softc->ctl_lock); 11487 /* 11488 * Remove any cmds from the other SC from the rtr queue. These 11489 * will obviously only be for LUNs for which we're the primary. 11490 * We can't send status or get/send data for these commands. 11491 * Since they haven't been executed yet, we can just remove them. 11492 * We'll either abort them or delete them below, depending on 11493 * which HA mode we're in. 11494 */ 11495#ifdef notyet 11496 mtx_lock(&ctl_softc->queue_lock); 11497 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11498 io != NULL; io = next_io) { 11499 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11500 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11501 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11502 ctl_io_hdr, links); 11503 } 11504 mtx_unlock(&ctl_softc->queue_lock); 11505#endif 11506 11507 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11508 lun = ctl_softc->ctl_luns[lun_idx]; 11509 if (lun==NULL) 11510 continue; 11511 11512 /* 11513 * Processor LUNs are primary on both sides. 11514 * XXX will this always be true? 11515 */ 11516 if (lun->be_lun->lun_type == T_PROCESSOR) 11517 continue; 11518 11519 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11520 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11521 printf("FAILOVER: primary lun %d\n", lun_idx); 11522 /* 11523 * Remove all commands from the other SC. First from the 11524 * blocked queue then from the ooa queue. Once we have 11525 * removed them. Call ctl_check_blocked to see if there 11526 * is anything that can run. 11527 */ 11528 for (io = (union ctl_io *)TAILQ_FIRST( 11529 &lun->blocked_queue); io != NULL; io = next_io) { 11530 11531 next_io = (union ctl_io *)TAILQ_NEXT( 11532 &io->io_hdr, blocked_links); 11533 11534 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11535 TAILQ_REMOVE(&lun->blocked_queue, 11536 &io->io_hdr,blocked_links); 11537 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11538 TAILQ_REMOVE(&lun->ooa_queue, 11539 &io->io_hdr, ooa_links); 11540 11541 ctl_free_io(io); 11542 } 11543 } 11544 11545 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11546 io != NULL; io = next_io) { 11547 11548 next_io = (union ctl_io *)TAILQ_NEXT( 11549 &io->io_hdr, ooa_links); 11550 11551 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11552 11553 TAILQ_REMOVE(&lun->ooa_queue, 11554 &io->io_hdr, 11555 ooa_links); 11556 11557 ctl_free_io(io); 11558 } 11559 } 11560 ctl_check_blocked(lun); 11561 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11562 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11563 11564 printf("FAILOVER: primary lun %d\n", lun_idx); 11565 /* 11566 * Abort all commands from the other SC. We can't 11567 * send status back for them now. These should get 11568 * cleaned up when they are completed or come out 11569 * for a datamove operation. 11570 */ 11571 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11572 io != NULL; io = next_io) { 11573 next_io = (union ctl_io *)TAILQ_NEXT( 11574 &io->io_hdr, ooa_links); 11575 11576 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11577 io->io_hdr.flags |= CTL_FLAG_ABORT; 11578 } 11579 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11580 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11581 11582 printf("FAILOVER: secondary lun %d\n", lun_idx); 11583 11584 lun->flags |= CTL_LUN_PRIMARY_SC; 11585 11586 /* 11587 * We send all I/O that was sent to this controller 11588 * and redirected to the other side back with 11589 * busy status, and have the initiator retry it. 11590 * Figuring out how much data has been transferred, 11591 * etc. and picking up where we left off would be 11592 * very tricky. 11593 * 11594 * XXX KDM need to remove I/O from the blocked 11595 * queue as well! 11596 */ 11597 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11598 &lun->ooa_queue); pending_io != NULL; 11599 pending_io = next_io) { 11600 11601 next_io = (union ctl_io *)TAILQ_NEXT( 11602 &pending_io->io_hdr, ooa_links); 11603 11604 pending_io->io_hdr.flags &= 11605 ~CTL_FLAG_SENT_2OTHER_SC; 11606 11607 if (pending_io->io_hdr.flags & 11608 CTL_FLAG_IO_ACTIVE) { 11609 pending_io->io_hdr.flags |= 11610 CTL_FLAG_FAILOVER; 11611 } else { 11612 ctl_set_busy(&pending_io->scsiio); 11613 ctl_done(pending_io); 11614 } 11615 } 11616 11617 /* 11618 * Build Unit Attention 11619 */ 11620 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11621 lun->pending_ua[i] |= 11622 CTL_UA_ASYM_ACC_CHANGE; 11623 } 11624 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11625 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11626 printf("FAILOVER: secondary lun %d\n", lun_idx); 11627 /* 11628 * if the first io on the OOA is not on the RtR queue 11629 * add it. 11630 */ 11631 lun->flags |= CTL_LUN_PRIMARY_SC; 11632 11633 pending_io = (union ctl_io *)TAILQ_FIRST( 11634 &lun->ooa_queue); 11635 if (pending_io==NULL) { 11636 printf("Nothing on OOA queue\n"); 11637 continue; 11638 } 11639 11640 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11641 if ((pending_io->io_hdr.flags & 11642 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11643 pending_io->io_hdr.flags |= 11644 CTL_FLAG_IS_WAS_ON_RTR; 11645 ctl_enqueue_rtr(pending_io); 11646 } 11647#if 0 11648 else 11649 { 11650 printf("Tag 0x%04x is running\n", 11651 pending_io->scsiio.tag_num); 11652 } 11653#endif 11654 11655 next_io = (union ctl_io *)TAILQ_NEXT( 11656 &pending_io->io_hdr, ooa_links); 11657 for (pending_io=next_io; pending_io != NULL; 11658 pending_io = next_io) { 11659 pending_io->io_hdr.flags &= 11660 ~CTL_FLAG_SENT_2OTHER_SC; 11661 next_io = (union ctl_io *)TAILQ_NEXT( 11662 &pending_io->io_hdr, ooa_links); 11663 if (pending_io->io_hdr.flags & 11664 CTL_FLAG_IS_WAS_ON_RTR) { 11665#if 0 11666 printf("Tag 0x%04x is running\n", 11667 pending_io->scsiio.tag_num); 11668#endif 11669 continue; 11670 } 11671 11672 switch (ctl_check_ooa(lun, pending_io, 11673 (union ctl_io *)TAILQ_PREV( 11674 &pending_io->io_hdr, ctl_ooaq, 11675 ooa_links))) { 11676 11677 case CTL_ACTION_BLOCK: 11678 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11679 &pending_io->io_hdr, 11680 blocked_links); 11681 pending_io->io_hdr.flags |= 11682 CTL_FLAG_BLOCKED; 11683 break; 11684 case CTL_ACTION_PASS: 11685 case CTL_ACTION_SKIP: 11686 pending_io->io_hdr.flags |= 11687 CTL_FLAG_IS_WAS_ON_RTR; 11688 ctl_enqueue_rtr(pending_io); 11689 break; 11690 case CTL_ACTION_OVERLAP: 11691 ctl_set_overlapped_cmd( 11692 (struct ctl_scsiio *)pending_io); 11693 ctl_done(pending_io); 11694 break; 11695 case CTL_ACTION_OVERLAP_TAG: 11696 ctl_set_overlapped_tag( 11697 (struct ctl_scsiio *)pending_io, 11698 pending_io->scsiio.tag_num & 0xff); 11699 ctl_done(pending_io); 11700 break; 11701 case CTL_ACTION_ERROR: 11702 default: 11703 ctl_set_internal_failure( 11704 (struct ctl_scsiio *)pending_io, 11705 0, // sks_valid 11706 0); //retry count 11707 ctl_done(pending_io); 11708 break; 11709 } 11710 } 11711 11712 /* 11713 * Build Unit Attention 11714 */ 11715 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11716 lun->pending_ua[i] |= 11717 CTL_UA_ASYM_ACC_CHANGE; 11718 } 11719 } else { 11720 panic("Unhandled HA mode failover, LUN flags = %#x, " 11721 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11722 } 11723 } 11724 ctl_pause_rtr = 0; 11725 mtx_unlock(&ctl_softc->ctl_lock); 11726} 11727 11728static int 11729ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11730{ 11731 struct ctl_lun *lun; 11732 const struct ctl_cmd_entry *entry; 11733 uint32_t initidx, targ_lun; 11734 int retval; 11735 11736 retval = 0; 11737 11738 lun = NULL; 11739 11740 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11741 if ((targ_lun < CTL_MAX_LUNS) 11742 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) { 11743 /* 11744 * If the LUN is invalid, pretend that it doesn't exist. 11745 * It will go away as soon as all pending I/O has been 11746 * completed. 11747 */ 11748 mtx_lock(&lun->lun_lock); 11749 if (lun->flags & CTL_LUN_DISABLED) { 11750 mtx_unlock(&lun->lun_lock); 11751 lun = NULL; 11752 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11753 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11754 } else { 11755 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11756 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11757 lun->be_lun; 11758 if (lun->be_lun->lun_type == T_PROCESSOR) { 11759 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11760 } 11761 11762 /* 11763 * Every I/O goes into the OOA queue for a 11764 * particular LUN, and stays there until completion. 11765 */ 11766 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11767 ooa_links); 11768 } 11769 } else { 11770 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11771 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11772 } 11773 11774 /* Get command entry and return error if it is unsuppotyed. */ 11775 entry = ctl_validate_command(ctsio); 11776 if (entry == NULL) { 11777 if (lun) 11778 mtx_unlock(&lun->lun_lock); 11779 return (retval); 11780 } 11781 11782 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11783 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11784 11785 /* 11786 * Check to see whether we can send this command to LUNs that don't 11787 * exist. This should pretty much only be the case for inquiry 11788 * and request sense. Further checks, below, really require having 11789 * a LUN, so we can't really check the command anymore. Just put 11790 * it on the rtr queue. 11791 */ 11792 if (lun == NULL) { 11793 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11794 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11795 ctl_enqueue_rtr((union ctl_io *)ctsio); 11796 return (retval); 11797 } 11798 11799 ctl_set_unsupported_lun(ctsio); 11800 ctl_done((union ctl_io *)ctsio); 11801 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11802 return (retval); 11803 } else { 11804 /* 11805 * Make sure we support this particular command on this LUN. 11806 * e.g., we don't support writes to the control LUN. 11807 */ 11808 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11809 mtx_unlock(&lun->lun_lock); 11810 ctl_set_invalid_opcode(ctsio); 11811 ctl_done((union ctl_io *)ctsio); 11812 return (retval); 11813 } 11814 } 11815 11816 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11817 11818#ifdef CTL_WITH_CA 11819 /* 11820 * If we've got a request sense, it'll clear the contingent 11821 * allegiance condition. Otherwise, if we have a CA condition for 11822 * this initiator, clear it, because it sent down a command other 11823 * than request sense. 11824 */ 11825 if ((ctsio->cdb[0] != REQUEST_SENSE) 11826 && (ctl_is_set(lun->have_ca, initidx))) 11827 ctl_clear_mask(lun->have_ca, initidx); 11828#endif 11829 11830 /* 11831 * If the command has this flag set, it handles its own unit 11832 * attention reporting, we shouldn't do anything. Otherwise we 11833 * check for any pending unit attentions, and send them back to the 11834 * initiator. We only do this when a command initially comes in, 11835 * not when we pull it off the blocked queue. 11836 * 11837 * According to SAM-3, section 5.3.2, the order that things get 11838 * presented back to the host is basically unit attentions caused 11839 * by some sort of reset event, busy status, reservation conflicts 11840 * or task set full, and finally any other status. 11841 * 11842 * One issue here is that some of the unit attentions we report 11843 * don't fall into the "reset" category (e.g. "reported luns data 11844 * has changed"). So reporting it here, before the reservation 11845 * check, may be technically wrong. I guess the only thing to do 11846 * would be to check for and report the reset events here, and then 11847 * check for the other unit attention types after we check for a 11848 * reservation conflict. 11849 * 11850 * XXX KDM need to fix this 11851 */ 11852 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11853 ctl_ua_type ua_type; 11854 11855 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11856 scsi_sense_data_type sense_format; 11857 11858 if (lun != NULL) 11859 sense_format = (lun->flags & 11860 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11861 SSD_TYPE_FIXED; 11862 else 11863 sense_format = SSD_TYPE_FIXED; 11864 11865 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11866 &ctsio->sense_data, sense_format); 11867 if (ua_type != CTL_UA_NONE) { 11868 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11869 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11870 CTL_AUTOSENSE; 11871 ctsio->sense_len = SSD_FULL_SIZE; 11872 mtx_unlock(&lun->lun_lock); 11873 ctl_done((union ctl_io *)ctsio); 11874 return (retval); 11875 } 11876 } 11877 } 11878 11879 11880 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11881 mtx_unlock(&lun->lun_lock); 11882 ctl_done((union ctl_io *)ctsio); 11883 return (retval); 11884 } 11885 11886 /* 11887 * XXX CHD this is where we want to send IO to other side if 11888 * this LUN is secondary on this SC. We will need to make a copy 11889 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11890 * the copy we send as FROM_OTHER. 11891 * We also need to stuff the address of the original IO so we can 11892 * find it easily. Something similar will need be done on the other 11893 * side so when we are done we can find the copy. 11894 */ 11895 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11896 union ctl_ha_msg msg_info; 11897 int isc_retval; 11898 11899 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11900 11901 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11902 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11903#if 0 11904 printf("1. ctsio %p\n", ctsio); 11905#endif 11906 msg_info.hdr.serializing_sc = NULL; 11907 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11908 msg_info.scsi.tag_num = ctsio->tag_num; 11909 msg_info.scsi.tag_type = ctsio->tag_type; 11910 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11911 11912 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11913 11914 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11915 (void *)&msg_info, sizeof(msg_info), 0)) > 11916 CTL_HA_STATUS_SUCCESS) { 11917 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11918 isc_retval); 11919 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11920 } else { 11921#if 0 11922 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11923#endif 11924 } 11925 11926 /* 11927 * XXX KDM this I/O is off the incoming queue, but hasn't 11928 * been inserted on any other queue. We may need to come 11929 * up with a holding queue while we wait for serialization 11930 * so that we have an idea of what we're waiting for from 11931 * the other side. 11932 */ 11933 mtx_unlock(&lun->lun_lock); 11934 return (retval); 11935 } 11936 11937 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11938 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11939 ctl_ooaq, ooa_links))) { 11940 case CTL_ACTION_BLOCK: 11941 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11942 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11943 blocked_links); 11944 mtx_unlock(&lun->lun_lock); 11945 return (retval); 11946 case CTL_ACTION_PASS: 11947 case CTL_ACTION_SKIP: 11948 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11949 mtx_unlock(&lun->lun_lock); 11950 ctl_enqueue_rtr((union ctl_io *)ctsio); 11951 break; 11952 case CTL_ACTION_OVERLAP: 11953 mtx_unlock(&lun->lun_lock); 11954 ctl_set_overlapped_cmd(ctsio); 11955 ctl_done((union ctl_io *)ctsio); 11956 break; 11957 case CTL_ACTION_OVERLAP_TAG: 11958 mtx_unlock(&lun->lun_lock); 11959 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11960 ctl_done((union ctl_io *)ctsio); 11961 break; 11962 case CTL_ACTION_ERROR: 11963 default: 11964 mtx_unlock(&lun->lun_lock); 11965 ctl_set_internal_failure(ctsio, 11966 /*sks_valid*/ 0, 11967 /*retry_count*/ 0); 11968 ctl_done((union ctl_io *)ctsio); 11969 break; 11970 } 11971 return (retval); 11972} 11973 11974const struct ctl_cmd_entry * 11975ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11976{ 11977 const struct ctl_cmd_entry *entry; 11978 int service_action; 11979 11980 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11981 if (sa) 11982 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11983 if (entry->flags & CTL_CMD_FLAG_SA5) { 11984 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11985 entry = &((const struct ctl_cmd_entry *) 11986 entry->execute)[service_action]; 11987 } 11988 return (entry); 11989} 11990 11991const struct ctl_cmd_entry * 11992ctl_validate_command(struct ctl_scsiio *ctsio) 11993{ 11994 const struct ctl_cmd_entry *entry; 11995 int i, sa; 11996 uint8_t diff; 11997 11998 entry = ctl_get_cmd_entry(ctsio, &sa); 11999 if (entry->execute == NULL) { 12000 if (sa) 12001 ctl_set_invalid_field(ctsio, 12002 /*sks_valid*/ 1, 12003 /*command*/ 1, 12004 /*field*/ 1, 12005 /*bit_valid*/ 1, 12006 /*bit*/ 4); 12007 else 12008 ctl_set_invalid_opcode(ctsio); 12009 ctl_done((union ctl_io *)ctsio); 12010 return (NULL); 12011 } 12012 KASSERT(entry->length > 0, 12013 ("Not defined length for command 0x%02x/0x%02x", 12014 ctsio->cdb[0], ctsio->cdb[1])); 12015 for (i = 1; i < entry->length; i++) { 12016 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 12017 if (diff == 0) 12018 continue; 12019 ctl_set_invalid_field(ctsio, 12020 /*sks_valid*/ 1, 12021 /*command*/ 1, 12022 /*field*/ i, 12023 /*bit_valid*/ 1, 12024 /*bit*/ fls(diff) - 1); 12025 ctl_done((union ctl_io *)ctsio); 12026 return (NULL); 12027 } 12028 return (entry); 12029} 12030 12031static int 12032ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 12033{ 12034 12035 switch (lun_type) { 12036 case T_PROCESSOR: 12037 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 12038 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 12039 return (0); 12040 break; 12041 case T_DIRECT: 12042 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 12043 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 12044 return (0); 12045 break; 12046 default: 12047 return (0); 12048 } 12049 return (1); 12050} 12051 12052static int 12053ctl_scsiio(struct ctl_scsiio *ctsio) 12054{ 12055 int retval; 12056 const struct ctl_cmd_entry *entry; 12057 12058 retval = CTL_RETVAL_COMPLETE; 12059 12060 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 12061 12062 entry = ctl_get_cmd_entry(ctsio, NULL); 12063 12064 /* 12065 * If this I/O has been aborted, just send it straight to 12066 * ctl_done() without executing it. 12067 */ 12068 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 12069 ctl_done((union ctl_io *)ctsio); 12070 goto bailout; 12071 } 12072 12073 /* 12074 * All the checks should have been handled by ctl_scsiio_precheck(). 12075 * We should be clear now to just execute the I/O. 12076 */ 12077 retval = entry->execute(ctsio); 12078 12079bailout: 12080 return (retval); 12081} 12082 12083/* 12084 * Since we only implement one target right now, a bus reset simply resets 12085 * our single target. 12086 */ 12087static int 12088ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 12089{ 12090 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 12091} 12092 12093static int 12094ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 12095 ctl_ua_type ua_type) 12096{ 12097 struct ctl_lun *lun; 12098 int retval; 12099 12100 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12101 union ctl_ha_msg msg_info; 12102 12103 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12104 msg_info.hdr.nexus = io->io_hdr.nexus; 12105 if (ua_type==CTL_UA_TARG_RESET) 12106 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 12107 else 12108 msg_info.task.task_action = CTL_TASK_BUS_RESET; 12109 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12110 msg_info.hdr.original_sc = NULL; 12111 msg_info.hdr.serializing_sc = NULL; 12112 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12113 (void *)&msg_info, sizeof(msg_info), 0)) { 12114 } 12115 } 12116 retval = 0; 12117 12118 mtx_lock(&ctl_softc->ctl_lock); 12119 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 12120 retval += ctl_lun_reset(lun, io, ua_type); 12121 mtx_unlock(&ctl_softc->ctl_lock); 12122 12123 return (retval); 12124} 12125 12126/* 12127 * The LUN should always be set. The I/O is optional, and is used to 12128 * distinguish between I/Os sent by this initiator, and by other 12129 * initiators. We set unit attention for initiators other than this one. 12130 * SAM-3 is vague on this point. It does say that a unit attention should 12131 * be established for other initiators when a LUN is reset (see section 12132 * 5.7.3), but it doesn't specifically say that the unit attention should 12133 * be established for this particular initiator when a LUN is reset. Here 12134 * is the relevant text, from SAM-3 rev 8: 12135 * 12136 * 5.7.2 When a SCSI initiator port aborts its own tasks 12137 * 12138 * When a SCSI initiator port causes its own task(s) to be aborted, no 12139 * notification that the task(s) have been aborted shall be returned to 12140 * the SCSI initiator port other than the completion response for the 12141 * command or task management function action that caused the task(s) to 12142 * be aborted and notification(s) associated with related effects of the 12143 * action (e.g., a reset unit attention condition). 12144 * 12145 * XXX KDM for now, we're setting unit attention for all initiators. 12146 */ 12147static int 12148ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 12149{ 12150 union ctl_io *xio; 12151#if 0 12152 uint32_t initindex; 12153#endif 12154 int i; 12155 12156 mtx_lock(&lun->lun_lock); 12157 /* 12158 * Run through the OOA queue and abort each I/O. 12159 */ 12160#if 0 12161 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12162#endif 12163 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12164 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12165 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 12166 } 12167 12168 /* 12169 * This version sets unit attention for every 12170 */ 12171#if 0 12172 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12173 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12174 if (initindex == i) 12175 continue; 12176 lun->pending_ua[i] |= ua_type; 12177 } 12178#endif 12179 12180 /* 12181 * A reset (any kind, really) clears reservations established with 12182 * RESERVE/RELEASE. It does not clear reservations established 12183 * with PERSISTENT RESERVE OUT, but we don't support that at the 12184 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 12185 * reservations made with the RESERVE/RELEASE commands, because 12186 * those commands are obsolete in SPC-3. 12187 */ 12188 lun->flags &= ~CTL_LUN_RESERVED; 12189 12190 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12191#ifdef CTL_WITH_CA 12192 ctl_clear_mask(lun->have_ca, i); 12193#endif 12194 lun->pending_ua[i] |= ua_type; 12195 } 12196 mtx_unlock(&lun->lun_lock); 12197 12198 return (0); 12199} 12200 12201static void 12202ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12203 int other_sc) 12204{ 12205 union ctl_io *xio; 12206 12207 mtx_assert(&lun->lun_lock, MA_OWNED); 12208 12209 /* 12210 * Run through the OOA queue and attempt to find the given I/O. 12211 * The target port, initiator ID, tag type and tag number have to 12212 * match the values that we got from the initiator. If we have an 12213 * untagged command to abort, simply abort the first untagged command 12214 * we come to. We only allow one untagged command at a time of course. 12215 */ 12216 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12217 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12218 12219 if ((targ_port == UINT32_MAX || 12220 targ_port == xio->io_hdr.nexus.targ_port) && 12221 (init_id == UINT32_MAX || 12222 init_id == xio->io_hdr.nexus.initid.id)) { 12223 if (targ_port != xio->io_hdr.nexus.targ_port || 12224 init_id != xio->io_hdr.nexus.initid.id) 12225 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12226 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12227 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12228 union ctl_ha_msg msg_info; 12229 12230 msg_info.hdr.nexus = xio->io_hdr.nexus; 12231 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12232 msg_info.task.tag_num = xio->scsiio.tag_num; 12233 msg_info.task.tag_type = xio->scsiio.tag_type; 12234 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12235 msg_info.hdr.original_sc = NULL; 12236 msg_info.hdr.serializing_sc = NULL; 12237 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12238 (void *)&msg_info, sizeof(msg_info), 0); 12239 } 12240 } 12241 } 12242} 12243 12244static int 12245ctl_abort_task_set(union ctl_io *io) 12246{ 12247 struct ctl_softc *softc = control_softc; 12248 struct ctl_lun *lun; 12249 uint32_t targ_lun; 12250 12251 /* 12252 * Look up the LUN. 12253 */ 12254 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12255 mtx_lock(&softc->ctl_lock); 12256 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12257 lun = softc->ctl_luns[targ_lun]; 12258 else { 12259 mtx_unlock(&softc->ctl_lock); 12260 return (1); 12261 } 12262 12263 mtx_lock(&lun->lun_lock); 12264 mtx_unlock(&softc->ctl_lock); 12265 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12266 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12267 io->io_hdr.nexus.initid.id, 12268 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12269 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12270 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12271 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12272 } 12273 mtx_unlock(&lun->lun_lock); 12274 return (0); 12275} 12276 12277static int 12278ctl_i_t_nexus_reset(union ctl_io *io) 12279{ 12280 struct ctl_softc *softc = control_softc; 12281 struct ctl_lun *lun; 12282 uint32_t initindex, residx; 12283 12284 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12285 residx = ctl_get_resindex(&io->io_hdr.nexus); 12286 mtx_lock(&softc->ctl_lock); 12287 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12288 mtx_lock(&lun->lun_lock); 12289 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12290 io->io_hdr.nexus.initid.id, 12291 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12292#ifdef CTL_WITH_CA 12293 ctl_clear_mask(lun->have_ca, initindex); 12294#endif 12295 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12296 lun->flags &= ~CTL_LUN_RESERVED; 12297 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12298 mtx_unlock(&lun->lun_lock); 12299 } 12300 mtx_unlock(&softc->ctl_lock); 12301 return (0); 12302} 12303 12304static int 12305ctl_abort_task(union ctl_io *io) 12306{ 12307 union ctl_io *xio; 12308 struct ctl_lun *lun; 12309 struct ctl_softc *ctl_softc; 12310#if 0 12311 struct sbuf sb; 12312 char printbuf[128]; 12313#endif 12314 int found; 12315 uint32_t targ_lun; 12316 12317 ctl_softc = control_softc; 12318 found = 0; 12319 12320 /* 12321 * Look up the LUN. 12322 */ 12323 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12324 mtx_lock(&ctl_softc->ctl_lock); 12325 if ((targ_lun < CTL_MAX_LUNS) 12326 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12327 lun = ctl_softc->ctl_luns[targ_lun]; 12328 else { 12329 mtx_unlock(&ctl_softc->ctl_lock); 12330 return (1); 12331 } 12332 12333#if 0 12334 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12335 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12336#endif 12337 12338 mtx_lock(&lun->lun_lock); 12339 mtx_unlock(&ctl_softc->ctl_lock); 12340 /* 12341 * Run through the OOA queue and attempt to find the given I/O. 12342 * The target port, initiator ID, tag type and tag number have to 12343 * match the values that we got from the initiator. If we have an 12344 * untagged command to abort, simply abort the first untagged command 12345 * we come to. We only allow one untagged command at a time of course. 12346 */ 12347#if 0 12348 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12349#endif 12350 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12351 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12352#if 0 12353 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12354 12355 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12356 lun->lun, xio->scsiio.tag_num, 12357 xio->scsiio.tag_type, 12358 (xio->io_hdr.blocked_links.tqe_prev 12359 == NULL) ? "" : " BLOCKED", 12360 (xio->io_hdr.flags & 12361 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12362 (xio->io_hdr.flags & 12363 CTL_FLAG_ABORT) ? " ABORT" : "", 12364 (xio->io_hdr.flags & 12365 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12366 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12367 sbuf_finish(&sb); 12368 printf("%s\n", sbuf_data(&sb)); 12369#endif 12370 12371 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12372 && (xio->io_hdr.nexus.initid.id == 12373 io->io_hdr.nexus.initid.id)) { 12374 /* 12375 * If the abort says that the task is untagged, the 12376 * task in the queue must be untagged. Otherwise, 12377 * we just check to see whether the tag numbers 12378 * match. This is because the QLogic firmware 12379 * doesn't pass back the tag type in an abort 12380 * request. 12381 */ 12382#if 0 12383 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12384 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12385 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12386#endif 12387 /* 12388 * XXX KDM we've got problems with FC, because it 12389 * doesn't send down a tag type with aborts. So we 12390 * can only really go by the tag number... 12391 * This may cause problems with parallel SCSI. 12392 * Need to figure that out!! 12393 */ 12394 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12395 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12396 found = 1; 12397 if ((io->io_hdr.flags & 12398 CTL_FLAG_FROM_OTHER_SC) == 0 && 12399 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12400 union ctl_ha_msg msg_info; 12401 12402 io->io_hdr.flags |= 12403 CTL_FLAG_SENT_2OTHER_SC; 12404 msg_info.hdr.nexus = io->io_hdr.nexus; 12405 msg_info.task.task_action = 12406 CTL_TASK_ABORT_TASK; 12407 msg_info.task.tag_num = 12408 io->taskio.tag_num; 12409 msg_info.task.tag_type = 12410 io->taskio.tag_type; 12411 msg_info.hdr.msg_type = 12412 CTL_MSG_MANAGE_TASKS; 12413 msg_info.hdr.original_sc = NULL; 12414 msg_info.hdr.serializing_sc = NULL; 12415#if 0 12416 printf("Sent Abort to other side\n"); 12417#endif 12418 if (CTL_HA_STATUS_SUCCESS != 12419 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12420 (void *)&msg_info, 12421 sizeof(msg_info), 0)) { 12422 } 12423 } 12424#if 0 12425 printf("ctl_abort_task: found I/O to abort\n"); 12426#endif 12427 break; 12428 } 12429 } 12430 } 12431 mtx_unlock(&lun->lun_lock); 12432 12433 if (found == 0) { 12434 /* 12435 * This isn't really an error. It's entirely possible for 12436 * the abort and command completion to cross on the wire. 12437 * This is more of an informative/diagnostic error. 12438 */ 12439#if 0 12440 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12441 "%d:%d:%d:%d tag %d type %d\n", 12442 io->io_hdr.nexus.initid.id, 12443 io->io_hdr.nexus.targ_port, 12444 io->io_hdr.nexus.targ_target.id, 12445 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12446 io->taskio.tag_type); 12447#endif 12448 } 12449 return (0); 12450} 12451 12452static void 12453ctl_run_task(union ctl_io *io) 12454{ 12455 struct ctl_softc *ctl_softc = control_softc; 12456 int retval = 1; 12457 const char *task_desc; 12458 12459 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12460 12461 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12462 ("ctl_run_task: Unextected io_type %d\n", 12463 io->io_hdr.io_type)); 12464 12465 task_desc = ctl_scsi_task_string(&io->taskio); 12466 if (task_desc != NULL) { 12467#ifdef NEEDTOPORT 12468 csevent_log(CSC_CTL | CSC_SHELF_SW | 12469 CTL_TASK_REPORT, 12470 csevent_LogType_Trace, 12471 csevent_Severity_Information, 12472 csevent_AlertLevel_Green, 12473 csevent_FRU_Firmware, 12474 csevent_FRU_Unknown, 12475 "CTL: received task: %s",task_desc); 12476#endif 12477 } else { 12478#ifdef NEEDTOPORT 12479 csevent_log(CSC_CTL | CSC_SHELF_SW | 12480 CTL_TASK_REPORT, 12481 csevent_LogType_Trace, 12482 csevent_Severity_Information, 12483 csevent_AlertLevel_Green, 12484 csevent_FRU_Firmware, 12485 csevent_FRU_Unknown, 12486 "CTL: received unknown task " 12487 "type: %d (%#x)", 12488 io->taskio.task_action, 12489 io->taskio.task_action); 12490#endif 12491 } 12492 switch (io->taskio.task_action) { 12493 case CTL_TASK_ABORT_TASK: 12494 retval = ctl_abort_task(io); 12495 break; 12496 case CTL_TASK_ABORT_TASK_SET: 12497 case CTL_TASK_CLEAR_TASK_SET: 12498 retval = ctl_abort_task_set(io); 12499 break; 12500 case CTL_TASK_CLEAR_ACA: 12501 break; 12502 case CTL_TASK_I_T_NEXUS_RESET: 12503 retval = ctl_i_t_nexus_reset(io); 12504 break; 12505 case CTL_TASK_LUN_RESET: { 12506 struct ctl_lun *lun; 12507 uint32_t targ_lun; 12508 12509 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12510 mtx_lock(&ctl_softc->ctl_lock); 12511 if ((targ_lun < CTL_MAX_LUNS) 12512 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12513 lun = ctl_softc->ctl_luns[targ_lun]; 12514 else { 12515 mtx_unlock(&ctl_softc->ctl_lock); 12516 retval = 1; 12517 break; 12518 } 12519 12520 if (!(io->io_hdr.flags & 12521 CTL_FLAG_FROM_OTHER_SC)) { 12522 union ctl_ha_msg msg_info; 12523 12524 io->io_hdr.flags |= 12525 CTL_FLAG_SENT_2OTHER_SC; 12526 msg_info.hdr.msg_type = 12527 CTL_MSG_MANAGE_TASKS; 12528 msg_info.hdr.nexus = io->io_hdr.nexus; 12529 msg_info.task.task_action = 12530 CTL_TASK_LUN_RESET; 12531 msg_info.hdr.original_sc = NULL; 12532 msg_info.hdr.serializing_sc = NULL; 12533 if (CTL_HA_STATUS_SUCCESS != 12534 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12535 (void *)&msg_info, 12536 sizeof(msg_info), 0)) { 12537 } 12538 } 12539 12540 retval = ctl_lun_reset(lun, io, 12541 CTL_UA_LUN_RESET); 12542 mtx_unlock(&ctl_softc->ctl_lock); 12543 break; 12544 } 12545 case CTL_TASK_TARGET_RESET: 12546 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12547 break; 12548 case CTL_TASK_BUS_RESET: 12549 retval = ctl_bus_reset(ctl_softc, io); 12550 break; 12551 case CTL_TASK_PORT_LOGIN: 12552 break; 12553 case CTL_TASK_PORT_LOGOUT: 12554 break; 12555 default: 12556 printf("ctl_run_task: got unknown task management event %d\n", 12557 io->taskio.task_action); 12558 break; 12559 } 12560 if (retval == 0) 12561 io->io_hdr.status = CTL_SUCCESS; 12562 else 12563 io->io_hdr.status = CTL_ERROR; 12564 ctl_done(io); 12565} 12566 12567/* 12568 * For HA operation. Handle commands that come in from the other 12569 * controller. 12570 */ 12571static void 12572ctl_handle_isc(union ctl_io *io) 12573{ 12574 int free_io; 12575 struct ctl_lun *lun; 12576 struct ctl_softc *ctl_softc; 12577 uint32_t targ_lun; 12578 12579 ctl_softc = control_softc; 12580 12581 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12582 lun = ctl_softc->ctl_luns[targ_lun]; 12583 12584 switch (io->io_hdr.msg_type) { 12585 case CTL_MSG_SERIALIZE: 12586 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12587 break; 12588 case CTL_MSG_R2R: { 12589 const struct ctl_cmd_entry *entry; 12590 12591 /* 12592 * This is only used in SER_ONLY mode. 12593 */ 12594 free_io = 0; 12595 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12596 mtx_lock(&lun->lun_lock); 12597 if (ctl_scsiio_lun_check(ctl_softc, lun, 12598 entry, (struct ctl_scsiio *)io) != 0) { 12599 mtx_unlock(&lun->lun_lock); 12600 ctl_done(io); 12601 break; 12602 } 12603 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12604 mtx_unlock(&lun->lun_lock); 12605 ctl_enqueue_rtr(io); 12606 break; 12607 } 12608 case CTL_MSG_FINISH_IO: 12609 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12610 free_io = 0; 12611 ctl_done(io); 12612 } else { 12613 free_io = 1; 12614 mtx_lock(&lun->lun_lock); 12615 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12616 ooa_links); 12617 ctl_check_blocked(lun); 12618 mtx_unlock(&lun->lun_lock); 12619 } 12620 break; 12621 case CTL_MSG_PERS_ACTION: 12622 ctl_hndl_per_res_out_on_other_sc( 12623 (union ctl_ha_msg *)&io->presio.pr_msg); 12624 free_io = 1; 12625 break; 12626 case CTL_MSG_BAD_JUJU: 12627 free_io = 0; 12628 ctl_done(io); 12629 break; 12630 case CTL_MSG_DATAMOVE: 12631 /* Only used in XFER mode */ 12632 free_io = 0; 12633 ctl_datamove_remote(io); 12634 break; 12635 case CTL_MSG_DATAMOVE_DONE: 12636 /* Only used in XFER mode */ 12637 free_io = 0; 12638 io->scsiio.be_move_done(io); 12639 break; 12640 default: 12641 free_io = 1; 12642 printf("%s: Invalid message type %d\n", 12643 __func__, io->io_hdr.msg_type); 12644 break; 12645 } 12646 if (free_io) 12647 ctl_free_io(io); 12648 12649} 12650 12651 12652/* 12653 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12654 * there is no match. 12655 */ 12656static ctl_lun_error_pattern 12657ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12658{ 12659 const struct ctl_cmd_entry *entry; 12660 ctl_lun_error_pattern filtered_pattern, pattern; 12661 12662 pattern = desc->error_pattern; 12663 12664 /* 12665 * XXX KDM we need more data passed into this function to match a 12666 * custom pattern, and we actually need to implement custom pattern 12667 * matching. 12668 */ 12669 if (pattern & CTL_LUN_PAT_CMD) 12670 return (CTL_LUN_PAT_CMD); 12671 12672 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12673 return (CTL_LUN_PAT_ANY); 12674 12675 entry = ctl_get_cmd_entry(ctsio, NULL); 12676 12677 filtered_pattern = entry->pattern & pattern; 12678 12679 /* 12680 * If the user requested specific flags in the pattern (e.g. 12681 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12682 * flags. 12683 * 12684 * If the user did not specify any flags, it doesn't matter whether 12685 * or not the command supports the flags. 12686 */ 12687 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12688 (pattern & ~CTL_LUN_PAT_MASK)) 12689 return (CTL_LUN_PAT_NONE); 12690 12691 /* 12692 * If the user asked for a range check, see if the requested LBA 12693 * range overlaps with this command's LBA range. 12694 */ 12695 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12696 uint64_t lba1; 12697 uint64_t len1; 12698 ctl_action action; 12699 int retval; 12700 12701 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12702 if (retval != 0) 12703 return (CTL_LUN_PAT_NONE); 12704 12705 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12706 desc->lba_range.len); 12707 /* 12708 * A "pass" means that the LBA ranges don't overlap, so 12709 * this doesn't match the user's range criteria. 12710 */ 12711 if (action == CTL_ACTION_PASS) 12712 return (CTL_LUN_PAT_NONE); 12713 } 12714 12715 return (filtered_pattern); 12716} 12717 12718static void 12719ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12720{ 12721 struct ctl_error_desc *desc, *desc2; 12722 12723 mtx_assert(&lun->lun_lock, MA_OWNED); 12724 12725 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12726 ctl_lun_error_pattern pattern; 12727 /* 12728 * Check to see whether this particular command matches 12729 * the pattern in the descriptor. 12730 */ 12731 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12732 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12733 continue; 12734 12735 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12736 case CTL_LUN_INJ_ABORTED: 12737 ctl_set_aborted(&io->scsiio); 12738 break; 12739 case CTL_LUN_INJ_MEDIUM_ERR: 12740 ctl_set_medium_error(&io->scsiio); 12741 break; 12742 case CTL_LUN_INJ_UA: 12743 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12744 * OCCURRED */ 12745 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12746 break; 12747 case CTL_LUN_INJ_CUSTOM: 12748 /* 12749 * We're assuming the user knows what he is doing. 12750 * Just copy the sense information without doing 12751 * checks. 12752 */ 12753 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12754 ctl_min(sizeof(desc->custom_sense), 12755 sizeof(io->scsiio.sense_data))); 12756 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12757 io->scsiio.sense_len = SSD_FULL_SIZE; 12758 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12759 break; 12760 case CTL_LUN_INJ_NONE: 12761 default: 12762 /* 12763 * If this is an error injection type we don't know 12764 * about, clear the continuous flag (if it is set) 12765 * so it will get deleted below. 12766 */ 12767 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12768 break; 12769 } 12770 /* 12771 * By default, each error injection action is a one-shot 12772 */ 12773 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12774 continue; 12775 12776 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12777 12778 free(desc, M_CTL); 12779 } 12780} 12781 12782#ifdef CTL_IO_DELAY 12783static void 12784ctl_datamove_timer_wakeup(void *arg) 12785{ 12786 union ctl_io *io; 12787 12788 io = (union ctl_io *)arg; 12789 12790 ctl_datamove(io); 12791} 12792#endif /* CTL_IO_DELAY */ 12793 12794void 12795ctl_datamove(union ctl_io *io) 12796{ 12797 void (*fe_datamove)(union ctl_io *io); 12798 12799 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12800 12801 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12802 12803#ifdef CTL_TIME_IO 12804 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12805 char str[256]; 12806 char path_str[64]; 12807 struct sbuf sb; 12808 12809 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12810 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12811 12812 sbuf_cat(&sb, path_str); 12813 switch (io->io_hdr.io_type) { 12814 case CTL_IO_SCSI: 12815 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12816 sbuf_printf(&sb, "\n"); 12817 sbuf_cat(&sb, path_str); 12818 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12819 io->scsiio.tag_num, io->scsiio.tag_type); 12820 break; 12821 case CTL_IO_TASK: 12822 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12823 "Tag Type: %d\n", io->taskio.task_action, 12824 io->taskio.tag_num, io->taskio.tag_type); 12825 break; 12826 default: 12827 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12828 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12829 break; 12830 } 12831 sbuf_cat(&sb, path_str); 12832 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12833 (intmax_t)time_uptime - io->io_hdr.start_time); 12834 sbuf_finish(&sb); 12835 printf("%s", sbuf_data(&sb)); 12836 } 12837#endif /* CTL_TIME_IO */ 12838 12839#ifdef CTL_IO_DELAY 12840 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12841 struct ctl_lun *lun; 12842 12843 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12844 12845 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12846 } else { 12847 struct ctl_lun *lun; 12848 12849 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12850 if ((lun != NULL) 12851 && (lun->delay_info.datamove_delay > 0)) { 12852 struct callout *callout; 12853 12854 callout = (struct callout *)&io->io_hdr.timer_bytes; 12855 callout_init(callout, /*mpsafe*/ 1); 12856 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12857 callout_reset(callout, 12858 lun->delay_info.datamove_delay * hz, 12859 ctl_datamove_timer_wakeup, io); 12860 if (lun->delay_info.datamove_type == 12861 CTL_DELAY_TYPE_ONESHOT) 12862 lun->delay_info.datamove_delay = 0; 12863 return; 12864 } 12865 } 12866#endif 12867 12868 /* 12869 * This command has been aborted. Set the port status, so we fail 12870 * the data move. 12871 */ 12872 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12873 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12874 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12875 io->io_hdr.nexus.targ_port, 12876 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12877 io->io_hdr.nexus.targ_lun); 12878 io->io_hdr.port_status = 31337; 12879 /* 12880 * Note that the backend, in this case, will get the 12881 * callback in its context. In other cases it may get 12882 * called in the frontend's interrupt thread context. 12883 */ 12884 io->scsiio.be_move_done(io); 12885 return; 12886 } 12887 12888 /* Don't confuse frontend with zero length data move. */ 12889 if (io->scsiio.kern_data_len == 0) { 12890 io->scsiio.be_move_done(io); 12891 return; 12892 } 12893 12894 /* 12895 * If we're in XFER mode and this I/O is from the other shelf 12896 * controller, we need to send the DMA to the other side to 12897 * actually transfer the data to/from the host. In serialize only 12898 * mode the transfer happens below CTL and ctl_datamove() is only 12899 * called on the machine that originally received the I/O. 12900 */ 12901 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12902 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12903 union ctl_ha_msg msg; 12904 uint32_t sg_entries_sent; 12905 int do_sg_copy; 12906 int i; 12907 12908 memset(&msg, 0, sizeof(msg)); 12909 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12910 msg.hdr.original_sc = io->io_hdr.original_sc; 12911 msg.hdr.serializing_sc = io; 12912 msg.hdr.nexus = io->io_hdr.nexus; 12913 msg.dt.flags = io->io_hdr.flags; 12914 /* 12915 * We convert everything into a S/G list here. We can't 12916 * pass by reference, only by value between controllers. 12917 * So we can't pass a pointer to the S/G list, only as many 12918 * S/G entries as we can fit in here. If it's possible for 12919 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12920 * then we need to break this up into multiple transfers. 12921 */ 12922 if (io->scsiio.kern_sg_entries == 0) { 12923 msg.dt.kern_sg_entries = 1; 12924 /* 12925 * If this is in cached memory, flush the cache 12926 * before we send the DMA request to the other 12927 * controller. We want to do this in either the 12928 * read or the write case. The read case is 12929 * straightforward. In the write case, we want to 12930 * make sure nothing is in the local cache that 12931 * could overwrite the DMAed data. 12932 */ 12933 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12934 /* 12935 * XXX KDM use bus_dmamap_sync() here. 12936 */ 12937 } 12938 12939 /* 12940 * Convert to a physical address if this is a 12941 * virtual address. 12942 */ 12943 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12944 msg.dt.sg_list[0].addr = 12945 io->scsiio.kern_data_ptr; 12946 } else { 12947 /* 12948 * XXX KDM use busdma here! 12949 */ 12950#if 0 12951 msg.dt.sg_list[0].addr = (void *) 12952 vtophys(io->scsiio.kern_data_ptr); 12953#endif 12954 } 12955 12956 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12957 do_sg_copy = 0; 12958 } else { 12959 struct ctl_sg_entry *sgl; 12960 12961 do_sg_copy = 1; 12962 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12963 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12964 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12965 /* 12966 * XXX KDM use bus_dmamap_sync() here. 12967 */ 12968 } 12969 } 12970 12971 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12972 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12973 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12974 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12975 msg.dt.sg_sequence = 0; 12976 12977 /* 12978 * Loop until we've sent all of the S/G entries. On the 12979 * other end, we'll recompose these S/G entries into one 12980 * contiguous list before passing it to the 12981 */ 12982 for (sg_entries_sent = 0; sg_entries_sent < 12983 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12984 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12985 sizeof(msg.dt.sg_list[0])), 12986 msg.dt.kern_sg_entries - sg_entries_sent); 12987 12988 if (do_sg_copy != 0) { 12989 struct ctl_sg_entry *sgl; 12990 int j; 12991 12992 sgl = (struct ctl_sg_entry *) 12993 io->scsiio.kern_data_ptr; 12994 /* 12995 * If this is in cached memory, flush the cache 12996 * before we send the DMA request to the other 12997 * controller. We want to do this in either 12998 * the * read or the write case. The read 12999 * case is straightforward. In the write 13000 * case, we want to make sure nothing is 13001 * in the local cache that could overwrite 13002 * the DMAed data. 13003 */ 13004 13005 for (i = sg_entries_sent, j = 0; 13006 i < msg.dt.cur_sg_entries; i++, j++) { 13007 if ((io->io_hdr.flags & 13008 CTL_FLAG_NO_DATASYNC) == 0) { 13009 /* 13010 * XXX KDM use bus_dmamap_sync() 13011 */ 13012 } 13013 if ((io->io_hdr.flags & 13014 CTL_FLAG_BUS_ADDR) == 0) { 13015 /* 13016 * XXX KDM use busdma. 13017 */ 13018#if 0 13019 msg.dt.sg_list[j].addr =(void *) 13020 vtophys(sgl[i].addr); 13021#endif 13022 } else { 13023 msg.dt.sg_list[j].addr = 13024 sgl[i].addr; 13025 } 13026 msg.dt.sg_list[j].len = sgl[i].len; 13027 } 13028 } 13029 13030 sg_entries_sent += msg.dt.cur_sg_entries; 13031 if (sg_entries_sent >= msg.dt.kern_sg_entries) 13032 msg.dt.sg_last = 1; 13033 else 13034 msg.dt.sg_last = 0; 13035 13036 /* 13037 * XXX KDM drop and reacquire the lock here? 13038 */ 13039 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13040 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13041 /* 13042 * XXX do something here. 13043 */ 13044 } 13045 13046 msg.dt.sent_sg_entries = sg_entries_sent; 13047 } 13048 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13049 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 13050 ctl_failover_io(io, /*have_lock*/ 0); 13051 13052 } else { 13053 13054 /* 13055 * Lookup the fe_datamove() function for this particular 13056 * front end. 13057 */ 13058 fe_datamove = 13059 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13060 13061 fe_datamove(io); 13062 } 13063} 13064 13065static void 13066ctl_send_datamove_done(union ctl_io *io, int have_lock) 13067{ 13068 union ctl_ha_msg msg; 13069 int isc_status; 13070 13071 memset(&msg, 0, sizeof(msg)); 13072 13073 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 13074 msg.hdr.original_sc = io; 13075 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13076 msg.hdr.nexus = io->io_hdr.nexus; 13077 msg.hdr.status = io->io_hdr.status; 13078 msg.scsi.tag_num = io->scsiio.tag_num; 13079 msg.scsi.tag_type = io->scsiio.tag_type; 13080 msg.scsi.scsi_status = io->scsiio.scsi_status; 13081 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13082 sizeof(io->scsiio.sense_data)); 13083 msg.scsi.sense_len = io->scsiio.sense_len; 13084 msg.scsi.sense_residual = io->scsiio.sense_residual; 13085 msg.scsi.fetd_status = io->io_hdr.port_status; 13086 msg.scsi.residual = io->scsiio.residual; 13087 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13088 13089 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13090 ctl_failover_io(io, /*have_lock*/ have_lock); 13091 return; 13092 } 13093 13094 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 13095 if (isc_status > CTL_HA_STATUS_SUCCESS) { 13096 /* XXX do something if this fails */ 13097 } 13098 13099} 13100 13101/* 13102 * The DMA to the remote side is done, now we need to tell the other side 13103 * we're done so it can continue with its data movement. 13104 */ 13105static void 13106ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 13107{ 13108 union ctl_io *io; 13109 13110 io = rq->context; 13111 13112 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13113 printf("%s: ISC DMA write failed with error %d", __func__, 13114 rq->ret); 13115 ctl_set_internal_failure(&io->scsiio, 13116 /*sks_valid*/ 1, 13117 /*retry_count*/ rq->ret); 13118 } 13119 13120 ctl_dt_req_free(rq); 13121 13122 /* 13123 * In this case, we had to malloc the memory locally. Free it. 13124 */ 13125 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13126 int i; 13127 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13128 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13129 } 13130 /* 13131 * The data is in local and remote memory, so now we need to send 13132 * status (good or back) back to the other side. 13133 */ 13134 ctl_send_datamove_done(io, /*have_lock*/ 0); 13135} 13136 13137/* 13138 * We've moved the data from the host/controller into local memory. Now we 13139 * need to push it over to the remote controller's memory. 13140 */ 13141static int 13142ctl_datamove_remote_dm_write_cb(union ctl_io *io) 13143{ 13144 int retval; 13145 13146 retval = 0; 13147 13148 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 13149 ctl_datamove_remote_write_cb); 13150 13151 return (retval); 13152} 13153 13154static void 13155ctl_datamove_remote_write(union ctl_io *io) 13156{ 13157 int retval; 13158 void (*fe_datamove)(union ctl_io *io); 13159 13160 /* 13161 * - Get the data from the host/HBA into local memory. 13162 * - DMA memory from the local controller to the remote controller. 13163 * - Send status back to the remote controller. 13164 */ 13165 13166 retval = ctl_datamove_remote_sgl_setup(io); 13167 if (retval != 0) 13168 return; 13169 13170 /* Switch the pointer over so the FETD knows what to do */ 13171 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13172 13173 /* 13174 * Use a custom move done callback, since we need to send completion 13175 * back to the other controller, not to the backend on this side. 13176 */ 13177 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 13178 13179 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13180 13181 fe_datamove(io); 13182 13183 return; 13184 13185} 13186 13187static int 13188ctl_datamove_remote_dm_read_cb(union ctl_io *io) 13189{ 13190#if 0 13191 char str[256]; 13192 char path_str[64]; 13193 struct sbuf sb; 13194#endif 13195 13196 /* 13197 * In this case, we had to malloc the memory locally. Free it. 13198 */ 13199 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13200 int i; 13201 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13202 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13203 } 13204 13205#if 0 13206 scsi_path_string(io, path_str, sizeof(path_str)); 13207 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13208 sbuf_cat(&sb, path_str); 13209 scsi_command_string(&io->scsiio, NULL, &sb); 13210 sbuf_printf(&sb, "\n"); 13211 sbuf_cat(&sb, path_str); 13212 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13213 io->scsiio.tag_num, io->scsiio.tag_type); 13214 sbuf_cat(&sb, path_str); 13215 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 13216 io->io_hdr.flags, io->io_hdr.status); 13217 sbuf_finish(&sb); 13218 printk("%s", sbuf_data(&sb)); 13219#endif 13220 13221 13222 /* 13223 * The read is done, now we need to send status (good or bad) back 13224 * to the other side. 13225 */ 13226 ctl_send_datamove_done(io, /*have_lock*/ 0); 13227 13228 return (0); 13229} 13230 13231static void 13232ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13233{ 13234 union ctl_io *io; 13235 void (*fe_datamove)(union ctl_io *io); 13236 13237 io = rq->context; 13238 13239 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13240 printf("%s: ISC DMA read failed with error %d", __func__, 13241 rq->ret); 13242 ctl_set_internal_failure(&io->scsiio, 13243 /*sks_valid*/ 1, 13244 /*retry_count*/ rq->ret); 13245 } 13246 13247 ctl_dt_req_free(rq); 13248 13249 /* Switch the pointer over so the FETD knows what to do */ 13250 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13251 13252 /* 13253 * Use a custom move done callback, since we need to send completion 13254 * back to the other controller, not to the backend on this side. 13255 */ 13256 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13257 13258 /* XXX KDM add checks like the ones in ctl_datamove? */ 13259 13260 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13261 13262 fe_datamove(io); 13263} 13264 13265static int 13266ctl_datamove_remote_sgl_setup(union ctl_io *io) 13267{ 13268 struct ctl_sg_entry *local_sglist, *remote_sglist; 13269 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13270 struct ctl_softc *softc; 13271 int retval; 13272 int i; 13273 13274 retval = 0; 13275 softc = control_softc; 13276 13277 local_sglist = io->io_hdr.local_sglist; 13278 local_dma_sglist = io->io_hdr.local_dma_sglist; 13279 remote_sglist = io->io_hdr.remote_sglist; 13280 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13281 13282 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13283 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13284 local_sglist[i].len = remote_sglist[i].len; 13285 13286 /* 13287 * XXX Detect the situation where the RS-level I/O 13288 * redirector on the other side has already read the 13289 * data off of the AOR RS on this side, and 13290 * transferred it to remote (mirror) memory on the 13291 * other side. Since we already have the data in 13292 * memory here, we just need to use it. 13293 * 13294 * XXX KDM this can probably be removed once we 13295 * get the cache device code in and take the 13296 * current AOR implementation out. 13297 */ 13298#ifdef NEEDTOPORT 13299 if ((remote_sglist[i].addr >= 13300 (void *)vtophys(softc->mirr->addr)) 13301 && (remote_sglist[i].addr < 13302 ((void *)vtophys(softc->mirr->addr) + 13303 CacheMirrorOffset))) { 13304 local_sglist[i].addr = remote_sglist[i].addr - 13305 CacheMirrorOffset; 13306 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13307 CTL_FLAG_DATA_IN) 13308 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13309 } else { 13310 local_sglist[i].addr = remote_sglist[i].addr + 13311 CacheMirrorOffset; 13312 } 13313#endif 13314#if 0 13315 printf("%s: local %p, remote %p, len %d\n", 13316 __func__, local_sglist[i].addr, 13317 remote_sglist[i].addr, local_sglist[i].len); 13318#endif 13319 } 13320 } else { 13321 uint32_t len_to_go; 13322 13323 /* 13324 * In this case, we don't have automatically allocated 13325 * memory for this I/O on this controller. This typically 13326 * happens with internal CTL I/O -- e.g. inquiry, mode 13327 * sense, etc. Anything coming from RAIDCore will have 13328 * a mirror area available. 13329 */ 13330 len_to_go = io->scsiio.kern_data_len; 13331 13332 /* 13333 * Clear the no datasync flag, we have to use malloced 13334 * buffers. 13335 */ 13336 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13337 13338 /* 13339 * The difficult thing here is that the size of the various 13340 * S/G segments may be different than the size from the 13341 * remote controller. That'll make it harder when DMAing 13342 * the data back to the other side. 13343 */ 13344 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13345 sizeof(io->io_hdr.remote_sglist[0])) && 13346 (len_to_go > 0); i++) { 13347 local_sglist[i].len = ctl_min(len_to_go, 131072); 13348 CTL_SIZE_8B(local_dma_sglist[i].len, 13349 local_sglist[i].len); 13350 local_sglist[i].addr = 13351 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13352 13353 local_dma_sglist[i].addr = local_sglist[i].addr; 13354 13355 if (local_sglist[i].addr == NULL) { 13356 int j; 13357 13358 printf("malloc failed for %zd bytes!", 13359 local_dma_sglist[i].len); 13360 for (j = 0; j < i; j++) { 13361 free(local_sglist[j].addr, M_CTL); 13362 } 13363 ctl_set_internal_failure(&io->scsiio, 13364 /*sks_valid*/ 1, 13365 /*retry_count*/ 4857); 13366 retval = 1; 13367 goto bailout_error; 13368 13369 } 13370 /* XXX KDM do we need a sync here? */ 13371 13372 len_to_go -= local_sglist[i].len; 13373 } 13374 /* 13375 * Reset the number of S/G entries accordingly. The 13376 * original number of S/G entries is available in 13377 * rem_sg_entries. 13378 */ 13379 io->scsiio.kern_sg_entries = i; 13380 13381#if 0 13382 printf("%s: kern_sg_entries = %d\n", __func__, 13383 io->scsiio.kern_sg_entries); 13384 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13385 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13386 local_sglist[i].addr, local_sglist[i].len, 13387 local_dma_sglist[i].len); 13388#endif 13389 } 13390 13391 13392 return (retval); 13393 13394bailout_error: 13395 13396 ctl_send_datamove_done(io, /*have_lock*/ 0); 13397 13398 return (retval); 13399} 13400 13401static int 13402ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13403 ctl_ha_dt_cb callback) 13404{ 13405 struct ctl_ha_dt_req *rq; 13406 struct ctl_sg_entry *remote_sglist, *local_sglist; 13407 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13408 uint32_t local_used, remote_used, total_used; 13409 int retval; 13410 int i, j; 13411 13412 retval = 0; 13413 13414 rq = ctl_dt_req_alloc(); 13415 13416 /* 13417 * If we failed to allocate the request, and if the DMA didn't fail 13418 * anyway, set busy status. This is just a resource allocation 13419 * failure. 13420 */ 13421 if ((rq == NULL) 13422 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13423 ctl_set_busy(&io->scsiio); 13424 13425 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13426 13427 if (rq != NULL) 13428 ctl_dt_req_free(rq); 13429 13430 /* 13431 * The data move failed. We need to return status back 13432 * to the other controller. No point in trying to DMA 13433 * data to the remote controller. 13434 */ 13435 13436 ctl_send_datamove_done(io, /*have_lock*/ 0); 13437 13438 retval = 1; 13439 13440 goto bailout; 13441 } 13442 13443 local_sglist = io->io_hdr.local_sglist; 13444 local_dma_sglist = io->io_hdr.local_dma_sglist; 13445 remote_sglist = io->io_hdr.remote_sglist; 13446 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13447 local_used = 0; 13448 remote_used = 0; 13449 total_used = 0; 13450 13451 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13452 rq->ret = CTL_HA_STATUS_SUCCESS; 13453 rq->context = io; 13454 callback(rq); 13455 goto bailout; 13456 } 13457 13458 /* 13459 * Pull/push the data over the wire from/to the other controller. 13460 * This takes into account the possibility that the local and 13461 * remote sglists may not be identical in terms of the size of 13462 * the elements and the number of elements. 13463 * 13464 * One fundamental assumption here is that the length allocated for 13465 * both the local and remote sglists is identical. Otherwise, we've 13466 * essentially got a coding error of some sort. 13467 */ 13468 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13469 int isc_ret; 13470 uint32_t cur_len, dma_length; 13471 uint8_t *tmp_ptr; 13472 13473 rq->id = CTL_HA_DATA_CTL; 13474 rq->command = command; 13475 rq->context = io; 13476 13477 /* 13478 * Both pointers should be aligned. But it is possible 13479 * that the allocation length is not. They should both 13480 * also have enough slack left over at the end, though, 13481 * to round up to the next 8 byte boundary. 13482 */ 13483 cur_len = ctl_min(local_sglist[i].len - local_used, 13484 remote_sglist[j].len - remote_used); 13485 13486 /* 13487 * In this case, we have a size issue and need to decrease 13488 * the size, except in the case where we actually have less 13489 * than 8 bytes left. In that case, we need to increase 13490 * the DMA length to get the last bit. 13491 */ 13492 if ((cur_len & 0x7) != 0) { 13493 if (cur_len > 0x7) { 13494 cur_len = cur_len - (cur_len & 0x7); 13495 dma_length = cur_len; 13496 } else { 13497 CTL_SIZE_8B(dma_length, cur_len); 13498 } 13499 13500 } else 13501 dma_length = cur_len; 13502 13503 /* 13504 * If we had to allocate memory for this I/O, instead of using 13505 * the non-cached mirror memory, we'll need to flush the cache 13506 * before trying to DMA to the other controller. 13507 * 13508 * We could end up doing this multiple times for the same 13509 * segment if we have a larger local segment than remote 13510 * segment. That shouldn't be an issue. 13511 */ 13512 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13513 /* 13514 * XXX KDM use bus_dmamap_sync() here. 13515 */ 13516 } 13517 13518 rq->size = dma_length; 13519 13520 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13521 tmp_ptr += local_used; 13522 13523 /* Use physical addresses when talking to ISC hardware */ 13524 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13525 /* XXX KDM use busdma */ 13526#if 0 13527 rq->local = vtophys(tmp_ptr); 13528#endif 13529 } else 13530 rq->local = tmp_ptr; 13531 13532 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13533 tmp_ptr += remote_used; 13534 rq->remote = tmp_ptr; 13535 13536 rq->callback = NULL; 13537 13538 local_used += cur_len; 13539 if (local_used >= local_sglist[i].len) { 13540 i++; 13541 local_used = 0; 13542 } 13543 13544 remote_used += cur_len; 13545 if (remote_used >= remote_sglist[j].len) { 13546 j++; 13547 remote_used = 0; 13548 } 13549 total_used += cur_len; 13550 13551 if (total_used >= io->scsiio.kern_data_len) 13552 rq->callback = callback; 13553 13554 if ((rq->size & 0x7) != 0) { 13555 printf("%s: warning: size %d is not on 8b boundary\n", 13556 __func__, rq->size); 13557 } 13558 if (((uintptr_t)rq->local & 0x7) != 0) { 13559 printf("%s: warning: local %p not on 8b boundary\n", 13560 __func__, rq->local); 13561 } 13562 if (((uintptr_t)rq->remote & 0x7) != 0) { 13563 printf("%s: warning: remote %p not on 8b boundary\n", 13564 __func__, rq->local); 13565 } 13566#if 0 13567 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13568 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13569 rq->local, rq->remote, rq->size); 13570#endif 13571 13572 isc_ret = ctl_dt_single(rq); 13573 if (isc_ret == CTL_HA_STATUS_WAIT) 13574 continue; 13575 13576 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13577 rq->ret = CTL_HA_STATUS_SUCCESS; 13578 } else { 13579 rq->ret = isc_ret; 13580 } 13581 callback(rq); 13582 goto bailout; 13583 } 13584 13585bailout: 13586 return (retval); 13587 13588} 13589 13590static void 13591ctl_datamove_remote_read(union ctl_io *io) 13592{ 13593 int retval; 13594 int i; 13595 13596 /* 13597 * This will send an error to the other controller in the case of a 13598 * failure. 13599 */ 13600 retval = ctl_datamove_remote_sgl_setup(io); 13601 if (retval != 0) 13602 return; 13603 13604 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13605 ctl_datamove_remote_read_cb); 13606 if ((retval != 0) 13607 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13608 /* 13609 * Make sure we free memory if there was an error.. The 13610 * ctl_datamove_remote_xfer() function will send the 13611 * datamove done message, or call the callback with an 13612 * error if there is a problem. 13613 */ 13614 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13615 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13616 } 13617 13618 return; 13619} 13620 13621/* 13622 * Process a datamove request from the other controller. This is used for 13623 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13624 * first. Once that is complete, the data gets DMAed into the remote 13625 * controller's memory. For reads, we DMA from the remote controller's 13626 * memory into our memory first, and then move it out to the FETD. 13627 */ 13628static void 13629ctl_datamove_remote(union ctl_io *io) 13630{ 13631 struct ctl_softc *softc; 13632 13633 softc = control_softc; 13634 13635 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13636 13637 /* 13638 * Note that we look for an aborted I/O here, but don't do some of 13639 * the other checks that ctl_datamove() normally does. 13640 * We don't need to run the datamove delay code, since that should 13641 * have been done if need be on the other controller. 13642 */ 13643 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13644 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13645 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13646 io->io_hdr.nexus.targ_port, 13647 io->io_hdr.nexus.targ_target.id, 13648 io->io_hdr.nexus.targ_lun); 13649 io->io_hdr.port_status = 31338; 13650 ctl_send_datamove_done(io, /*have_lock*/ 0); 13651 return; 13652 } 13653 13654 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13655 ctl_datamove_remote_write(io); 13656 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13657 ctl_datamove_remote_read(io); 13658 } else { 13659 union ctl_ha_msg msg; 13660 struct scsi_sense_data *sense; 13661 uint8_t sks[3]; 13662 int retry_count; 13663 13664 memset(&msg, 0, sizeof(msg)); 13665 13666 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13667 msg.hdr.status = CTL_SCSI_ERROR; 13668 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13669 13670 retry_count = 4243; 13671 13672 sense = &msg.scsi.sense_data; 13673 sks[0] = SSD_SCS_VALID; 13674 sks[1] = (retry_count >> 8) & 0xff; 13675 sks[2] = retry_count & 0xff; 13676 13677 /* "Internal target failure" */ 13678 scsi_set_sense_data(sense, 13679 /*sense_format*/ SSD_TYPE_NONE, 13680 /*current_error*/ 1, 13681 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13682 /*asc*/ 0x44, 13683 /*ascq*/ 0x00, 13684 /*type*/ SSD_ELEM_SKS, 13685 /*size*/ sizeof(sks), 13686 /*data*/ sks, 13687 SSD_ELEM_NONE); 13688 13689 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13690 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13691 ctl_failover_io(io, /*have_lock*/ 1); 13692 return; 13693 } 13694 13695 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13696 CTL_HA_STATUS_SUCCESS) { 13697 /* XXX KDM what to do if this fails? */ 13698 } 13699 return; 13700 } 13701 13702} 13703 13704static int 13705ctl_process_done(union ctl_io *io) 13706{ 13707 struct ctl_lun *lun; 13708 struct ctl_softc *ctl_softc = control_softc; 13709 void (*fe_done)(union ctl_io *io); 13710 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13711 13712 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13713 13714 fe_done = 13715 control_softc->ctl_ports[targ_port]->fe_done; 13716 13717#ifdef CTL_TIME_IO 13718 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13719 char str[256]; 13720 char path_str[64]; 13721 struct sbuf sb; 13722 13723 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13724 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13725 13726 sbuf_cat(&sb, path_str); 13727 switch (io->io_hdr.io_type) { 13728 case CTL_IO_SCSI: 13729 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13730 sbuf_printf(&sb, "\n"); 13731 sbuf_cat(&sb, path_str); 13732 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13733 io->scsiio.tag_num, io->scsiio.tag_type); 13734 break; 13735 case CTL_IO_TASK: 13736 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13737 "Tag Type: %d\n", io->taskio.task_action, 13738 io->taskio.tag_num, io->taskio.tag_type); 13739 break; 13740 default: 13741 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13742 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13743 break; 13744 } 13745 sbuf_cat(&sb, path_str); 13746 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13747 (intmax_t)time_uptime - io->io_hdr.start_time); 13748 sbuf_finish(&sb); 13749 printf("%s", sbuf_data(&sb)); 13750 } 13751#endif /* CTL_TIME_IO */ 13752 13753 switch (io->io_hdr.io_type) { 13754 case CTL_IO_SCSI: 13755 break; 13756 case CTL_IO_TASK: 13757 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13758 ctl_io_error_print(io, NULL); 13759 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13760 ctl_free_io(io); 13761 else 13762 fe_done(io); 13763 return (CTL_RETVAL_COMPLETE); 13764 default: 13765 panic("ctl_process_done: invalid io type %d\n", 13766 io->io_hdr.io_type); 13767 break; /* NOTREACHED */ 13768 } 13769 13770 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13771 if (lun == NULL) { 13772 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13773 io->io_hdr.nexus.targ_mapped_lun)); 13774 goto bailout; 13775 } 13776 13777 mtx_lock(&lun->lun_lock); 13778 13779 /* 13780 * Check to see if we have any errors to inject here. We only 13781 * inject errors for commands that don't already have errors set. 13782 */ 13783 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13784 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13785 ctl_inject_error(lun, io); 13786 13787 /* 13788 * XXX KDM how do we treat commands that aren't completed 13789 * successfully? 13790 * 13791 * XXX KDM should we also track I/O latency? 13792 */ 13793 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13794 io->io_hdr.io_type == CTL_IO_SCSI) { 13795#ifdef CTL_TIME_IO 13796 struct bintime cur_bt; 13797#endif 13798 int type; 13799 13800 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13801 CTL_FLAG_DATA_IN) 13802 type = CTL_STATS_READ; 13803 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13804 CTL_FLAG_DATA_OUT) 13805 type = CTL_STATS_WRITE; 13806 else 13807 type = CTL_STATS_NO_IO; 13808 13809 lun->stats.ports[targ_port].bytes[type] += 13810 io->scsiio.kern_total_len; 13811 lun->stats.ports[targ_port].operations[type]++; 13812#ifdef CTL_TIME_IO 13813 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13814 &io->io_hdr.dma_bt); 13815 lun->stats.ports[targ_port].num_dmas[type] += 13816 io->io_hdr.num_dmas; 13817 getbintime(&cur_bt); 13818 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13819 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13820#endif 13821 } 13822 13823 /* 13824 * Remove this from the OOA queue. 13825 */ 13826 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13827 13828 /* 13829 * Run through the blocked queue on this LUN and see if anything 13830 * has become unblocked, now that this transaction is done. 13831 */ 13832 ctl_check_blocked(lun); 13833 13834 /* 13835 * If the LUN has been invalidated, free it if there is nothing 13836 * left on its OOA queue. 13837 */ 13838 if ((lun->flags & CTL_LUN_INVALID) 13839 && TAILQ_EMPTY(&lun->ooa_queue)) { 13840 mtx_unlock(&lun->lun_lock); 13841 mtx_lock(&ctl_softc->ctl_lock); 13842 ctl_free_lun(lun); 13843 mtx_unlock(&ctl_softc->ctl_lock); 13844 } else 13845 mtx_unlock(&lun->lun_lock); 13846 13847bailout: 13848 13849 /* 13850 * If this command has been aborted, make sure we set the status 13851 * properly. The FETD is responsible for freeing the I/O and doing 13852 * whatever it needs to do to clean up its state. 13853 */ 13854 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13855 ctl_set_task_aborted(&io->scsiio); 13856 13857 /* 13858 * If enabled, print command error status. 13859 * We don't print UAs unless debugging was enabled explicitly. 13860 */ 13861 do { 13862 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13863 break; 13864 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13865 break; 13866 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13867 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13868 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13869 int error_code, sense_key, asc, ascq; 13870 13871 scsi_extract_sense_len(&io->scsiio.sense_data, 13872 io->scsiio.sense_len, &error_code, &sense_key, 13873 &asc, &ascq, /*show_errors*/ 0); 13874 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13875 break; 13876 } 13877 13878 ctl_io_error_print(io, NULL); 13879 } while (0); 13880 13881 /* 13882 * Tell the FETD or the other shelf controller we're done with this 13883 * command. Note that only SCSI commands get to this point. Task 13884 * management commands are completed above. 13885 * 13886 * We only send status to the other controller if we're in XFER 13887 * mode. In SER_ONLY mode, the I/O is done on the controller that 13888 * received the I/O (from CTL's perspective), and so the status is 13889 * generated there. 13890 * 13891 * XXX KDM if we hold the lock here, we could cause a deadlock 13892 * if the frontend comes back in in this context to queue 13893 * something. 13894 */ 13895 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13896 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13897 union ctl_ha_msg msg; 13898 13899 memset(&msg, 0, sizeof(msg)); 13900 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13901 msg.hdr.original_sc = io->io_hdr.original_sc; 13902 msg.hdr.nexus = io->io_hdr.nexus; 13903 msg.hdr.status = io->io_hdr.status; 13904 msg.scsi.scsi_status = io->scsiio.scsi_status; 13905 msg.scsi.tag_num = io->scsiio.tag_num; 13906 msg.scsi.tag_type = io->scsiio.tag_type; 13907 msg.scsi.sense_len = io->scsiio.sense_len; 13908 msg.scsi.sense_residual = io->scsiio.sense_residual; 13909 msg.scsi.residual = io->scsiio.residual; 13910 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13911 sizeof(io->scsiio.sense_data)); 13912 /* 13913 * We copy this whether or not this is an I/O-related 13914 * command. Otherwise, we'd have to go and check to see 13915 * whether it's a read/write command, and it really isn't 13916 * worth it. 13917 */ 13918 memcpy(&msg.scsi.lbalen, 13919 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13920 sizeof(msg.scsi.lbalen)); 13921 13922 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13923 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13924 /* XXX do something here */ 13925 } 13926 13927 ctl_free_io(io); 13928 } else 13929 fe_done(io); 13930 13931 return (CTL_RETVAL_COMPLETE); 13932} 13933 13934#ifdef CTL_WITH_CA 13935/* 13936 * Front end should call this if it doesn't do autosense. When the request 13937 * sense comes back in from the initiator, we'll dequeue this and send it. 13938 */ 13939int 13940ctl_queue_sense(union ctl_io *io) 13941{ 13942 struct ctl_lun *lun; 13943 struct ctl_softc *ctl_softc; 13944 uint32_t initidx, targ_lun; 13945 13946 ctl_softc = control_softc; 13947 13948 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13949 13950 /* 13951 * LUN lookup will likely move to the ctl_work_thread() once we 13952 * have our new queueing infrastructure (that doesn't put things on 13953 * a per-LUN queue initially). That is so that we can handle 13954 * things like an INQUIRY to a LUN that we don't have enabled. We 13955 * can't deal with that right now. 13956 */ 13957 mtx_lock(&ctl_softc->ctl_lock); 13958 13959 /* 13960 * If we don't have a LUN for this, just toss the sense 13961 * information. 13962 */ 13963 targ_lun = io->io_hdr.nexus.targ_lun; 13964 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13965 if ((targ_lun < CTL_MAX_LUNS) 13966 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13967 lun = ctl_softc->ctl_luns[targ_lun]; 13968 else 13969 goto bailout; 13970 13971 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13972 13973 mtx_lock(&lun->lun_lock); 13974 /* 13975 * Already have CA set for this LUN...toss the sense information. 13976 */ 13977 if (ctl_is_set(lun->have_ca, initidx)) { 13978 mtx_unlock(&lun->lun_lock); 13979 goto bailout; 13980 } 13981 13982 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13983 ctl_min(sizeof(lun->pending_sense[initidx]), 13984 sizeof(io->scsiio.sense_data))); 13985 ctl_set_mask(lun->have_ca, initidx); 13986 mtx_unlock(&lun->lun_lock); 13987 13988bailout: 13989 mtx_unlock(&ctl_softc->ctl_lock); 13990 13991 ctl_free_io(io); 13992 13993 return (CTL_RETVAL_COMPLETE); 13994} 13995#endif 13996 13997/* 13998 * Primary command inlet from frontend ports. All SCSI and task I/O 13999 * requests must go through this function. 14000 */ 14001int 14002ctl_queue(union ctl_io *io) 14003{ 14004 struct ctl_softc *ctl_softc; 14005 14006 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 14007 14008 ctl_softc = control_softc; 14009 14010#ifdef CTL_TIME_IO 14011 io->io_hdr.start_time = time_uptime; 14012 getbintime(&io->io_hdr.start_bt); 14013#endif /* CTL_TIME_IO */ 14014 14015 /* Map FE-specific LUN ID into global one. */ 14016 io->io_hdr.nexus.targ_mapped_lun = 14017 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 14018 14019 switch (io->io_hdr.io_type) { 14020 case CTL_IO_SCSI: 14021 case CTL_IO_TASK: 14022 if (ctl_debug & CTL_DEBUG_CDB) 14023 ctl_io_print(io); 14024 ctl_enqueue_incoming(io); 14025 break; 14026 default: 14027 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 14028 return (EINVAL); 14029 } 14030 14031 return (CTL_RETVAL_COMPLETE); 14032} 14033 14034#ifdef CTL_IO_DELAY 14035static void 14036ctl_done_timer_wakeup(void *arg) 14037{ 14038 union ctl_io *io; 14039 14040 io = (union ctl_io *)arg; 14041 ctl_done(io); 14042} 14043#endif /* CTL_IO_DELAY */ 14044 14045void 14046ctl_done(union ctl_io *io) 14047{ 14048 struct ctl_softc *ctl_softc; 14049 14050 ctl_softc = control_softc; 14051 14052 /* 14053 * Enable this to catch duplicate completion issues. 14054 */ 14055#if 0 14056 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 14057 printf("%s: type %d msg %d cdb %x iptl: " 14058 "%d:%d:%d:%d tag 0x%04x " 14059 "flag %#x status %x\n", 14060 __func__, 14061 io->io_hdr.io_type, 14062 io->io_hdr.msg_type, 14063 io->scsiio.cdb[0], 14064 io->io_hdr.nexus.initid.id, 14065 io->io_hdr.nexus.targ_port, 14066 io->io_hdr.nexus.targ_target.id, 14067 io->io_hdr.nexus.targ_lun, 14068 (io->io_hdr.io_type == 14069 CTL_IO_TASK) ? 14070 io->taskio.tag_num : 14071 io->scsiio.tag_num, 14072 io->io_hdr.flags, 14073 io->io_hdr.status); 14074 } else 14075 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 14076#endif 14077 14078 /* 14079 * This is an internal copy of an I/O, and should not go through 14080 * the normal done processing logic. 14081 */ 14082 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 14083 return; 14084 14085 /* 14086 * We need to send a msg to the serializing shelf to finish the IO 14087 * as well. We don't send a finish message to the other shelf if 14088 * this is a task management command. Task management commands 14089 * aren't serialized in the OOA queue, but rather just executed on 14090 * both shelf controllers for commands that originated on that 14091 * controller. 14092 */ 14093 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 14094 && (io->io_hdr.io_type != CTL_IO_TASK)) { 14095 union ctl_ha_msg msg_io; 14096 14097 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 14098 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 14099 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 14100 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 14101 } 14102 /* continue on to finish IO */ 14103 } 14104#ifdef CTL_IO_DELAY 14105 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 14106 struct ctl_lun *lun; 14107 14108 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14109 14110 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 14111 } else { 14112 struct ctl_lun *lun; 14113 14114 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14115 14116 if ((lun != NULL) 14117 && (lun->delay_info.done_delay > 0)) { 14118 struct callout *callout; 14119 14120 callout = (struct callout *)&io->io_hdr.timer_bytes; 14121 callout_init(callout, /*mpsafe*/ 1); 14122 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 14123 callout_reset(callout, 14124 lun->delay_info.done_delay * hz, 14125 ctl_done_timer_wakeup, io); 14126 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 14127 lun->delay_info.done_delay = 0; 14128 return; 14129 } 14130 } 14131#endif /* CTL_IO_DELAY */ 14132 14133 ctl_enqueue_done(io); 14134} 14135 14136int 14137ctl_isc(struct ctl_scsiio *ctsio) 14138{ 14139 struct ctl_lun *lun; 14140 int retval; 14141 14142 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14143 14144 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 14145 14146 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 14147 14148 retval = lun->backend->data_submit((union ctl_io *)ctsio); 14149 14150 return (retval); 14151} 14152 14153 14154static void 14155ctl_work_thread(void *arg) 14156{ 14157 struct ctl_thread *thr = (struct ctl_thread *)arg; 14158 struct ctl_softc *softc = thr->ctl_softc; 14159 union ctl_io *io; 14160 int retval; 14161 14162 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 14163 14164 for (;;) { 14165 retval = 0; 14166 14167 /* 14168 * We handle the queues in this order: 14169 * - ISC 14170 * - done queue (to free up resources, unblock other commands) 14171 * - RtR queue 14172 * - incoming queue 14173 * 14174 * If those queues are empty, we break out of the loop and 14175 * go to sleep. 14176 */ 14177 mtx_lock(&thr->queue_lock); 14178 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 14179 if (io != NULL) { 14180 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 14181 mtx_unlock(&thr->queue_lock); 14182 ctl_handle_isc(io); 14183 continue; 14184 } 14185 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 14186 if (io != NULL) { 14187 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 14188 /* clear any blocked commands, call fe_done */ 14189 mtx_unlock(&thr->queue_lock); 14190 retval = ctl_process_done(io); 14191 continue; 14192 } 14193 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 14194 if (io != NULL) { 14195 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 14196 mtx_unlock(&thr->queue_lock); 14197 if (io->io_hdr.io_type == CTL_IO_TASK) 14198 ctl_run_task(io); 14199 else 14200 ctl_scsiio_precheck(softc, &io->scsiio); 14201 continue; 14202 } 14203 if (!ctl_pause_rtr) { 14204 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 14205 if (io != NULL) { 14206 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14207 mtx_unlock(&thr->queue_lock); 14208 retval = ctl_scsiio(&io->scsiio); 14209 if (retval != CTL_RETVAL_COMPLETE) 14210 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14211 continue; 14212 } 14213 } 14214 14215 /* Sleep until we have something to do. */ 14216 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 14217 } 14218} 14219 14220static void 14221ctl_lun_thread(void *arg) 14222{ 14223 struct ctl_softc *softc = (struct ctl_softc *)arg; 14224 struct ctl_be_lun *be_lun; 14225 int retval; 14226 14227 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14228 14229 for (;;) { 14230 retval = 0; 14231 mtx_lock(&softc->ctl_lock); 14232 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14233 if (be_lun != NULL) { 14234 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14235 mtx_unlock(&softc->ctl_lock); 14236 ctl_create_lun(be_lun); 14237 continue; 14238 } 14239 14240 /* Sleep until we have something to do. */ 14241 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14242 PDROP | PRIBIO, "-", 0); 14243 } 14244} 14245 14246static void 14247ctl_thresh_thread(void *arg) 14248{ 14249 struct ctl_softc *softc = (struct ctl_softc *)arg; 14250 struct ctl_lun *lun; 14251 struct ctl_be_lun *be_lun; 14252 struct scsi_da_rw_recovery_page *rwpage; 14253 struct ctl_logical_block_provisioning_page *page; 14254 const char *attr; 14255 uint64_t thres, val; 14256 int i, e; 14257 14258 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 14259 14260 for (;;) { 14261 mtx_lock(&softc->ctl_lock); 14262 STAILQ_FOREACH(lun, &softc->lun_list, links) { 14263 be_lun = lun->be_lun; 14264 if ((lun->flags & CTL_LUN_DISABLED) || 14265 (lun->flags & CTL_LUN_OFFLINE) || 14266 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 14267 lun->backend->lun_attr == NULL) 14268 continue; 14269 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 14270 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 14271 continue; 14272 e = 0; 14273 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 14274 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 14275 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 14276 continue; 14277 thres = scsi_4btoul(page->descr[i].count); 14278 thres <<= CTL_LBP_EXPONENT; 14279 switch (page->descr[i].resource) { 14280 case 0x01: 14281 attr = "blocksavail"; 14282 break; 14283 case 0x02: 14284 attr = "blocksused"; 14285 break; 14286 case 0xf1: 14287 attr = "poolblocksavail"; 14288 break; 14289 case 0xf2: 14290 attr = "poolblocksused"; 14291 break; 14292 default: 14293 continue; 14294 } 14295 mtx_unlock(&softc->ctl_lock); // XXX 14296 val = lun->backend->lun_attr( 14297 lun->be_lun->be_lun, attr); 14298 mtx_lock(&softc->ctl_lock); 14299 if (val == UINT64_MAX) 14300 continue; 14301 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 14302 == SLBPPD_ARMING_INC) 14303 e |= (val >= thres); 14304 else 14305 e |= (val <= thres); 14306 } 14307 mtx_lock(&lun->lun_lock); 14308 if (e) { 14309 if (lun->lasttpt == 0 || 14310 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 14311 lun->lasttpt = time_uptime; 14312 for (i = 0; i < CTL_MAX_INITIATORS; i++) 14313 lun->pending_ua[i] |= 14314 CTL_UA_THIN_PROV_THRES; 14315 } 14316 } else { 14317 lun->lasttpt = 0; 14318 for (i = 0; i < CTL_MAX_INITIATORS; i++) 14319 lun->pending_ua[i] &= ~CTL_UA_THIN_PROV_THRES; 14320 } 14321 mtx_unlock(&lun->lun_lock); 14322 } 14323 mtx_unlock(&softc->ctl_lock); 14324 pause("-", CTL_LBP_PERIOD * hz); 14325 } 14326} 14327 14328static void 14329ctl_enqueue_incoming(union ctl_io *io) 14330{ 14331 struct ctl_softc *softc = control_softc; 14332 struct ctl_thread *thr; 14333 u_int idx; 14334 14335 idx = (io->io_hdr.nexus.targ_port * 127 + 14336 io->io_hdr.nexus.initid.id) % worker_threads; 14337 thr = &softc->threads[idx]; 14338 mtx_lock(&thr->queue_lock); 14339 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14340 mtx_unlock(&thr->queue_lock); 14341 wakeup(thr); 14342} 14343 14344static void 14345ctl_enqueue_rtr(union ctl_io *io) 14346{ 14347 struct ctl_softc *softc = control_softc; 14348 struct ctl_thread *thr; 14349 14350 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14351 mtx_lock(&thr->queue_lock); 14352 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14353 mtx_unlock(&thr->queue_lock); 14354 wakeup(thr); 14355} 14356 14357static void 14358ctl_enqueue_done(union ctl_io *io) 14359{ 14360 struct ctl_softc *softc = control_softc; 14361 struct ctl_thread *thr; 14362 14363 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14364 mtx_lock(&thr->queue_lock); 14365 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14366 mtx_unlock(&thr->queue_lock); 14367 wakeup(thr); 14368} 14369 14370static void 14371ctl_enqueue_isc(union ctl_io *io) 14372{ 14373 struct ctl_softc *softc = control_softc; 14374 struct ctl_thread *thr; 14375 14376 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14377 mtx_lock(&thr->queue_lock); 14378 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14379 mtx_unlock(&thr->queue_lock); 14380 wakeup(thr); 14381} 14382 14383/* Initialization and failover */ 14384 14385void 14386ctl_init_isc_msg(void) 14387{ 14388 printf("CTL: Still calling this thing\n"); 14389} 14390 14391/* 14392 * Init component 14393 * Initializes component into configuration defined by bootMode 14394 * (see hasc-sv.c) 14395 * returns hasc_Status: 14396 * OK 14397 * ERROR - fatal error 14398 */ 14399static ctl_ha_comp_status 14400ctl_isc_init(struct ctl_ha_component *c) 14401{ 14402 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14403 14404 c->status = ret; 14405 return ret; 14406} 14407 14408/* Start component 14409 * Starts component in state requested. If component starts successfully, 14410 * it must set its own state to the requestrd state 14411 * When requested state is HASC_STATE_HA, the component may refine it 14412 * by adding _SLAVE or _MASTER flags. 14413 * Currently allowed state transitions are: 14414 * UNKNOWN->HA - initial startup 14415 * UNKNOWN->SINGLE - initial startup when no parter detected 14416 * HA->SINGLE - failover 14417 * returns ctl_ha_comp_status: 14418 * OK - component successfully started in requested state 14419 * FAILED - could not start the requested state, failover may 14420 * be possible 14421 * ERROR - fatal error detected, no future startup possible 14422 */ 14423static ctl_ha_comp_status 14424ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14425{ 14426 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14427 14428 printf("%s: go\n", __func__); 14429 14430 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14431 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14432 control_softc->is_single = 0; 14433 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14434 != CTL_HA_STATUS_SUCCESS) { 14435 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14436 ret = CTL_HA_COMP_STATUS_ERROR; 14437 } 14438 } else if (CTL_HA_STATE_IS_HA(c->state) 14439 && CTL_HA_STATE_IS_SINGLE(state)){ 14440 // HA->SINGLE transition 14441 ctl_failover(); 14442 control_softc->is_single = 1; 14443 } else { 14444 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14445 c->state, state); 14446 ret = CTL_HA_COMP_STATUS_ERROR; 14447 } 14448 if (CTL_HA_STATE_IS_SINGLE(state)) 14449 control_softc->is_single = 1; 14450 14451 c->state = state; 14452 c->status = ret; 14453 return ret; 14454} 14455 14456/* 14457 * Quiesce component 14458 * The component must clear any error conditions (set status to OK) and 14459 * prepare itself to another Start call 14460 * returns ctl_ha_comp_status: 14461 * OK 14462 * ERROR 14463 */ 14464static ctl_ha_comp_status 14465ctl_isc_quiesce(struct ctl_ha_component *c) 14466{ 14467 int ret = CTL_HA_COMP_STATUS_OK; 14468 14469 ctl_pause_rtr = 1; 14470 c->status = ret; 14471 return ret; 14472} 14473 14474struct ctl_ha_component ctl_ha_component_ctlisc = 14475{ 14476 .name = "CTL ISC", 14477 .state = CTL_HA_STATE_UNKNOWN, 14478 .init = ctl_isc_init, 14479 .start = ctl_isc_start, 14480 .quiesce = ctl_isc_quiesce 14481}; 14482 14483/* 14484 * vim: ts=8 14485 */ 14486