ctl.c revision 275202
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 275202 2014-11-28 08:54:43Z 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; 360static int ctl_is_single = 1; 361 362SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 363static int worker_threads = -1; 364TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 365SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 366 &worker_threads, 1, "Number of worker threads"); 367static int ctl_debug = CTL_DEBUG_NONE; 368TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug); 369SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 370 &ctl_debug, 0, "Enabled debug flags"); 371 372/* 373 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 374 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 375 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 376 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 377 */ 378#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 379 380static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 381 int param); 382static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 383static int ctl_init(void); 384void ctl_shutdown(void); 385static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 386static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 387static void ctl_ioctl_online(void *arg); 388static void ctl_ioctl_offline(void *arg); 389static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 390static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 391static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 392static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 393static int ctl_ioctl_submit_wait(union ctl_io *io); 394static void ctl_ioctl_datamove(union ctl_io *io); 395static void ctl_ioctl_done(union ctl_io *io); 396static void ctl_ioctl_hard_startstop_callback(void *arg, 397 struct cfi_metatask *metatask); 398static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 399static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 400 struct ctl_ooa *ooa_hdr, 401 struct ctl_ooa_entry *kern_entries); 402static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 403 struct thread *td); 404static uint32_t ctl_map_lun(int port_num, uint32_t lun); 405static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 406#ifdef unused 407static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 408 uint32_t targ_target, uint32_t targ_lun, 409 int can_wait); 410static void ctl_kfree_io(union ctl_io *io); 411#endif /* unused */ 412static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 413 struct ctl_be_lun *be_lun, struct ctl_id target_id); 414static int ctl_free_lun(struct ctl_lun *lun); 415static void ctl_create_lun(struct ctl_be_lun *be_lun); 416/** 417static void ctl_failover_change_pages(struct ctl_softc *softc, 418 struct ctl_scsiio *ctsio, int master); 419**/ 420 421static int ctl_do_mode_select(union ctl_io *io); 422static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 423 uint64_t res_key, uint64_t sa_res_key, 424 uint8_t type, uint32_t residx, 425 struct ctl_scsiio *ctsio, 426 struct scsi_per_res_out *cdb, 427 struct scsi_per_res_out_parms* param); 428static void ctl_pro_preempt_other(struct ctl_lun *lun, 429 union ctl_ha_msg *msg); 430static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 431static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 432static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 433static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 434static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 435static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 436static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 437 int alloc_len); 438static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 439 int alloc_len); 440static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 441static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 442static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 443static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 444static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 445static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 446static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 447 union ctl_io *pending_io, union ctl_io *ooa_io); 448static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 449 union ctl_io *starting_io); 450static int ctl_check_blocked(struct ctl_lun *lun); 451static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 452 struct ctl_lun *lun, 453 const struct ctl_cmd_entry *entry, 454 struct ctl_scsiio *ctsio); 455//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 456static void ctl_failover(void); 457static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 458 struct ctl_scsiio *ctsio); 459static int ctl_scsiio(struct ctl_scsiio *ctsio); 460 461static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 462static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 463 ctl_ua_type ua_type); 464static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 465 ctl_ua_type ua_type); 466static int ctl_abort_task(union ctl_io *io); 467static int ctl_abort_task_set(union ctl_io *io); 468static int ctl_i_t_nexus_reset(union ctl_io *io); 469static void ctl_run_task(union ctl_io *io); 470#ifdef CTL_IO_DELAY 471static void ctl_datamove_timer_wakeup(void *arg); 472static void ctl_done_timer_wakeup(void *arg); 473#endif /* CTL_IO_DELAY */ 474 475static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 476static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 477static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 478static void ctl_datamove_remote_write(union ctl_io *io); 479static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 480static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 481static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 482static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 483 ctl_ha_dt_cb callback); 484static void ctl_datamove_remote_read(union ctl_io *io); 485static void ctl_datamove_remote(union ctl_io *io); 486static int ctl_process_done(union ctl_io *io); 487static void ctl_lun_thread(void *arg); 488static void ctl_thresh_thread(void *arg); 489static void ctl_work_thread(void *arg); 490static void ctl_enqueue_incoming(union ctl_io *io); 491static void ctl_enqueue_rtr(union ctl_io *io); 492static void ctl_enqueue_done(union ctl_io *io); 493static void ctl_enqueue_isc(union ctl_io *io); 494static const struct ctl_cmd_entry * 495 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 496static const struct ctl_cmd_entry * 497 ctl_validate_command(struct ctl_scsiio *ctsio); 498static int ctl_cmd_applicable(uint8_t lun_type, 499 const struct ctl_cmd_entry *entry); 500 501/* 502 * Load the serialization table. This isn't very pretty, but is probably 503 * the easiest way to do it. 504 */ 505#include "ctl_ser_table.c" 506 507/* 508 * We only need to define open, close and ioctl routines for this driver. 509 */ 510static struct cdevsw ctl_cdevsw = { 511 .d_version = D_VERSION, 512 .d_flags = 0, 513 .d_open = ctl_open, 514 .d_close = ctl_close, 515 .d_ioctl = ctl_ioctl, 516 .d_name = "ctl", 517}; 518 519 520MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 521MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 522 523static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 524 525static moduledata_t ctl_moduledata = { 526 "ctl", 527 ctl_module_event_handler, 528 NULL 529}; 530 531DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 532MODULE_VERSION(ctl, 1); 533 534static struct ctl_frontend ioctl_frontend = 535{ 536 .name = "ioctl", 537}; 538 539static void 540ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 541 union ctl_ha_msg *msg_info) 542{ 543 struct ctl_scsiio *ctsio; 544 545 if (msg_info->hdr.original_sc == NULL) { 546 printf("%s: original_sc == NULL!\n", __func__); 547 /* XXX KDM now what? */ 548 return; 549 } 550 551 ctsio = &msg_info->hdr.original_sc->scsiio; 552 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 553 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 554 ctsio->io_hdr.status = msg_info->hdr.status; 555 ctsio->scsi_status = msg_info->scsi.scsi_status; 556 ctsio->sense_len = msg_info->scsi.sense_len; 557 ctsio->sense_residual = msg_info->scsi.sense_residual; 558 ctsio->residual = msg_info->scsi.residual; 559 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 560 sizeof(ctsio->sense_data)); 561 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 562 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 563 ctl_enqueue_isc((union ctl_io *)ctsio); 564} 565 566static void 567ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 568 union ctl_ha_msg *msg_info) 569{ 570 struct ctl_scsiio *ctsio; 571 572 if (msg_info->hdr.serializing_sc == NULL) { 573 printf("%s: serializing_sc == NULL!\n", __func__); 574 /* XXX KDM now what? */ 575 return; 576 } 577 578 ctsio = &msg_info->hdr.serializing_sc->scsiio; 579#if 0 580 /* 581 * Attempt to catch the situation where an I/O has 582 * been freed, and we're using it again. 583 */ 584 if (ctsio->io_hdr.io_type == 0xff) { 585 union ctl_io *tmp_io; 586 tmp_io = (union ctl_io *)ctsio; 587 printf("%s: %p use after free!\n", __func__, 588 ctsio); 589 printf("%s: type %d msg %d cdb %x iptl: " 590 "%d:%d:%d:%d tag 0x%04x " 591 "flag %#x status %x\n", 592 __func__, 593 tmp_io->io_hdr.io_type, 594 tmp_io->io_hdr.msg_type, 595 tmp_io->scsiio.cdb[0], 596 tmp_io->io_hdr.nexus.initid.id, 597 tmp_io->io_hdr.nexus.targ_port, 598 tmp_io->io_hdr.nexus.targ_target.id, 599 tmp_io->io_hdr.nexus.targ_lun, 600 (tmp_io->io_hdr.io_type == 601 CTL_IO_TASK) ? 602 tmp_io->taskio.tag_num : 603 tmp_io->scsiio.tag_num, 604 tmp_io->io_hdr.flags, 605 tmp_io->io_hdr.status); 606 } 607#endif 608 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 609 ctl_enqueue_isc((union ctl_io *)ctsio); 610} 611 612/* 613 * ISC (Inter Shelf Communication) event handler. Events from the HA 614 * subsystem come in here. 615 */ 616static void 617ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 618{ 619 struct ctl_softc *ctl_softc; 620 union ctl_io *io; 621 struct ctl_prio *presio; 622 ctl_ha_status isc_status; 623 624 ctl_softc = control_softc; 625 io = NULL; 626 627 628#if 0 629 printf("CTL: Isc Msg event %d\n", event); 630#endif 631 if (event == CTL_HA_EVT_MSG_RECV) { 632 union ctl_ha_msg msg_info; 633 634 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 635 sizeof(msg_info), /*wait*/ 0); 636#if 0 637 printf("CTL: msg_type %d\n", msg_info.msg_type); 638#endif 639 if (isc_status != 0) { 640 printf("Error receiving message, status = %d\n", 641 isc_status); 642 return; 643 } 644 645 switch (msg_info.hdr.msg_type) { 646 case CTL_MSG_SERIALIZE: 647#if 0 648 printf("Serialize\n"); 649#endif 650 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 651 if (io == NULL) { 652 printf("ctl_isc_event_handler: can't allocate " 653 "ctl_io!\n"); 654 /* Bad Juju */ 655 /* Need to set busy and send msg back */ 656 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 657 msg_info.hdr.status = CTL_SCSI_ERROR; 658 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 659 msg_info.scsi.sense_len = 0; 660 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 661 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 662 } 663 goto bailout; 664 } 665 ctl_zero_io(io); 666 // populate ctsio from msg_info 667 io->io_hdr.io_type = CTL_IO_SCSI; 668 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 669 io->io_hdr.original_sc = msg_info.hdr.original_sc; 670#if 0 671 printf("pOrig %x\n", (int)msg_info.original_sc); 672#endif 673 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 674 CTL_FLAG_IO_ACTIVE; 675 /* 676 * If we're in serialization-only mode, we don't 677 * want to go through full done processing. Thus 678 * the COPY flag. 679 * 680 * XXX KDM add another flag that is more specific. 681 */ 682 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 683 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 684 io->io_hdr.nexus = msg_info.hdr.nexus; 685#if 0 686 printf("targ %d, port %d, iid %d, lun %d\n", 687 io->io_hdr.nexus.targ_target.id, 688 io->io_hdr.nexus.targ_port, 689 io->io_hdr.nexus.initid.id, 690 io->io_hdr.nexus.targ_lun); 691#endif 692 io->scsiio.tag_num = msg_info.scsi.tag_num; 693 io->scsiio.tag_type = msg_info.scsi.tag_type; 694 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 695 CTL_MAX_CDBLEN); 696 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 697 const struct ctl_cmd_entry *entry; 698 699 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 700 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 701 io->io_hdr.flags |= 702 entry->flags & CTL_FLAG_DATA_MASK; 703 } 704 ctl_enqueue_isc(io); 705 break; 706 707 /* Performed on the Originating SC, XFER mode only */ 708 case CTL_MSG_DATAMOVE: { 709 struct ctl_sg_entry *sgl; 710 int i, j; 711 712 io = msg_info.hdr.original_sc; 713 if (io == NULL) { 714 printf("%s: original_sc == NULL!\n", __func__); 715 /* XXX KDM do something here */ 716 break; 717 } 718 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 719 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 720 /* 721 * Keep track of this, we need to send it back over 722 * when the datamove is complete. 723 */ 724 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 725 726 if (msg_info.dt.sg_sequence == 0) { 727 /* 728 * XXX KDM we use the preallocated S/G list 729 * here, but we'll need to change this to 730 * dynamic allocation if we need larger S/G 731 * lists. 732 */ 733 if (msg_info.dt.kern_sg_entries > 734 sizeof(io->io_hdr.remote_sglist) / 735 sizeof(io->io_hdr.remote_sglist[0])) { 736 printf("%s: number of S/G entries " 737 "needed %u > allocated num %zd\n", 738 __func__, 739 msg_info.dt.kern_sg_entries, 740 sizeof(io->io_hdr.remote_sglist)/ 741 sizeof(io->io_hdr.remote_sglist[0])); 742 743 /* 744 * XXX KDM send a message back to 745 * the other side to shut down the 746 * DMA. The error will come back 747 * through via the normal channel. 748 */ 749 break; 750 } 751 sgl = io->io_hdr.remote_sglist; 752 memset(sgl, 0, 753 sizeof(io->io_hdr.remote_sglist)); 754 755 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 756 757 io->scsiio.kern_sg_entries = 758 msg_info.dt.kern_sg_entries; 759 io->scsiio.rem_sg_entries = 760 msg_info.dt.kern_sg_entries; 761 io->scsiio.kern_data_len = 762 msg_info.dt.kern_data_len; 763 io->scsiio.kern_total_len = 764 msg_info.dt.kern_total_len; 765 io->scsiio.kern_data_resid = 766 msg_info.dt.kern_data_resid; 767 io->scsiio.kern_rel_offset = 768 msg_info.dt.kern_rel_offset; 769 /* 770 * Clear out per-DMA flags. 771 */ 772 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 773 /* 774 * Add per-DMA flags that are set for this 775 * particular DMA request. 776 */ 777 io->io_hdr.flags |= msg_info.dt.flags & 778 CTL_FLAG_RDMA_MASK; 779 } else 780 sgl = (struct ctl_sg_entry *) 781 io->scsiio.kern_data_ptr; 782 783 for (i = msg_info.dt.sent_sg_entries, j = 0; 784 i < (msg_info.dt.sent_sg_entries + 785 msg_info.dt.cur_sg_entries); i++, j++) { 786 sgl[i].addr = msg_info.dt.sg_list[j].addr; 787 sgl[i].len = msg_info.dt.sg_list[j].len; 788 789#if 0 790 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 791 __func__, 792 msg_info.dt.sg_list[j].addr, 793 msg_info.dt.sg_list[j].len, 794 sgl[i].addr, sgl[i].len, j, i); 795#endif 796 } 797#if 0 798 memcpy(&sgl[msg_info.dt.sent_sg_entries], 799 msg_info.dt.sg_list, 800 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 801#endif 802 803 /* 804 * If this is the last piece of the I/O, we've got 805 * the full S/G list. Queue processing in the thread. 806 * Otherwise wait for the next piece. 807 */ 808 if (msg_info.dt.sg_last != 0) 809 ctl_enqueue_isc(io); 810 break; 811 } 812 /* Performed on the Serializing (primary) SC, XFER mode only */ 813 case CTL_MSG_DATAMOVE_DONE: { 814 if (msg_info.hdr.serializing_sc == NULL) { 815 printf("%s: serializing_sc == NULL!\n", 816 __func__); 817 /* XXX KDM now what? */ 818 break; 819 } 820 /* 821 * We grab the sense information here in case 822 * there was a failure, so we can return status 823 * back to the initiator. 824 */ 825 io = msg_info.hdr.serializing_sc; 826 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 827 io->io_hdr.status = msg_info.hdr.status; 828 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 829 io->scsiio.sense_len = msg_info.scsi.sense_len; 830 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 831 io->io_hdr.port_status = msg_info.scsi.fetd_status; 832 io->scsiio.residual = msg_info.scsi.residual; 833 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 834 sizeof(io->scsiio.sense_data)); 835 ctl_enqueue_isc(io); 836 break; 837 } 838 839 /* Preformed on Originating SC, SER_ONLY mode */ 840 case CTL_MSG_R2R: 841 io = msg_info.hdr.original_sc; 842 if (io == NULL) { 843 printf("%s: Major Bummer\n", __func__); 844 return; 845 } else { 846#if 0 847 printf("pOrig %x\n",(int) ctsio); 848#endif 849 } 850 io->io_hdr.msg_type = CTL_MSG_R2R; 851 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 852 ctl_enqueue_isc(io); 853 break; 854 855 /* 856 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 857 * mode. 858 * Performed on the Originating (i.e. secondary) SC in XFER 859 * mode 860 */ 861 case CTL_MSG_FINISH_IO: 862 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 863 ctl_isc_handler_finish_xfer(ctl_softc, 864 &msg_info); 865 else 866 ctl_isc_handler_finish_ser_only(ctl_softc, 867 &msg_info); 868 break; 869 870 /* Preformed on Originating SC */ 871 case CTL_MSG_BAD_JUJU: 872 io = msg_info.hdr.original_sc; 873 if (io == NULL) { 874 printf("%s: Bad JUJU!, original_sc is NULL!\n", 875 __func__); 876 break; 877 } 878 ctl_copy_sense_data(&msg_info, io); 879 /* 880 * IO should have already been cleaned up on other 881 * SC so clear this flag so we won't send a message 882 * back to finish the IO there. 883 */ 884 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 885 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 886 887 /* io = msg_info.hdr.serializing_sc; */ 888 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 889 ctl_enqueue_isc(io); 890 break; 891 892 /* Handle resets sent from the other side */ 893 case CTL_MSG_MANAGE_TASKS: { 894 struct ctl_taskio *taskio; 895 taskio = (struct ctl_taskio *)ctl_alloc_io( 896 (void *)ctl_softc->othersc_pool); 897 if (taskio == NULL) { 898 printf("ctl_isc_event_handler: can't allocate " 899 "ctl_io!\n"); 900 /* Bad Juju */ 901 /* should I just call the proper reset func 902 here??? */ 903 goto bailout; 904 } 905 ctl_zero_io((union ctl_io *)taskio); 906 taskio->io_hdr.io_type = CTL_IO_TASK; 907 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 908 taskio->io_hdr.nexus = msg_info.hdr.nexus; 909 taskio->task_action = msg_info.task.task_action; 910 taskio->tag_num = msg_info.task.tag_num; 911 taskio->tag_type = msg_info.task.tag_type; 912#ifdef CTL_TIME_IO 913 taskio->io_hdr.start_time = time_uptime; 914 getbintime(&taskio->io_hdr.start_bt); 915#if 0 916 cs_prof_gettime(&taskio->io_hdr.start_ticks); 917#endif 918#endif /* CTL_TIME_IO */ 919 ctl_run_task((union ctl_io *)taskio); 920 break; 921 } 922 /* Persistent Reserve action which needs attention */ 923 case CTL_MSG_PERS_ACTION: 924 presio = (struct ctl_prio *)ctl_alloc_io( 925 (void *)ctl_softc->othersc_pool); 926 if (presio == NULL) { 927 printf("ctl_isc_event_handler: can't allocate " 928 "ctl_io!\n"); 929 /* Bad Juju */ 930 /* Need to set busy and send msg back */ 931 goto bailout; 932 } 933 ctl_zero_io((union ctl_io *)presio); 934 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 935 presio->pr_msg = msg_info.pr; 936 ctl_enqueue_isc((union ctl_io *)presio); 937 break; 938 case CTL_MSG_SYNC_FE: 939 rcv_sync_msg = 1; 940 break; 941 default: 942 printf("How did I get here?\n"); 943 } 944 } else if (event == CTL_HA_EVT_MSG_SENT) { 945 if (param != CTL_HA_STATUS_SUCCESS) { 946 printf("Bad status from ctl_ha_msg_send status %d\n", 947 param); 948 } 949 return; 950 } else if (event == CTL_HA_EVT_DISCONNECT) { 951 printf("CTL: Got a disconnect from Isc\n"); 952 return; 953 } else { 954 printf("ctl_isc_event_handler: Unknown event %d\n", event); 955 return; 956 } 957 958bailout: 959 return; 960} 961 962static void 963ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 964{ 965 struct scsi_sense_data *sense; 966 967 sense = &dest->scsiio.sense_data; 968 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 969 dest->scsiio.scsi_status = src->scsi.scsi_status; 970 dest->scsiio.sense_len = src->scsi.sense_len; 971 dest->io_hdr.status = src->hdr.status; 972} 973 974static int 975ctl_init(void) 976{ 977 struct ctl_softc *softc; 978 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 979 struct ctl_port *port; 980 uint8_t sc_id =0; 981 int i, error, retval; 982 //int isc_retval; 983 984 retval = 0; 985 ctl_pause_rtr = 0; 986 rcv_sync_msg = 0; 987 988 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 989 M_WAITOK | M_ZERO); 990 softc = control_softc; 991 992 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 993 "cam/ctl"); 994 995 softc->dev->si_drv1 = softc; 996 997 /* 998 * By default, return a "bad LUN" peripheral qualifier for unknown 999 * LUNs. The user can override this default using the tunable or 1000 * sysctl. See the comment in ctl_inquiry_std() for more details. 1001 */ 1002 softc->inquiry_pq_no_lun = 1; 1003 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 1004 &softc->inquiry_pq_no_lun); 1005 sysctl_ctx_init(&softc->sysctl_ctx); 1006 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1007 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1008 CTLFLAG_RD, 0, "CAM Target Layer"); 1009 1010 if (softc->sysctl_tree == NULL) { 1011 printf("%s: unable to allocate sysctl tree\n", __func__); 1012 destroy_dev(softc->dev); 1013 free(control_softc, M_DEVBUF); 1014 control_softc = NULL; 1015 return (ENOMEM); 1016 } 1017 1018 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1019 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1020 "inquiry_pq_no_lun", CTLFLAG_RW, 1021 &softc->inquiry_pq_no_lun, 0, 1022 "Report no lun possible for invalid LUNs"); 1023 1024 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1025 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 1026 softc->open_count = 0; 1027 1028 /* 1029 * Default to actually sending a SYNCHRONIZE CACHE command down to 1030 * the drive. 1031 */ 1032 softc->flags = CTL_FLAG_REAL_SYNC; 1033 1034 /* 1035 * In Copan's HA scheme, the "master" and "slave" roles are 1036 * figured out through the slot the controller is in. Although it 1037 * is an active/active system, someone has to be in charge. 1038 */ 1039#ifdef NEEDTOPORT 1040 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1041#endif 1042 1043 if (sc_id == 0) { 1044 softc->flags |= CTL_FLAG_MASTER_SHELF; 1045 persis_offset = 0; 1046 } else 1047 persis_offset = CTL_MAX_INITIATORS; 1048 1049 /* 1050 * XXX KDM need to figure out where we want to get our target ID 1051 * and WWID. Is it different on each port? 1052 */ 1053 softc->target.id = 0; 1054 softc->target.wwid[0] = 0x12345678; 1055 softc->target.wwid[1] = 0x87654321; 1056 STAILQ_INIT(&softc->lun_list); 1057 STAILQ_INIT(&softc->pending_lun_queue); 1058 STAILQ_INIT(&softc->fe_list); 1059 STAILQ_INIT(&softc->port_list); 1060 STAILQ_INIT(&softc->be_list); 1061 STAILQ_INIT(&softc->io_pools); 1062 ctl_tpc_init(softc); 1063 1064 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1065 &internal_pool)!= 0){ 1066 printf("ctl: can't allocate %d entry internal pool, " 1067 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1068 return (ENOMEM); 1069 } 1070 1071 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1072 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1073 printf("ctl: can't allocate %d entry emergency pool, " 1074 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1075 ctl_pool_free(internal_pool); 1076 return (ENOMEM); 1077 } 1078 1079 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1080 &other_pool) != 0) 1081 { 1082 printf("ctl: can't allocate %d entry other SC pool, " 1083 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1084 ctl_pool_free(internal_pool); 1085 ctl_pool_free(emergency_pool); 1086 return (ENOMEM); 1087 } 1088 1089 softc->internal_pool = internal_pool; 1090 softc->emergency_pool = emergency_pool; 1091 softc->othersc_pool = other_pool; 1092 1093 if (worker_threads <= 0) 1094 worker_threads = max(1, mp_ncpus / 4); 1095 if (worker_threads > CTL_MAX_THREADS) 1096 worker_threads = CTL_MAX_THREADS; 1097 1098 for (i = 0; i < worker_threads; i++) { 1099 struct ctl_thread *thr = &softc->threads[i]; 1100 1101 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1102 thr->ctl_softc = softc; 1103 STAILQ_INIT(&thr->incoming_queue); 1104 STAILQ_INIT(&thr->rtr_queue); 1105 STAILQ_INIT(&thr->done_queue); 1106 STAILQ_INIT(&thr->isc_queue); 1107 1108 error = kproc_kthread_add(ctl_work_thread, thr, 1109 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1110 if (error != 0) { 1111 printf("error creating CTL work thread!\n"); 1112 ctl_pool_free(internal_pool); 1113 ctl_pool_free(emergency_pool); 1114 ctl_pool_free(other_pool); 1115 return (error); 1116 } 1117 } 1118 error = kproc_kthread_add(ctl_lun_thread, softc, 1119 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1120 if (error != 0) { 1121 printf("error creating CTL lun thread!\n"); 1122 ctl_pool_free(internal_pool); 1123 ctl_pool_free(emergency_pool); 1124 ctl_pool_free(other_pool); 1125 return (error); 1126 } 1127 error = kproc_kthread_add(ctl_thresh_thread, softc, 1128 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1129 if (error != 0) { 1130 printf("error creating CTL threshold thread!\n"); 1131 ctl_pool_free(internal_pool); 1132 ctl_pool_free(emergency_pool); 1133 ctl_pool_free(other_pool); 1134 return (error); 1135 } 1136 if (bootverbose) 1137 printf("ctl: CAM Target Layer loaded\n"); 1138 1139 /* 1140 * Initialize the ioctl front end. 1141 */ 1142 ctl_frontend_register(&ioctl_frontend); 1143 port = &softc->ioctl_info.port; 1144 port->frontend = &ioctl_frontend; 1145 sprintf(softc->ioctl_info.port_name, "ioctl"); 1146 port->port_type = CTL_PORT_IOCTL; 1147 port->num_requested_ctl_io = 100; 1148 port->port_name = softc->ioctl_info.port_name; 1149 port->port_online = ctl_ioctl_online; 1150 port->port_offline = ctl_ioctl_offline; 1151 port->onoff_arg = &softc->ioctl_info; 1152 port->lun_enable = ctl_ioctl_lun_enable; 1153 port->lun_disable = ctl_ioctl_lun_disable; 1154 port->targ_lun_arg = &softc->ioctl_info; 1155 port->fe_datamove = ctl_ioctl_datamove; 1156 port->fe_done = ctl_ioctl_done; 1157 port->max_targets = 15; 1158 port->max_target_id = 15; 1159 1160 if (ctl_port_register(&softc->ioctl_info.port, 1161 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1162 printf("ctl: ioctl front end registration failed, will " 1163 "continue anyway\n"); 1164 } 1165 1166#ifdef CTL_IO_DELAY 1167 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1168 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1169 sizeof(struct callout), CTL_TIMER_BYTES); 1170 return (EINVAL); 1171 } 1172#endif /* CTL_IO_DELAY */ 1173 1174 return (0); 1175} 1176 1177void 1178ctl_shutdown(void) 1179{ 1180 struct ctl_softc *softc; 1181 struct ctl_lun *lun, *next_lun; 1182 struct ctl_io_pool *pool; 1183 1184 softc = (struct ctl_softc *)control_softc; 1185 1186 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1187 printf("ctl: ioctl front end deregistration failed\n"); 1188 1189 mtx_lock(&softc->ctl_lock); 1190 1191 /* 1192 * Free up each LUN. 1193 */ 1194 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1195 next_lun = STAILQ_NEXT(lun, links); 1196 ctl_free_lun(lun); 1197 } 1198 1199 mtx_unlock(&softc->ctl_lock); 1200 1201 ctl_frontend_deregister(&ioctl_frontend); 1202 1203 /* 1204 * This will rip the rug out from under any FETDs or anyone else 1205 * that has a pool allocated. Since we increment our module 1206 * refcount any time someone outside the main CTL module allocates 1207 * a pool, we shouldn't have any problems here. The user won't be 1208 * able to unload the CTL module until client modules have 1209 * successfully unloaded. 1210 */ 1211 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1212 ctl_pool_free(pool); 1213 1214#if 0 1215 ctl_shutdown_thread(softc->work_thread); 1216 mtx_destroy(&softc->queue_lock); 1217#endif 1218 1219 ctl_tpc_shutdown(softc); 1220 mtx_destroy(&softc->pool_lock); 1221 mtx_destroy(&softc->ctl_lock); 1222 1223 destroy_dev(softc->dev); 1224 1225 sysctl_ctx_free(&softc->sysctl_ctx); 1226 1227 free(control_softc, M_DEVBUF); 1228 control_softc = NULL; 1229 1230 if (bootverbose) 1231 printf("ctl: CAM Target Layer unloaded\n"); 1232} 1233 1234static int 1235ctl_module_event_handler(module_t mod, int what, void *arg) 1236{ 1237 1238 switch (what) { 1239 case MOD_LOAD: 1240 return (ctl_init()); 1241 case MOD_UNLOAD: 1242 return (EBUSY); 1243 default: 1244 return (EOPNOTSUPP); 1245 } 1246} 1247 1248/* 1249 * XXX KDM should we do some access checks here? Bump a reference count to 1250 * prevent a CTL module from being unloaded while someone has it open? 1251 */ 1252static int 1253ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1254{ 1255 return (0); 1256} 1257 1258static int 1259ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1260{ 1261 return (0); 1262} 1263 1264int 1265ctl_port_enable(ctl_port_type port_type) 1266{ 1267 struct ctl_softc *softc; 1268 struct ctl_port *port; 1269 1270 if (ctl_is_single == 0) { 1271 union ctl_ha_msg msg_info; 1272 int isc_retval; 1273 1274#if 0 1275 printf("%s: HA mode, synchronizing frontend enable\n", 1276 __func__); 1277#endif 1278 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1279 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1280 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1281 printf("Sync msg send error retval %d\n", isc_retval); 1282 } 1283 if (!rcv_sync_msg) { 1284 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1285 sizeof(msg_info), 1); 1286 } 1287#if 0 1288 printf("CTL:Frontend Enable\n"); 1289 } else { 1290 printf("%s: single mode, skipping frontend synchronization\n", 1291 __func__); 1292#endif 1293 } 1294 1295 softc = control_softc; 1296 1297 STAILQ_FOREACH(port, &softc->port_list, links) { 1298 if (port_type & port->port_type) 1299 { 1300#if 0 1301 printf("port %d\n", port->targ_port); 1302#endif 1303 ctl_port_online(port); 1304 } 1305 } 1306 1307 return (0); 1308} 1309 1310int 1311ctl_port_disable(ctl_port_type port_type) 1312{ 1313 struct ctl_softc *softc; 1314 struct ctl_port *port; 1315 1316 softc = control_softc; 1317 1318 STAILQ_FOREACH(port, &softc->port_list, links) { 1319 if (port_type & port->port_type) 1320 ctl_port_offline(port); 1321 } 1322 1323 return (0); 1324} 1325 1326/* 1327 * Returns 0 for success, 1 for failure. 1328 * Currently the only failure mode is if there aren't enough entries 1329 * allocated. So, in case of a failure, look at num_entries_dropped, 1330 * reallocate and try again. 1331 */ 1332int 1333ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1334 int *num_entries_filled, int *num_entries_dropped, 1335 ctl_port_type port_type, int no_virtual) 1336{ 1337 struct ctl_softc *softc; 1338 struct ctl_port *port; 1339 int entries_dropped, entries_filled; 1340 int retval; 1341 int i; 1342 1343 softc = control_softc; 1344 1345 retval = 0; 1346 entries_filled = 0; 1347 entries_dropped = 0; 1348 1349 i = 0; 1350 mtx_lock(&softc->ctl_lock); 1351 STAILQ_FOREACH(port, &softc->port_list, links) { 1352 struct ctl_port_entry *entry; 1353 1354 if ((port->port_type & port_type) == 0) 1355 continue; 1356 1357 if ((no_virtual != 0) 1358 && (port->virtual_port != 0)) 1359 continue; 1360 1361 if (entries_filled >= num_entries_alloced) { 1362 entries_dropped++; 1363 continue; 1364 } 1365 entry = &entries[i]; 1366 1367 entry->port_type = port->port_type; 1368 strlcpy(entry->port_name, port->port_name, 1369 sizeof(entry->port_name)); 1370 entry->physical_port = port->physical_port; 1371 entry->virtual_port = port->virtual_port; 1372 entry->wwnn = port->wwnn; 1373 entry->wwpn = port->wwpn; 1374 1375 i++; 1376 entries_filled++; 1377 } 1378 1379 mtx_unlock(&softc->ctl_lock); 1380 1381 if (entries_dropped > 0) 1382 retval = 1; 1383 1384 *num_entries_dropped = entries_dropped; 1385 *num_entries_filled = entries_filled; 1386 1387 return (retval); 1388} 1389 1390static void 1391ctl_ioctl_online(void *arg) 1392{ 1393 struct ctl_ioctl_info *ioctl_info; 1394 1395 ioctl_info = (struct ctl_ioctl_info *)arg; 1396 1397 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1398} 1399 1400static void 1401ctl_ioctl_offline(void *arg) 1402{ 1403 struct ctl_ioctl_info *ioctl_info; 1404 1405 ioctl_info = (struct ctl_ioctl_info *)arg; 1406 1407 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1408} 1409 1410/* 1411 * Remove an initiator by port number and initiator ID. 1412 * Returns 0 for success, -1 for failure. 1413 */ 1414int 1415ctl_remove_initiator(struct ctl_port *port, int iid) 1416{ 1417 struct ctl_softc *softc = control_softc; 1418 1419 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1420 1421 if (iid > CTL_MAX_INIT_PER_PORT) { 1422 printf("%s: initiator ID %u > maximun %u!\n", 1423 __func__, iid, CTL_MAX_INIT_PER_PORT); 1424 return (-1); 1425 } 1426 1427 mtx_lock(&softc->ctl_lock); 1428 port->wwpn_iid[iid].in_use--; 1429 port->wwpn_iid[iid].last_use = time_uptime; 1430 mtx_unlock(&softc->ctl_lock); 1431 1432 return (0); 1433} 1434 1435/* 1436 * Add an initiator to the initiator map. 1437 * Returns iid for success, < 0 for failure. 1438 */ 1439int 1440ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1441{ 1442 struct ctl_softc *softc = control_softc; 1443 time_t best_time; 1444 int i, best; 1445 1446 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1447 1448 if (iid >= CTL_MAX_INIT_PER_PORT) { 1449 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1450 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1451 free(name, M_CTL); 1452 return (-1); 1453 } 1454 1455 mtx_lock(&softc->ctl_lock); 1456 1457 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1458 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1459 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1460 iid = i; 1461 break; 1462 } 1463 if (name != NULL && port->wwpn_iid[i].name != NULL && 1464 strcmp(name, port->wwpn_iid[i].name) == 0) { 1465 iid = i; 1466 break; 1467 } 1468 } 1469 } 1470 1471 if (iid < 0) { 1472 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1473 if (port->wwpn_iid[i].in_use == 0 && 1474 port->wwpn_iid[i].wwpn == 0 && 1475 port->wwpn_iid[i].name == NULL) { 1476 iid = i; 1477 break; 1478 } 1479 } 1480 } 1481 1482 if (iid < 0) { 1483 best = -1; 1484 best_time = INT32_MAX; 1485 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1486 if (port->wwpn_iid[i].in_use == 0) { 1487 if (port->wwpn_iid[i].last_use < best_time) { 1488 best = i; 1489 best_time = port->wwpn_iid[i].last_use; 1490 } 1491 } 1492 } 1493 iid = best; 1494 } 1495 1496 if (iid < 0) { 1497 mtx_unlock(&softc->ctl_lock); 1498 free(name, M_CTL); 1499 return (-2); 1500 } 1501 1502 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1503 /* 1504 * This is not an error yet. 1505 */ 1506 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1507#if 0 1508 printf("%s: port %d iid %u WWPN %#jx arrived" 1509 " again\n", __func__, port->targ_port, 1510 iid, (uintmax_t)wwpn); 1511#endif 1512 goto take; 1513 } 1514 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1515 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1516#if 0 1517 printf("%s: port %d iid %u name '%s' arrived" 1518 " again\n", __func__, port->targ_port, 1519 iid, name); 1520#endif 1521 goto take; 1522 } 1523 1524 /* 1525 * This is an error, but what do we do about it? The 1526 * driver is telling us we have a new WWPN for this 1527 * initiator ID, so we pretty much need to use it. 1528 */ 1529 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1530 " but WWPN %#jx '%s' is still at that address\n", 1531 __func__, port->targ_port, iid, wwpn, name, 1532 (uintmax_t)port->wwpn_iid[iid].wwpn, 1533 port->wwpn_iid[iid].name); 1534 1535 /* 1536 * XXX KDM clear have_ca and ua_pending on each LUN for 1537 * this initiator. 1538 */ 1539 } 1540take: 1541 free(port->wwpn_iid[iid].name, M_CTL); 1542 port->wwpn_iid[iid].name = name; 1543 port->wwpn_iid[iid].wwpn = wwpn; 1544 port->wwpn_iid[iid].in_use++; 1545 mtx_unlock(&softc->ctl_lock); 1546 1547 return (iid); 1548} 1549 1550static int 1551ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1552{ 1553 int len; 1554 1555 switch (port->port_type) { 1556 case CTL_PORT_FC: 1557 { 1558 struct scsi_transportid_fcp *id = 1559 (struct scsi_transportid_fcp *)buf; 1560 if (port->wwpn_iid[iid].wwpn == 0) 1561 return (0); 1562 memset(id, 0, sizeof(*id)); 1563 id->format_protocol = SCSI_PROTO_FC; 1564 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1565 return (sizeof(*id)); 1566 } 1567 case CTL_PORT_ISCSI: 1568 { 1569 struct scsi_transportid_iscsi_port *id = 1570 (struct scsi_transportid_iscsi_port *)buf; 1571 if (port->wwpn_iid[iid].name == NULL) 1572 return (0); 1573 memset(id, 0, 256); 1574 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1575 SCSI_PROTO_ISCSI; 1576 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1577 len = roundup2(min(len, 252), 4); 1578 scsi_ulto2b(len, id->additional_length); 1579 return (sizeof(*id) + len); 1580 } 1581 case CTL_PORT_SAS: 1582 { 1583 struct scsi_transportid_sas *id = 1584 (struct scsi_transportid_sas *)buf; 1585 if (port->wwpn_iid[iid].wwpn == 0) 1586 return (0); 1587 memset(id, 0, sizeof(*id)); 1588 id->format_protocol = SCSI_PROTO_SAS; 1589 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1590 return (sizeof(*id)); 1591 } 1592 default: 1593 { 1594 struct scsi_transportid_spi *id = 1595 (struct scsi_transportid_spi *)buf; 1596 memset(id, 0, sizeof(*id)); 1597 id->format_protocol = SCSI_PROTO_SPI; 1598 scsi_ulto2b(iid, id->scsi_addr); 1599 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1600 return (sizeof(*id)); 1601 } 1602 } 1603} 1604 1605static int 1606ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1607{ 1608 return (0); 1609} 1610 1611static int 1612ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1613{ 1614 return (0); 1615} 1616 1617/* 1618 * Data movement routine for the CTL ioctl frontend port. 1619 */ 1620static int 1621ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1622{ 1623 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1624 struct ctl_sg_entry ext_entry, kern_entry; 1625 int ext_sglen, ext_sg_entries, kern_sg_entries; 1626 int ext_sg_start, ext_offset; 1627 int len_to_copy, len_copied; 1628 int kern_watermark, ext_watermark; 1629 int ext_sglist_malloced; 1630 int i, j; 1631 1632 ext_sglist_malloced = 0; 1633 ext_sg_start = 0; 1634 ext_offset = 0; 1635 1636 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1637 1638 /* 1639 * If this flag is set, fake the data transfer. 1640 */ 1641 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1642 ctsio->ext_data_filled = ctsio->ext_data_len; 1643 goto bailout; 1644 } 1645 1646 /* 1647 * To simplify things here, if we have a single buffer, stick it in 1648 * a S/G entry and just make it a single entry S/G list. 1649 */ 1650 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1651 int len_seen; 1652 1653 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1654 1655 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1656 M_WAITOK); 1657 ext_sglist_malloced = 1; 1658 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1659 ext_sglen) != 0) { 1660 ctl_set_internal_failure(ctsio, 1661 /*sks_valid*/ 0, 1662 /*retry_count*/ 0); 1663 goto bailout; 1664 } 1665 ext_sg_entries = ctsio->ext_sg_entries; 1666 len_seen = 0; 1667 for (i = 0; i < ext_sg_entries; i++) { 1668 if ((len_seen + ext_sglist[i].len) >= 1669 ctsio->ext_data_filled) { 1670 ext_sg_start = i; 1671 ext_offset = ctsio->ext_data_filled - len_seen; 1672 break; 1673 } 1674 len_seen += ext_sglist[i].len; 1675 } 1676 } else { 1677 ext_sglist = &ext_entry; 1678 ext_sglist->addr = ctsio->ext_data_ptr; 1679 ext_sglist->len = ctsio->ext_data_len; 1680 ext_sg_entries = 1; 1681 ext_sg_start = 0; 1682 ext_offset = ctsio->ext_data_filled; 1683 } 1684 1685 if (ctsio->kern_sg_entries > 0) { 1686 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1687 kern_sg_entries = ctsio->kern_sg_entries; 1688 } else { 1689 kern_sglist = &kern_entry; 1690 kern_sglist->addr = ctsio->kern_data_ptr; 1691 kern_sglist->len = ctsio->kern_data_len; 1692 kern_sg_entries = 1; 1693 } 1694 1695 1696 kern_watermark = 0; 1697 ext_watermark = ext_offset; 1698 len_copied = 0; 1699 for (i = ext_sg_start, j = 0; 1700 i < ext_sg_entries && j < kern_sg_entries;) { 1701 uint8_t *ext_ptr, *kern_ptr; 1702 1703 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1704 kern_sglist[j].len - kern_watermark); 1705 1706 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1707 ext_ptr = ext_ptr + ext_watermark; 1708 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1709 /* 1710 * XXX KDM fix this! 1711 */ 1712 panic("need to implement bus address support"); 1713#if 0 1714 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1715#endif 1716 } else 1717 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1718 kern_ptr = kern_ptr + kern_watermark; 1719 1720 kern_watermark += len_to_copy; 1721 ext_watermark += len_to_copy; 1722 1723 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1724 CTL_FLAG_DATA_IN) { 1725 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1726 "bytes to user\n", len_to_copy)); 1727 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1728 "to %p\n", kern_ptr, ext_ptr)); 1729 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1730 ctl_set_internal_failure(ctsio, 1731 /*sks_valid*/ 0, 1732 /*retry_count*/ 0); 1733 goto bailout; 1734 } 1735 } else { 1736 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1737 "bytes from user\n", len_to_copy)); 1738 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1739 "to %p\n", ext_ptr, kern_ptr)); 1740 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1741 ctl_set_internal_failure(ctsio, 1742 /*sks_valid*/ 0, 1743 /*retry_count*/0); 1744 goto bailout; 1745 } 1746 } 1747 1748 len_copied += len_to_copy; 1749 1750 if (ext_sglist[i].len == ext_watermark) { 1751 i++; 1752 ext_watermark = 0; 1753 } 1754 1755 if (kern_sglist[j].len == kern_watermark) { 1756 j++; 1757 kern_watermark = 0; 1758 } 1759 } 1760 1761 ctsio->ext_data_filled += len_copied; 1762 1763 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1764 "kern_sg_entries: %d\n", ext_sg_entries, 1765 kern_sg_entries)); 1766 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1767 "kern_data_len = %d\n", ctsio->ext_data_len, 1768 ctsio->kern_data_len)); 1769 1770 1771 /* XXX KDM set residual?? */ 1772bailout: 1773 1774 if (ext_sglist_malloced != 0) 1775 free(ext_sglist, M_CTL); 1776 1777 return (CTL_RETVAL_COMPLETE); 1778} 1779 1780/* 1781 * Serialize a command that went down the "wrong" side, and so was sent to 1782 * this controller for execution. The logic is a little different than the 1783 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1784 * sent back to the other side, but in the success case, we execute the 1785 * command on this side (XFER mode) or tell the other side to execute it 1786 * (SER_ONLY mode). 1787 */ 1788static int 1789ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1790{ 1791 struct ctl_softc *ctl_softc; 1792 union ctl_ha_msg msg_info; 1793 struct ctl_lun *lun; 1794 int retval = 0; 1795 uint32_t targ_lun; 1796 1797 ctl_softc = control_softc; 1798 1799 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1800 lun = ctl_softc->ctl_luns[targ_lun]; 1801 if (lun==NULL) 1802 { 1803 /* 1804 * Why isn't LUN defined? The other side wouldn't 1805 * send a cmd if the LUN is undefined. 1806 */ 1807 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1808 1809 /* "Logical unit not supported" */ 1810 ctl_set_sense_data(&msg_info.scsi.sense_data, 1811 lun, 1812 /*sense_format*/SSD_TYPE_NONE, 1813 /*current_error*/ 1, 1814 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1815 /*asc*/ 0x25, 1816 /*ascq*/ 0x00, 1817 SSD_ELEM_NONE); 1818 1819 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1820 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1821 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1822 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1823 msg_info.hdr.serializing_sc = NULL; 1824 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1825 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1826 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1827 } 1828 return(1); 1829 1830 } 1831 1832 mtx_lock(&lun->lun_lock); 1833 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1834 1835 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1836 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1837 ooa_links))) { 1838 case CTL_ACTION_BLOCK: 1839 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1840 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1841 blocked_links); 1842 break; 1843 case CTL_ACTION_PASS: 1844 case CTL_ACTION_SKIP: 1845 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1846 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1847 ctl_enqueue_rtr((union ctl_io *)ctsio); 1848 } else { 1849 1850 /* send msg back to other side */ 1851 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1852 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1853 msg_info.hdr.msg_type = CTL_MSG_R2R; 1854#if 0 1855 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1856#endif 1857 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1858 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1859 } 1860 } 1861 break; 1862 case CTL_ACTION_OVERLAP: 1863 /* OVERLAPPED COMMANDS ATTEMPTED */ 1864 ctl_set_sense_data(&msg_info.scsi.sense_data, 1865 lun, 1866 /*sense_format*/SSD_TYPE_NONE, 1867 /*current_error*/ 1, 1868 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1869 /*asc*/ 0x4E, 1870 /*ascq*/ 0x00, 1871 SSD_ELEM_NONE); 1872 1873 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1874 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1875 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1876 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1877 msg_info.hdr.serializing_sc = NULL; 1878 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1879#if 0 1880 printf("BAD JUJU:Major Bummer Overlap\n"); 1881#endif 1882 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1883 retval = 1; 1884 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1885 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1886 } 1887 break; 1888 case CTL_ACTION_OVERLAP_TAG: 1889 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1890 ctl_set_sense_data(&msg_info.scsi.sense_data, 1891 lun, 1892 /*sense_format*/SSD_TYPE_NONE, 1893 /*current_error*/ 1, 1894 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1895 /*asc*/ 0x4D, 1896 /*ascq*/ ctsio->tag_num & 0xff, 1897 SSD_ELEM_NONE); 1898 1899 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1900 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1901 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1902 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1903 msg_info.hdr.serializing_sc = NULL; 1904 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1905#if 0 1906 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1907#endif 1908 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1909 retval = 1; 1910 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1911 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1912 } 1913 break; 1914 case CTL_ACTION_ERROR: 1915 default: 1916 /* "Internal target failure" */ 1917 ctl_set_sense_data(&msg_info.scsi.sense_data, 1918 lun, 1919 /*sense_format*/SSD_TYPE_NONE, 1920 /*current_error*/ 1, 1921 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1922 /*asc*/ 0x44, 1923 /*ascq*/ 0x00, 1924 SSD_ELEM_NONE); 1925 1926 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1927 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1928 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1929 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1930 msg_info.hdr.serializing_sc = NULL; 1931 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1932#if 0 1933 printf("BAD JUJU:Major Bummer HW Error\n"); 1934#endif 1935 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1936 retval = 1; 1937 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1938 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1939 } 1940 break; 1941 } 1942 mtx_unlock(&lun->lun_lock); 1943 return (retval); 1944} 1945 1946static int 1947ctl_ioctl_submit_wait(union ctl_io *io) 1948{ 1949 struct ctl_fe_ioctl_params params; 1950 ctl_fe_ioctl_state last_state; 1951 int done, retval; 1952 1953 retval = 0; 1954 1955 bzero(¶ms, sizeof(params)); 1956 1957 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1958 cv_init(¶ms.sem, "ctlioccv"); 1959 params.state = CTL_IOCTL_INPROG; 1960 last_state = params.state; 1961 1962 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1963 1964 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1965 1966 /* This shouldn't happen */ 1967 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1968 return (retval); 1969 1970 done = 0; 1971 1972 do { 1973 mtx_lock(¶ms.ioctl_mtx); 1974 /* 1975 * Check the state here, and don't sleep if the state has 1976 * already changed (i.e. wakeup has already occured, but we 1977 * weren't waiting yet). 1978 */ 1979 if (params.state == last_state) { 1980 /* XXX KDM cv_wait_sig instead? */ 1981 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1982 } 1983 last_state = params.state; 1984 1985 switch (params.state) { 1986 case CTL_IOCTL_INPROG: 1987 /* Why did we wake up? */ 1988 /* XXX KDM error here? */ 1989 mtx_unlock(¶ms.ioctl_mtx); 1990 break; 1991 case CTL_IOCTL_DATAMOVE: 1992 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1993 1994 /* 1995 * change last_state back to INPROG to avoid 1996 * deadlock on subsequent data moves. 1997 */ 1998 params.state = last_state = CTL_IOCTL_INPROG; 1999 2000 mtx_unlock(¶ms.ioctl_mtx); 2001 ctl_ioctl_do_datamove(&io->scsiio); 2002 /* 2003 * Note that in some cases, most notably writes, 2004 * this will queue the I/O and call us back later. 2005 * In other cases, generally reads, this routine 2006 * will immediately call back and wake us up, 2007 * probably using our own context. 2008 */ 2009 io->scsiio.be_move_done(io); 2010 break; 2011 case CTL_IOCTL_DONE: 2012 mtx_unlock(¶ms.ioctl_mtx); 2013 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 2014 done = 1; 2015 break; 2016 default: 2017 mtx_unlock(¶ms.ioctl_mtx); 2018 /* XXX KDM error here? */ 2019 break; 2020 } 2021 } while (done == 0); 2022 2023 mtx_destroy(¶ms.ioctl_mtx); 2024 cv_destroy(¶ms.sem); 2025 2026 return (CTL_RETVAL_COMPLETE); 2027} 2028 2029static void 2030ctl_ioctl_datamove(union ctl_io *io) 2031{ 2032 struct ctl_fe_ioctl_params *params; 2033 2034 params = (struct ctl_fe_ioctl_params *) 2035 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2036 2037 mtx_lock(¶ms->ioctl_mtx); 2038 params->state = CTL_IOCTL_DATAMOVE; 2039 cv_broadcast(¶ms->sem); 2040 mtx_unlock(¶ms->ioctl_mtx); 2041} 2042 2043static void 2044ctl_ioctl_done(union ctl_io *io) 2045{ 2046 struct ctl_fe_ioctl_params *params; 2047 2048 params = (struct ctl_fe_ioctl_params *) 2049 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2050 2051 mtx_lock(¶ms->ioctl_mtx); 2052 params->state = CTL_IOCTL_DONE; 2053 cv_broadcast(¶ms->sem); 2054 mtx_unlock(¶ms->ioctl_mtx); 2055} 2056 2057static void 2058ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2059{ 2060 struct ctl_fe_ioctl_startstop_info *sd_info; 2061 2062 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2063 2064 sd_info->hs_info.status = metatask->status; 2065 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2066 sd_info->hs_info.luns_complete = 2067 metatask->taskinfo.startstop.luns_complete; 2068 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2069 2070 cv_broadcast(&sd_info->sem); 2071} 2072 2073static void 2074ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2075{ 2076 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2077 2078 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2079 2080 mtx_lock(fe_bbr_info->lock); 2081 fe_bbr_info->bbr_info->status = metatask->status; 2082 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2083 fe_bbr_info->wakeup_done = 1; 2084 mtx_unlock(fe_bbr_info->lock); 2085 2086 cv_broadcast(&fe_bbr_info->sem); 2087} 2088 2089/* 2090 * Returns 0 for success, errno for failure. 2091 */ 2092static int 2093ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2094 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2095{ 2096 union ctl_io *io; 2097 int retval; 2098 2099 retval = 0; 2100 2101 mtx_lock(&lun->lun_lock); 2102 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2103 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2104 ooa_links)) { 2105 struct ctl_ooa_entry *entry; 2106 2107 /* 2108 * If we've got more than we can fit, just count the 2109 * remaining entries. 2110 */ 2111 if (*cur_fill_num >= ooa_hdr->alloc_num) 2112 continue; 2113 2114 entry = &kern_entries[*cur_fill_num]; 2115 2116 entry->tag_num = io->scsiio.tag_num; 2117 entry->lun_num = lun->lun; 2118#ifdef CTL_TIME_IO 2119 entry->start_bt = io->io_hdr.start_bt; 2120#endif 2121 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2122 entry->cdb_len = io->scsiio.cdb_len; 2123 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2124 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2125 2126 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2127 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2128 2129 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2130 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2131 2132 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2133 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2134 2135 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2136 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2137 } 2138 mtx_unlock(&lun->lun_lock); 2139 2140 return (retval); 2141} 2142 2143static void * 2144ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2145 size_t error_str_len) 2146{ 2147 void *kptr; 2148 2149 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2150 2151 if (copyin(user_addr, kptr, len) != 0) { 2152 snprintf(error_str, error_str_len, "Error copying %d bytes " 2153 "from user address %p to kernel address %p", len, 2154 user_addr, kptr); 2155 free(kptr, M_CTL); 2156 return (NULL); 2157 } 2158 2159 return (kptr); 2160} 2161 2162static void 2163ctl_free_args(int num_args, struct ctl_be_arg *args) 2164{ 2165 int i; 2166 2167 if (args == NULL) 2168 return; 2169 2170 for (i = 0; i < num_args; i++) { 2171 free(args[i].kname, M_CTL); 2172 free(args[i].kvalue, M_CTL); 2173 } 2174 2175 free(args, M_CTL); 2176} 2177 2178static struct ctl_be_arg * 2179ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2180 char *error_str, size_t error_str_len) 2181{ 2182 struct ctl_be_arg *args; 2183 int i; 2184 2185 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2186 error_str, error_str_len); 2187 2188 if (args == NULL) 2189 goto bailout; 2190 2191 for (i = 0; i < num_args; i++) { 2192 args[i].kname = NULL; 2193 args[i].kvalue = NULL; 2194 } 2195 2196 for (i = 0; i < num_args; i++) { 2197 uint8_t *tmpptr; 2198 2199 args[i].kname = ctl_copyin_alloc(args[i].name, 2200 args[i].namelen, error_str, error_str_len); 2201 if (args[i].kname == NULL) 2202 goto bailout; 2203 2204 if (args[i].kname[args[i].namelen - 1] != '\0') { 2205 snprintf(error_str, error_str_len, "Argument %d " 2206 "name is not NUL-terminated", i); 2207 goto bailout; 2208 } 2209 2210 if (args[i].flags & CTL_BEARG_RD) { 2211 tmpptr = ctl_copyin_alloc(args[i].value, 2212 args[i].vallen, error_str, error_str_len); 2213 if (tmpptr == NULL) 2214 goto bailout; 2215 if ((args[i].flags & CTL_BEARG_ASCII) 2216 && (tmpptr[args[i].vallen - 1] != '\0')) { 2217 snprintf(error_str, error_str_len, "Argument " 2218 "%d value is not NUL-terminated", i); 2219 goto bailout; 2220 } 2221 args[i].kvalue = tmpptr; 2222 } else { 2223 args[i].kvalue = malloc(args[i].vallen, 2224 M_CTL, M_WAITOK | M_ZERO); 2225 } 2226 } 2227 2228 return (args); 2229bailout: 2230 2231 ctl_free_args(num_args, args); 2232 2233 return (NULL); 2234} 2235 2236static void 2237ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2238{ 2239 int i; 2240 2241 for (i = 0; i < num_args; i++) { 2242 if (args[i].flags & CTL_BEARG_WR) 2243 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2244 } 2245} 2246 2247/* 2248 * Escape characters that are illegal or not recommended in XML. 2249 */ 2250int 2251ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2252{ 2253 char *end = str + size; 2254 int retval; 2255 2256 retval = 0; 2257 2258 for (; *str && str < end; str++) { 2259 switch (*str) { 2260 case '&': 2261 retval = sbuf_printf(sb, "&"); 2262 break; 2263 case '>': 2264 retval = sbuf_printf(sb, ">"); 2265 break; 2266 case '<': 2267 retval = sbuf_printf(sb, "<"); 2268 break; 2269 default: 2270 retval = sbuf_putc(sb, *str); 2271 break; 2272 } 2273 2274 if (retval != 0) 2275 break; 2276 2277 } 2278 2279 return (retval); 2280} 2281 2282static void 2283ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2284{ 2285 struct scsi_vpd_id_descriptor *desc; 2286 int i; 2287 2288 if (id == NULL || id->len < 4) 2289 return; 2290 desc = (struct scsi_vpd_id_descriptor *)id->data; 2291 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2292 case SVPD_ID_TYPE_T10: 2293 sbuf_printf(sb, "t10."); 2294 break; 2295 case SVPD_ID_TYPE_EUI64: 2296 sbuf_printf(sb, "eui."); 2297 break; 2298 case SVPD_ID_TYPE_NAA: 2299 sbuf_printf(sb, "naa."); 2300 break; 2301 case SVPD_ID_TYPE_SCSI_NAME: 2302 break; 2303 } 2304 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2305 case SVPD_ID_CODESET_BINARY: 2306 for (i = 0; i < desc->length; i++) 2307 sbuf_printf(sb, "%02x", desc->identifier[i]); 2308 break; 2309 case SVPD_ID_CODESET_ASCII: 2310 sbuf_printf(sb, "%.*s", (int)desc->length, 2311 (char *)desc->identifier); 2312 break; 2313 case SVPD_ID_CODESET_UTF8: 2314 sbuf_printf(sb, "%s", (char *)desc->identifier); 2315 break; 2316 } 2317} 2318 2319static int 2320ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2321 struct thread *td) 2322{ 2323 struct ctl_softc *softc; 2324 int retval; 2325 2326 softc = control_softc; 2327 2328 retval = 0; 2329 2330 switch (cmd) { 2331 case CTL_IO: { 2332 union ctl_io *io; 2333 void *pool_tmp; 2334 2335 /* 2336 * If we haven't been "enabled", don't allow any SCSI I/O 2337 * to this FETD. 2338 */ 2339 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2340 retval = EPERM; 2341 break; 2342 } 2343 2344 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2345 if (io == NULL) { 2346 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2347 retval = ENOSPC; 2348 break; 2349 } 2350 2351 /* 2352 * Need to save the pool reference so it doesn't get 2353 * spammed by the user's ctl_io. 2354 */ 2355 pool_tmp = io->io_hdr.pool; 2356 2357 memcpy(io, (void *)addr, sizeof(*io)); 2358 2359 io->io_hdr.pool = pool_tmp; 2360 /* 2361 * No status yet, so make sure the status is set properly. 2362 */ 2363 io->io_hdr.status = CTL_STATUS_NONE; 2364 2365 /* 2366 * The user sets the initiator ID, target and LUN IDs. 2367 */ 2368 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2369 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2370 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2371 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2372 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2373 2374 retval = ctl_ioctl_submit_wait(io); 2375 2376 if (retval != 0) { 2377 ctl_free_io(io); 2378 break; 2379 } 2380 2381 memcpy((void *)addr, io, sizeof(*io)); 2382 2383 /* return this to our pool */ 2384 ctl_free_io(io); 2385 2386 break; 2387 } 2388 case CTL_ENABLE_PORT: 2389 case CTL_DISABLE_PORT: 2390 case CTL_SET_PORT_WWNS: { 2391 struct ctl_port *port; 2392 struct ctl_port_entry *entry; 2393 2394 entry = (struct ctl_port_entry *)addr; 2395 2396 mtx_lock(&softc->ctl_lock); 2397 STAILQ_FOREACH(port, &softc->port_list, links) { 2398 int action, done; 2399 2400 action = 0; 2401 done = 0; 2402 2403 if ((entry->port_type == CTL_PORT_NONE) 2404 && (entry->targ_port == port->targ_port)) { 2405 /* 2406 * If the user only wants to enable or 2407 * disable or set WWNs on a specific port, 2408 * do the operation and we're done. 2409 */ 2410 action = 1; 2411 done = 1; 2412 } else if (entry->port_type & port->port_type) { 2413 /* 2414 * Compare the user's type mask with the 2415 * particular frontend type to see if we 2416 * have a match. 2417 */ 2418 action = 1; 2419 done = 0; 2420 2421 /* 2422 * Make sure the user isn't trying to set 2423 * WWNs on multiple ports at the same time. 2424 */ 2425 if (cmd == CTL_SET_PORT_WWNS) { 2426 printf("%s: Can't set WWNs on " 2427 "multiple ports\n", __func__); 2428 retval = EINVAL; 2429 break; 2430 } 2431 } 2432 if (action != 0) { 2433 /* 2434 * XXX KDM we have to drop the lock here, 2435 * because the online/offline operations 2436 * can potentially block. We need to 2437 * reference count the frontends so they 2438 * can't go away, 2439 */ 2440 mtx_unlock(&softc->ctl_lock); 2441 2442 if (cmd == CTL_ENABLE_PORT) { 2443 struct ctl_lun *lun; 2444 2445 STAILQ_FOREACH(lun, &softc->lun_list, 2446 links) { 2447 port->lun_enable(port->targ_lun_arg, 2448 lun->target, 2449 lun->lun); 2450 } 2451 2452 ctl_port_online(port); 2453 } else if (cmd == CTL_DISABLE_PORT) { 2454 struct ctl_lun *lun; 2455 2456 ctl_port_offline(port); 2457 2458 STAILQ_FOREACH(lun, &softc->lun_list, 2459 links) { 2460 port->lun_disable( 2461 port->targ_lun_arg, 2462 lun->target, 2463 lun->lun); 2464 } 2465 } 2466 2467 mtx_lock(&softc->ctl_lock); 2468 2469 if (cmd == CTL_SET_PORT_WWNS) 2470 ctl_port_set_wwns(port, 2471 (entry->flags & CTL_PORT_WWNN_VALID) ? 2472 1 : 0, entry->wwnn, 2473 (entry->flags & CTL_PORT_WWPN_VALID) ? 2474 1 : 0, entry->wwpn); 2475 } 2476 if (done != 0) 2477 break; 2478 } 2479 mtx_unlock(&softc->ctl_lock); 2480 break; 2481 } 2482 case CTL_GET_PORT_LIST: { 2483 struct ctl_port *port; 2484 struct ctl_port_list *list; 2485 int i; 2486 2487 list = (struct ctl_port_list *)addr; 2488 2489 if (list->alloc_len != (list->alloc_num * 2490 sizeof(struct ctl_port_entry))) { 2491 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2492 "alloc_num %u * sizeof(struct ctl_port_entry) " 2493 "%zu\n", __func__, list->alloc_len, 2494 list->alloc_num, sizeof(struct ctl_port_entry)); 2495 retval = EINVAL; 2496 break; 2497 } 2498 list->fill_len = 0; 2499 list->fill_num = 0; 2500 list->dropped_num = 0; 2501 i = 0; 2502 mtx_lock(&softc->ctl_lock); 2503 STAILQ_FOREACH(port, &softc->port_list, links) { 2504 struct ctl_port_entry entry, *list_entry; 2505 2506 if (list->fill_num >= list->alloc_num) { 2507 list->dropped_num++; 2508 continue; 2509 } 2510 2511 entry.port_type = port->port_type; 2512 strlcpy(entry.port_name, port->port_name, 2513 sizeof(entry.port_name)); 2514 entry.targ_port = port->targ_port; 2515 entry.physical_port = port->physical_port; 2516 entry.virtual_port = port->virtual_port; 2517 entry.wwnn = port->wwnn; 2518 entry.wwpn = port->wwpn; 2519 if (port->status & CTL_PORT_STATUS_ONLINE) 2520 entry.online = 1; 2521 else 2522 entry.online = 0; 2523 2524 list_entry = &list->entries[i]; 2525 2526 retval = copyout(&entry, list_entry, sizeof(entry)); 2527 if (retval != 0) { 2528 printf("%s: CTL_GET_PORT_LIST: copyout " 2529 "returned %d\n", __func__, retval); 2530 break; 2531 } 2532 i++; 2533 list->fill_num++; 2534 list->fill_len += sizeof(entry); 2535 } 2536 mtx_unlock(&softc->ctl_lock); 2537 2538 /* 2539 * If this is non-zero, we had a copyout fault, so there's 2540 * probably no point in attempting to set the status inside 2541 * the structure. 2542 */ 2543 if (retval != 0) 2544 break; 2545 2546 if (list->dropped_num > 0) 2547 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2548 else 2549 list->status = CTL_PORT_LIST_OK; 2550 break; 2551 } 2552 case CTL_DUMP_OOA: { 2553 struct ctl_lun *lun; 2554 union ctl_io *io; 2555 char printbuf[128]; 2556 struct sbuf sb; 2557 2558 mtx_lock(&softc->ctl_lock); 2559 printf("Dumping OOA queues:\n"); 2560 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2561 mtx_lock(&lun->lun_lock); 2562 for (io = (union ctl_io *)TAILQ_FIRST( 2563 &lun->ooa_queue); io != NULL; 2564 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2565 ooa_links)) { 2566 sbuf_new(&sb, printbuf, sizeof(printbuf), 2567 SBUF_FIXEDLEN); 2568 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2569 (intmax_t)lun->lun, 2570 io->scsiio.tag_num, 2571 (io->io_hdr.flags & 2572 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2573 (io->io_hdr.flags & 2574 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2575 (io->io_hdr.flags & 2576 CTL_FLAG_ABORT) ? " ABORT" : "", 2577 (io->io_hdr.flags & 2578 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2579 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2580 sbuf_finish(&sb); 2581 printf("%s\n", sbuf_data(&sb)); 2582 } 2583 mtx_unlock(&lun->lun_lock); 2584 } 2585 printf("OOA queues dump done\n"); 2586 mtx_unlock(&softc->ctl_lock); 2587 break; 2588 } 2589 case CTL_GET_OOA: { 2590 struct ctl_lun *lun; 2591 struct ctl_ooa *ooa_hdr; 2592 struct ctl_ooa_entry *entries; 2593 uint32_t cur_fill_num; 2594 2595 ooa_hdr = (struct ctl_ooa *)addr; 2596 2597 if ((ooa_hdr->alloc_len == 0) 2598 || (ooa_hdr->alloc_num == 0)) { 2599 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2600 "must be non-zero\n", __func__, 2601 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2602 retval = EINVAL; 2603 break; 2604 } 2605 2606 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2607 sizeof(struct ctl_ooa_entry))) { 2608 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2609 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2610 __func__, ooa_hdr->alloc_len, 2611 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2612 retval = EINVAL; 2613 break; 2614 } 2615 2616 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2617 if (entries == NULL) { 2618 printf("%s: could not allocate %d bytes for OOA " 2619 "dump\n", __func__, ooa_hdr->alloc_len); 2620 retval = ENOMEM; 2621 break; 2622 } 2623 2624 mtx_lock(&softc->ctl_lock); 2625 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2626 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2627 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2628 mtx_unlock(&softc->ctl_lock); 2629 free(entries, M_CTL); 2630 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2631 __func__, (uintmax_t)ooa_hdr->lun_num); 2632 retval = EINVAL; 2633 break; 2634 } 2635 2636 cur_fill_num = 0; 2637 2638 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2639 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2640 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2641 ooa_hdr, entries); 2642 if (retval != 0) 2643 break; 2644 } 2645 if (retval != 0) { 2646 mtx_unlock(&softc->ctl_lock); 2647 free(entries, M_CTL); 2648 break; 2649 } 2650 } else { 2651 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2652 2653 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2654 entries); 2655 } 2656 mtx_unlock(&softc->ctl_lock); 2657 2658 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2659 ooa_hdr->fill_len = ooa_hdr->fill_num * 2660 sizeof(struct ctl_ooa_entry); 2661 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2662 if (retval != 0) { 2663 printf("%s: error copying out %d bytes for OOA dump\n", 2664 __func__, ooa_hdr->fill_len); 2665 } 2666 2667 getbintime(&ooa_hdr->cur_bt); 2668 2669 if (cur_fill_num > ooa_hdr->alloc_num) { 2670 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2671 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2672 } else { 2673 ooa_hdr->dropped_num = 0; 2674 ooa_hdr->status = CTL_OOA_OK; 2675 } 2676 2677 free(entries, M_CTL); 2678 break; 2679 } 2680 case CTL_CHECK_OOA: { 2681 union ctl_io *io; 2682 struct ctl_lun *lun; 2683 struct ctl_ooa_info *ooa_info; 2684 2685 2686 ooa_info = (struct ctl_ooa_info *)addr; 2687 2688 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2689 ooa_info->status = CTL_OOA_INVALID_LUN; 2690 break; 2691 } 2692 mtx_lock(&softc->ctl_lock); 2693 lun = softc->ctl_luns[ooa_info->lun_id]; 2694 if (lun == NULL) { 2695 mtx_unlock(&softc->ctl_lock); 2696 ooa_info->status = CTL_OOA_INVALID_LUN; 2697 break; 2698 } 2699 mtx_lock(&lun->lun_lock); 2700 mtx_unlock(&softc->ctl_lock); 2701 ooa_info->num_entries = 0; 2702 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2703 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2704 &io->io_hdr, ooa_links)) { 2705 ooa_info->num_entries++; 2706 } 2707 mtx_unlock(&lun->lun_lock); 2708 2709 ooa_info->status = CTL_OOA_SUCCESS; 2710 2711 break; 2712 } 2713 case CTL_HARD_START: 2714 case CTL_HARD_STOP: { 2715 struct ctl_fe_ioctl_startstop_info ss_info; 2716 struct cfi_metatask *metatask; 2717 struct mtx hs_mtx; 2718 2719 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2720 2721 cv_init(&ss_info.sem, "hard start/stop cv" ); 2722 2723 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2724 if (metatask == NULL) { 2725 retval = ENOMEM; 2726 mtx_destroy(&hs_mtx); 2727 break; 2728 } 2729 2730 if (cmd == CTL_HARD_START) 2731 metatask->tasktype = CFI_TASK_STARTUP; 2732 else 2733 metatask->tasktype = CFI_TASK_SHUTDOWN; 2734 2735 metatask->callback = ctl_ioctl_hard_startstop_callback; 2736 metatask->callback_arg = &ss_info; 2737 2738 cfi_action(metatask); 2739 2740 /* Wait for the callback */ 2741 mtx_lock(&hs_mtx); 2742 cv_wait_sig(&ss_info.sem, &hs_mtx); 2743 mtx_unlock(&hs_mtx); 2744 2745 /* 2746 * All information has been copied from the metatask by the 2747 * time cv_broadcast() is called, so we free the metatask here. 2748 */ 2749 cfi_free_metatask(metatask); 2750 2751 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2752 2753 mtx_destroy(&hs_mtx); 2754 break; 2755 } 2756 case CTL_BBRREAD: { 2757 struct ctl_bbrread_info *bbr_info; 2758 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2759 struct mtx bbr_mtx; 2760 struct cfi_metatask *metatask; 2761 2762 bbr_info = (struct ctl_bbrread_info *)addr; 2763 2764 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2765 2766 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2767 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2768 2769 fe_bbr_info.bbr_info = bbr_info; 2770 fe_bbr_info.lock = &bbr_mtx; 2771 2772 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2773 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2774 2775 if (metatask == NULL) { 2776 mtx_destroy(&bbr_mtx); 2777 cv_destroy(&fe_bbr_info.sem); 2778 retval = ENOMEM; 2779 break; 2780 } 2781 metatask->tasktype = CFI_TASK_BBRREAD; 2782 metatask->callback = ctl_ioctl_bbrread_callback; 2783 metatask->callback_arg = &fe_bbr_info; 2784 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2785 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2786 metatask->taskinfo.bbrread.len = bbr_info->len; 2787 2788 cfi_action(metatask); 2789 2790 mtx_lock(&bbr_mtx); 2791 while (fe_bbr_info.wakeup_done == 0) 2792 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2793 mtx_unlock(&bbr_mtx); 2794 2795 bbr_info->status = metatask->status; 2796 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2797 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2798 memcpy(&bbr_info->sense_data, 2799 &metatask->taskinfo.bbrread.sense_data, 2800 ctl_min(sizeof(bbr_info->sense_data), 2801 sizeof(metatask->taskinfo.bbrread.sense_data))); 2802 2803 cfi_free_metatask(metatask); 2804 2805 mtx_destroy(&bbr_mtx); 2806 cv_destroy(&fe_bbr_info.sem); 2807 2808 break; 2809 } 2810 case CTL_DELAY_IO: { 2811 struct ctl_io_delay_info *delay_info; 2812#ifdef CTL_IO_DELAY 2813 struct ctl_lun *lun; 2814#endif /* CTL_IO_DELAY */ 2815 2816 delay_info = (struct ctl_io_delay_info *)addr; 2817 2818#ifdef CTL_IO_DELAY 2819 mtx_lock(&softc->ctl_lock); 2820 2821 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2822 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2823 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2824 } else { 2825 lun = softc->ctl_luns[delay_info->lun_id]; 2826 mtx_lock(&lun->lun_lock); 2827 2828 delay_info->status = CTL_DELAY_STATUS_OK; 2829 2830 switch (delay_info->delay_type) { 2831 case CTL_DELAY_TYPE_CONT: 2832 break; 2833 case CTL_DELAY_TYPE_ONESHOT: 2834 break; 2835 default: 2836 delay_info->status = 2837 CTL_DELAY_STATUS_INVALID_TYPE; 2838 break; 2839 } 2840 2841 switch (delay_info->delay_loc) { 2842 case CTL_DELAY_LOC_DATAMOVE: 2843 lun->delay_info.datamove_type = 2844 delay_info->delay_type; 2845 lun->delay_info.datamove_delay = 2846 delay_info->delay_secs; 2847 break; 2848 case CTL_DELAY_LOC_DONE: 2849 lun->delay_info.done_type = 2850 delay_info->delay_type; 2851 lun->delay_info.done_delay = 2852 delay_info->delay_secs; 2853 break; 2854 default: 2855 delay_info->status = 2856 CTL_DELAY_STATUS_INVALID_LOC; 2857 break; 2858 } 2859 mtx_unlock(&lun->lun_lock); 2860 } 2861 2862 mtx_unlock(&softc->ctl_lock); 2863#else 2864 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2865#endif /* CTL_IO_DELAY */ 2866 break; 2867 } 2868 case CTL_REALSYNC_SET: { 2869 int *syncstate; 2870 2871 syncstate = (int *)addr; 2872 2873 mtx_lock(&softc->ctl_lock); 2874 switch (*syncstate) { 2875 case 0: 2876 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2877 break; 2878 case 1: 2879 softc->flags |= CTL_FLAG_REAL_SYNC; 2880 break; 2881 default: 2882 retval = EINVAL; 2883 break; 2884 } 2885 mtx_unlock(&softc->ctl_lock); 2886 break; 2887 } 2888 case CTL_REALSYNC_GET: { 2889 int *syncstate; 2890 2891 syncstate = (int*)addr; 2892 2893 mtx_lock(&softc->ctl_lock); 2894 if (softc->flags & CTL_FLAG_REAL_SYNC) 2895 *syncstate = 1; 2896 else 2897 *syncstate = 0; 2898 mtx_unlock(&softc->ctl_lock); 2899 2900 break; 2901 } 2902 case CTL_SETSYNC: 2903 case CTL_GETSYNC: { 2904 struct ctl_sync_info *sync_info; 2905 struct ctl_lun *lun; 2906 2907 sync_info = (struct ctl_sync_info *)addr; 2908 2909 mtx_lock(&softc->ctl_lock); 2910 lun = softc->ctl_luns[sync_info->lun_id]; 2911 if (lun == NULL) { 2912 mtx_unlock(&softc->ctl_lock); 2913 sync_info->status = CTL_GS_SYNC_NO_LUN; 2914 } 2915 /* 2916 * Get or set the sync interval. We're not bounds checking 2917 * in the set case, hopefully the user won't do something 2918 * silly. 2919 */ 2920 mtx_lock(&lun->lun_lock); 2921 mtx_unlock(&softc->ctl_lock); 2922 if (cmd == CTL_GETSYNC) 2923 sync_info->sync_interval = lun->sync_interval; 2924 else 2925 lun->sync_interval = sync_info->sync_interval; 2926 mtx_unlock(&lun->lun_lock); 2927 2928 sync_info->status = CTL_GS_SYNC_OK; 2929 2930 break; 2931 } 2932 case CTL_GETSTATS: { 2933 struct ctl_stats *stats; 2934 struct ctl_lun *lun; 2935 int i; 2936 2937 stats = (struct ctl_stats *)addr; 2938 2939 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2940 stats->alloc_len) { 2941 stats->status = CTL_SS_NEED_MORE_SPACE; 2942 stats->num_luns = softc->num_luns; 2943 break; 2944 } 2945 /* 2946 * XXX KDM no locking here. If the LUN list changes, 2947 * things can blow up. 2948 */ 2949 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2950 i++, lun = STAILQ_NEXT(lun, links)) { 2951 retval = copyout(&lun->stats, &stats->lun_stats[i], 2952 sizeof(lun->stats)); 2953 if (retval != 0) 2954 break; 2955 } 2956 stats->num_luns = softc->num_luns; 2957 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2958 softc->num_luns; 2959 stats->status = CTL_SS_OK; 2960#ifdef CTL_TIME_IO 2961 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2962#else 2963 stats->flags = CTL_STATS_FLAG_NONE; 2964#endif 2965 getnanouptime(&stats->timestamp); 2966 break; 2967 } 2968 case CTL_ERROR_INJECT: { 2969 struct ctl_error_desc *err_desc, *new_err_desc; 2970 struct ctl_lun *lun; 2971 2972 err_desc = (struct ctl_error_desc *)addr; 2973 2974 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2975 M_WAITOK | M_ZERO); 2976 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2977 2978 mtx_lock(&softc->ctl_lock); 2979 lun = softc->ctl_luns[err_desc->lun_id]; 2980 if (lun == NULL) { 2981 mtx_unlock(&softc->ctl_lock); 2982 free(new_err_desc, M_CTL); 2983 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2984 __func__, (uintmax_t)err_desc->lun_id); 2985 retval = EINVAL; 2986 break; 2987 } 2988 mtx_lock(&lun->lun_lock); 2989 mtx_unlock(&softc->ctl_lock); 2990 2991 /* 2992 * We could do some checking here to verify the validity 2993 * of the request, but given the complexity of error 2994 * injection requests, the checking logic would be fairly 2995 * complex. 2996 * 2997 * For now, if the request is invalid, it just won't get 2998 * executed and might get deleted. 2999 */ 3000 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 3001 3002 /* 3003 * XXX KDM check to make sure the serial number is unique, 3004 * in case we somehow manage to wrap. That shouldn't 3005 * happen for a very long time, but it's the right thing to 3006 * do. 3007 */ 3008 new_err_desc->serial = lun->error_serial; 3009 err_desc->serial = lun->error_serial; 3010 lun->error_serial++; 3011 3012 mtx_unlock(&lun->lun_lock); 3013 break; 3014 } 3015 case CTL_ERROR_INJECT_DELETE: { 3016 struct ctl_error_desc *delete_desc, *desc, *desc2; 3017 struct ctl_lun *lun; 3018 int delete_done; 3019 3020 delete_desc = (struct ctl_error_desc *)addr; 3021 delete_done = 0; 3022 3023 mtx_lock(&softc->ctl_lock); 3024 lun = softc->ctl_luns[delete_desc->lun_id]; 3025 if (lun == NULL) { 3026 mtx_unlock(&softc->ctl_lock); 3027 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 3028 __func__, (uintmax_t)delete_desc->lun_id); 3029 retval = EINVAL; 3030 break; 3031 } 3032 mtx_lock(&lun->lun_lock); 3033 mtx_unlock(&softc->ctl_lock); 3034 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 3035 if (desc->serial != delete_desc->serial) 3036 continue; 3037 3038 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 3039 links); 3040 free(desc, M_CTL); 3041 delete_done = 1; 3042 } 3043 mtx_unlock(&lun->lun_lock); 3044 if (delete_done == 0) { 3045 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 3046 "error serial %ju on LUN %u\n", __func__, 3047 delete_desc->serial, delete_desc->lun_id); 3048 retval = EINVAL; 3049 break; 3050 } 3051 break; 3052 } 3053 case CTL_DUMP_STRUCTS: { 3054 int i, j, k, idx; 3055 struct ctl_port *port; 3056 struct ctl_frontend *fe; 3057 3058 mtx_lock(&softc->ctl_lock); 3059 printf("CTL Persistent Reservation information start:\n"); 3060 for (i = 0; i < CTL_MAX_LUNS; i++) { 3061 struct ctl_lun *lun; 3062 3063 lun = softc->ctl_luns[i]; 3064 3065 if ((lun == NULL) 3066 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3067 continue; 3068 3069 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3070 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3071 idx = j * CTL_MAX_INIT_PER_PORT + k; 3072 if (lun->pr_keys[idx] == 0) 3073 continue; 3074 printf(" LUN %d port %d iid %d key " 3075 "%#jx\n", i, j, k, 3076 (uintmax_t)lun->pr_keys[idx]); 3077 } 3078 } 3079 } 3080 printf("CTL Persistent Reservation information end\n"); 3081 printf("CTL Ports:\n"); 3082 STAILQ_FOREACH(port, &softc->port_list, links) { 3083 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3084 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3085 port->frontend->name, port->port_type, 3086 port->physical_port, port->virtual_port, 3087 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3088 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3089 if (port->wwpn_iid[j].in_use == 0 && 3090 port->wwpn_iid[j].wwpn == 0 && 3091 port->wwpn_iid[j].name == NULL) 3092 continue; 3093 3094 printf(" iid %u use %d WWPN %#jx '%s'\n", 3095 j, port->wwpn_iid[j].in_use, 3096 (uintmax_t)port->wwpn_iid[j].wwpn, 3097 port->wwpn_iid[j].name); 3098 } 3099 } 3100 printf("CTL Port information end\n"); 3101 mtx_unlock(&softc->ctl_lock); 3102 /* 3103 * XXX KDM calling this without a lock. We'd likely want 3104 * to drop the lock before calling the frontend's dump 3105 * routine anyway. 3106 */ 3107 printf("CTL Frontends:\n"); 3108 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3109 printf(" Frontend '%s'\n", fe->name); 3110 if (fe->fe_dump != NULL) 3111 fe->fe_dump(); 3112 } 3113 printf("CTL Frontend information end\n"); 3114 break; 3115 } 3116 case CTL_LUN_REQ: { 3117 struct ctl_lun_req *lun_req; 3118 struct ctl_backend_driver *backend; 3119 3120 lun_req = (struct ctl_lun_req *)addr; 3121 3122 backend = ctl_backend_find(lun_req->backend); 3123 if (backend == NULL) { 3124 lun_req->status = CTL_LUN_ERROR; 3125 snprintf(lun_req->error_str, 3126 sizeof(lun_req->error_str), 3127 "Backend \"%s\" not found.", 3128 lun_req->backend); 3129 break; 3130 } 3131 if (lun_req->num_be_args > 0) { 3132 lun_req->kern_be_args = ctl_copyin_args( 3133 lun_req->num_be_args, 3134 lun_req->be_args, 3135 lun_req->error_str, 3136 sizeof(lun_req->error_str)); 3137 if (lun_req->kern_be_args == NULL) { 3138 lun_req->status = CTL_LUN_ERROR; 3139 break; 3140 } 3141 } 3142 3143 retval = backend->ioctl(dev, cmd, addr, flag, td); 3144 3145 if (lun_req->num_be_args > 0) { 3146 ctl_copyout_args(lun_req->num_be_args, 3147 lun_req->kern_be_args); 3148 ctl_free_args(lun_req->num_be_args, 3149 lun_req->kern_be_args); 3150 } 3151 break; 3152 } 3153 case CTL_LUN_LIST: { 3154 struct sbuf *sb; 3155 struct ctl_lun *lun; 3156 struct ctl_lun_list *list; 3157 struct ctl_option *opt; 3158 3159 list = (struct ctl_lun_list *)addr; 3160 3161 /* 3162 * Allocate a fixed length sbuf here, based on the length 3163 * of the user's buffer. We could allocate an auto-extending 3164 * buffer, and then tell the user how much larger our 3165 * amount of data is than his buffer, but that presents 3166 * some problems: 3167 * 3168 * 1. The sbuf(9) routines use a blocking malloc, and so 3169 * we can't hold a lock while calling them with an 3170 * auto-extending buffer. 3171 * 3172 * 2. There is not currently a LUN reference counting 3173 * mechanism, outside of outstanding transactions on 3174 * the LUN's OOA queue. So a LUN could go away on us 3175 * while we're getting the LUN number, backend-specific 3176 * information, etc. Thus, given the way things 3177 * currently work, we need to hold the CTL lock while 3178 * grabbing LUN information. 3179 * 3180 * So, from the user's standpoint, the best thing to do is 3181 * allocate what he thinks is a reasonable buffer length, 3182 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3183 * double the buffer length and try again. (And repeat 3184 * that until he succeeds.) 3185 */ 3186 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3187 if (sb == NULL) { 3188 list->status = CTL_LUN_LIST_ERROR; 3189 snprintf(list->error_str, sizeof(list->error_str), 3190 "Unable to allocate %d bytes for LUN list", 3191 list->alloc_len); 3192 break; 3193 } 3194 3195 sbuf_printf(sb, "<ctllunlist>\n"); 3196 3197 mtx_lock(&softc->ctl_lock); 3198 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3199 mtx_lock(&lun->lun_lock); 3200 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3201 (uintmax_t)lun->lun); 3202 3203 /* 3204 * Bail out as soon as we see that we've overfilled 3205 * the buffer. 3206 */ 3207 if (retval != 0) 3208 break; 3209 3210 retval = sbuf_printf(sb, "\t<backend_type>%s" 3211 "</backend_type>\n", 3212 (lun->backend == NULL) ? "none" : 3213 lun->backend->name); 3214 3215 if (retval != 0) 3216 break; 3217 3218 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3219 lun->be_lun->lun_type); 3220 3221 if (retval != 0) 3222 break; 3223 3224 if (lun->backend == NULL) { 3225 retval = sbuf_printf(sb, "</lun>\n"); 3226 if (retval != 0) 3227 break; 3228 continue; 3229 } 3230 3231 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3232 (lun->be_lun->maxlba > 0) ? 3233 lun->be_lun->maxlba + 1 : 0); 3234 3235 if (retval != 0) 3236 break; 3237 3238 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3239 lun->be_lun->blocksize); 3240 3241 if (retval != 0) 3242 break; 3243 3244 retval = sbuf_printf(sb, "\t<serial_number>"); 3245 3246 if (retval != 0) 3247 break; 3248 3249 retval = ctl_sbuf_printf_esc(sb, 3250 lun->be_lun->serial_num, 3251 sizeof(lun->be_lun->serial_num)); 3252 3253 if (retval != 0) 3254 break; 3255 3256 retval = sbuf_printf(sb, "</serial_number>\n"); 3257 3258 if (retval != 0) 3259 break; 3260 3261 retval = sbuf_printf(sb, "\t<device_id>"); 3262 3263 if (retval != 0) 3264 break; 3265 3266 retval = ctl_sbuf_printf_esc(sb, 3267 lun->be_lun->device_id, 3268 sizeof(lun->be_lun->device_id)); 3269 3270 if (retval != 0) 3271 break; 3272 3273 retval = sbuf_printf(sb, "</device_id>\n"); 3274 3275 if (retval != 0) 3276 break; 3277 3278 if (lun->backend->lun_info != NULL) { 3279 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3280 if (retval != 0) 3281 break; 3282 } 3283 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3284 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3285 opt->name, opt->value, opt->name); 3286 if (retval != 0) 3287 break; 3288 } 3289 3290 retval = sbuf_printf(sb, "</lun>\n"); 3291 3292 if (retval != 0) 3293 break; 3294 mtx_unlock(&lun->lun_lock); 3295 } 3296 if (lun != NULL) 3297 mtx_unlock(&lun->lun_lock); 3298 mtx_unlock(&softc->ctl_lock); 3299 3300 if ((retval != 0) 3301 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3302 retval = 0; 3303 sbuf_delete(sb); 3304 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3305 snprintf(list->error_str, sizeof(list->error_str), 3306 "Out of space, %d bytes is too small", 3307 list->alloc_len); 3308 break; 3309 } 3310 3311 sbuf_finish(sb); 3312 3313 retval = copyout(sbuf_data(sb), list->lun_xml, 3314 sbuf_len(sb) + 1); 3315 3316 list->fill_len = sbuf_len(sb) + 1; 3317 list->status = CTL_LUN_LIST_OK; 3318 sbuf_delete(sb); 3319 break; 3320 } 3321 case CTL_ISCSI: { 3322 struct ctl_iscsi *ci; 3323 struct ctl_frontend *fe; 3324 3325 ci = (struct ctl_iscsi *)addr; 3326 3327 fe = ctl_frontend_find("iscsi"); 3328 if (fe == NULL) { 3329 ci->status = CTL_ISCSI_ERROR; 3330 snprintf(ci->error_str, sizeof(ci->error_str), 3331 "Frontend \"iscsi\" not found."); 3332 break; 3333 } 3334 3335 retval = fe->ioctl(dev, cmd, addr, flag, td); 3336 break; 3337 } 3338 case CTL_PORT_REQ: { 3339 struct ctl_req *req; 3340 struct ctl_frontend *fe; 3341 3342 req = (struct ctl_req *)addr; 3343 3344 fe = ctl_frontend_find(req->driver); 3345 if (fe == NULL) { 3346 req->status = CTL_LUN_ERROR; 3347 snprintf(req->error_str, sizeof(req->error_str), 3348 "Frontend \"%s\" not found.", req->driver); 3349 break; 3350 } 3351 if (req->num_args > 0) { 3352 req->kern_args = ctl_copyin_args(req->num_args, 3353 req->args, req->error_str, sizeof(req->error_str)); 3354 if (req->kern_args == NULL) { 3355 req->status = CTL_LUN_ERROR; 3356 break; 3357 } 3358 } 3359 3360 retval = fe->ioctl(dev, cmd, addr, flag, td); 3361 3362 if (req->num_args > 0) { 3363 ctl_copyout_args(req->num_args, req->kern_args); 3364 ctl_free_args(req->num_args, req->kern_args); 3365 } 3366 break; 3367 } 3368 case CTL_PORT_LIST: { 3369 struct sbuf *sb; 3370 struct ctl_port *port; 3371 struct ctl_lun_list *list; 3372 struct ctl_option *opt; 3373 int j; 3374 3375 list = (struct ctl_lun_list *)addr; 3376 3377 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3378 if (sb == NULL) { 3379 list->status = CTL_LUN_LIST_ERROR; 3380 snprintf(list->error_str, sizeof(list->error_str), 3381 "Unable to allocate %d bytes for LUN list", 3382 list->alloc_len); 3383 break; 3384 } 3385 3386 sbuf_printf(sb, "<ctlportlist>\n"); 3387 3388 mtx_lock(&softc->ctl_lock); 3389 STAILQ_FOREACH(port, &softc->port_list, links) { 3390 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3391 (uintmax_t)port->targ_port); 3392 3393 /* 3394 * Bail out as soon as we see that we've overfilled 3395 * the buffer. 3396 */ 3397 if (retval != 0) 3398 break; 3399 3400 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3401 "</frontend_type>\n", port->frontend->name); 3402 if (retval != 0) 3403 break; 3404 3405 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3406 port->port_type); 3407 if (retval != 0) 3408 break; 3409 3410 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3411 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3412 if (retval != 0) 3413 break; 3414 3415 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3416 port->port_name); 3417 if (retval != 0) 3418 break; 3419 3420 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3421 port->physical_port); 3422 if (retval != 0) 3423 break; 3424 3425 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3426 port->virtual_port); 3427 if (retval != 0) 3428 break; 3429 3430 if (port->target_devid != NULL) { 3431 sbuf_printf(sb, "\t<target>"); 3432 ctl_id_sbuf(port->target_devid, sb); 3433 sbuf_printf(sb, "</target>\n"); 3434 } 3435 3436 if (port->port_devid != NULL) { 3437 sbuf_printf(sb, "\t<port>"); 3438 ctl_id_sbuf(port->port_devid, sb); 3439 sbuf_printf(sb, "</port>\n"); 3440 } 3441 3442 if (port->port_info != NULL) { 3443 retval = port->port_info(port->onoff_arg, sb); 3444 if (retval != 0) 3445 break; 3446 } 3447 STAILQ_FOREACH(opt, &port->options, links) { 3448 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3449 opt->name, opt->value, opt->name); 3450 if (retval != 0) 3451 break; 3452 } 3453 3454 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3455 if (port->wwpn_iid[j].in_use == 0 || 3456 (port->wwpn_iid[j].wwpn == 0 && 3457 port->wwpn_iid[j].name == NULL)) 3458 continue; 3459 3460 if (port->wwpn_iid[j].name != NULL) 3461 retval = sbuf_printf(sb, 3462 "\t<initiator>%u %s</initiator>\n", 3463 j, port->wwpn_iid[j].name); 3464 else 3465 retval = sbuf_printf(sb, 3466 "\t<initiator>%u naa.%08jx</initiator>\n", 3467 j, port->wwpn_iid[j].wwpn); 3468 if (retval != 0) 3469 break; 3470 } 3471 if (retval != 0) 3472 break; 3473 3474 retval = sbuf_printf(sb, "</targ_port>\n"); 3475 if (retval != 0) 3476 break; 3477 } 3478 mtx_unlock(&softc->ctl_lock); 3479 3480 if ((retval != 0) 3481 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3482 retval = 0; 3483 sbuf_delete(sb); 3484 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3485 snprintf(list->error_str, sizeof(list->error_str), 3486 "Out of space, %d bytes is too small", 3487 list->alloc_len); 3488 break; 3489 } 3490 3491 sbuf_finish(sb); 3492 3493 retval = copyout(sbuf_data(sb), list->lun_xml, 3494 sbuf_len(sb) + 1); 3495 3496 list->fill_len = sbuf_len(sb) + 1; 3497 list->status = CTL_LUN_LIST_OK; 3498 sbuf_delete(sb); 3499 break; 3500 } 3501 default: { 3502 /* XXX KDM should we fix this? */ 3503#if 0 3504 struct ctl_backend_driver *backend; 3505 unsigned int type; 3506 int found; 3507 3508 found = 0; 3509 3510 /* 3511 * We encode the backend type as the ioctl type for backend 3512 * ioctls. So parse it out here, and then search for a 3513 * backend of this type. 3514 */ 3515 type = _IOC_TYPE(cmd); 3516 3517 STAILQ_FOREACH(backend, &softc->be_list, links) { 3518 if (backend->type == type) { 3519 found = 1; 3520 break; 3521 } 3522 } 3523 if (found == 0) { 3524 printf("ctl: unknown ioctl command %#lx or backend " 3525 "%d\n", cmd, type); 3526 retval = EINVAL; 3527 break; 3528 } 3529 retval = backend->ioctl(dev, cmd, addr, flag, td); 3530#endif 3531 retval = ENOTTY; 3532 break; 3533 } 3534 } 3535 return (retval); 3536} 3537 3538uint32_t 3539ctl_get_initindex(struct ctl_nexus *nexus) 3540{ 3541 if (nexus->targ_port < CTL_MAX_PORTS) 3542 return (nexus->initid.id + 3543 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3544 else 3545 return (nexus->initid.id + 3546 ((nexus->targ_port - CTL_MAX_PORTS) * 3547 CTL_MAX_INIT_PER_PORT)); 3548} 3549 3550uint32_t 3551ctl_get_resindex(struct ctl_nexus *nexus) 3552{ 3553 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3554} 3555 3556uint32_t 3557ctl_port_idx(int port_num) 3558{ 3559 if (port_num < CTL_MAX_PORTS) 3560 return(port_num); 3561 else 3562 return(port_num - CTL_MAX_PORTS); 3563} 3564 3565static uint32_t 3566ctl_map_lun(int port_num, uint32_t lun_id) 3567{ 3568 struct ctl_port *port; 3569 3570 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3571 if (port == NULL) 3572 return (UINT32_MAX); 3573 if (port->lun_map == NULL) 3574 return (lun_id); 3575 return (port->lun_map(port->targ_lun_arg, lun_id)); 3576} 3577 3578static uint32_t 3579ctl_map_lun_back(int port_num, uint32_t lun_id) 3580{ 3581 struct ctl_port *port; 3582 uint32_t i; 3583 3584 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3585 if (port->lun_map == NULL) 3586 return (lun_id); 3587 for (i = 0; i < CTL_MAX_LUNS; i++) { 3588 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3589 return (i); 3590 } 3591 return (UINT32_MAX); 3592} 3593 3594/* 3595 * Note: This only works for bitmask sizes that are at least 32 bits, and 3596 * that are a power of 2. 3597 */ 3598int 3599ctl_ffz(uint32_t *mask, uint32_t size) 3600{ 3601 uint32_t num_chunks, num_pieces; 3602 int i, j; 3603 3604 num_chunks = (size >> 5); 3605 if (num_chunks == 0) 3606 num_chunks++; 3607 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3608 3609 for (i = 0; i < num_chunks; i++) { 3610 for (j = 0; j < num_pieces; j++) { 3611 if ((mask[i] & (1 << j)) == 0) 3612 return ((i << 5) + j); 3613 } 3614 } 3615 3616 return (-1); 3617} 3618 3619int 3620ctl_set_mask(uint32_t *mask, uint32_t bit) 3621{ 3622 uint32_t chunk, piece; 3623 3624 chunk = bit >> 5; 3625 piece = bit % (sizeof(uint32_t) * 8); 3626 3627 if ((mask[chunk] & (1 << piece)) != 0) 3628 return (-1); 3629 else 3630 mask[chunk] |= (1 << piece); 3631 3632 return (0); 3633} 3634 3635int 3636ctl_clear_mask(uint32_t *mask, uint32_t bit) 3637{ 3638 uint32_t chunk, piece; 3639 3640 chunk = bit >> 5; 3641 piece = bit % (sizeof(uint32_t) * 8); 3642 3643 if ((mask[chunk] & (1 << piece)) == 0) 3644 return (-1); 3645 else 3646 mask[chunk] &= ~(1 << piece); 3647 3648 return (0); 3649} 3650 3651int 3652ctl_is_set(uint32_t *mask, uint32_t bit) 3653{ 3654 uint32_t chunk, piece; 3655 3656 chunk = bit >> 5; 3657 piece = bit % (sizeof(uint32_t) * 8); 3658 3659 if ((mask[chunk] & (1 << piece)) == 0) 3660 return (0); 3661 else 3662 return (1); 3663} 3664 3665#ifdef unused 3666/* 3667 * The bus, target and lun are optional, they can be filled in later. 3668 * can_wait is used to determine whether we can wait on the malloc or not. 3669 */ 3670union ctl_io* 3671ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3672 uint32_t targ_lun, int can_wait) 3673{ 3674 union ctl_io *io; 3675 3676 if (can_wait) 3677 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3678 else 3679 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3680 3681 if (io != NULL) { 3682 io->io_hdr.io_type = io_type; 3683 io->io_hdr.targ_port = targ_port; 3684 /* 3685 * XXX KDM this needs to change/go away. We need to move 3686 * to a preallocated pool of ctl_scsiio structures. 3687 */ 3688 io->io_hdr.nexus.targ_target.id = targ_target; 3689 io->io_hdr.nexus.targ_lun = targ_lun; 3690 } 3691 3692 return (io); 3693} 3694 3695void 3696ctl_kfree_io(union ctl_io *io) 3697{ 3698 free(io, M_CTL); 3699} 3700#endif /* unused */ 3701 3702/* 3703 * ctl_softc, pool_type, total_ctl_io are passed in. 3704 * npool is passed out. 3705 */ 3706int 3707ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3708 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3709{ 3710 uint32_t i; 3711 union ctl_io *cur_io, *next_io; 3712 struct ctl_io_pool *pool; 3713 int retval; 3714 3715 retval = 0; 3716 3717 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3718 M_NOWAIT | M_ZERO); 3719 if (pool == NULL) { 3720 retval = ENOMEM; 3721 goto bailout; 3722 } 3723 3724 pool->type = pool_type; 3725 pool->ctl_softc = ctl_softc; 3726 3727 mtx_lock(&ctl_softc->pool_lock); 3728 pool->id = ctl_softc->cur_pool_id++; 3729 mtx_unlock(&ctl_softc->pool_lock); 3730 3731 pool->flags = CTL_POOL_FLAG_NONE; 3732 pool->refcount = 1; /* Reference for validity. */ 3733 STAILQ_INIT(&pool->free_queue); 3734 3735 /* 3736 * XXX KDM other options here: 3737 * - allocate a page at a time 3738 * - allocate one big chunk of memory. 3739 * Page allocation might work well, but would take a little more 3740 * tracking. 3741 */ 3742 for (i = 0; i < total_ctl_io; i++) { 3743 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3744 M_NOWAIT); 3745 if (cur_io == NULL) { 3746 retval = ENOMEM; 3747 break; 3748 } 3749 cur_io->io_hdr.pool = pool; 3750 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3751 pool->total_ctl_io++; 3752 pool->free_ctl_io++; 3753 } 3754 3755 if (retval != 0) { 3756 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3757 cur_io != NULL; cur_io = next_io) { 3758 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3759 links); 3760 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3761 ctl_io_hdr, links); 3762 free(cur_io, M_CTLIO); 3763 } 3764 3765 free(pool, M_CTL); 3766 goto bailout; 3767 } 3768 mtx_lock(&ctl_softc->pool_lock); 3769 ctl_softc->num_pools++; 3770 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3771 /* 3772 * Increment our usage count if this is an external consumer, so we 3773 * can't get unloaded until the external consumer (most likely a 3774 * FETD) unloads and frees his pool. 3775 * 3776 * XXX KDM will this increment the caller's module use count, or 3777 * mine? 3778 */ 3779#if 0 3780 if ((pool_type != CTL_POOL_EMERGENCY) 3781 && (pool_type != CTL_POOL_INTERNAL) 3782 && (pool_type != CTL_POOL_4OTHERSC)) 3783 MOD_INC_USE_COUNT; 3784#endif 3785 3786 mtx_unlock(&ctl_softc->pool_lock); 3787 3788 *npool = pool; 3789 3790bailout: 3791 3792 return (retval); 3793} 3794 3795static int 3796ctl_pool_acquire(struct ctl_io_pool *pool) 3797{ 3798 3799 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3800 3801 if (pool->flags & CTL_POOL_FLAG_INVALID) 3802 return (EINVAL); 3803 3804 pool->refcount++; 3805 3806 return (0); 3807} 3808 3809static void 3810ctl_pool_release(struct ctl_io_pool *pool) 3811{ 3812 struct ctl_softc *ctl_softc = pool->ctl_softc; 3813 union ctl_io *io; 3814 3815 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3816 3817 if (--pool->refcount != 0) 3818 return; 3819 3820 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3821 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3822 links); 3823 free(io, M_CTLIO); 3824 } 3825 3826 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3827 ctl_softc->num_pools--; 3828 3829 /* 3830 * XXX KDM will this decrement the caller's usage count or mine? 3831 */ 3832#if 0 3833 if ((pool->type != CTL_POOL_EMERGENCY) 3834 && (pool->type != CTL_POOL_INTERNAL) 3835 && (pool->type != CTL_POOL_4OTHERSC)) 3836 MOD_DEC_USE_COUNT; 3837#endif 3838 3839 free(pool, M_CTL); 3840} 3841 3842void 3843ctl_pool_free(struct ctl_io_pool *pool) 3844{ 3845 struct ctl_softc *ctl_softc; 3846 3847 if (pool == NULL) 3848 return; 3849 3850 ctl_softc = pool->ctl_softc; 3851 mtx_lock(&ctl_softc->pool_lock); 3852 pool->flags |= CTL_POOL_FLAG_INVALID; 3853 ctl_pool_release(pool); 3854 mtx_unlock(&ctl_softc->pool_lock); 3855} 3856 3857/* 3858 * This routine does not block (except for spinlocks of course). 3859 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3860 * possible. 3861 */ 3862union ctl_io * 3863ctl_alloc_io(void *pool_ref) 3864{ 3865 union ctl_io *io; 3866 struct ctl_softc *ctl_softc; 3867 struct ctl_io_pool *pool, *npool; 3868 struct ctl_io_pool *emergency_pool; 3869 3870 pool = (struct ctl_io_pool *)pool_ref; 3871 3872 if (pool == NULL) { 3873 printf("%s: pool is NULL\n", __func__); 3874 return (NULL); 3875 } 3876 3877 emergency_pool = NULL; 3878 3879 ctl_softc = pool->ctl_softc; 3880 3881 mtx_lock(&ctl_softc->pool_lock); 3882 /* 3883 * First, try to get the io structure from the user's pool. 3884 */ 3885 if (ctl_pool_acquire(pool) == 0) { 3886 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3887 if (io != NULL) { 3888 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3889 pool->total_allocated++; 3890 pool->free_ctl_io--; 3891 mtx_unlock(&ctl_softc->pool_lock); 3892 return (io); 3893 } else 3894 ctl_pool_release(pool); 3895 } 3896 /* 3897 * If he doesn't have any io structures left, search for an 3898 * emergency pool and grab one from there. 3899 */ 3900 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3901 if (npool->type != CTL_POOL_EMERGENCY) 3902 continue; 3903 3904 if (ctl_pool_acquire(npool) != 0) 3905 continue; 3906 3907 emergency_pool = npool; 3908 3909 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3910 if (io != NULL) { 3911 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3912 npool->total_allocated++; 3913 npool->free_ctl_io--; 3914 mtx_unlock(&ctl_softc->pool_lock); 3915 return (io); 3916 } else 3917 ctl_pool_release(npool); 3918 } 3919 3920 /* Drop the spinlock before we malloc */ 3921 mtx_unlock(&ctl_softc->pool_lock); 3922 3923 /* 3924 * The emergency pool (if it exists) didn't have one, so try an 3925 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3926 */ 3927 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3928 if (io != NULL) { 3929 /* 3930 * If the emergency pool exists but is empty, add this 3931 * ctl_io to its list when it gets freed. 3932 */ 3933 if (emergency_pool != NULL) { 3934 mtx_lock(&ctl_softc->pool_lock); 3935 if (ctl_pool_acquire(emergency_pool) == 0) { 3936 io->io_hdr.pool = emergency_pool; 3937 emergency_pool->total_ctl_io++; 3938 /* 3939 * Need to bump this, otherwise 3940 * total_allocated and total_freed won't 3941 * match when we no longer have anything 3942 * outstanding. 3943 */ 3944 emergency_pool->total_allocated++; 3945 } 3946 mtx_unlock(&ctl_softc->pool_lock); 3947 } else 3948 io->io_hdr.pool = NULL; 3949 } 3950 3951 return (io); 3952} 3953 3954void 3955ctl_free_io(union ctl_io *io) 3956{ 3957 if (io == NULL) 3958 return; 3959 3960 /* 3961 * If this ctl_io has a pool, return it to that pool. 3962 */ 3963 if (io->io_hdr.pool != NULL) { 3964 struct ctl_io_pool *pool; 3965 3966 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3967 mtx_lock(&pool->ctl_softc->pool_lock); 3968 io->io_hdr.io_type = 0xff; 3969 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3970 pool->total_freed++; 3971 pool->free_ctl_io++; 3972 ctl_pool_release(pool); 3973 mtx_unlock(&pool->ctl_softc->pool_lock); 3974 } else { 3975 /* 3976 * Otherwise, just free it. We probably malloced it and 3977 * the emergency pool wasn't available. 3978 */ 3979 free(io, M_CTLIO); 3980 } 3981 3982} 3983 3984void 3985ctl_zero_io(union ctl_io *io) 3986{ 3987 void *pool_ref; 3988 3989 if (io == NULL) 3990 return; 3991 3992 /* 3993 * May need to preserve linked list pointers at some point too. 3994 */ 3995 pool_ref = io->io_hdr.pool; 3996 3997 memset(io, 0, sizeof(*io)); 3998 3999 io->io_hdr.pool = pool_ref; 4000} 4001 4002/* 4003 * This routine is currently used for internal copies of ctl_ios that need 4004 * to persist for some reason after we've already returned status to the 4005 * FETD. (Thus the flag set.) 4006 * 4007 * XXX XXX 4008 * Note that this makes a blind copy of all fields in the ctl_io, except 4009 * for the pool reference. This includes any memory that has been 4010 * allocated! That memory will no longer be valid after done has been 4011 * called, so this would be VERY DANGEROUS for command that actually does 4012 * any reads or writes. Right now (11/7/2005), this is only used for immediate 4013 * start and stop commands, which don't transfer any data, so this is not a 4014 * problem. If it is used for anything else, the caller would also need to 4015 * allocate data buffer space and this routine would need to be modified to 4016 * copy the data buffer(s) as well. 4017 */ 4018void 4019ctl_copy_io(union ctl_io *src, union ctl_io *dest) 4020{ 4021 void *pool_ref; 4022 4023 if ((src == NULL) 4024 || (dest == NULL)) 4025 return; 4026 4027 /* 4028 * May need to preserve linked list pointers at some point too. 4029 */ 4030 pool_ref = dest->io_hdr.pool; 4031 4032 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 4033 4034 dest->io_hdr.pool = pool_ref; 4035 /* 4036 * We need to know that this is an internal copy, and doesn't need 4037 * to get passed back to the FETD that allocated it. 4038 */ 4039 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 4040} 4041 4042static int 4043ctl_expand_number(const char *buf, uint64_t *num) 4044{ 4045 char *endptr; 4046 uint64_t number; 4047 unsigned shift; 4048 4049 number = strtoq(buf, &endptr, 0); 4050 4051 switch (tolower((unsigned char)*endptr)) { 4052 case 'e': 4053 shift = 60; 4054 break; 4055 case 'p': 4056 shift = 50; 4057 break; 4058 case 't': 4059 shift = 40; 4060 break; 4061 case 'g': 4062 shift = 30; 4063 break; 4064 case 'm': 4065 shift = 20; 4066 break; 4067 case 'k': 4068 shift = 10; 4069 break; 4070 case 'b': 4071 case '\0': /* No unit. */ 4072 *num = number; 4073 return (0); 4074 default: 4075 /* Unrecognized unit. */ 4076 return (-1); 4077 } 4078 4079 if ((number << shift) >> shift != number) { 4080 /* Overflow */ 4081 return (-1); 4082 } 4083 *num = number << shift; 4084 return (0); 4085} 4086 4087 4088/* 4089 * This routine could be used in the future to load default and/or saved 4090 * mode page parameters for a particuar lun. 4091 */ 4092static int 4093ctl_init_page_index(struct ctl_lun *lun) 4094{ 4095 int i; 4096 struct ctl_page_index *page_index; 4097 const char *value; 4098 uint64_t ival; 4099 4100 memcpy(&lun->mode_pages.index, page_index_template, 4101 sizeof(page_index_template)); 4102 4103 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4104 4105 page_index = &lun->mode_pages.index[i]; 4106 /* 4107 * If this is a disk-only mode page, there's no point in 4108 * setting it up. For some pages, we have to have some 4109 * basic information about the disk in order to calculate the 4110 * mode page data. 4111 */ 4112 if ((lun->be_lun->lun_type != T_DIRECT) 4113 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4114 continue; 4115 4116 switch (page_index->page_code & SMPH_PC_MASK) { 4117 case SMS_RW_ERROR_RECOVERY_PAGE: { 4118 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4119 panic("subpage is incorrect!"); 4120 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4121 &rw_er_page_default, 4122 sizeof(rw_er_page_default)); 4123 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4124 &rw_er_page_changeable, 4125 sizeof(rw_er_page_changeable)); 4126 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4127 &rw_er_page_default, 4128 sizeof(rw_er_page_default)); 4129 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4130 &rw_er_page_default, 4131 sizeof(rw_er_page_default)); 4132 page_index->page_data = 4133 (uint8_t *)lun->mode_pages.rw_er_page; 4134 break; 4135 } 4136 case SMS_FORMAT_DEVICE_PAGE: { 4137 struct scsi_format_page *format_page; 4138 4139 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4140 panic("subpage is incorrect!"); 4141 4142 /* 4143 * Sectors per track are set above. Bytes per 4144 * sector need to be set here on a per-LUN basis. 4145 */ 4146 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4147 &format_page_default, 4148 sizeof(format_page_default)); 4149 memcpy(&lun->mode_pages.format_page[ 4150 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4151 sizeof(format_page_changeable)); 4152 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4153 &format_page_default, 4154 sizeof(format_page_default)); 4155 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4156 &format_page_default, 4157 sizeof(format_page_default)); 4158 4159 format_page = &lun->mode_pages.format_page[ 4160 CTL_PAGE_CURRENT]; 4161 scsi_ulto2b(lun->be_lun->blocksize, 4162 format_page->bytes_per_sector); 4163 4164 format_page = &lun->mode_pages.format_page[ 4165 CTL_PAGE_DEFAULT]; 4166 scsi_ulto2b(lun->be_lun->blocksize, 4167 format_page->bytes_per_sector); 4168 4169 format_page = &lun->mode_pages.format_page[ 4170 CTL_PAGE_SAVED]; 4171 scsi_ulto2b(lun->be_lun->blocksize, 4172 format_page->bytes_per_sector); 4173 4174 page_index->page_data = 4175 (uint8_t *)lun->mode_pages.format_page; 4176 break; 4177 } 4178 case SMS_RIGID_DISK_PAGE: { 4179 struct scsi_rigid_disk_page *rigid_disk_page; 4180 uint32_t sectors_per_cylinder; 4181 uint64_t cylinders; 4182#ifndef __XSCALE__ 4183 int shift; 4184#endif /* !__XSCALE__ */ 4185 4186 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4187 panic("invalid subpage value %d", 4188 page_index->subpage); 4189 4190 /* 4191 * Rotation rate and sectors per track are set 4192 * above. We calculate the cylinders here based on 4193 * capacity. Due to the number of heads and 4194 * sectors per track we're using, smaller arrays 4195 * may turn out to have 0 cylinders. Linux and 4196 * FreeBSD don't pay attention to these mode pages 4197 * to figure out capacity, but Solaris does. It 4198 * seems to deal with 0 cylinders just fine, and 4199 * works out a fake geometry based on the capacity. 4200 */ 4201 memcpy(&lun->mode_pages.rigid_disk_page[ 4202 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4203 sizeof(rigid_disk_page_default)); 4204 memcpy(&lun->mode_pages.rigid_disk_page[ 4205 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4206 sizeof(rigid_disk_page_changeable)); 4207 4208 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4209 CTL_DEFAULT_HEADS; 4210 4211 /* 4212 * The divide method here will be more accurate, 4213 * probably, but results in floating point being 4214 * used in the kernel on i386 (__udivdi3()). On the 4215 * XScale, though, __udivdi3() is implemented in 4216 * software. 4217 * 4218 * The shift method for cylinder calculation is 4219 * accurate if sectors_per_cylinder is a power of 4220 * 2. Otherwise it might be slightly off -- you 4221 * might have a bit of a truncation problem. 4222 */ 4223#ifdef __XSCALE__ 4224 cylinders = (lun->be_lun->maxlba + 1) / 4225 sectors_per_cylinder; 4226#else 4227 for (shift = 31; shift > 0; shift--) { 4228 if (sectors_per_cylinder & (1 << shift)) 4229 break; 4230 } 4231 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4232#endif 4233 4234 /* 4235 * We've basically got 3 bytes, or 24 bits for the 4236 * cylinder size in the mode page. If we're over, 4237 * just round down to 2^24. 4238 */ 4239 if (cylinders > 0xffffff) 4240 cylinders = 0xffffff; 4241 4242 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4243 CTL_PAGE_DEFAULT]; 4244 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4245 4246 if ((value = ctl_get_opt(&lun->be_lun->options, 4247 "rpm")) != NULL) { 4248 scsi_ulto2b(strtol(value, NULL, 0), 4249 rigid_disk_page->rotation_rate); 4250 } 4251 4252 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4253 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4254 sizeof(rigid_disk_page_default)); 4255 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4256 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4257 sizeof(rigid_disk_page_default)); 4258 4259 page_index->page_data = 4260 (uint8_t *)lun->mode_pages.rigid_disk_page; 4261 break; 4262 } 4263 case SMS_CACHING_PAGE: { 4264 struct scsi_caching_page *caching_page; 4265 4266 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4267 panic("invalid subpage value %d", 4268 page_index->subpage); 4269 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4270 &caching_page_default, 4271 sizeof(caching_page_default)); 4272 memcpy(&lun->mode_pages.caching_page[ 4273 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4274 sizeof(caching_page_changeable)); 4275 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4276 &caching_page_default, 4277 sizeof(caching_page_default)); 4278 caching_page = &lun->mode_pages.caching_page[ 4279 CTL_PAGE_SAVED]; 4280 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4281 if (value != NULL && strcmp(value, "off") == 0) 4282 caching_page->flags1 &= ~SCP_WCE; 4283 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4284 if (value != NULL && strcmp(value, "off") == 0) 4285 caching_page->flags1 |= SCP_RCD; 4286 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4287 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4288 sizeof(caching_page_default)); 4289 page_index->page_data = 4290 (uint8_t *)lun->mode_pages.caching_page; 4291 break; 4292 } 4293 case SMS_CONTROL_MODE_PAGE: { 4294 struct scsi_control_page *control_page; 4295 4296 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4297 panic("invalid subpage value %d", 4298 page_index->subpage); 4299 4300 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4301 &control_page_default, 4302 sizeof(control_page_default)); 4303 memcpy(&lun->mode_pages.control_page[ 4304 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4305 sizeof(control_page_changeable)); 4306 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4307 &control_page_default, 4308 sizeof(control_page_default)); 4309 control_page = &lun->mode_pages.control_page[ 4310 CTL_PAGE_SAVED]; 4311 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4312 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4313 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4314 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4315 } 4316 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4317 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4318 sizeof(control_page_default)); 4319 page_index->page_data = 4320 (uint8_t *)lun->mode_pages.control_page; 4321 break; 4322 4323 } 4324 case SMS_INFO_EXCEPTIONS_PAGE: { 4325 switch (page_index->subpage) { 4326 case SMS_SUBPAGE_PAGE_0: 4327 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4328 &ie_page_default, 4329 sizeof(ie_page_default)); 4330 memcpy(&lun->mode_pages.ie_page[ 4331 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4332 sizeof(ie_page_changeable)); 4333 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4334 &ie_page_default, 4335 sizeof(ie_page_default)); 4336 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4337 &ie_page_default, 4338 sizeof(ie_page_default)); 4339 page_index->page_data = 4340 (uint8_t *)lun->mode_pages.ie_page; 4341 break; 4342 case 0x02: { 4343 struct ctl_logical_block_provisioning_page *page; 4344 4345 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4346 &lbp_page_default, 4347 sizeof(lbp_page_default)); 4348 memcpy(&lun->mode_pages.lbp_page[ 4349 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4350 sizeof(lbp_page_changeable)); 4351 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4352 &lbp_page_default, 4353 sizeof(lbp_page_default)); 4354 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4355 value = ctl_get_opt(&lun->be_lun->options, 4356 "avail-threshold"); 4357 if (value != NULL && 4358 ctl_expand_number(value, &ival) == 0) { 4359 page->descr[0].flags |= SLBPPD_ENABLED | 4360 SLBPPD_ARMING_DEC; 4361 if (lun->be_lun->blocksize) 4362 ival /= lun->be_lun->blocksize; 4363 else 4364 ival /= 512; 4365 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4366 page->descr[0].count); 4367 } 4368 value = ctl_get_opt(&lun->be_lun->options, 4369 "used-threshold"); 4370 if (value != NULL && 4371 ctl_expand_number(value, &ival) == 0) { 4372 page->descr[1].flags |= SLBPPD_ENABLED | 4373 SLBPPD_ARMING_INC; 4374 if (lun->be_lun->blocksize) 4375 ival /= lun->be_lun->blocksize; 4376 else 4377 ival /= 512; 4378 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4379 page->descr[1].count); 4380 } 4381 value = ctl_get_opt(&lun->be_lun->options, 4382 "pool-avail-threshold"); 4383 if (value != NULL && 4384 ctl_expand_number(value, &ival) == 0) { 4385 page->descr[2].flags |= SLBPPD_ENABLED | 4386 SLBPPD_ARMING_DEC; 4387 if (lun->be_lun->blocksize) 4388 ival /= lun->be_lun->blocksize; 4389 else 4390 ival /= 512; 4391 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4392 page->descr[2].count); 4393 } 4394 value = ctl_get_opt(&lun->be_lun->options, 4395 "pool-used-threshold"); 4396 if (value != NULL && 4397 ctl_expand_number(value, &ival) == 0) { 4398 page->descr[3].flags |= SLBPPD_ENABLED | 4399 SLBPPD_ARMING_INC; 4400 if (lun->be_lun->blocksize) 4401 ival /= lun->be_lun->blocksize; 4402 else 4403 ival /= 512; 4404 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4405 page->descr[3].count); 4406 } 4407 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4408 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4409 sizeof(lbp_page_default)); 4410 page_index->page_data = 4411 (uint8_t *)lun->mode_pages.lbp_page; 4412 }} 4413 break; 4414 } 4415 case SMS_VENDOR_SPECIFIC_PAGE:{ 4416 switch (page_index->subpage) { 4417 case DBGCNF_SUBPAGE_CODE: { 4418 struct copan_debugconf_subpage *current_page, 4419 *saved_page; 4420 4421 memcpy(&lun->mode_pages.debugconf_subpage[ 4422 CTL_PAGE_CURRENT], 4423 &debugconf_page_default, 4424 sizeof(debugconf_page_default)); 4425 memcpy(&lun->mode_pages.debugconf_subpage[ 4426 CTL_PAGE_CHANGEABLE], 4427 &debugconf_page_changeable, 4428 sizeof(debugconf_page_changeable)); 4429 memcpy(&lun->mode_pages.debugconf_subpage[ 4430 CTL_PAGE_DEFAULT], 4431 &debugconf_page_default, 4432 sizeof(debugconf_page_default)); 4433 memcpy(&lun->mode_pages.debugconf_subpage[ 4434 CTL_PAGE_SAVED], 4435 &debugconf_page_default, 4436 sizeof(debugconf_page_default)); 4437 page_index->page_data = 4438 (uint8_t *)lun->mode_pages.debugconf_subpage; 4439 4440 current_page = (struct copan_debugconf_subpage *) 4441 (page_index->page_data + 4442 (page_index->page_len * 4443 CTL_PAGE_CURRENT)); 4444 saved_page = (struct copan_debugconf_subpage *) 4445 (page_index->page_data + 4446 (page_index->page_len * 4447 CTL_PAGE_SAVED)); 4448 break; 4449 } 4450 default: 4451 panic("invalid subpage value %d", 4452 page_index->subpage); 4453 break; 4454 } 4455 break; 4456 } 4457 default: 4458 panic("invalid page value %d", 4459 page_index->page_code & SMPH_PC_MASK); 4460 break; 4461 } 4462 } 4463 4464 return (CTL_RETVAL_COMPLETE); 4465} 4466 4467static int 4468ctl_init_log_page_index(struct ctl_lun *lun) 4469{ 4470 struct ctl_page_index *page_index; 4471 int i, j, k, prev; 4472 4473 memcpy(&lun->log_pages.index, log_page_index_template, 4474 sizeof(log_page_index_template)); 4475 4476 prev = -1; 4477 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4478 4479 page_index = &lun->log_pages.index[i]; 4480 /* 4481 * If this is a disk-only mode page, there's no point in 4482 * setting it up. For some pages, we have to have some 4483 * basic information about the disk in order to calculate the 4484 * mode page data. 4485 */ 4486 if ((lun->be_lun->lun_type != T_DIRECT) 4487 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4488 continue; 4489 4490 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4491 ((lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 4492 lun->backend->lun_attr == NULL)) 4493 continue; 4494 4495 if (page_index->page_code != prev) { 4496 lun->log_pages.pages_page[j] = page_index->page_code; 4497 prev = page_index->page_code; 4498 j++; 4499 } 4500 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4501 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4502 k++; 4503 } 4504 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4505 lun->log_pages.index[0].page_len = j; 4506 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4507 lun->log_pages.index[1].page_len = k * 2; 4508 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4509 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4510 4511 return (CTL_RETVAL_COMPLETE); 4512} 4513 4514static int 4515hex2bin(const char *str, uint8_t *buf, int buf_size) 4516{ 4517 int i; 4518 u_char c; 4519 4520 memset(buf, 0, buf_size); 4521 while (isspace(str[0])) 4522 str++; 4523 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4524 str += 2; 4525 buf_size *= 2; 4526 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4527 c = str[i]; 4528 if (isdigit(c)) 4529 c -= '0'; 4530 else if (isalpha(c)) 4531 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4532 else 4533 break; 4534 if (c >= 16) 4535 break; 4536 if ((i & 1) == 0) 4537 buf[i / 2] |= (c << 4); 4538 else 4539 buf[i / 2] |= c; 4540 } 4541 return ((i + 1) / 2); 4542} 4543 4544/* 4545 * LUN allocation. 4546 * 4547 * Requirements: 4548 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4549 * wants us to allocate the LUN and he can block. 4550 * - ctl_softc is always set 4551 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4552 * 4553 * Returns 0 for success, non-zero (errno) for failure. 4554 */ 4555static int 4556ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4557 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4558{ 4559 struct ctl_lun *nlun, *lun; 4560 struct ctl_port *port; 4561 struct scsi_vpd_id_descriptor *desc; 4562 struct scsi_vpd_id_t10 *t10id; 4563 const char *eui, *naa, *scsiname, *vendor, *value; 4564 int lun_number, i, lun_malloced; 4565 int devidlen, idlen1, idlen2 = 0, len; 4566 4567 if (be_lun == NULL) 4568 return (EINVAL); 4569 4570 /* 4571 * We currently only support Direct Access or Processor LUN types. 4572 */ 4573 switch (be_lun->lun_type) { 4574 case T_DIRECT: 4575 break; 4576 case T_PROCESSOR: 4577 break; 4578 case T_SEQUENTIAL: 4579 case T_CHANGER: 4580 default: 4581 be_lun->lun_config_status(be_lun->be_lun, 4582 CTL_LUN_CONFIG_FAILURE); 4583 break; 4584 } 4585 if (ctl_lun == NULL) { 4586 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4587 lun_malloced = 1; 4588 } else { 4589 lun_malloced = 0; 4590 lun = ctl_lun; 4591 } 4592 4593 memset(lun, 0, sizeof(*lun)); 4594 if (lun_malloced) 4595 lun->flags = CTL_LUN_MALLOCED; 4596 4597 /* Generate LUN ID. */ 4598 devidlen = max(CTL_DEVID_MIN_LEN, 4599 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4600 idlen1 = sizeof(*t10id) + devidlen; 4601 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4602 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4603 if (scsiname != NULL) { 4604 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4605 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4606 } 4607 eui = ctl_get_opt(&be_lun->options, "eui"); 4608 if (eui != NULL) { 4609 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4610 } 4611 naa = ctl_get_opt(&be_lun->options, "naa"); 4612 if (naa != NULL) { 4613 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4614 } 4615 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4616 M_CTL, M_WAITOK | M_ZERO); 4617 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4618 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4619 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4620 desc->length = idlen1; 4621 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4622 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4623 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4624 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4625 } else { 4626 strncpy(t10id->vendor, vendor, 4627 min(sizeof(t10id->vendor), strlen(vendor))); 4628 } 4629 strncpy((char *)t10id->vendor_spec_id, 4630 (char *)be_lun->device_id, devidlen); 4631 if (scsiname != NULL) { 4632 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4633 desc->length); 4634 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4635 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4636 SVPD_ID_TYPE_SCSI_NAME; 4637 desc->length = idlen2; 4638 strlcpy(desc->identifier, scsiname, idlen2); 4639 } 4640 if (eui != NULL) { 4641 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4642 desc->length); 4643 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4644 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4645 SVPD_ID_TYPE_EUI64; 4646 desc->length = hex2bin(eui, desc->identifier, 16); 4647 desc->length = desc->length > 12 ? 16 : 4648 (desc->length > 8 ? 12 : 8); 4649 len -= 16 - desc->length; 4650 } 4651 if (naa != NULL) { 4652 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4653 desc->length); 4654 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4655 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4656 SVPD_ID_TYPE_NAA; 4657 desc->length = hex2bin(naa, desc->identifier, 16); 4658 desc->length = desc->length > 8 ? 16 : 8; 4659 len -= 16 - desc->length; 4660 } 4661 lun->lun_devid->len = len; 4662 4663 mtx_lock(&ctl_softc->ctl_lock); 4664 /* 4665 * See if the caller requested a particular LUN number. If so, see 4666 * if it is available. Otherwise, allocate the first available LUN. 4667 */ 4668 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4669 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4670 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4671 mtx_unlock(&ctl_softc->ctl_lock); 4672 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4673 printf("ctl: requested LUN ID %d is higher " 4674 "than CTL_MAX_LUNS - 1 (%d)\n", 4675 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4676 } else { 4677 /* 4678 * XXX KDM return an error, or just assign 4679 * another LUN ID in this case?? 4680 */ 4681 printf("ctl: requested LUN ID %d is already " 4682 "in use\n", be_lun->req_lun_id); 4683 } 4684 if (lun->flags & CTL_LUN_MALLOCED) 4685 free(lun, M_CTL); 4686 be_lun->lun_config_status(be_lun->be_lun, 4687 CTL_LUN_CONFIG_FAILURE); 4688 return (ENOSPC); 4689 } 4690 lun_number = be_lun->req_lun_id; 4691 } else { 4692 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4693 if (lun_number == -1) { 4694 mtx_unlock(&ctl_softc->ctl_lock); 4695 printf("ctl: can't allocate LUN on target %ju, out of " 4696 "LUNs\n", (uintmax_t)target_id.id); 4697 if (lun->flags & CTL_LUN_MALLOCED) 4698 free(lun, M_CTL); 4699 be_lun->lun_config_status(be_lun->be_lun, 4700 CTL_LUN_CONFIG_FAILURE); 4701 return (ENOSPC); 4702 } 4703 } 4704 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4705 4706 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4707 lun->target = target_id; 4708 lun->lun = lun_number; 4709 lun->be_lun = be_lun; 4710 /* 4711 * The processor LUN is always enabled. Disk LUNs come on line 4712 * disabled, and must be enabled by the backend. 4713 */ 4714 lun->flags |= CTL_LUN_DISABLED; 4715 lun->backend = be_lun->be; 4716 be_lun->ctl_lun = lun; 4717 be_lun->lun_id = lun_number; 4718 atomic_add_int(&be_lun->be->num_luns, 1); 4719 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4720 lun->flags |= CTL_LUN_OFFLINE; 4721 4722 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4723 lun->flags |= CTL_LUN_STOPPED; 4724 4725 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4726 lun->flags |= CTL_LUN_INOPERABLE; 4727 4728 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4729 lun->flags |= CTL_LUN_PRIMARY_SC; 4730 4731 value = ctl_get_opt(&be_lun->options, "readonly"); 4732 if (value != NULL && strcmp(value, "on") == 0) 4733 lun->flags |= CTL_LUN_READONLY; 4734 4735 lun->ctl_softc = ctl_softc; 4736 TAILQ_INIT(&lun->ooa_queue); 4737 TAILQ_INIT(&lun->blocked_queue); 4738 STAILQ_INIT(&lun->error_list); 4739 ctl_tpc_lun_init(lun); 4740 4741 /* 4742 * Initialize the mode and log page index. 4743 */ 4744 ctl_init_page_index(lun); 4745 ctl_init_log_page_index(lun); 4746 4747 /* 4748 * Set the poweron UA for all initiators on this LUN only. 4749 */ 4750 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4751 lun->pending_ua[i] = CTL_UA_POWERON; 4752 4753 /* 4754 * Now, before we insert this lun on the lun list, set the lun 4755 * inventory changed UA for all other luns. 4756 */ 4757 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4758 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4759 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4760 } 4761 } 4762 4763 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4764 4765 ctl_softc->ctl_luns[lun_number] = lun; 4766 4767 ctl_softc->num_luns++; 4768 4769 /* Setup statistics gathering */ 4770 lun->stats.device_type = be_lun->lun_type; 4771 lun->stats.lun_number = lun_number; 4772 if (lun->stats.device_type == T_DIRECT) 4773 lun->stats.blocksize = be_lun->blocksize; 4774 else 4775 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4776 for (i = 0;i < CTL_MAX_PORTS;i++) 4777 lun->stats.ports[i].targ_port = i; 4778 4779 mtx_unlock(&ctl_softc->ctl_lock); 4780 4781 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4782 4783 /* 4784 * Run through each registered FETD and bring it online if it isn't 4785 * already. Enable the target ID if it hasn't been enabled, and 4786 * enable this particular LUN. 4787 */ 4788 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4789 int retval; 4790 4791 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4792 if (retval != 0) { 4793 printf("ctl_alloc_lun: FETD %s port %d returned error " 4794 "%d for lun_enable on target %ju lun %d\n", 4795 port->port_name, port->targ_port, retval, 4796 (uintmax_t)target_id.id, lun_number); 4797 } else 4798 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4799 } 4800 return (0); 4801} 4802 4803/* 4804 * Delete a LUN. 4805 * Assumptions: 4806 * - LUN has already been marked invalid and any pending I/O has been taken 4807 * care of. 4808 */ 4809static int 4810ctl_free_lun(struct ctl_lun *lun) 4811{ 4812 struct ctl_softc *softc; 4813#if 0 4814 struct ctl_port *port; 4815#endif 4816 struct ctl_lun *nlun; 4817 int i; 4818 4819 softc = lun->ctl_softc; 4820 4821 mtx_assert(&softc->ctl_lock, MA_OWNED); 4822 4823 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4824 4825 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4826 4827 softc->ctl_luns[lun->lun] = NULL; 4828 4829 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4830 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4831 4832 softc->num_luns--; 4833 4834 /* 4835 * XXX KDM this scheme only works for a single target/multiple LUN 4836 * setup. It needs to be revamped for a multiple target scheme. 4837 * 4838 * XXX KDM this results in port->lun_disable() getting called twice, 4839 * once when ctl_disable_lun() is called, and a second time here. 4840 * We really need to re-think the LUN disable semantics. There 4841 * should probably be several steps/levels to LUN removal: 4842 * - disable 4843 * - invalidate 4844 * - free 4845 * 4846 * Right now we only have a disable method when communicating to 4847 * the front end ports, at least for individual LUNs. 4848 */ 4849#if 0 4850 STAILQ_FOREACH(port, &softc->port_list, links) { 4851 int retval; 4852 4853 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4854 lun->lun); 4855 if (retval != 0) { 4856 printf("ctl_free_lun: FETD %s port %d returned error " 4857 "%d for lun_disable on target %ju lun %jd\n", 4858 port->port_name, port->targ_port, retval, 4859 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4860 } 4861 4862 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4863 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4864 4865 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4866 if (retval != 0) { 4867 printf("ctl_free_lun: FETD %s port %d " 4868 "returned error %d for targ_disable on " 4869 "target %ju\n", port->port_name, 4870 port->targ_port, retval, 4871 (uintmax_t)lun->target.id); 4872 } else 4873 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4874 4875 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4876 continue; 4877 4878#if 0 4879 port->port_offline(port->onoff_arg); 4880 port->status &= ~CTL_PORT_STATUS_ONLINE; 4881#endif 4882 } 4883 } 4884#endif 4885 4886 /* 4887 * Tell the backend to free resources, if this LUN has a backend. 4888 */ 4889 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4890 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4891 4892 ctl_tpc_lun_shutdown(lun); 4893 mtx_destroy(&lun->lun_lock); 4894 free(lun->lun_devid, M_CTL); 4895 free(lun->write_buffer, M_CTL); 4896 if (lun->flags & CTL_LUN_MALLOCED) 4897 free(lun, M_CTL); 4898 4899 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4900 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4901 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4902 } 4903 } 4904 4905 return (0); 4906} 4907 4908static void 4909ctl_create_lun(struct ctl_be_lun *be_lun) 4910{ 4911 struct ctl_softc *ctl_softc; 4912 4913 ctl_softc = control_softc; 4914 4915 /* 4916 * ctl_alloc_lun() should handle all potential failure cases. 4917 */ 4918 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4919} 4920 4921int 4922ctl_add_lun(struct ctl_be_lun *be_lun) 4923{ 4924 struct ctl_softc *ctl_softc = control_softc; 4925 4926 mtx_lock(&ctl_softc->ctl_lock); 4927 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4928 mtx_unlock(&ctl_softc->ctl_lock); 4929 wakeup(&ctl_softc->pending_lun_queue); 4930 4931 return (0); 4932} 4933 4934int 4935ctl_enable_lun(struct ctl_be_lun *be_lun) 4936{ 4937 struct ctl_softc *ctl_softc; 4938 struct ctl_port *port, *nport; 4939 struct ctl_lun *lun; 4940 int retval; 4941 4942 ctl_softc = control_softc; 4943 4944 lun = (struct ctl_lun *)be_lun->ctl_lun; 4945 4946 mtx_lock(&ctl_softc->ctl_lock); 4947 mtx_lock(&lun->lun_lock); 4948 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4949 /* 4950 * eh? Why did we get called if the LUN is already 4951 * enabled? 4952 */ 4953 mtx_unlock(&lun->lun_lock); 4954 mtx_unlock(&ctl_softc->ctl_lock); 4955 return (0); 4956 } 4957 lun->flags &= ~CTL_LUN_DISABLED; 4958 mtx_unlock(&lun->lun_lock); 4959 4960 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4961 nport = STAILQ_NEXT(port, links); 4962 4963 /* 4964 * Drop the lock while we call the FETD's enable routine. 4965 * This can lead to a callback into CTL (at least in the 4966 * case of the internal initiator frontend. 4967 */ 4968 mtx_unlock(&ctl_softc->ctl_lock); 4969 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4970 mtx_lock(&ctl_softc->ctl_lock); 4971 if (retval != 0) { 4972 printf("%s: FETD %s port %d returned error " 4973 "%d for lun_enable on target %ju lun %jd\n", 4974 __func__, port->port_name, port->targ_port, retval, 4975 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4976 } 4977#if 0 4978 else { 4979 /* NOTE: TODO: why does lun enable affect port status? */ 4980 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4981 } 4982#endif 4983 } 4984 4985 mtx_unlock(&ctl_softc->ctl_lock); 4986 4987 return (0); 4988} 4989 4990int 4991ctl_disable_lun(struct ctl_be_lun *be_lun) 4992{ 4993 struct ctl_softc *ctl_softc; 4994 struct ctl_port *port; 4995 struct ctl_lun *lun; 4996 int retval; 4997 4998 ctl_softc = control_softc; 4999 5000 lun = (struct ctl_lun *)be_lun->ctl_lun; 5001 5002 mtx_lock(&ctl_softc->ctl_lock); 5003 mtx_lock(&lun->lun_lock); 5004 if (lun->flags & CTL_LUN_DISABLED) { 5005 mtx_unlock(&lun->lun_lock); 5006 mtx_unlock(&ctl_softc->ctl_lock); 5007 return (0); 5008 } 5009 lun->flags |= CTL_LUN_DISABLED; 5010 mtx_unlock(&lun->lun_lock); 5011 5012 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 5013 mtx_unlock(&ctl_softc->ctl_lock); 5014 /* 5015 * Drop the lock before we call the frontend's disable 5016 * routine, to avoid lock order reversals. 5017 * 5018 * XXX KDM what happens if the frontend list changes while 5019 * we're traversing it? It's unlikely, but should be handled. 5020 */ 5021 retval = port->lun_disable(port->targ_lun_arg, lun->target, 5022 lun->lun); 5023 mtx_lock(&ctl_softc->ctl_lock); 5024 if (retval != 0) { 5025 printf("ctl_alloc_lun: FETD %s port %d returned error " 5026 "%d for lun_disable on target %ju lun %jd\n", 5027 port->port_name, port->targ_port, retval, 5028 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 5029 } 5030 } 5031 5032 mtx_unlock(&ctl_softc->ctl_lock); 5033 5034 return (0); 5035} 5036 5037int 5038ctl_start_lun(struct ctl_be_lun *be_lun) 5039{ 5040 struct ctl_softc *ctl_softc; 5041 struct ctl_lun *lun; 5042 5043 ctl_softc = control_softc; 5044 5045 lun = (struct ctl_lun *)be_lun->ctl_lun; 5046 5047 mtx_lock(&lun->lun_lock); 5048 lun->flags &= ~CTL_LUN_STOPPED; 5049 mtx_unlock(&lun->lun_lock); 5050 5051 return (0); 5052} 5053 5054int 5055ctl_stop_lun(struct ctl_be_lun *be_lun) 5056{ 5057 struct ctl_softc *ctl_softc; 5058 struct ctl_lun *lun; 5059 5060 ctl_softc = control_softc; 5061 5062 lun = (struct ctl_lun *)be_lun->ctl_lun; 5063 5064 mtx_lock(&lun->lun_lock); 5065 lun->flags |= CTL_LUN_STOPPED; 5066 mtx_unlock(&lun->lun_lock); 5067 5068 return (0); 5069} 5070 5071int 5072ctl_lun_offline(struct ctl_be_lun *be_lun) 5073{ 5074 struct ctl_softc *ctl_softc; 5075 struct ctl_lun *lun; 5076 5077 ctl_softc = control_softc; 5078 5079 lun = (struct ctl_lun *)be_lun->ctl_lun; 5080 5081 mtx_lock(&lun->lun_lock); 5082 lun->flags |= CTL_LUN_OFFLINE; 5083 mtx_unlock(&lun->lun_lock); 5084 5085 return (0); 5086} 5087 5088int 5089ctl_lun_online(struct ctl_be_lun *be_lun) 5090{ 5091 struct ctl_softc *ctl_softc; 5092 struct ctl_lun *lun; 5093 5094 ctl_softc = control_softc; 5095 5096 lun = (struct ctl_lun *)be_lun->ctl_lun; 5097 5098 mtx_lock(&lun->lun_lock); 5099 lun->flags &= ~CTL_LUN_OFFLINE; 5100 mtx_unlock(&lun->lun_lock); 5101 5102 return (0); 5103} 5104 5105int 5106ctl_invalidate_lun(struct ctl_be_lun *be_lun) 5107{ 5108 struct ctl_softc *ctl_softc; 5109 struct ctl_lun *lun; 5110 5111 ctl_softc = control_softc; 5112 5113 lun = (struct ctl_lun *)be_lun->ctl_lun; 5114 5115 mtx_lock(&lun->lun_lock); 5116 5117 /* 5118 * The LUN needs to be disabled before it can be marked invalid. 5119 */ 5120 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 5121 mtx_unlock(&lun->lun_lock); 5122 return (-1); 5123 } 5124 /* 5125 * Mark the LUN invalid. 5126 */ 5127 lun->flags |= CTL_LUN_INVALID; 5128 5129 /* 5130 * If there is nothing in the OOA queue, go ahead and free the LUN. 5131 * If we have something in the OOA queue, we'll free it when the 5132 * last I/O completes. 5133 */ 5134 if (TAILQ_EMPTY(&lun->ooa_queue)) { 5135 mtx_unlock(&lun->lun_lock); 5136 mtx_lock(&ctl_softc->ctl_lock); 5137 ctl_free_lun(lun); 5138 mtx_unlock(&ctl_softc->ctl_lock); 5139 } else 5140 mtx_unlock(&lun->lun_lock); 5141 5142 return (0); 5143} 5144 5145int 5146ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5147{ 5148 struct ctl_softc *ctl_softc; 5149 struct ctl_lun *lun; 5150 5151 ctl_softc = control_softc; 5152 lun = (struct ctl_lun *)be_lun->ctl_lun; 5153 5154 mtx_lock(&lun->lun_lock); 5155 lun->flags |= CTL_LUN_INOPERABLE; 5156 mtx_unlock(&lun->lun_lock); 5157 5158 return (0); 5159} 5160 5161int 5162ctl_lun_operable(struct ctl_be_lun *be_lun) 5163{ 5164 struct ctl_softc *ctl_softc; 5165 struct ctl_lun *lun; 5166 5167 ctl_softc = control_softc; 5168 lun = (struct ctl_lun *)be_lun->ctl_lun; 5169 5170 mtx_lock(&lun->lun_lock); 5171 lun->flags &= ~CTL_LUN_INOPERABLE; 5172 mtx_unlock(&lun->lun_lock); 5173 5174 return (0); 5175} 5176 5177void 5178ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5179{ 5180 struct ctl_lun *lun; 5181 struct ctl_softc *softc; 5182 int i; 5183 5184 softc = control_softc; 5185 5186 lun = (struct ctl_lun *)be_lun->ctl_lun; 5187 5188 mtx_lock(&lun->lun_lock); 5189 5190 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5191 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5192 5193 mtx_unlock(&lun->lun_lock); 5194} 5195 5196/* 5197 * Backend "memory move is complete" callback for requests that never 5198 * make it down to say RAIDCore's configuration code. 5199 */ 5200int 5201ctl_config_move_done(union ctl_io *io) 5202{ 5203 int retval; 5204 5205 retval = CTL_RETVAL_COMPLETE; 5206 5207 5208 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5209 /* 5210 * XXX KDM this shouldn't happen, but what if it does? 5211 */ 5212 if (io->io_hdr.io_type != CTL_IO_SCSI) 5213 panic("I/O type isn't CTL_IO_SCSI!"); 5214 5215 if ((io->io_hdr.port_status == 0) 5216 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5217 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5218 io->io_hdr.status = CTL_SUCCESS; 5219 else if ((io->io_hdr.port_status != 0) 5220 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5221 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5222 /* 5223 * For hardware error sense keys, the sense key 5224 * specific value is defined to be a retry count, 5225 * but we use it to pass back an internal FETD 5226 * error code. XXX KDM Hopefully the FETD is only 5227 * using 16 bits for an error code, since that's 5228 * all the space we have in the sks field. 5229 */ 5230 ctl_set_internal_failure(&io->scsiio, 5231 /*sks_valid*/ 1, 5232 /*retry_count*/ 5233 io->io_hdr.port_status); 5234 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5235 free(io->scsiio.kern_data_ptr, M_CTL); 5236 ctl_done(io); 5237 goto bailout; 5238 } 5239 5240 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5241 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5242 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5243 /* 5244 * XXX KDM just assuming a single pointer here, and not a 5245 * S/G list. If we start using S/G lists for config data, 5246 * we'll need to know how to clean them up here as well. 5247 */ 5248 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5249 free(io->scsiio.kern_data_ptr, M_CTL); 5250 /* Hopefully the user has already set the status... */ 5251 ctl_done(io); 5252 } else { 5253 /* 5254 * XXX KDM now we need to continue data movement. Some 5255 * options: 5256 * - call ctl_scsiio() again? We don't do this for data 5257 * writes, because for those at least we know ahead of 5258 * time where the write will go and how long it is. For 5259 * config writes, though, that information is largely 5260 * contained within the write itself, thus we need to 5261 * parse out the data again. 5262 * 5263 * - Call some other function once the data is in? 5264 */ 5265 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5266 ctl_data_print(io); 5267 5268 /* 5269 * XXX KDM call ctl_scsiio() again for now, and check flag 5270 * bits to see whether we're allocated or not. 5271 */ 5272 retval = ctl_scsiio(&io->scsiio); 5273 } 5274bailout: 5275 return (retval); 5276} 5277 5278/* 5279 * This gets called by a backend driver when it is done with a 5280 * data_submit method. 5281 */ 5282void 5283ctl_data_submit_done(union ctl_io *io) 5284{ 5285 /* 5286 * If the IO_CONT flag is set, we need to call the supplied 5287 * function to continue processing the I/O, instead of completing 5288 * the I/O just yet. 5289 * 5290 * If there is an error, though, we don't want to keep processing. 5291 * Instead, just send status back to the initiator. 5292 */ 5293 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5294 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5295 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5296 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5297 io->scsiio.io_cont(io); 5298 return; 5299 } 5300 ctl_done(io); 5301} 5302 5303/* 5304 * This gets called by a backend driver when it is done with a 5305 * configuration write. 5306 */ 5307void 5308ctl_config_write_done(union ctl_io *io) 5309{ 5310 uint8_t *buf; 5311 5312 /* 5313 * If the IO_CONT flag is set, we need to call the supplied 5314 * function to continue processing the I/O, instead of completing 5315 * the I/O just yet. 5316 * 5317 * If there is an error, though, we don't want to keep processing. 5318 * Instead, just send status back to the initiator. 5319 */ 5320 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5321 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5322 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5323 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5324 io->scsiio.io_cont(io); 5325 return; 5326 } 5327 /* 5328 * Since a configuration write can be done for commands that actually 5329 * have data allocated, like write buffer, and commands that have 5330 * no data, like start/stop unit, we need to check here. 5331 */ 5332 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5333 buf = io->scsiio.kern_data_ptr; 5334 else 5335 buf = NULL; 5336 ctl_done(io); 5337 if (buf) 5338 free(buf, M_CTL); 5339} 5340 5341/* 5342 * SCSI release command. 5343 */ 5344int 5345ctl_scsi_release(struct ctl_scsiio *ctsio) 5346{ 5347 int length, longid, thirdparty_id, resv_id; 5348 struct ctl_softc *ctl_softc; 5349 struct ctl_lun *lun; 5350 uint32_t residx; 5351 5352 length = 0; 5353 resv_id = 0; 5354 5355 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5356 5357 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5358 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5359 ctl_softc = control_softc; 5360 5361 switch (ctsio->cdb[0]) { 5362 case RELEASE_10: { 5363 struct scsi_release_10 *cdb; 5364 5365 cdb = (struct scsi_release_10 *)ctsio->cdb; 5366 5367 if (cdb->byte2 & SR10_LONGID) 5368 longid = 1; 5369 else 5370 thirdparty_id = cdb->thirdparty_id; 5371 5372 resv_id = cdb->resv_id; 5373 length = scsi_2btoul(cdb->length); 5374 break; 5375 } 5376 } 5377 5378 5379 /* 5380 * XXX KDM right now, we only support LUN reservation. We don't 5381 * support 3rd party reservations, or extent reservations, which 5382 * might actually need the parameter list. If we've gotten this 5383 * far, we've got a LUN reservation. Anything else got kicked out 5384 * above. So, according to SPC, ignore the length. 5385 */ 5386 length = 0; 5387 5388 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5389 && (length > 0)) { 5390 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5391 ctsio->kern_data_len = length; 5392 ctsio->kern_total_len = length; 5393 ctsio->kern_data_resid = 0; 5394 ctsio->kern_rel_offset = 0; 5395 ctsio->kern_sg_entries = 0; 5396 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5397 ctsio->be_move_done = ctl_config_move_done; 5398 ctl_datamove((union ctl_io *)ctsio); 5399 5400 return (CTL_RETVAL_COMPLETE); 5401 } 5402 5403 if (length > 0) 5404 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5405 5406 mtx_lock(&lun->lun_lock); 5407 5408 /* 5409 * According to SPC, it is not an error for an intiator to attempt 5410 * to release a reservation on a LUN that isn't reserved, or that 5411 * is reserved by another initiator. The reservation can only be 5412 * released, though, by the initiator who made it or by one of 5413 * several reset type events. 5414 */ 5415 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5416 lun->flags &= ~CTL_LUN_RESERVED; 5417 5418 mtx_unlock(&lun->lun_lock); 5419 5420 ctsio->scsi_status = SCSI_STATUS_OK; 5421 ctsio->io_hdr.status = CTL_SUCCESS; 5422 5423 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5424 free(ctsio->kern_data_ptr, M_CTL); 5425 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5426 } 5427 5428 ctl_done((union ctl_io *)ctsio); 5429 return (CTL_RETVAL_COMPLETE); 5430} 5431 5432int 5433ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5434{ 5435 int extent, thirdparty, longid; 5436 int resv_id, length; 5437 uint64_t thirdparty_id; 5438 struct ctl_softc *ctl_softc; 5439 struct ctl_lun *lun; 5440 uint32_t residx; 5441 5442 extent = 0; 5443 thirdparty = 0; 5444 longid = 0; 5445 resv_id = 0; 5446 length = 0; 5447 thirdparty_id = 0; 5448 5449 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5450 5451 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5452 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5453 ctl_softc = control_softc; 5454 5455 switch (ctsio->cdb[0]) { 5456 case RESERVE_10: { 5457 struct scsi_reserve_10 *cdb; 5458 5459 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5460 5461 if (cdb->byte2 & SR10_LONGID) 5462 longid = 1; 5463 else 5464 thirdparty_id = cdb->thirdparty_id; 5465 5466 resv_id = cdb->resv_id; 5467 length = scsi_2btoul(cdb->length); 5468 break; 5469 } 5470 } 5471 5472 /* 5473 * XXX KDM right now, we only support LUN reservation. We don't 5474 * support 3rd party reservations, or extent reservations, which 5475 * might actually need the parameter list. If we've gotten this 5476 * far, we've got a LUN reservation. Anything else got kicked out 5477 * above. So, according to SPC, ignore the length. 5478 */ 5479 length = 0; 5480 5481 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5482 && (length > 0)) { 5483 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5484 ctsio->kern_data_len = length; 5485 ctsio->kern_total_len = length; 5486 ctsio->kern_data_resid = 0; 5487 ctsio->kern_rel_offset = 0; 5488 ctsio->kern_sg_entries = 0; 5489 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5490 ctsio->be_move_done = ctl_config_move_done; 5491 ctl_datamove((union ctl_io *)ctsio); 5492 5493 return (CTL_RETVAL_COMPLETE); 5494 } 5495 5496 if (length > 0) 5497 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5498 5499 mtx_lock(&lun->lun_lock); 5500 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5501 ctl_set_reservation_conflict(ctsio); 5502 goto bailout; 5503 } 5504 5505 lun->flags |= CTL_LUN_RESERVED; 5506 lun->res_idx = residx; 5507 5508 ctsio->scsi_status = SCSI_STATUS_OK; 5509 ctsio->io_hdr.status = CTL_SUCCESS; 5510 5511bailout: 5512 mtx_unlock(&lun->lun_lock); 5513 5514 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5515 free(ctsio->kern_data_ptr, M_CTL); 5516 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5517 } 5518 5519 ctl_done((union ctl_io *)ctsio); 5520 return (CTL_RETVAL_COMPLETE); 5521} 5522 5523int 5524ctl_start_stop(struct ctl_scsiio *ctsio) 5525{ 5526 struct scsi_start_stop_unit *cdb; 5527 struct ctl_lun *lun; 5528 struct ctl_softc *ctl_softc; 5529 int retval; 5530 5531 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5532 5533 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5534 ctl_softc = control_softc; 5535 retval = 0; 5536 5537 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5538 5539 /* 5540 * XXX KDM 5541 * We don't support the immediate bit on a stop unit. In order to 5542 * do that, we would need to code up a way to know that a stop is 5543 * pending, and hold off any new commands until it completes, one 5544 * way or another. Then we could accept or reject those commands 5545 * depending on its status. We would almost need to do the reverse 5546 * of what we do below for an immediate start -- return the copy of 5547 * the ctl_io to the FETD with status to send to the host (and to 5548 * free the copy!) and then free the original I/O once the stop 5549 * actually completes. That way, the OOA queue mechanism can work 5550 * to block commands that shouldn't proceed. Another alternative 5551 * would be to put the copy in the queue in place of the original, 5552 * and return the original back to the caller. That could be 5553 * slightly safer.. 5554 */ 5555 if ((cdb->byte2 & SSS_IMMED) 5556 && ((cdb->how & SSS_START) == 0)) { 5557 ctl_set_invalid_field(ctsio, 5558 /*sks_valid*/ 1, 5559 /*command*/ 1, 5560 /*field*/ 1, 5561 /*bit_valid*/ 1, 5562 /*bit*/ 0); 5563 ctl_done((union ctl_io *)ctsio); 5564 return (CTL_RETVAL_COMPLETE); 5565 } 5566 5567 if ((lun->flags & CTL_LUN_PR_RESERVED) 5568 && ((cdb->how & SSS_START)==0)) { 5569 uint32_t residx; 5570 5571 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5572 if (lun->pr_keys[residx] == 0 5573 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5574 5575 ctl_set_reservation_conflict(ctsio); 5576 ctl_done((union ctl_io *)ctsio); 5577 return (CTL_RETVAL_COMPLETE); 5578 } 5579 } 5580 5581 /* 5582 * If there is no backend on this device, we can't start or stop 5583 * it. In theory we shouldn't get any start/stop commands in the 5584 * first place at this level if the LUN doesn't have a backend. 5585 * That should get stopped by the command decode code. 5586 */ 5587 if (lun->backend == NULL) { 5588 ctl_set_invalid_opcode(ctsio); 5589 ctl_done((union ctl_io *)ctsio); 5590 return (CTL_RETVAL_COMPLETE); 5591 } 5592 5593 /* 5594 * XXX KDM Copan-specific offline behavior. 5595 * Figure out a reasonable way to port this? 5596 */ 5597#ifdef NEEDTOPORT 5598 mtx_lock(&lun->lun_lock); 5599 5600 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5601 && (lun->flags & CTL_LUN_OFFLINE)) { 5602 /* 5603 * If the LUN is offline, and the on/offline bit isn't set, 5604 * reject the start or stop. Otherwise, let it through. 5605 */ 5606 mtx_unlock(&lun->lun_lock); 5607 ctl_set_lun_not_ready(ctsio); 5608 ctl_done((union ctl_io *)ctsio); 5609 } else { 5610 mtx_unlock(&lun->lun_lock); 5611#endif /* NEEDTOPORT */ 5612 /* 5613 * This could be a start or a stop when we're online, 5614 * or a stop/offline or start/online. A start or stop when 5615 * we're offline is covered in the case above. 5616 */ 5617 /* 5618 * In the non-immediate case, we send the request to 5619 * the backend and return status to the user when 5620 * it is done. 5621 * 5622 * In the immediate case, we allocate a new ctl_io 5623 * to hold a copy of the request, and send that to 5624 * the backend. We then set good status on the 5625 * user's request and return it immediately. 5626 */ 5627 if (cdb->byte2 & SSS_IMMED) { 5628 union ctl_io *new_io; 5629 5630 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5631 if (new_io == NULL) { 5632 ctl_set_busy(ctsio); 5633 ctl_done((union ctl_io *)ctsio); 5634 } else { 5635 ctl_copy_io((union ctl_io *)ctsio, 5636 new_io); 5637 retval = lun->backend->config_write(new_io); 5638 ctl_set_success(ctsio); 5639 ctl_done((union ctl_io *)ctsio); 5640 } 5641 } else { 5642 retval = lun->backend->config_write( 5643 (union ctl_io *)ctsio); 5644 } 5645#ifdef NEEDTOPORT 5646 } 5647#endif 5648 return (retval); 5649} 5650 5651/* 5652 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5653 * we don't really do anything with the LBA and length fields if the user 5654 * passes them in. Instead we'll just flush out the cache for the entire 5655 * LUN. 5656 */ 5657int 5658ctl_sync_cache(struct ctl_scsiio *ctsio) 5659{ 5660 struct ctl_lun *lun; 5661 struct ctl_softc *ctl_softc; 5662 uint64_t starting_lba; 5663 uint32_t block_count; 5664 int retval; 5665 5666 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5667 5668 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5669 ctl_softc = control_softc; 5670 retval = 0; 5671 5672 switch (ctsio->cdb[0]) { 5673 case SYNCHRONIZE_CACHE: { 5674 struct scsi_sync_cache *cdb; 5675 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5676 5677 starting_lba = scsi_4btoul(cdb->begin_lba); 5678 block_count = scsi_2btoul(cdb->lb_count); 5679 break; 5680 } 5681 case SYNCHRONIZE_CACHE_16: { 5682 struct scsi_sync_cache_16 *cdb; 5683 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5684 5685 starting_lba = scsi_8btou64(cdb->begin_lba); 5686 block_count = scsi_4btoul(cdb->lb_count); 5687 break; 5688 } 5689 default: 5690 ctl_set_invalid_opcode(ctsio); 5691 ctl_done((union ctl_io *)ctsio); 5692 goto bailout; 5693 break; /* NOTREACHED */ 5694 } 5695 5696 /* 5697 * We check the LBA and length, but don't do anything with them. 5698 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5699 * get flushed. This check will just help satisfy anyone who wants 5700 * to see an error for an out of range LBA. 5701 */ 5702 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5703 ctl_set_lba_out_of_range(ctsio); 5704 ctl_done((union ctl_io *)ctsio); 5705 goto bailout; 5706 } 5707 5708 /* 5709 * If this LUN has no backend, we can't flush the cache anyway. 5710 */ 5711 if (lun->backend == NULL) { 5712 ctl_set_invalid_opcode(ctsio); 5713 ctl_done((union ctl_io *)ctsio); 5714 goto bailout; 5715 } 5716 5717 /* 5718 * Check to see whether we're configured to send the SYNCHRONIZE 5719 * CACHE command directly to the back end. 5720 */ 5721 mtx_lock(&lun->lun_lock); 5722 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5723 && (++(lun->sync_count) >= lun->sync_interval)) { 5724 lun->sync_count = 0; 5725 mtx_unlock(&lun->lun_lock); 5726 retval = lun->backend->config_write((union ctl_io *)ctsio); 5727 } else { 5728 mtx_unlock(&lun->lun_lock); 5729 ctl_set_success(ctsio); 5730 ctl_done((union ctl_io *)ctsio); 5731 } 5732 5733bailout: 5734 5735 return (retval); 5736} 5737 5738int 5739ctl_format(struct ctl_scsiio *ctsio) 5740{ 5741 struct scsi_format *cdb; 5742 struct ctl_lun *lun; 5743 struct ctl_softc *ctl_softc; 5744 int length, defect_list_len; 5745 5746 CTL_DEBUG_PRINT(("ctl_format\n")); 5747 5748 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5749 ctl_softc = control_softc; 5750 5751 cdb = (struct scsi_format *)ctsio->cdb; 5752 5753 length = 0; 5754 if (cdb->byte2 & SF_FMTDATA) { 5755 if (cdb->byte2 & SF_LONGLIST) 5756 length = sizeof(struct scsi_format_header_long); 5757 else 5758 length = sizeof(struct scsi_format_header_short); 5759 } 5760 5761 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5762 && (length > 0)) { 5763 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5764 ctsio->kern_data_len = length; 5765 ctsio->kern_total_len = length; 5766 ctsio->kern_data_resid = 0; 5767 ctsio->kern_rel_offset = 0; 5768 ctsio->kern_sg_entries = 0; 5769 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5770 ctsio->be_move_done = ctl_config_move_done; 5771 ctl_datamove((union ctl_io *)ctsio); 5772 5773 return (CTL_RETVAL_COMPLETE); 5774 } 5775 5776 defect_list_len = 0; 5777 5778 if (cdb->byte2 & SF_FMTDATA) { 5779 if (cdb->byte2 & SF_LONGLIST) { 5780 struct scsi_format_header_long *header; 5781 5782 header = (struct scsi_format_header_long *) 5783 ctsio->kern_data_ptr; 5784 5785 defect_list_len = scsi_4btoul(header->defect_list_len); 5786 if (defect_list_len != 0) { 5787 ctl_set_invalid_field(ctsio, 5788 /*sks_valid*/ 1, 5789 /*command*/ 0, 5790 /*field*/ 2, 5791 /*bit_valid*/ 0, 5792 /*bit*/ 0); 5793 goto bailout; 5794 } 5795 } else { 5796 struct scsi_format_header_short *header; 5797 5798 header = (struct scsi_format_header_short *) 5799 ctsio->kern_data_ptr; 5800 5801 defect_list_len = scsi_2btoul(header->defect_list_len); 5802 if (defect_list_len != 0) { 5803 ctl_set_invalid_field(ctsio, 5804 /*sks_valid*/ 1, 5805 /*command*/ 0, 5806 /*field*/ 2, 5807 /*bit_valid*/ 0, 5808 /*bit*/ 0); 5809 goto bailout; 5810 } 5811 } 5812 } 5813 5814 /* 5815 * The format command will clear out the "Medium format corrupted" 5816 * status if set by the configuration code. That status is really 5817 * just a way to notify the host that we have lost the media, and 5818 * get them to issue a command that will basically make them think 5819 * they're blowing away the media. 5820 */ 5821 mtx_lock(&lun->lun_lock); 5822 lun->flags &= ~CTL_LUN_INOPERABLE; 5823 mtx_unlock(&lun->lun_lock); 5824 5825 ctsio->scsi_status = SCSI_STATUS_OK; 5826 ctsio->io_hdr.status = CTL_SUCCESS; 5827bailout: 5828 5829 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5830 free(ctsio->kern_data_ptr, M_CTL); 5831 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5832 } 5833 5834 ctl_done((union ctl_io *)ctsio); 5835 return (CTL_RETVAL_COMPLETE); 5836} 5837 5838int 5839ctl_read_buffer(struct ctl_scsiio *ctsio) 5840{ 5841 struct scsi_read_buffer *cdb; 5842 struct ctl_lun *lun; 5843 int buffer_offset, len; 5844 static uint8_t descr[4]; 5845 static uint8_t echo_descr[4] = { 0 }; 5846 5847 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5848 5849 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5850 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5851 5852 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5853 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5854 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5855 ctl_set_invalid_field(ctsio, 5856 /*sks_valid*/ 1, 5857 /*command*/ 1, 5858 /*field*/ 1, 5859 /*bit_valid*/ 1, 5860 /*bit*/ 4); 5861 ctl_done((union ctl_io *)ctsio); 5862 return (CTL_RETVAL_COMPLETE); 5863 } 5864 5865 len = scsi_3btoul(cdb->length); 5866 buffer_offset = scsi_3btoul(cdb->offset); 5867 5868 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5869 ctl_set_invalid_field(ctsio, 5870 /*sks_valid*/ 1, 5871 /*command*/ 1, 5872 /*field*/ 6, 5873 /*bit_valid*/ 0, 5874 /*bit*/ 0); 5875 ctl_done((union ctl_io *)ctsio); 5876 return (CTL_RETVAL_COMPLETE); 5877 } 5878 5879 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5880 descr[0] = 0; 5881 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5882 ctsio->kern_data_ptr = descr; 5883 len = min(len, sizeof(descr)); 5884 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5885 ctsio->kern_data_ptr = echo_descr; 5886 len = min(len, sizeof(echo_descr)); 5887 } else { 5888 if (lun->write_buffer == NULL) { 5889 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5890 M_CTL, M_WAITOK); 5891 } 5892 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5893 } 5894 ctsio->kern_data_len = len; 5895 ctsio->kern_total_len = len; 5896 ctsio->kern_data_resid = 0; 5897 ctsio->kern_rel_offset = 0; 5898 ctsio->kern_sg_entries = 0; 5899 ctsio->be_move_done = ctl_config_move_done; 5900 ctl_datamove((union ctl_io *)ctsio); 5901 5902 return (CTL_RETVAL_COMPLETE); 5903} 5904 5905int 5906ctl_write_buffer(struct ctl_scsiio *ctsio) 5907{ 5908 struct scsi_write_buffer *cdb; 5909 struct ctl_lun *lun; 5910 int buffer_offset, len; 5911 5912 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5913 5914 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5915 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5916 5917 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5918 ctl_set_invalid_field(ctsio, 5919 /*sks_valid*/ 1, 5920 /*command*/ 1, 5921 /*field*/ 1, 5922 /*bit_valid*/ 1, 5923 /*bit*/ 4); 5924 ctl_done((union ctl_io *)ctsio); 5925 return (CTL_RETVAL_COMPLETE); 5926 } 5927 5928 len = scsi_3btoul(cdb->length); 5929 buffer_offset = scsi_3btoul(cdb->offset); 5930 5931 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5932 ctl_set_invalid_field(ctsio, 5933 /*sks_valid*/ 1, 5934 /*command*/ 1, 5935 /*field*/ 6, 5936 /*bit_valid*/ 0, 5937 /*bit*/ 0); 5938 ctl_done((union ctl_io *)ctsio); 5939 return (CTL_RETVAL_COMPLETE); 5940 } 5941 5942 /* 5943 * If we've got a kernel request that hasn't been malloced yet, 5944 * malloc it and tell the caller the data buffer is here. 5945 */ 5946 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5947 if (lun->write_buffer == NULL) { 5948 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5949 M_CTL, M_WAITOK); 5950 } 5951 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5952 ctsio->kern_data_len = len; 5953 ctsio->kern_total_len = len; 5954 ctsio->kern_data_resid = 0; 5955 ctsio->kern_rel_offset = 0; 5956 ctsio->kern_sg_entries = 0; 5957 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5958 ctsio->be_move_done = ctl_config_move_done; 5959 ctl_datamove((union ctl_io *)ctsio); 5960 5961 return (CTL_RETVAL_COMPLETE); 5962 } 5963 5964 ctl_done((union ctl_io *)ctsio); 5965 5966 return (CTL_RETVAL_COMPLETE); 5967} 5968 5969int 5970ctl_write_same(struct ctl_scsiio *ctsio) 5971{ 5972 struct ctl_lun *lun; 5973 struct ctl_lba_len_flags *lbalen; 5974 uint64_t lba; 5975 uint32_t num_blocks; 5976 int len, retval; 5977 uint8_t byte2; 5978 5979 retval = CTL_RETVAL_COMPLETE; 5980 5981 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5982 5983 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5984 5985 switch (ctsio->cdb[0]) { 5986 case WRITE_SAME_10: { 5987 struct scsi_write_same_10 *cdb; 5988 5989 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5990 5991 lba = scsi_4btoul(cdb->addr); 5992 num_blocks = scsi_2btoul(cdb->length); 5993 byte2 = cdb->byte2; 5994 break; 5995 } 5996 case WRITE_SAME_16: { 5997 struct scsi_write_same_16 *cdb; 5998 5999 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 6000 6001 lba = scsi_8btou64(cdb->addr); 6002 num_blocks = scsi_4btoul(cdb->length); 6003 byte2 = cdb->byte2; 6004 break; 6005 } 6006 default: 6007 /* 6008 * We got a command we don't support. This shouldn't 6009 * happen, commands should be filtered out above us. 6010 */ 6011 ctl_set_invalid_opcode(ctsio); 6012 ctl_done((union ctl_io *)ctsio); 6013 6014 return (CTL_RETVAL_COMPLETE); 6015 break; /* NOTREACHED */ 6016 } 6017 6018 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 6019 if ((byte2 & SWS_UNMAP) == 0 && 6020 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 6021 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 6022 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 6023 ctl_done((union ctl_io *)ctsio); 6024 return (CTL_RETVAL_COMPLETE); 6025 } 6026 6027 /* 6028 * The first check is to make sure we're in bounds, the second 6029 * check is to catch wrap-around problems. If the lba + num blocks 6030 * is less than the lba, then we've wrapped around and the block 6031 * range is invalid anyway. 6032 */ 6033 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6034 || ((lba + num_blocks) < lba)) { 6035 ctl_set_lba_out_of_range(ctsio); 6036 ctl_done((union ctl_io *)ctsio); 6037 return (CTL_RETVAL_COMPLETE); 6038 } 6039 6040 /* Zero number of blocks means "to the last logical block" */ 6041 if (num_blocks == 0) { 6042 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 6043 ctl_set_invalid_field(ctsio, 6044 /*sks_valid*/ 0, 6045 /*command*/ 1, 6046 /*field*/ 0, 6047 /*bit_valid*/ 0, 6048 /*bit*/ 0); 6049 ctl_done((union ctl_io *)ctsio); 6050 return (CTL_RETVAL_COMPLETE); 6051 } 6052 num_blocks = (lun->be_lun->maxlba + 1) - lba; 6053 } 6054 6055 len = lun->be_lun->blocksize; 6056 6057 /* 6058 * If we've got a kernel request that hasn't been malloced yet, 6059 * malloc it and tell the caller the data buffer is here. 6060 */ 6061 if ((byte2 & SWS_NDOB) == 0 && 6062 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6063 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6064 ctsio->kern_data_len = len; 6065 ctsio->kern_total_len = len; 6066 ctsio->kern_data_resid = 0; 6067 ctsio->kern_rel_offset = 0; 6068 ctsio->kern_sg_entries = 0; 6069 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6070 ctsio->be_move_done = ctl_config_move_done; 6071 ctl_datamove((union ctl_io *)ctsio); 6072 6073 return (CTL_RETVAL_COMPLETE); 6074 } 6075 6076 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6077 lbalen->lba = lba; 6078 lbalen->len = num_blocks; 6079 lbalen->flags = byte2; 6080 retval = lun->backend->config_write((union ctl_io *)ctsio); 6081 6082 return (retval); 6083} 6084 6085int 6086ctl_unmap(struct ctl_scsiio *ctsio) 6087{ 6088 struct ctl_lun *lun; 6089 struct scsi_unmap *cdb; 6090 struct ctl_ptr_len_flags *ptrlen; 6091 struct scsi_unmap_header *hdr; 6092 struct scsi_unmap_desc *buf, *end, *endnz, *range; 6093 uint64_t lba; 6094 uint32_t num_blocks; 6095 int len, retval; 6096 uint8_t byte2; 6097 6098 retval = CTL_RETVAL_COMPLETE; 6099 6100 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6101 6102 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6103 cdb = (struct scsi_unmap *)ctsio->cdb; 6104 6105 len = scsi_2btoul(cdb->length); 6106 byte2 = cdb->byte2; 6107 6108 /* 6109 * If we've got a kernel request that hasn't been malloced yet, 6110 * malloc it and tell the caller the data buffer is here. 6111 */ 6112 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6113 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6114 ctsio->kern_data_len = len; 6115 ctsio->kern_total_len = len; 6116 ctsio->kern_data_resid = 0; 6117 ctsio->kern_rel_offset = 0; 6118 ctsio->kern_sg_entries = 0; 6119 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6120 ctsio->be_move_done = ctl_config_move_done; 6121 ctl_datamove((union ctl_io *)ctsio); 6122 6123 return (CTL_RETVAL_COMPLETE); 6124 } 6125 6126 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6127 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6128 if (len < sizeof (*hdr) || 6129 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6130 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6131 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6132 ctl_set_invalid_field(ctsio, 6133 /*sks_valid*/ 0, 6134 /*command*/ 0, 6135 /*field*/ 0, 6136 /*bit_valid*/ 0, 6137 /*bit*/ 0); 6138 ctl_done((union ctl_io *)ctsio); 6139 return (CTL_RETVAL_COMPLETE); 6140 } 6141 len = scsi_2btoul(hdr->desc_length); 6142 buf = (struct scsi_unmap_desc *)(hdr + 1); 6143 end = buf + len / sizeof(*buf); 6144 6145 endnz = buf; 6146 for (range = buf; range < end; range++) { 6147 lba = scsi_8btou64(range->lba); 6148 num_blocks = scsi_4btoul(range->length); 6149 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6150 || ((lba + num_blocks) < lba)) { 6151 ctl_set_lba_out_of_range(ctsio); 6152 ctl_done((union ctl_io *)ctsio); 6153 return (CTL_RETVAL_COMPLETE); 6154 } 6155 if (num_blocks != 0) 6156 endnz = range + 1; 6157 } 6158 6159 /* 6160 * Block backend can not handle zero last range. 6161 * Filter it out and return if there is nothing left. 6162 */ 6163 len = (uint8_t *)endnz - (uint8_t *)buf; 6164 if (len == 0) { 6165 ctl_set_success(ctsio); 6166 ctl_done((union ctl_io *)ctsio); 6167 return (CTL_RETVAL_COMPLETE); 6168 } 6169 6170 mtx_lock(&lun->lun_lock); 6171 ptrlen = (struct ctl_ptr_len_flags *) 6172 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6173 ptrlen->ptr = (void *)buf; 6174 ptrlen->len = len; 6175 ptrlen->flags = byte2; 6176 ctl_check_blocked(lun); 6177 mtx_unlock(&lun->lun_lock); 6178 6179 retval = lun->backend->config_write((union ctl_io *)ctsio); 6180 return (retval); 6181} 6182 6183/* 6184 * Note that this function currently doesn't actually do anything inside 6185 * CTL to enforce things if the DQue bit is turned on. 6186 * 6187 * Also note that this function can't be used in the default case, because 6188 * the DQue bit isn't set in the changeable mask for the control mode page 6189 * anyway. This is just here as an example for how to implement a page 6190 * handler, and a placeholder in case we want to allow the user to turn 6191 * tagged queueing on and off. 6192 * 6193 * The D_SENSE bit handling is functional, however, and will turn 6194 * descriptor sense on and off for a given LUN. 6195 */ 6196int 6197ctl_control_page_handler(struct ctl_scsiio *ctsio, 6198 struct ctl_page_index *page_index, uint8_t *page_ptr) 6199{ 6200 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6201 struct ctl_lun *lun; 6202 struct ctl_softc *softc; 6203 int set_ua; 6204 uint32_t initidx; 6205 6206 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6207 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6208 set_ua = 0; 6209 6210 user_cp = (struct scsi_control_page *)page_ptr; 6211 current_cp = (struct scsi_control_page *) 6212 (page_index->page_data + (page_index->page_len * 6213 CTL_PAGE_CURRENT)); 6214 saved_cp = (struct scsi_control_page *) 6215 (page_index->page_data + (page_index->page_len * 6216 CTL_PAGE_SAVED)); 6217 6218 softc = control_softc; 6219 6220 mtx_lock(&lun->lun_lock); 6221 if (((current_cp->rlec & SCP_DSENSE) == 0) 6222 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6223 /* 6224 * Descriptor sense is currently turned off and the user 6225 * wants to turn it on. 6226 */ 6227 current_cp->rlec |= SCP_DSENSE; 6228 saved_cp->rlec |= SCP_DSENSE; 6229 lun->flags |= CTL_LUN_SENSE_DESC; 6230 set_ua = 1; 6231 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6232 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6233 /* 6234 * Descriptor sense is currently turned on, and the user 6235 * wants to turn it off. 6236 */ 6237 current_cp->rlec &= ~SCP_DSENSE; 6238 saved_cp->rlec &= ~SCP_DSENSE; 6239 lun->flags &= ~CTL_LUN_SENSE_DESC; 6240 set_ua = 1; 6241 } 6242 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6243 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6244 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6245 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6246 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6247 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6248 set_ua = 1; 6249 } 6250 if ((current_cp->eca_and_aen & SCP_SWP) != 6251 (user_cp->eca_and_aen & SCP_SWP)) { 6252 current_cp->eca_and_aen &= ~SCP_SWP; 6253 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6254 saved_cp->eca_and_aen &= ~SCP_SWP; 6255 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6256 set_ua = 1; 6257 } 6258 if (set_ua != 0) { 6259 int i; 6260 /* 6261 * Let other initiators know that the mode 6262 * parameters for this LUN have changed. 6263 */ 6264 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6265 if (i == initidx) 6266 continue; 6267 6268 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6269 } 6270 } 6271 mtx_unlock(&lun->lun_lock); 6272 6273 return (0); 6274} 6275 6276int 6277ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6278 struct ctl_page_index *page_index, uint8_t *page_ptr) 6279{ 6280 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6281 struct ctl_lun *lun; 6282 int set_ua; 6283 uint32_t initidx; 6284 6285 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6286 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6287 set_ua = 0; 6288 6289 user_cp = (struct scsi_caching_page *)page_ptr; 6290 current_cp = (struct scsi_caching_page *) 6291 (page_index->page_data + (page_index->page_len * 6292 CTL_PAGE_CURRENT)); 6293 saved_cp = (struct scsi_caching_page *) 6294 (page_index->page_data + (page_index->page_len * 6295 CTL_PAGE_SAVED)); 6296 6297 mtx_lock(&lun->lun_lock); 6298 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6299 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6300 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6301 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6302 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6303 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6304 set_ua = 1; 6305 } 6306 if (set_ua != 0) { 6307 int i; 6308 /* 6309 * Let other initiators know that the mode 6310 * parameters for this LUN have changed. 6311 */ 6312 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6313 if (i == initidx) 6314 continue; 6315 6316 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6317 } 6318 } 6319 mtx_unlock(&lun->lun_lock); 6320 6321 return (0); 6322} 6323 6324int 6325ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6326 struct ctl_page_index *page_index, 6327 uint8_t *page_ptr) 6328{ 6329 uint8_t *c; 6330 int i; 6331 6332 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6333 ctl_time_io_secs = 6334 (c[0] << 8) | 6335 (c[1] << 0) | 6336 0; 6337 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6338 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6339 printf("page data:"); 6340 for (i=0; i<8; i++) 6341 printf(" %.2x",page_ptr[i]); 6342 printf("\n"); 6343 return (0); 6344} 6345 6346int 6347ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6348 struct ctl_page_index *page_index, 6349 int pc) 6350{ 6351 struct copan_debugconf_subpage *page; 6352 6353 page = (struct copan_debugconf_subpage *)page_index->page_data + 6354 (page_index->page_len * pc); 6355 6356 switch (pc) { 6357 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6358 case SMS_PAGE_CTRL_DEFAULT >> 6: 6359 case SMS_PAGE_CTRL_SAVED >> 6: 6360 /* 6361 * We don't update the changable or default bits for this page. 6362 */ 6363 break; 6364 case SMS_PAGE_CTRL_CURRENT >> 6: 6365 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6366 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6367 break; 6368 default: 6369#ifdef NEEDTOPORT 6370 EPRINT(0, "Invalid PC %d!!", pc); 6371#endif /* NEEDTOPORT */ 6372 break; 6373 } 6374 return (0); 6375} 6376 6377 6378static int 6379ctl_do_mode_select(union ctl_io *io) 6380{ 6381 struct scsi_mode_page_header *page_header; 6382 struct ctl_page_index *page_index; 6383 struct ctl_scsiio *ctsio; 6384 int control_dev, page_len; 6385 int page_len_offset, page_len_size; 6386 union ctl_modepage_info *modepage_info; 6387 struct ctl_lun *lun; 6388 int *len_left, *len_used; 6389 int retval, i; 6390 6391 ctsio = &io->scsiio; 6392 page_index = NULL; 6393 page_len = 0; 6394 retval = CTL_RETVAL_COMPLETE; 6395 6396 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6397 6398 if (lun->be_lun->lun_type != T_DIRECT) 6399 control_dev = 1; 6400 else 6401 control_dev = 0; 6402 6403 modepage_info = (union ctl_modepage_info *) 6404 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6405 len_left = &modepage_info->header.len_left; 6406 len_used = &modepage_info->header.len_used; 6407 6408do_next_page: 6409 6410 page_header = (struct scsi_mode_page_header *) 6411 (ctsio->kern_data_ptr + *len_used); 6412 6413 if (*len_left == 0) { 6414 free(ctsio->kern_data_ptr, M_CTL); 6415 ctl_set_success(ctsio); 6416 ctl_done((union ctl_io *)ctsio); 6417 return (CTL_RETVAL_COMPLETE); 6418 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6419 6420 free(ctsio->kern_data_ptr, M_CTL); 6421 ctl_set_param_len_error(ctsio); 6422 ctl_done((union ctl_io *)ctsio); 6423 return (CTL_RETVAL_COMPLETE); 6424 6425 } else if ((page_header->page_code & SMPH_SPF) 6426 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6427 6428 free(ctsio->kern_data_ptr, M_CTL); 6429 ctl_set_param_len_error(ctsio); 6430 ctl_done((union ctl_io *)ctsio); 6431 return (CTL_RETVAL_COMPLETE); 6432 } 6433 6434 6435 /* 6436 * XXX KDM should we do something with the block descriptor? 6437 */ 6438 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6439 6440 if ((control_dev != 0) 6441 && (lun->mode_pages.index[i].page_flags & 6442 CTL_PAGE_FLAG_DISK_ONLY)) 6443 continue; 6444 6445 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6446 (page_header->page_code & SMPH_PC_MASK)) 6447 continue; 6448 6449 /* 6450 * If neither page has a subpage code, then we've got a 6451 * match. 6452 */ 6453 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6454 && ((page_header->page_code & SMPH_SPF) == 0)) { 6455 page_index = &lun->mode_pages.index[i]; 6456 page_len = page_header->page_length; 6457 break; 6458 } 6459 6460 /* 6461 * If both pages have subpages, then the subpage numbers 6462 * have to match. 6463 */ 6464 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6465 && (page_header->page_code & SMPH_SPF)) { 6466 struct scsi_mode_page_header_sp *sph; 6467 6468 sph = (struct scsi_mode_page_header_sp *)page_header; 6469 6470 if (lun->mode_pages.index[i].subpage == 6471 sph->subpage) { 6472 page_index = &lun->mode_pages.index[i]; 6473 page_len = scsi_2btoul(sph->page_length); 6474 break; 6475 } 6476 } 6477 } 6478 6479 /* 6480 * If we couldn't find the page, or if we don't have a mode select 6481 * handler for it, send back an error to the user. 6482 */ 6483 if ((page_index == NULL) 6484 || (page_index->select_handler == NULL)) { 6485 ctl_set_invalid_field(ctsio, 6486 /*sks_valid*/ 1, 6487 /*command*/ 0, 6488 /*field*/ *len_used, 6489 /*bit_valid*/ 0, 6490 /*bit*/ 0); 6491 free(ctsio->kern_data_ptr, M_CTL); 6492 ctl_done((union ctl_io *)ctsio); 6493 return (CTL_RETVAL_COMPLETE); 6494 } 6495 6496 if (page_index->page_code & SMPH_SPF) { 6497 page_len_offset = 2; 6498 page_len_size = 2; 6499 } else { 6500 page_len_size = 1; 6501 page_len_offset = 1; 6502 } 6503 6504 /* 6505 * If the length the initiator gives us isn't the one we specify in 6506 * the mode page header, or if they didn't specify enough data in 6507 * the CDB to avoid truncating this page, kick out the request. 6508 */ 6509 if ((page_len != (page_index->page_len - page_len_offset - 6510 page_len_size)) 6511 || (*len_left < page_index->page_len)) { 6512 6513 6514 ctl_set_invalid_field(ctsio, 6515 /*sks_valid*/ 1, 6516 /*command*/ 0, 6517 /*field*/ *len_used + page_len_offset, 6518 /*bit_valid*/ 0, 6519 /*bit*/ 0); 6520 free(ctsio->kern_data_ptr, M_CTL); 6521 ctl_done((union ctl_io *)ctsio); 6522 return (CTL_RETVAL_COMPLETE); 6523 } 6524 6525 /* 6526 * Run through the mode page, checking to make sure that the bits 6527 * the user changed are actually legal for him to change. 6528 */ 6529 for (i = 0; i < page_index->page_len; i++) { 6530 uint8_t *user_byte, *change_mask, *current_byte; 6531 int bad_bit; 6532 int j; 6533 6534 user_byte = (uint8_t *)page_header + i; 6535 change_mask = page_index->page_data + 6536 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6537 current_byte = page_index->page_data + 6538 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6539 6540 /* 6541 * Check to see whether the user set any bits in this byte 6542 * that he is not allowed to set. 6543 */ 6544 if ((*user_byte & ~(*change_mask)) == 6545 (*current_byte & ~(*change_mask))) 6546 continue; 6547 6548 /* 6549 * Go through bit by bit to determine which one is illegal. 6550 */ 6551 bad_bit = 0; 6552 for (j = 7; j >= 0; j--) { 6553 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6554 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6555 bad_bit = i; 6556 break; 6557 } 6558 } 6559 ctl_set_invalid_field(ctsio, 6560 /*sks_valid*/ 1, 6561 /*command*/ 0, 6562 /*field*/ *len_used + i, 6563 /*bit_valid*/ 1, 6564 /*bit*/ bad_bit); 6565 free(ctsio->kern_data_ptr, M_CTL); 6566 ctl_done((union ctl_io *)ctsio); 6567 return (CTL_RETVAL_COMPLETE); 6568 } 6569 6570 /* 6571 * Decrement these before we call the page handler, since we may 6572 * end up getting called back one way or another before the handler 6573 * returns to this context. 6574 */ 6575 *len_left -= page_index->page_len; 6576 *len_used += page_index->page_len; 6577 6578 retval = page_index->select_handler(ctsio, page_index, 6579 (uint8_t *)page_header); 6580 6581 /* 6582 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6583 * wait until this queued command completes to finish processing 6584 * the mode page. If it returns anything other than 6585 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6586 * already set the sense information, freed the data pointer, and 6587 * completed the io for us. 6588 */ 6589 if (retval != CTL_RETVAL_COMPLETE) 6590 goto bailout_no_done; 6591 6592 /* 6593 * If the initiator sent us more than one page, parse the next one. 6594 */ 6595 if (*len_left > 0) 6596 goto do_next_page; 6597 6598 ctl_set_success(ctsio); 6599 free(ctsio->kern_data_ptr, M_CTL); 6600 ctl_done((union ctl_io *)ctsio); 6601 6602bailout_no_done: 6603 6604 return (CTL_RETVAL_COMPLETE); 6605 6606} 6607 6608int 6609ctl_mode_select(struct ctl_scsiio *ctsio) 6610{ 6611 int param_len, pf, sp; 6612 int header_size, bd_len; 6613 int len_left, len_used; 6614 struct ctl_page_index *page_index; 6615 struct ctl_lun *lun; 6616 int control_dev, page_len; 6617 union ctl_modepage_info *modepage_info; 6618 int retval; 6619 6620 pf = 0; 6621 sp = 0; 6622 page_len = 0; 6623 len_used = 0; 6624 len_left = 0; 6625 retval = 0; 6626 bd_len = 0; 6627 page_index = NULL; 6628 6629 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6630 6631 if (lun->be_lun->lun_type != T_DIRECT) 6632 control_dev = 1; 6633 else 6634 control_dev = 0; 6635 6636 switch (ctsio->cdb[0]) { 6637 case MODE_SELECT_6: { 6638 struct scsi_mode_select_6 *cdb; 6639 6640 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6641 6642 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6643 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6644 6645 param_len = cdb->length; 6646 header_size = sizeof(struct scsi_mode_header_6); 6647 break; 6648 } 6649 case MODE_SELECT_10: { 6650 struct scsi_mode_select_10 *cdb; 6651 6652 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6653 6654 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6655 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6656 6657 param_len = scsi_2btoul(cdb->length); 6658 header_size = sizeof(struct scsi_mode_header_10); 6659 break; 6660 } 6661 default: 6662 ctl_set_invalid_opcode(ctsio); 6663 ctl_done((union ctl_io *)ctsio); 6664 return (CTL_RETVAL_COMPLETE); 6665 break; /* NOTREACHED */ 6666 } 6667 6668 /* 6669 * From SPC-3: 6670 * "A parameter list length of zero indicates that the Data-Out Buffer 6671 * shall be empty. This condition shall not be considered as an error." 6672 */ 6673 if (param_len == 0) { 6674 ctl_set_success(ctsio); 6675 ctl_done((union ctl_io *)ctsio); 6676 return (CTL_RETVAL_COMPLETE); 6677 } 6678 6679 /* 6680 * Since we'll hit this the first time through, prior to 6681 * allocation, we don't need to free a data buffer here. 6682 */ 6683 if (param_len < header_size) { 6684 ctl_set_param_len_error(ctsio); 6685 ctl_done((union ctl_io *)ctsio); 6686 return (CTL_RETVAL_COMPLETE); 6687 } 6688 6689 /* 6690 * Allocate the data buffer and grab the user's data. In theory, 6691 * we shouldn't have to sanity check the parameter list length here 6692 * because the maximum size is 64K. We should be able to malloc 6693 * that much without too many problems. 6694 */ 6695 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6696 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6697 ctsio->kern_data_len = param_len; 6698 ctsio->kern_total_len = param_len; 6699 ctsio->kern_data_resid = 0; 6700 ctsio->kern_rel_offset = 0; 6701 ctsio->kern_sg_entries = 0; 6702 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6703 ctsio->be_move_done = ctl_config_move_done; 6704 ctl_datamove((union ctl_io *)ctsio); 6705 6706 return (CTL_RETVAL_COMPLETE); 6707 } 6708 6709 switch (ctsio->cdb[0]) { 6710 case MODE_SELECT_6: { 6711 struct scsi_mode_header_6 *mh6; 6712 6713 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6714 bd_len = mh6->blk_desc_len; 6715 break; 6716 } 6717 case MODE_SELECT_10: { 6718 struct scsi_mode_header_10 *mh10; 6719 6720 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6721 bd_len = scsi_2btoul(mh10->blk_desc_len); 6722 break; 6723 } 6724 default: 6725 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6726 break; 6727 } 6728 6729 if (param_len < (header_size + bd_len)) { 6730 free(ctsio->kern_data_ptr, M_CTL); 6731 ctl_set_param_len_error(ctsio); 6732 ctl_done((union ctl_io *)ctsio); 6733 return (CTL_RETVAL_COMPLETE); 6734 } 6735 6736 /* 6737 * Set the IO_CONT flag, so that if this I/O gets passed to 6738 * ctl_config_write_done(), it'll get passed back to 6739 * ctl_do_mode_select() for further processing, or completion if 6740 * we're all done. 6741 */ 6742 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6743 ctsio->io_cont = ctl_do_mode_select; 6744 6745 modepage_info = (union ctl_modepage_info *) 6746 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6747 6748 memset(modepage_info, 0, sizeof(*modepage_info)); 6749 6750 len_left = param_len - header_size - bd_len; 6751 len_used = header_size + bd_len; 6752 6753 modepage_info->header.len_left = len_left; 6754 modepage_info->header.len_used = len_used; 6755 6756 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6757} 6758 6759int 6760ctl_mode_sense(struct ctl_scsiio *ctsio) 6761{ 6762 struct ctl_lun *lun; 6763 int pc, page_code, dbd, llba, subpage; 6764 int alloc_len, page_len, header_len, total_len; 6765 struct scsi_mode_block_descr *block_desc; 6766 struct ctl_page_index *page_index; 6767 int control_dev; 6768 6769 dbd = 0; 6770 llba = 0; 6771 block_desc = NULL; 6772 page_index = NULL; 6773 6774 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6775 6776 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6777 6778 if (lun->be_lun->lun_type != T_DIRECT) 6779 control_dev = 1; 6780 else 6781 control_dev = 0; 6782 6783 switch (ctsio->cdb[0]) { 6784 case MODE_SENSE_6: { 6785 struct scsi_mode_sense_6 *cdb; 6786 6787 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6788 6789 header_len = sizeof(struct scsi_mode_hdr_6); 6790 if (cdb->byte2 & SMS_DBD) 6791 dbd = 1; 6792 else 6793 header_len += sizeof(struct scsi_mode_block_descr); 6794 6795 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6796 page_code = cdb->page & SMS_PAGE_CODE; 6797 subpage = cdb->subpage; 6798 alloc_len = cdb->length; 6799 break; 6800 } 6801 case MODE_SENSE_10: { 6802 struct scsi_mode_sense_10 *cdb; 6803 6804 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6805 6806 header_len = sizeof(struct scsi_mode_hdr_10); 6807 6808 if (cdb->byte2 & SMS_DBD) 6809 dbd = 1; 6810 else 6811 header_len += sizeof(struct scsi_mode_block_descr); 6812 if (cdb->byte2 & SMS10_LLBAA) 6813 llba = 1; 6814 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6815 page_code = cdb->page & SMS_PAGE_CODE; 6816 subpage = cdb->subpage; 6817 alloc_len = scsi_2btoul(cdb->length); 6818 break; 6819 } 6820 default: 6821 ctl_set_invalid_opcode(ctsio); 6822 ctl_done((union ctl_io *)ctsio); 6823 return (CTL_RETVAL_COMPLETE); 6824 break; /* NOTREACHED */ 6825 } 6826 6827 /* 6828 * We have to make a first pass through to calculate the size of 6829 * the pages that match the user's query. Then we allocate enough 6830 * memory to hold it, and actually copy the data into the buffer. 6831 */ 6832 switch (page_code) { 6833 case SMS_ALL_PAGES_PAGE: { 6834 int i; 6835 6836 page_len = 0; 6837 6838 /* 6839 * At the moment, values other than 0 and 0xff here are 6840 * reserved according to SPC-3. 6841 */ 6842 if ((subpage != SMS_SUBPAGE_PAGE_0) 6843 && (subpage != SMS_SUBPAGE_ALL)) { 6844 ctl_set_invalid_field(ctsio, 6845 /*sks_valid*/ 1, 6846 /*command*/ 1, 6847 /*field*/ 3, 6848 /*bit_valid*/ 0, 6849 /*bit*/ 0); 6850 ctl_done((union ctl_io *)ctsio); 6851 return (CTL_RETVAL_COMPLETE); 6852 } 6853 6854 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6855 if ((control_dev != 0) 6856 && (lun->mode_pages.index[i].page_flags & 6857 CTL_PAGE_FLAG_DISK_ONLY)) 6858 continue; 6859 6860 /* 6861 * We don't use this subpage if the user didn't 6862 * request all subpages. 6863 */ 6864 if ((lun->mode_pages.index[i].subpage != 0) 6865 && (subpage == SMS_SUBPAGE_PAGE_0)) 6866 continue; 6867 6868#if 0 6869 printf("found page %#x len %d\n", 6870 lun->mode_pages.index[i].page_code & 6871 SMPH_PC_MASK, 6872 lun->mode_pages.index[i].page_len); 6873#endif 6874 page_len += lun->mode_pages.index[i].page_len; 6875 } 6876 break; 6877 } 6878 default: { 6879 int i; 6880 6881 page_len = 0; 6882 6883 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6884 /* Look for the right page code */ 6885 if ((lun->mode_pages.index[i].page_code & 6886 SMPH_PC_MASK) != page_code) 6887 continue; 6888 6889 /* Look for the right subpage or the subpage wildcard*/ 6890 if ((lun->mode_pages.index[i].subpage != subpage) 6891 && (subpage != SMS_SUBPAGE_ALL)) 6892 continue; 6893 6894 /* Make sure the page is supported for this dev type */ 6895 if ((control_dev != 0) 6896 && (lun->mode_pages.index[i].page_flags & 6897 CTL_PAGE_FLAG_DISK_ONLY)) 6898 continue; 6899 6900#if 0 6901 printf("found page %#x len %d\n", 6902 lun->mode_pages.index[i].page_code & 6903 SMPH_PC_MASK, 6904 lun->mode_pages.index[i].page_len); 6905#endif 6906 6907 page_len += lun->mode_pages.index[i].page_len; 6908 } 6909 6910 if (page_len == 0) { 6911 ctl_set_invalid_field(ctsio, 6912 /*sks_valid*/ 1, 6913 /*command*/ 1, 6914 /*field*/ 2, 6915 /*bit_valid*/ 1, 6916 /*bit*/ 5); 6917 ctl_done((union ctl_io *)ctsio); 6918 return (CTL_RETVAL_COMPLETE); 6919 } 6920 break; 6921 } 6922 } 6923 6924 total_len = header_len + page_len; 6925#if 0 6926 printf("header_len = %d, page_len = %d, total_len = %d\n", 6927 header_len, page_len, total_len); 6928#endif 6929 6930 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6931 ctsio->kern_sg_entries = 0; 6932 ctsio->kern_data_resid = 0; 6933 ctsio->kern_rel_offset = 0; 6934 if (total_len < alloc_len) { 6935 ctsio->residual = alloc_len - total_len; 6936 ctsio->kern_data_len = total_len; 6937 ctsio->kern_total_len = total_len; 6938 } else { 6939 ctsio->residual = 0; 6940 ctsio->kern_data_len = alloc_len; 6941 ctsio->kern_total_len = alloc_len; 6942 } 6943 6944 switch (ctsio->cdb[0]) { 6945 case MODE_SENSE_6: { 6946 struct scsi_mode_hdr_6 *header; 6947 6948 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6949 6950 header->datalen = ctl_min(total_len - 1, 254); 6951 if (control_dev == 0) { 6952 header->dev_specific = 0x10; /* DPOFUA */ 6953 if ((lun->flags & CTL_LUN_READONLY) || 6954 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6955 .eca_and_aen & SCP_SWP) != 0) 6956 header->dev_specific |= 0x80; /* WP */ 6957 } 6958 if (dbd) 6959 header->block_descr_len = 0; 6960 else 6961 header->block_descr_len = 6962 sizeof(struct scsi_mode_block_descr); 6963 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6964 break; 6965 } 6966 case MODE_SENSE_10: { 6967 struct scsi_mode_hdr_10 *header; 6968 int datalen; 6969 6970 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6971 6972 datalen = ctl_min(total_len - 2, 65533); 6973 scsi_ulto2b(datalen, header->datalen); 6974 if (control_dev == 0) { 6975 header->dev_specific = 0x10; /* DPOFUA */ 6976 if ((lun->flags & CTL_LUN_READONLY) || 6977 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6978 .eca_and_aen & SCP_SWP) != 0) 6979 header->dev_specific |= 0x80; /* WP */ 6980 } 6981 if (dbd) 6982 scsi_ulto2b(0, header->block_descr_len); 6983 else 6984 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6985 header->block_descr_len); 6986 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6987 break; 6988 } 6989 default: 6990 panic("invalid CDB type %#x", ctsio->cdb[0]); 6991 break; /* NOTREACHED */ 6992 } 6993 6994 /* 6995 * If we've got a disk, use its blocksize in the block 6996 * descriptor. Otherwise, just set it to 0. 6997 */ 6998 if (dbd == 0) { 6999 if (control_dev == 0) 7000 scsi_ulto3b(lun->be_lun->blocksize, 7001 block_desc->block_len); 7002 else 7003 scsi_ulto3b(0, block_desc->block_len); 7004 } 7005 7006 switch (page_code) { 7007 case SMS_ALL_PAGES_PAGE: { 7008 int i, data_used; 7009 7010 data_used = header_len; 7011 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7012 struct ctl_page_index *page_index; 7013 7014 page_index = &lun->mode_pages.index[i]; 7015 7016 if ((control_dev != 0) 7017 && (page_index->page_flags & 7018 CTL_PAGE_FLAG_DISK_ONLY)) 7019 continue; 7020 7021 /* 7022 * We don't use this subpage if the user didn't 7023 * request all subpages. We already checked (above) 7024 * to make sure the user only specified a subpage 7025 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7026 */ 7027 if ((page_index->subpage != 0) 7028 && (subpage == SMS_SUBPAGE_PAGE_0)) 7029 continue; 7030 7031 /* 7032 * Call the handler, if it exists, to update the 7033 * page to the latest values. 7034 */ 7035 if (page_index->sense_handler != NULL) 7036 page_index->sense_handler(ctsio, page_index,pc); 7037 7038 memcpy(ctsio->kern_data_ptr + data_used, 7039 page_index->page_data + 7040 (page_index->page_len * pc), 7041 page_index->page_len); 7042 data_used += page_index->page_len; 7043 } 7044 break; 7045 } 7046 default: { 7047 int i, data_used; 7048 7049 data_used = header_len; 7050 7051 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7052 struct ctl_page_index *page_index; 7053 7054 page_index = &lun->mode_pages.index[i]; 7055 7056 /* Look for the right page code */ 7057 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7058 continue; 7059 7060 /* Look for the right subpage or the subpage wildcard*/ 7061 if ((page_index->subpage != subpage) 7062 && (subpage != SMS_SUBPAGE_ALL)) 7063 continue; 7064 7065 /* Make sure the page is supported for this dev type */ 7066 if ((control_dev != 0) 7067 && (page_index->page_flags & 7068 CTL_PAGE_FLAG_DISK_ONLY)) 7069 continue; 7070 7071 /* 7072 * Call the handler, if it exists, to update the 7073 * page to the latest values. 7074 */ 7075 if (page_index->sense_handler != NULL) 7076 page_index->sense_handler(ctsio, page_index,pc); 7077 7078 memcpy(ctsio->kern_data_ptr + data_used, 7079 page_index->page_data + 7080 (page_index->page_len * pc), 7081 page_index->page_len); 7082 data_used += page_index->page_len; 7083 } 7084 break; 7085 } 7086 } 7087 7088 ctsio->scsi_status = SCSI_STATUS_OK; 7089 7090 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7091 ctsio->be_move_done = ctl_config_move_done; 7092 ctl_datamove((union ctl_io *)ctsio); 7093 7094 return (CTL_RETVAL_COMPLETE); 7095} 7096 7097int 7098ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 7099 struct ctl_page_index *page_index, 7100 int pc) 7101{ 7102 struct ctl_lun *lun; 7103 struct scsi_log_param_header *phdr; 7104 uint8_t *data; 7105 uint64_t val; 7106 7107 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7108 data = page_index->page_data; 7109 7110 if (lun->backend->lun_attr != NULL && 7111 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 7112 != UINT64_MAX) { 7113 phdr = (struct scsi_log_param_header *)data; 7114 scsi_ulto2b(0x0001, phdr->param_code); 7115 phdr->param_control = SLP_LBIN | SLP_LP; 7116 phdr->param_len = 8; 7117 data = (uint8_t *)(phdr + 1); 7118 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 7119 data[4] = 0x01; /* per-LUN */ 7120 data += phdr->param_len; 7121 } 7122 7123 if (lun->backend->lun_attr != NULL && 7124 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 7125 != UINT64_MAX) { 7126 phdr = (struct scsi_log_param_header *)data; 7127 scsi_ulto2b(0x0002, phdr->param_code); 7128 phdr->param_control = SLP_LBIN | SLP_LP; 7129 phdr->param_len = 8; 7130 data = (uint8_t *)(phdr + 1); 7131 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 7132 data[4] = 0x02; /* per-pool */ 7133 data += phdr->param_len; 7134 } 7135 7136 if (lun->backend->lun_attr != NULL && 7137 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 7138 != UINT64_MAX) { 7139 phdr = (struct scsi_log_param_header *)data; 7140 scsi_ulto2b(0x00f1, phdr->param_code); 7141 phdr->param_control = SLP_LBIN | SLP_LP; 7142 phdr->param_len = 8; 7143 data = (uint8_t *)(phdr + 1); 7144 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 7145 data[4] = 0x02; /* per-pool */ 7146 data += phdr->param_len; 7147 } 7148 7149 if (lun->backend->lun_attr != NULL && 7150 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 7151 != UINT64_MAX) { 7152 phdr = (struct scsi_log_param_header *)data; 7153 scsi_ulto2b(0x00f2, phdr->param_code); 7154 phdr->param_control = SLP_LBIN | SLP_LP; 7155 phdr->param_len = 8; 7156 data = (uint8_t *)(phdr + 1); 7157 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 7158 data[4] = 0x02; /* per-pool */ 7159 data += phdr->param_len; 7160 } 7161 7162 page_index->page_len = data - page_index->page_data; 7163 return (0); 7164} 7165 7166int 7167ctl_log_sense(struct ctl_scsiio *ctsio) 7168{ 7169 struct ctl_lun *lun; 7170 int i, pc, page_code, subpage; 7171 int alloc_len, total_len; 7172 struct ctl_page_index *page_index; 7173 struct scsi_log_sense *cdb; 7174 struct scsi_log_header *header; 7175 7176 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 7177 7178 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7179 cdb = (struct scsi_log_sense *)ctsio->cdb; 7180 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 7181 page_code = cdb->page & SLS_PAGE_CODE; 7182 subpage = cdb->subpage; 7183 alloc_len = scsi_2btoul(cdb->length); 7184 7185 page_index = NULL; 7186 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 7187 page_index = &lun->log_pages.index[i]; 7188 7189 /* Look for the right page code */ 7190 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 7191 continue; 7192 7193 /* Look for the right subpage or the subpage wildcard*/ 7194 if (page_index->subpage != subpage) 7195 continue; 7196 7197 break; 7198 } 7199 if (i >= CTL_NUM_LOG_PAGES) { 7200 ctl_set_invalid_field(ctsio, 7201 /*sks_valid*/ 1, 7202 /*command*/ 1, 7203 /*field*/ 2, 7204 /*bit_valid*/ 0, 7205 /*bit*/ 0); 7206 ctl_done((union ctl_io *)ctsio); 7207 return (CTL_RETVAL_COMPLETE); 7208 } 7209 7210 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 7211 7212 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7213 ctsio->kern_sg_entries = 0; 7214 ctsio->kern_data_resid = 0; 7215 ctsio->kern_rel_offset = 0; 7216 if (total_len < alloc_len) { 7217 ctsio->residual = alloc_len - total_len; 7218 ctsio->kern_data_len = total_len; 7219 ctsio->kern_total_len = total_len; 7220 } else { 7221 ctsio->residual = 0; 7222 ctsio->kern_data_len = alloc_len; 7223 ctsio->kern_total_len = alloc_len; 7224 } 7225 7226 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7227 header->page = page_index->page_code; 7228 if (page_index->subpage) { 7229 header->page |= SL_SPF; 7230 header->subpage = page_index->subpage; 7231 } 7232 scsi_ulto2b(page_index->page_len, header->datalen); 7233 7234 /* 7235 * Call the handler, if it exists, to update the 7236 * page to the latest values. 7237 */ 7238 if (page_index->sense_handler != NULL) 7239 page_index->sense_handler(ctsio, page_index, pc); 7240 7241 memcpy(header + 1, page_index->page_data, page_index->page_len); 7242 7243 ctsio->scsi_status = SCSI_STATUS_OK; 7244 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7245 ctsio->be_move_done = ctl_config_move_done; 7246 ctl_datamove((union ctl_io *)ctsio); 7247 7248 return (CTL_RETVAL_COMPLETE); 7249} 7250 7251int 7252ctl_read_capacity(struct ctl_scsiio *ctsio) 7253{ 7254 struct scsi_read_capacity *cdb; 7255 struct scsi_read_capacity_data *data; 7256 struct ctl_lun *lun; 7257 uint32_t lba; 7258 7259 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7260 7261 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7262 7263 lba = scsi_4btoul(cdb->addr); 7264 if (((cdb->pmi & SRC_PMI) == 0) 7265 && (lba != 0)) { 7266 ctl_set_invalid_field(/*ctsio*/ ctsio, 7267 /*sks_valid*/ 1, 7268 /*command*/ 1, 7269 /*field*/ 2, 7270 /*bit_valid*/ 0, 7271 /*bit*/ 0); 7272 ctl_done((union ctl_io *)ctsio); 7273 return (CTL_RETVAL_COMPLETE); 7274 } 7275 7276 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7277 7278 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7279 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7280 ctsio->residual = 0; 7281 ctsio->kern_data_len = sizeof(*data); 7282 ctsio->kern_total_len = sizeof(*data); 7283 ctsio->kern_data_resid = 0; 7284 ctsio->kern_rel_offset = 0; 7285 ctsio->kern_sg_entries = 0; 7286 7287 /* 7288 * If the maximum LBA is greater than 0xfffffffe, the user must 7289 * issue a SERVICE ACTION IN (16) command, with the read capacity 7290 * serivce action set. 7291 */ 7292 if (lun->be_lun->maxlba > 0xfffffffe) 7293 scsi_ulto4b(0xffffffff, data->addr); 7294 else 7295 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7296 7297 /* 7298 * XXX KDM this may not be 512 bytes... 7299 */ 7300 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7301 7302 ctsio->scsi_status = SCSI_STATUS_OK; 7303 7304 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7305 ctsio->be_move_done = ctl_config_move_done; 7306 ctl_datamove((union ctl_io *)ctsio); 7307 7308 return (CTL_RETVAL_COMPLETE); 7309} 7310 7311int 7312ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7313{ 7314 struct scsi_read_capacity_16 *cdb; 7315 struct scsi_read_capacity_data_long *data; 7316 struct ctl_lun *lun; 7317 uint64_t lba; 7318 uint32_t alloc_len; 7319 7320 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7321 7322 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7323 7324 alloc_len = scsi_4btoul(cdb->alloc_len); 7325 lba = scsi_8btou64(cdb->addr); 7326 7327 if ((cdb->reladr & SRC16_PMI) 7328 && (lba != 0)) { 7329 ctl_set_invalid_field(/*ctsio*/ ctsio, 7330 /*sks_valid*/ 1, 7331 /*command*/ 1, 7332 /*field*/ 2, 7333 /*bit_valid*/ 0, 7334 /*bit*/ 0); 7335 ctl_done((union ctl_io *)ctsio); 7336 return (CTL_RETVAL_COMPLETE); 7337 } 7338 7339 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7340 7341 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7342 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7343 7344 if (sizeof(*data) < alloc_len) { 7345 ctsio->residual = alloc_len - sizeof(*data); 7346 ctsio->kern_data_len = sizeof(*data); 7347 ctsio->kern_total_len = sizeof(*data); 7348 } else { 7349 ctsio->residual = 0; 7350 ctsio->kern_data_len = alloc_len; 7351 ctsio->kern_total_len = alloc_len; 7352 } 7353 ctsio->kern_data_resid = 0; 7354 ctsio->kern_rel_offset = 0; 7355 ctsio->kern_sg_entries = 0; 7356 7357 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7358 /* XXX KDM this may not be 512 bytes... */ 7359 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7360 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7361 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7362 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7363 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7364 7365 ctsio->scsi_status = SCSI_STATUS_OK; 7366 7367 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7368 ctsio->be_move_done = ctl_config_move_done; 7369 ctl_datamove((union ctl_io *)ctsio); 7370 7371 return (CTL_RETVAL_COMPLETE); 7372} 7373 7374int 7375ctl_read_defect(struct ctl_scsiio *ctsio) 7376{ 7377 struct scsi_read_defect_data_10 *ccb10; 7378 struct scsi_read_defect_data_12 *ccb12; 7379 struct scsi_read_defect_data_hdr_10 *data10; 7380 struct scsi_read_defect_data_hdr_12 *data12; 7381 uint32_t alloc_len, data_len; 7382 uint8_t format; 7383 7384 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7385 7386 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7387 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7388 format = ccb10->format; 7389 alloc_len = scsi_2btoul(ccb10->alloc_length); 7390 data_len = sizeof(*data10); 7391 } else { 7392 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7393 format = ccb12->format; 7394 alloc_len = scsi_4btoul(ccb12->alloc_length); 7395 data_len = sizeof(*data12); 7396 } 7397 if (alloc_len == 0) { 7398 ctl_set_success(ctsio); 7399 ctl_done((union ctl_io *)ctsio); 7400 return (CTL_RETVAL_COMPLETE); 7401 } 7402 7403 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7404 if (data_len < alloc_len) { 7405 ctsio->residual = alloc_len - data_len; 7406 ctsio->kern_data_len = data_len; 7407 ctsio->kern_total_len = data_len; 7408 } else { 7409 ctsio->residual = 0; 7410 ctsio->kern_data_len = alloc_len; 7411 ctsio->kern_total_len = alloc_len; 7412 } 7413 ctsio->kern_data_resid = 0; 7414 ctsio->kern_rel_offset = 0; 7415 ctsio->kern_sg_entries = 0; 7416 7417 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7418 data10 = (struct scsi_read_defect_data_hdr_10 *) 7419 ctsio->kern_data_ptr; 7420 data10->format = format; 7421 scsi_ulto2b(0, data10->length); 7422 } else { 7423 data12 = (struct scsi_read_defect_data_hdr_12 *) 7424 ctsio->kern_data_ptr; 7425 data12->format = format; 7426 scsi_ulto2b(0, data12->generation); 7427 scsi_ulto4b(0, data12->length); 7428 } 7429 7430 ctsio->scsi_status = SCSI_STATUS_OK; 7431 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7432 ctsio->be_move_done = ctl_config_move_done; 7433 ctl_datamove((union ctl_io *)ctsio); 7434 return (CTL_RETVAL_COMPLETE); 7435} 7436 7437int 7438ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7439{ 7440 struct scsi_maintenance_in *cdb; 7441 int retval; 7442 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7443 int num_target_port_groups, num_target_ports, single; 7444 struct ctl_lun *lun; 7445 struct ctl_softc *softc; 7446 struct ctl_port *port; 7447 struct scsi_target_group_data *rtg_ptr; 7448 struct scsi_target_group_data_extended *rtg_ext_ptr; 7449 struct scsi_target_port_group_descriptor *tpg_desc; 7450 7451 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7452 7453 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7454 softc = control_softc; 7455 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7456 7457 retval = CTL_RETVAL_COMPLETE; 7458 7459 switch (cdb->byte2 & STG_PDF_MASK) { 7460 case STG_PDF_LENGTH: 7461 ext = 0; 7462 break; 7463 case STG_PDF_EXTENDED: 7464 ext = 1; 7465 break; 7466 default: 7467 ctl_set_invalid_field(/*ctsio*/ ctsio, 7468 /*sks_valid*/ 1, 7469 /*command*/ 1, 7470 /*field*/ 2, 7471 /*bit_valid*/ 1, 7472 /*bit*/ 5); 7473 ctl_done((union ctl_io *)ctsio); 7474 return(retval); 7475 } 7476 7477 single = ctl_is_single; 7478 if (single) 7479 num_target_port_groups = 1; 7480 else 7481 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7482 num_target_ports = 0; 7483 mtx_lock(&softc->ctl_lock); 7484 STAILQ_FOREACH(port, &softc->port_list, links) { 7485 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7486 continue; 7487 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7488 continue; 7489 num_target_ports++; 7490 } 7491 mtx_unlock(&softc->ctl_lock); 7492 7493 if (ext) 7494 total_len = sizeof(struct scsi_target_group_data_extended); 7495 else 7496 total_len = sizeof(struct scsi_target_group_data); 7497 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7498 num_target_port_groups + 7499 sizeof(struct scsi_target_port_descriptor) * 7500 num_target_ports * num_target_port_groups; 7501 7502 alloc_len = scsi_4btoul(cdb->length); 7503 7504 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7505 7506 ctsio->kern_sg_entries = 0; 7507 7508 if (total_len < alloc_len) { 7509 ctsio->residual = alloc_len - total_len; 7510 ctsio->kern_data_len = total_len; 7511 ctsio->kern_total_len = total_len; 7512 } else { 7513 ctsio->residual = 0; 7514 ctsio->kern_data_len = alloc_len; 7515 ctsio->kern_total_len = alloc_len; 7516 } 7517 ctsio->kern_data_resid = 0; 7518 ctsio->kern_rel_offset = 0; 7519 7520 if (ext) { 7521 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7522 ctsio->kern_data_ptr; 7523 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7524 rtg_ext_ptr->format_type = 0x10; 7525 rtg_ext_ptr->implicit_transition_time = 0; 7526 tpg_desc = &rtg_ext_ptr->groups[0]; 7527 } else { 7528 rtg_ptr = (struct scsi_target_group_data *) 7529 ctsio->kern_data_ptr; 7530 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7531 tpg_desc = &rtg_ptr->groups[0]; 7532 } 7533 7534 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7535 mtx_lock(&softc->ctl_lock); 7536 for (g = 0; g < num_target_port_groups; g++) { 7537 if (g == pg) 7538 tpg_desc->pref_state = TPG_PRIMARY | 7539 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7540 else 7541 tpg_desc->pref_state = 7542 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7543 tpg_desc->support = TPG_AO_SUP; 7544 if (!single) 7545 tpg_desc->support |= TPG_AN_SUP; 7546 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7547 tpg_desc->status = TPG_IMPLICIT; 7548 pc = 0; 7549 STAILQ_FOREACH(port, &softc->port_list, links) { 7550 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7551 continue; 7552 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7553 CTL_MAX_LUNS) 7554 continue; 7555 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7556 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7557 relative_target_port_identifier); 7558 pc++; 7559 } 7560 tpg_desc->target_port_count = pc; 7561 tpg_desc = (struct scsi_target_port_group_descriptor *) 7562 &tpg_desc->descriptors[pc]; 7563 } 7564 mtx_unlock(&softc->ctl_lock); 7565 7566 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7567 ctsio->be_move_done = ctl_config_move_done; 7568 7569 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7570 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7571 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7572 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7573 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7574 7575 ctl_datamove((union ctl_io *)ctsio); 7576 return(retval); 7577} 7578 7579int 7580ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7581{ 7582 struct ctl_lun *lun; 7583 struct scsi_report_supported_opcodes *cdb; 7584 const struct ctl_cmd_entry *entry, *sentry; 7585 struct scsi_report_supported_opcodes_all *all; 7586 struct scsi_report_supported_opcodes_descr *descr; 7587 struct scsi_report_supported_opcodes_one *one; 7588 int retval; 7589 int alloc_len, total_len; 7590 int opcode, service_action, i, j, num; 7591 7592 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7593 7594 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7595 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7596 7597 retval = CTL_RETVAL_COMPLETE; 7598 7599 opcode = cdb->requested_opcode; 7600 service_action = scsi_2btoul(cdb->requested_service_action); 7601 switch (cdb->options & RSO_OPTIONS_MASK) { 7602 case RSO_OPTIONS_ALL: 7603 num = 0; 7604 for (i = 0; i < 256; i++) { 7605 entry = &ctl_cmd_table[i]; 7606 if (entry->flags & CTL_CMD_FLAG_SA5) { 7607 for (j = 0; j < 32; j++) { 7608 sentry = &((const struct ctl_cmd_entry *) 7609 entry->execute)[j]; 7610 if (ctl_cmd_applicable( 7611 lun->be_lun->lun_type, sentry)) 7612 num++; 7613 } 7614 } else { 7615 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7616 entry)) 7617 num++; 7618 } 7619 } 7620 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7621 num * sizeof(struct scsi_report_supported_opcodes_descr); 7622 break; 7623 case RSO_OPTIONS_OC: 7624 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7625 ctl_set_invalid_field(/*ctsio*/ ctsio, 7626 /*sks_valid*/ 1, 7627 /*command*/ 1, 7628 /*field*/ 2, 7629 /*bit_valid*/ 1, 7630 /*bit*/ 2); 7631 ctl_done((union ctl_io *)ctsio); 7632 return (CTL_RETVAL_COMPLETE); 7633 } 7634 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7635 break; 7636 case RSO_OPTIONS_OC_SA: 7637 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7638 service_action >= 32) { 7639 ctl_set_invalid_field(/*ctsio*/ ctsio, 7640 /*sks_valid*/ 1, 7641 /*command*/ 1, 7642 /*field*/ 2, 7643 /*bit_valid*/ 1, 7644 /*bit*/ 2); 7645 ctl_done((union ctl_io *)ctsio); 7646 return (CTL_RETVAL_COMPLETE); 7647 } 7648 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7649 break; 7650 default: 7651 ctl_set_invalid_field(/*ctsio*/ ctsio, 7652 /*sks_valid*/ 1, 7653 /*command*/ 1, 7654 /*field*/ 2, 7655 /*bit_valid*/ 1, 7656 /*bit*/ 2); 7657 ctl_done((union ctl_io *)ctsio); 7658 return (CTL_RETVAL_COMPLETE); 7659 } 7660 7661 alloc_len = scsi_4btoul(cdb->length); 7662 7663 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7664 7665 ctsio->kern_sg_entries = 0; 7666 7667 if (total_len < alloc_len) { 7668 ctsio->residual = alloc_len - total_len; 7669 ctsio->kern_data_len = total_len; 7670 ctsio->kern_total_len = total_len; 7671 } else { 7672 ctsio->residual = 0; 7673 ctsio->kern_data_len = alloc_len; 7674 ctsio->kern_total_len = alloc_len; 7675 } 7676 ctsio->kern_data_resid = 0; 7677 ctsio->kern_rel_offset = 0; 7678 7679 switch (cdb->options & RSO_OPTIONS_MASK) { 7680 case RSO_OPTIONS_ALL: 7681 all = (struct scsi_report_supported_opcodes_all *) 7682 ctsio->kern_data_ptr; 7683 num = 0; 7684 for (i = 0; i < 256; i++) { 7685 entry = &ctl_cmd_table[i]; 7686 if (entry->flags & CTL_CMD_FLAG_SA5) { 7687 for (j = 0; j < 32; j++) { 7688 sentry = &((const struct ctl_cmd_entry *) 7689 entry->execute)[j]; 7690 if (!ctl_cmd_applicable( 7691 lun->be_lun->lun_type, sentry)) 7692 continue; 7693 descr = &all->descr[num++]; 7694 descr->opcode = i; 7695 scsi_ulto2b(j, descr->service_action); 7696 descr->flags = RSO_SERVACTV; 7697 scsi_ulto2b(sentry->length, 7698 descr->cdb_length); 7699 } 7700 } else { 7701 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7702 entry)) 7703 continue; 7704 descr = &all->descr[num++]; 7705 descr->opcode = i; 7706 scsi_ulto2b(0, descr->service_action); 7707 descr->flags = 0; 7708 scsi_ulto2b(entry->length, descr->cdb_length); 7709 } 7710 } 7711 scsi_ulto4b( 7712 num * sizeof(struct scsi_report_supported_opcodes_descr), 7713 all->length); 7714 break; 7715 case RSO_OPTIONS_OC: 7716 one = (struct scsi_report_supported_opcodes_one *) 7717 ctsio->kern_data_ptr; 7718 entry = &ctl_cmd_table[opcode]; 7719 goto fill_one; 7720 case RSO_OPTIONS_OC_SA: 7721 one = (struct scsi_report_supported_opcodes_one *) 7722 ctsio->kern_data_ptr; 7723 entry = &ctl_cmd_table[opcode]; 7724 entry = &((const struct ctl_cmd_entry *) 7725 entry->execute)[service_action]; 7726fill_one: 7727 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7728 one->support = 3; 7729 scsi_ulto2b(entry->length, one->cdb_length); 7730 one->cdb_usage[0] = opcode; 7731 memcpy(&one->cdb_usage[1], entry->usage, 7732 entry->length - 1); 7733 } else 7734 one->support = 1; 7735 break; 7736 } 7737 7738 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7739 ctsio->be_move_done = ctl_config_move_done; 7740 7741 ctl_datamove((union ctl_io *)ctsio); 7742 return(retval); 7743} 7744 7745int 7746ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7747{ 7748 struct scsi_report_supported_tmf *cdb; 7749 struct scsi_report_supported_tmf_data *data; 7750 int retval; 7751 int alloc_len, total_len; 7752 7753 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7754 7755 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7756 7757 retval = CTL_RETVAL_COMPLETE; 7758 7759 total_len = sizeof(struct scsi_report_supported_tmf_data); 7760 alloc_len = scsi_4btoul(cdb->length); 7761 7762 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7763 7764 ctsio->kern_sg_entries = 0; 7765 7766 if (total_len < alloc_len) { 7767 ctsio->residual = alloc_len - total_len; 7768 ctsio->kern_data_len = total_len; 7769 ctsio->kern_total_len = total_len; 7770 } else { 7771 ctsio->residual = 0; 7772 ctsio->kern_data_len = alloc_len; 7773 ctsio->kern_total_len = alloc_len; 7774 } 7775 ctsio->kern_data_resid = 0; 7776 ctsio->kern_rel_offset = 0; 7777 7778 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7779 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7780 data->byte2 |= RST_ITNRS; 7781 7782 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7783 ctsio->be_move_done = ctl_config_move_done; 7784 7785 ctl_datamove((union ctl_io *)ctsio); 7786 return (retval); 7787} 7788 7789int 7790ctl_report_timestamp(struct ctl_scsiio *ctsio) 7791{ 7792 struct scsi_report_timestamp *cdb; 7793 struct scsi_report_timestamp_data *data; 7794 struct timeval tv; 7795 int64_t timestamp; 7796 int retval; 7797 int alloc_len, total_len; 7798 7799 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7800 7801 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7802 7803 retval = CTL_RETVAL_COMPLETE; 7804 7805 total_len = sizeof(struct scsi_report_timestamp_data); 7806 alloc_len = scsi_4btoul(cdb->length); 7807 7808 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7809 7810 ctsio->kern_sg_entries = 0; 7811 7812 if (total_len < alloc_len) { 7813 ctsio->residual = alloc_len - total_len; 7814 ctsio->kern_data_len = total_len; 7815 ctsio->kern_total_len = total_len; 7816 } else { 7817 ctsio->residual = 0; 7818 ctsio->kern_data_len = alloc_len; 7819 ctsio->kern_total_len = alloc_len; 7820 } 7821 ctsio->kern_data_resid = 0; 7822 ctsio->kern_rel_offset = 0; 7823 7824 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7825 scsi_ulto2b(sizeof(*data) - 2, data->length); 7826 data->origin = RTS_ORIG_OUTSIDE; 7827 getmicrotime(&tv); 7828 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7829 scsi_ulto4b(timestamp >> 16, data->timestamp); 7830 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7831 7832 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7833 ctsio->be_move_done = ctl_config_move_done; 7834 7835 ctl_datamove((union ctl_io *)ctsio); 7836 return (retval); 7837} 7838 7839int 7840ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7841{ 7842 struct scsi_per_res_in *cdb; 7843 int alloc_len, total_len = 0; 7844 /* struct scsi_per_res_in_rsrv in_data; */ 7845 struct ctl_lun *lun; 7846 struct ctl_softc *softc; 7847 7848 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7849 7850 softc = control_softc; 7851 7852 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7853 7854 alloc_len = scsi_2btoul(cdb->length); 7855 7856 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7857 7858retry: 7859 mtx_lock(&lun->lun_lock); 7860 switch (cdb->action) { 7861 case SPRI_RK: /* read keys */ 7862 total_len = sizeof(struct scsi_per_res_in_keys) + 7863 lun->pr_key_count * 7864 sizeof(struct scsi_per_res_key); 7865 break; 7866 case SPRI_RR: /* read reservation */ 7867 if (lun->flags & CTL_LUN_PR_RESERVED) 7868 total_len = sizeof(struct scsi_per_res_in_rsrv); 7869 else 7870 total_len = sizeof(struct scsi_per_res_in_header); 7871 break; 7872 case SPRI_RC: /* report capabilities */ 7873 total_len = sizeof(struct scsi_per_res_cap); 7874 break; 7875 case SPRI_RS: /* read full status */ 7876 total_len = sizeof(struct scsi_per_res_in_header) + 7877 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7878 lun->pr_key_count; 7879 break; 7880 default: 7881 panic("Invalid PR type %x", cdb->action); 7882 } 7883 mtx_unlock(&lun->lun_lock); 7884 7885 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7886 7887 if (total_len < alloc_len) { 7888 ctsio->residual = alloc_len - total_len; 7889 ctsio->kern_data_len = total_len; 7890 ctsio->kern_total_len = total_len; 7891 } else { 7892 ctsio->residual = 0; 7893 ctsio->kern_data_len = alloc_len; 7894 ctsio->kern_total_len = alloc_len; 7895 } 7896 7897 ctsio->kern_data_resid = 0; 7898 ctsio->kern_rel_offset = 0; 7899 ctsio->kern_sg_entries = 0; 7900 7901 mtx_lock(&lun->lun_lock); 7902 switch (cdb->action) { 7903 case SPRI_RK: { // read keys 7904 struct scsi_per_res_in_keys *res_keys; 7905 int i, key_count; 7906 7907 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7908 7909 /* 7910 * We had to drop the lock to allocate our buffer, which 7911 * leaves time for someone to come in with another 7912 * persistent reservation. (That is unlikely, though, 7913 * since this should be the only persistent reservation 7914 * command active right now.) 7915 */ 7916 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7917 (lun->pr_key_count * 7918 sizeof(struct scsi_per_res_key)))){ 7919 mtx_unlock(&lun->lun_lock); 7920 free(ctsio->kern_data_ptr, M_CTL); 7921 printf("%s: reservation length changed, retrying\n", 7922 __func__); 7923 goto retry; 7924 } 7925 7926 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7927 7928 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7929 lun->pr_key_count, res_keys->header.length); 7930 7931 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7932 if (lun->pr_keys[i] == 0) 7933 continue; 7934 7935 /* 7936 * We used lun->pr_key_count to calculate the 7937 * size to allocate. If it turns out the number of 7938 * initiators with the registered flag set is 7939 * larger than that (i.e. they haven't been kept in 7940 * sync), we've got a problem. 7941 */ 7942 if (key_count >= lun->pr_key_count) { 7943#ifdef NEEDTOPORT 7944 csevent_log(CSC_CTL | CSC_SHELF_SW | 7945 CTL_PR_ERROR, 7946 csevent_LogType_Fault, 7947 csevent_AlertLevel_Yellow, 7948 csevent_FRU_ShelfController, 7949 csevent_FRU_Firmware, 7950 csevent_FRU_Unknown, 7951 "registered keys %d >= key " 7952 "count %d", key_count, 7953 lun->pr_key_count); 7954#endif 7955 key_count++; 7956 continue; 7957 } 7958 scsi_u64to8b(lun->pr_keys[i], 7959 res_keys->keys[key_count].key); 7960 key_count++; 7961 } 7962 break; 7963 } 7964 case SPRI_RR: { // read reservation 7965 struct scsi_per_res_in_rsrv *res; 7966 int tmp_len, header_only; 7967 7968 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7969 7970 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7971 7972 if (lun->flags & CTL_LUN_PR_RESERVED) 7973 { 7974 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7975 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7976 res->header.length); 7977 header_only = 0; 7978 } else { 7979 tmp_len = sizeof(struct scsi_per_res_in_header); 7980 scsi_ulto4b(0, res->header.length); 7981 header_only = 1; 7982 } 7983 7984 /* 7985 * We had to drop the lock to allocate our buffer, which 7986 * leaves time for someone to come in with another 7987 * persistent reservation. (That is unlikely, though, 7988 * since this should be the only persistent reservation 7989 * command active right now.) 7990 */ 7991 if (tmp_len != total_len) { 7992 mtx_unlock(&lun->lun_lock); 7993 free(ctsio->kern_data_ptr, M_CTL); 7994 printf("%s: reservation status changed, retrying\n", 7995 __func__); 7996 goto retry; 7997 } 7998 7999 /* 8000 * No reservation held, so we're done. 8001 */ 8002 if (header_only != 0) 8003 break; 8004 8005 /* 8006 * If the registration is an All Registrants type, the key 8007 * is 0, since it doesn't really matter. 8008 */ 8009 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8010 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx], 8011 res->data.reservation); 8012 } 8013 res->data.scopetype = lun->res_type; 8014 break; 8015 } 8016 case SPRI_RC: //report capabilities 8017 { 8018 struct scsi_per_res_cap *res_cap; 8019 uint16_t type_mask; 8020 8021 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 8022 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 8023 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 8024 type_mask = SPRI_TM_WR_EX_AR | 8025 SPRI_TM_EX_AC_RO | 8026 SPRI_TM_WR_EX_RO | 8027 SPRI_TM_EX_AC | 8028 SPRI_TM_WR_EX | 8029 SPRI_TM_EX_AC_AR; 8030 scsi_ulto2b(type_mask, res_cap->type_mask); 8031 break; 8032 } 8033 case SPRI_RS: { // read full status 8034 struct scsi_per_res_in_full *res_status; 8035 struct scsi_per_res_in_full_desc *res_desc; 8036 struct ctl_port *port; 8037 int i, len; 8038 8039 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 8040 8041 /* 8042 * We had to drop the lock to allocate our buffer, which 8043 * leaves time for someone to come in with another 8044 * persistent reservation. (That is unlikely, though, 8045 * since this should be the only persistent reservation 8046 * command active right now.) 8047 */ 8048 if (total_len < (sizeof(struct scsi_per_res_in_header) + 8049 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 8050 lun->pr_key_count)){ 8051 mtx_unlock(&lun->lun_lock); 8052 free(ctsio->kern_data_ptr, M_CTL); 8053 printf("%s: reservation length changed, retrying\n", 8054 __func__); 8055 goto retry; 8056 } 8057 8058 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 8059 8060 res_desc = &res_status->desc[0]; 8061 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 8062 if (lun->pr_keys[i] == 0) 8063 continue; 8064 8065 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key); 8066 if ((lun->flags & CTL_LUN_PR_RESERVED) && 8067 (lun->pr_res_idx == i || 8068 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 8069 res_desc->flags = SPRI_FULL_R_HOLDER; 8070 res_desc->scopetype = lun->res_type; 8071 } 8072 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 8073 res_desc->rel_trgt_port_id); 8074 len = 0; 8075 port = softc->ctl_ports[ 8076 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 8077 if (port != NULL) 8078 len = ctl_create_iid(port, 8079 i % CTL_MAX_INIT_PER_PORT, 8080 res_desc->transport_id); 8081 scsi_ulto4b(len, res_desc->additional_length); 8082 res_desc = (struct scsi_per_res_in_full_desc *) 8083 &res_desc->transport_id[len]; 8084 } 8085 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 8086 res_status->header.length); 8087 break; 8088 } 8089 default: 8090 /* 8091 * This is a bug, because we just checked for this above, 8092 * and should have returned an error. 8093 */ 8094 panic("Invalid PR type %x", cdb->action); 8095 break; /* NOTREACHED */ 8096 } 8097 mtx_unlock(&lun->lun_lock); 8098 8099 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8100 ctsio->be_move_done = ctl_config_move_done; 8101 8102 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 8103 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 8104 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 8105 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 8106 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 8107 8108 ctl_datamove((union ctl_io *)ctsio); 8109 8110 return (CTL_RETVAL_COMPLETE); 8111} 8112 8113/* 8114 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 8115 * it should return. 8116 */ 8117static int 8118ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 8119 uint64_t sa_res_key, uint8_t type, uint32_t residx, 8120 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 8121 struct scsi_per_res_out_parms* param) 8122{ 8123 union ctl_ha_msg persis_io; 8124 int retval, i; 8125 int isc_retval; 8126 8127 retval = 0; 8128 8129 mtx_lock(&lun->lun_lock); 8130 if (sa_res_key == 0) { 8131 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8132 /* validate scope and type */ 8133 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8134 SPR_LU_SCOPE) { 8135 mtx_unlock(&lun->lun_lock); 8136 ctl_set_invalid_field(/*ctsio*/ ctsio, 8137 /*sks_valid*/ 1, 8138 /*command*/ 1, 8139 /*field*/ 2, 8140 /*bit_valid*/ 1, 8141 /*bit*/ 4); 8142 ctl_done((union ctl_io *)ctsio); 8143 return (1); 8144 } 8145 8146 if (type>8 || type==2 || type==4 || type==0) { 8147 mtx_unlock(&lun->lun_lock); 8148 ctl_set_invalid_field(/*ctsio*/ ctsio, 8149 /*sks_valid*/ 1, 8150 /*command*/ 1, 8151 /*field*/ 2, 8152 /*bit_valid*/ 1, 8153 /*bit*/ 0); 8154 ctl_done((union ctl_io *)ctsio); 8155 return (1); 8156 } 8157 8158 /* 8159 * Unregister everybody else and build UA for 8160 * them 8161 */ 8162 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8163 if (i == residx || lun->pr_keys[i] == 0) 8164 continue; 8165 8166 if (!persis_offset 8167 && i <CTL_MAX_INITIATORS) 8168 lun->pending_ua[i] |= 8169 CTL_UA_REG_PREEMPT; 8170 else if (persis_offset 8171 && i >= persis_offset) 8172 lun->pending_ua[i-persis_offset] |= 8173 CTL_UA_REG_PREEMPT; 8174 lun->pr_keys[i] = 0; 8175 } 8176 lun->pr_key_count = 1; 8177 lun->res_type = type; 8178 if (lun->res_type != SPR_TYPE_WR_EX_AR 8179 && lun->res_type != SPR_TYPE_EX_AC_AR) 8180 lun->pr_res_idx = residx; 8181 8182 /* send msg to other side */ 8183 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8184 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8185 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8186 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8187 persis_io.pr.pr_info.res_type = type; 8188 memcpy(persis_io.pr.pr_info.sa_res_key, 8189 param->serv_act_res_key, 8190 sizeof(param->serv_act_res_key)); 8191 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8192 &persis_io, sizeof(persis_io), 0)) > 8193 CTL_HA_STATUS_SUCCESS) { 8194 printf("CTL:Persis Out error returned " 8195 "from ctl_ha_msg_send %d\n", 8196 isc_retval); 8197 } 8198 } else { 8199 /* not all registrants */ 8200 mtx_unlock(&lun->lun_lock); 8201 free(ctsio->kern_data_ptr, M_CTL); 8202 ctl_set_invalid_field(ctsio, 8203 /*sks_valid*/ 1, 8204 /*command*/ 0, 8205 /*field*/ 8, 8206 /*bit_valid*/ 0, 8207 /*bit*/ 0); 8208 ctl_done((union ctl_io *)ctsio); 8209 return (1); 8210 } 8211 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8212 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8213 int found = 0; 8214 8215 if (res_key == sa_res_key) { 8216 /* special case */ 8217 /* 8218 * The spec implies this is not good but doesn't 8219 * say what to do. There are two choices either 8220 * generate a res conflict or check condition 8221 * with illegal field in parameter data. Since 8222 * that is what is done when the sa_res_key is 8223 * zero I'll take that approach since this has 8224 * to do with the sa_res_key. 8225 */ 8226 mtx_unlock(&lun->lun_lock); 8227 free(ctsio->kern_data_ptr, M_CTL); 8228 ctl_set_invalid_field(ctsio, 8229 /*sks_valid*/ 1, 8230 /*command*/ 0, 8231 /*field*/ 8, 8232 /*bit_valid*/ 0, 8233 /*bit*/ 0); 8234 ctl_done((union ctl_io *)ctsio); 8235 return (1); 8236 } 8237 8238 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8239 if (lun->pr_keys[i] != sa_res_key) 8240 continue; 8241 8242 found = 1; 8243 lun->pr_keys[i] = 0; 8244 lun->pr_key_count--; 8245 8246 if (!persis_offset && i < CTL_MAX_INITIATORS) 8247 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8248 else if (persis_offset && i >= persis_offset) 8249 lun->pending_ua[i-persis_offset] |= 8250 CTL_UA_REG_PREEMPT; 8251 } 8252 if (!found) { 8253 mtx_unlock(&lun->lun_lock); 8254 free(ctsio->kern_data_ptr, M_CTL); 8255 ctl_set_reservation_conflict(ctsio); 8256 ctl_done((union ctl_io *)ctsio); 8257 return (CTL_RETVAL_COMPLETE); 8258 } 8259 /* send msg to other side */ 8260 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8261 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8262 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8263 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8264 persis_io.pr.pr_info.res_type = type; 8265 memcpy(persis_io.pr.pr_info.sa_res_key, 8266 param->serv_act_res_key, 8267 sizeof(param->serv_act_res_key)); 8268 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8269 &persis_io, sizeof(persis_io), 0)) > 8270 CTL_HA_STATUS_SUCCESS) { 8271 printf("CTL:Persis Out error returned from " 8272 "ctl_ha_msg_send %d\n", isc_retval); 8273 } 8274 } else { 8275 /* Reserved but not all registrants */ 8276 /* sa_res_key is res holder */ 8277 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) { 8278 /* validate scope and type */ 8279 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8280 SPR_LU_SCOPE) { 8281 mtx_unlock(&lun->lun_lock); 8282 ctl_set_invalid_field(/*ctsio*/ ctsio, 8283 /*sks_valid*/ 1, 8284 /*command*/ 1, 8285 /*field*/ 2, 8286 /*bit_valid*/ 1, 8287 /*bit*/ 4); 8288 ctl_done((union ctl_io *)ctsio); 8289 return (1); 8290 } 8291 8292 if (type>8 || type==2 || type==4 || type==0) { 8293 mtx_unlock(&lun->lun_lock); 8294 ctl_set_invalid_field(/*ctsio*/ ctsio, 8295 /*sks_valid*/ 1, 8296 /*command*/ 1, 8297 /*field*/ 2, 8298 /*bit_valid*/ 1, 8299 /*bit*/ 0); 8300 ctl_done((union ctl_io *)ctsio); 8301 return (1); 8302 } 8303 8304 /* 8305 * Do the following: 8306 * if sa_res_key != res_key remove all 8307 * registrants w/sa_res_key and generate UA 8308 * for these registrants(Registrations 8309 * Preempted) if it wasn't an exclusive 8310 * reservation generate UA(Reservations 8311 * Preempted) for all other registered nexuses 8312 * if the type has changed. Establish the new 8313 * reservation and holder. If res_key and 8314 * sa_res_key are the same do the above 8315 * except don't unregister the res holder. 8316 */ 8317 8318 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8319 if (i == residx || lun->pr_keys[i] == 0) 8320 continue; 8321 8322 if (sa_res_key == lun->pr_keys[i]) { 8323 lun->pr_keys[i] = 0; 8324 lun->pr_key_count--; 8325 8326 if (!persis_offset 8327 && i < CTL_MAX_INITIATORS) 8328 lun->pending_ua[i] |= 8329 CTL_UA_REG_PREEMPT; 8330 else if (persis_offset 8331 && i >= persis_offset) 8332 lun->pending_ua[i-persis_offset] |= 8333 CTL_UA_REG_PREEMPT; 8334 } else if (type != lun->res_type 8335 && (lun->res_type == SPR_TYPE_WR_EX_RO 8336 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8337 if (!persis_offset 8338 && i < CTL_MAX_INITIATORS) 8339 lun->pending_ua[i] |= 8340 CTL_UA_RES_RELEASE; 8341 else if (persis_offset 8342 && i >= persis_offset) 8343 lun->pending_ua[ 8344 i-persis_offset] |= 8345 CTL_UA_RES_RELEASE; 8346 } 8347 } 8348 lun->res_type = type; 8349 if (lun->res_type != SPR_TYPE_WR_EX_AR 8350 && lun->res_type != SPR_TYPE_EX_AC_AR) 8351 lun->pr_res_idx = residx; 8352 else 8353 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8354 8355 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8356 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8357 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8358 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8359 persis_io.pr.pr_info.res_type = type; 8360 memcpy(persis_io.pr.pr_info.sa_res_key, 8361 param->serv_act_res_key, 8362 sizeof(param->serv_act_res_key)); 8363 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8364 &persis_io, sizeof(persis_io), 0)) > 8365 CTL_HA_STATUS_SUCCESS) { 8366 printf("CTL:Persis Out error returned " 8367 "from ctl_ha_msg_send %d\n", 8368 isc_retval); 8369 } 8370 } else { 8371 /* 8372 * sa_res_key is not the res holder just 8373 * remove registrants 8374 */ 8375 int found=0; 8376 8377 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8378 if (sa_res_key != lun->pr_keys[i]) 8379 continue; 8380 8381 found = 1; 8382 lun->pr_keys[i] = 0; 8383 lun->pr_key_count--; 8384 8385 if (!persis_offset 8386 && i < CTL_MAX_INITIATORS) 8387 lun->pending_ua[i] |= 8388 CTL_UA_REG_PREEMPT; 8389 else if (persis_offset 8390 && i >= persis_offset) 8391 lun->pending_ua[i-persis_offset] |= 8392 CTL_UA_REG_PREEMPT; 8393 } 8394 8395 if (!found) { 8396 mtx_unlock(&lun->lun_lock); 8397 free(ctsio->kern_data_ptr, M_CTL); 8398 ctl_set_reservation_conflict(ctsio); 8399 ctl_done((union ctl_io *)ctsio); 8400 return (1); 8401 } 8402 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8403 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8404 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8405 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8406 persis_io.pr.pr_info.res_type = type; 8407 memcpy(persis_io.pr.pr_info.sa_res_key, 8408 param->serv_act_res_key, 8409 sizeof(param->serv_act_res_key)); 8410 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8411 &persis_io, sizeof(persis_io), 0)) > 8412 CTL_HA_STATUS_SUCCESS) { 8413 printf("CTL:Persis Out error returned " 8414 "from ctl_ha_msg_send %d\n", 8415 isc_retval); 8416 } 8417 } 8418 } 8419 8420 lun->PRGeneration++; 8421 mtx_unlock(&lun->lun_lock); 8422 8423 return (retval); 8424} 8425 8426static void 8427ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8428{ 8429 uint64_t sa_res_key; 8430 int i; 8431 8432 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8433 8434 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8435 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8436 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) { 8437 if (sa_res_key == 0) { 8438 /* 8439 * Unregister everybody else and build UA for 8440 * them 8441 */ 8442 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8443 if (i == msg->pr.pr_info.residx || 8444 lun->pr_keys[i] == 0) 8445 continue; 8446 8447 if (!persis_offset 8448 && i < CTL_MAX_INITIATORS) 8449 lun->pending_ua[i] |= 8450 CTL_UA_REG_PREEMPT; 8451 else if (persis_offset && i >= persis_offset) 8452 lun->pending_ua[i - persis_offset] |= 8453 CTL_UA_REG_PREEMPT; 8454 lun->pr_keys[i] = 0; 8455 } 8456 8457 lun->pr_key_count = 1; 8458 lun->res_type = msg->pr.pr_info.res_type; 8459 if (lun->res_type != SPR_TYPE_WR_EX_AR 8460 && lun->res_type != SPR_TYPE_EX_AC_AR) 8461 lun->pr_res_idx = msg->pr.pr_info.residx; 8462 } else { 8463 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8464 if (sa_res_key == lun->pr_keys[i]) 8465 continue; 8466 8467 lun->pr_keys[i] = 0; 8468 lun->pr_key_count--; 8469 8470 if (!persis_offset 8471 && i < persis_offset) 8472 lun->pending_ua[i] |= 8473 CTL_UA_REG_PREEMPT; 8474 else if (persis_offset 8475 && i >= persis_offset) 8476 lun->pending_ua[i - persis_offset] |= 8477 CTL_UA_REG_PREEMPT; 8478 } 8479 } 8480 } else { 8481 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8482 if (i == msg->pr.pr_info.residx || 8483 lun->pr_keys[i] == 0) 8484 continue; 8485 8486 if (sa_res_key == lun->pr_keys[i]) { 8487 lun->pr_keys[i] = 0; 8488 lun->pr_key_count--; 8489 if (!persis_offset 8490 && i < CTL_MAX_INITIATORS) 8491 lun->pending_ua[i] |= 8492 CTL_UA_REG_PREEMPT; 8493 else if (persis_offset 8494 && i >= persis_offset) 8495 lun->pending_ua[i - persis_offset] |= 8496 CTL_UA_REG_PREEMPT; 8497 } else if (msg->pr.pr_info.res_type != lun->res_type 8498 && (lun->res_type == SPR_TYPE_WR_EX_RO 8499 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8500 if (!persis_offset 8501 && i < persis_offset) 8502 lun->pending_ua[i] |= 8503 CTL_UA_RES_RELEASE; 8504 else if (persis_offset 8505 && i >= persis_offset) 8506 lun->pending_ua[i - persis_offset] |= 8507 CTL_UA_RES_RELEASE; 8508 } 8509 } 8510 lun->res_type = msg->pr.pr_info.res_type; 8511 if (lun->res_type != SPR_TYPE_WR_EX_AR 8512 && lun->res_type != SPR_TYPE_EX_AC_AR) 8513 lun->pr_res_idx = msg->pr.pr_info.residx; 8514 else 8515 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8516 } 8517 lun->PRGeneration++; 8518 8519} 8520 8521 8522int 8523ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8524{ 8525 int retval; 8526 int isc_retval; 8527 u_int32_t param_len; 8528 struct scsi_per_res_out *cdb; 8529 struct ctl_lun *lun; 8530 struct scsi_per_res_out_parms* param; 8531 struct ctl_softc *softc; 8532 uint32_t residx; 8533 uint64_t res_key, sa_res_key; 8534 uint8_t type; 8535 union ctl_ha_msg persis_io; 8536 int i; 8537 8538 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8539 8540 retval = CTL_RETVAL_COMPLETE; 8541 8542 softc = control_softc; 8543 8544 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8545 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8546 8547 /* 8548 * We only support whole-LUN scope. The scope & type are ignored for 8549 * register, register and ignore existing key and clear. 8550 * We sometimes ignore scope and type on preempts too!! 8551 * Verify reservation type here as well. 8552 */ 8553 type = cdb->scope_type & SPR_TYPE_MASK; 8554 if ((cdb->action == SPRO_RESERVE) 8555 || (cdb->action == SPRO_RELEASE)) { 8556 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8557 ctl_set_invalid_field(/*ctsio*/ ctsio, 8558 /*sks_valid*/ 1, 8559 /*command*/ 1, 8560 /*field*/ 2, 8561 /*bit_valid*/ 1, 8562 /*bit*/ 4); 8563 ctl_done((union ctl_io *)ctsio); 8564 return (CTL_RETVAL_COMPLETE); 8565 } 8566 8567 if (type>8 || type==2 || type==4 || type==0) { 8568 ctl_set_invalid_field(/*ctsio*/ ctsio, 8569 /*sks_valid*/ 1, 8570 /*command*/ 1, 8571 /*field*/ 2, 8572 /*bit_valid*/ 1, 8573 /*bit*/ 0); 8574 ctl_done((union ctl_io *)ctsio); 8575 return (CTL_RETVAL_COMPLETE); 8576 } 8577 } 8578 8579 param_len = scsi_4btoul(cdb->length); 8580 8581 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8582 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8583 ctsio->kern_data_len = param_len; 8584 ctsio->kern_total_len = param_len; 8585 ctsio->kern_data_resid = 0; 8586 ctsio->kern_rel_offset = 0; 8587 ctsio->kern_sg_entries = 0; 8588 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8589 ctsio->be_move_done = ctl_config_move_done; 8590 ctl_datamove((union ctl_io *)ctsio); 8591 8592 return (CTL_RETVAL_COMPLETE); 8593 } 8594 8595 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8596 8597 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8598 res_key = scsi_8btou64(param->res_key.key); 8599 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8600 8601 /* 8602 * Validate the reservation key here except for SPRO_REG_IGNO 8603 * This must be done for all other service actions 8604 */ 8605 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8606 mtx_lock(&lun->lun_lock); 8607 if (lun->pr_keys[residx] != 0) { 8608 if (res_key != lun->pr_keys[residx]) { 8609 /* 8610 * The current key passed in doesn't match 8611 * the one the initiator previously 8612 * registered. 8613 */ 8614 mtx_unlock(&lun->lun_lock); 8615 free(ctsio->kern_data_ptr, M_CTL); 8616 ctl_set_reservation_conflict(ctsio); 8617 ctl_done((union ctl_io *)ctsio); 8618 return (CTL_RETVAL_COMPLETE); 8619 } 8620 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8621 /* 8622 * We are not registered 8623 */ 8624 mtx_unlock(&lun->lun_lock); 8625 free(ctsio->kern_data_ptr, M_CTL); 8626 ctl_set_reservation_conflict(ctsio); 8627 ctl_done((union ctl_io *)ctsio); 8628 return (CTL_RETVAL_COMPLETE); 8629 } else if (res_key != 0) { 8630 /* 8631 * We are not registered and trying to register but 8632 * the register key isn't zero. 8633 */ 8634 mtx_unlock(&lun->lun_lock); 8635 free(ctsio->kern_data_ptr, M_CTL); 8636 ctl_set_reservation_conflict(ctsio); 8637 ctl_done((union ctl_io *)ctsio); 8638 return (CTL_RETVAL_COMPLETE); 8639 } 8640 mtx_unlock(&lun->lun_lock); 8641 } 8642 8643 switch (cdb->action & SPRO_ACTION_MASK) { 8644 case SPRO_REGISTER: 8645 case SPRO_REG_IGNO: { 8646 8647#if 0 8648 printf("Registration received\n"); 8649#endif 8650 8651 /* 8652 * We don't support any of these options, as we report in 8653 * the read capabilities request (see 8654 * ctl_persistent_reserve_in(), above). 8655 */ 8656 if ((param->flags & SPR_SPEC_I_PT) 8657 || (param->flags & SPR_ALL_TG_PT) 8658 || (param->flags & SPR_APTPL)) { 8659 int bit_ptr; 8660 8661 if (param->flags & SPR_APTPL) 8662 bit_ptr = 0; 8663 else if (param->flags & SPR_ALL_TG_PT) 8664 bit_ptr = 2; 8665 else /* SPR_SPEC_I_PT */ 8666 bit_ptr = 3; 8667 8668 free(ctsio->kern_data_ptr, M_CTL); 8669 ctl_set_invalid_field(ctsio, 8670 /*sks_valid*/ 1, 8671 /*command*/ 0, 8672 /*field*/ 20, 8673 /*bit_valid*/ 1, 8674 /*bit*/ bit_ptr); 8675 ctl_done((union ctl_io *)ctsio); 8676 return (CTL_RETVAL_COMPLETE); 8677 } 8678 8679 mtx_lock(&lun->lun_lock); 8680 8681 /* 8682 * The initiator wants to clear the 8683 * key/unregister. 8684 */ 8685 if (sa_res_key == 0) { 8686 if ((res_key == 0 8687 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8688 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8689 && lun->pr_keys[residx] == 0)) { 8690 mtx_unlock(&lun->lun_lock); 8691 goto done; 8692 } 8693 8694 lun->pr_keys[residx] = 0; 8695 lun->pr_key_count--; 8696 8697 if (residx == lun->pr_res_idx) { 8698 lun->flags &= ~CTL_LUN_PR_RESERVED; 8699 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8700 8701 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8702 || lun->res_type == SPR_TYPE_EX_AC_RO) 8703 && lun->pr_key_count) { 8704 /* 8705 * If the reservation is a registrants 8706 * only type we need to generate a UA 8707 * for other registered inits. The 8708 * sense code should be RESERVATIONS 8709 * RELEASED 8710 */ 8711 8712 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8713 if (lun->pr_keys[ 8714 i + persis_offset] == 0) 8715 continue; 8716 lun->pending_ua[i] |= 8717 CTL_UA_RES_RELEASE; 8718 } 8719 } 8720 lun->res_type = 0; 8721 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8722 if (lun->pr_key_count==0) { 8723 lun->flags &= ~CTL_LUN_PR_RESERVED; 8724 lun->res_type = 0; 8725 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8726 } 8727 } 8728 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8729 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8730 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8731 persis_io.pr.pr_info.residx = residx; 8732 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8733 &persis_io, sizeof(persis_io), 0 )) > 8734 CTL_HA_STATUS_SUCCESS) { 8735 printf("CTL:Persis Out error returned from " 8736 "ctl_ha_msg_send %d\n", isc_retval); 8737 } 8738 } else /* sa_res_key != 0 */ { 8739 8740 /* 8741 * If we aren't registered currently then increment 8742 * the key count and set the registered flag. 8743 */ 8744 if (lun->pr_keys[residx] == 0) 8745 lun->pr_key_count++; 8746 lun->pr_keys[residx] = sa_res_key; 8747 8748 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8749 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8750 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8751 persis_io.pr.pr_info.residx = residx; 8752 memcpy(persis_io.pr.pr_info.sa_res_key, 8753 param->serv_act_res_key, 8754 sizeof(param->serv_act_res_key)); 8755 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8756 &persis_io, sizeof(persis_io), 0)) > 8757 CTL_HA_STATUS_SUCCESS) { 8758 printf("CTL:Persis Out error returned from " 8759 "ctl_ha_msg_send %d\n", isc_retval); 8760 } 8761 } 8762 lun->PRGeneration++; 8763 mtx_unlock(&lun->lun_lock); 8764 8765 break; 8766 } 8767 case SPRO_RESERVE: 8768#if 0 8769 printf("Reserve executed type %d\n", type); 8770#endif 8771 mtx_lock(&lun->lun_lock); 8772 if (lun->flags & CTL_LUN_PR_RESERVED) { 8773 /* 8774 * if this isn't the reservation holder and it's 8775 * not a "all registrants" type or if the type is 8776 * different then we have a conflict 8777 */ 8778 if ((lun->pr_res_idx != residx 8779 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8780 || lun->res_type != type) { 8781 mtx_unlock(&lun->lun_lock); 8782 free(ctsio->kern_data_ptr, M_CTL); 8783 ctl_set_reservation_conflict(ctsio); 8784 ctl_done((union ctl_io *)ctsio); 8785 return (CTL_RETVAL_COMPLETE); 8786 } 8787 mtx_unlock(&lun->lun_lock); 8788 } else /* create a reservation */ { 8789 /* 8790 * If it's not an "all registrants" type record 8791 * reservation holder 8792 */ 8793 if (type != SPR_TYPE_WR_EX_AR 8794 && type != SPR_TYPE_EX_AC_AR) 8795 lun->pr_res_idx = residx; /* Res holder */ 8796 else 8797 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8798 8799 lun->flags |= CTL_LUN_PR_RESERVED; 8800 lun->res_type = type; 8801 8802 mtx_unlock(&lun->lun_lock); 8803 8804 /* send msg to other side */ 8805 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8806 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8807 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8808 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8809 persis_io.pr.pr_info.res_type = type; 8810 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8811 &persis_io, sizeof(persis_io), 0)) > 8812 CTL_HA_STATUS_SUCCESS) { 8813 printf("CTL:Persis Out error returned from " 8814 "ctl_ha_msg_send %d\n", isc_retval); 8815 } 8816 } 8817 break; 8818 8819 case SPRO_RELEASE: 8820 mtx_lock(&lun->lun_lock); 8821 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8822 /* No reservation exists return good status */ 8823 mtx_unlock(&lun->lun_lock); 8824 goto done; 8825 } 8826 /* 8827 * Is this nexus a reservation holder? 8828 */ 8829 if (lun->pr_res_idx != residx 8830 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8831 /* 8832 * not a res holder return good status but 8833 * do nothing 8834 */ 8835 mtx_unlock(&lun->lun_lock); 8836 goto done; 8837 } 8838 8839 if (lun->res_type != type) { 8840 mtx_unlock(&lun->lun_lock); 8841 free(ctsio->kern_data_ptr, M_CTL); 8842 ctl_set_illegal_pr_release(ctsio); 8843 ctl_done((union ctl_io *)ctsio); 8844 return (CTL_RETVAL_COMPLETE); 8845 } 8846 8847 /* okay to release */ 8848 lun->flags &= ~CTL_LUN_PR_RESERVED; 8849 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8850 lun->res_type = 0; 8851 8852 /* 8853 * if this isn't an exclusive access 8854 * res generate UA for all other 8855 * registrants. 8856 */ 8857 if (type != SPR_TYPE_EX_AC 8858 && type != SPR_TYPE_WR_EX) { 8859 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8860 if (i == residx || 8861 lun->pr_keys[i + persis_offset] == 0) 8862 continue; 8863 lun->pending_ua[i] |= CTL_UA_RES_RELEASE; 8864 } 8865 } 8866 mtx_unlock(&lun->lun_lock); 8867 /* Send msg to other side */ 8868 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8869 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8870 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8871 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8872 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8873 printf("CTL:Persis Out error returned from " 8874 "ctl_ha_msg_send %d\n", isc_retval); 8875 } 8876 break; 8877 8878 case SPRO_CLEAR: 8879 /* send msg to other side */ 8880 8881 mtx_lock(&lun->lun_lock); 8882 lun->flags &= ~CTL_LUN_PR_RESERVED; 8883 lun->res_type = 0; 8884 lun->pr_key_count = 0; 8885 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8886 8887 lun->pr_keys[residx] = 0; 8888 8889 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8890 if (lun->pr_keys[i] != 0) { 8891 if (!persis_offset && i < CTL_MAX_INITIATORS) 8892 lun->pending_ua[i] |= 8893 CTL_UA_RES_PREEMPT; 8894 else if (persis_offset && i >= persis_offset) 8895 lun->pending_ua[i-persis_offset] |= 8896 CTL_UA_RES_PREEMPT; 8897 8898 lun->pr_keys[i] = 0; 8899 } 8900 lun->PRGeneration++; 8901 mtx_unlock(&lun->lun_lock); 8902 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8903 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8904 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8905 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8906 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8907 printf("CTL:Persis Out error returned from " 8908 "ctl_ha_msg_send %d\n", isc_retval); 8909 } 8910 break; 8911 8912 case SPRO_PREEMPT: 8913 case SPRO_PRE_ABO: { 8914 int nretval; 8915 8916 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8917 residx, ctsio, cdb, param); 8918 if (nretval != 0) 8919 return (CTL_RETVAL_COMPLETE); 8920 break; 8921 } 8922 default: 8923 panic("Invalid PR type %x", cdb->action); 8924 } 8925 8926done: 8927 free(ctsio->kern_data_ptr, M_CTL); 8928 ctl_set_success(ctsio); 8929 ctl_done((union ctl_io *)ctsio); 8930 8931 return (retval); 8932} 8933 8934/* 8935 * This routine is for handling a message from the other SC pertaining to 8936 * persistent reserve out. All the error checking will have been done 8937 * so only perorming the action need be done here to keep the two 8938 * in sync. 8939 */ 8940static void 8941ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8942{ 8943 struct ctl_lun *lun; 8944 struct ctl_softc *softc; 8945 int i; 8946 uint32_t targ_lun; 8947 8948 softc = control_softc; 8949 8950 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8951 lun = softc->ctl_luns[targ_lun]; 8952 mtx_lock(&lun->lun_lock); 8953 switch(msg->pr.pr_info.action) { 8954 case CTL_PR_REG_KEY: 8955 if (lun->pr_keys[msg->pr.pr_info.residx] == 0) 8956 lun->pr_key_count++; 8957 lun->pr_keys[msg->pr.pr_info.residx] = 8958 scsi_8btou64(msg->pr.pr_info.sa_res_key); 8959 lun->PRGeneration++; 8960 break; 8961 8962 case CTL_PR_UNREG_KEY: 8963 lun->pr_keys[msg->pr.pr_info.residx] = 0; 8964 lun->pr_key_count--; 8965 8966 /* XXX Need to see if the reservation has been released */ 8967 /* if so do we need to generate UA? */ 8968 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8969 lun->flags &= ~CTL_LUN_PR_RESERVED; 8970 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8971 8972 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8973 || lun->res_type == SPR_TYPE_EX_AC_RO) 8974 && lun->pr_key_count) { 8975 /* 8976 * If the reservation is a registrants 8977 * only type we need to generate a UA 8978 * for other registered inits. The 8979 * sense code should be RESERVATIONS 8980 * RELEASED 8981 */ 8982 8983 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8984 if (lun->pr_keys[i+ 8985 persis_offset] == 0) 8986 continue; 8987 8988 lun->pending_ua[i] |= 8989 CTL_UA_RES_RELEASE; 8990 } 8991 } 8992 lun->res_type = 0; 8993 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8994 if (lun->pr_key_count==0) { 8995 lun->flags &= ~CTL_LUN_PR_RESERVED; 8996 lun->res_type = 0; 8997 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8998 } 8999 } 9000 lun->PRGeneration++; 9001 break; 9002 9003 case CTL_PR_RESERVE: 9004 lun->flags |= CTL_LUN_PR_RESERVED; 9005 lun->res_type = msg->pr.pr_info.res_type; 9006 lun->pr_res_idx = msg->pr.pr_info.residx; 9007 9008 break; 9009 9010 case CTL_PR_RELEASE: 9011 /* 9012 * if this isn't an exclusive access res generate UA for all 9013 * other registrants. 9014 */ 9015 if (lun->res_type != SPR_TYPE_EX_AC 9016 && lun->res_type != SPR_TYPE_WR_EX) { 9017 for (i = 0; i < CTL_MAX_INITIATORS; i++) 9018 if (lun->pr_keys[i+persis_offset] != 0) 9019 lun->pending_ua[i] |= 9020 CTL_UA_RES_RELEASE; 9021 } 9022 9023 lun->flags &= ~CTL_LUN_PR_RESERVED; 9024 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 9025 lun->res_type = 0; 9026 break; 9027 9028 case CTL_PR_PREEMPT: 9029 ctl_pro_preempt_other(lun, msg); 9030 break; 9031 case CTL_PR_CLEAR: 9032 lun->flags &= ~CTL_LUN_PR_RESERVED; 9033 lun->res_type = 0; 9034 lun->pr_key_count = 0; 9035 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 9036 9037 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 9038 if (lun->pr_keys[i] == 0) 9039 continue; 9040 if (!persis_offset 9041 && i < CTL_MAX_INITIATORS) 9042 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 9043 else if (persis_offset 9044 && i >= persis_offset) 9045 lun->pending_ua[i-persis_offset] |= 9046 CTL_UA_RES_PREEMPT; 9047 lun->pr_keys[i] = 0; 9048 } 9049 lun->PRGeneration++; 9050 break; 9051 } 9052 9053 mtx_unlock(&lun->lun_lock); 9054} 9055 9056int 9057ctl_read_write(struct ctl_scsiio *ctsio) 9058{ 9059 struct ctl_lun *lun; 9060 struct ctl_lba_len_flags *lbalen; 9061 uint64_t lba; 9062 uint32_t num_blocks; 9063 int flags, retval; 9064 int isread; 9065 9066 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9067 9068 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 9069 9070 flags = 0; 9071 retval = CTL_RETVAL_COMPLETE; 9072 9073 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 9074 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 9075 switch (ctsio->cdb[0]) { 9076 case READ_6: 9077 case WRITE_6: { 9078 struct scsi_rw_6 *cdb; 9079 9080 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9081 9082 lba = scsi_3btoul(cdb->addr); 9083 /* only 5 bits are valid in the most significant address byte */ 9084 lba &= 0x1fffff; 9085 num_blocks = cdb->length; 9086 /* 9087 * This is correct according to SBC-2. 9088 */ 9089 if (num_blocks == 0) 9090 num_blocks = 256; 9091 break; 9092 } 9093 case READ_10: 9094 case WRITE_10: { 9095 struct scsi_rw_10 *cdb; 9096 9097 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9098 if (cdb->byte2 & SRW10_FUA) 9099 flags |= CTL_LLF_FUA; 9100 if (cdb->byte2 & SRW10_DPO) 9101 flags |= CTL_LLF_DPO; 9102 lba = scsi_4btoul(cdb->addr); 9103 num_blocks = scsi_2btoul(cdb->length); 9104 break; 9105 } 9106 case WRITE_VERIFY_10: { 9107 struct scsi_write_verify_10 *cdb; 9108 9109 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9110 flags |= CTL_LLF_FUA; 9111 if (cdb->byte2 & SWV_DPO) 9112 flags |= CTL_LLF_DPO; 9113 lba = scsi_4btoul(cdb->addr); 9114 num_blocks = scsi_2btoul(cdb->length); 9115 break; 9116 } 9117 case READ_12: 9118 case WRITE_12: { 9119 struct scsi_rw_12 *cdb; 9120 9121 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9122 if (cdb->byte2 & SRW12_FUA) 9123 flags |= CTL_LLF_FUA; 9124 if (cdb->byte2 & SRW12_DPO) 9125 flags |= CTL_LLF_DPO; 9126 lba = scsi_4btoul(cdb->addr); 9127 num_blocks = scsi_4btoul(cdb->length); 9128 break; 9129 } 9130 case WRITE_VERIFY_12: { 9131 struct scsi_write_verify_12 *cdb; 9132 9133 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9134 flags |= CTL_LLF_FUA; 9135 if (cdb->byte2 & SWV_DPO) 9136 flags |= CTL_LLF_DPO; 9137 lba = scsi_4btoul(cdb->addr); 9138 num_blocks = scsi_4btoul(cdb->length); 9139 break; 9140 } 9141 case READ_16: 9142 case WRITE_16: { 9143 struct scsi_rw_16 *cdb; 9144 9145 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9146 if (cdb->byte2 & SRW12_FUA) 9147 flags |= CTL_LLF_FUA; 9148 if (cdb->byte2 & SRW12_DPO) 9149 flags |= CTL_LLF_DPO; 9150 lba = scsi_8btou64(cdb->addr); 9151 num_blocks = scsi_4btoul(cdb->length); 9152 break; 9153 } 9154 case WRITE_ATOMIC_16: { 9155 struct scsi_rw_16 *cdb; 9156 9157 if (lun->be_lun->atomicblock == 0) { 9158 ctl_set_invalid_opcode(ctsio); 9159 ctl_done((union ctl_io *)ctsio); 9160 return (CTL_RETVAL_COMPLETE); 9161 } 9162 9163 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9164 if (cdb->byte2 & SRW12_FUA) 9165 flags |= CTL_LLF_FUA; 9166 if (cdb->byte2 & SRW12_DPO) 9167 flags |= CTL_LLF_DPO; 9168 lba = scsi_8btou64(cdb->addr); 9169 num_blocks = scsi_4btoul(cdb->length); 9170 if (num_blocks > lun->be_lun->atomicblock) { 9171 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 9172 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 9173 /*bit*/ 0); 9174 ctl_done((union ctl_io *)ctsio); 9175 return (CTL_RETVAL_COMPLETE); 9176 } 9177 break; 9178 } 9179 case WRITE_VERIFY_16: { 9180 struct scsi_write_verify_16 *cdb; 9181 9182 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9183 flags |= CTL_LLF_FUA; 9184 if (cdb->byte2 & SWV_DPO) 9185 flags |= CTL_LLF_DPO; 9186 lba = scsi_8btou64(cdb->addr); 9187 num_blocks = scsi_4btoul(cdb->length); 9188 break; 9189 } 9190 default: 9191 /* 9192 * We got a command we don't support. This shouldn't 9193 * happen, commands should be filtered out above us. 9194 */ 9195 ctl_set_invalid_opcode(ctsio); 9196 ctl_done((union ctl_io *)ctsio); 9197 9198 return (CTL_RETVAL_COMPLETE); 9199 break; /* NOTREACHED */ 9200 } 9201 9202 /* 9203 * The first check is to make sure we're in bounds, the second 9204 * check is to catch wrap-around problems. If the lba + num blocks 9205 * is less than the lba, then we've wrapped around and the block 9206 * range is invalid anyway. 9207 */ 9208 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9209 || ((lba + num_blocks) < lba)) { 9210 ctl_set_lba_out_of_range(ctsio); 9211 ctl_done((union ctl_io *)ctsio); 9212 return (CTL_RETVAL_COMPLETE); 9213 } 9214 9215 /* 9216 * According to SBC-3, a transfer length of 0 is not an error. 9217 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9218 * translates to 256 blocks for those commands. 9219 */ 9220 if (num_blocks == 0) { 9221 ctl_set_success(ctsio); 9222 ctl_done((union ctl_io *)ctsio); 9223 return (CTL_RETVAL_COMPLETE); 9224 } 9225 9226 /* Set FUA and/or DPO if caches are disabled. */ 9227 if (isread) { 9228 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9229 SCP_RCD) != 0) 9230 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9231 } else { 9232 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9233 SCP_WCE) == 0) 9234 flags |= CTL_LLF_FUA; 9235 } 9236 9237 lbalen = (struct ctl_lba_len_flags *) 9238 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9239 lbalen->lba = lba; 9240 lbalen->len = num_blocks; 9241 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9242 9243 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9244 ctsio->kern_rel_offset = 0; 9245 9246 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9247 9248 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9249 9250 return (retval); 9251} 9252 9253static int 9254ctl_cnw_cont(union ctl_io *io) 9255{ 9256 struct ctl_scsiio *ctsio; 9257 struct ctl_lun *lun; 9258 struct ctl_lba_len_flags *lbalen; 9259 int retval; 9260 9261 ctsio = &io->scsiio; 9262 ctsio->io_hdr.status = CTL_STATUS_NONE; 9263 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9264 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9265 lbalen = (struct ctl_lba_len_flags *) 9266 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9267 lbalen->flags &= ~CTL_LLF_COMPARE; 9268 lbalen->flags |= CTL_LLF_WRITE; 9269 9270 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9271 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9272 return (retval); 9273} 9274 9275int 9276ctl_cnw(struct ctl_scsiio *ctsio) 9277{ 9278 struct ctl_lun *lun; 9279 struct ctl_lba_len_flags *lbalen; 9280 uint64_t lba; 9281 uint32_t num_blocks; 9282 int flags, retval; 9283 9284 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9285 9286 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9287 9288 flags = 0; 9289 retval = CTL_RETVAL_COMPLETE; 9290 9291 switch (ctsio->cdb[0]) { 9292 case COMPARE_AND_WRITE: { 9293 struct scsi_compare_and_write *cdb; 9294 9295 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9296 if (cdb->byte2 & SRW10_FUA) 9297 flags |= CTL_LLF_FUA; 9298 if (cdb->byte2 & SRW10_DPO) 9299 flags |= CTL_LLF_DPO; 9300 lba = scsi_8btou64(cdb->addr); 9301 num_blocks = cdb->length; 9302 break; 9303 } 9304 default: 9305 /* 9306 * We got a command we don't support. This shouldn't 9307 * happen, commands should be filtered out above us. 9308 */ 9309 ctl_set_invalid_opcode(ctsio); 9310 ctl_done((union ctl_io *)ctsio); 9311 9312 return (CTL_RETVAL_COMPLETE); 9313 break; /* NOTREACHED */ 9314 } 9315 9316 /* 9317 * The first check is to make sure we're in bounds, the second 9318 * check is to catch wrap-around problems. If the lba + num blocks 9319 * is less than the lba, then we've wrapped around and the block 9320 * range is invalid anyway. 9321 */ 9322 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9323 || ((lba + num_blocks) < lba)) { 9324 ctl_set_lba_out_of_range(ctsio); 9325 ctl_done((union ctl_io *)ctsio); 9326 return (CTL_RETVAL_COMPLETE); 9327 } 9328 9329 /* 9330 * According to SBC-3, a transfer length of 0 is not an error. 9331 */ 9332 if (num_blocks == 0) { 9333 ctl_set_success(ctsio); 9334 ctl_done((union ctl_io *)ctsio); 9335 return (CTL_RETVAL_COMPLETE); 9336 } 9337 9338 /* Set FUA if write cache is disabled. */ 9339 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9340 SCP_WCE) == 0) 9341 flags |= CTL_LLF_FUA; 9342 9343 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9344 ctsio->kern_rel_offset = 0; 9345 9346 /* 9347 * Set the IO_CONT flag, so that if this I/O gets passed to 9348 * ctl_data_submit_done(), it'll get passed back to 9349 * ctl_ctl_cnw_cont() for further processing. 9350 */ 9351 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9352 ctsio->io_cont = ctl_cnw_cont; 9353 9354 lbalen = (struct ctl_lba_len_flags *) 9355 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9356 lbalen->lba = lba; 9357 lbalen->len = num_blocks; 9358 lbalen->flags = CTL_LLF_COMPARE | flags; 9359 9360 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9361 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9362 return (retval); 9363} 9364 9365int 9366ctl_verify(struct ctl_scsiio *ctsio) 9367{ 9368 struct ctl_lun *lun; 9369 struct ctl_lba_len_flags *lbalen; 9370 uint64_t lba; 9371 uint32_t num_blocks; 9372 int bytchk, flags; 9373 int retval; 9374 9375 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9376 9377 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9378 9379 bytchk = 0; 9380 flags = CTL_LLF_FUA; 9381 retval = CTL_RETVAL_COMPLETE; 9382 9383 switch (ctsio->cdb[0]) { 9384 case VERIFY_10: { 9385 struct scsi_verify_10 *cdb; 9386 9387 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9388 if (cdb->byte2 & SVFY_BYTCHK) 9389 bytchk = 1; 9390 if (cdb->byte2 & SVFY_DPO) 9391 flags |= CTL_LLF_DPO; 9392 lba = scsi_4btoul(cdb->addr); 9393 num_blocks = scsi_2btoul(cdb->length); 9394 break; 9395 } 9396 case VERIFY_12: { 9397 struct scsi_verify_12 *cdb; 9398 9399 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9400 if (cdb->byte2 & SVFY_BYTCHK) 9401 bytchk = 1; 9402 if (cdb->byte2 & SVFY_DPO) 9403 flags |= CTL_LLF_DPO; 9404 lba = scsi_4btoul(cdb->addr); 9405 num_blocks = scsi_4btoul(cdb->length); 9406 break; 9407 } 9408 case VERIFY_16: { 9409 struct scsi_rw_16 *cdb; 9410 9411 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9412 if (cdb->byte2 & SVFY_BYTCHK) 9413 bytchk = 1; 9414 if (cdb->byte2 & SVFY_DPO) 9415 flags |= CTL_LLF_DPO; 9416 lba = scsi_8btou64(cdb->addr); 9417 num_blocks = scsi_4btoul(cdb->length); 9418 break; 9419 } 9420 default: 9421 /* 9422 * We got a command we don't support. This shouldn't 9423 * happen, commands should be filtered out above us. 9424 */ 9425 ctl_set_invalid_opcode(ctsio); 9426 ctl_done((union ctl_io *)ctsio); 9427 return (CTL_RETVAL_COMPLETE); 9428 } 9429 9430 /* 9431 * The first check is to make sure we're in bounds, the second 9432 * check is to catch wrap-around problems. If the lba + num blocks 9433 * is less than the lba, then we've wrapped around and the block 9434 * range is invalid anyway. 9435 */ 9436 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9437 || ((lba + num_blocks) < lba)) { 9438 ctl_set_lba_out_of_range(ctsio); 9439 ctl_done((union ctl_io *)ctsio); 9440 return (CTL_RETVAL_COMPLETE); 9441 } 9442 9443 /* 9444 * According to SBC-3, a transfer length of 0 is not an error. 9445 */ 9446 if (num_blocks == 0) { 9447 ctl_set_success(ctsio); 9448 ctl_done((union ctl_io *)ctsio); 9449 return (CTL_RETVAL_COMPLETE); 9450 } 9451 9452 lbalen = (struct ctl_lba_len_flags *) 9453 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9454 lbalen->lba = lba; 9455 lbalen->len = num_blocks; 9456 if (bytchk) { 9457 lbalen->flags = CTL_LLF_COMPARE | flags; 9458 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9459 } else { 9460 lbalen->flags = CTL_LLF_VERIFY | flags; 9461 ctsio->kern_total_len = 0; 9462 } 9463 ctsio->kern_rel_offset = 0; 9464 9465 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9466 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9467 return (retval); 9468} 9469 9470int 9471ctl_report_luns(struct ctl_scsiio *ctsio) 9472{ 9473 struct scsi_report_luns *cdb; 9474 struct scsi_report_luns_data *lun_data; 9475 struct ctl_lun *lun, *request_lun; 9476 int num_luns, retval; 9477 uint32_t alloc_len, lun_datalen; 9478 int num_filled, well_known; 9479 uint32_t initidx, targ_lun_id, lun_id; 9480 9481 retval = CTL_RETVAL_COMPLETE; 9482 well_known = 0; 9483 9484 cdb = (struct scsi_report_luns *)ctsio->cdb; 9485 9486 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9487 9488 mtx_lock(&control_softc->ctl_lock); 9489 num_luns = control_softc->num_luns; 9490 mtx_unlock(&control_softc->ctl_lock); 9491 9492 switch (cdb->select_report) { 9493 case RPL_REPORT_DEFAULT: 9494 case RPL_REPORT_ALL: 9495 break; 9496 case RPL_REPORT_WELLKNOWN: 9497 well_known = 1; 9498 num_luns = 0; 9499 break; 9500 default: 9501 ctl_set_invalid_field(ctsio, 9502 /*sks_valid*/ 1, 9503 /*command*/ 1, 9504 /*field*/ 2, 9505 /*bit_valid*/ 0, 9506 /*bit*/ 0); 9507 ctl_done((union ctl_io *)ctsio); 9508 return (retval); 9509 break; /* NOTREACHED */ 9510 } 9511 9512 alloc_len = scsi_4btoul(cdb->length); 9513 /* 9514 * The initiator has to allocate at least 16 bytes for this request, 9515 * so he can at least get the header and the first LUN. Otherwise 9516 * we reject the request (per SPC-3 rev 14, section 6.21). 9517 */ 9518 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9519 sizeof(struct scsi_report_luns_lundata))) { 9520 ctl_set_invalid_field(ctsio, 9521 /*sks_valid*/ 1, 9522 /*command*/ 1, 9523 /*field*/ 6, 9524 /*bit_valid*/ 0, 9525 /*bit*/ 0); 9526 ctl_done((union ctl_io *)ctsio); 9527 return (retval); 9528 } 9529 9530 request_lun = (struct ctl_lun *) 9531 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9532 9533 lun_datalen = sizeof(*lun_data) + 9534 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9535 9536 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9537 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9538 ctsio->kern_sg_entries = 0; 9539 9540 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9541 9542 mtx_lock(&control_softc->ctl_lock); 9543 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9544 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9545 if (lun_id >= CTL_MAX_LUNS) 9546 continue; 9547 lun = control_softc->ctl_luns[lun_id]; 9548 if (lun == NULL) 9549 continue; 9550 9551 if (targ_lun_id <= 0xff) { 9552 /* 9553 * Peripheral addressing method, bus number 0. 9554 */ 9555 lun_data->luns[num_filled].lundata[0] = 9556 RPL_LUNDATA_ATYP_PERIPH; 9557 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9558 num_filled++; 9559 } else if (targ_lun_id <= 0x3fff) { 9560 /* 9561 * Flat addressing method. 9562 */ 9563 lun_data->luns[num_filled].lundata[0] = 9564 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9565 lun_data->luns[num_filled].lundata[1] = 9566 (targ_lun_id & 0xff); 9567 num_filled++; 9568 } else if (targ_lun_id <= 0xffffff) { 9569 /* 9570 * Extended flat addressing method. 9571 */ 9572 lun_data->luns[num_filled].lundata[0] = 9573 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9574 scsi_ulto3b(targ_lun_id, 9575 &lun_data->luns[num_filled].lundata[1]); 9576 num_filled++; 9577 } else { 9578 printf("ctl_report_luns: bogus LUN number %jd, " 9579 "skipping\n", (intmax_t)targ_lun_id); 9580 } 9581 /* 9582 * According to SPC-3, rev 14 section 6.21: 9583 * 9584 * "The execution of a REPORT LUNS command to any valid and 9585 * installed logical unit shall clear the REPORTED LUNS DATA 9586 * HAS CHANGED unit attention condition for all logical 9587 * units of that target with respect to the requesting 9588 * initiator. A valid and installed logical unit is one 9589 * having a PERIPHERAL QUALIFIER of 000b in the standard 9590 * INQUIRY data (see 6.4.2)." 9591 * 9592 * If request_lun is NULL, the LUN this report luns command 9593 * was issued to is either disabled or doesn't exist. In that 9594 * case, we shouldn't clear any pending lun change unit 9595 * attention. 9596 */ 9597 if (request_lun != NULL) { 9598 mtx_lock(&lun->lun_lock); 9599 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9600 mtx_unlock(&lun->lun_lock); 9601 } 9602 } 9603 mtx_unlock(&control_softc->ctl_lock); 9604 9605 /* 9606 * It's quite possible that we've returned fewer LUNs than we allocated 9607 * space for. Trim it. 9608 */ 9609 lun_datalen = sizeof(*lun_data) + 9610 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9611 9612 if (lun_datalen < alloc_len) { 9613 ctsio->residual = alloc_len - lun_datalen; 9614 ctsio->kern_data_len = lun_datalen; 9615 ctsio->kern_total_len = lun_datalen; 9616 } else { 9617 ctsio->residual = 0; 9618 ctsio->kern_data_len = alloc_len; 9619 ctsio->kern_total_len = alloc_len; 9620 } 9621 ctsio->kern_data_resid = 0; 9622 ctsio->kern_rel_offset = 0; 9623 ctsio->kern_sg_entries = 0; 9624 9625 /* 9626 * We set this to the actual data length, regardless of how much 9627 * space we actually have to return results. If the user looks at 9628 * this value, he'll know whether or not he allocated enough space 9629 * and reissue the command if necessary. We don't support well 9630 * known logical units, so if the user asks for that, return none. 9631 */ 9632 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9633 9634 /* 9635 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9636 * this request. 9637 */ 9638 ctsio->scsi_status = SCSI_STATUS_OK; 9639 9640 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9641 ctsio->be_move_done = ctl_config_move_done; 9642 ctl_datamove((union ctl_io *)ctsio); 9643 9644 return (retval); 9645} 9646 9647int 9648ctl_request_sense(struct ctl_scsiio *ctsio) 9649{ 9650 struct scsi_request_sense *cdb; 9651 struct scsi_sense_data *sense_ptr; 9652 struct ctl_lun *lun; 9653 uint32_t initidx; 9654 int have_error; 9655 scsi_sense_data_type sense_format; 9656 9657 cdb = (struct scsi_request_sense *)ctsio->cdb; 9658 9659 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9660 9661 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9662 9663 /* 9664 * Determine which sense format the user wants. 9665 */ 9666 if (cdb->byte2 & SRS_DESC) 9667 sense_format = SSD_TYPE_DESC; 9668 else 9669 sense_format = SSD_TYPE_FIXED; 9670 9671 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9672 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9673 ctsio->kern_sg_entries = 0; 9674 9675 /* 9676 * struct scsi_sense_data, which is currently set to 256 bytes, is 9677 * larger than the largest allowed value for the length field in the 9678 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9679 */ 9680 ctsio->residual = 0; 9681 ctsio->kern_data_len = cdb->length; 9682 ctsio->kern_total_len = cdb->length; 9683 9684 ctsio->kern_data_resid = 0; 9685 ctsio->kern_rel_offset = 0; 9686 ctsio->kern_sg_entries = 0; 9687 9688 /* 9689 * If we don't have a LUN, we don't have any pending sense. 9690 */ 9691 if (lun == NULL) 9692 goto no_sense; 9693 9694 have_error = 0; 9695 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9696 /* 9697 * Check for pending sense, and then for pending unit attentions. 9698 * Pending sense gets returned first, then pending unit attentions. 9699 */ 9700 mtx_lock(&lun->lun_lock); 9701#ifdef CTL_WITH_CA 9702 if (ctl_is_set(lun->have_ca, initidx)) { 9703 scsi_sense_data_type stored_format; 9704 9705 /* 9706 * Check to see which sense format was used for the stored 9707 * sense data. 9708 */ 9709 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9710 9711 /* 9712 * If the user requested a different sense format than the 9713 * one we stored, then we need to convert it to the other 9714 * format. If we're going from descriptor to fixed format 9715 * sense data, we may lose things in translation, depending 9716 * on what options were used. 9717 * 9718 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9719 * for some reason we'll just copy it out as-is. 9720 */ 9721 if ((stored_format == SSD_TYPE_FIXED) 9722 && (sense_format == SSD_TYPE_DESC)) 9723 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9724 &lun->pending_sense[initidx], 9725 (struct scsi_sense_data_desc *)sense_ptr); 9726 else if ((stored_format == SSD_TYPE_DESC) 9727 && (sense_format == SSD_TYPE_FIXED)) 9728 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9729 &lun->pending_sense[initidx], 9730 (struct scsi_sense_data_fixed *)sense_ptr); 9731 else 9732 memcpy(sense_ptr, &lun->pending_sense[initidx], 9733 ctl_min(sizeof(*sense_ptr), 9734 sizeof(lun->pending_sense[initidx]))); 9735 9736 ctl_clear_mask(lun->have_ca, initidx); 9737 have_error = 1; 9738 } else 9739#endif 9740 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9741 ctl_ua_type ua_type; 9742 9743 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9744 sense_ptr, sense_format); 9745 if (ua_type != CTL_UA_NONE) 9746 have_error = 1; 9747 } 9748 mtx_unlock(&lun->lun_lock); 9749 9750 /* 9751 * We already have a pending error, return it. 9752 */ 9753 if (have_error != 0) { 9754 /* 9755 * We report the SCSI status as OK, since the status of the 9756 * request sense command itself is OK. 9757 */ 9758 ctsio->scsi_status = SCSI_STATUS_OK; 9759 9760 /* 9761 * We report 0 for the sense length, because we aren't doing 9762 * autosense in this case. We're reporting sense as 9763 * parameter data. 9764 */ 9765 ctsio->sense_len = 0; 9766 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9767 ctsio->be_move_done = ctl_config_move_done; 9768 ctl_datamove((union ctl_io *)ctsio); 9769 9770 return (CTL_RETVAL_COMPLETE); 9771 } 9772 9773no_sense: 9774 9775 /* 9776 * No sense information to report, so we report that everything is 9777 * okay. 9778 */ 9779 ctl_set_sense_data(sense_ptr, 9780 lun, 9781 sense_format, 9782 /*current_error*/ 1, 9783 /*sense_key*/ SSD_KEY_NO_SENSE, 9784 /*asc*/ 0x00, 9785 /*ascq*/ 0x00, 9786 SSD_ELEM_NONE); 9787 9788 ctsio->scsi_status = SCSI_STATUS_OK; 9789 9790 /* 9791 * We report 0 for the sense length, because we aren't doing 9792 * autosense in this case. We're reporting sense as parameter data. 9793 */ 9794 ctsio->sense_len = 0; 9795 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9796 ctsio->be_move_done = ctl_config_move_done; 9797 ctl_datamove((union ctl_io *)ctsio); 9798 9799 return (CTL_RETVAL_COMPLETE); 9800} 9801 9802int 9803ctl_tur(struct ctl_scsiio *ctsio) 9804{ 9805 9806 CTL_DEBUG_PRINT(("ctl_tur\n")); 9807 9808 ctsio->scsi_status = SCSI_STATUS_OK; 9809 ctsio->io_hdr.status = CTL_SUCCESS; 9810 9811 ctl_done((union ctl_io *)ctsio); 9812 9813 return (CTL_RETVAL_COMPLETE); 9814} 9815 9816#ifdef notyet 9817static int 9818ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9819{ 9820 9821} 9822#endif 9823 9824static int 9825ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9826{ 9827 struct scsi_vpd_supported_pages *pages; 9828 int sup_page_size; 9829 struct ctl_lun *lun; 9830 9831 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9832 9833 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9834 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9835 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9836 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9837 ctsio->kern_sg_entries = 0; 9838 9839 if (sup_page_size < alloc_len) { 9840 ctsio->residual = alloc_len - sup_page_size; 9841 ctsio->kern_data_len = sup_page_size; 9842 ctsio->kern_total_len = sup_page_size; 9843 } else { 9844 ctsio->residual = 0; 9845 ctsio->kern_data_len = alloc_len; 9846 ctsio->kern_total_len = alloc_len; 9847 } 9848 ctsio->kern_data_resid = 0; 9849 ctsio->kern_rel_offset = 0; 9850 ctsio->kern_sg_entries = 0; 9851 9852 /* 9853 * The control device is always connected. The disk device, on the 9854 * other hand, may not be online all the time. Need to change this 9855 * to figure out whether the disk device is actually online or not. 9856 */ 9857 if (lun != NULL) 9858 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9859 lun->be_lun->lun_type; 9860 else 9861 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9862 9863 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9864 /* Supported VPD pages */ 9865 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9866 /* Serial Number */ 9867 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9868 /* Device Identification */ 9869 pages->page_list[2] = SVPD_DEVICE_ID; 9870 /* Extended INQUIRY Data */ 9871 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9872 /* Mode Page Policy */ 9873 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9874 /* SCSI Ports */ 9875 pages->page_list[5] = SVPD_SCSI_PORTS; 9876 /* Third-party Copy */ 9877 pages->page_list[6] = SVPD_SCSI_TPC; 9878 /* Block limits */ 9879 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9880 /* Block Device Characteristics */ 9881 pages->page_list[8] = SVPD_BDC; 9882 /* Logical Block Provisioning */ 9883 pages->page_list[9] = SVPD_LBP; 9884 9885 ctsio->scsi_status = SCSI_STATUS_OK; 9886 9887 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9888 ctsio->be_move_done = ctl_config_move_done; 9889 ctl_datamove((union ctl_io *)ctsio); 9890 9891 return (CTL_RETVAL_COMPLETE); 9892} 9893 9894static int 9895ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9896{ 9897 struct scsi_vpd_unit_serial_number *sn_ptr; 9898 struct ctl_lun *lun; 9899 int data_len; 9900 9901 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9902 9903 data_len = 4 + CTL_SN_LEN; 9904 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9905 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9906 if (data_len < alloc_len) { 9907 ctsio->residual = alloc_len - data_len; 9908 ctsio->kern_data_len = data_len; 9909 ctsio->kern_total_len = data_len; 9910 } else { 9911 ctsio->residual = 0; 9912 ctsio->kern_data_len = alloc_len; 9913 ctsio->kern_total_len = alloc_len; 9914 } 9915 ctsio->kern_data_resid = 0; 9916 ctsio->kern_rel_offset = 0; 9917 ctsio->kern_sg_entries = 0; 9918 9919 /* 9920 * The control device is always connected. The disk device, on the 9921 * other hand, may not be online all the time. Need to change this 9922 * to figure out whether the disk device is actually online or not. 9923 */ 9924 if (lun != NULL) 9925 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9926 lun->be_lun->lun_type; 9927 else 9928 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9929 9930 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9931 sn_ptr->length = CTL_SN_LEN; 9932 /* 9933 * If we don't have a LUN, we just leave the serial number as 9934 * all spaces. 9935 */ 9936 if (lun != NULL) { 9937 strncpy((char *)sn_ptr->serial_num, 9938 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9939 } else 9940 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9941 ctsio->scsi_status = SCSI_STATUS_OK; 9942 9943 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9944 ctsio->be_move_done = ctl_config_move_done; 9945 ctl_datamove((union ctl_io *)ctsio); 9946 9947 return (CTL_RETVAL_COMPLETE); 9948} 9949 9950 9951static int 9952ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9953{ 9954 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9955 struct ctl_lun *lun; 9956 int data_len; 9957 9958 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9959 9960 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9961 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9962 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9963 ctsio->kern_sg_entries = 0; 9964 9965 if (data_len < alloc_len) { 9966 ctsio->residual = alloc_len - data_len; 9967 ctsio->kern_data_len = data_len; 9968 ctsio->kern_total_len = data_len; 9969 } else { 9970 ctsio->residual = 0; 9971 ctsio->kern_data_len = alloc_len; 9972 ctsio->kern_total_len = alloc_len; 9973 } 9974 ctsio->kern_data_resid = 0; 9975 ctsio->kern_rel_offset = 0; 9976 ctsio->kern_sg_entries = 0; 9977 9978 /* 9979 * The control device is always connected. The disk device, on the 9980 * other hand, may not be online all the time. 9981 */ 9982 if (lun != NULL) 9983 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9984 lun->be_lun->lun_type; 9985 else 9986 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9987 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9988 eid_ptr->page_length = data_len - 4; 9989 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9990 eid_ptr->flags3 = SVPD_EID_V_SUP; 9991 9992 ctsio->scsi_status = SCSI_STATUS_OK; 9993 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9994 ctsio->be_move_done = ctl_config_move_done; 9995 ctl_datamove((union ctl_io *)ctsio); 9996 9997 return (CTL_RETVAL_COMPLETE); 9998} 9999 10000static int 10001ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 10002{ 10003 struct scsi_vpd_mode_page_policy *mpp_ptr; 10004 struct ctl_lun *lun; 10005 int data_len; 10006 10007 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10008 10009 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 10010 sizeof(struct scsi_vpd_mode_page_policy_descr); 10011 10012 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10013 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 10014 ctsio->kern_sg_entries = 0; 10015 10016 if (data_len < alloc_len) { 10017 ctsio->residual = alloc_len - data_len; 10018 ctsio->kern_data_len = data_len; 10019 ctsio->kern_total_len = data_len; 10020 } else { 10021 ctsio->residual = 0; 10022 ctsio->kern_data_len = alloc_len; 10023 ctsio->kern_total_len = alloc_len; 10024 } 10025 ctsio->kern_data_resid = 0; 10026 ctsio->kern_rel_offset = 0; 10027 ctsio->kern_sg_entries = 0; 10028 10029 /* 10030 * The control device is always connected. The disk device, on the 10031 * other hand, may not be online all the time. 10032 */ 10033 if (lun != NULL) 10034 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10035 lun->be_lun->lun_type; 10036 else 10037 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10038 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 10039 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 10040 mpp_ptr->descr[0].page_code = 0x3f; 10041 mpp_ptr->descr[0].subpage_code = 0xff; 10042 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 10043 10044 ctsio->scsi_status = SCSI_STATUS_OK; 10045 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10046 ctsio->be_move_done = ctl_config_move_done; 10047 ctl_datamove((union ctl_io *)ctsio); 10048 10049 return (CTL_RETVAL_COMPLETE); 10050} 10051 10052static int 10053ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 10054{ 10055 struct scsi_vpd_device_id *devid_ptr; 10056 struct scsi_vpd_id_descriptor *desc; 10057 struct ctl_softc *ctl_softc; 10058 struct ctl_lun *lun; 10059 struct ctl_port *port; 10060 int data_len; 10061 uint8_t proto; 10062 10063 ctl_softc = control_softc; 10064 10065 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 10066 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10067 10068 data_len = sizeof(struct scsi_vpd_device_id) + 10069 sizeof(struct scsi_vpd_id_descriptor) + 10070 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 10071 sizeof(struct scsi_vpd_id_descriptor) + 10072 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 10073 if (lun && lun->lun_devid) 10074 data_len += lun->lun_devid->len; 10075 if (port->port_devid) 10076 data_len += port->port_devid->len; 10077 if (port->target_devid) 10078 data_len += port->target_devid->len; 10079 10080 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10081 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 10082 ctsio->kern_sg_entries = 0; 10083 10084 if (data_len < alloc_len) { 10085 ctsio->residual = alloc_len - data_len; 10086 ctsio->kern_data_len = data_len; 10087 ctsio->kern_total_len = data_len; 10088 } else { 10089 ctsio->residual = 0; 10090 ctsio->kern_data_len = alloc_len; 10091 ctsio->kern_total_len = alloc_len; 10092 } 10093 ctsio->kern_data_resid = 0; 10094 ctsio->kern_rel_offset = 0; 10095 ctsio->kern_sg_entries = 0; 10096 10097 /* 10098 * The control device is always connected. The disk device, on the 10099 * other hand, may not be online all the time. 10100 */ 10101 if (lun != NULL) 10102 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10103 lun->be_lun->lun_type; 10104 else 10105 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10106 devid_ptr->page_code = SVPD_DEVICE_ID; 10107 scsi_ulto2b(data_len - 4, devid_ptr->length); 10108 10109 if (port->port_type == CTL_PORT_FC) 10110 proto = SCSI_PROTO_FC << 4; 10111 else if (port->port_type == CTL_PORT_ISCSI) 10112 proto = SCSI_PROTO_ISCSI << 4; 10113 else 10114 proto = SCSI_PROTO_SPI << 4; 10115 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 10116 10117 /* 10118 * We're using a LUN association here. i.e., this device ID is a 10119 * per-LUN identifier. 10120 */ 10121 if (lun && lun->lun_devid) { 10122 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 10123 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10124 lun->lun_devid->len); 10125 } 10126 10127 /* 10128 * This is for the WWPN which is a port association. 10129 */ 10130 if (port->port_devid) { 10131 memcpy(desc, port->port_devid->data, port->port_devid->len); 10132 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10133 port->port_devid->len); 10134 } 10135 10136 /* 10137 * This is for the Relative Target Port(type 4h) identifier 10138 */ 10139 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10140 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10141 SVPD_ID_TYPE_RELTARG; 10142 desc->length = 4; 10143 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 10144 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10145 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 10146 10147 /* 10148 * This is for the Target Port Group(type 5h) identifier 10149 */ 10150 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10151 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10152 SVPD_ID_TYPE_TPORTGRP; 10153 desc->length = 4; 10154 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10155 &desc->identifier[2]); 10156 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10157 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10158 10159 /* 10160 * This is for the Target identifier 10161 */ 10162 if (port->target_devid) { 10163 memcpy(desc, port->target_devid->data, port->target_devid->len); 10164 } 10165 10166 ctsio->scsi_status = SCSI_STATUS_OK; 10167 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10168 ctsio->be_move_done = ctl_config_move_done; 10169 ctl_datamove((union ctl_io *)ctsio); 10170 10171 return (CTL_RETVAL_COMPLETE); 10172} 10173 10174static int 10175ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10176{ 10177 struct ctl_softc *softc = control_softc; 10178 struct scsi_vpd_scsi_ports *sp; 10179 struct scsi_vpd_port_designation *pd; 10180 struct scsi_vpd_port_designation_cont *pdc; 10181 struct ctl_lun *lun; 10182 struct ctl_port *port; 10183 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10184 int num_target_port_groups, single; 10185 10186 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10187 10188 single = ctl_is_single; 10189 if (single) 10190 num_target_port_groups = 1; 10191 else 10192 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10193 num_target_ports = 0; 10194 iid_len = 0; 10195 id_len = 0; 10196 mtx_lock(&softc->ctl_lock); 10197 STAILQ_FOREACH(port, &softc->port_list, links) { 10198 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10199 continue; 10200 if (lun != NULL && 10201 ctl_map_lun_back(port->targ_port, lun->lun) >= 10202 CTL_MAX_LUNS) 10203 continue; 10204 num_target_ports++; 10205 if (port->init_devid) 10206 iid_len += port->init_devid->len; 10207 if (port->port_devid) 10208 id_len += port->port_devid->len; 10209 } 10210 mtx_unlock(&softc->ctl_lock); 10211 10212 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10213 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10214 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10215 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10216 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10217 ctsio->kern_sg_entries = 0; 10218 10219 if (data_len < alloc_len) { 10220 ctsio->residual = alloc_len - data_len; 10221 ctsio->kern_data_len = data_len; 10222 ctsio->kern_total_len = data_len; 10223 } else { 10224 ctsio->residual = 0; 10225 ctsio->kern_data_len = alloc_len; 10226 ctsio->kern_total_len = alloc_len; 10227 } 10228 ctsio->kern_data_resid = 0; 10229 ctsio->kern_rel_offset = 0; 10230 ctsio->kern_sg_entries = 0; 10231 10232 /* 10233 * The control device is always connected. The disk device, on the 10234 * other hand, may not be online all the time. Need to change this 10235 * to figure out whether the disk device is actually online or not. 10236 */ 10237 if (lun != NULL) 10238 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10239 lun->be_lun->lun_type; 10240 else 10241 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10242 10243 sp->page_code = SVPD_SCSI_PORTS; 10244 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10245 sp->page_length); 10246 pd = &sp->design[0]; 10247 10248 mtx_lock(&softc->ctl_lock); 10249 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10250 pg = 0; 10251 else 10252 pg = 1; 10253 for (g = 0; g < num_target_port_groups; g++) { 10254 STAILQ_FOREACH(port, &softc->port_list, links) { 10255 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10256 continue; 10257 if (lun != NULL && 10258 ctl_map_lun_back(port->targ_port, lun->lun) >= 10259 CTL_MAX_LUNS) 10260 continue; 10261 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10262 scsi_ulto2b(p, pd->relative_port_id); 10263 if (port->init_devid && g == pg) { 10264 iid_len = port->init_devid->len; 10265 memcpy(pd->initiator_transportid, 10266 port->init_devid->data, port->init_devid->len); 10267 } else 10268 iid_len = 0; 10269 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10270 pdc = (struct scsi_vpd_port_designation_cont *) 10271 (&pd->initiator_transportid[iid_len]); 10272 if (port->port_devid && g == pg) { 10273 id_len = port->port_devid->len; 10274 memcpy(pdc->target_port_descriptors, 10275 port->port_devid->data, port->port_devid->len); 10276 } else 10277 id_len = 0; 10278 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10279 pd = (struct scsi_vpd_port_designation *) 10280 ((uint8_t *)pdc->target_port_descriptors + id_len); 10281 } 10282 } 10283 mtx_unlock(&softc->ctl_lock); 10284 10285 ctsio->scsi_status = SCSI_STATUS_OK; 10286 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10287 ctsio->be_move_done = ctl_config_move_done; 10288 ctl_datamove((union ctl_io *)ctsio); 10289 10290 return (CTL_RETVAL_COMPLETE); 10291} 10292 10293static int 10294ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10295{ 10296 struct scsi_vpd_block_limits *bl_ptr; 10297 struct ctl_lun *lun; 10298 int bs; 10299 10300 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10301 10302 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10303 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10304 ctsio->kern_sg_entries = 0; 10305 10306 if (sizeof(*bl_ptr) < alloc_len) { 10307 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10308 ctsio->kern_data_len = sizeof(*bl_ptr); 10309 ctsio->kern_total_len = sizeof(*bl_ptr); 10310 } else { 10311 ctsio->residual = 0; 10312 ctsio->kern_data_len = alloc_len; 10313 ctsio->kern_total_len = alloc_len; 10314 } 10315 ctsio->kern_data_resid = 0; 10316 ctsio->kern_rel_offset = 0; 10317 ctsio->kern_sg_entries = 0; 10318 10319 /* 10320 * The control device is always connected. The disk device, on the 10321 * other hand, may not be online all the time. Need to change this 10322 * to figure out whether the disk device is actually online or not. 10323 */ 10324 if (lun != NULL) 10325 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10326 lun->be_lun->lun_type; 10327 else 10328 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10329 10330 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10331 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10332 bl_ptr->max_cmp_write_len = 0xff; 10333 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10334 if (lun != NULL) { 10335 bs = lun->be_lun->blocksize; 10336 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10337 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10338 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10339 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10340 if (lun->be_lun->pblockexp != 0) { 10341 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10342 bl_ptr->opt_unmap_grain); 10343 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10344 bl_ptr->unmap_grain_align); 10345 } 10346 } 10347 scsi_ulto4b(lun->be_lun->atomicblock, 10348 bl_ptr->max_atomic_transfer_length); 10349 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10350 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10351 } 10352 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10353 10354 ctsio->scsi_status = SCSI_STATUS_OK; 10355 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10356 ctsio->be_move_done = ctl_config_move_done; 10357 ctl_datamove((union ctl_io *)ctsio); 10358 10359 return (CTL_RETVAL_COMPLETE); 10360} 10361 10362static int 10363ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10364{ 10365 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10366 struct ctl_lun *lun; 10367 const char *value; 10368 u_int i; 10369 10370 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10371 10372 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10373 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10374 ctsio->kern_sg_entries = 0; 10375 10376 if (sizeof(*bdc_ptr) < alloc_len) { 10377 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10378 ctsio->kern_data_len = sizeof(*bdc_ptr); 10379 ctsio->kern_total_len = sizeof(*bdc_ptr); 10380 } else { 10381 ctsio->residual = 0; 10382 ctsio->kern_data_len = alloc_len; 10383 ctsio->kern_total_len = alloc_len; 10384 } 10385 ctsio->kern_data_resid = 0; 10386 ctsio->kern_rel_offset = 0; 10387 ctsio->kern_sg_entries = 0; 10388 10389 /* 10390 * The control device is always connected. The disk device, on the 10391 * other hand, may not be online all the time. Need to change this 10392 * to figure out whether the disk device is actually online or not. 10393 */ 10394 if (lun != NULL) 10395 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10396 lun->be_lun->lun_type; 10397 else 10398 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10399 bdc_ptr->page_code = SVPD_BDC; 10400 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10401 if (lun != NULL && 10402 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10403 i = strtol(value, NULL, 0); 10404 else 10405 i = CTL_DEFAULT_ROTATION_RATE; 10406 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10407 if (lun != NULL && 10408 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10409 i = strtol(value, NULL, 0); 10410 else 10411 i = 0; 10412 bdc_ptr->wab_wac_ff = (i & 0x0f); 10413 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10414 10415 ctsio->scsi_status = SCSI_STATUS_OK; 10416 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10417 ctsio->be_move_done = ctl_config_move_done; 10418 ctl_datamove((union ctl_io *)ctsio); 10419 10420 return (CTL_RETVAL_COMPLETE); 10421} 10422 10423static int 10424ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10425{ 10426 struct scsi_vpd_logical_block_prov *lbp_ptr; 10427 struct ctl_lun *lun; 10428 10429 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10430 10431 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10432 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10433 ctsio->kern_sg_entries = 0; 10434 10435 if (sizeof(*lbp_ptr) < alloc_len) { 10436 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10437 ctsio->kern_data_len = sizeof(*lbp_ptr); 10438 ctsio->kern_total_len = sizeof(*lbp_ptr); 10439 } else { 10440 ctsio->residual = 0; 10441 ctsio->kern_data_len = alloc_len; 10442 ctsio->kern_total_len = alloc_len; 10443 } 10444 ctsio->kern_data_resid = 0; 10445 ctsio->kern_rel_offset = 0; 10446 ctsio->kern_sg_entries = 0; 10447 10448 /* 10449 * The control device is always connected. The disk device, on the 10450 * other hand, may not be online all the time. Need to change this 10451 * to figure out whether the disk device is actually online or not. 10452 */ 10453 if (lun != NULL) 10454 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10455 lun->be_lun->lun_type; 10456 else 10457 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10458 10459 lbp_ptr->page_code = SVPD_LBP; 10460 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10461 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10462 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10463 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10464 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10465 lbp_ptr->prov_type = SVPD_LBP_THIN; 10466 } 10467 10468 ctsio->scsi_status = SCSI_STATUS_OK; 10469 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10470 ctsio->be_move_done = ctl_config_move_done; 10471 ctl_datamove((union ctl_io *)ctsio); 10472 10473 return (CTL_RETVAL_COMPLETE); 10474} 10475 10476static int 10477ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10478{ 10479 struct scsi_inquiry *cdb; 10480 int alloc_len, retval; 10481 10482 cdb = (struct scsi_inquiry *)ctsio->cdb; 10483 10484 retval = CTL_RETVAL_COMPLETE; 10485 10486 alloc_len = scsi_2btoul(cdb->length); 10487 10488 switch (cdb->page_code) { 10489 case SVPD_SUPPORTED_PAGES: 10490 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10491 break; 10492 case SVPD_UNIT_SERIAL_NUMBER: 10493 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10494 break; 10495 case SVPD_DEVICE_ID: 10496 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10497 break; 10498 case SVPD_EXTENDED_INQUIRY_DATA: 10499 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10500 break; 10501 case SVPD_MODE_PAGE_POLICY: 10502 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10503 break; 10504 case SVPD_SCSI_PORTS: 10505 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10506 break; 10507 case SVPD_SCSI_TPC: 10508 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10509 break; 10510 case SVPD_BLOCK_LIMITS: 10511 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10512 break; 10513 case SVPD_BDC: 10514 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10515 break; 10516 case SVPD_LBP: 10517 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10518 break; 10519 default: 10520 ctl_set_invalid_field(ctsio, 10521 /*sks_valid*/ 1, 10522 /*command*/ 1, 10523 /*field*/ 2, 10524 /*bit_valid*/ 0, 10525 /*bit*/ 0); 10526 ctl_done((union ctl_io *)ctsio); 10527 retval = CTL_RETVAL_COMPLETE; 10528 break; 10529 } 10530 10531 return (retval); 10532} 10533 10534static int 10535ctl_inquiry_std(struct ctl_scsiio *ctsio) 10536{ 10537 struct scsi_inquiry_data *inq_ptr; 10538 struct scsi_inquiry *cdb; 10539 struct ctl_softc *ctl_softc; 10540 struct ctl_lun *lun; 10541 char *val; 10542 uint32_t alloc_len, data_len; 10543 ctl_port_type port_type; 10544 10545 ctl_softc = control_softc; 10546 10547 /* 10548 * Figure out whether we're talking to a Fibre Channel port or not. 10549 * We treat the ioctl front end, and any SCSI adapters, as packetized 10550 * SCSI front ends. 10551 */ 10552 port_type = ctl_softc->ctl_ports[ 10553 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10554 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10555 port_type = CTL_PORT_SCSI; 10556 10557 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10558 cdb = (struct scsi_inquiry *)ctsio->cdb; 10559 alloc_len = scsi_2btoul(cdb->length); 10560 10561 /* 10562 * We malloc the full inquiry data size here and fill it 10563 * in. If the user only asks for less, we'll give him 10564 * that much. 10565 */ 10566 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10567 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10568 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10569 ctsio->kern_sg_entries = 0; 10570 ctsio->kern_data_resid = 0; 10571 ctsio->kern_rel_offset = 0; 10572 10573 if (data_len < alloc_len) { 10574 ctsio->residual = alloc_len - data_len; 10575 ctsio->kern_data_len = data_len; 10576 ctsio->kern_total_len = data_len; 10577 } else { 10578 ctsio->residual = 0; 10579 ctsio->kern_data_len = alloc_len; 10580 ctsio->kern_total_len = alloc_len; 10581 } 10582 10583 /* 10584 * If we have a LUN configured, report it as connected. Otherwise, 10585 * report that it is offline or no device is supported, depending 10586 * on the value of inquiry_pq_no_lun. 10587 * 10588 * According to the spec (SPC-4 r34), the peripheral qualifier 10589 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10590 * 10591 * "A peripheral device having the specified peripheral device type 10592 * is not connected to this logical unit. However, the device 10593 * server is capable of supporting the specified peripheral device 10594 * type on this logical unit." 10595 * 10596 * According to the same spec, the peripheral qualifier 10597 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10598 * 10599 * "The device server is not capable of supporting a peripheral 10600 * device on this logical unit. For this peripheral qualifier the 10601 * peripheral device type shall be set to 1Fh. All other peripheral 10602 * device type values are reserved for this peripheral qualifier." 10603 * 10604 * Given the text, it would seem that we probably want to report that 10605 * the LUN is offline here. There is no LUN connected, but we can 10606 * support a LUN at the given LUN number. 10607 * 10608 * In the real world, though, it sounds like things are a little 10609 * different: 10610 * 10611 * - Linux, when presented with a LUN with the offline peripheral 10612 * qualifier, will create an sg driver instance for it. So when 10613 * you attach it to CTL, you wind up with a ton of sg driver 10614 * instances. (One for every LUN that Linux bothered to probe.) 10615 * Linux does this despite the fact that it issues a REPORT LUNs 10616 * to LUN 0 to get the inventory of supported LUNs. 10617 * 10618 * - There is other anecdotal evidence (from Emulex folks) about 10619 * arrays that use the offline peripheral qualifier for LUNs that 10620 * are on the "passive" path in an active/passive array. 10621 * 10622 * So the solution is provide a hopefully reasonable default 10623 * (return bad/no LUN) and allow the user to change the behavior 10624 * with a tunable/sysctl variable. 10625 */ 10626 if (lun != NULL) 10627 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10628 lun->be_lun->lun_type; 10629 else if (ctl_softc->inquiry_pq_no_lun == 0) 10630 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10631 else 10632 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10633 10634 /* RMB in byte 2 is 0 */ 10635 inq_ptr->version = SCSI_REV_SPC4; 10636 10637 /* 10638 * According to SAM-3, even if a device only supports a single 10639 * level of LUN addressing, it should still set the HISUP bit: 10640 * 10641 * 4.9.1 Logical unit numbers overview 10642 * 10643 * All logical unit number formats described in this standard are 10644 * hierarchical in structure even when only a single level in that 10645 * hierarchy is used. The HISUP bit shall be set to one in the 10646 * standard INQUIRY data (see SPC-2) when any logical unit number 10647 * format described in this standard is used. Non-hierarchical 10648 * formats are outside the scope of this standard. 10649 * 10650 * Therefore we set the HiSup bit here. 10651 * 10652 * The reponse format is 2, per SPC-3. 10653 */ 10654 inq_ptr->response_format = SID_HiSup | 2; 10655 10656 inq_ptr->additional_length = data_len - 10657 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10658 CTL_DEBUG_PRINT(("additional_length = %d\n", 10659 inq_ptr->additional_length)); 10660 10661 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10662 /* 16 bit addressing */ 10663 if (port_type == CTL_PORT_SCSI) 10664 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10665 /* XXX set the SID_MultiP bit here if we're actually going to 10666 respond on multiple ports */ 10667 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10668 10669 /* 16 bit data bus, synchronous transfers */ 10670 if (port_type == CTL_PORT_SCSI) 10671 inq_ptr->flags = SID_WBus16 | SID_Sync; 10672 /* 10673 * XXX KDM do we want to support tagged queueing on the control 10674 * device at all? 10675 */ 10676 if ((lun == NULL) 10677 || (lun->be_lun->lun_type != T_PROCESSOR)) 10678 inq_ptr->flags |= SID_CmdQue; 10679 /* 10680 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10681 * We have 8 bytes for the vendor name, and 16 bytes for the device 10682 * name and 4 bytes for the revision. 10683 */ 10684 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10685 "vendor")) == NULL) { 10686 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10687 } else { 10688 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10689 strncpy(inq_ptr->vendor, val, 10690 min(sizeof(inq_ptr->vendor), strlen(val))); 10691 } 10692 if (lun == NULL) { 10693 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10694 sizeof(inq_ptr->product)); 10695 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10696 switch (lun->be_lun->lun_type) { 10697 case T_DIRECT: 10698 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10699 sizeof(inq_ptr->product)); 10700 break; 10701 case T_PROCESSOR: 10702 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10703 sizeof(inq_ptr->product)); 10704 break; 10705 default: 10706 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10707 sizeof(inq_ptr->product)); 10708 break; 10709 } 10710 } else { 10711 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10712 strncpy(inq_ptr->product, val, 10713 min(sizeof(inq_ptr->product), strlen(val))); 10714 } 10715 10716 /* 10717 * XXX make this a macro somewhere so it automatically gets 10718 * incremented when we make changes. 10719 */ 10720 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10721 "revision")) == NULL) { 10722 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10723 } else { 10724 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10725 strncpy(inq_ptr->revision, val, 10726 min(sizeof(inq_ptr->revision), strlen(val))); 10727 } 10728 10729 /* 10730 * For parallel SCSI, we support double transition and single 10731 * transition clocking. We also support QAS (Quick Arbitration 10732 * and Selection) and Information Unit transfers on both the 10733 * control and array devices. 10734 */ 10735 if (port_type == CTL_PORT_SCSI) 10736 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10737 SID_SPI_IUS; 10738 10739 /* SAM-5 (no version claimed) */ 10740 scsi_ulto2b(0x00A0, inq_ptr->version1); 10741 /* SPC-4 (no version claimed) */ 10742 scsi_ulto2b(0x0460, inq_ptr->version2); 10743 if (port_type == CTL_PORT_FC) { 10744 /* FCP-2 ANSI INCITS.350:2003 */ 10745 scsi_ulto2b(0x0917, inq_ptr->version3); 10746 } else if (port_type == CTL_PORT_SCSI) { 10747 /* SPI-4 ANSI INCITS.362:200x */ 10748 scsi_ulto2b(0x0B56, inq_ptr->version3); 10749 } else if (port_type == CTL_PORT_ISCSI) { 10750 /* iSCSI (no version claimed) */ 10751 scsi_ulto2b(0x0960, inq_ptr->version3); 10752 } else if (port_type == CTL_PORT_SAS) { 10753 /* SAS (no version claimed) */ 10754 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10755 } 10756 10757 if (lun == NULL) { 10758 /* SBC-4 (no version claimed) */ 10759 scsi_ulto2b(0x0600, inq_ptr->version4); 10760 } else { 10761 switch (lun->be_lun->lun_type) { 10762 case T_DIRECT: 10763 /* SBC-4 (no version claimed) */ 10764 scsi_ulto2b(0x0600, inq_ptr->version4); 10765 break; 10766 case T_PROCESSOR: 10767 default: 10768 break; 10769 } 10770 } 10771 10772 ctsio->scsi_status = SCSI_STATUS_OK; 10773 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10774 ctsio->be_move_done = ctl_config_move_done; 10775 ctl_datamove((union ctl_io *)ctsio); 10776 return (CTL_RETVAL_COMPLETE); 10777} 10778 10779int 10780ctl_inquiry(struct ctl_scsiio *ctsio) 10781{ 10782 struct scsi_inquiry *cdb; 10783 int retval; 10784 10785 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10786 10787 cdb = (struct scsi_inquiry *)ctsio->cdb; 10788 if (cdb->byte2 & SI_EVPD) 10789 retval = ctl_inquiry_evpd(ctsio); 10790 else if (cdb->page_code == 0) 10791 retval = ctl_inquiry_std(ctsio); 10792 else { 10793 ctl_set_invalid_field(ctsio, 10794 /*sks_valid*/ 1, 10795 /*command*/ 1, 10796 /*field*/ 2, 10797 /*bit_valid*/ 0, 10798 /*bit*/ 0); 10799 ctl_done((union ctl_io *)ctsio); 10800 return (CTL_RETVAL_COMPLETE); 10801 } 10802 10803 return (retval); 10804} 10805 10806/* 10807 * For known CDB types, parse the LBA and length. 10808 */ 10809static int 10810ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10811{ 10812 if (io->io_hdr.io_type != CTL_IO_SCSI) 10813 return (1); 10814 10815 switch (io->scsiio.cdb[0]) { 10816 case COMPARE_AND_WRITE: { 10817 struct scsi_compare_and_write *cdb; 10818 10819 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10820 10821 *lba = scsi_8btou64(cdb->addr); 10822 *len = cdb->length; 10823 break; 10824 } 10825 case READ_6: 10826 case WRITE_6: { 10827 struct scsi_rw_6 *cdb; 10828 10829 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10830 10831 *lba = scsi_3btoul(cdb->addr); 10832 /* only 5 bits are valid in the most significant address byte */ 10833 *lba &= 0x1fffff; 10834 *len = cdb->length; 10835 break; 10836 } 10837 case READ_10: 10838 case WRITE_10: { 10839 struct scsi_rw_10 *cdb; 10840 10841 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10842 10843 *lba = scsi_4btoul(cdb->addr); 10844 *len = scsi_2btoul(cdb->length); 10845 break; 10846 } 10847 case WRITE_VERIFY_10: { 10848 struct scsi_write_verify_10 *cdb; 10849 10850 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10851 10852 *lba = scsi_4btoul(cdb->addr); 10853 *len = scsi_2btoul(cdb->length); 10854 break; 10855 } 10856 case READ_12: 10857 case WRITE_12: { 10858 struct scsi_rw_12 *cdb; 10859 10860 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10861 10862 *lba = scsi_4btoul(cdb->addr); 10863 *len = scsi_4btoul(cdb->length); 10864 break; 10865 } 10866 case WRITE_VERIFY_12: { 10867 struct scsi_write_verify_12 *cdb; 10868 10869 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10870 10871 *lba = scsi_4btoul(cdb->addr); 10872 *len = scsi_4btoul(cdb->length); 10873 break; 10874 } 10875 case READ_16: 10876 case WRITE_16: 10877 case WRITE_ATOMIC_16: { 10878 struct scsi_rw_16 *cdb; 10879 10880 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10881 10882 *lba = scsi_8btou64(cdb->addr); 10883 *len = scsi_4btoul(cdb->length); 10884 break; 10885 } 10886 case WRITE_VERIFY_16: { 10887 struct scsi_write_verify_16 *cdb; 10888 10889 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10890 10891 *lba = scsi_8btou64(cdb->addr); 10892 *len = scsi_4btoul(cdb->length); 10893 break; 10894 } 10895 case WRITE_SAME_10: { 10896 struct scsi_write_same_10 *cdb; 10897 10898 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10899 10900 *lba = scsi_4btoul(cdb->addr); 10901 *len = scsi_2btoul(cdb->length); 10902 break; 10903 } 10904 case WRITE_SAME_16: { 10905 struct scsi_write_same_16 *cdb; 10906 10907 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10908 10909 *lba = scsi_8btou64(cdb->addr); 10910 *len = scsi_4btoul(cdb->length); 10911 break; 10912 } 10913 case VERIFY_10: { 10914 struct scsi_verify_10 *cdb; 10915 10916 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10917 10918 *lba = scsi_4btoul(cdb->addr); 10919 *len = scsi_2btoul(cdb->length); 10920 break; 10921 } 10922 case VERIFY_12: { 10923 struct scsi_verify_12 *cdb; 10924 10925 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10926 10927 *lba = scsi_4btoul(cdb->addr); 10928 *len = scsi_4btoul(cdb->length); 10929 break; 10930 } 10931 case VERIFY_16: { 10932 struct scsi_verify_16 *cdb; 10933 10934 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10935 10936 *lba = scsi_8btou64(cdb->addr); 10937 *len = scsi_4btoul(cdb->length); 10938 break; 10939 } 10940 case UNMAP: { 10941 *lba = 0; 10942 *len = UINT64_MAX; 10943 break; 10944 } 10945 default: 10946 return (1); 10947 break; /* NOTREACHED */ 10948 } 10949 10950 return (0); 10951} 10952 10953static ctl_action 10954ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10955{ 10956 uint64_t endlba1, endlba2; 10957 10958 endlba1 = lba1 + len1 - 1; 10959 endlba2 = lba2 + len2 - 1; 10960 10961 if ((endlba1 < lba2) 10962 || (endlba2 < lba1)) 10963 return (CTL_ACTION_PASS); 10964 else 10965 return (CTL_ACTION_BLOCK); 10966} 10967 10968static int 10969ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10970{ 10971 struct ctl_ptr_len_flags *ptrlen; 10972 struct scsi_unmap_desc *buf, *end, *range; 10973 uint64_t lba; 10974 uint32_t len; 10975 10976 /* If not UNMAP -- go other way. */ 10977 if (io->io_hdr.io_type != CTL_IO_SCSI || 10978 io->scsiio.cdb[0] != UNMAP) 10979 return (CTL_ACTION_ERROR); 10980 10981 /* If UNMAP without data -- block and wait for data. */ 10982 ptrlen = (struct ctl_ptr_len_flags *) 10983 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10984 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10985 ptrlen->ptr == NULL) 10986 return (CTL_ACTION_BLOCK); 10987 10988 /* UNMAP with data -- check for collision. */ 10989 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10990 end = buf + ptrlen->len / sizeof(*buf); 10991 for (range = buf; range < end; range++) { 10992 lba = scsi_8btou64(range->lba); 10993 len = scsi_4btoul(range->length); 10994 if ((lba < lba2 + len2) && (lba + len > lba2)) 10995 return (CTL_ACTION_BLOCK); 10996 } 10997 return (CTL_ACTION_PASS); 10998} 10999 11000static ctl_action 11001ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 11002{ 11003 uint64_t lba1, lba2; 11004 uint64_t len1, len2; 11005 int retval; 11006 11007 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 11008 return (CTL_ACTION_ERROR); 11009 11010 retval = ctl_extent_check_unmap(io2, lba1, len1); 11011 if (retval != CTL_ACTION_ERROR) 11012 return (retval); 11013 11014 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 11015 return (CTL_ACTION_ERROR); 11016 11017 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 11018} 11019 11020static ctl_action 11021ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 11022 union ctl_io *ooa_io) 11023{ 11024 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 11025 ctl_serialize_action *serialize_row; 11026 11027 /* 11028 * The initiator attempted multiple untagged commands at the same 11029 * time. Can't do that. 11030 */ 11031 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11032 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11033 && ((pending_io->io_hdr.nexus.targ_port == 11034 ooa_io->io_hdr.nexus.targ_port) 11035 && (pending_io->io_hdr.nexus.initid.id == 11036 ooa_io->io_hdr.nexus.initid.id)) 11037 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 11038 return (CTL_ACTION_OVERLAP); 11039 11040 /* 11041 * The initiator attempted to send multiple tagged commands with 11042 * the same ID. (It's fine if different initiators have the same 11043 * tag ID.) 11044 * 11045 * Even if all of those conditions are true, we don't kill the I/O 11046 * if the command ahead of us has been aborted. We won't end up 11047 * sending it to the FETD, and it's perfectly legal to resend a 11048 * command with the same tag number as long as the previous 11049 * instance of this tag number has been aborted somehow. 11050 */ 11051 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11052 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11053 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 11054 && ((pending_io->io_hdr.nexus.targ_port == 11055 ooa_io->io_hdr.nexus.targ_port) 11056 && (pending_io->io_hdr.nexus.initid.id == 11057 ooa_io->io_hdr.nexus.initid.id)) 11058 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 11059 return (CTL_ACTION_OVERLAP_TAG); 11060 11061 /* 11062 * If we get a head of queue tag, SAM-3 says that we should 11063 * immediately execute it. 11064 * 11065 * What happens if this command would normally block for some other 11066 * reason? e.g. a request sense with a head of queue tag 11067 * immediately after a write. Normally that would block, but this 11068 * will result in its getting executed immediately... 11069 * 11070 * We currently return "pass" instead of "skip", so we'll end up 11071 * going through the rest of the queue to check for overlapped tags. 11072 * 11073 * XXX KDM check for other types of blockage first?? 11074 */ 11075 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11076 return (CTL_ACTION_PASS); 11077 11078 /* 11079 * Ordered tags have to block until all items ahead of them 11080 * have completed. If we get called with an ordered tag, we always 11081 * block, if something else is ahead of us in the queue. 11082 */ 11083 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11084 return (CTL_ACTION_BLOCK); 11085 11086 /* 11087 * Simple tags get blocked until all head of queue and ordered tags 11088 * ahead of them have completed. I'm lumping untagged commands in 11089 * with simple tags here. XXX KDM is that the right thing to do? 11090 */ 11091 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11092 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11093 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11094 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11095 return (CTL_ACTION_BLOCK); 11096 11097 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11098 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11099 11100 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11101 11102 switch (serialize_row[pending_entry->seridx]) { 11103 case CTL_SER_BLOCK: 11104 return (CTL_ACTION_BLOCK); 11105 case CTL_SER_EXTENT: 11106 return (ctl_extent_check(pending_io, ooa_io)); 11107 case CTL_SER_EXTENTOPT: 11108 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11109 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11110 return (ctl_extent_check(pending_io, ooa_io)); 11111 /* FALLTHROUGH */ 11112 case CTL_SER_PASS: 11113 return (CTL_ACTION_PASS); 11114 case CTL_SER_BLOCKOPT: 11115 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11116 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11117 return (CTL_ACTION_BLOCK); 11118 return (CTL_ACTION_PASS); 11119 case CTL_SER_SKIP: 11120 return (CTL_ACTION_SKIP); 11121 default: 11122 panic("invalid serialization value %d", 11123 serialize_row[pending_entry->seridx]); 11124 } 11125 11126 return (CTL_ACTION_ERROR); 11127} 11128 11129/* 11130 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11131 * Assumptions: 11132 * - pending_io is generally either incoming, or on the blocked queue 11133 * - starting I/O is the I/O we want to start the check with. 11134 */ 11135static ctl_action 11136ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11137 union ctl_io *starting_io) 11138{ 11139 union ctl_io *ooa_io; 11140 ctl_action action; 11141 11142 mtx_assert(&lun->lun_lock, MA_OWNED); 11143 11144 /* 11145 * Run back along the OOA queue, starting with the current 11146 * blocked I/O and going through every I/O before it on the 11147 * queue. If starting_io is NULL, we'll just end up returning 11148 * CTL_ACTION_PASS. 11149 */ 11150 for (ooa_io = starting_io; ooa_io != NULL; 11151 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11152 ooa_links)){ 11153 11154 /* 11155 * This routine just checks to see whether 11156 * cur_blocked is blocked by ooa_io, which is ahead 11157 * of it in the queue. It doesn't queue/dequeue 11158 * cur_blocked. 11159 */ 11160 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11161 switch (action) { 11162 case CTL_ACTION_BLOCK: 11163 case CTL_ACTION_OVERLAP: 11164 case CTL_ACTION_OVERLAP_TAG: 11165 case CTL_ACTION_SKIP: 11166 case CTL_ACTION_ERROR: 11167 return (action); 11168 break; /* NOTREACHED */ 11169 case CTL_ACTION_PASS: 11170 break; 11171 default: 11172 panic("invalid action %d", action); 11173 break; /* NOTREACHED */ 11174 } 11175 } 11176 11177 return (CTL_ACTION_PASS); 11178} 11179 11180/* 11181 * Assumptions: 11182 * - An I/O has just completed, and has been removed from the per-LUN OOA 11183 * queue, so some items on the blocked queue may now be unblocked. 11184 */ 11185static int 11186ctl_check_blocked(struct ctl_lun *lun) 11187{ 11188 union ctl_io *cur_blocked, *next_blocked; 11189 11190 mtx_assert(&lun->lun_lock, MA_OWNED); 11191 11192 /* 11193 * Run forward from the head of the blocked queue, checking each 11194 * entry against the I/Os prior to it on the OOA queue to see if 11195 * there is still any blockage. 11196 * 11197 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11198 * with our removing a variable on it while it is traversing the 11199 * list. 11200 */ 11201 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11202 cur_blocked != NULL; cur_blocked = next_blocked) { 11203 union ctl_io *prev_ooa; 11204 ctl_action action; 11205 11206 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11207 blocked_links); 11208 11209 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11210 ctl_ooaq, ooa_links); 11211 11212 /* 11213 * If cur_blocked happens to be the first item in the OOA 11214 * queue now, prev_ooa will be NULL, and the action 11215 * returned will just be CTL_ACTION_PASS. 11216 */ 11217 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11218 11219 switch (action) { 11220 case CTL_ACTION_BLOCK: 11221 /* Nothing to do here, still blocked */ 11222 break; 11223 case CTL_ACTION_OVERLAP: 11224 case CTL_ACTION_OVERLAP_TAG: 11225 /* 11226 * This shouldn't happen! In theory we've already 11227 * checked this command for overlap... 11228 */ 11229 break; 11230 case CTL_ACTION_PASS: 11231 case CTL_ACTION_SKIP: { 11232 struct ctl_softc *softc; 11233 const struct ctl_cmd_entry *entry; 11234 uint32_t initidx; 11235 int isc_retval; 11236 11237 /* 11238 * The skip case shouldn't happen, this transaction 11239 * should have never made it onto the blocked queue. 11240 */ 11241 /* 11242 * This I/O is no longer blocked, we can remove it 11243 * from the blocked queue. Since this is a TAILQ 11244 * (doubly linked list), we can do O(1) removals 11245 * from any place on the list. 11246 */ 11247 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11248 blocked_links); 11249 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11250 11251 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11252 /* 11253 * Need to send IO back to original side to 11254 * run 11255 */ 11256 union ctl_ha_msg msg_info; 11257 11258 msg_info.hdr.original_sc = 11259 cur_blocked->io_hdr.original_sc; 11260 msg_info.hdr.serializing_sc = cur_blocked; 11261 msg_info.hdr.msg_type = CTL_MSG_R2R; 11262 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11263 &msg_info, sizeof(msg_info), 0)) > 11264 CTL_HA_STATUS_SUCCESS) { 11265 printf("CTL:Check Blocked error from " 11266 "ctl_ha_msg_send %d\n", 11267 isc_retval); 11268 } 11269 break; 11270 } 11271 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11272 softc = control_softc; 11273 11274 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11275 11276 /* 11277 * Check this I/O for LUN state changes that may 11278 * have happened while this command was blocked. 11279 * The LUN state may have been changed by a command 11280 * ahead of us in the queue, so we need to re-check 11281 * for any states that can be caused by SCSI 11282 * commands. 11283 */ 11284 if (ctl_scsiio_lun_check(softc, lun, entry, 11285 &cur_blocked->scsiio) == 0) { 11286 cur_blocked->io_hdr.flags |= 11287 CTL_FLAG_IS_WAS_ON_RTR; 11288 ctl_enqueue_rtr(cur_blocked); 11289 } else 11290 ctl_done(cur_blocked); 11291 break; 11292 } 11293 default: 11294 /* 11295 * This probably shouldn't happen -- we shouldn't 11296 * get CTL_ACTION_ERROR, or anything else. 11297 */ 11298 break; 11299 } 11300 } 11301 11302 return (CTL_RETVAL_COMPLETE); 11303} 11304 11305/* 11306 * This routine (with one exception) checks LUN flags that can be set by 11307 * commands ahead of us in the OOA queue. These flags have to be checked 11308 * when a command initially comes in, and when we pull a command off the 11309 * blocked queue and are preparing to execute it. The reason we have to 11310 * check these flags for commands on the blocked queue is that the LUN 11311 * state may have been changed by a command ahead of us while we're on the 11312 * blocked queue. 11313 * 11314 * Ordering is somewhat important with these checks, so please pay 11315 * careful attention to the placement of any new checks. 11316 */ 11317static int 11318ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11319 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11320{ 11321 int retval; 11322 uint32_t residx; 11323 11324 retval = 0; 11325 11326 mtx_assert(&lun->lun_lock, MA_OWNED); 11327 11328 /* 11329 * If this shelf is a secondary shelf controller, we have to reject 11330 * any media access commands. 11331 */ 11332#if 0 11333 /* No longer needed for HA */ 11334 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11335 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11336 ctl_set_lun_standby(ctsio); 11337 retval = 1; 11338 goto bailout; 11339 } 11340#endif 11341 11342 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11343 if (lun->flags & CTL_LUN_READONLY) { 11344 ctl_set_sense(ctsio, /*current_error*/ 1, 11345 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11346 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11347 retval = 1; 11348 goto bailout; 11349 } 11350 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11351 .eca_and_aen & SCP_SWP) != 0) { 11352 ctl_set_sense(ctsio, /*current_error*/ 1, 11353 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11354 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11355 retval = 1; 11356 goto bailout; 11357 } 11358 } 11359 11360 /* 11361 * Check for a reservation conflict. If this command isn't allowed 11362 * even on reserved LUNs, and if this initiator isn't the one who 11363 * reserved us, reject the command with a reservation conflict. 11364 */ 11365 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11366 if ((lun->flags & CTL_LUN_RESERVED) 11367 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11368 if (lun->res_idx != residx) { 11369 ctl_set_reservation_conflict(ctsio); 11370 retval = 1; 11371 goto bailout; 11372 } 11373 } 11374 11375 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11376 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11377 /* No reservation or command is allowed. */; 11378 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11379 (lun->res_type == SPR_TYPE_WR_EX || 11380 lun->res_type == SPR_TYPE_WR_EX_RO || 11381 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11382 /* The command is allowed for Write Exclusive resv. */; 11383 } else { 11384 /* 11385 * if we aren't registered or it's a res holder type 11386 * reservation and this isn't the res holder then set a 11387 * conflict. 11388 */ 11389 if (lun->pr_keys[residx] == 0 11390 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11391 ctl_set_reservation_conflict(ctsio); 11392 retval = 1; 11393 goto bailout; 11394 } 11395 11396 } 11397 11398 if ((lun->flags & CTL_LUN_OFFLINE) 11399 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11400 ctl_set_lun_not_ready(ctsio); 11401 retval = 1; 11402 goto bailout; 11403 } 11404 11405 /* 11406 * If the LUN is stopped, see if this particular command is allowed 11407 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11408 */ 11409 if ((lun->flags & CTL_LUN_STOPPED) 11410 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11411 /* "Logical unit not ready, initializing cmd. required" */ 11412 ctl_set_lun_stopped(ctsio); 11413 retval = 1; 11414 goto bailout; 11415 } 11416 11417 if ((lun->flags & CTL_LUN_INOPERABLE) 11418 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11419 /* "Medium format corrupted" */ 11420 ctl_set_medium_format_corrupted(ctsio); 11421 retval = 1; 11422 goto bailout; 11423 } 11424 11425bailout: 11426 return (retval); 11427 11428} 11429 11430static void 11431ctl_failover_io(union ctl_io *io, int have_lock) 11432{ 11433 ctl_set_busy(&io->scsiio); 11434 ctl_done(io); 11435} 11436 11437static void 11438ctl_failover(void) 11439{ 11440 struct ctl_lun *lun; 11441 struct ctl_softc *ctl_softc; 11442 union ctl_io *next_io, *pending_io; 11443 union ctl_io *io; 11444 int lun_idx; 11445 int i; 11446 11447 ctl_softc = control_softc; 11448 11449 mtx_lock(&ctl_softc->ctl_lock); 11450 /* 11451 * Remove any cmds from the other SC from the rtr queue. These 11452 * will obviously only be for LUNs for which we're the primary. 11453 * We can't send status or get/send data for these commands. 11454 * Since they haven't been executed yet, we can just remove them. 11455 * We'll either abort them or delete them below, depending on 11456 * which HA mode we're in. 11457 */ 11458#ifdef notyet 11459 mtx_lock(&ctl_softc->queue_lock); 11460 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11461 io != NULL; io = next_io) { 11462 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11463 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11464 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11465 ctl_io_hdr, links); 11466 } 11467 mtx_unlock(&ctl_softc->queue_lock); 11468#endif 11469 11470 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11471 lun = ctl_softc->ctl_luns[lun_idx]; 11472 if (lun==NULL) 11473 continue; 11474 11475 /* 11476 * Processor LUNs are primary on both sides. 11477 * XXX will this always be true? 11478 */ 11479 if (lun->be_lun->lun_type == T_PROCESSOR) 11480 continue; 11481 11482 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11483 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11484 printf("FAILOVER: primary lun %d\n", lun_idx); 11485 /* 11486 * Remove all commands from the other SC. First from the 11487 * blocked queue then from the ooa queue. Once we have 11488 * removed them. Call ctl_check_blocked to see if there 11489 * is anything that can run. 11490 */ 11491 for (io = (union ctl_io *)TAILQ_FIRST( 11492 &lun->blocked_queue); io != NULL; io = next_io) { 11493 11494 next_io = (union ctl_io *)TAILQ_NEXT( 11495 &io->io_hdr, blocked_links); 11496 11497 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11498 TAILQ_REMOVE(&lun->blocked_queue, 11499 &io->io_hdr,blocked_links); 11500 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11501 TAILQ_REMOVE(&lun->ooa_queue, 11502 &io->io_hdr, ooa_links); 11503 11504 ctl_free_io(io); 11505 } 11506 } 11507 11508 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11509 io != NULL; io = next_io) { 11510 11511 next_io = (union ctl_io *)TAILQ_NEXT( 11512 &io->io_hdr, ooa_links); 11513 11514 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11515 11516 TAILQ_REMOVE(&lun->ooa_queue, 11517 &io->io_hdr, 11518 ooa_links); 11519 11520 ctl_free_io(io); 11521 } 11522 } 11523 ctl_check_blocked(lun); 11524 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11525 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11526 11527 printf("FAILOVER: primary lun %d\n", lun_idx); 11528 /* 11529 * Abort all commands from the other SC. We can't 11530 * send status back for them now. These should get 11531 * cleaned up when they are completed or come out 11532 * for a datamove operation. 11533 */ 11534 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11535 io != NULL; io = next_io) { 11536 next_io = (union ctl_io *)TAILQ_NEXT( 11537 &io->io_hdr, ooa_links); 11538 11539 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11540 io->io_hdr.flags |= CTL_FLAG_ABORT; 11541 } 11542 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11543 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11544 11545 printf("FAILOVER: secondary lun %d\n", lun_idx); 11546 11547 lun->flags |= CTL_LUN_PRIMARY_SC; 11548 11549 /* 11550 * We send all I/O that was sent to this controller 11551 * and redirected to the other side back with 11552 * busy status, and have the initiator retry it. 11553 * Figuring out how much data has been transferred, 11554 * etc. and picking up where we left off would be 11555 * very tricky. 11556 * 11557 * XXX KDM need to remove I/O from the blocked 11558 * queue as well! 11559 */ 11560 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11561 &lun->ooa_queue); pending_io != NULL; 11562 pending_io = next_io) { 11563 11564 next_io = (union ctl_io *)TAILQ_NEXT( 11565 &pending_io->io_hdr, ooa_links); 11566 11567 pending_io->io_hdr.flags &= 11568 ~CTL_FLAG_SENT_2OTHER_SC; 11569 11570 if (pending_io->io_hdr.flags & 11571 CTL_FLAG_IO_ACTIVE) { 11572 pending_io->io_hdr.flags |= 11573 CTL_FLAG_FAILOVER; 11574 } else { 11575 ctl_set_busy(&pending_io->scsiio); 11576 ctl_done(pending_io); 11577 } 11578 } 11579 11580 /* 11581 * Build Unit Attention 11582 */ 11583 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11584 lun->pending_ua[i] |= 11585 CTL_UA_ASYM_ACC_CHANGE; 11586 } 11587 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11588 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11589 printf("FAILOVER: secondary lun %d\n", lun_idx); 11590 /* 11591 * if the first io on the OOA is not on the RtR queue 11592 * add it. 11593 */ 11594 lun->flags |= CTL_LUN_PRIMARY_SC; 11595 11596 pending_io = (union ctl_io *)TAILQ_FIRST( 11597 &lun->ooa_queue); 11598 if (pending_io==NULL) { 11599 printf("Nothing on OOA queue\n"); 11600 continue; 11601 } 11602 11603 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11604 if ((pending_io->io_hdr.flags & 11605 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11606 pending_io->io_hdr.flags |= 11607 CTL_FLAG_IS_WAS_ON_RTR; 11608 ctl_enqueue_rtr(pending_io); 11609 } 11610#if 0 11611 else 11612 { 11613 printf("Tag 0x%04x is running\n", 11614 pending_io->scsiio.tag_num); 11615 } 11616#endif 11617 11618 next_io = (union ctl_io *)TAILQ_NEXT( 11619 &pending_io->io_hdr, ooa_links); 11620 for (pending_io=next_io; pending_io != NULL; 11621 pending_io = next_io) { 11622 pending_io->io_hdr.flags &= 11623 ~CTL_FLAG_SENT_2OTHER_SC; 11624 next_io = (union ctl_io *)TAILQ_NEXT( 11625 &pending_io->io_hdr, ooa_links); 11626 if (pending_io->io_hdr.flags & 11627 CTL_FLAG_IS_WAS_ON_RTR) { 11628#if 0 11629 printf("Tag 0x%04x is running\n", 11630 pending_io->scsiio.tag_num); 11631#endif 11632 continue; 11633 } 11634 11635 switch (ctl_check_ooa(lun, pending_io, 11636 (union ctl_io *)TAILQ_PREV( 11637 &pending_io->io_hdr, ctl_ooaq, 11638 ooa_links))) { 11639 11640 case CTL_ACTION_BLOCK: 11641 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11642 &pending_io->io_hdr, 11643 blocked_links); 11644 pending_io->io_hdr.flags |= 11645 CTL_FLAG_BLOCKED; 11646 break; 11647 case CTL_ACTION_PASS: 11648 case CTL_ACTION_SKIP: 11649 pending_io->io_hdr.flags |= 11650 CTL_FLAG_IS_WAS_ON_RTR; 11651 ctl_enqueue_rtr(pending_io); 11652 break; 11653 case CTL_ACTION_OVERLAP: 11654 ctl_set_overlapped_cmd( 11655 (struct ctl_scsiio *)pending_io); 11656 ctl_done(pending_io); 11657 break; 11658 case CTL_ACTION_OVERLAP_TAG: 11659 ctl_set_overlapped_tag( 11660 (struct ctl_scsiio *)pending_io, 11661 pending_io->scsiio.tag_num & 0xff); 11662 ctl_done(pending_io); 11663 break; 11664 case CTL_ACTION_ERROR: 11665 default: 11666 ctl_set_internal_failure( 11667 (struct ctl_scsiio *)pending_io, 11668 0, // sks_valid 11669 0); //retry count 11670 ctl_done(pending_io); 11671 break; 11672 } 11673 } 11674 11675 /* 11676 * Build Unit Attention 11677 */ 11678 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11679 lun->pending_ua[i] |= 11680 CTL_UA_ASYM_ACC_CHANGE; 11681 } 11682 } else { 11683 panic("Unhandled HA mode failover, LUN flags = %#x, " 11684 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11685 } 11686 } 11687 ctl_pause_rtr = 0; 11688 mtx_unlock(&ctl_softc->ctl_lock); 11689} 11690 11691static int 11692ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11693{ 11694 struct ctl_lun *lun; 11695 const struct ctl_cmd_entry *entry; 11696 uint32_t initidx, targ_lun; 11697 int retval; 11698 11699 retval = 0; 11700 11701 lun = NULL; 11702 11703 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11704 if ((targ_lun < CTL_MAX_LUNS) 11705 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) { 11706 /* 11707 * If the LUN is invalid, pretend that it doesn't exist. 11708 * It will go away as soon as all pending I/O has been 11709 * completed. 11710 */ 11711 mtx_lock(&lun->lun_lock); 11712 if (lun->flags & CTL_LUN_DISABLED) { 11713 mtx_unlock(&lun->lun_lock); 11714 lun = NULL; 11715 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11716 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11717 } else { 11718 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11719 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11720 lun->be_lun; 11721 if (lun->be_lun->lun_type == T_PROCESSOR) { 11722 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11723 } 11724 11725 /* 11726 * Every I/O goes into the OOA queue for a 11727 * particular LUN, and stays there until completion. 11728 */ 11729 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11730 ooa_links); 11731 } 11732 } else { 11733 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11734 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11735 } 11736 11737 /* Get command entry and return error if it is unsuppotyed. */ 11738 entry = ctl_validate_command(ctsio); 11739 if (entry == NULL) { 11740 if (lun) 11741 mtx_unlock(&lun->lun_lock); 11742 return (retval); 11743 } 11744 11745 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11746 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11747 11748 /* 11749 * Check to see whether we can send this command to LUNs that don't 11750 * exist. This should pretty much only be the case for inquiry 11751 * and request sense. Further checks, below, really require having 11752 * a LUN, so we can't really check the command anymore. Just put 11753 * it on the rtr queue. 11754 */ 11755 if (lun == NULL) { 11756 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11757 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11758 ctl_enqueue_rtr((union ctl_io *)ctsio); 11759 return (retval); 11760 } 11761 11762 ctl_set_unsupported_lun(ctsio); 11763 ctl_done((union ctl_io *)ctsio); 11764 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11765 return (retval); 11766 } else { 11767 /* 11768 * Make sure we support this particular command on this LUN. 11769 * e.g., we don't support writes to the control LUN. 11770 */ 11771 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11772 mtx_unlock(&lun->lun_lock); 11773 ctl_set_invalid_opcode(ctsio); 11774 ctl_done((union ctl_io *)ctsio); 11775 return (retval); 11776 } 11777 } 11778 11779 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11780 11781#ifdef CTL_WITH_CA 11782 /* 11783 * If we've got a request sense, it'll clear the contingent 11784 * allegiance condition. Otherwise, if we have a CA condition for 11785 * this initiator, clear it, because it sent down a command other 11786 * than request sense. 11787 */ 11788 if ((ctsio->cdb[0] != REQUEST_SENSE) 11789 && (ctl_is_set(lun->have_ca, initidx))) 11790 ctl_clear_mask(lun->have_ca, initidx); 11791#endif 11792 11793 /* 11794 * If the command has this flag set, it handles its own unit 11795 * attention reporting, we shouldn't do anything. Otherwise we 11796 * check for any pending unit attentions, and send them back to the 11797 * initiator. We only do this when a command initially comes in, 11798 * not when we pull it off the blocked queue. 11799 * 11800 * According to SAM-3, section 5.3.2, the order that things get 11801 * presented back to the host is basically unit attentions caused 11802 * by some sort of reset event, busy status, reservation conflicts 11803 * or task set full, and finally any other status. 11804 * 11805 * One issue here is that some of the unit attentions we report 11806 * don't fall into the "reset" category (e.g. "reported luns data 11807 * has changed"). So reporting it here, before the reservation 11808 * check, may be technically wrong. I guess the only thing to do 11809 * would be to check for and report the reset events here, and then 11810 * check for the other unit attention types after we check for a 11811 * reservation conflict. 11812 * 11813 * XXX KDM need to fix this 11814 */ 11815 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11816 ctl_ua_type ua_type; 11817 11818 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11819 scsi_sense_data_type sense_format; 11820 11821 if (lun != NULL) 11822 sense_format = (lun->flags & 11823 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11824 SSD_TYPE_FIXED; 11825 else 11826 sense_format = SSD_TYPE_FIXED; 11827 11828 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11829 &ctsio->sense_data, sense_format); 11830 if (ua_type != CTL_UA_NONE) { 11831 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11832 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11833 CTL_AUTOSENSE; 11834 ctsio->sense_len = SSD_FULL_SIZE; 11835 mtx_unlock(&lun->lun_lock); 11836 ctl_done((union ctl_io *)ctsio); 11837 return (retval); 11838 } 11839 } 11840 } 11841 11842 11843 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11844 mtx_unlock(&lun->lun_lock); 11845 ctl_done((union ctl_io *)ctsio); 11846 return (retval); 11847 } 11848 11849 /* 11850 * XXX CHD this is where we want to send IO to other side if 11851 * this LUN is secondary on this SC. We will need to make a copy 11852 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11853 * the copy we send as FROM_OTHER. 11854 * We also need to stuff the address of the original IO so we can 11855 * find it easily. Something similar will need be done on the other 11856 * side so when we are done we can find the copy. 11857 */ 11858 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11859 union ctl_ha_msg msg_info; 11860 int isc_retval; 11861 11862 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11863 11864 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11865 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11866#if 0 11867 printf("1. ctsio %p\n", ctsio); 11868#endif 11869 msg_info.hdr.serializing_sc = NULL; 11870 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11871 msg_info.scsi.tag_num = ctsio->tag_num; 11872 msg_info.scsi.tag_type = ctsio->tag_type; 11873 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11874 11875 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11876 11877 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11878 (void *)&msg_info, sizeof(msg_info), 0)) > 11879 CTL_HA_STATUS_SUCCESS) { 11880 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11881 isc_retval); 11882 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11883 } else { 11884#if 0 11885 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11886#endif 11887 } 11888 11889 /* 11890 * XXX KDM this I/O is off the incoming queue, but hasn't 11891 * been inserted on any other queue. We may need to come 11892 * up with a holding queue while we wait for serialization 11893 * so that we have an idea of what we're waiting for from 11894 * the other side. 11895 */ 11896 mtx_unlock(&lun->lun_lock); 11897 return (retval); 11898 } 11899 11900 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11901 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11902 ctl_ooaq, ooa_links))) { 11903 case CTL_ACTION_BLOCK: 11904 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11905 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11906 blocked_links); 11907 mtx_unlock(&lun->lun_lock); 11908 return (retval); 11909 case CTL_ACTION_PASS: 11910 case CTL_ACTION_SKIP: 11911 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11912 mtx_unlock(&lun->lun_lock); 11913 ctl_enqueue_rtr((union ctl_io *)ctsio); 11914 break; 11915 case CTL_ACTION_OVERLAP: 11916 mtx_unlock(&lun->lun_lock); 11917 ctl_set_overlapped_cmd(ctsio); 11918 ctl_done((union ctl_io *)ctsio); 11919 break; 11920 case CTL_ACTION_OVERLAP_TAG: 11921 mtx_unlock(&lun->lun_lock); 11922 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11923 ctl_done((union ctl_io *)ctsio); 11924 break; 11925 case CTL_ACTION_ERROR: 11926 default: 11927 mtx_unlock(&lun->lun_lock); 11928 ctl_set_internal_failure(ctsio, 11929 /*sks_valid*/ 0, 11930 /*retry_count*/ 0); 11931 ctl_done((union ctl_io *)ctsio); 11932 break; 11933 } 11934 return (retval); 11935} 11936 11937const struct ctl_cmd_entry * 11938ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11939{ 11940 const struct ctl_cmd_entry *entry; 11941 int service_action; 11942 11943 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11944 if (sa) 11945 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11946 if (entry->flags & CTL_CMD_FLAG_SA5) { 11947 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11948 entry = &((const struct ctl_cmd_entry *) 11949 entry->execute)[service_action]; 11950 } 11951 return (entry); 11952} 11953 11954const struct ctl_cmd_entry * 11955ctl_validate_command(struct ctl_scsiio *ctsio) 11956{ 11957 const struct ctl_cmd_entry *entry; 11958 int i, sa; 11959 uint8_t diff; 11960 11961 entry = ctl_get_cmd_entry(ctsio, &sa); 11962 if (entry->execute == NULL) { 11963 if (sa) 11964 ctl_set_invalid_field(ctsio, 11965 /*sks_valid*/ 1, 11966 /*command*/ 1, 11967 /*field*/ 1, 11968 /*bit_valid*/ 1, 11969 /*bit*/ 4); 11970 else 11971 ctl_set_invalid_opcode(ctsio); 11972 ctl_done((union ctl_io *)ctsio); 11973 return (NULL); 11974 } 11975 KASSERT(entry->length > 0, 11976 ("Not defined length for command 0x%02x/0x%02x", 11977 ctsio->cdb[0], ctsio->cdb[1])); 11978 for (i = 1; i < entry->length; i++) { 11979 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11980 if (diff == 0) 11981 continue; 11982 ctl_set_invalid_field(ctsio, 11983 /*sks_valid*/ 1, 11984 /*command*/ 1, 11985 /*field*/ i, 11986 /*bit_valid*/ 1, 11987 /*bit*/ fls(diff) - 1); 11988 ctl_done((union ctl_io *)ctsio); 11989 return (NULL); 11990 } 11991 return (entry); 11992} 11993 11994static int 11995ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11996{ 11997 11998 switch (lun_type) { 11999 case T_PROCESSOR: 12000 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 12001 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 12002 return (0); 12003 break; 12004 case T_DIRECT: 12005 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 12006 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 12007 return (0); 12008 break; 12009 default: 12010 return (0); 12011 } 12012 return (1); 12013} 12014 12015static int 12016ctl_scsiio(struct ctl_scsiio *ctsio) 12017{ 12018 int retval; 12019 const struct ctl_cmd_entry *entry; 12020 12021 retval = CTL_RETVAL_COMPLETE; 12022 12023 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 12024 12025 entry = ctl_get_cmd_entry(ctsio, NULL); 12026 12027 /* 12028 * If this I/O has been aborted, just send it straight to 12029 * ctl_done() without executing it. 12030 */ 12031 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 12032 ctl_done((union ctl_io *)ctsio); 12033 goto bailout; 12034 } 12035 12036 /* 12037 * All the checks should have been handled by ctl_scsiio_precheck(). 12038 * We should be clear now to just execute the I/O. 12039 */ 12040 retval = entry->execute(ctsio); 12041 12042bailout: 12043 return (retval); 12044} 12045 12046/* 12047 * Since we only implement one target right now, a bus reset simply resets 12048 * our single target. 12049 */ 12050static int 12051ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 12052{ 12053 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 12054} 12055 12056static int 12057ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 12058 ctl_ua_type ua_type) 12059{ 12060 struct ctl_lun *lun; 12061 int retval; 12062 12063 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12064 union ctl_ha_msg msg_info; 12065 12066 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12067 msg_info.hdr.nexus = io->io_hdr.nexus; 12068 if (ua_type==CTL_UA_TARG_RESET) 12069 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 12070 else 12071 msg_info.task.task_action = CTL_TASK_BUS_RESET; 12072 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12073 msg_info.hdr.original_sc = NULL; 12074 msg_info.hdr.serializing_sc = NULL; 12075 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12076 (void *)&msg_info, sizeof(msg_info), 0)) { 12077 } 12078 } 12079 retval = 0; 12080 12081 mtx_lock(&ctl_softc->ctl_lock); 12082 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 12083 retval += ctl_lun_reset(lun, io, ua_type); 12084 mtx_unlock(&ctl_softc->ctl_lock); 12085 12086 return (retval); 12087} 12088 12089/* 12090 * The LUN should always be set. The I/O is optional, and is used to 12091 * distinguish between I/Os sent by this initiator, and by other 12092 * initiators. We set unit attention for initiators other than this one. 12093 * SAM-3 is vague on this point. It does say that a unit attention should 12094 * be established for other initiators when a LUN is reset (see section 12095 * 5.7.3), but it doesn't specifically say that the unit attention should 12096 * be established for this particular initiator when a LUN is reset. Here 12097 * is the relevant text, from SAM-3 rev 8: 12098 * 12099 * 5.7.2 When a SCSI initiator port aborts its own tasks 12100 * 12101 * When a SCSI initiator port causes its own task(s) to be aborted, no 12102 * notification that the task(s) have been aborted shall be returned to 12103 * the SCSI initiator port other than the completion response for the 12104 * command or task management function action that caused the task(s) to 12105 * be aborted and notification(s) associated with related effects of the 12106 * action (e.g., a reset unit attention condition). 12107 * 12108 * XXX KDM for now, we're setting unit attention for all initiators. 12109 */ 12110static int 12111ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 12112{ 12113 union ctl_io *xio; 12114#if 0 12115 uint32_t initindex; 12116#endif 12117 int i; 12118 12119 mtx_lock(&lun->lun_lock); 12120 /* 12121 * Run through the OOA queue and abort each I/O. 12122 */ 12123#if 0 12124 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12125#endif 12126 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12127 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12128 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 12129 } 12130 12131 /* 12132 * This version sets unit attention for every 12133 */ 12134#if 0 12135 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12136 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12137 if (initindex == i) 12138 continue; 12139 lun->pending_ua[i] |= ua_type; 12140 } 12141#endif 12142 12143 /* 12144 * A reset (any kind, really) clears reservations established with 12145 * RESERVE/RELEASE. It does not clear reservations established 12146 * with PERSISTENT RESERVE OUT, but we don't support that at the 12147 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 12148 * reservations made with the RESERVE/RELEASE commands, because 12149 * those commands are obsolete in SPC-3. 12150 */ 12151 lun->flags &= ~CTL_LUN_RESERVED; 12152 12153 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12154#ifdef CTL_WITH_CA 12155 ctl_clear_mask(lun->have_ca, i); 12156#endif 12157 lun->pending_ua[i] |= ua_type; 12158 } 12159 mtx_unlock(&lun->lun_lock); 12160 12161 return (0); 12162} 12163 12164static void 12165ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12166 int other_sc) 12167{ 12168 union ctl_io *xio; 12169 12170 mtx_assert(&lun->lun_lock, MA_OWNED); 12171 12172 /* 12173 * Run through the OOA queue and attempt to find the given I/O. 12174 * The target port, initiator ID, tag type and tag number have to 12175 * match the values that we got from the initiator. If we have an 12176 * untagged command to abort, simply abort the first untagged command 12177 * we come to. We only allow one untagged command at a time of course. 12178 */ 12179 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12180 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12181 12182 if ((targ_port == UINT32_MAX || 12183 targ_port == xio->io_hdr.nexus.targ_port) && 12184 (init_id == UINT32_MAX || 12185 init_id == xio->io_hdr.nexus.initid.id)) { 12186 if (targ_port != xio->io_hdr.nexus.targ_port || 12187 init_id != xio->io_hdr.nexus.initid.id) 12188 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12189 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12190 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12191 union ctl_ha_msg msg_info; 12192 12193 msg_info.hdr.nexus = xio->io_hdr.nexus; 12194 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12195 msg_info.task.tag_num = xio->scsiio.tag_num; 12196 msg_info.task.tag_type = xio->scsiio.tag_type; 12197 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12198 msg_info.hdr.original_sc = NULL; 12199 msg_info.hdr.serializing_sc = NULL; 12200 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12201 (void *)&msg_info, sizeof(msg_info), 0); 12202 } 12203 } 12204 } 12205} 12206 12207static int 12208ctl_abort_task_set(union ctl_io *io) 12209{ 12210 struct ctl_softc *softc = control_softc; 12211 struct ctl_lun *lun; 12212 uint32_t targ_lun; 12213 12214 /* 12215 * Look up the LUN. 12216 */ 12217 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12218 mtx_lock(&softc->ctl_lock); 12219 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12220 lun = softc->ctl_luns[targ_lun]; 12221 else { 12222 mtx_unlock(&softc->ctl_lock); 12223 return (1); 12224 } 12225 12226 mtx_lock(&lun->lun_lock); 12227 mtx_unlock(&softc->ctl_lock); 12228 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12229 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12230 io->io_hdr.nexus.initid.id, 12231 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12232 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12233 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12234 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12235 } 12236 mtx_unlock(&lun->lun_lock); 12237 return (0); 12238} 12239 12240static int 12241ctl_i_t_nexus_reset(union ctl_io *io) 12242{ 12243 struct ctl_softc *softc = control_softc; 12244 struct ctl_lun *lun; 12245 uint32_t initindex, residx; 12246 12247 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12248 residx = ctl_get_resindex(&io->io_hdr.nexus); 12249 mtx_lock(&softc->ctl_lock); 12250 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12251 mtx_lock(&lun->lun_lock); 12252 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12253 io->io_hdr.nexus.initid.id, 12254 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12255#ifdef CTL_WITH_CA 12256 ctl_clear_mask(lun->have_ca, initindex); 12257#endif 12258 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12259 lun->flags &= ~CTL_LUN_RESERVED; 12260 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12261 mtx_unlock(&lun->lun_lock); 12262 } 12263 mtx_unlock(&softc->ctl_lock); 12264 return (0); 12265} 12266 12267static int 12268ctl_abort_task(union ctl_io *io) 12269{ 12270 union ctl_io *xio; 12271 struct ctl_lun *lun; 12272 struct ctl_softc *ctl_softc; 12273#if 0 12274 struct sbuf sb; 12275 char printbuf[128]; 12276#endif 12277 int found; 12278 uint32_t targ_lun; 12279 12280 ctl_softc = control_softc; 12281 found = 0; 12282 12283 /* 12284 * Look up the LUN. 12285 */ 12286 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12287 mtx_lock(&ctl_softc->ctl_lock); 12288 if ((targ_lun < CTL_MAX_LUNS) 12289 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12290 lun = ctl_softc->ctl_luns[targ_lun]; 12291 else { 12292 mtx_unlock(&ctl_softc->ctl_lock); 12293 return (1); 12294 } 12295 12296#if 0 12297 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12298 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12299#endif 12300 12301 mtx_lock(&lun->lun_lock); 12302 mtx_unlock(&ctl_softc->ctl_lock); 12303 /* 12304 * Run through the OOA queue and attempt to find the given I/O. 12305 * The target port, initiator ID, tag type and tag number have to 12306 * match the values that we got from the initiator. If we have an 12307 * untagged command to abort, simply abort the first untagged command 12308 * we come to. We only allow one untagged command at a time of course. 12309 */ 12310#if 0 12311 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12312#endif 12313 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12314 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12315#if 0 12316 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12317 12318 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12319 lun->lun, xio->scsiio.tag_num, 12320 xio->scsiio.tag_type, 12321 (xio->io_hdr.blocked_links.tqe_prev 12322 == NULL) ? "" : " BLOCKED", 12323 (xio->io_hdr.flags & 12324 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12325 (xio->io_hdr.flags & 12326 CTL_FLAG_ABORT) ? " ABORT" : "", 12327 (xio->io_hdr.flags & 12328 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12329 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12330 sbuf_finish(&sb); 12331 printf("%s\n", sbuf_data(&sb)); 12332#endif 12333 12334 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12335 && (xio->io_hdr.nexus.initid.id == 12336 io->io_hdr.nexus.initid.id)) { 12337 /* 12338 * If the abort says that the task is untagged, the 12339 * task in the queue must be untagged. Otherwise, 12340 * we just check to see whether the tag numbers 12341 * match. This is because the QLogic firmware 12342 * doesn't pass back the tag type in an abort 12343 * request. 12344 */ 12345#if 0 12346 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12347 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12348 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12349#endif 12350 /* 12351 * XXX KDM we've got problems with FC, because it 12352 * doesn't send down a tag type with aborts. So we 12353 * can only really go by the tag number... 12354 * This may cause problems with parallel SCSI. 12355 * Need to figure that out!! 12356 */ 12357 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12358 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12359 found = 1; 12360 if ((io->io_hdr.flags & 12361 CTL_FLAG_FROM_OTHER_SC) == 0 && 12362 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12363 union ctl_ha_msg msg_info; 12364 12365 io->io_hdr.flags |= 12366 CTL_FLAG_SENT_2OTHER_SC; 12367 msg_info.hdr.nexus = io->io_hdr.nexus; 12368 msg_info.task.task_action = 12369 CTL_TASK_ABORT_TASK; 12370 msg_info.task.tag_num = 12371 io->taskio.tag_num; 12372 msg_info.task.tag_type = 12373 io->taskio.tag_type; 12374 msg_info.hdr.msg_type = 12375 CTL_MSG_MANAGE_TASKS; 12376 msg_info.hdr.original_sc = NULL; 12377 msg_info.hdr.serializing_sc = NULL; 12378#if 0 12379 printf("Sent Abort to other side\n"); 12380#endif 12381 if (CTL_HA_STATUS_SUCCESS != 12382 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12383 (void *)&msg_info, 12384 sizeof(msg_info), 0)) { 12385 } 12386 } 12387#if 0 12388 printf("ctl_abort_task: found I/O to abort\n"); 12389#endif 12390 break; 12391 } 12392 } 12393 } 12394 mtx_unlock(&lun->lun_lock); 12395 12396 if (found == 0) { 12397 /* 12398 * This isn't really an error. It's entirely possible for 12399 * the abort and command completion to cross on the wire. 12400 * This is more of an informative/diagnostic error. 12401 */ 12402#if 0 12403 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12404 "%d:%d:%d:%d tag %d type %d\n", 12405 io->io_hdr.nexus.initid.id, 12406 io->io_hdr.nexus.targ_port, 12407 io->io_hdr.nexus.targ_target.id, 12408 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12409 io->taskio.tag_type); 12410#endif 12411 } 12412 return (0); 12413} 12414 12415static void 12416ctl_run_task(union ctl_io *io) 12417{ 12418 struct ctl_softc *ctl_softc = control_softc; 12419 int retval = 1; 12420 const char *task_desc; 12421 12422 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12423 12424 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12425 ("ctl_run_task: Unextected io_type %d\n", 12426 io->io_hdr.io_type)); 12427 12428 task_desc = ctl_scsi_task_string(&io->taskio); 12429 if (task_desc != NULL) { 12430#ifdef NEEDTOPORT 12431 csevent_log(CSC_CTL | CSC_SHELF_SW | 12432 CTL_TASK_REPORT, 12433 csevent_LogType_Trace, 12434 csevent_Severity_Information, 12435 csevent_AlertLevel_Green, 12436 csevent_FRU_Firmware, 12437 csevent_FRU_Unknown, 12438 "CTL: received task: %s",task_desc); 12439#endif 12440 } else { 12441#ifdef NEEDTOPORT 12442 csevent_log(CSC_CTL | CSC_SHELF_SW | 12443 CTL_TASK_REPORT, 12444 csevent_LogType_Trace, 12445 csevent_Severity_Information, 12446 csevent_AlertLevel_Green, 12447 csevent_FRU_Firmware, 12448 csevent_FRU_Unknown, 12449 "CTL: received unknown task " 12450 "type: %d (%#x)", 12451 io->taskio.task_action, 12452 io->taskio.task_action); 12453#endif 12454 } 12455 switch (io->taskio.task_action) { 12456 case CTL_TASK_ABORT_TASK: 12457 retval = ctl_abort_task(io); 12458 break; 12459 case CTL_TASK_ABORT_TASK_SET: 12460 case CTL_TASK_CLEAR_TASK_SET: 12461 retval = ctl_abort_task_set(io); 12462 break; 12463 case CTL_TASK_CLEAR_ACA: 12464 break; 12465 case CTL_TASK_I_T_NEXUS_RESET: 12466 retval = ctl_i_t_nexus_reset(io); 12467 break; 12468 case CTL_TASK_LUN_RESET: { 12469 struct ctl_lun *lun; 12470 uint32_t targ_lun; 12471 12472 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12473 mtx_lock(&ctl_softc->ctl_lock); 12474 if ((targ_lun < CTL_MAX_LUNS) 12475 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12476 lun = ctl_softc->ctl_luns[targ_lun]; 12477 else { 12478 mtx_unlock(&ctl_softc->ctl_lock); 12479 retval = 1; 12480 break; 12481 } 12482 12483 if (!(io->io_hdr.flags & 12484 CTL_FLAG_FROM_OTHER_SC)) { 12485 union ctl_ha_msg msg_info; 12486 12487 io->io_hdr.flags |= 12488 CTL_FLAG_SENT_2OTHER_SC; 12489 msg_info.hdr.msg_type = 12490 CTL_MSG_MANAGE_TASKS; 12491 msg_info.hdr.nexus = io->io_hdr.nexus; 12492 msg_info.task.task_action = 12493 CTL_TASK_LUN_RESET; 12494 msg_info.hdr.original_sc = NULL; 12495 msg_info.hdr.serializing_sc = NULL; 12496 if (CTL_HA_STATUS_SUCCESS != 12497 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12498 (void *)&msg_info, 12499 sizeof(msg_info), 0)) { 12500 } 12501 } 12502 12503 retval = ctl_lun_reset(lun, io, 12504 CTL_UA_LUN_RESET); 12505 mtx_unlock(&ctl_softc->ctl_lock); 12506 break; 12507 } 12508 case CTL_TASK_TARGET_RESET: 12509 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12510 break; 12511 case CTL_TASK_BUS_RESET: 12512 retval = ctl_bus_reset(ctl_softc, io); 12513 break; 12514 case CTL_TASK_PORT_LOGIN: 12515 break; 12516 case CTL_TASK_PORT_LOGOUT: 12517 break; 12518 default: 12519 printf("ctl_run_task: got unknown task management event %d\n", 12520 io->taskio.task_action); 12521 break; 12522 } 12523 if (retval == 0) 12524 io->io_hdr.status = CTL_SUCCESS; 12525 else 12526 io->io_hdr.status = CTL_ERROR; 12527 ctl_done(io); 12528} 12529 12530/* 12531 * For HA operation. Handle commands that come in from the other 12532 * controller. 12533 */ 12534static void 12535ctl_handle_isc(union ctl_io *io) 12536{ 12537 int free_io; 12538 struct ctl_lun *lun; 12539 struct ctl_softc *ctl_softc; 12540 uint32_t targ_lun; 12541 12542 ctl_softc = control_softc; 12543 12544 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12545 lun = ctl_softc->ctl_luns[targ_lun]; 12546 12547 switch (io->io_hdr.msg_type) { 12548 case CTL_MSG_SERIALIZE: 12549 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12550 break; 12551 case CTL_MSG_R2R: { 12552 const struct ctl_cmd_entry *entry; 12553 12554 /* 12555 * This is only used in SER_ONLY mode. 12556 */ 12557 free_io = 0; 12558 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12559 mtx_lock(&lun->lun_lock); 12560 if (ctl_scsiio_lun_check(ctl_softc, lun, 12561 entry, (struct ctl_scsiio *)io) != 0) { 12562 mtx_unlock(&lun->lun_lock); 12563 ctl_done(io); 12564 break; 12565 } 12566 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12567 mtx_unlock(&lun->lun_lock); 12568 ctl_enqueue_rtr(io); 12569 break; 12570 } 12571 case CTL_MSG_FINISH_IO: 12572 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12573 free_io = 0; 12574 ctl_done(io); 12575 } else { 12576 free_io = 1; 12577 mtx_lock(&lun->lun_lock); 12578 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12579 ooa_links); 12580 ctl_check_blocked(lun); 12581 mtx_unlock(&lun->lun_lock); 12582 } 12583 break; 12584 case CTL_MSG_PERS_ACTION: 12585 ctl_hndl_per_res_out_on_other_sc( 12586 (union ctl_ha_msg *)&io->presio.pr_msg); 12587 free_io = 1; 12588 break; 12589 case CTL_MSG_BAD_JUJU: 12590 free_io = 0; 12591 ctl_done(io); 12592 break; 12593 case CTL_MSG_DATAMOVE: 12594 /* Only used in XFER mode */ 12595 free_io = 0; 12596 ctl_datamove_remote(io); 12597 break; 12598 case CTL_MSG_DATAMOVE_DONE: 12599 /* Only used in XFER mode */ 12600 free_io = 0; 12601 io->scsiio.be_move_done(io); 12602 break; 12603 default: 12604 free_io = 1; 12605 printf("%s: Invalid message type %d\n", 12606 __func__, io->io_hdr.msg_type); 12607 break; 12608 } 12609 if (free_io) 12610 ctl_free_io(io); 12611 12612} 12613 12614 12615/* 12616 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12617 * there is no match. 12618 */ 12619static ctl_lun_error_pattern 12620ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12621{ 12622 const struct ctl_cmd_entry *entry; 12623 ctl_lun_error_pattern filtered_pattern, pattern; 12624 12625 pattern = desc->error_pattern; 12626 12627 /* 12628 * XXX KDM we need more data passed into this function to match a 12629 * custom pattern, and we actually need to implement custom pattern 12630 * matching. 12631 */ 12632 if (pattern & CTL_LUN_PAT_CMD) 12633 return (CTL_LUN_PAT_CMD); 12634 12635 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12636 return (CTL_LUN_PAT_ANY); 12637 12638 entry = ctl_get_cmd_entry(ctsio, NULL); 12639 12640 filtered_pattern = entry->pattern & pattern; 12641 12642 /* 12643 * If the user requested specific flags in the pattern (e.g. 12644 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12645 * flags. 12646 * 12647 * If the user did not specify any flags, it doesn't matter whether 12648 * or not the command supports the flags. 12649 */ 12650 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12651 (pattern & ~CTL_LUN_PAT_MASK)) 12652 return (CTL_LUN_PAT_NONE); 12653 12654 /* 12655 * If the user asked for a range check, see if the requested LBA 12656 * range overlaps with this command's LBA range. 12657 */ 12658 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12659 uint64_t lba1; 12660 uint64_t len1; 12661 ctl_action action; 12662 int retval; 12663 12664 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12665 if (retval != 0) 12666 return (CTL_LUN_PAT_NONE); 12667 12668 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12669 desc->lba_range.len); 12670 /* 12671 * A "pass" means that the LBA ranges don't overlap, so 12672 * this doesn't match the user's range criteria. 12673 */ 12674 if (action == CTL_ACTION_PASS) 12675 return (CTL_LUN_PAT_NONE); 12676 } 12677 12678 return (filtered_pattern); 12679} 12680 12681static void 12682ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12683{ 12684 struct ctl_error_desc *desc, *desc2; 12685 12686 mtx_assert(&lun->lun_lock, MA_OWNED); 12687 12688 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12689 ctl_lun_error_pattern pattern; 12690 /* 12691 * Check to see whether this particular command matches 12692 * the pattern in the descriptor. 12693 */ 12694 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12695 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12696 continue; 12697 12698 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12699 case CTL_LUN_INJ_ABORTED: 12700 ctl_set_aborted(&io->scsiio); 12701 break; 12702 case CTL_LUN_INJ_MEDIUM_ERR: 12703 ctl_set_medium_error(&io->scsiio); 12704 break; 12705 case CTL_LUN_INJ_UA: 12706 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12707 * OCCURRED */ 12708 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12709 break; 12710 case CTL_LUN_INJ_CUSTOM: 12711 /* 12712 * We're assuming the user knows what he is doing. 12713 * Just copy the sense information without doing 12714 * checks. 12715 */ 12716 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12717 ctl_min(sizeof(desc->custom_sense), 12718 sizeof(io->scsiio.sense_data))); 12719 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12720 io->scsiio.sense_len = SSD_FULL_SIZE; 12721 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12722 break; 12723 case CTL_LUN_INJ_NONE: 12724 default: 12725 /* 12726 * If this is an error injection type we don't know 12727 * about, clear the continuous flag (if it is set) 12728 * so it will get deleted below. 12729 */ 12730 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12731 break; 12732 } 12733 /* 12734 * By default, each error injection action is a one-shot 12735 */ 12736 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12737 continue; 12738 12739 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12740 12741 free(desc, M_CTL); 12742 } 12743} 12744 12745#ifdef CTL_IO_DELAY 12746static void 12747ctl_datamove_timer_wakeup(void *arg) 12748{ 12749 union ctl_io *io; 12750 12751 io = (union ctl_io *)arg; 12752 12753 ctl_datamove(io); 12754} 12755#endif /* CTL_IO_DELAY */ 12756 12757void 12758ctl_datamove(union ctl_io *io) 12759{ 12760 void (*fe_datamove)(union ctl_io *io); 12761 12762 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12763 12764 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12765 12766#ifdef CTL_TIME_IO 12767 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12768 char str[256]; 12769 char path_str[64]; 12770 struct sbuf sb; 12771 12772 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12773 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12774 12775 sbuf_cat(&sb, path_str); 12776 switch (io->io_hdr.io_type) { 12777 case CTL_IO_SCSI: 12778 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12779 sbuf_printf(&sb, "\n"); 12780 sbuf_cat(&sb, path_str); 12781 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12782 io->scsiio.tag_num, io->scsiio.tag_type); 12783 break; 12784 case CTL_IO_TASK: 12785 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12786 "Tag Type: %d\n", io->taskio.task_action, 12787 io->taskio.tag_num, io->taskio.tag_type); 12788 break; 12789 default: 12790 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12791 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12792 break; 12793 } 12794 sbuf_cat(&sb, path_str); 12795 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12796 (intmax_t)time_uptime - io->io_hdr.start_time); 12797 sbuf_finish(&sb); 12798 printf("%s", sbuf_data(&sb)); 12799 } 12800#endif /* CTL_TIME_IO */ 12801 12802#ifdef CTL_IO_DELAY 12803 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12804 struct ctl_lun *lun; 12805 12806 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12807 12808 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12809 } else { 12810 struct ctl_lun *lun; 12811 12812 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12813 if ((lun != NULL) 12814 && (lun->delay_info.datamove_delay > 0)) { 12815 struct callout *callout; 12816 12817 callout = (struct callout *)&io->io_hdr.timer_bytes; 12818 callout_init(callout, /*mpsafe*/ 1); 12819 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12820 callout_reset(callout, 12821 lun->delay_info.datamove_delay * hz, 12822 ctl_datamove_timer_wakeup, io); 12823 if (lun->delay_info.datamove_type == 12824 CTL_DELAY_TYPE_ONESHOT) 12825 lun->delay_info.datamove_delay = 0; 12826 return; 12827 } 12828 } 12829#endif 12830 12831 /* 12832 * This command has been aborted. Set the port status, so we fail 12833 * the data move. 12834 */ 12835 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12836 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12837 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12838 io->io_hdr.nexus.targ_port, 12839 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12840 io->io_hdr.nexus.targ_lun); 12841 io->io_hdr.port_status = 31337; 12842 /* 12843 * Note that the backend, in this case, will get the 12844 * callback in its context. In other cases it may get 12845 * called in the frontend's interrupt thread context. 12846 */ 12847 io->scsiio.be_move_done(io); 12848 return; 12849 } 12850 12851 /* Don't confuse frontend with zero length data move. */ 12852 if (io->scsiio.kern_data_len == 0) { 12853 io->scsiio.be_move_done(io); 12854 return; 12855 } 12856 12857 /* 12858 * If we're in XFER mode and this I/O is from the other shelf 12859 * controller, we need to send the DMA to the other side to 12860 * actually transfer the data to/from the host. In serialize only 12861 * mode the transfer happens below CTL and ctl_datamove() is only 12862 * called on the machine that originally received the I/O. 12863 */ 12864 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12865 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12866 union ctl_ha_msg msg; 12867 uint32_t sg_entries_sent; 12868 int do_sg_copy; 12869 int i; 12870 12871 memset(&msg, 0, sizeof(msg)); 12872 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12873 msg.hdr.original_sc = io->io_hdr.original_sc; 12874 msg.hdr.serializing_sc = io; 12875 msg.hdr.nexus = io->io_hdr.nexus; 12876 msg.dt.flags = io->io_hdr.flags; 12877 /* 12878 * We convert everything into a S/G list here. We can't 12879 * pass by reference, only by value between controllers. 12880 * So we can't pass a pointer to the S/G list, only as many 12881 * S/G entries as we can fit in here. If it's possible for 12882 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12883 * then we need to break this up into multiple transfers. 12884 */ 12885 if (io->scsiio.kern_sg_entries == 0) { 12886 msg.dt.kern_sg_entries = 1; 12887 /* 12888 * If this is in cached memory, flush the cache 12889 * before we send the DMA request to the other 12890 * controller. We want to do this in either the 12891 * read or the write case. The read case is 12892 * straightforward. In the write case, we want to 12893 * make sure nothing is in the local cache that 12894 * could overwrite the DMAed data. 12895 */ 12896 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12897 /* 12898 * XXX KDM use bus_dmamap_sync() here. 12899 */ 12900 } 12901 12902 /* 12903 * Convert to a physical address if this is a 12904 * virtual address. 12905 */ 12906 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12907 msg.dt.sg_list[0].addr = 12908 io->scsiio.kern_data_ptr; 12909 } else { 12910 /* 12911 * XXX KDM use busdma here! 12912 */ 12913#if 0 12914 msg.dt.sg_list[0].addr = (void *) 12915 vtophys(io->scsiio.kern_data_ptr); 12916#endif 12917 } 12918 12919 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12920 do_sg_copy = 0; 12921 } else { 12922 struct ctl_sg_entry *sgl; 12923 12924 do_sg_copy = 1; 12925 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12926 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 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 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12935 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12936 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12937 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12938 msg.dt.sg_sequence = 0; 12939 12940 /* 12941 * Loop until we've sent all of the S/G entries. On the 12942 * other end, we'll recompose these S/G entries into one 12943 * contiguous list before passing it to the 12944 */ 12945 for (sg_entries_sent = 0; sg_entries_sent < 12946 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12947 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12948 sizeof(msg.dt.sg_list[0])), 12949 msg.dt.kern_sg_entries - sg_entries_sent); 12950 12951 if (do_sg_copy != 0) { 12952 struct ctl_sg_entry *sgl; 12953 int j; 12954 12955 sgl = (struct ctl_sg_entry *) 12956 io->scsiio.kern_data_ptr; 12957 /* 12958 * If this is in cached memory, flush the cache 12959 * before we send the DMA request to the other 12960 * controller. We want to do this in either 12961 * the * read or the write case. The read 12962 * case is straightforward. In the write 12963 * case, we want to make sure nothing is 12964 * in the local cache that could overwrite 12965 * the DMAed data. 12966 */ 12967 12968 for (i = sg_entries_sent, j = 0; 12969 i < msg.dt.cur_sg_entries; i++, j++) { 12970 if ((io->io_hdr.flags & 12971 CTL_FLAG_NO_DATASYNC) == 0) { 12972 /* 12973 * XXX KDM use bus_dmamap_sync() 12974 */ 12975 } 12976 if ((io->io_hdr.flags & 12977 CTL_FLAG_BUS_ADDR) == 0) { 12978 /* 12979 * XXX KDM use busdma. 12980 */ 12981#if 0 12982 msg.dt.sg_list[j].addr =(void *) 12983 vtophys(sgl[i].addr); 12984#endif 12985 } else { 12986 msg.dt.sg_list[j].addr = 12987 sgl[i].addr; 12988 } 12989 msg.dt.sg_list[j].len = sgl[i].len; 12990 } 12991 } 12992 12993 sg_entries_sent += msg.dt.cur_sg_entries; 12994 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12995 msg.dt.sg_last = 1; 12996 else 12997 msg.dt.sg_last = 0; 12998 12999 /* 13000 * XXX KDM drop and reacquire the lock here? 13001 */ 13002 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13003 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13004 /* 13005 * XXX do something here. 13006 */ 13007 } 13008 13009 msg.dt.sent_sg_entries = sg_entries_sent; 13010 } 13011 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13012 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 13013 ctl_failover_io(io, /*have_lock*/ 0); 13014 13015 } else { 13016 13017 /* 13018 * Lookup the fe_datamove() function for this particular 13019 * front end. 13020 */ 13021 fe_datamove = 13022 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13023 13024 fe_datamove(io); 13025 } 13026} 13027 13028static void 13029ctl_send_datamove_done(union ctl_io *io, int have_lock) 13030{ 13031 union ctl_ha_msg msg; 13032 int isc_status; 13033 13034 memset(&msg, 0, sizeof(msg)); 13035 13036 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 13037 msg.hdr.original_sc = io; 13038 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13039 msg.hdr.nexus = io->io_hdr.nexus; 13040 msg.hdr.status = io->io_hdr.status; 13041 msg.scsi.tag_num = io->scsiio.tag_num; 13042 msg.scsi.tag_type = io->scsiio.tag_type; 13043 msg.scsi.scsi_status = io->scsiio.scsi_status; 13044 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13045 sizeof(io->scsiio.sense_data)); 13046 msg.scsi.sense_len = io->scsiio.sense_len; 13047 msg.scsi.sense_residual = io->scsiio.sense_residual; 13048 msg.scsi.fetd_status = io->io_hdr.port_status; 13049 msg.scsi.residual = io->scsiio.residual; 13050 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13051 13052 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13053 ctl_failover_io(io, /*have_lock*/ have_lock); 13054 return; 13055 } 13056 13057 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 13058 if (isc_status > CTL_HA_STATUS_SUCCESS) { 13059 /* XXX do something if this fails */ 13060 } 13061 13062} 13063 13064/* 13065 * The DMA to the remote side is done, now we need to tell the other side 13066 * we're done so it can continue with its data movement. 13067 */ 13068static void 13069ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 13070{ 13071 union ctl_io *io; 13072 13073 io = rq->context; 13074 13075 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13076 printf("%s: ISC DMA write failed with error %d", __func__, 13077 rq->ret); 13078 ctl_set_internal_failure(&io->scsiio, 13079 /*sks_valid*/ 1, 13080 /*retry_count*/ rq->ret); 13081 } 13082 13083 ctl_dt_req_free(rq); 13084 13085 /* 13086 * In this case, we had to malloc the memory locally. Free it. 13087 */ 13088 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13089 int i; 13090 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13091 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13092 } 13093 /* 13094 * The data is in local and remote memory, so now we need to send 13095 * status (good or back) back to the other side. 13096 */ 13097 ctl_send_datamove_done(io, /*have_lock*/ 0); 13098} 13099 13100/* 13101 * We've moved the data from the host/controller into local memory. Now we 13102 * need to push it over to the remote controller's memory. 13103 */ 13104static int 13105ctl_datamove_remote_dm_write_cb(union ctl_io *io) 13106{ 13107 int retval; 13108 13109 retval = 0; 13110 13111 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 13112 ctl_datamove_remote_write_cb); 13113 13114 return (retval); 13115} 13116 13117static void 13118ctl_datamove_remote_write(union ctl_io *io) 13119{ 13120 int retval; 13121 void (*fe_datamove)(union ctl_io *io); 13122 13123 /* 13124 * - Get the data from the host/HBA into local memory. 13125 * - DMA memory from the local controller to the remote controller. 13126 * - Send status back to the remote controller. 13127 */ 13128 13129 retval = ctl_datamove_remote_sgl_setup(io); 13130 if (retval != 0) 13131 return; 13132 13133 /* Switch the pointer over so the FETD knows what to do */ 13134 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13135 13136 /* 13137 * Use a custom move done callback, since we need to send completion 13138 * back to the other controller, not to the backend on this side. 13139 */ 13140 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 13141 13142 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13143 13144 fe_datamove(io); 13145 13146 return; 13147 13148} 13149 13150static int 13151ctl_datamove_remote_dm_read_cb(union ctl_io *io) 13152{ 13153#if 0 13154 char str[256]; 13155 char path_str[64]; 13156 struct sbuf sb; 13157#endif 13158 13159 /* 13160 * In this case, we had to malloc the memory locally. Free it. 13161 */ 13162 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13163 int i; 13164 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13165 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13166 } 13167 13168#if 0 13169 scsi_path_string(io, path_str, sizeof(path_str)); 13170 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13171 sbuf_cat(&sb, path_str); 13172 scsi_command_string(&io->scsiio, NULL, &sb); 13173 sbuf_printf(&sb, "\n"); 13174 sbuf_cat(&sb, path_str); 13175 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13176 io->scsiio.tag_num, io->scsiio.tag_type); 13177 sbuf_cat(&sb, path_str); 13178 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 13179 io->io_hdr.flags, io->io_hdr.status); 13180 sbuf_finish(&sb); 13181 printk("%s", sbuf_data(&sb)); 13182#endif 13183 13184 13185 /* 13186 * The read is done, now we need to send status (good or bad) back 13187 * to the other side. 13188 */ 13189 ctl_send_datamove_done(io, /*have_lock*/ 0); 13190 13191 return (0); 13192} 13193 13194static void 13195ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13196{ 13197 union ctl_io *io; 13198 void (*fe_datamove)(union ctl_io *io); 13199 13200 io = rq->context; 13201 13202 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13203 printf("%s: ISC DMA read failed with error %d", __func__, 13204 rq->ret); 13205 ctl_set_internal_failure(&io->scsiio, 13206 /*sks_valid*/ 1, 13207 /*retry_count*/ rq->ret); 13208 } 13209 13210 ctl_dt_req_free(rq); 13211 13212 /* Switch the pointer over so the FETD knows what to do */ 13213 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13214 13215 /* 13216 * Use a custom move done callback, since we need to send completion 13217 * back to the other controller, not to the backend on this side. 13218 */ 13219 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13220 13221 /* XXX KDM add checks like the ones in ctl_datamove? */ 13222 13223 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13224 13225 fe_datamove(io); 13226} 13227 13228static int 13229ctl_datamove_remote_sgl_setup(union ctl_io *io) 13230{ 13231 struct ctl_sg_entry *local_sglist, *remote_sglist; 13232 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13233 struct ctl_softc *softc; 13234 int retval; 13235 int i; 13236 13237 retval = 0; 13238 softc = control_softc; 13239 13240 local_sglist = io->io_hdr.local_sglist; 13241 local_dma_sglist = io->io_hdr.local_dma_sglist; 13242 remote_sglist = io->io_hdr.remote_sglist; 13243 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13244 13245 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13246 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13247 local_sglist[i].len = remote_sglist[i].len; 13248 13249 /* 13250 * XXX Detect the situation where the RS-level I/O 13251 * redirector on the other side has already read the 13252 * data off of the AOR RS on this side, and 13253 * transferred it to remote (mirror) memory on the 13254 * other side. Since we already have the data in 13255 * memory here, we just need to use it. 13256 * 13257 * XXX KDM this can probably be removed once we 13258 * get the cache device code in and take the 13259 * current AOR implementation out. 13260 */ 13261#ifdef NEEDTOPORT 13262 if ((remote_sglist[i].addr >= 13263 (void *)vtophys(softc->mirr->addr)) 13264 && (remote_sglist[i].addr < 13265 ((void *)vtophys(softc->mirr->addr) + 13266 CacheMirrorOffset))) { 13267 local_sglist[i].addr = remote_sglist[i].addr - 13268 CacheMirrorOffset; 13269 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13270 CTL_FLAG_DATA_IN) 13271 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13272 } else { 13273 local_sglist[i].addr = remote_sglist[i].addr + 13274 CacheMirrorOffset; 13275 } 13276#endif 13277#if 0 13278 printf("%s: local %p, remote %p, len %d\n", 13279 __func__, local_sglist[i].addr, 13280 remote_sglist[i].addr, local_sglist[i].len); 13281#endif 13282 } 13283 } else { 13284 uint32_t len_to_go; 13285 13286 /* 13287 * In this case, we don't have automatically allocated 13288 * memory for this I/O on this controller. This typically 13289 * happens with internal CTL I/O -- e.g. inquiry, mode 13290 * sense, etc. Anything coming from RAIDCore will have 13291 * a mirror area available. 13292 */ 13293 len_to_go = io->scsiio.kern_data_len; 13294 13295 /* 13296 * Clear the no datasync flag, we have to use malloced 13297 * buffers. 13298 */ 13299 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13300 13301 /* 13302 * The difficult thing here is that the size of the various 13303 * S/G segments may be different than the size from the 13304 * remote controller. That'll make it harder when DMAing 13305 * the data back to the other side. 13306 */ 13307 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13308 sizeof(io->io_hdr.remote_sglist[0])) && 13309 (len_to_go > 0); i++) { 13310 local_sglist[i].len = ctl_min(len_to_go, 131072); 13311 CTL_SIZE_8B(local_dma_sglist[i].len, 13312 local_sglist[i].len); 13313 local_sglist[i].addr = 13314 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13315 13316 local_dma_sglist[i].addr = local_sglist[i].addr; 13317 13318 if (local_sglist[i].addr == NULL) { 13319 int j; 13320 13321 printf("malloc failed for %zd bytes!", 13322 local_dma_sglist[i].len); 13323 for (j = 0; j < i; j++) { 13324 free(local_sglist[j].addr, M_CTL); 13325 } 13326 ctl_set_internal_failure(&io->scsiio, 13327 /*sks_valid*/ 1, 13328 /*retry_count*/ 4857); 13329 retval = 1; 13330 goto bailout_error; 13331 13332 } 13333 /* XXX KDM do we need a sync here? */ 13334 13335 len_to_go -= local_sglist[i].len; 13336 } 13337 /* 13338 * Reset the number of S/G entries accordingly. The 13339 * original number of S/G entries is available in 13340 * rem_sg_entries. 13341 */ 13342 io->scsiio.kern_sg_entries = i; 13343 13344#if 0 13345 printf("%s: kern_sg_entries = %d\n", __func__, 13346 io->scsiio.kern_sg_entries); 13347 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13348 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13349 local_sglist[i].addr, local_sglist[i].len, 13350 local_dma_sglist[i].len); 13351#endif 13352 } 13353 13354 13355 return (retval); 13356 13357bailout_error: 13358 13359 ctl_send_datamove_done(io, /*have_lock*/ 0); 13360 13361 return (retval); 13362} 13363 13364static int 13365ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13366 ctl_ha_dt_cb callback) 13367{ 13368 struct ctl_ha_dt_req *rq; 13369 struct ctl_sg_entry *remote_sglist, *local_sglist; 13370 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13371 uint32_t local_used, remote_used, total_used; 13372 int retval; 13373 int i, j; 13374 13375 retval = 0; 13376 13377 rq = ctl_dt_req_alloc(); 13378 13379 /* 13380 * If we failed to allocate the request, and if the DMA didn't fail 13381 * anyway, set busy status. This is just a resource allocation 13382 * failure. 13383 */ 13384 if ((rq == NULL) 13385 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13386 ctl_set_busy(&io->scsiio); 13387 13388 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13389 13390 if (rq != NULL) 13391 ctl_dt_req_free(rq); 13392 13393 /* 13394 * The data move failed. We need to return status back 13395 * to the other controller. No point in trying to DMA 13396 * data to the remote controller. 13397 */ 13398 13399 ctl_send_datamove_done(io, /*have_lock*/ 0); 13400 13401 retval = 1; 13402 13403 goto bailout; 13404 } 13405 13406 local_sglist = io->io_hdr.local_sglist; 13407 local_dma_sglist = io->io_hdr.local_dma_sglist; 13408 remote_sglist = io->io_hdr.remote_sglist; 13409 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13410 local_used = 0; 13411 remote_used = 0; 13412 total_used = 0; 13413 13414 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13415 rq->ret = CTL_HA_STATUS_SUCCESS; 13416 rq->context = io; 13417 callback(rq); 13418 goto bailout; 13419 } 13420 13421 /* 13422 * Pull/push the data over the wire from/to the other controller. 13423 * This takes into account the possibility that the local and 13424 * remote sglists may not be identical in terms of the size of 13425 * the elements and the number of elements. 13426 * 13427 * One fundamental assumption here is that the length allocated for 13428 * both the local and remote sglists is identical. Otherwise, we've 13429 * essentially got a coding error of some sort. 13430 */ 13431 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13432 int isc_ret; 13433 uint32_t cur_len, dma_length; 13434 uint8_t *tmp_ptr; 13435 13436 rq->id = CTL_HA_DATA_CTL; 13437 rq->command = command; 13438 rq->context = io; 13439 13440 /* 13441 * Both pointers should be aligned. But it is possible 13442 * that the allocation length is not. They should both 13443 * also have enough slack left over at the end, though, 13444 * to round up to the next 8 byte boundary. 13445 */ 13446 cur_len = ctl_min(local_sglist[i].len - local_used, 13447 remote_sglist[j].len - remote_used); 13448 13449 /* 13450 * In this case, we have a size issue and need to decrease 13451 * the size, except in the case where we actually have less 13452 * than 8 bytes left. In that case, we need to increase 13453 * the DMA length to get the last bit. 13454 */ 13455 if ((cur_len & 0x7) != 0) { 13456 if (cur_len > 0x7) { 13457 cur_len = cur_len - (cur_len & 0x7); 13458 dma_length = cur_len; 13459 } else { 13460 CTL_SIZE_8B(dma_length, cur_len); 13461 } 13462 13463 } else 13464 dma_length = cur_len; 13465 13466 /* 13467 * If we had to allocate memory for this I/O, instead of using 13468 * the non-cached mirror memory, we'll need to flush the cache 13469 * before trying to DMA to the other controller. 13470 * 13471 * We could end up doing this multiple times for the same 13472 * segment if we have a larger local segment than remote 13473 * segment. That shouldn't be an issue. 13474 */ 13475 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13476 /* 13477 * XXX KDM use bus_dmamap_sync() here. 13478 */ 13479 } 13480 13481 rq->size = dma_length; 13482 13483 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13484 tmp_ptr += local_used; 13485 13486 /* Use physical addresses when talking to ISC hardware */ 13487 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13488 /* XXX KDM use busdma */ 13489#if 0 13490 rq->local = vtophys(tmp_ptr); 13491#endif 13492 } else 13493 rq->local = tmp_ptr; 13494 13495 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13496 tmp_ptr += remote_used; 13497 rq->remote = tmp_ptr; 13498 13499 rq->callback = NULL; 13500 13501 local_used += cur_len; 13502 if (local_used >= local_sglist[i].len) { 13503 i++; 13504 local_used = 0; 13505 } 13506 13507 remote_used += cur_len; 13508 if (remote_used >= remote_sglist[j].len) { 13509 j++; 13510 remote_used = 0; 13511 } 13512 total_used += cur_len; 13513 13514 if (total_used >= io->scsiio.kern_data_len) 13515 rq->callback = callback; 13516 13517 if ((rq->size & 0x7) != 0) { 13518 printf("%s: warning: size %d is not on 8b boundary\n", 13519 __func__, rq->size); 13520 } 13521 if (((uintptr_t)rq->local & 0x7) != 0) { 13522 printf("%s: warning: local %p not on 8b boundary\n", 13523 __func__, rq->local); 13524 } 13525 if (((uintptr_t)rq->remote & 0x7) != 0) { 13526 printf("%s: warning: remote %p not on 8b boundary\n", 13527 __func__, rq->local); 13528 } 13529#if 0 13530 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13531 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13532 rq->local, rq->remote, rq->size); 13533#endif 13534 13535 isc_ret = ctl_dt_single(rq); 13536 if (isc_ret == CTL_HA_STATUS_WAIT) 13537 continue; 13538 13539 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13540 rq->ret = CTL_HA_STATUS_SUCCESS; 13541 } else { 13542 rq->ret = isc_ret; 13543 } 13544 callback(rq); 13545 goto bailout; 13546 } 13547 13548bailout: 13549 return (retval); 13550 13551} 13552 13553static void 13554ctl_datamove_remote_read(union ctl_io *io) 13555{ 13556 int retval; 13557 int i; 13558 13559 /* 13560 * This will send an error to the other controller in the case of a 13561 * failure. 13562 */ 13563 retval = ctl_datamove_remote_sgl_setup(io); 13564 if (retval != 0) 13565 return; 13566 13567 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13568 ctl_datamove_remote_read_cb); 13569 if ((retval != 0) 13570 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13571 /* 13572 * Make sure we free memory if there was an error.. The 13573 * ctl_datamove_remote_xfer() function will send the 13574 * datamove done message, or call the callback with an 13575 * error if there is a problem. 13576 */ 13577 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13578 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13579 } 13580 13581 return; 13582} 13583 13584/* 13585 * Process a datamove request from the other controller. This is used for 13586 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13587 * first. Once that is complete, the data gets DMAed into the remote 13588 * controller's memory. For reads, we DMA from the remote controller's 13589 * memory into our memory first, and then move it out to the FETD. 13590 */ 13591static void 13592ctl_datamove_remote(union ctl_io *io) 13593{ 13594 struct ctl_softc *softc; 13595 13596 softc = control_softc; 13597 13598 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13599 13600 /* 13601 * Note that we look for an aborted I/O here, but don't do some of 13602 * the other checks that ctl_datamove() normally does. 13603 * We don't need to run the datamove delay code, since that should 13604 * have been done if need be on the other controller. 13605 */ 13606 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13607 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13608 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13609 io->io_hdr.nexus.targ_port, 13610 io->io_hdr.nexus.targ_target.id, 13611 io->io_hdr.nexus.targ_lun); 13612 io->io_hdr.port_status = 31338; 13613 ctl_send_datamove_done(io, /*have_lock*/ 0); 13614 return; 13615 } 13616 13617 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13618 ctl_datamove_remote_write(io); 13619 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13620 ctl_datamove_remote_read(io); 13621 } else { 13622 union ctl_ha_msg msg; 13623 struct scsi_sense_data *sense; 13624 uint8_t sks[3]; 13625 int retry_count; 13626 13627 memset(&msg, 0, sizeof(msg)); 13628 13629 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13630 msg.hdr.status = CTL_SCSI_ERROR; 13631 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13632 13633 retry_count = 4243; 13634 13635 sense = &msg.scsi.sense_data; 13636 sks[0] = SSD_SCS_VALID; 13637 sks[1] = (retry_count >> 8) & 0xff; 13638 sks[2] = retry_count & 0xff; 13639 13640 /* "Internal target failure" */ 13641 scsi_set_sense_data(sense, 13642 /*sense_format*/ SSD_TYPE_NONE, 13643 /*current_error*/ 1, 13644 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13645 /*asc*/ 0x44, 13646 /*ascq*/ 0x00, 13647 /*type*/ SSD_ELEM_SKS, 13648 /*size*/ sizeof(sks), 13649 /*data*/ sks, 13650 SSD_ELEM_NONE); 13651 13652 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13653 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13654 ctl_failover_io(io, /*have_lock*/ 1); 13655 return; 13656 } 13657 13658 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13659 CTL_HA_STATUS_SUCCESS) { 13660 /* XXX KDM what to do if this fails? */ 13661 } 13662 return; 13663 } 13664 13665} 13666 13667static int 13668ctl_process_done(union ctl_io *io) 13669{ 13670 struct ctl_lun *lun; 13671 struct ctl_softc *ctl_softc = control_softc; 13672 void (*fe_done)(union ctl_io *io); 13673 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13674 13675 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13676 13677 fe_done = 13678 control_softc->ctl_ports[targ_port]->fe_done; 13679 13680#ifdef CTL_TIME_IO 13681 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13682 char str[256]; 13683 char path_str[64]; 13684 struct sbuf sb; 13685 13686 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13687 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13688 13689 sbuf_cat(&sb, path_str); 13690 switch (io->io_hdr.io_type) { 13691 case CTL_IO_SCSI: 13692 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13693 sbuf_printf(&sb, "\n"); 13694 sbuf_cat(&sb, path_str); 13695 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13696 io->scsiio.tag_num, io->scsiio.tag_type); 13697 break; 13698 case CTL_IO_TASK: 13699 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13700 "Tag Type: %d\n", io->taskio.task_action, 13701 io->taskio.tag_num, io->taskio.tag_type); 13702 break; 13703 default: 13704 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13705 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13706 break; 13707 } 13708 sbuf_cat(&sb, path_str); 13709 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13710 (intmax_t)time_uptime - io->io_hdr.start_time); 13711 sbuf_finish(&sb); 13712 printf("%s", sbuf_data(&sb)); 13713 } 13714#endif /* CTL_TIME_IO */ 13715 13716 switch (io->io_hdr.io_type) { 13717 case CTL_IO_SCSI: 13718 break; 13719 case CTL_IO_TASK: 13720 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13721 ctl_io_error_print(io, NULL); 13722 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13723 ctl_free_io(io); 13724 else 13725 fe_done(io); 13726 return (CTL_RETVAL_COMPLETE); 13727 default: 13728 panic("ctl_process_done: invalid io type %d\n", 13729 io->io_hdr.io_type); 13730 break; /* NOTREACHED */ 13731 } 13732 13733 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13734 if (lun == NULL) { 13735 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13736 io->io_hdr.nexus.targ_mapped_lun)); 13737 goto bailout; 13738 } 13739 13740 mtx_lock(&lun->lun_lock); 13741 13742 /* 13743 * Check to see if we have any errors to inject here. We only 13744 * inject errors for commands that don't already have errors set. 13745 */ 13746 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13747 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13748 ctl_inject_error(lun, io); 13749 13750 /* 13751 * XXX KDM how do we treat commands that aren't completed 13752 * successfully? 13753 * 13754 * XXX KDM should we also track I/O latency? 13755 */ 13756 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13757 io->io_hdr.io_type == CTL_IO_SCSI) { 13758#ifdef CTL_TIME_IO 13759 struct bintime cur_bt; 13760#endif 13761 int type; 13762 13763 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13764 CTL_FLAG_DATA_IN) 13765 type = CTL_STATS_READ; 13766 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13767 CTL_FLAG_DATA_OUT) 13768 type = CTL_STATS_WRITE; 13769 else 13770 type = CTL_STATS_NO_IO; 13771 13772 lun->stats.ports[targ_port].bytes[type] += 13773 io->scsiio.kern_total_len; 13774 lun->stats.ports[targ_port].operations[type]++; 13775#ifdef CTL_TIME_IO 13776 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13777 &io->io_hdr.dma_bt); 13778 lun->stats.ports[targ_port].num_dmas[type] += 13779 io->io_hdr.num_dmas; 13780 getbintime(&cur_bt); 13781 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13782 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13783#endif 13784 } 13785 13786 /* 13787 * Remove this from the OOA queue. 13788 */ 13789 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13790 13791 /* 13792 * Run through the blocked queue on this LUN and see if anything 13793 * has become unblocked, now that this transaction is done. 13794 */ 13795 ctl_check_blocked(lun); 13796 13797 /* 13798 * If the LUN has been invalidated, free it if there is nothing 13799 * left on its OOA queue. 13800 */ 13801 if ((lun->flags & CTL_LUN_INVALID) 13802 && TAILQ_EMPTY(&lun->ooa_queue)) { 13803 mtx_unlock(&lun->lun_lock); 13804 mtx_lock(&ctl_softc->ctl_lock); 13805 ctl_free_lun(lun); 13806 mtx_unlock(&ctl_softc->ctl_lock); 13807 } else 13808 mtx_unlock(&lun->lun_lock); 13809 13810bailout: 13811 13812 /* 13813 * If this command has been aborted, make sure we set the status 13814 * properly. The FETD is responsible for freeing the I/O and doing 13815 * whatever it needs to do to clean up its state. 13816 */ 13817 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13818 ctl_set_task_aborted(&io->scsiio); 13819 13820 /* 13821 * If enabled, print command error status. 13822 * We don't print UAs unless debugging was enabled explicitly. 13823 */ 13824 do { 13825 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13826 break; 13827 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13828 break; 13829 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13830 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13831 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13832 int error_code, sense_key, asc, ascq; 13833 13834 scsi_extract_sense_len(&io->scsiio.sense_data, 13835 io->scsiio.sense_len, &error_code, &sense_key, 13836 &asc, &ascq, /*show_errors*/ 0); 13837 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13838 break; 13839 } 13840 13841 ctl_io_error_print(io, NULL); 13842 } while (0); 13843 13844 /* 13845 * Tell the FETD or the other shelf controller we're done with this 13846 * command. Note that only SCSI commands get to this point. Task 13847 * management commands are completed above. 13848 * 13849 * We only send status to the other controller if we're in XFER 13850 * mode. In SER_ONLY mode, the I/O is done on the controller that 13851 * received the I/O (from CTL's perspective), and so the status is 13852 * generated there. 13853 * 13854 * XXX KDM if we hold the lock here, we could cause a deadlock 13855 * if the frontend comes back in in this context to queue 13856 * something. 13857 */ 13858 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13859 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13860 union ctl_ha_msg msg; 13861 13862 memset(&msg, 0, sizeof(msg)); 13863 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13864 msg.hdr.original_sc = io->io_hdr.original_sc; 13865 msg.hdr.nexus = io->io_hdr.nexus; 13866 msg.hdr.status = io->io_hdr.status; 13867 msg.scsi.scsi_status = io->scsiio.scsi_status; 13868 msg.scsi.tag_num = io->scsiio.tag_num; 13869 msg.scsi.tag_type = io->scsiio.tag_type; 13870 msg.scsi.sense_len = io->scsiio.sense_len; 13871 msg.scsi.sense_residual = io->scsiio.sense_residual; 13872 msg.scsi.residual = io->scsiio.residual; 13873 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13874 sizeof(io->scsiio.sense_data)); 13875 /* 13876 * We copy this whether or not this is an I/O-related 13877 * command. Otherwise, we'd have to go and check to see 13878 * whether it's a read/write command, and it really isn't 13879 * worth it. 13880 */ 13881 memcpy(&msg.scsi.lbalen, 13882 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13883 sizeof(msg.scsi.lbalen)); 13884 13885 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13886 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13887 /* XXX do something here */ 13888 } 13889 13890 ctl_free_io(io); 13891 } else 13892 fe_done(io); 13893 13894 return (CTL_RETVAL_COMPLETE); 13895} 13896 13897#ifdef CTL_WITH_CA 13898/* 13899 * Front end should call this if it doesn't do autosense. When the request 13900 * sense comes back in from the initiator, we'll dequeue this and send it. 13901 */ 13902int 13903ctl_queue_sense(union ctl_io *io) 13904{ 13905 struct ctl_lun *lun; 13906 struct ctl_softc *ctl_softc; 13907 uint32_t initidx, targ_lun; 13908 13909 ctl_softc = control_softc; 13910 13911 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13912 13913 /* 13914 * LUN lookup will likely move to the ctl_work_thread() once we 13915 * have our new queueing infrastructure (that doesn't put things on 13916 * a per-LUN queue initially). That is so that we can handle 13917 * things like an INQUIRY to a LUN that we don't have enabled. We 13918 * can't deal with that right now. 13919 */ 13920 mtx_lock(&ctl_softc->ctl_lock); 13921 13922 /* 13923 * If we don't have a LUN for this, just toss the sense 13924 * information. 13925 */ 13926 targ_lun = io->io_hdr.nexus.targ_lun; 13927 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13928 if ((targ_lun < CTL_MAX_LUNS) 13929 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13930 lun = ctl_softc->ctl_luns[targ_lun]; 13931 else 13932 goto bailout; 13933 13934 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13935 13936 mtx_lock(&lun->lun_lock); 13937 /* 13938 * Already have CA set for this LUN...toss the sense information. 13939 */ 13940 if (ctl_is_set(lun->have_ca, initidx)) { 13941 mtx_unlock(&lun->lun_lock); 13942 goto bailout; 13943 } 13944 13945 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13946 ctl_min(sizeof(lun->pending_sense[initidx]), 13947 sizeof(io->scsiio.sense_data))); 13948 ctl_set_mask(lun->have_ca, initidx); 13949 mtx_unlock(&lun->lun_lock); 13950 13951bailout: 13952 mtx_unlock(&ctl_softc->ctl_lock); 13953 13954 ctl_free_io(io); 13955 13956 return (CTL_RETVAL_COMPLETE); 13957} 13958#endif 13959 13960/* 13961 * Primary command inlet from frontend ports. All SCSI and task I/O 13962 * requests must go through this function. 13963 */ 13964int 13965ctl_queue(union ctl_io *io) 13966{ 13967 struct ctl_softc *ctl_softc; 13968 13969 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13970 13971 ctl_softc = control_softc; 13972 13973#ifdef CTL_TIME_IO 13974 io->io_hdr.start_time = time_uptime; 13975 getbintime(&io->io_hdr.start_bt); 13976#endif /* CTL_TIME_IO */ 13977 13978 /* Map FE-specific LUN ID into global one. */ 13979 io->io_hdr.nexus.targ_mapped_lun = 13980 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13981 13982 switch (io->io_hdr.io_type) { 13983 case CTL_IO_SCSI: 13984 case CTL_IO_TASK: 13985 if (ctl_debug & CTL_DEBUG_CDB) 13986 ctl_io_print(io); 13987 ctl_enqueue_incoming(io); 13988 break; 13989 default: 13990 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13991 return (EINVAL); 13992 } 13993 13994 return (CTL_RETVAL_COMPLETE); 13995} 13996 13997#ifdef CTL_IO_DELAY 13998static void 13999ctl_done_timer_wakeup(void *arg) 14000{ 14001 union ctl_io *io; 14002 14003 io = (union ctl_io *)arg; 14004 ctl_done(io); 14005} 14006#endif /* CTL_IO_DELAY */ 14007 14008void 14009ctl_done(union ctl_io *io) 14010{ 14011 struct ctl_softc *ctl_softc; 14012 14013 ctl_softc = control_softc; 14014 14015 /* 14016 * Enable this to catch duplicate completion issues. 14017 */ 14018#if 0 14019 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 14020 printf("%s: type %d msg %d cdb %x iptl: " 14021 "%d:%d:%d:%d tag 0x%04x " 14022 "flag %#x status %x\n", 14023 __func__, 14024 io->io_hdr.io_type, 14025 io->io_hdr.msg_type, 14026 io->scsiio.cdb[0], 14027 io->io_hdr.nexus.initid.id, 14028 io->io_hdr.nexus.targ_port, 14029 io->io_hdr.nexus.targ_target.id, 14030 io->io_hdr.nexus.targ_lun, 14031 (io->io_hdr.io_type == 14032 CTL_IO_TASK) ? 14033 io->taskio.tag_num : 14034 io->scsiio.tag_num, 14035 io->io_hdr.flags, 14036 io->io_hdr.status); 14037 } else 14038 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 14039#endif 14040 14041 /* 14042 * This is an internal copy of an I/O, and should not go through 14043 * the normal done processing logic. 14044 */ 14045 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 14046 return; 14047 14048 /* 14049 * We need to send a msg to the serializing shelf to finish the IO 14050 * as well. We don't send a finish message to the other shelf if 14051 * this is a task management command. Task management commands 14052 * aren't serialized in the OOA queue, but rather just executed on 14053 * both shelf controllers for commands that originated on that 14054 * controller. 14055 */ 14056 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 14057 && (io->io_hdr.io_type != CTL_IO_TASK)) { 14058 union ctl_ha_msg msg_io; 14059 14060 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 14061 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 14062 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 14063 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 14064 } 14065 /* continue on to finish IO */ 14066 } 14067#ifdef CTL_IO_DELAY 14068 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 14069 struct ctl_lun *lun; 14070 14071 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14072 14073 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 14074 } else { 14075 struct ctl_lun *lun; 14076 14077 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14078 14079 if ((lun != NULL) 14080 && (lun->delay_info.done_delay > 0)) { 14081 struct callout *callout; 14082 14083 callout = (struct callout *)&io->io_hdr.timer_bytes; 14084 callout_init(callout, /*mpsafe*/ 1); 14085 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 14086 callout_reset(callout, 14087 lun->delay_info.done_delay * hz, 14088 ctl_done_timer_wakeup, io); 14089 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 14090 lun->delay_info.done_delay = 0; 14091 return; 14092 } 14093 } 14094#endif /* CTL_IO_DELAY */ 14095 14096 ctl_enqueue_done(io); 14097} 14098 14099int 14100ctl_isc(struct ctl_scsiio *ctsio) 14101{ 14102 struct ctl_lun *lun; 14103 int retval; 14104 14105 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14106 14107 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 14108 14109 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 14110 14111 retval = lun->backend->data_submit((union ctl_io *)ctsio); 14112 14113 return (retval); 14114} 14115 14116 14117static void 14118ctl_work_thread(void *arg) 14119{ 14120 struct ctl_thread *thr = (struct ctl_thread *)arg; 14121 struct ctl_softc *softc = thr->ctl_softc; 14122 union ctl_io *io; 14123 int retval; 14124 14125 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 14126 14127 for (;;) { 14128 retval = 0; 14129 14130 /* 14131 * We handle the queues in this order: 14132 * - ISC 14133 * - done queue (to free up resources, unblock other commands) 14134 * - RtR queue 14135 * - incoming queue 14136 * 14137 * If those queues are empty, we break out of the loop and 14138 * go to sleep. 14139 */ 14140 mtx_lock(&thr->queue_lock); 14141 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 14142 if (io != NULL) { 14143 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 14144 mtx_unlock(&thr->queue_lock); 14145 ctl_handle_isc(io); 14146 continue; 14147 } 14148 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 14149 if (io != NULL) { 14150 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 14151 /* clear any blocked commands, call fe_done */ 14152 mtx_unlock(&thr->queue_lock); 14153 retval = ctl_process_done(io); 14154 continue; 14155 } 14156 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 14157 if (io != NULL) { 14158 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 14159 mtx_unlock(&thr->queue_lock); 14160 if (io->io_hdr.io_type == CTL_IO_TASK) 14161 ctl_run_task(io); 14162 else 14163 ctl_scsiio_precheck(softc, &io->scsiio); 14164 continue; 14165 } 14166 if (!ctl_pause_rtr) { 14167 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 14168 if (io != NULL) { 14169 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14170 mtx_unlock(&thr->queue_lock); 14171 retval = ctl_scsiio(&io->scsiio); 14172 if (retval != CTL_RETVAL_COMPLETE) 14173 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14174 continue; 14175 } 14176 } 14177 14178 /* Sleep until we have something to do. */ 14179 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 14180 } 14181} 14182 14183static void 14184ctl_lun_thread(void *arg) 14185{ 14186 struct ctl_softc *softc = (struct ctl_softc *)arg; 14187 struct ctl_be_lun *be_lun; 14188 int retval; 14189 14190 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14191 14192 for (;;) { 14193 retval = 0; 14194 mtx_lock(&softc->ctl_lock); 14195 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14196 if (be_lun != NULL) { 14197 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14198 mtx_unlock(&softc->ctl_lock); 14199 ctl_create_lun(be_lun); 14200 continue; 14201 } 14202 14203 /* Sleep until we have something to do. */ 14204 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14205 PDROP | PRIBIO, "-", 0); 14206 } 14207} 14208 14209static void 14210ctl_thresh_thread(void *arg) 14211{ 14212 struct ctl_softc *softc = (struct ctl_softc *)arg; 14213 struct ctl_lun *lun; 14214 struct ctl_be_lun *be_lun; 14215 struct scsi_da_rw_recovery_page *rwpage; 14216 struct ctl_logical_block_provisioning_page *page; 14217 const char *attr; 14218 uint64_t thres, val; 14219 int i, e; 14220 14221 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 14222 14223 for (;;) { 14224 mtx_lock(&softc->ctl_lock); 14225 STAILQ_FOREACH(lun, &softc->lun_list, links) { 14226 be_lun = lun->be_lun; 14227 if ((lun->flags & CTL_LUN_DISABLED) || 14228 (lun->flags & CTL_LUN_OFFLINE) || 14229 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 14230 lun->backend->lun_attr == NULL) 14231 continue; 14232 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 14233 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 14234 continue; 14235 e = 0; 14236 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 14237 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 14238 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 14239 continue; 14240 thres = scsi_4btoul(page->descr[i].count); 14241 thres <<= CTL_LBP_EXPONENT; 14242 switch (page->descr[i].resource) { 14243 case 0x01: 14244 attr = "blocksavail"; 14245 break; 14246 case 0x02: 14247 attr = "blocksused"; 14248 break; 14249 case 0xf1: 14250 attr = "poolblocksavail"; 14251 break; 14252 case 0xf2: 14253 attr = "poolblocksused"; 14254 break; 14255 default: 14256 continue; 14257 } 14258 mtx_unlock(&softc->ctl_lock); // XXX 14259 val = lun->backend->lun_attr( 14260 lun->be_lun->be_lun, attr); 14261 mtx_lock(&softc->ctl_lock); 14262 if (val == UINT64_MAX) 14263 continue; 14264 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 14265 == SLBPPD_ARMING_INC) 14266 e |= (val >= thres); 14267 else 14268 e |= (val <= thres); 14269 } 14270 mtx_lock(&lun->lun_lock); 14271 if (e) { 14272 if (lun->lasttpt == 0 || 14273 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 14274 lun->lasttpt = time_uptime; 14275 for (i = 0; i < CTL_MAX_INITIATORS; i++) 14276 lun->pending_ua[i] |= 14277 CTL_UA_THIN_PROV_THRES; 14278 } 14279 } else { 14280 lun->lasttpt = 0; 14281 for (i = 0; i < CTL_MAX_INITIATORS; i++) 14282 lun->pending_ua[i] &= ~CTL_UA_THIN_PROV_THRES; 14283 } 14284 mtx_unlock(&lun->lun_lock); 14285 } 14286 mtx_unlock(&softc->ctl_lock); 14287 pause("-", CTL_LBP_PERIOD * hz); 14288 } 14289} 14290 14291static void 14292ctl_enqueue_incoming(union ctl_io *io) 14293{ 14294 struct ctl_softc *softc = control_softc; 14295 struct ctl_thread *thr; 14296 u_int idx; 14297 14298 idx = (io->io_hdr.nexus.targ_port * 127 + 14299 io->io_hdr.nexus.initid.id) % worker_threads; 14300 thr = &softc->threads[idx]; 14301 mtx_lock(&thr->queue_lock); 14302 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14303 mtx_unlock(&thr->queue_lock); 14304 wakeup(thr); 14305} 14306 14307static void 14308ctl_enqueue_rtr(union ctl_io *io) 14309{ 14310 struct ctl_softc *softc = control_softc; 14311 struct ctl_thread *thr; 14312 14313 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14314 mtx_lock(&thr->queue_lock); 14315 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14316 mtx_unlock(&thr->queue_lock); 14317 wakeup(thr); 14318} 14319 14320static void 14321ctl_enqueue_done(union ctl_io *io) 14322{ 14323 struct ctl_softc *softc = control_softc; 14324 struct ctl_thread *thr; 14325 14326 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14327 mtx_lock(&thr->queue_lock); 14328 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14329 mtx_unlock(&thr->queue_lock); 14330 wakeup(thr); 14331} 14332 14333static void 14334ctl_enqueue_isc(union ctl_io *io) 14335{ 14336 struct ctl_softc *softc = control_softc; 14337 struct ctl_thread *thr; 14338 14339 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14340 mtx_lock(&thr->queue_lock); 14341 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14342 mtx_unlock(&thr->queue_lock); 14343 wakeup(thr); 14344} 14345 14346/* Initialization and failover */ 14347 14348void 14349ctl_init_isc_msg(void) 14350{ 14351 printf("CTL: Still calling this thing\n"); 14352} 14353 14354/* 14355 * Init component 14356 * Initializes component into configuration defined by bootMode 14357 * (see hasc-sv.c) 14358 * returns hasc_Status: 14359 * OK 14360 * ERROR - fatal error 14361 */ 14362static ctl_ha_comp_status 14363ctl_isc_init(struct ctl_ha_component *c) 14364{ 14365 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14366 14367 c->status = ret; 14368 return ret; 14369} 14370 14371/* Start component 14372 * Starts component in state requested. If component starts successfully, 14373 * it must set its own state to the requestrd state 14374 * When requested state is HASC_STATE_HA, the component may refine it 14375 * by adding _SLAVE or _MASTER flags. 14376 * Currently allowed state transitions are: 14377 * UNKNOWN->HA - initial startup 14378 * UNKNOWN->SINGLE - initial startup when no parter detected 14379 * HA->SINGLE - failover 14380 * returns ctl_ha_comp_status: 14381 * OK - component successfully started in requested state 14382 * FAILED - could not start the requested state, failover may 14383 * be possible 14384 * ERROR - fatal error detected, no future startup possible 14385 */ 14386static ctl_ha_comp_status 14387ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14388{ 14389 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14390 14391 printf("%s: go\n", __func__); 14392 14393 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14394 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14395 ctl_is_single = 0; 14396 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14397 != CTL_HA_STATUS_SUCCESS) { 14398 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14399 ret = CTL_HA_COMP_STATUS_ERROR; 14400 } 14401 } else if (CTL_HA_STATE_IS_HA(c->state) 14402 && CTL_HA_STATE_IS_SINGLE(state)){ 14403 // HA->SINGLE transition 14404 ctl_failover(); 14405 ctl_is_single = 1; 14406 } else { 14407 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14408 c->state, state); 14409 ret = CTL_HA_COMP_STATUS_ERROR; 14410 } 14411 if (CTL_HA_STATE_IS_SINGLE(state)) 14412 ctl_is_single = 1; 14413 14414 c->state = state; 14415 c->status = ret; 14416 return ret; 14417} 14418 14419/* 14420 * Quiesce component 14421 * The component must clear any error conditions (set status to OK) and 14422 * prepare itself to another Start call 14423 * returns ctl_ha_comp_status: 14424 * OK 14425 * ERROR 14426 */ 14427static ctl_ha_comp_status 14428ctl_isc_quiesce(struct ctl_ha_component *c) 14429{ 14430 int ret = CTL_HA_COMP_STATUS_OK; 14431 14432 ctl_pause_rtr = 1; 14433 c->status = ret; 14434 return ret; 14435} 14436 14437struct ctl_ha_component ctl_ha_component_ctlisc = 14438{ 14439 .name = "CTL ISC", 14440 .state = CTL_HA_STATE_UNKNOWN, 14441 .init = ctl_isc_init, 14442 .start = ctl_isc_start, 14443 .quiesce = ctl_isc_quiesce 14444}; 14445 14446/* 14447 * vim: ts=8 14448 */ 14449