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