ctl.c revision 288769
1110566Smike/*- 2110566Smike * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3141379Sdas * Copyright (c) 2012 The FreeBSD Foundation 4110566Smike * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org> 5110566Smike * All rights reserved. 6110566Smike * 7110566Smike * Portions of this software were developed by Edward Tomasz Napierala 8110566Smike * under sponsorship from the FreeBSD Foundation. 9110566Smike * 10110566Smike * Redistribution and use in source and binary forms, with or without 11110566Smike * modification, are permitted provided that the following conditions 12110566Smike * are met: 13110566Smike * 1. Redistributions of source code must retain the above copyright 14110566Smike * notice, this list of conditions, and the following disclaimer, 15110566Smike * without modification. 16110566Smike * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17110566Smike * substantially similar to the "NO WARRANTY" disclaimer below 18110566Smike * ("Disclaimer") and any redistribution must be conditioned upon 19110566Smike * including a substantially similar Disclaimer requirement for further 20110566Smike * binary redistribution. 21110566Smike * 22110566Smike * NO WARRANTY 23110566Smike * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24110566Smike * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25110566Smike * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26110566Smike * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27110566Smike * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28110566Smike * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29110566Smike * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30110566Smike * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31110566Smike * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32110566Smike * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33186461Smarcel * POSSIBILITY OF SUCH DAMAGES. 34186461Smarcel * 35186461Smarcel * $Id$ 36186461Smarcel */ 37110566Smike/* 38110566Smike * CAM Target Layer, a SCSI device emulation subsystem. 39110566Smike * 40110566Smike * Author: Ken Merry <ken@FreeBSD.org> 41110566Smike */ 42110566Smike 43110566Smike#define _CTL_C 44110566Smike 45110566Smike#include <sys/cdefs.h> 46110566Smike__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 288769 2015-10-05 10:36:28Z mav $"); 47110566Smike 48110566Smike#include <sys/param.h> 49110566Smike#include <sys/systm.h> 50110566Smike#include <sys/ctype.h> 51110566Smike#include <sys/kernel.h> 52124655Sdas#include <sys/types.h> 53124655Sdas#include <sys/kthread.h> 54124655Sdas#include <sys/bio.h> 55110566Smike#include <sys/fcntl.h> 56110566Smike#include <sys/lock.h> 57110566Smike#include <sys/module.h> 58110566Smike#include <sys/mutex.h> 59186461Smarcel#include <sys/condvar.h> 60110566Smike#include <sys/malloc.h> 61186461Smarcel#include <sys/conf.h> 62110566Smike#include <sys/ioccom.h> 63110566Smike#include <sys/queue.h> 64110566Smike#include <sys/sbuf.h> 65186461Smarcel#include <sys/smp.h> 66186461Smarcel#include <sys/endian.h> 67186461Smarcel#include <sys/sysctl.h> 68110566Smike#include <vm/uma.h> 69110566Smike 70110566Smike#include <cam/cam.h> 71110566Smike#include <cam/scsi/scsi_all.h> 72110566Smike#include <cam/scsi/scsi_da.h> 73110566Smike#include <cam/ctl/ctl_io.h> 74110566Smike#include <cam/ctl/ctl.h> 75110566Smike#include <cam/ctl/ctl_frontend.h> 76#include <cam/ctl/ctl_util.h> 77#include <cam/ctl/ctl_backend.h> 78#include <cam/ctl/ctl_ioctl.h> 79#include <cam/ctl/ctl_ha.h> 80#include <cam/ctl/ctl_private.h> 81#include <cam/ctl/ctl_debug.h> 82#include <cam/ctl/ctl_scsi_all.h> 83#include <cam/ctl/ctl_error.h> 84 85struct ctl_softc *control_softc = NULL; 86 87/* 88 * Template mode pages. 89 */ 90 91/* 92 * Note that these are default values only. The actual values will be 93 * filled in when the user does a mode sense. 94 */ 95const static struct copan_debugconf_subpage debugconf_page_default = { 96 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 97 DBGCNF_SUBPAGE_CODE, /* subpage */ 98 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 99 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 100 DBGCNF_VERSION, /* page_version */ 101 {CTL_TIME_IO_DEFAULT_SECS>>8, 102 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 103}; 104 105const static struct copan_debugconf_subpage debugconf_page_changeable = { 106 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 107 DBGCNF_SUBPAGE_CODE, /* subpage */ 108 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 109 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 110 0, /* page_version */ 111 {0xff,0xff}, /* ctl_time_io_secs */ 112}; 113 114const static struct scsi_da_rw_recovery_page rw_er_page_default = { 115 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 116 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 117 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 118 /*read_retry_count*/0, 119 /*correction_span*/0, 120 /*head_offset_count*/0, 121 /*data_strobe_offset_cnt*/0, 122 /*byte8*/SMS_RWER_LBPERE, 123 /*write_retry_count*/0, 124 /*reserved2*/0, 125 /*recovery_time_limit*/{0, 0}, 126}; 127 128const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 129 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 130 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 131 /*byte3*/0, 132 /*read_retry_count*/0, 133 /*correction_span*/0, 134 /*head_offset_count*/0, 135 /*data_strobe_offset_cnt*/0, 136 /*byte8*/0, 137 /*write_retry_count*/0, 138 /*reserved2*/0, 139 /*recovery_time_limit*/{0, 0}, 140}; 141 142const static struct scsi_format_page format_page_default = { 143 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 144 /*page_length*/sizeof(struct scsi_format_page) - 2, 145 /*tracks_per_zone*/ {0, 0}, 146 /*alt_sectors_per_zone*/ {0, 0}, 147 /*alt_tracks_per_zone*/ {0, 0}, 148 /*alt_tracks_per_lun*/ {0, 0}, 149 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 150 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 151 /*bytes_per_sector*/ {0, 0}, 152 /*interleave*/ {0, 0}, 153 /*track_skew*/ {0, 0}, 154 /*cylinder_skew*/ {0, 0}, 155 /*flags*/ SFP_HSEC, 156 /*reserved*/ {0, 0, 0} 157}; 158 159const static struct scsi_format_page format_page_changeable = { 160 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 161 /*page_length*/sizeof(struct scsi_format_page) - 2, 162 /*tracks_per_zone*/ {0, 0}, 163 /*alt_sectors_per_zone*/ {0, 0}, 164 /*alt_tracks_per_zone*/ {0, 0}, 165 /*alt_tracks_per_lun*/ {0, 0}, 166 /*sectors_per_track*/ {0, 0}, 167 /*bytes_per_sector*/ {0, 0}, 168 /*interleave*/ {0, 0}, 169 /*track_skew*/ {0, 0}, 170 /*cylinder_skew*/ {0, 0}, 171 /*flags*/ 0, 172 /*reserved*/ {0, 0, 0} 173}; 174 175const static struct scsi_rigid_disk_page rigid_disk_page_default = { 176 /*page_code*/SMS_RIGID_DISK_PAGE, 177 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 178 /*cylinders*/ {0, 0, 0}, 179 /*heads*/ CTL_DEFAULT_HEADS, 180 /*start_write_precomp*/ {0, 0, 0}, 181 /*start_reduced_current*/ {0, 0, 0}, 182 /*step_rate*/ {0, 0}, 183 /*landing_zone_cylinder*/ {0, 0, 0}, 184 /*rpl*/ SRDP_RPL_DISABLED, 185 /*rotational_offset*/ 0, 186 /*reserved1*/ 0, 187 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 188 CTL_DEFAULT_ROTATION_RATE & 0xff}, 189 /*reserved2*/ {0, 0} 190}; 191 192const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 193 /*page_code*/SMS_RIGID_DISK_PAGE, 194 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 195 /*cylinders*/ {0, 0, 0}, 196 /*heads*/ 0, 197 /*start_write_precomp*/ {0, 0, 0}, 198 /*start_reduced_current*/ {0, 0, 0}, 199 /*step_rate*/ {0, 0}, 200 /*landing_zone_cylinder*/ {0, 0, 0}, 201 /*rpl*/ 0, 202 /*rotational_offset*/ 0, 203 /*reserved1*/ 0, 204 /*rotation_rate*/ {0, 0}, 205 /*reserved2*/ {0, 0} 206}; 207 208const static struct scsi_caching_page caching_page_default = { 209 /*page_code*/SMS_CACHING_PAGE, 210 /*page_length*/sizeof(struct scsi_caching_page) - 2, 211 /*flags1*/ SCP_DISC | SCP_WCE, 212 /*ret_priority*/ 0, 213 /*disable_pf_transfer_len*/ {0xff, 0xff}, 214 /*min_prefetch*/ {0, 0}, 215 /*max_prefetch*/ {0xff, 0xff}, 216 /*max_pf_ceiling*/ {0xff, 0xff}, 217 /*flags2*/ 0, 218 /*cache_segments*/ 0, 219 /*cache_seg_size*/ {0, 0}, 220 /*reserved*/ 0, 221 /*non_cache_seg_size*/ {0, 0, 0} 222}; 223 224const static struct scsi_caching_page caching_page_changeable = { 225 /*page_code*/SMS_CACHING_PAGE, 226 /*page_length*/sizeof(struct scsi_caching_page) - 2, 227 /*flags1*/ SCP_WCE | SCP_RCD, 228 /*ret_priority*/ 0, 229 /*disable_pf_transfer_len*/ {0, 0}, 230 /*min_prefetch*/ {0, 0}, 231 /*max_prefetch*/ {0, 0}, 232 /*max_pf_ceiling*/ {0, 0}, 233 /*flags2*/ 0, 234 /*cache_segments*/ 0, 235 /*cache_seg_size*/ {0, 0}, 236 /*reserved*/ 0, 237 /*non_cache_seg_size*/ {0, 0, 0} 238}; 239 240const static struct scsi_control_page control_page_default = { 241 /*page_code*/SMS_CONTROL_MODE_PAGE, 242 /*page_length*/sizeof(struct scsi_control_page) - 2, 243 /*rlec*/0, 244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 245 /*eca_and_aen*/0, 246 /*flags4*/SCP_TAS, 247 /*aen_holdoff_period*/{0, 0}, 248 /*busy_timeout_period*/{0, 0}, 249 /*extended_selftest_completion_time*/{0, 0} 250}; 251 252const static struct scsi_control_page control_page_changeable = { 253 /*page_code*/SMS_CONTROL_MODE_PAGE, 254 /*page_length*/sizeof(struct scsi_control_page) - 2, 255 /*rlec*/SCP_DSENSE, 256 /*queue_flags*/SCP_QUEUE_ALG_MASK, 257 /*eca_and_aen*/SCP_SWP, 258 /*flags4*/0, 259 /*aen_holdoff_period*/{0, 0}, 260 /*busy_timeout_period*/{0, 0}, 261 /*extended_selftest_completion_time*/{0, 0} 262}; 263 264const static struct scsi_info_exceptions_page ie_page_default = { 265 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 266 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 267 /*info_flags*/SIEP_FLAGS_DEXCPT, 268 /*mrie*/0, 269 /*interval_timer*/{0, 0, 0, 0}, 270 /*report_count*/{0, 0, 0, 0} 271}; 272 273const static struct scsi_info_exceptions_page ie_page_changeable = { 274 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 275 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 276 /*info_flags*/0, 277 /*mrie*/0, 278 /*interval_timer*/{0, 0, 0, 0}, 279 /*report_count*/{0, 0, 0, 0} 280}; 281 282#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 283 284const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 286 /*subpage_code*/0x02, 287 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 288 /*flags*/0, 289 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 290 /*descr*/{}}, 291 {{/*flags*/0, 292 /*resource*/0x01, 293 /*reserved*/{0, 0}, 294 /*count*/{0, 0, 0, 0}}, 295 {/*flags*/0, 296 /*resource*/0x02, 297 /*reserved*/{0, 0}, 298 /*count*/{0, 0, 0, 0}}, 299 {/*flags*/0, 300 /*resource*/0xf1, 301 /*reserved*/{0, 0}, 302 /*count*/{0, 0, 0, 0}}, 303 {/*flags*/0, 304 /*resource*/0xf2, 305 /*reserved*/{0, 0}, 306 /*count*/{0, 0, 0, 0}} 307 } 308}; 309 310const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 311 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 312 /*subpage_code*/0x02, 313 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 314 /*flags*/0, 315 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 316 /*descr*/{}}, 317 {{/*flags*/0, 318 /*resource*/0, 319 /*reserved*/{0, 0}, 320 /*count*/{0, 0, 0, 0}}, 321 {/*flags*/0, 322 /*resource*/0, 323 /*reserved*/{0, 0}, 324 /*count*/{0, 0, 0, 0}}, 325 {/*flags*/0, 326 /*resource*/0, 327 /*reserved*/{0, 0}, 328 /*count*/{0, 0, 0, 0}}, 329 {/*flags*/0, 330 /*resource*/0, 331 /*reserved*/{0, 0}, 332 /*count*/{0, 0, 0, 0}} 333 } 334}; 335 336SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 337static int worker_threads = -1; 338TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 339SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 340 &worker_threads, 1, "Number of worker threads"); 341static int ctl_debug = CTL_DEBUG_NONE; 342TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug); 343SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 344 &ctl_debug, 0, "Enabled debug flags"); 345 346/* 347 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 348 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 349 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 350 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 351 */ 352#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 353 354static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 355 int param); 356static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 357static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 358static int ctl_init(void); 359void ctl_shutdown(void); 360static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 361static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 362static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 363static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 364 struct ctl_ooa *ooa_hdr, 365 struct ctl_ooa_entry *kern_entries); 366static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 367 struct thread *td); 368static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 369 struct ctl_be_lun *be_lun); 370static int ctl_free_lun(struct ctl_lun *lun); 371static void ctl_create_lun(struct ctl_be_lun *be_lun); 372static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 373 374static int ctl_do_mode_select(union ctl_io *io); 375static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 376 uint64_t res_key, uint64_t sa_res_key, 377 uint8_t type, uint32_t residx, 378 struct ctl_scsiio *ctsio, 379 struct scsi_per_res_out *cdb, 380 struct scsi_per_res_out_parms* param); 381static void ctl_pro_preempt_other(struct ctl_lun *lun, 382 union ctl_ha_msg *msg); 383static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 384static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 385static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 386static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 387static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 388static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 389static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 390 int alloc_len); 391static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 392 int alloc_len); 393static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 394static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 395static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 396static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 397static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 398static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 399 bool seq); 400static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 401static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 402 union ctl_io *pending_io, union ctl_io *ooa_io); 403static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 404 union ctl_io *starting_io); 405static int ctl_check_blocked(struct ctl_lun *lun); 406static int ctl_scsiio_lun_check(struct ctl_lun *lun, 407 const struct ctl_cmd_entry *entry, 408 struct ctl_scsiio *ctsio); 409static void ctl_failover_lun(struct ctl_lun *lun); 410static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 411 struct ctl_scsiio *ctsio); 412static int ctl_scsiio(struct ctl_scsiio *ctsio); 413 414static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 415static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 416 ctl_ua_type ua_type); 417static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, 418 ctl_ua_type ua_type); 419static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 420static int ctl_abort_task(union ctl_io *io); 421static int ctl_abort_task_set(union ctl_io *io); 422static int ctl_query_task(union ctl_io *io, int task_set); 423static int ctl_i_t_nexus_reset(union ctl_io *io); 424static int ctl_query_async_event(union ctl_io *io); 425static void ctl_run_task(union ctl_io *io); 426#ifdef CTL_IO_DELAY 427static void ctl_datamove_timer_wakeup(void *arg); 428static void ctl_done_timer_wakeup(void *arg); 429#endif /* CTL_IO_DELAY */ 430 431static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 432static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 433static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 434static void ctl_datamove_remote_write(union ctl_io *io); 435static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 436static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 437static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 438static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 439 ctl_ha_dt_cb callback); 440static void ctl_datamove_remote_read(union ctl_io *io); 441static void ctl_datamove_remote(union ctl_io *io); 442static int ctl_process_done(union ctl_io *io); 443static void ctl_lun_thread(void *arg); 444static void ctl_thresh_thread(void *arg); 445static void ctl_work_thread(void *arg); 446static void ctl_enqueue_incoming(union ctl_io *io); 447static void ctl_enqueue_rtr(union ctl_io *io); 448static void ctl_enqueue_done(union ctl_io *io); 449static void ctl_enqueue_isc(union ctl_io *io); 450static const struct ctl_cmd_entry * 451 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 452static const struct ctl_cmd_entry * 453 ctl_validate_command(struct ctl_scsiio *ctsio); 454static int ctl_cmd_applicable(uint8_t lun_type, 455 const struct ctl_cmd_entry *entry); 456 457static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 458static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 459static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 460static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 461 462/* 463 * Load the serialization table. This isn't very pretty, but is probably 464 * the easiest way to do it. 465 */ 466#include "ctl_ser_table.c" 467 468/* 469 * We only need to define open, close and ioctl routines for this driver. 470 */ 471static struct cdevsw ctl_cdevsw = { 472 .d_version = D_VERSION, 473 .d_flags = 0, 474 .d_open = ctl_open, 475 .d_close = ctl_close, 476 .d_ioctl = ctl_ioctl, 477 .d_name = "ctl", 478}; 479 480 481MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 482 483static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 484 485static moduledata_t ctl_moduledata = { 486 "ctl", 487 ctl_module_event_handler, 488 NULL 489}; 490 491DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 492MODULE_VERSION(ctl, 1); 493 494static struct ctl_frontend ha_frontend = 495{ 496 .name = "ha", 497}; 498 499static void 500ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 501 union ctl_ha_msg *msg_info) 502{ 503 struct ctl_scsiio *ctsio; 504 505 if (msg_info->hdr.original_sc == NULL) { 506 printf("%s: original_sc == NULL!\n", __func__); 507 /* XXX KDM now what? */ 508 return; 509 } 510 511 ctsio = &msg_info->hdr.original_sc->scsiio; 512 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 513 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 514 ctsio->io_hdr.status = msg_info->hdr.status; 515 ctsio->scsi_status = msg_info->scsi.scsi_status; 516 ctsio->sense_len = msg_info->scsi.sense_len; 517 ctsio->sense_residual = msg_info->scsi.sense_residual; 518 ctsio->residual = msg_info->scsi.residual; 519 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 520 msg_info->scsi.sense_len); 521 ctl_enqueue_isc((union ctl_io *)ctsio); 522} 523 524static void 525ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 526 union ctl_ha_msg *msg_info) 527{ 528 struct ctl_scsiio *ctsio; 529 530 if (msg_info->hdr.serializing_sc == NULL) { 531 printf("%s: serializing_sc == NULL!\n", __func__); 532 /* XXX KDM now what? */ 533 return; 534 } 535 536 ctsio = &msg_info->hdr.serializing_sc->scsiio; 537 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 538 ctl_enqueue_isc((union ctl_io *)ctsio); 539} 540 541void 542ctl_isc_announce_lun(struct ctl_lun *lun) 543{ 544 struct ctl_softc *softc = lun->ctl_softc; 545 union ctl_ha_msg *msg; 546 struct ctl_ha_msg_lun_pr_key pr_key; 547 int i, k; 548 549 if (softc->ha_link != CTL_HA_LINK_ONLINE) 550 return; 551 mtx_lock(&lun->lun_lock); 552 i = sizeof(msg->lun); 553 if (lun->lun_devid) 554 i += lun->lun_devid->len; 555 i += sizeof(pr_key) * lun->pr_key_count; 556alloc: 557 mtx_unlock(&lun->lun_lock); 558 msg = malloc(i, M_CTL, M_WAITOK); 559 mtx_lock(&lun->lun_lock); 560 k = sizeof(msg->lun); 561 if (lun->lun_devid) 562 k += lun->lun_devid->len; 563 k += sizeof(pr_key) * lun->pr_key_count; 564 if (i < k) { 565 free(msg, M_CTL); 566 i = k; 567 goto alloc; 568 } 569 bzero(&msg->lun, sizeof(msg->lun)); 570 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 571 msg->hdr.nexus.targ_lun = lun->lun; 572 msg->hdr.nexus.targ_mapped_lun = lun->lun; 573 msg->lun.flags = lun->flags; 574 msg->lun.pr_generation = lun->PRGeneration; 575 msg->lun.pr_res_idx = lun->pr_res_idx; 576 msg->lun.pr_res_type = lun->res_type; 577 msg->lun.pr_key_count = lun->pr_key_count; 578 i = 0; 579 if (lun->lun_devid) { 580 msg->lun.lun_devid_len = lun->lun_devid->len; 581 memcpy(&msg->lun.data[i], lun->lun_devid->data, 582 msg->lun.lun_devid_len); 583 i += msg->lun.lun_devid_len; 584 } 585 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 586 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 587 continue; 588 pr_key.pr_iid = k; 589 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 590 i += sizeof(pr_key); 591 } 592 mtx_unlock(&lun->lun_lock); 593 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 594 M_WAITOK); 595 free(msg, M_CTL); 596} 597 598void 599ctl_isc_announce_port(struct ctl_port *port) 600{ 601 struct ctl_softc *softc = control_softc; 602 union ctl_ha_msg *msg; 603 int i; 604 605 if (port->targ_port < softc->port_min || 606 port->targ_port >= softc->port_max || 607 softc->ha_link != CTL_HA_LINK_ONLINE) 608 return; 609 i = sizeof(msg->port) + strlen(port->port_name) + 1; 610 if (port->lun_map) 611 i += sizeof(uint32_t) * CTL_MAX_LUNS; 612 if (port->port_devid) 613 i += port->port_devid->len; 614 if (port->target_devid) 615 i += port->target_devid->len; 616 if (port->init_devid) 617 i += port->init_devid->len; 618 msg = malloc(i, M_CTL, M_WAITOK); 619 bzero(&msg->port, sizeof(msg->port)); 620 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 621 msg->hdr.nexus.targ_port = port->targ_port; 622 msg->port.port_type = port->port_type; 623 msg->port.physical_port = port->physical_port; 624 msg->port.virtual_port = port->virtual_port; 625 msg->port.status = port->status; 626 i = 0; 627 msg->port.name_len = sprintf(&msg->port.data[i], 628 "%d:%s", softc->ha_id, port->port_name) + 1; 629 i += msg->port.name_len; 630 if (port->lun_map) { 631 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS; 632 memcpy(&msg->port.data[i], port->lun_map, 633 msg->port.lun_map_len); 634 i += msg->port.lun_map_len; 635 } 636 if (port->port_devid) { 637 msg->port.port_devid_len = port->port_devid->len; 638 memcpy(&msg->port.data[i], port->port_devid->data, 639 msg->port.port_devid_len); 640 i += msg->port.port_devid_len; 641 } 642 if (port->target_devid) { 643 msg->port.target_devid_len = port->target_devid->len; 644 memcpy(&msg->port.data[i], port->target_devid->data, 645 msg->port.target_devid_len); 646 i += msg->port.target_devid_len; 647 } 648 if (port->init_devid) { 649 msg->port.init_devid_len = port->init_devid->len; 650 memcpy(&msg->port.data[i], port->init_devid->data, 651 msg->port.init_devid_len); 652 i += msg->port.init_devid_len; 653 } 654 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 655 M_WAITOK); 656 free(msg, M_CTL); 657} 658 659static void 660ctl_isc_ha_link_up(struct ctl_softc *softc) 661{ 662 struct ctl_port *port; 663 struct ctl_lun *lun; 664 665 STAILQ_FOREACH(port, &softc->port_list, links) 666 ctl_isc_announce_port(port); 667 STAILQ_FOREACH(lun, &softc->lun_list, links) 668 ctl_isc_announce_lun(lun); 669} 670 671static void 672ctl_isc_ha_link_down(struct ctl_softc *softc) 673{ 674 struct ctl_port *port; 675 struct ctl_lun *lun; 676 union ctl_io *io; 677 678 mtx_lock(&softc->ctl_lock); 679 STAILQ_FOREACH(lun, &softc->lun_list, links) { 680 mtx_lock(&lun->lun_lock); 681 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 682 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 683 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 684 } 685 mtx_unlock(&lun->lun_lock); 686 687 mtx_unlock(&softc->ctl_lock); 688 io = ctl_alloc_io(softc->othersc_pool); 689 mtx_lock(&softc->ctl_lock); 690 ctl_zero_io(io); 691 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 692 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 693 ctl_enqueue_isc(io); 694 } 695 696 STAILQ_FOREACH(port, &softc->port_list, links) { 697 if (port->targ_port >= softc->port_min && 698 port->targ_port < softc->port_max) 699 continue; 700 port->status &= ~CTL_PORT_STATUS_ONLINE; 701 } 702 mtx_unlock(&softc->ctl_lock); 703} 704 705static void 706ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 707{ 708 struct ctl_lun *lun; 709 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 710 711 mtx_lock(&softc->ctl_lock); 712 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS && 713 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) { 714 mtx_lock(&lun->lun_lock); 715 mtx_unlock(&softc->ctl_lock); 716 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && 717 msg->ua.ua_set) 718 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 719 if (msg->ua.ua_all) { 720 if (msg->ua.ua_set) 721 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 722 else 723 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 724 } else { 725 if (msg->ua.ua_set) 726 ctl_est_ua(lun, iid, msg->ua.ua_type); 727 else 728 ctl_clr_ua(lun, iid, msg->ua.ua_type); 729 } 730 mtx_unlock(&lun->lun_lock); 731 } else 732 mtx_unlock(&softc->ctl_lock); 733} 734 735static void 736ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 737{ 738 struct ctl_lun *lun; 739 struct ctl_ha_msg_lun_pr_key pr_key; 740 int i, k; 741 ctl_lun_flags oflags; 742 uint32_t targ_lun; 743 744 targ_lun = msg->hdr.nexus.targ_mapped_lun; 745 mtx_lock(&softc->ctl_lock); 746 if ((targ_lun >= CTL_MAX_LUNS) || 747 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 748 mtx_unlock(&softc->ctl_lock); 749 return; 750 } 751 mtx_lock(&lun->lun_lock); 752 mtx_unlock(&softc->ctl_lock); 753 if (lun->flags & CTL_LUN_DISABLED) { 754 mtx_unlock(&lun->lun_lock); 755 return; 756 } 757 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 758 if (msg->lun.lun_devid_len != i || (i > 0 && 759 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 760 mtx_unlock(&lun->lun_lock); 761 printf("%s: Received conflicting HA LUN %d\n", 762 __func__, msg->hdr.nexus.targ_lun); 763 return; 764 } else { 765 /* Record whether peer is primary. */ 766 oflags = lun->flags; 767 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 768 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 769 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 770 else 771 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 772 if (oflags != lun->flags) 773 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 774 775 /* If peer is primary and we are not -- use data */ 776 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 777 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 778 lun->PRGeneration = msg->lun.pr_generation; 779 lun->pr_res_idx = msg->lun.pr_res_idx; 780 lun->res_type = msg->lun.pr_res_type; 781 lun->pr_key_count = msg->lun.pr_key_count; 782 for (k = 0; k < CTL_MAX_INITIATORS; k++) 783 ctl_clr_prkey(lun, k); 784 for (k = 0; k < msg->lun.pr_key_count; k++) { 785 memcpy(&pr_key, &msg->lun.data[i], 786 sizeof(pr_key)); 787 ctl_alloc_prkey(lun, pr_key.pr_iid); 788 ctl_set_prkey(lun, pr_key.pr_iid, 789 pr_key.pr_key); 790 i += sizeof(pr_key); 791 } 792 } 793 794 mtx_unlock(&lun->lun_lock); 795 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 796 __func__, msg->hdr.nexus.targ_lun, 797 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 798 "primary" : "secondary")); 799 800 /* If we are primary but peer doesn't know -- notify */ 801 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 802 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 803 ctl_isc_announce_lun(lun); 804 } 805} 806 807static void 808ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 809{ 810 struct ctl_port *port; 811 struct ctl_lun *lun; 812 int i, new; 813 814 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 815 if (port == NULL) { 816 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 817 msg->hdr.nexus.targ_port)); 818 new = 1; 819 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 820 port->frontend = &ha_frontend; 821 port->targ_port = msg->hdr.nexus.targ_port; 822 } else if (port->frontend == &ha_frontend) { 823 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 824 msg->hdr.nexus.targ_port)); 825 new = 0; 826 } else { 827 printf("%s: Received conflicting HA port %d\n", 828 __func__, msg->hdr.nexus.targ_port); 829 return; 830 } 831 port->port_type = msg->port.port_type; 832 port->physical_port = msg->port.physical_port; 833 port->virtual_port = msg->port.virtual_port; 834 port->status = msg->port.status; 835 i = 0; 836 free(port->port_name, M_CTL); 837 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 838 M_CTL); 839 i += msg->port.name_len; 840 if (msg->port.lun_map_len != 0) { 841 if (port->lun_map == NULL) 842 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 843 M_CTL, M_WAITOK); 844 memcpy(port->lun_map, &msg->port.data[i], 845 sizeof(uint32_t) * CTL_MAX_LUNS); 846 i += msg->port.lun_map_len; 847 } else { 848 free(port->lun_map, M_CTL); 849 port->lun_map = NULL; 850 } 851 if (msg->port.port_devid_len != 0) { 852 if (port->port_devid == NULL || 853 port->port_devid->len != msg->port.port_devid_len) { 854 free(port->port_devid, M_CTL); 855 port->port_devid = malloc(sizeof(struct ctl_devid) + 856 msg->port.port_devid_len, M_CTL, M_WAITOK); 857 } 858 memcpy(port->port_devid->data, &msg->port.data[i], 859 msg->port.port_devid_len); 860 port->port_devid->len = msg->port.port_devid_len; 861 i += msg->port.port_devid_len; 862 } else { 863 free(port->port_devid, M_CTL); 864 port->port_devid = NULL; 865 } 866 if (msg->port.target_devid_len != 0) { 867 if (port->target_devid == NULL || 868 port->target_devid->len != msg->port.target_devid_len) { 869 free(port->target_devid, M_CTL); 870 port->target_devid = malloc(sizeof(struct ctl_devid) + 871 msg->port.target_devid_len, M_CTL, M_WAITOK); 872 } 873 memcpy(port->target_devid->data, &msg->port.data[i], 874 msg->port.target_devid_len); 875 port->target_devid->len = msg->port.target_devid_len; 876 i += msg->port.target_devid_len; 877 } else { 878 free(port->target_devid, M_CTL); 879 port->target_devid = NULL; 880 } 881 if (msg->port.init_devid_len != 0) { 882 if (port->init_devid == NULL || 883 port->init_devid->len != msg->port.init_devid_len) { 884 free(port->init_devid, M_CTL); 885 port->init_devid = malloc(sizeof(struct ctl_devid) + 886 msg->port.init_devid_len, M_CTL, M_WAITOK); 887 } 888 memcpy(port->init_devid->data, &msg->port.data[i], 889 msg->port.init_devid_len); 890 port->init_devid->len = msg->port.init_devid_len; 891 i += msg->port.init_devid_len; 892 } else { 893 free(port->init_devid, M_CTL); 894 port->init_devid = NULL; 895 } 896 if (new) { 897 if (ctl_port_register(port) != 0) { 898 printf("%s: ctl_port_register() failed with error\n", 899 __func__); 900 } 901 } 902 mtx_lock(&softc->ctl_lock); 903 STAILQ_FOREACH(lun, &softc->lun_list, links) { 904 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 905 continue; 906 mtx_lock(&lun->lun_lock); 907 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 908 mtx_unlock(&lun->lun_lock); 909 } 910 mtx_unlock(&softc->ctl_lock); 911} 912 913/* 914 * ISC (Inter Shelf Communication) event handler. Events from the HA 915 * subsystem come in here. 916 */ 917static void 918ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 919{ 920 struct ctl_softc *softc; 921 union ctl_io *io; 922 struct ctl_prio *presio; 923 ctl_ha_status isc_status; 924 925 softc = control_softc; 926 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 927 if (event == CTL_HA_EVT_MSG_RECV) { 928 union ctl_ha_msg *msg, msgbuf; 929 930 if (param > sizeof(msgbuf)) 931 msg = malloc(param, M_CTL, M_WAITOK); 932 else 933 msg = &msgbuf; 934 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 935 M_WAITOK); 936 if (isc_status != CTL_HA_STATUS_SUCCESS) { 937 printf("%s: Error receiving message: %d\n", 938 __func__, isc_status); 939 if (msg != &msgbuf) 940 free(msg, M_CTL); 941 return; 942 } 943 944 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 945 switch (msg->hdr.msg_type) { 946 case CTL_MSG_SERIALIZE: 947 io = ctl_alloc_io(softc->othersc_pool); 948 ctl_zero_io(io); 949 // populate ctsio from msg 950 io->io_hdr.io_type = CTL_IO_SCSI; 951 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 952 io->io_hdr.original_sc = msg->hdr.original_sc; 953 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 954 CTL_FLAG_IO_ACTIVE; 955 /* 956 * If we're in serialization-only mode, we don't 957 * want to go through full done processing. Thus 958 * the COPY flag. 959 * 960 * XXX KDM add another flag that is more specific. 961 */ 962 if (softc->ha_mode != CTL_HA_MODE_XFER) 963 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 964 io->io_hdr.nexus = msg->hdr.nexus; 965#if 0 966 printf("port %u, iid %u, lun %u\n", 967 io->io_hdr.nexus.targ_port, 968 io->io_hdr.nexus.initid, 969 io->io_hdr.nexus.targ_lun); 970#endif 971 io->scsiio.tag_num = msg->scsi.tag_num; 972 io->scsiio.tag_type = msg->scsi.tag_type; 973#ifdef CTL_TIME_IO 974 io->io_hdr.start_time = time_uptime; 975 getbintime(&io->io_hdr.start_bt); 976#endif /* CTL_TIME_IO */ 977 io->scsiio.cdb_len = msg->scsi.cdb_len; 978 memcpy(io->scsiio.cdb, msg->scsi.cdb, 979 CTL_MAX_CDBLEN); 980 if (softc->ha_mode == CTL_HA_MODE_XFER) { 981 const struct ctl_cmd_entry *entry; 982 983 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 984 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 985 io->io_hdr.flags |= 986 entry->flags & CTL_FLAG_DATA_MASK; 987 } 988 ctl_enqueue_isc(io); 989 break; 990 991 /* Performed on the Originating SC, XFER mode only */ 992 case CTL_MSG_DATAMOVE: { 993 struct ctl_sg_entry *sgl; 994 int i, j; 995 996 io = msg->hdr.original_sc; 997 if (io == NULL) { 998 printf("%s: original_sc == NULL!\n", __func__); 999 /* XXX KDM do something here */ 1000 break; 1001 } 1002 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1003 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1004 /* 1005 * Keep track of this, we need to send it back over 1006 * when the datamove is complete. 1007 */ 1008 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1009 if (msg->hdr.status == CTL_SUCCESS) 1010 io->io_hdr.status = msg->hdr.status; 1011 1012 if (msg->dt.sg_sequence == 0) { 1013 i = msg->dt.kern_sg_entries + 1014 io->scsiio.kern_data_len / 1015 CTL_HA_DATAMOVE_SEGMENT + 1; 1016 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1017 M_WAITOK | M_ZERO); 1018 io->io_hdr.remote_sglist = sgl; 1019 io->io_hdr.local_sglist = 1020 &sgl[msg->dt.kern_sg_entries]; 1021 1022 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1023 1024 io->scsiio.kern_sg_entries = 1025 msg->dt.kern_sg_entries; 1026 io->scsiio.rem_sg_entries = 1027 msg->dt.kern_sg_entries; 1028 io->scsiio.kern_data_len = 1029 msg->dt.kern_data_len; 1030 io->scsiio.kern_total_len = 1031 msg->dt.kern_total_len; 1032 io->scsiio.kern_data_resid = 1033 msg->dt.kern_data_resid; 1034 io->scsiio.kern_rel_offset = 1035 msg->dt.kern_rel_offset; 1036 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1037 io->io_hdr.flags |= msg->dt.flags & 1038 CTL_FLAG_BUS_ADDR; 1039 } else 1040 sgl = (struct ctl_sg_entry *) 1041 io->scsiio.kern_data_ptr; 1042 1043 for (i = msg->dt.sent_sg_entries, j = 0; 1044 i < (msg->dt.sent_sg_entries + 1045 msg->dt.cur_sg_entries); i++, j++) { 1046 sgl[i].addr = msg->dt.sg_list[j].addr; 1047 sgl[i].len = msg->dt.sg_list[j].len; 1048 1049#if 0 1050 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 1051 __func__, 1052 msg->dt.sg_list[j].addr, 1053 msg->dt.sg_list[j].len, 1054 sgl[i].addr, sgl[i].len, j, i); 1055#endif 1056 } 1057 1058 /* 1059 * If this is the last piece of the I/O, we've got 1060 * the full S/G list. Queue processing in the thread. 1061 * Otherwise wait for the next piece. 1062 */ 1063 if (msg->dt.sg_last != 0) 1064 ctl_enqueue_isc(io); 1065 break; 1066 } 1067 /* Performed on the Serializing (primary) SC, XFER mode only */ 1068 case CTL_MSG_DATAMOVE_DONE: { 1069 if (msg->hdr.serializing_sc == NULL) { 1070 printf("%s: serializing_sc == NULL!\n", 1071 __func__); 1072 /* XXX KDM now what? */ 1073 break; 1074 } 1075 /* 1076 * We grab the sense information here in case 1077 * there was a failure, so we can return status 1078 * back to the initiator. 1079 */ 1080 io = msg->hdr.serializing_sc; 1081 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1082 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1083 io->io_hdr.port_status = msg->scsi.fetd_status; 1084 io->scsiio.residual = msg->scsi.residual; 1085 if (msg->hdr.status != CTL_STATUS_NONE) { 1086 io->io_hdr.status = msg->hdr.status; 1087 io->scsiio.scsi_status = msg->scsi.scsi_status; 1088 io->scsiio.sense_len = msg->scsi.sense_len; 1089 io->scsiio.sense_residual =msg->scsi.sense_residual; 1090 memcpy(&io->scsiio.sense_data, 1091 &msg->scsi.sense_data, 1092 msg->scsi.sense_len); 1093 if (msg->hdr.status == CTL_SUCCESS) 1094 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1095 } 1096 ctl_enqueue_isc(io); 1097 break; 1098 } 1099 1100 /* Preformed on Originating SC, SER_ONLY mode */ 1101 case CTL_MSG_R2R: 1102 io = msg->hdr.original_sc; 1103 if (io == NULL) { 1104 printf("%s: original_sc == NULL!\n", 1105 __func__); 1106 break; 1107 } 1108 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1109 io->io_hdr.msg_type = CTL_MSG_R2R; 1110 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1111 ctl_enqueue_isc(io); 1112 break; 1113 1114 /* 1115 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1116 * mode. 1117 * Performed on the Originating (i.e. secondary) SC in XFER 1118 * mode 1119 */ 1120 case CTL_MSG_FINISH_IO: 1121 if (softc->ha_mode == CTL_HA_MODE_XFER) 1122 ctl_isc_handler_finish_xfer(softc, msg); 1123 else 1124 ctl_isc_handler_finish_ser_only(softc, msg); 1125 break; 1126 1127 /* Preformed on Originating SC */ 1128 case CTL_MSG_BAD_JUJU: 1129 io = msg->hdr.original_sc; 1130 if (io == NULL) { 1131 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1132 __func__); 1133 break; 1134 } 1135 ctl_copy_sense_data(msg, io); 1136 /* 1137 * IO should have already been cleaned up on other 1138 * SC so clear this flag so we won't send a message 1139 * back to finish the IO there. 1140 */ 1141 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1142 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1143 1144 /* io = msg->hdr.serializing_sc; */ 1145 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1146 ctl_enqueue_isc(io); 1147 break; 1148 1149 /* Handle resets sent from the other side */ 1150 case CTL_MSG_MANAGE_TASKS: { 1151 struct ctl_taskio *taskio; 1152 taskio = (struct ctl_taskio *)ctl_alloc_io( 1153 softc->othersc_pool); 1154 ctl_zero_io((union ctl_io *)taskio); 1155 taskio->io_hdr.io_type = CTL_IO_TASK; 1156 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1157 taskio->io_hdr.nexus = msg->hdr.nexus; 1158 taskio->task_action = msg->task.task_action; 1159 taskio->tag_num = msg->task.tag_num; 1160 taskio->tag_type = msg->task.tag_type; 1161#ifdef CTL_TIME_IO 1162 taskio->io_hdr.start_time = time_uptime; 1163 getbintime(&taskio->io_hdr.start_bt); 1164#endif /* CTL_TIME_IO */ 1165 ctl_run_task((union ctl_io *)taskio); 1166 break; 1167 } 1168 /* Persistent Reserve action which needs attention */ 1169 case CTL_MSG_PERS_ACTION: 1170 presio = (struct ctl_prio *)ctl_alloc_io( 1171 softc->othersc_pool); 1172 ctl_zero_io((union ctl_io *)presio); 1173 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1174 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1175 presio->io_hdr.nexus = msg->hdr.nexus; 1176 presio->pr_msg = msg->pr; 1177 ctl_enqueue_isc((union ctl_io *)presio); 1178 break; 1179 case CTL_MSG_UA: 1180 ctl_isc_ua(softc, msg, param); 1181 break; 1182 case CTL_MSG_PORT_SYNC: 1183 ctl_isc_port_sync(softc, msg, param); 1184 break; 1185 case CTL_MSG_LUN_SYNC: 1186 ctl_isc_lun_sync(softc, msg, param); 1187 break; 1188 default: 1189 printf("Received HA message of unknown type %d\n", 1190 msg->hdr.msg_type); 1191 break; 1192 } 1193 if (msg != &msgbuf) 1194 free(msg, M_CTL); 1195 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1196 printf("CTL: HA link status changed from %d to %d\n", 1197 softc->ha_link, param); 1198 if (param == softc->ha_link) 1199 return; 1200 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1201 softc->ha_link = param; 1202 ctl_isc_ha_link_down(softc); 1203 } else { 1204 softc->ha_link = param; 1205 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1206 ctl_isc_ha_link_up(softc); 1207 } 1208 return; 1209 } else { 1210 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1211 return; 1212 } 1213} 1214 1215static void 1216ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1217{ 1218 1219 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1220 src->scsi.sense_len); 1221 dest->scsiio.scsi_status = src->scsi.scsi_status; 1222 dest->scsiio.sense_len = src->scsi.sense_len; 1223 dest->io_hdr.status = src->hdr.status; 1224} 1225 1226static void 1227ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1228{ 1229 1230 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1231 src->scsiio.sense_len); 1232 dest->scsi.scsi_status = src->scsiio.scsi_status; 1233 dest->scsi.sense_len = src->scsiio.sense_len; 1234 dest->hdr.status = src->io_hdr.status; 1235} 1236 1237void 1238ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1239{ 1240 struct ctl_softc *softc = lun->ctl_softc; 1241 ctl_ua_type *pu; 1242 1243 if (initidx < softc->init_min || initidx >= softc->init_max) 1244 return; 1245 mtx_assert(&lun->lun_lock, MA_OWNED); 1246 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1247 if (pu == NULL) 1248 return; 1249 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1250} 1251 1252void 1253ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1254{ 1255 int i; 1256 1257 mtx_assert(&lun->lun_lock, MA_OWNED); 1258 if (lun->pending_ua[port] == NULL) 1259 return; 1260 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1261 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1262 continue; 1263 lun->pending_ua[port][i] |= ua; 1264 } 1265} 1266 1267void 1268ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1269{ 1270 struct ctl_softc *softc = lun->ctl_softc; 1271 int i; 1272 1273 mtx_assert(&lun->lun_lock, MA_OWNED); 1274 for (i = softc->port_min; i < softc->port_max; i++) 1275 ctl_est_ua_port(lun, i, except, ua); 1276} 1277 1278void 1279ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1280{ 1281 struct ctl_softc *softc = lun->ctl_softc; 1282 ctl_ua_type *pu; 1283 1284 if (initidx < softc->init_min || initidx >= softc->init_max) 1285 return; 1286 mtx_assert(&lun->lun_lock, MA_OWNED); 1287 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1288 if (pu == NULL) 1289 return; 1290 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1291} 1292 1293void 1294ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1295{ 1296 struct ctl_softc *softc = lun->ctl_softc; 1297 int i, j; 1298 1299 mtx_assert(&lun->lun_lock, MA_OWNED); 1300 for (i = softc->port_min; i < softc->port_max; i++) { 1301 if (lun->pending_ua[i] == NULL) 1302 continue; 1303 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1304 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1305 continue; 1306 lun->pending_ua[i][j] &= ~ua; 1307 } 1308 } 1309} 1310 1311void 1312ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1313 ctl_ua_type ua_type) 1314{ 1315 struct ctl_lun *lun; 1316 1317 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1318 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1319 mtx_lock(&lun->lun_lock); 1320 ctl_clr_ua(lun, initidx, ua_type); 1321 mtx_unlock(&lun->lun_lock); 1322 } 1323} 1324 1325static int 1326ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1327{ 1328 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1329 struct ctl_lun *lun; 1330 struct ctl_lun_req ireq; 1331 int error, value; 1332 1333 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1334 error = sysctl_handle_int(oidp, &value, 0, req); 1335 if ((error != 0) || (req->newptr == NULL)) 1336 return (error); 1337 1338 mtx_lock(&softc->ctl_lock); 1339 if (value == 0) 1340 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1341 else 1342 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1343 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1344 mtx_unlock(&softc->ctl_lock); 1345 bzero(&ireq, sizeof(ireq)); 1346 ireq.reqtype = CTL_LUNREQ_MODIFY; 1347 ireq.reqdata.modify.lun_id = lun->lun; 1348 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1349 curthread); 1350 if (ireq.status != CTL_LUN_OK) { 1351 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1352 __func__, ireq.status, ireq.error_str); 1353 } 1354 mtx_lock(&softc->ctl_lock); 1355 } 1356 mtx_unlock(&softc->ctl_lock); 1357 return (0); 1358} 1359 1360static int 1361ctl_init(void) 1362{ 1363 struct ctl_softc *softc; 1364 void *other_pool; 1365 int i, error, retval; 1366 1367 retval = 0; 1368 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1369 M_WAITOK | M_ZERO); 1370 softc = control_softc; 1371 1372 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1373 "cam/ctl"); 1374 1375 softc->dev->si_drv1 = softc; 1376 1377 sysctl_ctx_init(&softc->sysctl_ctx); 1378 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1379 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1380 CTLFLAG_RD, 0, "CAM Target Layer"); 1381 1382 if (softc->sysctl_tree == NULL) { 1383 printf("%s: unable to allocate sysctl tree\n", __func__); 1384 destroy_dev(softc->dev); 1385 free(control_softc, M_DEVBUF); 1386 control_softc = NULL; 1387 return (ENOMEM); 1388 } 1389 1390 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1391 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1392 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1393 softc->open_count = 0; 1394 1395 /* 1396 * Default to actually sending a SYNCHRONIZE CACHE command down to 1397 * the drive. 1398 */ 1399 softc->flags = CTL_FLAG_REAL_SYNC; 1400 1401 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1402 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1403 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1404 1405 /* 1406 * In Copan's HA scheme, the "master" and "slave" roles are 1407 * figured out through the slot the controller is in. Although it 1408 * is an active/active system, someone has to be in charge. 1409 */ 1410 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1411 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1412 "HA head ID (0 - no HA)"); 1413 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) { 1414 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1415 softc->is_single = 1; 1416 softc->port_cnt = CTL_MAX_PORTS; 1417 softc->port_min = 0; 1418 } else { 1419 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS; 1420 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1421 } 1422 softc->port_max = softc->port_min + softc->port_cnt; 1423 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1424 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1425 1426 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1427 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1428 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1429 1430 STAILQ_INIT(&softc->lun_list); 1431 STAILQ_INIT(&softc->pending_lun_queue); 1432 STAILQ_INIT(&softc->fe_list); 1433 STAILQ_INIT(&softc->port_list); 1434 STAILQ_INIT(&softc->be_list); 1435 ctl_tpc_init(softc); 1436 1437 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1438 &other_pool) != 0) 1439 { 1440 printf("ctl: can't allocate %d entry other SC pool, " 1441 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1442 return (ENOMEM); 1443 } 1444 softc->othersc_pool = other_pool; 1445 1446 if (worker_threads <= 0) 1447 worker_threads = max(1, mp_ncpus / 4); 1448 if (worker_threads > CTL_MAX_THREADS) 1449 worker_threads = CTL_MAX_THREADS; 1450 1451 for (i = 0; i < worker_threads; i++) { 1452 struct ctl_thread *thr = &softc->threads[i]; 1453 1454 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1455 thr->ctl_softc = softc; 1456 STAILQ_INIT(&thr->incoming_queue); 1457 STAILQ_INIT(&thr->rtr_queue); 1458 STAILQ_INIT(&thr->done_queue); 1459 STAILQ_INIT(&thr->isc_queue); 1460 1461 error = kproc_kthread_add(ctl_work_thread, thr, 1462 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1463 if (error != 0) { 1464 printf("error creating CTL work thread!\n"); 1465 ctl_pool_free(other_pool); 1466 return (error); 1467 } 1468 } 1469 error = kproc_kthread_add(ctl_lun_thread, softc, 1470 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1471 if (error != 0) { 1472 printf("error creating CTL lun thread!\n"); 1473 ctl_pool_free(other_pool); 1474 return (error); 1475 } 1476 error = kproc_kthread_add(ctl_thresh_thread, softc, 1477 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1478 if (error != 0) { 1479 printf("error creating CTL threshold thread!\n"); 1480 ctl_pool_free(other_pool); 1481 return (error); 1482 } 1483 1484 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1485 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1486 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1487 1488 if (softc->is_single == 0) { 1489 ctl_frontend_register(&ha_frontend); 1490 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1491 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1492 softc->is_single = 1; 1493 } else 1494 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1495 != CTL_HA_STATUS_SUCCESS) { 1496 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1497 softc->is_single = 1; 1498 } 1499 } 1500 return (0); 1501} 1502 1503void 1504ctl_shutdown(void) 1505{ 1506 struct ctl_softc *softc; 1507 struct ctl_lun *lun, *next_lun; 1508 1509 softc = (struct ctl_softc *)control_softc; 1510 1511 if (softc->is_single == 0) { 1512 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1513 != CTL_HA_STATUS_SUCCESS) { 1514 printf("ctl_shutdown: ctl_ha_msg_deregister failed.\n"); 1515 } 1516 if (ctl_ha_msg_shutdown(softc) != CTL_HA_STATUS_SUCCESS) { 1517 printf("ctl_shutdown: ctl_ha_msg_shutdown failed.\n"); 1518 } 1519 ctl_frontend_deregister(&ha_frontend); 1520 } 1521 1522 mtx_lock(&softc->ctl_lock); 1523 1524 /* 1525 * Free up each LUN. 1526 */ 1527 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1528 next_lun = STAILQ_NEXT(lun, links); 1529 ctl_free_lun(lun); 1530 } 1531 1532 mtx_unlock(&softc->ctl_lock); 1533 1534#if 0 1535 ctl_shutdown_thread(softc->work_thread); 1536 mtx_destroy(&softc->queue_lock); 1537#endif 1538 1539 ctl_tpc_shutdown(softc); 1540 uma_zdestroy(softc->io_zone); 1541 mtx_destroy(&softc->ctl_lock); 1542 1543 destroy_dev(softc->dev); 1544 1545 sysctl_ctx_free(&softc->sysctl_ctx); 1546 1547 free(control_softc, M_DEVBUF); 1548 control_softc = NULL; 1549} 1550 1551static int 1552ctl_module_event_handler(module_t mod, int what, void *arg) 1553{ 1554 1555 switch (what) { 1556 case MOD_LOAD: 1557 return (ctl_init()); 1558 case MOD_UNLOAD: 1559 return (EBUSY); 1560 default: 1561 return (EOPNOTSUPP); 1562 } 1563} 1564 1565/* 1566 * XXX KDM should we do some access checks here? Bump a reference count to 1567 * prevent a CTL module from being unloaded while someone has it open? 1568 */ 1569static int 1570ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1571{ 1572 return (0); 1573} 1574 1575static int 1576ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1577{ 1578 return (0); 1579} 1580 1581/* 1582 * Remove an initiator by port number and initiator ID. 1583 * Returns 0 for success, -1 for failure. 1584 */ 1585int 1586ctl_remove_initiator(struct ctl_port *port, int iid) 1587{ 1588 struct ctl_softc *softc = control_softc; 1589 1590 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1591 1592 if (iid > CTL_MAX_INIT_PER_PORT) { 1593 printf("%s: initiator ID %u > maximun %u!\n", 1594 __func__, iid, CTL_MAX_INIT_PER_PORT); 1595 return (-1); 1596 } 1597 1598 mtx_lock(&softc->ctl_lock); 1599 port->wwpn_iid[iid].in_use--; 1600 port->wwpn_iid[iid].last_use = time_uptime; 1601 mtx_unlock(&softc->ctl_lock); 1602 1603 return (0); 1604} 1605 1606/* 1607 * Add an initiator to the initiator map. 1608 * Returns iid for success, < 0 for failure. 1609 */ 1610int 1611ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1612{ 1613 struct ctl_softc *softc = control_softc; 1614 time_t best_time; 1615 int i, best; 1616 1617 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1618 1619 if (iid >= CTL_MAX_INIT_PER_PORT) { 1620 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1621 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1622 free(name, M_CTL); 1623 return (-1); 1624 } 1625 1626 mtx_lock(&softc->ctl_lock); 1627 1628 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1629 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1630 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1631 iid = i; 1632 break; 1633 } 1634 if (name != NULL && port->wwpn_iid[i].name != NULL && 1635 strcmp(name, port->wwpn_iid[i].name) == 0) { 1636 iid = i; 1637 break; 1638 } 1639 } 1640 } 1641 1642 if (iid < 0) { 1643 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1644 if (port->wwpn_iid[i].in_use == 0 && 1645 port->wwpn_iid[i].wwpn == 0 && 1646 port->wwpn_iid[i].name == NULL) { 1647 iid = i; 1648 break; 1649 } 1650 } 1651 } 1652 1653 if (iid < 0) { 1654 best = -1; 1655 best_time = INT32_MAX; 1656 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1657 if (port->wwpn_iid[i].in_use == 0) { 1658 if (port->wwpn_iid[i].last_use < best_time) { 1659 best = i; 1660 best_time = port->wwpn_iid[i].last_use; 1661 } 1662 } 1663 } 1664 iid = best; 1665 } 1666 1667 if (iid < 0) { 1668 mtx_unlock(&softc->ctl_lock); 1669 free(name, M_CTL); 1670 return (-2); 1671 } 1672 1673 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1674 /* 1675 * This is not an error yet. 1676 */ 1677 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1678#if 0 1679 printf("%s: port %d iid %u WWPN %#jx arrived" 1680 " again\n", __func__, port->targ_port, 1681 iid, (uintmax_t)wwpn); 1682#endif 1683 goto take; 1684 } 1685 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1686 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1687#if 0 1688 printf("%s: port %d iid %u name '%s' arrived" 1689 " again\n", __func__, port->targ_port, 1690 iid, name); 1691#endif 1692 goto take; 1693 } 1694 1695 /* 1696 * This is an error, but what do we do about it? The 1697 * driver is telling us we have a new WWPN for this 1698 * initiator ID, so we pretty much need to use it. 1699 */ 1700 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1701 " but WWPN %#jx '%s' is still at that address\n", 1702 __func__, port->targ_port, iid, wwpn, name, 1703 (uintmax_t)port->wwpn_iid[iid].wwpn, 1704 port->wwpn_iid[iid].name); 1705 1706 /* 1707 * XXX KDM clear have_ca and ua_pending on each LUN for 1708 * this initiator. 1709 */ 1710 } 1711take: 1712 free(port->wwpn_iid[iid].name, M_CTL); 1713 port->wwpn_iid[iid].name = name; 1714 port->wwpn_iid[iid].wwpn = wwpn; 1715 port->wwpn_iid[iid].in_use++; 1716 mtx_unlock(&softc->ctl_lock); 1717 1718 return (iid); 1719} 1720 1721static int 1722ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1723{ 1724 int len; 1725 1726 switch (port->port_type) { 1727 case CTL_PORT_FC: 1728 { 1729 struct scsi_transportid_fcp *id = 1730 (struct scsi_transportid_fcp *)buf; 1731 if (port->wwpn_iid[iid].wwpn == 0) 1732 return (0); 1733 memset(id, 0, sizeof(*id)); 1734 id->format_protocol = SCSI_PROTO_FC; 1735 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1736 return (sizeof(*id)); 1737 } 1738 case CTL_PORT_ISCSI: 1739 { 1740 struct scsi_transportid_iscsi_port *id = 1741 (struct scsi_transportid_iscsi_port *)buf; 1742 if (port->wwpn_iid[iid].name == NULL) 1743 return (0); 1744 memset(id, 0, 256); 1745 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1746 SCSI_PROTO_ISCSI; 1747 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1748 len = roundup2(min(len, 252), 4); 1749 scsi_ulto2b(len, id->additional_length); 1750 return (sizeof(*id) + len); 1751 } 1752 case CTL_PORT_SAS: 1753 { 1754 struct scsi_transportid_sas *id = 1755 (struct scsi_transportid_sas *)buf; 1756 if (port->wwpn_iid[iid].wwpn == 0) 1757 return (0); 1758 memset(id, 0, sizeof(*id)); 1759 id->format_protocol = SCSI_PROTO_SAS; 1760 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1761 return (sizeof(*id)); 1762 } 1763 default: 1764 { 1765 struct scsi_transportid_spi *id = 1766 (struct scsi_transportid_spi *)buf; 1767 memset(id, 0, sizeof(*id)); 1768 id->format_protocol = SCSI_PROTO_SPI; 1769 scsi_ulto2b(iid, id->scsi_addr); 1770 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1771 return (sizeof(*id)); 1772 } 1773 } 1774} 1775 1776/* 1777 * Serialize a command that went down the "wrong" side, and so was sent to 1778 * this controller for execution. The logic is a little different than the 1779 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1780 * sent back to the other side, but in the success case, we execute the 1781 * command on this side (XFER mode) or tell the other side to execute it 1782 * (SER_ONLY mode). 1783 */ 1784static int 1785ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1786{ 1787 struct ctl_softc *softc; 1788 union ctl_ha_msg msg_info; 1789 struct ctl_lun *lun; 1790 const struct ctl_cmd_entry *entry; 1791 int retval = 0; 1792 uint32_t targ_lun; 1793 1794 softc = control_softc; 1795 1796 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1797 mtx_lock(&softc->ctl_lock); 1798 if ((targ_lun < CTL_MAX_LUNS) && 1799 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 1800 mtx_lock(&lun->lun_lock); 1801 mtx_unlock(&softc->ctl_lock); 1802 /* 1803 * If the LUN is invalid, pretend that it doesn't exist. 1804 * It will go away as soon as all pending I/O has been 1805 * completed. 1806 */ 1807 if (lun->flags & CTL_LUN_DISABLED) { 1808 mtx_unlock(&lun->lun_lock); 1809 lun = NULL; 1810 } 1811 } else { 1812 mtx_unlock(&softc->ctl_lock); 1813 lun = NULL; 1814 } 1815 if (lun == NULL) { 1816 /* 1817 * The other node would not send this request to us unless 1818 * received announce that we are primary node for this LUN. 1819 * If this LUN does not exist now, it is probably result of 1820 * a race, so respond to initiator in the most opaque way. 1821 */ 1822 ctl_set_busy(ctsio); 1823 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1824 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1825 msg_info.hdr.serializing_sc = NULL; 1826 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1827 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1828 sizeof(msg_info.scsi), M_WAITOK); 1829 return(1); 1830 } 1831 1832 entry = ctl_get_cmd_entry(ctsio, NULL); 1833 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 1834 mtx_unlock(&lun->lun_lock); 1835 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1836 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1837 msg_info.hdr.serializing_sc = NULL; 1838 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1839 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1840 sizeof(msg_info.scsi), M_WAITOK); 1841 return(1); 1842 } 1843 1844 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 1845 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun; 1846 1847 /* 1848 * Every I/O goes into the OOA queue for a 1849 * particular LUN, and stays there until completion. 1850 */ 1851#ifdef CTL_TIME_IO 1852 if (TAILQ_EMPTY(&lun->ooa_queue)) 1853 lun->idle_time += getsbinuptime() - lun->last_busy; 1854#endif 1855 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1856 1857 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1858 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1859 ooa_links))) { 1860 case CTL_ACTION_BLOCK: 1861 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1862 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1863 blocked_links); 1864 mtx_unlock(&lun->lun_lock); 1865 break; 1866 case CTL_ACTION_PASS: 1867 case CTL_ACTION_SKIP: 1868 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1869 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1870 ctl_enqueue_rtr((union ctl_io *)ctsio); 1871 mtx_unlock(&lun->lun_lock); 1872 } else { 1873 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 1874 mtx_unlock(&lun->lun_lock); 1875 1876 /* send msg back to other side */ 1877 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1878 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1879 msg_info.hdr.msg_type = CTL_MSG_R2R; 1880 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1881 sizeof(msg_info.hdr), M_WAITOK); 1882 } 1883 break; 1884 case CTL_ACTION_OVERLAP: 1885 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1886 mtx_unlock(&lun->lun_lock); 1887 retval = 1; 1888 1889 ctl_set_overlapped_cmd(ctsio); 1890 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1891 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1892 msg_info.hdr.serializing_sc = NULL; 1893 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1894 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1895 sizeof(msg_info.scsi), M_WAITOK); 1896 break; 1897 case CTL_ACTION_OVERLAP_TAG: 1898 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1899 mtx_unlock(&lun->lun_lock); 1900 retval = 1; 1901 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 1902 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1903 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1904 msg_info.hdr.serializing_sc = NULL; 1905 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1906 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1907 sizeof(msg_info.scsi), M_WAITOK); 1908 break; 1909 case CTL_ACTION_ERROR: 1910 default: 1911 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1912 mtx_unlock(&lun->lun_lock); 1913 retval = 1; 1914 1915 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 1916 /*retry_count*/ 0); 1917 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1918 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1919 msg_info.hdr.serializing_sc = NULL; 1920 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1921 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1922 sizeof(msg_info.scsi), M_WAITOK); 1923 break; 1924 } 1925 return (retval); 1926} 1927 1928/* 1929 * Returns 0 for success, errno for failure. 1930 */ 1931static int 1932ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 1933 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 1934{ 1935 union ctl_io *io; 1936 int retval; 1937 1938 retval = 0; 1939 1940 mtx_lock(&lun->lun_lock); 1941 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 1942 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 1943 ooa_links)) { 1944 struct ctl_ooa_entry *entry; 1945 1946 /* 1947 * If we've got more than we can fit, just count the 1948 * remaining entries. 1949 */ 1950 if (*cur_fill_num >= ooa_hdr->alloc_num) 1951 continue; 1952 1953 entry = &kern_entries[*cur_fill_num]; 1954 1955 entry->tag_num = io->scsiio.tag_num; 1956 entry->lun_num = lun->lun; 1957#ifdef CTL_TIME_IO 1958 entry->start_bt = io->io_hdr.start_bt; 1959#endif 1960 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 1961 entry->cdb_len = io->scsiio.cdb_len; 1962 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 1963 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 1964 1965 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 1966 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 1967 1968 if (io->io_hdr.flags & CTL_FLAG_ABORT) 1969 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 1970 1971 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 1972 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 1973 1974 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 1975 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 1976 } 1977 mtx_unlock(&lun->lun_lock); 1978 1979 return (retval); 1980} 1981 1982static void * 1983ctl_copyin_alloc(void *user_addr, int len, char *error_str, 1984 size_t error_str_len) 1985{ 1986 void *kptr; 1987 1988 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 1989 1990 if (copyin(user_addr, kptr, len) != 0) { 1991 snprintf(error_str, error_str_len, "Error copying %d bytes " 1992 "from user address %p to kernel address %p", len, 1993 user_addr, kptr); 1994 free(kptr, M_CTL); 1995 return (NULL); 1996 } 1997 1998 return (kptr); 1999} 2000 2001static void 2002ctl_free_args(int num_args, struct ctl_be_arg *args) 2003{ 2004 int i; 2005 2006 if (args == NULL) 2007 return; 2008 2009 for (i = 0; i < num_args; i++) { 2010 free(args[i].kname, M_CTL); 2011 free(args[i].kvalue, M_CTL); 2012 } 2013 2014 free(args, M_CTL); 2015} 2016 2017static struct ctl_be_arg * 2018ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2019 char *error_str, size_t error_str_len) 2020{ 2021 struct ctl_be_arg *args; 2022 int i; 2023 2024 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2025 error_str, error_str_len); 2026 2027 if (args == NULL) 2028 goto bailout; 2029 2030 for (i = 0; i < num_args; i++) { 2031 args[i].kname = NULL; 2032 args[i].kvalue = NULL; 2033 } 2034 2035 for (i = 0; i < num_args; i++) { 2036 uint8_t *tmpptr; 2037 2038 args[i].kname = ctl_copyin_alloc(args[i].name, 2039 args[i].namelen, error_str, error_str_len); 2040 if (args[i].kname == NULL) 2041 goto bailout; 2042 2043 if (args[i].kname[args[i].namelen - 1] != '\0') { 2044 snprintf(error_str, error_str_len, "Argument %d " 2045 "name is not NUL-terminated", i); 2046 goto bailout; 2047 } 2048 2049 if (args[i].flags & CTL_BEARG_RD) { 2050 tmpptr = ctl_copyin_alloc(args[i].value, 2051 args[i].vallen, error_str, error_str_len); 2052 if (tmpptr == NULL) 2053 goto bailout; 2054 if ((args[i].flags & CTL_BEARG_ASCII) 2055 && (tmpptr[args[i].vallen - 1] != '\0')) { 2056 snprintf(error_str, error_str_len, "Argument " 2057 "%d value is not NUL-terminated", i); 2058 goto bailout; 2059 } 2060 args[i].kvalue = tmpptr; 2061 } else { 2062 args[i].kvalue = malloc(args[i].vallen, 2063 M_CTL, M_WAITOK | M_ZERO); 2064 } 2065 } 2066 2067 return (args); 2068bailout: 2069 2070 ctl_free_args(num_args, args); 2071 2072 return (NULL); 2073} 2074 2075static void 2076ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2077{ 2078 int i; 2079 2080 for (i = 0; i < num_args; i++) { 2081 if (args[i].flags & CTL_BEARG_WR) 2082 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2083 } 2084} 2085 2086/* 2087 * Escape characters that are illegal or not recommended in XML. 2088 */ 2089int 2090ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2091{ 2092 char *end = str + size; 2093 int retval; 2094 2095 retval = 0; 2096 2097 for (; *str && str < end; str++) { 2098 switch (*str) { 2099 case '&': 2100 retval = sbuf_printf(sb, "&"); 2101 break; 2102 case '>': 2103 retval = sbuf_printf(sb, ">"); 2104 break; 2105 case '<': 2106 retval = sbuf_printf(sb, "<"); 2107 break; 2108 default: 2109 retval = sbuf_putc(sb, *str); 2110 break; 2111 } 2112 2113 if (retval != 0) 2114 break; 2115 2116 } 2117 2118 return (retval); 2119} 2120 2121static void 2122ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2123{ 2124 struct scsi_vpd_id_descriptor *desc; 2125 int i; 2126 2127 if (id == NULL || id->len < 4) 2128 return; 2129 desc = (struct scsi_vpd_id_descriptor *)id->data; 2130 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2131 case SVPD_ID_TYPE_T10: 2132 sbuf_printf(sb, "t10."); 2133 break; 2134 case SVPD_ID_TYPE_EUI64: 2135 sbuf_printf(sb, "eui."); 2136 break; 2137 case SVPD_ID_TYPE_NAA: 2138 sbuf_printf(sb, "naa."); 2139 break; 2140 case SVPD_ID_TYPE_SCSI_NAME: 2141 break; 2142 } 2143 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2144 case SVPD_ID_CODESET_BINARY: 2145 for (i = 0; i < desc->length; i++) 2146 sbuf_printf(sb, "%02x", desc->identifier[i]); 2147 break; 2148 case SVPD_ID_CODESET_ASCII: 2149 sbuf_printf(sb, "%.*s", (int)desc->length, 2150 (char *)desc->identifier); 2151 break; 2152 case SVPD_ID_CODESET_UTF8: 2153 sbuf_printf(sb, "%s", (char *)desc->identifier); 2154 break; 2155 } 2156} 2157 2158static int 2159ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2160 struct thread *td) 2161{ 2162 struct ctl_softc *softc; 2163 struct ctl_lun *lun; 2164 int retval; 2165 2166 softc = control_softc; 2167 2168 retval = 0; 2169 2170 switch (cmd) { 2171 case CTL_IO: 2172 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2173 break; 2174 case CTL_ENABLE_PORT: 2175 case CTL_DISABLE_PORT: 2176 case CTL_SET_PORT_WWNS: { 2177 struct ctl_port *port; 2178 struct ctl_port_entry *entry; 2179 2180 entry = (struct ctl_port_entry *)addr; 2181 2182 mtx_lock(&softc->ctl_lock); 2183 STAILQ_FOREACH(port, &softc->port_list, links) { 2184 int action, done; 2185 2186 if (port->targ_port < softc->port_min || 2187 port->targ_port >= softc->port_max) 2188 continue; 2189 2190 action = 0; 2191 done = 0; 2192 if ((entry->port_type == CTL_PORT_NONE) 2193 && (entry->targ_port == port->targ_port)) { 2194 /* 2195 * If the user only wants to enable or 2196 * disable or set WWNs on a specific port, 2197 * do the operation and we're done. 2198 */ 2199 action = 1; 2200 done = 1; 2201 } else if (entry->port_type & port->port_type) { 2202 /* 2203 * Compare the user's type mask with the 2204 * particular frontend type to see if we 2205 * have a match. 2206 */ 2207 action = 1; 2208 done = 0; 2209 2210 /* 2211 * Make sure the user isn't trying to set 2212 * WWNs on multiple ports at the same time. 2213 */ 2214 if (cmd == CTL_SET_PORT_WWNS) { 2215 printf("%s: Can't set WWNs on " 2216 "multiple ports\n", __func__); 2217 retval = EINVAL; 2218 break; 2219 } 2220 } 2221 if (action == 0) 2222 continue; 2223 2224 /* 2225 * XXX KDM we have to drop the lock here, because 2226 * the online/offline operations can potentially 2227 * block. We need to reference count the frontends 2228 * so they can't go away, 2229 */ 2230 if (cmd == CTL_ENABLE_PORT) { 2231 mtx_unlock(&softc->ctl_lock); 2232 ctl_port_online(port); 2233 mtx_lock(&softc->ctl_lock); 2234 } else if (cmd == CTL_DISABLE_PORT) { 2235 mtx_unlock(&softc->ctl_lock); 2236 ctl_port_offline(port); 2237 mtx_lock(&softc->ctl_lock); 2238 } else if (cmd == CTL_SET_PORT_WWNS) { 2239 ctl_port_set_wwns(port, 2240 (entry->flags & CTL_PORT_WWNN_VALID) ? 2241 1 : 0, entry->wwnn, 2242 (entry->flags & CTL_PORT_WWPN_VALID) ? 2243 1 : 0, entry->wwpn); 2244 } 2245 if (done != 0) 2246 break; 2247 } 2248 mtx_unlock(&softc->ctl_lock); 2249 break; 2250 } 2251 case CTL_GET_PORT_LIST: { 2252 struct ctl_port *port; 2253 struct ctl_port_list *list; 2254 int i; 2255 2256 list = (struct ctl_port_list *)addr; 2257 2258 if (list->alloc_len != (list->alloc_num * 2259 sizeof(struct ctl_port_entry))) { 2260 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2261 "alloc_num %u * sizeof(struct ctl_port_entry) " 2262 "%zu\n", __func__, list->alloc_len, 2263 list->alloc_num, sizeof(struct ctl_port_entry)); 2264 retval = EINVAL; 2265 break; 2266 } 2267 list->fill_len = 0; 2268 list->fill_num = 0; 2269 list->dropped_num = 0; 2270 i = 0; 2271 mtx_lock(&softc->ctl_lock); 2272 STAILQ_FOREACH(port, &softc->port_list, links) { 2273 struct ctl_port_entry entry, *list_entry; 2274 2275 if (list->fill_num >= list->alloc_num) { 2276 list->dropped_num++; 2277 continue; 2278 } 2279 2280 entry.port_type = port->port_type; 2281 strlcpy(entry.port_name, port->port_name, 2282 sizeof(entry.port_name)); 2283 entry.targ_port = port->targ_port; 2284 entry.physical_port = port->physical_port; 2285 entry.virtual_port = port->virtual_port; 2286 entry.wwnn = port->wwnn; 2287 entry.wwpn = port->wwpn; 2288 if (port->status & CTL_PORT_STATUS_ONLINE) 2289 entry.online = 1; 2290 else 2291 entry.online = 0; 2292 2293 list_entry = &list->entries[i]; 2294 2295 retval = copyout(&entry, list_entry, sizeof(entry)); 2296 if (retval != 0) { 2297 printf("%s: CTL_GET_PORT_LIST: copyout " 2298 "returned %d\n", __func__, retval); 2299 break; 2300 } 2301 i++; 2302 list->fill_num++; 2303 list->fill_len += sizeof(entry); 2304 } 2305 mtx_unlock(&softc->ctl_lock); 2306 2307 /* 2308 * If this is non-zero, we had a copyout fault, so there's 2309 * probably no point in attempting to set the status inside 2310 * the structure. 2311 */ 2312 if (retval != 0) 2313 break; 2314 2315 if (list->dropped_num > 0) 2316 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2317 else 2318 list->status = CTL_PORT_LIST_OK; 2319 break; 2320 } 2321 case CTL_DUMP_OOA: { 2322 union ctl_io *io; 2323 char printbuf[128]; 2324 struct sbuf sb; 2325 2326 mtx_lock(&softc->ctl_lock); 2327 printf("Dumping OOA queues:\n"); 2328 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2329 mtx_lock(&lun->lun_lock); 2330 for (io = (union ctl_io *)TAILQ_FIRST( 2331 &lun->ooa_queue); io != NULL; 2332 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2333 ooa_links)) { 2334 sbuf_new(&sb, printbuf, sizeof(printbuf), 2335 SBUF_FIXEDLEN); 2336 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2337 (intmax_t)lun->lun, 2338 io->scsiio.tag_num, 2339 (io->io_hdr.flags & 2340 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2341 (io->io_hdr.flags & 2342 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2343 (io->io_hdr.flags & 2344 CTL_FLAG_ABORT) ? " ABORT" : "", 2345 (io->io_hdr.flags & 2346 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2347 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2348 sbuf_finish(&sb); 2349 printf("%s\n", sbuf_data(&sb)); 2350 } 2351 mtx_unlock(&lun->lun_lock); 2352 } 2353 printf("OOA queues dump done\n"); 2354 mtx_unlock(&softc->ctl_lock); 2355 break; 2356 } 2357 case CTL_GET_OOA: { 2358 struct ctl_ooa *ooa_hdr; 2359 struct ctl_ooa_entry *entries; 2360 uint32_t cur_fill_num; 2361 2362 ooa_hdr = (struct ctl_ooa *)addr; 2363 2364 if ((ooa_hdr->alloc_len == 0) 2365 || (ooa_hdr->alloc_num == 0)) { 2366 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2367 "must be non-zero\n", __func__, 2368 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2369 retval = EINVAL; 2370 break; 2371 } 2372 2373 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2374 sizeof(struct ctl_ooa_entry))) { 2375 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2376 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2377 __func__, ooa_hdr->alloc_len, 2378 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2379 retval = EINVAL; 2380 break; 2381 } 2382 2383 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2384 if (entries == NULL) { 2385 printf("%s: could not allocate %d bytes for OOA " 2386 "dump\n", __func__, ooa_hdr->alloc_len); 2387 retval = ENOMEM; 2388 break; 2389 } 2390 2391 mtx_lock(&softc->ctl_lock); 2392 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2393 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2394 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2395 mtx_unlock(&softc->ctl_lock); 2396 free(entries, M_CTL); 2397 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2398 __func__, (uintmax_t)ooa_hdr->lun_num); 2399 retval = EINVAL; 2400 break; 2401 } 2402 2403 cur_fill_num = 0; 2404 2405 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2406 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2407 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2408 ooa_hdr, entries); 2409 if (retval != 0) 2410 break; 2411 } 2412 if (retval != 0) { 2413 mtx_unlock(&softc->ctl_lock); 2414 free(entries, M_CTL); 2415 break; 2416 } 2417 } else { 2418 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2419 2420 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2421 entries); 2422 } 2423 mtx_unlock(&softc->ctl_lock); 2424 2425 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2426 ooa_hdr->fill_len = ooa_hdr->fill_num * 2427 sizeof(struct ctl_ooa_entry); 2428 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2429 if (retval != 0) { 2430 printf("%s: error copying out %d bytes for OOA dump\n", 2431 __func__, ooa_hdr->fill_len); 2432 } 2433 2434 getbintime(&ooa_hdr->cur_bt); 2435 2436 if (cur_fill_num > ooa_hdr->alloc_num) { 2437 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2438 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2439 } else { 2440 ooa_hdr->dropped_num = 0; 2441 ooa_hdr->status = CTL_OOA_OK; 2442 } 2443 2444 free(entries, M_CTL); 2445 break; 2446 } 2447 case CTL_CHECK_OOA: { 2448 union ctl_io *io; 2449 struct ctl_ooa_info *ooa_info; 2450 2451 2452 ooa_info = (struct ctl_ooa_info *)addr; 2453 2454 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2455 ooa_info->status = CTL_OOA_INVALID_LUN; 2456 break; 2457 } 2458 mtx_lock(&softc->ctl_lock); 2459 lun = softc->ctl_luns[ooa_info->lun_id]; 2460 if (lun == NULL) { 2461 mtx_unlock(&softc->ctl_lock); 2462 ooa_info->status = CTL_OOA_INVALID_LUN; 2463 break; 2464 } 2465 mtx_lock(&lun->lun_lock); 2466 mtx_unlock(&softc->ctl_lock); 2467 ooa_info->num_entries = 0; 2468 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2469 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2470 &io->io_hdr, ooa_links)) { 2471 ooa_info->num_entries++; 2472 } 2473 mtx_unlock(&lun->lun_lock); 2474 2475 ooa_info->status = CTL_OOA_SUCCESS; 2476 2477 break; 2478 } 2479 case CTL_DELAY_IO: { 2480 struct ctl_io_delay_info *delay_info; 2481 2482 delay_info = (struct ctl_io_delay_info *)addr; 2483 2484#ifdef CTL_IO_DELAY 2485 mtx_lock(&softc->ctl_lock); 2486 2487 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2488 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2489 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2490 } else { 2491 lun = softc->ctl_luns[delay_info->lun_id]; 2492 mtx_lock(&lun->lun_lock); 2493 2494 delay_info->status = CTL_DELAY_STATUS_OK; 2495 2496 switch (delay_info->delay_type) { 2497 case CTL_DELAY_TYPE_CONT: 2498 break; 2499 case CTL_DELAY_TYPE_ONESHOT: 2500 break; 2501 default: 2502 delay_info->status = 2503 CTL_DELAY_STATUS_INVALID_TYPE; 2504 break; 2505 } 2506 2507 switch (delay_info->delay_loc) { 2508 case CTL_DELAY_LOC_DATAMOVE: 2509 lun->delay_info.datamove_type = 2510 delay_info->delay_type; 2511 lun->delay_info.datamove_delay = 2512 delay_info->delay_secs; 2513 break; 2514 case CTL_DELAY_LOC_DONE: 2515 lun->delay_info.done_type = 2516 delay_info->delay_type; 2517 lun->delay_info.done_delay = 2518 delay_info->delay_secs; 2519 break; 2520 default: 2521 delay_info->status = 2522 CTL_DELAY_STATUS_INVALID_LOC; 2523 break; 2524 } 2525 mtx_unlock(&lun->lun_lock); 2526 } 2527 2528 mtx_unlock(&softc->ctl_lock); 2529#else 2530 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2531#endif /* CTL_IO_DELAY */ 2532 break; 2533 } 2534 case CTL_REALSYNC_SET: { 2535 int *syncstate; 2536 2537 syncstate = (int *)addr; 2538 2539 mtx_lock(&softc->ctl_lock); 2540 switch (*syncstate) { 2541 case 0: 2542 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2543 break; 2544 case 1: 2545 softc->flags |= CTL_FLAG_REAL_SYNC; 2546 break; 2547 default: 2548 retval = EINVAL; 2549 break; 2550 } 2551 mtx_unlock(&softc->ctl_lock); 2552 break; 2553 } 2554 case CTL_REALSYNC_GET: { 2555 int *syncstate; 2556 2557 syncstate = (int*)addr; 2558 2559 mtx_lock(&softc->ctl_lock); 2560 if (softc->flags & CTL_FLAG_REAL_SYNC) 2561 *syncstate = 1; 2562 else 2563 *syncstate = 0; 2564 mtx_unlock(&softc->ctl_lock); 2565 2566 break; 2567 } 2568 case CTL_SETSYNC: 2569 case CTL_GETSYNC: { 2570 struct ctl_sync_info *sync_info; 2571 2572 sync_info = (struct ctl_sync_info *)addr; 2573 2574 mtx_lock(&softc->ctl_lock); 2575 lun = softc->ctl_luns[sync_info->lun_id]; 2576 if (lun == NULL) { 2577 mtx_unlock(&softc->ctl_lock); 2578 sync_info->status = CTL_GS_SYNC_NO_LUN; 2579 break; 2580 } 2581 /* 2582 * Get or set the sync interval. We're not bounds checking 2583 * in the set case, hopefully the user won't do something 2584 * silly. 2585 */ 2586 mtx_lock(&lun->lun_lock); 2587 mtx_unlock(&softc->ctl_lock); 2588 if (cmd == CTL_GETSYNC) 2589 sync_info->sync_interval = lun->sync_interval; 2590 else 2591 lun->sync_interval = sync_info->sync_interval; 2592 mtx_unlock(&lun->lun_lock); 2593 2594 sync_info->status = CTL_GS_SYNC_OK; 2595 2596 break; 2597 } 2598 case CTL_GETSTATS: { 2599 struct ctl_stats *stats; 2600 int i; 2601 2602 stats = (struct ctl_stats *)addr; 2603 2604 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2605 stats->alloc_len) { 2606 stats->status = CTL_SS_NEED_MORE_SPACE; 2607 stats->num_luns = softc->num_luns; 2608 break; 2609 } 2610 /* 2611 * XXX KDM no locking here. If the LUN list changes, 2612 * things can blow up. 2613 */ 2614 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2615 i++, lun = STAILQ_NEXT(lun, links)) { 2616 retval = copyout(&lun->stats, &stats->lun_stats[i], 2617 sizeof(lun->stats)); 2618 if (retval != 0) 2619 break; 2620 } 2621 stats->num_luns = softc->num_luns; 2622 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2623 softc->num_luns; 2624 stats->status = CTL_SS_OK; 2625#ifdef CTL_TIME_IO 2626 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2627#else 2628 stats->flags = CTL_STATS_FLAG_NONE; 2629#endif 2630 getnanouptime(&stats->timestamp); 2631 break; 2632 } 2633 case CTL_ERROR_INJECT: { 2634 struct ctl_error_desc *err_desc, *new_err_desc; 2635 2636 err_desc = (struct ctl_error_desc *)addr; 2637 2638 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2639 M_WAITOK | M_ZERO); 2640 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2641 2642 mtx_lock(&softc->ctl_lock); 2643 lun = softc->ctl_luns[err_desc->lun_id]; 2644 if (lun == NULL) { 2645 mtx_unlock(&softc->ctl_lock); 2646 free(new_err_desc, M_CTL); 2647 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2648 __func__, (uintmax_t)err_desc->lun_id); 2649 retval = EINVAL; 2650 break; 2651 } 2652 mtx_lock(&lun->lun_lock); 2653 mtx_unlock(&softc->ctl_lock); 2654 2655 /* 2656 * We could do some checking here to verify the validity 2657 * of the request, but given the complexity of error 2658 * injection requests, the checking logic would be fairly 2659 * complex. 2660 * 2661 * For now, if the request is invalid, it just won't get 2662 * executed and might get deleted. 2663 */ 2664 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2665 2666 /* 2667 * XXX KDM check to make sure the serial number is unique, 2668 * in case we somehow manage to wrap. That shouldn't 2669 * happen for a very long time, but it's the right thing to 2670 * do. 2671 */ 2672 new_err_desc->serial = lun->error_serial; 2673 err_desc->serial = lun->error_serial; 2674 lun->error_serial++; 2675 2676 mtx_unlock(&lun->lun_lock); 2677 break; 2678 } 2679 case CTL_ERROR_INJECT_DELETE: { 2680 struct ctl_error_desc *delete_desc, *desc, *desc2; 2681 int delete_done; 2682 2683 delete_desc = (struct ctl_error_desc *)addr; 2684 delete_done = 0; 2685 2686 mtx_lock(&softc->ctl_lock); 2687 lun = softc->ctl_luns[delete_desc->lun_id]; 2688 if (lun == NULL) { 2689 mtx_unlock(&softc->ctl_lock); 2690 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2691 __func__, (uintmax_t)delete_desc->lun_id); 2692 retval = EINVAL; 2693 break; 2694 } 2695 mtx_lock(&lun->lun_lock); 2696 mtx_unlock(&softc->ctl_lock); 2697 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2698 if (desc->serial != delete_desc->serial) 2699 continue; 2700 2701 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2702 links); 2703 free(desc, M_CTL); 2704 delete_done = 1; 2705 } 2706 mtx_unlock(&lun->lun_lock); 2707 if (delete_done == 0) { 2708 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2709 "error serial %ju on LUN %u\n", __func__, 2710 delete_desc->serial, delete_desc->lun_id); 2711 retval = EINVAL; 2712 break; 2713 } 2714 break; 2715 } 2716 case CTL_DUMP_STRUCTS: { 2717 int i, j, k; 2718 struct ctl_port *port; 2719 struct ctl_frontend *fe; 2720 2721 mtx_lock(&softc->ctl_lock); 2722 printf("CTL Persistent Reservation information start:\n"); 2723 for (i = 0; i < CTL_MAX_LUNS; i++) { 2724 lun = softc->ctl_luns[i]; 2725 2726 if ((lun == NULL) 2727 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2728 continue; 2729 2730 for (j = 0; j < CTL_MAX_PORTS; j++) { 2731 if (lun->pr_keys[j] == NULL) 2732 continue; 2733 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2734 if (lun->pr_keys[j][k] == 0) 2735 continue; 2736 printf(" LUN %d port %d iid %d key " 2737 "%#jx\n", i, j, k, 2738 (uintmax_t)lun->pr_keys[j][k]); 2739 } 2740 } 2741 } 2742 printf("CTL Persistent Reservation information end\n"); 2743 printf("CTL Ports:\n"); 2744 STAILQ_FOREACH(port, &softc->port_list, links) { 2745 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2746 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2747 port->frontend->name, port->port_type, 2748 port->physical_port, port->virtual_port, 2749 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2750 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2751 if (port->wwpn_iid[j].in_use == 0 && 2752 port->wwpn_iid[j].wwpn == 0 && 2753 port->wwpn_iid[j].name == NULL) 2754 continue; 2755 2756 printf(" iid %u use %d WWPN %#jx '%s'\n", 2757 j, port->wwpn_iid[j].in_use, 2758 (uintmax_t)port->wwpn_iid[j].wwpn, 2759 port->wwpn_iid[j].name); 2760 } 2761 } 2762 printf("CTL Port information end\n"); 2763 mtx_unlock(&softc->ctl_lock); 2764 /* 2765 * XXX KDM calling this without a lock. We'd likely want 2766 * to drop the lock before calling the frontend's dump 2767 * routine anyway. 2768 */ 2769 printf("CTL Frontends:\n"); 2770 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2771 printf(" Frontend '%s'\n", fe->name); 2772 if (fe->fe_dump != NULL) 2773 fe->fe_dump(); 2774 } 2775 printf("CTL Frontend information end\n"); 2776 break; 2777 } 2778 case CTL_LUN_REQ: { 2779 struct ctl_lun_req *lun_req; 2780 struct ctl_backend_driver *backend; 2781 2782 lun_req = (struct ctl_lun_req *)addr; 2783 2784 backend = ctl_backend_find(lun_req->backend); 2785 if (backend == NULL) { 2786 lun_req->status = CTL_LUN_ERROR; 2787 snprintf(lun_req->error_str, 2788 sizeof(lun_req->error_str), 2789 "Backend \"%s\" not found.", 2790 lun_req->backend); 2791 break; 2792 } 2793 if (lun_req->num_be_args > 0) { 2794 lun_req->kern_be_args = ctl_copyin_args( 2795 lun_req->num_be_args, 2796 lun_req->be_args, 2797 lun_req->error_str, 2798 sizeof(lun_req->error_str)); 2799 if (lun_req->kern_be_args == NULL) { 2800 lun_req->status = CTL_LUN_ERROR; 2801 break; 2802 } 2803 } 2804 2805 retval = backend->ioctl(dev, cmd, addr, flag, td); 2806 2807 if (lun_req->num_be_args > 0) { 2808 ctl_copyout_args(lun_req->num_be_args, 2809 lun_req->kern_be_args); 2810 ctl_free_args(lun_req->num_be_args, 2811 lun_req->kern_be_args); 2812 } 2813 break; 2814 } 2815 case CTL_LUN_LIST: { 2816 struct sbuf *sb; 2817 struct ctl_lun_list *list; 2818 struct ctl_option *opt; 2819 2820 list = (struct ctl_lun_list *)addr; 2821 2822 /* 2823 * Allocate a fixed length sbuf here, based on the length 2824 * of the user's buffer. We could allocate an auto-extending 2825 * buffer, and then tell the user how much larger our 2826 * amount of data is than his buffer, but that presents 2827 * some problems: 2828 * 2829 * 1. The sbuf(9) routines use a blocking malloc, and so 2830 * we can't hold a lock while calling them with an 2831 * auto-extending buffer. 2832 * 2833 * 2. There is not currently a LUN reference counting 2834 * mechanism, outside of outstanding transactions on 2835 * the LUN's OOA queue. So a LUN could go away on us 2836 * while we're getting the LUN number, backend-specific 2837 * information, etc. Thus, given the way things 2838 * currently work, we need to hold the CTL lock while 2839 * grabbing LUN information. 2840 * 2841 * So, from the user's standpoint, the best thing to do is 2842 * allocate what he thinks is a reasonable buffer length, 2843 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 2844 * double the buffer length and try again. (And repeat 2845 * that until he succeeds.) 2846 */ 2847 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 2848 if (sb == NULL) { 2849 list->status = CTL_LUN_LIST_ERROR; 2850 snprintf(list->error_str, sizeof(list->error_str), 2851 "Unable to allocate %d bytes for LUN list", 2852 list->alloc_len); 2853 break; 2854 } 2855 2856 sbuf_printf(sb, "<ctllunlist>\n"); 2857 2858 mtx_lock(&softc->ctl_lock); 2859 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2860 mtx_lock(&lun->lun_lock); 2861 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 2862 (uintmax_t)lun->lun); 2863 2864 /* 2865 * Bail out as soon as we see that we've overfilled 2866 * the buffer. 2867 */ 2868 if (retval != 0) 2869 break; 2870 2871 retval = sbuf_printf(sb, "\t<backend_type>%s" 2872 "</backend_type>\n", 2873 (lun->backend == NULL) ? "none" : 2874 lun->backend->name); 2875 2876 if (retval != 0) 2877 break; 2878 2879 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 2880 lun->be_lun->lun_type); 2881 2882 if (retval != 0) 2883 break; 2884 2885 if (lun->backend == NULL) { 2886 retval = sbuf_printf(sb, "</lun>\n"); 2887 if (retval != 0) 2888 break; 2889 continue; 2890 } 2891 2892 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 2893 (lun->be_lun->maxlba > 0) ? 2894 lun->be_lun->maxlba + 1 : 0); 2895 2896 if (retval != 0) 2897 break; 2898 2899 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 2900 lun->be_lun->blocksize); 2901 2902 if (retval != 0) 2903 break; 2904 2905 retval = sbuf_printf(sb, "\t<serial_number>"); 2906 2907 if (retval != 0) 2908 break; 2909 2910 retval = ctl_sbuf_printf_esc(sb, 2911 lun->be_lun->serial_num, 2912 sizeof(lun->be_lun->serial_num)); 2913 2914 if (retval != 0) 2915 break; 2916 2917 retval = sbuf_printf(sb, "</serial_number>\n"); 2918 2919 if (retval != 0) 2920 break; 2921 2922 retval = sbuf_printf(sb, "\t<device_id>"); 2923 2924 if (retval != 0) 2925 break; 2926 2927 retval = ctl_sbuf_printf_esc(sb, 2928 lun->be_lun->device_id, 2929 sizeof(lun->be_lun->device_id)); 2930 2931 if (retval != 0) 2932 break; 2933 2934 retval = sbuf_printf(sb, "</device_id>\n"); 2935 2936 if (retval != 0) 2937 break; 2938 2939 if (lun->backend->lun_info != NULL) { 2940 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 2941 if (retval != 0) 2942 break; 2943 } 2944 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 2945 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 2946 opt->name, opt->value, opt->name); 2947 if (retval != 0) 2948 break; 2949 } 2950 2951 retval = sbuf_printf(sb, "</lun>\n"); 2952 2953 if (retval != 0) 2954 break; 2955 mtx_unlock(&lun->lun_lock); 2956 } 2957 if (lun != NULL) 2958 mtx_unlock(&lun->lun_lock); 2959 mtx_unlock(&softc->ctl_lock); 2960 2961 if ((retval != 0) 2962 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 2963 retval = 0; 2964 sbuf_delete(sb); 2965 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 2966 snprintf(list->error_str, sizeof(list->error_str), 2967 "Out of space, %d bytes is too small", 2968 list->alloc_len); 2969 break; 2970 } 2971 2972 sbuf_finish(sb); 2973 2974 retval = copyout(sbuf_data(sb), list->lun_xml, 2975 sbuf_len(sb) + 1); 2976 2977 list->fill_len = sbuf_len(sb) + 1; 2978 list->status = CTL_LUN_LIST_OK; 2979 sbuf_delete(sb); 2980 break; 2981 } 2982 case CTL_ISCSI: { 2983 struct ctl_iscsi *ci; 2984 struct ctl_frontend *fe; 2985 2986 ci = (struct ctl_iscsi *)addr; 2987 2988 fe = ctl_frontend_find("iscsi"); 2989 if (fe == NULL) { 2990 ci->status = CTL_ISCSI_ERROR; 2991 snprintf(ci->error_str, sizeof(ci->error_str), 2992 "Frontend \"iscsi\" not found."); 2993 break; 2994 } 2995 2996 retval = fe->ioctl(dev, cmd, addr, flag, td); 2997 break; 2998 } 2999 case CTL_PORT_REQ: { 3000 struct ctl_req *req; 3001 struct ctl_frontend *fe; 3002 3003 req = (struct ctl_req *)addr; 3004 3005 fe = ctl_frontend_find(req->driver); 3006 if (fe == NULL) { 3007 req->status = CTL_LUN_ERROR; 3008 snprintf(req->error_str, sizeof(req->error_str), 3009 "Frontend \"%s\" not found.", req->driver); 3010 break; 3011 } 3012 if (req->num_args > 0) { 3013 req->kern_args = ctl_copyin_args(req->num_args, 3014 req->args, req->error_str, sizeof(req->error_str)); 3015 if (req->kern_args == NULL) { 3016 req->status = CTL_LUN_ERROR; 3017 break; 3018 } 3019 } 3020 3021 if (fe->ioctl) 3022 retval = fe->ioctl(dev, cmd, addr, flag, td); 3023 else 3024 retval = ENODEV; 3025 3026 if (req->num_args > 0) { 3027 ctl_copyout_args(req->num_args, req->kern_args); 3028 ctl_free_args(req->num_args, req->kern_args); 3029 } 3030 break; 3031 } 3032 case CTL_PORT_LIST: { 3033 struct sbuf *sb; 3034 struct ctl_port *port; 3035 struct ctl_lun_list *list; 3036 struct ctl_option *opt; 3037 int j; 3038 uint32_t plun; 3039 3040 list = (struct ctl_lun_list *)addr; 3041 3042 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3043 if (sb == NULL) { 3044 list->status = CTL_LUN_LIST_ERROR; 3045 snprintf(list->error_str, sizeof(list->error_str), 3046 "Unable to allocate %d bytes for LUN list", 3047 list->alloc_len); 3048 break; 3049 } 3050 3051 sbuf_printf(sb, "<ctlportlist>\n"); 3052 3053 mtx_lock(&softc->ctl_lock); 3054 STAILQ_FOREACH(port, &softc->port_list, links) { 3055 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3056 (uintmax_t)port->targ_port); 3057 3058 /* 3059 * Bail out as soon as we see that we've overfilled 3060 * the buffer. 3061 */ 3062 if (retval != 0) 3063 break; 3064 3065 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3066 "</frontend_type>\n", port->frontend->name); 3067 if (retval != 0) 3068 break; 3069 3070 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3071 port->port_type); 3072 if (retval != 0) 3073 break; 3074 3075 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3076 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3077 if (retval != 0) 3078 break; 3079 3080 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3081 port->port_name); 3082 if (retval != 0) 3083 break; 3084 3085 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3086 port->physical_port); 3087 if (retval != 0) 3088 break; 3089 3090 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3091 port->virtual_port); 3092 if (retval != 0) 3093 break; 3094 3095 if (port->target_devid != NULL) { 3096 sbuf_printf(sb, "\t<target>"); 3097 ctl_id_sbuf(port->target_devid, sb); 3098 sbuf_printf(sb, "</target>\n"); 3099 } 3100 3101 if (port->port_devid != NULL) { 3102 sbuf_printf(sb, "\t<port>"); 3103 ctl_id_sbuf(port->port_devid, sb); 3104 sbuf_printf(sb, "</port>\n"); 3105 } 3106 3107 if (port->port_info != NULL) { 3108 retval = port->port_info(port->onoff_arg, sb); 3109 if (retval != 0) 3110 break; 3111 } 3112 STAILQ_FOREACH(opt, &port->options, links) { 3113 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3114 opt->name, opt->value, opt->name); 3115 if (retval != 0) 3116 break; 3117 } 3118 3119 if (port->lun_map != NULL) { 3120 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3121 for (j = 0; j < CTL_MAX_LUNS; j++) { 3122 plun = ctl_lun_map_from_port(port, j); 3123 if (plun >= CTL_MAX_LUNS) 3124 continue; 3125 sbuf_printf(sb, 3126 "\t<lun id=\"%u\">%u</lun>\n", 3127 j, plun); 3128 } 3129 } 3130 3131 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3132 if (port->wwpn_iid[j].in_use == 0 || 3133 (port->wwpn_iid[j].wwpn == 0 && 3134 port->wwpn_iid[j].name == NULL)) 3135 continue; 3136 3137 if (port->wwpn_iid[j].name != NULL) 3138 retval = sbuf_printf(sb, 3139 "\t<initiator id=\"%u\">%s</initiator>\n", 3140 j, port->wwpn_iid[j].name); 3141 else 3142 retval = sbuf_printf(sb, 3143 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3144 j, port->wwpn_iid[j].wwpn); 3145 if (retval != 0) 3146 break; 3147 } 3148 if (retval != 0) 3149 break; 3150 3151 retval = sbuf_printf(sb, "</targ_port>\n"); 3152 if (retval != 0) 3153 break; 3154 } 3155 mtx_unlock(&softc->ctl_lock); 3156 3157 if ((retval != 0) 3158 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3159 retval = 0; 3160 sbuf_delete(sb); 3161 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3162 snprintf(list->error_str, sizeof(list->error_str), 3163 "Out of space, %d bytes is too small", 3164 list->alloc_len); 3165 break; 3166 } 3167 3168 sbuf_finish(sb); 3169 3170 retval = copyout(sbuf_data(sb), list->lun_xml, 3171 sbuf_len(sb) + 1); 3172 3173 list->fill_len = sbuf_len(sb) + 1; 3174 list->status = CTL_LUN_LIST_OK; 3175 sbuf_delete(sb); 3176 break; 3177 } 3178 case CTL_LUN_MAP: { 3179 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3180 struct ctl_port *port; 3181 3182 mtx_lock(&softc->ctl_lock); 3183 if (lm->port < softc->port_min || 3184 lm->port >= softc->port_max || 3185 (port = softc->ctl_ports[lm->port]) == NULL) { 3186 mtx_unlock(&softc->ctl_lock); 3187 return (ENXIO); 3188 } 3189 if (port->status & CTL_PORT_STATUS_ONLINE) { 3190 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3191 if (ctl_lun_map_to_port(port, lun->lun) >= 3192 CTL_MAX_LUNS) 3193 continue; 3194 mtx_lock(&lun->lun_lock); 3195 ctl_est_ua_port(lun, lm->port, -1, 3196 CTL_UA_LUN_CHANGE); 3197 mtx_unlock(&lun->lun_lock); 3198 } 3199 } 3200 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3201 if (lm->plun < CTL_MAX_LUNS) { 3202 if (lm->lun == UINT32_MAX) 3203 retval = ctl_lun_map_unset(port, lm->plun); 3204 else if (lm->lun < CTL_MAX_LUNS && 3205 softc->ctl_luns[lm->lun] != NULL) 3206 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3207 else 3208 return (ENXIO); 3209 } else if (lm->plun == UINT32_MAX) { 3210 if (lm->lun == UINT32_MAX) 3211 retval = ctl_lun_map_deinit(port); 3212 else 3213 retval = ctl_lun_map_init(port); 3214 } else 3215 return (ENXIO); 3216 if (port->status & CTL_PORT_STATUS_ONLINE) 3217 ctl_isc_announce_port(port); 3218 break; 3219 } 3220 default: { 3221 /* XXX KDM should we fix this? */ 3222#if 0 3223 struct ctl_backend_driver *backend; 3224 unsigned int type; 3225 int found; 3226 3227 found = 0; 3228 3229 /* 3230 * We encode the backend type as the ioctl type for backend 3231 * ioctls. So parse it out here, and then search for a 3232 * backend of this type. 3233 */ 3234 type = _IOC_TYPE(cmd); 3235 3236 STAILQ_FOREACH(backend, &softc->be_list, links) { 3237 if (backend->type == type) { 3238 found = 1; 3239 break; 3240 } 3241 } 3242 if (found == 0) { 3243 printf("ctl: unknown ioctl command %#lx or backend " 3244 "%d\n", cmd, type); 3245 retval = EINVAL; 3246 break; 3247 } 3248 retval = backend->ioctl(dev, cmd, addr, flag, td); 3249#endif 3250 retval = ENOTTY; 3251 break; 3252 } 3253 } 3254 return (retval); 3255} 3256 3257uint32_t 3258ctl_get_initindex(struct ctl_nexus *nexus) 3259{ 3260 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3261} 3262 3263int 3264ctl_lun_map_init(struct ctl_port *port) 3265{ 3266 struct ctl_softc *softc = control_softc; 3267 struct ctl_lun *lun; 3268 uint32_t i; 3269 3270 if (port->lun_map == NULL) 3271 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3272 M_CTL, M_NOWAIT); 3273 if (port->lun_map == NULL) 3274 return (ENOMEM); 3275 for (i = 0; i < CTL_MAX_LUNS; i++) 3276 port->lun_map[i] = UINT32_MAX; 3277 if (port->status & CTL_PORT_STATUS_ONLINE) { 3278 if (port->lun_disable != NULL) { 3279 STAILQ_FOREACH(lun, &softc->lun_list, links) 3280 port->lun_disable(port->targ_lun_arg, lun->lun); 3281 } 3282 ctl_isc_announce_port(port); 3283 } 3284 return (0); 3285} 3286 3287int 3288ctl_lun_map_deinit(struct ctl_port *port) 3289{ 3290 struct ctl_softc *softc = control_softc; 3291 struct ctl_lun *lun; 3292 3293 if (port->lun_map == NULL) 3294 return (0); 3295 free(port->lun_map, M_CTL); 3296 port->lun_map = NULL; 3297 if (port->status & CTL_PORT_STATUS_ONLINE) { 3298 if (port->lun_enable != NULL) { 3299 STAILQ_FOREACH(lun, &softc->lun_list, links) 3300 port->lun_enable(port->targ_lun_arg, lun->lun); 3301 } 3302 ctl_isc_announce_port(port); 3303 } 3304 return (0); 3305} 3306 3307int 3308ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3309{ 3310 int status; 3311 uint32_t old; 3312 3313 if (port->lun_map == NULL) { 3314 status = ctl_lun_map_init(port); 3315 if (status != 0) 3316 return (status); 3317 } 3318 old = port->lun_map[plun]; 3319 port->lun_map[plun] = glun; 3320 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) { 3321 if (port->lun_enable != NULL) 3322 port->lun_enable(port->targ_lun_arg, plun); 3323 ctl_isc_announce_port(port); 3324 } 3325 return (0); 3326} 3327 3328int 3329ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3330{ 3331 uint32_t old; 3332 3333 if (port->lun_map == NULL) 3334 return (0); 3335 old = port->lun_map[plun]; 3336 port->lun_map[plun] = UINT32_MAX; 3337 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) { 3338 if (port->lun_disable != NULL) 3339 port->lun_disable(port->targ_lun_arg, plun); 3340 ctl_isc_announce_port(port); 3341 } 3342 return (0); 3343} 3344 3345uint32_t 3346ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3347{ 3348 3349 if (port == NULL) 3350 return (UINT32_MAX); 3351 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3352 return (lun_id); 3353 return (port->lun_map[lun_id]); 3354} 3355 3356uint32_t 3357ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3358{ 3359 uint32_t i; 3360 3361 if (port == NULL) 3362 return (UINT32_MAX); 3363 if (port->lun_map == NULL) 3364 return (lun_id); 3365 for (i = 0; i < CTL_MAX_LUNS; i++) { 3366 if (port->lun_map[i] == lun_id) 3367 return (i); 3368 } 3369 return (UINT32_MAX); 3370} 3371 3372static struct ctl_port * 3373ctl_io_port(struct ctl_io_hdr *io_hdr) 3374{ 3375 3376 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]); 3377} 3378 3379int 3380ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3381{ 3382 int i; 3383 3384 for (i = first; i < last; i++) { 3385 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3386 return (i); 3387 } 3388 return (-1); 3389} 3390 3391int 3392ctl_set_mask(uint32_t *mask, uint32_t bit) 3393{ 3394 uint32_t chunk, piece; 3395 3396 chunk = bit >> 5; 3397 piece = bit % (sizeof(uint32_t) * 8); 3398 3399 if ((mask[chunk] & (1 << piece)) != 0) 3400 return (-1); 3401 else 3402 mask[chunk] |= (1 << piece); 3403 3404 return (0); 3405} 3406 3407int 3408ctl_clear_mask(uint32_t *mask, uint32_t bit) 3409{ 3410 uint32_t chunk, piece; 3411 3412 chunk = bit >> 5; 3413 piece = bit % (sizeof(uint32_t) * 8); 3414 3415 if ((mask[chunk] & (1 << piece)) == 0) 3416 return (-1); 3417 else 3418 mask[chunk] &= ~(1 << piece); 3419 3420 return (0); 3421} 3422 3423int 3424ctl_is_set(uint32_t *mask, uint32_t bit) 3425{ 3426 uint32_t chunk, piece; 3427 3428 chunk = bit >> 5; 3429 piece = bit % (sizeof(uint32_t) * 8); 3430 3431 if ((mask[chunk] & (1 << piece)) == 0) 3432 return (0); 3433 else 3434 return (1); 3435} 3436 3437static uint64_t 3438ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3439{ 3440 uint64_t *t; 3441 3442 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3443 if (t == NULL) 3444 return (0); 3445 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3446} 3447 3448static void 3449ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3450{ 3451 uint64_t *t; 3452 3453 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3454 if (t == NULL) 3455 return; 3456 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3457} 3458 3459static void 3460ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3461{ 3462 uint64_t *p; 3463 u_int i; 3464 3465 i = residx/CTL_MAX_INIT_PER_PORT; 3466 if (lun->pr_keys[i] != NULL) 3467 return; 3468 mtx_unlock(&lun->lun_lock); 3469 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3470 M_WAITOK | M_ZERO); 3471 mtx_lock(&lun->lun_lock); 3472 if (lun->pr_keys[i] == NULL) 3473 lun->pr_keys[i] = p; 3474 else 3475 free(p, M_CTL); 3476} 3477 3478static void 3479ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3480{ 3481 uint64_t *t; 3482 3483 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3484 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3485 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3486} 3487 3488/* 3489 * ctl_softc, pool_name, total_ctl_io are passed in. 3490 * npool is passed out. 3491 */ 3492int 3493ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3494 uint32_t total_ctl_io, void **npool) 3495{ 3496#ifdef IO_POOLS 3497 struct ctl_io_pool *pool; 3498 3499 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3500 M_NOWAIT | M_ZERO); 3501 if (pool == NULL) 3502 return (ENOMEM); 3503 3504 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3505 pool->ctl_softc = ctl_softc; 3506 pool->zone = uma_zsecond_create(pool->name, NULL, 3507 NULL, NULL, NULL, ctl_softc->io_zone); 3508 /* uma_prealloc(pool->zone, total_ctl_io); */ 3509 3510 *npool = pool; 3511#else 3512 *npool = ctl_softc->io_zone; 3513#endif 3514 return (0); 3515} 3516 3517void 3518ctl_pool_free(struct ctl_io_pool *pool) 3519{ 3520 3521 if (pool == NULL) 3522 return; 3523 3524#ifdef IO_POOLS 3525 uma_zdestroy(pool->zone); 3526 free(pool, M_CTL); 3527#endif 3528} 3529 3530union ctl_io * 3531ctl_alloc_io(void *pool_ref) 3532{ 3533 union ctl_io *io; 3534#ifdef IO_POOLS 3535 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3536 3537 io = uma_zalloc(pool->zone, M_WAITOK); 3538#else 3539 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3540#endif 3541 if (io != NULL) 3542 io->io_hdr.pool = pool_ref; 3543 return (io); 3544} 3545 3546union ctl_io * 3547ctl_alloc_io_nowait(void *pool_ref) 3548{ 3549 union ctl_io *io; 3550#ifdef IO_POOLS 3551 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3552 3553 io = uma_zalloc(pool->zone, M_NOWAIT); 3554#else 3555 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3556#endif 3557 if (io != NULL) 3558 io->io_hdr.pool = pool_ref; 3559 return (io); 3560} 3561 3562void 3563ctl_free_io(union ctl_io *io) 3564{ 3565#ifdef IO_POOLS 3566 struct ctl_io_pool *pool; 3567#endif 3568 3569 if (io == NULL) 3570 return; 3571 3572#ifdef IO_POOLS 3573 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3574 uma_zfree(pool->zone, io); 3575#else 3576 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3577#endif 3578} 3579 3580void 3581ctl_zero_io(union ctl_io *io) 3582{ 3583 void *pool_ref; 3584 3585 if (io == NULL) 3586 return; 3587 3588 /* 3589 * May need to preserve linked list pointers at some point too. 3590 */ 3591 pool_ref = io->io_hdr.pool; 3592 memset(io, 0, sizeof(*io)); 3593 io->io_hdr.pool = pool_ref; 3594} 3595 3596/* 3597 * This routine is currently used for internal copies of ctl_ios that need 3598 * to persist for some reason after we've already returned status to the 3599 * FETD. (Thus the flag set.) 3600 * 3601 * XXX XXX 3602 * Note that this makes a blind copy of all fields in the ctl_io, except 3603 * for the pool reference. This includes any memory that has been 3604 * allocated! That memory will no longer be valid after done has been 3605 * called, so this would be VERY DANGEROUS for command that actually does 3606 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3607 * start and stop commands, which don't transfer any data, so this is not a 3608 * problem. If it is used for anything else, the caller would also need to 3609 * allocate data buffer space and this routine would need to be modified to 3610 * copy the data buffer(s) as well. 3611 */ 3612void 3613ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3614{ 3615 void *pool_ref; 3616 3617 if ((src == NULL) 3618 || (dest == NULL)) 3619 return; 3620 3621 /* 3622 * May need to preserve linked list pointers at some point too. 3623 */ 3624 pool_ref = dest->io_hdr.pool; 3625 3626 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest))); 3627 3628 dest->io_hdr.pool = pool_ref; 3629 /* 3630 * We need to know that this is an internal copy, and doesn't need 3631 * to get passed back to the FETD that allocated it. 3632 */ 3633 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3634} 3635 3636int 3637ctl_expand_number(const char *buf, uint64_t *num) 3638{ 3639 char *endptr; 3640 uint64_t number; 3641 unsigned shift; 3642 3643 number = strtoq(buf, &endptr, 0); 3644 3645 switch (tolower((unsigned char)*endptr)) { 3646 case 'e': 3647 shift = 60; 3648 break; 3649 case 'p': 3650 shift = 50; 3651 break; 3652 case 't': 3653 shift = 40; 3654 break; 3655 case 'g': 3656 shift = 30; 3657 break; 3658 case 'm': 3659 shift = 20; 3660 break; 3661 case 'k': 3662 shift = 10; 3663 break; 3664 case 'b': 3665 case '\0': /* No unit. */ 3666 *num = number; 3667 return (0); 3668 default: 3669 /* Unrecognized unit. */ 3670 return (-1); 3671 } 3672 3673 if ((number << shift) >> shift != number) { 3674 /* Overflow */ 3675 return (-1); 3676 } 3677 *num = number << shift; 3678 return (0); 3679} 3680 3681 3682/* 3683 * This routine could be used in the future to load default and/or saved 3684 * mode page parameters for a particuar lun. 3685 */ 3686static int 3687ctl_init_page_index(struct ctl_lun *lun) 3688{ 3689 int i; 3690 struct ctl_page_index *page_index; 3691 const char *value; 3692 uint64_t ival; 3693 3694 memcpy(&lun->mode_pages.index, page_index_template, 3695 sizeof(page_index_template)); 3696 3697 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3698 3699 page_index = &lun->mode_pages.index[i]; 3700 /* 3701 * If this is a disk-only mode page, there's no point in 3702 * setting it up. For some pages, we have to have some 3703 * basic information about the disk in order to calculate the 3704 * mode page data. 3705 */ 3706 if ((lun->be_lun->lun_type != T_DIRECT) 3707 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3708 continue; 3709 3710 switch (page_index->page_code & SMPH_PC_MASK) { 3711 case SMS_RW_ERROR_RECOVERY_PAGE: { 3712 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3713 panic("subpage is incorrect!"); 3714 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3715 &rw_er_page_default, 3716 sizeof(rw_er_page_default)); 3717 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3718 &rw_er_page_changeable, 3719 sizeof(rw_er_page_changeable)); 3720 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3721 &rw_er_page_default, 3722 sizeof(rw_er_page_default)); 3723 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3724 &rw_er_page_default, 3725 sizeof(rw_er_page_default)); 3726 page_index->page_data = 3727 (uint8_t *)lun->mode_pages.rw_er_page; 3728 break; 3729 } 3730 case SMS_FORMAT_DEVICE_PAGE: { 3731 struct scsi_format_page *format_page; 3732 3733 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3734 panic("subpage is incorrect!"); 3735 3736 /* 3737 * Sectors per track are set above. Bytes per 3738 * sector need to be set here on a per-LUN basis. 3739 */ 3740 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3741 &format_page_default, 3742 sizeof(format_page_default)); 3743 memcpy(&lun->mode_pages.format_page[ 3744 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3745 sizeof(format_page_changeable)); 3746 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3747 &format_page_default, 3748 sizeof(format_page_default)); 3749 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3750 &format_page_default, 3751 sizeof(format_page_default)); 3752 3753 format_page = &lun->mode_pages.format_page[ 3754 CTL_PAGE_CURRENT]; 3755 scsi_ulto2b(lun->be_lun->blocksize, 3756 format_page->bytes_per_sector); 3757 3758 format_page = &lun->mode_pages.format_page[ 3759 CTL_PAGE_DEFAULT]; 3760 scsi_ulto2b(lun->be_lun->blocksize, 3761 format_page->bytes_per_sector); 3762 3763 format_page = &lun->mode_pages.format_page[ 3764 CTL_PAGE_SAVED]; 3765 scsi_ulto2b(lun->be_lun->blocksize, 3766 format_page->bytes_per_sector); 3767 3768 page_index->page_data = 3769 (uint8_t *)lun->mode_pages.format_page; 3770 break; 3771 } 3772 case SMS_RIGID_DISK_PAGE: { 3773 struct scsi_rigid_disk_page *rigid_disk_page; 3774 uint32_t sectors_per_cylinder; 3775 uint64_t cylinders; 3776#ifndef __XSCALE__ 3777 int shift; 3778#endif /* !__XSCALE__ */ 3779 3780 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3781 panic("invalid subpage value %d", 3782 page_index->subpage); 3783 3784 /* 3785 * Rotation rate and sectors per track are set 3786 * above. We calculate the cylinders here based on 3787 * capacity. Due to the number of heads and 3788 * sectors per track we're using, smaller arrays 3789 * may turn out to have 0 cylinders. Linux and 3790 * FreeBSD don't pay attention to these mode pages 3791 * to figure out capacity, but Solaris does. It 3792 * seems to deal with 0 cylinders just fine, and 3793 * works out a fake geometry based on the capacity. 3794 */ 3795 memcpy(&lun->mode_pages.rigid_disk_page[ 3796 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3797 sizeof(rigid_disk_page_default)); 3798 memcpy(&lun->mode_pages.rigid_disk_page[ 3799 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3800 sizeof(rigid_disk_page_changeable)); 3801 3802 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 3803 CTL_DEFAULT_HEADS; 3804 3805 /* 3806 * The divide method here will be more accurate, 3807 * probably, but results in floating point being 3808 * used in the kernel on i386 (__udivdi3()). On the 3809 * XScale, though, __udivdi3() is implemented in 3810 * software. 3811 * 3812 * The shift method for cylinder calculation is 3813 * accurate if sectors_per_cylinder is a power of 3814 * 2. Otherwise it might be slightly off -- you 3815 * might have a bit of a truncation problem. 3816 */ 3817#ifdef __XSCALE__ 3818 cylinders = (lun->be_lun->maxlba + 1) / 3819 sectors_per_cylinder; 3820#else 3821 for (shift = 31; shift > 0; shift--) { 3822 if (sectors_per_cylinder & (1 << shift)) 3823 break; 3824 } 3825 cylinders = (lun->be_lun->maxlba + 1) >> shift; 3826#endif 3827 3828 /* 3829 * We've basically got 3 bytes, or 24 bits for the 3830 * cylinder size in the mode page. If we're over, 3831 * just round down to 2^24. 3832 */ 3833 if (cylinders > 0xffffff) 3834 cylinders = 0xffffff; 3835 3836 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 3837 CTL_PAGE_DEFAULT]; 3838 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 3839 3840 if ((value = ctl_get_opt(&lun->be_lun->options, 3841 "rpm")) != NULL) { 3842 scsi_ulto2b(strtol(value, NULL, 0), 3843 rigid_disk_page->rotation_rate); 3844 } 3845 3846 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 3847 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3848 sizeof(rigid_disk_page_default)); 3849 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 3850 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3851 sizeof(rigid_disk_page_default)); 3852 3853 page_index->page_data = 3854 (uint8_t *)lun->mode_pages.rigid_disk_page; 3855 break; 3856 } 3857 case SMS_CACHING_PAGE: { 3858 struct scsi_caching_page *caching_page; 3859 3860 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3861 panic("invalid subpage value %d", 3862 page_index->subpage); 3863 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 3864 &caching_page_default, 3865 sizeof(caching_page_default)); 3866 memcpy(&lun->mode_pages.caching_page[ 3867 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 3868 sizeof(caching_page_changeable)); 3869 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 3870 &caching_page_default, 3871 sizeof(caching_page_default)); 3872 caching_page = &lun->mode_pages.caching_page[ 3873 CTL_PAGE_SAVED]; 3874 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 3875 if (value != NULL && strcmp(value, "off") == 0) 3876 caching_page->flags1 &= ~SCP_WCE; 3877 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 3878 if (value != NULL && strcmp(value, "off") == 0) 3879 caching_page->flags1 |= SCP_RCD; 3880 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 3881 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 3882 sizeof(caching_page_default)); 3883 page_index->page_data = 3884 (uint8_t *)lun->mode_pages.caching_page; 3885 break; 3886 } 3887 case SMS_CONTROL_MODE_PAGE: { 3888 struct scsi_control_page *control_page; 3889 3890 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3891 panic("invalid subpage value %d", 3892 page_index->subpage); 3893 3894 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 3895 &control_page_default, 3896 sizeof(control_page_default)); 3897 memcpy(&lun->mode_pages.control_page[ 3898 CTL_PAGE_CHANGEABLE], &control_page_changeable, 3899 sizeof(control_page_changeable)); 3900 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 3901 &control_page_default, 3902 sizeof(control_page_default)); 3903 control_page = &lun->mode_pages.control_page[ 3904 CTL_PAGE_SAVED]; 3905 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 3906 if (value != NULL && strcmp(value, "unrestricted") == 0) { 3907 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 3908 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 3909 } 3910 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 3911 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 3912 sizeof(control_page_default)); 3913 page_index->page_data = 3914 (uint8_t *)lun->mode_pages.control_page; 3915 break; 3916 3917 } 3918 case SMS_INFO_EXCEPTIONS_PAGE: { 3919 switch (page_index->subpage) { 3920 case SMS_SUBPAGE_PAGE_0: 3921 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 3922 &ie_page_default, 3923 sizeof(ie_page_default)); 3924 memcpy(&lun->mode_pages.ie_page[ 3925 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 3926 sizeof(ie_page_changeable)); 3927 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 3928 &ie_page_default, 3929 sizeof(ie_page_default)); 3930 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 3931 &ie_page_default, 3932 sizeof(ie_page_default)); 3933 page_index->page_data = 3934 (uint8_t *)lun->mode_pages.ie_page; 3935 break; 3936 case 0x02: { 3937 struct ctl_logical_block_provisioning_page *page; 3938 3939 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 3940 &lbp_page_default, 3941 sizeof(lbp_page_default)); 3942 memcpy(&lun->mode_pages.lbp_page[ 3943 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 3944 sizeof(lbp_page_changeable)); 3945 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 3946 &lbp_page_default, 3947 sizeof(lbp_page_default)); 3948 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 3949 value = ctl_get_opt(&lun->be_lun->options, 3950 "avail-threshold"); 3951 if (value != NULL && 3952 ctl_expand_number(value, &ival) == 0) { 3953 page->descr[0].flags |= SLBPPD_ENABLED | 3954 SLBPPD_ARMING_DEC; 3955 if (lun->be_lun->blocksize) 3956 ival /= lun->be_lun->blocksize; 3957 else 3958 ival /= 512; 3959 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3960 page->descr[0].count); 3961 } 3962 value = ctl_get_opt(&lun->be_lun->options, 3963 "used-threshold"); 3964 if (value != NULL && 3965 ctl_expand_number(value, &ival) == 0) { 3966 page->descr[1].flags |= SLBPPD_ENABLED | 3967 SLBPPD_ARMING_INC; 3968 if (lun->be_lun->blocksize) 3969 ival /= lun->be_lun->blocksize; 3970 else 3971 ival /= 512; 3972 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3973 page->descr[1].count); 3974 } 3975 value = ctl_get_opt(&lun->be_lun->options, 3976 "pool-avail-threshold"); 3977 if (value != NULL && 3978 ctl_expand_number(value, &ival) == 0) { 3979 page->descr[2].flags |= SLBPPD_ENABLED | 3980 SLBPPD_ARMING_DEC; 3981 if (lun->be_lun->blocksize) 3982 ival /= lun->be_lun->blocksize; 3983 else 3984 ival /= 512; 3985 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3986 page->descr[2].count); 3987 } 3988 value = ctl_get_opt(&lun->be_lun->options, 3989 "pool-used-threshold"); 3990 if (value != NULL && 3991 ctl_expand_number(value, &ival) == 0) { 3992 page->descr[3].flags |= SLBPPD_ENABLED | 3993 SLBPPD_ARMING_INC; 3994 if (lun->be_lun->blocksize) 3995 ival /= lun->be_lun->blocksize; 3996 else 3997 ival /= 512; 3998 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3999 page->descr[3].count); 4000 } 4001 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4002 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4003 sizeof(lbp_page_default)); 4004 page_index->page_data = 4005 (uint8_t *)lun->mode_pages.lbp_page; 4006 }} 4007 break; 4008 } 4009 case SMS_VENDOR_SPECIFIC_PAGE:{ 4010 switch (page_index->subpage) { 4011 case DBGCNF_SUBPAGE_CODE: { 4012 struct copan_debugconf_subpage *current_page, 4013 *saved_page; 4014 4015 memcpy(&lun->mode_pages.debugconf_subpage[ 4016 CTL_PAGE_CURRENT], 4017 &debugconf_page_default, 4018 sizeof(debugconf_page_default)); 4019 memcpy(&lun->mode_pages.debugconf_subpage[ 4020 CTL_PAGE_CHANGEABLE], 4021 &debugconf_page_changeable, 4022 sizeof(debugconf_page_changeable)); 4023 memcpy(&lun->mode_pages.debugconf_subpage[ 4024 CTL_PAGE_DEFAULT], 4025 &debugconf_page_default, 4026 sizeof(debugconf_page_default)); 4027 memcpy(&lun->mode_pages.debugconf_subpage[ 4028 CTL_PAGE_SAVED], 4029 &debugconf_page_default, 4030 sizeof(debugconf_page_default)); 4031 page_index->page_data = 4032 (uint8_t *)lun->mode_pages.debugconf_subpage; 4033 4034 current_page = (struct copan_debugconf_subpage *) 4035 (page_index->page_data + 4036 (page_index->page_len * 4037 CTL_PAGE_CURRENT)); 4038 saved_page = (struct copan_debugconf_subpage *) 4039 (page_index->page_data + 4040 (page_index->page_len * 4041 CTL_PAGE_SAVED)); 4042 break; 4043 } 4044 default: 4045 panic("invalid subpage value %d", 4046 page_index->subpage); 4047 break; 4048 } 4049 break; 4050 } 4051 default: 4052 panic("invalid page value %d", 4053 page_index->page_code & SMPH_PC_MASK); 4054 break; 4055 } 4056 } 4057 4058 return (CTL_RETVAL_COMPLETE); 4059} 4060 4061static int 4062ctl_init_log_page_index(struct ctl_lun *lun) 4063{ 4064 struct ctl_page_index *page_index; 4065 int i, j, k, prev; 4066 4067 memcpy(&lun->log_pages.index, log_page_index_template, 4068 sizeof(log_page_index_template)); 4069 4070 prev = -1; 4071 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4072 4073 page_index = &lun->log_pages.index[i]; 4074 /* 4075 * If this is a disk-only mode page, there's no point in 4076 * setting it up. For some pages, we have to have some 4077 * basic information about the disk in order to calculate the 4078 * mode page data. 4079 */ 4080 if ((lun->be_lun->lun_type != T_DIRECT) 4081 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4082 continue; 4083 4084 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4085 lun->backend->lun_attr == NULL) 4086 continue; 4087 4088 if (page_index->page_code != prev) { 4089 lun->log_pages.pages_page[j] = page_index->page_code; 4090 prev = page_index->page_code; 4091 j++; 4092 } 4093 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4094 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4095 k++; 4096 } 4097 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4098 lun->log_pages.index[0].page_len = j; 4099 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4100 lun->log_pages.index[1].page_len = k * 2; 4101 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4102 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4103 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4104 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4105 4106 return (CTL_RETVAL_COMPLETE); 4107} 4108 4109static int 4110hex2bin(const char *str, uint8_t *buf, int buf_size) 4111{ 4112 int i; 4113 u_char c; 4114 4115 memset(buf, 0, buf_size); 4116 while (isspace(str[0])) 4117 str++; 4118 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4119 str += 2; 4120 buf_size *= 2; 4121 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4122 c = str[i]; 4123 if (isdigit(c)) 4124 c -= '0'; 4125 else if (isalpha(c)) 4126 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4127 else 4128 break; 4129 if (c >= 16) 4130 break; 4131 if ((i & 1) == 0) 4132 buf[i / 2] |= (c << 4); 4133 else 4134 buf[i / 2] |= c; 4135 } 4136 return ((i + 1) / 2); 4137} 4138 4139/* 4140 * LUN allocation. 4141 * 4142 * Requirements: 4143 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4144 * wants us to allocate the LUN and he can block. 4145 * - ctl_softc is always set 4146 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4147 * 4148 * Returns 0 for success, non-zero (errno) for failure. 4149 */ 4150static int 4151ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4152 struct ctl_be_lun *const be_lun) 4153{ 4154 struct ctl_lun *nlun, *lun; 4155 struct scsi_vpd_id_descriptor *desc; 4156 struct scsi_vpd_id_t10 *t10id; 4157 const char *eui, *naa, *scsiname, *vendor; 4158 int lun_number, i, lun_malloced; 4159 int devidlen, idlen1, idlen2 = 0, len; 4160 4161 if (be_lun == NULL) 4162 return (EINVAL); 4163 4164 /* 4165 * We currently only support Direct Access or Processor LUN types. 4166 */ 4167 switch (be_lun->lun_type) { 4168 case T_DIRECT: 4169 break; 4170 case T_PROCESSOR: 4171 break; 4172 case T_SEQUENTIAL: 4173 case T_CHANGER: 4174 default: 4175 be_lun->lun_config_status(be_lun->be_lun, 4176 CTL_LUN_CONFIG_FAILURE); 4177 break; 4178 } 4179 if (ctl_lun == NULL) { 4180 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4181 lun_malloced = 1; 4182 } else { 4183 lun_malloced = 0; 4184 lun = ctl_lun; 4185 } 4186 4187 memset(lun, 0, sizeof(*lun)); 4188 if (lun_malloced) 4189 lun->flags = CTL_LUN_MALLOCED; 4190 4191 /* Generate LUN ID. */ 4192 devidlen = max(CTL_DEVID_MIN_LEN, 4193 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4194 idlen1 = sizeof(*t10id) + devidlen; 4195 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4196 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4197 if (scsiname != NULL) { 4198 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4199 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4200 } 4201 eui = ctl_get_opt(&be_lun->options, "eui"); 4202 if (eui != NULL) { 4203 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4204 } 4205 naa = ctl_get_opt(&be_lun->options, "naa"); 4206 if (naa != NULL) { 4207 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4208 } 4209 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4210 M_CTL, M_WAITOK | M_ZERO); 4211 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4212 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4213 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4214 desc->length = idlen1; 4215 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4216 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4217 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4218 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4219 } else { 4220 strncpy(t10id->vendor, vendor, 4221 min(sizeof(t10id->vendor), strlen(vendor))); 4222 } 4223 strncpy((char *)t10id->vendor_spec_id, 4224 (char *)be_lun->device_id, devidlen); 4225 if (scsiname != NULL) { 4226 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4227 desc->length); 4228 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4229 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4230 SVPD_ID_TYPE_SCSI_NAME; 4231 desc->length = idlen2; 4232 strlcpy(desc->identifier, scsiname, idlen2); 4233 } 4234 if (eui != NULL) { 4235 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4236 desc->length); 4237 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4238 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4239 SVPD_ID_TYPE_EUI64; 4240 desc->length = hex2bin(eui, desc->identifier, 16); 4241 desc->length = desc->length > 12 ? 16 : 4242 (desc->length > 8 ? 12 : 8); 4243 len -= 16 - desc->length; 4244 } 4245 if (naa != NULL) { 4246 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4247 desc->length); 4248 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4249 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4250 SVPD_ID_TYPE_NAA; 4251 desc->length = hex2bin(naa, desc->identifier, 16); 4252 desc->length = desc->length > 8 ? 16 : 8; 4253 len -= 16 - desc->length; 4254 } 4255 lun->lun_devid->len = len; 4256 4257 mtx_lock(&ctl_softc->ctl_lock); 4258 /* 4259 * See if the caller requested a particular LUN number. If so, see 4260 * if it is available. Otherwise, allocate the first available LUN. 4261 */ 4262 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4263 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4264 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4265 mtx_unlock(&ctl_softc->ctl_lock); 4266 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4267 printf("ctl: requested LUN ID %d is higher " 4268 "than CTL_MAX_LUNS - 1 (%d)\n", 4269 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4270 } else { 4271 /* 4272 * XXX KDM return an error, or just assign 4273 * another LUN ID in this case?? 4274 */ 4275 printf("ctl: requested LUN ID %d is already " 4276 "in use\n", be_lun->req_lun_id); 4277 } 4278 if (lun->flags & CTL_LUN_MALLOCED) 4279 free(lun, M_CTL); 4280 be_lun->lun_config_status(be_lun->be_lun, 4281 CTL_LUN_CONFIG_FAILURE); 4282 return (ENOSPC); 4283 } 4284 lun_number = be_lun->req_lun_id; 4285 } else { 4286 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4287 if (lun_number == -1) { 4288 mtx_unlock(&ctl_softc->ctl_lock); 4289 printf("ctl: can't allocate LUN, out of LUNs\n"); 4290 if (lun->flags & CTL_LUN_MALLOCED) 4291 free(lun, M_CTL); 4292 be_lun->lun_config_status(be_lun->be_lun, 4293 CTL_LUN_CONFIG_FAILURE); 4294 return (ENOSPC); 4295 } 4296 } 4297 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4298 4299 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4300 lun->lun = lun_number; 4301 lun->be_lun = be_lun; 4302 /* 4303 * The processor LUN is always enabled. Disk LUNs come on line 4304 * disabled, and must be enabled by the backend. 4305 */ 4306 lun->flags |= CTL_LUN_DISABLED; 4307 lun->backend = be_lun->be; 4308 be_lun->ctl_lun = lun; 4309 be_lun->lun_id = lun_number; 4310 atomic_add_int(&be_lun->be->num_luns, 1); 4311 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4312 lun->flags |= CTL_LUN_OFFLINE; 4313 4314 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4315 lun->flags |= CTL_LUN_STOPPED; 4316 4317 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4318 lun->flags |= CTL_LUN_INOPERABLE; 4319 4320 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4321 lun->flags |= CTL_LUN_PRIMARY_SC; 4322 4323 lun->ctl_softc = ctl_softc; 4324#ifdef CTL_TIME_IO 4325 lun->last_busy = getsbinuptime(); 4326#endif 4327 TAILQ_INIT(&lun->ooa_queue); 4328 TAILQ_INIT(&lun->blocked_queue); 4329 STAILQ_INIT(&lun->error_list); 4330 ctl_tpc_lun_init(lun); 4331 4332 /* 4333 * Initialize the mode and log page index. 4334 */ 4335 ctl_init_page_index(lun); 4336 ctl_init_log_page_index(lun); 4337 4338 /* 4339 * Now, before we insert this lun on the lun list, set the lun 4340 * inventory changed UA for all other luns. 4341 */ 4342 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4343 mtx_lock(&nlun->lun_lock); 4344 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4345 mtx_unlock(&nlun->lun_lock); 4346 } 4347 4348 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4349 4350 ctl_softc->ctl_luns[lun_number] = lun; 4351 4352 ctl_softc->num_luns++; 4353 4354 /* Setup statistics gathering */ 4355 lun->stats.device_type = be_lun->lun_type; 4356 lun->stats.lun_number = lun_number; 4357 if (lun->stats.device_type == T_DIRECT) 4358 lun->stats.blocksize = be_lun->blocksize; 4359 else 4360 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4361 for (i = 0;i < CTL_MAX_PORTS;i++) 4362 lun->stats.ports[i].targ_port = i; 4363 4364 mtx_unlock(&ctl_softc->ctl_lock); 4365 4366 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4367 return (0); 4368} 4369 4370/* 4371 * Delete a LUN. 4372 * Assumptions: 4373 * - LUN has already been marked invalid and any pending I/O has been taken 4374 * care of. 4375 */ 4376static int 4377ctl_free_lun(struct ctl_lun *lun) 4378{ 4379 struct ctl_softc *softc; 4380 struct ctl_lun *nlun; 4381 int i; 4382 4383 softc = lun->ctl_softc; 4384 4385 mtx_assert(&softc->ctl_lock, MA_OWNED); 4386 4387 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4388 4389 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4390 4391 softc->ctl_luns[lun->lun] = NULL; 4392 4393 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4394 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4395 4396 softc->num_luns--; 4397 4398 /* 4399 * Tell the backend to free resources, if this LUN has a backend. 4400 */ 4401 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4402 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4403 4404 ctl_tpc_lun_shutdown(lun); 4405 mtx_destroy(&lun->lun_lock); 4406 free(lun->lun_devid, M_CTL); 4407 for (i = 0; i < CTL_MAX_PORTS; i++) 4408 free(lun->pending_ua[i], M_CTL); 4409 for (i = 0; i < CTL_MAX_PORTS; i++) 4410 free(lun->pr_keys[i], M_CTL); 4411 free(lun->write_buffer, M_CTL); 4412 if (lun->flags & CTL_LUN_MALLOCED) 4413 free(lun, M_CTL); 4414 4415 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4416 mtx_lock(&nlun->lun_lock); 4417 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4418 mtx_unlock(&nlun->lun_lock); 4419 } 4420 4421 return (0); 4422} 4423 4424static void 4425ctl_create_lun(struct ctl_be_lun *be_lun) 4426{ 4427 struct ctl_softc *softc; 4428 4429 softc = control_softc; 4430 4431 /* 4432 * ctl_alloc_lun() should handle all potential failure cases. 4433 */ 4434 ctl_alloc_lun(softc, NULL, be_lun); 4435} 4436 4437int 4438ctl_add_lun(struct ctl_be_lun *be_lun) 4439{ 4440 struct ctl_softc *softc = control_softc; 4441 4442 mtx_lock(&softc->ctl_lock); 4443 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4444 mtx_unlock(&softc->ctl_lock); 4445 wakeup(&softc->pending_lun_queue); 4446 4447 return (0); 4448} 4449 4450int 4451ctl_enable_lun(struct ctl_be_lun *be_lun) 4452{ 4453 struct ctl_softc *softc; 4454 struct ctl_port *port, *nport; 4455 struct ctl_lun *lun; 4456 int retval; 4457 4458 lun = (struct ctl_lun *)be_lun->ctl_lun; 4459 softc = lun->ctl_softc; 4460 4461 mtx_lock(&softc->ctl_lock); 4462 mtx_lock(&lun->lun_lock); 4463 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4464 /* 4465 * eh? Why did we get called if the LUN is already 4466 * enabled? 4467 */ 4468 mtx_unlock(&lun->lun_lock); 4469 mtx_unlock(&softc->ctl_lock); 4470 return (0); 4471 } 4472 lun->flags &= ~CTL_LUN_DISABLED; 4473 mtx_unlock(&lun->lun_lock); 4474 4475 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) { 4476 nport = STAILQ_NEXT(port, links); 4477 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4478 port->lun_map != NULL || port->lun_enable == NULL) 4479 continue; 4480 4481 /* 4482 * Drop the lock while we call the FETD's enable routine. 4483 * This can lead to a callback into CTL (at least in the 4484 * case of the internal initiator frontend. 4485 */ 4486 mtx_unlock(&softc->ctl_lock); 4487 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4488 mtx_lock(&softc->ctl_lock); 4489 if (retval != 0) { 4490 printf("%s: FETD %s port %d returned error " 4491 "%d for lun_enable on lun %jd\n", 4492 __func__, port->port_name, port->targ_port, 4493 retval, (intmax_t)lun->lun); 4494 } 4495 } 4496 4497 mtx_unlock(&softc->ctl_lock); 4498 ctl_isc_announce_lun(lun); 4499 4500 return (0); 4501} 4502 4503int 4504ctl_disable_lun(struct ctl_be_lun *be_lun) 4505{ 4506 struct ctl_softc *softc; 4507 struct ctl_port *port; 4508 struct ctl_lun *lun; 4509 int retval; 4510 4511 lun = (struct ctl_lun *)be_lun->ctl_lun; 4512 softc = lun->ctl_softc; 4513 4514 mtx_lock(&softc->ctl_lock); 4515 mtx_lock(&lun->lun_lock); 4516 if (lun->flags & CTL_LUN_DISABLED) { 4517 mtx_unlock(&lun->lun_lock); 4518 mtx_unlock(&softc->ctl_lock); 4519 return (0); 4520 } 4521 lun->flags |= CTL_LUN_DISABLED; 4522 mtx_unlock(&lun->lun_lock); 4523 4524 STAILQ_FOREACH(port, &softc->port_list, links) { 4525 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4526 port->lun_map != NULL || port->lun_disable == NULL) 4527 continue; 4528 4529 /* 4530 * Drop the lock before we call the frontend's disable 4531 * routine, to avoid lock order reversals. 4532 * 4533 * XXX KDM what happens if the frontend list changes while 4534 * we're traversing it? It's unlikely, but should be handled. 4535 */ 4536 mtx_unlock(&softc->ctl_lock); 4537 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4538 mtx_lock(&softc->ctl_lock); 4539 if (retval != 0) { 4540 printf("%s: FETD %s port %d returned error " 4541 "%d for lun_disable on lun %jd\n", 4542 __func__, port->port_name, port->targ_port, 4543 retval, (intmax_t)lun->lun); 4544 } 4545 } 4546 4547 mtx_unlock(&softc->ctl_lock); 4548 ctl_isc_announce_lun(lun); 4549 4550 return (0); 4551} 4552 4553int 4554ctl_start_lun(struct ctl_be_lun *be_lun) 4555{ 4556 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4557 4558 mtx_lock(&lun->lun_lock); 4559 lun->flags &= ~CTL_LUN_STOPPED; 4560 mtx_unlock(&lun->lun_lock); 4561 return (0); 4562} 4563 4564int 4565ctl_stop_lun(struct ctl_be_lun *be_lun) 4566{ 4567 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4568 4569 mtx_lock(&lun->lun_lock); 4570 lun->flags |= CTL_LUN_STOPPED; 4571 mtx_unlock(&lun->lun_lock); 4572 return (0); 4573} 4574 4575int 4576ctl_lun_offline(struct ctl_be_lun *be_lun) 4577{ 4578 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4579 4580 mtx_lock(&lun->lun_lock); 4581 lun->flags |= CTL_LUN_OFFLINE; 4582 mtx_unlock(&lun->lun_lock); 4583 return (0); 4584} 4585 4586int 4587ctl_lun_online(struct ctl_be_lun *be_lun) 4588{ 4589 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4590 4591 mtx_lock(&lun->lun_lock); 4592 lun->flags &= ~CTL_LUN_OFFLINE; 4593 mtx_unlock(&lun->lun_lock); 4594 return (0); 4595} 4596 4597int 4598ctl_lun_primary(struct ctl_be_lun *be_lun) 4599{ 4600 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4601 4602 mtx_lock(&lun->lun_lock); 4603 lun->flags |= CTL_LUN_PRIMARY_SC; 4604 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4605 mtx_unlock(&lun->lun_lock); 4606 ctl_isc_announce_lun(lun); 4607 return (0); 4608} 4609 4610int 4611ctl_lun_secondary(struct ctl_be_lun *be_lun) 4612{ 4613 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4614 4615 mtx_lock(&lun->lun_lock); 4616 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4617 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4618 mtx_unlock(&lun->lun_lock); 4619 ctl_isc_announce_lun(lun); 4620 return (0); 4621} 4622 4623int 4624ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4625{ 4626 struct ctl_softc *softc; 4627 struct ctl_lun *lun; 4628 4629 lun = (struct ctl_lun *)be_lun->ctl_lun; 4630 softc = lun->ctl_softc; 4631 4632 mtx_lock(&lun->lun_lock); 4633 4634 /* 4635 * The LUN needs to be disabled before it can be marked invalid. 4636 */ 4637 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4638 mtx_unlock(&lun->lun_lock); 4639 return (-1); 4640 } 4641 /* 4642 * Mark the LUN invalid. 4643 */ 4644 lun->flags |= CTL_LUN_INVALID; 4645 4646 /* 4647 * If there is nothing in the OOA queue, go ahead and free the LUN. 4648 * If we have something in the OOA queue, we'll free it when the 4649 * last I/O completes. 4650 */ 4651 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4652 mtx_unlock(&lun->lun_lock); 4653 mtx_lock(&softc->ctl_lock); 4654 ctl_free_lun(lun); 4655 mtx_unlock(&softc->ctl_lock); 4656 } else 4657 mtx_unlock(&lun->lun_lock); 4658 4659 return (0); 4660} 4661 4662int 4663ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4664{ 4665 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4666 4667 mtx_lock(&lun->lun_lock); 4668 lun->flags |= CTL_LUN_INOPERABLE; 4669 mtx_unlock(&lun->lun_lock); 4670 return (0); 4671} 4672 4673int 4674ctl_lun_operable(struct ctl_be_lun *be_lun) 4675{ 4676 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4677 4678 mtx_lock(&lun->lun_lock); 4679 lun->flags &= ~CTL_LUN_INOPERABLE; 4680 mtx_unlock(&lun->lun_lock); 4681 return (0); 4682} 4683 4684void 4685ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4686{ 4687 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4688 union ctl_ha_msg msg; 4689 4690 mtx_lock(&lun->lun_lock); 4691 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED); 4692 mtx_unlock(&lun->lun_lock); 4693 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4694 /* Send msg to other side. */ 4695 bzero(&msg.ua, sizeof(msg.ua)); 4696 msg.hdr.msg_type = CTL_MSG_UA; 4697 msg.hdr.nexus.initid = -1; 4698 msg.hdr.nexus.targ_port = -1; 4699 msg.hdr.nexus.targ_lun = lun->lun; 4700 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4701 msg.ua.ua_all = 1; 4702 msg.ua.ua_set = 1; 4703 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGED; 4704 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4705 M_WAITOK); 4706 } 4707} 4708 4709/* 4710 * Backend "memory move is complete" callback for requests that never 4711 * make it down to say RAIDCore's configuration code. 4712 */ 4713int 4714ctl_config_move_done(union ctl_io *io) 4715{ 4716 int retval; 4717 4718 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4719 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4720 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 4721 4722 if ((io->io_hdr.port_status != 0) && 4723 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4724 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4725 /* 4726 * For hardware error sense keys, the sense key 4727 * specific value is defined to be a retry count, 4728 * but we use it to pass back an internal FETD 4729 * error code. XXX KDM Hopefully the FETD is only 4730 * using 16 bits for an error code, since that's 4731 * all the space we have in the sks field. 4732 */ 4733 ctl_set_internal_failure(&io->scsiio, 4734 /*sks_valid*/ 1, 4735 /*retry_count*/ 4736 io->io_hdr.port_status); 4737 } 4738 4739 if (ctl_debug & CTL_DEBUG_CDB_DATA) 4740 ctl_data_print(io); 4741 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 4742 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4743 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 4744 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4745 /* 4746 * XXX KDM just assuming a single pointer here, and not a 4747 * S/G list. If we start using S/G lists for config data, 4748 * we'll need to know how to clean them up here as well. 4749 */ 4750 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4751 free(io->scsiio.kern_data_ptr, M_CTL); 4752 ctl_done(io); 4753 retval = CTL_RETVAL_COMPLETE; 4754 } else { 4755 /* 4756 * XXX KDM now we need to continue data movement. Some 4757 * options: 4758 * - call ctl_scsiio() again? We don't do this for data 4759 * writes, because for those at least we know ahead of 4760 * time where the write will go and how long it is. For 4761 * config writes, though, that information is largely 4762 * contained within the write itself, thus we need to 4763 * parse out the data again. 4764 * 4765 * - Call some other function once the data is in? 4766 */ 4767 4768 /* 4769 * XXX KDM call ctl_scsiio() again for now, and check flag 4770 * bits to see whether we're allocated or not. 4771 */ 4772 retval = ctl_scsiio(&io->scsiio); 4773 } 4774 return (retval); 4775} 4776 4777/* 4778 * This gets called by a backend driver when it is done with a 4779 * data_submit method. 4780 */ 4781void 4782ctl_data_submit_done(union ctl_io *io) 4783{ 4784 /* 4785 * If the IO_CONT flag is set, we need to call the supplied 4786 * function to continue processing the I/O, instead of completing 4787 * the I/O just yet. 4788 * 4789 * If there is an error, though, we don't want to keep processing. 4790 * Instead, just send status back to the initiator. 4791 */ 4792 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4793 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4794 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4795 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4796 io->scsiio.io_cont(io); 4797 return; 4798 } 4799 ctl_done(io); 4800} 4801 4802/* 4803 * This gets called by a backend driver when it is done with a 4804 * configuration write. 4805 */ 4806void 4807ctl_config_write_done(union ctl_io *io) 4808{ 4809 uint8_t *buf; 4810 4811 /* 4812 * If the IO_CONT flag is set, we need to call the supplied 4813 * function to continue processing the I/O, instead of completing 4814 * the I/O just yet. 4815 * 4816 * If there is an error, though, we don't want to keep processing. 4817 * Instead, just send status back to the initiator. 4818 */ 4819 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4820 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4821 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4822 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4823 io->scsiio.io_cont(io); 4824 return; 4825 } 4826 /* 4827 * Since a configuration write can be done for commands that actually 4828 * have data allocated, like write buffer, and commands that have 4829 * no data, like start/stop unit, we need to check here. 4830 */ 4831 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4832 buf = io->scsiio.kern_data_ptr; 4833 else 4834 buf = NULL; 4835 ctl_done(io); 4836 if (buf) 4837 free(buf, M_CTL); 4838} 4839 4840void 4841ctl_config_read_done(union ctl_io *io) 4842{ 4843 uint8_t *buf; 4844 4845 /* 4846 * If there is some error -- we are done, skip data transfer. 4847 */ 4848 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 4849 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4850 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 4851 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4852 buf = io->scsiio.kern_data_ptr; 4853 else 4854 buf = NULL; 4855 ctl_done(io); 4856 if (buf) 4857 free(buf, M_CTL); 4858 return; 4859 } 4860 4861 /* 4862 * If the IO_CONT flag is set, we need to call the supplied 4863 * function to continue processing the I/O, instead of completing 4864 * the I/O just yet. 4865 */ 4866 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 4867 io->scsiio.io_cont(io); 4868 return; 4869 } 4870 4871 ctl_datamove(io); 4872} 4873 4874/* 4875 * SCSI release command. 4876 */ 4877int 4878ctl_scsi_release(struct ctl_scsiio *ctsio) 4879{ 4880 int length, longid, thirdparty_id, resv_id; 4881 struct ctl_lun *lun; 4882 uint32_t residx; 4883 4884 length = 0; 4885 resv_id = 0; 4886 4887 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 4888 4889 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 4890 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 4891 4892 switch (ctsio->cdb[0]) { 4893 case RELEASE_10: { 4894 struct scsi_release_10 *cdb; 4895 4896 cdb = (struct scsi_release_10 *)ctsio->cdb; 4897 4898 if (cdb->byte2 & SR10_LONGID) 4899 longid = 1; 4900 else 4901 thirdparty_id = cdb->thirdparty_id; 4902 4903 resv_id = cdb->resv_id; 4904 length = scsi_2btoul(cdb->length); 4905 break; 4906 } 4907 } 4908 4909 4910 /* 4911 * XXX KDM right now, we only support LUN reservation. We don't 4912 * support 3rd party reservations, or extent reservations, which 4913 * might actually need the parameter list. If we've gotten this 4914 * far, we've got a LUN reservation. Anything else got kicked out 4915 * above. So, according to SPC, ignore the length. 4916 */ 4917 length = 0; 4918 4919 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 4920 && (length > 0)) { 4921 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 4922 ctsio->kern_data_len = length; 4923 ctsio->kern_total_len = length; 4924 ctsio->kern_data_resid = 0; 4925 ctsio->kern_rel_offset = 0; 4926 ctsio->kern_sg_entries = 0; 4927 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 4928 ctsio->be_move_done = ctl_config_move_done; 4929 ctl_datamove((union ctl_io *)ctsio); 4930 4931 return (CTL_RETVAL_COMPLETE); 4932 } 4933 4934 if (length > 0) 4935 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 4936 4937 mtx_lock(&lun->lun_lock); 4938 4939 /* 4940 * According to SPC, it is not an error for an intiator to attempt 4941 * to release a reservation on a LUN that isn't reserved, or that 4942 * is reserved by another initiator. The reservation can only be 4943 * released, though, by the initiator who made it or by one of 4944 * several reset type events. 4945 */ 4946 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 4947 lun->flags &= ~CTL_LUN_RESERVED; 4948 4949 mtx_unlock(&lun->lun_lock); 4950 4951 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 4952 free(ctsio->kern_data_ptr, M_CTL); 4953 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 4954 } 4955 4956 ctl_set_success(ctsio); 4957 ctl_done((union ctl_io *)ctsio); 4958 return (CTL_RETVAL_COMPLETE); 4959} 4960 4961int 4962ctl_scsi_reserve(struct ctl_scsiio *ctsio) 4963{ 4964 int extent, thirdparty, longid; 4965 int resv_id, length; 4966 uint64_t thirdparty_id; 4967 struct ctl_lun *lun; 4968 uint32_t residx; 4969 4970 extent = 0; 4971 thirdparty = 0; 4972 longid = 0; 4973 resv_id = 0; 4974 length = 0; 4975 thirdparty_id = 0; 4976 4977 CTL_DEBUG_PRINT(("ctl_reserve\n")); 4978 4979 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 4980 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 4981 4982 switch (ctsio->cdb[0]) { 4983 case RESERVE_10: { 4984 struct scsi_reserve_10 *cdb; 4985 4986 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 4987 4988 if (cdb->byte2 & SR10_LONGID) 4989 longid = 1; 4990 else 4991 thirdparty_id = cdb->thirdparty_id; 4992 4993 resv_id = cdb->resv_id; 4994 length = scsi_2btoul(cdb->length); 4995 break; 4996 } 4997 } 4998 4999 /* 5000 * XXX KDM right now, we only support LUN reservation. We don't 5001 * support 3rd party reservations, or extent reservations, which 5002 * might actually need the parameter list. If we've gotten this 5003 * far, we've got a LUN reservation. Anything else got kicked out 5004 * above. So, according to SPC, ignore the length. 5005 */ 5006 length = 0; 5007 5008 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5009 && (length > 0)) { 5010 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5011 ctsio->kern_data_len = length; 5012 ctsio->kern_total_len = length; 5013 ctsio->kern_data_resid = 0; 5014 ctsio->kern_rel_offset = 0; 5015 ctsio->kern_sg_entries = 0; 5016 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5017 ctsio->be_move_done = ctl_config_move_done; 5018 ctl_datamove((union ctl_io *)ctsio); 5019 5020 return (CTL_RETVAL_COMPLETE); 5021 } 5022 5023 if (length > 0) 5024 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5025 5026 mtx_lock(&lun->lun_lock); 5027 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5028 ctl_set_reservation_conflict(ctsio); 5029 goto bailout; 5030 } 5031 5032 lun->flags |= CTL_LUN_RESERVED; 5033 lun->res_idx = residx; 5034 5035 ctl_set_success(ctsio); 5036 5037bailout: 5038 mtx_unlock(&lun->lun_lock); 5039 5040 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5041 free(ctsio->kern_data_ptr, M_CTL); 5042 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5043 } 5044 5045 ctl_done((union ctl_io *)ctsio); 5046 return (CTL_RETVAL_COMPLETE); 5047} 5048 5049int 5050ctl_start_stop(struct ctl_scsiio *ctsio) 5051{ 5052 struct scsi_start_stop_unit *cdb; 5053 struct ctl_lun *lun; 5054 int retval; 5055 5056 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5057 5058 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5059 retval = 0; 5060 5061 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5062 5063 /* 5064 * XXX KDM 5065 * We don't support the immediate bit on a stop unit. In order to 5066 * do that, we would need to code up a way to know that a stop is 5067 * pending, and hold off any new commands until it completes, one 5068 * way or another. Then we could accept or reject those commands 5069 * depending on its status. We would almost need to do the reverse 5070 * of what we do below for an immediate start -- return the copy of 5071 * the ctl_io to the FETD with status to send to the host (and to 5072 * free the copy!) and then free the original I/O once the stop 5073 * actually completes. That way, the OOA queue mechanism can work 5074 * to block commands that shouldn't proceed. Another alternative 5075 * would be to put the copy in the queue in place of the original, 5076 * and return the original back to the caller. That could be 5077 * slightly safer.. 5078 */ 5079 if ((cdb->byte2 & SSS_IMMED) 5080 && ((cdb->how & SSS_START) == 0)) { 5081 ctl_set_invalid_field(ctsio, 5082 /*sks_valid*/ 1, 5083 /*command*/ 1, 5084 /*field*/ 1, 5085 /*bit_valid*/ 1, 5086 /*bit*/ 0); 5087 ctl_done((union ctl_io *)ctsio); 5088 return (CTL_RETVAL_COMPLETE); 5089 } 5090 5091 if ((lun->flags & CTL_LUN_PR_RESERVED) 5092 && ((cdb->how & SSS_START)==0)) { 5093 uint32_t residx; 5094 5095 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5096 if (ctl_get_prkey(lun, residx) == 0 5097 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5098 5099 ctl_set_reservation_conflict(ctsio); 5100 ctl_done((union ctl_io *)ctsio); 5101 return (CTL_RETVAL_COMPLETE); 5102 } 5103 } 5104 5105 /* 5106 * If there is no backend on this device, we can't start or stop 5107 * it. In theory we shouldn't get any start/stop commands in the 5108 * first place at this level if the LUN doesn't have a backend. 5109 * That should get stopped by the command decode code. 5110 */ 5111 if (lun->backend == NULL) { 5112 ctl_set_invalid_opcode(ctsio); 5113 ctl_done((union ctl_io *)ctsio); 5114 return (CTL_RETVAL_COMPLETE); 5115 } 5116 5117 /* 5118 * XXX KDM Copan-specific offline behavior. 5119 * Figure out a reasonable way to port this? 5120 */ 5121#ifdef NEEDTOPORT 5122 mtx_lock(&lun->lun_lock); 5123 5124 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5125 && (lun->flags & CTL_LUN_OFFLINE)) { 5126 /* 5127 * If the LUN is offline, and the on/offline bit isn't set, 5128 * reject the start or stop. Otherwise, let it through. 5129 */ 5130 mtx_unlock(&lun->lun_lock); 5131 ctl_set_lun_not_ready(ctsio); 5132 ctl_done((union ctl_io *)ctsio); 5133 } else { 5134 mtx_unlock(&lun->lun_lock); 5135#endif /* NEEDTOPORT */ 5136 /* 5137 * This could be a start or a stop when we're online, 5138 * or a stop/offline or start/online. A start or stop when 5139 * we're offline is covered in the case above. 5140 */ 5141 /* 5142 * In the non-immediate case, we send the request to 5143 * the backend and return status to the user when 5144 * it is done. 5145 * 5146 * In the immediate case, we allocate a new ctl_io 5147 * to hold a copy of the request, and send that to 5148 * the backend. We then set good status on the 5149 * user's request and return it immediately. 5150 */ 5151 if (cdb->byte2 & SSS_IMMED) { 5152 union ctl_io *new_io; 5153 5154 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5155 ctl_copy_io((union ctl_io *)ctsio, new_io); 5156 retval = lun->backend->config_write(new_io); 5157 ctl_set_success(ctsio); 5158 ctl_done((union ctl_io *)ctsio); 5159 } else { 5160 retval = lun->backend->config_write( 5161 (union ctl_io *)ctsio); 5162 } 5163#ifdef NEEDTOPORT 5164 } 5165#endif 5166 return (retval); 5167} 5168 5169/* 5170 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5171 * we don't really do anything with the LBA and length fields if the user 5172 * passes them in. Instead we'll just flush out the cache for the entire 5173 * LUN. 5174 */ 5175int 5176ctl_sync_cache(struct ctl_scsiio *ctsio) 5177{ 5178 struct ctl_lun *lun; 5179 struct ctl_softc *softc; 5180 struct ctl_lba_len_flags *lbalen; 5181 uint64_t starting_lba; 5182 uint32_t block_count; 5183 int retval; 5184 uint8_t byte2; 5185 5186 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5187 5188 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5189 softc = lun->ctl_softc; 5190 retval = 0; 5191 5192 switch (ctsio->cdb[0]) { 5193 case SYNCHRONIZE_CACHE: { 5194 struct scsi_sync_cache *cdb; 5195 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5196 5197 starting_lba = scsi_4btoul(cdb->begin_lba); 5198 block_count = scsi_2btoul(cdb->lb_count); 5199 byte2 = cdb->byte2; 5200 break; 5201 } 5202 case SYNCHRONIZE_CACHE_16: { 5203 struct scsi_sync_cache_16 *cdb; 5204 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5205 5206 starting_lba = scsi_8btou64(cdb->begin_lba); 5207 block_count = scsi_4btoul(cdb->lb_count); 5208 byte2 = cdb->byte2; 5209 break; 5210 } 5211 default: 5212 ctl_set_invalid_opcode(ctsio); 5213 ctl_done((union ctl_io *)ctsio); 5214 goto bailout; 5215 break; /* NOTREACHED */ 5216 } 5217 5218 /* 5219 * We check the LBA and length, but don't do anything with them. 5220 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5221 * get flushed. This check will just help satisfy anyone who wants 5222 * to see an error for an out of range LBA. 5223 */ 5224 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5225 ctl_set_lba_out_of_range(ctsio); 5226 ctl_done((union ctl_io *)ctsio); 5227 goto bailout; 5228 } 5229 5230 /* 5231 * If this LUN has no backend, we can't flush the cache anyway. 5232 */ 5233 if (lun->backend == NULL) { 5234 ctl_set_invalid_opcode(ctsio); 5235 ctl_done((union ctl_io *)ctsio); 5236 goto bailout; 5237 } 5238 5239 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5240 lbalen->lba = starting_lba; 5241 lbalen->len = block_count; 5242 lbalen->flags = byte2; 5243 5244 /* 5245 * Check to see whether we're configured to send the SYNCHRONIZE 5246 * CACHE command directly to the back end. 5247 */ 5248 mtx_lock(&lun->lun_lock); 5249 if ((softc->flags & CTL_FLAG_REAL_SYNC) 5250 && (++(lun->sync_count) >= lun->sync_interval)) { 5251 lun->sync_count = 0; 5252 mtx_unlock(&lun->lun_lock); 5253 retval = lun->backend->config_write((union ctl_io *)ctsio); 5254 } else { 5255 mtx_unlock(&lun->lun_lock); 5256 ctl_set_success(ctsio); 5257 ctl_done((union ctl_io *)ctsio); 5258 } 5259 5260bailout: 5261 5262 return (retval); 5263} 5264 5265int 5266ctl_format(struct ctl_scsiio *ctsio) 5267{ 5268 struct scsi_format *cdb; 5269 struct ctl_lun *lun; 5270 int length, defect_list_len; 5271 5272 CTL_DEBUG_PRINT(("ctl_format\n")); 5273 5274 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5275 5276 cdb = (struct scsi_format *)ctsio->cdb; 5277 5278 length = 0; 5279 if (cdb->byte2 & SF_FMTDATA) { 5280 if (cdb->byte2 & SF_LONGLIST) 5281 length = sizeof(struct scsi_format_header_long); 5282 else 5283 length = sizeof(struct scsi_format_header_short); 5284 } 5285 5286 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5287 && (length > 0)) { 5288 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5289 ctsio->kern_data_len = length; 5290 ctsio->kern_total_len = length; 5291 ctsio->kern_data_resid = 0; 5292 ctsio->kern_rel_offset = 0; 5293 ctsio->kern_sg_entries = 0; 5294 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5295 ctsio->be_move_done = ctl_config_move_done; 5296 ctl_datamove((union ctl_io *)ctsio); 5297 5298 return (CTL_RETVAL_COMPLETE); 5299 } 5300 5301 defect_list_len = 0; 5302 5303 if (cdb->byte2 & SF_FMTDATA) { 5304 if (cdb->byte2 & SF_LONGLIST) { 5305 struct scsi_format_header_long *header; 5306 5307 header = (struct scsi_format_header_long *) 5308 ctsio->kern_data_ptr; 5309 5310 defect_list_len = scsi_4btoul(header->defect_list_len); 5311 if (defect_list_len != 0) { 5312 ctl_set_invalid_field(ctsio, 5313 /*sks_valid*/ 1, 5314 /*command*/ 0, 5315 /*field*/ 2, 5316 /*bit_valid*/ 0, 5317 /*bit*/ 0); 5318 goto bailout; 5319 } 5320 } else { 5321 struct scsi_format_header_short *header; 5322 5323 header = (struct scsi_format_header_short *) 5324 ctsio->kern_data_ptr; 5325 5326 defect_list_len = scsi_2btoul(header->defect_list_len); 5327 if (defect_list_len != 0) { 5328 ctl_set_invalid_field(ctsio, 5329 /*sks_valid*/ 1, 5330 /*command*/ 0, 5331 /*field*/ 2, 5332 /*bit_valid*/ 0, 5333 /*bit*/ 0); 5334 goto bailout; 5335 } 5336 } 5337 } 5338 5339 /* 5340 * The format command will clear out the "Medium format corrupted" 5341 * status if set by the configuration code. That status is really 5342 * just a way to notify the host that we have lost the media, and 5343 * get them to issue a command that will basically make them think 5344 * they're blowing away the media. 5345 */ 5346 mtx_lock(&lun->lun_lock); 5347 lun->flags &= ~CTL_LUN_INOPERABLE; 5348 mtx_unlock(&lun->lun_lock); 5349 5350 ctl_set_success(ctsio); 5351bailout: 5352 5353 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5354 free(ctsio->kern_data_ptr, M_CTL); 5355 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5356 } 5357 5358 ctl_done((union ctl_io *)ctsio); 5359 return (CTL_RETVAL_COMPLETE); 5360} 5361 5362int 5363ctl_read_buffer(struct ctl_scsiio *ctsio) 5364{ 5365 struct scsi_read_buffer *cdb; 5366 struct ctl_lun *lun; 5367 int buffer_offset, len; 5368 static uint8_t descr[4]; 5369 static uint8_t echo_descr[4] = { 0 }; 5370 5371 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5372 5373 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5374 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5375 5376 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5377 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5378 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5379 ctl_set_invalid_field(ctsio, 5380 /*sks_valid*/ 1, 5381 /*command*/ 1, 5382 /*field*/ 1, 5383 /*bit_valid*/ 1, 5384 /*bit*/ 4); 5385 ctl_done((union ctl_io *)ctsio); 5386 return (CTL_RETVAL_COMPLETE); 5387 } 5388 5389 len = scsi_3btoul(cdb->length); 5390 buffer_offset = scsi_3btoul(cdb->offset); 5391 5392 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5393 ctl_set_invalid_field(ctsio, 5394 /*sks_valid*/ 1, 5395 /*command*/ 1, 5396 /*field*/ 6, 5397 /*bit_valid*/ 0, 5398 /*bit*/ 0); 5399 ctl_done((union ctl_io *)ctsio); 5400 return (CTL_RETVAL_COMPLETE); 5401 } 5402 5403 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5404 descr[0] = 0; 5405 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5406 ctsio->kern_data_ptr = descr; 5407 len = min(len, sizeof(descr)); 5408 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5409 ctsio->kern_data_ptr = echo_descr; 5410 len = min(len, sizeof(echo_descr)); 5411 } else { 5412 if (lun->write_buffer == NULL) { 5413 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5414 M_CTL, M_WAITOK); 5415 } 5416 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5417 } 5418 ctsio->kern_data_len = len; 5419 ctsio->kern_total_len = len; 5420 ctsio->kern_data_resid = 0; 5421 ctsio->kern_rel_offset = 0; 5422 ctsio->kern_sg_entries = 0; 5423 ctl_set_success(ctsio); 5424 ctsio->be_move_done = ctl_config_move_done; 5425 ctl_datamove((union ctl_io *)ctsio); 5426 return (CTL_RETVAL_COMPLETE); 5427} 5428 5429int 5430ctl_write_buffer(struct ctl_scsiio *ctsio) 5431{ 5432 struct scsi_write_buffer *cdb; 5433 struct ctl_lun *lun; 5434 int buffer_offset, len; 5435 5436 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5437 5438 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5439 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5440 5441 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5442 ctl_set_invalid_field(ctsio, 5443 /*sks_valid*/ 1, 5444 /*command*/ 1, 5445 /*field*/ 1, 5446 /*bit_valid*/ 1, 5447 /*bit*/ 4); 5448 ctl_done((union ctl_io *)ctsio); 5449 return (CTL_RETVAL_COMPLETE); 5450 } 5451 5452 len = scsi_3btoul(cdb->length); 5453 buffer_offset = scsi_3btoul(cdb->offset); 5454 5455 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5456 ctl_set_invalid_field(ctsio, 5457 /*sks_valid*/ 1, 5458 /*command*/ 1, 5459 /*field*/ 6, 5460 /*bit_valid*/ 0, 5461 /*bit*/ 0); 5462 ctl_done((union ctl_io *)ctsio); 5463 return (CTL_RETVAL_COMPLETE); 5464 } 5465 5466 /* 5467 * If we've got a kernel request that hasn't been malloced yet, 5468 * malloc it and tell the caller the data buffer is here. 5469 */ 5470 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5471 if (lun->write_buffer == NULL) { 5472 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5473 M_CTL, M_WAITOK); 5474 } 5475 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5476 ctsio->kern_data_len = len; 5477 ctsio->kern_total_len = len; 5478 ctsio->kern_data_resid = 0; 5479 ctsio->kern_rel_offset = 0; 5480 ctsio->kern_sg_entries = 0; 5481 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5482 ctsio->be_move_done = ctl_config_move_done; 5483 ctl_datamove((union ctl_io *)ctsio); 5484 5485 return (CTL_RETVAL_COMPLETE); 5486 } 5487 5488 ctl_set_success(ctsio); 5489 ctl_done((union ctl_io *)ctsio); 5490 return (CTL_RETVAL_COMPLETE); 5491} 5492 5493int 5494ctl_write_same(struct ctl_scsiio *ctsio) 5495{ 5496 struct ctl_lun *lun; 5497 struct ctl_lba_len_flags *lbalen; 5498 uint64_t lba; 5499 uint32_t num_blocks; 5500 int len, retval; 5501 uint8_t byte2; 5502 5503 retval = CTL_RETVAL_COMPLETE; 5504 5505 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5506 5507 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5508 5509 switch (ctsio->cdb[0]) { 5510 case WRITE_SAME_10: { 5511 struct scsi_write_same_10 *cdb; 5512 5513 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5514 5515 lba = scsi_4btoul(cdb->addr); 5516 num_blocks = scsi_2btoul(cdb->length); 5517 byte2 = cdb->byte2; 5518 break; 5519 } 5520 case WRITE_SAME_16: { 5521 struct scsi_write_same_16 *cdb; 5522 5523 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5524 5525 lba = scsi_8btou64(cdb->addr); 5526 num_blocks = scsi_4btoul(cdb->length); 5527 byte2 = cdb->byte2; 5528 break; 5529 } 5530 default: 5531 /* 5532 * We got a command we don't support. This shouldn't 5533 * happen, commands should be filtered out above us. 5534 */ 5535 ctl_set_invalid_opcode(ctsio); 5536 ctl_done((union ctl_io *)ctsio); 5537 5538 return (CTL_RETVAL_COMPLETE); 5539 break; /* NOTREACHED */ 5540 } 5541 5542 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5543 if ((byte2 & SWS_UNMAP) == 0 && 5544 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5545 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5546 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5547 ctl_done((union ctl_io *)ctsio); 5548 return (CTL_RETVAL_COMPLETE); 5549 } 5550 5551 /* 5552 * The first check is to make sure we're in bounds, the second 5553 * check is to catch wrap-around problems. If the lba + num blocks 5554 * is less than the lba, then we've wrapped around and the block 5555 * range is invalid anyway. 5556 */ 5557 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5558 || ((lba + num_blocks) < lba)) { 5559 ctl_set_lba_out_of_range(ctsio); 5560 ctl_done((union ctl_io *)ctsio); 5561 return (CTL_RETVAL_COMPLETE); 5562 } 5563 5564 /* Zero number of blocks means "to the last logical block" */ 5565 if (num_blocks == 0) { 5566 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5567 ctl_set_invalid_field(ctsio, 5568 /*sks_valid*/ 0, 5569 /*command*/ 1, 5570 /*field*/ 0, 5571 /*bit_valid*/ 0, 5572 /*bit*/ 0); 5573 ctl_done((union ctl_io *)ctsio); 5574 return (CTL_RETVAL_COMPLETE); 5575 } 5576 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5577 } 5578 5579 len = lun->be_lun->blocksize; 5580 5581 /* 5582 * If we've got a kernel request that hasn't been malloced yet, 5583 * malloc it and tell the caller the data buffer is here. 5584 */ 5585 if ((byte2 & SWS_NDOB) == 0 && 5586 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5587 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5588 ctsio->kern_data_len = len; 5589 ctsio->kern_total_len = len; 5590 ctsio->kern_data_resid = 0; 5591 ctsio->kern_rel_offset = 0; 5592 ctsio->kern_sg_entries = 0; 5593 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5594 ctsio->be_move_done = ctl_config_move_done; 5595 ctl_datamove((union ctl_io *)ctsio); 5596 5597 return (CTL_RETVAL_COMPLETE); 5598 } 5599 5600 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5601 lbalen->lba = lba; 5602 lbalen->len = num_blocks; 5603 lbalen->flags = byte2; 5604 retval = lun->backend->config_write((union ctl_io *)ctsio); 5605 5606 return (retval); 5607} 5608 5609int 5610ctl_unmap(struct ctl_scsiio *ctsio) 5611{ 5612 struct ctl_lun *lun; 5613 struct scsi_unmap *cdb; 5614 struct ctl_ptr_len_flags *ptrlen; 5615 struct scsi_unmap_header *hdr; 5616 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5617 uint64_t lba; 5618 uint32_t num_blocks; 5619 int len, retval; 5620 uint8_t byte2; 5621 5622 retval = CTL_RETVAL_COMPLETE; 5623 5624 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5625 5626 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5627 cdb = (struct scsi_unmap *)ctsio->cdb; 5628 5629 len = scsi_2btoul(cdb->length); 5630 byte2 = cdb->byte2; 5631 5632 /* 5633 * If we've got a kernel request that hasn't been malloced yet, 5634 * malloc it and tell the caller the data buffer is here. 5635 */ 5636 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5637 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5638 ctsio->kern_data_len = len; 5639 ctsio->kern_total_len = len; 5640 ctsio->kern_data_resid = 0; 5641 ctsio->kern_rel_offset = 0; 5642 ctsio->kern_sg_entries = 0; 5643 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5644 ctsio->be_move_done = ctl_config_move_done; 5645 ctl_datamove((union ctl_io *)ctsio); 5646 5647 return (CTL_RETVAL_COMPLETE); 5648 } 5649 5650 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5651 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5652 if (len < sizeof (*hdr) || 5653 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5654 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5655 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5656 ctl_set_invalid_field(ctsio, 5657 /*sks_valid*/ 0, 5658 /*command*/ 0, 5659 /*field*/ 0, 5660 /*bit_valid*/ 0, 5661 /*bit*/ 0); 5662 goto done; 5663 } 5664 len = scsi_2btoul(hdr->desc_length); 5665 buf = (struct scsi_unmap_desc *)(hdr + 1); 5666 end = buf + len / sizeof(*buf); 5667 5668 endnz = buf; 5669 for (range = buf; range < end; range++) { 5670 lba = scsi_8btou64(range->lba); 5671 num_blocks = scsi_4btoul(range->length); 5672 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5673 || ((lba + num_blocks) < lba)) { 5674 ctl_set_lba_out_of_range(ctsio); 5675 ctl_done((union ctl_io *)ctsio); 5676 return (CTL_RETVAL_COMPLETE); 5677 } 5678 if (num_blocks != 0) 5679 endnz = range + 1; 5680 } 5681 5682 /* 5683 * Block backend can not handle zero last range. 5684 * Filter it out and return if there is nothing left. 5685 */ 5686 len = (uint8_t *)endnz - (uint8_t *)buf; 5687 if (len == 0) { 5688 ctl_set_success(ctsio); 5689 goto done; 5690 } 5691 5692 mtx_lock(&lun->lun_lock); 5693 ptrlen = (struct ctl_ptr_len_flags *) 5694 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5695 ptrlen->ptr = (void *)buf; 5696 ptrlen->len = len; 5697 ptrlen->flags = byte2; 5698 ctl_check_blocked(lun); 5699 mtx_unlock(&lun->lun_lock); 5700 5701 retval = lun->backend->config_write((union ctl_io *)ctsio); 5702 return (retval); 5703 5704done: 5705 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5706 free(ctsio->kern_data_ptr, M_CTL); 5707 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5708 } 5709 ctl_done((union ctl_io *)ctsio); 5710 return (CTL_RETVAL_COMPLETE); 5711} 5712 5713/* 5714 * Note that this function currently doesn't actually do anything inside 5715 * CTL to enforce things if the DQue bit is turned on. 5716 * 5717 * Also note that this function can't be used in the default case, because 5718 * the DQue bit isn't set in the changeable mask for the control mode page 5719 * anyway. This is just here as an example for how to implement a page 5720 * handler, and a placeholder in case we want to allow the user to turn 5721 * tagged queueing on and off. 5722 * 5723 * The D_SENSE bit handling is functional, however, and will turn 5724 * descriptor sense on and off for a given LUN. 5725 */ 5726int 5727ctl_control_page_handler(struct ctl_scsiio *ctsio, 5728 struct ctl_page_index *page_index, uint8_t *page_ptr) 5729{ 5730 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5731 struct ctl_lun *lun; 5732 int set_ua; 5733 uint32_t initidx; 5734 5735 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5736 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5737 set_ua = 0; 5738 5739 user_cp = (struct scsi_control_page *)page_ptr; 5740 current_cp = (struct scsi_control_page *) 5741 (page_index->page_data + (page_index->page_len * 5742 CTL_PAGE_CURRENT)); 5743 saved_cp = (struct scsi_control_page *) 5744 (page_index->page_data + (page_index->page_len * 5745 CTL_PAGE_SAVED)); 5746 5747 mtx_lock(&lun->lun_lock); 5748 if (((current_cp->rlec & SCP_DSENSE) == 0) 5749 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5750 /* 5751 * Descriptor sense is currently turned off and the user 5752 * wants to turn it on. 5753 */ 5754 current_cp->rlec |= SCP_DSENSE; 5755 saved_cp->rlec |= SCP_DSENSE; 5756 lun->flags |= CTL_LUN_SENSE_DESC; 5757 set_ua = 1; 5758 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 5759 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 5760 /* 5761 * Descriptor sense is currently turned on, and the user 5762 * wants to turn it off. 5763 */ 5764 current_cp->rlec &= ~SCP_DSENSE; 5765 saved_cp->rlec &= ~SCP_DSENSE; 5766 lun->flags &= ~CTL_LUN_SENSE_DESC; 5767 set_ua = 1; 5768 } 5769 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 5770 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 5771 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5772 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5773 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5774 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5775 set_ua = 1; 5776 } 5777 if ((current_cp->eca_and_aen & SCP_SWP) != 5778 (user_cp->eca_and_aen & SCP_SWP)) { 5779 current_cp->eca_and_aen &= ~SCP_SWP; 5780 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5781 saved_cp->eca_and_aen &= ~SCP_SWP; 5782 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5783 set_ua = 1; 5784 } 5785 if (set_ua != 0) 5786 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5787 mtx_unlock(&lun->lun_lock); 5788 5789 return (0); 5790} 5791 5792int 5793ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 5794 struct ctl_page_index *page_index, uint8_t *page_ptr) 5795{ 5796 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 5797 struct ctl_lun *lun; 5798 int set_ua; 5799 uint32_t initidx; 5800 5801 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5802 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5803 set_ua = 0; 5804 5805 user_cp = (struct scsi_caching_page *)page_ptr; 5806 current_cp = (struct scsi_caching_page *) 5807 (page_index->page_data + (page_index->page_len * 5808 CTL_PAGE_CURRENT)); 5809 saved_cp = (struct scsi_caching_page *) 5810 (page_index->page_data + (page_index->page_len * 5811 CTL_PAGE_SAVED)); 5812 5813 mtx_lock(&lun->lun_lock); 5814 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 5815 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 5816 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5817 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5818 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5819 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5820 set_ua = 1; 5821 } 5822 if (set_ua != 0) 5823 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5824 mtx_unlock(&lun->lun_lock); 5825 5826 return (0); 5827} 5828 5829int 5830ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 5831 struct ctl_page_index *page_index, 5832 uint8_t *page_ptr) 5833{ 5834 uint8_t *c; 5835 int i; 5836 5837 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 5838 ctl_time_io_secs = 5839 (c[0] << 8) | 5840 (c[1] << 0) | 5841 0; 5842 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 5843 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 5844 printf("page data:"); 5845 for (i=0; i<8; i++) 5846 printf(" %.2x",page_ptr[i]); 5847 printf("\n"); 5848 return (0); 5849} 5850 5851int 5852ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 5853 struct ctl_page_index *page_index, 5854 int pc) 5855{ 5856 struct copan_debugconf_subpage *page; 5857 5858 page = (struct copan_debugconf_subpage *)page_index->page_data + 5859 (page_index->page_len * pc); 5860 5861 switch (pc) { 5862 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 5863 case SMS_PAGE_CTRL_DEFAULT >> 6: 5864 case SMS_PAGE_CTRL_SAVED >> 6: 5865 /* 5866 * We don't update the changable or default bits for this page. 5867 */ 5868 break; 5869 case SMS_PAGE_CTRL_CURRENT >> 6: 5870 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 5871 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 5872 break; 5873 default: 5874#ifdef NEEDTOPORT 5875 EPRINT(0, "Invalid PC %d!!", pc); 5876#endif /* NEEDTOPORT */ 5877 break; 5878 } 5879 return (0); 5880} 5881 5882 5883static int 5884ctl_do_mode_select(union ctl_io *io) 5885{ 5886 struct scsi_mode_page_header *page_header; 5887 struct ctl_page_index *page_index; 5888 struct ctl_scsiio *ctsio; 5889 int control_dev, page_len; 5890 int page_len_offset, page_len_size; 5891 union ctl_modepage_info *modepage_info; 5892 struct ctl_lun *lun; 5893 int *len_left, *len_used; 5894 int retval, i; 5895 5896 ctsio = &io->scsiio; 5897 page_index = NULL; 5898 page_len = 0; 5899 retval = CTL_RETVAL_COMPLETE; 5900 5901 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5902 5903 if (lun->be_lun->lun_type != T_DIRECT) 5904 control_dev = 1; 5905 else 5906 control_dev = 0; 5907 5908 modepage_info = (union ctl_modepage_info *) 5909 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5910 len_left = &modepage_info->header.len_left; 5911 len_used = &modepage_info->header.len_used; 5912 5913do_next_page: 5914 5915 page_header = (struct scsi_mode_page_header *) 5916 (ctsio->kern_data_ptr + *len_used); 5917 5918 if (*len_left == 0) { 5919 free(ctsio->kern_data_ptr, M_CTL); 5920 ctl_set_success(ctsio); 5921 ctl_done((union ctl_io *)ctsio); 5922 return (CTL_RETVAL_COMPLETE); 5923 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5924 5925 free(ctsio->kern_data_ptr, M_CTL); 5926 ctl_set_param_len_error(ctsio); 5927 ctl_done((union ctl_io *)ctsio); 5928 return (CTL_RETVAL_COMPLETE); 5929 5930 } else if ((page_header->page_code & SMPH_SPF) 5931 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5932 5933 free(ctsio->kern_data_ptr, M_CTL); 5934 ctl_set_param_len_error(ctsio); 5935 ctl_done((union ctl_io *)ctsio); 5936 return (CTL_RETVAL_COMPLETE); 5937 } 5938 5939 5940 /* 5941 * XXX KDM should we do something with the block descriptor? 5942 */ 5943 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5944 5945 if ((control_dev != 0) 5946 && (lun->mode_pages.index[i].page_flags & 5947 CTL_PAGE_FLAG_DISK_ONLY)) 5948 continue; 5949 5950 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5951 (page_header->page_code & SMPH_PC_MASK)) 5952 continue; 5953 5954 /* 5955 * If neither page has a subpage code, then we've got a 5956 * match. 5957 */ 5958 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 5959 && ((page_header->page_code & SMPH_SPF) == 0)) { 5960 page_index = &lun->mode_pages.index[i]; 5961 page_len = page_header->page_length; 5962 break; 5963 } 5964 5965 /* 5966 * If both pages have subpages, then the subpage numbers 5967 * have to match. 5968 */ 5969 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 5970 && (page_header->page_code & SMPH_SPF)) { 5971 struct scsi_mode_page_header_sp *sph; 5972 5973 sph = (struct scsi_mode_page_header_sp *)page_header; 5974 5975 if (lun->mode_pages.index[i].subpage == 5976 sph->subpage) { 5977 page_index = &lun->mode_pages.index[i]; 5978 page_len = scsi_2btoul(sph->page_length); 5979 break; 5980 } 5981 } 5982 } 5983 5984 /* 5985 * If we couldn't find the page, or if we don't have a mode select 5986 * handler for it, send back an error to the user. 5987 */ 5988 if ((page_index == NULL) 5989 || (page_index->select_handler == NULL)) { 5990 ctl_set_invalid_field(ctsio, 5991 /*sks_valid*/ 1, 5992 /*command*/ 0, 5993 /*field*/ *len_used, 5994 /*bit_valid*/ 0, 5995 /*bit*/ 0); 5996 free(ctsio->kern_data_ptr, M_CTL); 5997 ctl_done((union ctl_io *)ctsio); 5998 return (CTL_RETVAL_COMPLETE); 5999 } 6000 6001 if (page_index->page_code & SMPH_SPF) { 6002 page_len_offset = 2; 6003 page_len_size = 2; 6004 } else { 6005 page_len_size = 1; 6006 page_len_offset = 1; 6007 } 6008 6009 /* 6010 * If the length the initiator gives us isn't the one we specify in 6011 * the mode page header, or if they didn't specify enough data in 6012 * the CDB to avoid truncating this page, kick out the request. 6013 */ 6014 if ((page_len != (page_index->page_len - page_len_offset - 6015 page_len_size)) 6016 || (*len_left < page_index->page_len)) { 6017 6018 6019 ctl_set_invalid_field(ctsio, 6020 /*sks_valid*/ 1, 6021 /*command*/ 0, 6022 /*field*/ *len_used + page_len_offset, 6023 /*bit_valid*/ 0, 6024 /*bit*/ 0); 6025 free(ctsio->kern_data_ptr, M_CTL); 6026 ctl_done((union ctl_io *)ctsio); 6027 return (CTL_RETVAL_COMPLETE); 6028 } 6029 6030 /* 6031 * Run through the mode page, checking to make sure that the bits 6032 * the user changed are actually legal for him to change. 6033 */ 6034 for (i = 0; i < page_index->page_len; i++) { 6035 uint8_t *user_byte, *change_mask, *current_byte; 6036 int bad_bit; 6037 int j; 6038 6039 user_byte = (uint8_t *)page_header + i; 6040 change_mask = page_index->page_data + 6041 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6042 current_byte = page_index->page_data + 6043 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6044 6045 /* 6046 * Check to see whether the user set any bits in this byte 6047 * that he is not allowed to set. 6048 */ 6049 if ((*user_byte & ~(*change_mask)) == 6050 (*current_byte & ~(*change_mask))) 6051 continue; 6052 6053 /* 6054 * Go through bit by bit to determine which one is illegal. 6055 */ 6056 bad_bit = 0; 6057 for (j = 7; j >= 0; j--) { 6058 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6059 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6060 bad_bit = i; 6061 break; 6062 } 6063 } 6064 ctl_set_invalid_field(ctsio, 6065 /*sks_valid*/ 1, 6066 /*command*/ 0, 6067 /*field*/ *len_used + i, 6068 /*bit_valid*/ 1, 6069 /*bit*/ bad_bit); 6070 free(ctsio->kern_data_ptr, M_CTL); 6071 ctl_done((union ctl_io *)ctsio); 6072 return (CTL_RETVAL_COMPLETE); 6073 } 6074 6075 /* 6076 * Decrement these before we call the page handler, since we may 6077 * end up getting called back one way or another before the handler 6078 * returns to this context. 6079 */ 6080 *len_left -= page_index->page_len; 6081 *len_used += page_index->page_len; 6082 6083 retval = page_index->select_handler(ctsio, page_index, 6084 (uint8_t *)page_header); 6085 6086 /* 6087 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6088 * wait until this queued command completes to finish processing 6089 * the mode page. If it returns anything other than 6090 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6091 * already set the sense information, freed the data pointer, and 6092 * completed the io for us. 6093 */ 6094 if (retval != CTL_RETVAL_COMPLETE) 6095 goto bailout_no_done; 6096 6097 /* 6098 * If the initiator sent us more than one page, parse the next one. 6099 */ 6100 if (*len_left > 0) 6101 goto do_next_page; 6102 6103 ctl_set_success(ctsio); 6104 free(ctsio->kern_data_ptr, M_CTL); 6105 ctl_done((union ctl_io *)ctsio); 6106 6107bailout_no_done: 6108 6109 return (CTL_RETVAL_COMPLETE); 6110 6111} 6112 6113int 6114ctl_mode_select(struct ctl_scsiio *ctsio) 6115{ 6116 int param_len, pf, sp; 6117 int header_size, bd_len; 6118 int len_left, len_used; 6119 struct ctl_page_index *page_index; 6120 struct ctl_lun *lun; 6121 int control_dev, page_len; 6122 union ctl_modepage_info *modepage_info; 6123 int retval; 6124 6125 pf = 0; 6126 sp = 0; 6127 page_len = 0; 6128 len_used = 0; 6129 len_left = 0; 6130 retval = 0; 6131 bd_len = 0; 6132 page_index = NULL; 6133 6134 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6135 6136 if (lun->be_lun->lun_type != T_DIRECT) 6137 control_dev = 1; 6138 else 6139 control_dev = 0; 6140 6141 switch (ctsio->cdb[0]) { 6142 case MODE_SELECT_6: { 6143 struct scsi_mode_select_6 *cdb; 6144 6145 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6146 6147 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6148 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6149 6150 param_len = cdb->length; 6151 header_size = sizeof(struct scsi_mode_header_6); 6152 break; 6153 } 6154 case MODE_SELECT_10: { 6155 struct scsi_mode_select_10 *cdb; 6156 6157 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6158 6159 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6160 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6161 6162 param_len = scsi_2btoul(cdb->length); 6163 header_size = sizeof(struct scsi_mode_header_10); 6164 break; 6165 } 6166 default: 6167 ctl_set_invalid_opcode(ctsio); 6168 ctl_done((union ctl_io *)ctsio); 6169 return (CTL_RETVAL_COMPLETE); 6170 break; /* NOTREACHED */ 6171 } 6172 6173 /* 6174 * From SPC-3: 6175 * "A parameter list length of zero indicates that the Data-Out Buffer 6176 * shall be empty. This condition shall not be considered as an error." 6177 */ 6178 if (param_len == 0) { 6179 ctl_set_success(ctsio); 6180 ctl_done((union ctl_io *)ctsio); 6181 return (CTL_RETVAL_COMPLETE); 6182 } 6183 6184 /* 6185 * Since we'll hit this the first time through, prior to 6186 * allocation, we don't need to free a data buffer here. 6187 */ 6188 if (param_len < header_size) { 6189 ctl_set_param_len_error(ctsio); 6190 ctl_done((union ctl_io *)ctsio); 6191 return (CTL_RETVAL_COMPLETE); 6192 } 6193 6194 /* 6195 * Allocate the data buffer and grab the user's data. In theory, 6196 * we shouldn't have to sanity check the parameter list length here 6197 * because the maximum size is 64K. We should be able to malloc 6198 * that much without too many problems. 6199 */ 6200 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6201 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6202 ctsio->kern_data_len = param_len; 6203 ctsio->kern_total_len = param_len; 6204 ctsio->kern_data_resid = 0; 6205 ctsio->kern_rel_offset = 0; 6206 ctsio->kern_sg_entries = 0; 6207 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6208 ctsio->be_move_done = ctl_config_move_done; 6209 ctl_datamove((union ctl_io *)ctsio); 6210 6211 return (CTL_RETVAL_COMPLETE); 6212 } 6213 6214 switch (ctsio->cdb[0]) { 6215 case MODE_SELECT_6: { 6216 struct scsi_mode_header_6 *mh6; 6217 6218 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6219 bd_len = mh6->blk_desc_len; 6220 break; 6221 } 6222 case MODE_SELECT_10: { 6223 struct scsi_mode_header_10 *mh10; 6224 6225 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6226 bd_len = scsi_2btoul(mh10->blk_desc_len); 6227 break; 6228 } 6229 default: 6230 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6231 break; 6232 } 6233 6234 if (param_len < (header_size + bd_len)) { 6235 free(ctsio->kern_data_ptr, M_CTL); 6236 ctl_set_param_len_error(ctsio); 6237 ctl_done((union ctl_io *)ctsio); 6238 return (CTL_RETVAL_COMPLETE); 6239 } 6240 6241 /* 6242 * Set the IO_CONT flag, so that if this I/O gets passed to 6243 * ctl_config_write_done(), it'll get passed back to 6244 * ctl_do_mode_select() for further processing, or completion if 6245 * we're all done. 6246 */ 6247 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6248 ctsio->io_cont = ctl_do_mode_select; 6249 6250 modepage_info = (union ctl_modepage_info *) 6251 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6252 6253 memset(modepage_info, 0, sizeof(*modepage_info)); 6254 6255 len_left = param_len - header_size - bd_len; 6256 len_used = header_size + bd_len; 6257 6258 modepage_info->header.len_left = len_left; 6259 modepage_info->header.len_used = len_used; 6260 6261 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6262} 6263 6264int 6265ctl_mode_sense(struct ctl_scsiio *ctsio) 6266{ 6267 struct ctl_lun *lun; 6268 int pc, page_code, dbd, llba, subpage; 6269 int alloc_len, page_len, header_len, total_len; 6270 struct scsi_mode_block_descr *block_desc; 6271 struct ctl_page_index *page_index; 6272 int control_dev; 6273 6274 dbd = 0; 6275 llba = 0; 6276 block_desc = NULL; 6277 page_index = NULL; 6278 6279 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6280 6281 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6282 6283 if (lun->be_lun->lun_type != T_DIRECT) 6284 control_dev = 1; 6285 else 6286 control_dev = 0; 6287 6288 switch (ctsio->cdb[0]) { 6289 case MODE_SENSE_6: { 6290 struct scsi_mode_sense_6 *cdb; 6291 6292 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6293 6294 header_len = sizeof(struct scsi_mode_hdr_6); 6295 if (cdb->byte2 & SMS_DBD) 6296 dbd = 1; 6297 else 6298 header_len += sizeof(struct scsi_mode_block_descr); 6299 6300 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6301 page_code = cdb->page & SMS_PAGE_CODE; 6302 subpage = cdb->subpage; 6303 alloc_len = cdb->length; 6304 break; 6305 } 6306 case MODE_SENSE_10: { 6307 struct scsi_mode_sense_10 *cdb; 6308 6309 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6310 6311 header_len = sizeof(struct scsi_mode_hdr_10); 6312 6313 if (cdb->byte2 & SMS_DBD) 6314 dbd = 1; 6315 else 6316 header_len += sizeof(struct scsi_mode_block_descr); 6317 if (cdb->byte2 & SMS10_LLBAA) 6318 llba = 1; 6319 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6320 page_code = cdb->page & SMS_PAGE_CODE; 6321 subpage = cdb->subpage; 6322 alloc_len = scsi_2btoul(cdb->length); 6323 break; 6324 } 6325 default: 6326 ctl_set_invalid_opcode(ctsio); 6327 ctl_done((union ctl_io *)ctsio); 6328 return (CTL_RETVAL_COMPLETE); 6329 break; /* NOTREACHED */ 6330 } 6331 6332 /* 6333 * We have to make a first pass through to calculate the size of 6334 * the pages that match the user's query. Then we allocate enough 6335 * memory to hold it, and actually copy the data into the buffer. 6336 */ 6337 switch (page_code) { 6338 case SMS_ALL_PAGES_PAGE: { 6339 int i; 6340 6341 page_len = 0; 6342 6343 /* 6344 * At the moment, values other than 0 and 0xff here are 6345 * reserved according to SPC-3. 6346 */ 6347 if ((subpage != SMS_SUBPAGE_PAGE_0) 6348 && (subpage != SMS_SUBPAGE_ALL)) { 6349 ctl_set_invalid_field(ctsio, 6350 /*sks_valid*/ 1, 6351 /*command*/ 1, 6352 /*field*/ 3, 6353 /*bit_valid*/ 0, 6354 /*bit*/ 0); 6355 ctl_done((union ctl_io *)ctsio); 6356 return (CTL_RETVAL_COMPLETE); 6357 } 6358 6359 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6360 if ((control_dev != 0) 6361 && (lun->mode_pages.index[i].page_flags & 6362 CTL_PAGE_FLAG_DISK_ONLY)) 6363 continue; 6364 6365 /* 6366 * We don't use this subpage if the user didn't 6367 * request all subpages. 6368 */ 6369 if ((lun->mode_pages.index[i].subpage != 0) 6370 && (subpage == SMS_SUBPAGE_PAGE_0)) 6371 continue; 6372 6373#if 0 6374 printf("found page %#x len %d\n", 6375 lun->mode_pages.index[i].page_code & 6376 SMPH_PC_MASK, 6377 lun->mode_pages.index[i].page_len); 6378#endif 6379 page_len += lun->mode_pages.index[i].page_len; 6380 } 6381 break; 6382 } 6383 default: { 6384 int i; 6385 6386 page_len = 0; 6387 6388 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6389 /* Look for the right page code */ 6390 if ((lun->mode_pages.index[i].page_code & 6391 SMPH_PC_MASK) != page_code) 6392 continue; 6393 6394 /* Look for the right subpage or the subpage wildcard*/ 6395 if ((lun->mode_pages.index[i].subpage != subpage) 6396 && (subpage != SMS_SUBPAGE_ALL)) 6397 continue; 6398 6399 /* Make sure the page is supported for this dev type */ 6400 if ((control_dev != 0) 6401 && (lun->mode_pages.index[i].page_flags & 6402 CTL_PAGE_FLAG_DISK_ONLY)) 6403 continue; 6404 6405#if 0 6406 printf("found page %#x len %d\n", 6407 lun->mode_pages.index[i].page_code & 6408 SMPH_PC_MASK, 6409 lun->mode_pages.index[i].page_len); 6410#endif 6411 6412 page_len += lun->mode_pages.index[i].page_len; 6413 } 6414 6415 if (page_len == 0) { 6416 ctl_set_invalid_field(ctsio, 6417 /*sks_valid*/ 1, 6418 /*command*/ 1, 6419 /*field*/ 2, 6420 /*bit_valid*/ 1, 6421 /*bit*/ 5); 6422 ctl_done((union ctl_io *)ctsio); 6423 return (CTL_RETVAL_COMPLETE); 6424 } 6425 break; 6426 } 6427 } 6428 6429 total_len = header_len + page_len; 6430#if 0 6431 printf("header_len = %d, page_len = %d, total_len = %d\n", 6432 header_len, page_len, total_len); 6433#endif 6434 6435 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6436 ctsio->kern_sg_entries = 0; 6437 ctsio->kern_data_resid = 0; 6438 ctsio->kern_rel_offset = 0; 6439 if (total_len < alloc_len) { 6440 ctsio->residual = alloc_len - total_len; 6441 ctsio->kern_data_len = total_len; 6442 ctsio->kern_total_len = total_len; 6443 } else { 6444 ctsio->residual = 0; 6445 ctsio->kern_data_len = alloc_len; 6446 ctsio->kern_total_len = alloc_len; 6447 } 6448 6449 switch (ctsio->cdb[0]) { 6450 case MODE_SENSE_6: { 6451 struct scsi_mode_hdr_6 *header; 6452 6453 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6454 6455 header->datalen = MIN(total_len - 1, 254); 6456 if (control_dev == 0) { 6457 header->dev_specific = 0x10; /* DPOFUA */ 6458 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6459 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6460 .eca_and_aen & SCP_SWP) != 0) 6461 header->dev_specific |= 0x80; /* WP */ 6462 } 6463 if (dbd) 6464 header->block_descr_len = 0; 6465 else 6466 header->block_descr_len = 6467 sizeof(struct scsi_mode_block_descr); 6468 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6469 break; 6470 } 6471 case MODE_SENSE_10: { 6472 struct scsi_mode_hdr_10 *header; 6473 int datalen; 6474 6475 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6476 6477 datalen = MIN(total_len - 2, 65533); 6478 scsi_ulto2b(datalen, header->datalen); 6479 if (control_dev == 0) { 6480 header->dev_specific = 0x10; /* DPOFUA */ 6481 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6482 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6483 .eca_and_aen & SCP_SWP) != 0) 6484 header->dev_specific |= 0x80; /* WP */ 6485 } 6486 if (dbd) 6487 scsi_ulto2b(0, header->block_descr_len); 6488 else 6489 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6490 header->block_descr_len); 6491 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6492 break; 6493 } 6494 default: 6495 panic("invalid CDB type %#x", ctsio->cdb[0]); 6496 break; /* NOTREACHED */ 6497 } 6498 6499 /* 6500 * If we've got a disk, use its blocksize in the block 6501 * descriptor. Otherwise, just set it to 0. 6502 */ 6503 if (dbd == 0) { 6504 if (control_dev == 0) 6505 scsi_ulto3b(lun->be_lun->blocksize, 6506 block_desc->block_len); 6507 else 6508 scsi_ulto3b(0, block_desc->block_len); 6509 } 6510 6511 switch (page_code) { 6512 case SMS_ALL_PAGES_PAGE: { 6513 int i, data_used; 6514 6515 data_used = header_len; 6516 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6517 struct ctl_page_index *page_index; 6518 6519 page_index = &lun->mode_pages.index[i]; 6520 6521 if ((control_dev != 0) 6522 && (page_index->page_flags & 6523 CTL_PAGE_FLAG_DISK_ONLY)) 6524 continue; 6525 6526 /* 6527 * We don't use this subpage if the user didn't 6528 * request all subpages. We already checked (above) 6529 * to make sure the user only specified a subpage 6530 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6531 */ 6532 if ((page_index->subpage != 0) 6533 && (subpage == SMS_SUBPAGE_PAGE_0)) 6534 continue; 6535 6536 /* 6537 * Call the handler, if it exists, to update the 6538 * page to the latest values. 6539 */ 6540 if (page_index->sense_handler != NULL) 6541 page_index->sense_handler(ctsio, page_index,pc); 6542 6543 memcpy(ctsio->kern_data_ptr + data_used, 6544 page_index->page_data + 6545 (page_index->page_len * pc), 6546 page_index->page_len); 6547 data_used += page_index->page_len; 6548 } 6549 break; 6550 } 6551 default: { 6552 int i, data_used; 6553 6554 data_used = header_len; 6555 6556 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6557 struct ctl_page_index *page_index; 6558 6559 page_index = &lun->mode_pages.index[i]; 6560 6561 /* Look for the right page code */ 6562 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6563 continue; 6564 6565 /* Look for the right subpage or the subpage wildcard*/ 6566 if ((page_index->subpage != subpage) 6567 && (subpage != SMS_SUBPAGE_ALL)) 6568 continue; 6569 6570 /* Make sure the page is supported for this dev type */ 6571 if ((control_dev != 0) 6572 && (page_index->page_flags & 6573 CTL_PAGE_FLAG_DISK_ONLY)) 6574 continue; 6575 6576 /* 6577 * Call the handler, if it exists, to update the 6578 * page to the latest values. 6579 */ 6580 if (page_index->sense_handler != NULL) 6581 page_index->sense_handler(ctsio, page_index,pc); 6582 6583 memcpy(ctsio->kern_data_ptr + data_used, 6584 page_index->page_data + 6585 (page_index->page_len * pc), 6586 page_index->page_len); 6587 data_used += page_index->page_len; 6588 } 6589 break; 6590 } 6591 } 6592 6593 ctl_set_success(ctsio); 6594 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6595 ctsio->be_move_done = ctl_config_move_done; 6596 ctl_datamove((union ctl_io *)ctsio); 6597 return (CTL_RETVAL_COMPLETE); 6598} 6599 6600int 6601ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6602 struct ctl_page_index *page_index, 6603 int pc) 6604{ 6605 struct ctl_lun *lun; 6606 struct scsi_log_param_header *phdr; 6607 uint8_t *data; 6608 uint64_t val; 6609 6610 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6611 data = page_index->page_data; 6612 6613 if (lun->backend->lun_attr != NULL && 6614 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6615 != UINT64_MAX) { 6616 phdr = (struct scsi_log_param_header *)data; 6617 scsi_ulto2b(0x0001, phdr->param_code); 6618 phdr->param_control = SLP_LBIN | SLP_LP; 6619 phdr->param_len = 8; 6620 data = (uint8_t *)(phdr + 1); 6621 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6622 data[4] = 0x02; /* per-pool */ 6623 data += phdr->param_len; 6624 } 6625 6626 if (lun->backend->lun_attr != NULL && 6627 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6628 != UINT64_MAX) { 6629 phdr = (struct scsi_log_param_header *)data; 6630 scsi_ulto2b(0x0002, phdr->param_code); 6631 phdr->param_control = SLP_LBIN | SLP_LP; 6632 phdr->param_len = 8; 6633 data = (uint8_t *)(phdr + 1); 6634 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6635 data[4] = 0x01; /* per-LUN */ 6636 data += phdr->param_len; 6637 } 6638 6639 if (lun->backend->lun_attr != NULL && 6640 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6641 != UINT64_MAX) { 6642 phdr = (struct scsi_log_param_header *)data; 6643 scsi_ulto2b(0x00f1, phdr->param_code); 6644 phdr->param_control = SLP_LBIN | SLP_LP; 6645 phdr->param_len = 8; 6646 data = (uint8_t *)(phdr + 1); 6647 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6648 data[4] = 0x02; /* per-pool */ 6649 data += phdr->param_len; 6650 } 6651 6652 if (lun->backend->lun_attr != NULL && 6653 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6654 != UINT64_MAX) { 6655 phdr = (struct scsi_log_param_header *)data; 6656 scsi_ulto2b(0x00f2, phdr->param_code); 6657 phdr->param_control = SLP_LBIN | SLP_LP; 6658 phdr->param_len = 8; 6659 data = (uint8_t *)(phdr + 1); 6660 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6661 data[4] = 0x02; /* per-pool */ 6662 data += phdr->param_len; 6663 } 6664 6665 page_index->page_len = data - page_index->page_data; 6666 return (0); 6667} 6668 6669int 6670ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6671 struct ctl_page_index *page_index, 6672 int pc) 6673{ 6674 struct ctl_lun *lun; 6675 struct stat_page *data; 6676 uint64_t rn, wn, rb, wb; 6677 struct bintime rt, wt; 6678 int i; 6679 6680 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6681 data = (struct stat_page *)page_index->page_data; 6682 6683 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6684 data->sap.hdr.param_control = SLP_LBIN; 6685 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6686 sizeof(struct scsi_log_param_header); 6687 rn = wn = rb = wb = 0; 6688 bintime_clear(&rt); 6689 bintime_clear(&wt); 6690 for (i = 0; i < CTL_MAX_PORTS; i++) { 6691 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6692 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6693 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6694 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6695 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6696 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6697 } 6698 scsi_u64to8b(rn, data->sap.read_num); 6699 scsi_u64to8b(wn, data->sap.write_num); 6700 if (lun->stats.blocksize > 0) { 6701 scsi_u64to8b(wb / lun->stats.blocksize, 6702 data->sap.recvieved_lba); 6703 scsi_u64to8b(rb / lun->stats.blocksize, 6704 data->sap.transmitted_lba); 6705 } 6706 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6707 data->sap.read_int); 6708 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6709 data->sap.write_int); 6710 scsi_u64to8b(0, data->sap.weighted_num); 6711 scsi_u64to8b(0, data->sap.weighted_int); 6712 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6713 data->it.hdr.param_control = SLP_LBIN; 6714 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6715 sizeof(struct scsi_log_param_header); 6716#ifdef CTL_TIME_IO 6717 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6718#endif 6719 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6720 data->it.hdr.param_control = SLP_LBIN; 6721 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6722 sizeof(struct scsi_log_param_header); 6723 scsi_ulto4b(3, data->ti.exponent); 6724 scsi_ulto4b(1, data->ti.integer); 6725 6726 page_index->page_len = sizeof(*data); 6727 return (0); 6728} 6729 6730int 6731ctl_log_sense(struct ctl_scsiio *ctsio) 6732{ 6733 struct ctl_lun *lun; 6734 int i, pc, page_code, subpage; 6735 int alloc_len, total_len; 6736 struct ctl_page_index *page_index; 6737 struct scsi_log_sense *cdb; 6738 struct scsi_log_header *header; 6739 6740 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6741 6742 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6743 cdb = (struct scsi_log_sense *)ctsio->cdb; 6744 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6745 page_code = cdb->page & SLS_PAGE_CODE; 6746 subpage = cdb->subpage; 6747 alloc_len = scsi_2btoul(cdb->length); 6748 6749 page_index = NULL; 6750 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6751 page_index = &lun->log_pages.index[i]; 6752 6753 /* Look for the right page code */ 6754 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6755 continue; 6756 6757 /* Look for the right subpage or the subpage wildcard*/ 6758 if (page_index->subpage != subpage) 6759 continue; 6760 6761 break; 6762 } 6763 if (i >= CTL_NUM_LOG_PAGES) { 6764 ctl_set_invalid_field(ctsio, 6765 /*sks_valid*/ 1, 6766 /*command*/ 1, 6767 /*field*/ 2, 6768 /*bit_valid*/ 0, 6769 /*bit*/ 0); 6770 ctl_done((union ctl_io *)ctsio); 6771 return (CTL_RETVAL_COMPLETE); 6772 } 6773 6774 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6775 6776 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6777 ctsio->kern_sg_entries = 0; 6778 ctsio->kern_data_resid = 0; 6779 ctsio->kern_rel_offset = 0; 6780 if (total_len < alloc_len) { 6781 ctsio->residual = alloc_len - total_len; 6782 ctsio->kern_data_len = total_len; 6783 ctsio->kern_total_len = total_len; 6784 } else { 6785 ctsio->residual = 0; 6786 ctsio->kern_data_len = alloc_len; 6787 ctsio->kern_total_len = alloc_len; 6788 } 6789 6790 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6791 header->page = page_index->page_code; 6792 if (page_index->subpage) { 6793 header->page |= SL_SPF; 6794 header->subpage = page_index->subpage; 6795 } 6796 scsi_ulto2b(page_index->page_len, header->datalen); 6797 6798 /* 6799 * Call the handler, if it exists, to update the 6800 * page to the latest values. 6801 */ 6802 if (page_index->sense_handler != NULL) 6803 page_index->sense_handler(ctsio, page_index, pc); 6804 6805 memcpy(header + 1, page_index->page_data, page_index->page_len); 6806 6807 ctl_set_success(ctsio); 6808 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6809 ctsio->be_move_done = ctl_config_move_done; 6810 ctl_datamove((union ctl_io *)ctsio); 6811 return (CTL_RETVAL_COMPLETE); 6812} 6813 6814int 6815ctl_read_capacity(struct ctl_scsiio *ctsio) 6816{ 6817 struct scsi_read_capacity *cdb; 6818 struct scsi_read_capacity_data *data; 6819 struct ctl_lun *lun; 6820 uint32_t lba; 6821 6822 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6823 6824 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6825 6826 lba = scsi_4btoul(cdb->addr); 6827 if (((cdb->pmi & SRC_PMI) == 0) 6828 && (lba != 0)) { 6829 ctl_set_invalid_field(/*ctsio*/ ctsio, 6830 /*sks_valid*/ 1, 6831 /*command*/ 1, 6832 /*field*/ 2, 6833 /*bit_valid*/ 0, 6834 /*bit*/ 0); 6835 ctl_done((union ctl_io *)ctsio); 6836 return (CTL_RETVAL_COMPLETE); 6837 } 6838 6839 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6840 6841 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6842 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6843 ctsio->residual = 0; 6844 ctsio->kern_data_len = sizeof(*data); 6845 ctsio->kern_total_len = sizeof(*data); 6846 ctsio->kern_data_resid = 0; 6847 ctsio->kern_rel_offset = 0; 6848 ctsio->kern_sg_entries = 0; 6849 6850 /* 6851 * If the maximum LBA is greater than 0xfffffffe, the user must 6852 * issue a SERVICE ACTION IN (16) command, with the read capacity 6853 * serivce action set. 6854 */ 6855 if (lun->be_lun->maxlba > 0xfffffffe) 6856 scsi_ulto4b(0xffffffff, data->addr); 6857 else 6858 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6859 6860 /* 6861 * XXX KDM this may not be 512 bytes... 6862 */ 6863 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6864 6865 ctl_set_success(ctsio); 6866 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6867 ctsio->be_move_done = ctl_config_move_done; 6868 ctl_datamove((union ctl_io *)ctsio); 6869 return (CTL_RETVAL_COMPLETE); 6870} 6871 6872int 6873ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6874{ 6875 struct scsi_read_capacity_16 *cdb; 6876 struct scsi_read_capacity_data_long *data; 6877 struct ctl_lun *lun; 6878 uint64_t lba; 6879 uint32_t alloc_len; 6880 6881 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6882 6883 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6884 6885 alloc_len = scsi_4btoul(cdb->alloc_len); 6886 lba = scsi_8btou64(cdb->addr); 6887 6888 if ((cdb->reladr & SRC16_PMI) 6889 && (lba != 0)) { 6890 ctl_set_invalid_field(/*ctsio*/ ctsio, 6891 /*sks_valid*/ 1, 6892 /*command*/ 1, 6893 /*field*/ 2, 6894 /*bit_valid*/ 0, 6895 /*bit*/ 0); 6896 ctl_done((union ctl_io *)ctsio); 6897 return (CTL_RETVAL_COMPLETE); 6898 } 6899 6900 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6901 6902 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6903 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6904 6905 if (sizeof(*data) < alloc_len) { 6906 ctsio->residual = alloc_len - sizeof(*data); 6907 ctsio->kern_data_len = sizeof(*data); 6908 ctsio->kern_total_len = sizeof(*data); 6909 } else { 6910 ctsio->residual = 0; 6911 ctsio->kern_data_len = alloc_len; 6912 ctsio->kern_total_len = alloc_len; 6913 } 6914 ctsio->kern_data_resid = 0; 6915 ctsio->kern_rel_offset = 0; 6916 ctsio->kern_sg_entries = 0; 6917 6918 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6919 /* XXX KDM this may not be 512 bytes... */ 6920 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6921 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6922 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6923 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6924 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6925 6926 ctl_set_success(ctsio); 6927 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6928 ctsio->be_move_done = ctl_config_move_done; 6929 ctl_datamove((union ctl_io *)ctsio); 6930 return (CTL_RETVAL_COMPLETE); 6931} 6932 6933int 6934ctl_get_lba_status(struct ctl_scsiio *ctsio) 6935{ 6936 struct scsi_get_lba_status *cdb; 6937 struct scsi_get_lba_status_data *data; 6938 struct ctl_lun *lun; 6939 struct ctl_lba_len_flags *lbalen; 6940 uint64_t lba; 6941 uint32_t alloc_len, total_len; 6942 int retval; 6943 6944 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 6945 6946 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6947 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 6948 lba = scsi_8btou64(cdb->addr); 6949 alloc_len = scsi_4btoul(cdb->alloc_len); 6950 6951 if (lba > lun->be_lun->maxlba) { 6952 ctl_set_lba_out_of_range(ctsio); 6953 ctl_done((union ctl_io *)ctsio); 6954 return (CTL_RETVAL_COMPLETE); 6955 } 6956 6957 total_len = sizeof(*data) + sizeof(data->descr[0]); 6958 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6959 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 6960 6961 if (total_len < alloc_len) { 6962 ctsio->residual = alloc_len - total_len; 6963 ctsio->kern_data_len = total_len; 6964 ctsio->kern_total_len = total_len; 6965 } else { 6966 ctsio->residual = 0; 6967 ctsio->kern_data_len = alloc_len; 6968 ctsio->kern_total_len = alloc_len; 6969 } 6970 ctsio->kern_data_resid = 0; 6971 ctsio->kern_rel_offset = 0; 6972 ctsio->kern_sg_entries = 0; 6973 6974 /* Fill dummy data in case backend can't tell anything. */ 6975 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 6976 scsi_u64to8b(lba, data->descr[0].addr); 6977 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 6978 data->descr[0].length); 6979 data->descr[0].status = 0; /* Mapped or unknown. */ 6980 6981 ctl_set_success(ctsio); 6982 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6983 ctsio->be_move_done = ctl_config_move_done; 6984 6985 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6986 lbalen->lba = lba; 6987 lbalen->len = total_len; 6988 lbalen->flags = 0; 6989 retval = lun->backend->config_read((union ctl_io *)ctsio); 6990 return (CTL_RETVAL_COMPLETE); 6991} 6992 6993int 6994ctl_read_defect(struct ctl_scsiio *ctsio) 6995{ 6996 struct scsi_read_defect_data_10 *ccb10; 6997 struct scsi_read_defect_data_12 *ccb12; 6998 struct scsi_read_defect_data_hdr_10 *data10; 6999 struct scsi_read_defect_data_hdr_12 *data12; 7000 uint32_t alloc_len, data_len; 7001 uint8_t format; 7002 7003 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7004 7005 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7006 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7007 format = ccb10->format; 7008 alloc_len = scsi_2btoul(ccb10->alloc_length); 7009 data_len = sizeof(*data10); 7010 } else { 7011 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7012 format = ccb12->format; 7013 alloc_len = scsi_4btoul(ccb12->alloc_length); 7014 data_len = sizeof(*data12); 7015 } 7016 if (alloc_len == 0) { 7017 ctl_set_success(ctsio); 7018 ctl_done((union ctl_io *)ctsio); 7019 return (CTL_RETVAL_COMPLETE); 7020 } 7021 7022 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7023 if (data_len < alloc_len) { 7024 ctsio->residual = alloc_len - data_len; 7025 ctsio->kern_data_len = data_len; 7026 ctsio->kern_total_len = data_len; 7027 } else { 7028 ctsio->residual = 0; 7029 ctsio->kern_data_len = alloc_len; 7030 ctsio->kern_total_len = alloc_len; 7031 } 7032 ctsio->kern_data_resid = 0; 7033 ctsio->kern_rel_offset = 0; 7034 ctsio->kern_sg_entries = 0; 7035 7036 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7037 data10 = (struct scsi_read_defect_data_hdr_10 *) 7038 ctsio->kern_data_ptr; 7039 data10->format = format; 7040 scsi_ulto2b(0, data10->length); 7041 } else { 7042 data12 = (struct scsi_read_defect_data_hdr_12 *) 7043 ctsio->kern_data_ptr; 7044 data12->format = format; 7045 scsi_ulto2b(0, data12->generation); 7046 scsi_ulto4b(0, data12->length); 7047 } 7048 7049 ctl_set_success(ctsio); 7050 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7051 ctsio->be_move_done = ctl_config_move_done; 7052 ctl_datamove((union ctl_io *)ctsio); 7053 return (CTL_RETVAL_COMPLETE); 7054} 7055 7056int 7057ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7058{ 7059 struct scsi_maintenance_in *cdb; 7060 int retval; 7061 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os; 7062 int num_target_port_groups, num_target_ports; 7063 struct ctl_lun *lun; 7064 struct ctl_softc *softc; 7065 struct ctl_port *port; 7066 struct scsi_target_group_data *rtg_ptr; 7067 struct scsi_target_group_data_extended *rtg_ext_ptr; 7068 struct scsi_target_port_group_descriptor *tpg_desc; 7069 7070 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7071 7072 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7073 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7074 softc = lun->ctl_softc; 7075 7076 retval = CTL_RETVAL_COMPLETE; 7077 7078 switch (cdb->byte2 & STG_PDF_MASK) { 7079 case STG_PDF_LENGTH: 7080 ext = 0; 7081 break; 7082 case STG_PDF_EXTENDED: 7083 ext = 1; 7084 break; 7085 default: 7086 ctl_set_invalid_field(/*ctsio*/ ctsio, 7087 /*sks_valid*/ 1, 7088 /*command*/ 1, 7089 /*field*/ 2, 7090 /*bit_valid*/ 1, 7091 /*bit*/ 5); 7092 ctl_done((union ctl_io *)ctsio); 7093 return(retval); 7094 } 7095 7096 if (softc->is_single) 7097 num_target_port_groups = 1; 7098 else 7099 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7100 num_target_ports = 0; 7101 mtx_lock(&softc->ctl_lock); 7102 STAILQ_FOREACH(port, &softc->port_list, links) { 7103 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7104 continue; 7105 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7106 continue; 7107 num_target_ports++; 7108 } 7109 mtx_unlock(&softc->ctl_lock); 7110 7111 if (ext) 7112 total_len = sizeof(struct scsi_target_group_data_extended); 7113 else 7114 total_len = sizeof(struct scsi_target_group_data); 7115 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7116 num_target_port_groups + 7117 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7118 7119 alloc_len = scsi_4btoul(cdb->length); 7120 7121 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7122 7123 ctsio->kern_sg_entries = 0; 7124 7125 if (total_len < alloc_len) { 7126 ctsio->residual = alloc_len - total_len; 7127 ctsio->kern_data_len = total_len; 7128 ctsio->kern_total_len = total_len; 7129 } else { 7130 ctsio->residual = 0; 7131 ctsio->kern_data_len = alloc_len; 7132 ctsio->kern_total_len = alloc_len; 7133 } 7134 ctsio->kern_data_resid = 0; 7135 ctsio->kern_rel_offset = 0; 7136 7137 if (ext) { 7138 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7139 ctsio->kern_data_ptr; 7140 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7141 rtg_ext_ptr->format_type = 0x10; 7142 rtg_ext_ptr->implicit_transition_time = 0; 7143 tpg_desc = &rtg_ext_ptr->groups[0]; 7144 } else { 7145 rtg_ptr = (struct scsi_target_group_data *) 7146 ctsio->kern_data_ptr; 7147 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7148 tpg_desc = &rtg_ptr->groups[0]; 7149 } 7150 7151 mtx_lock(&softc->ctl_lock); 7152 pg = softc->port_min / softc->port_cnt; 7153 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7154 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7155 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7156 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7157 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7158 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7159 else 7160 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7161 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7162 os = gs; 7163 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7164 } else 7165 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7166 for (g = 0; g < num_target_port_groups; g++) { 7167 tpg_desc->pref_state = (g == pg) ? gs : os; 7168 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7169 TPG_U_SUP | TPG_T_SUP; 7170 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7171 tpg_desc->status = TPG_IMPLICIT; 7172 pc = 0; 7173 STAILQ_FOREACH(port, &softc->port_list, links) { 7174 if (port->targ_port < g * softc->port_cnt || 7175 port->targ_port >= (g + 1) * softc->port_cnt) 7176 continue; 7177 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7178 continue; 7179 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7180 continue; 7181 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7182 relative_target_port_identifier); 7183 pc++; 7184 } 7185 tpg_desc->target_port_count = pc; 7186 tpg_desc = (struct scsi_target_port_group_descriptor *) 7187 &tpg_desc->descriptors[pc]; 7188 } 7189 mtx_unlock(&softc->ctl_lock); 7190 7191 ctl_set_success(ctsio); 7192 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7193 ctsio->be_move_done = ctl_config_move_done; 7194 ctl_datamove((union ctl_io *)ctsio); 7195 return(retval); 7196} 7197 7198int 7199ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7200{ 7201 struct ctl_lun *lun; 7202 struct scsi_report_supported_opcodes *cdb; 7203 const struct ctl_cmd_entry *entry, *sentry; 7204 struct scsi_report_supported_opcodes_all *all; 7205 struct scsi_report_supported_opcodes_descr *descr; 7206 struct scsi_report_supported_opcodes_one *one; 7207 int retval; 7208 int alloc_len, total_len; 7209 int opcode, service_action, i, j, num; 7210 7211 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7212 7213 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7214 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7215 7216 retval = CTL_RETVAL_COMPLETE; 7217 7218 opcode = cdb->requested_opcode; 7219 service_action = scsi_2btoul(cdb->requested_service_action); 7220 switch (cdb->options & RSO_OPTIONS_MASK) { 7221 case RSO_OPTIONS_ALL: 7222 num = 0; 7223 for (i = 0; i < 256; i++) { 7224 entry = &ctl_cmd_table[i]; 7225 if (entry->flags & CTL_CMD_FLAG_SA5) { 7226 for (j = 0; j < 32; j++) { 7227 sentry = &((const struct ctl_cmd_entry *) 7228 entry->execute)[j]; 7229 if (ctl_cmd_applicable( 7230 lun->be_lun->lun_type, sentry)) 7231 num++; 7232 } 7233 } else { 7234 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7235 entry)) 7236 num++; 7237 } 7238 } 7239 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7240 num * sizeof(struct scsi_report_supported_opcodes_descr); 7241 break; 7242 case RSO_OPTIONS_OC: 7243 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7244 ctl_set_invalid_field(/*ctsio*/ ctsio, 7245 /*sks_valid*/ 1, 7246 /*command*/ 1, 7247 /*field*/ 2, 7248 /*bit_valid*/ 1, 7249 /*bit*/ 2); 7250 ctl_done((union ctl_io *)ctsio); 7251 return (CTL_RETVAL_COMPLETE); 7252 } 7253 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7254 break; 7255 case RSO_OPTIONS_OC_SA: 7256 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7257 service_action >= 32) { 7258 ctl_set_invalid_field(/*ctsio*/ ctsio, 7259 /*sks_valid*/ 1, 7260 /*command*/ 1, 7261 /*field*/ 2, 7262 /*bit_valid*/ 1, 7263 /*bit*/ 2); 7264 ctl_done((union ctl_io *)ctsio); 7265 return (CTL_RETVAL_COMPLETE); 7266 } 7267 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7268 break; 7269 default: 7270 ctl_set_invalid_field(/*ctsio*/ ctsio, 7271 /*sks_valid*/ 1, 7272 /*command*/ 1, 7273 /*field*/ 2, 7274 /*bit_valid*/ 1, 7275 /*bit*/ 2); 7276 ctl_done((union ctl_io *)ctsio); 7277 return (CTL_RETVAL_COMPLETE); 7278 } 7279 7280 alloc_len = scsi_4btoul(cdb->length); 7281 7282 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7283 7284 ctsio->kern_sg_entries = 0; 7285 7286 if (total_len < alloc_len) { 7287 ctsio->residual = alloc_len - total_len; 7288 ctsio->kern_data_len = total_len; 7289 ctsio->kern_total_len = total_len; 7290 } else { 7291 ctsio->residual = 0; 7292 ctsio->kern_data_len = alloc_len; 7293 ctsio->kern_total_len = alloc_len; 7294 } 7295 ctsio->kern_data_resid = 0; 7296 ctsio->kern_rel_offset = 0; 7297 7298 switch (cdb->options & RSO_OPTIONS_MASK) { 7299 case RSO_OPTIONS_ALL: 7300 all = (struct scsi_report_supported_opcodes_all *) 7301 ctsio->kern_data_ptr; 7302 num = 0; 7303 for (i = 0; i < 256; i++) { 7304 entry = &ctl_cmd_table[i]; 7305 if (entry->flags & CTL_CMD_FLAG_SA5) { 7306 for (j = 0; j < 32; j++) { 7307 sentry = &((const struct ctl_cmd_entry *) 7308 entry->execute)[j]; 7309 if (!ctl_cmd_applicable( 7310 lun->be_lun->lun_type, sentry)) 7311 continue; 7312 descr = &all->descr[num++]; 7313 descr->opcode = i; 7314 scsi_ulto2b(j, descr->service_action); 7315 descr->flags = RSO_SERVACTV; 7316 scsi_ulto2b(sentry->length, 7317 descr->cdb_length); 7318 } 7319 } else { 7320 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7321 entry)) 7322 continue; 7323 descr = &all->descr[num++]; 7324 descr->opcode = i; 7325 scsi_ulto2b(0, descr->service_action); 7326 descr->flags = 0; 7327 scsi_ulto2b(entry->length, descr->cdb_length); 7328 } 7329 } 7330 scsi_ulto4b( 7331 num * sizeof(struct scsi_report_supported_opcodes_descr), 7332 all->length); 7333 break; 7334 case RSO_OPTIONS_OC: 7335 one = (struct scsi_report_supported_opcodes_one *) 7336 ctsio->kern_data_ptr; 7337 entry = &ctl_cmd_table[opcode]; 7338 goto fill_one; 7339 case RSO_OPTIONS_OC_SA: 7340 one = (struct scsi_report_supported_opcodes_one *) 7341 ctsio->kern_data_ptr; 7342 entry = &ctl_cmd_table[opcode]; 7343 entry = &((const struct ctl_cmd_entry *) 7344 entry->execute)[service_action]; 7345fill_one: 7346 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7347 one->support = 3; 7348 scsi_ulto2b(entry->length, one->cdb_length); 7349 one->cdb_usage[0] = opcode; 7350 memcpy(&one->cdb_usage[1], entry->usage, 7351 entry->length - 1); 7352 } else 7353 one->support = 1; 7354 break; 7355 } 7356 7357 ctl_set_success(ctsio); 7358 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7359 ctsio->be_move_done = ctl_config_move_done; 7360 ctl_datamove((union ctl_io *)ctsio); 7361 return(retval); 7362} 7363 7364int 7365ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7366{ 7367 struct scsi_report_supported_tmf *cdb; 7368 struct scsi_report_supported_tmf_data *data; 7369 int retval; 7370 int alloc_len, total_len; 7371 7372 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7373 7374 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7375 7376 retval = CTL_RETVAL_COMPLETE; 7377 7378 total_len = sizeof(struct scsi_report_supported_tmf_data); 7379 alloc_len = scsi_4btoul(cdb->length); 7380 7381 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7382 7383 ctsio->kern_sg_entries = 0; 7384 7385 if (total_len < alloc_len) { 7386 ctsio->residual = alloc_len - total_len; 7387 ctsio->kern_data_len = total_len; 7388 ctsio->kern_total_len = total_len; 7389 } else { 7390 ctsio->residual = 0; 7391 ctsio->kern_data_len = alloc_len; 7392 ctsio->kern_total_len = alloc_len; 7393 } 7394 ctsio->kern_data_resid = 0; 7395 ctsio->kern_rel_offset = 0; 7396 7397 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7398 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7399 RST_TRS; 7400 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7401 7402 ctl_set_success(ctsio); 7403 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7404 ctsio->be_move_done = ctl_config_move_done; 7405 ctl_datamove((union ctl_io *)ctsio); 7406 return (retval); 7407} 7408 7409int 7410ctl_report_timestamp(struct ctl_scsiio *ctsio) 7411{ 7412 struct scsi_report_timestamp *cdb; 7413 struct scsi_report_timestamp_data *data; 7414 struct timeval tv; 7415 int64_t timestamp; 7416 int retval; 7417 int alloc_len, total_len; 7418 7419 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7420 7421 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7422 7423 retval = CTL_RETVAL_COMPLETE; 7424 7425 total_len = sizeof(struct scsi_report_timestamp_data); 7426 alloc_len = scsi_4btoul(cdb->length); 7427 7428 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7429 7430 ctsio->kern_sg_entries = 0; 7431 7432 if (total_len < alloc_len) { 7433 ctsio->residual = alloc_len - total_len; 7434 ctsio->kern_data_len = total_len; 7435 ctsio->kern_total_len = total_len; 7436 } else { 7437 ctsio->residual = 0; 7438 ctsio->kern_data_len = alloc_len; 7439 ctsio->kern_total_len = alloc_len; 7440 } 7441 ctsio->kern_data_resid = 0; 7442 ctsio->kern_rel_offset = 0; 7443 7444 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7445 scsi_ulto2b(sizeof(*data) - 2, data->length); 7446 data->origin = RTS_ORIG_OUTSIDE; 7447 getmicrotime(&tv); 7448 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7449 scsi_ulto4b(timestamp >> 16, data->timestamp); 7450 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7451 7452 ctl_set_success(ctsio); 7453 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7454 ctsio->be_move_done = ctl_config_move_done; 7455 ctl_datamove((union ctl_io *)ctsio); 7456 return (retval); 7457} 7458 7459int 7460ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7461{ 7462 struct scsi_per_res_in *cdb; 7463 int alloc_len, total_len = 0; 7464 /* struct scsi_per_res_in_rsrv in_data; */ 7465 struct ctl_lun *lun; 7466 struct ctl_softc *softc; 7467 uint64_t key; 7468 7469 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7470 7471 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7472 7473 alloc_len = scsi_2btoul(cdb->length); 7474 7475 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7476 softc = lun->ctl_softc; 7477 7478retry: 7479 mtx_lock(&lun->lun_lock); 7480 switch (cdb->action) { 7481 case SPRI_RK: /* read keys */ 7482 total_len = sizeof(struct scsi_per_res_in_keys) + 7483 lun->pr_key_count * 7484 sizeof(struct scsi_per_res_key); 7485 break; 7486 case SPRI_RR: /* read reservation */ 7487 if (lun->flags & CTL_LUN_PR_RESERVED) 7488 total_len = sizeof(struct scsi_per_res_in_rsrv); 7489 else 7490 total_len = sizeof(struct scsi_per_res_in_header); 7491 break; 7492 case SPRI_RC: /* report capabilities */ 7493 total_len = sizeof(struct scsi_per_res_cap); 7494 break; 7495 case SPRI_RS: /* read full status */ 7496 total_len = sizeof(struct scsi_per_res_in_header) + 7497 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7498 lun->pr_key_count; 7499 break; 7500 default: 7501 panic("Invalid PR type %x", cdb->action); 7502 } 7503 mtx_unlock(&lun->lun_lock); 7504 7505 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7506 7507 if (total_len < alloc_len) { 7508 ctsio->residual = alloc_len - total_len; 7509 ctsio->kern_data_len = total_len; 7510 ctsio->kern_total_len = total_len; 7511 } else { 7512 ctsio->residual = 0; 7513 ctsio->kern_data_len = alloc_len; 7514 ctsio->kern_total_len = alloc_len; 7515 } 7516 7517 ctsio->kern_data_resid = 0; 7518 ctsio->kern_rel_offset = 0; 7519 ctsio->kern_sg_entries = 0; 7520 7521 mtx_lock(&lun->lun_lock); 7522 switch (cdb->action) { 7523 case SPRI_RK: { // read keys 7524 struct scsi_per_res_in_keys *res_keys; 7525 int i, key_count; 7526 7527 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7528 7529 /* 7530 * We had to drop the lock to allocate our buffer, which 7531 * leaves time for someone to come in with another 7532 * persistent reservation. (That is unlikely, though, 7533 * since this should be the only persistent reservation 7534 * command active right now.) 7535 */ 7536 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7537 (lun->pr_key_count * 7538 sizeof(struct scsi_per_res_key)))){ 7539 mtx_unlock(&lun->lun_lock); 7540 free(ctsio->kern_data_ptr, M_CTL); 7541 printf("%s: reservation length changed, retrying\n", 7542 __func__); 7543 goto retry; 7544 } 7545 7546 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7547 7548 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7549 lun->pr_key_count, res_keys->header.length); 7550 7551 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7552 if ((key = ctl_get_prkey(lun, i)) == 0) 7553 continue; 7554 7555 /* 7556 * We used lun->pr_key_count to calculate the 7557 * size to allocate. If it turns out the number of 7558 * initiators with the registered flag set is 7559 * larger than that (i.e. they haven't been kept in 7560 * sync), we've got a problem. 7561 */ 7562 if (key_count >= lun->pr_key_count) { 7563#ifdef NEEDTOPORT 7564 csevent_log(CSC_CTL | CSC_SHELF_SW | 7565 CTL_PR_ERROR, 7566 csevent_LogType_Fault, 7567 csevent_AlertLevel_Yellow, 7568 csevent_FRU_ShelfController, 7569 csevent_FRU_Firmware, 7570 csevent_FRU_Unknown, 7571 "registered keys %d >= key " 7572 "count %d", key_count, 7573 lun->pr_key_count); 7574#endif 7575 key_count++; 7576 continue; 7577 } 7578 scsi_u64to8b(key, res_keys->keys[key_count].key); 7579 key_count++; 7580 } 7581 break; 7582 } 7583 case SPRI_RR: { // read reservation 7584 struct scsi_per_res_in_rsrv *res; 7585 int tmp_len, header_only; 7586 7587 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7588 7589 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7590 7591 if (lun->flags & CTL_LUN_PR_RESERVED) 7592 { 7593 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7594 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7595 res->header.length); 7596 header_only = 0; 7597 } else { 7598 tmp_len = sizeof(struct scsi_per_res_in_header); 7599 scsi_ulto4b(0, res->header.length); 7600 header_only = 1; 7601 } 7602 7603 /* 7604 * We had to drop the lock to allocate our buffer, which 7605 * leaves time for someone to come in with another 7606 * persistent reservation. (That is unlikely, though, 7607 * since this should be the only persistent reservation 7608 * command active right now.) 7609 */ 7610 if (tmp_len != total_len) { 7611 mtx_unlock(&lun->lun_lock); 7612 free(ctsio->kern_data_ptr, M_CTL); 7613 printf("%s: reservation status changed, retrying\n", 7614 __func__); 7615 goto retry; 7616 } 7617 7618 /* 7619 * No reservation held, so we're done. 7620 */ 7621 if (header_only != 0) 7622 break; 7623 7624 /* 7625 * If the registration is an All Registrants type, the key 7626 * is 0, since it doesn't really matter. 7627 */ 7628 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7629 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7630 res->data.reservation); 7631 } 7632 res->data.scopetype = lun->res_type; 7633 break; 7634 } 7635 case SPRI_RC: //report capabilities 7636 { 7637 struct scsi_per_res_cap *res_cap; 7638 uint16_t type_mask; 7639 7640 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7641 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7642 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7643 type_mask = SPRI_TM_WR_EX_AR | 7644 SPRI_TM_EX_AC_RO | 7645 SPRI_TM_WR_EX_RO | 7646 SPRI_TM_EX_AC | 7647 SPRI_TM_WR_EX | 7648 SPRI_TM_EX_AC_AR; 7649 scsi_ulto2b(type_mask, res_cap->type_mask); 7650 break; 7651 } 7652 case SPRI_RS: { // read full status 7653 struct scsi_per_res_in_full *res_status; 7654 struct scsi_per_res_in_full_desc *res_desc; 7655 struct ctl_port *port; 7656 int i, len; 7657 7658 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7659 7660 /* 7661 * We had to drop the lock to allocate our buffer, which 7662 * leaves time for someone to come in with another 7663 * persistent reservation. (That is unlikely, though, 7664 * since this should be the only persistent reservation 7665 * command active right now.) 7666 */ 7667 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7668 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7669 lun->pr_key_count)){ 7670 mtx_unlock(&lun->lun_lock); 7671 free(ctsio->kern_data_ptr, M_CTL); 7672 printf("%s: reservation length changed, retrying\n", 7673 __func__); 7674 goto retry; 7675 } 7676 7677 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7678 7679 res_desc = &res_status->desc[0]; 7680 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7681 if ((key = ctl_get_prkey(lun, i)) == 0) 7682 continue; 7683 7684 scsi_u64to8b(key, res_desc->res_key.key); 7685 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7686 (lun->pr_res_idx == i || 7687 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7688 res_desc->flags = SPRI_FULL_R_HOLDER; 7689 res_desc->scopetype = lun->res_type; 7690 } 7691 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7692 res_desc->rel_trgt_port_id); 7693 len = 0; 7694 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7695 if (port != NULL) 7696 len = ctl_create_iid(port, 7697 i % CTL_MAX_INIT_PER_PORT, 7698 res_desc->transport_id); 7699 scsi_ulto4b(len, res_desc->additional_length); 7700 res_desc = (struct scsi_per_res_in_full_desc *) 7701 &res_desc->transport_id[len]; 7702 } 7703 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7704 res_status->header.length); 7705 break; 7706 } 7707 default: 7708 /* 7709 * This is a bug, because we just checked for this above, 7710 * and should have returned an error. 7711 */ 7712 panic("Invalid PR type %x", cdb->action); 7713 break; /* NOTREACHED */ 7714 } 7715 mtx_unlock(&lun->lun_lock); 7716 7717 ctl_set_success(ctsio); 7718 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7719 ctsio->be_move_done = ctl_config_move_done; 7720 ctl_datamove((union ctl_io *)ctsio); 7721 return (CTL_RETVAL_COMPLETE); 7722} 7723 7724/* 7725 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7726 * it should return. 7727 */ 7728static int 7729ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7730 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7731 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7732 struct scsi_per_res_out_parms* param) 7733{ 7734 union ctl_ha_msg persis_io; 7735 int i; 7736 7737 mtx_lock(&lun->lun_lock); 7738 if (sa_res_key == 0) { 7739 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7740 /* validate scope and type */ 7741 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7742 SPR_LU_SCOPE) { 7743 mtx_unlock(&lun->lun_lock); 7744 ctl_set_invalid_field(/*ctsio*/ ctsio, 7745 /*sks_valid*/ 1, 7746 /*command*/ 1, 7747 /*field*/ 2, 7748 /*bit_valid*/ 1, 7749 /*bit*/ 4); 7750 ctl_done((union ctl_io *)ctsio); 7751 return (1); 7752 } 7753 7754 if (type>8 || type==2 || type==4 || type==0) { 7755 mtx_unlock(&lun->lun_lock); 7756 ctl_set_invalid_field(/*ctsio*/ ctsio, 7757 /*sks_valid*/ 1, 7758 /*command*/ 1, 7759 /*field*/ 2, 7760 /*bit_valid*/ 1, 7761 /*bit*/ 0); 7762 ctl_done((union ctl_io *)ctsio); 7763 return (1); 7764 } 7765 7766 /* 7767 * Unregister everybody else and build UA for 7768 * them 7769 */ 7770 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7771 if (i == residx || ctl_get_prkey(lun, i) == 0) 7772 continue; 7773 7774 ctl_clr_prkey(lun, i); 7775 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7776 } 7777 lun->pr_key_count = 1; 7778 lun->res_type = type; 7779 if (lun->res_type != SPR_TYPE_WR_EX_AR 7780 && lun->res_type != SPR_TYPE_EX_AC_AR) 7781 lun->pr_res_idx = residx; 7782 lun->PRGeneration++; 7783 mtx_unlock(&lun->lun_lock); 7784 7785 /* send msg to other side */ 7786 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7787 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7788 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7789 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7790 persis_io.pr.pr_info.res_type = type; 7791 memcpy(persis_io.pr.pr_info.sa_res_key, 7792 param->serv_act_res_key, 7793 sizeof(param->serv_act_res_key)); 7794 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7795 sizeof(persis_io.pr), M_WAITOK); 7796 } else { 7797 /* not all registrants */ 7798 mtx_unlock(&lun->lun_lock); 7799 free(ctsio->kern_data_ptr, M_CTL); 7800 ctl_set_invalid_field(ctsio, 7801 /*sks_valid*/ 1, 7802 /*command*/ 0, 7803 /*field*/ 8, 7804 /*bit_valid*/ 0, 7805 /*bit*/ 0); 7806 ctl_done((union ctl_io *)ctsio); 7807 return (1); 7808 } 7809 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7810 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7811 int found = 0; 7812 7813 if (res_key == sa_res_key) { 7814 /* special case */ 7815 /* 7816 * The spec implies this is not good but doesn't 7817 * say what to do. There are two choices either 7818 * generate a res conflict or check condition 7819 * with illegal field in parameter data. Since 7820 * that is what is done when the sa_res_key is 7821 * zero I'll take that approach since this has 7822 * to do with the sa_res_key. 7823 */ 7824 mtx_unlock(&lun->lun_lock); 7825 free(ctsio->kern_data_ptr, M_CTL); 7826 ctl_set_invalid_field(ctsio, 7827 /*sks_valid*/ 1, 7828 /*command*/ 0, 7829 /*field*/ 8, 7830 /*bit_valid*/ 0, 7831 /*bit*/ 0); 7832 ctl_done((union ctl_io *)ctsio); 7833 return (1); 7834 } 7835 7836 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7837 if (ctl_get_prkey(lun, i) != sa_res_key) 7838 continue; 7839 7840 found = 1; 7841 ctl_clr_prkey(lun, i); 7842 lun->pr_key_count--; 7843 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7844 } 7845 if (!found) { 7846 mtx_unlock(&lun->lun_lock); 7847 free(ctsio->kern_data_ptr, M_CTL); 7848 ctl_set_reservation_conflict(ctsio); 7849 ctl_done((union ctl_io *)ctsio); 7850 return (CTL_RETVAL_COMPLETE); 7851 } 7852 lun->PRGeneration++; 7853 mtx_unlock(&lun->lun_lock); 7854 7855 /* send msg to other side */ 7856 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7857 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7858 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7859 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7860 persis_io.pr.pr_info.res_type = type; 7861 memcpy(persis_io.pr.pr_info.sa_res_key, 7862 param->serv_act_res_key, 7863 sizeof(param->serv_act_res_key)); 7864 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7865 sizeof(persis_io.pr), M_WAITOK); 7866 } else { 7867 /* Reserved but not all registrants */ 7868 /* sa_res_key is res holder */ 7869 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7870 /* validate scope and type */ 7871 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7872 SPR_LU_SCOPE) { 7873 mtx_unlock(&lun->lun_lock); 7874 ctl_set_invalid_field(/*ctsio*/ ctsio, 7875 /*sks_valid*/ 1, 7876 /*command*/ 1, 7877 /*field*/ 2, 7878 /*bit_valid*/ 1, 7879 /*bit*/ 4); 7880 ctl_done((union ctl_io *)ctsio); 7881 return (1); 7882 } 7883 7884 if (type>8 || type==2 || type==4 || type==0) { 7885 mtx_unlock(&lun->lun_lock); 7886 ctl_set_invalid_field(/*ctsio*/ ctsio, 7887 /*sks_valid*/ 1, 7888 /*command*/ 1, 7889 /*field*/ 2, 7890 /*bit_valid*/ 1, 7891 /*bit*/ 0); 7892 ctl_done((union ctl_io *)ctsio); 7893 return (1); 7894 } 7895 7896 /* 7897 * Do the following: 7898 * if sa_res_key != res_key remove all 7899 * registrants w/sa_res_key and generate UA 7900 * for these registrants(Registrations 7901 * Preempted) if it wasn't an exclusive 7902 * reservation generate UA(Reservations 7903 * Preempted) for all other registered nexuses 7904 * if the type has changed. Establish the new 7905 * reservation and holder. If res_key and 7906 * sa_res_key are the same do the above 7907 * except don't unregister the res holder. 7908 */ 7909 7910 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7911 if (i == residx || ctl_get_prkey(lun, i) == 0) 7912 continue; 7913 7914 if (sa_res_key == ctl_get_prkey(lun, i)) { 7915 ctl_clr_prkey(lun, i); 7916 lun->pr_key_count--; 7917 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7918 } else if (type != lun->res_type 7919 && (lun->res_type == SPR_TYPE_WR_EX_RO 7920 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 7921 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 7922 } 7923 } 7924 lun->res_type = type; 7925 if (lun->res_type != SPR_TYPE_WR_EX_AR 7926 && lun->res_type != SPR_TYPE_EX_AC_AR) 7927 lun->pr_res_idx = residx; 7928 else 7929 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7930 lun->PRGeneration++; 7931 mtx_unlock(&lun->lun_lock); 7932 7933 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7934 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7935 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7936 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7937 persis_io.pr.pr_info.res_type = type; 7938 memcpy(persis_io.pr.pr_info.sa_res_key, 7939 param->serv_act_res_key, 7940 sizeof(param->serv_act_res_key)); 7941 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7942 sizeof(persis_io.pr), M_WAITOK); 7943 } else { 7944 /* 7945 * sa_res_key is not the res holder just 7946 * remove registrants 7947 */ 7948 int found=0; 7949 7950 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7951 if (sa_res_key != ctl_get_prkey(lun, i)) 7952 continue; 7953 7954 found = 1; 7955 ctl_clr_prkey(lun, i); 7956 lun->pr_key_count--; 7957 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7958 } 7959 7960 if (!found) { 7961 mtx_unlock(&lun->lun_lock); 7962 free(ctsio->kern_data_ptr, M_CTL); 7963 ctl_set_reservation_conflict(ctsio); 7964 ctl_done((union ctl_io *)ctsio); 7965 return (1); 7966 } 7967 lun->PRGeneration++; 7968 mtx_unlock(&lun->lun_lock); 7969 7970 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7971 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7972 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7973 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7974 persis_io.pr.pr_info.res_type = type; 7975 memcpy(persis_io.pr.pr_info.sa_res_key, 7976 param->serv_act_res_key, 7977 sizeof(param->serv_act_res_key)); 7978 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7979 sizeof(persis_io.pr), M_WAITOK); 7980 } 7981 } 7982 return (0); 7983} 7984 7985static void 7986ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 7987{ 7988 uint64_t sa_res_key; 7989 int i; 7990 7991 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 7992 7993 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7994 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 7995 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 7996 if (sa_res_key == 0) { 7997 /* 7998 * Unregister everybody else and build UA for 7999 * them 8000 */ 8001 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8002 if (i == msg->pr.pr_info.residx || 8003 ctl_get_prkey(lun, i) == 0) 8004 continue; 8005 8006 ctl_clr_prkey(lun, i); 8007 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8008 } 8009 8010 lun->pr_key_count = 1; 8011 lun->res_type = msg->pr.pr_info.res_type; 8012 if (lun->res_type != SPR_TYPE_WR_EX_AR 8013 && lun->res_type != SPR_TYPE_EX_AC_AR) 8014 lun->pr_res_idx = msg->pr.pr_info.residx; 8015 } else { 8016 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8017 if (sa_res_key == ctl_get_prkey(lun, i)) 8018 continue; 8019 8020 ctl_clr_prkey(lun, i); 8021 lun->pr_key_count--; 8022 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8023 } 8024 } 8025 } else { 8026 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8027 if (i == msg->pr.pr_info.residx || 8028 ctl_get_prkey(lun, i) == 0) 8029 continue; 8030 8031 if (sa_res_key == ctl_get_prkey(lun, i)) { 8032 ctl_clr_prkey(lun, i); 8033 lun->pr_key_count--; 8034 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8035 } else if (msg->pr.pr_info.res_type != lun->res_type 8036 && (lun->res_type == SPR_TYPE_WR_EX_RO 8037 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8038 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8039 } 8040 } 8041 lun->res_type = msg->pr.pr_info.res_type; 8042 if (lun->res_type != SPR_TYPE_WR_EX_AR 8043 && lun->res_type != SPR_TYPE_EX_AC_AR) 8044 lun->pr_res_idx = msg->pr.pr_info.residx; 8045 else 8046 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8047 } 8048 lun->PRGeneration++; 8049 8050} 8051 8052 8053int 8054ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8055{ 8056 int retval; 8057 u_int32_t param_len; 8058 struct scsi_per_res_out *cdb; 8059 struct ctl_lun *lun; 8060 struct scsi_per_res_out_parms* param; 8061 struct ctl_softc *softc; 8062 uint32_t residx; 8063 uint64_t res_key, sa_res_key, key; 8064 uint8_t type; 8065 union ctl_ha_msg persis_io; 8066 int i; 8067 8068 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8069 8070 retval = CTL_RETVAL_COMPLETE; 8071 8072 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8073 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8074 softc = lun->ctl_softc; 8075 8076 /* 8077 * We only support whole-LUN scope. The scope & type are ignored for 8078 * register, register and ignore existing key and clear. 8079 * We sometimes ignore scope and type on preempts too!! 8080 * Verify reservation type here as well. 8081 */ 8082 type = cdb->scope_type & SPR_TYPE_MASK; 8083 if ((cdb->action == SPRO_RESERVE) 8084 || (cdb->action == SPRO_RELEASE)) { 8085 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8086 ctl_set_invalid_field(/*ctsio*/ ctsio, 8087 /*sks_valid*/ 1, 8088 /*command*/ 1, 8089 /*field*/ 2, 8090 /*bit_valid*/ 1, 8091 /*bit*/ 4); 8092 ctl_done((union ctl_io *)ctsio); 8093 return (CTL_RETVAL_COMPLETE); 8094 } 8095 8096 if (type>8 || type==2 || type==4 || type==0) { 8097 ctl_set_invalid_field(/*ctsio*/ ctsio, 8098 /*sks_valid*/ 1, 8099 /*command*/ 1, 8100 /*field*/ 2, 8101 /*bit_valid*/ 1, 8102 /*bit*/ 0); 8103 ctl_done((union ctl_io *)ctsio); 8104 return (CTL_RETVAL_COMPLETE); 8105 } 8106 } 8107 8108 param_len = scsi_4btoul(cdb->length); 8109 8110 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8111 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8112 ctsio->kern_data_len = param_len; 8113 ctsio->kern_total_len = param_len; 8114 ctsio->kern_data_resid = 0; 8115 ctsio->kern_rel_offset = 0; 8116 ctsio->kern_sg_entries = 0; 8117 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8118 ctsio->be_move_done = ctl_config_move_done; 8119 ctl_datamove((union ctl_io *)ctsio); 8120 8121 return (CTL_RETVAL_COMPLETE); 8122 } 8123 8124 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8125 8126 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8127 res_key = scsi_8btou64(param->res_key.key); 8128 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8129 8130 /* 8131 * Validate the reservation key here except for SPRO_REG_IGNO 8132 * This must be done for all other service actions 8133 */ 8134 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8135 mtx_lock(&lun->lun_lock); 8136 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8137 if (res_key != key) { 8138 /* 8139 * The current key passed in doesn't match 8140 * the one the initiator previously 8141 * registered. 8142 */ 8143 mtx_unlock(&lun->lun_lock); 8144 free(ctsio->kern_data_ptr, M_CTL); 8145 ctl_set_reservation_conflict(ctsio); 8146 ctl_done((union ctl_io *)ctsio); 8147 return (CTL_RETVAL_COMPLETE); 8148 } 8149 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8150 /* 8151 * We are not registered 8152 */ 8153 mtx_unlock(&lun->lun_lock); 8154 free(ctsio->kern_data_ptr, M_CTL); 8155 ctl_set_reservation_conflict(ctsio); 8156 ctl_done((union ctl_io *)ctsio); 8157 return (CTL_RETVAL_COMPLETE); 8158 } else if (res_key != 0) { 8159 /* 8160 * We are not registered and trying to register but 8161 * the register key isn't zero. 8162 */ 8163 mtx_unlock(&lun->lun_lock); 8164 free(ctsio->kern_data_ptr, M_CTL); 8165 ctl_set_reservation_conflict(ctsio); 8166 ctl_done((union ctl_io *)ctsio); 8167 return (CTL_RETVAL_COMPLETE); 8168 } 8169 mtx_unlock(&lun->lun_lock); 8170 } 8171 8172 switch (cdb->action & SPRO_ACTION_MASK) { 8173 case SPRO_REGISTER: 8174 case SPRO_REG_IGNO: { 8175 8176#if 0 8177 printf("Registration received\n"); 8178#endif 8179 8180 /* 8181 * We don't support any of these options, as we report in 8182 * the read capabilities request (see 8183 * ctl_persistent_reserve_in(), above). 8184 */ 8185 if ((param->flags & SPR_SPEC_I_PT) 8186 || (param->flags & SPR_ALL_TG_PT) 8187 || (param->flags & SPR_APTPL)) { 8188 int bit_ptr; 8189 8190 if (param->flags & SPR_APTPL) 8191 bit_ptr = 0; 8192 else if (param->flags & SPR_ALL_TG_PT) 8193 bit_ptr = 2; 8194 else /* SPR_SPEC_I_PT */ 8195 bit_ptr = 3; 8196 8197 free(ctsio->kern_data_ptr, M_CTL); 8198 ctl_set_invalid_field(ctsio, 8199 /*sks_valid*/ 1, 8200 /*command*/ 0, 8201 /*field*/ 20, 8202 /*bit_valid*/ 1, 8203 /*bit*/ bit_ptr); 8204 ctl_done((union ctl_io *)ctsio); 8205 return (CTL_RETVAL_COMPLETE); 8206 } 8207 8208 mtx_lock(&lun->lun_lock); 8209 8210 /* 8211 * The initiator wants to clear the 8212 * key/unregister. 8213 */ 8214 if (sa_res_key == 0) { 8215 if ((res_key == 0 8216 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8217 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8218 && ctl_get_prkey(lun, residx) == 0)) { 8219 mtx_unlock(&lun->lun_lock); 8220 goto done; 8221 } 8222 8223 ctl_clr_prkey(lun, residx); 8224 lun->pr_key_count--; 8225 8226 if (residx == lun->pr_res_idx) { 8227 lun->flags &= ~CTL_LUN_PR_RESERVED; 8228 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8229 8230 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8231 || lun->res_type == SPR_TYPE_EX_AC_RO) 8232 && lun->pr_key_count) { 8233 /* 8234 * If the reservation is a registrants 8235 * only type we need to generate a UA 8236 * for other registered inits. The 8237 * sense code should be RESERVATIONS 8238 * RELEASED 8239 */ 8240 8241 for (i = softc->init_min; i < softc->init_max; i++){ 8242 if (ctl_get_prkey(lun, i) == 0) 8243 continue; 8244 ctl_est_ua(lun, i, 8245 CTL_UA_RES_RELEASE); 8246 } 8247 } 8248 lun->res_type = 0; 8249 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8250 if (lun->pr_key_count==0) { 8251 lun->flags &= ~CTL_LUN_PR_RESERVED; 8252 lun->res_type = 0; 8253 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8254 } 8255 } 8256 lun->PRGeneration++; 8257 mtx_unlock(&lun->lun_lock); 8258 8259 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8260 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8261 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8262 persis_io.pr.pr_info.residx = residx; 8263 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8264 sizeof(persis_io.pr), M_WAITOK); 8265 } else /* sa_res_key != 0 */ { 8266 8267 /* 8268 * If we aren't registered currently then increment 8269 * the key count and set the registered flag. 8270 */ 8271 ctl_alloc_prkey(lun, residx); 8272 if (ctl_get_prkey(lun, residx) == 0) 8273 lun->pr_key_count++; 8274 ctl_set_prkey(lun, residx, sa_res_key); 8275 lun->PRGeneration++; 8276 mtx_unlock(&lun->lun_lock); 8277 8278 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8279 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8280 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8281 persis_io.pr.pr_info.residx = residx; 8282 memcpy(persis_io.pr.pr_info.sa_res_key, 8283 param->serv_act_res_key, 8284 sizeof(param->serv_act_res_key)); 8285 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8286 sizeof(persis_io.pr), M_WAITOK); 8287 } 8288 8289 break; 8290 } 8291 case SPRO_RESERVE: 8292#if 0 8293 printf("Reserve executed type %d\n", type); 8294#endif 8295 mtx_lock(&lun->lun_lock); 8296 if (lun->flags & CTL_LUN_PR_RESERVED) { 8297 /* 8298 * if this isn't the reservation holder and it's 8299 * not a "all registrants" type or if the type is 8300 * different then we have a conflict 8301 */ 8302 if ((lun->pr_res_idx != residx 8303 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8304 || lun->res_type != type) { 8305 mtx_unlock(&lun->lun_lock); 8306 free(ctsio->kern_data_ptr, M_CTL); 8307 ctl_set_reservation_conflict(ctsio); 8308 ctl_done((union ctl_io *)ctsio); 8309 return (CTL_RETVAL_COMPLETE); 8310 } 8311 mtx_unlock(&lun->lun_lock); 8312 } else /* create a reservation */ { 8313 /* 8314 * If it's not an "all registrants" type record 8315 * reservation holder 8316 */ 8317 if (type != SPR_TYPE_WR_EX_AR 8318 && type != SPR_TYPE_EX_AC_AR) 8319 lun->pr_res_idx = residx; /* Res holder */ 8320 else 8321 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8322 8323 lun->flags |= CTL_LUN_PR_RESERVED; 8324 lun->res_type = type; 8325 8326 mtx_unlock(&lun->lun_lock); 8327 8328 /* send msg to other side */ 8329 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8330 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8331 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8332 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8333 persis_io.pr.pr_info.res_type = type; 8334 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8335 sizeof(persis_io.pr), M_WAITOK); 8336 } 8337 break; 8338 8339 case SPRO_RELEASE: 8340 mtx_lock(&lun->lun_lock); 8341 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8342 /* No reservation exists return good status */ 8343 mtx_unlock(&lun->lun_lock); 8344 goto done; 8345 } 8346 /* 8347 * Is this nexus a reservation holder? 8348 */ 8349 if (lun->pr_res_idx != residx 8350 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8351 /* 8352 * not a res holder return good status but 8353 * do nothing 8354 */ 8355 mtx_unlock(&lun->lun_lock); 8356 goto done; 8357 } 8358 8359 if (lun->res_type != type) { 8360 mtx_unlock(&lun->lun_lock); 8361 free(ctsio->kern_data_ptr, M_CTL); 8362 ctl_set_illegal_pr_release(ctsio); 8363 ctl_done((union ctl_io *)ctsio); 8364 return (CTL_RETVAL_COMPLETE); 8365 } 8366 8367 /* okay to release */ 8368 lun->flags &= ~CTL_LUN_PR_RESERVED; 8369 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8370 lun->res_type = 0; 8371 8372 /* 8373 * if this isn't an exclusive access 8374 * res generate UA for all other 8375 * registrants. 8376 */ 8377 if (type != SPR_TYPE_EX_AC 8378 && type != SPR_TYPE_WR_EX) { 8379 for (i = softc->init_min; i < softc->init_max; i++) { 8380 if (i == residx || ctl_get_prkey(lun, i) == 0) 8381 continue; 8382 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8383 } 8384 } 8385 mtx_unlock(&lun->lun_lock); 8386 8387 /* Send msg to other side */ 8388 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8389 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8390 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8391 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8392 sizeof(persis_io.pr), M_WAITOK); 8393 break; 8394 8395 case SPRO_CLEAR: 8396 /* send msg to other side */ 8397 8398 mtx_lock(&lun->lun_lock); 8399 lun->flags &= ~CTL_LUN_PR_RESERVED; 8400 lun->res_type = 0; 8401 lun->pr_key_count = 0; 8402 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8403 8404 ctl_clr_prkey(lun, residx); 8405 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8406 if (ctl_get_prkey(lun, i) != 0) { 8407 ctl_clr_prkey(lun, i); 8408 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8409 } 8410 lun->PRGeneration++; 8411 mtx_unlock(&lun->lun_lock); 8412 8413 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8414 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8415 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8416 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8417 sizeof(persis_io.pr), M_WAITOK); 8418 break; 8419 8420 case SPRO_PREEMPT: 8421 case SPRO_PRE_ABO: { 8422 int nretval; 8423 8424 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8425 residx, ctsio, cdb, param); 8426 if (nretval != 0) 8427 return (CTL_RETVAL_COMPLETE); 8428 break; 8429 } 8430 default: 8431 panic("Invalid PR type %x", cdb->action); 8432 } 8433 8434done: 8435 free(ctsio->kern_data_ptr, M_CTL); 8436 ctl_set_success(ctsio); 8437 ctl_done((union ctl_io *)ctsio); 8438 8439 return (retval); 8440} 8441 8442/* 8443 * This routine is for handling a message from the other SC pertaining to 8444 * persistent reserve out. All the error checking will have been done 8445 * so only perorming the action need be done here to keep the two 8446 * in sync. 8447 */ 8448static void 8449ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8450{ 8451 struct ctl_lun *lun; 8452 struct ctl_softc *softc; 8453 int i; 8454 uint32_t residx, targ_lun; 8455 8456 softc = control_softc; 8457 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8458 mtx_lock(&softc->ctl_lock); 8459 if ((targ_lun >= CTL_MAX_LUNS) || 8460 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 8461 mtx_unlock(&softc->ctl_lock); 8462 return; 8463 } 8464 mtx_lock(&lun->lun_lock); 8465 mtx_unlock(&softc->ctl_lock); 8466 if (lun->flags & CTL_LUN_DISABLED) { 8467 mtx_unlock(&lun->lun_lock); 8468 return; 8469 } 8470 residx = ctl_get_initindex(&msg->hdr.nexus); 8471 switch(msg->pr.pr_info.action) { 8472 case CTL_PR_REG_KEY: 8473 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8474 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8475 lun->pr_key_count++; 8476 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8477 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8478 lun->PRGeneration++; 8479 break; 8480 8481 case CTL_PR_UNREG_KEY: 8482 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8483 lun->pr_key_count--; 8484 8485 /* XXX Need to see if the reservation has been released */ 8486 /* if so do we need to generate UA? */ 8487 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8488 lun->flags &= ~CTL_LUN_PR_RESERVED; 8489 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8490 8491 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8492 || lun->res_type == SPR_TYPE_EX_AC_RO) 8493 && lun->pr_key_count) { 8494 /* 8495 * If the reservation is a registrants 8496 * only type we need to generate a UA 8497 * for other registered inits. The 8498 * sense code should be RESERVATIONS 8499 * RELEASED 8500 */ 8501 8502 for (i = softc->init_min; i < softc->init_max; i++) { 8503 if (ctl_get_prkey(lun, i) == 0) 8504 continue; 8505 8506 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8507 } 8508 } 8509 lun->res_type = 0; 8510 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8511 if (lun->pr_key_count==0) { 8512 lun->flags &= ~CTL_LUN_PR_RESERVED; 8513 lun->res_type = 0; 8514 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8515 } 8516 } 8517 lun->PRGeneration++; 8518 break; 8519 8520 case CTL_PR_RESERVE: 8521 lun->flags |= CTL_LUN_PR_RESERVED; 8522 lun->res_type = msg->pr.pr_info.res_type; 8523 lun->pr_res_idx = msg->pr.pr_info.residx; 8524 8525 break; 8526 8527 case CTL_PR_RELEASE: 8528 /* 8529 * if this isn't an exclusive access res generate UA for all 8530 * other registrants. 8531 */ 8532 if (lun->res_type != SPR_TYPE_EX_AC 8533 && lun->res_type != SPR_TYPE_WR_EX) { 8534 for (i = softc->init_min; i < softc->init_max; i++) 8535 if (i == residx || ctl_get_prkey(lun, i) == 0) 8536 continue; 8537 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8538 } 8539 8540 lun->flags &= ~CTL_LUN_PR_RESERVED; 8541 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8542 lun->res_type = 0; 8543 break; 8544 8545 case CTL_PR_PREEMPT: 8546 ctl_pro_preempt_other(lun, msg); 8547 break; 8548 case CTL_PR_CLEAR: 8549 lun->flags &= ~CTL_LUN_PR_RESERVED; 8550 lun->res_type = 0; 8551 lun->pr_key_count = 0; 8552 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8553 8554 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8555 if (ctl_get_prkey(lun, i) == 0) 8556 continue; 8557 ctl_clr_prkey(lun, i); 8558 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8559 } 8560 lun->PRGeneration++; 8561 break; 8562 } 8563 8564 mtx_unlock(&lun->lun_lock); 8565} 8566 8567int 8568ctl_read_write(struct ctl_scsiio *ctsio) 8569{ 8570 struct ctl_lun *lun; 8571 struct ctl_lba_len_flags *lbalen; 8572 uint64_t lba; 8573 uint32_t num_blocks; 8574 int flags, retval; 8575 int isread; 8576 8577 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8578 8579 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8580 8581 flags = 0; 8582 retval = CTL_RETVAL_COMPLETE; 8583 8584 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8585 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8586 switch (ctsio->cdb[0]) { 8587 case READ_6: 8588 case WRITE_6: { 8589 struct scsi_rw_6 *cdb; 8590 8591 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8592 8593 lba = scsi_3btoul(cdb->addr); 8594 /* only 5 bits are valid in the most significant address byte */ 8595 lba &= 0x1fffff; 8596 num_blocks = cdb->length; 8597 /* 8598 * This is correct according to SBC-2. 8599 */ 8600 if (num_blocks == 0) 8601 num_blocks = 256; 8602 break; 8603 } 8604 case READ_10: 8605 case WRITE_10: { 8606 struct scsi_rw_10 *cdb; 8607 8608 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8609 if (cdb->byte2 & SRW10_FUA) 8610 flags |= CTL_LLF_FUA; 8611 if (cdb->byte2 & SRW10_DPO) 8612 flags |= CTL_LLF_DPO; 8613 lba = scsi_4btoul(cdb->addr); 8614 num_blocks = scsi_2btoul(cdb->length); 8615 break; 8616 } 8617 case WRITE_VERIFY_10: { 8618 struct scsi_write_verify_10 *cdb; 8619 8620 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8621 flags |= CTL_LLF_FUA; 8622 if (cdb->byte2 & SWV_DPO) 8623 flags |= CTL_LLF_DPO; 8624 lba = scsi_4btoul(cdb->addr); 8625 num_blocks = scsi_2btoul(cdb->length); 8626 break; 8627 } 8628 case READ_12: 8629 case WRITE_12: { 8630 struct scsi_rw_12 *cdb; 8631 8632 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8633 if (cdb->byte2 & SRW12_FUA) 8634 flags |= CTL_LLF_FUA; 8635 if (cdb->byte2 & SRW12_DPO) 8636 flags |= CTL_LLF_DPO; 8637 lba = scsi_4btoul(cdb->addr); 8638 num_blocks = scsi_4btoul(cdb->length); 8639 break; 8640 } 8641 case WRITE_VERIFY_12: { 8642 struct scsi_write_verify_12 *cdb; 8643 8644 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8645 flags |= CTL_LLF_FUA; 8646 if (cdb->byte2 & SWV_DPO) 8647 flags |= CTL_LLF_DPO; 8648 lba = scsi_4btoul(cdb->addr); 8649 num_blocks = scsi_4btoul(cdb->length); 8650 break; 8651 } 8652 case READ_16: 8653 case WRITE_16: { 8654 struct scsi_rw_16 *cdb; 8655 8656 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8657 if (cdb->byte2 & SRW12_FUA) 8658 flags |= CTL_LLF_FUA; 8659 if (cdb->byte2 & SRW12_DPO) 8660 flags |= CTL_LLF_DPO; 8661 lba = scsi_8btou64(cdb->addr); 8662 num_blocks = scsi_4btoul(cdb->length); 8663 break; 8664 } 8665 case WRITE_ATOMIC_16: { 8666 struct scsi_rw_16 *cdb; 8667 8668 if (lun->be_lun->atomicblock == 0) { 8669 ctl_set_invalid_opcode(ctsio); 8670 ctl_done((union ctl_io *)ctsio); 8671 return (CTL_RETVAL_COMPLETE); 8672 } 8673 8674 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8675 if (cdb->byte2 & SRW12_FUA) 8676 flags |= CTL_LLF_FUA; 8677 if (cdb->byte2 & SRW12_DPO) 8678 flags |= CTL_LLF_DPO; 8679 lba = scsi_8btou64(cdb->addr); 8680 num_blocks = scsi_4btoul(cdb->length); 8681 if (num_blocks > lun->be_lun->atomicblock) { 8682 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8683 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8684 /*bit*/ 0); 8685 ctl_done((union ctl_io *)ctsio); 8686 return (CTL_RETVAL_COMPLETE); 8687 } 8688 break; 8689 } 8690 case WRITE_VERIFY_16: { 8691 struct scsi_write_verify_16 *cdb; 8692 8693 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8694 flags |= CTL_LLF_FUA; 8695 if (cdb->byte2 & SWV_DPO) 8696 flags |= CTL_LLF_DPO; 8697 lba = scsi_8btou64(cdb->addr); 8698 num_blocks = scsi_4btoul(cdb->length); 8699 break; 8700 } 8701 default: 8702 /* 8703 * We got a command we don't support. This shouldn't 8704 * happen, commands should be filtered out above us. 8705 */ 8706 ctl_set_invalid_opcode(ctsio); 8707 ctl_done((union ctl_io *)ctsio); 8708 8709 return (CTL_RETVAL_COMPLETE); 8710 break; /* NOTREACHED */ 8711 } 8712 8713 /* 8714 * The first check is to make sure we're in bounds, the second 8715 * check is to catch wrap-around problems. If the lba + num blocks 8716 * is less than the lba, then we've wrapped around and the block 8717 * range is invalid anyway. 8718 */ 8719 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8720 || ((lba + num_blocks) < lba)) { 8721 ctl_set_lba_out_of_range(ctsio); 8722 ctl_done((union ctl_io *)ctsio); 8723 return (CTL_RETVAL_COMPLETE); 8724 } 8725 8726 /* 8727 * According to SBC-3, a transfer length of 0 is not an error. 8728 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8729 * translates to 256 blocks for those commands. 8730 */ 8731 if (num_blocks == 0) { 8732 ctl_set_success(ctsio); 8733 ctl_done((union ctl_io *)ctsio); 8734 return (CTL_RETVAL_COMPLETE); 8735 } 8736 8737 /* Set FUA and/or DPO if caches are disabled. */ 8738 if (isread) { 8739 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8740 SCP_RCD) != 0) 8741 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8742 } else { 8743 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8744 SCP_WCE) == 0) 8745 flags |= CTL_LLF_FUA; 8746 } 8747 8748 lbalen = (struct ctl_lba_len_flags *) 8749 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8750 lbalen->lba = lba; 8751 lbalen->len = num_blocks; 8752 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8753 8754 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8755 ctsio->kern_rel_offset = 0; 8756 8757 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8758 8759 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8760 8761 return (retval); 8762} 8763 8764static int 8765ctl_cnw_cont(union ctl_io *io) 8766{ 8767 struct ctl_scsiio *ctsio; 8768 struct ctl_lun *lun; 8769 struct ctl_lba_len_flags *lbalen; 8770 int retval; 8771 8772 ctsio = &io->scsiio; 8773 ctsio->io_hdr.status = CTL_STATUS_NONE; 8774 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8775 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8776 lbalen = (struct ctl_lba_len_flags *) 8777 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8778 lbalen->flags &= ~CTL_LLF_COMPARE; 8779 lbalen->flags |= CTL_LLF_WRITE; 8780 8781 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8782 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8783 return (retval); 8784} 8785 8786int 8787ctl_cnw(struct ctl_scsiio *ctsio) 8788{ 8789 struct ctl_lun *lun; 8790 struct ctl_lba_len_flags *lbalen; 8791 uint64_t lba; 8792 uint32_t num_blocks; 8793 int flags, retval; 8794 8795 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8796 8797 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8798 8799 flags = 0; 8800 retval = CTL_RETVAL_COMPLETE; 8801 8802 switch (ctsio->cdb[0]) { 8803 case COMPARE_AND_WRITE: { 8804 struct scsi_compare_and_write *cdb; 8805 8806 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8807 if (cdb->byte2 & SRW10_FUA) 8808 flags |= CTL_LLF_FUA; 8809 if (cdb->byte2 & SRW10_DPO) 8810 flags |= CTL_LLF_DPO; 8811 lba = scsi_8btou64(cdb->addr); 8812 num_blocks = cdb->length; 8813 break; 8814 } 8815 default: 8816 /* 8817 * We got a command we don't support. This shouldn't 8818 * happen, commands should be filtered out above us. 8819 */ 8820 ctl_set_invalid_opcode(ctsio); 8821 ctl_done((union ctl_io *)ctsio); 8822 8823 return (CTL_RETVAL_COMPLETE); 8824 break; /* NOTREACHED */ 8825 } 8826 8827 /* 8828 * The first check is to make sure we're in bounds, the second 8829 * check is to catch wrap-around problems. If the lba + num blocks 8830 * is less than the lba, then we've wrapped around and the block 8831 * range is invalid anyway. 8832 */ 8833 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8834 || ((lba + num_blocks) < lba)) { 8835 ctl_set_lba_out_of_range(ctsio); 8836 ctl_done((union ctl_io *)ctsio); 8837 return (CTL_RETVAL_COMPLETE); 8838 } 8839 8840 /* 8841 * According to SBC-3, a transfer length of 0 is not an error. 8842 */ 8843 if (num_blocks == 0) { 8844 ctl_set_success(ctsio); 8845 ctl_done((union ctl_io *)ctsio); 8846 return (CTL_RETVAL_COMPLETE); 8847 } 8848 8849 /* Set FUA if write cache is disabled. */ 8850 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8851 SCP_WCE) == 0) 8852 flags |= CTL_LLF_FUA; 8853 8854 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8855 ctsio->kern_rel_offset = 0; 8856 8857 /* 8858 * Set the IO_CONT flag, so that if this I/O gets passed to 8859 * ctl_data_submit_done(), it'll get passed back to 8860 * ctl_ctl_cnw_cont() for further processing. 8861 */ 8862 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8863 ctsio->io_cont = ctl_cnw_cont; 8864 8865 lbalen = (struct ctl_lba_len_flags *) 8866 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8867 lbalen->lba = lba; 8868 lbalen->len = num_blocks; 8869 lbalen->flags = CTL_LLF_COMPARE | flags; 8870 8871 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8872 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8873 return (retval); 8874} 8875 8876int 8877ctl_verify(struct ctl_scsiio *ctsio) 8878{ 8879 struct ctl_lun *lun; 8880 struct ctl_lba_len_flags *lbalen; 8881 uint64_t lba; 8882 uint32_t num_blocks; 8883 int bytchk, flags; 8884 int retval; 8885 8886 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8887 8888 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8889 8890 bytchk = 0; 8891 flags = CTL_LLF_FUA; 8892 retval = CTL_RETVAL_COMPLETE; 8893 8894 switch (ctsio->cdb[0]) { 8895 case VERIFY_10: { 8896 struct scsi_verify_10 *cdb; 8897 8898 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8899 if (cdb->byte2 & SVFY_BYTCHK) 8900 bytchk = 1; 8901 if (cdb->byte2 & SVFY_DPO) 8902 flags |= CTL_LLF_DPO; 8903 lba = scsi_4btoul(cdb->addr); 8904 num_blocks = scsi_2btoul(cdb->length); 8905 break; 8906 } 8907 case VERIFY_12: { 8908 struct scsi_verify_12 *cdb; 8909 8910 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8911 if (cdb->byte2 & SVFY_BYTCHK) 8912 bytchk = 1; 8913 if (cdb->byte2 & SVFY_DPO) 8914 flags |= CTL_LLF_DPO; 8915 lba = scsi_4btoul(cdb->addr); 8916 num_blocks = scsi_4btoul(cdb->length); 8917 break; 8918 } 8919 case VERIFY_16: { 8920 struct scsi_rw_16 *cdb; 8921 8922 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8923 if (cdb->byte2 & SVFY_BYTCHK) 8924 bytchk = 1; 8925 if (cdb->byte2 & SVFY_DPO) 8926 flags |= CTL_LLF_DPO; 8927 lba = scsi_8btou64(cdb->addr); 8928 num_blocks = scsi_4btoul(cdb->length); 8929 break; 8930 } 8931 default: 8932 /* 8933 * We got a command we don't support. This shouldn't 8934 * happen, commands should be filtered out above us. 8935 */ 8936 ctl_set_invalid_opcode(ctsio); 8937 ctl_done((union ctl_io *)ctsio); 8938 return (CTL_RETVAL_COMPLETE); 8939 } 8940 8941 /* 8942 * The first check is to make sure we're in bounds, the second 8943 * check is to catch wrap-around problems. If the lba + num blocks 8944 * is less than the lba, then we've wrapped around and the block 8945 * range is invalid anyway. 8946 */ 8947 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8948 || ((lba + num_blocks) < lba)) { 8949 ctl_set_lba_out_of_range(ctsio); 8950 ctl_done((union ctl_io *)ctsio); 8951 return (CTL_RETVAL_COMPLETE); 8952 } 8953 8954 /* 8955 * According to SBC-3, a transfer length of 0 is not an error. 8956 */ 8957 if (num_blocks == 0) { 8958 ctl_set_success(ctsio); 8959 ctl_done((union ctl_io *)ctsio); 8960 return (CTL_RETVAL_COMPLETE); 8961 } 8962 8963 lbalen = (struct ctl_lba_len_flags *) 8964 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8965 lbalen->lba = lba; 8966 lbalen->len = num_blocks; 8967 if (bytchk) { 8968 lbalen->flags = CTL_LLF_COMPARE | flags; 8969 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8970 } else { 8971 lbalen->flags = CTL_LLF_VERIFY | flags; 8972 ctsio->kern_total_len = 0; 8973 } 8974 ctsio->kern_rel_offset = 0; 8975 8976 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 8977 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8978 return (retval); 8979} 8980 8981int 8982ctl_report_luns(struct ctl_scsiio *ctsio) 8983{ 8984 struct ctl_softc *softc = control_softc; 8985 struct scsi_report_luns *cdb; 8986 struct scsi_report_luns_data *lun_data; 8987 struct ctl_lun *lun, *request_lun; 8988 struct ctl_port *port; 8989 int num_luns, retval; 8990 uint32_t alloc_len, lun_datalen; 8991 int num_filled, well_known; 8992 uint32_t initidx, targ_lun_id, lun_id; 8993 8994 retval = CTL_RETVAL_COMPLETE; 8995 well_known = 0; 8996 8997 cdb = (struct scsi_report_luns *)ctsio->cdb; 8998 port = ctl_io_port(&ctsio->io_hdr); 8999 9000 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9001 9002 mtx_lock(&softc->ctl_lock); 9003 num_luns = 0; 9004 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 9005 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 9006 num_luns++; 9007 } 9008 mtx_unlock(&softc->ctl_lock); 9009 9010 switch (cdb->select_report) { 9011 case RPL_REPORT_DEFAULT: 9012 case RPL_REPORT_ALL: 9013 break; 9014 case RPL_REPORT_WELLKNOWN: 9015 well_known = 1; 9016 num_luns = 0; 9017 break; 9018 default: 9019 ctl_set_invalid_field(ctsio, 9020 /*sks_valid*/ 1, 9021 /*command*/ 1, 9022 /*field*/ 2, 9023 /*bit_valid*/ 0, 9024 /*bit*/ 0); 9025 ctl_done((union ctl_io *)ctsio); 9026 return (retval); 9027 break; /* NOTREACHED */ 9028 } 9029 9030 alloc_len = scsi_4btoul(cdb->length); 9031 /* 9032 * The initiator has to allocate at least 16 bytes for this request, 9033 * so he can at least get the header and the first LUN. Otherwise 9034 * we reject the request (per SPC-3 rev 14, section 6.21). 9035 */ 9036 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9037 sizeof(struct scsi_report_luns_lundata))) { 9038 ctl_set_invalid_field(ctsio, 9039 /*sks_valid*/ 1, 9040 /*command*/ 1, 9041 /*field*/ 6, 9042 /*bit_valid*/ 0, 9043 /*bit*/ 0); 9044 ctl_done((union ctl_io *)ctsio); 9045 return (retval); 9046 } 9047 9048 request_lun = (struct ctl_lun *) 9049 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9050 9051 lun_datalen = sizeof(*lun_data) + 9052 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9053 9054 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9055 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9056 ctsio->kern_sg_entries = 0; 9057 9058 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9059 9060 mtx_lock(&softc->ctl_lock); 9061 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9062 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9063 if (lun_id >= CTL_MAX_LUNS) 9064 continue; 9065 lun = softc->ctl_luns[lun_id]; 9066 if (lun == NULL) 9067 continue; 9068 9069 if (targ_lun_id <= 0xff) { 9070 /* 9071 * Peripheral addressing method, bus number 0. 9072 */ 9073 lun_data->luns[num_filled].lundata[0] = 9074 RPL_LUNDATA_ATYP_PERIPH; 9075 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9076 num_filled++; 9077 } else if (targ_lun_id <= 0x3fff) { 9078 /* 9079 * Flat addressing method. 9080 */ 9081 lun_data->luns[num_filled].lundata[0] = 9082 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9083 lun_data->luns[num_filled].lundata[1] = 9084 (targ_lun_id & 0xff); 9085 num_filled++; 9086 } else if (targ_lun_id <= 0xffffff) { 9087 /* 9088 * Extended flat addressing method. 9089 */ 9090 lun_data->luns[num_filled].lundata[0] = 9091 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9092 scsi_ulto3b(targ_lun_id, 9093 &lun_data->luns[num_filled].lundata[1]); 9094 num_filled++; 9095 } else { 9096 printf("ctl_report_luns: bogus LUN number %jd, " 9097 "skipping\n", (intmax_t)targ_lun_id); 9098 } 9099 /* 9100 * According to SPC-3, rev 14 section 6.21: 9101 * 9102 * "The execution of a REPORT LUNS command to any valid and 9103 * installed logical unit shall clear the REPORTED LUNS DATA 9104 * HAS CHANGED unit attention condition for all logical 9105 * units of that target with respect to the requesting 9106 * initiator. A valid and installed logical unit is one 9107 * having a PERIPHERAL QUALIFIER of 000b in the standard 9108 * INQUIRY data (see 6.4.2)." 9109 * 9110 * If request_lun is NULL, the LUN this report luns command 9111 * was issued to is either disabled or doesn't exist. In that 9112 * case, we shouldn't clear any pending lun change unit 9113 * attention. 9114 */ 9115 if (request_lun != NULL) { 9116 mtx_lock(&lun->lun_lock); 9117 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9118 mtx_unlock(&lun->lun_lock); 9119 } 9120 } 9121 mtx_unlock(&softc->ctl_lock); 9122 9123 /* 9124 * It's quite possible that we've returned fewer LUNs than we allocated 9125 * space for. Trim it. 9126 */ 9127 lun_datalen = sizeof(*lun_data) + 9128 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9129 9130 if (lun_datalen < alloc_len) { 9131 ctsio->residual = alloc_len - lun_datalen; 9132 ctsio->kern_data_len = lun_datalen; 9133 ctsio->kern_total_len = lun_datalen; 9134 } else { 9135 ctsio->residual = 0; 9136 ctsio->kern_data_len = alloc_len; 9137 ctsio->kern_total_len = alloc_len; 9138 } 9139 ctsio->kern_data_resid = 0; 9140 ctsio->kern_rel_offset = 0; 9141 ctsio->kern_sg_entries = 0; 9142 9143 /* 9144 * We set this to the actual data length, regardless of how much 9145 * space we actually have to return results. If the user looks at 9146 * this value, he'll know whether or not he allocated enough space 9147 * and reissue the command if necessary. We don't support well 9148 * known logical units, so if the user asks for that, return none. 9149 */ 9150 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9151 9152 /* 9153 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9154 * this request. 9155 */ 9156 ctl_set_success(ctsio); 9157 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9158 ctsio->be_move_done = ctl_config_move_done; 9159 ctl_datamove((union ctl_io *)ctsio); 9160 return (retval); 9161} 9162 9163int 9164ctl_request_sense(struct ctl_scsiio *ctsio) 9165{ 9166 struct scsi_request_sense *cdb; 9167 struct scsi_sense_data *sense_ptr; 9168 struct ctl_softc *ctl_softc; 9169 struct ctl_lun *lun; 9170 uint32_t initidx; 9171 int have_error; 9172 scsi_sense_data_type sense_format; 9173 ctl_ua_type ua_type; 9174 9175 cdb = (struct scsi_request_sense *)ctsio->cdb; 9176 9177 ctl_softc = control_softc; 9178 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9179 9180 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9181 9182 /* 9183 * Determine which sense format the user wants. 9184 */ 9185 if (cdb->byte2 & SRS_DESC) 9186 sense_format = SSD_TYPE_DESC; 9187 else 9188 sense_format = SSD_TYPE_FIXED; 9189 9190 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9191 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9192 ctsio->kern_sg_entries = 0; 9193 9194 /* 9195 * struct scsi_sense_data, which is currently set to 256 bytes, is 9196 * larger than the largest allowed value for the length field in the 9197 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9198 */ 9199 ctsio->residual = 0; 9200 ctsio->kern_data_len = cdb->length; 9201 ctsio->kern_total_len = cdb->length; 9202 9203 ctsio->kern_data_resid = 0; 9204 ctsio->kern_rel_offset = 0; 9205 ctsio->kern_sg_entries = 0; 9206 9207 /* 9208 * If we don't have a LUN, we don't have any pending sense. 9209 */ 9210 if (lun == NULL) 9211 goto no_sense; 9212 9213 have_error = 0; 9214 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9215 /* 9216 * Check for pending sense, and then for pending unit attentions. 9217 * Pending sense gets returned first, then pending unit attentions. 9218 */ 9219 mtx_lock(&lun->lun_lock); 9220#ifdef CTL_WITH_CA 9221 if (ctl_is_set(lun->have_ca, initidx)) { 9222 scsi_sense_data_type stored_format; 9223 9224 /* 9225 * Check to see which sense format was used for the stored 9226 * sense data. 9227 */ 9228 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9229 9230 /* 9231 * If the user requested a different sense format than the 9232 * one we stored, then we need to convert it to the other 9233 * format. If we're going from descriptor to fixed format 9234 * sense data, we may lose things in translation, depending 9235 * on what options were used. 9236 * 9237 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9238 * for some reason we'll just copy it out as-is. 9239 */ 9240 if ((stored_format == SSD_TYPE_FIXED) 9241 && (sense_format == SSD_TYPE_DESC)) 9242 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9243 &lun->pending_sense[initidx], 9244 (struct scsi_sense_data_desc *)sense_ptr); 9245 else if ((stored_format == SSD_TYPE_DESC) 9246 && (sense_format == SSD_TYPE_FIXED)) 9247 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9248 &lun->pending_sense[initidx], 9249 (struct scsi_sense_data_fixed *)sense_ptr); 9250 else 9251 memcpy(sense_ptr, &lun->pending_sense[initidx], 9252 MIN(sizeof(*sense_ptr), 9253 sizeof(lun->pending_sense[initidx]))); 9254 9255 ctl_clear_mask(lun->have_ca, initidx); 9256 have_error = 1; 9257 } else 9258#endif 9259 { 9260 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9261 if (ua_type != CTL_UA_NONE) 9262 have_error = 1; 9263 if (ua_type == CTL_UA_LUN_CHANGE) { 9264 mtx_unlock(&lun->lun_lock); 9265 mtx_lock(&ctl_softc->ctl_lock); 9266 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type); 9267 mtx_unlock(&ctl_softc->ctl_lock); 9268 mtx_lock(&lun->lun_lock); 9269 } 9270 9271 } 9272 mtx_unlock(&lun->lun_lock); 9273 9274 /* 9275 * We already have a pending error, return it. 9276 */ 9277 if (have_error != 0) { 9278 /* 9279 * We report the SCSI status as OK, since the status of the 9280 * request sense command itself is OK. 9281 * We report 0 for the sense length, because we aren't doing 9282 * autosense in this case. We're reporting sense as 9283 * parameter data. 9284 */ 9285 ctl_set_success(ctsio); 9286 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9287 ctsio->be_move_done = ctl_config_move_done; 9288 ctl_datamove((union ctl_io *)ctsio); 9289 return (CTL_RETVAL_COMPLETE); 9290 } 9291 9292no_sense: 9293 9294 /* 9295 * No sense information to report, so we report that everything is 9296 * okay. 9297 */ 9298 ctl_set_sense_data(sense_ptr, 9299 lun, 9300 sense_format, 9301 /*current_error*/ 1, 9302 /*sense_key*/ SSD_KEY_NO_SENSE, 9303 /*asc*/ 0x00, 9304 /*ascq*/ 0x00, 9305 SSD_ELEM_NONE); 9306 9307 /* 9308 * We report 0 for the sense length, because we aren't doing 9309 * autosense in this case. We're reporting sense as parameter data. 9310 */ 9311 ctl_set_success(ctsio); 9312 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9313 ctsio->be_move_done = ctl_config_move_done; 9314 ctl_datamove((union ctl_io *)ctsio); 9315 return (CTL_RETVAL_COMPLETE); 9316} 9317 9318int 9319ctl_tur(struct ctl_scsiio *ctsio) 9320{ 9321 9322 CTL_DEBUG_PRINT(("ctl_tur\n")); 9323 9324 ctl_set_success(ctsio); 9325 ctl_done((union ctl_io *)ctsio); 9326 9327 return (CTL_RETVAL_COMPLETE); 9328} 9329 9330/* 9331 * SCSI VPD page 0x00, the Supported VPD Pages page. 9332 */ 9333static int 9334ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9335{ 9336 struct scsi_vpd_supported_pages *pages; 9337 int sup_page_size; 9338 struct ctl_lun *lun; 9339 int p; 9340 9341 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9342 9343 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9344 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9345 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9346 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9347 ctsio->kern_sg_entries = 0; 9348 9349 if (sup_page_size < alloc_len) { 9350 ctsio->residual = alloc_len - sup_page_size; 9351 ctsio->kern_data_len = sup_page_size; 9352 ctsio->kern_total_len = sup_page_size; 9353 } else { 9354 ctsio->residual = 0; 9355 ctsio->kern_data_len = alloc_len; 9356 ctsio->kern_total_len = alloc_len; 9357 } 9358 ctsio->kern_data_resid = 0; 9359 ctsio->kern_rel_offset = 0; 9360 ctsio->kern_sg_entries = 0; 9361 9362 /* 9363 * The control device is always connected. The disk device, on the 9364 * other hand, may not be online all the time. Need to change this 9365 * to figure out whether the disk device is actually online or not. 9366 */ 9367 if (lun != NULL) 9368 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9369 lun->be_lun->lun_type; 9370 else 9371 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9372 9373 p = 0; 9374 /* Supported VPD pages */ 9375 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9376 /* Serial Number */ 9377 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9378 /* Device Identification */ 9379 pages->page_list[p++] = SVPD_DEVICE_ID; 9380 /* Extended INQUIRY Data */ 9381 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9382 /* Mode Page Policy */ 9383 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9384 /* SCSI Ports */ 9385 pages->page_list[p++] = SVPD_SCSI_PORTS; 9386 /* Third-party Copy */ 9387 pages->page_list[p++] = SVPD_SCSI_TPC; 9388 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9389 /* Block limits */ 9390 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9391 /* Block Device Characteristics */ 9392 pages->page_list[p++] = SVPD_BDC; 9393 /* Logical Block Provisioning */ 9394 pages->page_list[p++] = SVPD_LBP; 9395 } 9396 pages->length = p; 9397 9398 ctl_set_success(ctsio); 9399 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9400 ctsio->be_move_done = ctl_config_move_done; 9401 ctl_datamove((union ctl_io *)ctsio); 9402 return (CTL_RETVAL_COMPLETE); 9403} 9404 9405/* 9406 * SCSI VPD page 0x80, the Unit Serial Number page. 9407 */ 9408static int 9409ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9410{ 9411 struct scsi_vpd_unit_serial_number *sn_ptr; 9412 struct ctl_lun *lun; 9413 int data_len; 9414 9415 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9416 9417 data_len = 4 + CTL_SN_LEN; 9418 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9419 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9420 if (data_len < alloc_len) { 9421 ctsio->residual = alloc_len - data_len; 9422 ctsio->kern_data_len = data_len; 9423 ctsio->kern_total_len = data_len; 9424 } else { 9425 ctsio->residual = 0; 9426 ctsio->kern_data_len = alloc_len; 9427 ctsio->kern_total_len = alloc_len; 9428 } 9429 ctsio->kern_data_resid = 0; 9430 ctsio->kern_rel_offset = 0; 9431 ctsio->kern_sg_entries = 0; 9432 9433 /* 9434 * The control device is always connected. The disk device, on the 9435 * other hand, may not be online all the time. Need to change this 9436 * to figure out whether the disk device is actually online or not. 9437 */ 9438 if (lun != NULL) 9439 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9440 lun->be_lun->lun_type; 9441 else 9442 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9443 9444 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9445 sn_ptr->length = CTL_SN_LEN; 9446 /* 9447 * If we don't have a LUN, we just leave the serial number as 9448 * all spaces. 9449 */ 9450 if (lun != NULL) { 9451 strncpy((char *)sn_ptr->serial_num, 9452 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9453 } else 9454 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9455 9456 ctl_set_success(ctsio); 9457 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9458 ctsio->be_move_done = ctl_config_move_done; 9459 ctl_datamove((union ctl_io *)ctsio); 9460 return (CTL_RETVAL_COMPLETE); 9461} 9462 9463 9464/* 9465 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9466 */ 9467static int 9468ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9469{ 9470 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9471 struct ctl_lun *lun; 9472 int data_len; 9473 9474 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9475 9476 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9477 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9478 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9479 ctsio->kern_sg_entries = 0; 9480 9481 if (data_len < alloc_len) { 9482 ctsio->residual = alloc_len - data_len; 9483 ctsio->kern_data_len = data_len; 9484 ctsio->kern_total_len = data_len; 9485 } else { 9486 ctsio->residual = 0; 9487 ctsio->kern_data_len = alloc_len; 9488 ctsio->kern_total_len = alloc_len; 9489 } 9490 ctsio->kern_data_resid = 0; 9491 ctsio->kern_rel_offset = 0; 9492 ctsio->kern_sg_entries = 0; 9493 9494 /* 9495 * The control device is always connected. The disk device, on the 9496 * other hand, may not be online all the time. 9497 */ 9498 if (lun != NULL) 9499 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9500 lun->be_lun->lun_type; 9501 else 9502 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9503 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9504 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9505 /* 9506 * We support head of queue, ordered and simple tags. 9507 */ 9508 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9509 /* 9510 * Volatile cache supported. 9511 */ 9512 eid_ptr->flags3 = SVPD_EID_V_SUP; 9513 9514 /* 9515 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9516 * attention for a particular IT nexus on all LUNs once we report 9517 * it to that nexus once. This bit is required as of SPC-4. 9518 */ 9519 eid_ptr->flags4 = SVPD_EID_LUICLT; 9520 9521 /* 9522 * XXX KDM in order to correctly answer this, we would need 9523 * information from the SIM to determine how much sense data it 9524 * can send. So this would really be a path inquiry field, most 9525 * likely. This can be set to a maximum of 252 according to SPC-4, 9526 * but the hardware may or may not be able to support that much. 9527 * 0 just means that the maximum sense data length is not reported. 9528 */ 9529 eid_ptr->max_sense_length = 0; 9530 9531 ctl_set_success(ctsio); 9532 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9533 ctsio->be_move_done = ctl_config_move_done; 9534 ctl_datamove((union ctl_io *)ctsio); 9535 return (CTL_RETVAL_COMPLETE); 9536} 9537 9538static int 9539ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9540{ 9541 struct scsi_vpd_mode_page_policy *mpp_ptr; 9542 struct ctl_lun *lun; 9543 int data_len; 9544 9545 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9546 9547 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9548 sizeof(struct scsi_vpd_mode_page_policy_descr); 9549 9550 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9551 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9552 ctsio->kern_sg_entries = 0; 9553 9554 if (data_len < alloc_len) { 9555 ctsio->residual = alloc_len - data_len; 9556 ctsio->kern_data_len = data_len; 9557 ctsio->kern_total_len = data_len; 9558 } else { 9559 ctsio->residual = 0; 9560 ctsio->kern_data_len = alloc_len; 9561 ctsio->kern_total_len = alloc_len; 9562 } 9563 ctsio->kern_data_resid = 0; 9564 ctsio->kern_rel_offset = 0; 9565 ctsio->kern_sg_entries = 0; 9566 9567 /* 9568 * The control device is always connected. The disk device, on the 9569 * other hand, may not be online all the time. 9570 */ 9571 if (lun != NULL) 9572 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9573 lun->be_lun->lun_type; 9574 else 9575 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9576 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9577 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9578 mpp_ptr->descr[0].page_code = 0x3f; 9579 mpp_ptr->descr[0].subpage_code = 0xff; 9580 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9581 9582 ctl_set_success(ctsio); 9583 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9584 ctsio->be_move_done = ctl_config_move_done; 9585 ctl_datamove((union ctl_io *)ctsio); 9586 return (CTL_RETVAL_COMPLETE); 9587} 9588 9589/* 9590 * SCSI VPD page 0x83, the Device Identification page. 9591 */ 9592static int 9593ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9594{ 9595 struct scsi_vpd_device_id *devid_ptr; 9596 struct scsi_vpd_id_descriptor *desc; 9597 struct ctl_softc *softc; 9598 struct ctl_lun *lun; 9599 struct ctl_port *port; 9600 int data_len; 9601 uint8_t proto; 9602 9603 softc = control_softc; 9604 9605 port = ctl_io_port(&ctsio->io_hdr); 9606 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9607 9608 data_len = sizeof(struct scsi_vpd_device_id) + 9609 sizeof(struct scsi_vpd_id_descriptor) + 9610 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9611 sizeof(struct scsi_vpd_id_descriptor) + 9612 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9613 if (lun && lun->lun_devid) 9614 data_len += lun->lun_devid->len; 9615 if (port && port->port_devid) 9616 data_len += port->port_devid->len; 9617 if (port && port->target_devid) 9618 data_len += port->target_devid->len; 9619 9620 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9621 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9622 ctsio->kern_sg_entries = 0; 9623 9624 if (data_len < alloc_len) { 9625 ctsio->residual = alloc_len - data_len; 9626 ctsio->kern_data_len = data_len; 9627 ctsio->kern_total_len = data_len; 9628 } else { 9629 ctsio->residual = 0; 9630 ctsio->kern_data_len = alloc_len; 9631 ctsio->kern_total_len = alloc_len; 9632 } 9633 ctsio->kern_data_resid = 0; 9634 ctsio->kern_rel_offset = 0; 9635 ctsio->kern_sg_entries = 0; 9636 9637 /* 9638 * The control device is always connected. The disk device, on the 9639 * other hand, may not be online all the time. 9640 */ 9641 if (lun != NULL) 9642 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9643 lun->be_lun->lun_type; 9644 else 9645 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9646 devid_ptr->page_code = SVPD_DEVICE_ID; 9647 scsi_ulto2b(data_len - 4, devid_ptr->length); 9648 9649 if (port && port->port_type == CTL_PORT_FC) 9650 proto = SCSI_PROTO_FC << 4; 9651 else if (port && port->port_type == CTL_PORT_ISCSI) 9652 proto = SCSI_PROTO_ISCSI << 4; 9653 else 9654 proto = SCSI_PROTO_SPI << 4; 9655 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9656 9657 /* 9658 * We're using a LUN association here. i.e., this device ID is a 9659 * per-LUN identifier. 9660 */ 9661 if (lun && lun->lun_devid) { 9662 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9663 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9664 lun->lun_devid->len); 9665 } 9666 9667 /* 9668 * This is for the WWPN which is a port association. 9669 */ 9670 if (port && port->port_devid) { 9671 memcpy(desc, port->port_devid->data, port->port_devid->len); 9672 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9673 port->port_devid->len); 9674 } 9675 9676 /* 9677 * This is for the Relative Target Port(type 4h) identifier 9678 */ 9679 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9680 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9681 SVPD_ID_TYPE_RELTARG; 9682 desc->length = 4; 9683 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9684 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9685 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9686 9687 /* 9688 * This is for the Target Port Group(type 5h) identifier 9689 */ 9690 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9691 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9692 SVPD_ID_TYPE_TPORTGRP; 9693 desc->length = 4; 9694 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1, 9695 &desc->identifier[2]); 9696 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9697 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9698 9699 /* 9700 * This is for the Target identifier 9701 */ 9702 if (port && port->target_devid) { 9703 memcpy(desc, port->target_devid->data, port->target_devid->len); 9704 } 9705 9706 ctl_set_success(ctsio); 9707 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9708 ctsio->be_move_done = ctl_config_move_done; 9709 ctl_datamove((union ctl_io *)ctsio); 9710 return (CTL_RETVAL_COMPLETE); 9711} 9712 9713static int 9714ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9715{ 9716 struct ctl_softc *softc = control_softc; 9717 struct scsi_vpd_scsi_ports *sp; 9718 struct scsi_vpd_port_designation *pd; 9719 struct scsi_vpd_port_designation_cont *pdc; 9720 struct ctl_lun *lun; 9721 struct ctl_port *port; 9722 int data_len, num_target_ports, iid_len, id_len; 9723 9724 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9725 9726 num_target_ports = 0; 9727 iid_len = 0; 9728 id_len = 0; 9729 mtx_lock(&softc->ctl_lock); 9730 STAILQ_FOREACH(port, &softc->port_list, links) { 9731 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9732 continue; 9733 if (lun != NULL && 9734 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9735 continue; 9736 num_target_ports++; 9737 if (port->init_devid) 9738 iid_len += port->init_devid->len; 9739 if (port->port_devid) 9740 id_len += port->port_devid->len; 9741 } 9742 mtx_unlock(&softc->ctl_lock); 9743 9744 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9745 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9746 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9747 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9748 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9749 ctsio->kern_sg_entries = 0; 9750 9751 if (data_len < alloc_len) { 9752 ctsio->residual = alloc_len - data_len; 9753 ctsio->kern_data_len = data_len; 9754 ctsio->kern_total_len = data_len; 9755 } else { 9756 ctsio->residual = 0; 9757 ctsio->kern_data_len = alloc_len; 9758 ctsio->kern_total_len = alloc_len; 9759 } 9760 ctsio->kern_data_resid = 0; 9761 ctsio->kern_rel_offset = 0; 9762 ctsio->kern_sg_entries = 0; 9763 9764 /* 9765 * The control device is always connected. The disk device, on the 9766 * other hand, may not be online all the time. Need to change this 9767 * to figure out whether the disk device is actually online or not. 9768 */ 9769 if (lun != NULL) 9770 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9771 lun->be_lun->lun_type; 9772 else 9773 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9774 9775 sp->page_code = SVPD_SCSI_PORTS; 9776 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9777 sp->page_length); 9778 pd = &sp->design[0]; 9779 9780 mtx_lock(&softc->ctl_lock); 9781 STAILQ_FOREACH(port, &softc->port_list, links) { 9782 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9783 continue; 9784 if (lun != NULL && 9785 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9786 continue; 9787 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9788 if (port->init_devid) { 9789 iid_len = port->init_devid->len; 9790 memcpy(pd->initiator_transportid, 9791 port->init_devid->data, port->init_devid->len); 9792 } else 9793 iid_len = 0; 9794 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9795 pdc = (struct scsi_vpd_port_designation_cont *) 9796 (&pd->initiator_transportid[iid_len]); 9797 if (port->port_devid) { 9798 id_len = port->port_devid->len; 9799 memcpy(pdc->target_port_descriptors, 9800 port->port_devid->data, port->port_devid->len); 9801 } else 9802 id_len = 0; 9803 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9804 pd = (struct scsi_vpd_port_designation *) 9805 ((uint8_t *)pdc->target_port_descriptors + id_len); 9806 } 9807 mtx_unlock(&softc->ctl_lock); 9808 9809 ctl_set_success(ctsio); 9810 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9811 ctsio->be_move_done = ctl_config_move_done; 9812 ctl_datamove((union ctl_io *)ctsio); 9813 return (CTL_RETVAL_COMPLETE); 9814} 9815 9816static int 9817ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9818{ 9819 struct scsi_vpd_block_limits *bl_ptr; 9820 struct ctl_lun *lun; 9821 int bs; 9822 9823 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9824 9825 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9826 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9827 ctsio->kern_sg_entries = 0; 9828 9829 if (sizeof(*bl_ptr) < alloc_len) { 9830 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9831 ctsio->kern_data_len = sizeof(*bl_ptr); 9832 ctsio->kern_total_len = sizeof(*bl_ptr); 9833 } else { 9834 ctsio->residual = 0; 9835 ctsio->kern_data_len = alloc_len; 9836 ctsio->kern_total_len = alloc_len; 9837 } 9838 ctsio->kern_data_resid = 0; 9839 ctsio->kern_rel_offset = 0; 9840 ctsio->kern_sg_entries = 0; 9841 9842 /* 9843 * The control device is always connected. The disk device, on the 9844 * other hand, may not be online all the time. Need to change this 9845 * to figure out whether the disk device is actually online or not. 9846 */ 9847 if (lun != NULL) 9848 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9849 lun->be_lun->lun_type; 9850 else 9851 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9852 9853 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9854 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9855 bl_ptr->max_cmp_write_len = 0xff; 9856 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9857 if (lun != NULL) { 9858 bs = lun->be_lun->blocksize; 9859 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9860 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9861 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 9862 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 9863 if (lun->be_lun->ublockexp != 0) { 9864 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9865 bl_ptr->opt_unmap_grain); 9866 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9867 bl_ptr->unmap_grain_align); 9868 } 9869 } 9870 scsi_ulto4b(lun->be_lun->atomicblock, 9871 bl_ptr->max_atomic_transfer_length); 9872 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9873 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9874 } 9875 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 9876 9877 ctl_set_success(ctsio); 9878 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9879 ctsio->be_move_done = ctl_config_move_done; 9880 ctl_datamove((union ctl_io *)ctsio); 9881 return (CTL_RETVAL_COMPLETE); 9882} 9883 9884static int 9885ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9886{ 9887 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9888 struct ctl_lun *lun; 9889 const char *value; 9890 u_int i; 9891 9892 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9893 9894 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9895 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9896 ctsio->kern_sg_entries = 0; 9897 9898 if (sizeof(*bdc_ptr) < alloc_len) { 9899 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9900 ctsio->kern_data_len = sizeof(*bdc_ptr); 9901 ctsio->kern_total_len = sizeof(*bdc_ptr); 9902 } else { 9903 ctsio->residual = 0; 9904 ctsio->kern_data_len = alloc_len; 9905 ctsio->kern_total_len = alloc_len; 9906 } 9907 ctsio->kern_data_resid = 0; 9908 ctsio->kern_rel_offset = 0; 9909 ctsio->kern_sg_entries = 0; 9910 9911 /* 9912 * The control device is always connected. The disk device, on the 9913 * other hand, may not be online all the time. Need to change this 9914 * to figure out whether the disk device is actually online or not. 9915 */ 9916 if (lun != NULL) 9917 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9918 lun->be_lun->lun_type; 9919 else 9920 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9921 bdc_ptr->page_code = SVPD_BDC; 9922 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9923 if (lun != NULL && 9924 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9925 i = strtol(value, NULL, 0); 9926 else 9927 i = CTL_DEFAULT_ROTATION_RATE; 9928 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9929 if (lun != NULL && 9930 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9931 i = strtol(value, NULL, 0); 9932 else 9933 i = 0; 9934 bdc_ptr->wab_wac_ff = (i & 0x0f); 9935 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9936 9937 ctl_set_success(ctsio); 9938 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9939 ctsio->be_move_done = ctl_config_move_done; 9940 ctl_datamove((union ctl_io *)ctsio); 9941 return (CTL_RETVAL_COMPLETE); 9942} 9943 9944static int 9945ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9946{ 9947 struct scsi_vpd_logical_block_prov *lbp_ptr; 9948 struct ctl_lun *lun; 9949 9950 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9951 9952 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9953 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9954 ctsio->kern_sg_entries = 0; 9955 9956 if (sizeof(*lbp_ptr) < alloc_len) { 9957 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9958 ctsio->kern_data_len = sizeof(*lbp_ptr); 9959 ctsio->kern_total_len = sizeof(*lbp_ptr); 9960 } else { 9961 ctsio->residual = 0; 9962 ctsio->kern_data_len = alloc_len; 9963 ctsio->kern_total_len = alloc_len; 9964 } 9965 ctsio->kern_data_resid = 0; 9966 ctsio->kern_rel_offset = 0; 9967 ctsio->kern_sg_entries = 0; 9968 9969 /* 9970 * The control device is always connected. The disk device, on the 9971 * other hand, may not be online all the time. Need to change this 9972 * to figure out whether the disk device is actually online or not. 9973 */ 9974 if (lun != NULL) 9975 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9976 lun->be_lun->lun_type; 9977 else 9978 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9979 9980 lbp_ptr->page_code = SVPD_LBP; 9981 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9982 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9983 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9984 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9985 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9986 lbp_ptr->prov_type = SVPD_LBP_THIN; 9987 } 9988 9989 ctl_set_success(ctsio); 9990 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9991 ctsio->be_move_done = ctl_config_move_done; 9992 ctl_datamove((union ctl_io *)ctsio); 9993 return (CTL_RETVAL_COMPLETE); 9994} 9995 9996/* 9997 * INQUIRY with the EVPD bit set. 9998 */ 9999static int 10000ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10001{ 10002 struct ctl_lun *lun; 10003 struct scsi_inquiry *cdb; 10004 int alloc_len, retval; 10005 10006 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10007 cdb = (struct scsi_inquiry *)ctsio->cdb; 10008 alloc_len = scsi_2btoul(cdb->length); 10009 10010 switch (cdb->page_code) { 10011 case SVPD_SUPPORTED_PAGES: 10012 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10013 break; 10014 case SVPD_UNIT_SERIAL_NUMBER: 10015 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10016 break; 10017 case SVPD_DEVICE_ID: 10018 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10019 break; 10020 case SVPD_EXTENDED_INQUIRY_DATA: 10021 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10022 break; 10023 case SVPD_MODE_PAGE_POLICY: 10024 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10025 break; 10026 case SVPD_SCSI_PORTS: 10027 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10028 break; 10029 case SVPD_SCSI_TPC: 10030 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10031 break; 10032 case SVPD_BLOCK_LIMITS: 10033 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10034 goto err; 10035 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10036 break; 10037 case SVPD_BDC: 10038 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10039 goto err; 10040 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10041 break; 10042 case SVPD_LBP: 10043 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10044 goto err; 10045 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10046 break; 10047 default: 10048err: 10049 ctl_set_invalid_field(ctsio, 10050 /*sks_valid*/ 1, 10051 /*command*/ 1, 10052 /*field*/ 2, 10053 /*bit_valid*/ 0, 10054 /*bit*/ 0); 10055 ctl_done((union ctl_io *)ctsio); 10056 retval = CTL_RETVAL_COMPLETE; 10057 break; 10058 } 10059 10060 return (retval); 10061} 10062 10063/* 10064 * Standard INQUIRY data. 10065 */ 10066static int 10067ctl_inquiry_std(struct ctl_scsiio *ctsio) 10068{ 10069 struct scsi_inquiry_data *inq_ptr; 10070 struct scsi_inquiry *cdb; 10071 struct ctl_softc *softc; 10072 struct ctl_port *port; 10073 struct ctl_lun *lun; 10074 char *val; 10075 uint32_t alloc_len, data_len; 10076 ctl_port_type port_type; 10077 10078 softc = control_softc; 10079 10080 /* 10081 * Figure out whether we're talking to a Fibre Channel port or not. 10082 * We treat the ioctl front end, and any SCSI adapters, as packetized 10083 * SCSI front ends. 10084 */ 10085 port = ctl_io_port(&ctsio->io_hdr); 10086 if (port != NULL) 10087 port_type = port->port_type; 10088 else 10089 port_type = CTL_PORT_SCSI; 10090 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10091 port_type = CTL_PORT_SCSI; 10092 10093 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10094 cdb = (struct scsi_inquiry *)ctsio->cdb; 10095 alloc_len = scsi_2btoul(cdb->length); 10096 10097 /* 10098 * We malloc the full inquiry data size here and fill it 10099 * in. If the user only asks for less, we'll give him 10100 * that much. 10101 */ 10102 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10103 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10104 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10105 ctsio->kern_sg_entries = 0; 10106 ctsio->kern_data_resid = 0; 10107 ctsio->kern_rel_offset = 0; 10108 10109 if (data_len < alloc_len) { 10110 ctsio->residual = alloc_len - data_len; 10111 ctsio->kern_data_len = data_len; 10112 ctsio->kern_total_len = data_len; 10113 } else { 10114 ctsio->residual = 0; 10115 ctsio->kern_data_len = alloc_len; 10116 ctsio->kern_total_len = alloc_len; 10117 } 10118 10119 if (lun != NULL) { 10120 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10121 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10122 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10123 lun->be_lun->lun_type; 10124 } else { 10125 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10126 lun->be_lun->lun_type; 10127 } 10128 } else 10129 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10130 10131 /* RMB in byte 2 is 0 */ 10132 inq_ptr->version = SCSI_REV_SPC4; 10133 10134 /* 10135 * According to SAM-3, even if a device only supports a single 10136 * level of LUN addressing, it should still set the HISUP bit: 10137 * 10138 * 4.9.1 Logical unit numbers overview 10139 * 10140 * All logical unit number formats described in this standard are 10141 * hierarchical in structure even when only a single level in that 10142 * hierarchy is used. The HISUP bit shall be set to one in the 10143 * standard INQUIRY data (see SPC-2) when any logical unit number 10144 * format described in this standard is used. Non-hierarchical 10145 * formats are outside the scope of this standard. 10146 * 10147 * Therefore we set the HiSup bit here. 10148 * 10149 * The reponse format is 2, per SPC-3. 10150 */ 10151 inq_ptr->response_format = SID_HiSup | 2; 10152 10153 inq_ptr->additional_length = data_len - 10154 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10155 CTL_DEBUG_PRINT(("additional_length = %d\n", 10156 inq_ptr->additional_length)); 10157 10158 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10159 /* 16 bit addressing */ 10160 if (port_type == CTL_PORT_SCSI) 10161 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10162 /* XXX set the SID_MultiP bit here if we're actually going to 10163 respond on multiple ports */ 10164 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10165 10166 /* 16 bit data bus, synchronous transfers */ 10167 if (port_type == CTL_PORT_SCSI) 10168 inq_ptr->flags = SID_WBus16 | SID_Sync; 10169 /* 10170 * XXX KDM do we want to support tagged queueing on the control 10171 * device at all? 10172 */ 10173 if ((lun == NULL) 10174 || (lun->be_lun->lun_type != T_PROCESSOR)) 10175 inq_ptr->flags |= SID_CmdQue; 10176 /* 10177 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10178 * We have 8 bytes for the vendor name, and 16 bytes for the device 10179 * name and 4 bytes for the revision. 10180 */ 10181 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10182 "vendor")) == NULL) { 10183 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10184 } else { 10185 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10186 strncpy(inq_ptr->vendor, val, 10187 min(sizeof(inq_ptr->vendor), strlen(val))); 10188 } 10189 if (lun == NULL) { 10190 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10191 sizeof(inq_ptr->product)); 10192 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10193 switch (lun->be_lun->lun_type) { 10194 case T_DIRECT: 10195 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10196 sizeof(inq_ptr->product)); 10197 break; 10198 case T_PROCESSOR: 10199 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10200 sizeof(inq_ptr->product)); 10201 break; 10202 default: 10203 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10204 sizeof(inq_ptr->product)); 10205 break; 10206 } 10207 } else { 10208 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10209 strncpy(inq_ptr->product, val, 10210 min(sizeof(inq_ptr->product), strlen(val))); 10211 } 10212 10213 /* 10214 * XXX make this a macro somewhere so it automatically gets 10215 * incremented when we make changes. 10216 */ 10217 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10218 "revision")) == NULL) { 10219 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10220 } else { 10221 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10222 strncpy(inq_ptr->revision, val, 10223 min(sizeof(inq_ptr->revision), strlen(val))); 10224 } 10225 10226 /* 10227 * For parallel SCSI, we support double transition and single 10228 * transition clocking. We also support QAS (Quick Arbitration 10229 * and Selection) and Information Unit transfers on both the 10230 * control and array devices. 10231 */ 10232 if (port_type == CTL_PORT_SCSI) 10233 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10234 SID_SPI_IUS; 10235 10236 /* SAM-5 (no version claimed) */ 10237 scsi_ulto2b(0x00A0, inq_ptr->version1); 10238 /* SPC-4 (no version claimed) */ 10239 scsi_ulto2b(0x0460, inq_ptr->version2); 10240 if (port_type == CTL_PORT_FC) { 10241 /* FCP-2 ANSI INCITS.350:2003 */ 10242 scsi_ulto2b(0x0917, inq_ptr->version3); 10243 } else if (port_type == CTL_PORT_SCSI) { 10244 /* SPI-4 ANSI INCITS.362:200x */ 10245 scsi_ulto2b(0x0B56, inq_ptr->version3); 10246 } else if (port_type == CTL_PORT_ISCSI) { 10247 /* iSCSI (no version claimed) */ 10248 scsi_ulto2b(0x0960, inq_ptr->version3); 10249 } else if (port_type == CTL_PORT_SAS) { 10250 /* SAS (no version claimed) */ 10251 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10252 } 10253 10254 if (lun == NULL) { 10255 /* SBC-4 (no version claimed) */ 10256 scsi_ulto2b(0x0600, inq_ptr->version4); 10257 } else { 10258 switch (lun->be_lun->lun_type) { 10259 case T_DIRECT: 10260 /* SBC-4 (no version claimed) */ 10261 scsi_ulto2b(0x0600, inq_ptr->version4); 10262 break; 10263 case T_PROCESSOR: 10264 default: 10265 break; 10266 } 10267 } 10268 10269 ctl_set_success(ctsio); 10270 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10271 ctsio->be_move_done = ctl_config_move_done; 10272 ctl_datamove((union ctl_io *)ctsio); 10273 return (CTL_RETVAL_COMPLETE); 10274} 10275 10276int 10277ctl_inquiry(struct ctl_scsiio *ctsio) 10278{ 10279 struct scsi_inquiry *cdb; 10280 int retval; 10281 10282 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10283 10284 cdb = (struct scsi_inquiry *)ctsio->cdb; 10285 if (cdb->byte2 & SI_EVPD) 10286 retval = ctl_inquiry_evpd(ctsio); 10287 else if (cdb->page_code == 0) 10288 retval = ctl_inquiry_std(ctsio); 10289 else { 10290 ctl_set_invalid_field(ctsio, 10291 /*sks_valid*/ 1, 10292 /*command*/ 1, 10293 /*field*/ 2, 10294 /*bit_valid*/ 0, 10295 /*bit*/ 0); 10296 ctl_done((union ctl_io *)ctsio); 10297 return (CTL_RETVAL_COMPLETE); 10298 } 10299 10300 return (retval); 10301} 10302 10303/* 10304 * For known CDB types, parse the LBA and length. 10305 */ 10306static int 10307ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10308{ 10309 if (io->io_hdr.io_type != CTL_IO_SCSI) 10310 return (1); 10311 10312 switch (io->scsiio.cdb[0]) { 10313 case COMPARE_AND_WRITE: { 10314 struct scsi_compare_and_write *cdb; 10315 10316 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10317 10318 *lba = scsi_8btou64(cdb->addr); 10319 *len = cdb->length; 10320 break; 10321 } 10322 case READ_6: 10323 case WRITE_6: { 10324 struct scsi_rw_6 *cdb; 10325 10326 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10327 10328 *lba = scsi_3btoul(cdb->addr); 10329 /* only 5 bits are valid in the most significant address byte */ 10330 *lba &= 0x1fffff; 10331 *len = cdb->length; 10332 break; 10333 } 10334 case READ_10: 10335 case WRITE_10: { 10336 struct scsi_rw_10 *cdb; 10337 10338 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10339 10340 *lba = scsi_4btoul(cdb->addr); 10341 *len = scsi_2btoul(cdb->length); 10342 break; 10343 } 10344 case WRITE_VERIFY_10: { 10345 struct scsi_write_verify_10 *cdb; 10346 10347 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10348 10349 *lba = scsi_4btoul(cdb->addr); 10350 *len = scsi_2btoul(cdb->length); 10351 break; 10352 } 10353 case READ_12: 10354 case WRITE_12: { 10355 struct scsi_rw_12 *cdb; 10356 10357 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10358 10359 *lba = scsi_4btoul(cdb->addr); 10360 *len = scsi_4btoul(cdb->length); 10361 break; 10362 } 10363 case WRITE_VERIFY_12: { 10364 struct scsi_write_verify_12 *cdb; 10365 10366 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10367 10368 *lba = scsi_4btoul(cdb->addr); 10369 *len = scsi_4btoul(cdb->length); 10370 break; 10371 } 10372 case READ_16: 10373 case WRITE_16: 10374 case WRITE_ATOMIC_16: { 10375 struct scsi_rw_16 *cdb; 10376 10377 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10378 10379 *lba = scsi_8btou64(cdb->addr); 10380 *len = scsi_4btoul(cdb->length); 10381 break; 10382 } 10383 case WRITE_VERIFY_16: { 10384 struct scsi_write_verify_16 *cdb; 10385 10386 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10387 10388 *lba = scsi_8btou64(cdb->addr); 10389 *len = scsi_4btoul(cdb->length); 10390 break; 10391 } 10392 case WRITE_SAME_10: { 10393 struct scsi_write_same_10 *cdb; 10394 10395 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10396 10397 *lba = scsi_4btoul(cdb->addr); 10398 *len = scsi_2btoul(cdb->length); 10399 break; 10400 } 10401 case WRITE_SAME_16: { 10402 struct scsi_write_same_16 *cdb; 10403 10404 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10405 10406 *lba = scsi_8btou64(cdb->addr); 10407 *len = scsi_4btoul(cdb->length); 10408 break; 10409 } 10410 case VERIFY_10: { 10411 struct scsi_verify_10 *cdb; 10412 10413 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10414 10415 *lba = scsi_4btoul(cdb->addr); 10416 *len = scsi_2btoul(cdb->length); 10417 break; 10418 } 10419 case VERIFY_12: { 10420 struct scsi_verify_12 *cdb; 10421 10422 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10423 10424 *lba = scsi_4btoul(cdb->addr); 10425 *len = scsi_4btoul(cdb->length); 10426 break; 10427 } 10428 case VERIFY_16: { 10429 struct scsi_verify_16 *cdb; 10430 10431 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10432 10433 *lba = scsi_8btou64(cdb->addr); 10434 *len = scsi_4btoul(cdb->length); 10435 break; 10436 } 10437 case UNMAP: { 10438 *lba = 0; 10439 *len = UINT64_MAX; 10440 break; 10441 } 10442 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10443 struct scsi_get_lba_status *cdb; 10444 10445 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10446 *lba = scsi_8btou64(cdb->addr); 10447 *len = UINT32_MAX; 10448 break; 10449 } 10450 default: 10451 return (1); 10452 break; /* NOTREACHED */ 10453 } 10454 10455 return (0); 10456} 10457 10458static ctl_action 10459ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10460 bool seq) 10461{ 10462 uint64_t endlba1, endlba2; 10463 10464 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10465 endlba2 = lba2 + len2 - 1; 10466 10467 if ((endlba1 < lba2) || (endlba2 < lba1)) 10468 return (CTL_ACTION_PASS); 10469 else 10470 return (CTL_ACTION_BLOCK); 10471} 10472 10473static int 10474ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10475{ 10476 struct ctl_ptr_len_flags *ptrlen; 10477 struct scsi_unmap_desc *buf, *end, *range; 10478 uint64_t lba; 10479 uint32_t len; 10480 10481 /* If not UNMAP -- go other way. */ 10482 if (io->io_hdr.io_type != CTL_IO_SCSI || 10483 io->scsiio.cdb[0] != UNMAP) 10484 return (CTL_ACTION_ERROR); 10485 10486 /* If UNMAP without data -- block and wait for data. */ 10487 ptrlen = (struct ctl_ptr_len_flags *) 10488 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10489 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10490 ptrlen->ptr == NULL) 10491 return (CTL_ACTION_BLOCK); 10492 10493 /* UNMAP with data -- check for collision. */ 10494 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10495 end = buf + ptrlen->len / sizeof(*buf); 10496 for (range = buf; range < end; range++) { 10497 lba = scsi_8btou64(range->lba); 10498 len = scsi_4btoul(range->length); 10499 if ((lba < lba2 + len2) && (lba + len > lba2)) 10500 return (CTL_ACTION_BLOCK); 10501 } 10502 return (CTL_ACTION_PASS); 10503} 10504 10505static ctl_action 10506ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10507{ 10508 uint64_t lba1, lba2; 10509 uint64_t len1, len2; 10510 int retval; 10511 10512 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10513 return (CTL_ACTION_ERROR); 10514 10515 retval = ctl_extent_check_unmap(io1, lba2, len2); 10516 if (retval != CTL_ACTION_ERROR) 10517 return (retval); 10518 10519 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10520 return (CTL_ACTION_ERROR); 10521 10522 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10523} 10524 10525static ctl_action 10526ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10527{ 10528 uint64_t lba1, lba2; 10529 uint64_t len1, len2; 10530 10531 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10532 return (CTL_ACTION_ERROR); 10533 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10534 return (CTL_ACTION_ERROR); 10535 10536 if (lba1 + len1 == lba2) 10537 return (CTL_ACTION_BLOCK); 10538 return (CTL_ACTION_PASS); 10539} 10540 10541static ctl_action 10542ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10543 union ctl_io *ooa_io) 10544{ 10545 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10546 ctl_serialize_action *serialize_row; 10547 10548 /* 10549 * The initiator attempted multiple untagged commands at the same 10550 * time. Can't do that. 10551 */ 10552 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10553 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10554 && ((pending_io->io_hdr.nexus.targ_port == 10555 ooa_io->io_hdr.nexus.targ_port) 10556 && (pending_io->io_hdr.nexus.initid == 10557 ooa_io->io_hdr.nexus.initid)) 10558 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10559 CTL_FLAG_STATUS_SENT)) == 0)) 10560 return (CTL_ACTION_OVERLAP); 10561 10562 /* 10563 * The initiator attempted to send multiple tagged commands with 10564 * the same ID. (It's fine if different initiators have the same 10565 * tag ID.) 10566 * 10567 * Even if all of those conditions are true, we don't kill the I/O 10568 * if the command ahead of us has been aborted. We won't end up 10569 * sending it to the FETD, and it's perfectly legal to resend a 10570 * command with the same tag number as long as the previous 10571 * instance of this tag number has been aborted somehow. 10572 */ 10573 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10574 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10575 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10576 && ((pending_io->io_hdr.nexus.targ_port == 10577 ooa_io->io_hdr.nexus.targ_port) 10578 && (pending_io->io_hdr.nexus.initid == 10579 ooa_io->io_hdr.nexus.initid)) 10580 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10581 CTL_FLAG_STATUS_SENT)) == 0)) 10582 return (CTL_ACTION_OVERLAP_TAG); 10583 10584 /* 10585 * If we get a head of queue tag, SAM-3 says that we should 10586 * immediately execute it. 10587 * 10588 * What happens if this command would normally block for some other 10589 * reason? e.g. a request sense with a head of queue tag 10590 * immediately after a write. Normally that would block, but this 10591 * will result in its getting executed immediately... 10592 * 10593 * We currently return "pass" instead of "skip", so we'll end up 10594 * going through the rest of the queue to check for overlapped tags. 10595 * 10596 * XXX KDM check for other types of blockage first?? 10597 */ 10598 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10599 return (CTL_ACTION_PASS); 10600 10601 /* 10602 * Ordered tags have to block until all items ahead of them 10603 * have completed. If we get called with an ordered tag, we always 10604 * block, if something else is ahead of us in the queue. 10605 */ 10606 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10607 return (CTL_ACTION_BLOCK); 10608 10609 /* 10610 * Simple tags get blocked until all head of queue and ordered tags 10611 * ahead of them have completed. I'm lumping untagged commands in 10612 * with simple tags here. XXX KDM is that the right thing to do? 10613 */ 10614 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10615 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10616 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10617 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10618 return (CTL_ACTION_BLOCK); 10619 10620 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10621 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10622 10623 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10624 10625 switch (serialize_row[pending_entry->seridx]) { 10626 case CTL_SER_BLOCK: 10627 return (CTL_ACTION_BLOCK); 10628 case CTL_SER_EXTENT: 10629 return (ctl_extent_check(ooa_io, pending_io, 10630 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10631 case CTL_SER_EXTENTOPT: 10632 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10633 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10634 return (ctl_extent_check(ooa_io, pending_io, 10635 (lun->be_lun && 10636 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10637 return (CTL_ACTION_PASS); 10638 case CTL_SER_EXTENTSEQ: 10639 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 10640 return (ctl_extent_check_seq(ooa_io, pending_io)); 10641 return (CTL_ACTION_PASS); 10642 case CTL_SER_PASS: 10643 return (CTL_ACTION_PASS); 10644 case CTL_SER_BLOCKOPT: 10645 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10646 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10647 return (CTL_ACTION_BLOCK); 10648 return (CTL_ACTION_PASS); 10649 case CTL_SER_SKIP: 10650 return (CTL_ACTION_SKIP); 10651 default: 10652 panic("invalid serialization value %d", 10653 serialize_row[pending_entry->seridx]); 10654 } 10655 10656 return (CTL_ACTION_ERROR); 10657} 10658 10659/* 10660 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10661 * Assumptions: 10662 * - pending_io is generally either incoming, or on the blocked queue 10663 * - starting I/O is the I/O we want to start the check with. 10664 */ 10665static ctl_action 10666ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10667 union ctl_io *starting_io) 10668{ 10669 union ctl_io *ooa_io; 10670 ctl_action action; 10671 10672 mtx_assert(&lun->lun_lock, MA_OWNED); 10673 10674 /* 10675 * Run back along the OOA queue, starting with the current 10676 * blocked I/O and going through every I/O before it on the 10677 * queue. If starting_io is NULL, we'll just end up returning 10678 * CTL_ACTION_PASS. 10679 */ 10680 for (ooa_io = starting_io; ooa_io != NULL; 10681 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10682 ooa_links)){ 10683 10684 /* 10685 * This routine just checks to see whether 10686 * cur_blocked is blocked by ooa_io, which is ahead 10687 * of it in the queue. It doesn't queue/dequeue 10688 * cur_blocked. 10689 */ 10690 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10691 switch (action) { 10692 case CTL_ACTION_BLOCK: 10693 case CTL_ACTION_OVERLAP: 10694 case CTL_ACTION_OVERLAP_TAG: 10695 case CTL_ACTION_SKIP: 10696 case CTL_ACTION_ERROR: 10697 return (action); 10698 break; /* NOTREACHED */ 10699 case CTL_ACTION_PASS: 10700 break; 10701 default: 10702 panic("invalid action %d", action); 10703 break; /* NOTREACHED */ 10704 } 10705 } 10706 10707 return (CTL_ACTION_PASS); 10708} 10709 10710/* 10711 * Assumptions: 10712 * - An I/O has just completed, and has been removed from the per-LUN OOA 10713 * queue, so some items on the blocked queue may now be unblocked. 10714 */ 10715static int 10716ctl_check_blocked(struct ctl_lun *lun) 10717{ 10718 struct ctl_softc *softc = lun->ctl_softc; 10719 union ctl_io *cur_blocked, *next_blocked; 10720 10721 mtx_assert(&lun->lun_lock, MA_OWNED); 10722 10723 /* 10724 * Run forward from the head of the blocked queue, checking each 10725 * entry against the I/Os prior to it on the OOA queue to see if 10726 * there is still any blockage. 10727 * 10728 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10729 * with our removing a variable on it while it is traversing the 10730 * list. 10731 */ 10732 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10733 cur_blocked != NULL; cur_blocked = next_blocked) { 10734 union ctl_io *prev_ooa; 10735 ctl_action action; 10736 10737 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10738 blocked_links); 10739 10740 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10741 ctl_ooaq, ooa_links); 10742 10743 /* 10744 * If cur_blocked happens to be the first item in the OOA 10745 * queue now, prev_ooa will be NULL, and the action 10746 * returned will just be CTL_ACTION_PASS. 10747 */ 10748 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10749 10750 switch (action) { 10751 case CTL_ACTION_BLOCK: 10752 /* Nothing to do here, still blocked */ 10753 break; 10754 case CTL_ACTION_OVERLAP: 10755 case CTL_ACTION_OVERLAP_TAG: 10756 /* 10757 * This shouldn't happen! In theory we've already 10758 * checked this command for overlap... 10759 */ 10760 break; 10761 case CTL_ACTION_PASS: 10762 case CTL_ACTION_SKIP: { 10763 const struct ctl_cmd_entry *entry; 10764 10765 /* 10766 * The skip case shouldn't happen, this transaction 10767 * should have never made it onto the blocked queue. 10768 */ 10769 /* 10770 * This I/O is no longer blocked, we can remove it 10771 * from the blocked queue. Since this is a TAILQ 10772 * (doubly linked list), we can do O(1) removals 10773 * from any place on the list. 10774 */ 10775 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10776 blocked_links); 10777 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10778 10779 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 10780 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 10781 /* 10782 * Need to send IO back to original side to 10783 * run 10784 */ 10785 union ctl_ha_msg msg_info; 10786 10787 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 10788 msg_info.hdr.original_sc = 10789 cur_blocked->io_hdr.original_sc; 10790 msg_info.hdr.serializing_sc = cur_blocked; 10791 msg_info.hdr.msg_type = CTL_MSG_R2R; 10792 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 10793 sizeof(msg_info.hdr), M_NOWAIT); 10794 break; 10795 } 10796 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 10797 10798 /* 10799 * Check this I/O for LUN state changes that may 10800 * have happened while this command was blocked. 10801 * The LUN state may have been changed by a command 10802 * ahead of us in the queue, so we need to re-check 10803 * for any states that can be caused by SCSI 10804 * commands. 10805 */ 10806 if (ctl_scsiio_lun_check(lun, entry, 10807 &cur_blocked->scsiio) == 0) { 10808 cur_blocked->io_hdr.flags |= 10809 CTL_FLAG_IS_WAS_ON_RTR; 10810 ctl_enqueue_rtr(cur_blocked); 10811 } else 10812 ctl_done(cur_blocked); 10813 break; 10814 } 10815 default: 10816 /* 10817 * This probably shouldn't happen -- we shouldn't 10818 * get CTL_ACTION_ERROR, or anything else. 10819 */ 10820 break; 10821 } 10822 } 10823 10824 return (CTL_RETVAL_COMPLETE); 10825} 10826 10827/* 10828 * This routine (with one exception) checks LUN flags that can be set by 10829 * commands ahead of us in the OOA queue. These flags have to be checked 10830 * when a command initially comes in, and when we pull a command off the 10831 * blocked queue and are preparing to execute it. The reason we have to 10832 * check these flags for commands on the blocked queue is that the LUN 10833 * state may have been changed by a command ahead of us while we're on the 10834 * blocked queue. 10835 * 10836 * Ordering is somewhat important with these checks, so please pay 10837 * careful attention to the placement of any new checks. 10838 */ 10839static int 10840ctl_scsiio_lun_check(struct ctl_lun *lun, 10841 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 10842{ 10843 struct ctl_softc *softc = lun->ctl_softc; 10844 int retval; 10845 uint32_t residx; 10846 10847 retval = 0; 10848 10849 mtx_assert(&lun->lun_lock, MA_OWNED); 10850 10851 /* 10852 * If this shelf is a secondary shelf controller, we may have to 10853 * reject some commands disallowed by HA mode and link state. 10854 */ 10855 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 10856 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 10857 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 10858 ctl_set_lun_unavail(ctsio); 10859 retval = 1; 10860 goto bailout; 10861 } 10862 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 10863 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 10864 ctl_set_lun_transit(ctsio); 10865 retval = 1; 10866 goto bailout; 10867 } 10868 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 10869 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 10870 ctl_set_lun_standby(ctsio); 10871 retval = 1; 10872 goto bailout; 10873 } 10874 10875 /* The rest of checks are only done on executing side */ 10876 if (softc->ha_mode == CTL_HA_MODE_XFER) 10877 goto bailout; 10878 } 10879 10880 if (entry->pattern & CTL_LUN_PAT_WRITE) { 10881 if (lun->be_lun && 10882 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 10883 ctl_set_hw_write_protected(ctsio); 10884 retval = 1; 10885 goto bailout; 10886 } 10887 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 10888 .eca_and_aen & SCP_SWP) != 0) { 10889 ctl_set_sense(ctsio, /*current_error*/ 1, 10890 /*sense_key*/ SSD_KEY_DATA_PROTECT, 10891 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 10892 retval = 1; 10893 goto bailout; 10894 } 10895 } 10896 10897 /* 10898 * Check for a reservation conflict. If this command isn't allowed 10899 * even on reserved LUNs, and if this initiator isn't the one who 10900 * reserved us, reject the command with a reservation conflict. 10901 */ 10902 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 10903 if ((lun->flags & CTL_LUN_RESERVED) 10904 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 10905 if (lun->res_idx != residx) { 10906 ctl_set_reservation_conflict(ctsio); 10907 retval = 1; 10908 goto bailout; 10909 } 10910 } 10911 10912 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 10913 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 10914 /* No reservation or command is allowed. */; 10915 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 10916 (lun->res_type == SPR_TYPE_WR_EX || 10917 lun->res_type == SPR_TYPE_WR_EX_RO || 10918 lun->res_type == SPR_TYPE_WR_EX_AR)) { 10919 /* The command is allowed for Write Exclusive resv. */; 10920 } else { 10921 /* 10922 * if we aren't registered or it's a res holder type 10923 * reservation and this isn't the res holder then set a 10924 * conflict. 10925 */ 10926 if (ctl_get_prkey(lun, residx) == 0 10927 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 10928 ctl_set_reservation_conflict(ctsio); 10929 retval = 1; 10930 goto bailout; 10931 } 10932 } 10933 10934 if ((lun->flags & CTL_LUN_OFFLINE) 10935 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0)) { 10936 ctl_set_lun_not_ready(ctsio); 10937 retval = 1; 10938 goto bailout; 10939 } 10940 10941 if ((lun->flags & CTL_LUN_STOPPED) 10942 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 10943 /* "Logical unit not ready, initializing cmd. required" */ 10944 ctl_set_lun_stopped(ctsio); 10945 retval = 1; 10946 goto bailout; 10947 } 10948 10949 if ((lun->flags & CTL_LUN_INOPERABLE) 10950 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 10951 /* "Medium format corrupted" */ 10952 ctl_set_medium_format_corrupted(ctsio); 10953 retval = 1; 10954 goto bailout; 10955 } 10956 10957bailout: 10958 return (retval); 10959} 10960 10961static void 10962ctl_failover_io(union ctl_io *io, int have_lock) 10963{ 10964 ctl_set_busy(&io->scsiio); 10965 ctl_done(io); 10966} 10967 10968static void 10969ctl_failover_lun(struct ctl_lun *lun) 10970{ 10971 struct ctl_softc *softc = lun->ctl_softc; 10972 struct ctl_io_hdr *io, *next_io; 10973 10974 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", lun->lun)); 10975 if (softc->ha_mode == CTL_HA_MODE_XFER) { 10976 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 10977 /* We are master */ 10978 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 10979 if (io->flags & CTL_FLAG_IO_ACTIVE) { 10980 io->flags |= CTL_FLAG_ABORT; 10981 io->flags |= CTL_FLAG_FAILOVER; 10982 } else { /* This can be only due to DATAMOVE */ 10983 io->msg_type = CTL_MSG_DATAMOVE_DONE; 10984 io->flags |= CTL_FLAG_IO_ACTIVE; 10985 io->port_status = 31340; 10986 ctl_enqueue_isc((union ctl_io *)io); 10987 } 10988 } 10989 /* We are slave */ 10990 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 10991 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 10992 if (io->flags & CTL_FLAG_IO_ACTIVE) { 10993 io->flags |= CTL_FLAG_FAILOVER; 10994 } else { 10995 ctl_set_busy(&((union ctl_io *)io)-> 10996 scsiio); 10997 ctl_done((union ctl_io *)io); 10998 } 10999 } 11000 } 11001 } else { /* SERIALIZE modes */ 11002 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11003 next_io) { 11004 /* We are master */ 11005 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11006 TAILQ_REMOVE(&lun->blocked_queue, io, 11007 blocked_links); 11008 io->flags &= ~CTL_FLAG_BLOCKED; 11009 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11010 ctl_free_io((union ctl_io *)io); 11011 } 11012 } 11013 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11014 /* We are master */ 11015 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11016 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11017 ctl_free_io((union ctl_io *)io); 11018 } 11019 /* We are slave */ 11020 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11021 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11022 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11023 ctl_set_busy(&((union ctl_io *)io)-> 11024 scsiio); 11025 ctl_done((union ctl_io *)io); 11026 } 11027 } 11028 } 11029 ctl_check_blocked(lun); 11030 } 11031} 11032 11033static int 11034ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11035{ 11036 struct ctl_lun *lun; 11037 const struct ctl_cmd_entry *entry; 11038 uint32_t initidx, targ_lun; 11039 int retval; 11040 11041 retval = 0; 11042 11043 lun = NULL; 11044 11045 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11046 if ((targ_lun < CTL_MAX_LUNS) 11047 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11048 /* 11049 * If the LUN is invalid, pretend that it doesn't exist. 11050 * It will go away as soon as all pending I/O has been 11051 * completed. 11052 */ 11053 mtx_lock(&lun->lun_lock); 11054 if (lun->flags & CTL_LUN_DISABLED) { 11055 mtx_unlock(&lun->lun_lock); 11056 lun = NULL; 11057 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11058 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11059 } else { 11060 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11061 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11062 lun->be_lun; 11063 11064 /* 11065 * Every I/O goes into the OOA queue for a 11066 * particular LUN, and stays there until completion. 11067 */ 11068#ifdef CTL_TIME_IO 11069 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11070 lun->idle_time += getsbinuptime() - 11071 lun->last_busy; 11072 } 11073#endif 11074 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11075 ooa_links); 11076 } 11077 } else { 11078 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11079 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11080 } 11081 11082 /* Get command entry and return error if it is unsuppotyed. */ 11083 entry = ctl_validate_command(ctsio); 11084 if (entry == NULL) { 11085 if (lun) 11086 mtx_unlock(&lun->lun_lock); 11087 return (retval); 11088 } 11089 11090 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11091 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11092 11093 /* 11094 * Check to see whether we can send this command to LUNs that don't 11095 * exist. This should pretty much only be the case for inquiry 11096 * and request sense. Further checks, below, really require having 11097 * a LUN, so we can't really check the command anymore. Just put 11098 * it on the rtr queue. 11099 */ 11100 if (lun == NULL) { 11101 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11102 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11103 ctl_enqueue_rtr((union ctl_io *)ctsio); 11104 return (retval); 11105 } 11106 11107 ctl_set_unsupported_lun(ctsio); 11108 ctl_done((union ctl_io *)ctsio); 11109 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11110 return (retval); 11111 } else { 11112 /* 11113 * Make sure we support this particular command on this LUN. 11114 * e.g., we don't support writes to the control LUN. 11115 */ 11116 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11117 mtx_unlock(&lun->lun_lock); 11118 ctl_set_invalid_opcode(ctsio); 11119 ctl_done((union ctl_io *)ctsio); 11120 return (retval); 11121 } 11122 } 11123 11124 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11125 11126#ifdef CTL_WITH_CA 11127 /* 11128 * If we've got a request sense, it'll clear the contingent 11129 * allegiance condition. Otherwise, if we have a CA condition for 11130 * this initiator, clear it, because it sent down a command other 11131 * than request sense. 11132 */ 11133 if ((ctsio->cdb[0] != REQUEST_SENSE) 11134 && (ctl_is_set(lun->have_ca, initidx))) 11135 ctl_clear_mask(lun->have_ca, initidx); 11136#endif 11137 11138 /* 11139 * If the command has this flag set, it handles its own unit 11140 * attention reporting, we shouldn't do anything. Otherwise we 11141 * check for any pending unit attentions, and send them back to the 11142 * initiator. We only do this when a command initially comes in, 11143 * not when we pull it off the blocked queue. 11144 * 11145 * According to SAM-3, section 5.3.2, the order that things get 11146 * presented back to the host is basically unit attentions caused 11147 * by some sort of reset event, busy status, reservation conflicts 11148 * or task set full, and finally any other status. 11149 * 11150 * One issue here is that some of the unit attentions we report 11151 * don't fall into the "reset" category (e.g. "reported luns data 11152 * has changed"). So reporting it here, before the reservation 11153 * check, may be technically wrong. I guess the only thing to do 11154 * would be to check for and report the reset events here, and then 11155 * check for the other unit attention types after we check for a 11156 * reservation conflict. 11157 * 11158 * XXX KDM need to fix this 11159 */ 11160 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11161 ctl_ua_type ua_type; 11162 11163 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11164 SSD_TYPE_NONE); 11165 if (ua_type != CTL_UA_NONE) { 11166 mtx_unlock(&lun->lun_lock); 11167 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11168 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11169 ctsio->sense_len = SSD_FULL_SIZE; 11170 ctl_done((union ctl_io *)ctsio); 11171 return (retval); 11172 } 11173 } 11174 11175 11176 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11177 mtx_unlock(&lun->lun_lock); 11178 ctl_done((union ctl_io *)ctsio); 11179 return (retval); 11180 } 11181 11182 /* 11183 * XXX CHD this is where we want to send IO to other side if 11184 * this LUN is secondary on this SC. We will need to make a copy 11185 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11186 * the copy we send as FROM_OTHER. 11187 * We also need to stuff the address of the original IO so we can 11188 * find it easily. Something similar will need be done on the other 11189 * side so when we are done we can find the copy. 11190 */ 11191 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11192 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0) { 11193 union ctl_ha_msg msg_info; 11194 int isc_retval; 11195 11196 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11197 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11198 mtx_unlock(&lun->lun_lock); 11199 11200 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11201 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11202 msg_info.hdr.serializing_sc = NULL; 11203 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11204 msg_info.scsi.tag_num = ctsio->tag_num; 11205 msg_info.scsi.tag_type = ctsio->tag_type; 11206 msg_info.scsi.cdb_len = ctsio->cdb_len; 11207 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11208 11209 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11210 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11211 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11212 ctl_set_busy(ctsio); 11213 ctl_done((union ctl_io *)ctsio); 11214 return (retval); 11215 } 11216 return (retval); 11217 } 11218 11219 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11220 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11221 ctl_ooaq, ooa_links))) { 11222 case CTL_ACTION_BLOCK: 11223 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11224 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11225 blocked_links); 11226 mtx_unlock(&lun->lun_lock); 11227 return (retval); 11228 case CTL_ACTION_PASS: 11229 case CTL_ACTION_SKIP: 11230 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11231 mtx_unlock(&lun->lun_lock); 11232 ctl_enqueue_rtr((union ctl_io *)ctsio); 11233 break; 11234 case CTL_ACTION_OVERLAP: 11235 mtx_unlock(&lun->lun_lock); 11236 ctl_set_overlapped_cmd(ctsio); 11237 ctl_done((union ctl_io *)ctsio); 11238 break; 11239 case CTL_ACTION_OVERLAP_TAG: 11240 mtx_unlock(&lun->lun_lock); 11241 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11242 ctl_done((union ctl_io *)ctsio); 11243 break; 11244 case CTL_ACTION_ERROR: 11245 default: 11246 mtx_unlock(&lun->lun_lock); 11247 ctl_set_internal_failure(ctsio, 11248 /*sks_valid*/ 0, 11249 /*retry_count*/ 0); 11250 ctl_done((union ctl_io *)ctsio); 11251 break; 11252 } 11253 return (retval); 11254} 11255 11256const struct ctl_cmd_entry * 11257ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11258{ 11259 const struct ctl_cmd_entry *entry; 11260 int service_action; 11261 11262 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11263 if (sa) 11264 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11265 if (entry->flags & CTL_CMD_FLAG_SA5) { 11266 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11267 entry = &((const struct ctl_cmd_entry *) 11268 entry->execute)[service_action]; 11269 } 11270 return (entry); 11271} 11272 11273const struct ctl_cmd_entry * 11274ctl_validate_command(struct ctl_scsiio *ctsio) 11275{ 11276 const struct ctl_cmd_entry *entry; 11277 int i, sa; 11278 uint8_t diff; 11279 11280 entry = ctl_get_cmd_entry(ctsio, &sa); 11281 if (entry->execute == NULL) { 11282 if (sa) 11283 ctl_set_invalid_field(ctsio, 11284 /*sks_valid*/ 1, 11285 /*command*/ 1, 11286 /*field*/ 1, 11287 /*bit_valid*/ 1, 11288 /*bit*/ 4); 11289 else 11290 ctl_set_invalid_opcode(ctsio); 11291 ctl_done((union ctl_io *)ctsio); 11292 return (NULL); 11293 } 11294 KASSERT(entry->length > 0, 11295 ("Not defined length for command 0x%02x/0x%02x", 11296 ctsio->cdb[0], ctsio->cdb[1])); 11297 for (i = 1; i < entry->length; i++) { 11298 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11299 if (diff == 0) 11300 continue; 11301 ctl_set_invalid_field(ctsio, 11302 /*sks_valid*/ 1, 11303 /*command*/ 1, 11304 /*field*/ i, 11305 /*bit_valid*/ 1, 11306 /*bit*/ fls(diff) - 1); 11307 ctl_done((union ctl_io *)ctsio); 11308 return (NULL); 11309 } 11310 return (entry); 11311} 11312 11313static int 11314ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11315{ 11316 11317 switch (lun_type) { 11318 case T_PROCESSOR: 11319 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11320 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11321 return (0); 11322 break; 11323 case T_DIRECT: 11324 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11325 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11326 return (0); 11327 break; 11328 default: 11329 return (0); 11330 } 11331 return (1); 11332} 11333 11334static int 11335ctl_scsiio(struct ctl_scsiio *ctsio) 11336{ 11337 int retval; 11338 const struct ctl_cmd_entry *entry; 11339 11340 retval = CTL_RETVAL_COMPLETE; 11341 11342 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11343 11344 entry = ctl_get_cmd_entry(ctsio, NULL); 11345 11346 /* 11347 * If this I/O has been aborted, just send it straight to 11348 * ctl_done() without executing it. 11349 */ 11350 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11351 ctl_done((union ctl_io *)ctsio); 11352 goto bailout; 11353 } 11354 11355 /* 11356 * All the checks should have been handled by ctl_scsiio_precheck(). 11357 * We should be clear now to just execute the I/O. 11358 */ 11359 retval = entry->execute(ctsio); 11360 11361bailout: 11362 return (retval); 11363} 11364 11365/* 11366 * Since we only implement one target right now, a bus reset simply resets 11367 * our single target. 11368 */ 11369static int 11370ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11371{ 11372 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11373} 11374 11375static int 11376ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11377 ctl_ua_type ua_type) 11378{ 11379 struct ctl_port *port; 11380 struct ctl_lun *lun; 11381 int retval; 11382 11383 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11384 union ctl_ha_msg msg_info; 11385 11386 msg_info.hdr.nexus = io->io_hdr.nexus; 11387 if (ua_type==CTL_UA_TARG_RESET) 11388 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11389 else 11390 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11391 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11392 msg_info.hdr.original_sc = NULL; 11393 msg_info.hdr.serializing_sc = NULL; 11394 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11395 sizeof(msg_info.task), M_WAITOK); 11396 } 11397 retval = 0; 11398 11399 mtx_lock(&softc->ctl_lock); 11400 port = softc->ctl_ports[io->io_hdr.nexus.targ_port]; 11401 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11402 if (port != NULL && 11403 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 11404 continue; 11405 retval += ctl_do_lun_reset(lun, io, ua_type); 11406 } 11407 mtx_unlock(&softc->ctl_lock); 11408 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11409 return (retval); 11410} 11411 11412/* 11413 * The LUN should always be set. The I/O is optional, and is used to 11414 * distinguish between I/Os sent by this initiator, and by other 11415 * initiators. We set unit attention for initiators other than this one. 11416 * SAM-3 is vague on this point. It does say that a unit attention should 11417 * be established for other initiators when a LUN is reset (see section 11418 * 5.7.3), but it doesn't specifically say that the unit attention should 11419 * be established for this particular initiator when a LUN is reset. Here 11420 * is the relevant text, from SAM-3 rev 8: 11421 * 11422 * 5.7.2 When a SCSI initiator port aborts its own tasks 11423 * 11424 * When a SCSI initiator port causes its own task(s) to be aborted, no 11425 * notification that the task(s) have been aborted shall be returned to 11426 * the SCSI initiator port other than the completion response for the 11427 * command or task management function action that caused the task(s) to 11428 * be aborted and notification(s) associated with related effects of the 11429 * action (e.g., a reset unit attention condition). 11430 * 11431 * XXX KDM for now, we're setting unit attention for all initiators. 11432 */ 11433static int 11434ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11435{ 11436 union ctl_io *xio; 11437#if 0 11438 uint32_t initidx; 11439#endif 11440#ifdef CTL_WITH_CA 11441 int i; 11442#endif 11443 11444 mtx_lock(&lun->lun_lock); 11445 /* 11446 * Run through the OOA queue and abort each I/O. 11447 */ 11448 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11449 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11450 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11451 } 11452 11453 /* 11454 * This version sets unit attention for every 11455 */ 11456#if 0 11457 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11458 ctl_est_ua_all(lun, initidx, ua_type); 11459#else 11460 ctl_est_ua_all(lun, -1, ua_type); 11461#endif 11462 11463 /* 11464 * A reset (any kind, really) clears reservations established with 11465 * RESERVE/RELEASE. It does not clear reservations established 11466 * with PERSISTENT RESERVE OUT, but we don't support that at the 11467 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11468 * reservations made with the RESERVE/RELEASE commands, because 11469 * those commands are obsolete in SPC-3. 11470 */ 11471 lun->flags &= ~CTL_LUN_RESERVED; 11472 11473#ifdef CTL_WITH_CA 11474 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11475 ctl_clear_mask(lun->have_ca, i); 11476#endif 11477 mtx_unlock(&lun->lun_lock); 11478 11479 return (0); 11480} 11481 11482static int 11483ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11484{ 11485 struct ctl_lun *lun; 11486 uint32_t targ_lun; 11487 int retval; 11488 11489 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11490 mtx_lock(&softc->ctl_lock); 11491 if ((targ_lun >= CTL_MAX_LUNS) || 11492 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11493 mtx_unlock(&softc->ctl_lock); 11494 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11495 return (1); 11496 } 11497 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 11498 mtx_unlock(&softc->ctl_lock); 11499 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11500 11501 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11502 union ctl_ha_msg msg_info; 11503 11504 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11505 msg_info.hdr.nexus = io->io_hdr.nexus; 11506 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11507 msg_info.hdr.original_sc = NULL; 11508 msg_info.hdr.serializing_sc = NULL; 11509 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11510 sizeof(msg_info.task), M_WAITOK); 11511 } 11512 return (retval); 11513} 11514 11515static void 11516ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11517 int other_sc) 11518{ 11519 union ctl_io *xio; 11520 11521 mtx_assert(&lun->lun_lock, MA_OWNED); 11522 11523 /* 11524 * Run through the OOA queue and attempt to find the given I/O. 11525 * The target port, initiator ID, tag type and tag number have to 11526 * match the values that we got from the initiator. If we have an 11527 * untagged command to abort, simply abort the first untagged command 11528 * we come to. We only allow one untagged command at a time of course. 11529 */ 11530 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11531 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11532 11533 if ((targ_port == UINT32_MAX || 11534 targ_port == xio->io_hdr.nexus.targ_port) && 11535 (init_id == UINT32_MAX || 11536 init_id == xio->io_hdr.nexus.initid)) { 11537 if (targ_port != xio->io_hdr.nexus.targ_port || 11538 init_id != xio->io_hdr.nexus.initid) 11539 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11540 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11541 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11542 union ctl_ha_msg msg_info; 11543 11544 msg_info.hdr.nexus = xio->io_hdr.nexus; 11545 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11546 msg_info.task.tag_num = xio->scsiio.tag_num; 11547 msg_info.task.tag_type = xio->scsiio.tag_type; 11548 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11549 msg_info.hdr.original_sc = NULL; 11550 msg_info.hdr.serializing_sc = NULL; 11551 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11552 sizeof(msg_info.task), M_NOWAIT); 11553 } 11554 } 11555 } 11556} 11557 11558static int 11559ctl_abort_task_set(union ctl_io *io) 11560{ 11561 struct ctl_softc *softc = control_softc; 11562 struct ctl_lun *lun; 11563 uint32_t targ_lun; 11564 11565 /* 11566 * Look up the LUN. 11567 */ 11568 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11569 mtx_lock(&softc->ctl_lock); 11570 if ((targ_lun >= CTL_MAX_LUNS) || 11571 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11572 mtx_unlock(&softc->ctl_lock); 11573 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11574 return (1); 11575 } 11576 11577 mtx_lock(&lun->lun_lock); 11578 mtx_unlock(&softc->ctl_lock); 11579 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11580 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11581 io->io_hdr.nexus.initid, 11582 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11583 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11584 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11585 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11586 } 11587 mtx_unlock(&lun->lun_lock); 11588 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11589 return (0); 11590} 11591 11592static int 11593ctl_i_t_nexus_reset(union ctl_io *io) 11594{ 11595 struct ctl_softc *softc = control_softc; 11596 struct ctl_lun *lun; 11597 uint32_t initidx; 11598 11599 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11600 union ctl_ha_msg msg_info; 11601 11602 msg_info.hdr.nexus = io->io_hdr.nexus; 11603 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 11604 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11605 msg_info.hdr.original_sc = NULL; 11606 msg_info.hdr.serializing_sc = NULL; 11607 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11608 sizeof(msg_info.task), M_WAITOK); 11609 } 11610 11611 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11612 mtx_lock(&softc->ctl_lock); 11613 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11614 mtx_lock(&lun->lun_lock); 11615 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11616 io->io_hdr.nexus.initid, 1); 11617#ifdef CTL_WITH_CA 11618 ctl_clear_mask(lun->have_ca, initidx); 11619#endif 11620 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 11621 lun->flags &= ~CTL_LUN_RESERVED; 11622 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 11623 mtx_unlock(&lun->lun_lock); 11624 } 11625 mtx_unlock(&softc->ctl_lock); 11626 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11627 return (0); 11628} 11629 11630static int 11631ctl_abort_task(union ctl_io *io) 11632{ 11633 union ctl_io *xio; 11634 struct ctl_lun *lun; 11635 struct ctl_softc *softc; 11636#if 0 11637 struct sbuf sb; 11638 char printbuf[128]; 11639#endif 11640 int found; 11641 uint32_t targ_lun; 11642 11643 softc = control_softc; 11644 found = 0; 11645 11646 /* 11647 * Look up the LUN. 11648 */ 11649 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11650 mtx_lock(&softc->ctl_lock); 11651 if ((targ_lun >= CTL_MAX_LUNS) || 11652 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11653 mtx_unlock(&softc->ctl_lock); 11654 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11655 return (1); 11656 } 11657 11658#if 0 11659 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11660 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11661#endif 11662 11663 mtx_lock(&lun->lun_lock); 11664 mtx_unlock(&softc->ctl_lock); 11665 /* 11666 * Run through the OOA queue and attempt to find the given I/O. 11667 * The target port, initiator ID, tag type and tag number have to 11668 * match the values that we got from the initiator. If we have an 11669 * untagged command to abort, simply abort the first untagged command 11670 * we come to. We only allow one untagged command at a time of course. 11671 */ 11672 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11673 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11674#if 0 11675 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11676 11677 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11678 lun->lun, xio->scsiio.tag_num, 11679 xio->scsiio.tag_type, 11680 (xio->io_hdr.blocked_links.tqe_prev 11681 == NULL) ? "" : " BLOCKED", 11682 (xio->io_hdr.flags & 11683 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11684 (xio->io_hdr.flags & 11685 CTL_FLAG_ABORT) ? " ABORT" : "", 11686 (xio->io_hdr.flags & 11687 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11688 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11689 sbuf_finish(&sb); 11690 printf("%s\n", sbuf_data(&sb)); 11691#endif 11692 11693 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 11694 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 11695 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 11696 continue; 11697 11698 /* 11699 * If the abort says that the task is untagged, the 11700 * task in the queue must be untagged. Otherwise, 11701 * we just check to see whether the tag numbers 11702 * match. This is because the QLogic firmware 11703 * doesn't pass back the tag type in an abort 11704 * request. 11705 */ 11706#if 0 11707 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 11708 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 11709 || (xio->scsiio.tag_num == io->taskio.tag_num)) 11710#endif 11711 /* 11712 * XXX KDM we've got problems with FC, because it 11713 * doesn't send down a tag type with aborts. So we 11714 * can only really go by the tag number... 11715 * This may cause problems with parallel SCSI. 11716 * Need to figure that out!! 11717 */ 11718 if (xio->scsiio.tag_num == io->taskio.tag_num) { 11719 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11720 found = 1; 11721 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 11722 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11723 union ctl_ha_msg msg_info; 11724 11725 msg_info.hdr.nexus = io->io_hdr.nexus; 11726 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11727 msg_info.task.tag_num = io->taskio.tag_num; 11728 msg_info.task.tag_type = io->taskio.tag_type; 11729 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11730 msg_info.hdr.original_sc = NULL; 11731 msg_info.hdr.serializing_sc = NULL; 11732#if 0 11733 printf("Sent Abort to other side\n"); 11734#endif 11735 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11736 sizeof(msg_info.task), M_NOWAIT); 11737 } 11738#if 0 11739 printf("ctl_abort_task: found I/O to abort\n"); 11740#endif 11741 } 11742 } 11743 mtx_unlock(&lun->lun_lock); 11744 11745 if (found == 0) { 11746 /* 11747 * This isn't really an error. It's entirely possible for 11748 * the abort and command completion to cross on the wire. 11749 * This is more of an informative/diagnostic error. 11750 */ 11751#if 0 11752 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 11753 "%u:%u:%u tag %d type %d\n", 11754 io->io_hdr.nexus.initid, 11755 io->io_hdr.nexus.targ_port, 11756 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 11757 io->taskio.tag_type); 11758#endif 11759 } 11760 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11761 return (0); 11762} 11763 11764static int 11765ctl_query_task(union ctl_io *io, int task_set) 11766{ 11767 union ctl_io *xio; 11768 struct ctl_lun *lun; 11769 struct ctl_softc *softc; 11770 int found = 0; 11771 uint32_t targ_lun; 11772 11773 softc = control_softc; 11774 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11775 mtx_lock(&softc->ctl_lock); 11776 if ((targ_lun >= CTL_MAX_LUNS) || 11777 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11778 mtx_unlock(&softc->ctl_lock); 11779 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11780 return (1); 11781 } 11782 mtx_lock(&lun->lun_lock); 11783 mtx_unlock(&softc->ctl_lock); 11784 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11785 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11786 11787 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 11788 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 11789 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 11790 continue; 11791 11792 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 11793 found = 1; 11794 break; 11795 } 11796 } 11797 mtx_unlock(&lun->lun_lock); 11798 if (found) 11799 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 11800 else 11801 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11802 return (0); 11803} 11804 11805static int 11806ctl_query_async_event(union ctl_io *io) 11807{ 11808 struct ctl_lun *lun; 11809 struct ctl_softc *softc; 11810 ctl_ua_type ua; 11811 uint32_t targ_lun, initidx; 11812 11813 softc = control_softc; 11814 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11815 mtx_lock(&softc->ctl_lock); 11816 if ((targ_lun >= CTL_MAX_LUNS) || 11817 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11818 mtx_unlock(&softc->ctl_lock); 11819 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11820 return (1); 11821 } 11822 mtx_lock(&lun->lun_lock); 11823 mtx_unlock(&softc->ctl_lock); 11824 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11825 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 11826 mtx_unlock(&lun->lun_lock); 11827 if (ua != CTL_UA_NONE) 11828 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 11829 else 11830 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11831 return (0); 11832} 11833 11834static void 11835ctl_run_task(union ctl_io *io) 11836{ 11837 struct ctl_softc *softc = control_softc; 11838 int retval = 1; 11839 11840 CTL_DEBUG_PRINT(("ctl_run_task\n")); 11841 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 11842 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 11843 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 11844 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 11845 switch (io->taskio.task_action) { 11846 case CTL_TASK_ABORT_TASK: 11847 retval = ctl_abort_task(io); 11848 break; 11849 case CTL_TASK_ABORT_TASK_SET: 11850 case CTL_TASK_CLEAR_TASK_SET: 11851 retval = ctl_abort_task_set(io); 11852 break; 11853 case CTL_TASK_CLEAR_ACA: 11854 break; 11855 case CTL_TASK_I_T_NEXUS_RESET: 11856 retval = ctl_i_t_nexus_reset(io); 11857 break; 11858 case CTL_TASK_LUN_RESET: 11859 retval = ctl_lun_reset(softc, io); 11860 break; 11861 case CTL_TASK_TARGET_RESET: 11862 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 11863 break; 11864 case CTL_TASK_BUS_RESET: 11865 retval = ctl_bus_reset(softc, io); 11866 break; 11867 case CTL_TASK_PORT_LOGIN: 11868 break; 11869 case CTL_TASK_PORT_LOGOUT: 11870 break; 11871 case CTL_TASK_QUERY_TASK: 11872 retval = ctl_query_task(io, 0); 11873 break; 11874 case CTL_TASK_QUERY_TASK_SET: 11875 retval = ctl_query_task(io, 1); 11876 break; 11877 case CTL_TASK_QUERY_ASYNC_EVENT: 11878 retval = ctl_query_async_event(io); 11879 break; 11880 default: 11881 printf("%s: got unknown task management event %d\n", 11882 __func__, io->taskio.task_action); 11883 break; 11884 } 11885 if (retval == 0) 11886 io->io_hdr.status = CTL_SUCCESS; 11887 else 11888 io->io_hdr.status = CTL_ERROR; 11889 ctl_done(io); 11890} 11891 11892/* 11893 * For HA operation. Handle commands that come in from the other 11894 * controller. 11895 */ 11896static void 11897ctl_handle_isc(union ctl_io *io) 11898{ 11899 int free_io; 11900 struct ctl_lun *lun; 11901 struct ctl_softc *softc; 11902 uint32_t targ_lun; 11903 11904 softc = control_softc; 11905 11906 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11907 lun = softc->ctl_luns[targ_lun]; 11908 11909 switch (io->io_hdr.msg_type) { 11910 case CTL_MSG_SERIALIZE: 11911 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 11912 break; 11913 case CTL_MSG_R2R: { 11914 const struct ctl_cmd_entry *entry; 11915 11916 /* 11917 * This is only used in SER_ONLY mode. 11918 */ 11919 free_io = 0; 11920 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 11921 mtx_lock(&lun->lun_lock); 11922 if (ctl_scsiio_lun_check(lun, 11923 entry, (struct ctl_scsiio *)io) != 0) { 11924 mtx_unlock(&lun->lun_lock); 11925 ctl_done(io); 11926 break; 11927 } 11928 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11929 mtx_unlock(&lun->lun_lock); 11930 ctl_enqueue_rtr(io); 11931 break; 11932 } 11933 case CTL_MSG_FINISH_IO: 11934 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11935 free_io = 0; 11936 ctl_done(io); 11937 } else { 11938 free_io = 1; 11939 mtx_lock(&lun->lun_lock); 11940 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 11941 ooa_links); 11942 ctl_check_blocked(lun); 11943 mtx_unlock(&lun->lun_lock); 11944 } 11945 break; 11946 case CTL_MSG_PERS_ACTION: 11947 ctl_hndl_per_res_out_on_other_sc( 11948 (union ctl_ha_msg *)&io->presio.pr_msg); 11949 free_io = 1; 11950 break; 11951 case CTL_MSG_BAD_JUJU: 11952 free_io = 0; 11953 ctl_done(io); 11954 break; 11955 case CTL_MSG_DATAMOVE: 11956 /* Only used in XFER mode */ 11957 free_io = 0; 11958 ctl_datamove_remote(io); 11959 break; 11960 case CTL_MSG_DATAMOVE_DONE: 11961 /* Only used in XFER mode */ 11962 free_io = 0; 11963 io->scsiio.be_move_done(io); 11964 break; 11965 case CTL_MSG_FAILOVER: 11966 mtx_lock(&lun->lun_lock); 11967 ctl_failover_lun(lun); 11968 mtx_unlock(&lun->lun_lock); 11969 free_io = 1; 11970 break; 11971 default: 11972 free_io = 1; 11973 printf("%s: Invalid message type %d\n", 11974 __func__, io->io_hdr.msg_type); 11975 break; 11976 } 11977 if (free_io) 11978 ctl_free_io(io); 11979 11980} 11981 11982 11983/* 11984 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 11985 * there is no match. 11986 */ 11987static ctl_lun_error_pattern 11988ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 11989{ 11990 const struct ctl_cmd_entry *entry; 11991 ctl_lun_error_pattern filtered_pattern, pattern; 11992 11993 pattern = desc->error_pattern; 11994 11995 /* 11996 * XXX KDM we need more data passed into this function to match a 11997 * custom pattern, and we actually need to implement custom pattern 11998 * matching. 11999 */ 12000 if (pattern & CTL_LUN_PAT_CMD) 12001 return (CTL_LUN_PAT_CMD); 12002 12003 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12004 return (CTL_LUN_PAT_ANY); 12005 12006 entry = ctl_get_cmd_entry(ctsio, NULL); 12007 12008 filtered_pattern = entry->pattern & pattern; 12009 12010 /* 12011 * If the user requested specific flags in the pattern (e.g. 12012 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12013 * flags. 12014 * 12015 * If the user did not specify any flags, it doesn't matter whether 12016 * or not the command supports the flags. 12017 */ 12018 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12019 (pattern & ~CTL_LUN_PAT_MASK)) 12020 return (CTL_LUN_PAT_NONE); 12021 12022 /* 12023 * If the user asked for a range check, see if the requested LBA 12024 * range overlaps with this command's LBA range. 12025 */ 12026 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12027 uint64_t lba1; 12028 uint64_t len1; 12029 ctl_action action; 12030 int retval; 12031 12032 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12033 if (retval != 0) 12034 return (CTL_LUN_PAT_NONE); 12035 12036 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12037 desc->lba_range.len, FALSE); 12038 /* 12039 * A "pass" means that the LBA ranges don't overlap, so 12040 * this doesn't match the user's range criteria. 12041 */ 12042 if (action == CTL_ACTION_PASS) 12043 return (CTL_LUN_PAT_NONE); 12044 } 12045 12046 return (filtered_pattern); 12047} 12048 12049static void 12050ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12051{ 12052 struct ctl_error_desc *desc, *desc2; 12053 12054 mtx_assert(&lun->lun_lock, MA_OWNED); 12055 12056 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12057 ctl_lun_error_pattern pattern; 12058 /* 12059 * Check to see whether this particular command matches 12060 * the pattern in the descriptor. 12061 */ 12062 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12063 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12064 continue; 12065 12066 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12067 case CTL_LUN_INJ_ABORTED: 12068 ctl_set_aborted(&io->scsiio); 12069 break; 12070 case CTL_LUN_INJ_MEDIUM_ERR: 12071 ctl_set_medium_error(&io->scsiio, 12072 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12073 CTL_FLAG_DATA_OUT); 12074 break; 12075 case CTL_LUN_INJ_UA: 12076 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12077 * OCCURRED */ 12078 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12079 break; 12080 case CTL_LUN_INJ_CUSTOM: 12081 /* 12082 * We're assuming the user knows what he is doing. 12083 * Just copy the sense information without doing 12084 * checks. 12085 */ 12086 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12087 MIN(sizeof(desc->custom_sense), 12088 sizeof(io->scsiio.sense_data))); 12089 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12090 io->scsiio.sense_len = SSD_FULL_SIZE; 12091 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12092 break; 12093 case CTL_LUN_INJ_NONE: 12094 default: 12095 /* 12096 * If this is an error injection type we don't know 12097 * about, clear the continuous flag (if it is set) 12098 * so it will get deleted below. 12099 */ 12100 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12101 break; 12102 } 12103 /* 12104 * By default, each error injection action is a one-shot 12105 */ 12106 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12107 continue; 12108 12109 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12110 12111 free(desc, M_CTL); 12112 } 12113} 12114 12115#ifdef CTL_IO_DELAY 12116static void 12117ctl_datamove_timer_wakeup(void *arg) 12118{ 12119 union ctl_io *io; 12120 12121 io = (union ctl_io *)arg; 12122 12123 ctl_datamove(io); 12124} 12125#endif /* CTL_IO_DELAY */ 12126 12127void 12128ctl_datamove(union ctl_io *io) 12129{ 12130 struct ctl_lun *lun; 12131 void (*fe_datamove)(union ctl_io *io); 12132 12133 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12134 12135 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12136 12137 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12138#ifdef CTL_TIME_IO 12139 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12140 char str[256]; 12141 char path_str[64]; 12142 struct sbuf sb; 12143 12144 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12145 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12146 12147 sbuf_cat(&sb, path_str); 12148 switch (io->io_hdr.io_type) { 12149 case CTL_IO_SCSI: 12150 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12151 sbuf_printf(&sb, "\n"); 12152 sbuf_cat(&sb, path_str); 12153 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12154 io->scsiio.tag_num, io->scsiio.tag_type); 12155 break; 12156 case CTL_IO_TASK: 12157 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12158 "Tag Type: %d\n", io->taskio.task_action, 12159 io->taskio.tag_num, io->taskio.tag_type); 12160 break; 12161 default: 12162 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12163 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12164 break; 12165 } 12166 sbuf_cat(&sb, path_str); 12167 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12168 (intmax_t)time_uptime - io->io_hdr.start_time); 12169 sbuf_finish(&sb); 12170 printf("%s", sbuf_data(&sb)); 12171 } 12172#endif /* CTL_TIME_IO */ 12173 12174#ifdef CTL_IO_DELAY 12175 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12176 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12177 } else { 12178 if ((lun != NULL) 12179 && (lun->delay_info.datamove_delay > 0)) { 12180 12181 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12182 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12183 callout_reset(&io->io_hdr.delay_callout, 12184 lun->delay_info.datamove_delay * hz, 12185 ctl_datamove_timer_wakeup, io); 12186 if (lun->delay_info.datamove_type == 12187 CTL_DELAY_TYPE_ONESHOT) 12188 lun->delay_info.datamove_delay = 0; 12189 return; 12190 } 12191 } 12192#endif 12193 12194 /* 12195 * This command has been aborted. Set the port status, so we fail 12196 * the data move. 12197 */ 12198 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12199 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12200 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12201 io->io_hdr.nexus.targ_port, 12202 io->io_hdr.nexus.targ_lun); 12203 io->io_hdr.port_status = 31337; 12204 /* 12205 * Note that the backend, in this case, will get the 12206 * callback in its context. In other cases it may get 12207 * called in the frontend's interrupt thread context. 12208 */ 12209 io->scsiio.be_move_done(io); 12210 return; 12211 } 12212 12213 /* Don't confuse frontend with zero length data move. */ 12214 if (io->scsiio.kern_data_len == 0) { 12215 io->scsiio.be_move_done(io); 12216 return; 12217 } 12218 12219 /* 12220 * If we're in XFER mode and this I/O is from the other shelf 12221 * controller, we need to send the DMA to the other side to 12222 * actually transfer the data to/from the host. In serialize only 12223 * mode the transfer happens below CTL and ctl_datamove() is only 12224 * called on the machine that originally received the I/O. 12225 */ 12226 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12227 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12228 union ctl_ha_msg msg; 12229 uint32_t sg_entries_sent; 12230 int do_sg_copy; 12231 int i; 12232 12233 memset(&msg, 0, sizeof(msg)); 12234 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12235 msg.hdr.original_sc = io->io_hdr.original_sc; 12236 msg.hdr.serializing_sc = io; 12237 msg.hdr.nexus = io->io_hdr.nexus; 12238 msg.hdr.status = io->io_hdr.status; 12239 msg.dt.flags = io->io_hdr.flags; 12240 /* 12241 * We convert everything into a S/G list here. We can't 12242 * pass by reference, only by value between controllers. 12243 * So we can't pass a pointer to the S/G list, only as many 12244 * S/G entries as we can fit in here. If it's possible for 12245 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12246 * then we need to break this up into multiple transfers. 12247 */ 12248 if (io->scsiio.kern_sg_entries == 0) { 12249 msg.dt.kern_sg_entries = 1; 12250#if 0 12251 /* 12252 * Convert to a physical address if this is a 12253 * virtual address. 12254 */ 12255 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12256 msg.dt.sg_list[0].addr = 12257 io->scsiio.kern_data_ptr; 12258 } else { 12259 /* 12260 * XXX KDM use busdma here! 12261 */ 12262 msg.dt.sg_list[0].addr = (void *) 12263 vtophys(io->scsiio.kern_data_ptr); 12264 } 12265#else 12266 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12267 ("HA does not support BUS_ADDR")); 12268 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 12269#endif 12270 12271 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12272 do_sg_copy = 0; 12273 } else { 12274 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12275 do_sg_copy = 1; 12276 } 12277 12278 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12279 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12280 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12281 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12282 msg.dt.sg_sequence = 0; 12283 12284 /* 12285 * Loop until we've sent all of the S/G entries. On the 12286 * other end, we'll recompose these S/G entries into one 12287 * contiguous list before passing it to the 12288 */ 12289 for (sg_entries_sent = 0; sg_entries_sent < 12290 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12291 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/ 12292 sizeof(msg.dt.sg_list[0])), 12293 msg.dt.kern_sg_entries - sg_entries_sent); 12294 12295 if (do_sg_copy != 0) { 12296 struct ctl_sg_entry *sgl; 12297 int j; 12298 12299 sgl = (struct ctl_sg_entry *) 12300 io->scsiio.kern_data_ptr; 12301 /* 12302 * If this is in cached memory, flush the cache 12303 * before we send the DMA request to the other 12304 * controller. We want to do this in either 12305 * the * read or the write case. The read 12306 * case is straightforward. In the write 12307 * case, we want to make sure nothing is 12308 * in the local cache that could overwrite 12309 * the DMAed data. 12310 */ 12311 12312 for (i = sg_entries_sent, j = 0; 12313 i < msg.dt.cur_sg_entries; i++, j++) { 12314#if 0 12315 if ((io->io_hdr.flags & 12316 CTL_FLAG_BUS_ADDR) == 0) { 12317 /* 12318 * XXX KDM use busdma. 12319 */ 12320 msg.dt.sg_list[j].addr =(void *) 12321 vtophys(sgl[i].addr); 12322 } else { 12323 msg.dt.sg_list[j].addr = 12324 sgl[i].addr; 12325 } 12326#else 12327 KASSERT((io->io_hdr.flags & 12328 CTL_FLAG_BUS_ADDR) == 0, 12329 ("HA does not support BUS_ADDR")); 12330 msg.dt.sg_list[j].addr = sgl[i].addr; 12331#endif 12332 msg.dt.sg_list[j].len = sgl[i].len; 12333 } 12334 } 12335 12336 sg_entries_sent += msg.dt.cur_sg_entries; 12337 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12338 msg.dt.sg_last = 1; 12339 else 12340 msg.dt.sg_last = 0; 12341 12342 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12343 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 12344 sizeof(struct ctl_sg_entry)*msg.dt.cur_sg_entries, 12345 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 12346 io->io_hdr.port_status = 31341; 12347 io->scsiio.be_move_done(io); 12348 return; 12349 } 12350 12351 msg.dt.sent_sg_entries = sg_entries_sent; 12352 } 12353 12354 /* 12355 * Officially handover the request from us to peer. 12356 * If failover has just happened, then we must return error. 12357 * If failover happen just after, then it is not our problem. 12358 */ 12359 if (lun) 12360 mtx_lock(&lun->lun_lock); 12361 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12362 if (lun) 12363 mtx_unlock(&lun->lun_lock); 12364 io->io_hdr.port_status = 31342; 12365 io->scsiio.be_move_done(io); 12366 return; 12367 } 12368 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12369 if (lun) 12370 mtx_unlock(&lun->lun_lock); 12371 } else { 12372 12373 /* 12374 * Lookup the fe_datamove() function for this particular 12375 * front end. 12376 */ 12377 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12378 12379 fe_datamove(io); 12380 } 12381} 12382 12383static void 12384ctl_send_datamove_done(union ctl_io *io, int have_lock) 12385{ 12386 union ctl_ha_msg msg; 12387 12388 memset(&msg, 0, sizeof(msg)); 12389 12390 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12391 msg.hdr.original_sc = io; 12392 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12393 msg.hdr.nexus = io->io_hdr.nexus; 12394 msg.hdr.status = io->io_hdr.status; 12395 msg.scsi.tag_num = io->scsiio.tag_num; 12396 msg.scsi.tag_type = io->scsiio.tag_type; 12397 msg.scsi.scsi_status = io->scsiio.scsi_status; 12398 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12399 io->scsiio.sense_len); 12400 msg.scsi.sense_len = io->scsiio.sense_len; 12401 msg.scsi.sense_residual = io->scsiio.sense_residual; 12402 msg.scsi.fetd_status = io->io_hdr.port_status; 12403 msg.scsi.residual = io->scsiio.residual; 12404 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12405 12406 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12407 ctl_failover_io(io, /*have_lock*/ have_lock); 12408 return; 12409 } 12410 12411 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12412 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12413 msg.scsi.sense_len, M_WAITOK); 12414} 12415 12416/* 12417 * The DMA to the remote side is done, now we need to tell the other side 12418 * we're done so it can continue with its data movement. 12419 */ 12420static void 12421ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12422{ 12423 union ctl_io *io; 12424 int i; 12425 12426 io = rq->context; 12427 12428 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12429 printf("%s: ISC DMA write failed with error %d", __func__, 12430 rq->ret); 12431 ctl_set_internal_failure(&io->scsiio, 12432 /*sks_valid*/ 1, 12433 /*retry_count*/ rq->ret); 12434 } 12435 12436 ctl_dt_req_free(rq); 12437 12438 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12439 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12440 free(io->io_hdr.remote_sglist, M_CTL); 12441 io->io_hdr.remote_sglist = NULL; 12442 io->io_hdr.local_sglist = NULL; 12443 12444 /* 12445 * The data is in local and remote memory, so now we need to send 12446 * status (good or back) back to the other side. 12447 */ 12448 ctl_send_datamove_done(io, /*have_lock*/ 0); 12449} 12450 12451/* 12452 * We've moved the data from the host/controller into local memory. Now we 12453 * need to push it over to the remote controller's memory. 12454 */ 12455static int 12456ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12457{ 12458 int retval; 12459 12460 retval = 0; 12461 12462 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12463 ctl_datamove_remote_write_cb); 12464 12465 return (retval); 12466} 12467 12468static void 12469ctl_datamove_remote_write(union ctl_io *io) 12470{ 12471 int retval; 12472 void (*fe_datamove)(union ctl_io *io); 12473 12474 /* 12475 * - Get the data from the host/HBA into local memory. 12476 * - DMA memory from the local controller to the remote controller. 12477 * - Send status back to the remote controller. 12478 */ 12479 12480 retval = ctl_datamove_remote_sgl_setup(io); 12481 if (retval != 0) 12482 return; 12483 12484 /* Switch the pointer over so the FETD knows what to do */ 12485 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12486 12487 /* 12488 * Use a custom move done callback, since we need to send completion 12489 * back to the other controller, not to the backend on this side. 12490 */ 12491 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12492 12493 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12494 12495 fe_datamove(io); 12496 12497 return; 12498 12499} 12500 12501static int 12502ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12503{ 12504#if 0 12505 char str[256]; 12506 char path_str[64]; 12507 struct sbuf sb; 12508#endif 12509 int i; 12510 12511 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12512 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12513 free(io->io_hdr.remote_sglist, M_CTL); 12514 io->io_hdr.remote_sglist = NULL; 12515 io->io_hdr.local_sglist = NULL; 12516 12517#if 0 12518 scsi_path_string(io, path_str, sizeof(path_str)); 12519 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12520 sbuf_cat(&sb, path_str); 12521 scsi_command_string(&io->scsiio, NULL, &sb); 12522 sbuf_printf(&sb, "\n"); 12523 sbuf_cat(&sb, path_str); 12524 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12525 io->scsiio.tag_num, io->scsiio.tag_type); 12526 sbuf_cat(&sb, path_str); 12527 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12528 io->io_hdr.flags, io->io_hdr.status); 12529 sbuf_finish(&sb); 12530 printk("%s", sbuf_data(&sb)); 12531#endif 12532 12533 12534 /* 12535 * The read is done, now we need to send status (good or bad) back 12536 * to the other side. 12537 */ 12538 ctl_send_datamove_done(io, /*have_lock*/ 0); 12539 12540 return (0); 12541} 12542 12543static void 12544ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12545{ 12546 union ctl_io *io; 12547 void (*fe_datamove)(union ctl_io *io); 12548 12549 io = rq->context; 12550 12551 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12552 printf("%s: ISC DMA read failed with error %d\n", __func__, 12553 rq->ret); 12554 ctl_set_internal_failure(&io->scsiio, 12555 /*sks_valid*/ 1, 12556 /*retry_count*/ rq->ret); 12557 } 12558 12559 ctl_dt_req_free(rq); 12560 12561 /* Switch the pointer over so the FETD knows what to do */ 12562 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12563 12564 /* 12565 * Use a custom move done callback, since we need to send completion 12566 * back to the other controller, not to the backend on this side. 12567 */ 12568 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12569 12570 /* XXX KDM add checks like the ones in ctl_datamove? */ 12571 12572 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12573 12574 fe_datamove(io); 12575} 12576 12577static int 12578ctl_datamove_remote_sgl_setup(union ctl_io *io) 12579{ 12580 struct ctl_sg_entry *local_sglist, *remote_sglist; 12581 struct ctl_softc *softc; 12582 uint32_t len_to_go; 12583 int retval; 12584 int i; 12585 12586 retval = 0; 12587 softc = control_softc; 12588 local_sglist = io->io_hdr.local_sglist; 12589 remote_sglist = io->io_hdr.remote_sglist; 12590 len_to_go = io->scsiio.kern_data_len; 12591 12592 /* 12593 * The difficult thing here is that the size of the various 12594 * S/G segments may be different than the size from the 12595 * remote controller. That'll make it harder when DMAing 12596 * the data back to the other side. 12597 */ 12598 for (i = 0; len_to_go > 0; i++) { 12599 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12600 local_sglist[i].addr = 12601 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12602 12603 len_to_go -= local_sglist[i].len; 12604 } 12605 /* 12606 * Reset the number of S/G entries accordingly. The original 12607 * number of S/G entries is available in rem_sg_entries. 12608 */ 12609 io->scsiio.kern_sg_entries = i; 12610 12611#if 0 12612 printf("%s: kern_sg_entries = %d\n", __func__, 12613 io->scsiio.kern_sg_entries); 12614 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12615 printf("%s: sg[%d] = %p, %d\n", __func__, i, 12616 local_sglist[i].addr, local_sglist[i].len); 12617#endif 12618 12619 return (retval); 12620} 12621 12622static int 12623ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12624 ctl_ha_dt_cb callback) 12625{ 12626 struct ctl_ha_dt_req *rq; 12627 struct ctl_sg_entry *remote_sglist, *local_sglist; 12628 uint32_t local_used, remote_used, total_used; 12629 int i, j, isc_ret; 12630 12631 rq = ctl_dt_req_alloc(); 12632 12633 /* 12634 * If we failed to allocate the request, and if the DMA didn't fail 12635 * anyway, set busy status. This is just a resource allocation 12636 * failure. 12637 */ 12638 if ((rq == NULL) 12639 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12640 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12641 ctl_set_busy(&io->scsiio); 12642 12643 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12644 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12645 12646 if (rq != NULL) 12647 ctl_dt_req_free(rq); 12648 12649 /* 12650 * The data move failed. We need to return status back 12651 * to the other controller. No point in trying to DMA 12652 * data to the remote controller. 12653 */ 12654 12655 ctl_send_datamove_done(io, /*have_lock*/ 0); 12656 12657 return (1); 12658 } 12659 12660 local_sglist = io->io_hdr.local_sglist; 12661 remote_sglist = io->io_hdr.remote_sglist; 12662 local_used = 0; 12663 remote_used = 0; 12664 total_used = 0; 12665 12666 /* 12667 * Pull/push the data over the wire from/to the other controller. 12668 * This takes into account the possibility that the local and 12669 * remote sglists may not be identical in terms of the size of 12670 * the elements and the number of elements. 12671 * 12672 * One fundamental assumption here is that the length allocated for 12673 * both the local and remote sglists is identical. Otherwise, we've 12674 * essentially got a coding error of some sort. 12675 */ 12676 isc_ret = CTL_HA_STATUS_SUCCESS; 12677 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12678 uint32_t cur_len; 12679 uint8_t *tmp_ptr; 12680 12681 rq->command = command; 12682 rq->context = io; 12683 12684 /* 12685 * Both pointers should be aligned. But it is possible 12686 * that the allocation length is not. They should both 12687 * also have enough slack left over at the end, though, 12688 * to round up to the next 8 byte boundary. 12689 */ 12690 cur_len = MIN(local_sglist[i].len - local_used, 12691 remote_sglist[j].len - remote_used); 12692 rq->size = cur_len; 12693 12694 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12695 tmp_ptr += local_used; 12696 12697#if 0 12698 /* Use physical addresses when talking to ISC hardware */ 12699 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12700 /* XXX KDM use busdma */ 12701 rq->local = vtophys(tmp_ptr); 12702 } else 12703 rq->local = tmp_ptr; 12704#else 12705 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12706 ("HA does not support BUS_ADDR")); 12707 rq->local = tmp_ptr; 12708#endif 12709 12710 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12711 tmp_ptr += remote_used; 12712 rq->remote = tmp_ptr; 12713 12714 rq->callback = NULL; 12715 12716 local_used += cur_len; 12717 if (local_used >= local_sglist[i].len) { 12718 i++; 12719 local_used = 0; 12720 } 12721 12722 remote_used += cur_len; 12723 if (remote_used >= remote_sglist[j].len) { 12724 j++; 12725 remote_used = 0; 12726 } 12727 total_used += cur_len; 12728 12729 if (total_used >= io->scsiio.kern_data_len) 12730 rq->callback = callback; 12731 12732#if 0 12733 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 12734 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12735 rq->local, rq->remote, rq->size); 12736#endif 12737 12738 isc_ret = ctl_dt_single(rq); 12739 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12740 break; 12741 } 12742 if (isc_ret != CTL_HA_STATUS_WAIT) { 12743 rq->ret = isc_ret; 12744 callback(rq); 12745 } 12746 12747 return (0); 12748} 12749 12750static void 12751ctl_datamove_remote_read(union ctl_io *io) 12752{ 12753 int retval; 12754 int i; 12755 12756 /* 12757 * This will send an error to the other controller in the case of a 12758 * failure. 12759 */ 12760 retval = ctl_datamove_remote_sgl_setup(io); 12761 if (retval != 0) 12762 return; 12763 12764 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12765 ctl_datamove_remote_read_cb); 12766 if (retval != 0) { 12767 /* 12768 * Make sure we free memory if there was an error.. The 12769 * ctl_datamove_remote_xfer() function will send the 12770 * datamove done message, or call the callback with an 12771 * error if there is a problem. 12772 */ 12773 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12774 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12775 free(io->io_hdr.remote_sglist, M_CTL); 12776 io->io_hdr.remote_sglist = NULL; 12777 io->io_hdr.local_sglist = NULL; 12778 } 12779 12780 return; 12781} 12782 12783/* 12784 * Process a datamove request from the other controller. This is used for 12785 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12786 * first. Once that is complete, the data gets DMAed into the remote 12787 * controller's memory. For reads, we DMA from the remote controller's 12788 * memory into our memory first, and then move it out to the FETD. 12789 */ 12790static void 12791ctl_datamove_remote(union ctl_io *io) 12792{ 12793 12794 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12795 12796 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12797 ctl_failover_io(io, /*have_lock*/ 0); 12798 return; 12799 } 12800 12801 /* 12802 * Note that we look for an aborted I/O here, but don't do some of 12803 * the other checks that ctl_datamove() normally does. 12804 * We don't need to run the datamove delay code, since that should 12805 * have been done if need be on the other controller. 12806 */ 12807 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12808 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 12809 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12810 io->io_hdr.nexus.targ_port, 12811 io->io_hdr.nexus.targ_lun); 12812 io->io_hdr.port_status = 31338; 12813 ctl_send_datamove_done(io, /*have_lock*/ 0); 12814 return; 12815 } 12816 12817 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 12818 ctl_datamove_remote_write(io); 12819 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 12820 ctl_datamove_remote_read(io); 12821 else { 12822 io->io_hdr.port_status = 31339; 12823 ctl_send_datamove_done(io, /*have_lock*/ 0); 12824 } 12825} 12826 12827static int 12828ctl_process_done(union ctl_io *io) 12829{ 12830 struct ctl_lun *lun; 12831 struct ctl_softc *softc = control_softc; 12832 void (*fe_done)(union ctl_io *io); 12833 union ctl_ha_msg msg; 12834 uint32_t targ_port = io->io_hdr.nexus.targ_port; 12835 12836 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12837 12838 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) 12839 fe_done = softc->ctl_ports[targ_port]->fe_done; 12840 else 12841 fe_done = NULL; 12842 12843#ifdef CTL_TIME_IO 12844 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12845 char str[256]; 12846 char path_str[64]; 12847 struct sbuf sb; 12848 12849 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12850 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12851 12852 sbuf_cat(&sb, path_str); 12853 switch (io->io_hdr.io_type) { 12854 case CTL_IO_SCSI: 12855 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12856 sbuf_printf(&sb, "\n"); 12857 sbuf_cat(&sb, path_str); 12858 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12859 io->scsiio.tag_num, io->scsiio.tag_type); 12860 break; 12861 case CTL_IO_TASK: 12862 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12863 "Tag Type: %d\n", io->taskio.task_action, 12864 io->taskio.tag_num, io->taskio.tag_type); 12865 break; 12866 default: 12867 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12868 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12869 break; 12870 } 12871 sbuf_cat(&sb, path_str); 12872 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 12873 (intmax_t)time_uptime - io->io_hdr.start_time); 12874 sbuf_finish(&sb); 12875 printf("%s", sbuf_data(&sb)); 12876 } 12877#endif /* CTL_TIME_IO */ 12878 12879 switch (io->io_hdr.io_type) { 12880 case CTL_IO_SCSI: 12881 break; 12882 case CTL_IO_TASK: 12883 if (ctl_debug & CTL_DEBUG_INFO) 12884 ctl_io_error_print(io, NULL); 12885 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 12886 ctl_free_io(io); 12887 else 12888 fe_done(io); 12889 return (CTL_RETVAL_COMPLETE); 12890 default: 12891 panic("ctl_process_done: invalid io type %d\n", 12892 io->io_hdr.io_type); 12893 break; /* NOTREACHED */ 12894 } 12895 12896 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12897 if (lun == NULL) { 12898 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 12899 io->io_hdr.nexus.targ_mapped_lun)); 12900 goto bailout; 12901 } 12902 12903 mtx_lock(&lun->lun_lock); 12904 12905 /* 12906 * Check to see if we have any errors to inject here. We only 12907 * inject errors for commands that don't already have errors set. 12908 */ 12909 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 12910 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 12911 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 12912 ctl_inject_error(lun, io); 12913 12914 /* 12915 * XXX KDM how do we treat commands that aren't completed 12916 * successfully? 12917 * 12918 * XXX KDM should we also track I/O latency? 12919 */ 12920 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 12921 io->io_hdr.io_type == CTL_IO_SCSI) { 12922#ifdef CTL_TIME_IO 12923 struct bintime cur_bt; 12924#endif 12925 int type; 12926 12927 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12928 CTL_FLAG_DATA_IN) 12929 type = CTL_STATS_READ; 12930 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12931 CTL_FLAG_DATA_OUT) 12932 type = CTL_STATS_WRITE; 12933 else 12934 type = CTL_STATS_NO_IO; 12935 12936 lun->stats.ports[targ_port].bytes[type] += 12937 io->scsiio.kern_total_len; 12938 lun->stats.ports[targ_port].operations[type]++; 12939#ifdef CTL_TIME_IO 12940 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 12941 &io->io_hdr.dma_bt); 12942 lun->stats.ports[targ_port].num_dmas[type] += 12943 io->io_hdr.num_dmas; 12944 getbintime(&cur_bt); 12945 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 12946 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 12947#endif 12948 } 12949 12950 /* 12951 * Remove this from the OOA queue. 12952 */ 12953 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12954#ifdef CTL_TIME_IO 12955 if (TAILQ_EMPTY(&lun->ooa_queue)) 12956 lun->last_busy = getsbinuptime(); 12957#endif 12958 12959 /* 12960 * Run through the blocked queue on this LUN and see if anything 12961 * has become unblocked, now that this transaction is done. 12962 */ 12963 ctl_check_blocked(lun); 12964 12965 /* 12966 * If the LUN has been invalidated, free it if there is nothing 12967 * left on its OOA queue. 12968 */ 12969 if ((lun->flags & CTL_LUN_INVALID) 12970 && TAILQ_EMPTY(&lun->ooa_queue)) { 12971 mtx_unlock(&lun->lun_lock); 12972 mtx_lock(&softc->ctl_lock); 12973 ctl_free_lun(lun); 12974 mtx_unlock(&softc->ctl_lock); 12975 } else 12976 mtx_unlock(&lun->lun_lock); 12977 12978bailout: 12979 12980 /* 12981 * If this command has been aborted, make sure we set the status 12982 * properly. The FETD is responsible for freeing the I/O and doing 12983 * whatever it needs to do to clean up its state. 12984 */ 12985 if (io->io_hdr.flags & CTL_FLAG_ABORT) 12986 ctl_set_task_aborted(&io->scsiio); 12987 12988 /* 12989 * If enabled, print command error status. 12990 */ 12991 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 12992 (ctl_debug & CTL_DEBUG_INFO) != 0) 12993 ctl_io_error_print(io, NULL); 12994 12995 /* 12996 * Tell the FETD or the other shelf controller we're done with this 12997 * command. Note that only SCSI commands get to this point. Task 12998 * management commands are completed above. 12999 */ 13000 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13001 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13002 memset(&msg, 0, sizeof(msg)); 13003 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13004 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13005 msg.hdr.nexus = io->io_hdr.nexus; 13006 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13007 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13008 M_WAITOK); 13009 } 13010 if ((softc->ha_mode == CTL_HA_MODE_XFER) 13011 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13012 memset(&msg, 0, sizeof(msg)); 13013 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13014 msg.hdr.original_sc = io->io_hdr.original_sc; 13015 msg.hdr.nexus = io->io_hdr.nexus; 13016 msg.hdr.status = io->io_hdr.status; 13017 msg.scsi.scsi_status = io->scsiio.scsi_status; 13018 msg.scsi.tag_num = io->scsiio.tag_num; 13019 msg.scsi.tag_type = io->scsiio.tag_type; 13020 msg.scsi.sense_len = io->scsiio.sense_len; 13021 msg.scsi.sense_residual = io->scsiio.sense_residual; 13022 msg.scsi.residual = io->scsiio.residual; 13023 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13024 io->scsiio.sense_len); 13025 13026 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13027 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 13028 msg.scsi.sense_len, M_WAITOK); 13029 ctl_free_io(io); 13030 } else 13031 fe_done(io); 13032 13033 return (CTL_RETVAL_COMPLETE); 13034} 13035 13036#ifdef CTL_WITH_CA 13037/* 13038 * Front end should call this if it doesn't do autosense. When the request 13039 * sense comes back in from the initiator, we'll dequeue this and send it. 13040 */ 13041int 13042ctl_queue_sense(union ctl_io *io) 13043{ 13044 struct ctl_lun *lun; 13045 struct ctl_port *port; 13046 struct ctl_softc *softc; 13047 uint32_t initidx, targ_lun; 13048 13049 softc = control_softc; 13050 13051 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13052 13053 /* 13054 * LUN lookup will likely move to the ctl_work_thread() once we 13055 * have our new queueing infrastructure (that doesn't put things on 13056 * a per-LUN queue initially). That is so that we can handle 13057 * things like an INQUIRY to a LUN that we don't have enabled. We 13058 * can't deal with that right now. 13059 */ 13060 mtx_lock(&softc->ctl_lock); 13061 13062 /* 13063 * If we don't have a LUN for this, just toss the sense 13064 * information. 13065 */ 13066 port = ctl_io_port(&ctsio->io_hdr); 13067 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13068 if ((targ_lun < CTL_MAX_LUNS) 13069 && (softc->ctl_luns[targ_lun] != NULL)) 13070 lun = softc->ctl_luns[targ_lun]; 13071 else 13072 goto bailout; 13073 13074 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13075 13076 mtx_lock(&lun->lun_lock); 13077 /* 13078 * Already have CA set for this LUN...toss the sense information. 13079 */ 13080 if (ctl_is_set(lun->have_ca, initidx)) { 13081 mtx_unlock(&lun->lun_lock); 13082 goto bailout; 13083 } 13084 13085 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13086 MIN(sizeof(lun->pending_sense[initidx]), 13087 sizeof(io->scsiio.sense_data))); 13088 ctl_set_mask(lun->have_ca, initidx); 13089 mtx_unlock(&lun->lun_lock); 13090 13091bailout: 13092 mtx_unlock(&softc->ctl_lock); 13093 13094 ctl_free_io(io); 13095 13096 return (CTL_RETVAL_COMPLETE); 13097} 13098#endif 13099 13100/* 13101 * Primary command inlet from frontend ports. All SCSI and task I/O 13102 * requests must go through this function. 13103 */ 13104int 13105ctl_queue(union ctl_io *io) 13106{ 13107 struct ctl_port *port; 13108 13109 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13110 13111#ifdef CTL_TIME_IO 13112 io->io_hdr.start_time = time_uptime; 13113 getbintime(&io->io_hdr.start_bt); 13114#endif /* CTL_TIME_IO */ 13115 13116 /* Map FE-specific LUN ID into global one. */ 13117 port = ctl_io_port(&io->io_hdr); 13118 io->io_hdr.nexus.targ_mapped_lun = 13119 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13120 13121 switch (io->io_hdr.io_type) { 13122 case CTL_IO_SCSI: 13123 case CTL_IO_TASK: 13124 if (ctl_debug & CTL_DEBUG_CDB) 13125 ctl_io_print(io); 13126 ctl_enqueue_incoming(io); 13127 break; 13128 default: 13129 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13130 return (EINVAL); 13131 } 13132 13133 return (CTL_RETVAL_COMPLETE); 13134} 13135 13136#ifdef CTL_IO_DELAY 13137static void 13138ctl_done_timer_wakeup(void *arg) 13139{ 13140 union ctl_io *io; 13141 13142 io = (union ctl_io *)arg; 13143 ctl_done(io); 13144} 13145#endif /* CTL_IO_DELAY */ 13146 13147void 13148ctl_done(union ctl_io *io) 13149{ 13150 13151 /* 13152 * Enable this to catch duplicate completion issues. 13153 */ 13154#if 0 13155 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13156 printf("%s: type %d msg %d cdb %x iptl: " 13157 "%u:%u:%u tag 0x%04x " 13158 "flag %#x status %x\n", 13159 __func__, 13160 io->io_hdr.io_type, 13161 io->io_hdr.msg_type, 13162 io->scsiio.cdb[0], 13163 io->io_hdr.nexus.initid, 13164 io->io_hdr.nexus.targ_port, 13165 io->io_hdr.nexus.targ_lun, 13166 (io->io_hdr.io_type == 13167 CTL_IO_TASK) ? 13168 io->taskio.tag_num : 13169 io->scsiio.tag_num, 13170 io->io_hdr.flags, 13171 io->io_hdr.status); 13172 } else 13173 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13174#endif 13175 13176 /* 13177 * This is an internal copy of an I/O, and should not go through 13178 * the normal done processing logic. 13179 */ 13180 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13181 return; 13182 13183#ifdef CTL_IO_DELAY 13184 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13185 struct ctl_lun *lun; 13186 13187 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13188 13189 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13190 } else { 13191 struct ctl_lun *lun; 13192 13193 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13194 13195 if ((lun != NULL) 13196 && (lun->delay_info.done_delay > 0)) { 13197 13198 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13199 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13200 callout_reset(&io->io_hdr.delay_callout, 13201 lun->delay_info.done_delay * hz, 13202 ctl_done_timer_wakeup, io); 13203 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13204 lun->delay_info.done_delay = 0; 13205 return; 13206 } 13207 } 13208#endif /* CTL_IO_DELAY */ 13209 13210 ctl_enqueue_done(io); 13211} 13212 13213static void 13214ctl_work_thread(void *arg) 13215{ 13216 struct ctl_thread *thr = (struct ctl_thread *)arg; 13217 struct ctl_softc *softc = thr->ctl_softc; 13218 union ctl_io *io; 13219 int retval; 13220 13221 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13222 13223 for (;;) { 13224 retval = 0; 13225 13226 /* 13227 * We handle the queues in this order: 13228 * - ISC 13229 * - done queue (to free up resources, unblock other commands) 13230 * - RtR queue 13231 * - incoming queue 13232 * 13233 * If those queues are empty, we break out of the loop and 13234 * go to sleep. 13235 */ 13236 mtx_lock(&thr->queue_lock); 13237 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13238 if (io != NULL) { 13239 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13240 mtx_unlock(&thr->queue_lock); 13241 ctl_handle_isc(io); 13242 continue; 13243 } 13244 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13245 if (io != NULL) { 13246 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13247 /* clear any blocked commands, call fe_done */ 13248 mtx_unlock(&thr->queue_lock); 13249 retval = ctl_process_done(io); 13250 continue; 13251 } 13252 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13253 if (io != NULL) { 13254 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13255 mtx_unlock(&thr->queue_lock); 13256 if (io->io_hdr.io_type == CTL_IO_TASK) 13257 ctl_run_task(io); 13258 else 13259 ctl_scsiio_precheck(softc, &io->scsiio); 13260 continue; 13261 } 13262 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13263 if (io != NULL) { 13264 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13265 mtx_unlock(&thr->queue_lock); 13266 retval = ctl_scsiio(&io->scsiio); 13267 if (retval != CTL_RETVAL_COMPLETE) 13268 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13269 continue; 13270 } 13271 13272 /* Sleep until we have something to do. */ 13273 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13274 } 13275} 13276 13277static void 13278ctl_lun_thread(void *arg) 13279{ 13280 struct ctl_softc *softc = (struct ctl_softc *)arg; 13281 struct ctl_be_lun *be_lun; 13282 int retval; 13283 13284 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13285 13286 for (;;) { 13287 retval = 0; 13288 mtx_lock(&softc->ctl_lock); 13289 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13290 if (be_lun != NULL) { 13291 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13292 mtx_unlock(&softc->ctl_lock); 13293 ctl_create_lun(be_lun); 13294 continue; 13295 } 13296 13297 /* Sleep until we have something to do. */ 13298 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13299 PDROP | PRIBIO, "-", 0); 13300 } 13301} 13302 13303static void 13304ctl_thresh_thread(void *arg) 13305{ 13306 struct ctl_softc *softc = (struct ctl_softc *)arg; 13307 struct ctl_lun *lun; 13308 struct ctl_be_lun *be_lun; 13309 struct scsi_da_rw_recovery_page *rwpage; 13310 struct ctl_logical_block_provisioning_page *page; 13311 const char *attr; 13312 union ctl_ha_msg msg; 13313 uint64_t thres, val; 13314 int i, e, set; 13315 13316 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13317 13318 for (;;) { 13319 mtx_lock(&softc->ctl_lock); 13320 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13321 be_lun = lun->be_lun; 13322 if ((lun->flags & CTL_LUN_DISABLED) || 13323 (lun->flags & CTL_LUN_OFFLINE) || 13324 lun->backend->lun_attr == NULL) 13325 continue; 13326 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13327 softc->ha_mode == CTL_HA_MODE_XFER) 13328 continue; 13329 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13330 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13331 continue; 13332 e = 0; 13333 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13334 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13335 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13336 continue; 13337 thres = scsi_4btoul(page->descr[i].count); 13338 thres <<= CTL_LBP_EXPONENT; 13339 switch (page->descr[i].resource) { 13340 case 0x01: 13341 attr = "blocksavail"; 13342 break; 13343 case 0x02: 13344 attr = "blocksused"; 13345 break; 13346 case 0xf1: 13347 attr = "poolblocksavail"; 13348 break; 13349 case 0xf2: 13350 attr = "poolblocksused"; 13351 break; 13352 default: 13353 continue; 13354 } 13355 mtx_unlock(&softc->ctl_lock); // XXX 13356 val = lun->backend->lun_attr( 13357 lun->be_lun->be_lun, attr); 13358 mtx_lock(&softc->ctl_lock); 13359 if (val == UINT64_MAX) 13360 continue; 13361 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13362 == SLBPPD_ARMING_INC) 13363 e = (val >= thres); 13364 else 13365 e = (val <= thres); 13366 if (e) 13367 break; 13368 } 13369 mtx_lock(&lun->lun_lock); 13370 if (e) { 13371 scsi_u64to8b((uint8_t *)&page->descr[i] - 13372 (uint8_t *)page, lun->ua_tpt_info); 13373 if (lun->lasttpt == 0 || 13374 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13375 lun->lasttpt = time_uptime; 13376 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13377 set = 1; 13378 } else 13379 set = 0; 13380 } else { 13381 lun->lasttpt = 0; 13382 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13383 set = -1; 13384 } 13385 mtx_unlock(&lun->lun_lock); 13386 if (set != 0 && 13387 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13388 /* Send msg to other side. */ 13389 bzero(&msg.ua, sizeof(msg.ua)); 13390 msg.hdr.msg_type = CTL_MSG_UA; 13391 msg.hdr.nexus.initid = -1; 13392 msg.hdr.nexus.targ_port = -1; 13393 msg.hdr.nexus.targ_lun = lun->lun; 13394 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13395 msg.ua.ua_all = 1; 13396 msg.ua.ua_set = (set > 0); 13397 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13398 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13399 mtx_unlock(&softc->ctl_lock); // XXX 13400 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13401 sizeof(msg.ua), M_WAITOK); 13402 mtx_lock(&softc->ctl_lock); 13403 } 13404 } 13405 mtx_unlock(&softc->ctl_lock); 13406 pause("-", CTL_LBP_PERIOD * hz); 13407 } 13408} 13409 13410static void 13411ctl_enqueue_incoming(union ctl_io *io) 13412{ 13413 struct ctl_softc *softc = control_softc; 13414 struct ctl_thread *thr; 13415 u_int idx; 13416 13417 idx = (io->io_hdr.nexus.targ_port * 127 + 13418 io->io_hdr.nexus.initid) % worker_threads; 13419 thr = &softc->threads[idx]; 13420 mtx_lock(&thr->queue_lock); 13421 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13422 mtx_unlock(&thr->queue_lock); 13423 wakeup(thr); 13424} 13425 13426static void 13427ctl_enqueue_rtr(union ctl_io *io) 13428{ 13429 struct ctl_softc *softc = control_softc; 13430 struct ctl_thread *thr; 13431 13432 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13433 mtx_lock(&thr->queue_lock); 13434 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13435 mtx_unlock(&thr->queue_lock); 13436 wakeup(thr); 13437} 13438 13439static void 13440ctl_enqueue_done(union ctl_io *io) 13441{ 13442 struct ctl_softc *softc = control_softc; 13443 struct ctl_thread *thr; 13444 13445 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13446 mtx_lock(&thr->queue_lock); 13447 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13448 mtx_unlock(&thr->queue_lock); 13449 wakeup(thr); 13450} 13451 13452static void 13453ctl_enqueue_isc(union ctl_io *io) 13454{ 13455 struct ctl_softc *softc = control_softc; 13456 struct ctl_thread *thr; 13457 13458 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13459 mtx_lock(&thr->queue_lock); 13460 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13461 mtx_unlock(&thr->queue_lock); 13462 wakeup(thr); 13463} 13464 13465/* 13466 * vim: ts=8 13467 */ 13468