ctl.c revision 288148
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Edward Tomasz Napierala 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions, and the following disclaimer, 15 * without modification. 16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17 * substantially similar to the "NO WARRANTY" disclaimer below 18 * ("Disclaimer") and any redistribution must be conditioned upon 19 * including a substantially similar Disclaimer requirement for further 20 * binary redistribution. 21 * 22 * NO WARRANTY 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGES. 34 * 35 * $Id$ 36 */ 37/* 38 * CAM Target Layer, a SCSI device emulation subsystem. 39 * 40 * Author: Ken Merry <ken@FreeBSD.org> 41 */ 42 43#define _CTL_C 44 45#include <sys/cdefs.h> 46__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl.c 288148 2015-09-23 18:33:00Z mav $"); 47 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/ctype.h> 51#include <sys/kernel.h> 52#include <sys/types.h> 53#include <sys/kthread.h> 54#include <sys/bio.h> 55#include <sys/fcntl.h> 56#include <sys/lock.h> 57#include <sys/module.h> 58#include <sys/mutex.h> 59#include <sys/condvar.h> 60#include <sys/malloc.h> 61#include <sys/conf.h> 62#include <sys/ioccom.h> 63#include <sys/queue.h> 64#include <sys/sbuf.h> 65#include <sys/smp.h> 66#include <sys/endian.h> 67#include <sys/sysctl.h> 68#include <vm/uma.h> 69 70#include <cam/cam.h> 71#include <cam/scsi/scsi_all.h> 72#include <cam/scsi/scsi_da.h> 73#include <cam/ctl/ctl_io.h> 74#include <cam/ctl/ctl.h> 75#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 264#define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4) 265 266const static struct scsi_control_ext_page control_ext_page_default = { 267 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 268 /*subpage_code*/0x01, 269 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 270 /*flags*/0, 271 /*prio*/0, 272 /*max_sense*/0 273}; 274 275const static struct scsi_control_ext_page control_ext_page_changeable = { 276 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 277 /*subpage_code*/0x01, 278 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 279 /*flags*/0, 280 /*prio*/0, 281 /*max_sense*/0 282}; 283 284const static struct scsi_info_exceptions_page ie_page_default = { 285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 286 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 287 /*info_flags*/SIEP_FLAGS_DEXCPT, 288 /*mrie*/0, 289 /*interval_timer*/{0, 0, 0, 0}, 290 /*report_count*/{0, 0, 0, 0} 291}; 292 293const static struct scsi_info_exceptions_page ie_page_changeable = { 294 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 295 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 296 /*info_flags*/0, 297 /*mrie*/0, 298 /*interval_timer*/{0, 0, 0, 0}, 299 /*report_count*/{0, 0, 0, 0} 300}; 301 302#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 303 304const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 305 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 306 /*subpage_code*/0x02, 307 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 308 /*flags*/0, 309 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 310 /*descr*/{}}, 311 {{/*flags*/0, 312 /*resource*/0x01, 313 /*reserved*/{0, 0}, 314 /*count*/{0, 0, 0, 0}}, 315 {/*flags*/0, 316 /*resource*/0x02, 317 /*reserved*/{0, 0}, 318 /*count*/{0, 0, 0, 0}}, 319 {/*flags*/0, 320 /*resource*/0xf1, 321 /*reserved*/{0, 0}, 322 /*count*/{0, 0, 0, 0}}, 323 {/*flags*/0, 324 /*resource*/0xf2, 325 /*reserved*/{0, 0}, 326 /*count*/{0, 0, 0, 0}} 327 } 328}; 329 330const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 331 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 332 /*subpage_code*/0x02, 333 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 334 /*flags*/0, 335 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 336 /*descr*/{}}, 337 {{/*flags*/0, 338 /*resource*/0, 339 /*reserved*/{0, 0}, 340 /*count*/{0, 0, 0, 0}}, 341 {/*flags*/0, 342 /*resource*/0, 343 /*reserved*/{0, 0}, 344 /*count*/{0, 0, 0, 0}}, 345 {/*flags*/0, 346 /*resource*/0, 347 /*reserved*/{0, 0}, 348 /*count*/{0, 0, 0, 0}}, 349 {/*flags*/0, 350 /*resource*/0, 351 /*reserved*/{0, 0}, 352 /*count*/{0, 0, 0, 0}} 353 } 354}; 355 356SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 357static int worker_threads = -1; 358SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 359 &worker_threads, 1, "Number of worker threads"); 360static int ctl_debug = CTL_DEBUG_NONE; 361SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 362 &ctl_debug, 0, "Enabled debug flags"); 363 364/* 365 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 366 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 367 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 368 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 369 */ 370#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 371 372static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 373 int param); 374static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 375static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 376static int ctl_init(void); 377void ctl_shutdown(void); 378static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 379static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 380static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 381static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 382 struct ctl_ooa *ooa_hdr, 383 struct ctl_ooa_entry *kern_entries); 384static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 385 struct thread *td); 386static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 387 struct ctl_be_lun *be_lun); 388static int ctl_free_lun(struct ctl_lun *lun); 389static void ctl_create_lun(struct ctl_be_lun *be_lun); 390static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 391 392static int ctl_do_mode_select(union ctl_io *io); 393static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 394 uint64_t res_key, uint64_t sa_res_key, 395 uint8_t type, uint32_t residx, 396 struct ctl_scsiio *ctsio, 397 struct scsi_per_res_out *cdb, 398 struct scsi_per_res_out_parms* param); 399static void ctl_pro_preempt_other(struct ctl_lun *lun, 400 union ctl_ha_msg *msg); 401static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 402static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 403static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 404static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 405static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 406static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 407static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 408 int alloc_len); 409static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 410 int alloc_len); 411static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 412static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 413static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 414static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 415static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 416static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 417 bool seq); 418static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 419static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 420 union ctl_io *pending_io, union ctl_io *ooa_io); 421static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 422 union ctl_io *starting_io); 423static int ctl_check_blocked(struct ctl_lun *lun); 424static int ctl_scsiio_lun_check(struct ctl_lun *lun, 425 const struct ctl_cmd_entry *entry, 426 struct ctl_scsiio *ctsio); 427static void ctl_failover_lun(struct ctl_lun *lun); 428static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 429 struct ctl_scsiio *ctsio); 430static int ctl_scsiio(struct ctl_scsiio *ctsio); 431 432static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 433static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 434 ctl_ua_type ua_type); 435static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, 436 ctl_ua_type ua_type); 437static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 438static int ctl_abort_task(union ctl_io *io); 439static int ctl_abort_task_set(union ctl_io *io); 440static int ctl_query_task(union ctl_io *io, int task_set); 441static int ctl_i_t_nexus_reset(union ctl_io *io); 442static int ctl_query_async_event(union ctl_io *io); 443static void ctl_run_task(union ctl_io *io); 444#ifdef CTL_IO_DELAY 445static void ctl_datamove_timer_wakeup(void *arg); 446static void ctl_done_timer_wakeup(void *arg); 447#endif /* CTL_IO_DELAY */ 448 449static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 450static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 451static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 452static void ctl_datamove_remote_write(union ctl_io *io); 453static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 454static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 455static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 456static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 457 ctl_ha_dt_cb callback); 458static void ctl_datamove_remote_read(union ctl_io *io); 459static void ctl_datamove_remote(union ctl_io *io); 460static int ctl_process_done(union ctl_io *io); 461static void ctl_lun_thread(void *arg); 462static void ctl_thresh_thread(void *arg); 463static void ctl_work_thread(void *arg); 464static void ctl_enqueue_incoming(union ctl_io *io); 465static void ctl_enqueue_rtr(union ctl_io *io); 466static void ctl_enqueue_done(union ctl_io *io); 467static void ctl_enqueue_isc(union ctl_io *io); 468static const struct ctl_cmd_entry * 469 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 470static const struct ctl_cmd_entry * 471 ctl_validate_command(struct ctl_scsiio *ctsio); 472static int ctl_cmd_applicable(uint8_t lun_type, 473 const struct ctl_cmd_entry *entry); 474 475static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 476static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 477static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 478static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 479 480/* 481 * Load the serialization table. This isn't very pretty, but is probably 482 * the easiest way to do it. 483 */ 484#include "ctl_ser_table.c" 485 486/* 487 * We only need to define open, close and ioctl routines for this driver. 488 */ 489static struct cdevsw ctl_cdevsw = { 490 .d_version = D_VERSION, 491 .d_flags = 0, 492 .d_open = ctl_open, 493 .d_close = ctl_close, 494 .d_ioctl = ctl_ioctl, 495 .d_name = "ctl", 496}; 497 498 499MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 500 501static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 502 503static moduledata_t ctl_moduledata = { 504 "ctl", 505 ctl_module_event_handler, 506 NULL 507}; 508 509DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 510MODULE_VERSION(ctl, 1); 511 512static struct ctl_frontend ha_frontend = 513{ 514 .name = "ha", 515}; 516 517static void 518ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 519 union ctl_ha_msg *msg_info) 520{ 521 struct ctl_scsiio *ctsio; 522 523 if (msg_info->hdr.original_sc == NULL) { 524 printf("%s: original_sc == NULL!\n", __func__); 525 /* XXX KDM now what? */ 526 return; 527 } 528 529 ctsio = &msg_info->hdr.original_sc->scsiio; 530 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 531 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 532 ctsio->io_hdr.status = msg_info->hdr.status; 533 ctsio->scsi_status = msg_info->scsi.scsi_status; 534 ctsio->sense_len = msg_info->scsi.sense_len; 535 ctsio->sense_residual = msg_info->scsi.sense_residual; 536 ctsio->residual = msg_info->scsi.residual; 537 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 538 msg_info->scsi.sense_len); 539 ctl_enqueue_isc((union ctl_io *)ctsio); 540} 541 542static void 543ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 544 union ctl_ha_msg *msg_info) 545{ 546 struct ctl_scsiio *ctsio; 547 548 if (msg_info->hdr.serializing_sc == NULL) { 549 printf("%s: serializing_sc == NULL!\n", __func__); 550 /* XXX KDM now what? */ 551 return; 552 } 553 554 ctsio = &msg_info->hdr.serializing_sc->scsiio; 555 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 556 ctl_enqueue_isc((union ctl_io *)ctsio); 557} 558 559void 560ctl_isc_announce_lun(struct ctl_lun *lun) 561{ 562 struct ctl_softc *softc = lun->ctl_softc; 563 union ctl_ha_msg *msg; 564 struct ctl_ha_msg_lun_pr_key pr_key; 565 int i, k; 566 567 if (softc->ha_link != CTL_HA_LINK_ONLINE) 568 return; 569 mtx_lock(&lun->lun_lock); 570 i = sizeof(msg->lun); 571 if (lun->lun_devid) 572 i += lun->lun_devid->len; 573 i += sizeof(pr_key) * lun->pr_key_count; 574alloc: 575 mtx_unlock(&lun->lun_lock); 576 msg = malloc(i, M_CTL, M_WAITOK); 577 mtx_lock(&lun->lun_lock); 578 k = sizeof(msg->lun); 579 if (lun->lun_devid) 580 k += lun->lun_devid->len; 581 k += sizeof(pr_key) * lun->pr_key_count; 582 if (i < k) { 583 free(msg, M_CTL); 584 i = k; 585 goto alloc; 586 } 587 bzero(&msg->lun, sizeof(msg->lun)); 588 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 589 msg->hdr.nexus.targ_lun = lun->lun; 590 msg->hdr.nexus.targ_mapped_lun = lun->lun; 591 msg->lun.flags = lun->flags; 592 msg->lun.pr_generation = lun->PRGeneration; 593 msg->lun.pr_res_idx = lun->pr_res_idx; 594 msg->lun.pr_res_type = lun->res_type; 595 msg->lun.pr_key_count = lun->pr_key_count; 596 i = 0; 597 if (lun->lun_devid) { 598 msg->lun.lun_devid_len = lun->lun_devid->len; 599 memcpy(&msg->lun.data[i], lun->lun_devid->data, 600 msg->lun.lun_devid_len); 601 i += msg->lun.lun_devid_len; 602 } 603 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 604 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 605 continue; 606 pr_key.pr_iid = k; 607 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 608 i += sizeof(pr_key); 609 } 610 mtx_unlock(&lun->lun_lock); 611 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 612 M_WAITOK); 613 free(msg, M_CTL); 614 615 if (lun->flags & CTL_LUN_PRIMARY_SC) { 616 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 617 ctl_isc_announce_mode(lun, -1, 618 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 619 lun->mode_pages.index[i].subpage); 620 } 621 } 622} 623 624void 625ctl_isc_announce_port(struct ctl_port *port) 626{ 627 struct ctl_softc *softc = control_softc; 628 union ctl_ha_msg *msg; 629 int i; 630 631 if (port->targ_port < softc->port_min || 632 port->targ_port >= softc->port_max || 633 softc->ha_link != CTL_HA_LINK_ONLINE) 634 return; 635 i = sizeof(msg->port) + strlen(port->port_name) + 1; 636 if (port->lun_map) 637 i += sizeof(uint32_t) * CTL_MAX_LUNS; 638 if (port->port_devid) 639 i += port->port_devid->len; 640 if (port->target_devid) 641 i += port->target_devid->len; 642 if (port->init_devid) 643 i += port->init_devid->len; 644 msg = malloc(i, M_CTL, M_WAITOK); 645 bzero(&msg->port, sizeof(msg->port)); 646 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 647 msg->hdr.nexus.targ_port = port->targ_port; 648 msg->port.port_type = port->port_type; 649 msg->port.physical_port = port->physical_port; 650 msg->port.virtual_port = port->virtual_port; 651 msg->port.status = port->status; 652 i = 0; 653 msg->port.name_len = sprintf(&msg->port.data[i], 654 "%d:%s", softc->ha_id, port->port_name) + 1; 655 i += msg->port.name_len; 656 if (port->lun_map) { 657 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS; 658 memcpy(&msg->port.data[i], port->lun_map, 659 msg->port.lun_map_len); 660 i += msg->port.lun_map_len; 661 } 662 if (port->port_devid) { 663 msg->port.port_devid_len = port->port_devid->len; 664 memcpy(&msg->port.data[i], port->port_devid->data, 665 msg->port.port_devid_len); 666 i += msg->port.port_devid_len; 667 } 668 if (port->target_devid) { 669 msg->port.target_devid_len = port->target_devid->len; 670 memcpy(&msg->port.data[i], port->target_devid->data, 671 msg->port.target_devid_len); 672 i += msg->port.target_devid_len; 673 } 674 if (port->init_devid) { 675 msg->port.init_devid_len = port->init_devid->len; 676 memcpy(&msg->port.data[i], port->init_devid->data, 677 msg->port.init_devid_len); 678 i += msg->port.init_devid_len; 679 } 680 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 681 M_WAITOK); 682 free(msg, M_CTL); 683} 684 685void 686ctl_isc_announce_iid(struct ctl_port *port, int iid) 687{ 688 struct ctl_softc *softc = control_softc; 689 union ctl_ha_msg *msg; 690 int i, l; 691 692 if (port->targ_port < softc->port_min || 693 port->targ_port >= softc->port_max || 694 softc->ha_link != CTL_HA_LINK_ONLINE) 695 return; 696 mtx_lock(&softc->ctl_lock); 697 i = sizeof(msg->iid); 698 l = 0; 699 if (port->wwpn_iid[iid].name) 700 l = strlen(port->wwpn_iid[iid].name) + 1; 701 i += l; 702 msg = malloc(i, M_CTL, M_NOWAIT); 703 if (msg == NULL) { 704 mtx_unlock(&softc->ctl_lock); 705 return; 706 } 707 bzero(&msg->iid, sizeof(msg->iid)); 708 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 709 msg->hdr.nexus.targ_port = port->targ_port; 710 msg->hdr.nexus.initid = iid; 711 msg->iid.in_use = port->wwpn_iid[iid].in_use; 712 msg->iid.name_len = l; 713 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 714 if (port->wwpn_iid[iid].name) 715 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 716 mtx_unlock(&softc->ctl_lock); 717 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 718 free(msg, M_CTL); 719} 720 721void 722ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 723 uint8_t page, uint8_t subpage) 724{ 725 struct ctl_softc *softc = lun->ctl_softc; 726 union ctl_ha_msg msg; 727 int i; 728 729 if (softc->ha_link != CTL_HA_LINK_ONLINE) 730 return; 731 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 732 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 733 page && lun->mode_pages.index[i].subpage == subpage) 734 break; 735 } 736 if (i == CTL_NUM_MODE_PAGES) 737 return; 738 bzero(&msg.mode, sizeof(msg.mode)); 739 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 740 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 741 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 742 msg.hdr.nexus.targ_lun = lun->lun; 743 msg.hdr.nexus.targ_mapped_lun = lun->lun; 744 msg.mode.page_code = page; 745 msg.mode.subpage = subpage; 746 msg.mode.page_len = lun->mode_pages.index[i].page_len; 747 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 748 msg.mode.page_len); 749 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 750 M_WAITOK); 751} 752 753static void 754ctl_isc_ha_link_up(struct ctl_softc *softc) 755{ 756 struct ctl_port *port; 757 struct ctl_lun *lun; 758 union ctl_ha_msg msg; 759 int i; 760 761 /* Announce this node parameters to peer for validation. */ 762 msg.login.msg_type = CTL_MSG_LOGIN; 763 msg.login.version = CTL_HA_VERSION; 764 msg.login.ha_mode = softc->ha_mode; 765 msg.login.ha_id = softc->ha_id; 766 msg.login.max_luns = CTL_MAX_LUNS; 767 msg.login.max_ports = CTL_MAX_PORTS; 768 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 769 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 770 M_WAITOK); 771 772 STAILQ_FOREACH(port, &softc->port_list, links) { 773 ctl_isc_announce_port(port); 774 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 775 if (port->wwpn_iid[i].in_use) 776 ctl_isc_announce_iid(port, i); 777 } 778 } 779 STAILQ_FOREACH(lun, &softc->lun_list, links) 780 ctl_isc_announce_lun(lun); 781} 782 783static void 784ctl_isc_ha_link_down(struct ctl_softc *softc) 785{ 786 struct ctl_port *port; 787 struct ctl_lun *lun; 788 union ctl_io *io; 789 int i; 790 791 mtx_lock(&softc->ctl_lock); 792 STAILQ_FOREACH(lun, &softc->lun_list, links) { 793 mtx_lock(&lun->lun_lock); 794 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 795 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 796 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 797 } 798 mtx_unlock(&lun->lun_lock); 799 800 mtx_unlock(&softc->ctl_lock); 801 io = ctl_alloc_io(softc->othersc_pool); 802 mtx_lock(&softc->ctl_lock); 803 ctl_zero_io(io); 804 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 805 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 806 ctl_enqueue_isc(io); 807 } 808 809 STAILQ_FOREACH(port, &softc->port_list, links) { 810 if (port->targ_port >= softc->port_min && 811 port->targ_port < softc->port_max) 812 continue; 813 port->status &= ~CTL_PORT_STATUS_ONLINE; 814 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 815 port->wwpn_iid[i].in_use = 0; 816 free(port->wwpn_iid[i].name, M_CTL); 817 port->wwpn_iid[i].name = NULL; 818 } 819 } 820 mtx_unlock(&softc->ctl_lock); 821} 822 823static void 824ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 825{ 826 struct ctl_lun *lun; 827 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 828 829 mtx_lock(&softc->ctl_lock); 830 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS && 831 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) { 832 mtx_lock(&lun->lun_lock); 833 mtx_unlock(&softc->ctl_lock); 834 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && 835 msg->ua.ua_set) 836 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 837 if (msg->ua.ua_all) { 838 if (msg->ua.ua_set) 839 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 840 else 841 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 842 } else { 843 if (msg->ua.ua_set) 844 ctl_est_ua(lun, iid, msg->ua.ua_type); 845 else 846 ctl_clr_ua(lun, iid, msg->ua.ua_type); 847 } 848 mtx_unlock(&lun->lun_lock); 849 } else 850 mtx_unlock(&softc->ctl_lock); 851} 852 853static void 854ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 855{ 856 struct ctl_lun *lun; 857 struct ctl_ha_msg_lun_pr_key pr_key; 858 int i, k; 859 ctl_lun_flags oflags; 860 uint32_t targ_lun; 861 862 targ_lun = msg->hdr.nexus.targ_mapped_lun; 863 mtx_lock(&softc->ctl_lock); 864 if ((targ_lun >= CTL_MAX_LUNS) || 865 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 866 mtx_unlock(&softc->ctl_lock); 867 return; 868 } 869 mtx_lock(&lun->lun_lock); 870 mtx_unlock(&softc->ctl_lock); 871 if (lun->flags & CTL_LUN_DISABLED) { 872 mtx_unlock(&lun->lun_lock); 873 return; 874 } 875 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 876 if (msg->lun.lun_devid_len != i || (i > 0 && 877 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 878 mtx_unlock(&lun->lun_lock); 879 printf("%s: Received conflicting HA LUN %d\n", 880 __func__, msg->hdr.nexus.targ_lun); 881 return; 882 } else { 883 /* Record whether peer is primary. */ 884 oflags = lun->flags; 885 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 886 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 887 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 888 else 889 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 890 if (oflags != lun->flags) 891 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 892 893 /* If peer is primary and we are not -- use data */ 894 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 895 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 896 lun->PRGeneration = msg->lun.pr_generation; 897 lun->pr_res_idx = msg->lun.pr_res_idx; 898 lun->res_type = msg->lun.pr_res_type; 899 lun->pr_key_count = msg->lun.pr_key_count; 900 for (k = 0; k < CTL_MAX_INITIATORS; k++) 901 ctl_clr_prkey(lun, k); 902 for (k = 0; k < msg->lun.pr_key_count; k++) { 903 memcpy(&pr_key, &msg->lun.data[i], 904 sizeof(pr_key)); 905 ctl_alloc_prkey(lun, pr_key.pr_iid); 906 ctl_set_prkey(lun, pr_key.pr_iid, 907 pr_key.pr_key); 908 i += sizeof(pr_key); 909 } 910 } 911 912 mtx_unlock(&lun->lun_lock); 913 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 914 __func__, msg->hdr.nexus.targ_lun, 915 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 916 "primary" : "secondary")); 917 918 /* If we are primary but peer doesn't know -- notify */ 919 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 920 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 921 ctl_isc_announce_lun(lun); 922 } 923} 924 925static void 926ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 927{ 928 struct ctl_port *port; 929 struct ctl_lun *lun; 930 int i, new; 931 932 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 933 if (port == NULL) { 934 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 935 msg->hdr.nexus.targ_port)); 936 new = 1; 937 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 938 port->frontend = &ha_frontend; 939 port->targ_port = msg->hdr.nexus.targ_port; 940 } else if (port->frontend == &ha_frontend) { 941 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 942 msg->hdr.nexus.targ_port)); 943 new = 0; 944 } else { 945 printf("%s: Received conflicting HA port %d\n", 946 __func__, msg->hdr.nexus.targ_port); 947 return; 948 } 949 port->port_type = msg->port.port_type; 950 port->physical_port = msg->port.physical_port; 951 port->virtual_port = msg->port.virtual_port; 952 port->status = msg->port.status; 953 i = 0; 954 free(port->port_name, M_CTL); 955 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 956 M_CTL); 957 i += msg->port.name_len; 958 if (msg->port.lun_map_len != 0) { 959 if (port->lun_map == NULL) 960 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 961 M_CTL, M_WAITOK); 962 memcpy(port->lun_map, &msg->port.data[i], 963 sizeof(uint32_t) * CTL_MAX_LUNS); 964 i += msg->port.lun_map_len; 965 } else { 966 free(port->lun_map, M_CTL); 967 port->lun_map = NULL; 968 } 969 if (msg->port.port_devid_len != 0) { 970 if (port->port_devid == NULL || 971 port->port_devid->len != msg->port.port_devid_len) { 972 free(port->port_devid, M_CTL); 973 port->port_devid = malloc(sizeof(struct ctl_devid) + 974 msg->port.port_devid_len, M_CTL, M_WAITOK); 975 } 976 memcpy(port->port_devid->data, &msg->port.data[i], 977 msg->port.port_devid_len); 978 port->port_devid->len = msg->port.port_devid_len; 979 i += msg->port.port_devid_len; 980 } else { 981 free(port->port_devid, M_CTL); 982 port->port_devid = NULL; 983 } 984 if (msg->port.target_devid_len != 0) { 985 if (port->target_devid == NULL || 986 port->target_devid->len != msg->port.target_devid_len) { 987 free(port->target_devid, M_CTL); 988 port->target_devid = malloc(sizeof(struct ctl_devid) + 989 msg->port.target_devid_len, M_CTL, M_WAITOK); 990 } 991 memcpy(port->target_devid->data, &msg->port.data[i], 992 msg->port.target_devid_len); 993 port->target_devid->len = msg->port.target_devid_len; 994 i += msg->port.target_devid_len; 995 } else { 996 free(port->target_devid, M_CTL); 997 port->target_devid = NULL; 998 } 999 if (msg->port.init_devid_len != 0) { 1000 if (port->init_devid == NULL || 1001 port->init_devid->len != msg->port.init_devid_len) { 1002 free(port->init_devid, M_CTL); 1003 port->init_devid = malloc(sizeof(struct ctl_devid) + 1004 msg->port.init_devid_len, M_CTL, M_WAITOK); 1005 } 1006 memcpy(port->init_devid->data, &msg->port.data[i], 1007 msg->port.init_devid_len); 1008 port->init_devid->len = msg->port.init_devid_len; 1009 i += msg->port.init_devid_len; 1010 } else { 1011 free(port->init_devid, M_CTL); 1012 port->init_devid = NULL; 1013 } 1014 if (new) { 1015 if (ctl_port_register(port) != 0) { 1016 printf("%s: ctl_port_register() failed with error\n", 1017 __func__); 1018 } 1019 } 1020 mtx_lock(&softc->ctl_lock); 1021 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1022 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 1023 continue; 1024 mtx_lock(&lun->lun_lock); 1025 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1026 mtx_unlock(&lun->lun_lock); 1027 } 1028 mtx_unlock(&softc->ctl_lock); 1029} 1030 1031static void 1032ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1033{ 1034 struct ctl_port *port; 1035 int iid; 1036 1037 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1038 if (port == NULL) { 1039 printf("%s: Received IID for unknown port %d\n", 1040 __func__, msg->hdr.nexus.targ_port); 1041 return; 1042 } 1043 iid = msg->hdr.nexus.initid; 1044 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1045 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1046 free(port->wwpn_iid[iid].name, M_CTL); 1047 if (msg->iid.name_len) { 1048 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1049 msg->iid.name_len, M_CTL); 1050 } else 1051 port->wwpn_iid[iid].name = NULL; 1052} 1053 1054static void 1055ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1056{ 1057 1058 if (msg->login.version != CTL_HA_VERSION) { 1059 printf("CTL HA peers have different versions %d != %d\n", 1060 msg->login.version, CTL_HA_VERSION); 1061 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1062 return; 1063 } 1064 if (msg->login.ha_mode != softc->ha_mode) { 1065 printf("CTL HA peers have different ha_mode %d != %d\n", 1066 msg->login.ha_mode, softc->ha_mode); 1067 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1068 return; 1069 } 1070 if (msg->login.ha_id == softc->ha_id) { 1071 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1072 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1073 return; 1074 } 1075 if (msg->login.max_luns != CTL_MAX_LUNS || 1076 msg->login.max_ports != CTL_MAX_PORTS || 1077 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1078 printf("CTL HA peers have different limits\n"); 1079 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1080 return; 1081 } 1082} 1083 1084static void 1085ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1086{ 1087 struct ctl_lun *lun; 1088 int i; 1089 uint32_t initidx, targ_lun; 1090 1091 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1092 mtx_lock(&softc->ctl_lock); 1093 if ((targ_lun >= CTL_MAX_LUNS) || 1094 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 1095 mtx_unlock(&softc->ctl_lock); 1096 return; 1097 } 1098 mtx_lock(&lun->lun_lock); 1099 mtx_unlock(&softc->ctl_lock); 1100 if (lun->flags & CTL_LUN_DISABLED) { 1101 mtx_unlock(&lun->lun_lock); 1102 return; 1103 } 1104 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1105 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1106 msg->mode.page_code && 1107 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1108 break; 1109 } 1110 if (i == CTL_NUM_MODE_PAGES) { 1111 mtx_unlock(&lun->lun_lock); 1112 return; 1113 } 1114 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1115 lun->mode_pages.index[i].page_len); 1116 initidx = ctl_get_initindex(&msg->hdr.nexus); 1117 if (initidx != -1) 1118 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1119 mtx_unlock(&lun->lun_lock); 1120} 1121 1122/* 1123 * ISC (Inter Shelf Communication) event handler. Events from the HA 1124 * subsystem come in here. 1125 */ 1126static void 1127ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1128{ 1129 struct ctl_softc *softc; 1130 union ctl_io *io; 1131 struct ctl_prio *presio; 1132 ctl_ha_status isc_status; 1133 1134 softc = control_softc; 1135 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1136 if (event == CTL_HA_EVT_MSG_RECV) { 1137 union ctl_ha_msg *msg, msgbuf; 1138 1139 if (param > sizeof(msgbuf)) 1140 msg = malloc(param, M_CTL, M_WAITOK); 1141 else 1142 msg = &msgbuf; 1143 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1144 M_WAITOK); 1145 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1146 printf("%s: Error receiving message: %d\n", 1147 __func__, isc_status); 1148 if (msg != &msgbuf) 1149 free(msg, M_CTL); 1150 return; 1151 } 1152 1153 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1154 switch (msg->hdr.msg_type) { 1155 case CTL_MSG_SERIALIZE: 1156 io = ctl_alloc_io(softc->othersc_pool); 1157 ctl_zero_io(io); 1158 // populate ctsio from msg 1159 io->io_hdr.io_type = CTL_IO_SCSI; 1160 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1161 io->io_hdr.original_sc = msg->hdr.original_sc; 1162 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1163 CTL_FLAG_IO_ACTIVE; 1164 /* 1165 * If we're in serialization-only mode, we don't 1166 * want to go through full done processing. Thus 1167 * the COPY flag. 1168 * 1169 * XXX KDM add another flag that is more specific. 1170 */ 1171 if (softc->ha_mode != CTL_HA_MODE_XFER) 1172 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1173 io->io_hdr.nexus = msg->hdr.nexus; 1174#if 0 1175 printf("port %u, iid %u, lun %u\n", 1176 io->io_hdr.nexus.targ_port, 1177 io->io_hdr.nexus.initid, 1178 io->io_hdr.nexus.targ_lun); 1179#endif 1180 io->scsiio.tag_num = msg->scsi.tag_num; 1181 io->scsiio.tag_type = msg->scsi.tag_type; 1182#ifdef CTL_TIME_IO 1183 io->io_hdr.start_time = time_uptime; 1184 getbintime(&io->io_hdr.start_bt); 1185#endif /* CTL_TIME_IO */ 1186 io->scsiio.cdb_len = msg->scsi.cdb_len; 1187 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1188 CTL_MAX_CDBLEN); 1189 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1190 const struct ctl_cmd_entry *entry; 1191 1192 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1193 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1194 io->io_hdr.flags |= 1195 entry->flags & CTL_FLAG_DATA_MASK; 1196 } 1197 ctl_enqueue_isc(io); 1198 break; 1199 1200 /* Performed on the Originating SC, XFER mode only */ 1201 case CTL_MSG_DATAMOVE: { 1202 struct ctl_sg_entry *sgl; 1203 int i, j; 1204 1205 io = msg->hdr.original_sc; 1206 if (io == NULL) { 1207 printf("%s: original_sc == NULL!\n", __func__); 1208 /* XXX KDM do something here */ 1209 break; 1210 } 1211 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1212 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1213 /* 1214 * Keep track of this, we need to send it back over 1215 * when the datamove is complete. 1216 */ 1217 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1218 if (msg->hdr.status == CTL_SUCCESS) 1219 io->io_hdr.status = msg->hdr.status; 1220 1221 if (msg->dt.sg_sequence == 0) { 1222 i = msg->dt.kern_sg_entries + 1223 msg->dt.kern_data_len / 1224 CTL_HA_DATAMOVE_SEGMENT + 1; 1225 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1226 M_WAITOK | M_ZERO); 1227 io->io_hdr.remote_sglist = sgl; 1228 io->io_hdr.local_sglist = 1229 &sgl[msg->dt.kern_sg_entries]; 1230 1231 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1232 1233 io->scsiio.kern_sg_entries = 1234 msg->dt.kern_sg_entries; 1235 io->scsiio.rem_sg_entries = 1236 msg->dt.kern_sg_entries; 1237 io->scsiio.kern_data_len = 1238 msg->dt.kern_data_len; 1239 io->scsiio.kern_total_len = 1240 msg->dt.kern_total_len; 1241 io->scsiio.kern_data_resid = 1242 msg->dt.kern_data_resid; 1243 io->scsiio.kern_rel_offset = 1244 msg->dt.kern_rel_offset; 1245 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1246 io->io_hdr.flags |= msg->dt.flags & 1247 CTL_FLAG_BUS_ADDR; 1248 } else 1249 sgl = (struct ctl_sg_entry *) 1250 io->scsiio.kern_data_ptr; 1251 1252 for (i = msg->dt.sent_sg_entries, j = 0; 1253 i < (msg->dt.sent_sg_entries + 1254 msg->dt.cur_sg_entries); i++, j++) { 1255 sgl[i].addr = msg->dt.sg_list[j].addr; 1256 sgl[i].len = msg->dt.sg_list[j].len; 1257 1258#if 0 1259 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1260 __func__, sgl[i].addr, sgl[i].len, j, i); 1261#endif 1262 } 1263 1264 /* 1265 * If this is the last piece of the I/O, we've got 1266 * the full S/G list. Queue processing in the thread. 1267 * Otherwise wait for the next piece. 1268 */ 1269 if (msg->dt.sg_last != 0) 1270 ctl_enqueue_isc(io); 1271 break; 1272 } 1273 /* Performed on the Serializing (primary) SC, XFER mode only */ 1274 case CTL_MSG_DATAMOVE_DONE: { 1275 if (msg->hdr.serializing_sc == NULL) { 1276 printf("%s: serializing_sc == NULL!\n", 1277 __func__); 1278 /* XXX KDM now what? */ 1279 break; 1280 } 1281 /* 1282 * We grab the sense information here in case 1283 * there was a failure, so we can return status 1284 * back to the initiator. 1285 */ 1286 io = msg->hdr.serializing_sc; 1287 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1288 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1289 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1290 io->io_hdr.port_status = msg->scsi.fetd_status; 1291 io->scsiio.residual = msg->scsi.residual; 1292 if (msg->hdr.status != CTL_STATUS_NONE) { 1293 io->io_hdr.status = msg->hdr.status; 1294 io->scsiio.scsi_status = msg->scsi.scsi_status; 1295 io->scsiio.sense_len = msg->scsi.sense_len; 1296 io->scsiio.sense_residual =msg->scsi.sense_residual; 1297 memcpy(&io->scsiio.sense_data, 1298 &msg->scsi.sense_data, 1299 msg->scsi.sense_len); 1300 if (msg->hdr.status == CTL_SUCCESS) 1301 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1302 } 1303 ctl_enqueue_isc(io); 1304 break; 1305 } 1306 1307 /* Preformed on Originating SC, SER_ONLY mode */ 1308 case CTL_MSG_R2R: 1309 io = msg->hdr.original_sc; 1310 if (io == NULL) { 1311 printf("%s: original_sc == NULL!\n", 1312 __func__); 1313 break; 1314 } 1315 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1316 io->io_hdr.msg_type = CTL_MSG_R2R; 1317 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1318 ctl_enqueue_isc(io); 1319 break; 1320 1321 /* 1322 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1323 * mode. 1324 * Performed on the Originating (i.e. secondary) SC in XFER 1325 * mode 1326 */ 1327 case CTL_MSG_FINISH_IO: 1328 if (softc->ha_mode == CTL_HA_MODE_XFER) 1329 ctl_isc_handler_finish_xfer(softc, msg); 1330 else 1331 ctl_isc_handler_finish_ser_only(softc, msg); 1332 break; 1333 1334 /* Preformed on Originating SC */ 1335 case CTL_MSG_BAD_JUJU: 1336 io = msg->hdr.original_sc; 1337 if (io == NULL) { 1338 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1339 __func__); 1340 break; 1341 } 1342 ctl_copy_sense_data(msg, io); 1343 /* 1344 * IO should have already been cleaned up on other 1345 * SC so clear this flag so we won't send a message 1346 * back to finish the IO there. 1347 */ 1348 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1349 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1350 1351 /* io = msg->hdr.serializing_sc; */ 1352 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1353 ctl_enqueue_isc(io); 1354 break; 1355 1356 /* Handle resets sent from the other side */ 1357 case CTL_MSG_MANAGE_TASKS: { 1358 struct ctl_taskio *taskio; 1359 taskio = (struct ctl_taskio *)ctl_alloc_io( 1360 softc->othersc_pool); 1361 ctl_zero_io((union ctl_io *)taskio); 1362 taskio->io_hdr.io_type = CTL_IO_TASK; 1363 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1364 taskio->io_hdr.nexus = msg->hdr.nexus; 1365 taskio->task_action = msg->task.task_action; 1366 taskio->tag_num = msg->task.tag_num; 1367 taskio->tag_type = msg->task.tag_type; 1368#ifdef CTL_TIME_IO 1369 taskio->io_hdr.start_time = time_uptime; 1370 getbintime(&taskio->io_hdr.start_bt); 1371#endif /* CTL_TIME_IO */ 1372 ctl_run_task((union ctl_io *)taskio); 1373 break; 1374 } 1375 /* Persistent Reserve action which needs attention */ 1376 case CTL_MSG_PERS_ACTION: 1377 presio = (struct ctl_prio *)ctl_alloc_io( 1378 softc->othersc_pool); 1379 ctl_zero_io((union ctl_io *)presio); 1380 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1381 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1382 presio->io_hdr.nexus = msg->hdr.nexus; 1383 presio->pr_msg = msg->pr; 1384 ctl_enqueue_isc((union ctl_io *)presio); 1385 break; 1386 case CTL_MSG_UA: 1387 ctl_isc_ua(softc, msg, param); 1388 break; 1389 case CTL_MSG_PORT_SYNC: 1390 ctl_isc_port_sync(softc, msg, param); 1391 break; 1392 case CTL_MSG_LUN_SYNC: 1393 ctl_isc_lun_sync(softc, msg, param); 1394 break; 1395 case CTL_MSG_IID_SYNC: 1396 ctl_isc_iid_sync(softc, msg, param); 1397 break; 1398 case CTL_MSG_LOGIN: 1399 ctl_isc_login(softc, msg, param); 1400 break; 1401 case CTL_MSG_MODE_SYNC: 1402 ctl_isc_mode_sync(softc, msg, param); 1403 break; 1404 default: 1405 printf("Received HA message of unknown type %d\n", 1406 msg->hdr.msg_type); 1407 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1408 break; 1409 } 1410 if (msg != &msgbuf) 1411 free(msg, M_CTL); 1412 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1413 printf("CTL: HA link status changed from %d to %d\n", 1414 softc->ha_link, param); 1415 if (param == softc->ha_link) 1416 return; 1417 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1418 softc->ha_link = param; 1419 ctl_isc_ha_link_down(softc); 1420 } else { 1421 softc->ha_link = param; 1422 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1423 ctl_isc_ha_link_up(softc); 1424 } 1425 return; 1426 } else { 1427 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1428 return; 1429 } 1430} 1431 1432static void 1433ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1434{ 1435 1436 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1437 src->scsi.sense_len); 1438 dest->scsiio.scsi_status = src->scsi.scsi_status; 1439 dest->scsiio.sense_len = src->scsi.sense_len; 1440 dest->io_hdr.status = src->hdr.status; 1441} 1442 1443static void 1444ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1445{ 1446 1447 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1448 src->scsiio.sense_len); 1449 dest->scsi.scsi_status = src->scsiio.scsi_status; 1450 dest->scsi.sense_len = src->scsiio.sense_len; 1451 dest->hdr.status = src->io_hdr.status; 1452} 1453 1454void 1455ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1456{ 1457 struct ctl_softc *softc = lun->ctl_softc; 1458 ctl_ua_type *pu; 1459 1460 if (initidx < softc->init_min || initidx >= softc->init_max) 1461 return; 1462 mtx_assert(&lun->lun_lock, MA_OWNED); 1463 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1464 if (pu == NULL) 1465 return; 1466 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1467} 1468 1469void 1470ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1471{ 1472 int i; 1473 1474 mtx_assert(&lun->lun_lock, MA_OWNED); 1475 if (lun->pending_ua[port] == NULL) 1476 return; 1477 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1478 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1479 continue; 1480 lun->pending_ua[port][i] |= ua; 1481 } 1482} 1483 1484void 1485ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1486{ 1487 struct ctl_softc *softc = lun->ctl_softc; 1488 int i; 1489 1490 mtx_assert(&lun->lun_lock, MA_OWNED); 1491 for (i = softc->port_min; i < softc->port_max; i++) 1492 ctl_est_ua_port(lun, i, except, ua); 1493} 1494 1495void 1496ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1497{ 1498 struct ctl_softc *softc = lun->ctl_softc; 1499 ctl_ua_type *pu; 1500 1501 if (initidx < softc->init_min || initidx >= softc->init_max) 1502 return; 1503 mtx_assert(&lun->lun_lock, MA_OWNED); 1504 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1505 if (pu == NULL) 1506 return; 1507 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1508} 1509 1510void 1511ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1512{ 1513 struct ctl_softc *softc = lun->ctl_softc; 1514 int i, j; 1515 1516 mtx_assert(&lun->lun_lock, MA_OWNED); 1517 for (i = softc->port_min; i < softc->port_max; i++) { 1518 if (lun->pending_ua[i] == NULL) 1519 continue; 1520 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1521 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1522 continue; 1523 lun->pending_ua[i][j] &= ~ua; 1524 } 1525 } 1526} 1527 1528void 1529ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1530 ctl_ua_type ua_type) 1531{ 1532 struct ctl_lun *lun; 1533 1534 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1535 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1536 mtx_lock(&lun->lun_lock); 1537 ctl_clr_ua(lun, initidx, ua_type); 1538 mtx_unlock(&lun->lun_lock); 1539 } 1540} 1541 1542static int 1543ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1544{ 1545 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1546 struct ctl_lun *lun; 1547 struct ctl_lun_req ireq; 1548 int error, value; 1549 1550 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1551 error = sysctl_handle_int(oidp, &value, 0, req); 1552 if ((error != 0) || (req->newptr == NULL)) 1553 return (error); 1554 1555 mtx_lock(&softc->ctl_lock); 1556 if (value == 0) 1557 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1558 else 1559 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1560 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1561 mtx_unlock(&softc->ctl_lock); 1562 bzero(&ireq, sizeof(ireq)); 1563 ireq.reqtype = CTL_LUNREQ_MODIFY; 1564 ireq.reqdata.modify.lun_id = lun->lun; 1565 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1566 curthread); 1567 if (ireq.status != CTL_LUN_OK) { 1568 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1569 __func__, ireq.status, ireq.error_str); 1570 } 1571 mtx_lock(&softc->ctl_lock); 1572 } 1573 mtx_unlock(&softc->ctl_lock); 1574 return (0); 1575} 1576 1577static int 1578ctl_init(void) 1579{ 1580 struct ctl_softc *softc; 1581 void *other_pool; 1582 int i, error, retval; 1583 1584 retval = 0; 1585 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1586 M_WAITOK | M_ZERO); 1587 softc = control_softc; 1588 1589 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1590 "cam/ctl"); 1591 1592 softc->dev->si_drv1 = softc; 1593 1594 sysctl_ctx_init(&softc->sysctl_ctx); 1595 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1596 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1597 CTLFLAG_RD, 0, "CAM Target Layer"); 1598 1599 if (softc->sysctl_tree == NULL) { 1600 printf("%s: unable to allocate sysctl tree\n", __func__); 1601 destroy_dev(softc->dev); 1602 free(control_softc, M_DEVBUF); 1603 control_softc = NULL; 1604 return (ENOMEM); 1605 } 1606 1607 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1608 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1609 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1610 softc->open_count = 0; 1611 1612 /* 1613 * Default to actually sending a SYNCHRONIZE CACHE command down to 1614 * the drive. 1615 */ 1616 softc->flags = CTL_FLAG_REAL_SYNC; 1617 1618 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1619 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1620 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1621 1622 /* 1623 * In Copan's HA scheme, the "master" and "slave" roles are 1624 * figured out through the slot the controller is in. Although it 1625 * is an active/active system, someone has to be in charge. 1626 */ 1627 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1628 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1629 "HA head ID (0 - no HA)"); 1630 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) { 1631 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1632 softc->is_single = 1; 1633 softc->port_cnt = CTL_MAX_PORTS; 1634 softc->port_min = 0; 1635 } else { 1636 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS; 1637 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1638 } 1639 softc->port_max = softc->port_min + softc->port_cnt; 1640 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1641 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1642 1643 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1644 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1645 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1646 1647 STAILQ_INIT(&softc->lun_list); 1648 STAILQ_INIT(&softc->pending_lun_queue); 1649 STAILQ_INIT(&softc->fe_list); 1650 STAILQ_INIT(&softc->port_list); 1651 STAILQ_INIT(&softc->be_list); 1652 ctl_tpc_init(softc); 1653 1654 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1655 &other_pool) != 0) 1656 { 1657 printf("ctl: can't allocate %d entry other SC pool, " 1658 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1659 return (ENOMEM); 1660 } 1661 softc->othersc_pool = other_pool; 1662 1663 if (worker_threads <= 0) 1664 worker_threads = max(1, mp_ncpus / 4); 1665 if (worker_threads > CTL_MAX_THREADS) 1666 worker_threads = CTL_MAX_THREADS; 1667 1668 for (i = 0; i < worker_threads; i++) { 1669 struct ctl_thread *thr = &softc->threads[i]; 1670 1671 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1672 thr->ctl_softc = softc; 1673 STAILQ_INIT(&thr->incoming_queue); 1674 STAILQ_INIT(&thr->rtr_queue); 1675 STAILQ_INIT(&thr->done_queue); 1676 STAILQ_INIT(&thr->isc_queue); 1677 1678 error = kproc_kthread_add(ctl_work_thread, thr, 1679 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1680 if (error != 0) { 1681 printf("error creating CTL work thread!\n"); 1682 ctl_pool_free(other_pool); 1683 return (error); 1684 } 1685 } 1686 error = kproc_kthread_add(ctl_lun_thread, softc, 1687 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1688 if (error != 0) { 1689 printf("error creating CTL lun thread!\n"); 1690 ctl_pool_free(other_pool); 1691 return (error); 1692 } 1693 error = kproc_kthread_add(ctl_thresh_thread, softc, 1694 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1695 if (error != 0) { 1696 printf("error creating CTL threshold thread!\n"); 1697 ctl_pool_free(other_pool); 1698 return (error); 1699 } 1700 1701 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1702 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1703 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1704 1705 if (softc->is_single == 0) { 1706 ctl_frontend_register(&ha_frontend); 1707 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1708 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1709 softc->is_single = 1; 1710 } else 1711 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1712 != CTL_HA_STATUS_SUCCESS) { 1713 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1714 softc->is_single = 1; 1715 } 1716 } 1717 return (0); 1718} 1719 1720void 1721ctl_shutdown(void) 1722{ 1723 struct ctl_softc *softc; 1724 struct ctl_lun *lun, *next_lun; 1725 1726 softc = (struct ctl_softc *)control_softc; 1727 1728 if (softc->is_single == 0) { 1729 ctl_ha_msg_shutdown(softc); 1730 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1731 != CTL_HA_STATUS_SUCCESS) 1732 printf("%s: ctl_ha_msg_deregister failed.\n", __func__); 1733 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 1734 printf("%s: ctl_ha_msg_destroy failed.\n", __func__); 1735 ctl_frontend_deregister(&ha_frontend); 1736 } 1737 1738 mtx_lock(&softc->ctl_lock); 1739 1740 /* 1741 * Free up each LUN. 1742 */ 1743 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1744 next_lun = STAILQ_NEXT(lun, links); 1745 ctl_free_lun(lun); 1746 } 1747 1748 mtx_unlock(&softc->ctl_lock); 1749 1750#if 0 1751 ctl_shutdown_thread(softc->work_thread); 1752 mtx_destroy(&softc->queue_lock); 1753#endif 1754 1755 ctl_tpc_shutdown(softc); 1756 uma_zdestroy(softc->io_zone); 1757 mtx_destroy(&softc->ctl_lock); 1758 1759 destroy_dev(softc->dev); 1760 1761 sysctl_ctx_free(&softc->sysctl_ctx); 1762 1763 free(control_softc, M_DEVBUF); 1764 control_softc = NULL; 1765} 1766 1767static int 1768ctl_module_event_handler(module_t mod, int what, void *arg) 1769{ 1770 1771 switch (what) { 1772 case MOD_LOAD: 1773 return (ctl_init()); 1774 case MOD_UNLOAD: 1775 return (EBUSY); 1776 default: 1777 return (EOPNOTSUPP); 1778 } 1779} 1780 1781/* 1782 * XXX KDM should we do some access checks here? Bump a reference count to 1783 * prevent a CTL module from being unloaded while someone has it open? 1784 */ 1785static int 1786ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1787{ 1788 return (0); 1789} 1790 1791static int 1792ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1793{ 1794 return (0); 1795} 1796 1797/* 1798 * Remove an initiator by port number and initiator ID. 1799 * Returns 0 for success, -1 for failure. 1800 */ 1801int 1802ctl_remove_initiator(struct ctl_port *port, int iid) 1803{ 1804 struct ctl_softc *softc = control_softc; 1805 1806 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1807 1808 if (iid > CTL_MAX_INIT_PER_PORT) { 1809 printf("%s: initiator ID %u > maximun %u!\n", 1810 __func__, iid, CTL_MAX_INIT_PER_PORT); 1811 return (-1); 1812 } 1813 1814 mtx_lock(&softc->ctl_lock); 1815 port->wwpn_iid[iid].in_use--; 1816 port->wwpn_iid[iid].last_use = time_uptime; 1817 mtx_unlock(&softc->ctl_lock); 1818 ctl_isc_announce_iid(port, iid); 1819 1820 return (0); 1821} 1822 1823/* 1824 * Add an initiator to the initiator map. 1825 * Returns iid for success, < 0 for failure. 1826 */ 1827int 1828ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1829{ 1830 struct ctl_softc *softc = control_softc; 1831 time_t best_time; 1832 int i, best; 1833 1834 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1835 1836 if (iid >= CTL_MAX_INIT_PER_PORT) { 1837 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1838 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1839 free(name, M_CTL); 1840 return (-1); 1841 } 1842 1843 mtx_lock(&softc->ctl_lock); 1844 1845 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1846 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1847 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1848 iid = i; 1849 break; 1850 } 1851 if (name != NULL && port->wwpn_iid[i].name != NULL && 1852 strcmp(name, port->wwpn_iid[i].name) == 0) { 1853 iid = i; 1854 break; 1855 } 1856 } 1857 } 1858 1859 if (iid < 0) { 1860 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1861 if (port->wwpn_iid[i].in_use == 0 && 1862 port->wwpn_iid[i].wwpn == 0 && 1863 port->wwpn_iid[i].name == NULL) { 1864 iid = i; 1865 break; 1866 } 1867 } 1868 } 1869 1870 if (iid < 0) { 1871 best = -1; 1872 best_time = INT32_MAX; 1873 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1874 if (port->wwpn_iid[i].in_use == 0) { 1875 if (port->wwpn_iid[i].last_use < best_time) { 1876 best = i; 1877 best_time = port->wwpn_iid[i].last_use; 1878 } 1879 } 1880 } 1881 iid = best; 1882 } 1883 1884 if (iid < 0) { 1885 mtx_unlock(&softc->ctl_lock); 1886 free(name, M_CTL); 1887 return (-2); 1888 } 1889 1890 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1891 /* 1892 * This is not an error yet. 1893 */ 1894 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1895#if 0 1896 printf("%s: port %d iid %u WWPN %#jx arrived" 1897 " again\n", __func__, port->targ_port, 1898 iid, (uintmax_t)wwpn); 1899#endif 1900 goto take; 1901 } 1902 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1903 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1904#if 0 1905 printf("%s: port %d iid %u name '%s' arrived" 1906 " again\n", __func__, port->targ_port, 1907 iid, name); 1908#endif 1909 goto take; 1910 } 1911 1912 /* 1913 * This is an error, but what do we do about it? The 1914 * driver is telling us we have a new WWPN for this 1915 * initiator ID, so we pretty much need to use it. 1916 */ 1917 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1918 " but WWPN %#jx '%s' is still at that address\n", 1919 __func__, port->targ_port, iid, wwpn, name, 1920 (uintmax_t)port->wwpn_iid[iid].wwpn, 1921 port->wwpn_iid[iid].name); 1922 1923 /* 1924 * XXX KDM clear have_ca and ua_pending on each LUN for 1925 * this initiator. 1926 */ 1927 } 1928take: 1929 free(port->wwpn_iid[iid].name, M_CTL); 1930 port->wwpn_iid[iid].name = name; 1931 port->wwpn_iid[iid].wwpn = wwpn; 1932 port->wwpn_iid[iid].in_use++; 1933 mtx_unlock(&softc->ctl_lock); 1934 ctl_isc_announce_iid(port, iid); 1935 1936 return (iid); 1937} 1938 1939static int 1940ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1941{ 1942 int len; 1943 1944 switch (port->port_type) { 1945 case CTL_PORT_FC: 1946 { 1947 struct scsi_transportid_fcp *id = 1948 (struct scsi_transportid_fcp *)buf; 1949 if (port->wwpn_iid[iid].wwpn == 0) 1950 return (0); 1951 memset(id, 0, sizeof(*id)); 1952 id->format_protocol = SCSI_PROTO_FC; 1953 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1954 return (sizeof(*id)); 1955 } 1956 case CTL_PORT_ISCSI: 1957 { 1958 struct scsi_transportid_iscsi_port *id = 1959 (struct scsi_transportid_iscsi_port *)buf; 1960 if (port->wwpn_iid[iid].name == NULL) 1961 return (0); 1962 memset(id, 0, 256); 1963 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1964 SCSI_PROTO_ISCSI; 1965 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1966 len = roundup2(min(len, 252), 4); 1967 scsi_ulto2b(len, id->additional_length); 1968 return (sizeof(*id) + len); 1969 } 1970 case CTL_PORT_SAS: 1971 { 1972 struct scsi_transportid_sas *id = 1973 (struct scsi_transportid_sas *)buf; 1974 if (port->wwpn_iid[iid].wwpn == 0) 1975 return (0); 1976 memset(id, 0, sizeof(*id)); 1977 id->format_protocol = SCSI_PROTO_SAS; 1978 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1979 return (sizeof(*id)); 1980 } 1981 default: 1982 { 1983 struct scsi_transportid_spi *id = 1984 (struct scsi_transportid_spi *)buf; 1985 memset(id, 0, sizeof(*id)); 1986 id->format_protocol = SCSI_PROTO_SPI; 1987 scsi_ulto2b(iid, id->scsi_addr); 1988 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1989 return (sizeof(*id)); 1990 } 1991 } 1992} 1993 1994/* 1995 * Serialize a command that went down the "wrong" side, and so was sent to 1996 * this controller for execution. The logic is a little different than the 1997 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1998 * sent back to the other side, but in the success case, we execute the 1999 * command on this side (XFER mode) or tell the other side to execute it 2000 * (SER_ONLY mode). 2001 */ 2002static int 2003ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2004{ 2005 struct ctl_softc *softc; 2006 union ctl_ha_msg msg_info; 2007 struct ctl_lun *lun; 2008 const struct ctl_cmd_entry *entry; 2009 int retval = 0; 2010 uint32_t targ_lun; 2011 2012 softc = control_softc; 2013 2014 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2015 mtx_lock(&softc->ctl_lock); 2016 if ((targ_lun < CTL_MAX_LUNS) && 2017 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 2018 mtx_lock(&lun->lun_lock); 2019 mtx_unlock(&softc->ctl_lock); 2020 /* 2021 * If the LUN is invalid, pretend that it doesn't exist. 2022 * It will go away as soon as all pending I/O has been 2023 * completed. 2024 */ 2025 if (lun->flags & CTL_LUN_DISABLED) { 2026 mtx_unlock(&lun->lun_lock); 2027 lun = NULL; 2028 } 2029 } else { 2030 mtx_unlock(&softc->ctl_lock); 2031 lun = NULL; 2032 } 2033 if (lun == NULL) { 2034 /* 2035 * The other node would not send this request to us unless 2036 * received announce that we are primary node for this LUN. 2037 * If this LUN does not exist now, it is probably result of 2038 * a race, so respond to initiator in the most opaque way. 2039 */ 2040 ctl_set_busy(ctsio); 2041 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2042 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2043 msg_info.hdr.serializing_sc = NULL; 2044 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2045 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2046 sizeof(msg_info.scsi), M_WAITOK); 2047 return(1); 2048 } 2049 2050 entry = ctl_get_cmd_entry(ctsio, NULL); 2051 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2052 mtx_unlock(&lun->lun_lock); 2053 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2054 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2055 msg_info.hdr.serializing_sc = NULL; 2056 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2057 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2058 sizeof(msg_info.scsi), M_WAITOK); 2059 return(1); 2060 } 2061 2062 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 2063 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun; 2064 2065 /* 2066 * Every I/O goes into the OOA queue for a 2067 * particular LUN, and stays there until completion. 2068 */ 2069#ifdef CTL_TIME_IO 2070 if (TAILQ_EMPTY(&lun->ooa_queue)) 2071 lun->idle_time += getsbinuptime() - lun->last_busy; 2072#endif 2073 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2074 2075 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2076 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2077 ooa_links))) { 2078 case CTL_ACTION_BLOCK: 2079 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2080 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2081 blocked_links); 2082 mtx_unlock(&lun->lun_lock); 2083 break; 2084 case CTL_ACTION_PASS: 2085 case CTL_ACTION_SKIP: 2086 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2087 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2088 ctl_enqueue_rtr((union ctl_io *)ctsio); 2089 mtx_unlock(&lun->lun_lock); 2090 } else { 2091 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2092 mtx_unlock(&lun->lun_lock); 2093 2094 /* send msg back to other side */ 2095 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2096 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2097 msg_info.hdr.msg_type = CTL_MSG_R2R; 2098 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2099 sizeof(msg_info.hdr), M_WAITOK); 2100 } 2101 break; 2102 case CTL_ACTION_OVERLAP: 2103 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2104 mtx_unlock(&lun->lun_lock); 2105 retval = 1; 2106 2107 ctl_set_overlapped_cmd(ctsio); 2108 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2109 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2110 msg_info.hdr.serializing_sc = NULL; 2111 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2112 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2113 sizeof(msg_info.scsi), M_WAITOK); 2114 break; 2115 case CTL_ACTION_OVERLAP_TAG: 2116 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2117 mtx_unlock(&lun->lun_lock); 2118 retval = 1; 2119 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2120 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2121 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2122 msg_info.hdr.serializing_sc = NULL; 2123 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2124 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2125 sizeof(msg_info.scsi), M_WAITOK); 2126 break; 2127 case CTL_ACTION_ERROR: 2128 default: 2129 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2130 mtx_unlock(&lun->lun_lock); 2131 retval = 1; 2132 2133 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2134 /*retry_count*/ 0); 2135 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2136 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2137 msg_info.hdr.serializing_sc = NULL; 2138 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2139 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2140 sizeof(msg_info.scsi), M_WAITOK); 2141 break; 2142 } 2143 return (retval); 2144} 2145 2146/* 2147 * Returns 0 for success, errno for failure. 2148 */ 2149static int 2150ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2151 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2152{ 2153 union ctl_io *io; 2154 int retval; 2155 2156 retval = 0; 2157 2158 mtx_lock(&lun->lun_lock); 2159 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2160 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2161 ooa_links)) { 2162 struct ctl_ooa_entry *entry; 2163 2164 /* 2165 * If we've got more than we can fit, just count the 2166 * remaining entries. 2167 */ 2168 if (*cur_fill_num >= ooa_hdr->alloc_num) 2169 continue; 2170 2171 entry = &kern_entries[*cur_fill_num]; 2172 2173 entry->tag_num = io->scsiio.tag_num; 2174 entry->lun_num = lun->lun; 2175#ifdef CTL_TIME_IO 2176 entry->start_bt = io->io_hdr.start_bt; 2177#endif 2178 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2179 entry->cdb_len = io->scsiio.cdb_len; 2180 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2181 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2182 2183 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2184 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2185 2186 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2187 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2188 2189 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2190 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2191 2192 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2193 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2194 } 2195 mtx_unlock(&lun->lun_lock); 2196 2197 return (retval); 2198} 2199 2200static void * 2201ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2202 size_t error_str_len) 2203{ 2204 void *kptr; 2205 2206 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2207 2208 if (copyin(user_addr, kptr, len) != 0) { 2209 snprintf(error_str, error_str_len, "Error copying %d bytes " 2210 "from user address %p to kernel address %p", len, 2211 user_addr, kptr); 2212 free(kptr, M_CTL); 2213 return (NULL); 2214 } 2215 2216 return (kptr); 2217} 2218 2219static void 2220ctl_free_args(int num_args, struct ctl_be_arg *args) 2221{ 2222 int i; 2223 2224 if (args == NULL) 2225 return; 2226 2227 for (i = 0; i < num_args; i++) { 2228 free(args[i].kname, M_CTL); 2229 free(args[i].kvalue, M_CTL); 2230 } 2231 2232 free(args, M_CTL); 2233} 2234 2235static struct ctl_be_arg * 2236ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2237 char *error_str, size_t error_str_len) 2238{ 2239 struct ctl_be_arg *args; 2240 int i; 2241 2242 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2243 error_str, error_str_len); 2244 2245 if (args == NULL) 2246 goto bailout; 2247 2248 for (i = 0; i < num_args; i++) { 2249 args[i].kname = NULL; 2250 args[i].kvalue = NULL; 2251 } 2252 2253 for (i = 0; i < num_args; i++) { 2254 uint8_t *tmpptr; 2255 2256 args[i].kname = ctl_copyin_alloc(args[i].name, 2257 args[i].namelen, error_str, error_str_len); 2258 if (args[i].kname == NULL) 2259 goto bailout; 2260 2261 if (args[i].kname[args[i].namelen - 1] != '\0') { 2262 snprintf(error_str, error_str_len, "Argument %d " 2263 "name is not NUL-terminated", i); 2264 goto bailout; 2265 } 2266 2267 if (args[i].flags & CTL_BEARG_RD) { 2268 tmpptr = ctl_copyin_alloc(args[i].value, 2269 args[i].vallen, error_str, error_str_len); 2270 if (tmpptr == NULL) 2271 goto bailout; 2272 if ((args[i].flags & CTL_BEARG_ASCII) 2273 && (tmpptr[args[i].vallen - 1] != '\0')) { 2274 snprintf(error_str, error_str_len, "Argument " 2275 "%d value is not NUL-terminated", i); 2276 goto bailout; 2277 } 2278 args[i].kvalue = tmpptr; 2279 } else { 2280 args[i].kvalue = malloc(args[i].vallen, 2281 M_CTL, M_WAITOK | M_ZERO); 2282 } 2283 } 2284 2285 return (args); 2286bailout: 2287 2288 ctl_free_args(num_args, args); 2289 2290 return (NULL); 2291} 2292 2293static void 2294ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2295{ 2296 int i; 2297 2298 for (i = 0; i < num_args; i++) { 2299 if (args[i].flags & CTL_BEARG_WR) 2300 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2301 } 2302} 2303 2304/* 2305 * Escape characters that are illegal or not recommended in XML. 2306 */ 2307int 2308ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2309{ 2310 char *end = str + size; 2311 int retval; 2312 2313 retval = 0; 2314 2315 for (; *str && str < end; str++) { 2316 switch (*str) { 2317 case '&': 2318 retval = sbuf_printf(sb, "&"); 2319 break; 2320 case '>': 2321 retval = sbuf_printf(sb, ">"); 2322 break; 2323 case '<': 2324 retval = sbuf_printf(sb, "<"); 2325 break; 2326 default: 2327 retval = sbuf_putc(sb, *str); 2328 break; 2329 } 2330 2331 if (retval != 0) 2332 break; 2333 2334 } 2335 2336 return (retval); 2337} 2338 2339static void 2340ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2341{ 2342 struct scsi_vpd_id_descriptor *desc; 2343 int i; 2344 2345 if (id == NULL || id->len < 4) 2346 return; 2347 desc = (struct scsi_vpd_id_descriptor *)id->data; 2348 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2349 case SVPD_ID_TYPE_T10: 2350 sbuf_printf(sb, "t10."); 2351 break; 2352 case SVPD_ID_TYPE_EUI64: 2353 sbuf_printf(sb, "eui."); 2354 break; 2355 case SVPD_ID_TYPE_NAA: 2356 sbuf_printf(sb, "naa."); 2357 break; 2358 case SVPD_ID_TYPE_SCSI_NAME: 2359 break; 2360 } 2361 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2362 case SVPD_ID_CODESET_BINARY: 2363 for (i = 0; i < desc->length; i++) 2364 sbuf_printf(sb, "%02x", desc->identifier[i]); 2365 break; 2366 case SVPD_ID_CODESET_ASCII: 2367 sbuf_printf(sb, "%.*s", (int)desc->length, 2368 (char *)desc->identifier); 2369 break; 2370 case SVPD_ID_CODESET_UTF8: 2371 sbuf_printf(sb, "%s", (char *)desc->identifier); 2372 break; 2373 } 2374} 2375 2376static int 2377ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2378 struct thread *td) 2379{ 2380 struct ctl_softc *softc; 2381 struct ctl_lun *lun; 2382 int retval; 2383 2384 softc = control_softc; 2385 2386 retval = 0; 2387 2388 switch (cmd) { 2389 case CTL_IO: 2390 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2391 break; 2392 case CTL_ENABLE_PORT: 2393 case CTL_DISABLE_PORT: 2394 case CTL_SET_PORT_WWNS: { 2395 struct ctl_port *port; 2396 struct ctl_port_entry *entry; 2397 2398 entry = (struct ctl_port_entry *)addr; 2399 2400 mtx_lock(&softc->ctl_lock); 2401 STAILQ_FOREACH(port, &softc->port_list, links) { 2402 int action, done; 2403 2404 if (port->targ_port < softc->port_min || 2405 port->targ_port >= softc->port_max) 2406 continue; 2407 2408 action = 0; 2409 done = 0; 2410 if ((entry->port_type == CTL_PORT_NONE) 2411 && (entry->targ_port == port->targ_port)) { 2412 /* 2413 * If the user only wants to enable or 2414 * disable or set WWNs on a specific port, 2415 * do the operation and we're done. 2416 */ 2417 action = 1; 2418 done = 1; 2419 } else if (entry->port_type & port->port_type) { 2420 /* 2421 * Compare the user's type mask with the 2422 * particular frontend type to see if we 2423 * have a match. 2424 */ 2425 action = 1; 2426 done = 0; 2427 2428 /* 2429 * Make sure the user isn't trying to set 2430 * WWNs on multiple ports at the same time. 2431 */ 2432 if (cmd == CTL_SET_PORT_WWNS) { 2433 printf("%s: Can't set WWNs on " 2434 "multiple ports\n", __func__); 2435 retval = EINVAL; 2436 break; 2437 } 2438 } 2439 if (action == 0) 2440 continue; 2441 2442 /* 2443 * XXX KDM we have to drop the lock here, because 2444 * the online/offline operations can potentially 2445 * block. We need to reference count the frontends 2446 * so they can't go away, 2447 */ 2448 if (cmd == CTL_ENABLE_PORT) { 2449 mtx_unlock(&softc->ctl_lock); 2450 ctl_port_online(port); 2451 mtx_lock(&softc->ctl_lock); 2452 } else if (cmd == CTL_DISABLE_PORT) { 2453 mtx_unlock(&softc->ctl_lock); 2454 ctl_port_offline(port); 2455 mtx_lock(&softc->ctl_lock); 2456 } else if (cmd == CTL_SET_PORT_WWNS) { 2457 ctl_port_set_wwns(port, 2458 (entry->flags & CTL_PORT_WWNN_VALID) ? 2459 1 : 0, entry->wwnn, 2460 (entry->flags & CTL_PORT_WWPN_VALID) ? 2461 1 : 0, entry->wwpn); 2462 } 2463 if (done != 0) 2464 break; 2465 } 2466 mtx_unlock(&softc->ctl_lock); 2467 break; 2468 } 2469 case CTL_GET_PORT_LIST: { 2470 struct ctl_port *port; 2471 struct ctl_port_list *list; 2472 int i; 2473 2474 list = (struct ctl_port_list *)addr; 2475 2476 if (list->alloc_len != (list->alloc_num * 2477 sizeof(struct ctl_port_entry))) { 2478 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2479 "alloc_num %u * sizeof(struct ctl_port_entry) " 2480 "%zu\n", __func__, list->alloc_len, 2481 list->alloc_num, sizeof(struct ctl_port_entry)); 2482 retval = EINVAL; 2483 break; 2484 } 2485 list->fill_len = 0; 2486 list->fill_num = 0; 2487 list->dropped_num = 0; 2488 i = 0; 2489 mtx_lock(&softc->ctl_lock); 2490 STAILQ_FOREACH(port, &softc->port_list, links) { 2491 struct ctl_port_entry entry, *list_entry; 2492 2493 if (list->fill_num >= list->alloc_num) { 2494 list->dropped_num++; 2495 continue; 2496 } 2497 2498 entry.port_type = port->port_type; 2499 strlcpy(entry.port_name, port->port_name, 2500 sizeof(entry.port_name)); 2501 entry.targ_port = port->targ_port; 2502 entry.physical_port = port->physical_port; 2503 entry.virtual_port = port->virtual_port; 2504 entry.wwnn = port->wwnn; 2505 entry.wwpn = port->wwpn; 2506 if (port->status & CTL_PORT_STATUS_ONLINE) 2507 entry.online = 1; 2508 else 2509 entry.online = 0; 2510 2511 list_entry = &list->entries[i]; 2512 2513 retval = copyout(&entry, list_entry, sizeof(entry)); 2514 if (retval != 0) { 2515 printf("%s: CTL_GET_PORT_LIST: copyout " 2516 "returned %d\n", __func__, retval); 2517 break; 2518 } 2519 i++; 2520 list->fill_num++; 2521 list->fill_len += sizeof(entry); 2522 } 2523 mtx_unlock(&softc->ctl_lock); 2524 2525 /* 2526 * If this is non-zero, we had a copyout fault, so there's 2527 * probably no point in attempting to set the status inside 2528 * the structure. 2529 */ 2530 if (retval != 0) 2531 break; 2532 2533 if (list->dropped_num > 0) 2534 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2535 else 2536 list->status = CTL_PORT_LIST_OK; 2537 break; 2538 } 2539 case CTL_DUMP_OOA: { 2540 union ctl_io *io; 2541 char printbuf[128]; 2542 struct sbuf sb; 2543 2544 mtx_lock(&softc->ctl_lock); 2545 printf("Dumping OOA queues:\n"); 2546 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2547 mtx_lock(&lun->lun_lock); 2548 for (io = (union ctl_io *)TAILQ_FIRST( 2549 &lun->ooa_queue); io != NULL; 2550 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2551 ooa_links)) { 2552 sbuf_new(&sb, printbuf, sizeof(printbuf), 2553 SBUF_FIXEDLEN); 2554 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2555 (intmax_t)lun->lun, 2556 io->scsiio.tag_num, 2557 (io->io_hdr.flags & 2558 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2559 (io->io_hdr.flags & 2560 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2561 (io->io_hdr.flags & 2562 CTL_FLAG_ABORT) ? " ABORT" : "", 2563 (io->io_hdr.flags & 2564 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2565 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2566 sbuf_finish(&sb); 2567 printf("%s\n", sbuf_data(&sb)); 2568 } 2569 mtx_unlock(&lun->lun_lock); 2570 } 2571 printf("OOA queues dump done\n"); 2572 mtx_unlock(&softc->ctl_lock); 2573 break; 2574 } 2575 case CTL_GET_OOA: { 2576 struct ctl_ooa *ooa_hdr; 2577 struct ctl_ooa_entry *entries; 2578 uint32_t cur_fill_num; 2579 2580 ooa_hdr = (struct ctl_ooa *)addr; 2581 2582 if ((ooa_hdr->alloc_len == 0) 2583 || (ooa_hdr->alloc_num == 0)) { 2584 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2585 "must be non-zero\n", __func__, 2586 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2587 retval = EINVAL; 2588 break; 2589 } 2590 2591 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2592 sizeof(struct ctl_ooa_entry))) { 2593 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2594 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2595 __func__, ooa_hdr->alloc_len, 2596 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2597 retval = EINVAL; 2598 break; 2599 } 2600 2601 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2602 if (entries == NULL) { 2603 printf("%s: could not allocate %d bytes for OOA " 2604 "dump\n", __func__, ooa_hdr->alloc_len); 2605 retval = ENOMEM; 2606 break; 2607 } 2608 2609 mtx_lock(&softc->ctl_lock); 2610 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2611 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2612 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2613 mtx_unlock(&softc->ctl_lock); 2614 free(entries, M_CTL); 2615 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2616 __func__, (uintmax_t)ooa_hdr->lun_num); 2617 retval = EINVAL; 2618 break; 2619 } 2620 2621 cur_fill_num = 0; 2622 2623 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2624 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2625 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2626 ooa_hdr, entries); 2627 if (retval != 0) 2628 break; 2629 } 2630 if (retval != 0) { 2631 mtx_unlock(&softc->ctl_lock); 2632 free(entries, M_CTL); 2633 break; 2634 } 2635 } else { 2636 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2637 2638 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2639 entries); 2640 } 2641 mtx_unlock(&softc->ctl_lock); 2642 2643 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2644 ooa_hdr->fill_len = ooa_hdr->fill_num * 2645 sizeof(struct ctl_ooa_entry); 2646 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2647 if (retval != 0) { 2648 printf("%s: error copying out %d bytes for OOA dump\n", 2649 __func__, ooa_hdr->fill_len); 2650 } 2651 2652 getbintime(&ooa_hdr->cur_bt); 2653 2654 if (cur_fill_num > ooa_hdr->alloc_num) { 2655 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2656 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2657 } else { 2658 ooa_hdr->dropped_num = 0; 2659 ooa_hdr->status = CTL_OOA_OK; 2660 } 2661 2662 free(entries, M_CTL); 2663 break; 2664 } 2665 case CTL_CHECK_OOA: { 2666 union ctl_io *io; 2667 struct ctl_ooa_info *ooa_info; 2668 2669 2670 ooa_info = (struct ctl_ooa_info *)addr; 2671 2672 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2673 ooa_info->status = CTL_OOA_INVALID_LUN; 2674 break; 2675 } 2676 mtx_lock(&softc->ctl_lock); 2677 lun = softc->ctl_luns[ooa_info->lun_id]; 2678 if (lun == NULL) { 2679 mtx_unlock(&softc->ctl_lock); 2680 ooa_info->status = CTL_OOA_INVALID_LUN; 2681 break; 2682 } 2683 mtx_lock(&lun->lun_lock); 2684 mtx_unlock(&softc->ctl_lock); 2685 ooa_info->num_entries = 0; 2686 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2687 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2688 &io->io_hdr, ooa_links)) { 2689 ooa_info->num_entries++; 2690 } 2691 mtx_unlock(&lun->lun_lock); 2692 2693 ooa_info->status = CTL_OOA_SUCCESS; 2694 2695 break; 2696 } 2697 case CTL_DELAY_IO: { 2698 struct ctl_io_delay_info *delay_info; 2699 2700 delay_info = (struct ctl_io_delay_info *)addr; 2701 2702#ifdef CTL_IO_DELAY 2703 mtx_lock(&softc->ctl_lock); 2704 2705 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2706 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2707 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2708 } else { 2709 lun = softc->ctl_luns[delay_info->lun_id]; 2710 mtx_lock(&lun->lun_lock); 2711 2712 delay_info->status = CTL_DELAY_STATUS_OK; 2713 2714 switch (delay_info->delay_type) { 2715 case CTL_DELAY_TYPE_CONT: 2716 break; 2717 case CTL_DELAY_TYPE_ONESHOT: 2718 break; 2719 default: 2720 delay_info->status = 2721 CTL_DELAY_STATUS_INVALID_TYPE; 2722 break; 2723 } 2724 2725 switch (delay_info->delay_loc) { 2726 case CTL_DELAY_LOC_DATAMOVE: 2727 lun->delay_info.datamove_type = 2728 delay_info->delay_type; 2729 lun->delay_info.datamove_delay = 2730 delay_info->delay_secs; 2731 break; 2732 case CTL_DELAY_LOC_DONE: 2733 lun->delay_info.done_type = 2734 delay_info->delay_type; 2735 lun->delay_info.done_delay = 2736 delay_info->delay_secs; 2737 break; 2738 default: 2739 delay_info->status = 2740 CTL_DELAY_STATUS_INVALID_LOC; 2741 break; 2742 } 2743 mtx_unlock(&lun->lun_lock); 2744 } 2745 2746 mtx_unlock(&softc->ctl_lock); 2747#else 2748 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2749#endif /* CTL_IO_DELAY */ 2750 break; 2751 } 2752 case CTL_REALSYNC_SET: { 2753 int *syncstate; 2754 2755 syncstate = (int *)addr; 2756 2757 mtx_lock(&softc->ctl_lock); 2758 switch (*syncstate) { 2759 case 0: 2760 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2761 break; 2762 case 1: 2763 softc->flags |= CTL_FLAG_REAL_SYNC; 2764 break; 2765 default: 2766 retval = EINVAL; 2767 break; 2768 } 2769 mtx_unlock(&softc->ctl_lock); 2770 break; 2771 } 2772 case CTL_REALSYNC_GET: { 2773 int *syncstate; 2774 2775 syncstate = (int*)addr; 2776 2777 mtx_lock(&softc->ctl_lock); 2778 if (softc->flags & CTL_FLAG_REAL_SYNC) 2779 *syncstate = 1; 2780 else 2781 *syncstate = 0; 2782 mtx_unlock(&softc->ctl_lock); 2783 2784 break; 2785 } 2786 case CTL_SETSYNC: 2787 case CTL_GETSYNC: { 2788 struct ctl_sync_info *sync_info; 2789 2790 sync_info = (struct ctl_sync_info *)addr; 2791 2792 mtx_lock(&softc->ctl_lock); 2793 lun = softc->ctl_luns[sync_info->lun_id]; 2794 if (lun == NULL) { 2795 mtx_unlock(&softc->ctl_lock); 2796 sync_info->status = CTL_GS_SYNC_NO_LUN; 2797 break; 2798 } 2799 /* 2800 * Get or set the sync interval. We're not bounds checking 2801 * in the set case, hopefully the user won't do something 2802 * silly. 2803 */ 2804 mtx_lock(&lun->lun_lock); 2805 mtx_unlock(&softc->ctl_lock); 2806 if (cmd == CTL_GETSYNC) 2807 sync_info->sync_interval = lun->sync_interval; 2808 else 2809 lun->sync_interval = sync_info->sync_interval; 2810 mtx_unlock(&lun->lun_lock); 2811 2812 sync_info->status = CTL_GS_SYNC_OK; 2813 2814 break; 2815 } 2816 case CTL_GETSTATS: { 2817 struct ctl_stats *stats; 2818 int i; 2819 2820 stats = (struct ctl_stats *)addr; 2821 2822 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2823 stats->alloc_len) { 2824 stats->status = CTL_SS_NEED_MORE_SPACE; 2825 stats->num_luns = softc->num_luns; 2826 break; 2827 } 2828 /* 2829 * XXX KDM no locking here. If the LUN list changes, 2830 * things can blow up. 2831 */ 2832 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2833 i++, lun = STAILQ_NEXT(lun, links)) { 2834 retval = copyout(&lun->stats, &stats->lun_stats[i], 2835 sizeof(lun->stats)); 2836 if (retval != 0) 2837 break; 2838 } 2839 stats->num_luns = softc->num_luns; 2840 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2841 softc->num_luns; 2842 stats->status = CTL_SS_OK; 2843#ifdef CTL_TIME_IO 2844 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2845#else 2846 stats->flags = CTL_STATS_FLAG_NONE; 2847#endif 2848 getnanouptime(&stats->timestamp); 2849 break; 2850 } 2851 case CTL_ERROR_INJECT: { 2852 struct ctl_error_desc *err_desc, *new_err_desc; 2853 2854 err_desc = (struct ctl_error_desc *)addr; 2855 2856 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2857 M_WAITOK | M_ZERO); 2858 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2859 2860 mtx_lock(&softc->ctl_lock); 2861 lun = softc->ctl_luns[err_desc->lun_id]; 2862 if (lun == NULL) { 2863 mtx_unlock(&softc->ctl_lock); 2864 free(new_err_desc, M_CTL); 2865 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2866 __func__, (uintmax_t)err_desc->lun_id); 2867 retval = EINVAL; 2868 break; 2869 } 2870 mtx_lock(&lun->lun_lock); 2871 mtx_unlock(&softc->ctl_lock); 2872 2873 /* 2874 * We could do some checking here to verify the validity 2875 * of the request, but given the complexity of error 2876 * injection requests, the checking logic would be fairly 2877 * complex. 2878 * 2879 * For now, if the request is invalid, it just won't get 2880 * executed and might get deleted. 2881 */ 2882 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2883 2884 /* 2885 * XXX KDM check to make sure the serial number is unique, 2886 * in case we somehow manage to wrap. That shouldn't 2887 * happen for a very long time, but it's the right thing to 2888 * do. 2889 */ 2890 new_err_desc->serial = lun->error_serial; 2891 err_desc->serial = lun->error_serial; 2892 lun->error_serial++; 2893 2894 mtx_unlock(&lun->lun_lock); 2895 break; 2896 } 2897 case CTL_ERROR_INJECT_DELETE: { 2898 struct ctl_error_desc *delete_desc, *desc, *desc2; 2899 int delete_done; 2900 2901 delete_desc = (struct ctl_error_desc *)addr; 2902 delete_done = 0; 2903 2904 mtx_lock(&softc->ctl_lock); 2905 lun = softc->ctl_luns[delete_desc->lun_id]; 2906 if (lun == NULL) { 2907 mtx_unlock(&softc->ctl_lock); 2908 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2909 __func__, (uintmax_t)delete_desc->lun_id); 2910 retval = EINVAL; 2911 break; 2912 } 2913 mtx_lock(&lun->lun_lock); 2914 mtx_unlock(&softc->ctl_lock); 2915 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2916 if (desc->serial != delete_desc->serial) 2917 continue; 2918 2919 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2920 links); 2921 free(desc, M_CTL); 2922 delete_done = 1; 2923 } 2924 mtx_unlock(&lun->lun_lock); 2925 if (delete_done == 0) { 2926 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2927 "error serial %ju on LUN %u\n", __func__, 2928 delete_desc->serial, delete_desc->lun_id); 2929 retval = EINVAL; 2930 break; 2931 } 2932 break; 2933 } 2934 case CTL_DUMP_STRUCTS: { 2935 int i, j, k; 2936 struct ctl_port *port; 2937 struct ctl_frontend *fe; 2938 2939 mtx_lock(&softc->ctl_lock); 2940 printf("CTL Persistent Reservation information start:\n"); 2941 for (i = 0; i < CTL_MAX_LUNS; i++) { 2942 lun = softc->ctl_luns[i]; 2943 2944 if ((lun == NULL) 2945 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2946 continue; 2947 2948 for (j = 0; j < CTL_MAX_PORTS; j++) { 2949 if (lun->pr_keys[j] == NULL) 2950 continue; 2951 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2952 if (lun->pr_keys[j][k] == 0) 2953 continue; 2954 printf(" LUN %d port %d iid %d key " 2955 "%#jx\n", i, j, k, 2956 (uintmax_t)lun->pr_keys[j][k]); 2957 } 2958 } 2959 } 2960 printf("CTL Persistent Reservation information end\n"); 2961 printf("CTL Ports:\n"); 2962 STAILQ_FOREACH(port, &softc->port_list, links) { 2963 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2964 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2965 port->frontend->name, port->port_type, 2966 port->physical_port, port->virtual_port, 2967 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2968 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2969 if (port->wwpn_iid[j].in_use == 0 && 2970 port->wwpn_iid[j].wwpn == 0 && 2971 port->wwpn_iid[j].name == NULL) 2972 continue; 2973 2974 printf(" iid %u use %d WWPN %#jx '%s'\n", 2975 j, port->wwpn_iid[j].in_use, 2976 (uintmax_t)port->wwpn_iid[j].wwpn, 2977 port->wwpn_iid[j].name); 2978 } 2979 } 2980 printf("CTL Port information end\n"); 2981 mtx_unlock(&softc->ctl_lock); 2982 /* 2983 * XXX KDM calling this without a lock. We'd likely want 2984 * to drop the lock before calling the frontend's dump 2985 * routine anyway. 2986 */ 2987 printf("CTL Frontends:\n"); 2988 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2989 printf(" Frontend '%s'\n", fe->name); 2990 if (fe->fe_dump != NULL) 2991 fe->fe_dump(); 2992 } 2993 printf("CTL Frontend information end\n"); 2994 break; 2995 } 2996 case CTL_LUN_REQ: { 2997 struct ctl_lun_req *lun_req; 2998 struct ctl_backend_driver *backend; 2999 3000 lun_req = (struct ctl_lun_req *)addr; 3001 3002 backend = ctl_backend_find(lun_req->backend); 3003 if (backend == NULL) { 3004 lun_req->status = CTL_LUN_ERROR; 3005 snprintf(lun_req->error_str, 3006 sizeof(lun_req->error_str), 3007 "Backend \"%s\" not found.", 3008 lun_req->backend); 3009 break; 3010 } 3011 if (lun_req->num_be_args > 0) { 3012 lun_req->kern_be_args = ctl_copyin_args( 3013 lun_req->num_be_args, 3014 lun_req->be_args, 3015 lun_req->error_str, 3016 sizeof(lun_req->error_str)); 3017 if (lun_req->kern_be_args == NULL) { 3018 lun_req->status = CTL_LUN_ERROR; 3019 break; 3020 } 3021 } 3022 3023 retval = backend->ioctl(dev, cmd, addr, flag, td); 3024 3025 if (lun_req->num_be_args > 0) { 3026 ctl_copyout_args(lun_req->num_be_args, 3027 lun_req->kern_be_args); 3028 ctl_free_args(lun_req->num_be_args, 3029 lun_req->kern_be_args); 3030 } 3031 break; 3032 } 3033 case CTL_LUN_LIST: { 3034 struct sbuf *sb; 3035 struct ctl_lun_list *list; 3036 struct ctl_option *opt; 3037 3038 list = (struct ctl_lun_list *)addr; 3039 3040 /* 3041 * Allocate a fixed length sbuf here, based on the length 3042 * of the user's buffer. We could allocate an auto-extending 3043 * buffer, and then tell the user how much larger our 3044 * amount of data is than his buffer, but that presents 3045 * some problems: 3046 * 3047 * 1. The sbuf(9) routines use a blocking malloc, and so 3048 * we can't hold a lock while calling them with an 3049 * auto-extending buffer. 3050 * 3051 * 2. There is not currently a LUN reference counting 3052 * mechanism, outside of outstanding transactions on 3053 * the LUN's OOA queue. So a LUN could go away on us 3054 * while we're getting the LUN number, backend-specific 3055 * information, etc. Thus, given the way things 3056 * currently work, we need to hold the CTL lock while 3057 * grabbing LUN information. 3058 * 3059 * So, from the user's standpoint, the best thing to do is 3060 * allocate what he thinks is a reasonable buffer length, 3061 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3062 * double the buffer length and try again. (And repeat 3063 * that until he succeeds.) 3064 */ 3065 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3066 if (sb == NULL) { 3067 list->status = CTL_LUN_LIST_ERROR; 3068 snprintf(list->error_str, sizeof(list->error_str), 3069 "Unable to allocate %d bytes for LUN list", 3070 list->alloc_len); 3071 break; 3072 } 3073 3074 sbuf_printf(sb, "<ctllunlist>\n"); 3075 3076 mtx_lock(&softc->ctl_lock); 3077 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3078 mtx_lock(&lun->lun_lock); 3079 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3080 (uintmax_t)lun->lun); 3081 3082 /* 3083 * Bail out as soon as we see that we've overfilled 3084 * the buffer. 3085 */ 3086 if (retval != 0) 3087 break; 3088 3089 retval = sbuf_printf(sb, "\t<backend_type>%s" 3090 "</backend_type>\n", 3091 (lun->backend == NULL) ? "none" : 3092 lun->backend->name); 3093 3094 if (retval != 0) 3095 break; 3096 3097 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3098 lun->be_lun->lun_type); 3099 3100 if (retval != 0) 3101 break; 3102 3103 if (lun->backend == NULL) { 3104 retval = sbuf_printf(sb, "</lun>\n"); 3105 if (retval != 0) 3106 break; 3107 continue; 3108 } 3109 3110 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3111 (lun->be_lun->maxlba > 0) ? 3112 lun->be_lun->maxlba + 1 : 0); 3113 3114 if (retval != 0) 3115 break; 3116 3117 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3118 lun->be_lun->blocksize); 3119 3120 if (retval != 0) 3121 break; 3122 3123 retval = sbuf_printf(sb, "\t<serial_number>"); 3124 3125 if (retval != 0) 3126 break; 3127 3128 retval = ctl_sbuf_printf_esc(sb, 3129 lun->be_lun->serial_num, 3130 sizeof(lun->be_lun->serial_num)); 3131 3132 if (retval != 0) 3133 break; 3134 3135 retval = sbuf_printf(sb, "</serial_number>\n"); 3136 3137 if (retval != 0) 3138 break; 3139 3140 retval = sbuf_printf(sb, "\t<device_id>"); 3141 3142 if (retval != 0) 3143 break; 3144 3145 retval = ctl_sbuf_printf_esc(sb, 3146 lun->be_lun->device_id, 3147 sizeof(lun->be_lun->device_id)); 3148 3149 if (retval != 0) 3150 break; 3151 3152 retval = sbuf_printf(sb, "</device_id>\n"); 3153 3154 if (retval != 0) 3155 break; 3156 3157 if (lun->backend->lun_info != NULL) { 3158 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3159 if (retval != 0) 3160 break; 3161 } 3162 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3163 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3164 opt->name, opt->value, opt->name); 3165 if (retval != 0) 3166 break; 3167 } 3168 3169 retval = sbuf_printf(sb, "</lun>\n"); 3170 3171 if (retval != 0) 3172 break; 3173 mtx_unlock(&lun->lun_lock); 3174 } 3175 if (lun != NULL) 3176 mtx_unlock(&lun->lun_lock); 3177 mtx_unlock(&softc->ctl_lock); 3178 3179 if ((retval != 0) 3180 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3181 retval = 0; 3182 sbuf_delete(sb); 3183 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3184 snprintf(list->error_str, sizeof(list->error_str), 3185 "Out of space, %d bytes is too small", 3186 list->alloc_len); 3187 break; 3188 } 3189 3190 sbuf_finish(sb); 3191 3192 retval = copyout(sbuf_data(sb), list->lun_xml, 3193 sbuf_len(sb) + 1); 3194 3195 list->fill_len = sbuf_len(sb) + 1; 3196 list->status = CTL_LUN_LIST_OK; 3197 sbuf_delete(sb); 3198 break; 3199 } 3200 case CTL_ISCSI: { 3201 struct ctl_iscsi *ci; 3202 struct ctl_frontend *fe; 3203 3204 ci = (struct ctl_iscsi *)addr; 3205 3206 fe = ctl_frontend_find("iscsi"); 3207 if (fe == NULL) { 3208 ci->status = CTL_ISCSI_ERROR; 3209 snprintf(ci->error_str, sizeof(ci->error_str), 3210 "Frontend \"iscsi\" not found."); 3211 break; 3212 } 3213 3214 retval = fe->ioctl(dev, cmd, addr, flag, td); 3215 break; 3216 } 3217 case CTL_PORT_REQ: { 3218 struct ctl_req *req; 3219 struct ctl_frontend *fe; 3220 3221 req = (struct ctl_req *)addr; 3222 3223 fe = ctl_frontend_find(req->driver); 3224 if (fe == NULL) { 3225 req->status = CTL_LUN_ERROR; 3226 snprintf(req->error_str, sizeof(req->error_str), 3227 "Frontend \"%s\" not found.", req->driver); 3228 break; 3229 } 3230 if (req->num_args > 0) { 3231 req->kern_args = ctl_copyin_args(req->num_args, 3232 req->args, req->error_str, sizeof(req->error_str)); 3233 if (req->kern_args == NULL) { 3234 req->status = CTL_LUN_ERROR; 3235 break; 3236 } 3237 } 3238 3239 if (fe->ioctl) 3240 retval = fe->ioctl(dev, cmd, addr, flag, td); 3241 else 3242 retval = ENODEV; 3243 3244 if (req->num_args > 0) { 3245 ctl_copyout_args(req->num_args, req->kern_args); 3246 ctl_free_args(req->num_args, req->kern_args); 3247 } 3248 break; 3249 } 3250 case CTL_PORT_LIST: { 3251 struct sbuf *sb; 3252 struct ctl_port *port; 3253 struct ctl_lun_list *list; 3254 struct ctl_option *opt; 3255 int j; 3256 uint32_t plun; 3257 3258 list = (struct ctl_lun_list *)addr; 3259 3260 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3261 if (sb == NULL) { 3262 list->status = CTL_LUN_LIST_ERROR; 3263 snprintf(list->error_str, sizeof(list->error_str), 3264 "Unable to allocate %d bytes for LUN list", 3265 list->alloc_len); 3266 break; 3267 } 3268 3269 sbuf_printf(sb, "<ctlportlist>\n"); 3270 3271 mtx_lock(&softc->ctl_lock); 3272 STAILQ_FOREACH(port, &softc->port_list, links) { 3273 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3274 (uintmax_t)port->targ_port); 3275 3276 /* 3277 * Bail out as soon as we see that we've overfilled 3278 * the buffer. 3279 */ 3280 if (retval != 0) 3281 break; 3282 3283 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3284 "</frontend_type>\n", port->frontend->name); 3285 if (retval != 0) 3286 break; 3287 3288 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3289 port->port_type); 3290 if (retval != 0) 3291 break; 3292 3293 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3294 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3295 if (retval != 0) 3296 break; 3297 3298 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3299 port->port_name); 3300 if (retval != 0) 3301 break; 3302 3303 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3304 port->physical_port); 3305 if (retval != 0) 3306 break; 3307 3308 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3309 port->virtual_port); 3310 if (retval != 0) 3311 break; 3312 3313 if (port->target_devid != NULL) { 3314 sbuf_printf(sb, "\t<target>"); 3315 ctl_id_sbuf(port->target_devid, sb); 3316 sbuf_printf(sb, "</target>\n"); 3317 } 3318 3319 if (port->port_devid != NULL) { 3320 sbuf_printf(sb, "\t<port>"); 3321 ctl_id_sbuf(port->port_devid, sb); 3322 sbuf_printf(sb, "</port>\n"); 3323 } 3324 3325 if (port->port_info != NULL) { 3326 retval = port->port_info(port->onoff_arg, sb); 3327 if (retval != 0) 3328 break; 3329 } 3330 STAILQ_FOREACH(opt, &port->options, links) { 3331 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3332 opt->name, opt->value, opt->name); 3333 if (retval != 0) 3334 break; 3335 } 3336 3337 if (port->lun_map != NULL) { 3338 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3339 for (j = 0; j < CTL_MAX_LUNS; j++) { 3340 plun = ctl_lun_map_from_port(port, j); 3341 if (plun >= CTL_MAX_LUNS) 3342 continue; 3343 sbuf_printf(sb, 3344 "\t<lun id=\"%u\">%u</lun>\n", 3345 j, plun); 3346 } 3347 } 3348 3349 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3350 if (port->wwpn_iid[j].in_use == 0 || 3351 (port->wwpn_iid[j].wwpn == 0 && 3352 port->wwpn_iid[j].name == NULL)) 3353 continue; 3354 3355 if (port->wwpn_iid[j].name != NULL) 3356 retval = sbuf_printf(sb, 3357 "\t<initiator id=\"%u\">%s</initiator>\n", 3358 j, port->wwpn_iid[j].name); 3359 else 3360 retval = sbuf_printf(sb, 3361 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3362 j, port->wwpn_iid[j].wwpn); 3363 if (retval != 0) 3364 break; 3365 } 3366 if (retval != 0) 3367 break; 3368 3369 retval = sbuf_printf(sb, "</targ_port>\n"); 3370 if (retval != 0) 3371 break; 3372 } 3373 mtx_unlock(&softc->ctl_lock); 3374 3375 if ((retval != 0) 3376 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3377 retval = 0; 3378 sbuf_delete(sb); 3379 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3380 snprintf(list->error_str, sizeof(list->error_str), 3381 "Out of space, %d bytes is too small", 3382 list->alloc_len); 3383 break; 3384 } 3385 3386 sbuf_finish(sb); 3387 3388 retval = copyout(sbuf_data(sb), list->lun_xml, 3389 sbuf_len(sb) + 1); 3390 3391 list->fill_len = sbuf_len(sb) + 1; 3392 list->status = CTL_LUN_LIST_OK; 3393 sbuf_delete(sb); 3394 break; 3395 } 3396 case CTL_LUN_MAP: { 3397 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3398 struct ctl_port *port; 3399 3400 mtx_lock(&softc->ctl_lock); 3401 if (lm->port < softc->port_min || 3402 lm->port >= softc->port_max || 3403 (port = softc->ctl_ports[lm->port]) == NULL) { 3404 mtx_unlock(&softc->ctl_lock); 3405 return (ENXIO); 3406 } 3407 if (port->status & CTL_PORT_STATUS_ONLINE) { 3408 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3409 if (ctl_lun_map_to_port(port, lun->lun) >= 3410 CTL_MAX_LUNS) 3411 continue; 3412 mtx_lock(&lun->lun_lock); 3413 ctl_est_ua_port(lun, lm->port, -1, 3414 CTL_UA_LUN_CHANGE); 3415 mtx_unlock(&lun->lun_lock); 3416 } 3417 } 3418 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3419 if (lm->plun < CTL_MAX_LUNS) { 3420 if (lm->lun == UINT32_MAX) 3421 retval = ctl_lun_map_unset(port, lm->plun); 3422 else if (lm->lun < CTL_MAX_LUNS && 3423 softc->ctl_luns[lm->lun] != NULL) 3424 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3425 else 3426 return (ENXIO); 3427 } else if (lm->plun == UINT32_MAX) { 3428 if (lm->lun == UINT32_MAX) 3429 retval = ctl_lun_map_deinit(port); 3430 else 3431 retval = ctl_lun_map_init(port); 3432 } else 3433 return (ENXIO); 3434 if (port->status & CTL_PORT_STATUS_ONLINE) 3435 ctl_isc_announce_port(port); 3436 break; 3437 } 3438 default: { 3439 /* XXX KDM should we fix this? */ 3440#if 0 3441 struct ctl_backend_driver *backend; 3442 unsigned int type; 3443 int found; 3444 3445 found = 0; 3446 3447 /* 3448 * We encode the backend type as the ioctl type for backend 3449 * ioctls. So parse it out here, and then search for a 3450 * backend of this type. 3451 */ 3452 type = _IOC_TYPE(cmd); 3453 3454 STAILQ_FOREACH(backend, &softc->be_list, links) { 3455 if (backend->type == type) { 3456 found = 1; 3457 break; 3458 } 3459 } 3460 if (found == 0) { 3461 printf("ctl: unknown ioctl command %#lx or backend " 3462 "%d\n", cmd, type); 3463 retval = EINVAL; 3464 break; 3465 } 3466 retval = backend->ioctl(dev, cmd, addr, flag, td); 3467#endif 3468 retval = ENOTTY; 3469 break; 3470 } 3471 } 3472 return (retval); 3473} 3474 3475uint32_t 3476ctl_get_initindex(struct ctl_nexus *nexus) 3477{ 3478 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3479} 3480 3481int 3482ctl_lun_map_init(struct ctl_port *port) 3483{ 3484 struct ctl_softc *softc = control_softc; 3485 struct ctl_lun *lun; 3486 uint32_t i; 3487 3488 if (port->lun_map == NULL) 3489 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3490 M_CTL, M_NOWAIT); 3491 if (port->lun_map == NULL) 3492 return (ENOMEM); 3493 for (i = 0; i < CTL_MAX_LUNS; i++) 3494 port->lun_map[i] = UINT32_MAX; 3495 if (port->status & CTL_PORT_STATUS_ONLINE) { 3496 if (port->lun_disable != NULL) { 3497 STAILQ_FOREACH(lun, &softc->lun_list, links) 3498 port->lun_disable(port->targ_lun_arg, lun->lun); 3499 } 3500 ctl_isc_announce_port(port); 3501 } 3502 return (0); 3503} 3504 3505int 3506ctl_lun_map_deinit(struct ctl_port *port) 3507{ 3508 struct ctl_softc *softc = control_softc; 3509 struct ctl_lun *lun; 3510 3511 if (port->lun_map == NULL) 3512 return (0); 3513 free(port->lun_map, M_CTL); 3514 port->lun_map = NULL; 3515 if (port->status & CTL_PORT_STATUS_ONLINE) { 3516 if (port->lun_enable != NULL) { 3517 STAILQ_FOREACH(lun, &softc->lun_list, links) 3518 port->lun_enable(port->targ_lun_arg, lun->lun); 3519 } 3520 ctl_isc_announce_port(port); 3521 } 3522 return (0); 3523} 3524 3525int 3526ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3527{ 3528 int status; 3529 uint32_t old; 3530 3531 if (port->lun_map == NULL) { 3532 status = ctl_lun_map_init(port); 3533 if (status != 0) 3534 return (status); 3535 } 3536 old = port->lun_map[plun]; 3537 port->lun_map[plun] = glun; 3538 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) { 3539 if (port->lun_enable != NULL) 3540 port->lun_enable(port->targ_lun_arg, plun); 3541 ctl_isc_announce_port(port); 3542 } 3543 return (0); 3544} 3545 3546int 3547ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3548{ 3549 uint32_t old; 3550 3551 if (port->lun_map == NULL) 3552 return (0); 3553 old = port->lun_map[plun]; 3554 port->lun_map[plun] = UINT32_MAX; 3555 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) { 3556 if (port->lun_disable != NULL) 3557 port->lun_disable(port->targ_lun_arg, plun); 3558 ctl_isc_announce_port(port); 3559 } 3560 return (0); 3561} 3562 3563uint32_t 3564ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3565{ 3566 3567 if (port == NULL) 3568 return (UINT32_MAX); 3569 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3570 return (lun_id); 3571 return (port->lun_map[lun_id]); 3572} 3573 3574uint32_t 3575ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3576{ 3577 uint32_t i; 3578 3579 if (port == NULL) 3580 return (UINT32_MAX); 3581 if (port->lun_map == NULL) 3582 return (lun_id); 3583 for (i = 0; i < CTL_MAX_LUNS; i++) { 3584 if (port->lun_map[i] == lun_id) 3585 return (i); 3586 } 3587 return (UINT32_MAX); 3588} 3589 3590static struct ctl_port * 3591ctl_io_port(struct ctl_io_hdr *io_hdr) 3592{ 3593 3594 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]); 3595} 3596 3597int 3598ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3599{ 3600 int i; 3601 3602 for (i = first; i < last; i++) { 3603 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3604 return (i); 3605 } 3606 return (-1); 3607} 3608 3609int 3610ctl_set_mask(uint32_t *mask, uint32_t bit) 3611{ 3612 uint32_t chunk, piece; 3613 3614 chunk = bit >> 5; 3615 piece = bit % (sizeof(uint32_t) * 8); 3616 3617 if ((mask[chunk] & (1 << piece)) != 0) 3618 return (-1); 3619 else 3620 mask[chunk] |= (1 << piece); 3621 3622 return (0); 3623} 3624 3625int 3626ctl_clear_mask(uint32_t *mask, uint32_t bit) 3627{ 3628 uint32_t chunk, piece; 3629 3630 chunk = bit >> 5; 3631 piece = bit % (sizeof(uint32_t) * 8); 3632 3633 if ((mask[chunk] & (1 << piece)) == 0) 3634 return (-1); 3635 else 3636 mask[chunk] &= ~(1 << piece); 3637 3638 return (0); 3639} 3640 3641int 3642ctl_is_set(uint32_t *mask, uint32_t bit) 3643{ 3644 uint32_t chunk, piece; 3645 3646 chunk = bit >> 5; 3647 piece = bit % (sizeof(uint32_t) * 8); 3648 3649 if ((mask[chunk] & (1 << piece)) == 0) 3650 return (0); 3651 else 3652 return (1); 3653} 3654 3655static uint64_t 3656ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3657{ 3658 uint64_t *t; 3659 3660 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3661 if (t == NULL) 3662 return (0); 3663 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3664} 3665 3666static void 3667ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3668{ 3669 uint64_t *t; 3670 3671 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3672 if (t == NULL) 3673 return; 3674 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3675} 3676 3677static void 3678ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3679{ 3680 uint64_t *p; 3681 u_int i; 3682 3683 i = residx/CTL_MAX_INIT_PER_PORT; 3684 if (lun->pr_keys[i] != NULL) 3685 return; 3686 mtx_unlock(&lun->lun_lock); 3687 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3688 M_WAITOK | M_ZERO); 3689 mtx_lock(&lun->lun_lock); 3690 if (lun->pr_keys[i] == NULL) 3691 lun->pr_keys[i] = p; 3692 else 3693 free(p, M_CTL); 3694} 3695 3696static void 3697ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3698{ 3699 uint64_t *t; 3700 3701 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3702 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3703 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3704} 3705 3706/* 3707 * ctl_softc, pool_name, total_ctl_io are passed in. 3708 * npool is passed out. 3709 */ 3710int 3711ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3712 uint32_t total_ctl_io, void **npool) 3713{ 3714#ifdef IO_POOLS 3715 struct ctl_io_pool *pool; 3716 3717 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3718 M_NOWAIT | M_ZERO); 3719 if (pool == NULL) 3720 return (ENOMEM); 3721 3722 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3723 pool->ctl_softc = ctl_softc; 3724 pool->zone = uma_zsecond_create(pool->name, NULL, 3725 NULL, NULL, NULL, ctl_softc->io_zone); 3726 /* uma_prealloc(pool->zone, total_ctl_io); */ 3727 3728 *npool = pool; 3729#else 3730 *npool = ctl_softc->io_zone; 3731#endif 3732 return (0); 3733} 3734 3735void 3736ctl_pool_free(struct ctl_io_pool *pool) 3737{ 3738 3739 if (pool == NULL) 3740 return; 3741 3742#ifdef IO_POOLS 3743 uma_zdestroy(pool->zone); 3744 free(pool, M_CTL); 3745#endif 3746} 3747 3748union ctl_io * 3749ctl_alloc_io(void *pool_ref) 3750{ 3751 union ctl_io *io; 3752#ifdef IO_POOLS 3753 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3754 3755 io = uma_zalloc(pool->zone, M_WAITOK); 3756#else 3757 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3758#endif 3759 if (io != NULL) 3760 io->io_hdr.pool = pool_ref; 3761 return (io); 3762} 3763 3764union ctl_io * 3765ctl_alloc_io_nowait(void *pool_ref) 3766{ 3767 union ctl_io *io; 3768#ifdef IO_POOLS 3769 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3770 3771 io = uma_zalloc(pool->zone, M_NOWAIT); 3772#else 3773 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3774#endif 3775 if (io != NULL) 3776 io->io_hdr.pool = pool_ref; 3777 return (io); 3778} 3779 3780void 3781ctl_free_io(union ctl_io *io) 3782{ 3783#ifdef IO_POOLS 3784 struct ctl_io_pool *pool; 3785#endif 3786 3787 if (io == NULL) 3788 return; 3789 3790#ifdef IO_POOLS 3791 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3792 uma_zfree(pool->zone, io); 3793#else 3794 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3795#endif 3796} 3797 3798void 3799ctl_zero_io(union ctl_io *io) 3800{ 3801 void *pool_ref; 3802 3803 if (io == NULL) 3804 return; 3805 3806 /* 3807 * May need to preserve linked list pointers at some point too. 3808 */ 3809 pool_ref = io->io_hdr.pool; 3810 memset(io, 0, sizeof(*io)); 3811 io->io_hdr.pool = pool_ref; 3812} 3813 3814/* 3815 * This routine is currently used for internal copies of ctl_ios that need 3816 * to persist for some reason after we've already returned status to the 3817 * FETD. (Thus the flag set.) 3818 * 3819 * XXX XXX 3820 * Note that this makes a blind copy of all fields in the ctl_io, except 3821 * for the pool reference. This includes any memory that has been 3822 * allocated! That memory will no longer be valid after done has been 3823 * called, so this would be VERY DANGEROUS for command that actually does 3824 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3825 * start and stop commands, which don't transfer any data, so this is not a 3826 * problem. If it is used for anything else, the caller would also need to 3827 * allocate data buffer space and this routine would need to be modified to 3828 * copy the data buffer(s) as well. 3829 */ 3830void 3831ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3832{ 3833 void *pool_ref; 3834 3835 if ((src == NULL) 3836 || (dest == NULL)) 3837 return; 3838 3839 /* 3840 * May need to preserve linked list pointers at some point too. 3841 */ 3842 pool_ref = dest->io_hdr.pool; 3843 3844 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest))); 3845 3846 dest->io_hdr.pool = pool_ref; 3847 /* 3848 * We need to know that this is an internal copy, and doesn't need 3849 * to get passed back to the FETD that allocated it. 3850 */ 3851 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3852} 3853 3854int 3855ctl_expand_number(const char *buf, uint64_t *num) 3856{ 3857 char *endptr; 3858 uint64_t number; 3859 unsigned shift; 3860 3861 number = strtoq(buf, &endptr, 0); 3862 3863 switch (tolower((unsigned char)*endptr)) { 3864 case 'e': 3865 shift = 60; 3866 break; 3867 case 'p': 3868 shift = 50; 3869 break; 3870 case 't': 3871 shift = 40; 3872 break; 3873 case 'g': 3874 shift = 30; 3875 break; 3876 case 'm': 3877 shift = 20; 3878 break; 3879 case 'k': 3880 shift = 10; 3881 break; 3882 case 'b': 3883 case '\0': /* No unit. */ 3884 *num = number; 3885 return (0); 3886 default: 3887 /* Unrecognized unit. */ 3888 return (-1); 3889 } 3890 3891 if ((number << shift) >> shift != number) { 3892 /* Overflow */ 3893 return (-1); 3894 } 3895 *num = number << shift; 3896 return (0); 3897} 3898 3899 3900/* 3901 * This routine could be used in the future to load default and/or saved 3902 * mode page parameters for a particuar lun. 3903 */ 3904static int 3905ctl_init_page_index(struct ctl_lun *lun) 3906{ 3907 int i; 3908 struct ctl_page_index *page_index; 3909 const char *value; 3910 uint64_t ival; 3911 3912 memcpy(&lun->mode_pages.index, page_index_template, 3913 sizeof(page_index_template)); 3914 3915 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3916 3917 page_index = &lun->mode_pages.index[i]; 3918 /* 3919 * If this is a disk-only mode page, there's no point in 3920 * setting it up. For some pages, we have to have some 3921 * basic information about the disk in order to calculate the 3922 * mode page data. 3923 */ 3924 if ((lun->be_lun->lun_type != T_DIRECT) 3925 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3926 continue; 3927 3928 switch (page_index->page_code & SMPH_PC_MASK) { 3929 case SMS_RW_ERROR_RECOVERY_PAGE: { 3930 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3931 panic("subpage is incorrect!"); 3932 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3933 &rw_er_page_default, 3934 sizeof(rw_er_page_default)); 3935 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3936 &rw_er_page_changeable, 3937 sizeof(rw_er_page_changeable)); 3938 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3939 &rw_er_page_default, 3940 sizeof(rw_er_page_default)); 3941 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3942 &rw_er_page_default, 3943 sizeof(rw_er_page_default)); 3944 page_index->page_data = 3945 (uint8_t *)lun->mode_pages.rw_er_page; 3946 break; 3947 } 3948 case SMS_FORMAT_DEVICE_PAGE: { 3949 struct scsi_format_page *format_page; 3950 3951 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3952 panic("subpage is incorrect!"); 3953 3954 /* 3955 * Sectors per track are set above. Bytes per 3956 * sector need to be set here on a per-LUN basis. 3957 */ 3958 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3959 &format_page_default, 3960 sizeof(format_page_default)); 3961 memcpy(&lun->mode_pages.format_page[ 3962 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3963 sizeof(format_page_changeable)); 3964 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3965 &format_page_default, 3966 sizeof(format_page_default)); 3967 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3968 &format_page_default, 3969 sizeof(format_page_default)); 3970 3971 format_page = &lun->mode_pages.format_page[ 3972 CTL_PAGE_CURRENT]; 3973 scsi_ulto2b(lun->be_lun->blocksize, 3974 format_page->bytes_per_sector); 3975 3976 format_page = &lun->mode_pages.format_page[ 3977 CTL_PAGE_DEFAULT]; 3978 scsi_ulto2b(lun->be_lun->blocksize, 3979 format_page->bytes_per_sector); 3980 3981 format_page = &lun->mode_pages.format_page[ 3982 CTL_PAGE_SAVED]; 3983 scsi_ulto2b(lun->be_lun->blocksize, 3984 format_page->bytes_per_sector); 3985 3986 page_index->page_data = 3987 (uint8_t *)lun->mode_pages.format_page; 3988 break; 3989 } 3990 case SMS_RIGID_DISK_PAGE: { 3991 struct scsi_rigid_disk_page *rigid_disk_page; 3992 uint32_t sectors_per_cylinder; 3993 uint64_t cylinders; 3994#ifndef __XSCALE__ 3995 int shift; 3996#endif /* !__XSCALE__ */ 3997 3998 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3999 panic("invalid subpage value %d", 4000 page_index->subpage); 4001 4002 /* 4003 * Rotation rate and sectors per track are set 4004 * above. We calculate the cylinders here based on 4005 * capacity. Due to the number of heads and 4006 * sectors per track we're using, smaller arrays 4007 * may turn out to have 0 cylinders. Linux and 4008 * FreeBSD don't pay attention to these mode pages 4009 * to figure out capacity, but Solaris does. It 4010 * seems to deal with 0 cylinders just fine, and 4011 * works out a fake geometry based on the capacity. 4012 */ 4013 memcpy(&lun->mode_pages.rigid_disk_page[ 4014 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4015 sizeof(rigid_disk_page_default)); 4016 memcpy(&lun->mode_pages.rigid_disk_page[ 4017 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4018 sizeof(rigid_disk_page_changeable)); 4019 4020 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4021 CTL_DEFAULT_HEADS; 4022 4023 /* 4024 * The divide method here will be more accurate, 4025 * probably, but results in floating point being 4026 * used in the kernel on i386 (__udivdi3()). On the 4027 * XScale, though, __udivdi3() is implemented in 4028 * software. 4029 * 4030 * The shift method for cylinder calculation is 4031 * accurate if sectors_per_cylinder is a power of 4032 * 2. Otherwise it might be slightly off -- you 4033 * might have a bit of a truncation problem. 4034 */ 4035#ifdef __XSCALE__ 4036 cylinders = (lun->be_lun->maxlba + 1) / 4037 sectors_per_cylinder; 4038#else 4039 for (shift = 31; shift > 0; shift--) { 4040 if (sectors_per_cylinder & (1 << shift)) 4041 break; 4042 } 4043 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4044#endif 4045 4046 /* 4047 * We've basically got 3 bytes, or 24 bits for the 4048 * cylinder size in the mode page. If we're over, 4049 * just round down to 2^24. 4050 */ 4051 if (cylinders > 0xffffff) 4052 cylinders = 0xffffff; 4053 4054 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4055 CTL_PAGE_DEFAULT]; 4056 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4057 4058 if ((value = ctl_get_opt(&lun->be_lun->options, 4059 "rpm")) != NULL) { 4060 scsi_ulto2b(strtol(value, NULL, 0), 4061 rigid_disk_page->rotation_rate); 4062 } 4063 4064 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4065 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4066 sizeof(rigid_disk_page_default)); 4067 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4068 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4069 sizeof(rigid_disk_page_default)); 4070 4071 page_index->page_data = 4072 (uint8_t *)lun->mode_pages.rigid_disk_page; 4073 break; 4074 } 4075 case SMS_CACHING_PAGE: { 4076 struct scsi_caching_page *caching_page; 4077 4078 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4079 panic("invalid subpage value %d", 4080 page_index->subpage); 4081 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4082 &caching_page_default, 4083 sizeof(caching_page_default)); 4084 memcpy(&lun->mode_pages.caching_page[ 4085 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4086 sizeof(caching_page_changeable)); 4087 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4088 &caching_page_default, 4089 sizeof(caching_page_default)); 4090 caching_page = &lun->mode_pages.caching_page[ 4091 CTL_PAGE_SAVED]; 4092 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4093 if (value != NULL && strcmp(value, "off") == 0) 4094 caching_page->flags1 &= ~SCP_WCE; 4095 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4096 if (value != NULL && strcmp(value, "off") == 0) 4097 caching_page->flags1 |= SCP_RCD; 4098 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4099 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4100 sizeof(caching_page_default)); 4101 page_index->page_data = 4102 (uint8_t *)lun->mode_pages.caching_page; 4103 break; 4104 } 4105 case SMS_CONTROL_MODE_PAGE: { 4106 switch (page_index->subpage) { 4107 case SMS_SUBPAGE_PAGE_0: { 4108 struct scsi_control_page *control_page; 4109 4110 memcpy(&lun->mode_pages.control_page[ 4111 CTL_PAGE_DEFAULT], 4112 &control_page_default, 4113 sizeof(control_page_default)); 4114 memcpy(&lun->mode_pages.control_page[ 4115 CTL_PAGE_CHANGEABLE], 4116 &control_page_changeable, 4117 sizeof(control_page_changeable)); 4118 memcpy(&lun->mode_pages.control_page[ 4119 CTL_PAGE_SAVED], 4120 &control_page_default, 4121 sizeof(control_page_default)); 4122 control_page = &lun->mode_pages.control_page[ 4123 CTL_PAGE_SAVED]; 4124 value = ctl_get_opt(&lun->be_lun->options, 4125 "reordering"); 4126 if (value != NULL && 4127 strcmp(value, "unrestricted") == 0) { 4128 control_page->queue_flags &= 4129 ~SCP_QUEUE_ALG_MASK; 4130 control_page->queue_flags |= 4131 SCP_QUEUE_ALG_UNRESTRICTED; 4132 } 4133 memcpy(&lun->mode_pages.control_page[ 4134 CTL_PAGE_CURRENT], 4135 &lun->mode_pages.control_page[ 4136 CTL_PAGE_SAVED], 4137 sizeof(control_page_default)); 4138 page_index->page_data = 4139 (uint8_t *)lun->mode_pages.control_page; 4140 break; 4141 } 4142 case 0x01: 4143 memcpy(&lun->mode_pages.control_ext_page[ 4144 CTL_PAGE_DEFAULT], 4145 &control_ext_page_default, 4146 sizeof(control_ext_page_default)); 4147 memcpy(&lun->mode_pages.control_ext_page[ 4148 CTL_PAGE_CHANGEABLE], 4149 &control_ext_page_changeable, 4150 sizeof(control_ext_page_changeable)); 4151 memcpy(&lun->mode_pages.control_ext_page[ 4152 CTL_PAGE_SAVED], 4153 &control_ext_page_default, 4154 sizeof(control_ext_page_default)); 4155 memcpy(&lun->mode_pages.control_ext_page[ 4156 CTL_PAGE_CURRENT], 4157 &lun->mode_pages.control_ext_page[ 4158 CTL_PAGE_SAVED], 4159 sizeof(control_ext_page_default)); 4160 page_index->page_data = 4161 (uint8_t *)lun->mode_pages.control_ext_page; 4162 break; 4163 } 4164 break; 4165 } 4166 case SMS_INFO_EXCEPTIONS_PAGE: { 4167 switch (page_index->subpage) { 4168 case SMS_SUBPAGE_PAGE_0: 4169 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4170 &ie_page_default, 4171 sizeof(ie_page_default)); 4172 memcpy(&lun->mode_pages.ie_page[ 4173 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4174 sizeof(ie_page_changeable)); 4175 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4176 &ie_page_default, 4177 sizeof(ie_page_default)); 4178 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4179 &ie_page_default, 4180 sizeof(ie_page_default)); 4181 page_index->page_data = 4182 (uint8_t *)lun->mode_pages.ie_page; 4183 break; 4184 case 0x02: { 4185 struct ctl_logical_block_provisioning_page *page; 4186 4187 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4188 &lbp_page_default, 4189 sizeof(lbp_page_default)); 4190 memcpy(&lun->mode_pages.lbp_page[ 4191 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4192 sizeof(lbp_page_changeable)); 4193 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4194 &lbp_page_default, 4195 sizeof(lbp_page_default)); 4196 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4197 value = ctl_get_opt(&lun->be_lun->options, 4198 "avail-threshold"); 4199 if (value != NULL && 4200 ctl_expand_number(value, &ival) == 0) { 4201 page->descr[0].flags |= SLBPPD_ENABLED | 4202 SLBPPD_ARMING_DEC; 4203 if (lun->be_lun->blocksize) 4204 ival /= lun->be_lun->blocksize; 4205 else 4206 ival /= 512; 4207 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4208 page->descr[0].count); 4209 } 4210 value = ctl_get_opt(&lun->be_lun->options, 4211 "used-threshold"); 4212 if (value != NULL && 4213 ctl_expand_number(value, &ival) == 0) { 4214 page->descr[1].flags |= SLBPPD_ENABLED | 4215 SLBPPD_ARMING_INC; 4216 if (lun->be_lun->blocksize) 4217 ival /= lun->be_lun->blocksize; 4218 else 4219 ival /= 512; 4220 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4221 page->descr[1].count); 4222 } 4223 value = ctl_get_opt(&lun->be_lun->options, 4224 "pool-avail-threshold"); 4225 if (value != NULL && 4226 ctl_expand_number(value, &ival) == 0) { 4227 page->descr[2].flags |= SLBPPD_ENABLED | 4228 SLBPPD_ARMING_DEC; 4229 if (lun->be_lun->blocksize) 4230 ival /= lun->be_lun->blocksize; 4231 else 4232 ival /= 512; 4233 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4234 page->descr[2].count); 4235 } 4236 value = ctl_get_opt(&lun->be_lun->options, 4237 "pool-used-threshold"); 4238 if (value != NULL && 4239 ctl_expand_number(value, &ival) == 0) { 4240 page->descr[3].flags |= SLBPPD_ENABLED | 4241 SLBPPD_ARMING_INC; 4242 if (lun->be_lun->blocksize) 4243 ival /= lun->be_lun->blocksize; 4244 else 4245 ival /= 512; 4246 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4247 page->descr[3].count); 4248 } 4249 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4250 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4251 sizeof(lbp_page_default)); 4252 page_index->page_data = 4253 (uint8_t *)lun->mode_pages.lbp_page; 4254 }} 4255 break; 4256 } 4257 case SMS_VENDOR_SPECIFIC_PAGE:{ 4258 switch (page_index->subpage) { 4259 case DBGCNF_SUBPAGE_CODE: { 4260 struct copan_debugconf_subpage *current_page, 4261 *saved_page; 4262 4263 memcpy(&lun->mode_pages.debugconf_subpage[ 4264 CTL_PAGE_CURRENT], 4265 &debugconf_page_default, 4266 sizeof(debugconf_page_default)); 4267 memcpy(&lun->mode_pages.debugconf_subpage[ 4268 CTL_PAGE_CHANGEABLE], 4269 &debugconf_page_changeable, 4270 sizeof(debugconf_page_changeable)); 4271 memcpy(&lun->mode_pages.debugconf_subpage[ 4272 CTL_PAGE_DEFAULT], 4273 &debugconf_page_default, 4274 sizeof(debugconf_page_default)); 4275 memcpy(&lun->mode_pages.debugconf_subpage[ 4276 CTL_PAGE_SAVED], 4277 &debugconf_page_default, 4278 sizeof(debugconf_page_default)); 4279 page_index->page_data = 4280 (uint8_t *)lun->mode_pages.debugconf_subpage; 4281 4282 current_page = (struct copan_debugconf_subpage *) 4283 (page_index->page_data + 4284 (page_index->page_len * 4285 CTL_PAGE_CURRENT)); 4286 saved_page = (struct copan_debugconf_subpage *) 4287 (page_index->page_data + 4288 (page_index->page_len * 4289 CTL_PAGE_SAVED)); 4290 break; 4291 } 4292 default: 4293 panic("invalid subpage value %d", 4294 page_index->subpage); 4295 break; 4296 } 4297 break; 4298 } 4299 default: 4300 panic("invalid page value %d", 4301 page_index->page_code & SMPH_PC_MASK); 4302 break; 4303 } 4304 } 4305 4306 return (CTL_RETVAL_COMPLETE); 4307} 4308 4309static int 4310ctl_init_log_page_index(struct ctl_lun *lun) 4311{ 4312 struct ctl_page_index *page_index; 4313 int i, j, k, prev; 4314 4315 memcpy(&lun->log_pages.index, log_page_index_template, 4316 sizeof(log_page_index_template)); 4317 4318 prev = -1; 4319 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4320 4321 page_index = &lun->log_pages.index[i]; 4322 /* 4323 * If this is a disk-only mode page, there's no point in 4324 * setting it up. For some pages, we have to have some 4325 * basic information about the disk in order to calculate the 4326 * mode page data. 4327 */ 4328 if ((lun->be_lun->lun_type != T_DIRECT) 4329 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4330 continue; 4331 4332 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4333 lun->backend->lun_attr == NULL) 4334 continue; 4335 4336 if (page_index->page_code != prev) { 4337 lun->log_pages.pages_page[j] = page_index->page_code; 4338 prev = page_index->page_code; 4339 j++; 4340 } 4341 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4342 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4343 k++; 4344 } 4345 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4346 lun->log_pages.index[0].page_len = j; 4347 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4348 lun->log_pages.index[1].page_len = k * 2; 4349 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4350 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4351 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4352 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4353 4354 return (CTL_RETVAL_COMPLETE); 4355} 4356 4357static int 4358hex2bin(const char *str, uint8_t *buf, int buf_size) 4359{ 4360 int i; 4361 u_char c; 4362 4363 memset(buf, 0, buf_size); 4364 while (isspace(str[0])) 4365 str++; 4366 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4367 str += 2; 4368 buf_size *= 2; 4369 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4370 c = str[i]; 4371 if (isdigit(c)) 4372 c -= '0'; 4373 else if (isalpha(c)) 4374 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4375 else 4376 break; 4377 if (c >= 16) 4378 break; 4379 if ((i & 1) == 0) 4380 buf[i / 2] |= (c << 4); 4381 else 4382 buf[i / 2] |= c; 4383 } 4384 return ((i + 1) / 2); 4385} 4386 4387/* 4388 * LUN allocation. 4389 * 4390 * Requirements: 4391 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4392 * wants us to allocate the LUN and he can block. 4393 * - ctl_softc is always set 4394 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4395 * 4396 * Returns 0 for success, non-zero (errno) for failure. 4397 */ 4398static int 4399ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4400 struct ctl_be_lun *const be_lun) 4401{ 4402 struct ctl_lun *nlun, *lun; 4403 struct scsi_vpd_id_descriptor *desc; 4404 struct scsi_vpd_id_t10 *t10id; 4405 const char *eui, *naa, *scsiname, *vendor; 4406 int lun_number, i, lun_malloced; 4407 int devidlen, idlen1, idlen2 = 0, len; 4408 4409 if (be_lun == NULL) 4410 return (EINVAL); 4411 4412 /* 4413 * We currently only support Direct Access or Processor LUN types. 4414 */ 4415 switch (be_lun->lun_type) { 4416 case T_DIRECT: 4417 break; 4418 case T_PROCESSOR: 4419 break; 4420 case T_SEQUENTIAL: 4421 case T_CHANGER: 4422 default: 4423 be_lun->lun_config_status(be_lun->be_lun, 4424 CTL_LUN_CONFIG_FAILURE); 4425 break; 4426 } 4427 if (ctl_lun == NULL) { 4428 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4429 lun_malloced = 1; 4430 } else { 4431 lun_malloced = 0; 4432 lun = ctl_lun; 4433 } 4434 4435 memset(lun, 0, sizeof(*lun)); 4436 if (lun_malloced) 4437 lun->flags = CTL_LUN_MALLOCED; 4438 4439 /* Generate LUN ID. */ 4440 devidlen = max(CTL_DEVID_MIN_LEN, 4441 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4442 idlen1 = sizeof(*t10id) + devidlen; 4443 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4444 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4445 if (scsiname != NULL) { 4446 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4447 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4448 } 4449 eui = ctl_get_opt(&be_lun->options, "eui"); 4450 if (eui != NULL) { 4451 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4452 } 4453 naa = ctl_get_opt(&be_lun->options, "naa"); 4454 if (naa != NULL) { 4455 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4456 } 4457 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4458 M_CTL, M_WAITOK | M_ZERO); 4459 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4460 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4461 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4462 desc->length = idlen1; 4463 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4464 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4465 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4466 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4467 } else { 4468 strncpy(t10id->vendor, vendor, 4469 min(sizeof(t10id->vendor), strlen(vendor))); 4470 } 4471 strncpy((char *)t10id->vendor_spec_id, 4472 (char *)be_lun->device_id, devidlen); 4473 if (scsiname != NULL) { 4474 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4475 desc->length); 4476 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4477 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4478 SVPD_ID_TYPE_SCSI_NAME; 4479 desc->length = idlen2; 4480 strlcpy(desc->identifier, scsiname, idlen2); 4481 } 4482 if (eui != NULL) { 4483 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4484 desc->length); 4485 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4486 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4487 SVPD_ID_TYPE_EUI64; 4488 desc->length = hex2bin(eui, desc->identifier, 16); 4489 desc->length = desc->length > 12 ? 16 : 4490 (desc->length > 8 ? 12 : 8); 4491 len -= 16 - desc->length; 4492 } 4493 if (naa != NULL) { 4494 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4495 desc->length); 4496 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4497 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4498 SVPD_ID_TYPE_NAA; 4499 desc->length = hex2bin(naa, desc->identifier, 16); 4500 desc->length = desc->length > 8 ? 16 : 8; 4501 len -= 16 - desc->length; 4502 } 4503 lun->lun_devid->len = len; 4504 4505 mtx_lock(&ctl_softc->ctl_lock); 4506 /* 4507 * See if the caller requested a particular LUN number. If so, see 4508 * if it is available. Otherwise, allocate the first available LUN. 4509 */ 4510 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4511 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4512 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4513 mtx_unlock(&ctl_softc->ctl_lock); 4514 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4515 printf("ctl: requested LUN ID %d is higher " 4516 "than CTL_MAX_LUNS - 1 (%d)\n", 4517 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4518 } else { 4519 /* 4520 * XXX KDM return an error, or just assign 4521 * another LUN ID in this case?? 4522 */ 4523 printf("ctl: requested LUN ID %d is already " 4524 "in use\n", be_lun->req_lun_id); 4525 } 4526 if (lun->flags & CTL_LUN_MALLOCED) 4527 free(lun, M_CTL); 4528 be_lun->lun_config_status(be_lun->be_lun, 4529 CTL_LUN_CONFIG_FAILURE); 4530 return (ENOSPC); 4531 } 4532 lun_number = be_lun->req_lun_id; 4533 } else { 4534 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4535 if (lun_number == -1) { 4536 mtx_unlock(&ctl_softc->ctl_lock); 4537 printf("ctl: can't allocate LUN, out of LUNs\n"); 4538 if (lun->flags & CTL_LUN_MALLOCED) 4539 free(lun, M_CTL); 4540 be_lun->lun_config_status(be_lun->be_lun, 4541 CTL_LUN_CONFIG_FAILURE); 4542 return (ENOSPC); 4543 } 4544 } 4545 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4546 4547 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4548 lun->lun = lun_number; 4549 lun->be_lun = be_lun; 4550 /* 4551 * The processor LUN is always enabled. Disk LUNs come on line 4552 * disabled, and must be enabled by the backend. 4553 */ 4554 lun->flags |= CTL_LUN_DISABLED; 4555 lun->backend = be_lun->be; 4556 be_lun->ctl_lun = lun; 4557 be_lun->lun_id = lun_number; 4558 atomic_add_int(&be_lun->be->num_luns, 1); 4559 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4560 lun->flags |= CTL_LUN_OFFLINE; 4561 4562 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4563 lun->flags |= CTL_LUN_STOPPED; 4564 4565 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4566 lun->flags |= CTL_LUN_INOPERABLE; 4567 4568 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4569 lun->flags |= CTL_LUN_PRIMARY_SC; 4570 4571 lun->ctl_softc = ctl_softc; 4572#ifdef CTL_TIME_IO 4573 lun->last_busy = getsbinuptime(); 4574#endif 4575 TAILQ_INIT(&lun->ooa_queue); 4576 TAILQ_INIT(&lun->blocked_queue); 4577 STAILQ_INIT(&lun->error_list); 4578 ctl_tpc_lun_init(lun); 4579 4580 /* 4581 * Initialize the mode and log page index. 4582 */ 4583 ctl_init_page_index(lun); 4584 ctl_init_log_page_index(lun); 4585 4586 /* 4587 * Now, before we insert this lun on the lun list, set the lun 4588 * inventory changed UA for all other luns. 4589 */ 4590 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4591 mtx_lock(&nlun->lun_lock); 4592 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4593 mtx_unlock(&nlun->lun_lock); 4594 } 4595 4596 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4597 4598 ctl_softc->ctl_luns[lun_number] = lun; 4599 4600 ctl_softc->num_luns++; 4601 4602 /* Setup statistics gathering */ 4603 lun->stats.device_type = be_lun->lun_type; 4604 lun->stats.lun_number = lun_number; 4605 if (lun->stats.device_type == T_DIRECT) 4606 lun->stats.blocksize = be_lun->blocksize; 4607 else 4608 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4609 for (i = 0;i < CTL_MAX_PORTS;i++) 4610 lun->stats.ports[i].targ_port = i; 4611 4612 mtx_unlock(&ctl_softc->ctl_lock); 4613 4614 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4615 return (0); 4616} 4617 4618/* 4619 * Delete a LUN. 4620 * Assumptions: 4621 * - LUN has already been marked invalid and any pending I/O has been taken 4622 * care of. 4623 */ 4624static int 4625ctl_free_lun(struct ctl_lun *lun) 4626{ 4627 struct ctl_softc *softc; 4628 struct ctl_lun *nlun; 4629 int i; 4630 4631 softc = lun->ctl_softc; 4632 4633 mtx_assert(&softc->ctl_lock, MA_OWNED); 4634 4635 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4636 4637 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4638 4639 softc->ctl_luns[lun->lun] = NULL; 4640 4641 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4642 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4643 4644 softc->num_luns--; 4645 4646 /* 4647 * Tell the backend to free resources, if this LUN has a backend. 4648 */ 4649 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4650 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4651 4652 ctl_tpc_lun_shutdown(lun); 4653 mtx_destroy(&lun->lun_lock); 4654 free(lun->lun_devid, M_CTL); 4655 for (i = 0; i < CTL_MAX_PORTS; i++) 4656 free(lun->pending_ua[i], M_CTL); 4657 for (i = 0; i < CTL_MAX_PORTS; i++) 4658 free(lun->pr_keys[i], M_CTL); 4659 free(lun->write_buffer, M_CTL); 4660 if (lun->flags & CTL_LUN_MALLOCED) 4661 free(lun, M_CTL); 4662 4663 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4664 mtx_lock(&nlun->lun_lock); 4665 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4666 mtx_unlock(&nlun->lun_lock); 4667 } 4668 4669 return (0); 4670} 4671 4672static void 4673ctl_create_lun(struct ctl_be_lun *be_lun) 4674{ 4675 struct ctl_softc *softc; 4676 4677 softc = control_softc; 4678 4679 /* 4680 * ctl_alloc_lun() should handle all potential failure cases. 4681 */ 4682 ctl_alloc_lun(softc, NULL, be_lun); 4683} 4684 4685int 4686ctl_add_lun(struct ctl_be_lun *be_lun) 4687{ 4688 struct ctl_softc *softc = control_softc; 4689 4690 mtx_lock(&softc->ctl_lock); 4691 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4692 mtx_unlock(&softc->ctl_lock); 4693 wakeup(&softc->pending_lun_queue); 4694 4695 return (0); 4696} 4697 4698int 4699ctl_enable_lun(struct ctl_be_lun *be_lun) 4700{ 4701 struct ctl_softc *softc; 4702 struct ctl_port *port, *nport; 4703 struct ctl_lun *lun; 4704 int retval; 4705 4706 lun = (struct ctl_lun *)be_lun->ctl_lun; 4707 softc = lun->ctl_softc; 4708 4709 mtx_lock(&softc->ctl_lock); 4710 mtx_lock(&lun->lun_lock); 4711 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4712 /* 4713 * eh? Why did we get called if the LUN is already 4714 * enabled? 4715 */ 4716 mtx_unlock(&lun->lun_lock); 4717 mtx_unlock(&softc->ctl_lock); 4718 return (0); 4719 } 4720 lun->flags &= ~CTL_LUN_DISABLED; 4721 mtx_unlock(&lun->lun_lock); 4722 4723 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) { 4724 nport = STAILQ_NEXT(port, links); 4725 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4726 port->lun_map != NULL || port->lun_enable == NULL) 4727 continue; 4728 4729 /* 4730 * Drop the lock while we call the FETD's enable routine. 4731 * This can lead to a callback into CTL (at least in the 4732 * case of the internal initiator frontend. 4733 */ 4734 mtx_unlock(&softc->ctl_lock); 4735 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4736 mtx_lock(&softc->ctl_lock); 4737 if (retval != 0) { 4738 printf("%s: FETD %s port %d returned error " 4739 "%d for lun_enable on lun %jd\n", 4740 __func__, port->port_name, port->targ_port, 4741 retval, (intmax_t)lun->lun); 4742 } 4743 } 4744 4745 mtx_unlock(&softc->ctl_lock); 4746 ctl_isc_announce_lun(lun); 4747 4748 return (0); 4749} 4750 4751int 4752ctl_disable_lun(struct ctl_be_lun *be_lun) 4753{ 4754 struct ctl_softc *softc; 4755 struct ctl_port *port; 4756 struct ctl_lun *lun; 4757 int retval; 4758 4759 lun = (struct ctl_lun *)be_lun->ctl_lun; 4760 softc = lun->ctl_softc; 4761 4762 mtx_lock(&softc->ctl_lock); 4763 mtx_lock(&lun->lun_lock); 4764 if (lun->flags & CTL_LUN_DISABLED) { 4765 mtx_unlock(&lun->lun_lock); 4766 mtx_unlock(&softc->ctl_lock); 4767 return (0); 4768 } 4769 lun->flags |= CTL_LUN_DISABLED; 4770 mtx_unlock(&lun->lun_lock); 4771 4772 STAILQ_FOREACH(port, &softc->port_list, links) { 4773 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4774 port->lun_map != NULL || port->lun_disable == NULL) 4775 continue; 4776 4777 /* 4778 * Drop the lock before we call the frontend's disable 4779 * routine, to avoid lock order reversals. 4780 * 4781 * XXX KDM what happens if the frontend list changes while 4782 * we're traversing it? It's unlikely, but should be handled. 4783 */ 4784 mtx_unlock(&softc->ctl_lock); 4785 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4786 mtx_lock(&softc->ctl_lock); 4787 if (retval != 0) { 4788 printf("%s: FETD %s port %d returned error " 4789 "%d for lun_disable on lun %jd\n", 4790 __func__, port->port_name, port->targ_port, 4791 retval, (intmax_t)lun->lun); 4792 } 4793 } 4794 4795 mtx_unlock(&softc->ctl_lock); 4796 ctl_isc_announce_lun(lun); 4797 4798 return (0); 4799} 4800 4801int 4802ctl_start_lun(struct ctl_be_lun *be_lun) 4803{ 4804 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4805 4806 mtx_lock(&lun->lun_lock); 4807 lun->flags &= ~CTL_LUN_STOPPED; 4808 mtx_unlock(&lun->lun_lock); 4809 return (0); 4810} 4811 4812int 4813ctl_stop_lun(struct ctl_be_lun *be_lun) 4814{ 4815 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4816 4817 mtx_lock(&lun->lun_lock); 4818 lun->flags |= CTL_LUN_STOPPED; 4819 mtx_unlock(&lun->lun_lock); 4820 return (0); 4821} 4822 4823int 4824ctl_lun_offline(struct ctl_be_lun *be_lun) 4825{ 4826 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4827 4828 mtx_lock(&lun->lun_lock); 4829 lun->flags |= CTL_LUN_OFFLINE; 4830 mtx_unlock(&lun->lun_lock); 4831 return (0); 4832} 4833 4834int 4835ctl_lun_online(struct ctl_be_lun *be_lun) 4836{ 4837 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4838 4839 mtx_lock(&lun->lun_lock); 4840 lun->flags &= ~CTL_LUN_OFFLINE; 4841 mtx_unlock(&lun->lun_lock); 4842 return (0); 4843} 4844 4845int 4846ctl_lun_primary(struct ctl_be_lun *be_lun) 4847{ 4848 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4849 4850 mtx_lock(&lun->lun_lock); 4851 lun->flags |= CTL_LUN_PRIMARY_SC; 4852 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4853 mtx_unlock(&lun->lun_lock); 4854 ctl_isc_announce_lun(lun); 4855 return (0); 4856} 4857 4858int 4859ctl_lun_secondary(struct ctl_be_lun *be_lun) 4860{ 4861 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4862 4863 mtx_lock(&lun->lun_lock); 4864 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4865 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4866 mtx_unlock(&lun->lun_lock); 4867 ctl_isc_announce_lun(lun); 4868 return (0); 4869} 4870 4871int 4872ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4873{ 4874 struct ctl_softc *softc; 4875 struct ctl_lun *lun; 4876 4877 lun = (struct ctl_lun *)be_lun->ctl_lun; 4878 softc = lun->ctl_softc; 4879 4880 mtx_lock(&lun->lun_lock); 4881 4882 /* 4883 * The LUN needs to be disabled before it can be marked invalid. 4884 */ 4885 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4886 mtx_unlock(&lun->lun_lock); 4887 return (-1); 4888 } 4889 /* 4890 * Mark the LUN invalid. 4891 */ 4892 lun->flags |= CTL_LUN_INVALID; 4893 4894 /* 4895 * If there is nothing in the OOA queue, go ahead and free the LUN. 4896 * If we have something in the OOA queue, we'll free it when the 4897 * last I/O completes. 4898 */ 4899 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4900 mtx_unlock(&lun->lun_lock); 4901 mtx_lock(&softc->ctl_lock); 4902 ctl_free_lun(lun); 4903 mtx_unlock(&softc->ctl_lock); 4904 } else 4905 mtx_unlock(&lun->lun_lock); 4906 4907 return (0); 4908} 4909 4910int 4911ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4912{ 4913 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4914 4915 mtx_lock(&lun->lun_lock); 4916 lun->flags |= CTL_LUN_INOPERABLE; 4917 mtx_unlock(&lun->lun_lock); 4918 return (0); 4919} 4920 4921int 4922ctl_lun_operable(struct ctl_be_lun *be_lun) 4923{ 4924 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4925 4926 mtx_lock(&lun->lun_lock); 4927 lun->flags &= ~CTL_LUN_INOPERABLE; 4928 mtx_unlock(&lun->lun_lock); 4929 return (0); 4930} 4931 4932void 4933ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4934{ 4935 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4936 union ctl_ha_msg msg; 4937 4938 mtx_lock(&lun->lun_lock); 4939 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED); 4940 mtx_unlock(&lun->lun_lock); 4941 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4942 /* Send msg to other side. */ 4943 bzero(&msg.ua, sizeof(msg.ua)); 4944 msg.hdr.msg_type = CTL_MSG_UA; 4945 msg.hdr.nexus.initid = -1; 4946 msg.hdr.nexus.targ_port = -1; 4947 msg.hdr.nexus.targ_lun = lun->lun; 4948 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4949 msg.ua.ua_all = 1; 4950 msg.ua.ua_set = 1; 4951 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGED; 4952 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4953 M_WAITOK); 4954 } 4955} 4956 4957/* 4958 * Backend "memory move is complete" callback for requests that never 4959 * make it down to say RAIDCore's configuration code. 4960 */ 4961int 4962ctl_config_move_done(union ctl_io *io) 4963{ 4964 int retval; 4965 4966 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4967 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4968 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 4969 4970 if ((io->io_hdr.port_status != 0) && 4971 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4972 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4973 /* 4974 * For hardware error sense keys, the sense key 4975 * specific value is defined to be a retry count, 4976 * but we use it to pass back an internal FETD 4977 * error code. XXX KDM Hopefully the FETD is only 4978 * using 16 bits for an error code, since that's 4979 * all the space we have in the sks field. 4980 */ 4981 ctl_set_internal_failure(&io->scsiio, 4982 /*sks_valid*/ 1, 4983 /*retry_count*/ 4984 io->io_hdr.port_status); 4985 } 4986 4987 if (ctl_debug & CTL_DEBUG_CDB_DATA) 4988 ctl_data_print(io); 4989 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 4990 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4991 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 4992 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4993 /* 4994 * XXX KDM just assuming a single pointer here, and not a 4995 * S/G list. If we start using S/G lists for config data, 4996 * we'll need to know how to clean them up here as well. 4997 */ 4998 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4999 free(io->scsiio.kern_data_ptr, M_CTL); 5000 ctl_done(io); 5001 retval = CTL_RETVAL_COMPLETE; 5002 } else { 5003 /* 5004 * XXX KDM now we need to continue data movement. Some 5005 * options: 5006 * - call ctl_scsiio() again? We don't do this for data 5007 * writes, because for those at least we know ahead of 5008 * time where the write will go and how long it is. For 5009 * config writes, though, that information is largely 5010 * contained within the write itself, thus we need to 5011 * parse out the data again. 5012 * 5013 * - Call some other function once the data is in? 5014 */ 5015 5016 /* 5017 * XXX KDM call ctl_scsiio() again for now, and check flag 5018 * bits to see whether we're allocated or not. 5019 */ 5020 retval = ctl_scsiio(&io->scsiio); 5021 } 5022 return (retval); 5023} 5024 5025/* 5026 * This gets called by a backend driver when it is done with a 5027 * data_submit method. 5028 */ 5029void 5030ctl_data_submit_done(union ctl_io *io) 5031{ 5032 /* 5033 * If the IO_CONT flag is set, we need to call the supplied 5034 * function to continue processing the I/O, instead of completing 5035 * the I/O just yet. 5036 * 5037 * If there is an error, though, we don't want to keep processing. 5038 * Instead, just send status back to the initiator. 5039 */ 5040 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5041 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5042 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5043 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5044 io->scsiio.io_cont(io); 5045 return; 5046 } 5047 ctl_done(io); 5048} 5049 5050/* 5051 * This gets called by a backend driver when it is done with a 5052 * configuration write. 5053 */ 5054void 5055ctl_config_write_done(union ctl_io *io) 5056{ 5057 uint8_t *buf; 5058 5059 /* 5060 * If the IO_CONT flag is set, we need to call the supplied 5061 * function to continue processing the I/O, instead of completing 5062 * the I/O just yet. 5063 * 5064 * If there is an error, though, we don't want to keep processing. 5065 * Instead, just send status back to the initiator. 5066 */ 5067 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5068 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5069 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5070 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5071 io->scsiio.io_cont(io); 5072 return; 5073 } 5074 /* 5075 * Since a configuration write can be done for commands that actually 5076 * have data allocated, like write buffer, and commands that have 5077 * no data, like start/stop unit, we need to check here. 5078 */ 5079 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5080 buf = io->scsiio.kern_data_ptr; 5081 else 5082 buf = NULL; 5083 ctl_done(io); 5084 if (buf) 5085 free(buf, M_CTL); 5086} 5087 5088void 5089ctl_config_read_done(union ctl_io *io) 5090{ 5091 uint8_t *buf; 5092 5093 /* 5094 * If there is some error -- we are done, skip data transfer. 5095 */ 5096 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5097 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5098 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5099 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5100 buf = io->scsiio.kern_data_ptr; 5101 else 5102 buf = NULL; 5103 ctl_done(io); 5104 if (buf) 5105 free(buf, M_CTL); 5106 return; 5107 } 5108 5109 /* 5110 * If the IO_CONT flag is set, we need to call the supplied 5111 * function to continue processing the I/O, instead of completing 5112 * the I/O just yet. 5113 */ 5114 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5115 io->scsiio.io_cont(io); 5116 return; 5117 } 5118 5119 ctl_datamove(io); 5120} 5121 5122/* 5123 * SCSI release command. 5124 */ 5125int 5126ctl_scsi_release(struct ctl_scsiio *ctsio) 5127{ 5128 int length, longid, thirdparty_id, resv_id; 5129 struct ctl_lun *lun; 5130 uint32_t residx; 5131 5132 length = 0; 5133 resv_id = 0; 5134 5135 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5136 5137 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5138 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5139 5140 switch (ctsio->cdb[0]) { 5141 case RELEASE_10: { 5142 struct scsi_release_10 *cdb; 5143 5144 cdb = (struct scsi_release_10 *)ctsio->cdb; 5145 5146 if (cdb->byte2 & SR10_LONGID) 5147 longid = 1; 5148 else 5149 thirdparty_id = cdb->thirdparty_id; 5150 5151 resv_id = cdb->resv_id; 5152 length = scsi_2btoul(cdb->length); 5153 break; 5154 } 5155 } 5156 5157 5158 /* 5159 * XXX KDM right now, we only support LUN reservation. We don't 5160 * support 3rd party reservations, or extent reservations, which 5161 * might actually need the parameter list. If we've gotten this 5162 * far, we've got a LUN reservation. Anything else got kicked out 5163 * above. So, according to SPC, ignore the length. 5164 */ 5165 length = 0; 5166 5167 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5168 && (length > 0)) { 5169 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5170 ctsio->kern_data_len = length; 5171 ctsio->kern_total_len = length; 5172 ctsio->kern_data_resid = 0; 5173 ctsio->kern_rel_offset = 0; 5174 ctsio->kern_sg_entries = 0; 5175 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5176 ctsio->be_move_done = ctl_config_move_done; 5177 ctl_datamove((union ctl_io *)ctsio); 5178 5179 return (CTL_RETVAL_COMPLETE); 5180 } 5181 5182 if (length > 0) 5183 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5184 5185 mtx_lock(&lun->lun_lock); 5186 5187 /* 5188 * According to SPC, it is not an error for an intiator to attempt 5189 * to release a reservation on a LUN that isn't reserved, or that 5190 * is reserved by another initiator. The reservation can only be 5191 * released, though, by the initiator who made it or by one of 5192 * several reset type events. 5193 */ 5194 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5195 lun->flags &= ~CTL_LUN_RESERVED; 5196 5197 mtx_unlock(&lun->lun_lock); 5198 5199 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5200 free(ctsio->kern_data_ptr, M_CTL); 5201 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5202 } 5203 5204 ctl_set_success(ctsio); 5205 ctl_done((union ctl_io *)ctsio); 5206 return (CTL_RETVAL_COMPLETE); 5207} 5208 5209int 5210ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5211{ 5212 int extent, thirdparty, longid; 5213 int resv_id, length; 5214 uint64_t thirdparty_id; 5215 struct ctl_lun *lun; 5216 uint32_t residx; 5217 5218 extent = 0; 5219 thirdparty = 0; 5220 longid = 0; 5221 resv_id = 0; 5222 length = 0; 5223 thirdparty_id = 0; 5224 5225 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5226 5227 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5228 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5229 5230 switch (ctsio->cdb[0]) { 5231 case RESERVE_10: { 5232 struct scsi_reserve_10 *cdb; 5233 5234 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5235 5236 if (cdb->byte2 & SR10_LONGID) 5237 longid = 1; 5238 else 5239 thirdparty_id = cdb->thirdparty_id; 5240 5241 resv_id = cdb->resv_id; 5242 length = scsi_2btoul(cdb->length); 5243 break; 5244 } 5245 } 5246 5247 /* 5248 * XXX KDM right now, we only support LUN reservation. We don't 5249 * support 3rd party reservations, or extent reservations, which 5250 * might actually need the parameter list. If we've gotten this 5251 * far, we've got a LUN reservation. Anything else got kicked out 5252 * above. So, according to SPC, ignore the length. 5253 */ 5254 length = 0; 5255 5256 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5257 && (length > 0)) { 5258 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5259 ctsio->kern_data_len = length; 5260 ctsio->kern_total_len = length; 5261 ctsio->kern_data_resid = 0; 5262 ctsio->kern_rel_offset = 0; 5263 ctsio->kern_sg_entries = 0; 5264 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5265 ctsio->be_move_done = ctl_config_move_done; 5266 ctl_datamove((union ctl_io *)ctsio); 5267 5268 return (CTL_RETVAL_COMPLETE); 5269 } 5270 5271 if (length > 0) 5272 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5273 5274 mtx_lock(&lun->lun_lock); 5275 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5276 ctl_set_reservation_conflict(ctsio); 5277 goto bailout; 5278 } 5279 5280 lun->flags |= CTL_LUN_RESERVED; 5281 lun->res_idx = residx; 5282 5283 ctl_set_success(ctsio); 5284 5285bailout: 5286 mtx_unlock(&lun->lun_lock); 5287 5288 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5289 free(ctsio->kern_data_ptr, M_CTL); 5290 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5291 } 5292 5293 ctl_done((union ctl_io *)ctsio); 5294 return (CTL_RETVAL_COMPLETE); 5295} 5296 5297int 5298ctl_start_stop(struct ctl_scsiio *ctsio) 5299{ 5300 struct scsi_start_stop_unit *cdb; 5301 struct ctl_lun *lun; 5302 int retval; 5303 5304 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5305 5306 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5307 retval = 0; 5308 5309 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5310 5311 /* 5312 * XXX KDM 5313 * We don't support the immediate bit on a stop unit. In order to 5314 * do that, we would need to code up a way to know that a stop is 5315 * pending, and hold off any new commands until it completes, one 5316 * way or another. Then we could accept or reject those commands 5317 * depending on its status. We would almost need to do the reverse 5318 * of what we do below for an immediate start -- return the copy of 5319 * the ctl_io to the FETD with status to send to the host (and to 5320 * free the copy!) and then free the original I/O once the stop 5321 * actually completes. That way, the OOA queue mechanism can work 5322 * to block commands that shouldn't proceed. Another alternative 5323 * would be to put the copy in the queue in place of the original, 5324 * and return the original back to the caller. That could be 5325 * slightly safer.. 5326 */ 5327 if ((cdb->byte2 & SSS_IMMED) 5328 && ((cdb->how & SSS_START) == 0)) { 5329 ctl_set_invalid_field(ctsio, 5330 /*sks_valid*/ 1, 5331 /*command*/ 1, 5332 /*field*/ 1, 5333 /*bit_valid*/ 1, 5334 /*bit*/ 0); 5335 ctl_done((union ctl_io *)ctsio); 5336 return (CTL_RETVAL_COMPLETE); 5337 } 5338 5339 if ((lun->flags & CTL_LUN_PR_RESERVED) 5340 && ((cdb->how & SSS_START)==0)) { 5341 uint32_t residx; 5342 5343 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5344 if (ctl_get_prkey(lun, residx) == 0 5345 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5346 5347 ctl_set_reservation_conflict(ctsio); 5348 ctl_done((union ctl_io *)ctsio); 5349 return (CTL_RETVAL_COMPLETE); 5350 } 5351 } 5352 5353 /* 5354 * If there is no backend on this device, we can't start or stop 5355 * it. In theory we shouldn't get any start/stop commands in the 5356 * first place at this level if the LUN doesn't have a backend. 5357 * That should get stopped by the command decode code. 5358 */ 5359 if (lun->backend == NULL) { 5360 ctl_set_invalid_opcode(ctsio); 5361 ctl_done((union ctl_io *)ctsio); 5362 return (CTL_RETVAL_COMPLETE); 5363 } 5364 5365 /* 5366 * XXX KDM Copan-specific offline behavior. 5367 * Figure out a reasonable way to port this? 5368 */ 5369#ifdef NEEDTOPORT 5370 mtx_lock(&lun->lun_lock); 5371 5372 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5373 && (lun->flags & CTL_LUN_OFFLINE)) { 5374 /* 5375 * If the LUN is offline, and the on/offline bit isn't set, 5376 * reject the start or stop. Otherwise, let it through. 5377 */ 5378 mtx_unlock(&lun->lun_lock); 5379 ctl_set_lun_not_ready(ctsio); 5380 ctl_done((union ctl_io *)ctsio); 5381 } else { 5382 mtx_unlock(&lun->lun_lock); 5383#endif /* NEEDTOPORT */ 5384 /* 5385 * This could be a start or a stop when we're online, 5386 * or a stop/offline or start/online. A start or stop when 5387 * we're offline is covered in the case above. 5388 */ 5389 /* 5390 * In the non-immediate case, we send the request to 5391 * the backend and return status to the user when 5392 * it is done. 5393 * 5394 * In the immediate case, we allocate a new ctl_io 5395 * to hold a copy of the request, and send that to 5396 * the backend. We then set good status on the 5397 * user's request and return it immediately. 5398 */ 5399 if (cdb->byte2 & SSS_IMMED) { 5400 union ctl_io *new_io; 5401 5402 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5403 ctl_copy_io((union ctl_io *)ctsio, new_io); 5404 retval = lun->backend->config_write(new_io); 5405 ctl_set_success(ctsio); 5406 ctl_done((union ctl_io *)ctsio); 5407 } else { 5408 retval = lun->backend->config_write( 5409 (union ctl_io *)ctsio); 5410 } 5411#ifdef NEEDTOPORT 5412 } 5413#endif 5414 return (retval); 5415} 5416 5417/* 5418 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5419 * we don't really do anything with the LBA and length fields if the user 5420 * passes them in. Instead we'll just flush out the cache for the entire 5421 * LUN. 5422 */ 5423int 5424ctl_sync_cache(struct ctl_scsiio *ctsio) 5425{ 5426 struct ctl_lun *lun; 5427 struct ctl_softc *softc; 5428 struct ctl_lba_len_flags *lbalen; 5429 uint64_t starting_lba; 5430 uint32_t block_count; 5431 int retval; 5432 uint8_t byte2; 5433 5434 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5435 5436 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5437 softc = lun->ctl_softc; 5438 retval = 0; 5439 5440 switch (ctsio->cdb[0]) { 5441 case SYNCHRONIZE_CACHE: { 5442 struct scsi_sync_cache *cdb; 5443 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5444 5445 starting_lba = scsi_4btoul(cdb->begin_lba); 5446 block_count = scsi_2btoul(cdb->lb_count); 5447 byte2 = cdb->byte2; 5448 break; 5449 } 5450 case SYNCHRONIZE_CACHE_16: { 5451 struct scsi_sync_cache_16 *cdb; 5452 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5453 5454 starting_lba = scsi_8btou64(cdb->begin_lba); 5455 block_count = scsi_4btoul(cdb->lb_count); 5456 byte2 = cdb->byte2; 5457 break; 5458 } 5459 default: 5460 ctl_set_invalid_opcode(ctsio); 5461 ctl_done((union ctl_io *)ctsio); 5462 goto bailout; 5463 break; /* NOTREACHED */ 5464 } 5465 5466 /* 5467 * We check the LBA and length, but don't do anything with them. 5468 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5469 * get flushed. This check will just help satisfy anyone who wants 5470 * to see an error for an out of range LBA. 5471 */ 5472 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5473 ctl_set_lba_out_of_range(ctsio); 5474 ctl_done((union ctl_io *)ctsio); 5475 goto bailout; 5476 } 5477 5478 /* 5479 * If this LUN has no backend, we can't flush the cache anyway. 5480 */ 5481 if (lun->backend == NULL) { 5482 ctl_set_invalid_opcode(ctsio); 5483 ctl_done((union ctl_io *)ctsio); 5484 goto bailout; 5485 } 5486 5487 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5488 lbalen->lba = starting_lba; 5489 lbalen->len = block_count; 5490 lbalen->flags = byte2; 5491 5492 /* 5493 * Check to see whether we're configured to send the SYNCHRONIZE 5494 * CACHE command directly to the back end. 5495 */ 5496 mtx_lock(&lun->lun_lock); 5497 if ((softc->flags & CTL_FLAG_REAL_SYNC) 5498 && (++(lun->sync_count) >= lun->sync_interval)) { 5499 lun->sync_count = 0; 5500 mtx_unlock(&lun->lun_lock); 5501 retval = lun->backend->config_write((union ctl_io *)ctsio); 5502 } else { 5503 mtx_unlock(&lun->lun_lock); 5504 ctl_set_success(ctsio); 5505 ctl_done((union ctl_io *)ctsio); 5506 } 5507 5508bailout: 5509 5510 return (retval); 5511} 5512 5513int 5514ctl_format(struct ctl_scsiio *ctsio) 5515{ 5516 struct scsi_format *cdb; 5517 struct ctl_lun *lun; 5518 int length, defect_list_len; 5519 5520 CTL_DEBUG_PRINT(("ctl_format\n")); 5521 5522 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5523 5524 cdb = (struct scsi_format *)ctsio->cdb; 5525 5526 length = 0; 5527 if (cdb->byte2 & SF_FMTDATA) { 5528 if (cdb->byte2 & SF_LONGLIST) 5529 length = sizeof(struct scsi_format_header_long); 5530 else 5531 length = sizeof(struct scsi_format_header_short); 5532 } 5533 5534 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5535 && (length > 0)) { 5536 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5537 ctsio->kern_data_len = length; 5538 ctsio->kern_total_len = length; 5539 ctsio->kern_data_resid = 0; 5540 ctsio->kern_rel_offset = 0; 5541 ctsio->kern_sg_entries = 0; 5542 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5543 ctsio->be_move_done = ctl_config_move_done; 5544 ctl_datamove((union ctl_io *)ctsio); 5545 5546 return (CTL_RETVAL_COMPLETE); 5547 } 5548 5549 defect_list_len = 0; 5550 5551 if (cdb->byte2 & SF_FMTDATA) { 5552 if (cdb->byte2 & SF_LONGLIST) { 5553 struct scsi_format_header_long *header; 5554 5555 header = (struct scsi_format_header_long *) 5556 ctsio->kern_data_ptr; 5557 5558 defect_list_len = scsi_4btoul(header->defect_list_len); 5559 if (defect_list_len != 0) { 5560 ctl_set_invalid_field(ctsio, 5561 /*sks_valid*/ 1, 5562 /*command*/ 0, 5563 /*field*/ 2, 5564 /*bit_valid*/ 0, 5565 /*bit*/ 0); 5566 goto bailout; 5567 } 5568 } else { 5569 struct scsi_format_header_short *header; 5570 5571 header = (struct scsi_format_header_short *) 5572 ctsio->kern_data_ptr; 5573 5574 defect_list_len = scsi_2btoul(header->defect_list_len); 5575 if (defect_list_len != 0) { 5576 ctl_set_invalid_field(ctsio, 5577 /*sks_valid*/ 1, 5578 /*command*/ 0, 5579 /*field*/ 2, 5580 /*bit_valid*/ 0, 5581 /*bit*/ 0); 5582 goto bailout; 5583 } 5584 } 5585 } 5586 5587 /* 5588 * The format command will clear out the "Medium format corrupted" 5589 * status if set by the configuration code. That status is really 5590 * just a way to notify the host that we have lost the media, and 5591 * get them to issue a command that will basically make them think 5592 * they're blowing away the media. 5593 */ 5594 mtx_lock(&lun->lun_lock); 5595 lun->flags &= ~CTL_LUN_INOPERABLE; 5596 mtx_unlock(&lun->lun_lock); 5597 5598 ctl_set_success(ctsio); 5599bailout: 5600 5601 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5602 free(ctsio->kern_data_ptr, M_CTL); 5603 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5604 } 5605 5606 ctl_done((union ctl_io *)ctsio); 5607 return (CTL_RETVAL_COMPLETE); 5608} 5609 5610int 5611ctl_read_buffer(struct ctl_scsiio *ctsio) 5612{ 5613 struct scsi_read_buffer *cdb; 5614 struct ctl_lun *lun; 5615 int buffer_offset, len; 5616 static uint8_t descr[4]; 5617 static uint8_t echo_descr[4] = { 0 }; 5618 5619 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5620 5621 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5622 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5623 5624 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5625 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5626 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5627 ctl_set_invalid_field(ctsio, 5628 /*sks_valid*/ 1, 5629 /*command*/ 1, 5630 /*field*/ 1, 5631 /*bit_valid*/ 1, 5632 /*bit*/ 4); 5633 ctl_done((union ctl_io *)ctsio); 5634 return (CTL_RETVAL_COMPLETE); 5635 } 5636 5637 len = scsi_3btoul(cdb->length); 5638 buffer_offset = scsi_3btoul(cdb->offset); 5639 5640 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5641 ctl_set_invalid_field(ctsio, 5642 /*sks_valid*/ 1, 5643 /*command*/ 1, 5644 /*field*/ 6, 5645 /*bit_valid*/ 0, 5646 /*bit*/ 0); 5647 ctl_done((union ctl_io *)ctsio); 5648 return (CTL_RETVAL_COMPLETE); 5649 } 5650 5651 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5652 descr[0] = 0; 5653 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5654 ctsio->kern_data_ptr = descr; 5655 len = min(len, sizeof(descr)); 5656 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5657 ctsio->kern_data_ptr = echo_descr; 5658 len = min(len, sizeof(echo_descr)); 5659 } else { 5660 if (lun->write_buffer == NULL) { 5661 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5662 M_CTL, M_WAITOK); 5663 } 5664 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5665 } 5666 ctsio->kern_data_len = len; 5667 ctsio->kern_total_len = len; 5668 ctsio->kern_data_resid = 0; 5669 ctsio->kern_rel_offset = 0; 5670 ctsio->kern_sg_entries = 0; 5671 ctl_set_success(ctsio); 5672 ctsio->be_move_done = ctl_config_move_done; 5673 ctl_datamove((union ctl_io *)ctsio); 5674 return (CTL_RETVAL_COMPLETE); 5675} 5676 5677int 5678ctl_write_buffer(struct ctl_scsiio *ctsio) 5679{ 5680 struct scsi_write_buffer *cdb; 5681 struct ctl_lun *lun; 5682 int buffer_offset, len; 5683 5684 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5685 5686 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5687 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5688 5689 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5690 ctl_set_invalid_field(ctsio, 5691 /*sks_valid*/ 1, 5692 /*command*/ 1, 5693 /*field*/ 1, 5694 /*bit_valid*/ 1, 5695 /*bit*/ 4); 5696 ctl_done((union ctl_io *)ctsio); 5697 return (CTL_RETVAL_COMPLETE); 5698 } 5699 5700 len = scsi_3btoul(cdb->length); 5701 buffer_offset = scsi_3btoul(cdb->offset); 5702 5703 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5704 ctl_set_invalid_field(ctsio, 5705 /*sks_valid*/ 1, 5706 /*command*/ 1, 5707 /*field*/ 6, 5708 /*bit_valid*/ 0, 5709 /*bit*/ 0); 5710 ctl_done((union ctl_io *)ctsio); 5711 return (CTL_RETVAL_COMPLETE); 5712 } 5713 5714 /* 5715 * If we've got a kernel request that hasn't been malloced yet, 5716 * malloc it and tell the caller the data buffer is here. 5717 */ 5718 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5719 if (lun->write_buffer == NULL) { 5720 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5721 M_CTL, M_WAITOK); 5722 } 5723 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5724 ctsio->kern_data_len = len; 5725 ctsio->kern_total_len = len; 5726 ctsio->kern_data_resid = 0; 5727 ctsio->kern_rel_offset = 0; 5728 ctsio->kern_sg_entries = 0; 5729 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5730 ctsio->be_move_done = ctl_config_move_done; 5731 ctl_datamove((union ctl_io *)ctsio); 5732 5733 return (CTL_RETVAL_COMPLETE); 5734 } 5735 5736 ctl_set_success(ctsio); 5737 ctl_done((union ctl_io *)ctsio); 5738 return (CTL_RETVAL_COMPLETE); 5739} 5740 5741int 5742ctl_write_same(struct ctl_scsiio *ctsio) 5743{ 5744 struct ctl_lun *lun; 5745 struct ctl_lba_len_flags *lbalen; 5746 uint64_t lba; 5747 uint32_t num_blocks; 5748 int len, retval; 5749 uint8_t byte2; 5750 5751 retval = CTL_RETVAL_COMPLETE; 5752 5753 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5754 5755 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5756 5757 switch (ctsio->cdb[0]) { 5758 case WRITE_SAME_10: { 5759 struct scsi_write_same_10 *cdb; 5760 5761 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5762 5763 lba = scsi_4btoul(cdb->addr); 5764 num_blocks = scsi_2btoul(cdb->length); 5765 byte2 = cdb->byte2; 5766 break; 5767 } 5768 case WRITE_SAME_16: { 5769 struct scsi_write_same_16 *cdb; 5770 5771 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5772 5773 lba = scsi_8btou64(cdb->addr); 5774 num_blocks = scsi_4btoul(cdb->length); 5775 byte2 = cdb->byte2; 5776 break; 5777 } 5778 default: 5779 /* 5780 * We got a command we don't support. This shouldn't 5781 * happen, commands should be filtered out above us. 5782 */ 5783 ctl_set_invalid_opcode(ctsio); 5784 ctl_done((union ctl_io *)ctsio); 5785 5786 return (CTL_RETVAL_COMPLETE); 5787 break; /* NOTREACHED */ 5788 } 5789 5790 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5791 if ((byte2 & SWS_UNMAP) == 0 && 5792 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5793 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5794 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5795 ctl_done((union ctl_io *)ctsio); 5796 return (CTL_RETVAL_COMPLETE); 5797 } 5798 5799 /* 5800 * The first check is to make sure we're in bounds, the second 5801 * check is to catch wrap-around problems. If the lba + num blocks 5802 * is less than the lba, then we've wrapped around and the block 5803 * range is invalid anyway. 5804 */ 5805 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5806 || ((lba + num_blocks) < lba)) { 5807 ctl_set_lba_out_of_range(ctsio); 5808 ctl_done((union ctl_io *)ctsio); 5809 return (CTL_RETVAL_COMPLETE); 5810 } 5811 5812 /* Zero number of blocks means "to the last logical block" */ 5813 if (num_blocks == 0) { 5814 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5815 ctl_set_invalid_field(ctsio, 5816 /*sks_valid*/ 0, 5817 /*command*/ 1, 5818 /*field*/ 0, 5819 /*bit_valid*/ 0, 5820 /*bit*/ 0); 5821 ctl_done((union ctl_io *)ctsio); 5822 return (CTL_RETVAL_COMPLETE); 5823 } 5824 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5825 } 5826 5827 len = lun->be_lun->blocksize; 5828 5829 /* 5830 * If we've got a kernel request that hasn't been malloced yet, 5831 * malloc it and tell the caller the data buffer is here. 5832 */ 5833 if ((byte2 & SWS_NDOB) == 0 && 5834 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5835 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5836 ctsio->kern_data_len = len; 5837 ctsio->kern_total_len = len; 5838 ctsio->kern_data_resid = 0; 5839 ctsio->kern_rel_offset = 0; 5840 ctsio->kern_sg_entries = 0; 5841 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5842 ctsio->be_move_done = ctl_config_move_done; 5843 ctl_datamove((union ctl_io *)ctsio); 5844 5845 return (CTL_RETVAL_COMPLETE); 5846 } 5847 5848 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5849 lbalen->lba = lba; 5850 lbalen->len = num_blocks; 5851 lbalen->flags = byte2; 5852 retval = lun->backend->config_write((union ctl_io *)ctsio); 5853 5854 return (retval); 5855} 5856 5857int 5858ctl_unmap(struct ctl_scsiio *ctsio) 5859{ 5860 struct ctl_lun *lun; 5861 struct scsi_unmap *cdb; 5862 struct ctl_ptr_len_flags *ptrlen; 5863 struct scsi_unmap_header *hdr; 5864 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5865 uint64_t lba; 5866 uint32_t num_blocks; 5867 int len, retval; 5868 uint8_t byte2; 5869 5870 retval = CTL_RETVAL_COMPLETE; 5871 5872 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5873 5874 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5875 cdb = (struct scsi_unmap *)ctsio->cdb; 5876 5877 len = scsi_2btoul(cdb->length); 5878 byte2 = cdb->byte2; 5879 5880 /* 5881 * If we've got a kernel request that hasn't been malloced yet, 5882 * malloc it and tell the caller the data buffer is here. 5883 */ 5884 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5885 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5886 ctsio->kern_data_len = len; 5887 ctsio->kern_total_len = len; 5888 ctsio->kern_data_resid = 0; 5889 ctsio->kern_rel_offset = 0; 5890 ctsio->kern_sg_entries = 0; 5891 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5892 ctsio->be_move_done = ctl_config_move_done; 5893 ctl_datamove((union ctl_io *)ctsio); 5894 5895 return (CTL_RETVAL_COMPLETE); 5896 } 5897 5898 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5899 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5900 if (len < sizeof (*hdr) || 5901 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5902 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5903 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5904 ctl_set_invalid_field(ctsio, 5905 /*sks_valid*/ 0, 5906 /*command*/ 0, 5907 /*field*/ 0, 5908 /*bit_valid*/ 0, 5909 /*bit*/ 0); 5910 goto done; 5911 } 5912 len = scsi_2btoul(hdr->desc_length); 5913 buf = (struct scsi_unmap_desc *)(hdr + 1); 5914 end = buf + len / sizeof(*buf); 5915 5916 endnz = buf; 5917 for (range = buf; range < end; range++) { 5918 lba = scsi_8btou64(range->lba); 5919 num_blocks = scsi_4btoul(range->length); 5920 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5921 || ((lba + num_blocks) < lba)) { 5922 ctl_set_lba_out_of_range(ctsio); 5923 ctl_done((union ctl_io *)ctsio); 5924 return (CTL_RETVAL_COMPLETE); 5925 } 5926 if (num_blocks != 0) 5927 endnz = range + 1; 5928 } 5929 5930 /* 5931 * Block backend can not handle zero last range. 5932 * Filter it out and return if there is nothing left. 5933 */ 5934 len = (uint8_t *)endnz - (uint8_t *)buf; 5935 if (len == 0) { 5936 ctl_set_success(ctsio); 5937 goto done; 5938 } 5939 5940 mtx_lock(&lun->lun_lock); 5941 ptrlen = (struct ctl_ptr_len_flags *) 5942 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5943 ptrlen->ptr = (void *)buf; 5944 ptrlen->len = len; 5945 ptrlen->flags = byte2; 5946 ctl_check_blocked(lun); 5947 mtx_unlock(&lun->lun_lock); 5948 5949 retval = lun->backend->config_write((union ctl_io *)ctsio); 5950 return (retval); 5951 5952done: 5953 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5954 free(ctsio->kern_data_ptr, M_CTL); 5955 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5956 } 5957 ctl_done((union ctl_io *)ctsio); 5958 return (CTL_RETVAL_COMPLETE); 5959} 5960 5961/* 5962 * Note that this function currently doesn't actually do anything inside 5963 * CTL to enforce things if the DQue bit is turned on. 5964 * 5965 * Also note that this function can't be used in the default case, because 5966 * the DQue bit isn't set in the changeable mask for the control mode page 5967 * anyway. This is just here as an example for how to implement a page 5968 * handler, and a placeholder in case we want to allow the user to turn 5969 * tagged queueing on and off. 5970 * 5971 * The D_SENSE bit handling is functional, however, and will turn 5972 * descriptor sense on and off for a given LUN. 5973 */ 5974int 5975ctl_control_page_handler(struct ctl_scsiio *ctsio, 5976 struct ctl_page_index *page_index, uint8_t *page_ptr) 5977{ 5978 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5979 struct ctl_lun *lun; 5980 int set_ua; 5981 uint32_t initidx; 5982 5983 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5984 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5985 set_ua = 0; 5986 5987 user_cp = (struct scsi_control_page *)page_ptr; 5988 current_cp = (struct scsi_control_page *) 5989 (page_index->page_data + (page_index->page_len * 5990 CTL_PAGE_CURRENT)); 5991 saved_cp = (struct scsi_control_page *) 5992 (page_index->page_data + (page_index->page_len * 5993 CTL_PAGE_SAVED)); 5994 5995 mtx_lock(&lun->lun_lock); 5996 if (((current_cp->rlec & SCP_DSENSE) == 0) 5997 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5998 /* 5999 * Descriptor sense is currently turned off and the user 6000 * wants to turn it on. 6001 */ 6002 current_cp->rlec |= SCP_DSENSE; 6003 saved_cp->rlec |= SCP_DSENSE; 6004 lun->flags |= CTL_LUN_SENSE_DESC; 6005 set_ua = 1; 6006 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6007 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6008 /* 6009 * Descriptor sense is currently turned on, and the user 6010 * wants to turn it off. 6011 */ 6012 current_cp->rlec &= ~SCP_DSENSE; 6013 saved_cp->rlec &= ~SCP_DSENSE; 6014 lun->flags &= ~CTL_LUN_SENSE_DESC; 6015 set_ua = 1; 6016 } 6017 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6018 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6019 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6020 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6021 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6022 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6023 set_ua = 1; 6024 } 6025 if ((current_cp->eca_and_aen & SCP_SWP) != 6026 (user_cp->eca_and_aen & SCP_SWP)) { 6027 current_cp->eca_and_aen &= ~SCP_SWP; 6028 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6029 saved_cp->eca_and_aen &= ~SCP_SWP; 6030 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6031 set_ua = 1; 6032 } 6033 if (set_ua != 0) 6034 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6035 mtx_unlock(&lun->lun_lock); 6036 if (set_ua) { 6037 ctl_isc_announce_mode(lun, 6038 ctl_get_initindex(&ctsio->io_hdr.nexus), 6039 page_index->page_code, page_index->subpage); 6040 } 6041 return (0); 6042} 6043 6044int 6045ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6046 struct ctl_page_index *page_index, uint8_t *page_ptr) 6047{ 6048 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6049 struct ctl_lun *lun; 6050 int set_ua; 6051 uint32_t initidx; 6052 6053 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6054 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6055 set_ua = 0; 6056 6057 user_cp = (struct scsi_caching_page *)page_ptr; 6058 current_cp = (struct scsi_caching_page *) 6059 (page_index->page_data + (page_index->page_len * 6060 CTL_PAGE_CURRENT)); 6061 saved_cp = (struct scsi_caching_page *) 6062 (page_index->page_data + (page_index->page_len * 6063 CTL_PAGE_SAVED)); 6064 6065 mtx_lock(&lun->lun_lock); 6066 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6067 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6068 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6069 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6070 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6071 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6072 set_ua = 1; 6073 } 6074 if (set_ua != 0) 6075 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6076 mtx_unlock(&lun->lun_lock); 6077 if (set_ua) { 6078 ctl_isc_announce_mode(lun, 6079 ctl_get_initindex(&ctsio->io_hdr.nexus), 6080 page_index->page_code, page_index->subpage); 6081 } 6082 return (0); 6083} 6084 6085int 6086ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6087 struct ctl_page_index *page_index, 6088 uint8_t *page_ptr) 6089{ 6090 uint8_t *c; 6091 int i; 6092 6093 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6094 ctl_time_io_secs = 6095 (c[0] << 8) | 6096 (c[1] << 0) | 6097 0; 6098 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6099 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6100 printf("page data:"); 6101 for (i=0; i<8; i++) 6102 printf(" %.2x",page_ptr[i]); 6103 printf("\n"); 6104 return (0); 6105} 6106 6107int 6108ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6109 struct ctl_page_index *page_index, 6110 int pc) 6111{ 6112 struct copan_debugconf_subpage *page; 6113 6114 page = (struct copan_debugconf_subpage *)page_index->page_data + 6115 (page_index->page_len * pc); 6116 6117 switch (pc) { 6118 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6119 case SMS_PAGE_CTRL_DEFAULT >> 6: 6120 case SMS_PAGE_CTRL_SAVED >> 6: 6121 /* 6122 * We don't update the changable or default bits for this page. 6123 */ 6124 break; 6125 case SMS_PAGE_CTRL_CURRENT >> 6: 6126 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6127 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6128 break; 6129 default: 6130#ifdef NEEDTOPORT 6131 EPRINT(0, "Invalid PC %d!!", pc); 6132#endif /* NEEDTOPORT */ 6133 break; 6134 } 6135 return (0); 6136} 6137 6138 6139static int 6140ctl_do_mode_select(union ctl_io *io) 6141{ 6142 struct scsi_mode_page_header *page_header; 6143 struct ctl_page_index *page_index; 6144 struct ctl_scsiio *ctsio; 6145 int control_dev, page_len; 6146 int page_len_offset, page_len_size; 6147 union ctl_modepage_info *modepage_info; 6148 struct ctl_lun *lun; 6149 int *len_left, *len_used; 6150 int retval, i; 6151 6152 ctsio = &io->scsiio; 6153 page_index = NULL; 6154 page_len = 0; 6155 retval = CTL_RETVAL_COMPLETE; 6156 6157 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6158 6159 if (lun->be_lun->lun_type != T_DIRECT) 6160 control_dev = 1; 6161 else 6162 control_dev = 0; 6163 6164 modepage_info = (union ctl_modepage_info *) 6165 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6166 len_left = &modepage_info->header.len_left; 6167 len_used = &modepage_info->header.len_used; 6168 6169do_next_page: 6170 6171 page_header = (struct scsi_mode_page_header *) 6172 (ctsio->kern_data_ptr + *len_used); 6173 6174 if (*len_left == 0) { 6175 free(ctsio->kern_data_ptr, M_CTL); 6176 ctl_set_success(ctsio); 6177 ctl_done((union ctl_io *)ctsio); 6178 return (CTL_RETVAL_COMPLETE); 6179 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6180 6181 free(ctsio->kern_data_ptr, M_CTL); 6182 ctl_set_param_len_error(ctsio); 6183 ctl_done((union ctl_io *)ctsio); 6184 return (CTL_RETVAL_COMPLETE); 6185 6186 } else if ((page_header->page_code & SMPH_SPF) 6187 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6188 6189 free(ctsio->kern_data_ptr, M_CTL); 6190 ctl_set_param_len_error(ctsio); 6191 ctl_done((union ctl_io *)ctsio); 6192 return (CTL_RETVAL_COMPLETE); 6193 } 6194 6195 6196 /* 6197 * XXX KDM should we do something with the block descriptor? 6198 */ 6199 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6200 6201 if ((control_dev != 0) 6202 && (lun->mode_pages.index[i].page_flags & 6203 CTL_PAGE_FLAG_DISK_ONLY)) 6204 continue; 6205 6206 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6207 (page_header->page_code & SMPH_PC_MASK)) 6208 continue; 6209 6210 /* 6211 * If neither page has a subpage code, then we've got a 6212 * match. 6213 */ 6214 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6215 && ((page_header->page_code & SMPH_SPF) == 0)) { 6216 page_index = &lun->mode_pages.index[i]; 6217 page_len = page_header->page_length; 6218 break; 6219 } 6220 6221 /* 6222 * If both pages have subpages, then the subpage numbers 6223 * have to match. 6224 */ 6225 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6226 && (page_header->page_code & SMPH_SPF)) { 6227 struct scsi_mode_page_header_sp *sph; 6228 6229 sph = (struct scsi_mode_page_header_sp *)page_header; 6230 6231 if (lun->mode_pages.index[i].subpage == 6232 sph->subpage) { 6233 page_index = &lun->mode_pages.index[i]; 6234 page_len = scsi_2btoul(sph->page_length); 6235 break; 6236 } 6237 } 6238 } 6239 6240 /* 6241 * If we couldn't find the page, or if we don't have a mode select 6242 * handler for it, send back an error to the user. 6243 */ 6244 if ((page_index == NULL) 6245 || (page_index->select_handler == NULL)) { 6246 ctl_set_invalid_field(ctsio, 6247 /*sks_valid*/ 1, 6248 /*command*/ 0, 6249 /*field*/ *len_used, 6250 /*bit_valid*/ 0, 6251 /*bit*/ 0); 6252 free(ctsio->kern_data_ptr, M_CTL); 6253 ctl_done((union ctl_io *)ctsio); 6254 return (CTL_RETVAL_COMPLETE); 6255 } 6256 6257 if (page_index->page_code & SMPH_SPF) { 6258 page_len_offset = 2; 6259 page_len_size = 2; 6260 } else { 6261 page_len_size = 1; 6262 page_len_offset = 1; 6263 } 6264 6265 /* 6266 * If the length the initiator gives us isn't the one we specify in 6267 * the mode page header, or if they didn't specify enough data in 6268 * the CDB to avoid truncating this page, kick out the request. 6269 */ 6270 if ((page_len != (page_index->page_len - page_len_offset - 6271 page_len_size)) 6272 || (*len_left < page_index->page_len)) { 6273 6274 6275 ctl_set_invalid_field(ctsio, 6276 /*sks_valid*/ 1, 6277 /*command*/ 0, 6278 /*field*/ *len_used + page_len_offset, 6279 /*bit_valid*/ 0, 6280 /*bit*/ 0); 6281 free(ctsio->kern_data_ptr, M_CTL); 6282 ctl_done((union ctl_io *)ctsio); 6283 return (CTL_RETVAL_COMPLETE); 6284 } 6285 6286 /* 6287 * Run through the mode page, checking to make sure that the bits 6288 * the user changed are actually legal for him to change. 6289 */ 6290 for (i = 0; i < page_index->page_len; i++) { 6291 uint8_t *user_byte, *change_mask, *current_byte; 6292 int bad_bit; 6293 int j; 6294 6295 user_byte = (uint8_t *)page_header + i; 6296 change_mask = page_index->page_data + 6297 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6298 current_byte = page_index->page_data + 6299 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6300 6301 /* 6302 * Check to see whether the user set any bits in this byte 6303 * that he is not allowed to set. 6304 */ 6305 if ((*user_byte & ~(*change_mask)) == 6306 (*current_byte & ~(*change_mask))) 6307 continue; 6308 6309 /* 6310 * Go through bit by bit to determine which one is illegal. 6311 */ 6312 bad_bit = 0; 6313 for (j = 7; j >= 0; j--) { 6314 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6315 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6316 bad_bit = i; 6317 break; 6318 } 6319 } 6320 ctl_set_invalid_field(ctsio, 6321 /*sks_valid*/ 1, 6322 /*command*/ 0, 6323 /*field*/ *len_used + i, 6324 /*bit_valid*/ 1, 6325 /*bit*/ bad_bit); 6326 free(ctsio->kern_data_ptr, M_CTL); 6327 ctl_done((union ctl_io *)ctsio); 6328 return (CTL_RETVAL_COMPLETE); 6329 } 6330 6331 /* 6332 * Decrement these before we call the page handler, since we may 6333 * end up getting called back one way or another before the handler 6334 * returns to this context. 6335 */ 6336 *len_left -= page_index->page_len; 6337 *len_used += page_index->page_len; 6338 6339 retval = page_index->select_handler(ctsio, page_index, 6340 (uint8_t *)page_header); 6341 6342 /* 6343 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6344 * wait until this queued command completes to finish processing 6345 * the mode page. If it returns anything other than 6346 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6347 * already set the sense information, freed the data pointer, and 6348 * completed the io for us. 6349 */ 6350 if (retval != CTL_RETVAL_COMPLETE) 6351 goto bailout_no_done; 6352 6353 /* 6354 * If the initiator sent us more than one page, parse the next one. 6355 */ 6356 if (*len_left > 0) 6357 goto do_next_page; 6358 6359 ctl_set_success(ctsio); 6360 free(ctsio->kern_data_ptr, M_CTL); 6361 ctl_done((union ctl_io *)ctsio); 6362 6363bailout_no_done: 6364 6365 return (CTL_RETVAL_COMPLETE); 6366 6367} 6368 6369int 6370ctl_mode_select(struct ctl_scsiio *ctsio) 6371{ 6372 int param_len, pf, sp; 6373 int header_size, bd_len; 6374 int len_left, len_used; 6375 struct ctl_page_index *page_index; 6376 struct ctl_lun *lun; 6377 int control_dev, page_len; 6378 union ctl_modepage_info *modepage_info; 6379 int retval; 6380 6381 pf = 0; 6382 sp = 0; 6383 page_len = 0; 6384 len_used = 0; 6385 len_left = 0; 6386 retval = 0; 6387 bd_len = 0; 6388 page_index = NULL; 6389 6390 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6391 6392 if (lun->be_lun->lun_type != T_DIRECT) 6393 control_dev = 1; 6394 else 6395 control_dev = 0; 6396 6397 switch (ctsio->cdb[0]) { 6398 case MODE_SELECT_6: { 6399 struct scsi_mode_select_6 *cdb; 6400 6401 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6402 6403 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6404 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6405 6406 param_len = cdb->length; 6407 header_size = sizeof(struct scsi_mode_header_6); 6408 break; 6409 } 6410 case MODE_SELECT_10: { 6411 struct scsi_mode_select_10 *cdb; 6412 6413 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6414 6415 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6416 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6417 6418 param_len = scsi_2btoul(cdb->length); 6419 header_size = sizeof(struct scsi_mode_header_10); 6420 break; 6421 } 6422 default: 6423 ctl_set_invalid_opcode(ctsio); 6424 ctl_done((union ctl_io *)ctsio); 6425 return (CTL_RETVAL_COMPLETE); 6426 break; /* NOTREACHED */ 6427 } 6428 6429 /* 6430 * From SPC-3: 6431 * "A parameter list length of zero indicates that the Data-Out Buffer 6432 * shall be empty. This condition shall not be considered as an error." 6433 */ 6434 if (param_len == 0) { 6435 ctl_set_success(ctsio); 6436 ctl_done((union ctl_io *)ctsio); 6437 return (CTL_RETVAL_COMPLETE); 6438 } 6439 6440 /* 6441 * Since we'll hit this the first time through, prior to 6442 * allocation, we don't need to free a data buffer here. 6443 */ 6444 if (param_len < header_size) { 6445 ctl_set_param_len_error(ctsio); 6446 ctl_done((union ctl_io *)ctsio); 6447 return (CTL_RETVAL_COMPLETE); 6448 } 6449 6450 /* 6451 * Allocate the data buffer and grab the user's data. In theory, 6452 * we shouldn't have to sanity check the parameter list length here 6453 * because the maximum size is 64K. We should be able to malloc 6454 * that much without too many problems. 6455 */ 6456 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6457 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6458 ctsio->kern_data_len = param_len; 6459 ctsio->kern_total_len = param_len; 6460 ctsio->kern_data_resid = 0; 6461 ctsio->kern_rel_offset = 0; 6462 ctsio->kern_sg_entries = 0; 6463 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6464 ctsio->be_move_done = ctl_config_move_done; 6465 ctl_datamove((union ctl_io *)ctsio); 6466 6467 return (CTL_RETVAL_COMPLETE); 6468 } 6469 6470 switch (ctsio->cdb[0]) { 6471 case MODE_SELECT_6: { 6472 struct scsi_mode_header_6 *mh6; 6473 6474 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6475 bd_len = mh6->blk_desc_len; 6476 break; 6477 } 6478 case MODE_SELECT_10: { 6479 struct scsi_mode_header_10 *mh10; 6480 6481 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6482 bd_len = scsi_2btoul(mh10->blk_desc_len); 6483 break; 6484 } 6485 default: 6486 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6487 break; 6488 } 6489 6490 if (param_len < (header_size + bd_len)) { 6491 free(ctsio->kern_data_ptr, M_CTL); 6492 ctl_set_param_len_error(ctsio); 6493 ctl_done((union ctl_io *)ctsio); 6494 return (CTL_RETVAL_COMPLETE); 6495 } 6496 6497 /* 6498 * Set the IO_CONT flag, so that if this I/O gets passed to 6499 * ctl_config_write_done(), it'll get passed back to 6500 * ctl_do_mode_select() for further processing, or completion if 6501 * we're all done. 6502 */ 6503 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6504 ctsio->io_cont = ctl_do_mode_select; 6505 6506 modepage_info = (union ctl_modepage_info *) 6507 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6508 6509 memset(modepage_info, 0, sizeof(*modepage_info)); 6510 6511 len_left = param_len - header_size - bd_len; 6512 len_used = header_size + bd_len; 6513 6514 modepage_info->header.len_left = len_left; 6515 modepage_info->header.len_used = len_used; 6516 6517 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6518} 6519 6520int 6521ctl_mode_sense(struct ctl_scsiio *ctsio) 6522{ 6523 struct ctl_lun *lun; 6524 int pc, page_code, dbd, llba, subpage; 6525 int alloc_len, page_len, header_len, total_len; 6526 struct scsi_mode_block_descr *block_desc; 6527 struct ctl_page_index *page_index; 6528 int control_dev; 6529 6530 dbd = 0; 6531 llba = 0; 6532 block_desc = NULL; 6533 page_index = NULL; 6534 6535 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6536 6537 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6538 6539 if (lun->be_lun->lun_type != T_DIRECT) 6540 control_dev = 1; 6541 else 6542 control_dev = 0; 6543 6544 switch (ctsio->cdb[0]) { 6545 case MODE_SENSE_6: { 6546 struct scsi_mode_sense_6 *cdb; 6547 6548 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6549 6550 header_len = sizeof(struct scsi_mode_hdr_6); 6551 if (cdb->byte2 & SMS_DBD) 6552 dbd = 1; 6553 else 6554 header_len += sizeof(struct scsi_mode_block_descr); 6555 6556 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6557 page_code = cdb->page & SMS_PAGE_CODE; 6558 subpage = cdb->subpage; 6559 alloc_len = cdb->length; 6560 break; 6561 } 6562 case MODE_SENSE_10: { 6563 struct scsi_mode_sense_10 *cdb; 6564 6565 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6566 6567 header_len = sizeof(struct scsi_mode_hdr_10); 6568 6569 if (cdb->byte2 & SMS_DBD) 6570 dbd = 1; 6571 else 6572 header_len += sizeof(struct scsi_mode_block_descr); 6573 if (cdb->byte2 & SMS10_LLBAA) 6574 llba = 1; 6575 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6576 page_code = cdb->page & SMS_PAGE_CODE; 6577 subpage = cdb->subpage; 6578 alloc_len = scsi_2btoul(cdb->length); 6579 break; 6580 } 6581 default: 6582 ctl_set_invalid_opcode(ctsio); 6583 ctl_done((union ctl_io *)ctsio); 6584 return (CTL_RETVAL_COMPLETE); 6585 break; /* NOTREACHED */ 6586 } 6587 6588 /* 6589 * We have to make a first pass through to calculate the size of 6590 * the pages that match the user's query. Then we allocate enough 6591 * memory to hold it, and actually copy the data into the buffer. 6592 */ 6593 switch (page_code) { 6594 case SMS_ALL_PAGES_PAGE: { 6595 int i; 6596 6597 page_len = 0; 6598 6599 /* 6600 * At the moment, values other than 0 and 0xff here are 6601 * reserved according to SPC-3. 6602 */ 6603 if ((subpage != SMS_SUBPAGE_PAGE_0) 6604 && (subpage != SMS_SUBPAGE_ALL)) { 6605 ctl_set_invalid_field(ctsio, 6606 /*sks_valid*/ 1, 6607 /*command*/ 1, 6608 /*field*/ 3, 6609 /*bit_valid*/ 0, 6610 /*bit*/ 0); 6611 ctl_done((union ctl_io *)ctsio); 6612 return (CTL_RETVAL_COMPLETE); 6613 } 6614 6615 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6616 if ((control_dev != 0) 6617 && (lun->mode_pages.index[i].page_flags & 6618 CTL_PAGE_FLAG_DISK_ONLY)) 6619 continue; 6620 6621 /* 6622 * We don't use this subpage if the user didn't 6623 * request all subpages. 6624 */ 6625 if ((lun->mode_pages.index[i].subpage != 0) 6626 && (subpage == SMS_SUBPAGE_PAGE_0)) 6627 continue; 6628 6629#if 0 6630 printf("found page %#x len %d\n", 6631 lun->mode_pages.index[i].page_code & 6632 SMPH_PC_MASK, 6633 lun->mode_pages.index[i].page_len); 6634#endif 6635 page_len += lun->mode_pages.index[i].page_len; 6636 } 6637 break; 6638 } 6639 default: { 6640 int i; 6641 6642 page_len = 0; 6643 6644 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6645 /* Look for the right page code */ 6646 if ((lun->mode_pages.index[i].page_code & 6647 SMPH_PC_MASK) != page_code) 6648 continue; 6649 6650 /* Look for the right subpage or the subpage wildcard*/ 6651 if ((lun->mode_pages.index[i].subpage != subpage) 6652 && (subpage != SMS_SUBPAGE_ALL)) 6653 continue; 6654 6655 /* Make sure the page is supported for this dev type */ 6656 if ((control_dev != 0) 6657 && (lun->mode_pages.index[i].page_flags & 6658 CTL_PAGE_FLAG_DISK_ONLY)) 6659 continue; 6660 6661#if 0 6662 printf("found page %#x len %d\n", 6663 lun->mode_pages.index[i].page_code & 6664 SMPH_PC_MASK, 6665 lun->mode_pages.index[i].page_len); 6666#endif 6667 6668 page_len += lun->mode_pages.index[i].page_len; 6669 } 6670 6671 if (page_len == 0) { 6672 ctl_set_invalid_field(ctsio, 6673 /*sks_valid*/ 1, 6674 /*command*/ 1, 6675 /*field*/ 2, 6676 /*bit_valid*/ 1, 6677 /*bit*/ 5); 6678 ctl_done((union ctl_io *)ctsio); 6679 return (CTL_RETVAL_COMPLETE); 6680 } 6681 break; 6682 } 6683 } 6684 6685 total_len = header_len + page_len; 6686#if 0 6687 printf("header_len = %d, page_len = %d, total_len = %d\n", 6688 header_len, page_len, total_len); 6689#endif 6690 6691 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6692 ctsio->kern_sg_entries = 0; 6693 ctsio->kern_data_resid = 0; 6694 ctsio->kern_rel_offset = 0; 6695 if (total_len < alloc_len) { 6696 ctsio->residual = alloc_len - total_len; 6697 ctsio->kern_data_len = total_len; 6698 ctsio->kern_total_len = total_len; 6699 } else { 6700 ctsio->residual = 0; 6701 ctsio->kern_data_len = alloc_len; 6702 ctsio->kern_total_len = alloc_len; 6703 } 6704 6705 switch (ctsio->cdb[0]) { 6706 case MODE_SENSE_6: { 6707 struct scsi_mode_hdr_6 *header; 6708 6709 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6710 6711 header->datalen = MIN(total_len - 1, 254); 6712 if (control_dev == 0) { 6713 header->dev_specific = 0x10; /* DPOFUA */ 6714 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6715 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6716 .eca_and_aen & SCP_SWP) != 0) 6717 header->dev_specific |= 0x80; /* WP */ 6718 } 6719 if (dbd) 6720 header->block_descr_len = 0; 6721 else 6722 header->block_descr_len = 6723 sizeof(struct scsi_mode_block_descr); 6724 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6725 break; 6726 } 6727 case MODE_SENSE_10: { 6728 struct scsi_mode_hdr_10 *header; 6729 int datalen; 6730 6731 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6732 6733 datalen = MIN(total_len - 2, 65533); 6734 scsi_ulto2b(datalen, header->datalen); 6735 if (control_dev == 0) { 6736 header->dev_specific = 0x10; /* DPOFUA */ 6737 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6738 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6739 .eca_and_aen & SCP_SWP) != 0) 6740 header->dev_specific |= 0x80; /* WP */ 6741 } 6742 if (dbd) 6743 scsi_ulto2b(0, header->block_descr_len); 6744 else 6745 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6746 header->block_descr_len); 6747 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6748 break; 6749 } 6750 default: 6751 panic("invalid CDB type %#x", ctsio->cdb[0]); 6752 break; /* NOTREACHED */ 6753 } 6754 6755 /* 6756 * If we've got a disk, use its blocksize in the block 6757 * descriptor. Otherwise, just set it to 0. 6758 */ 6759 if (dbd == 0) { 6760 if (control_dev == 0) 6761 scsi_ulto3b(lun->be_lun->blocksize, 6762 block_desc->block_len); 6763 else 6764 scsi_ulto3b(0, block_desc->block_len); 6765 } 6766 6767 switch (page_code) { 6768 case SMS_ALL_PAGES_PAGE: { 6769 int i, data_used; 6770 6771 data_used = header_len; 6772 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6773 struct ctl_page_index *page_index; 6774 6775 page_index = &lun->mode_pages.index[i]; 6776 6777 if ((control_dev != 0) 6778 && (page_index->page_flags & 6779 CTL_PAGE_FLAG_DISK_ONLY)) 6780 continue; 6781 6782 /* 6783 * We don't use this subpage if the user didn't 6784 * request all subpages. We already checked (above) 6785 * to make sure the user only specified a subpage 6786 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6787 */ 6788 if ((page_index->subpage != 0) 6789 && (subpage == SMS_SUBPAGE_PAGE_0)) 6790 continue; 6791 6792 /* 6793 * Call the handler, if it exists, to update the 6794 * page to the latest values. 6795 */ 6796 if (page_index->sense_handler != NULL) 6797 page_index->sense_handler(ctsio, page_index,pc); 6798 6799 memcpy(ctsio->kern_data_ptr + data_used, 6800 page_index->page_data + 6801 (page_index->page_len * pc), 6802 page_index->page_len); 6803 data_used += page_index->page_len; 6804 } 6805 break; 6806 } 6807 default: { 6808 int i, data_used; 6809 6810 data_used = header_len; 6811 6812 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6813 struct ctl_page_index *page_index; 6814 6815 page_index = &lun->mode_pages.index[i]; 6816 6817 /* Look for the right page code */ 6818 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6819 continue; 6820 6821 /* Look for the right subpage or the subpage wildcard*/ 6822 if ((page_index->subpage != subpage) 6823 && (subpage != SMS_SUBPAGE_ALL)) 6824 continue; 6825 6826 /* Make sure the page is supported for this dev type */ 6827 if ((control_dev != 0) 6828 && (page_index->page_flags & 6829 CTL_PAGE_FLAG_DISK_ONLY)) 6830 continue; 6831 6832 /* 6833 * Call the handler, if it exists, to update the 6834 * page to the latest values. 6835 */ 6836 if (page_index->sense_handler != NULL) 6837 page_index->sense_handler(ctsio, page_index,pc); 6838 6839 memcpy(ctsio->kern_data_ptr + data_used, 6840 page_index->page_data + 6841 (page_index->page_len * pc), 6842 page_index->page_len); 6843 data_used += page_index->page_len; 6844 } 6845 break; 6846 } 6847 } 6848 6849 ctl_set_success(ctsio); 6850 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6851 ctsio->be_move_done = ctl_config_move_done; 6852 ctl_datamove((union ctl_io *)ctsio); 6853 return (CTL_RETVAL_COMPLETE); 6854} 6855 6856int 6857ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6858 struct ctl_page_index *page_index, 6859 int pc) 6860{ 6861 struct ctl_lun *lun; 6862 struct scsi_log_param_header *phdr; 6863 uint8_t *data; 6864 uint64_t val; 6865 6866 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6867 data = page_index->page_data; 6868 6869 if (lun->backend->lun_attr != NULL && 6870 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6871 != UINT64_MAX) { 6872 phdr = (struct scsi_log_param_header *)data; 6873 scsi_ulto2b(0x0001, phdr->param_code); 6874 phdr->param_control = SLP_LBIN | SLP_LP; 6875 phdr->param_len = 8; 6876 data = (uint8_t *)(phdr + 1); 6877 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6878 data[4] = 0x02; /* per-pool */ 6879 data += phdr->param_len; 6880 } 6881 6882 if (lun->backend->lun_attr != NULL && 6883 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6884 != UINT64_MAX) { 6885 phdr = (struct scsi_log_param_header *)data; 6886 scsi_ulto2b(0x0002, phdr->param_code); 6887 phdr->param_control = SLP_LBIN | SLP_LP; 6888 phdr->param_len = 8; 6889 data = (uint8_t *)(phdr + 1); 6890 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6891 data[4] = 0x01; /* per-LUN */ 6892 data += phdr->param_len; 6893 } 6894 6895 if (lun->backend->lun_attr != NULL && 6896 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6897 != UINT64_MAX) { 6898 phdr = (struct scsi_log_param_header *)data; 6899 scsi_ulto2b(0x00f1, phdr->param_code); 6900 phdr->param_control = SLP_LBIN | SLP_LP; 6901 phdr->param_len = 8; 6902 data = (uint8_t *)(phdr + 1); 6903 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6904 data[4] = 0x02; /* per-pool */ 6905 data += phdr->param_len; 6906 } 6907 6908 if (lun->backend->lun_attr != NULL && 6909 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6910 != UINT64_MAX) { 6911 phdr = (struct scsi_log_param_header *)data; 6912 scsi_ulto2b(0x00f2, phdr->param_code); 6913 phdr->param_control = SLP_LBIN | SLP_LP; 6914 phdr->param_len = 8; 6915 data = (uint8_t *)(phdr + 1); 6916 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6917 data[4] = 0x02; /* per-pool */ 6918 data += phdr->param_len; 6919 } 6920 6921 page_index->page_len = data - page_index->page_data; 6922 return (0); 6923} 6924 6925int 6926ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6927 struct ctl_page_index *page_index, 6928 int pc) 6929{ 6930 struct ctl_lun *lun; 6931 struct stat_page *data; 6932 uint64_t rn, wn, rb, wb; 6933 struct bintime rt, wt; 6934 int i; 6935 6936 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6937 data = (struct stat_page *)page_index->page_data; 6938 6939 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6940 data->sap.hdr.param_control = SLP_LBIN; 6941 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6942 sizeof(struct scsi_log_param_header); 6943 rn = wn = rb = wb = 0; 6944 bintime_clear(&rt); 6945 bintime_clear(&wt); 6946 for (i = 0; i < CTL_MAX_PORTS; i++) { 6947 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6948 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6949 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6950 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6951 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6952 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6953 } 6954 scsi_u64to8b(rn, data->sap.read_num); 6955 scsi_u64to8b(wn, data->sap.write_num); 6956 if (lun->stats.blocksize > 0) { 6957 scsi_u64to8b(wb / lun->stats.blocksize, 6958 data->sap.recvieved_lba); 6959 scsi_u64to8b(rb / lun->stats.blocksize, 6960 data->sap.transmitted_lba); 6961 } 6962 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6963 data->sap.read_int); 6964 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6965 data->sap.write_int); 6966 scsi_u64to8b(0, data->sap.weighted_num); 6967 scsi_u64to8b(0, data->sap.weighted_int); 6968 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6969 data->it.hdr.param_control = SLP_LBIN; 6970 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6971 sizeof(struct scsi_log_param_header); 6972#ifdef CTL_TIME_IO 6973 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6974#endif 6975 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6976 data->it.hdr.param_control = SLP_LBIN; 6977 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6978 sizeof(struct scsi_log_param_header); 6979 scsi_ulto4b(3, data->ti.exponent); 6980 scsi_ulto4b(1, data->ti.integer); 6981 6982 page_index->page_len = sizeof(*data); 6983 return (0); 6984} 6985 6986int 6987ctl_log_sense(struct ctl_scsiio *ctsio) 6988{ 6989 struct ctl_lun *lun; 6990 int i, pc, page_code, subpage; 6991 int alloc_len, total_len; 6992 struct ctl_page_index *page_index; 6993 struct scsi_log_sense *cdb; 6994 struct scsi_log_header *header; 6995 6996 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6997 6998 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6999 cdb = (struct scsi_log_sense *)ctsio->cdb; 7000 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 7001 page_code = cdb->page & SLS_PAGE_CODE; 7002 subpage = cdb->subpage; 7003 alloc_len = scsi_2btoul(cdb->length); 7004 7005 page_index = NULL; 7006 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 7007 page_index = &lun->log_pages.index[i]; 7008 7009 /* Look for the right page code */ 7010 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 7011 continue; 7012 7013 /* Look for the right subpage or the subpage wildcard*/ 7014 if (page_index->subpage != subpage) 7015 continue; 7016 7017 break; 7018 } 7019 if (i >= CTL_NUM_LOG_PAGES) { 7020 ctl_set_invalid_field(ctsio, 7021 /*sks_valid*/ 1, 7022 /*command*/ 1, 7023 /*field*/ 2, 7024 /*bit_valid*/ 0, 7025 /*bit*/ 0); 7026 ctl_done((union ctl_io *)ctsio); 7027 return (CTL_RETVAL_COMPLETE); 7028 } 7029 7030 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 7031 7032 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7033 ctsio->kern_sg_entries = 0; 7034 ctsio->kern_data_resid = 0; 7035 ctsio->kern_rel_offset = 0; 7036 if (total_len < alloc_len) { 7037 ctsio->residual = alloc_len - total_len; 7038 ctsio->kern_data_len = total_len; 7039 ctsio->kern_total_len = total_len; 7040 } else { 7041 ctsio->residual = 0; 7042 ctsio->kern_data_len = alloc_len; 7043 ctsio->kern_total_len = alloc_len; 7044 } 7045 7046 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7047 header->page = page_index->page_code; 7048 if (page_index->subpage) { 7049 header->page |= SL_SPF; 7050 header->subpage = page_index->subpage; 7051 } 7052 scsi_ulto2b(page_index->page_len, header->datalen); 7053 7054 /* 7055 * Call the handler, if it exists, to update the 7056 * page to the latest values. 7057 */ 7058 if (page_index->sense_handler != NULL) 7059 page_index->sense_handler(ctsio, page_index, pc); 7060 7061 memcpy(header + 1, page_index->page_data, page_index->page_len); 7062 7063 ctl_set_success(ctsio); 7064 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7065 ctsio->be_move_done = ctl_config_move_done; 7066 ctl_datamove((union ctl_io *)ctsio); 7067 return (CTL_RETVAL_COMPLETE); 7068} 7069 7070int 7071ctl_read_capacity(struct ctl_scsiio *ctsio) 7072{ 7073 struct scsi_read_capacity *cdb; 7074 struct scsi_read_capacity_data *data; 7075 struct ctl_lun *lun; 7076 uint32_t lba; 7077 7078 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7079 7080 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7081 7082 lba = scsi_4btoul(cdb->addr); 7083 if (((cdb->pmi & SRC_PMI) == 0) 7084 && (lba != 0)) { 7085 ctl_set_invalid_field(/*ctsio*/ ctsio, 7086 /*sks_valid*/ 1, 7087 /*command*/ 1, 7088 /*field*/ 2, 7089 /*bit_valid*/ 0, 7090 /*bit*/ 0); 7091 ctl_done((union ctl_io *)ctsio); 7092 return (CTL_RETVAL_COMPLETE); 7093 } 7094 7095 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7096 7097 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7098 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7099 ctsio->residual = 0; 7100 ctsio->kern_data_len = sizeof(*data); 7101 ctsio->kern_total_len = sizeof(*data); 7102 ctsio->kern_data_resid = 0; 7103 ctsio->kern_rel_offset = 0; 7104 ctsio->kern_sg_entries = 0; 7105 7106 /* 7107 * If the maximum LBA is greater than 0xfffffffe, the user must 7108 * issue a SERVICE ACTION IN (16) command, with the read capacity 7109 * serivce action set. 7110 */ 7111 if (lun->be_lun->maxlba > 0xfffffffe) 7112 scsi_ulto4b(0xffffffff, data->addr); 7113 else 7114 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7115 7116 /* 7117 * XXX KDM this may not be 512 bytes... 7118 */ 7119 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7120 7121 ctl_set_success(ctsio); 7122 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7123 ctsio->be_move_done = ctl_config_move_done; 7124 ctl_datamove((union ctl_io *)ctsio); 7125 return (CTL_RETVAL_COMPLETE); 7126} 7127 7128int 7129ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7130{ 7131 struct scsi_read_capacity_16 *cdb; 7132 struct scsi_read_capacity_data_long *data; 7133 struct ctl_lun *lun; 7134 uint64_t lba; 7135 uint32_t alloc_len; 7136 7137 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7138 7139 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7140 7141 alloc_len = scsi_4btoul(cdb->alloc_len); 7142 lba = scsi_8btou64(cdb->addr); 7143 7144 if ((cdb->reladr & SRC16_PMI) 7145 && (lba != 0)) { 7146 ctl_set_invalid_field(/*ctsio*/ ctsio, 7147 /*sks_valid*/ 1, 7148 /*command*/ 1, 7149 /*field*/ 2, 7150 /*bit_valid*/ 0, 7151 /*bit*/ 0); 7152 ctl_done((union ctl_io *)ctsio); 7153 return (CTL_RETVAL_COMPLETE); 7154 } 7155 7156 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7157 7158 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7159 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7160 7161 if (sizeof(*data) < alloc_len) { 7162 ctsio->residual = alloc_len - sizeof(*data); 7163 ctsio->kern_data_len = sizeof(*data); 7164 ctsio->kern_total_len = sizeof(*data); 7165 } else { 7166 ctsio->residual = 0; 7167 ctsio->kern_data_len = alloc_len; 7168 ctsio->kern_total_len = alloc_len; 7169 } 7170 ctsio->kern_data_resid = 0; 7171 ctsio->kern_rel_offset = 0; 7172 ctsio->kern_sg_entries = 0; 7173 7174 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7175 /* XXX KDM this may not be 512 bytes... */ 7176 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7177 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7178 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7179 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7180 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7181 7182 ctl_set_success(ctsio); 7183 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7184 ctsio->be_move_done = ctl_config_move_done; 7185 ctl_datamove((union ctl_io *)ctsio); 7186 return (CTL_RETVAL_COMPLETE); 7187} 7188 7189int 7190ctl_get_lba_status(struct ctl_scsiio *ctsio) 7191{ 7192 struct scsi_get_lba_status *cdb; 7193 struct scsi_get_lba_status_data *data; 7194 struct ctl_lun *lun; 7195 struct ctl_lba_len_flags *lbalen; 7196 uint64_t lba; 7197 uint32_t alloc_len, total_len; 7198 int retval; 7199 7200 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7201 7202 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7203 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7204 lba = scsi_8btou64(cdb->addr); 7205 alloc_len = scsi_4btoul(cdb->alloc_len); 7206 7207 if (lba > lun->be_lun->maxlba) { 7208 ctl_set_lba_out_of_range(ctsio); 7209 ctl_done((union ctl_io *)ctsio); 7210 return (CTL_RETVAL_COMPLETE); 7211 } 7212 7213 total_len = sizeof(*data) + sizeof(data->descr[0]); 7214 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7215 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7216 7217 if (total_len < alloc_len) { 7218 ctsio->residual = alloc_len - total_len; 7219 ctsio->kern_data_len = total_len; 7220 ctsio->kern_total_len = total_len; 7221 } else { 7222 ctsio->residual = 0; 7223 ctsio->kern_data_len = alloc_len; 7224 ctsio->kern_total_len = alloc_len; 7225 } 7226 ctsio->kern_data_resid = 0; 7227 ctsio->kern_rel_offset = 0; 7228 ctsio->kern_sg_entries = 0; 7229 7230 /* Fill dummy data in case backend can't tell anything. */ 7231 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7232 scsi_u64to8b(lba, data->descr[0].addr); 7233 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7234 data->descr[0].length); 7235 data->descr[0].status = 0; /* Mapped or unknown. */ 7236 7237 ctl_set_success(ctsio); 7238 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7239 ctsio->be_move_done = ctl_config_move_done; 7240 7241 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7242 lbalen->lba = lba; 7243 lbalen->len = total_len; 7244 lbalen->flags = 0; 7245 retval = lun->backend->config_read((union ctl_io *)ctsio); 7246 return (CTL_RETVAL_COMPLETE); 7247} 7248 7249int 7250ctl_read_defect(struct ctl_scsiio *ctsio) 7251{ 7252 struct scsi_read_defect_data_10 *ccb10; 7253 struct scsi_read_defect_data_12 *ccb12; 7254 struct scsi_read_defect_data_hdr_10 *data10; 7255 struct scsi_read_defect_data_hdr_12 *data12; 7256 uint32_t alloc_len, data_len; 7257 uint8_t format; 7258 7259 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7260 7261 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7262 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7263 format = ccb10->format; 7264 alloc_len = scsi_2btoul(ccb10->alloc_length); 7265 data_len = sizeof(*data10); 7266 } else { 7267 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7268 format = ccb12->format; 7269 alloc_len = scsi_4btoul(ccb12->alloc_length); 7270 data_len = sizeof(*data12); 7271 } 7272 if (alloc_len == 0) { 7273 ctl_set_success(ctsio); 7274 ctl_done((union ctl_io *)ctsio); 7275 return (CTL_RETVAL_COMPLETE); 7276 } 7277 7278 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7279 if (data_len < alloc_len) { 7280 ctsio->residual = alloc_len - data_len; 7281 ctsio->kern_data_len = data_len; 7282 ctsio->kern_total_len = data_len; 7283 } else { 7284 ctsio->residual = 0; 7285 ctsio->kern_data_len = alloc_len; 7286 ctsio->kern_total_len = alloc_len; 7287 } 7288 ctsio->kern_data_resid = 0; 7289 ctsio->kern_rel_offset = 0; 7290 ctsio->kern_sg_entries = 0; 7291 7292 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7293 data10 = (struct scsi_read_defect_data_hdr_10 *) 7294 ctsio->kern_data_ptr; 7295 data10->format = format; 7296 scsi_ulto2b(0, data10->length); 7297 } else { 7298 data12 = (struct scsi_read_defect_data_hdr_12 *) 7299 ctsio->kern_data_ptr; 7300 data12->format = format; 7301 scsi_ulto2b(0, data12->generation); 7302 scsi_ulto4b(0, data12->length); 7303 } 7304 7305 ctl_set_success(ctsio); 7306 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7307 ctsio->be_move_done = ctl_config_move_done; 7308 ctl_datamove((union ctl_io *)ctsio); 7309 return (CTL_RETVAL_COMPLETE); 7310} 7311 7312int 7313ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7314{ 7315 struct scsi_maintenance_in *cdb; 7316 int retval; 7317 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os; 7318 int num_target_port_groups, num_target_ports; 7319 struct ctl_lun *lun; 7320 struct ctl_softc *softc; 7321 struct ctl_port *port; 7322 struct scsi_target_group_data *rtg_ptr; 7323 struct scsi_target_group_data_extended *rtg_ext_ptr; 7324 struct scsi_target_port_group_descriptor *tpg_desc; 7325 7326 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7327 7328 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7329 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7330 softc = lun->ctl_softc; 7331 7332 retval = CTL_RETVAL_COMPLETE; 7333 7334 switch (cdb->byte2 & STG_PDF_MASK) { 7335 case STG_PDF_LENGTH: 7336 ext = 0; 7337 break; 7338 case STG_PDF_EXTENDED: 7339 ext = 1; 7340 break; 7341 default: 7342 ctl_set_invalid_field(/*ctsio*/ ctsio, 7343 /*sks_valid*/ 1, 7344 /*command*/ 1, 7345 /*field*/ 2, 7346 /*bit_valid*/ 1, 7347 /*bit*/ 5); 7348 ctl_done((union ctl_io *)ctsio); 7349 return(retval); 7350 } 7351 7352 if (softc->is_single) 7353 num_target_port_groups = 1; 7354 else 7355 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7356 num_target_ports = 0; 7357 mtx_lock(&softc->ctl_lock); 7358 STAILQ_FOREACH(port, &softc->port_list, links) { 7359 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7360 continue; 7361 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7362 continue; 7363 num_target_ports++; 7364 } 7365 mtx_unlock(&softc->ctl_lock); 7366 7367 if (ext) 7368 total_len = sizeof(struct scsi_target_group_data_extended); 7369 else 7370 total_len = sizeof(struct scsi_target_group_data); 7371 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7372 num_target_port_groups + 7373 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7374 7375 alloc_len = scsi_4btoul(cdb->length); 7376 7377 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7378 7379 ctsio->kern_sg_entries = 0; 7380 7381 if (total_len < alloc_len) { 7382 ctsio->residual = alloc_len - total_len; 7383 ctsio->kern_data_len = total_len; 7384 ctsio->kern_total_len = total_len; 7385 } else { 7386 ctsio->residual = 0; 7387 ctsio->kern_data_len = alloc_len; 7388 ctsio->kern_total_len = alloc_len; 7389 } 7390 ctsio->kern_data_resid = 0; 7391 ctsio->kern_rel_offset = 0; 7392 7393 if (ext) { 7394 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7395 ctsio->kern_data_ptr; 7396 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7397 rtg_ext_ptr->format_type = 0x10; 7398 rtg_ext_ptr->implicit_transition_time = 0; 7399 tpg_desc = &rtg_ext_ptr->groups[0]; 7400 } else { 7401 rtg_ptr = (struct scsi_target_group_data *) 7402 ctsio->kern_data_ptr; 7403 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7404 tpg_desc = &rtg_ptr->groups[0]; 7405 } 7406 7407 mtx_lock(&softc->ctl_lock); 7408 pg = softc->port_min / softc->port_cnt; 7409 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7410 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7411 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7412 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7413 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7414 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7415 else 7416 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7417 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7418 os = gs; 7419 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7420 } else 7421 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7422 for (g = 0; g < num_target_port_groups; g++) { 7423 tpg_desc->pref_state = (g == pg) ? gs : os; 7424 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7425 TPG_U_SUP | TPG_T_SUP; 7426 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7427 tpg_desc->status = TPG_IMPLICIT; 7428 pc = 0; 7429 STAILQ_FOREACH(port, &softc->port_list, links) { 7430 if (port->targ_port < g * softc->port_cnt || 7431 port->targ_port >= (g + 1) * softc->port_cnt) 7432 continue; 7433 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7434 continue; 7435 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7436 continue; 7437 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7438 relative_target_port_identifier); 7439 pc++; 7440 } 7441 tpg_desc->target_port_count = pc; 7442 tpg_desc = (struct scsi_target_port_group_descriptor *) 7443 &tpg_desc->descriptors[pc]; 7444 } 7445 mtx_unlock(&softc->ctl_lock); 7446 7447 ctl_set_success(ctsio); 7448 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7449 ctsio->be_move_done = ctl_config_move_done; 7450 ctl_datamove((union ctl_io *)ctsio); 7451 return(retval); 7452} 7453 7454int 7455ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7456{ 7457 struct ctl_lun *lun; 7458 struct scsi_report_supported_opcodes *cdb; 7459 const struct ctl_cmd_entry *entry, *sentry; 7460 struct scsi_report_supported_opcodes_all *all; 7461 struct scsi_report_supported_opcodes_descr *descr; 7462 struct scsi_report_supported_opcodes_one *one; 7463 int retval; 7464 int alloc_len, total_len; 7465 int opcode, service_action, i, j, num; 7466 7467 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7468 7469 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7470 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7471 7472 retval = CTL_RETVAL_COMPLETE; 7473 7474 opcode = cdb->requested_opcode; 7475 service_action = scsi_2btoul(cdb->requested_service_action); 7476 switch (cdb->options & RSO_OPTIONS_MASK) { 7477 case RSO_OPTIONS_ALL: 7478 num = 0; 7479 for (i = 0; i < 256; i++) { 7480 entry = &ctl_cmd_table[i]; 7481 if (entry->flags & CTL_CMD_FLAG_SA5) { 7482 for (j = 0; j < 32; j++) { 7483 sentry = &((const struct ctl_cmd_entry *) 7484 entry->execute)[j]; 7485 if (ctl_cmd_applicable( 7486 lun->be_lun->lun_type, sentry)) 7487 num++; 7488 } 7489 } else { 7490 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7491 entry)) 7492 num++; 7493 } 7494 } 7495 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7496 num * sizeof(struct scsi_report_supported_opcodes_descr); 7497 break; 7498 case RSO_OPTIONS_OC: 7499 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7500 ctl_set_invalid_field(/*ctsio*/ ctsio, 7501 /*sks_valid*/ 1, 7502 /*command*/ 1, 7503 /*field*/ 2, 7504 /*bit_valid*/ 1, 7505 /*bit*/ 2); 7506 ctl_done((union ctl_io *)ctsio); 7507 return (CTL_RETVAL_COMPLETE); 7508 } 7509 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7510 break; 7511 case RSO_OPTIONS_OC_SA: 7512 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7513 service_action >= 32) { 7514 ctl_set_invalid_field(/*ctsio*/ ctsio, 7515 /*sks_valid*/ 1, 7516 /*command*/ 1, 7517 /*field*/ 2, 7518 /*bit_valid*/ 1, 7519 /*bit*/ 2); 7520 ctl_done((union ctl_io *)ctsio); 7521 return (CTL_RETVAL_COMPLETE); 7522 } 7523 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7524 break; 7525 default: 7526 ctl_set_invalid_field(/*ctsio*/ ctsio, 7527 /*sks_valid*/ 1, 7528 /*command*/ 1, 7529 /*field*/ 2, 7530 /*bit_valid*/ 1, 7531 /*bit*/ 2); 7532 ctl_done((union ctl_io *)ctsio); 7533 return (CTL_RETVAL_COMPLETE); 7534 } 7535 7536 alloc_len = scsi_4btoul(cdb->length); 7537 7538 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7539 7540 ctsio->kern_sg_entries = 0; 7541 7542 if (total_len < alloc_len) { 7543 ctsio->residual = alloc_len - total_len; 7544 ctsio->kern_data_len = total_len; 7545 ctsio->kern_total_len = total_len; 7546 } else { 7547 ctsio->residual = 0; 7548 ctsio->kern_data_len = alloc_len; 7549 ctsio->kern_total_len = alloc_len; 7550 } 7551 ctsio->kern_data_resid = 0; 7552 ctsio->kern_rel_offset = 0; 7553 7554 switch (cdb->options & RSO_OPTIONS_MASK) { 7555 case RSO_OPTIONS_ALL: 7556 all = (struct scsi_report_supported_opcodes_all *) 7557 ctsio->kern_data_ptr; 7558 num = 0; 7559 for (i = 0; i < 256; i++) { 7560 entry = &ctl_cmd_table[i]; 7561 if (entry->flags & CTL_CMD_FLAG_SA5) { 7562 for (j = 0; j < 32; j++) { 7563 sentry = &((const struct ctl_cmd_entry *) 7564 entry->execute)[j]; 7565 if (!ctl_cmd_applicable( 7566 lun->be_lun->lun_type, sentry)) 7567 continue; 7568 descr = &all->descr[num++]; 7569 descr->opcode = i; 7570 scsi_ulto2b(j, descr->service_action); 7571 descr->flags = RSO_SERVACTV; 7572 scsi_ulto2b(sentry->length, 7573 descr->cdb_length); 7574 } 7575 } else { 7576 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7577 entry)) 7578 continue; 7579 descr = &all->descr[num++]; 7580 descr->opcode = i; 7581 scsi_ulto2b(0, descr->service_action); 7582 descr->flags = 0; 7583 scsi_ulto2b(entry->length, descr->cdb_length); 7584 } 7585 } 7586 scsi_ulto4b( 7587 num * sizeof(struct scsi_report_supported_opcodes_descr), 7588 all->length); 7589 break; 7590 case RSO_OPTIONS_OC: 7591 one = (struct scsi_report_supported_opcodes_one *) 7592 ctsio->kern_data_ptr; 7593 entry = &ctl_cmd_table[opcode]; 7594 goto fill_one; 7595 case RSO_OPTIONS_OC_SA: 7596 one = (struct scsi_report_supported_opcodes_one *) 7597 ctsio->kern_data_ptr; 7598 entry = &ctl_cmd_table[opcode]; 7599 entry = &((const struct ctl_cmd_entry *) 7600 entry->execute)[service_action]; 7601fill_one: 7602 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7603 one->support = 3; 7604 scsi_ulto2b(entry->length, one->cdb_length); 7605 one->cdb_usage[0] = opcode; 7606 memcpy(&one->cdb_usage[1], entry->usage, 7607 entry->length - 1); 7608 } else 7609 one->support = 1; 7610 break; 7611 } 7612 7613 ctl_set_success(ctsio); 7614 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7615 ctsio->be_move_done = ctl_config_move_done; 7616 ctl_datamove((union ctl_io *)ctsio); 7617 return(retval); 7618} 7619 7620int 7621ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7622{ 7623 struct scsi_report_supported_tmf *cdb; 7624 struct scsi_report_supported_tmf_data *data; 7625 int retval; 7626 int alloc_len, total_len; 7627 7628 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7629 7630 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7631 7632 retval = CTL_RETVAL_COMPLETE; 7633 7634 total_len = sizeof(struct scsi_report_supported_tmf_data); 7635 alloc_len = scsi_4btoul(cdb->length); 7636 7637 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7638 7639 ctsio->kern_sg_entries = 0; 7640 7641 if (total_len < alloc_len) { 7642 ctsio->residual = alloc_len - total_len; 7643 ctsio->kern_data_len = total_len; 7644 ctsio->kern_total_len = total_len; 7645 } else { 7646 ctsio->residual = 0; 7647 ctsio->kern_data_len = alloc_len; 7648 ctsio->kern_total_len = alloc_len; 7649 } 7650 ctsio->kern_data_resid = 0; 7651 ctsio->kern_rel_offset = 0; 7652 7653 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7654 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7655 RST_TRS; 7656 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7657 7658 ctl_set_success(ctsio); 7659 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7660 ctsio->be_move_done = ctl_config_move_done; 7661 ctl_datamove((union ctl_io *)ctsio); 7662 return (retval); 7663} 7664 7665int 7666ctl_report_timestamp(struct ctl_scsiio *ctsio) 7667{ 7668 struct scsi_report_timestamp *cdb; 7669 struct scsi_report_timestamp_data *data; 7670 struct timeval tv; 7671 int64_t timestamp; 7672 int retval; 7673 int alloc_len, total_len; 7674 7675 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7676 7677 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7678 7679 retval = CTL_RETVAL_COMPLETE; 7680 7681 total_len = sizeof(struct scsi_report_timestamp_data); 7682 alloc_len = scsi_4btoul(cdb->length); 7683 7684 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7685 7686 ctsio->kern_sg_entries = 0; 7687 7688 if (total_len < alloc_len) { 7689 ctsio->residual = alloc_len - total_len; 7690 ctsio->kern_data_len = total_len; 7691 ctsio->kern_total_len = total_len; 7692 } else { 7693 ctsio->residual = 0; 7694 ctsio->kern_data_len = alloc_len; 7695 ctsio->kern_total_len = alloc_len; 7696 } 7697 ctsio->kern_data_resid = 0; 7698 ctsio->kern_rel_offset = 0; 7699 7700 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7701 scsi_ulto2b(sizeof(*data) - 2, data->length); 7702 data->origin = RTS_ORIG_OUTSIDE; 7703 getmicrotime(&tv); 7704 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7705 scsi_ulto4b(timestamp >> 16, data->timestamp); 7706 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7707 7708 ctl_set_success(ctsio); 7709 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7710 ctsio->be_move_done = ctl_config_move_done; 7711 ctl_datamove((union ctl_io *)ctsio); 7712 return (retval); 7713} 7714 7715int 7716ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7717{ 7718 struct scsi_per_res_in *cdb; 7719 int alloc_len, total_len = 0; 7720 /* struct scsi_per_res_in_rsrv in_data; */ 7721 struct ctl_lun *lun; 7722 struct ctl_softc *softc; 7723 uint64_t key; 7724 7725 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7726 7727 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7728 7729 alloc_len = scsi_2btoul(cdb->length); 7730 7731 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7732 softc = lun->ctl_softc; 7733 7734retry: 7735 mtx_lock(&lun->lun_lock); 7736 switch (cdb->action) { 7737 case SPRI_RK: /* read keys */ 7738 total_len = sizeof(struct scsi_per_res_in_keys) + 7739 lun->pr_key_count * 7740 sizeof(struct scsi_per_res_key); 7741 break; 7742 case SPRI_RR: /* read reservation */ 7743 if (lun->flags & CTL_LUN_PR_RESERVED) 7744 total_len = sizeof(struct scsi_per_res_in_rsrv); 7745 else 7746 total_len = sizeof(struct scsi_per_res_in_header); 7747 break; 7748 case SPRI_RC: /* report capabilities */ 7749 total_len = sizeof(struct scsi_per_res_cap); 7750 break; 7751 case SPRI_RS: /* read full status */ 7752 total_len = sizeof(struct scsi_per_res_in_header) + 7753 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7754 lun->pr_key_count; 7755 break; 7756 default: 7757 panic("Invalid PR type %x", cdb->action); 7758 } 7759 mtx_unlock(&lun->lun_lock); 7760 7761 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7762 7763 if (total_len < alloc_len) { 7764 ctsio->residual = alloc_len - total_len; 7765 ctsio->kern_data_len = total_len; 7766 ctsio->kern_total_len = total_len; 7767 } else { 7768 ctsio->residual = 0; 7769 ctsio->kern_data_len = alloc_len; 7770 ctsio->kern_total_len = alloc_len; 7771 } 7772 7773 ctsio->kern_data_resid = 0; 7774 ctsio->kern_rel_offset = 0; 7775 ctsio->kern_sg_entries = 0; 7776 7777 mtx_lock(&lun->lun_lock); 7778 switch (cdb->action) { 7779 case SPRI_RK: { // read keys 7780 struct scsi_per_res_in_keys *res_keys; 7781 int i, key_count; 7782 7783 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7784 7785 /* 7786 * We had to drop the lock to allocate our buffer, which 7787 * leaves time for someone to come in with another 7788 * persistent reservation. (That is unlikely, though, 7789 * since this should be the only persistent reservation 7790 * command active right now.) 7791 */ 7792 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7793 (lun->pr_key_count * 7794 sizeof(struct scsi_per_res_key)))){ 7795 mtx_unlock(&lun->lun_lock); 7796 free(ctsio->kern_data_ptr, M_CTL); 7797 printf("%s: reservation length changed, retrying\n", 7798 __func__); 7799 goto retry; 7800 } 7801 7802 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7803 7804 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7805 lun->pr_key_count, res_keys->header.length); 7806 7807 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7808 if ((key = ctl_get_prkey(lun, i)) == 0) 7809 continue; 7810 7811 /* 7812 * We used lun->pr_key_count to calculate the 7813 * size to allocate. If it turns out the number of 7814 * initiators with the registered flag set is 7815 * larger than that (i.e. they haven't been kept in 7816 * sync), we've got a problem. 7817 */ 7818 if (key_count >= lun->pr_key_count) { 7819#ifdef NEEDTOPORT 7820 csevent_log(CSC_CTL | CSC_SHELF_SW | 7821 CTL_PR_ERROR, 7822 csevent_LogType_Fault, 7823 csevent_AlertLevel_Yellow, 7824 csevent_FRU_ShelfController, 7825 csevent_FRU_Firmware, 7826 csevent_FRU_Unknown, 7827 "registered keys %d >= key " 7828 "count %d", key_count, 7829 lun->pr_key_count); 7830#endif 7831 key_count++; 7832 continue; 7833 } 7834 scsi_u64to8b(key, res_keys->keys[key_count].key); 7835 key_count++; 7836 } 7837 break; 7838 } 7839 case SPRI_RR: { // read reservation 7840 struct scsi_per_res_in_rsrv *res; 7841 int tmp_len, header_only; 7842 7843 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7844 7845 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7846 7847 if (lun->flags & CTL_LUN_PR_RESERVED) 7848 { 7849 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7850 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7851 res->header.length); 7852 header_only = 0; 7853 } else { 7854 tmp_len = sizeof(struct scsi_per_res_in_header); 7855 scsi_ulto4b(0, res->header.length); 7856 header_only = 1; 7857 } 7858 7859 /* 7860 * We had to drop the lock to allocate our buffer, which 7861 * leaves time for someone to come in with another 7862 * persistent reservation. (That is unlikely, though, 7863 * since this should be the only persistent reservation 7864 * command active right now.) 7865 */ 7866 if (tmp_len != total_len) { 7867 mtx_unlock(&lun->lun_lock); 7868 free(ctsio->kern_data_ptr, M_CTL); 7869 printf("%s: reservation status changed, retrying\n", 7870 __func__); 7871 goto retry; 7872 } 7873 7874 /* 7875 * No reservation held, so we're done. 7876 */ 7877 if (header_only != 0) 7878 break; 7879 7880 /* 7881 * If the registration is an All Registrants type, the key 7882 * is 0, since it doesn't really matter. 7883 */ 7884 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7885 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7886 res->data.reservation); 7887 } 7888 res->data.scopetype = lun->res_type; 7889 break; 7890 } 7891 case SPRI_RC: //report capabilities 7892 { 7893 struct scsi_per_res_cap *res_cap; 7894 uint16_t type_mask; 7895 7896 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7897 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7898 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7899 type_mask = SPRI_TM_WR_EX_AR | 7900 SPRI_TM_EX_AC_RO | 7901 SPRI_TM_WR_EX_RO | 7902 SPRI_TM_EX_AC | 7903 SPRI_TM_WR_EX | 7904 SPRI_TM_EX_AC_AR; 7905 scsi_ulto2b(type_mask, res_cap->type_mask); 7906 break; 7907 } 7908 case SPRI_RS: { // read full status 7909 struct scsi_per_res_in_full *res_status; 7910 struct scsi_per_res_in_full_desc *res_desc; 7911 struct ctl_port *port; 7912 int i, len; 7913 7914 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7915 7916 /* 7917 * We had to drop the lock to allocate our buffer, which 7918 * leaves time for someone to come in with another 7919 * persistent reservation. (That is unlikely, though, 7920 * since this should be the only persistent reservation 7921 * command active right now.) 7922 */ 7923 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7924 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7925 lun->pr_key_count)){ 7926 mtx_unlock(&lun->lun_lock); 7927 free(ctsio->kern_data_ptr, M_CTL); 7928 printf("%s: reservation length changed, retrying\n", 7929 __func__); 7930 goto retry; 7931 } 7932 7933 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7934 7935 res_desc = &res_status->desc[0]; 7936 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7937 if ((key = ctl_get_prkey(lun, i)) == 0) 7938 continue; 7939 7940 scsi_u64to8b(key, res_desc->res_key.key); 7941 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7942 (lun->pr_res_idx == i || 7943 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7944 res_desc->flags = SPRI_FULL_R_HOLDER; 7945 res_desc->scopetype = lun->res_type; 7946 } 7947 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7948 res_desc->rel_trgt_port_id); 7949 len = 0; 7950 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7951 if (port != NULL) 7952 len = ctl_create_iid(port, 7953 i % CTL_MAX_INIT_PER_PORT, 7954 res_desc->transport_id); 7955 scsi_ulto4b(len, res_desc->additional_length); 7956 res_desc = (struct scsi_per_res_in_full_desc *) 7957 &res_desc->transport_id[len]; 7958 } 7959 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7960 res_status->header.length); 7961 break; 7962 } 7963 default: 7964 /* 7965 * This is a bug, because we just checked for this above, 7966 * and should have returned an error. 7967 */ 7968 panic("Invalid PR type %x", cdb->action); 7969 break; /* NOTREACHED */ 7970 } 7971 mtx_unlock(&lun->lun_lock); 7972 7973 ctl_set_success(ctsio); 7974 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7975 ctsio->be_move_done = ctl_config_move_done; 7976 ctl_datamove((union ctl_io *)ctsio); 7977 return (CTL_RETVAL_COMPLETE); 7978} 7979 7980/* 7981 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7982 * it should return. 7983 */ 7984static int 7985ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7986 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7987 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7988 struct scsi_per_res_out_parms* param) 7989{ 7990 union ctl_ha_msg persis_io; 7991 int i; 7992 7993 mtx_lock(&lun->lun_lock); 7994 if (sa_res_key == 0) { 7995 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7996 /* validate scope and type */ 7997 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7998 SPR_LU_SCOPE) { 7999 mtx_unlock(&lun->lun_lock); 8000 ctl_set_invalid_field(/*ctsio*/ ctsio, 8001 /*sks_valid*/ 1, 8002 /*command*/ 1, 8003 /*field*/ 2, 8004 /*bit_valid*/ 1, 8005 /*bit*/ 4); 8006 ctl_done((union ctl_io *)ctsio); 8007 return (1); 8008 } 8009 8010 if (type>8 || type==2 || type==4 || type==0) { 8011 mtx_unlock(&lun->lun_lock); 8012 ctl_set_invalid_field(/*ctsio*/ ctsio, 8013 /*sks_valid*/ 1, 8014 /*command*/ 1, 8015 /*field*/ 2, 8016 /*bit_valid*/ 1, 8017 /*bit*/ 0); 8018 ctl_done((union ctl_io *)ctsio); 8019 return (1); 8020 } 8021 8022 /* 8023 * Unregister everybody else and build UA for 8024 * them 8025 */ 8026 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8027 if (i == residx || ctl_get_prkey(lun, i) == 0) 8028 continue; 8029 8030 ctl_clr_prkey(lun, i); 8031 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8032 } 8033 lun->pr_key_count = 1; 8034 lun->res_type = type; 8035 if (lun->res_type != SPR_TYPE_WR_EX_AR 8036 && lun->res_type != SPR_TYPE_EX_AC_AR) 8037 lun->pr_res_idx = residx; 8038 lun->PRGeneration++; 8039 mtx_unlock(&lun->lun_lock); 8040 8041 /* send msg to other side */ 8042 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8043 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8044 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8045 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8046 persis_io.pr.pr_info.res_type = type; 8047 memcpy(persis_io.pr.pr_info.sa_res_key, 8048 param->serv_act_res_key, 8049 sizeof(param->serv_act_res_key)); 8050 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8051 sizeof(persis_io.pr), M_WAITOK); 8052 } else { 8053 /* not all registrants */ 8054 mtx_unlock(&lun->lun_lock); 8055 free(ctsio->kern_data_ptr, M_CTL); 8056 ctl_set_invalid_field(ctsio, 8057 /*sks_valid*/ 1, 8058 /*command*/ 0, 8059 /*field*/ 8, 8060 /*bit_valid*/ 0, 8061 /*bit*/ 0); 8062 ctl_done((union ctl_io *)ctsio); 8063 return (1); 8064 } 8065 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8066 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8067 int found = 0; 8068 8069 if (res_key == sa_res_key) { 8070 /* special case */ 8071 /* 8072 * The spec implies this is not good but doesn't 8073 * say what to do. There are two choices either 8074 * generate a res conflict or check condition 8075 * with illegal field in parameter data. Since 8076 * that is what is done when the sa_res_key is 8077 * zero I'll take that approach since this has 8078 * to do with the sa_res_key. 8079 */ 8080 mtx_unlock(&lun->lun_lock); 8081 free(ctsio->kern_data_ptr, M_CTL); 8082 ctl_set_invalid_field(ctsio, 8083 /*sks_valid*/ 1, 8084 /*command*/ 0, 8085 /*field*/ 8, 8086 /*bit_valid*/ 0, 8087 /*bit*/ 0); 8088 ctl_done((union ctl_io *)ctsio); 8089 return (1); 8090 } 8091 8092 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8093 if (ctl_get_prkey(lun, i) != sa_res_key) 8094 continue; 8095 8096 found = 1; 8097 ctl_clr_prkey(lun, i); 8098 lun->pr_key_count--; 8099 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8100 } 8101 if (!found) { 8102 mtx_unlock(&lun->lun_lock); 8103 free(ctsio->kern_data_ptr, M_CTL); 8104 ctl_set_reservation_conflict(ctsio); 8105 ctl_done((union ctl_io *)ctsio); 8106 return (CTL_RETVAL_COMPLETE); 8107 } 8108 lun->PRGeneration++; 8109 mtx_unlock(&lun->lun_lock); 8110 8111 /* send msg to other side */ 8112 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8113 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8114 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8115 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8116 persis_io.pr.pr_info.res_type = type; 8117 memcpy(persis_io.pr.pr_info.sa_res_key, 8118 param->serv_act_res_key, 8119 sizeof(param->serv_act_res_key)); 8120 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8121 sizeof(persis_io.pr), M_WAITOK); 8122 } else { 8123 /* Reserved but not all registrants */ 8124 /* sa_res_key is res holder */ 8125 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 8126 /* validate scope and type */ 8127 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8128 SPR_LU_SCOPE) { 8129 mtx_unlock(&lun->lun_lock); 8130 ctl_set_invalid_field(/*ctsio*/ ctsio, 8131 /*sks_valid*/ 1, 8132 /*command*/ 1, 8133 /*field*/ 2, 8134 /*bit_valid*/ 1, 8135 /*bit*/ 4); 8136 ctl_done((union ctl_io *)ctsio); 8137 return (1); 8138 } 8139 8140 if (type>8 || type==2 || type==4 || type==0) { 8141 mtx_unlock(&lun->lun_lock); 8142 ctl_set_invalid_field(/*ctsio*/ ctsio, 8143 /*sks_valid*/ 1, 8144 /*command*/ 1, 8145 /*field*/ 2, 8146 /*bit_valid*/ 1, 8147 /*bit*/ 0); 8148 ctl_done((union ctl_io *)ctsio); 8149 return (1); 8150 } 8151 8152 /* 8153 * Do the following: 8154 * if sa_res_key != res_key remove all 8155 * registrants w/sa_res_key and generate UA 8156 * for these registrants(Registrations 8157 * Preempted) if it wasn't an exclusive 8158 * reservation generate UA(Reservations 8159 * Preempted) for all other registered nexuses 8160 * if the type has changed. Establish the new 8161 * reservation and holder. If res_key and 8162 * sa_res_key are the same do the above 8163 * except don't unregister the res holder. 8164 */ 8165 8166 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8167 if (i == residx || ctl_get_prkey(lun, i) == 0) 8168 continue; 8169 8170 if (sa_res_key == ctl_get_prkey(lun, i)) { 8171 ctl_clr_prkey(lun, i); 8172 lun->pr_key_count--; 8173 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8174 } else if (type != lun->res_type 8175 && (lun->res_type == SPR_TYPE_WR_EX_RO 8176 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8177 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8178 } 8179 } 8180 lun->res_type = type; 8181 if (lun->res_type != SPR_TYPE_WR_EX_AR 8182 && lun->res_type != SPR_TYPE_EX_AC_AR) 8183 lun->pr_res_idx = residx; 8184 else 8185 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8186 lun->PRGeneration++; 8187 mtx_unlock(&lun->lun_lock); 8188 8189 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8190 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8191 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8192 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8193 persis_io.pr.pr_info.res_type = type; 8194 memcpy(persis_io.pr.pr_info.sa_res_key, 8195 param->serv_act_res_key, 8196 sizeof(param->serv_act_res_key)); 8197 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8198 sizeof(persis_io.pr), M_WAITOK); 8199 } else { 8200 /* 8201 * sa_res_key is not the res holder just 8202 * remove registrants 8203 */ 8204 int found=0; 8205 8206 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8207 if (sa_res_key != ctl_get_prkey(lun, i)) 8208 continue; 8209 8210 found = 1; 8211 ctl_clr_prkey(lun, i); 8212 lun->pr_key_count--; 8213 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8214 } 8215 8216 if (!found) { 8217 mtx_unlock(&lun->lun_lock); 8218 free(ctsio->kern_data_ptr, M_CTL); 8219 ctl_set_reservation_conflict(ctsio); 8220 ctl_done((union ctl_io *)ctsio); 8221 return (1); 8222 } 8223 lun->PRGeneration++; 8224 mtx_unlock(&lun->lun_lock); 8225 8226 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8227 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8228 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8229 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8230 persis_io.pr.pr_info.res_type = type; 8231 memcpy(persis_io.pr.pr_info.sa_res_key, 8232 param->serv_act_res_key, 8233 sizeof(param->serv_act_res_key)); 8234 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8235 sizeof(persis_io.pr), M_WAITOK); 8236 } 8237 } 8238 return (0); 8239} 8240 8241static void 8242ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8243{ 8244 uint64_t sa_res_key; 8245 int i; 8246 8247 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8248 8249 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8250 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8251 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8252 if (sa_res_key == 0) { 8253 /* 8254 * Unregister everybody else and build UA for 8255 * them 8256 */ 8257 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8258 if (i == msg->pr.pr_info.residx || 8259 ctl_get_prkey(lun, i) == 0) 8260 continue; 8261 8262 ctl_clr_prkey(lun, i); 8263 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8264 } 8265 8266 lun->pr_key_count = 1; 8267 lun->res_type = msg->pr.pr_info.res_type; 8268 if (lun->res_type != SPR_TYPE_WR_EX_AR 8269 && lun->res_type != SPR_TYPE_EX_AC_AR) 8270 lun->pr_res_idx = msg->pr.pr_info.residx; 8271 } else { 8272 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8273 if (sa_res_key == ctl_get_prkey(lun, i)) 8274 continue; 8275 8276 ctl_clr_prkey(lun, i); 8277 lun->pr_key_count--; 8278 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8279 } 8280 } 8281 } else { 8282 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8283 if (i == msg->pr.pr_info.residx || 8284 ctl_get_prkey(lun, i) == 0) 8285 continue; 8286 8287 if (sa_res_key == ctl_get_prkey(lun, i)) { 8288 ctl_clr_prkey(lun, i); 8289 lun->pr_key_count--; 8290 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8291 } else if (msg->pr.pr_info.res_type != lun->res_type 8292 && (lun->res_type == SPR_TYPE_WR_EX_RO 8293 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8294 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8295 } 8296 } 8297 lun->res_type = msg->pr.pr_info.res_type; 8298 if (lun->res_type != SPR_TYPE_WR_EX_AR 8299 && lun->res_type != SPR_TYPE_EX_AC_AR) 8300 lun->pr_res_idx = msg->pr.pr_info.residx; 8301 else 8302 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8303 } 8304 lun->PRGeneration++; 8305 8306} 8307 8308 8309int 8310ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8311{ 8312 int retval; 8313 u_int32_t param_len; 8314 struct scsi_per_res_out *cdb; 8315 struct ctl_lun *lun; 8316 struct scsi_per_res_out_parms* param; 8317 struct ctl_softc *softc; 8318 uint32_t residx; 8319 uint64_t res_key, sa_res_key, key; 8320 uint8_t type; 8321 union ctl_ha_msg persis_io; 8322 int i; 8323 8324 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8325 8326 retval = CTL_RETVAL_COMPLETE; 8327 8328 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8329 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8330 softc = lun->ctl_softc; 8331 8332 /* 8333 * We only support whole-LUN scope. The scope & type are ignored for 8334 * register, register and ignore existing key and clear. 8335 * We sometimes ignore scope and type on preempts too!! 8336 * Verify reservation type here as well. 8337 */ 8338 type = cdb->scope_type & SPR_TYPE_MASK; 8339 if ((cdb->action == SPRO_RESERVE) 8340 || (cdb->action == SPRO_RELEASE)) { 8341 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8342 ctl_set_invalid_field(/*ctsio*/ ctsio, 8343 /*sks_valid*/ 1, 8344 /*command*/ 1, 8345 /*field*/ 2, 8346 /*bit_valid*/ 1, 8347 /*bit*/ 4); 8348 ctl_done((union ctl_io *)ctsio); 8349 return (CTL_RETVAL_COMPLETE); 8350 } 8351 8352 if (type>8 || type==2 || type==4 || type==0) { 8353 ctl_set_invalid_field(/*ctsio*/ ctsio, 8354 /*sks_valid*/ 1, 8355 /*command*/ 1, 8356 /*field*/ 2, 8357 /*bit_valid*/ 1, 8358 /*bit*/ 0); 8359 ctl_done((union ctl_io *)ctsio); 8360 return (CTL_RETVAL_COMPLETE); 8361 } 8362 } 8363 8364 param_len = scsi_4btoul(cdb->length); 8365 8366 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8367 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8368 ctsio->kern_data_len = param_len; 8369 ctsio->kern_total_len = param_len; 8370 ctsio->kern_data_resid = 0; 8371 ctsio->kern_rel_offset = 0; 8372 ctsio->kern_sg_entries = 0; 8373 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8374 ctsio->be_move_done = ctl_config_move_done; 8375 ctl_datamove((union ctl_io *)ctsio); 8376 8377 return (CTL_RETVAL_COMPLETE); 8378 } 8379 8380 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8381 8382 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8383 res_key = scsi_8btou64(param->res_key.key); 8384 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8385 8386 /* 8387 * Validate the reservation key here except for SPRO_REG_IGNO 8388 * This must be done for all other service actions 8389 */ 8390 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8391 mtx_lock(&lun->lun_lock); 8392 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8393 if (res_key != key) { 8394 /* 8395 * The current key passed in doesn't match 8396 * the one the initiator previously 8397 * registered. 8398 */ 8399 mtx_unlock(&lun->lun_lock); 8400 free(ctsio->kern_data_ptr, M_CTL); 8401 ctl_set_reservation_conflict(ctsio); 8402 ctl_done((union ctl_io *)ctsio); 8403 return (CTL_RETVAL_COMPLETE); 8404 } 8405 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8406 /* 8407 * We are not registered 8408 */ 8409 mtx_unlock(&lun->lun_lock); 8410 free(ctsio->kern_data_ptr, M_CTL); 8411 ctl_set_reservation_conflict(ctsio); 8412 ctl_done((union ctl_io *)ctsio); 8413 return (CTL_RETVAL_COMPLETE); 8414 } else if (res_key != 0) { 8415 /* 8416 * We are not registered and trying to register but 8417 * the register key isn't zero. 8418 */ 8419 mtx_unlock(&lun->lun_lock); 8420 free(ctsio->kern_data_ptr, M_CTL); 8421 ctl_set_reservation_conflict(ctsio); 8422 ctl_done((union ctl_io *)ctsio); 8423 return (CTL_RETVAL_COMPLETE); 8424 } 8425 mtx_unlock(&lun->lun_lock); 8426 } 8427 8428 switch (cdb->action & SPRO_ACTION_MASK) { 8429 case SPRO_REGISTER: 8430 case SPRO_REG_IGNO: { 8431 8432#if 0 8433 printf("Registration received\n"); 8434#endif 8435 8436 /* 8437 * We don't support any of these options, as we report in 8438 * the read capabilities request (see 8439 * ctl_persistent_reserve_in(), above). 8440 */ 8441 if ((param->flags & SPR_SPEC_I_PT) 8442 || (param->flags & SPR_ALL_TG_PT) 8443 || (param->flags & SPR_APTPL)) { 8444 int bit_ptr; 8445 8446 if (param->flags & SPR_APTPL) 8447 bit_ptr = 0; 8448 else if (param->flags & SPR_ALL_TG_PT) 8449 bit_ptr = 2; 8450 else /* SPR_SPEC_I_PT */ 8451 bit_ptr = 3; 8452 8453 free(ctsio->kern_data_ptr, M_CTL); 8454 ctl_set_invalid_field(ctsio, 8455 /*sks_valid*/ 1, 8456 /*command*/ 0, 8457 /*field*/ 20, 8458 /*bit_valid*/ 1, 8459 /*bit*/ bit_ptr); 8460 ctl_done((union ctl_io *)ctsio); 8461 return (CTL_RETVAL_COMPLETE); 8462 } 8463 8464 mtx_lock(&lun->lun_lock); 8465 8466 /* 8467 * The initiator wants to clear the 8468 * key/unregister. 8469 */ 8470 if (sa_res_key == 0) { 8471 if ((res_key == 0 8472 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8473 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8474 && ctl_get_prkey(lun, residx) == 0)) { 8475 mtx_unlock(&lun->lun_lock); 8476 goto done; 8477 } 8478 8479 ctl_clr_prkey(lun, residx); 8480 lun->pr_key_count--; 8481 8482 if (residx == lun->pr_res_idx) { 8483 lun->flags &= ~CTL_LUN_PR_RESERVED; 8484 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8485 8486 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8487 || lun->res_type == SPR_TYPE_EX_AC_RO) 8488 && lun->pr_key_count) { 8489 /* 8490 * If the reservation is a registrants 8491 * only type we need to generate a UA 8492 * for other registered inits. The 8493 * sense code should be RESERVATIONS 8494 * RELEASED 8495 */ 8496 8497 for (i = softc->init_min; i < softc->init_max; i++){ 8498 if (ctl_get_prkey(lun, i) == 0) 8499 continue; 8500 ctl_est_ua(lun, i, 8501 CTL_UA_RES_RELEASE); 8502 } 8503 } 8504 lun->res_type = 0; 8505 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8506 if (lun->pr_key_count==0) { 8507 lun->flags &= ~CTL_LUN_PR_RESERVED; 8508 lun->res_type = 0; 8509 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8510 } 8511 } 8512 lun->PRGeneration++; 8513 mtx_unlock(&lun->lun_lock); 8514 8515 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8516 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8517 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8518 persis_io.pr.pr_info.residx = residx; 8519 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8520 sizeof(persis_io.pr), M_WAITOK); 8521 } else /* sa_res_key != 0 */ { 8522 8523 /* 8524 * If we aren't registered currently then increment 8525 * the key count and set the registered flag. 8526 */ 8527 ctl_alloc_prkey(lun, residx); 8528 if (ctl_get_prkey(lun, residx) == 0) 8529 lun->pr_key_count++; 8530 ctl_set_prkey(lun, residx, sa_res_key); 8531 lun->PRGeneration++; 8532 mtx_unlock(&lun->lun_lock); 8533 8534 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8535 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8536 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8537 persis_io.pr.pr_info.residx = residx; 8538 memcpy(persis_io.pr.pr_info.sa_res_key, 8539 param->serv_act_res_key, 8540 sizeof(param->serv_act_res_key)); 8541 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8542 sizeof(persis_io.pr), M_WAITOK); 8543 } 8544 8545 break; 8546 } 8547 case SPRO_RESERVE: 8548#if 0 8549 printf("Reserve executed type %d\n", type); 8550#endif 8551 mtx_lock(&lun->lun_lock); 8552 if (lun->flags & CTL_LUN_PR_RESERVED) { 8553 /* 8554 * if this isn't the reservation holder and it's 8555 * not a "all registrants" type or if the type is 8556 * different then we have a conflict 8557 */ 8558 if ((lun->pr_res_idx != residx 8559 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8560 || lun->res_type != type) { 8561 mtx_unlock(&lun->lun_lock); 8562 free(ctsio->kern_data_ptr, M_CTL); 8563 ctl_set_reservation_conflict(ctsio); 8564 ctl_done((union ctl_io *)ctsio); 8565 return (CTL_RETVAL_COMPLETE); 8566 } 8567 mtx_unlock(&lun->lun_lock); 8568 } else /* create a reservation */ { 8569 /* 8570 * If it's not an "all registrants" type record 8571 * reservation holder 8572 */ 8573 if (type != SPR_TYPE_WR_EX_AR 8574 && type != SPR_TYPE_EX_AC_AR) 8575 lun->pr_res_idx = residx; /* Res holder */ 8576 else 8577 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8578 8579 lun->flags |= CTL_LUN_PR_RESERVED; 8580 lun->res_type = type; 8581 8582 mtx_unlock(&lun->lun_lock); 8583 8584 /* send msg to other side */ 8585 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8586 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8587 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8588 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8589 persis_io.pr.pr_info.res_type = type; 8590 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8591 sizeof(persis_io.pr), M_WAITOK); 8592 } 8593 break; 8594 8595 case SPRO_RELEASE: 8596 mtx_lock(&lun->lun_lock); 8597 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8598 /* No reservation exists return good status */ 8599 mtx_unlock(&lun->lun_lock); 8600 goto done; 8601 } 8602 /* 8603 * Is this nexus a reservation holder? 8604 */ 8605 if (lun->pr_res_idx != residx 8606 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8607 /* 8608 * not a res holder return good status but 8609 * do nothing 8610 */ 8611 mtx_unlock(&lun->lun_lock); 8612 goto done; 8613 } 8614 8615 if (lun->res_type != type) { 8616 mtx_unlock(&lun->lun_lock); 8617 free(ctsio->kern_data_ptr, M_CTL); 8618 ctl_set_illegal_pr_release(ctsio); 8619 ctl_done((union ctl_io *)ctsio); 8620 return (CTL_RETVAL_COMPLETE); 8621 } 8622 8623 /* okay to release */ 8624 lun->flags &= ~CTL_LUN_PR_RESERVED; 8625 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8626 lun->res_type = 0; 8627 8628 /* 8629 * if this isn't an exclusive access 8630 * res generate UA for all other 8631 * registrants. 8632 */ 8633 if (type != SPR_TYPE_EX_AC 8634 && type != SPR_TYPE_WR_EX) { 8635 for (i = softc->init_min; i < softc->init_max; i++) { 8636 if (i == residx || ctl_get_prkey(lun, i) == 0) 8637 continue; 8638 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8639 } 8640 } 8641 mtx_unlock(&lun->lun_lock); 8642 8643 /* Send msg to other side */ 8644 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8645 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8646 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8647 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8648 sizeof(persis_io.pr), M_WAITOK); 8649 break; 8650 8651 case SPRO_CLEAR: 8652 /* send msg to other side */ 8653 8654 mtx_lock(&lun->lun_lock); 8655 lun->flags &= ~CTL_LUN_PR_RESERVED; 8656 lun->res_type = 0; 8657 lun->pr_key_count = 0; 8658 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8659 8660 ctl_clr_prkey(lun, residx); 8661 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8662 if (ctl_get_prkey(lun, i) != 0) { 8663 ctl_clr_prkey(lun, i); 8664 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8665 } 8666 lun->PRGeneration++; 8667 mtx_unlock(&lun->lun_lock); 8668 8669 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8670 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8671 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8672 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8673 sizeof(persis_io.pr), M_WAITOK); 8674 break; 8675 8676 case SPRO_PREEMPT: 8677 case SPRO_PRE_ABO: { 8678 int nretval; 8679 8680 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8681 residx, ctsio, cdb, param); 8682 if (nretval != 0) 8683 return (CTL_RETVAL_COMPLETE); 8684 break; 8685 } 8686 default: 8687 panic("Invalid PR type %x", cdb->action); 8688 } 8689 8690done: 8691 free(ctsio->kern_data_ptr, M_CTL); 8692 ctl_set_success(ctsio); 8693 ctl_done((union ctl_io *)ctsio); 8694 8695 return (retval); 8696} 8697 8698/* 8699 * This routine is for handling a message from the other SC pertaining to 8700 * persistent reserve out. All the error checking will have been done 8701 * so only perorming the action need be done here to keep the two 8702 * in sync. 8703 */ 8704static void 8705ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8706{ 8707 struct ctl_lun *lun; 8708 struct ctl_softc *softc; 8709 int i; 8710 uint32_t residx, targ_lun; 8711 8712 softc = control_softc; 8713 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8714 mtx_lock(&softc->ctl_lock); 8715 if ((targ_lun >= CTL_MAX_LUNS) || 8716 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 8717 mtx_unlock(&softc->ctl_lock); 8718 return; 8719 } 8720 mtx_lock(&lun->lun_lock); 8721 mtx_unlock(&softc->ctl_lock); 8722 if (lun->flags & CTL_LUN_DISABLED) { 8723 mtx_unlock(&lun->lun_lock); 8724 return; 8725 } 8726 residx = ctl_get_initindex(&msg->hdr.nexus); 8727 switch(msg->pr.pr_info.action) { 8728 case CTL_PR_REG_KEY: 8729 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8730 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8731 lun->pr_key_count++; 8732 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8733 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8734 lun->PRGeneration++; 8735 break; 8736 8737 case CTL_PR_UNREG_KEY: 8738 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8739 lun->pr_key_count--; 8740 8741 /* XXX Need to see if the reservation has been released */ 8742 /* if so do we need to generate UA? */ 8743 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8744 lun->flags &= ~CTL_LUN_PR_RESERVED; 8745 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8746 8747 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8748 || lun->res_type == SPR_TYPE_EX_AC_RO) 8749 && lun->pr_key_count) { 8750 /* 8751 * If the reservation is a registrants 8752 * only type we need to generate a UA 8753 * for other registered inits. The 8754 * sense code should be RESERVATIONS 8755 * RELEASED 8756 */ 8757 8758 for (i = softc->init_min; i < softc->init_max; i++) { 8759 if (ctl_get_prkey(lun, i) == 0) 8760 continue; 8761 8762 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8763 } 8764 } 8765 lun->res_type = 0; 8766 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8767 if (lun->pr_key_count==0) { 8768 lun->flags &= ~CTL_LUN_PR_RESERVED; 8769 lun->res_type = 0; 8770 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8771 } 8772 } 8773 lun->PRGeneration++; 8774 break; 8775 8776 case CTL_PR_RESERVE: 8777 lun->flags |= CTL_LUN_PR_RESERVED; 8778 lun->res_type = msg->pr.pr_info.res_type; 8779 lun->pr_res_idx = msg->pr.pr_info.residx; 8780 8781 break; 8782 8783 case CTL_PR_RELEASE: 8784 /* 8785 * if this isn't an exclusive access res generate UA for all 8786 * other registrants. 8787 */ 8788 if (lun->res_type != SPR_TYPE_EX_AC 8789 && lun->res_type != SPR_TYPE_WR_EX) { 8790 for (i = softc->init_min; i < softc->init_max; i++) 8791 if (i == residx || ctl_get_prkey(lun, i) == 0) 8792 continue; 8793 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8794 } 8795 8796 lun->flags &= ~CTL_LUN_PR_RESERVED; 8797 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8798 lun->res_type = 0; 8799 break; 8800 8801 case CTL_PR_PREEMPT: 8802 ctl_pro_preempt_other(lun, msg); 8803 break; 8804 case CTL_PR_CLEAR: 8805 lun->flags &= ~CTL_LUN_PR_RESERVED; 8806 lun->res_type = 0; 8807 lun->pr_key_count = 0; 8808 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8809 8810 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8811 if (ctl_get_prkey(lun, i) == 0) 8812 continue; 8813 ctl_clr_prkey(lun, i); 8814 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8815 } 8816 lun->PRGeneration++; 8817 break; 8818 } 8819 8820 mtx_unlock(&lun->lun_lock); 8821} 8822 8823int 8824ctl_read_write(struct ctl_scsiio *ctsio) 8825{ 8826 struct ctl_lun *lun; 8827 struct ctl_lba_len_flags *lbalen; 8828 uint64_t lba; 8829 uint32_t num_blocks; 8830 int flags, retval; 8831 int isread; 8832 8833 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8834 8835 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8836 8837 flags = 0; 8838 retval = CTL_RETVAL_COMPLETE; 8839 8840 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8841 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8842 switch (ctsio->cdb[0]) { 8843 case READ_6: 8844 case WRITE_6: { 8845 struct scsi_rw_6 *cdb; 8846 8847 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8848 8849 lba = scsi_3btoul(cdb->addr); 8850 /* only 5 bits are valid in the most significant address byte */ 8851 lba &= 0x1fffff; 8852 num_blocks = cdb->length; 8853 /* 8854 * This is correct according to SBC-2. 8855 */ 8856 if (num_blocks == 0) 8857 num_blocks = 256; 8858 break; 8859 } 8860 case READ_10: 8861 case WRITE_10: { 8862 struct scsi_rw_10 *cdb; 8863 8864 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8865 if (cdb->byte2 & SRW10_FUA) 8866 flags |= CTL_LLF_FUA; 8867 if (cdb->byte2 & SRW10_DPO) 8868 flags |= CTL_LLF_DPO; 8869 lba = scsi_4btoul(cdb->addr); 8870 num_blocks = scsi_2btoul(cdb->length); 8871 break; 8872 } 8873 case WRITE_VERIFY_10: { 8874 struct scsi_write_verify_10 *cdb; 8875 8876 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8877 flags |= CTL_LLF_FUA; 8878 if (cdb->byte2 & SWV_DPO) 8879 flags |= CTL_LLF_DPO; 8880 lba = scsi_4btoul(cdb->addr); 8881 num_blocks = scsi_2btoul(cdb->length); 8882 break; 8883 } 8884 case READ_12: 8885 case WRITE_12: { 8886 struct scsi_rw_12 *cdb; 8887 8888 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8889 if (cdb->byte2 & SRW12_FUA) 8890 flags |= CTL_LLF_FUA; 8891 if (cdb->byte2 & SRW12_DPO) 8892 flags |= CTL_LLF_DPO; 8893 lba = scsi_4btoul(cdb->addr); 8894 num_blocks = scsi_4btoul(cdb->length); 8895 break; 8896 } 8897 case WRITE_VERIFY_12: { 8898 struct scsi_write_verify_12 *cdb; 8899 8900 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8901 flags |= CTL_LLF_FUA; 8902 if (cdb->byte2 & SWV_DPO) 8903 flags |= CTL_LLF_DPO; 8904 lba = scsi_4btoul(cdb->addr); 8905 num_blocks = scsi_4btoul(cdb->length); 8906 break; 8907 } 8908 case READ_16: 8909 case WRITE_16: { 8910 struct scsi_rw_16 *cdb; 8911 8912 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8913 if (cdb->byte2 & SRW12_FUA) 8914 flags |= CTL_LLF_FUA; 8915 if (cdb->byte2 & SRW12_DPO) 8916 flags |= CTL_LLF_DPO; 8917 lba = scsi_8btou64(cdb->addr); 8918 num_blocks = scsi_4btoul(cdb->length); 8919 break; 8920 } 8921 case WRITE_ATOMIC_16: { 8922 struct scsi_rw_16 *cdb; 8923 8924 if (lun->be_lun->atomicblock == 0) { 8925 ctl_set_invalid_opcode(ctsio); 8926 ctl_done((union ctl_io *)ctsio); 8927 return (CTL_RETVAL_COMPLETE); 8928 } 8929 8930 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8931 if (cdb->byte2 & SRW12_FUA) 8932 flags |= CTL_LLF_FUA; 8933 if (cdb->byte2 & SRW12_DPO) 8934 flags |= CTL_LLF_DPO; 8935 lba = scsi_8btou64(cdb->addr); 8936 num_blocks = scsi_4btoul(cdb->length); 8937 if (num_blocks > lun->be_lun->atomicblock) { 8938 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8939 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8940 /*bit*/ 0); 8941 ctl_done((union ctl_io *)ctsio); 8942 return (CTL_RETVAL_COMPLETE); 8943 } 8944 break; 8945 } 8946 case WRITE_VERIFY_16: { 8947 struct scsi_write_verify_16 *cdb; 8948 8949 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8950 flags |= CTL_LLF_FUA; 8951 if (cdb->byte2 & SWV_DPO) 8952 flags |= CTL_LLF_DPO; 8953 lba = scsi_8btou64(cdb->addr); 8954 num_blocks = scsi_4btoul(cdb->length); 8955 break; 8956 } 8957 default: 8958 /* 8959 * We got a command we don't support. This shouldn't 8960 * happen, commands should be filtered out above us. 8961 */ 8962 ctl_set_invalid_opcode(ctsio); 8963 ctl_done((union ctl_io *)ctsio); 8964 8965 return (CTL_RETVAL_COMPLETE); 8966 break; /* NOTREACHED */ 8967 } 8968 8969 /* 8970 * The first check is to make sure we're in bounds, the second 8971 * check is to catch wrap-around problems. If the lba + num blocks 8972 * is less than the lba, then we've wrapped around and the block 8973 * range is invalid anyway. 8974 */ 8975 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8976 || ((lba + num_blocks) < lba)) { 8977 ctl_set_lba_out_of_range(ctsio); 8978 ctl_done((union ctl_io *)ctsio); 8979 return (CTL_RETVAL_COMPLETE); 8980 } 8981 8982 /* 8983 * According to SBC-3, a transfer length of 0 is not an error. 8984 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8985 * translates to 256 blocks for those commands. 8986 */ 8987 if (num_blocks == 0) { 8988 ctl_set_success(ctsio); 8989 ctl_done((union ctl_io *)ctsio); 8990 return (CTL_RETVAL_COMPLETE); 8991 } 8992 8993 /* Set FUA and/or DPO if caches are disabled. */ 8994 if (isread) { 8995 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8996 SCP_RCD) != 0) 8997 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8998 } else { 8999 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9000 SCP_WCE) == 0) 9001 flags |= CTL_LLF_FUA; 9002 } 9003 9004 lbalen = (struct ctl_lba_len_flags *) 9005 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9006 lbalen->lba = lba; 9007 lbalen->len = num_blocks; 9008 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9009 9010 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9011 ctsio->kern_rel_offset = 0; 9012 9013 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9014 9015 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9016 9017 return (retval); 9018} 9019 9020static int 9021ctl_cnw_cont(union ctl_io *io) 9022{ 9023 struct ctl_scsiio *ctsio; 9024 struct ctl_lun *lun; 9025 struct ctl_lba_len_flags *lbalen; 9026 int retval; 9027 9028 ctsio = &io->scsiio; 9029 ctsio->io_hdr.status = CTL_STATUS_NONE; 9030 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9031 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9032 lbalen = (struct ctl_lba_len_flags *) 9033 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9034 lbalen->flags &= ~CTL_LLF_COMPARE; 9035 lbalen->flags |= CTL_LLF_WRITE; 9036 9037 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9038 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9039 return (retval); 9040} 9041 9042int 9043ctl_cnw(struct ctl_scsiio *ctsio) 9044{ 9045 struct ctl_lun *lun; 9046 struct ctl_lba_len_flags *lbalen; 9047 uint64_t lba; 9048 uint32_t num_blocks; 9049 int flags, retval; 9050 9051 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9052 9053 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9054 9055 flags = 0; 9056 retval = CTL_RETVAL_COMPLETE; 9057 9058 switch (ctsio->cdb[0]) { 9059 case COMPARE_AND_WRITE: { 9060 struct scsi_compare_and_write *cdb; 9061 9062 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9063 if (cdb->byte2 & SRW10_FUA) 9064 flags |= CTL_LLF_FUA; 9065 if (cdb->byte2 & SRW10_DPO) 9066 flags |= CTL_LLF_DPO; 9067 lba = scsi_8btou64(cdb->addr); 9068 num_blocks = cdb->length; 9069 break; 9070 } 9071 default: 9072 /* 9073 * We got a command we don't support. This shouldn't 9074 * happen, commands should be filtered out above us. 9075 */ 9076 ctl_set_invalid_opcode(ctsio); 9077 ctl_done((union ctl_io *)ctsio); 9078 9079 return (CTL_RETVAL_COMPLETE); 9080 break; /* NOTREACHED */ 9081 } 9082 9083 /* 9084 * The first check is to make sure we're in bounds, the second 9085 * check is to catch wrap-around problems. If the lba + num blocks 9086 * is less than the lba, then we've wrapped around and the block 9087 * range is invalid anyway. 9088 */ 9089 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9090 || ((lba + num_blocks) < lba)) { 9091 ctl_set_lba_out_of_range(ctsio); 9092 ctl_done((union ctl_io *)ctsio); 9093 return (CTL_RETVAL_COMPLETE); 9094 } 9095 9096 /* 9097 * According to SBC-3, a transfer length of 0 is not an error. 9098 */ 9099 if (num_blocks == 0) { 9100 ctl_set_success(ctsio); 9101 ctl_done((union ctl_io *)ctsio); 9102 return (CTL_RETVAL_COMPLETE); 9103 } 9104 9105 /* Set FUA if write cache is disabled. */ 9106 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9107 SCP_WCE) == 0) 9108 flags |= CTL_LLF_FUA; 9109 9110 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9111 ctsio->kern_rel_offset = 0; 9112 9113 /* 9114 * Set the IO_CONT flag, so that if this I/O gets passed to 9115 * ctl_data_submit_done(), it'll get passed back to 9116 * ctl_ctl_cnw_cont() for further processing. 9117 */ 9118 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9119 ctsio->io_cont = ctl_cnw_cont; 9120 9121 lbalen = (struct ctl_lba_len_flags *) 9122 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9123 lbalen->lba = lba; 9124 lbalen->len = num_blocks; 9125 lbalen->flags = CTL_LLF_COMPARE | flags; 9126 9127 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9128 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9129 return (retval); 9130} 9131 9132int 9133ctl_verify(struct ctl_scsiio *ctsio) 9134{ 9135 struct ctl_lun *lun; 9136 struct ctl_lba_len_flags *lbalen; 9137 uint64_t lba; 9138 uint32_t num_blocks; 9139 int bytchk, flags; 9140 int retval; 9141 9142 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9143 9144 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9145 9146 bytchk = 0; 9147 flags = CTL_LLF_FUA; 9148 retval = CTL_RETVAL_COMPLETE; 9149 9150 switch (ctsio->cdb[0]) { 9151 case VERIFY_10: { 9152 struct scsi_verify_10 *cdb; 9153 9154 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9155 if (cdb->byte2 & SVFY_BYTCHK) 9156 bytchk = 1; 9157 if (cdb->byte2 & SVFY_DPO) 9158 flags |= CTL_LLF_DPO; 9159 lba = scsi_4btoul(cdb->addr); 9160 num_blocks = scsi_2btoul(cdb->length); 9161 break; 9162 } 9163 case VERIFY_12: { 9164 struct scsi_verify_12 *cdb; 9165 9166 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9167 if (cdb->byte2 & SVFY_BYTCHK) 9168 bytchk = 1; 9169 if (cdb->byte2 & SVFY_DPO) 9170 flags |= CTL_LLF_DPO; 9171 lba = scsi_4btoul(cdb->addr); 9172 num_blocks = scsi_4btoul(cdb->length); 9173 break; 9174 } 9175 case VERIFY_16: { 9176 struct scsi_rw_16 *cdb; 9177 9178 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9179 if (cdb->byte2 & SVFY_BYTCHK) 9180 bytchk = 1; 9181 if (cdb->byte2 & SVFY_DPO) 9182 flags |= CTL_LLF_DPO; 9183 lba = scsi_8btou64(cdb->addr); 9184 num_blocks = scsi_4btoul(cdb->length); 9185 break; 9186 } 9187 default: 9188 /* 9189 * We got a command we don't support. This shouldn't 9190 * happen, commands should be filtered out above us. 9191 */ 9192 ctl_set_invalid_opcode(ctsio); 9193 ctl_done((union ctl_io *)ctsio); 9194 return (CTL_RETVAL_COMPLETE); 9195 } 9196 9197 /* 9198 * The first check is to make sure we're in bounds, the second 9199 * check is to catch wrap-around problems. If the lba + num blocks 9200 * is less than the lba, then we've wrapped around and the block 9201 * range is invalid anyway. 9202 */ 9203 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9204 || ((lba + num_blocks) < lba)) { 9205 ctl_set_lba_out_of_range(ctsio); 9206 ctl_done((union ctl_io *)ctsio); 9207 return (CTL_RETVAL_COMPLETE); 9208 } 9209 9210 /* 9211 * According to SBC-3, a transfer length of 0 is not an error. 9212 */ 9213 if (num_blocks == 0) { 9214 ctl_set_success(ctsio); 9215 ctl_done((union ctl_io *)ctsio); 9216 return (CTL_RETVAL_COMPLETE); 9217 } 9218 9219 lbalen = (struct ctl_lba_len_flags *) 9220 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9221 lbalen->lba = lba; 9222 lbalen->len = num_blocks; 9223 if (bytchk) { 9224 lbalen->flags = CTL_LLF_COMPARE | flags; 9225 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9226 } else { 9227 lbalen->flags = CTL_LLF_VERIFY | flags; 9228 ctsio->kern_total_len = 0; 9229 } 9230 ctsio->kern_rel_offset = 0; 9231 9232 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9233 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9234 return (retval); 9235} 9236 9237int 9238ctl_report_luns(struct ctl_scsiio *ctsio) 9239{ 9240 struct ctl_softc *softc = control_softc; 9241 struct scsi_report_luns *cdb; 9242 struct scsi_report_luns_data *lun_data; 9243 struct ctl_lun *lun, *request_lun; 9244 struct ctl_port *port; 9245 int num_luns, retval; 9246 uint32_t alloc_len, lun_datalen; 9247 int num_filled, well_known; 9248 uint32_t initidx, targ_lun_id, lun_id; 9249 9250 retval = CTL_RETVAL_COMPLETE; 9251 well_known = 0; 9252 9253 cdb = (struct scsi_report_luns *)ctsio->cdb; 9254 port = ctl_io_port(&ctsio->io_hdr); 9255 9256 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9257 9258 mtx_lock(&softc->ctl_lock); 9259 num_luns = 0; 9260 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 9261 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 9262 num_luns++; 9263 } 9264 mtx_unlock(&softc->ctl_lock); 9265 9266 switch (cdb->select_report) { 9267 case RPL_REPORT_DEFAULT: 9268 case RPL_REPORT_ALL: 9269 break; 9270 case RPL_REPORT_WELLKNOWN: 9271 well_known = 1; 9272 num_luns = 0; 9273 break; 9274 default: 9275 ctl_set_invalid_field(ctsio, 9276 /*sks_valid*/ 1, 9277 /*command*/ 1, 9278 /*field*/ 2, 9279 /*bit_valid*/ 0, 9280 /*bit*/ 0); 9281 ctl_done((union ctl_io *)ctsio); 9282 return (retval); 9283 break; /* NOTREACHED */ 9284 } 9285 9286 alloc_len = scsi_4btoul(cdb->length); 9287 /* 9288 * The initiator has to allocate at least 16 bytes for this request, 9289 * so he can at least get the header and the first LUN. Otherwise 9290 * we reject the request (per SPC-3 rev 14, section 6.21). 9291 */ 9292 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9293 sizeof(struct scsi_report_luns_lundata))) { 9294 ctl_set_invalid_field(ctsio, 9295 /*sks_valid*/ 1, 9296 /*command*/ 1, 9297 /*field*/ 6, 9298 /*bit_valid*/ 0, 9299 /*bit*/ 0); 9300 ctl_done((union ctl_io *)ctsio); 9301 return (retval); 9302 } 9303 9304 request_lun = (struct ctl_lun *) 9305 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9306 9307 lun_datalen = sizeof(*lun_data) + 9308 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9309 9310 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9311 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9312 ctsio->kern_sg_entries = 0; 9313 9314 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9315 9316 mtx_lock(&softc->ctl_lock); 9317 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9318 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9319 if (lun_id >= CTL_MAX_LUNS) 9320 continue; 9321 lun = softc->ctl_luns[lun_id]; 9322 if (lun == NULL) 9323 continue; 9324 9325 if (targ_lun_id <= 0xff) { 9326 /* 9327 * Peripheral addressing method, bus number 0. 9328 */ 9329 lun_data->luns[num_filled].lundata[0] = 9330 RPL_LUNDATA_ATYP_PERIPH; 9331 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9332 num_filled++; 9333 } else if (targ_lun_id <= 0x3fff) { 9334 /* 9335 * Flat addressing method. 9336 */ 9337 lun_data->luns[num_filled].lundata[0] = 9338 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9339 lun_data->luns[num_filled].lundata[1] = 9340 (targ_lun_id & 0xff); 9341 num_filled++; 9342 } else if (targ_lun_id <= 0xffffff) { 9343 /* 9344 * Extended flat addressing method. 9345 */ 9346 lun_data->luns[num_filled].lundata[0] = 9347 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9348 scsi_ulto3b(targ_lun_id, 9349 &lun_data->luns[num_filled].lundata[1]); 9350 num_filled++; 9351 } else { 9352 printf("ctl_report_luns: bogus LUN number %jd, " 9353 "skipping\n", (intmax_t)targ_lun_id); 9354 } 9355 /* 9356 * According to SPC-3, rev 14 section 6.21: 9357 * 9358 * "The execution of a REPORT LUNS command to any valid and 9359 * installed logical unit shall clear the REPORTED LUNS DATA 9360 * HAS CHANGED unit attention condition for all logical 9361 * units of that target with respect to the requesting 9362 * initiator. A valid and installed logical unit is one 9363 * having a PERIPHERAL QUALIFIER of 000b in the standard 9364 * INQUIRY data (see 6.4.2)." 9365 * 9366 * If request_lun is NULL, the LUN this report luns command 9367 * was issued to is either disabled or doesn't exist. In that 9368 * case, we shouldn't clear any pending lun change unit 9369 * attention. 9370 */ 9371 if (request_lun != NULL) { 9372 mtx_lock(&lun->lun_lock); 9373 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9374 mtx_unlock(&lun->lun_lock); 9375 } 9376 } 9377 mtx_unlock(&softc->ctl_lock); 9378 9379 /* 9380 * It's quite possible that we've returned fewer LUNs than we allocated 9381 * space for. Trim it. 9382 */ 9383 lun_datalen = sizeof(*lun_data) + 9384 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9385 9386 if (lun_datalen < alloc_len) { 9387 ctsio->residual = alloc_len - lun_datalen; 9388 ctsio->kern_data_len = lun_datalen; 9389 ctsio->kern_total_len = lun_datalen; 9390 } else { 9391 ctsio->residual = 0; 9392 ctsio->kern_data_len = alloc_len; 9393 ctsio->kern_total_len = alloc_len; 9394 } 9395 ctsio->kern_data_resid = 0; 9396 ctsio->kern_rel_offset = 0; 9397 ctsio->kern_sg_entries = 0; 9398 9399 /* 9400 * We set this to the actual data length, regardless of how much 9401 * space we actually have to return results. If the user looks at 9402 * this value, he'll know whether or not he allocated enough space 9403 * and reissue the command if necessary. We don't support well 9404 * known logical units, so if the user asks for that, return none. 9405 */ 9406 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9407 9408 /* 9409 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9410 * this request. 9411 */ 9412 ctl_set_success(ctsio); 9413 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9414 ctsio->be_move_done = ctl_config_move_done; 9415 ctl_datamove((union ctl_io *)ctsio); 9416 return (retval); 9417} 9418 9419int 9420ctl_request_sense(struct ctl_scsiio *ctsio) 9421{ 9422 struct scsi_request_sense *cdb; 9423 struct scsi_sense_data *sense_ptr; 9424 struct ctl_softc *ctl_softc; 9425 struct ctl_lun *lun; 9426 uint32_t initidx; 9427 int have_error; 9428 scsi_sense_data_type sense_format; 9429 ctl_ua_type ua_type; 9430 9431 cdb = (struct scsi_request_sense *)ctsio->cdb; 9432 9433 ctl_softc = control_softc; 9434 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9435 9436 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9437 9438 /* 9439 * Determine which sense format the user wants. 9440 */ 9441 if (cdb->byte2 & SRS_DESC) 9442 sense_format = SSD_TYPE_DESC; 9443 else 9444 sense_format = SSD_TYPE_FIXED; 9445 9446 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9447 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9448 ctsio->kern_sg_entries = 0; 9449 9450 /* 9451 * struct scsi_sense_data, which is currently set to 256 bytes, is 9452 * larger than the largest allowed value for the length field in the 9453 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9454 */ 9455 ctsio->residual = 0; 9456 ctsio->kern_data_len = cdb->length; 9457 ctsio->kern_total_len = cdb->length; 9458 9459 ctsio->kern_data_resid = 0; 9460 ctsio->kern_rel_offset = 0; 9461 ctsio->kern_sg_entries = 0; 9462 9463 /* 9464 * If we don't have a LUN, we don't have any pending sense. 9465 */ 9466 if (lun == NULL) 9467 goto no_sense; 9468 9469 have_error = 0; 9470 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9471 /* 9472 * Check for pending sense, and then for pending unit attentions. 9473 * Pending sense gets returned first, then pending unit attentions. 9474 */ 9475 mtx_lock(&lun->lun_lock); 9476#ifdef CTL_WITH_CA 9477 if (ctl_is_set(lun->have_ca, initidx)) { 9478 scsi_sense_data_type stored_format; 9479 9480 /* 9481 * Check to see which sense format was used for the stored 9482 * sense data. 9483 */ 9484 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9485 9486 /* 9487 * If the user requested a different sense format than the 9488 * one we stored, then we need to convert it to the other 9489 * format. If we're going from descriptor to fixed format 9490 * sense data, we may lose things in translation, depending 9491 * on what options were used. 9492 * 9493 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9494 * for some reason we'll just copy it out as-is. 9495 */ 9496 if ((stored_format == SSD_TYPE_FIXED) 9497 && (sense_format == SSD_TYPE_DESC)) 9498 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9499 &lun->pending_sense[initidx], 9500 (struct scsi_sense_data_desc *)sense_ptr); 9501 else if ((stored_format == SSD_TYPE_DESC) 9502 && (sense_format == SSD_TYPE_FIXED)) 9503 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9504 &lun->pending_sense[initidx], 9505 (struct scsi_sense_data_fixed *)sense_ptr); 9506 else 9507 memcpy(sense_ptr, &lun->pending_sense[initidx], 9508 MIN(sizeof(*sense_ptr), 9509 sizeof(lun->pending_sense[initidx]))); 9510 9511 ctl_clear_mask(lun->have_ca, initidx); 9512 have_error = 1; 9513 } else 9514#endif 9515 { 9516 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9517 if (ua_type != CTL_UA_NONE) 9518 have_error = 1; 9519 if (ua_type == CTL_UA_LUN_CHANGE) { 9520 mtx_unlock(&lun->lun_lock); 9521 mtx_lock(&ctl_softc->ctl_lock); 9522 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type); 9523 mtx_unlock(&ctl_softc->ctl_lock); 9524 mtx_lock(&lun->lun_lock); 9525 } 9526 9527 } 9528 mtx_unlock(&lun->lun_lock); 9529 9530 /* 9531 * We already have a pending error, return it. 9532 */ 9533 if (have_error != 0) { 9534 /* 9535 * We report the SCSI status as OK, since the status of the 9536 * request sense command itself is OK. 9537 * We report 0 for the sense length, because we aren't doing 9538 * autosense in this case. We're reporting sense as 9539 * parameter data. 9540 */ 9541 ctl_set_success(ctsio); 9542 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9543 ctsio->be_move_done = ctl_config_move_done; 9544 ctl_datamove((union ctl_io *)ctsio); 9545 return (CTL_RETVAL_COMPLETE); 9546 } 9547 9548no_sense: 9549 9550 /* 9551 * No sense information to report, so we report that everything is 9552 * okay. 9553 */ 9554 ctl_set_sense_data(sense_ptr, 9555 lun, 9556 sense_format, 9557 /*current_error*/ 1, 9558 /*sense_key*/ SSD_KEY_NO_SENSE, 9559 /*asc*/ 0x00, 9560 /*ascq*/ 0x00, 9561 SSD_ELEM_NONE); 9562 9563 /* 9564 * We report 0 for the sense length, because we aren't doing 9565 * autosense in this case. We're reporting sense as parameter data. 9566 */ 9567 ctl_set_success(ctsio); 9568 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9569 ctsio->be_move_done = ctl_config_move_done; 9570 ctl_datamove((union ctl_io *)ctsio); 9571 return (CTL_RETVAL_COMPLETE); 9572} 9573 9574int 9575ctl_tur(struct ctl_scsiio *ctsio) 9576{ 9577 9578 CTL_DEBUG_PRINT(("ctl_tur\n")); 9579 9580 ctl_set_success(ctsio); 9581 ctl_done((union ctl_io *)ctsio); 9582 9583 return (CTL_RETVAL_COMPLETE); 9584} 9585 9586/* 9587 * SCSI VPD page 0x00, the Supported VPD Pages page. 9588 */ 9589static int 9590ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9591{ 9592 struct scsi_vpd_supported_pages *pages; 9593 int sup_page_size; 9594 struct ctl_lun *lun; 9595 int p; 9596 9597 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9598 9599 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9600 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9601 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9602 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9603 ctsio->kern_sg_entries = 0; 9604 9605 if (sup_page_size < alloc_len) { 9606 ctsio->residual = alloc_len - sup_page_size; 9607 ctsio->kern_data_len = sup_page_size; 9608 ctsio->kern_total_len = sup_page_size; 9609 } else { 9610 ctsio->residual = 0; 9611 ctsio->kern_data_len = alloc_len; 9612 ctsio->kern_total_len = alloc_len; 9613 } 9614 ctsio->kern_data_resid = 0; 9615 ctsio->kern_rel_offset = 0; 9616 ctsio->kern_sg_entries = 0; 9617 9618 /* 9619 * The control device is always connected. The disk device, on the 9620 * other hand, may not be online all the time. Need to change this 9621 * to figure out whether the disk device is actually online or not. 9622 */ 9623 if (lun != NULL) 9624 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9625 lun->be_lun->lun_type; 9626 else 9627 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9628 9629 p = 0; 9630 /* Supported VPD pages */ 9631 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9632 /* Serial Number */ 9633 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9634 /* Device Identification */ 9635 pages->page_list[p++] = SVPD_DEVICE_ID; 9636 /* Extended INQUIRY Data */ 9637 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9638 /* Mode Page Policy */ 9639 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9640 /* SCSI Ports */ 9641 pages->page_list[p++] = SVPD_SCSI_PORTS; 9642 /* Third-party Copy */ 9643 pages->page_list[p++] = SVPD_SCSI_TPC; 9644 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9645 /* Block limits */ 9646 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9647 /* Block Device Characteristics */ 9648 pages->page_list[p++] = SVPD_BDC; 9649 /* Logical Block Provisioning */ 9650 pages->page_list[p++] = SVPD_LBP; 9651 } 9652 pages->length = p; 9653 9654 ctl_set_success(ctsio); 9655 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9656 ctsio->be_move_done = ctl_config_move_done; 9657 ctl_datamove((union ctl_io *)ctsio); 9658 return (CTL_RETVAL_COMPLETE); 9659} 9660 9661/* 9662 * SCSI VPD page 0x80, the Unit Serial Number page. 9663 */ 9664static int 9665ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9666{ 9667 struct scsi_vpd_unit_serial_number *sn_ptr; 9668 struct ctl_lun *lun; 9669 int data_len; 9670 9671 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9672 9673 data_len = 4 + CTL_SN_LEN; 9674 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9675 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9676 if (data_len < alloc_len) { 9677 ctsio->residual = alloc_len - data_len; 9678 ctsio->kern_data_len = data_len; 9679 ctsio->kern_total_len = data_len; 9680 } else { 9681 ctsio->residual = 0; 9682 ctsio->kern_data_len = alloc_len; 9683 ctsio->kern_total_len = alloc_len; 9684 } 9685 ctsio->kern_data_resid = 0; 9686 ctsio->kern_rel_offset = 0; 9687 ctsio->kern_sg_entries = 0; 9688 9689 /* 9690 * The control device is always connected. The disk device, on the 9691 * other hand, may not be online all the time. Need to change this 9692 * to figure out whether the disk device is actually online or not. 9693 */ 9694 if (lun != NULL) 9695 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9696 lun->be_lun->lun_type; 9697 else 9698 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9699 9700 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9701 sn_ptr->length = CTL_SN_LEN; 9702 /* 9703 * If we don't have a LUN, we just leave the serial number as 9704 * all spaces. 9705 */ 9706 if (lun != NULL) { 9707 strncpy((char *)sn_ptr->serial_num, 9708 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9709 } else 9710 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9711 9712 ctl_set_success(ctsio); 9713 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9714 ctsio->be_move_done = ctl_config_move_done; 9715 ctl_datamove((union ctl_io *)ctsio); 9716 return (CTL_RETVAL_COMPLETE); 9717} 9718 9719 9720/* 9721 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9722 */ 9723static int 9724ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9725{ 9726 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9727 struct ctl_lun *lun; 9728 int data_len; 9729 9730 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9731 9732 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9733 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9734 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9735 ctsio->kern_sg_entries = 0; 9736 9737 if (data_len < alloc_len) { 9738 ctsio->residual = alloc_len - data_len; 9739 ctsio->kern_data_len = data_len; 9740 ctsio->kern_total_len = data_len; 9741 } else { 9742 ctsio->residual = 0; 9743 ctsio->kern_data_len = alloc_len; 9744 ctsio->kern_total_len = alloc_len; 9745 } 9746 ctsio->kern_data_resid = 0; 9747 ctsio->kern_rel_offset = 0; 9748 ctsio->kern_sg_entries = 0; 9749 9750 /* 9751 * The control device is always connected. The disk device, on the 9752 * other hand, may not be online all the time. 9753 */ 9754 if (lun != NULL) 9755 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9756 lun->be_lun->lun_type; 9757 else 9758 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9759 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9760 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9761 /* 9762 * We support head of queue, ordered and simple tags. 9763 */ 9764 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9765 /* 9766 * Volatile cache supported. 9767 */ 9768 eid_ptr->flags3 = SVPD_EID_V_SUP; 9769 9770 /* 9771 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9772 * attention for a particular IT nexus on all LUNs once we report 9773 * it to that nexus once. This bit is required as of SPC-4. 9774 */ 9775 eid_ptr->flags4 = SVPD_EID_LUICLT; 9776 9777 /* 9778 * XXX KDM in order to correctly answer this, we would need 9779 * information from the SIM to determine how much sense data it 9780 * can send. So this would really be a path inquiry field, most 9781 * likely. This can be set to a maximum of 252 according to SPC-4, 9782 * but the hardware may or may not be able to support that much. 9783 * 0 just means that the maximum sense data length is not reported. 9784 */ 9785 eid_ptr->max_sense_length = 0; 9786 9787 ctl_set_success(ctsio); 9788 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9789 ctsio->be_move_done = ctl_config_move_done; 9790 ctl_datamove((union ctl_io *)ctsio); 9791 return (CTL_RETVAL_COMPLETE); 9792} 9793 9794static int 9795ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9796{ 9797 struct scsi_vpd_mode_page_policy *mpp_ptr; 9798 struct ctl_lun *lun; 9799 int data_len; 9800 9801 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9802 9803 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9804 sizeof(struct scsi_vpd_mode_page_policy_descr); 9805 9806 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9807 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9808 ctsio->kern_sg_entries = 0; 9809 9810 if (data_len < alloc_len) { 9811 ctsio->residual = alloc_len - data_len; 9812 ctsio->kern_data_len = data_len; 9813 ctsio->kern_total_len = data_len; 9814 } else { 9815 ctsio->residual = 0; 9816 ctsio->kern_data_len = alloc_len; 9817 ctsio->kern_total_len = alloc_len; 9818 } 9819 ctsio->kern_data_resid = 0; 9820 ctsio->kern_rel_offset = 0; 9821 ctsio->kern_sg_entries = 0; 9822 9823 /* 9824 * The control device is always connected. The disk device, on the 9825 * other hand, may not be online all the time. 9826 */ 9827 if (lun != NULL) 9828 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9829 lun->be_lun->lun_type; 9830 else 9831 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9832 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9833 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9834 mpp_ptr->descr[0].page_code = 0x3f; 9835 mpp_ptr->descr[0].subpage_code = 0xff; 9836 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9837 9838 ctl_set_success(ctsio); 9839 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9840 ctsio->be_move_done = ctl_config_move_done; 9841 ctl_datamove((union ctl_io *)ctsio); 9842 return (CTL_RETVAL_COMPLETE); 9843} 9844 9845/* 9846 * SCSI VPD page 0x83, the Device Identification page. 9847 */ 9848static int 9849ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9850{ 9851 struct scsi_vpd_device_id *devid_ptr; 9852 struct scsi_vpd_id_descriptor *desc; 9853 struct ctl_softc *softc; 9854 struct ctl_lun *lun; 9855 struct ctl_port *port; 9856 int data_len; 9857 uint8_t proto; 9858 9859 softc = control_softc; 9860 9861 port = ctl_io_port(&ctsio->io_hdr); 9862 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9863 9864 data_len = sizeof(struct scsi_vpd_device_id) + 9865 sizeof(struct scsi_vpd_id_descriptor) + 9866 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9867 sizeof(struct scsi_vpd_id_descriptor) + 9868 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9869 if (lun && lun->lun_devid) 9870 data_len += lun->lun_devid->len; 9871 if (port && port->port_devid) 9872 data_len += port->port_devid->len; 9873 if (port && port->target_devid) 9874 data_len += port->target_devid->len; 9875 9876 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9877 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9878 ctsio->kern_sg_entries = 0; 9879 9880 if (data_len < alloc_len) { 9881 ctsio->residual = alloc_len - data_len; 9882 ctsio->kern_data_len = data_len; 9883 ctsio->kern_total_len = data_len; 9884 } else { 9885 ctsio->residual = 0; 9886 ctsio->kern_data_len = alloc_len; 9887 ctsio->kern_total_len = alloc_len; 9888 } 9889 ctsio->kern_data_resid = 0; 9890 ctsio->kern_rel_offset = 0; 9891 ctsio->kern_sg_entries = 0; 9892 9893 /* 9894 * The control device is always connected. The disk device, on the 9895 * other hand, may not be online all the time. 9896 */ 9897 if (lun != NULL) 9898 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9899 lun->be_lun->lun_type; 9900 else 9901 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9902 devid_ptr->page_code = SVPD_DEVICE_ID; 9903 scsi_ulto2b(data_len - 4, devid_ptr->length); 9904 9905 if (port && port->port_type == CTL_PORT_FC) 9906 proto = SCSI_PROTO_FC << 4; 9907 else if (port && port->port_type == CTL_PORT_ISCSI) 9908 proto = SCSI_PROTO_ISCSI << 4; 9909 else 9910 proto = SCSI_PROTO_SPI << 4; 9911 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9912 9913 /* 9914 * We're using a LUN association here. i.e., this device ID is a 9915 * per-LUN identifier. 9916 */ 9917 if (lun && lun->lun_devid) { 9918 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9919 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9920 lun->lun_devid->len); 9921 } 9922 9923 /* 9924 * This is for the WWPN which is a port association. 9925 */ 9926 if (port && port->port_devid) { 9927 memcpy(desc, port->port_devid->data, port->port_devid->len); 9928 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9929 port->port_devid->len); 9930 } 9931 9932 /* 9933 * This is for the Relative Target Port(type 4h) identifier 9934 */ 9935 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9936 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9937 SVPD_ID_TYPE_RELTARG; 9938 desc->length = 4; 9939 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9940 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9941 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9942 9943 /* 9944 * This is for the Target Port Group(type 5h) identifier 9945 */ 9946 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9947 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9948 SVPD_ID_TYPE_TPORTGRP; 9949 desc->length = 4; 9950 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1, 9951 &desc->identifier[2]); 9952 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9953 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9954 9955 /* 9956 * This is for the Target identifier 9957 */ 9958 if (port && port->target_devid) { 9959 memcpy(desc, port->target_devid->data, port->target_devid->len); 9960 } 9961 9962 ctl_set_success(ctsio); 9963 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9964 ctsio->be_move_done = ctl_config_move_done; 9965 ctl_datamove((union ctl_io *)ctsio); 9966 return (CTL_RETVAL_COMPLETE); 9967} 9968 9969static int 9970ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9971{ 9972 struct ctl_softc *softc = control_softc; 9973 struct scsi_vpd_scsi_ports *sp; 9974 struct scsi_vpd_port_designation *pd; 9975 struct scsi_vpd_port_designation_cont *pdc; 9976 struct ctl_lun *lun; 9977 struct ctl_port *port; 9978 int data_len, num_target_ports, iid_len, id_len; 9979 9980 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9981 9982 num_target_ports = 0; 9983 iid_len = 0; 9984 id_len = 0; 9985 mtx_lock(&softc->ctl_lock); 9986 STAILQ_FOREACH(port, &softc->port_list, links) { 9987 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9988 continue; 9989 if (lun != NULL && 9990 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9991 continue; 9992 num_target_ports++; 9993 if (port->init_devid) 9994 iid_len += port->init_devid->len; 9995 if (port->port_devid) 9996 id_len += port->port_devid->len; 9997 } 9998 mtx_unlock(&softc->ctl_lock); 9999 10000 data_len = sizeof(struct scsi_vpd_scsi_ports) + 10001 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10002 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10003 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10004 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10005 ctsio->kern_sg_entries = 0; 10006 10007 if (data_len < alloc_len) { 10008 ctsio->residual = alloc_len - data_len; 10009 ctsio->kern_data_len = data_len; 10010 ctsio->kern_total_len = data_len; 10011 } else { 10012 ctsio->residual = 0; 10013 ctsio->kern_data_len = alloc_len; 10014 ctsio->kern_total_len = alloc_len; 10015 } 10016 ctsio->kern_data_resid = 0; 10017 ctsio->kern_rel_offset = 0; 10018 ctsio->kern_sg_entries = 0; 10019 10020 /* 10021 * The control device is always connected. The disk device, on the 10022 * other hand, may not be online all the time. Need to change this 10023 * to figure out whether the disk device is actually online or not. 10024 */ 10025 if (lun != NULL) 10026 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10027 lun->be_lun->lun_type; 10028 else 10029 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10030 10031 sp->page_code = SVPD_SCSI_PORTS; 10032 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10033 sp->page_length); 10034 pd = &sp->design[0]; 10035 10036 mtx_lock(&softc->ctl_lock); 10037 STAILQ_FOREACH(port, &softc->port_list, links) { 10038 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10039 continue; 10040 if (lun != NULL && 10041 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 10042 continue; 10043 scsi_ulto2b(port->targ_port, pd->relative_port_id); 10044 if (port->init_devid) { 10045 iid_len = port->init_devid->len; 10046 memcpy(pd->initiator_transportid, 10047 port->init_devid->data, port->init_devid->len); 10048 } else 10049 iid_len = 0; 10050 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10051 pdc = (struct scsi_vpd_port_designation_cont *) 10052 (&pd->initiator_transportid[iid_len]); 10053 if (port->port_devid) { 10054 id_len = port->port_devid->len; 10055 memcpy(pdc->target_port_descriptors, 10056 port->port_devid->data, port->port_devid->len); 10057 } else 10058 id_len = 0; 10059 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10060 pd = (struct scsi_vpd_port_designation *) 10061 ((uint8_t *)pdc->target_port_descriptors + id_len); 10062 } 10063 mtx_unlock(&softc->ctl_lock); 10064 10065 ctl_set_success(ctsio); 10066 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10067 ctsio->be_move_done = ctl_config_move_done; 10068 ctl_datamove((union ctl_io *)ctsio); 10069 return (CTL_RETVAL_COMPLETE); 10070} 10071 10072static int 10073ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10074{ 10075 struct scsi_vpd_block_limits *bl_ptr; 10076 struct ctl_lun *lun; 10077 int bs; 10078 10079 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10080 10081 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10082 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10083 ctsio->kern_sg_entries = 0; 10084 10085 if (sizeof(*bl_ptr) < alloc_len) { 10086 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10087 ctsio->kern_data_len = sizeof(*bl_ptr); 10088 ctsio->kern_total_len = sizeof(*bl_ptr); 10089 } else { 10090 ctsio->residual = 0; 10091 ctsio->kern_data_len = alloc_len; 10092 ctsio->kern_total_len = alloc_len; 10093 } 10094 ctsio->kern_data_resid = 0; 10095 ctsio->kern_rel_offset = 0; 10096 ctsio->kern_sg_entries = 0; 10097 10098 /* 10099 * The control device is always connected. The disk device, on the 10100 * other hand, may not be online all the time. Need to change this 10101 * to figure out whether the disk device is actually online or not. 10102 */ 10103 if (lun != NULL) 10104 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10105 lun->be_lun->lun_type; 10106 else 10107 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10108 10109 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10110 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10111 bl_ptr->max_cmp_write_len = 0xff; 10112 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10113 if (lun != NULL) { 10114 bs = lun->be_lun->blocksize; 10115 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 10116 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10117 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10118 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10119 if (lun->be_lun->ublockexp != 0) { 10120 scsi_ulto4b((1 << lun->be_lun->ublockexp), 10121 bl_ptr->opt_unmap_grain); 10122 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 10123 bl_ptr->unmap_grain_align); 10124 } 10125 } 10126 scsi_ulto4b(lun->be_lun->atomicblock, 10127 bl_ptr->max_atomic_transfer_length); 10128 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10129 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10130 } 10131 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10132 10133 ctl_set_success(ctsio); 10134 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10135 ctsio->be_move_done = ctl_config_move_done; 10136 ctl_datamove((union ctl_io *)ctsio); 10137 return (CTL_RETVAL_COMPLETE); 10138} 10139 10140static int 10141ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10142{ 10143 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10144 struct ctl_lun *lun; 10145 const char *value; 10146 u_int i; 10147 10148 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10149 10150 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10151 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10152 ctsio->kern_sg_entries = 0; 10153 10154 if (sizeof(*bdc_ptr) < alloc_len) { 10155 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10156 ctsio->kern_data_len = sizeof(*bdc_ptr); 10157 ctsio->kern_total_len = sizeof(*bdc_ptr); 10158 } else { 10159 ctsio->residual = 0; 10160 ctsio->kern_data_len = alloc_len; 10161 ctsio->kern_total_len = alloc_len; 10162 } 10163 ctsio->kern_data_resid = 0; 10164 ctsio->kern_rel_offset = 0; 10165 ctsio->kern_sg_entries = 0; 10166 10167 /* 10168 * The control device is always connected. The disk device, on the 10169 * other hand, may not be online all the time. Need to change this 10170 * to figure out whether the disk device is actually online or not. 10171 */ 10172 if (lun != NULL) 10173 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10174 lun->be_lun->lun_type; 10175 else 10176 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10177 bdc_ptr->page_code = SVPD_BDC; 10178 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10179 if (lun != NULL && 10180 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10181 i = strtol(value, NULL, 0); 10182 else 10183 i = CTL_DEFAULT_ROTATION_RATE; 10184 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10185 if (lun != NULL && 10186 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10187 i = strtol(value, NULL, 0); 10188 else 10189 i = 0; 10190 bdc_ptr->wab_wac_ff = (i & 0x0f); 10191 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10192 10193 ctl_set_success(ctsio); 10194 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10195 ctsio->be_move_done = ctl_config_move_done; 10196 ctl_datamove((union ctl_io *)ctsio); 10197 return (CTL_RETVAL_COMPLETE); 10198} 10199 10200static int 10201ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10202{ 10203 struct scsi_vpd_logical_block_prov *lbp_ptr; 10204 struct ctl_lun *lun; 10205 10206 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10207 10208 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10209 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10210 ctsio->kern_sg_entries = 0; 10211 10212 if (sizeof(*lbp_ptr) < alloc_len) { 10213 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10214 ctsio->kern_data_len = sizeof(*lbp_ptr); 10215 ctsio->kern_total_len = sizeof(*lbp_ptr); 10216 } else { 10217 ctsio->residual = 0; 10218 ctsio->kern_data_len = alloc_len; 10219 ctsio->kern_total_len = alloc_len; 10220 } 10221 ctsio->kern_data_resid = 0; 10222 ctsio->kern_rel_offset = 0; 10223 ctsio->kern_sg_entries = 0; 10224 10225 /* 10226 * The control device is always connected. The disk device, on the 10227 * other hand, may not be online all the time. Need to change this 10228 * to figure out whether the disk device is actually online or not. 10229 */ 10230 if (lun != NULL) 10231 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10232 lun->be_lun->lun_type; 10233 else 10234 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10235 10236 lbp_ptr->page_code = SVPD_LBP; 10237 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10238 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10239 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10240 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10241 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10242 lbp_ptr->prov_type = SVPD_LBP_THIN; 10243 } 10244 10245 ctl_set_success(ctsio); 10246 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10247 ctsio->be_move_done = ctl_config_move_done; 10248 ctl_datamove((union ctl_io *)ctsio); 10249 return (CTL_RETVAL_COMPLETE); 10250} 10251 10252/* 10253 * INQUIRY with the EVPD bit set. 10254 */ 10255static int 10256ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10257{ 10258 struct ctl_lun *lun; 10259 struct scsi_inquiry *cdb; 10260 int alloc_len, retval; 10261 10262 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10263 cdb = (struct scsi_inquiry *)ctsio->cdb; 10264 alloc_len = scsi_2btoul(cdb->length); 10265 10266 switch (cdb->page_code) { 10267 case SVPD_SUPPORTED_PAGES: 10268 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10269 break; 10270 case SVPD_UNIT_SERIAL_NUMBER: 10271 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10272 break; 10273 case SVPD_DEVICE_ID: 10274 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10275 break; 10276 case SVPD_EXTENDED_INQUIRY_DATA: 10277 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10278 break; 10279 case SVPD_MODE_PAGE_POLICY: 10280 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10281 break; 10282 case SVPD_SCSI_PORTS: 10283 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10284 break; 10285 case SVPD_SCSI_TPC: 10286 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10287 break; 10288 case SVPD_BLOCK_LIMITS: 10289 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10290 goto err; 10291 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10292 break; 10293 case SVPD_BDC: 10294 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10295 goto err; 10296 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10297 break; 10298 case SVPD_LBP: 10299 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10300 goto err; 10301 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10302 break; 10303 default: 10304err: 10305 ctl_set_invalid_field(ctsio, 10306 /*sks_valid*/ 1, 10307 /*command*/ 1, 10308 /*field*/ 2, 10309 /*bit_valid*/ 0, 10310 /*bit*/ 0); 10311 ctl_done((union ctl_io *)ctsio); 10312 retval = CTL_RETVAL_COMPLETE; 10313 break; 10314 } 10315 10316 return (retval); 10317} 10318 10319/* 10320 * Standard INQUIRY data. 10321 */ 10322static int 10323ctl_inquiry_std(struct ctl_scsiio *ctsio) 10324{ 10325 struct scsi_inquiry_data *inq_ptr; 10326 struct scsi_inquiry *cdb; 10327 struct ctl_softc *softc; 10328 struct ctl_port *port; 10329 struct ctl_lun *lun; 10330 char *val; 10331 uint32_t alloc_len, data_len; 10332 ctl_port_type port_type; 10333 10334 softc = control_softc; 10335 10336 /* 10337 * Figure out whether we're talking to a Fibre Channel port or not. 10338 * We treat the ioctl front end, and any SCSI adapters, as packetized 10339 * SCSI front ends. 10340 */ 10341 port = ctl_io_port(&ctsio->io_hdr); 10342 if (port != NULL) 10343 port_type = port->port_type; 10344 else 10345 port_type = CTL_PORT_SCSI; 10346 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10347 port_type = CTL_PORT_SCSI; 10348 10349 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10350 cdb = (struct scsi_inquiry *)ctsio->cdb; 10351 alloc_len = scsi_2btoul(cdb->length); 10352 10353 /* 10354 * We malloc the full inquiry data size here and fill it 10355 * in. If the user only asks for less, we'll give him 10356 * that much. 10357 */ 10358 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10359 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10360 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10361 ctsio->kern_sg_entries = 0; 10362 ctsio->kern_data_resid = 0; 10363 ctsio->kern_rel_offset = 0; 10364 10365 if (data_len < alloc_len) { 10366 ctsio->residual = alloc_len - data_len; 10367 ctsio->kern_data_len = data_len; 10368 ctsio->kern_total_len = data_len; 10369 } else { 10370 ctsio->residual = 0; 10371 ctsio->kern_data_len = alloc_len; 10372 ctsio->kern_total_len = alloc_len; 10373 } 10374 10375 if (lun != NULL) { 10376 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10377 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10378 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10379 lun->be_lun->lun_type; 10380 } else { 10381 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10382 lun->be_lun->lun_type; 10383 } 10384 } else 10385 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10386 10387 /* RMB in byte 2 is 0 */ 10388 inq_ptr->version = SCSI_REV_SPC4; 10389 10390 /* 10391 * According to SAM-3, even if a device only supports a single 10392 * level of LUN addressing, it should still set the HISUP bit: 10393 * 10394 * 4.9.1 Logical unit numbers overview 10395 * 10396 * All logical unit number formats described in this standard are 10397 * hierarchical in structure even when only a single level in that 10398 * hierarchy is used. The HISUP bit shall be set to one in the 10399 * standard INQUIRY data (see SPC-2) when any logical unit number 10400 * format described in this standard is used. Non-hierarchical 10401 * formats are outside the scope of this standard. 10402 * 10403 * Therefore we set the HiSup bit here. 10404 * 10405 * The reponse format is 2, per SPC-3. 10406 */ 10407 inq_ptr->response_format = SID_HiSup | 2; 10408 10409 inq_ptr->additional_length = data_len - 10410 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10411 CTL_DEBUG_PRINT(("additional_length = %d\n", 10412 inq_ptr->additional_length)); 10413 10414 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10415 /* 16 bit addressing */ 10416 if (port_type == CTL_PORT_SCSI) 10417 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10418 /* XXX set the SID_MultiP bit here if we're actually going to 10419 respond on multiple ports */ 10420 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10421 10422 /* 16 bit data bus, synchronous transfers */ 10423 if (port_type == CTL_PORT_SCSI) 10424 inq_ptr->flags = SID_WBus16 | SID_Sync; 10425 /* 10426 * XXX KDM do we want to support tagged queueing on the control 10427 * device at all? 10428 */ 10429 if ((lun == NULL) 10430 || (lun->be_lun->lun_type != T_PROCESSOR)) 10431 inq_ptr->flags |= SID_CmdQue; 10432 /* 10433 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10434 * We have 8 bytes for the vendor name, and 16 bytes for the device 10435 * name and 4 bytes for the revision. 10436 */ 10437 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10438 "vendor")) == NULL) { 10439 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10440 } else { 10441 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10442 strncpy(inq_ptr->vendor, val, 10443 min(sizeof(inq_ptr->vendor), strlen(val))); 10444 } 10445 if (lun == NULL) { 10446 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10447 sizeof(inq_ptr->product)); 10448 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10449 switch (lun->be_lun->lun_type) { 10450 case T_DIRECT: 10451 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10452 sizeof(inq_ptr->product)); 10453 break; 10454 case T_PROCESSOR: 10455 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10456 sizeof(inq_ptr->product)); 10457 break; 10458 default: 10459 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10460 sizeof(inq_ptr->product)); 10461 break; 10462 } 10463 } else { 10464 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10465 strncpy(inq_ptr->product, val, 10466 min(sizeof(inq_ptr->product), strlen(val))); 10467 } 10468 10469 /* 10470 * XXX make this a macro somewhere so it automatically gets 10471 * incremented when we make changes. 10472 */ 10473 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10474 "revision")) == NULL) { 10475 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10476 } else { 10477 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10478 strncpy(inq_ptr->revision, val, 10479 min(sizeof(inq_ptr->revision), strlen(val))); 10480 } 10481 10482 /* 10483 * For parallel SCSI, we support double transition and single 10484 * transition clocking. We also support QAS (Quick Arbitration 10485 * and Selection) and Information Unit transfers on both the 10486 * control and array devices. 10487 */ 10488 if (port_type == CTL_PORT_SCSI) 10489 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10490 SID_SPI_IUS; 10491 10492 /* SAM-5 (no version claimed) */ 10493 scsi_ulto2b(0x00A0, inq_ptr->version1); 10494 /* SPC-4 (no version claimed) */ 10495 scsi_ulto2b(0x0460, inq_ptr->version2); 10496 if (port_type == CTL_PORT_FC) { 10497 /* FCP-2 ANSI INCITS.350:2003 */ 10498 scsi_ulto2b(0x0917, inq_ptr->version3); 10499 } else if (port_type == CTL_PORT_SCSI) { 10500 /* SPI-4 ANSI INCITS.362:200x */ 10501 scsi_ulto2b(0x0B56, inq_ptr->version3); 10502 } else if (port_type == CTL_PORT_ISCSI) { 10503 /* iSCSI (no version claimed) */ 10504 scsi_ulto2b(0x0960, inq_ptr->version3); 10505 } else if (port_type == CTL_PORT_SAS) { 10506 /* SAS (no version claimed) */ 10507 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10508 } 10509 10510 if (lun == NULL) { 10511 /* SBC-4 (no version claimed) */ 10512 scsi_ulto2b(0x0600, inq_ptr->version4); 10513 } else { 10514 switch (lun->be_lun->lun_type) { 10515 case T_DIRECT: 10516 /* SBC-4 (no version claimed) */ 10517 scsi_ulto2b(0x0600, inq_ptr->version4); 10518 break; 10519 case T_PROCESSOR: 10520 default: 10521 break; 10522 } 10523 } 10524 10525 ctl_set_success(ctsio); 10526 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10527 ctsio->be_move_done = ctl_config_move_done; 10528 ctl_datamove((union ctl_io *)ctsio); 10529 return (CTL_RETVAL_COMPLETE); 10530} 10531 10532int 10533ctl_inquiry(struct ctl_scsiio *ctsio) 10534{ 10535 struct scsi_inquiry *cdb; 10536 int retval; 10537 10538 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10539 10540 cdb = (struct scsi_inquiry *)ctsio->cdb; 10541 if (cdb->byte2 & SI_EVPD) 10542 retval = ctl_inquiry_evpd(ctsio); 10543 else if (cdb->page_code == 0) 10544 retval = ctl_inquiry_std(ctsio); 10545 else { 10546 ctl_set_invalid_field(ctsio, 10547 /*sks_valid*/ 1, 10548 /*command*/ 1, 10549 /*field*/ 2, 10550 /*bit_valid*/ 0, 10551 /*bit*/ 0); 10552 ctl_done((union ctl_io *)ctsio); 10553 return (CTL_RETVAL_COMPLETE); 10554 } 10555 10556 return (retval); 10557} 10558 10559/* 10560 * For known CDB types, parse the LBA and length. 10561 */ 10562static int 10563ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10564{ 10565 if (io->io_hdr.io_type != CTL_IO_SCSI) 10566 return (1); 10567 10568 switch (io->scsiio.cdb[0]) { 10569 case COMPARE_AND_WRITE: { 10570 struct scsi_compare_and_write *cdb; 10571 10572 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10573 10574 *lba = scsi_8btou64(cdb->addr); 10575 *len = cdb->length; 10576 break; 10577 } 10578 case READ_6: 10579 case WRITE_6: { 10580 struct scsi_rw_6 *cdb; 10581 10582 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10583 10584 *lba = scsi_3btoul(cdb->addr); 10585 /* only 5 bits are valid in the most significant address byte */ 10586 *lba &= 0x1fffff; 10587 *len = cdb->length; 10588 break; 10589 } 10590 case READ_10: 10591 case WRITE_10: { 10592 struct scsi_rw_10 *cdb; 10593 10594 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10595 10596 *lba = scsi_4btoul(cdb->addr); 10597 *len = scsi_2btoul(cdb->length); 10598 break; 10599 } 10600 case WRITE_VERIFY_10: { 10601 struct scsi_write_verify_10 *cdb; 10602 10603 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10604 10605 *lba = scsi_4btoul(cdb->addr); 10606 *len = scsi_2btoul(cdb->length); 10607 break; 10608 } 10609 case READ_12: 10610 case WRITE_12: { 10611 struct scsi_rw_12 *cdb; 10612 10613 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10614 10615 *lba = scsi_4btoul(cdb->addr); 10616 *len = scsi_4btoul(cdb->length); 10617 break; 10618 } 10619 case WRITE_VERIFY_12: { 10620 struct scsi_write_verify_12 *cdb; 10621 10622 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10623 10624 *lba = scsi_4btoul(cdb->addr); 10625 *len = scsi_4btoul(cdb->length); 10626 break; 10627 } 10628 case READ_16: 10629 case WRITE_16: 10630 case WRITE_ATOMIC_16: { 10631 struct scsi_rw_16 *cdb; 10632 10633 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10634 10635 *lba = scsi_8btou64(cdb->addr); 10636 *len = scsi_4btoul(cdb->length); 10637 break; 10638 } 10639 case WRITE_VERIFY_16: { 10640 struct scsi_write_verify_16 *cdb; 10641 10642 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10643 10644 *lba = scsi_8btou64(cdb->addr); 10645 *len = scsi_4btoul(cdb->length); 10646 break; 10647 } 10648 case WRITE_SAME_10: { 10649 struct scsi_write_same_10 *cdb; 10650 10651 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10652 10653 *lba = scsi_4btoul(cdb->addr); 10654 *len = scsi_2btoul(cdb->length); 10655 break; 10656 } 10657 case WRITE_SAME_16: { 10658 struct scsi_write_same_16 *cdb; 10659 10660 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10661 10662 *lba = scsi_8btou64(cdb->addr); 10663 *len = scsi_4btoul(cdb->length); 10664 break; 10665 } 10666 case VERIFY_10: { 10667 struct scsi_verify_10 *cdb; 10668 10669 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10670 10671 *lba = scsi_4btoul(cdb->addr); 10672 *len = scsi_2btoul(cdb->length); 10673 break; 10674 } 10675 case VERIFY_12: { 10676 struct scsi_verify_12 *cdb; 10677 10678 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10679 10680 *lba = scsi_4btoul(cdb->addr); 10681 *len = scsi_4btoul(cdb->length); 10682 break; 10683 } 10684 case VERIFY_16: { 10685 struct scsi_verify_16 *cdb; 10686 10687 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10688 10689 *lba = scsi_8btou64(cdb->addr); 10690 *len = scsi_4btoul(cdb->length); 10691 break; 10692 } 10693 case UNMAP: { 10694 *lba = 0; 10695 *len = UINT64_MAX; 10696 break; 10697 } 10698 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10699 struct scsi_get_lba_status *cdb; 10700 10701 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10702 *lba = scsi_8btou64(cdb->addr); 10703 *len = UINT32_MAX; 10704 break; 10705 } 10706 default: 10707 return (1); 10708 break; /* NOTREACHED */ 10709 } 10710 10711 return (0); 10712} 10713 10714static ctl_action 10715ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10716 bool seq) 10717{ 10718 uint64_t endlba1, endlba2; 10719 10720 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10721 endlba2 = lba2 + len2 - 1; 10722 10723 if ((endlba1 < lba2) || (endlba2 < lba1)) 10724 return (CTL_ACTION_PASS); 10725 else 10726 return (CTL_ACTION_BLOCK); 10727} 10728 10729static int 10730ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10731{ 10732 struct ctl_ptr_len_flags *ptrlen; 10733 struct scsi_unmap_desc *buf, *end, *range; 10734 uint64_t lba; 10735 uint32_t len; 10736 10737 /* If not UNMAP -- go other way. */ 10738 if (io->io_hdr.io_type != CTL_IO_SCSI || 10739 io->scsiio.cdb[0] != UNMAP) 10740 return (CTL_ACTION_ERROR); 10741 10742 /* If UNMAP without data -- block and wait for data. */ 10743 ptrlen = (struct ctl_ptr_len_flags *) 10744 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10745 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10746 ptrlen->ptr == NULL) 10747 return (CTL_ACTION_BLOCK); 10748 10749 /* UNMAP with data -- check for collision. */ 10750 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10751 end = buf + ptrlen->len / sizeof(*buf); 10752 for (range = buf; range < end; range++) { 10753 lba = scsi_8btou64(range->lba); 10754 len = scsi_4btoul(range->length); 10755 if ((lba < lba2 + len2) && (lba + len > lba2)) 10756 return (CTL_ACTION_BLOCK); 10757 } 10758 return (CTL_ACTION_PASS); 10759} 10760 10761static ctl_action 10762ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10763{ 10764 uint64_t lba1, lba2; 10765 uint64_t len1, len2; 10766 int retval; 10767 10768 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10769 return (CTL_ACTION_ERROR); 10770 10771 retval = ctl_extent_check_unmap(io1, lba2, len2); 10772 if (retval != CTL_ACTION_ERROR) 10773 return (retval); 10774 10775 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10776 return (CTL_ACTION_ERROR); 10777 10778 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10779 seq = FALSE; 10780 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10781} 10782 10783static ctl_action 10784ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10785{ 10786 uint64_t lba1, lba2; 10787 uint64_t len1, len2; 10788 10789 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10790 return (CTL_ACTION_PASS); 10791 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10792 return (CTL_ACTION_ERROR); 10793 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10794 return (CTL_ACTION_ERROR); 10795 10796 if (lba1 + len1 == lba2) 10797 return (CTL_ACTION_BLOCK); 10798 return (CTL_ACTION_PASS); 10799} 10800 10801static ctl_action 10802ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10803 union ctl_io *ooa_io) 10804{ 10805 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10806 ctl_serialize_action *serialize_row; 10807 10808 /* 10809 * The initiator attempted multiple untagged commands at the same 10810 * time. Can't do that. 10811 */ 10812 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10813 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10814 && ((pending_io->io_hdr.nexus.targ_port == 10815 ooa_io->io_hdr.nexus.targ_port) 10816 && (pending_io->io_hdr.nexus.initid == 10817 ooa_io->io_hdr.nexus.initid)) 10818 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10819 CTL_FLAG_STATUS_SENT)) == 0)) 10820 return (CTL_ACTION_OVERLAP); 10821 10822 /* 10823 * The initiator attempted to send multiple tagged commands with 10824 * the same ID. (It's fine if different initiators have the same 10825 * tag ID.) 10826 * 10827 * Even if all of those conditions are true, we don't kill the I/O 10828 * if the command ahead of us has been aborted. We won't end up 10829 * sending it to the FETD, and it's perfectly legal to resend a 10830 * command with the same tag number as long as the previous 10831 * instance of this tag number has been aborted somehow. 10832 */ 10833 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10834 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10835 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10836 && ((pending_io->io_hdr.nexus.targ_port == 10837 ooa_io->io_hdr.nexus.targ_port) 10838 && (pending_io->io_hdr.nexus.initid == 10839 ooa_io->io_hdr.nexus.initid)) 10840 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10841 CTL_FLAG_STATUS_SENT)) == 0)) 10842 return (CTL_ACTION_OVERLAP_TAG); 10843 10844 /* 10845 * If we get a head of queue tag, SAM-3 says that we should 10846 * immediately execute it. 10847 * 10848 * What happens if this command would normally block for some other 10849 * reason? e.g. a request sense with a head of queue tag 10850 * immediately after a write. Normally that would block, but this 10851 * will result in its getting executed immediately... 10852 * 10853 * We currently return "pass" instead of "skip", so we'll end up 10854 * going through the rest of the queue to check for overlapped tags. 10855 * 10856 * XXX KDM check for other types of blockage first?? 10857 */ 10858 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10859 return (CTL_ACTION_PASS); 10860 10861 /* 10862 * Ordered tags have to block until all items ahead of them 10863 * have completed. If we get called with an ordered tag, we always 10864 * block, if something else is ahead of us in the queue. 10865 */ 10866 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10867 return (CTL_ACTION_BLOCK); 10868 10869 /* 10870 * Simple tags get blocked until all head of queue and ordered tags 10871 * ahead of them have completed. I'm lumping untagged commands in 10872 * with simple tags here. XXX KDM is that the right thing to do? 10873 */ 10874 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10875 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10876 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10877 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10878 return (CTL_ACTION_BLOCK); 10879 10880 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10881 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10882 10883 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10884 10885 switch (serialize_row[pending_entry->seridx]) { 10886 case CTL_SER_BLOCK: 10887 return (CTL_ACTION_BLOCK); 10888 case CTL_SER_EXTENT: 10889 return (ctl_extent_check(ooa_io, pending_io, 10890 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10891 case CTL_SER_EXTENTOPT: 10892 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10893 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10894 return (ctl_extent_check(ooa_io, pending_io, 10895 (lun->be_lun && 10896 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10897 return (CTL_ACTION_PASS); 10898 case CTL_SER_EXTENTSEQ: 10899 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 10900 return (ctl_extent_check_seq(ooa_io, pending_io)); 10901 return (CTL_ACTION_PASS); 10902 case CTL_SER_PASS: 10903 return (CTL_ACTION_PASS); 10904 case CTL_SER_BLOCKOPT: 10905 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10906 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10907 return (CTL_ACTION_BLOCK); 10908 return (CTL_ACTION_PASS); 10909 case CTL_SER_SKIP: 10910 return (CTL_ACTION_SKIP); 10911 default: 10912 panic("invalid serialization value %d", 10913 serialize_row[pending_entry->seridx]); 10914 } 10915 10916 return (CTL_ACTION_ERROR); 10917} 10918 10919/* 10920 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10921 * Assumptions: 10922 * - pending_io is generally either incoming, or on the blocked queue 10923 * - starting I/O is the I/O we want to start the check with. 10924 */ 10925static ctl_action 10926ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10927 union ctl_io *starting_io) 10928{ 10929 union ctl_io *ooa_io; 10930 ctl_action action; 10931 10932 mtx_assert(&lun->lun_lock, MA_OWNED); 10933 10934 /* 10935 * Run back along the OOA queue, starting with the current 10936 * blocked I/O and going through every I/O before it on the 10937 * queue. If starting_io is NULL, we'll just end up returning 10938 * CTL_ACTION_PASS. 10939 */ 10940 for (ooa_io = starting_io; ooa_io != NULL; 10941 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10942 ooa_links)){ 10943 10944 /* 10945 * This routine just checks to see whether 10946 * cur_blocked is blocked by ooa_io, which is ahead 10947 * of it in the queue. It doesn't queue/dequeue 10948 * cur_blocked. 10949 */ 10950 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10951 switch (action) { 10952 case CTL_ACTION_BLOCK: 10953 case CTL_ACTION_OVERLAP: 10954 case CTL_ACTION_OVERLAP_TAG: 10955 case CTL_ACTION_SKIP: 10956 case CTL_ACTION_ERROR: 10957 return (action); 10958 break; /* NOTREACHED */ 10959 case CTL_ACTION_PASS: 10960 break; 10961 default: 10962 panic("invalid action %d", action); 10963 break; /* NOTREACHED */ 10964 } 10965 } 10966 10967 return (CTL_ACTION_PASS); 10968} 10969 10970/* 10971 * Assumptions: 10972 * - An I/O has just completed, and has been removed from the per-LUN OOA 10973 * queue, so some items on the blocked queue may now be unblocked. 10974 */ 10975static int 10976ctl_check_blocked(struct ctl_lun *lun) 10977{ 10978 struct ctl_softc *softc = lun->ctl_softc; 10979 union ctl_io *cur_blocked, *next_blocked; 10980 10981 mtx_assert(&lun->lun_lock, MA_OWNED); 10982 10983 /* 10984 * Run forward from the head of the blocked queue, checking each 10985 * entry against the I/Os prior to it on the OOA queue to see if 10986 * there is still any blockage. 10987 * 10988 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10989 * with our removing a variable on it while it is traversing the 10990 * list. 10991 */ 10992 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10993 cur_blocked != NULL; cur_blocked = next_blocked) { 10994 union ctl_io *prev_ooa; 10995 ctl_action action; 10996 10997 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10998 blocked_links); 10999 11000 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11001 ctl_ooaq, ooa_links); 11002 11003 /* 11004 * If cur_blocked happens to be the first item in the OOA 11005 * queue now, prev_ooa will be NULL, and the action 11006 * returned will just be CTL_ACTION_PASS. 11007 */ 11008 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11009 11010 switch (action) { 11011 case CTL_ACTION_BLOCK: 11012 /* Nothing to do here, still blocked */ 11013 break; 11014 case CTL_ACTION_OVERLAP: 11015 case CTL_ACTION_OVERLAP_TAG: 11016 /* 11017 * This shouldn't happen! In theory we've already 11018 * checked this command for overlap... 11019 */ 11020 break; 11021 case CTL_ACTION_PASS: 11022 case CTL_ACTION_SKIP: { 11023 const struct ctl_cmd_entry *entry; 11024 11025 /* 11026 * The skip case shouldn't happen, this transaction 11027 * should have never made it onto the blocked queue. 11028 */ 11029 /* 11030 * This I/O is no longer blocked, we can remove it 11031 * from the blocked queue. Since this is a TAILQ 11032 * (doubly linked list), we can do O(1) removals 11033 * from any place on the list. 11034 */ 11035 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11036 blocked_links); 11037 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11038 11039 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11040 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11041 /* 11042 * Need to send IO back to original side to 11043 * run 11044 */ 11045 union ctl_ha_msg msg_info; 11046 11047 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11048 msg_info.hdr.original_sc = 11049 cur_blocked->io_hdr.original_sc; 11050 msg_info.hdr.serializing_sc = cur_blocked; 11051 msg_info.hdr.msg_type = CTL_MSG_R2R; 11052 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11053 sizeof(msg_info.hdr), M_NOWAIT); 11054 break; 11055 } 11056 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11057 11058 /* 11059 * Check this I/O for LUN state changes that may 11060 * have happened while this command was blocked. 11061 * The LUN state may have been changed by a command 11062 * ahead of us in the queue, so we need to re-check 11063 * for any states that can be caused by SCSI 11064 * commands. 11065 */ 11066 if (ctl_scsiio_lun_check(lun, entry, 11067 &cur_blocked->scsiio) == 0) { 11068 cur_blocked->io_hdr.flags |= 11069 CTL_FLAG_IS_WAS_ON_RTR; 11070 ctl_enqueue_rtr(cur_blocked); 11071 } else 11072 ctl_done(cur_blocked); 11073 break; 11074 } 11075 default: 11076 /* 11077 * This probably shouldn't happen -- we shouldn't 11078 * get CTL_ACTION_ERROR, or anything else. 11079 */ 11080 break; 11081 } 11082 } 11083 11084 return (CTL_RETVAL_COMPLETE); 11085} 11086 11087/* 11088 * This routine (with one exception) checks LUN flags that can be set by 11089 * commands ahead of us in the OOA queue. These flags have to be checked 11090 * when a command initially comes in, and when we pull a command off the 11091 * blocked queue and are preparing to execute it. The reason we have to 11092 * check these flags for commands on the blocked queue is that the LUN 11093 * state may have been changed by a command ahead of us while we're on the 11094 * blocked queue. 11095 * 11096 * Ordering is somewhat important with these checks, so please pay 11097 * careful attention to the placement of any new checks. 11098 */ 11099static int 11100ctl_scsiio_lun_check(struct ctl_lun *lun, 11101 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11102{ 11103 struct ctl_softc *softc = lun->ctl_softc; 11104 int retval; 11105 uint32_t residx; 11106 11107 retval = 0; 11108 11109 mtx_assert(&lun->lun_lock, MA_OWNED); 11110 11111 /* 11112 * If this shelf is a secondary shelf controller, we may have to 11113 * reject some commands disallowed by HA mode and link state. 11114 */ 11115 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11116 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11117 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11118 ctl_set_lun_unavail(ctsio); 11119 retval = 1; 11120 goto bailout; 11121 } 11122 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11123 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11124 ctl_set_lun_transit(ctsio); 11125 retval = 1; 11126 goto bailout; 11127 } 11128 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11129 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11130 ctl_set_lun_standby(ctsio); 11131 retval = 1; 11132 goto bailout; 11133 } 11134 11135 /* The rest of checks are only done on executing side */ 11136 if (softc->ha_mode == CTL_HA_MODE_XFER) 11137 goto bailout; 11138 } 11139 11140 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11141 if (lun->be_lun && 11142 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11143 ctl_set_hw_write_protected(ctsio); 11144 retval = 1; 11145 goto bailout; 11146 } 11147 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11148 .eca_and_aen & SCP_SWP) != 0) { 11149 ctl_set_sense(ctsio, /*current_error*/ 1, 11150 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11151 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11152 retval = 1; 11153 goto bailout; 11154 } 11155 } 11156 11157 /* 11158 * Check for a reservation conflict. If this command isn't allowed 11159 * even on reserved LUNs, and if this initiator isn't the one who 11160 * reserved us, reject the command with a reservation conflict. 11161 */ 11162 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11163 if ((lun->flags & CTL_LUN_RESERVED) 11164 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11165 if (lun->res_idx != residx) { 11166 ctl_set_reservation_conflict(ctsio); 11167 retval = 1; 11168 goto bailout; 11169 } 11170 } 11171 11172 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11173 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11174 /* No reservation or command is allowed. */; 11175 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11176 (lun->res_type == SPR_TYPE_WR_EX || 11177 lun->res_type == SPR_TYPE_WR_EX_RO || 11178 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11179 /* The command is allowed for Write Exclusive resv. */; 11180 } else { 11181 /* 11182 * if we aren't registered or it's a res holder type 11183 * reservation and this isn't the res holder then set a 11184 * conflict. 11185 */ 11186 if (ctl_get_prkey(lun, residx) == 0 11187 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11188 ctl_set_reservation_conflict(ctsio); 11189 retval = 1; 11190 goto bailout; 11191 } 11192 } 11193 11194 if ((lun->flags & CTL_LUN_OFFLINE) 11195 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0)) { 11196 ctl_set_lun_not_ready(ctsio); 11197 retval = 1; 11198 goto bailout; 11199 } 11200 11201 if ((lun->flags & CTL_LUN_STOPPED) 11202 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11203 /* "Logical unit not ready, initializing cmd. required" */ 11204 ctl_set_lun_stopped(ctsio); 11205 retval = 1; 11206 goto bailout; 11207 } 11208 11209 if ((lun->flags & CTL_LUN_INOPERABLE) 11210 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11211 /* "Medium format corrupted" */ 11212 ctl_set_medium_format_corrupted(ctsio); 11213 retval = 1; 11214 goto bailout; 11215 } 11216 11217bailout: 11218 return (retval); 11219} 11220 11221static void 11222ctl_failover_io(union ctl_io *io, int have_lock) 11223{ 11224 ctl_set_busy(&io->scsiio); 11225 ctl_done(io); 11226} 11227 11228static void 11229ctl_failover_lun(struct ctl_lun *lun) 11230{ 11231 struct ctl_softc *softc = lun->ctl_softc; 11232 struct ctl_io_hdr *io, *next_io; 11233 11234 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", lun->lun)); 11235 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11236 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11237 /* We are master */ 11238 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11239 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11240 io->flags |= CTL_FLAG_ABORT; 11241 io->flags |= CTL_FLAG_FAILOVER; 11242 } else { /* This can be only due to DATAMOVE */ 11243 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11244 io->flags &= ~CTL_FLAG_DMA_INPROG; 11245 io->flags |= CTL_FLAG_IO_ACTIVE; 11246 io->port_status = 31340; 11247 ctl_enqueue_isc((union ctl_io *)io); 11248 } 11249 } 11250 /* We are slave */ 11251 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11252 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11253 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11254 io->flags |= CTL_FLAG_FAILOVER; 11255 } else { 11256 ctl_set_busy(&((union ctl_io *)io)-> 11257 scsiio); 11258 ctl_done((union ctl_io *)io); 11259 } 11260 } 11261 } 11262 } else { /* SERIALIZE modes */ 11263 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11264 next_io) { 11265 /* We are master */ 11266 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11267 TAILQ_REMOVE(&lun->blocked_queue, io, 11268 blocked_links); 11269 io->flags &= ~CTL_FLAG_BLOCKED; 11270 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11271 ctl_free_io((union ctl_io *)io); 11272 } 11273 } 11274 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11275 /* We are master */ 11276 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11277 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11278 ctl_free_io((union ctl_io *)io); 11279 } 11280 /* We are slave */ 11281 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11282 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11283 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11284 ctl_set_busy(&((union ctl_io *)io)-> 11285 scsiio); 11286 ctl_done((union ctl_io *)io); 11287 } 11288 } 11289 } 11290 ctl_check_blocked(lun); 11291 } 11292} 11293 11294static int 11295ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11296{ 11297 struct ctl_lun *lun; 11298 const struct ctl_cmd_entry *entry; 11299 uint32_t initidx, targ_lun; 11300 int retval; 11301 11302 retval = 0; 11303 11304 lun = NULL; 11305 11306 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11307 if ((targ_lun < CTL_MAX_LUNS) 11308 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11309 /* 11310 * If the LUN is invalid, pretend that it doesn't exist. 11311 * It will go away as soon as all pending I/O has been 11312 * completed. 11313 */ 11314 mtx_lock(&lun->lun_lock); 11315 if (lun->flags & CTL_LUN_DISABLED) { 11316 mtx_unlock(&lun->lun_lock); 11317 lun = NULL; 11318 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11319 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11320 } else { 11321 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11322 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11323 lun->be_lun; 11324 11325 /* 11326 * Every I/O goes into the OOA queue for a 11327 * particular LUN, and stays there until completion. 11328 */ 11329#ifdef CTL_TIME_IO 11330 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11331 lun->idle_time += getsbinuptime() - 11332 lun->last_busy; 11333 } 11334#endif 11335 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11336 ooa_links); 11337 } 11338 } else { 11339 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11340 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11341 } 11342 11343 /* Get command entry and return error if it is unsuppotyed. */ 11344 entry = ctl_validate_command(ctsio); 11345 if (entry == NULL) { 11346 if (lun) 11347 mtx_unlock(&lun->lun_lock); 11348 return (retval); 11349 } 11350 11351 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11352 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11353 11354 /* 11355 * Check to see whether we can send this command to LUNs that don't 11356 * exist. This should pretty much only be the case for inquiry 11357 * and request sense. Further checks, below, really require having 11358 * a LUN, so we can't really check the command anymore. Just put 11359 * it on the rtr queue. 11360 */ 11361 if (lun == NULL) { 11362 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11363 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11364 ctl_enqueue_rtr((union ctl_io *)ctsio); 11365 return (retval); 11366 } 11367 11368 ctl_set_unsupported_lun(ctsio); 11369 ctl_done((union ctl_io *)ctsio); 11370 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11371 return (retval); 11372 } else { 11373 /* 11374 * Make sure we support this particular command on this LUN. 11375 * e.g., we don't support writes to the control LUN. 11376 */ 11377 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11378 mtx_unlock(&lun->lun_lock); 11379 ctl_set_invalid_opcode(ctsio); 11380 ctl_done((union ctl_io *)ctsio); 11381 return (retval); 11382 } 11383 } 11384 11385 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11386 11387#ifdef CTL_WITH_CA 11388 /* 11389 * If we've got a request sense, it'll clear the contingent 11390 * allegiance condition. Otherwise, if we have a CA condition for 11391 * this initiator, clear it, because it sent down a command other 11392 * than request sense. 11393 */ 11394 if ((ctsio->cdb[0] != REQUEST_SENSE) 11395 && (ctl_is_set(lun->have_ca, initidx))) 11396 ctl_clear_mask(lun->have_ca, initidx); 11397#endif 11398 11399 /* 11400 * If the command has this flag set, it handles its own unit 11401 * attention reporting, we shouldn't do anything. Otherwise we 11402 * check for any pending unit attentions, and send them back to the 11403 * initiator. We only do this when a command initially comes in, 11404 * not when we pull it off the blocked queue. 11405 * 11406 * According to SAM-3, section 5.3.2, the order that things get 11407 * presented back to the host is basically unit attentions caused 11408 * by some sort of reset event, busy status, reservation conflicts 11409 * or task set full, and finally any other status. 11410 * 11411 * One issue here is that some of the unit attentions we report 11412 * don't fall into the "reset" category (e.g. "reported luns data 11413 * has changed"). So reporting it here, before the reservation 11414 * check, may be technically wrong. I guess the only thing to do 11415 * would be to check for and report the reset events here, and then 11416 * check for the other unit attention types after we check for a 11417 * reservation conflict. 11418 * 11419 * XXX KDM need to fix this 11420 */ 11421 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11422 ctl_ua_type ua_type; 11423 11424 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11425 SSD_TYPE_NONE); 11426 if (ua_type != CTL_UA_NONE) { 11427 mtx_unlock(&lun->lun_lock); 11428 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11429 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11430 ctsio->sense_len = SSD_FULL_SIZE; 11431 ctl_done((union ctl_io *)ctsio); 11432 return (retval); 11433 } 11434 } 11435 11436 11437 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11438 mtx_unlock(&lun->lun_lock); 11439 ctl_done((union ctl_io *)ctsio); 11440 return (retval); 11441 } 11442 11443 /* 11444 * XXX CHD this is where we want to send IO to other side if 11445 * this LUN is secondary on this SC. We will need to make a copy 11446 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11447 * the copy we send as FROM_OTHER. 11448 * We also need to stuff the address of the original IO so we can 11449 * find it easily. Something similar will need be done on the other 11450 * side so when we are done we can find the copy. 11451 */ 11452 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11453 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11454 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11455 union ctl_ha_msg msg_info; 11456 int isc_retval; 11457 11458 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11459 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11460 mtx_unlock(&lun->lun_lock); 11461 11462 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11463 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11464 msg_info.hdr.serializing_sc = NULL; 11465 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11466 msg_info.scsi.tag_num = ctsio->tag_num; 11467 msg_info.scsi.tag_type = ctsio->tag_type; 11468 msg_info.scsi.cdb_len = ctsio->cdb_len; 11469 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11470 11471 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11472 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11473 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11474 ctl_set_busy(ctsio); 11475 ctl_done((union ctl_io *)ctsio); 11476 return (retval); 11477 } 11478 return (retval); 11479 } 11480 11481 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11482 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11483 ctl_ooaq, ooa_links))) { 11484 case CTL_ACTION_BLOCK: 11485 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11486 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11487 blocked_links); 11488 mtx_unlock(&lun->lun_lock); 11489 return (retval); 11490 case CTL_ACTION_PASS: 11491 case CTL_ACTION_SKIP: 11492 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11493 mtx_unlock(&lun->lun_lock); 11494 ctl_enqueue_rtr((union ctl_io *)ctsio); 11495 break; 11496 case CTL_ACTION_OVERLAP: 11497 mtx_unlock(&lun->lun_lock); 11498 ctl_set_overlapped_cmd(ctsio); 11499 ctl_done((union ctl_io *)ctsio); 11500 break; 11501 case CTL_ACTION_OVERLAP_TAG: 11502 mtx_unlock(&lun->lun_lock); 11503 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11504 ctl_done((union ctl_io *)ctsio); 11505 break; 11506 case CTL_ACTION_ERROR: 11507 default: 11508 mtx_unlock(&lun->lun_lock); 11509 ctl_set_internal_failure(ctsio, 11510 /*sks_valid*/ 0, 11511 /*retry_count*/ 0); 11512 ctl_done((union ctl_io *)ctsio); 11513 break; 11514 } 11515 return (retval); 11516} 11517 11518const struct ctl_cmd_entry * 11519ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11520{ 11521 const struct ctl_cmd_entry *entry; 11522 int service_action; 11523 11524 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11525 if (sa) 11526 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11527 if (entry->flags & CTL_CMD_FLAG_SA5) { 11528 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11529 entry = &((const struct ctl_cmd_entry *) 11530 entry->execute)[service_action]; 11531 } 11532 return (entry); 11533} 11534 11535const struct ctl_cmd_entry * 11536ctl_validate_command(struct ctl_scsiio *ctsio) 11537{ 11538 const struct ctl_cmd_entry *entry; 11539 int i, sa; 11540 uint8_t diff; 11541 11542 entry = ctl_get_cmd_entry(ctsio, &sa); 11543 if (entry->execute == NULL) { 11544 if (sa) 11545 ctl_set_invalid_field(ctsio, 11546 /*sks_valid*/ 1, 11547 /*command*/ 1, 11548 /*field*/ 1, 11549 /*bit_valid*/ 1, 11550 /*bit*/ 4); 11551 else 11552 ctl_set_invalid_opcode(ctsio); 11553 ctl_done((union ctl_io *)ctsio); 11554 return (NULL); 11555 } 11556 KASSERT(entry->length > 0, 11557 ("Not defined length for command 0x%02x/0x%02x", 11558 ctsio->cdb[0], ctsio->cdb[1])); 11559 for (i = 1; i < entry->length; i++) { 11560 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11561 if (diff == 0) 11562 continue; 11563 ctl_set_invalid_field(ctsio, 11564 /*sks_valid*/ 1, 11565 /*command*/ 1, 11566 /*field*/ i, 11567 /*bit_valid*/ 1, 11568 /*bit*/ fls(diff) - 1); 11569 ctl_done((union ctl_io *)ctsio); 11570 return (NULL); 11571 } 11572 return (entry); 11573} 11574 11575static int 11576ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11577{ 11578 11579 switch (lun_type) { 11580 case T_PROCESSOR: 11581 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11582 return (0); 11583 break; 11584 case T_DIRECT: 11585 if ((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) 11586 return (0); 11587 break; 11588 default: 11589 return (0); 11590 } 11591 return (1); 11592} 11593 11594static int 11595ctl_scsiio(struct ctl_scsiio *ctsio) 11596{ 11597 int retval; 11598 const struct ctl_cmd_entry *entry; 11599 11600 retval = CTL_RETVAL_COMPLETE; 11601 11602 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11603 11604 entry = ctl_get_cmd_entry(ctsio, NULL); 11605 11606 /* 11607 * If this I/O has been aborted, just send it straight to 11608 * ctl_done() without executing it. 11609 */ 11610 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11611 ctl_done((union ctl_io *)ctsio); 11612 goto bailout; 11613 } 11614 11615 /* 11616 * All the checks should have been handled by ctl_scsiio_precheck(). 11617 * We should be clear now to just execute the I/O. 11618 */ 11619 retval = entry->execute(ctsio); 11620 11621bailout: 11622 return (retval); 11623} 11624 11625/* 11626 * Since we only implement one target right now, a bus reset simply resets 11627 * our single target. 11628 */ 11629static int 11630ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11631{ 11632 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11633} 11634 11635static int 11636ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11637 ctl_ua_type ua_type) 11638{ 11639 struct ctl_port *port; 11640 struct ctl_lun *lun; 11641 int retval; 11642 11643 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11644 union ctl_ha_msg msg_info; 11645 11646 msg_info.hdr.nexus = io->io_hdr.nexus; 11647 if (ua_type==CTL_UA_TARG_RESET) 11648 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11649 else 11650 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11651 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11652 msg_info.hdr.original_sc = NULL; 11653 msg_info.hdr.serializing_sc = NULL; 11654 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11655 sizeof(msg_info.task), M_WAITOK); 11656 } 11657 retval = 0; 11658 11659 mtx_lock(&softc->ctl_lock); 11660 port = softc->ctl_ports[io->io_hdr.nexus.targ_port]; 11661 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11662 if (port != NULL && 11663 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 11664 continue; 11665 retval += ctl_do_lun_reset(lun, io, ua_type); 11666 } 11667 mtx_unlock(&softc->ctl_lock); 11668 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11669 return (retval); 11670} 11671 11672/* 11673 * The LUN should always be set. The I/O is optional, and is used to 11674 * distinguish between I/Os sent by this initiator, and by other 11675 * initiators. We set unit attention for initiators other than this one. 11676 * SAM-3 is vague on this point. It does say that a unit attention should 11677 * be established for other initiators when a LUN is reset (see section 11678 * 5.7.3), but it doesn't specifically say that the unit attention should 11679 * be established for this particular initiator when a LUN is reset. Here 11680 * is the relevant text, from SAM-3 rev 8: 11681 * 11682 * 5.7.2 When a SCSI initiator port aborts its own tasks 11683 * 11684 * When a SCSI initiator port causes its own task(s) to be aborted, no 11685 * notification that the task(s) have been aborted shall be returned to 11686 * the SCSI initiator port other than the completion response for the 11687 * command or task management function action that caused the task(s) to 11688 * be aborted and notification(s) associated with related effects of the 11689 * action (e.g., a reset unit attention condition). 11690 * 11691 * XXX KDM for now, we're setting unit attention for all initiators. 11692 */ 11693static int 11694ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11695{ 11696 union ctl_io *xio; 11697#if 0 11698 uint32_t initidx; 11699#endif 11700#ifdef CTL_WITH_CA 11701 int i; 11702#endif 11703 11704 mtx_lock(&lun->lun_lock); 11705 /* 11706 * Run through the OOA queue and abort each I/O. 11707 */ 11708 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11709 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11710 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11711 } 11712 11713 /* 11714 * This version sets unit attention for every 11715 */ 11716#if 0 11717 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11718 ctl_est_ua_all(lun, initidx, ua_type); 11719#else 11720 ctl_est_ua_all(lun, -1, ua_type); 11721#endif 11722 11723 /* 11724 * A reset (any kind, really) clears reservations established with 11725 * RESERVE/RELEASE. It does not clear reservations established 11726 * with PERSISTENT RESERVE OUT, but we don't support that at the 11727 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11728 * reservations made with the RESERVE/RELEASE commands, because 11729 * those commands are obsolete in SPC-3. 11730 */ 11731 lun->flags &= ~CTL_LUN_RESERVED; 11732 11733#ifdef CTL_WITH_CA 11734 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11735 ctl_clear_mask(lun->have_ca, i); 11736#endif 11737 mtx_unlock(&lun->lun_lock); 11738 11739 return (0); 11740} 11741 11742static int 11743ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11744{ 11745 struct ctl_lun *lun; 11746 uint32_t targ_lun; 11747 int retval; 11748 11749 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11750 mtx_lock(&softc->ctl_lock); 11751 if ((targ_lun >= CTL_MAX_LUNS) || 11752 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11753 mtx_unlock(&softc->ctl_lock); 11754 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11755 return (1); 11756 } 11757 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 11758 mtx_unlock(&softc->ctl_lock); 11759 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11760 11761 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11762 union ctl_ha_msg msg_info; 11763 11764 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11765 msg_info.hdr.nexus = io->io_hdr.nexus; 11766 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11767 msg_info.hdr.original_sc = NULL; 11768 msg_info.hdr.serializing_sc = NULL; 11769 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11770 sizeof(msg_info.task), M_WAITOK); 11771 } 11772 return (retval); 11773} 11774 11775static void 11776ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11777 int other_sc) 11778{ 11779 union ctl_io *xio; 11780 11781 mtx_assert(&lun->lun_lock, MA_OWNED); 11782 11783 /* 11784 * Run through the OOA queue and attempt to find the given I/O. 11785 * The target port, initiator ID, tag type and tag number have to 11786 * match the values that we got from the initiator. If we have an 11787 * untagged command to abort, simply abort the first untagged command 11788 * we come to. We only allow one untagged command at a time of course. 11789 */ 11790 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11791 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11792 11793 if ((targ_port == UINT32_MAX || 11794 targ_port == xio->io_hdr.nexus.targ_port) && 11795 (init_id == UINT32_MAX || 11796 init_id == xio->io_hdr.nexus.initid)) { 11797 if (targ_port != xio->io_hdr.nexus.targ_port || 11798 init_id != xio->io_hdr.nexus.initid) 11799 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11800 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11801 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11802 union ctl_ha_msg msg_info; 11803 11804 msg_info.hdr.nexus = xio->io_hdr.nexus; 11805 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11806 msg_info.task.tag_num = xio->scsiio.tag_num; 11807 msg_info.task.tag_type = xio->scsiio.tag_type; 11808 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11809 msg_info.hdr.original_sc = NULL; 11810 msg_info.hdr.serializing_sc = NULL; 11811 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11812 sizeof(msg_info.task), M_NOWAIT); 11813 } 11814 } 11815 } 11816} 11817 11818static int 11819ctl_abort_task_set(union ctl_io *io) 11820{ 11821 struct ctl_softc *softc = control_softc; 11822 struct ctl_lun *lun; 11823 uint32_t targ_lun; 11824 11825 /* 11826 * Look up the LUN. 11827 */ 11828 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11829 mtx_lock(&softc->ctl_lock); 11830 if ((targ_lun >= CTL_MAX_LUNS) || 11831 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11832 mtx_unlock(&softc->ctl_lock); 11833 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11834 return (1); 11835 } 11836 11837 mtx_lock(&lun->lun_lock); 11838 mtx_unlock(&softc->ctl_lock); 11839 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11840 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11841 io->io_hdr.nexus.initid, 11842 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11843 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11844 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11845 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11846 } 11847 mtx_unlock(&lun->lun_lock); 11848 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11849 return (0); 11850} 11851 11852static int 11853ctl_i_t_nexus_reset(union ctl_io *io) 11854{ 11855 struct ctl_softc *softc = control_softc; 11856 struct ctl_lun *lun; 11857 uint32_t initidx; 11858 11859 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11860 union ctl_ha_msg msg_info; 11861 11862 msg_info.hdr.nexus = io->io_hdr.nexus; 11863 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 11864 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11865 msg_info.hdr.original_sc = NULL; 11866 msg_info.hdr.serializing_sc = NULL; 11867 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11868 sizeof(msg_info.task), M_WAITOK); 11869 } 11870 11871 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11872 mtx_lock(&softc->ctl_lock); 11873 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11874 mtx_lock(&lun->lun_lock); 11875 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11876 io->io_hdr.nexus.initid, 1); 11877#ifdef CTL_WITH_CA 11878 ctl_clear_mask(lun->have_ca, initidx); 11879#endif 11880 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 11881 lun->flags &= ~CTL_LUN_RESERVED; 11882 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 11883 mtx_unlock(&lun->lun_lock); 11884 } 11885 mtx_unlock(&softc->ctl_lock); 11886 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11887 return (0); 11888} 11889 11890static int 11891ctl_abort_task(union ctl_io *io) 11892{ 11893 union ctl_io *xio; 11894 struct ctl_lun *lun; 11895 struct ctl_softc *softc; 11896#if 0 11897 struct sbuf sb; 11898 char printbuf[128]; 11899#endif 11900 int found; 11901 uint32_t targ_lun; 11902 11903 softc = control_softc; 11904 found = 0; 11905 11906 /* 11907 * Look up the LUN. 11908 */ 11909 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11910 mtx_lock(&softc->ctl_lock); 11911 if ((targ_lun >= CTL_MAX_LUNS) || 11912 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11913 mtx_unlock(&softc->ctl_lock); 11914 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11915 return (1); 11916 } 11917 11918#if 0 11919 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11920 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11921#endif 11922 11923 mtx_lock(&lun->lun_lock); 11924 mtx_unlock(&softc->ctl_lock); 11925 /* 11926 * Run through the OOA queue and attempt to find the given I/O. 11927 * The target port, initiator ID, tag type and tag number have to 11928 * match the values that we got from the initiator. If we have an 11929 * untagged command to abort, simply abort the first untagged command 11930 * we come to. We only allow one untagged command at a time of course. 11931 */ 11932 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11933 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11934#if 0 11935 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11936 11937 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11938 lun->lun, xio->scsiio.tag_num, 11939 xio->scsiio.tag_type, 11940 (xio->io_hdr.blocked_links.tqe_prev 11941 == NULL) ? "" : " BLOCKED", 11942 (xio->io_hdr.flags & 11943 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11944 (xio->io_hdr.flags & 11945 CTL_FLAG_ABORT) ? " ABORT" : "", 11946 (xio->io_hdr.flags & 11947 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11948 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11949 sbuf_finish(&sb); 11950 printf("%s\n", sbuf_data(&sb)); 11951#endif 11952 11953 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 11954 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 11955 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 11956 continue; 11957 11958 /* 11959 * If the abort says that the task is untagged, the 11960 * task in the queue must be untagged. Otherwise, 11961 * we just check to see whether the tag numbers 11962 * match. This is because the QLogic firmware 11963 * doesn't pass back the tag type in an abort 11964 * request. 11965 */ 11966#if 0 11967 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 11968 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 11969 || (xio->scsiio.tag_num == io->taskio.tag_num)) 11970#endif 11971 /* 11972 * XXX KDM we've got problems with FC, because it 11973 * doesn't send down a tag type with aborts. So we 11974 * can only really go by the tag number... 11975 * This may cause problems with parallel SCSI. 11976 * Need to figure that out!! 11977 */ 11978 if (xio->scsiio.tag_num == io->taskio.tag_num) { 11979 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11980 found = 1; 11981 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 11982 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11983 union ctl_ha_msg msg_info; 11984 11985 msg_info.hdr.nexus = io->io_hdr.nexus; 11986 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11987 msg_info.task.tag_num = io->taskio.tag_num; 11988 msg_info.task.tag_type = io->taskio.tag_type; 11989 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11990 msg_info.hdr.original_sc = NULL; 11991 msg_info.hdr.serializing_sc = NULL; 11992#if 0 11993 printf("Sent Abort to other side\n"); 11994#endif 11995 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11996 sizeof(msg_info.task), M_NOWAIT); 11997 } 11998#if 0 11999 printf("ctl_abort_task: found I/O to abort\n"); 12000#endif 12001 } 12002 } 12003 mtx_unlock(&lun->lun_lock); 12004 12005 if (found == 0) { 12006 /* 12007 * This isn't really an error. It's entirely possible for 12008 * the abort and command completion to cross on the wire. 12009 * This is more of an informative/diagnostic error. 12010 */ 12011#if 0 12012 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12013 "%u:%u:%u tag %d type %d\n", 12014 io->io_hdr.nexus.initid, 12015 io->io_hdr.nexus.targ_port, 12016 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12017 io->taskio.tag_type); 12018#endif 12019 } 12020 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12021 return (0); 12022} 12023 12024static int 12025ctl_query_task(union ctl_io *io, int task_set) 12026{ 12027 union ctl_io *xio; 12028 struct ctl_lun *lun; 12029 struct ctl_softc *softc; 12030 int found = 0; 12031 uint32_t targ_lun; 12032 12033 softc = control_softc; 12034 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12035 mtx_lock(&softc->ctl_lock); 12036 if ((targ_lun >= CTL_MAX_LUNS) || 12037 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12038 mtx_unlock(&softc->ctl_lock); 12039 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12040 return (1); 12041 } 12042 mtx_lock(&lun->lun_lock); 12043 mtx_unlock(&softc->ctl_lock); 12044 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12045 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12046 12047 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12048 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12049 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12050 continue; 12051 12052 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12053 found = 1; 12054 break; 12055 } 12056 } 12057 mtx_unlock(&lun->lun_lock); 12058 if (found) 12059 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12060 else 12061 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12062 return (0); 12063} 12064 12065static int 12066ctl_query_async_event(union ctl_io *io) 12067{ 12068 struct ctl_lun *lun; 12069 struct ctl_softc *softc; 12070 ctl_ua_type ua; 12071 uint32_t targ_lun, initidx; 12072 12073 softc = control_softc; 12074 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12075 mtx_lock(&softc->ctl_lock); 12076 if ((targ_lun >= CTL_MAX_LUNS) || 12077 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12078 mtx_unlock(&softc->ctl_lock); 12079 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12080 return (1); 12081 } 12082 mtx_lock(&lun->lun_lock); 12083 mtx_unlock(&softc->ctl_lock); 12084 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12085 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12086 mtx_unlock(&lun->lun_lock); 12087 if (ua != CTL_UA_NONE) 12088 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12089 else 12090 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12091 return (0); 12092} 12093 12094static void 12095ctl_run_task(union ctl_io *io) 12096{ 12097 struct ctl_softc *softc = control_softc; 12098 int retval = 1; 12099 12100 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12101 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12102 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12103 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12104 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12105 switch (io->taskio.task_action) { 12106 case CTL_TASK_ABORT_TASK: 12107 retval = ctl_abort_task(io); 12108 break; 12109 case CTL_TASK_ABORT_TASK_SET: 12110 case CTL_TASK_CLEAR_TASK_SET: 12111 retval = ctl_abort_task_set(io); 12112 break; 12113 case CTL_TASK_CLEAR_ACA: 12114 break; 12115 case CTL_TASK_I_T_NEXUS_RESET: 12116 retval = ctl_i_t_nexus_reset(io); 12117 break; 12118 case CTL_TASK_LUN_RESET: 12119 retval = ctl_lun_reset(softc, io); 12120 break; 12121 case CTL_TASK_TARGET_RESET: 12122 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12123 break; 12124 case CTL_TASK_BUS_RESET: 12125 retval = ctl_bus_reset(softc, io); 12126 break; 12127 case CTL_TASK_PORT_LOGIN: 12128 break; 12129 case CTL_TASK_PORT_LOGOUT: 12130 break; 12131 case CTL_TASK_QUERY_TASK: 12132 retval = ctl_query_task(io, 0); 12133 break; 12134 case CTL_TASK_QUERY_TASK_SET: 12135 retval = ctl_query_task(io, 1); 12136 break; 12137 case CTL_TASK_QUERY_ASYNC_EVENT: 12138 retval = ctl_query_async_event(io); 12139 break; 12140 default: 12141 printf("%s: got unknown task management event %d\n", 12142 __func__, io->taskio.task_action); 12143 break; 12144 } 12145 if (retval == 0) 12146 io->io_hdr.status = CTL_SUCCESS; 12147 else 12148 io->io_hdr.status = CTL_ERROR; 12149 ctl_done(io); 12150} 12151 12152/* 12153 * For HA operation. Handle commands that come in from the other 12154 * controller. 12155 */ 12156static void 12157ctl_handle_isc(union ctl_io *io) 12158{ 12159 int free_io; 12160 struct ctl_lun *lun; 12161 struct ctl_softc *softc; 12162 uint32_t targ_lun; 12163 12164 softc = control_softc; 12165 12166 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12167 lun = softc->ctl_luns[targ_lun]; 12168 12169 switch (io->io_hdr.msg_type) { 12170 case CTL_MSG_SERIALIZE: 12171 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12172 break; 12173 case CTL_MSG_R2R: { 12174 const struct ctl_cmd_entry *entry; 12175 12176 /* 12177 * This is only used in SER_ONLY mode. 12178 */ 12179 free_io = 0; 12180 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12181 mtx_lock(&lun->lun_lock); 12182 if (ctl_scsiio_lun_check(lun, 12183 entry, (struct ctl_scsiio *)io) != 0) { 12184 mtx_unlock(&lun->lun_lock); 12185 ctl_done(io); 12186 break; 12187 } 12188 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12189 mtx_unlock(&lun->lun_lock); 12190 ctl_enqueue_rtr(io); 12191 break; 12192 } 12193 case CTL_MSG_FINISH_IO: 12194 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12195 free_io = 0; 12196 ctl_done(io); 12197 } else { 12198 free_io = 1; 12199 mtx_lock(&lun->lun_lock); 12200 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12201 ooa_links); 12202 ctl_check_blocked(lun); 12203 mtx_unlock(&lun->lun_lock); 12204 } 12205 break; 12206 case CTL_MSG_PERS_ACTION: 12207 ctl_hndl_per_res_out_on_other_sc( 12208 (union ctl_ha_msg *)&io->presio.pr_msg); 12209 free_io = 1; 12210 break; 12211 case CTL_MSG_BAD_JUJU: 12212 free_io = 0; 12213 ctl_done(io); 12214 break; 12215 case CTL_MSG_DATAMOVE: 12216 /* Only used in XFER mode */ 12217 free_io = 0; 12218 ctl_datamove_remote(io); 12219 break; 12220 case CTL_MSG_DATAMOVE_DONE: 12221 /* Only used in XFER mode */ 12222 free_io = 0; 12223 io->scsiio.be_move_done(io); 12224 break; 12225 case CTL_MSG_FAILOVER: 12226 mtx_lock(&lun->lun_lock); 12227 ctl_failover_lun(lun); 12228 mtx_unlock(&lun->lun_lock); 12229 free_io = 1; 12230 break; 12231 default: 12232 free_io = 1; 12233 printf("%s: Invalid message type %d\n", 12234 __func__, io->io_hdr.msg_type); 12235 break; 12236 } 12237 if (free_io) 12238 ctl_free_io(io); 12239 12240} 12241 12242 12243/* 12244 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12245 * there is no match. 12246 */ 12247static ctl_lun_error_pattern 12248ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12249{ 12250 const struct ctl_cmd_entry *entry; 12251 ctl_lun_error_pattern filtered_pattern, pattern; 12252 12253 pattern = desc->error_pattern; 12254 12255 /* 12256 * XXX KDM we need more data passed into this function to match a 12257 * custom pattern, and we actually need to implement custom pattern 12258 * matching. 12259 */ 12260 if (pattern & CTL_LUN_PAT_CMD) 12261 return (CTL_LUN_PAT_CMD); 12262 12263 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12264 return (CTL_LUN_PAT_ANY); 12265 12266 entry = ctl_get_cmd_entry(ctsio, NULL); 12267 12268 filtered_pattern = entry->pattern & pattern; 12269 12270 /* 12271 * If the user requested specific flags in the pattern (e.g. 12272 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12273 * flags. 12274 * 12275 * If the user did not specify any flags, it doesn't matter whether 12276 * or not the command supports the flags. 12277 */ 12278 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12279 (pattern & ~CTL_LUN_PAT_MASK)) 12280 return (CTL_LUN_PAT_NONE); 12281 12282 /* 12283 * If the user asked for a range check, see if the requested LBA 12284 * range overlaps with this command's LBA range. 12285 */ 12286 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12287 uint64_t lba1; 12288 uint64_t len1; 12289 ctl_action action; 12290 int retval; 12291 12292 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12293 if (retval != 0) 12294 return (CTL_LUN_PAT_NONE); 12295 12296 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12297 desc->lba_range.len, FALSE); 12298 /* 12299 * A "pass" means that the LBA ranges don't overlap, so 12300 * this doesn't match the user's range criteria. 12301 */ 12302 if (action == CTL_ACTION_PASS) 12303 return (CTL_LUN_PAT_NONE); 12304 } 12305 12306 return (filtered_pattern); 12307} 12308 12309static void 12310ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12311{ 12312 struct ctl_error_desc *desc, *desc2; 12313 12314 mtx_assert(&lun->lun_lock, MA_OWNED); 12315 12316 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12317 ctl_lun_error_pattern pattern; 12318 /* 12319 * Check to see whether this particular command matches 12320 * the pattern in the descriptor. 12321 */ 12322 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12323 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12324 continue; 12325 12326 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12327 case CTL_LUN_INJ_ABORTED: 12328 ctl_set_aborted(&io->scsiio); 12329 break; 12330 case CTL_LUN_INJ_MEDIUM_ERR: 12331 ctl_set_medium_error(&io->scsiio, 12332 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12333 CTL_FLAG_DATA_OUT); 12334 break; 12335 case CTL_LUN_INJ_UA: 12336 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12337 * OCCURRED */ 12338 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12339 break; 12340 case CTL_LUN_INJ_CUSTOM: 12341 /* 12342 * We're assuming the user knows what he is doing. 12343 * Just copy the sense information without doing 12344 * checks. 12345 */ 12346 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12347 MIN(sizeof(desc->custom_sense), 12348 sizeof(io->scsiio.sense_data))); 12349 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12350 io->scsiio.sense_len = SSD_FULL_SIZE; 12351 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12352 break; 12353 case CTL_LUN_INJ_NONE: 12354 default: 12355 /* 12356 * If this is an error injection type we don't know 12357 * about, clear the continuous flag (if it is set) 12358 * so it will get deleted below. 12359 */ 12360 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12361 break; 12362 } 12363 /* 12364 * By default, each error injection action is a one-shot 12365 */ 12366 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12367 continue; 12368 12369 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12370 12371 free(desc, M_CTL); 12372 } 12373} 12374 12375#ifdef CTL_IO_DELAY 12376static void 12377ctl_datamove_timer_wakeup(void *arg) 12378{ 12379 union ctl_io *io; 12380 12381 io = (union ctl_io *)arg; 12382 12383 ctl_datamove(io); 12384} 12385#endif /* CTL_IO_DELAY */ 12386 12387void 12388ctl_datamove(union ctl_io *io) 12389{ 12390 struct ctl_lun *lun; 12391 void (*fe_datamove)(union ctl_io *io); 12392 12393 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12394 12395 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12396 12397 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12398#ifdef CTL_TIME_IO 12399 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12400 char str[256]; 12401 char path_str[64]; 12402 struct sbuf sb; 12403 12404 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12405 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12406 12407 sbuf_cat(&sb, path_str); 12408 switch (io->io_hdr.io_type) { 12409 case CTL_IO_SCSI: 12410 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12411 sbuf_printf(&sb, "\n"); 12412 sbuf_cat(&sb, path_str); 12413 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12414 io->scsiio.tag_num, io->scsiio.tag_type); 12415 break; 12416 case CTL_IO_TASK: 12417 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12418 "Tag Type: %d\n", io->taskio.task_action, 12419 io->taskio.tag_num, io->taskio.tag_type); 12420 break; 12421 default: 12422 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12423 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12424 break; 12425 } 12426 sbuf_cat(&sb, path_str); 12427 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12428 (intmax_t)time_uptime - io->io_hdr.start_time); 12429 sbuf_finish(&sb); 12430 printf("%s", sbuf_data(&sb)); 12431 } 12432#endif /* CTL_TIME_IO */ 12433 12434#ifdef CTL_IO_DELAY 12435 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12436 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12437 } else { 12438 if ((lun != NULL) 12439 && (lun->delay_info.datamove_delay > 0)) { 12440 12441 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12442 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12443 callout_reset(&io->io_hdr.delay_callout, 12444 lun->delay_info.datamove_delay * hz, 12445 ctl_datamove_timer_wakeup, io); 12446 if (lun->delay_info.datamove_type == 12447 CTL_DELAY_TYPE_ONESHOT) 12448 lun->delay_info.datamove_delay = 0; 12449 return; 12450 } 12451 } 12452#endif 12453 12454 /* 12455 * This command has been aborted. Set the port status, so we fail 12456 * the data move. 12457 */ 12458 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12459 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12460 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12461 io->io_hdr.nexus.targ_port, 12462 io->io_hdr.nexus.targ_lun); 12463 io->io_hdr.port_status = 31337; 12464 /* 12465 * Note that the backend, in this case, will get the 12466 * callback in its context. In other cases it may get 12467 * called in the frontend's interrupt thread context. 12468 */ 12469 io->scsiio.be_move_done(io); 12470 return; 12471 } 12472 12473 /* Don't confuse frontend with zero length data move. */ 12474 if (io->scsiio.kern_data_len == 0) { 12475 io->scsiio.be_move_done(io); 12476 return; 12477 } 12478 12479 /* 12480 * If we're in XFER mode and this I/O is from the other shelf 12481 * controller, we need to send the DMA to the other side to 12482 * actually transfer the data to/from the host. In serialize only 12483 * mode the transfer happens below CTL and ctl_datamove() is only 12484 * called on the machine that originally received the I/O. 12485 */ 12486 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12487 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12488 union ctl_ha_msg msg; 12489 uint32_t sg_entries_sent; 12490 int do_sg_copy; 12491 int i; 12492 12493 memset(&msg, 0, sizeof(msg)); 12494 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12495 msg.hdr.original_sc = io->io_hdr.original_sc; 12496 msg.hdr.serializing_sc = io; 12497 msg.hdr.nexus = io->io_hdr.nexus; 12498 msg.hdr.status = io->io_hdr.status; 12499 msg.dt.flags = io->io_hdr.flags; 12500 /* 12501 * We convert everything into a S/G list here. We can't 12502 * pass by reference, only by value between controllers. 12503 * So we can't pass a pointer to the S/G list, only as many 12504 * S/G entries as we can fit in here. If it's possible for 12505 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12506 * then we need to break this up into multiple transfers. 12507 */ 12508 if (io->scsiio.kern_sg_entries == 0) { 12509 msg.dt.kern_sg_entries = 1; 12510#if 0 12511 /* 12512 * Convert to a physical address if this is a 12513 * virtual address. 12514 */ 12515 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12516 msg.dt.sg_list[0].addr = 12517 io->scsiio.kern_data_ptr; 12518 } else { 12519 /* 12520 * XXX KDM use busdma here! 12521 */ 12522 msg.dt.sg_list[0].addr = (void *) 12523 vtophys(io->scsiio.kern_data_ptr); 12524 } 12525#else 12526 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12527 ("HA does not support BUS_ADDR")); 12528 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 12529#endif 12530 12531 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12532 do_sg_copy = 0; 12533 } else { 12534 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12535 do_sg_copy = 1; 12536 } 12537 12538 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12539 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12540 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12541 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12542 msg.dt.sg_sequence = 0; 12543 12544 /* 12545 * Loop until we've sent all of the S/G entries. On the 12546 * other end, we'll recompose these S/G entries into one 12547 * contiguous list before passing it to the 12548 */ 12549 for (sg_entries_sent = 0; sg_entries_sent < 12550 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12551 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/ 12552 sizeof(msg.dt.sg_list[0])), 12553 msg.dt.kern_sg_entries - sg_entries_sent); 12554 12555 if (do_sg_copy != 0) { 12556 struct ctl_sg_entry *sgl; 12557 int j; 12558 12559 sgl = (struct ctl_sg_entry *) 12560 io->scsiio.kern_data_ptr; 12561 /* 12562 * If this is in cached memory, flush the cache 12563 * before we send the DMA request to the other 12564 * controller. We want to do this in either 12565 * the * read or the write case. The read 12566 * case is straightforward. In the write 12567 * case, we want to make sure nothing is 12568 * in the local cache that could overwrite 12569 * the DMAed data. 12570 */ 12571 12572 for (i = sg_entries_sent, j = 0; 12573 i < msg.dt.cur_sg_entries; i++, j++) { 12574#if 0 12575 if ((io->io_hdr.flags & 12576 CTL_FLAG_BUS_ADDR) == 0) { 12577 /* 12578 * XXX KDM use busdma. 12579 */ 12580 msg.dt.sg_list[j].addr =(void *) 12581 vtophys(sgl[i].addr); 12582 } else { 12583 msg.dt.sg_list[j].addr = 12584 sgl[i].addr; 12585 } 12586#else 12587 KASSERT((io->io_hdr.flags & 12588 CTL_FLAG_BUS_ADDR) == 0, 12589 ("HA does not support BUS_ADDR")); 12590 msg.dt.sg_list[j].addr = sgl[i].addr; 12591#endif 12592 msg.dt.sg_list[j].len = sgl[i].len; 12593 } 12594 } 12595 12596 sg_entries_sent += msg.dt.cur_sg_entries; 12597 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12598 msg.dt.sg_last = 1; 12599 else 12600 msg.dt.sg_last = 0; 12601 12602 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12603 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 12604 sizeof(struct ctl_sg_entry)*msg.dt.cur_sg_entries, 12605 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 12606 io->io_hdr.port_status = 31341; 12607 io->scsiio.be_move_done(io); 12608 return; 12609 } 12610 12611 msg.dt.sent_sg_entries = sg_entries_sent; 12612 } 12613 12614 /* 12615 * Officially handover the request from us to peer. 12616 * If failover has just happened, then we must return error. 12617 * If failover happen just after, then it is not our problem. 12618 */ 12619 if (lun) 12620 mtx_lock(&lun->lun_lock); 12621 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12622 if (lun) 12623 mtx_unlock(&lun->lun_lock); 12624 io->io_hdr.port_status = 31342; 12625 io->scsiio.be_move_done(io); 12626 return; 12627 } 12628 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12629 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 12630 if (lun) 12631 mtx_unlock(&lun->lun_lock); 12632 } else { 12633 12634 /* 12635 * Lookup the fe_datamove() function for this particular 12636 * front end. 12637 */ 12638 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12639 12640 fe_datamove(io); 12641 } 12642} 12643 12644static void 12645ctl_send_datamove_done(union ctl_io *io, int have_lock) 12646{ 12647 union ctl_ha_msg msg; 12648 12649 memset(&msg, 0, sizeof(msg)); 12650 12651 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12652 msg.hdr.original_sc = io; 12653 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12654 msg.hdr.nexus = io->io_hdr.nexus; 12655 msg.hdr.status = io->io_hdr.status; 12656 msg.scsi.tag_num = io->scsiio.tag_num; 12657 msg.scsi.tag_type = io->scsiio.tag_type; 12658 msg.scsi.scsi_status = io->scsiio.scsi_status; 12659 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12660 io->scsiio.sense_len); 12661 msg.scsi.sense_len = io->scsiio.sense_len; 12662 msg.scsi.sense_residual = io->scsiio.sense_residual; 12663 msg.scsi.fetd_status = io->io_hdr.port_status; 12664 msg.scsi.residual = io->scsiio.residual; 12665 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12666 12667 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12668 ctl_failover_io(io, /*have_lock*/ have_lock); 12669 return; 12670 } 12671 12672 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12673 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12674 msg.scsi.sense_len, M_WAITOK); 12675} 12676 12677/* 12678 * The DMA to the remote side is done, now we need to tell the other side 12679 * we're done so it can continue with its data movement. 12680 */ 12681static void 12682ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12683{ 12684 union ctl_io *io; 12685 int i; 12686 12687 io = rq->context; 12688 12689 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12690 printf("%s: ISC DMA write failed with error %d", __func__, 12691 rq->ret); 12692 ctl_set_internal_failure(&io->scsiio, 12693 /*sks_valid*/ 1, 12694 /*retry_count*/ rq->ret); 12695 } 12696 12697 ctl_dt_req_free(rq); 12698 12699 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12700 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12701 free(io->io_hdr.remote_sglist, M_CTL); 12702 io->io_hdr.remote_sglist = NULL; 12703 io->io_hdr.local_sglist = NULL; 12704 12705 /* 12706 * The data is in local and remote memory, so now we need to send 12707 * status (good or back) back to the other side. 12708 */ 12709 ctl_send_datamove_done(io, /*have_lock*/ 0); 12710} 12711 12712/* 12713 * We've moved the data from the host/controller into local memory. Now we 12714 * need to push it over to the remote controller's memory. 12715 */ 12716static int 12717ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12718{ 12719 int retval; 12720 12721 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12722 ctl_datamove_remote_write_cb); 12723 return (retval); 12724} 12725 12726static void 12727ctl_datamove_remote_write(union ctl_io *io) 12728{ 12729 int retval; 12730 void (*fe_datamove)(union ctl_io *io); 12731 12732 /* 12733 * - Get the data from the host/HBA into local memory. 12734 * - DMA memory from the local controller to the remote controller. 12735 * - Send status back to the remote controller. 12736 */ 12737 12738 retval = ctl_datamove_remote_sgl_setup(io); 12739 if (retval != 0) 12740 return; 12741 12742 /* Switch the pointer over so the FETD knows what to do */ 12743 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12744 12745 /* 12746 * Use a custom move done callback, since we need to send completion 12747 * back to the other controller, not to the backend on this side. 12748 */ 12749 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12750 12751 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12752 fe_datamove(io); 12753} 12754 12755static int 12756ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12757{ 12758#if 0 12759 char str[256]; 12760 char path_str[64]; 12761 struct sbuf sb; 12762#endif 12763 int i; 12764 12765 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12766 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12767 free(io->io_hdr.remote_sglist, M_CTL); 12768 io->io_hdr.remote_sglist = NULL; 12769 io->io_hdr.local_sglist = NULL; 12770 12771#if 0 12772 scsi_path_string(io, path_str, sizeof(path_str)); 12773 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12774 sbuf_cat(&sb, path_str); 12775 scsi_command_string(&io->scsiio, NULL, &sb); 12776 sbuf_printf(&sb, "\n"); 12777 sbuf_cat(&sb, path_str); 12778 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12779 io->scsiio.tag_num, io->scsiio.tag_type); 12780 sbuf_cat(&sb, path_str); 12781 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12782 io->io_hdr.flags, io->io_hdr.status); 12783 sbuf_finish(&sb); 12784 printk("%s", sbuf_data(&sb)); 12785#endif 12786 12787 12788 /* 12789 * The read is done, now we need to send status (good or bad) back 12790 * to the other side. 12791 */ 12792 ctl_send_datamove_done(io, /*have_lock*/ 0); 12793 12794 return (0); 12795} 12796 12797static void 12798ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12799{ 12800 union ctl_io *io; 12801 void (*fe_datamove)(union ctl_io *io); 12802 12803 io = rq->context; 12804 12805 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12806 printf("%s: ISC DMA read failed with error %d\n", __func__, 12807 rq->ret); 12808 ctl_set_internal_failure(&io->scsiio, 12809 /*sks_valid*/ 1, 12810 /*retry_count*/ rq->ret); 12811 } 12812 12813 ctl_dt_req_free(rq); 12814 12815 /* Switch the pointer over so the FETD knows what to do */ 12816 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12817 12818 /* 12819 * Use a custom move done callback, since we need to send completion 12820 * back to the other controller, not to the backend on this side. 12821 */ 12822 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12823 12824 /* XXX KDM add checks like the ones in ctl_datamove? */ 12825 12826 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12827 fe_datamove(io); 12828} 12829 12830static int 12831ctl_datamove_remote_sgl_setup(union ctl_io *io) 12832{ 12833 struct ctl_sg_entry *local_sglist; 12834 struct ctl_softc *softc; 12835 uint32_t len_to_go; 12836 int retval; 12837 int i; 12838 12839 retval = 0; 12840 softc = control_softc; 12841 local_sglist = io->io_hdr.local_sglist; 12842 len_to_go = io->scsiio.kern_data_len; 12843 12844 /* 12845 * The difficult thing here is that the size of the various 12846 * S/G segments may be different than the size from the 12847 * remote controller. That'll make it harder when DMAing 12848 * the data back to the other side. 12849 */ 12850 for (i = 0; len_to_go > 0; i++) { 12851 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12852 local_sglist[i].addr = 12853 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12854 12855 len_to_go -= local_sglist[i].len; 12856 } 12857 /* 12858 * Reset the number of S/G entries accordingly. The original 12859 * number of S/G entries is available in rem_sg_entries. 12860 */ 12861 io->scsiio.kern_sg_entries = i; 12862 12863#if 0 12864 printf("%s: kern_sg_entries = %d\n", __func__, 12865 io->scsiio.kern_sg_entries); 12866 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12867 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12868 local_sglist[i].addr, local_sglist[i].len); 12869#endif 12870 12871 return (retval); 12872} 12873 12874static int 12875ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12876 ctl_ha_dt_cb callback) 12877{ 12878 struct ctl_ha_dt_req *rq; 12879 struct ctl_sg_entry *remote_sglist, *local_sglist; 12880 uint32_t local_used, remote_used, total_used; 12881 int i, j, isc_ret; 12882 12883 rq = ctl_dt_req_alloc(); 12884 12885 /* 12886 * If we failed to allocate the request, and if the DMA didn't fail 12887 * anyway, set busy status. This is just a resource allocation 12888 * failure. 12889 */ 12890 if ((rq == NULL) 12891 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12892 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12893 ctl_set_busy(&io->scsiio); 12894 12895 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12896 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12897 12898 if (rq != NULL) 12899 ctl_dt_req_free(rq); 12900 12901 /* 12902 * The data move failed. We need to return status back 12903 * to the other controller. No point in trying to DMA 12904 * data to the remote controller. 12905 */ 12906 12907 ctl_send_datamove_done(io, /*have_lock*/ 0); 12908 12909 return (1); 12910 } 12911 12912 local_sglist = io->io_hdr.local_sglist; 12913 remote_sglist = io->io_hdr.remote_sglist; 12914 local_used = 0; 12915 remote_used = 0; 12916 total_used = 0; 12917 12918 /* 12919 * Pull/push the data over the wire from/to the other controller. 12920 * This takes into account the possibility that the local and 12921 * remote sglists may not be identical in terms of the size of 12922 * the elements and the number of elements. 12923 * 12924 * One fundamental assumption here is that the length allocated for 12925 * both the local and remote sglists is identical. Otherwise, we've 12926 * essentially got a coding error of some sort. 12927 */ 12928 isc_ret = CTL_HA_STATUS_SUCCESS; 12929 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12930 uint32_t cur_len; 12931 uint8_t *tmp_ptr; 12932 12933 rq->command = command; 12934 rq->context = io; 12935 12936 /* 12937 * Both pointers should be aligned. But it is possible 12938 * that the allocation length is not. They should both 12939 * also have enough slack left over at the end, though, 12940 * to round up to the next 8 byte boundary. 12941 */ 12942 cur_len = MIN(local_sglist[i].len - local_used, 12943 remote_sglist[j].len - remote_used); 12944 rq->size = cur_len; 12945 12946 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12947 tmp_ptr += local_used; 12948 12949#if 0 12950 /* Use physical addresses when talking to ISC hardware */ 12951 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12952 /* XXX KDM use busdma */ 12953 rq->local = vtophys(tmp_ptr); 12954 } else 12955 rq->local = tmp_ptr; 12956#else 12957 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12958 ("HA does not support BUS_ADDR")); 12959 rq->local = tmp_ptr; 12960#endif 12961 12962 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12963 tmp_ptr += remote_used; 12964 rq->remote = tmp_ptr; 12965 12966 rq->callback = NULL; 12967 12968 local_used += cur_len; 12969 if (local_used >= local_sglist[i].len) { 12970 i++; 12971 local_used = 0; 12972 } 12973 12974 remote_used += cur_len; 12975 if (remote_used >= remote_sglist[j].len) { 12976 j++; 12977 remote_used = 0; 12978 } 12979 total_used += cur_len; 12980 12981 if (total_used >= io->scsiio.kern_data_len) 12982 rq->callback = callback; 12983 12984#if 0 12985 printf("%s: %s: local %p remote %p size %d\n", __func__, 12986 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12987 rq->local, rq->remote, rq->size); 12988#endif 12989 12990 isc_ret = ctl_dt_single(rq); 12991 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12992 break; 12993 } 12994 if (isc_ret != CTL_HA_STATUS_WAIT) { 12995 rq->ret = isc_ret; 12996 callback(rq); 12997 } 12998 12999 return (0); 13000} 13001 13002static void 13003ctl_datamove_remote_read(union ctl_io *io) 13004{ 13005 int retval; 13006 int i; 13007 13008 /* 13009 * This will send an error to the other controller in the case of a 13010 * failure. 13011 */ 13012 retval = ctl_datamove_remote_sgl_setup(io); 13013 if (retval != 0) 13014 return; 13015 13016 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13017 ctl_datamove_remote_read_cb); 13018 if (retval != 0) { 13019 /* 13020 * Make sure we free memory if there was an error.. The 13021 * ctl_datamove_remote_xfer() function will send the 13022 * datamove done message, or call the callback with an 13023 * error if there is a problem. 13024 */ 13025 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13026 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13027 free(io->io_hdr.remote_sglist, M_CTL); 13028 io->io_hdr.remote_sglist = NULL; 13029 io->io_hdr.local_sglist = NULL; 13030 } 13031} 13032 13033/* 13034 * Process a datamove request from the other controller. This is used for 13035 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13036 * first. Once that is complete, the data gets DMAed into the remote 13037 * controller's memory. For reads, we DMA from the remote controller's 13038 * memory into our memory first, and then move it out to the FETD. 13039 */ 13040static void 13041ctl_datamove_remote(union ctl_io *io) 13042{ 13043 13044 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 13045 13046 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13047 ctl_failover_io(io, /*have_lock*/ 0); 13048 return; 13049 } 13050 13051 /* 13052 * Note that we look for an aborted I/O here, but don't do some of 13053 * the other checks that ctl_datamove() normally does. 13054 * We don't need to run the datamove delay code, since that should 13055 * have been done if need be on the other controller. 13056 */ 13057 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13058 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 13059 io->scsiio.tag_num, io->io_hdr.nexus.initid, 13060 io->io_hdr.nexus.targ_port, 13061 io->io_hdr.nexus.targ_lun); 13062 io->io_hdr.port_status = 31338; 13063 ctl_send_datamove_done(io, /*have_lock*/ 0); 13064 return; 13065 } 13066 13067 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 13068 ctl_datamove_remote_write(io); 13069 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 13070 ctl_datamove_remote_read(io); 13071 else { 13072 io->io_hdr.port_status = 31339; 13073 ctl_send_datamove_done(io, /*have_lock*/ 0); 13074 } 13075} 13076 13077static int 13078ctl_process_done(union ctl_io *io) 13079{ 13080 struct ctl_lun *lun; 13081 struct ctl_softc *softc = control_softc; 13082 void (*fe_done)(union ctl_io *io); 13083 union ctl_ha_msg msg; 13084 uint32_t targ_port = io->io_hdr.nexus.targ_port; 13085 13086 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13087 13088 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) 13089 fe_done = softc->ctl_ports[targ_port]->fe_done; 13090 else 13091 fe_done = NULL; 13092 13093#ifdef CTL_TIME_IO 13094 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13095 char str[256]; 13096 char path_str[64]; 13097 struct sbuf sb; 13098 13099 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13100 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13101 13102 sbuf_cat(&sb, path_str); 13103 switch (io->io_hdr.io_type) { 13104 case CTL_IO_SCSI: 13105 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13106 sbuf_printf(&sb, "\n"); 13107 sbuf_cat(&sb, path_str); 13108 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13109 io->scsiio.tag_num, io->scsiio.tag_type); 13110 break; 13111 case CTL_IO_TASK: 13112 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13113 "Tag Type: %d\n", io->taskio.task_action, 13114 io->taskio.tag_num, io->taskio.tag_type); 13115 break; 13116 default: 13117 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13118 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13119 break; 13120 } 13121 sbuf_cat(&sb, path_str); 13122 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13123 (intmax_t)time_uptime - io->io_hdr.start_time); 13124 sbuf_finish(&sb); 13125 printf("%s", sbuf_data(&sb)); 13126 } 13127#endif /* CTL_TIME_IO */ 13128 13129 switch (io->io_hdr.io_type) { 13130 case CTL_IO_SCSI: 13131 break; 13132 case CTL_IO_TASK: 13133 if (ctl_debug & CTL_DEBUG_INFO) 13134 ctl_io_error_print(io, NULL); 13135 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13136 ctl_free_io(io); 13137 else 13138 fe_done(io); 13139 return (CTL_RETVAL_COMPLETE); 13140 default: 13141 panic("ctl_process_done: invalid io type %d\n", 13142 io->io_hdr.io_type); 13143 break; /* NOTREACHED */ 13144 } 13145 13146 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13147 if (lun == NULL) { 13148 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13149 io->io_hdr.nexus.targ_mapped_lun)); 13150 goto bailout; 13151 } 13152 13153 mtx_lock(&lun->lun_lock); 13154 13155 /* 13156 * Check to see if we have any errors to inject here. We only 13157 * inject errors for commands that don't already have errors set. 13158 */ 13159 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13160 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13161 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13162 ctl_inject_error(lun, io); 13163 13164 /* 13165 * XXX KDM how do we treat commands that aren't completed 13166 * successfully? 13167 * 13168 * XXX KDM should we also track I/O latency? 13169 */ 13170 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13171 io->io_hdr.io_type == CTL_IO_SCSI) { 13172#ifdef CTL_TIME_IO 13173 struct bintime cur_bt; 13174#endif 13175 int type; 13176 13177 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13178 CTL_FLAG_DATA_IN) 13179 type = CTL_STATS_READ; 13180 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13181 CTL_FLAG_DATA_OUT) 13182 type = CTL_STATS_WRITE; 13183 else 13184 type = CTL_STATS_NO_IO; 13185 13186 lun->stats.ports[targ_port].bytes[type] += 13187 io->scsiio.kern_total_len; 13188 lun->stats.ports[targ_port].operations[type]++; 13189#ifdef CTL_TIME_IO 13190 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13191 &io->io_hdr.dma_bt); 13192 lun->stats.ports[targ_port].num_dmas[type] += 13193 io->io_hdr.num_dmas; 13194 getbintime(&cur_bt); 13195 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13196 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13197#endif 13198 } 13199 13200 /* 13201 * Remove this from the OOA queue. 13202 */ 13203 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13204#ifdef CTL_TIME_IO 13205 if (TAILQ_EMPTY(&lun->ooa_queue)) 13206 lun->last_busy = getsbinuptime(); 13207#endif 13208 13209 /* 13210 * Run through the blocked queue on this LUN and see if anything 13211 * has become unblocked, now that this transaction is done. 13212 */ 13213 ctl_check_blocked(lun); 13214 13215 /* 13216 * If the LUN has been invalidated, free it if there is nothing 13217 * left on its OOA queue. 13218 */ 13219 if ((lun->flags & CTL_LUN_INVALID) 13220 && TAILQ_EMPTY(&lun->ooa_queue)) { 13221 mtx_unlock(&lun->lun_lock); 13222 mtx_lock(&softc->ctl_lock); 13223 ctl_free_lun(lun); 13224 mtx_unlock(&softc->ctl_lock); 13225 } else 13226 mtx_unlock(&lun->lun_lock); 13227 13228bailout: 13229 13230 /* 13231 * If this command has been aborted, make sure we set the status 13232 * properly. The FETD is responsible for freeing the I/O and doing 13233 * whatever it needs to do to clean up its state. 13234 */ 13235 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13236 ctl_set_task_aborted(&io->scsiio); 13237 13238 /* 13239 * If enabled, print command error status. 13240 */ 13241 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13242 (ctl_debug & CTL_DEBUG_INFO) != 0) 13243 ctl_io_error_print(io, NULL); 13244 13245 /* 13246 * Tell the FETD or the other shelf controller we're done with this 13247 * command. Note that only SCSI commands get to this point. Task 13248 * management commands are completed above. 13249 */ 13250 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13251 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13252 memset(&msg, 0, sizeof(msg)); 13253 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13254 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13255 msg.hdr.nexus = io->io_hdr.nexus; 13256 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13257 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13258 M_WAITOK); 13259 } 13260 if ((softc->ha_mode == CTL_HA_MODE_XFER) 13261 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13262 memset(&msg, 0, sizeof(msg)); 13263 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13264 msg.hdr.original_sc = io->io_hdr.original_sc; 13265 msg.hdr.nexus = io->io_hdr.nexus; 13266 msg.hdr.status = io->io_hdr.status; 13267 msg.scsi.scsi_status = io->scsiio.scsi_status; 13268 msg.scsi.tag_num = io->scsiio.tag_num; 13269 msg.scsi.tag_type = io->scsiio.tag_type; 13270 msg.scsi.sense_len = io->scsiio.sense_len; 13271 msg.scsi.sense_residual = io->scsiio.sense_residual; 13272 msg.scsi.residual = io->scsiio.residual; 13273 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13274 io->scsiio.sense_len); 13275 13276 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13277 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 13278 msg.scsi.sense_len, M_WAITOK); 13279 ctl_free_io(io); 13280 } else 13281 fe_done(io); 13282 13283 return (CTL_RETVAL_COMPLETE); 13284} 13285 13286#ifdef CTL_WITH_CA 13287/* 13288 * Front end should call this if it doesn't do autosense. When the request 13289 * sense comes back in from the initiator, we'll dequeue this and send it. 13290 */ 13291int 13292ctl_queue_sense(union ctl_io *io) 13293{ 13294 struct ctl_lun *lun; 13295 struct ctl_port *port; 13296 struct ctl_softc *softc; 13297 uint32_t initidx, targ_lun; 13298 13299 softc = control_softc; 13300 13301 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13302 13303 /* 13304 * LUN lookup will likely move to the ctl_work_thread() once we 13305 * have our new queueing infrastructure (that doesn't put things on 13306 * a per-LUN queue initially). That is so that we can handle 13307 * things like an INQUIRY to a LUN that we don't have enabled. We 13308 * can't deal with that right now. 13309 */ 13310 mtx_lock(&softc->ctl_lock); 13311 13312 /* 13313 * If we don't have a LUN for this, just toss the sense 13314 * information. 13315 */ 13316 port = ctl_io_port(&ctsio->io_hdr); 13317 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13318 if ((targ_lun < CTL_MAX_LUNS) 13319 && (softc->ctl_luns[targ_lun] != NULL)) 13320 lun = softc->ctl_luns[targ_lun]; 13321 else 13322 goto bailout; 13323 13324 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13325 13326 mtx_lock(&lun->lun_lock); 13327 /* 13328 * Already have CA set for this LUN...toss the sense information. 13329 */ 13330 if (ctl_is_set(lun->have_ca, initidx)) { 13331 mtx_unlock(&lun->lun_lock); 13332 goto bailout; 13333 } 13334 13335 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13336 MIN(sizeof(lun->pending_sense[initidx]), 13337 sizeof(io->scsiio.sense_data))); 13338 ctl_set_mask(lun->have_ca, initidx); 13339 mtx_unlock(&lun->lun_lock); 13340 13341bailout: 13342 mtx_unlock(&softc->ctl_lock); 13343 13344 ctl_free_io(io); 13345 13346 return (CTL_RETVAL_COMPLETE); 13347} 13348#endif 13349 13350/* 13351 * Primary command inlet from frontend ports. All SCSI and task I/O 13352 * requests must go through this function. 13353 */ 13354int 13355ctl_queue(union ctl_io *io) 13356{ 13357 struct ctl_port *port; 13358 13359 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13360 13361#ifdef CTL_TIME_IO 13362 io->io_hdr.start_time = time_uptime; 13363 getbintime(&io->io_hdr.start_bt); 13364#endif /* CTL_TIME_IO */ 13365 13366 /* Map FE-specific LUN ID into global one. */ 13367 port = ctl_io_port(&io->io_hdr); 13368 io->io_hdr.nexus.targ_mapped_lun = 13369 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13370 13371 switch (io->io_hdr.io_type) { 13372 case CTL_IO_SCSI: 13373 case CTL_IO_TASK: 13374 if (ctl_debug & CTL_DEBUG_CDB) 13375 ctl_io_print(io); 13376 ctl_enqueue_incoming(io); 13377 break; 13378 default: 13379 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13380 return (EINVAL); 13381 } 13382 13383 return (CTL_RETVAL_COMPLETE); 13384} 13385 13386#ifdef CTL_IO_DELAY 13387static void 13388ctl_done_timer_wakeup(void *arg) 13389{ 13390 union ctl_io *io; 13391 13392 io = (union ctl_io *)arg; 13393 ctl_done(io); 13394} 13395#endif /* CTL_IO_DELAY */ 13396 13397void 13398ctl_serseq_done(union ctl_io *io) 13399{ 13400 struct ctl_lun *lun; 13401 13402 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13403 if (lun->be_lun == NULL || 13404 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13405 return; 13406 mtx_lock(&lun->lun_lock); 13407 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13408 ctl_check_blocked(lun); 13409 mtx_unlock(&lun->lun_lock); 13410} 13411 13412void 13413ctl_done(union ctl_io *io) 13414{ 13415 13416 /* 13417 * Enable this to catch duplicate completion issues. 13418 */ 13419#if 0 13420 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13421 printf("%s: type %d msg %d cdb %x iptl: " 13422 "%u:%u:%u tag 0x%04x " 13423 "flag %#x status %x\n", 13424 __func__, 13425 io->io_hdr.io_type, 13426 io->io_hdr.msg_type, 13427 io->scsiio.cdb[0], 13428 io->io_hdr.nexus.initid, 13429 io->io_hdr.nexus.targ_port, 13430 io->io_hdr.nexus.targ_lun, 13431 (io->io_hdr.io_type == 13432 CTL_IO_TASK) ? 13433 io->taskio.tag_num : 13434 io->scsiio.tag_num, 13435 io->io_hdr.flags, 13436 io->io_hdr.status); 13437 } else 13438 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13439#endif 13440 13441 /* 13442 * This is an internal copy of an I/O, and should not go through 13443 * the normal done processing logic. 13444 */ 13445 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13446 return; 13447 13448#ifdef CTL_IO_DELAY 13449 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13450 struct ctl_lun *lun; 13451 13452 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13453 13454 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13455 } else { 13456 struct ctl_lun *lun; 13457 13458 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13459 13460 if ((lun != NULL) 13461 && (lun->delay_info.done_delay > 0)) { 13462 13463 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13464 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13465 callout_reset(&io->io_hdr.delay_callout, 13466 lun->delay_info.done_delay * hz, 13467 ctl_done_timer_wakeup, io); 13468 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13469 lun->delay_info.done_delay = 0; 13470 return; 13471 } 13472 } 13473#endif /* CTL_IO_DELAY */ 13474 13475 ctl_enqueue_done(io); 13476} 13477 13478static void 13479ctl_work_thread(void *arg) 13480{ 13481 struct ctl_thread *thr = (struct ctl_thread *)arg; 13482 struct ctl_softc *softc = thr->ctl_softc; 13483 union ctl_io *io; 13484 int retval; 13485 13486 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13487 13488 for (;;) { 13489 retval = 0; 13490 13491 /* 13492 * We handle the queues in this order: 13493 * - ISC 13494 * - done queue (to free up resources, unblock other commands) 13495 * - RtR queue 13496 * - incoming queue 13497 * 13498 * If those queues are empty, we break out of the loop and 13499 * go to sleep. 13500 */ 13501 mtx_lock(&thr->queue_lock); 13502 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13503 if (io != NULL) { 13504 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13505 mtx_unlock(&thr->queue_lock); 13506 ctl_handle_isc(io); 13507 continue; 13508 } 13509 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13510 if (io != NULL) { 13511 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13512 /* clear any blocked commands, call fe_done */ 13513 mtx_unlock(&thr->queue_lock); 13514 retval = ctl_process_done(io); 13515 continue; 13516 } 13517 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13518 if (io != NULL) { 13519 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13520 mtx_unlock(&thr->queue_lock); 13521 if (io->io_hdr.io_type == CTL_IO_TASK) 13522 ctl_run_task(io); 13523 else 13524 ctl_scsiio_precheck(softc, &io->scsiio); 13525 continue; 13526 } 13527 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13528 if (io != NULL) { 13529 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13530 mtx_unlock(&thr->queue_lock); 13531 retval = ctl_scsiio(&io->scsiio); 13532 if (retval != CTL_RETVAL_COMPLETE) 13533 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13534 continue; 13535 } 13536 13537 /* Sleep until we have something to do. */ 13538 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13539 } 13540} 13541 13542static void 13543ctl_lun_thread(void *arg) 13544{ 13545 struct ctl_softc *softc = (struct ctl_softc *)arg; 13546 struct ctl_be_lun *be_lun; 13547 int retval; 13548 13549 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13550 13551 for (;;) { 13552 retval = 0; 13553 mtx_lock(&softc->ctl_lock); 13554 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13555 if (be_lun != NULL) { 13556 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13557 mtx_unlock(&softc->ctl_lock); 13558 ctl_create_lun(be_lun); 13559 continue; 13560 } 13561 13562 /* Sleep until we have something to do. */ 13563 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13564 PDROP | PRIBIO, "-", 0); 13565 } 13566} 13567 13568static void 13569ctl_thresh_thread(void *arg) 13570{ 13571 struct ctl_softc *softc = (struct ctl_softc *)arg; 13572 struct ctl_lun *lun; 13573 struct ctl_be_lun *be_lun; 13574 struct scsi_da_rw_recovery_page *rwpage; 13575 struct ctl_logical_block_provisioning_page *page; 13576 const char *attr; 13577 union ctl_ha_msg msg; 13578 uint64_t thres, val; 13579 int i, e, set; 13580 13581 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13582 13583 for (;;) { 13584 mtx_lock(&softc->ctl_lock); 13585 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13586 be_lun = lun->be_lun; 13587 if ((lun->flags & CTL_LUN_DISABLED) || 13588 (lun->flags & CTL_LUN_OFFLINE) || 13589 lun->backend->lun_attr == NULL) 13590 continue; 13591 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13592 softc->ha_mode == CTL_HA_MODE_XFER) 13593 continue; 13594 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13595 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13596 continue; 13597 e = 0; 13598 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13599 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13600 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13601 continue; 13602 thres = scsi_4btoul(page->descr[i].count); 13603 thres <<= CTL_LBP_EXPONENT; 13604 switch (page->descr[i].resource) { 13605 case 0x01: 13606 attr = "blocksavail"; 13607 break; 13608 case 0x02: 13609 attr = "blocksused"; 13610 break; 13611 case 0xf1: 13612 attr = "poolblocksavail"; 13613 break; 13614 case 0xf2: 13615 attr = "poolblocksused"; 13616 break; 13617 default: 13618 continue; 13619 } 13620 mtx_unlock(&softc->ctl_lock); // XXX 13621 val = lun->backend->lun_attr( 13622 lun->be_lun->be_lun, attr); 13623 mtx_lock(&softc->ctl_lock); 13624 if (val == UINT64_MAX) 13625 continue; 13626 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13627 == SLBPPD_ARMING_INC) 13628 e = (val >= thres); 13629 else 13630 e = (val <= thres); 13631 if (e) 13632 break; 13633 } 13634 mtx_lock(&lun->lun_lock); 13635 if (e) { 13636 scsi_u64to8b((uint8_t *)&page->descr[i] - 13637 (uint8_t *)page, lun->ua_tpt_info); 13638 if (lun->lasttpt == 0 || 13639 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13640 lun->lasttpt = time_uptime; 13641 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13642 set = 1; 13643 } else 13644 set = 0; 13645 } else { 13646 lun->lasttpt = 0; 13647 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13648 set = -1; 13649 } 13650 mtx_unlock(&lun->lun_lock); 13651 if (set != 0 && 13652 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13653 /* Send msg to other side. */ 13654 bzero(&msg.ua, sizeof(msg.ua)); 13655 msg.hdr.msg_type = CTL_MSG_UA; 13656 msg.hdr.nexus.initid = -1; 13657 msg.hdr.nexus.targ_port = -1; 13658 msg.hdr.nexus.targ_lun = lun->lun; 13659 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13660 msg.ua.ua_all = 1; 13661 msg.ua.ua_set = (set > 0); 13662 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13663 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13664 mtx_unlock(&softc->ctl_lock); // XXX 13665 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13666 sizeof(msg.ua), M_WAITOK); 13667 mtx_lock(&softc->ctl_lock); 13668 } 13669 } 13670 mtx_unlock(&softc->ctl_lock); 13671 pause("-", CTL_LBP_PERIOD * hz); 13672 } 13673} 13674 13675static void 13676ctl_enqueue_incoming(union ctl_io *io) 13677{ 13678 struct ctl_softc *softc = control_softc; 13679 struct ctl_thread *thr; 13680 u_int idx; 13681 13682 idx = (io->io_hdr.nexus.targ_port * 127 + 13683 io->io_hdr.nexus.initid) % worker_threads; 13684 thr = &softc->threads[idx]; 13685 mtx_lock(&thr->queue_lock); 13686 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13687 mtx_unlock(&thr->queue_lock); 13688 wakeup(thr); 13689} 13690 13691static void 13692ctl_enqueue_rtr(union ctl_io *io) 13693{ 13694 struct ctl_softc *softc = control_softc; 13695 struct ctl_thread *thr; 13696 13697 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13698 mtx_lock(&thr->queue_lock); 13699 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13700 mtx_unlock(&thr->queue_lock); 13701 wakeup(thr); 13702} 13703 13704static void 13705ctl_enqueue_done(union ctl_io *io) 13706{ 13707 struct ctl_softc *softc = control_softc; 13708 struct ctl_thread *thr; 13709 13710 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13711 mtx_lock(&thr->queue_lock); 13712 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13713 mtx_unlock(&thr->queue_lock); 13714 wakeup(thr); 13715} 13716 13717static void 13718ctl_enqueue_isc(union ctl_io *io) 13719{ 13720 struct ctl_softc *softc = control_softc; 13721 struct ctl_thread *thr; 13722 13723 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13724 mtx_lock(&thr->queue_lock); 13725 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13726 mtx_unlock(&thr->queue_lock); 13727 wakeup(thr); 13728} 13729 13730/* 13731 * vim: ts=8 13732 */ 13733