ctl.c revision 288214
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 288214 2015-09-25 09:55:51Z 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_ha_datamove(union ctl_io *io) 519{ 520 struct ctl_lun *lun; 521 struct ctl_sg_entry *sgl; 522 union ctl_ha_msg msg; 523 uint32_t sg_entries_sent; 524 int do_sg_copy, i, j; 525 526 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 527 memset(&msg.dt, 0, sizeof(msg.dt)); 528 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 529 msg.hdr.original_sc = io->io_hdr.original_sc; 530 msg.hdr.serializing_sc = io; 531 msg.hdr.nexus = io->io_hdr.nexus; 532 msg.hdr.status = io->io_hdr.status; 533 msg.dt.flags = io->io_hdr.flags; 534 535 /* 536 * We convert everything into a S/G list here. We can't 537 * pass by reference, only by value between controllers. 538 * So we can't pass a pointer to the S/G list, only as many 539 * S/G entries as we can fit in here. If it's possible for 540 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 541 * then we need to break this up into multiple transfers. 542 */ 543 if (io->scsiio.kern_sg_entries == 0) { 544 msg.dt.kern_sg_entries = 1; 545#if 0 546 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 547 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 548 } else { 549 /* XXX KDM use busdma here! */ 550 msg.dt.sg_list[0].addr = 551 (void *)vtophys(io->scsiio.kern_data_ptr); 552 } 553#else 554 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 555 ("HA does not support BUS_ADDR")); 556 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 557#endif 558 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 559 do_sg_copy = 0; 560 } else { 561 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 562 do_sg_copy = 1; 563 } 564 565 msg.dt.kern_data_len = io->scsiio.kern_data_len; 566 msg.dt.kern_total_len = io->scsiio.kern_total_len; 567 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 568 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 569 msg.dt.sg_sequence = 0; 570 571 /* 572 * Loop until we've sent all of the S/G entries. On the 573 * other end, we'll recompose these S/G entries into one 574 * contiguous list before processing. 575 */ 576 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 577 msg.dt.sg_sequence++) { 578 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 579 sizeof(msg.dt.sg_list[0])), 580 msg.dt.kern_sg_entries - sg_entries_sent); 581 if (do_sg_copy != 0) { 582 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 583 for (i = sg_entries_sent, j = 0; 584 i < msg.dt.cur_sg_entries; i++, j++) { 585#if 0 586 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 587 msg.dt.sg_list[j].addr = sgl[i].addr; 588 } else { 589 /* XXX KDM use busdma here! */ 590 msg.dt.sg_list[j].addr = 591 (void *)vtophys(sgl[i].addr); 592 } 593#else 594 KASSERT((io->io_hdr.flags & 595 CTL_FLAG_BUS_ADDR) == 0, 596 ("HA does not support BUS_ADDR")); 597 msg.dt.sg_list[j].addr = sgl[i].addr; 598#endif 599 msg.dt.sg_list[j].len = sgl[i].len; 600 } 601 } 602 603 sg_entries_sent += msg.dt.cur_sg_entries; 604 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 605 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 606 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 607 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 608 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 609 io->io_hdr.port_status = 31341; 610 io->scsiio.be_move_done(io); 611 return; 612 } 613 msg.dt.sent_sg_entries = sg_entries_sent; 614 } 615 616 /* 617 * Officially handover the request from us to peer. 618 * If failover has just happened, then we must return error. 619 * If failover happen just after, then it is not our problem. 620 */ 621 if (lun) 622 mtx_lock(&lun->lun_lock); 623 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 624 if (lun) 625 mtx_unlock(&lun->lun_lock); 626 io->io_hdr.port_status = 31342; 627 io->scsiio.be_move_done(io); 628 return; 629 } 630 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 631 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 632 if (lun) 633 mtx_unlock(&lun->lun_lock); 634} 635 636static void 637ctl_ha_done(union ctl_io *io) 638{ 639 union ctl_ha_msg msg; 640 641 if (io->io_hdr.io_type == CTL_IO_SCSI) { 642 memset(&msg, 0, sizeof(msg)); 643 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 644 msg.hdr.original_sc = io->io_hdr.original_sc; 645 msg.hdr.nexus = io->io_hdr.nexus; 646 msg.hdr.status = io->io_hdr.status; 647 msg.scsi.scsi_status = io->scsiio.scsi_status; 648 msg.scsi.tag_num = io->scsiio.tag_num; 649 msg.scsi.tag_type = io->scsiio.tag_type; 650 msg.scsi.sense_len = io->scsiio.sense_len; 651 msg.scsi.sense_residual = io->scsiio.sense_residual; 652 msg.scsi.residual = io->scsiio.residual; 653 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 654 io->scsiio.sense_len); 655 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 656 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 657 msg.scsi.sense_len, M_WAITOK); 658 } 659 ctl_free_io(io); 660} 661 662static void 663ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 664 union ctl_ha_msg *msg_info) 665{ 666 struct ctl_scsiio *ctsio; 667 668 if (msg_info->hdr.original_sc == NULL) { 669 printf("%s: original_sc == NULL!\n", __func__); 670 /* XXX KDM now what? */ 671 return; 672 } 673 674 ctsio = &msg_info->hdr.original_sc->scsiio; 675 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 676 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 677 ctsio->io_hdr.status = msg_info->hdr.status; 678 ctsio->scsi_status = msg_info->scsi.scsi_status; 679 ctsio->sense_len = msg_info->scsi.sense_len; 680 ctsio->sense_residual = msg_info->scsi.sense_residual; 681 ctsio->residual = msg_info->scsi.residual; 682 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 683 msg_info->scsi.sense_len); 684 ctl_enqueue_isc((union ctl_io *)ctsio); 685} 686 687static void 688ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 689 union ctl_ha_msg *msg_info) 690{ 691 struct ctl_scsiio *ctsio; 692 693 if (msg_info->hdr.serializing_sc == NULL) { 694 printf("%s: serializing_sc == NULL!\n", __func__); 695 /* XXX KDM now what? */ 696 return; 697 } 698 699 ctsio = &msg_info->hdr.serializing_sc->scsiio; 700 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 701 ctl_enqueue_isc((union ctl_io *)ctsio); 702} 703 704void 705ctl_isc_announce_lun(struct ctl_lun *lun) 706{ 707 struct ctl_softc *softc = lun->ctl_softc; 708 union ctl_ha_msg *msg; 709 struct ctl_ha_msg_lun_pr_key pr_key; 710 int i, k; 711 712 if (softc->ha_link != CTL_HA_LINK_ONLINE) 713 return; 714 mtx_lock(&lun->lun_lock); 715 i = sizeof(msg->lun); 716 if (lun->lun_devid) 717 i += lun->lun_devid->len; 718 i += sizeof(pr_key) * lun->pr_key_count; 719alloc: 720 mtx_unlock(&lun->lun_lock); 721 msg = malloc(i, M_CTL, M_WAITOK); 722 mtx_lock(&lun->lun_lock); 723 k = sizeof(msg->lun); 724 if (lun->lun_devid) 725 k += lun->lun_devid->len; 726 k += sizeof(pr_key) * lun->pr_key_count; 727 if (i < k) { 728 free(msg, M_CTL); 729 i = k; 730 goto alloc; 731 } 732 bzero(&msg->lun, sizeof(msg->lun)); 733 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 734 msg->hdr.nexus.targ_lun = lun->lun; 735 msg->hdr.nexus.targ_mapped_lun = lun->lun; 736 msg->lun.flags = lun->flags; 737 msg->lun.pr_generation = lun->PRGeneration; 738 msg->lun.pr_res_idx = lun->pr_res_idx; 739 msg->lun.pr_res_type = lun->res_type; 740 msg->lun.pr_key_count = lun->pr_key_count; 741 i = 0; 742 if (lun->lun_devid) { 743 msg->lun.lun_devid_len = lun->lun_devid->len; 744 memcpy(&msg->lun.data[i], lun->lun_devid->data, 745 msg->lun.lun_devid_len); 746 i += msg->lun.lun_devid_len; 747 } 748 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 749 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 750 continue; 751 pr_key.pr_iid = k; 752 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 753 i += sizeof(pr_key); 754 } 755 mtx_unlock(&lun->lun_lock); 756 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 757 M_WAITOK); 758 free(msg, M_CTL); 759 760 if (lun->flags & CTL_LUN_PRIMARY_SC) { 761 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 762 ctl_isc_announce_mode(lun, -1, 763 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 764 lun->mode_pages.index[i].subpage); 765 } 766 } 767} 768 769void 770ctl_isc_announce_port(struct ctl_port *port) 771{ 772 struct ctl_softc *softc = port->ctl_softc; 773 union ctl_ha_msg *msg; 774 int i; 775 776 if (port->targ_port < softc->port_min || 777 port->targ_port >= softc->port_max || 778 softc->ha_link != CTL_HA_LINK_ONLINE) 779 return; 780 i = sizeof(msg->port) + strlen(port->port_name) + 1; 781 if (port->lun_map) 782 i += sizeof(uint32_t) * CTL_MAX_LUNS; 783 if (port->port_devid) 784 i += port->port_devid->len; 785 if (port->target_devid) 786 i += port->target_devid->len; 787 if (port->init_devid) 788 i += port->init_devid->len; 789 msg = malloc(i, M_CTL, M_WAITOK); 790 bzero(&msg->port, sizeof(msg->port)); 791 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 792 msg->hdr.nexus.targ_port = port->targ_port; 793 msg->port.port_type = port->port_type; 794 msg->port.physical_port = port->physical_port; 795 msg->port.virtual_port = port->virtual_port; 796 msg->port.status = port->status; 797 i = 0; 798 msg->port.name_len = sprintf(&msg->port.data[i], 799 "%d:%s", softc->ha_id, port->port_name) + 1; 800 i += msg->port.name_len; 801 if (port->lun_map) { 802 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS; 803 memcpy(&msg->port.data[i], port->lun_map, 804 msg->port.lun_map_len); 805 i += msg->port.lun_map_len; 806 } 807 if (port->port_devid) { 808 msg->port.port_devid_len = port->port_devid->len; 809 memcpy(&msg->port.data[i], port->port_devid->data, 810 msg->port.port_devid_len); 811 i += msg->port.port_devid_len; 812 } 813 if (port->target_devid) { 814 msg->port.target_devid_len = port->target_devid->len; 815 memcpy(&msg->port.data[i], port->target_devid->data, 816 msg->port.target_devid_len); 817 i += msg->port.target_devid_len; 818 } 819 if (port->init_devid) { 820 msg->port.init_devid_len = port->init_devid->len; 821 memcpy(&msg->port.data[i], port->init_devid->data, 822 msg->port.init_devid_len); 823 i += msg->port.init_devid_len; 824 } 825 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 826 M_WAITOK); 827 free(msg, M_CTL); 828} 829 830void 831ctl_isc_announce_iid(struct ctl_port *port, int iid) 832{ 833 struct ctl_softc *softc = port->ctl_softc; 834 union ctl_ha_msg *msg; 835 int i, l; 836 837 if (port->targ_port < softc->port_min || 838 port->targ_port >= softc->port_max || 839 softc->ha_link != CTL_HA_LINK_ONLINE) 840 return; 841 mtx_lock(&softc->ctl_lock); 842 i = sizeof(msg->iid); 843 l = 0; 844 if (port->wwpn_iid[iid].name) 845 l = strlen(port->wwpn_iid[iid].name) + 1; 846 i += l; 847 msg = malloc(i, M_CTL, M_NOWAIT); 848 if (msg == NULL) { 849 mtx_unlock(&softc->ctl_lock); 850 return; 851 } 852 bzero(&msg->iid, sizeof(msg->iid)); 853 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 854 msg->hdr.nexus.targ_port = port->targ_port; 855 msg->hdr.nexus.initid = iid; 856 msg->iid.in_use = port->wwpn_iid[iid].in_use; 857 msg->iid.name_len = l; 858 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 859 if (port->wwpn_iid[iid].name) 860 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 861 mtx_unlock(&softc->ctl_lock); 862 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 863 free(msg, M_CTL); 864} 865 866void 867ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 868 uint8_t page, uint8_t subpage) 869{ 870 struct ctl_softc *softc = lun->ctl_softc; 871 union ctl_ha_msg msg; 872 int i; 873 874 if (softc->ha_link != CTL_HA_LINK_ONLINE) 875 return; 876 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 877 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 878 page && lun->mode_pages.index[i].subpage == subpage) 879 break; 880 } 881 if (i == CTL_NUM_MODE_PAGES) 882 return; 883 bzero(&msg.mode, sizeof(msg.mode)); 884 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 885 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 886 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 887 msg.hdr.nexus.targ_lun = lun->lun; 888 msg.hdr.nexus.targ_mapped_lun = lun->lun; 889 msg.mode.page_code = page; 890 msg.mode.subpage = subpage; 891 msg.mode.page_len = lun->mode_pages.index[i].page_len; 892 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 893 msg.mode.page_len); 894 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 895 M_WAITOK); 896} 897 898static void 899ctl_isc_ha_link_up(struct ctl_softc *softc) 900{ 901 struct ctl_port *port; 902 struct ctl_lun *lun; 903 union ctl_ha_msg msg; 904 int i; 905 906 /* Announce this node parameters to peer for validation. */ 907 msg.login.msg_type = CTL_MSG_LOGIN; 908 msg.login.version = CTL_HA_VERSION; 909 msg.login.ha_mode = softc->ha_mode; 910 msg.login.ha_id = softc->ha_id; 911 msg.login.max_luns = CTL_MAX_LUNS; 912 msg.login.max_ports = CTL_MAX_PORTS; 913 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 914 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 915 M_WAITOK); 916 917 STAILQ_FOREACH(port, &softc->port_list, links) { 918 ctl_isc_announce_port(port); 919 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 920 if (port->wwpn_iid[i].in_use) 921 ctl_isc_announce_iid(port, i); 922 } 923 } 924 STAILQ_FOREACH(lun, &softc->lun_list, links) 925 ctl_isc_announce_lun(lun); 926} 927 928static void 929ctl_isc_ha_link_down(struct ctl_softc *softc) 930{ 931 struct ctl_port *port; 932 struct ctl_lun *lun; 933 union ctl_io *io; 934 int i; 935 936 mtx_lock(&softc->ctl_lock); 937 STAILQ_FOREACH(lun, &softc->lun_list, links) { 938 mtx_lock(&lun->lun_lock); 939 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 940 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 941 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 942 } 943 mtx_unlock(&lun->lun_lock); 944 945 mtx_unlock(&softc->ctl_lock); 946 io = ctl_alloc_io(softc->othersc_pool); 947 mtx_lock(&softc->ctl_lock); 948 ctl_zero_io(io); 949 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 950 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 951 ctl_enqueue_isc(io); 952 } 953 954 STAILQ_FOREACH(port, &softc->port_list, links) { 955 if (port->targ_port >= softc->port_min && 956 port->targ_port < softc->port_max) 957 continue; 958 port->status &= ~CTL_PORT_STATUS_ONLINE; 959 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 960 port->wwpn_iid[i].in_use = 0; 961 free(port->wwpn_iid[i].name, M_CTL); 962 port->wwpn_iid[i].name = NULL; 963 } 964 } 965 mtx_unlock(&softc->ctl_lock); 966} 967 968static void 969ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 970{ 971 struct ctl_lun *lun; 972 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 973 974 mtx_lock(&softc->ctl_lock); 975 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS && 976 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) { 977 mtx_lock(&lun->lun_lock); 978 mtx_unlock(&softc->ctl_lock); 979 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && 980 msg->ua.ua_set) 981 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 982 if (msg->ua.ua_all) { 983 if (msg->ua.ua_set) 984 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 985 else 986 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 987 } else { 988 if (msg->ua.ua_set) 989 ctl_est_ua(lun, iid, msg->ua.ua_type); 990 else 991 ctl_clr_ua(lun, iid, msg->ua.ua_type); 992 } 993 mtx_unlock(&lun->lun_lock); 994 } else 995 mtx_unlock(&softc->ctl_lock); 996} 997 998static void 999ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1000{ 1001 struct ctl_lun *lun; 1002 struct ctl_ha_msg_lun_pr_key pr_key; 1003 int i, k; 1004 ctl_lun_flags oflags; 1005 uint32_t targ_lun; 1006 1007 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1008 mtx_lock(&softc->ctl_lock); 1009 if ((targ_lun >= CTL_MAX_LUNS) || 1010 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 1011 mtx_unlock(&softc->ctl_lock); 1012 return; 1013 } 1014 mtx_lock(&lun->lun_lock); 1015 mtx_unlock(&softc->ctl_lock); 1016 if (lun->flags & CTL_LUN_DISABLED) { 1017 mtx_unlock(&lun->lun_lock); 1018 return; 1019 } 1020 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1021 if (msg->lun.lun_devid_len != i || (i > 0 && 1022 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1023 mtx_unlock(&lun->lun_lock); 1024 printf("%s: Received conflicting HA LUN %d\n", 1025 __func__, msg->hdr.nexus.targ_lun); 1026 return; 1027 } else { 1028 /* Record whether peer is primary. */ 1029 oflags = lun->flags; 1030 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1031 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1032 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1033 else 1034 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1035 if (oflags != lun->flags) 1036 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1037 1038 /* If peer is primary and we are not -- use data */ 1039 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1040 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1041 lun->PRGeneration = msg->lun.pr_generation; 1042 lun->pr_res_idx = msg->lun.pr_res_idx; 1043 lun->res_type = msg->lun.pr_res_type; 1044 lun->pr_key_count = msg->lun.pr_key_count; 1045 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1046 ctl_clr_prkey(lun, k); 1047 for (k = 0; k < msg->lun.pr_key_count; k++) { 1048 memcpy(&pr_key, &msg->lun.data[i], 1049 sizeof(pr_key)); 1050 ctl_alloc_prkey(lun, pr_key.pr_iid); 1051 ctl_set_prkey(lun, pr_key.pr_iid, 1052 pr_key.pr_key); 1053 i += sizeof(pr_key); 1054 } 1055 } 1056 1057 mtx_unlock(&lun->lun_lock); 1058 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1059 __func__, msg->hdr.nexus.targ_lun, 1060 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1061 "primary" : "secondary")); 1062 1063 /* If we are primary but peer doesn't know -- notify */ 1064 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1065 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1066 ctl_isc_announce_lun(lun); 1067 } 1068} 1069 1070static void 1071ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1072{ 1073 struct ctl_port *port; 1074 struct ctl_lun *lun; 1075 int i, new; 1076 1077 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1078 if (port == NULL) { 1079 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1080 msg->hdr.nexus.targ_port)); 1081 new = 1; 1082 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1083 port->frontend = &ha_frontend; 1084 port->targ_port = msg->hdr.nexus.targ_port; 1085 port->fe_datamove = ctl_ha_datamove; 1086 port->fe_done = ctl_ha_done; 1087 } else if (port->frontend == &ha_frontend) { 1088 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1089 msg->hdr.nexus.targ_port)); 1090 new = 0; 1091 } else { 1092 printf("%s: Received conflicting HA port %d\n", 1093 __func__, msg->hdr.nexus.targ_port); 1094 return; 1095 } 1096 port->port_type = msg->port.port_type; 1097 port->physical_port = msg->port.physical_port; 1098 port->virtual_port = msg->port.virtual_port; 1099 port->status = msg->port.status; 1100 i = 0; 1101 free(port->port_name, M_CTL); 1102 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1103 M_CTL); 1104 i += msg->port.name_len; 1105 if (msg->port.lun_map_len != 0) { 1106 if (port->lun_map == NULL) 1107 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 1108 M_CTL, M_WAITOK); 1109 memcpy(port->lun_map, &msg->port.data[i], 1110 sizeof(uint32_t) * CTL_MAX_LUNS); 1111 i += msg->port.lun_map_len; 1112 } else { 1113 free(port->lun_map, M_CTL); 1114 port->lun_map = NULL; 1115 } 1116 if (msg->port.port_devid_len != 0) { 1117 if (port->port_devid == NULL || 1118 port->port_devid->len != msg->port.port_devid_len) { 1119 free(port->port_devid, M_CTL); 1120 port->port_devid = malloc(sizeof(struct ctl_devid) + 1121 msg->port.port_devid_len, M_CTL, M_WAITOK); 1122 } 1123 memcpy(port->port_devid->data, &msg->port.data[i], 1124 msg->port.port_devid_len); 1125 port->port_devid->len = msg->port.port_devid_len; 1126 i += msg->port.port_devid_len; 1127 } else { 1128 free(port->port_devid, M_CTL); 1129 port->port_devid = NULL; 1130 } 1131 if (msg->port.target_devid_len != 0) { 1132 if (port->target_devid == NULL || 1133 port->target_devid->len != msg->port.target_devid_len) { 1134 free(port->target_devid, M_CTL); 1135 port->target_devid = malloc(sizeof(struct ctl_devid) + 1136 msg->port.target_devid_len, M_CTL, M_WAITOK); 1137 } 1138 memcpy(port->target_devid->data, &msg->port.data[i], 1139 msg->port.target_devid_len); 1140 port->target_devid->len = msg->port.target_devid_len; 1141 i += msg->port.target_devid_len; 1142 } else { 1143 free(port->target_devid, M_CTL); 1144 port->target_devid = NULL; 1145 } 1146 if (msg->port.init_devid_len != 0) { 1147 if (port->init_devid == NULL || 1148 port->init_devid->len != msg->port.init_devid_len) { 1149 free(port->init_devid, M_CTL); 1150 port->init_devid = malloc(sizeof(struct ctl_devid) + 1151 msg->port.init_devid_len, M_CTL, M_WAITOK); 1152 } 1153 memcpy(port->init_devid->data, &msg->port.data[i], 1154 msg->port.init_devid_len); 1155 port->init_devid->len = msg->port.init_devid_len; 1156 i += msg->port.init_devid_len; 1157 } else { 1158 free(port->init_devid, M_CTL); 1159 port->init_devid = NULL; 1160 } 1161 if (new) { 1162 if (ctl_port_register(port) != 0) { 1163 printf("%s: ctl_port_register() failed with error\n", 1164 __func__); 1165 } 1166 } 1167 mtx_lock(&softc->ctl_lock); 1168 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1169 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 1170 continue; 1171 mtx_lock(&lun->lun_lock); 1172 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1173 mtx_unlock(&lun->lun_lock); 1174 } 1175 mtx_unlock(&softc->ctl_lock); 1176} 1177 1178static void 1179ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1180{ 1181 struct ctl_port *port; 1182 int iid; 1183 1184 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1185 if (port == NULL) { 1186 printf("%s: Received IID for unknown port %d\n", 1187 __func__, msg->hdr.nexus.targ_port); 1188 return; 1189 } 1190 iid = msg->hdr.nexus.initid; 1191 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1192 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1193 free(port->wwpn_iid[iid].name, M_CTL); 1194 if (msg->iid.name_len) { 1195 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1196 msg->iid.name_len, M_CTL); 1197 } else 1198 port->wwpn_iid[iid].name = NULL; 1199} 1200 1201static void 1202ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1203{ 1204 1205 if (msg->login.version != CTL_HA_VERSION) { 1206 printf("CTL HA peers have different versions %d != %d\n", 1207 msg->login.version, CTL_HA_VERSION); 1208 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1209 return; 1210 } 1211 if (msg->login.ha_mode != softc->ha_mode) { 1212 printf("CTL HA peers have different ha_mode %d != %d\n", 1213 msg->login.ha_mode, softc->ha_mode); 1214 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1215 return; 1216 } 1217 if (msg->login.ha_id == softc->ha_id) { 1218 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1219 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1220 return; 1221 } 1222 if (msg->login.max_luns != CTL_MAX_LUNS || 1223 msg->login.max_ports != CTL_MAX_PORTS || 1224 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1225 printf("CTL HA peers have different limits\n"); 1226 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1227 return; 1228 } 1229} 1230 1231static void 1232ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1233{ 1234 struct ctl_lun *lun; 1235 int i; 1236 uint32_t initidx, targ_lun; 1237 1238 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1239 mtx_lock(&softc->ctl_lock); 1240 if ((targ_lun >= CTL_MAX_LUNS) || 1241 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 1242 mtx_unlock(&softc->ctl_lock); 1243 return; 1244 } 1245 mtx_lock(&lun->lun_lock); 1246 mtx_unlock(&softc->ctl_lock); 1247 if (lun->flags & CTL_LUN_DISABLED) { 1248 mtx_unlock(&lun->lun_lock); 1249 return; 1250 } 1251 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1252 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1253 msg->mode.page_code && 1254 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1255 break; 1256 } 1257 if (i == CTL_NUM_MODE_PAGES) { 1258 mtx_unlock(&lun->lun_lock); 1259 return; 1260 } 1261 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1262 lun->mode_pages.index[i].page_len); 1263 initidx = ctl_get_initindex(&msg->hdr.nexus); 1264 if (initidx != -1) 1265 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1266 mtx_unlock(&lun->lun_lock); 1267} 1268 1269/* 1270 * ISC (Inter Shelf Communication) event handler. Events from the HA 1271 * subsystem come in here. 1272 */ 1273static void 1274ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1275{ 1276 struct ctl_softc *softc = control_softc; 1277 union ctl_io *io; 1278 struct ctl_prio *presio; 1279 ctl_ha_status isc_status; 1280 1281 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1282 if (event == CTL_HA_EVT_MSG_RECV) { 1283 union ctl_ha_msg *msg, msgbuf; 1284 1285 if (param > sizeof(msgbuf)) 1286 msg = malloc(param, M_CTL, M_WAITOK); 1287 else 1288 msg = &msgbuf; 1289 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1290 M_WAITOK); 1291 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1292 printf("%s: Error receiving message: %d\n", 1293 __func__, isc_status); 1294 if (msg != &msgbuf) 1295 free(msg, M_CTL); 1296 return; 1297 } 1298 1299 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1300 switch (msg->hdr.msg_type) { 1301 case CTL_MSG_SERIALIZE: 1302 io = ctl_alloc_io(softc->othersc_pool); 1303 ctl_zero_io(io); 1304 // populate ctsio from msg 1305 io->io_hdr.io_type = CTL_IO_SCSI; 1306 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1307 io->io_hdr.original_sc = msg->hdr.original_sc; 1308 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1309 CTL_FLAG_IO_ACTIVE; 1310 /* 1311 * If we're in serialization-only mode, we don't 1312 * want to go through full done processing. Thus 1313 * the COPY flag. 1314 * 1315 * XXX KDM add another flag that is more specific. 1316 */ 1317 if (softc->ha_mode != CTL_HA_MODE_XFER) 1318 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1319 io->io_hdr.nexus = msg->hdr.nexus; 1320#if 0 1321 printf("port %u, iid %u, lun %u\n", 1322 io->io_hdr.nexus.targ_port, 1323 io->io_hdr.nexus.initid, 1324 io->io_hdr.nexus.targ_lun); 1325#endif 1326 io->scsiio.tag_num = msg->scsi.tag_num; 1327 io->scsiio.tag_type = msg->scsi.tag_type; 1328#ifdef CTL_TIME_IO 1329 io->io_hdr.start_time = time_uptime; 1330 getbintime(&io->io_hdr.start_bt); 1331#endif /* CTL_TIME_IO */ 1332 io->scsiio.cdb_len = msg->scsi.cdb_len; 1333 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1334 CTL_MAX_CDBLEN); 1335 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1336 const struct ctl_cmd_entry *entry; 1337 1338 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1339 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1340 io->io_hdr.flags |= 1341 entry->flags & CTL_FLAG_DATA_MASK; 1342 } 1343 ctl_enqueue_isc(io); 1344 break; 1345 1346 /* Performed on the Originating SC, XFER mode only */ 1347 case CTL_MSG_DATAMOVE: { 1348 struct ctl_sg_entry *sgl; 1349 int i, j; 1350 1351 io = msg->hdr.original_sc; 1352 if (io == NULL) { 1353 printf("%s: original_sc == NULL!\n", __func__); 1354 /* XXX KDM do something here */ 1355 break; 1356 } 1357 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1358 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1359 /* 1360 * Keep track of this, we need to send it back over 1361 * when the datamove is complete. 1362 */ 1363 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1364 if (msg->hdr.status == CTL_SUCCESS) 1365 io->io_hdr.status = msg->hdr.status; 1366 1367 if (msg->dt.sg_sequence == 0) { 1368#ifdef CTL_TIME_IO 1369 getbintime(&io->io_hdr.dma_start_bt); 1370#endif 1371 i = msg->dt.kern_sg_entries + 1372 msg->dt.kern_data_len / 1373 CTL_HA_DATAMOVE_SEGMENT + 1; 1374 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1375 M_WAITOK | M_ZERO); 1376 io->io_hdr.remote_sglist = sgl; 1377 io->io_hdr.local_sglist = 1378 &sgl[msg->dt.kern_sg_entries]; 1379 1380 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1381 1382 io->scsiio.kern_sg_entries = 1383 msg->dt.kern_sg_entries; 1384 io->scsiio.rem_sg_entries = 1385 msg->dt.kern_sg_entries; 1386 io->scsiio.kern_data_len = 1387 msg->dt.kern_data_len; 1388 io->scsiio.kern_total_len = 1389 msg->dt.kern_total_len; 1390 io->scsiio.kern_data_resid = 1391 msg->dt.kern_data_resid; 1392 io->scsiio.kern_rel_offset = 1393 msg->dt.kern_rel_offset; 1394 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1395 io->io_hdr.flags |= msg->dt.flags & 1396 CTL_FLAG_BUS_ADDR; 1397 } else 1398 sgl = (struct ctl_sg_entry *) 1399 io->scsiio.kern_data_ptr; 1400 1401 for (i = msg->dt.sent_sg_entries, j = 0; 1402 i < (msg->dt.sent_sg_entries + 1403 msg->dt.cur_sg_entries); i++, j++) { 1404 sgl[i].addr = msg->dt.sg_list[j].addr; 1405 sgl[i].len = msg->dt.sg_list[j].len; 1406 1407#if 0 1408 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1409 __func__, sgl[i].addr, sgl[i].len, j, i); 1410#endif 1411 } 1412 1413 /* 1414 * If this is the last piece of the I/O, we've got 1415 * the full S/G list. Queue processing in the thread. 1416 * Otherwise wait for the next piece. 1417 */ 1418 if (msg->dt.sg_last != 0) 1419 ctl_enqueue_isc(io); 1420 break; 1421 } 1422 /* Performed on the Serializing (primary) SC, XFER mode only */ 1423 case CTL_MSG_DATAMOVE_DONE: { 1424 if (msg->hdr.serializing_sc == NULL) { 1425 printf("%s: serializing_sc == NULL!\n", 1426 __func__); 1427 /* XXX KDM now what? */ 1428 break; 1429 } 1430 /* 1431 * We grab the sense information here in case 1432 * there was a failure, so we can return status 1433 * back to the initiator. 1434 */ 1435 io = msg->hdr.serializing_sc; 1436 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1437 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1438 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1439 io->io_hdr.port_status = msg->scsi.fetd_status; 1440 io->scsiio.residual = msg->scsi.residual; 1441 if (msg->hdr.status != CTL_STATUS_NONE) { 1442 io->io_hdr.status = msg->hdr.status; 1443 io->scsiio.scsi_status = msg->scsi.scsi_status; 1444 io->scsiio.sense_len = msg->scsi.sense_len; 1445 io->scsiio.sense_residual =msg->scsi.sense_residual; 1446 memcpy(&io->scsiio.sense_data, 1447 &msg->scsi.sense_data, 1448 msg->scsi.sense_len); 1449 if (msg->hdr.status == CTL_SUCCESS) 1450 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1451 } 1452 ctl_enqueue_isc(io); 1453 break; 1454 } 1455 1456 /* Preformed on Originating SC, SER_ONLY mode */ 1457 case CTL_MSG_R2R: 1458 io = msg->hdr.original_sc; 1459 if (io == NULL) { 1460 printf("%s: original_sc == NULL!\n", 1461 __func__); 1462 break; 1463 } 1464 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1465 io->io_hdr.msg_type = CTL_MSG_R2R; 1466 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1467 ctl_enqueue_isc(io); 1468 break; 1469 1470 /* 1471 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1472 * mode. 1473 * Performed on the Originating (i.e. secondary) SC in XFER 1474 * mode 1475 */ 1476 case CTL_MSG_FINISH_IO: 1477 if (softc->ha_mode == CTL_HA_MODE_XFER) 1478 ctl_isc_handler_finish_xfer(softc, msg); 1479 else 1480 ctl_isc_handler_finish_ser_only(softc, msg); 1481 break; 1482 1483 /* Preformed on Originating SC */ 1484 case CTL_MSG_BAD_JUJU: 1485 io = msg->hdr.original_sc; 1486 if (io == NULL) { 1487 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1488 __func__); 1489 break; 1490 } 1491 ctl_copy_sense_data(msg, io); 1492 /* 1493 * IO should have already been cleaned up on other 1494 * SC so clear this flag so we won't send a message 1495 * back to finish the IO there. 1496 */ 1497 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1498 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1499 1500 /* io = msg->hdr.serializing_sc; */ 1501 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1502 ctl_enqueue_isc(io); 1503 break; 1504 1505 /* Handle resets sent from the other side */ 1506 case CTL_MSG_MANAGE_TASKS: { 1507 struct ctl_taskio *taskio; 1508 taskio = (struct ctl_taskio *)ctl_alloc_io( 1509 softc->othersc_pool); 1510 ctl_zero_io((union ctl_io *)taskio); 1511 taskio->io_hdr.io_type = CTL_IO_TASK; 1512 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1513 taskio->io_hdr.nexus = msg->hdr.nexus; 1514 taskio->task_action = msg->task.task_action; 1515 taskio->tag_num = msg->task.tag_num; 1516 taskio->tag_type = msg->task.tag_type; 1517#ifdef CTL_TIME_IO 1518 taskio->io_hdr.start_time = time_uptime; 1519 getbintime(&taskio->io_hdr.start_bt); 1520#endif /* CTL_TIME_IO */ 1521 ctl_run_task((union ctl_io *)taskio); 1522 break; 1523 } 1524 /* Persistent Reserve action which needs attention */ 1525 case CTL_MSG_PERS_ACTION: 1526 presio = (struct ctl_prio *)ctl_alloc_io( 1527 softc->othersc_pool); 1528 ctl_zero_io((union ctl_io *)presio); 1529 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1530 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1531 presio->io_hdr.nexus = msg->hdr.nexus; 1532 presio->pr_msg = msg->pr; 1533 ctl_enqueue_isc((union ctl_io *)presio); 1534 break; 1535 case CTL_MSG_UA: 1536 ctl_isc_ua(softc, msg, param); 1537 break; 1538 case CTL_MSG_PORT_SYNC: 1539 ctl_isc_port_sync(softc, msg, param); 1540 break; 1541 case CTL_MSG_LUN_SYNC: 1542 ctl_isc_lun_sync(softc, msg, param); 1543 break; 1544 case CTL_MSG_IID_SYNC: 1545 ctl_isc_iid_sync(softc, msg, param); 1546 break; 1547 case CTL_MSG_LOGIN: 1548 ctl_isc_login(softc, msg, param); 1549 break; 1550 case CTL_MSG_MODE_SYNC: 1551 ctl_isc_mode_sync(softc, msg, param); 1552 break; 1553 default: 1554 printf("Received HA message of unknown type %d\n", 1555 msg->hdr.msg_type); 1556 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1557 break; 1558 } 1559 if (msg != &msgbuf) 1560 free(msg, M_CTL); 1561 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1562 printf("CTL: HA link status changed from %d to %d\n", 1563 softc->ha_link, param); 1564 if (param == softc->ha_link) 1565 return; 1566 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1567 softc->ha_link = param; 1568 ctl_isc_ha_link_down(softc); 1569 } else { 1570 softc->ha_link = param; 1571 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1572 ctl_isc_ha_link_up(softc); 1573 } 1574 return; 1575 } else { 1576 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1577 return; 1578 } 1579} 1580 1581static void 1582ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1583{ 1584 1585 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1586 src->scsi.sense_len); 1587 dest->scsiio.scsi_status = src->scsi.scsi_status; 1588 dest->scsiio.sense_len = src->scsi.sense_len; 1589 dest->io_hdr.status = src->hdr.status; 1590} 1591 1592static void 1593ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1594{ 1595 1596 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1597 src->scsiio.sense_len); 1598 dest->scsi.scsi_status = src->scsiio.scsi_status; 1599 dest->scsi.sense_len = src->scsiio.sense_len; 1600 dest->hdr.status = src->io_hdr.status; 1601} 1602 1603void 1604ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1605{ 1606 struct ctl_softc *softc = lun->ctl_softc; 1607 ctl_ua_type *pu; 1608 1609 if (initidx < softc->init_min || initidx >= softc->init_max) 1610 return; 1611 mtx_assert(&lun->lun_lock, MA_OWNED); 1612 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1613 if (pu == NULL) 1614 return; 1615 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1616} 1617 1618void 1619ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1620{ 1621 int i; 1622 1623 mtx_assert(&lun->lun_lock, MA_OWNED); 1624 if (lun->pending_ua[port] == NULL) 1625 return; 1626 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1627 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1628 continue; 1629 lun->pending_ua[port][i] |= ua; 1630 } 1631} 1632 1633void 1634ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1635{ 1636 struct ctl_softc *softc = lun->ctl_softc; 1637 int i; 1638 1639 mtx_assert(&lun->lun_lock, MA_OWNED); 1640 for (i = softc->port_min; i < softc->port_max; i++) 1641 ctl_est_ua_port(lun, i, except, ua); 1642} 1643 1644void 1645ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1646{ 1647 struct ctl_softc *softc = lun->ctl_softc; 1648 ctl_ua_type *pu; 1649 1650 if (initidx < softc->init_min || initidx >= softc->init_max) 1651 return; 1652 mtx_assert(&lun->lun_lock, MA_OWNED); 1653 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1654 if (pu == NULL) 1655 return; 1656 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1657} 1658 1659void 1660ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1661{ 1662 struct ctl_softc *softc = lun->ctl_softc; 1663 int i, j; 1664 1665 mtx_assert(&lun->lun_lock, MA_OWNED); 1666 for (i = softc->port_min; i < softc->port_max; i++) { 1667 if (lun->pending_ua[i] == NULL) 1668 continue; 1669 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1670 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1671 continue; 1672 lun->pending_ua[i][j] &= ~ua; 1673 } 1674 } 1675} 1676 1677void 1678ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1679 ctl_ua_type ua_type) 1680{ 1681 struct ctl_lun *lun; 1682 1683 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1684 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1685 mtx_lock(&lun->lun_lock); 1686 ctl_clr_ua(lun, initidx, ua_type); 1687 mtx_unlock(&lun->lun_lock); 1688 } 1689} 1690 1691static int 1692ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1693{ 1694 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1695 struct ctl_lun *lun; 1696 struct ctl_lun_req ireq; 1697 int error, value; 1698 1699 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1700 error = sysctl_handle_int(oidp, &value, 0, req); 1701 if ((error != 0) || (req->newptr == NULL)) 1702 return (error); 1703 1704 mtx_lock(&softc->ctl_lock); 1705 if (value == 0) 1706 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1707 else 1708 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1709 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1710 mtx_unlock(&softc->ctl_lock); 1711 bzero(&ireq, sizeof(ireq)); 1712 ireq.reqtype = CTL_LUNREQ_MODIFY; 1713 ireq.reqdata.modify.lun_id = lun->lun; 1714 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1715 curthread); 1716 if (ireq.status != CTL_LUN_OK) { 1717 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1718 __func__, ireq.status, ireq.error_str); 1719 } 1720 mtx_lock(&softc->ctl_lock); 1721 } 1722 mtx_unlock(&softc->ctl_lock); 1723 return (0); 1724} 1725 1726static int 1727ctl_init(void) 1728{ 1729 struct ctl_softc *softc; 1730 void *other_pool; 1731 int i, error, retval; 1732 1733 retval = 0; 1734 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1735 M_WAITOK | M_ZERO); 1736 1737 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1738 "cam/ctl"); 1739 softc->dev->si_drv1 = softc; 1740 1741 sysctl_ctx_init(&softc->sysctl_ctx); 1742 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1743 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1744 CTLFLAG_RD, 0, "CAM Target Layer"); 1745 1746 if (softc->sysctl_tree == NULL) { 1747 printf("%s: unable to allocate sysctl tree\n", __func__); 1748 destroy_dev(softc->dev); 1749 free(control_softc, M_DEVBUF); 1750 control_softc = NULL; 1751 return (ENOMEM); 1752 } 1753 1754 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1755 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1756 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1757 softc->open_count = 0; 1758 1759 /* 1760 * Default to actually sending a SYNCHRONIZE CACHE command down to 1761 * the drive. 1762 */ 1763 softc->flags = CTL_FLAG_REAL_SYNC; 1764 1765 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1766 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1767 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1768 1769 /* 1770 * In Copan's HA scheme, the "master" and "slave" roles are 1771 * figured out through the slot the controller is in. Although it 1772 * is an active/active system, someone has to be in charge. 1773 */ 1774 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1775 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1776 "HA head ID (0 - no HA)"); 1777 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) { 1778 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1779 softc->is_single = 1; 1780 softc->port_cnt = CTL_MAX_PORTS; 1781 softc->port_min = 0; 1782 } else { 1783 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS; 1784 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1785 } 1786 softc->port_max = softc->port_min + softc->port_cnt; 1787 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1788 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1789 1790 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1791 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1792 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1793 1794 STAILQ_INIT(&softc->lun_list); 1795 STAILQ_INIT(&softc->pending_lun_queue); 1796 STAILQ_INIT(&softc->fe_list); 1797 STAILQ_INIT(&softc->port_list); 1798 STAILQ_INIT(&softc->be_list); 1799 ctl_tpc_init(softc); 1800 1801 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1802 &other_pool) != 0) 1803 { 1804 printf("ctl: can't allocate %d entry other SC pool, " 1805 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1806 return (ENOMEM); 1807 } 1808 softc->othersc_pool = other_pool; 1809 1810 if (worker_threads <= 0) 1811 worker_threads = max(1, mp_ncpus / 4); 1812 if (worker_threads > CTL_MAX_THREADS) 1813 worker_threads = CTL_MAX_THREADS; 1814 1815 for (i = 0; i < worker_threads; i++) { 1816 struct ctl_thread *thr = &softc->threads[i]; 1817 1818 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1819 thr->ctl_softc = softc; 1820 STAILQ_INIT(&thr->incoming_queue); 1821 STAILQ_INIT(&thr->rtr_queue); 1822 STAILQ_INIT(&thr->done_queue); 1823 STAILQ_INIT(&thr->isc_queue); 1824 1825 error = kproc_kthread_add(ctl_work_thread, thr, 1826 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1827 if (error != 0) { 1828 printf("error creating CTL work thread!\n"); 1829 ctl_pool_free(other_pool); 1830 return (error); 1831 } 1832 } 1833 error = kproc_kthread_add(ctl_lun_thread, softc, 1834 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1835 if (error != 0) { 1836 printf("error creating CTL lun thread!\n"); 1837 ctl_pool_free(other_pool); 1838 return (error); 1839 } 1840 error = kproc_kthread_add(ctl_thresh_thread, softc, 1841 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1842 if (error != 0) { 1843 printf("error creating CTL threshold thread!\n"); 1844 ctl_pool_free(other_pool); 1845 return (error); 1846 } 1847 1848 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1849 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1850 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1851 1852 if (softc->is_single == 0) { 1853 ctl_frontend_register(&ha_frontend); 1854 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1855 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1856 softc->is_single = 1; 1857 } else 1858 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1859 != CTL_HA_STATUS_SUCCESS) { 1860 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1861 softc->is_single = 1; 1862 } 1863 } 1864 return (0); 1865} 1866 1867void 1868ctl_shutdown(void) 1869{ 1870 struct ctl_softc *softc = control_softc; 1871 struct ctl_lun *lun, *next_lun; 1872 1873 if (softc->is_single == 0) { 1874 ctl_ha_msg_shutdown(softc); 1875 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1876 != CTL_HA_STATUS_SUCCESS) 1877 printf("%s: ctl_ha_msg_deregister failed.\n", __func__); 1878 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 1879 printf("%s: ctl_ha_msg_destroy failed.\n", __func__); 1880 ctl_frontend_deregister(&ha_frontend); 1881 } 1882 1883 mtx_lock(&softc->ctl_lock); 1884 1885 /* 1886 * Free up each LUN. 1887 */ 1888 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1889 next_lun = STAILQ_NEXT(lun, links); 1890 ctl_free_lun(lun); 1891 } 1892 1893 mtx_unlock(&softc->ctl_lock); 1894 1895#if 0 1896 ctl_shutdown_thread(softc->work_thread); 1897 mtx_destroy(&softc->queue_lock); 1898#endif 1899 1900 ctl_tpc_shutdown(softc); 1901 uma_zdestroy(softc->io_zone); 1902 mtx_destroy(&softc->ctl_lock); 1903 1904 destroy_dev(softc->dev); 1905 1906 sysctl_ctx_free(&softc->sysctl_ctx); 1907 1908 free(control_softc, M_DEVBUF); 1909 control_softc = NULL; 1910} 1911 1912static int 1913ctl_module_event_handler(module_t mod, int what, void *arg) 1914{ 1915 1916 switch (what) { 1917 case MOD_LOAD: 1918 return (ctl_init()); 1919 case MOD_UNLOAD: 1920 return (EBUSY); 1921 default: 1922 return (EOPNOTSUPP); 1923 } 1924} 1925 1926/* 1927 * XXX KDM should we do some access checks here? Bump a reference count to 1928 * prevent a CTL module from being unloaded while someone has it open? 1929 */ 1930static int 1931ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1932{ 1933 return (0); 1934} 1935 1936static int 1937ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1938{ 1939 return (0); 1940} 1941 1942/* 1943 * Remove an initiator by port number and initiator ID. 1944 * Returns 0 for success, -1 for failure. 1945 */ 1946int 1947ctl_remove_initiator(struct ctl_port *port, int iid) 1948{ 1949 struct ctl_softc *softc = port->ctl_softc; 1950 1951 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1952 1953 if (iid > CTL_MAX_INIT_PER_PORT) { 1954 printf("%s: initiator ID %u > maximun %u!\n", 1955 __func__, iid, CTL_MAX_INIT_PER_PORT); 1956 return (-1); 1957 } 1958 1959 mtx_lock(&softc->ctl_lock); 1960 port->wwpn_iid[iid].in_use--; 1961 port->wwpn_iid[iid].last_use = time_uptime; 1962 mtx_unlock(&softc->ctl_lock); 1963 ctl_isc_announce_iid(port, iid); 1964 1965 return (0); 1966} 1967 1968/* 1969 * Add an initiator to the initiator map. 1970 * Returns iid for success, < 0 for failure. 1971 */ 1972int 1973ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1974{ 1975 struct ctl_softc *softc = port->ctl_softc; 1976 time_t best_time; 1977 int i, best; 1978 1979 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1980 1981 if (iid >= CTL_MAX_INIT_PER_PORT) { 1982 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1983 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1984 free(name, M_CTL); 1985 return (-1); 1986 } 1987 1988 mtx_lock(&softc->ctl_lock); 1989 1990 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1991 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1992 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1993 iid = i; 1994 break; 1995 } 1996 if (name != NULL && port->wwpn_iid[i].name != NULL && 1997 strcmp(name, port->wwpn_iid[i].name) == 0) { 1998 iid = i; 1999 break; 2000 } 2001 } 2002 } 2003 2004 if (iid < 0) { 2005 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2006 if (port->wwpn_iid[i].in_use == 0 && 2007 port->wwpn_iid[i].wwpn == 0 && 2008 port->wwpn_iid[i].name == NULL) { 2009 iid = i; 2010 break; 2011 } 2012 } 2013 } 2014 2015 if (iid < 0) { 2016 best = -1; 2017 best_time = INT32_MAX; 2018 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2019 if (port->wwpn_iid[i].in_use == 0) { 2020 if (port->wwpn_iid[i].last_use < best_time) { 2021 best = i; 2022 best_time = port->wwpn_iid[i].last_use; 2023 } 2024 } 2025 } 2026 iid = best; 2027 } 2028 2029 if (iid < 0) { 2030 mtx_unlock(&softc->ctl_lock); 2031 free(name, M_CTL); 2032 return (-2); 2033 } 2034 2035 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2036 /* 2037 * This is not an error yet. 2038 */ 2039 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2040#if 0 2041 printf("%s: port %d iid %u WWPN %#jx arrived" 2042 " again\n", __func__, port->targ_port, 2043 iid, (uintmax_t)wwpn); 2044#endif 2045 goto take; 2046 } 2047 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2048 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2049#if 0 2050 printf("%s: port %d iid %u name '%s' arrived" 2051 " again\n", __func__, port->targ_port, 2052 iid, name); 2053#endif 2054 goto take; 2055 } 2056 2057 /* 2058 * This is an error, but what do we do about it? The 2059 * driver is telling us we have a new WWPN for this 2060 * initiator ID, so we pretty much need to use it. 2061 */ 2062 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2063 " but WWPN %#jx '%s' is still at that address\n", 2064 __func__, port->targ_port, iid, wwpn, name, 2065 (uintmax_t)port->wwpn_iid[iid].wwpn, 2066 port->wwpn_iid[iid].name); 2067 2068 /* 2069 * XXX KDM clear have_ca and ua_pending on each LUN for 2070 * this initiator. 2071 */ 2072 } 2073take: 2074 free(port->wwpn_iid[iid].name, M_CTL); 2075 port->wwpn_iid[iid].name = name; 2076 port->wwpn_iid[iid].wwpn = wwpn; 2077 port->wwpn_iid[iid].in_use++; 2078 mtx_unlock(&softc->ctl_lock); 2079 ctl_isc_announce_iid(port, iid); 2080 2081 return (iid); 2082} 2083 2084static int 2085ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2086{ 2087 int len; 2088 2089 switch (port->port_type) { 2090 case CTL_PORT_FC: 2091 { 2092 struct scsi_transportid_fcp *id = 2093 (struct scsi_transportid_fcp *)buf; 2094 if (port->wwpn_iid[iid].wwpn == 0) 2095 return (0); 2096 memset(id, 0, sizeof(*id)); 2097 id->format_protocol = SCSI_PROTO_FC; 2098 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2099 return (sizeof(*id)); 2100 } 2101 case CTL_PORT_ISCSI: 2102 { 2103 struct scsi_transportid_iscsi_port *id = 2104 (struct scsi_transportid_iscsi_port *)buf; 2105 if (port->wwpn_iid[iid].name == NULL) 2106 return (0); 2107 memset(id, 0, 256); 2108 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2109 SCSI_PROTO_ISCSI; 2110 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2111 len = roundup2(min(len, 252), 4); 2112 scsi_ulto2b(len, id->additional_length); 2113 return (sizeof(*id) + len); 2114 } 2115 case CTL_PORT_SAS: 2116 { 2117 struct scsi_transportid_sas *id = 2118 (struct scsi_transportid_sas *)buf; 2119 if (port->wwpn_iid[iid].wwpn == 0) 2120 return (0); 2121 memset(id, 0, sizeof(*id)); 2122 id->format_protocol = SCSI_PROTO_SAS; 2123 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2124 return (sizeof(*id)); 2125 } 2126 default: 2127 { 2128 struct scsi_transportid_spi *id = 2129 (struct scsi_transportid_spi *)buf; 2130 memset(id, 0, sizeof(*id)); 2131 id->format_protocol = SCSI_PROTO_SPI; 2132 scsi_ulto2b(iid, id->scsi_addr); 2133 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2134 return (sizeof(*id)); 2135 } 2136 } 2137} 2138 2139/* 2140 * Serialize a command that went down the "wrong" side, and so was sent to 2141 * this controller for execution. The logic is a little different than the 2142 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2143 * sent back to the other side, but in the success case, we execute the 2144 * command on this side (XFER mode) or tell the other side to execute it 2145 * (SER_ONLY mode). 2146 */ 2147static int 2148ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2149{ 2150 struct ctl_softc *softc = control_softc; 2151 union ctl_ha_msg msg_info; 2152 struct ctl_port *port; 2153 struct ctl_lun *lun; 2154 const struct ctl_cmd_entry *entry; 2155 int retval = 0; 2156 uint32_t targ_lun; 2157 2158 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2159 mtx_lock(&softc->ctl_lock); 2160 2161 /* Make sure that we know about this port. */ 2162 port = ctl_io_port(&ctsio->io_hdr); 2163 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2164 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2165 /*retry_count*/ 1); 2166 goto badjuju; 2167 } 2168 2169 /* Make sure that we know about this LUN. */ 2170 if ((targ_lun < CTL_MAX_LUNS) && 2171 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 2172 mtx_lock(&lun->lun_lock); 2173 mtx_unlock(&softc->ctl_lock); 2174 /* 2175 * If the LUN is invalid, pretend that it doesn't exist. 2176 * It will go away as soon as all pending I/O has been 2177 * completed. 2178 */ 2179 if (lun->flags & CTL_LUN_DISABLED) { 2180 mtx_unlock(&lun->lun_lock); 2181 lun = NULL; 2182 } 2183 } else { 2184 mtx_unlock(&softc->ctl_lock); 2185 lun = NULL; 2186 } 2187 if (lun == NULL) { 2188 /* 2189 * The other node would not send this request to us unless 2190 * received announce that we are primary node for this LUN. 2191 * If this LUN does not exist now, it is probably result of 2192 * a race, so respond to initiator in the most opaque way. 2193 */ 2194 ctl_set_busy(ctsio); 2195 goto badjuju; 2196 } 2197 2198 entry = ctl_get_cmd_entry(ctsio, NULL); 2199 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2200 mtx_unlock(&lun->lun_lock); 2201 goto badjuju; 2202 } 2203 2204 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 2205 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun; 2206 2207 /* 2208 * Every I/O goes into the OOA queue for a 2209 * particular LUN, and stays there until completion. 2210 */ 2211#ifdef CTL_TIME_IO 2212 if (TAILQ_EMPTY(&lun->ooa_queue)) 2213 lun->idle_time += getsbinuptime() - lun->last_busy; 2214#endif 2215 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2216 2217 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2218 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2219 ooa_links))) { 2220 case CTL_ACTION_BLOCK: 2221 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2222 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2223 blocked_links); 2224 mtx_unlock(&lun->lun_lock); 2225 break; 2226 case CTL_ACTION_PASS: 2227 case CTL_ACTION_SKIP: 2228 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2229 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2230 ctl_enqueue_rtr((union ctl_io *)ctsio); 2231 mtx_unlock(&lun->lun_lock); 2232 } else { 2233 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2234 mtx_unlock(&lun->lun_lock); 2235 2236 /* send msg back to other side */ 2237 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2238 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2239 msg_info.hdr.msg_type = CTL_MSG_R2R; 2240 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2241 sizeof(msg_info.hdr), M_WAITOK); 2242 } 2243 break; 2244 case CTL_ACTION_OVERLAP: 2245 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2246 mtx_unlock(&lun->lun_lock); 2247 ctl_set_overlapped_cmd(ctsio); 2248 goto badjuju; 2249 case CTL_ACTION_OVERLAP_TAG: 2250 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2251 mtx_unlock(&lun->lun_lock); 2252 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2253 goto badjuju; 2254 case CTL_ACTION_ERROR: 2255 default: 2256 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2257 mtx_unlock(&lun->lun_lock); 2258 2259 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2260 /*retry_count*/ 0); 2261badjuju: 2262 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2263 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2264 msg_info.hdr.serializing_sc = NULL; 2265 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2266 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2267 sizeof(msg_info.scsi), M_WAITOK); 2268 retval = 1; 2269 break; 2270 } 2271 return (retval); 2272} 2273 2274/* 2275 * Returns 0 for success, errno for failure. 2276 */ 2277static int 2278ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2279 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2280{ 2281 union ctl_io *io; 2282 int retval; 2283 2284 retval = 0; 2285 2286 mtx_lock(&lun->lun_lock); 2287 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2288 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2289 ooa_links)) { 2290 struct ctl_ooa_entry *entry; 2291 2292 /* 2293 * If we've got more than we can fit, just count the 2294 * remaining entries. 2295 */ 2296 if (*cur_fill_num >= ooa_hdr->alloc_num) 2297 continue; 2298 2299 entry = &kern_entries[*cur_fill_num]; 2300 2301 entry->tag_num = io->scsiio.tag_num; 2302 entry->lun_num = lun->lun; 2303#ifdef CTL_TIME_IO 2304 entry->start_bt = io->io_hdr.start_bt; 2305#endif 2306 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2307 entry->cdb_len = io->scsiio.cdb_len; 2308 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2309 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2310 2311 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2312 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2313 2314 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2315 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2316 2317 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2318 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2319 2320 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2321 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2322 } 2323 mtx_unlock(&lun->lun_lock); 2324 2325 return (retval); 2326} 2327 2328static void * 2329ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2330 size_t error_str_len) 2331{ 2332 void *kptr; 2333 2334 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2335 2336 if (copyin(user_addr, kptr, len) != 0) { 2337 snprintf(error_str, error_str_len, "Error copying %d bytes " 2338 "from user address %p to kernel address %p", len, 2339 user_addr, kptr); 2340 free(kptr, M_CTL); 2341 return (NULL); 2342 } 2343 2344 return (kptr); 2345} 2346 2347static void 2348ctl_free_args(int num_args, struct ctl_be_arg *args) 2349{ 2350 int i; 2351 2352 if (args == NULL) 2353 return; 2354 2355 for (i = 0; i < num_args; i++) { 2356 free(args[i].kname, M_CTL); 2357 free(args[i].kvalue, M_CTL); 2358 } 2359 2360 free(args, M_CTL); 2361} 2362 2363static struct ctl_be_arg * 2364ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2365 char *error_str, size_t error_str_len) 2366{ 2367 struct ctl_be_arg *args; 2368 int i; 2369 2370 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2371 error_str, error_str_len); 2372 2373 if (args == NULL) 2374 goto bailout; 2375 2376 for (i = 0; i < num_args; i++) { 2377 args[i].kname = NULL; 2378 args[i].kvalue = NULL; 2379 } 2380 2381 for (i = 0; i < num_args; i++) { 2382 uint8_t *tmpptr; 2383 2384 args[i].kname = ctl_copyin_alloc(args[i].name, 2385 args[i].namelen, error_str, error_str_len); 2386 if (args[i].kname == NULL) 2387 goto bailout; 2388 2389 if (args[i].kname[args[i].namelen - 1] != '\0') { 2390 snprintf(error_str, error_str_len, "Argument %d " 2391 "name is not NUL-terminated", i); 2392 goto bailout; 2393 } 2394 2395 if (args[i].flags & CTL_BEARG_RD) { 2396 tmpptr = ctl_copyin_alloc(args[i].value, 2397 args[i].vallen, error_str, error_str_len); 2398 if (tmpptr == NULL) 2399 goto bailout; 2400 if ((args[i].flags & CTL_BEARG_ASCII) 2401 && (tmpptr[args[i].vallen - 1] != '\0')) { 2402 snprintf(error_str, error_str_len, "Argument " 2403 "%d value is not NUL-terminated", i); 2404 goto bailout; 2405 } 2406 args[i].kvalue = tmpptr; 2407 } else { 2408 args[i].kvalue = malloc(args[i].vallen, 2409 M_CTL, M_WAITOK | M_ZERO); 2410 } 2411 } 2412 2413 return (args); 2414bailout: 2415 2416 ctl_free_args(num_args, args); 2417 2418 return (NULL); 2419} 2420 2421static void 2422ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2423{ 2424 int i; 2425 2426 for (i = 0; i < num_args; i++) { 2427 if (args[i].flags & CTL_BEARG_WR) 2428 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2429 } 2430} 2431 2432/* 2433 * Escape characters that are illegal or not recommended in XML. 2434 */ 2435int 2436ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2437{ 2438 char *end = str + size; 2439 int retval; 2440 2441 retval = 0; 2442 2443 for (; *str && str < end; str++) { 2444 switch (*str) { 2445 case '&': 2446 retval = sbuf_printf(sb, "&"); 2447 break; 2448 case '>': 2449 retval = sbuf_printf(sb, ">"); 2450 break; 2451 case '<': 2452 retval = sbuf_printf(sb, "<"); 2453 break; 2454 default: 2455 retval = sbuf_putc(sb, *str); 2456 break; 2457 } 2458 2459 if (retval != 0) 2460 break; 2461 2462 } 2463 2464 return (retval); 2465} 2466 2467static void 2468ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2469{ 2470 struct scsi_vpd_id_descriptor *desc; 2471 int i; 2472 2473 if (id == NULL || id->len < 4) 2474 return; 2475 desc = (struct scsi_vpd_id_descriptor *)id->data; 2476 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2477 case SVPD_ID_TYPE_T10: 2478 sbuf_printf(sb, "t10."); 2479 break; 2480 case SVPD_ID_TYPE_EUI64: 2481 sbuf_printf(sb, "eui."); 2482 break; 2483 case SVPD_ID_TYPE_NAA: 2484 sbuf_printf(sb, "naa."); 2485 break; 2486 case SVPD_ID_TYPE_SCSI_NAME: 2487 break; 2488 } 2489 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2490 case SVPD_ID_CODESET_BINARY: 2491 for (i = 0; i < desc->length; i++) 2492 sbuf_printf(sb, "%02x", desc->identifier[i]); 2493 break; 2494 case SVPD_ID_CODESET_ASCII: 2495 sbuf_printf(sb, "%.*s", (int)desc->length, 2496 (char *)desc->identifier); 2497 break; 2498 case SVPD_ID_CODESET_UTF8: 2499 sbuf_printf(sb, "%s", (char *)desc->identifier); 2500 break; 2501 } 2502} 2503 2504static int 2505ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2506 struct thread *td) 2507{ 2508 struct ctl_softc *softc = dev->si_drv1; 2509 struct ctl_lun *lun; 2510 int retval; 2511 2512 retval = 0; 2513 2514 switch (cmd) { 2515 case CTL_IO: 2516 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2517 break; 2518 case CTL_ENABLE_PORT: 2519 case CTL_DISABLE_PORT: 2520 case CTL_SET_PORT_WWNS: { 2521 struct ctl_port *port; 2522 struct ctl_port_entry *entry; 2523 2524 entry = (struct ctl_port_entry *)addr; 2525 2526 mtx_lock(&softc->ctl_lock); 2527 STAILQ_FOREACH(port, &softc->port_list, links) { 2528 int action, done; 2529 2530 if (port->targ_port < softc->port_min || 2531 port->targ_port >= softc->port_max) 2532 continue; 2533 2534 action = 0; 2535 done = 0; 2536 if ((entry->port_type == CTL_PORT_NONE) 2537 && (entry->targ_port == port->targ_port)) { 2538 /* 2539 * If the user only wants to enable or 2540 * disable or set WWNs on a specific port, 2541 * do the operation and we're done. 2542 */ 2543 action = 1; 2544 done = 1; 2545 } else if (entry->port_type & port->port_type) { 2546 /* 2547 * Compare the user's type mask with the 2548 * particular frontend type to see if we 2549 * have a match. 2550 */ 2551 action = 1; 2552 done = 0; 2553 2554 /* 2555 * Make sure the user isn't trying to set 2556 * WWNs on multiple ports at the same time. 2557 */ 2558 if (cmd == CTL_SET_PORT_WWNS) { 2559 printf("%s: Can't set WWNs on " 2560 "multiple ports\n", __func__); 2561 retval = EINVAL; 2562 break; 2563 } 2564 } 2565 if (action == 0) 2566 continue; 2567 2568 /* 2569 * XXX KDM we have to drop the lock here, because 2570 * the online/offline operations can potentially 2571 * block. We need to reference count the frontends 2572 * so they can't go away, 2573 */ 2574 if (cmd == CTL_ENABLE_PORT) { 2575 mtx_unlock(&softc->ctl_lock); 2576 ctl_port_online(port); 2577 mtx_lock(&softc->ctl_lock); 2578 } else if (cmd == CTL_DISABLE_PORT) { 2579 mtx_unlock(&softc->ctl_lock); 2580 ctl_port_offline(port); 2581 mtx_lock(&softc->ctl_lock); 2582 } else if (cmd == CTL_SET_PORT_WWNS) { 2583 ctl_port_set_wwns(port, 2584 (entry->flags & CTL_PORT_WWNN_VALID) ? 2585 1 : 0, entry->wwnn, 2586 (entry->flags & CTL_PORT_WWPN_VALID) ? 2587 1 : 0, entry->wwpn); 2588 } 2589 if (done != 0) 2590 break; 2591 } 2592 mtx_unlock(&softc->ctl_lock); 2593 break; 2594 } 2595 case CTL_GET_PORT_LIST: { 2596 struct ctl_port *port; 2597 struct ctl_port_list *list; 2598 int i; 2599 2600 list = (struct ctl_port_list *)addr; 2601 2602 if (list->alloc_len != (list->alloc_num * 2603 sizeof(struct ctl_port_entry))) { 2604 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2605 "alloc_num %u * sizeof(struct ctl_port_entry) " 2606 "%zu\n", __func__, list->alloc_len, 2607 list->alloc_num, sizeof(struct ctl_port_entry)); 2608 retval = EINVAL; 2609 break; 2610 } 2611 list->fill_len = 0; 2612 list->fill_num = 0; 2613 list->dropped_num = 0; 2614 i = 0; 2615 mtx_lock(&softc->ctl_lock); 2616 STAILQ_FOREACH(port, &softc->port_list, links) { 2617 struct ctl_port_entry entry, *list_entry; 2618 2619 if (list->fill_num >= list->alloc_num) { 2620 list->dropped_num++; 2621 continue; 2622 } 2623 2624 entry.port_type = port->port_type; 2625 strlcpy(entry.port_name, port->port_name, 2626 sizeof(entry.port_name)); 2627 entry.targ_port = port->targ_port; 2628 entry.physical_port = port->physical_port; 2629 entry.virtual_port = port->virtual_port; 2630 entry.wwnn = port->wwnn; 2631 entry.wwpn = port->wwpn; 2632 if (port->status & CTL_PORT_STATUS_ONLINE) 2633 entry.online = 1; 2634 else 2635 entry.online = 0; 2636 2637 list_entry = &list->entries[i]; 2638 2639 retval = copyout(&entry, list_entry, sizeof(entry)); 2640 if (retval != 0) { 2641 printf("%s: CTL_GET_PORT_LIST: copyout " 2642 "returned %d\n", __func__, retval); 2643 break; 2644 } 2645 i++; 2646 list->fill_num++; 2647 list->fill_len += sizeof(entry); 2648 } 2649 mtx_unlock(&softc->ctl_lock); 2650 2651 /* 2652 * If this is non-zero, we had a copyout fault, so there's 2653 * probably no point in attempting to set the status inside 2654 * the structure. 2655 */ 2656 if (retval != 0) 2657 break; 2658 2659 if (list->dropped_num > 0) 2660 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2661 else 2662 list->status = CTL_PORT_LIST_OK; 2663 break; 2664 } 2665 case CTL_DUMP_OOA: { 2666 union ctl_io *io; 2667 char printbuf[128]; 2668 struct sbuf sb; 2669 2670 mtx_lock(&softc->ctl_lock); 2671 printf("Dumping OOA queues:\n"); 2672 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2673 mtx_lock(&lun->lun_lock); 2674 for (io = (union ctl_io *)TAILQ_FIRST( 2675 &lun->ooa_queue); io != NULL; 2676 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2677 ooa_links)) { 2678 sbuf_new(&sb, printbuf, sizeof(printbuf), 2679 SBUF_FIXEDLEN); 2680 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2681 (intmax_t)lun->lun, 2682 io->scsiio.tag_num, 2683 (io->io_hdr.flags & 2684 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2685 (io->io_hdr.flags & 2686 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2687 (io->io_hdr.flags & 2688 CTL_FLAG_ABORT) ? " ABORT" : "", 2689 (io->io_hdr.flags & 2690 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2691 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2692 sbuf_finish(&sb); 2693 printf("%s\n", sbuf_data(&sb)); 2694 } 2695 mtx_unlock(&lun->lun_lock); 2696 } 2697 printf("OOA queues dump done\n"); 2698 mtx_unlock(&softc->ctl_lock); 2699 break; 2700 } 2701 case CTL_GET_OOA: { 2702 struct ctl_ooa *ooa_hdr; 2703 struct ctl_ooa_entry *entries; 2704 uint32_t cur_fill_num; 2705 2706 ooa_hdr = (struct ctl_ooa *)addr; 2707 2708 if ((ooa_hdr->alloc_len == 0) 2709 || (ooa_hdr->alloc_num == 0)) { 2710 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2711 "must be non-zero\n", __func__, 2712 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2713 retval = EINVAL; 2714 break; 2715 } 2716 2717 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2718 sizeof(struct ctl_ooa_entry))) { 2719 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2720 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2721 __func__, ooa_hdr->alloc_len, 2722 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2723 retval = EINVAL; 2724 break; 2725 } 2726 2727 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2728 if (entries == NULL) { 2729 printf("%s: could not allocate %d bytes for OOA " 2730 "dump\n", __func__, ooa_hdr->alloc_len); 2731 retval = ENOMEM; 2732 break; 2733 } 2734 2735 mtx_lock(&softc->ctl_lock); 2736 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2737 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2738 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2739 mtx_unlock(&softc->ctl_lock); 2740 free(entries, M_CTL); 2741 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2742 __func__, (uintmax_t)ooa_hdr->lun_num); 2743 retval = EINVAL; 2744 break; 2745 } 2746 2747 cur_fill_num = 0; 2748 2749 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2750 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2751 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2752 ooa_hdr, entries); 2753 if (retval != 0) 2754 break; 2755 } 2756 if (retval != 0) { 2757 mtx_unlock(&softc->ctl_lock); 2758 free(entries, M_CTL); 2759 break; 2760 } 2761 } else { 2762 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2763 2764 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2765 entries); 2766 } 2767 mtx_unlock(&softc->ctl_lock); 2768 2769 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2770 ooa_hdr->fill_len = ooa_hdr->fill_num * 2771 sizeof(struct ctl_ooa_entry); 2772 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2773 if (retval != 0) { 2774 printf("%s: error copying out %d bytes for OOA dump\n", 2775 __func__, ooa_hdr->fill_len); 2776 } 2777 2778 getbintime(&ooa_hdr->cur_bt); 2779 2780 if (cur_fill_num > ooa_hdr->alloc_num) { 2781 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2782 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2783 } else { 2784 ooa_hdr->dropped_num = 0; 2785 ooa_hdr->status = CTL_OOA_OK; 2786 } 2787 2788 free(entries, M_CTL); 2789 break; 2790 } 2791 case CTL_CHECK_OOA: { 2792 union ctl_io *io; 2793 struct ctl_ooa_info *ooa_info; 2794 2795 2796 ooa_info = (struct ctl_ooa_info *)addr; 2797 2798 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2799 ooa_info->status = CTL_OOA_INVALID_LUN; 2800 break; 2801 } 2802 mtx_lock(&softc->ctl_lock); 2803 lun = softc->ctl_luns[ooa_info->lun_id]; 2804 if (lun == NULL) { 2805 mtx_unlock(&softc->ctl_lock); 2806 ooa_info->status = CTL_OOA_INVALID_LUN; 2807 break; 2808 } 2809 mtx_lock(&lun->lun_lock); 2810 mtx_unlock(&softc->ctl_lock); 2811 ooa_info->num_entries = 0; 2812 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2813 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2814 &io->io_hdr, ooa_links)) { 2815 ooa_info->num_entries++; 2816 } 2817 mtx_unlock(&lun->lun_lock); 2818 2819 ooa_info->status = CTL_OOA_SUCCESS; 2820 2821 break; 2822 } 2823 case CTL_DELAY_IO: { 2824 struct ctl_io_delay_info *delay_info; 2825 2826 delay_info = (struct ctl_io_delay_info *)addr; 2827 2828#ifdef CTL_IO_DELAY 2829 mtx_lock(&softc->ctl_lock); 2830 2831 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2832 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2833 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2834 } else { 2835 lun = softc->ctl_luns[delay_info->lun_id]; 2836 mtx_lock(&lun->lun_lock); 2837 2838 delay_info->status = CTL_DELAY_STATUS_OK; 2839 2840 switch (delay_info->delay_type) { 2841 case CTL_DELAY_TYPE_CONT: 2842 break; 2843 case CTL_DELAY_TYPE_ONESHOT: 2844 break; 2845 default: 2846 delay_info->status = 2847 CTL_DELAY_STATUS_INVALID_TYPE; 2848 break; 2849 } 2850 2851 switch (delay_info->delay_loc) { 2852 case CTL_DELAY_LOC_DATAMOVE: 2853 lun->delay_info.datamove_type = 2854 delay_info->delay_type; 2855 lun->delay_info.datamove_delay = 2856 delay_info->delay_secs; 2857 break; 2858 case CTL_DELAY_LOC_DONE: 2859 lun->delay_info.done_type = 2860 delay_info->delay_type; 2861 lun->delay_info.done_delay = 2862 delay_info->delay_secs; 2863 break; 2864 default: 2865 delay_info->status = 2866 CTL_DELAY_STATUS_INVALID_LOC; 2867 break; 2868 } 2869 mtx_unlock(&lun->lun_lock); 2870 } 2871 2872 mtx_unlock(&softc->ctl_lock); 2873#else 2874 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2875#endif /* CTL_IO_DELAY */ 2876 break; 2877 } 2878 case CTL_REALSYNC_SET: { 2879 int *syncstate; 2880 2881 syncstate = (int *)addr; 2882 2883 mtx_lock(&softc->ctl_lock); 2884 switch (*syncstate) { 2885 case 0: 2886 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2887 break; 2888 case 1: 2889 softc->flags |= CTL_FLAG_REAL_SYNC; 2890 break; 2891 default: 2892 retval = EINVAL; 2893 break; 2894 } 2895 mtx_unlock(&softc->ctl_lock); 2896 break; 2897 } 2898 case CTL_REALSYNC_GET: { 2899 int *syncstate; 2900 2901 syncstate = (int*)addr; 2902 2903 mtx_lock(&softc->ctl_lock); 2904 if (softc->flags & CTL_FLAG_REAL_SYNC) 2905 *syncstate = 1; 2906 else 2907 *syncstate = 0; 2908 mtx_unlock(&softc->ctl_lock); 2909 2910 break; 2911 } 2912 case CTL_SETSYNC: 2913 case CTL_GETSYNC: { 2914 struct ctl_sync_info *sync_info; 2915 2916 sync_info = (struct ctl_sync_info *)addr; 2917 2918 mtx_lock(&softc->ctl_lock); 2919 lun = softc->ctl_luns[sync_info->lun_id]; 2920 if (lun == NULL) { 2921 mtx_unlock(&softc->ctl_lock); 2922 sync_info->status = CTL_GS_SYNC_NO_LUN; 2923 break; 2924 } 2925 /* 2926 * Get or set the sync interval. We're not bounds checking 2927 * in the set case, hopefully the user won't do something 2928 * silly. 2929 */ 2930 mtx_lock(&lun->lun_lock); 2931 mtx_unlock(&softc->ctl_lock); 2932 if (cmd == CTL_GETSYNC) 2933 sync_info->sync_interval = lun->sync_interval; 2934 else 2935 lun->sync_interval = sync_info->sync_interval; 2936 mtx_unlock(&lun->lun_lock); 2937 2938 sync_info->status = CTL_GS_SYNC_OK; 2939 2940 break; 2941 } 2942 case CTL_GETSTATS: { 2943 struct ctl_stats *stats; 2944 int i; 2945 2946 stats = (struct ctl_stats *)addr; 2947 2948 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2949 stats->alloc_len) { 2950 stats->status = CTL_SS_NEED_MORE_SPACE; 2951 stats->num_luns = softc->num_luns; 2952 break; 2953 } 2954 /* 2955 * XXX KDM no locking here. If the LUN list changes, 2956 * things can blow up. 2957 */ 2958 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2959 i++, lun = STAILQ_NEXT(lun, links)) { 2960 retval = copyout(&lun->stats, &stats->lun_stats[i], 2961 sizeof(lun->stats)); 2962 if (retval != 0) 2963 break; 2964 } 2965 stats->num_luns = softc->num_luns; 2966 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2967 softc->num_luns; 2968 stats->status = CTL_SS_OK; 2969#ifdef CTL_TIME_IO 2970 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2971#else 2972 stats->flags = CTL_STATS_FLAG_NONE; 2973#endif 2974 getnanouptime(&stats->timestamp); 2975 break; 2976 } 2977 case CTL_ERROR_INJECT: { 2978 struct ctl_error_desc *err_desc, *new_err_desc; 2979 2980 err_desc = (struct ctl_error_desc *)addr; 2981 2982 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2983 M_WAITOK | M_ZERO); 2984 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2985 2986 mtx_lock(&softc->ctl_lock); 2987 lun = softc->ctl_luns[err_desc->lun_id]; 2988 if (lun == NULL) { 2989 mtx_unlock(&softc->ctl_lock); 2990 free(new_err_desc, M_CTL); 2991 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2992 __func__, (uintmax_t)err_desc->lun_id); 2993 retval = EINVAL; 2994 break; 2995 } 2996 mtx_lock(&lun->lun_lock); 2997 mtx_unlock(&softc->ctl_lock); 2998 2999 /* 3000 * We could do some checking here to verify the validity 3001 * of the request, but given the complexity of error 3002 * injection requests, the checking logic would be fairly 3003 * complex. 3004 * 3005 * For now, if the request is invalid, it just won't get 3006 * executed and might get deleted. 3007 */ 3008 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 3009 3010 /* 3011 * XXX KDM check to make sure the serial number is unique, 3012 * in case we somehow manage to wrap. That shouldn't 3013 * happen for a very long time, but it's the right thing to 3014 * do. 3015 */ 3016 new_err_desc->serial = lun->error_serial; 3017 err_desc->serial = lun->error_serial; 3018 lun->error_serial++; 3019 3020 mtx_unlock(&lun->lun_lock); 3021 break; 3022 } 3023 case CTL_ERROR_INJECT_DELETE: { 3024 struct ctl_error_desc *delete_desc, *desc, *desc2; 3025 int delete_done; 3026 3027 delete_desc = (struct ctl_error_desc *)addr; 3028 delete_done = 0; 3029 3030 mtx_lock(&softc->ctl_lock); 3031 lun = softc->ctl_luns[delete_desc->lun_id]; 3032 if (lun == NULL) { 3033 mtx_unlock(&softc->ctl_lock); 3034 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 3035 __func__, (uintmax_t)delete_desc->lun_id); 3036 retval = EINVAL; 3037 break; 3038 } 3039 mtx_lock(&lun->lun_lock); 3040 mtx_unlock(&softc->ctl_lock); 3041 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 3042 if (desc->serial != delete_desc->serial) 3043 continue; 3044 3045 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 3046 links); 3047 free(desc, M_CTL); 3048 delete_done = 1; 3049 } 3050 mtx_unlock(&lun->lun_lock); 3051 if (delete_done == 0) { 3052 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 3053 "error serial %ju on LUN %u\n", __func__, 3054 delete_desc->serial, delete_desc->lun_id); 3055 retval = EINVAL; 3056 break; 3057 } 3058 break; 3059 } 3060 case CTL_DUMP_STRUCTS: { 3061 int i, j, k; 3062 struct ctl_port *port; 3063 struct ctl_frontend *fe; 3064 3065 mtx_lock(&softc->ctl_lock); 3066 printf("CTL Persistent Reservation information start:\n"); 3067 for (i = 0; i < CTL_MAX_LUNS; i++) { 3068 lun = softc->ctl_luns[i]; 3069 3070 if ((lun == NULL) 3071 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3072 continue; 3073 3074 for (j = 0; j < CTL_MAX_PORTS; j++) { 3075 if (lun->pr_keys[j] == NULL) 3076 continue; 3077 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3078 if (lun->pr_keys[j][k] == 0) 3079 continue; 3080 printf(" LUN %d port %d iid %d key " 3081 "%#jx\n", i, j, k, 3082 (uintmax_t)lun->pr_keys[j][k]); 3083 } 3084 } 3085 } 3086 printf("CTL Persistent Reservation information end\n"); 3087 printf("CTL Ports:\n"); 3088 STAILQ_FOREACH(port, &softc->port_list, links) { 3089 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3090 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3091 port->frontend->name, port->port_type, 3092 port->physical_port, port->virtual_port, 3093 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3094 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3095 if (port->wwpn_iid[j].in_use == 0 && 3096 port->wwpn_iid[j].wwpn == 0 && 3097 port->wwpn_iid[j].name == NULL) 3098 continue; 3099 3100 printf(" iid %u use %d WWPN %#jx '%s'\n", 3101 j, port->wwpn_iid[j].in_use, 3102 (uintmax_t)port->wwpn_iid[j].wwpn, 3103 port->wwpn_iid[j].name); 3104 } 3105 } 3106 printf("CTL Port information end\n"); 3107 mtx_unlock(&softc->ctl_lock); 3108 /* 3109 * XXX KDM calling this without a lock. We'd likely want 3110 * to drop the lock before calling the frontend's dump 3111 * routine anyway. 3112 */ 3113 printf("CTL Frontends:\n"); 3114 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3115 printf(" Frontend '%s'\n", fe->name); 3116 if (fe->fe_dump != NULL) 3117 fe->fe_dump(); 3118 } 3119 printf("CTL Frontend information end\n"); 3120 break; 3121 } 3122 case CTL_LUN_REQ: { 3123 struct ctl_lun_req *lun_req; 3124 struct ctl_backend_driver *backend; 3125 3126 lun_req = (struct ctl_lun_req *)addr; 3127 3128 backend = ctl_backend_find(lun_req->backend); 3129 if (backend == NULL) { 3130 lun_req->status = CTL_LUN_ERROR; 3131 snprintf(lun_req->error_str, 3132 sizeof(lun_req->error_str), 3133 "Backend \"%s\" not found.", 3134 lun_req->backend); 3135 break; 3136 } 3137 if (lun_req->num_be_args > 0) { 3138 lun_req->kern_be_args = ctl_copyin_args( 3139 lun_req->num_be_args, 3140 lun_req->be_args, 3141 lun_req->error_str, 3142 sizeof(lun_req->error_str)); 3143 if (lun_req->kern_be_args == NULL) { 3144 lun_req->status = CTL_LUN_ERROR; 3145 break; 3146 } 3147 } 3148 3149 retval = backend->ioctl(dev, cmd, addr, flag, td); 3150 3151 if (lun_req->num_be_args > 0) { 3152 ctl_copyout_args(lun_req->num_be_args, 3153 lun_req->kern_be_args); 3154 ctl_free_args(lun_req->num_be_args, 3155 lun_req->kern_be_args); 3156 } 3157 break; 3158 } 3159 case CTL_LUN_LIST: { 3160 struct sbuf *sb; 3161 struct ctl_lun_list *list; 3162 struct ctl_option *opt; 3163 3164 list = (struct ctl_lun_list *)addr; 3165 3166 /* 3167 * Allocate a fixed length sbuf here, based on the length 3168 * of the user's buffer. We could allocate an auto-extending 3169 * buffer, and then tell the user how much larger our 3170 * amount of data is than his buffer, but that presents 3171 * some problems: 3172 * 3173 * 1. The sbuf(9) routines use a blocking malloc, and so 3174 * we can't hold a lock while calling them with an 3175 * auto-extending buffer. 3176 * 3177 * 2. There is not currently a LUN reference counting 3178 * mechanism, outside of outstanding transactions on 3179 * the LUN's OOA queue. So a LUN could go away on us 3180 * while we're getting the LUN number, backend-specific 3181 * information, etc. Thus, given the way things 3182 * currently work, we need to hold the CTL lock while 3183 * grabbing LUN information. 3184 * 3185 * So, from the user's standpoint, the best thing to do is 3186 * allocate what he thinks is a reasonable buffer length, 3187 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3188 * double the buffer length and try again. (And repeat 3189 * that until he succeeds.) 3190 */ 3191 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3192 if (sb == NULL) { 3193 list->status = CTL_LUN_LIST_ERROR; 3194 snprintf(list->error_str, sizeof(list->error_str), 3195 "Unable to allocate %d bytes for LUN list", 3196 list->alloc_len); 3197 break; 3198 } 3199 3200 sbuf_printf(sb, "<ctllunlist>\n"); 3201 3202 mtx_lock(&softc->ctl_lock); 3203 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3204 mtx_lock(&lun->lun_lock); 3205 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3206 (uintmax_t)lun->lun); 3207 3208 /* 3209 * Bail out as soon as we see that we've overfilled 3210 * the buffer. 3211 */ 3212 if (retval != 0) 3213 break; 3214 3215 retval = sbuf_printf(sb, "\t<backend_type>%s" 3216 "</backend_type>\n", 3217 (lun->backend == NULL) ? "none" : 3218 lun->backend->name); 3219 3220 if (retval != 0) 3221 break; 3222 3223 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3224 lun->be_lun->lun_type); 3225 3226 if (retval != 0) 3227 break; 3228 3229 if (lun->backend == NULL) { 3230 retval = sbuf_printf(sb, "</lun>\n"); 3231 if (retval != 0) 3232 break; 3233 continue; 3234 } 3235 3236 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3237 (lun->be_lun->maxlba > 0) ? 3238 lun->be_lun->maxlba + 1 : 0); 3239 3240 if (retval != 0) 3241 break; 3242 3243 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3244 lun->be_lun->blocksize); 3245 3246 if (retval != 0) 3247 break; 3248 3249 retval = sbuf_printf(sb, "\t<serial_number>"); 3250 3251 if (retval != 0) 3252 break; 3253 3254 retval = ctl_sbuf_printf_esc(sb, 3255 lun->be_lun->serial_num, 3256 sizeof(lun->be_lun->serial_num)); 3257 3258 if (retval != 0) 3259 break; 3260 3261 retval = sbuf_printf(sb, "</serial_number>\n"); 3262 3263 if (retval != 0) 3264 break; 3265 3266 retval = sbuf_printf(sb, "\t<device_id>"); 3267 3268 if (retval != 0) 3269 break; 3270 3271 retval = ctl_sbuf_printf_esc(sb, 3272 lun->be_lun->device_id, 3273 sizeof(lun->be_lun->device_id)); 3274 3275 if (retval != 0) 3276 break; 3277 3278 retval = sbuf_printf(sb, "</device_id>\n"); 3279 3280 if (retval != 0) 3281 break; 3282 3283 if (lun->backend->lun_info != NULL) { 3284 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3285 if (retval != 0) 3286 break; 3287 } 3288 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3289 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3290 opt->name, opt->value, opt->name); 3291 if (retval != 0) 3292 break; 3293 } 3294 3295 retval = sbuf_printf(sb, "</lun>\n"); 3296 3297 if (retval != 0) 3298 break; 3299 mtx_unlock(&lun->lun_lock); 3300 } 3301 if (lun != NULL) 3302 mtx_unlock(&lun->lun_lock); 3303 mtx_unlock(&softc->ctl_lock); 3304 3305 if ((retval != 0) 3306 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3307 retval = 0; 3308 sbuf_delete(sb); 3309 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3310 snprintf(list->error_str, sizeof(list->error_str), 3311 "Out of space, %d bytes is too small", 3312 list->alloc_len); 3313 break; 3314 } 3315 3316 sbuf_finish(sb); 3317 3318 retval = copyout(sbuf_data(sb), list->lun_xml, 3319 sbuf_len(sb) + 1); 3320 3321 list->fill_len = sbuf_len(sb) + 1; 3322 list->status = CTL_LUN_LIST_OK; 3323 sbuf_delete(sb); 3324 break; 3325 } 3326 case CTL_ISCSI: { 3327 struct ctl_iscsi *ci; 3328 struct ctl_frontend *fe; 3329 3330 ci = (struct ctl_iscsi *)addr; 3331 3332 fe = ctl_frontend_find("iscsi"); 3333 if (fe == NULL) { 3334 ci->status = CTL_ISCSI_ERROR; 3335 snprintf(ci->error_str, sizeof(ci->error_str), 3336 "Frontend \"iscsi\" not found."); 3337 break; 3338 } 3339 3340 retval = fe->ioctl(dev, cmd, addr, flag, td); 3341 break; 3342 } 3343 case CTL_PORT_REQ: { 3344 struct ctl_req *req; 3345 struct ctl_frontend *fe; 3346 3347 req = (struct ctl_req *)addr; 3348 3349 fe = ctl_frontend_find(req->driver); 3350 if (fe == NULL) { 3351 req->status = CTL_LUN_ERROR; 3352 snprintf(req->error_str, sizeof(req->error_str), 3353 "Frontend \"%s\" not found.", req->driver); 3354 break; 3355 } 3356 if (req->num_args > 0) { 3357 req->kern_args = ctl_copyin_args(req->num_args, 3358 req->args, req->error_str, sizeof(req->error_str)); 3359 if (req->kern_args == NULL) { 3360 req->status = CTL_LUN_ERROR; 3361 break; 3362 } 3363 } 3364 3365 if (fe->ioctl) 3366 retval = fe->ioctl(dev, cmd, addr, flag, td); 3367 else 3368 retval = ENODEV; 3369 3370 if (req->num_args > 0) { 3371 ctl_copyout_args(req->num_args, req->kern_args); 3372 ctl_free_args(req->num_args, req->kern_args); 3373 } 3374 break; 3375 } 3376 case CTL_PORT_LIST: { 3377 struct sbuf *sb; 3378 struct ctl_port *port; 3379 struct ctl_lun_list *list; 3380 struct ctl_option *opt; 3381 int j; 3382 uint32_t plun; 3383 3384 list = (struct ctl_lun_list *)addr; 3385 3386 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3387 if (sb == NULL) { 3388 list->status = CTL_LUN_LIST_ERROR; 3389 snprintf(list->error_str, sizeof(list->error_str), 3390 "Unable to allocate %d bytes for LUN list", 3391 list->alloc_len); 3392 break; 3393 } 3394 3395 sbuf_printf(sb, "<ctlportlist>\n"); 3396 3397 mtx_lock(&softc->ctl_lock); 3398 STAILQ_FOREACH(port, &softc->port_list, links) { 3399 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3400 (uintmax_t)port->targ_port); 3401 3402 /* 3403 * Bail out as soon as we see that we've overfilled 3404 * the buffer. 3405 */ 3406 if (retval != 0) 3407 break; 3408 3409 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3410 "</frontend_type>\n", port->frontend->name); 3411 if (retval != 0) 3412 break; 3413 3414 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3415 port->port_type); 3416 if (retval != 0) 3417 break; 3418 3419 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3420 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3421 if (retval != 0) 3422 break; 3423 3424 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3425 port->port_name); 3426 if (retval != 0) 3427 break; 3428 3429 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3430 port->physical_port); 3431 if (retval != 0) 3432 break; 3433 3434 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3435 port->virtual_port); 3436 if (retval != 0) 3437 break; 3438 3439 if (port->target_devid != NULL) { 3440 sbuf_printf(sb, "\t<target>"); 3441 ctl_id_sbuf(port->target_devid, sb); 3442 sbuf_printf(sb, "</target>\n"); 3443 } 3444 3445 if (port->port_devid != NULL) { 3446 sbuf_printf(sb, "\t<port>"); 3447 ctl_id_sbuf(port->port_devid, sb); 3448 sbuf_printf(sb, "</port>\n"); 3449 } 3450 3451 if (port->port_info != NULL) { 3452 retval = port->port_info(port->onoff_arg, sb); 3453 if (retval != 0) 3454 break; 3455 } 3456 STAILQ_FOREACH(opt, &port->options, links) { 3457 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3458 opt->name, opt->value, opt->name); 3459 if (retval != 0) 3460 break; 3461 } 3462 3463 if (port->lun_map != NULL) { 3464 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3465 for (j = 0; j < CTL_MAX_LUNS; j++) { 3466 plun = ctl_lun_map_from_port(port, j); 3467 if (plun >= CTL_MAX_LUNS) 3468 continue; 3469 sbuf_printf(sb, 3470 "\t<lun id=\"%u\">%u</lun>\n", 3471 j, plun); 3472 } 3473 } 3474 3475 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3476 if (port->wwpn_iid[j].in_use == 0 || 3477 (port->wwpn_iid[j].wwpn == 0 && 3478 port->wwpn_iid[j].name == NULL)) 3479 continue; 3480 3481 if (port->wwpn_iid[j].name != NULL) 3482 retval = sbuf_printf(sb, 3483 "\t<initiator id=\"%u\">%s</initiator>\n", 3484 j, port->wwpn_iid[j].name); 3485 else 3486 retval = sbuf_printf(sb, 3487 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3488 j, port->wwpn_iid[j].wwpn); 3489 if (retval != 0) 3490 break; 3491 } 3492 if (retval != 0) 3493 break; 3494 3495 retval = sbuf_printf(sb, "</targ_port>\n"); 3496 if (retval != 0) 3497 break; 3498 } 3499 mtx_unlock(&softc->ctl_lock); 3500 3501 if ((retval != 0) 3502 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3503 retval = 0; 3504 sbuf_delete(sb); 3505 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3506 snprintf(list->error_str, sizeof(list->error_str), 3507 "Out of space, %d bytes is too small", 3508 list->alloc_len); 3509 break; 3510 } 3511 3512 sbuf_finish(sb); 3513 3514 retval = copyout(sbuf_data(sb), list->lun_xml, 3515 sbuf_len(sb) + 1); 3516 3517 list->fill_len = sbuf_len(sb) + 1; 3518 list->status = CTL_LUN_LIST_OK; 3519 sbuf_delete(sb); 3520 break; 3521 } 3522 case CTL_LUN_MAP: { 3523 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3524 struct ctl_port *port; 3525 3526 mtx_lock(&softc->ctl_lock); 3527 if (lm->port < softc->port_min || 3528 lm->port >= softc->port_max || 3529 (port = softc->ctl_ports[lm->port]) == NULL) { 3530 mtx_unlock(&softc->ctl_lock); 3531 return (ENXIO); 3532 } 3533 if (port->status & CTL_PORT_STATUS_ONLINE) { 3534 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3535 if (ctl_lun_map_to_port(port, lun->lun) >= 3536 CTL_MAX_LUNS) 3537 continue; 3538 mtx_lock(&lun->lun_lock); 3539 ctl_est_ua_port(lun, lm->port, -1, 3540 CTL_UA_LUN_CHANGE); 3541 mtx_unlock(&lun->lun_lock); 3542 } 3543 } 3544 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3545 if (lm->plun < CTL_MAX_LUNS) { 3546 if (lm->lun == UINT32_MAX) 3547 retval = ctl_lun_map_unset(port, lm->plun); 3548 else if (lm->lun < CTL_MAX_LUNS && 3549 softc->ctl_luns[lm->lun] != NULL) 3550 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3551 else 3552 return (ENXIO); 3553 } else if (lm->plun == UINT32_MAX) { 3554 if (lm->lun == UINT32_MAX) 3555 retval = ctl_lun_map_deinit(port); 3556 else 3557 retval = ctl_lun_map_init(port); 3558 } else 3559 return (ENXIO); 3560 if (port->status & CTL_PORT_STATUS_ONLINE) 3561 ctl_isc_announce_port(port); 3562 break; 3563 } 3564 default: { 3565 /* XXX KDM should we fix this? */ 3566#if 0 3567 struct ctl_backend_driver *backend; 3568 unsigned int type; 3569 int found; 3570 3571 found = 0; 3572 3573 /* 3574 * We encode the backend type as the ioctl type for backend 3575 * ioctls. So parse it out here, and then search for a 3576 * backend of this type. 3577 */ 3578 type = _IOC_TYPE(cmd); 3579 3580 STAILQ_FOREACH(backend, &softc->be_list, links) { 3581 if (backend->type == type) { 3582 found = 1; 3583 break; 3584 } 3585 } 3586 if (found == 0) { 3587 printf("ctl: unknown ioctl command %#lx or backend " 3588 "%d\n", cmd, type); 3589 retval = EINVAL; 3590 break; 3591 } 3592 retval = backend->ioctl(dev, cmd, addr, flag, td); 3593#endif 3594 retval = ENOTTY; 3595 break; 3596 } 3597 } 3598 return (retval); 3599} 3600 3601uint32_t 3602ctl_get_initindex(struct ctl_nexus *nexus) 3603{ 3604 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3605} 3606 3607int 3608ctl_lun_map_init(struct ctl_port *port) 3609{ 3610 struct ctl_softc *softc = port->ctl_softc; 3611 struct ctl_lun *lun; 3612 uint32_t i; 3613 3614 if (port->lun_map == NULL) 3615 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3616 M_CTL, M_NOWAIT); 3617 if (port->lun_map == NULL) 3618 return (ENOMEM); 3619 for (i = 0; i < CTL_MAX_LUNS; i++) 3620 port->lun_map[i] = UINT32_MAX; 3621 if (port->status & CTL_PORT_STATUS_ONLINE) { 3622 if (port->lun_disable != NULL) { 3623 STAILQ_FOREACH(lun, &softc->lun_list, links) 3624 port->lun_disable(port->targ_lun_arg, lun->lun); 3625 } 3626 ctl_isc_announce_port(port); 3627 } 3628 return (0); 3629} 3630 3631int 3632ctl_lun_map_deinit(struct ctl_port *port) 3633{ 3634 struct ctl_softc *softc = port->ctl_softc; 3635 struct ctl_lun *lun; 3636 3637 if (port->lun_map == NULL) 3638 return (0); 3639 free(port->lun_map, M_CTL); 3640 port->lun_map = NULL; 3641 if (port->status & CTL_PORT_STATUS_ONLINE) { 3642 if (port->lun_enable != NULL) { 3643 STAILQ_FOREACH(lun, &softc->lun_list, links) 3644 port->lun_enable(port->targ_lun_arg, lun->lun); 3645 } 3646 ctl_isc_announce_port(port); 3647 } 3648 return (0); 3649} 3650 3651int 3652ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3653{ 3654 int status; 3655 uint32_t old; 3656 3657 if (port->lun_map == NULL) { 3658 status = ctl_lun_map_init(port); 3659 if (status != 0) 3660 return (status); 3661 } 3662 old = port->lun_map[plun]; 3663 port->lun_map[plun] = glun; 3664 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) { 3665 if (port->lun_enable != NULL) 3666 port->lun_enable(port->targ_lun_arg, plun); 3667 ctl_isc_announce_port(port); 3668 } 3669 return (0); 3670} 3671 3672int 3673ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3674{ 3675 uint32_t old; 3676 3677 if (port->lun_map == NULL) 3678 return (0); 3679 old = port->lun_map[plun]; 3680 port->lun_map[plun] = UINT32_MAX; 3681 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) { 3682 if (port->lun_disable != NULL) 3683 port->lun_disable(port->targ_lun_arg, plun); 3684 ctl_isc_announce_port(port); 3685 } 3686 return (0); 3687} 3688 3689uint32_t 3690ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3691{ 3692 3693 if (port == NULL) 3694 return (UINT32_MAX); 3695 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3696 return (lun_id); 3697 return (port->lun_map[lun_id]); 3698} 3699 3700uint32_t 3701ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3702{ 3703 uint32_t i; 3704 3705 if (port == NULL) 3706 return (UINT32_MAX); 3707 if (port->lun_map == NULL) 3708 return (lun_id); 3709 for (i = 0; i < CTL_MAX_LUNS; i++) { 3710 if (port->lun_map[i] == lun_id) 3711 return (i); 3712 } 3713 return (UINT32_MAX); 3714} 3715 3716static struct ctl_port * 3717ctl_io_port(struct ctl_io_hdr *io_hdr) 3718{ 3719 3720 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]); 3721} 3722 3723int 3724ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3725{ 3726 int i; 3727 3728 for (i = first; i < last; i++) { 3729 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3730 return (i); 3731 } 3732 return (-1); 3733} 3734 3735int 3736ctl_set_mask(uint32_t *mask, uint32_t bit) 3737{ 3738 uint32_t chunk, piece; 3739 3740 chunk = bit >> 5; 3741 piece = bit % (sizeof(uint32_t) * 8); 3742 3743 if ((mask[chunk] & (1 << piece)) != 0) 3744 return (-1); 3745 else 3746 mask[chunk] |= (1 << piece); 3747 3748 return (0); 3749} 3750 3751int 3752ctl_clear_mask(uint32_t *mask, uint32_t bit) 3753{ 3754 uint32_t chunk, piece; 3755 3756 chunk = bit >> 5; 3757 piece = bit % (sizeof(uint32_t) * 8); 3758 3759 if ((mask[chunk] & (1 << piece)) == 0) 3760 return (-1); 3761 else 3762 mask[chunk] &= ~(1 << piece); 3763 3764 return (0); 3765} 3766 3767int 3768ctl_is_set(uint32_t *mask, uint32_t bit) 3769{ 3770 uint32_t chunk, piece; 3771 3772 chunk = bit >> 5; 3773 piece = bit % (sizeof(uint32_t) * 8); 3774 3775 if ((mask[chunk] & (1 << piece)) == 0) 3776 return (0); 3777 else 3778 return (1); 3779} 3780 3781static uint64_t 3782ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3783{ 3784 uint64_t *t; 3785 3786 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3787 if (t == NULL) 3788 return (0); 3789 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3790} 3791 3792static void 3793ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3794{ 3795 uint64_t *t; 3796 3797 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3798 if (t == NULL) 3799 return; 3800 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3801} 3802 3803static void 3804ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3805{ 3806 uint64_t *p; 3807 u_int i; 3808 3809 i = residx/CTL_MAX_INIT_PER_PORT; 3810 if (lun->pr_keys[i] != NULL) 3811 return; 3812 mtx_unlock(&lun->lun_lock); 3813 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3814 M_WAITOK | M_ZERO); 3815 mtx_lock(&lun->lun_lock); 3816 if (lun->pr_keys[i] == NULL) 3817 lun->pr_keys[i] = p; 3818 else 3819 free(p, M_CTL); 3820} 3821 3822static void 3823ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3824{ 3825 uint64_t *t; 3826 3827 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3828 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3829 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3830} 3831 3832/* 3833 * ctl_softc, pool_name, total_ctl_io are passed in. 3834 * npool is passed out. 3835 */ 3836int 3837ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3838 uint32_t total_ctl_io, void **npool) 3839{ 3840#ifdef IO_POOLS 3841 struct ctl_io_pool *pool; 3842 3843 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3844 M_NOWAIT | M_ZERO); 3845 if (pool == NULL) 3846 return (ENOMEM); 3847 3848 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3849 pool->ctl_softc = ctl_softc; 3850 pool->zone = uma_zsecond_create(pool->name, NULL, 3851 NULL, NULL, NULL, ctl_softc->io_zone); 3852 /* uma_prealloc(pool->zone, total_ctl_io); */ 3853 3854 *npool = pool; 3855#else 3856 *npool = ctl_softc->io_zone; 3857#endif 3858 return (0); 3859} 3860 3861void 3862ctl_pool_free(struct ctl_io_pool *pool) 3863{ 3864 3865 if (pool == NULL) 3866 return; 3867 3868#ifdef IO_POOLS 3869 uma_zdestroy(pool->zone); 3870 free(pool, M_CTL); 3871#endif 3872} 3873 3874union ctl_io * 3875ctl_alloc_io(void *pool_ref) 3876{ 3877 union ctl_io *io; 3878#ifdef IO_POOLS 3879 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3880 3881 io = uma_zalloc(pool->zone, M_WAITOK); 3882#else 3883 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3884#endif 3885 if (io != NULL) 3886 io->io_hdr.pool = pool_ref; 3887 return (io); 3888} 3889 3890union ctl_io * 3891ctl_alloc_io_nowait(void *pool_ref) 3892{ 3893 union ctl_io *io; 3894#ifdef IO_POOLS 3895 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3896 3897 io = uma_zalloc(pool->zone, M_NOWAIT); 3898#else 3899 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3900#endif 3901 if (io != NULL) 3902 io->io_hdr.pool = pool_ref; 3903 return (io); 3904} 3905 3906void 3907ctl_free_io(union ctl_io *io) 3908{ 3909#ifdef IO_POOLS 3910 struct ctl_io_pool *pool; 3911#endif 3912 3913 if (io == NULL) 3914 return; 3915 3916#ifdef IO_POOLS 3917 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3918 uma_zfree(pool->zone, io); 3919#else 3920 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3921#endif 3922} 3923 3924void 3925ctl_zero_io(union ctl_io *io) 3926{ 3927 void *pool_ref; 3928 3929 if (io == NULL) 3930 return; 3931 3932 /* 3933 * May need to preserve linked list pointers at some point too. 3934 */ 3935 pool_ref = io->io_hdr.pool; 3936 memset(io, 0, sizeof(*io)); 3937 io->io_hdr.pool = pool_ref; 3938} 3939 3940/* 3941 * This routine is currently used for internal copies of ctl_ios that need 3942 * to persist for some reason after we've already returned status to the 3943 * FETD. (Thus the flag set.) 3944 * 3945 * XXX XXX 3946 * Note that this makes a blind copy of all fields in the ctl_io, except 3947 * for the pool reference. This includes any memory that has been 3948 * allocated! That memory will no longer be valid after done has been 3949 * called, so this would be VERY DANGEROUS for command that actually does 3950 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3951 * start and stop commands, which don't transfer any data, so this is not a 3952 * problem. If it is used for anything else, the caller would also need to 3953 * allocate data buffer space and this routine would need to be modified to 3954 * copy the data buffer(s) as well. 3955 */ 3956void 3957ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3958{ 3959 void *pool_ref; 3960 3961 if ((src == NULL) 3962 || (dest == NULL)) 3963 return; 3964 3965 /* 3966 * May need to preserve linked list pointers at some point too. 3967 */ 3968 pool_ref = dest->io_hdr.pool; 3969 3970 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest))); 3971 3972 dest->io_hdr.pool = pool_ref; 3973 /* 3974 * We need to know that this is an internal copy, and doesn't need 3975 * to get passed back to the FETD that allocated it. 3976 */ 3977 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3978} 3979 3980int 3981ctl_expand_number(const char *buf, uint64_t *num) 3982{ 3983 char *endptr; 3984 uint64_t number; 3985 unsigned shift; 3986 3987 number = strtoq(buf, &endptr, 0); 3988 3989 switch (tolower((unsigned char)*endptr)) { 3990 case 'e': 3991 shift = 60; 3992 break; 3993 case 'p': 3994 shift = 50; 3995 break; 3996 case 't': 3997 shift = 40; 3998 break; 3999 case 'g': 4000 shift = 30; 4001 break; 4002 case 'm': 4003 shift = 20; 4004 break; 4005 case 'k': 4006 shift = 10; 4007 break; 4008 case 'b': 4009 case '\0': /* No unit. */ 4010 *num = number; 4011 return (0); 4012 default: 4013 /* Unrecognized unit. */ 4014 return (-1); 4015 } 4016 4017 if ((number << shift) >> shift != number) { 4018 /* Overflow */ 4019 return (-1); 4020 } 4021 *num = number << shift; 4022 return (0); 4023} 4024 4025 4026/* 4027 * This routine could be used in the future to load default and/or saved 4028 * mode page parameters for a particuar lun. 4029 */ 4030static int 4031ctl_init_page_index(struct ctl_lun *lun) 4032{ 4033 int i; 4034 struct ctl_page_index *page_index; 4035 const char *value; 4036 uint64_t ival; 4037 4038 memcpy(&lun->mode_pages.index, page_index_template, 4039 sizeof(page_index_template)); 4040 4041 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4042 4043 page_index = &lun->mode_pages.index[i]; 4044 /* 4045 * If this is a disk-only mode page, there's no point in 4046 * setting it up. For some pages, we have to have some 4047 * basic information about the disk in order to calculate the 4048 * mode page data. 4049 */ 4050 if ((lun->be_lun->lun_type != T_DIRECT) 4051 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4052 continue; 4053 4054 switch (page_index->page_code & SMPH_PC_MASK) { 4055 case SMS_RW_ERROR_RECOVERY_PAGE: { 4056 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4057 panic("subpage is incorrect!"); 4058 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4059 &rw_er_page_default, 4060 sizeof(rw_er_page_default)); 4061 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4062 &rw_er_page_changeable, 4063 sizeof(rw_er_page_changeable)); 4064 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4065 &rw_er_page_default, 4066 sizeof(rw_er_page_default)); 4067 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4068 &rw_er_page_default, 4069 sizeof(rw_er_page_default)); 4070 page_index->page_data = 4071 (uint8_t *)lun->mode_pages.rw_er_page; 4072 break; 4073 } 4074 case SMS_FORMAT_DEVICE_PAGE: { 4075 struct scsi_format_page *format_page; 4076 4077 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4078 panic("subpage is incorrect!"); 4079 4080 /* 4081 * Sectors per track are set above. Bytes per 4082 * sector need to be set here on a per-LUN basis. 4083 */ 4084 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4085 &format_page_default, 4086 sizeof(format_page_default)); 4087 memcpy(&lun->mode_pages.format_page[ 4088 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4089 sizeof(format_page_changeable)); 4090 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4091 &format_page_default, 4092 sizeof(format_page_default)); 4093 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4094 &format_page_default, 4095 sizeof(format_page_default)); 4096 4097 format_page = &lun->mode_pages.format_page[ 4098 CTL_PAGE_CURRENT]; 4099 scsi_ulto2b(lun->be_lun->blocksize, 4100 format_page->bytes_per_sector); 4101 4102 format_page = &lun->mode_pages.format_page[ 4103 CTL_PAGE_DEFAULT]; 4104 scsi_ulto2b(lun->be_lun->blocksize, 4105 format_page->bytes_per_sector); 4106 4107 format_page = &lun->mode_pages.format_page[ 4108 CTL_PAGE_SAVED]; 4109 scsi_ulto2b(lun->be_lun->blocksize, 4110 format_page->bytes_per_sector); 4111 4112 page_index->page_data = 4113 (uint8_t *)lun->mode_pages.format_page; 4114 break; 4115 } 4116 case SMS_RIGID_DISK_PAGE: { 4117 struct scsi_rigid_disk_page *rigid_disk_page; 4118 uint32_t sectors_per_cylinder; 4119 uint64_t cylinders; 4120#ifndef __XSCALE__ 4121 int shift; 4122#endif /* !__XSCALE__ */ 4123 4124 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4125 panic("invalid subpage value %d", 4126 page_index->subpage); 4127 4128 /* 4129 * Rotation rate and sectors per track are set 4130 * above. We calculate the cylinders here based on 4131 * capacity. Due to the number of heads and 4132 * sectors per track we're using, smaller arrays 4133 * may turn out to have 0 cylinders. Linux and 4134 * FreeBSD don't pay attention to these mode pages 4135 * to figure out capacity, but Solaris does. It 4136 * seems to deal with 0 cylinders just fine, and 4137 * works out a fake geometry based on the capacity. 4138 */ 4139 memcpy(&lun->mode_pages.rigid_disk_page[ 4140 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4141 sizeof(rigid_disk_page_default)); 4142 memcpy(&lun->mode_pages.rigid_disk_page[ 4143 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4144 sizeof(rigid_disk_page_changeable)); 4145 4146 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4147 CTL_DEFAULT_HEADS; 4148 4149 /* 4150 * The divide method here will be more accurate, 4151 * probably, but results in floating point being 4152 * used in the kernel on i386 (__udivdi3()). On the 4153 * XScale, though, __udivdi3() is implemented in 4154 * software. 4155 * 4156 * The shift method for cylinder calculation is 4157 * accurate if sectors_per_cylinder is a power of 4158 * 2. Otherwise it might be slightly off -- you 4159 * might have a bit of a truncation problem. 4160 */ 4161#ifdef __XSCALE__ 4162 cylinders = (lun->be_lun->maxlba + 1) / 4163 sectors_per_cylinder; 4164#else 4165 for (shift = 31; shift > 0; shift--) { 4166 if (sectors_per_cylinder & (1 << shift)) 4167 break; 4168 } 4169 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4170#endif 4171 4172 /* 4173 * We've basically got 3 bytes, or 24 bits for the 4174 * cylinder size in the mode page. If we're over, 4175 * just round down to 2^24. 4176 */ 4177 if (cylinders > 0xffffff) 4178 cylinders = 0xffffff; 4179 4180 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4181 CTL_PAGE_DEFAULT]; 4182 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4183 4184 if ((value = ctl_get_opt(&lun->be_lun->options, 4185 "rpm")) != NULL) { 4186 scsi_ulto2b(strtol(value, NULL, 0), 4187 rigid_disk_page->rotation_rate); 4188 } 4189 4190 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4191 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4192 sizeof(rigid_disk_page_default)); 4193 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4194 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4195 sizeof(rigid_disk_page_default)); 4196 4197 page_index->page_data = 4198 (uint8_t *)lun->mode_pages.rigid_disk_page; 4199 break; 4200 } 4201 case SMS_CACHING_PAGE: { 4202 struct scsi_caching_page *caching_page; 4203 4204 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4205 panic("invalid subpage value %d", 4206 page_index->subpage); 4207 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4208 &caching_page_default, 4209 sizeof(caching_page_default)); 4210 memcpy(&lun->mode_pages.caching_page[ 4211 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4212 sizeof(caching_page_changeable)); 4213 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4214 &caching_page_default, 4215 sizeof(caching_page_default)); 4216 caching_page = &lun->mode_pages.caching_page[ 4217 CTL_PAGE_SAVED]; 4218 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4219 if (value != NULL && strcmp(value, "off") == 0) 4220 caching_page->flags1 &= ~SCP_WCE; 4221 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4222 if (value != NULL && strcmp(value, "off") == 0) 4223 caching_page->flags1 |= SCP_RCD; 4224 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4225 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4226 sizeof(caching_page_default)); 4227 page_index->page_data = 4228 (uint8_t *)lun->mode_pages.caching_page; 4229 break; 4230 } 4231 case SMS_CONTROL_MODE_PAGE: { 4232 switch (page_index->subpage) { 4233 case SMS_SUBPAGE_PAGE_0: { 4234 struct scsi_control_page *control_page; 4235 4236 memcpy(&lun->mode_pages.control_page[ 4237 CTL_PAGE_DEFAULT], 4238 &control_page_default, 4239 sizeof(control_page_default)); 4240 memcpy(&lun->mode_pages.control_page[ 4241 CTL_PAGE_CHANGEABLE], 4242 &control_page_changeable, 4243 sizeof(control_page_changeable)); 4244 memcpy(&lun->mode_pages.control_page[ 4245 CTL_PAGE_SAVED], 4246 &control_page_default, 4247 sizeof(control_page_default)); 4248 control_page = &lun->mode_pages.control_page[ 4249 CTL_PAGE_SAVED]; 4250 value = ctl_get_opt(&lun->be_lun->options, 4251 "reordering"); 4252 if (value != NULL && 4253 strcmp(value, "unrestricted") == 0) { 4254 control_page->queue_flags &= 4255 ~SCP_QUEUE_ALG_MASK; 4256 control_page->queue_flags |= 4257 SCP_QUEUE_ALG_UNRESTRICTED; 4258 } 4259 memcpy(&lun->mode_pages.control_page[ 4260 CTL_PAGE_CURRENT], 4261 &lun->mode_pages.control_page[ 4262 CTL_PAGE_SAVED], 4263 sizeof(control_page_default)); 4264 page_index->page_data = 4265 (uint8_t *)lun->mode_pages.control_page; 4266 break; 4267 } 4268 case 0x01: 4269 memcpy(&lun->mode_pages.control_ext_page[ 4270 CTL_PAGE_DEFAULT], 4271 &control_ext_page_default, 4272 sizeof(control_ext_page_default)); 4273 memcpy(&lun->mode_pages.control_ext_page[ 4274 CTL_PAGE_CHANGEABLE], 4275 &control_ext_page_changeable, 4276 sizeof(control_ext_page_changeable)); 4277 memcpy(&lun->mode_pages.control_ext_page[ 4278 CTL_PAGE_SAVED], 4279 &control_ext_page_default, 4280 sizeof(control_ext_page_default)); 4281 memcpy(&lun->mode_pages.control_ext_page[ 4282 CTL_PAGE_CURRENT], 4283 &lun->mode_pages.control_ext_page[ 4284 CTL_PAGE_SAVED], 4285 sizeof(control_ext_page_default)); 4286 page_index->page_data = 4287 (uint8_t *)lun->mode_pages.control_ext_page; 4288 break; 4289 } 4290 break; 4291 } 4292 case SMS_INFO_EXCEPTIONS_PAGE: { 4293 switch (page_index->subpage) { 4294 case SMS_SUBPAGE_PAGE_0: 4295 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4296 &ie_page_default, 4297 sizeof(ie_page_default)); 4298 memcpy(&lun->mode_pages.ie_page[ 4299 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4300 sizeof(ie_page_changeable)); 4301 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4302 &ie_page_default, 4303 sizeof(ie_page_default)); 4304 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4305 &ie_page_default, 4306 sizeof(ie_page_default)); 4307 page_index->page_data = 4308 (uint8_t *)lun->mode_pages.ie_page; 4309 break; 4310 case 0x02: { 4311 struct ctl_logical_block_provisioning_page *page; 4312 4313 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4314 &lbp_page_default, 4315 sizeof(lbp_page_default)); 4316 memcpy(&lun->mode_pages.lbp_page[ 4317 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4318 sizeof(lbp_page_changeable)); 4319 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4320 &lbp_page_default, 4321 sizeof(lbp_page_default)); 4322 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4323 value = ctl_get_opt(&lun->be_lun->options, 4324 "avail-threshold"); 4325 if (value != NULL && 4326 ctl_expand_number(value, &ival) == 0) { 4327 page->descr[0].flags |= SLBPPD_ENABLED | 4328 SLBPPD_ARMING_DEC; 4329 if (lun->be_lun->blocksize) 4330 ival /= lun->be_lun->blocksize; 4331 else 4332 ival /= 512; 4333 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4334 page->descr[0].count); 4335 } 4336 value = ctl_get_opt(&lun->be_lun->options, 4337 "used-threshold"); 4338 if (value != NULL && 4339 ctl_expand_number(value, &ival) == 0) { 4340 page->descr[1].flags |= SLBPPD_ENABLED | 4341 SLBPPD_ARMING_INC; 4342 if (lun->be_lun->blocksize) 4343 ival /= lun->be_lun->blocksize; 4344 else 4345 ival /= 512; 4346 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4347 page->descr[1].count); 4348 } 4349 value = ctl_get_opt(&lun->be_lun->options, 4350 "pool-avail-threshold"); 4351 if (value != NULL && 4352 ctl_expand_number(value, &ival) == 0) { 4353 page->descr[2].flags |= SLBPPD_ENABLED | 4354 SLBPPD_ARMING_DEC; 4355 if (lun->be_lun->blocksize) 4356 ival /= lun->be_lun->blocksize; 4357 else 4358 ival /= 512; 4359 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4360 page->descr[2].count); 4361 } 4362 value = ctl_get_opt(&lun->be_lun->options, 4363 "pool-used-threshold"); 4364 if (value != NULL && 4365 ctl_expand_number(value, &ival) == 0) { 4366 page->descr[3].flags |= SLBPPD_ENABLED | 4367 SLBPPD_ARMING_INC; 4368 if (lun->be_lun->blocksize) 4369 ival /= lun->be_lun->blocksize; 4370 else 4371 ival /= 512; 4372 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4373 page->descr[3].count); 4374 } 4375 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4376 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4377 sizeof(lbp_page_default)); 4378 page_index->page_data = 4379 (uint8_t *)lun->mode_pages.lbp_page; 4380 }} 4381 break; 4382 } 4383 case SMS_VENDOR_SPECIFIC_PAGE:{ 4384 switch (page_index->subpage) { 4385 case DBGCNF_SUBPAGE_CODE: { 4386 struct copan_debugconf_subpage *current_page, 4387 *saved_page; 4388 4389 memcpy(&lun->mode_pages.debugconf_subpage[ 4390 CTL_PAGE_CURRENT], 4391 &debugconf_page_default, 4392 sizeof(debugconf_page_default)); 4393 memcpy(&lun->mode_pages.debugconf_subpage[ 4394 CTL_PAGE_CHANGEABLE], 4395 &debugconf_page_changeable, 4396 sizeof(debugconf_page_changeable)); 4397 memcpy(&lun->mode_pages.debugconf_subpage[ 4398 CTL_PAGE_DEFAULT], 4399 &debugconf_page_default, 4400 sizeof(debugconf_page_default)); 4401 memcpy(&lun->mode_pages.debugconf_subpage[ 4402 CTL_PAGE_SAVED], 4403 &debugconf_page_default, 4404 sizeof(debugconf_page_default)); 4405 page_index->page_data = 4406 (uint8_t *)lun->mode_pages.debugconf_subpage; 4407 4408 current_page = (struct copan_debugconf_subpage *) 4409 (page_index->page_data + 4410 (page_index->page_len * 4411 CTL_PAGE_CURRENT)); 4412 saved_page = (struct copan_debugconf_subpage *) 4413 (page_index->page_data + 4414 (page_index->page_len * 4415 CTL_PAGE_SAVED)); 4416 break; 4417 } 4418 default: 4419 panic("invalid subpage value %d", 4420 page_index->subpage); 4421 break; 4422 } 4423 break; 4424 } 4425 default: 4426 panic("invalid page value %d", 4427 page_index->page_code & SMPH_PC_MASK); 4428 break; 4429 } 4430 } 4431 4432 return (CTL_RETVAL_COMPLETE); 4433} 4434 4435static int 4436ctl_init_log_page_index(struct ctl_lun *lun) 4437{ 4438 struct ctl_page_index *page_index; 4439 int i, j, k, prev; 4440 4441 memcpy(&lun->log_pages.index, log_page_index_template, 4442 sizeof(log_page_index_template)); 4443 4444 prev = -1; 4445 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4446 4447 page_index = &lun->log_pages.index[i]; 4448 /* 4449 * If this is a disk-only mode page, there's no point in 4450 * setting it up. For some pages, we have to have some 4451 * basic information about the disk in order to calculate the 4452 * mode page data. 4453 */ 4454 if ((lun->be_lun->lun_type != T_DIRECT) 4455 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4456 continue; 4457 4458 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4459 lun->backend->lun_attr == NULL) 4460 continue; 4461 4462 if (page_index->page_code != prev) { 4463 lun->log_pages.pages_page[j] = page_index->page_code; 4464 prev = page_index->page_code; 4465 j++; 4466 } 4467 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4468 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4469 k++; 4470 } 4471 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4472 lun->log_pages.index[0].page_len = j; 4473 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4474 lun->log_pages.index[1].page_len = k * 2; 4475 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4476 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4477 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4478 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4479 4480 return (CTL_RETVAL_COMPLETE); 4481} 4482 4483static int 4484hex2bin(const char *str, uint8_t *buf, int buf_size) 4485{ 4486 int i; 4487 u_char c; 4488 4489 memset(buf, 0, buf_size); 4490 while (isspace(str[0])) 4491 str++; 4492 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4493 str += 2; 4494 buf_size *= 2; 4495 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4496 c = str[i]; 4497 if (isdigit(c)) 4498 c -= '0'; 4499 else if (isalpha(c)) 4500 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4501 else 4502 break; 4503 if (c >= 16) 4504 break; 4505 if ((i & 1) == 0) 4506 buf[i / 2] |= (c << 4); 4507 else 4508 buf[i / 2] |= c; 4509 } 4510 return ((i + 1) / 2); 4511} 4512 4513/* 4514 * LUN allocation. 4515 * 4516 * Requirements: 4517 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4518 * wants us to allocate the LUN and he can block. 4519 * - ctl_softc is always set 4520 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4521 * 4522 * Returns 0 for success, non-zero (errno) for failure. 4523 */ 4524static int 4525ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4526 struct ctl_be_lun *const be_lun) 4527{ 4528 struct ctl_lun *nlun, *lun; 4529 struct scsi_vpd_id_descriptor *desc; 4530 struct scsi_vpd_id_t10 *t10id; 4531 const char *eui, *naa, *scsiname, *vendor; 4532 int lun_number, i, lun_malloced; 4533 int devidlen, idlen1, idlen2 = 0, len; 4534 4535 if (be_lun == NULL) 4536 return (EINVAL); 4537 4538 /* 4539 * We currently only support Direct Access or Processor LUN types. 4540 */ 4541 switch (be_lun->lun_type) { 4542 case T_DIRECT: 4543 break; 4544 case T_PROCESSOR: 4545 break; 4546 case T_SEQUENTIAL: 4547 case T_CHANGER: 4548 default: 4549 be_lun->lun_config_status(be_lun->be_lun, 4550 CTL_LUN_CONFIG_FAILURE); 4551 break; 4552 } 4553 if (ctl_lun == NULL) { 4554 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4555 lun_malloced = 1; 4556 } else { 4557 lun_malloced = 0; 4558 lun = ctl_lun; 4559 } 4560 4561 memset(lun, 0, sizeof(*lun)); 4562 if (lun_malloced) 4563 lun->flags = CTL_LUN_MALLOCED; 4564 4565 /* Generate LUN ID. */ 4566 devidlen = max(CTL_DEVID_MIN_LEN, 4567 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4568 idlen1 = sizeof(*t10id) + devidlen; 4569 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4570 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4571 if (scsiname != NULL) { 4572 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4573 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4574 } 4575 eui = ctl_get_opt(&be_lun->options, "eui"); 4576 if (eui != NULL) { 4577 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4578 } 4579 naa = ctl_get_opt(&be_lun->options, "naa"); 4580 if (naa != NULL) { 4581 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4582 } 4583 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4584 M_CTL, M_WAITOK | M_ZERO); 4585 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4586 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4587 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4588 desc->length = idlen1; 4589 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4590 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4591 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4592 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4593 } else { 4594 strncpy(t10id->vendor, vendor, 4595 min(sizeof(t10id->vendor), strlen(vendor))); 4596 } 4597 strncpy((char *)t10id->vendor_spec_id, 4598 (char *)be_lun->device_id, devidlen); 4599 if (scsiname != NULL) { 4600 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4601 desc->length); 4602 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4603 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4604 SVPD_ID_TYPE_SCSI_NAME; 4605 desc->length = idlen2; 4606 strlcpy(desc->identifier, scsiname, idlen2); 4607 } 4608 if (eui != NULL) { 4609 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4610 desc->length); 4611 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4612 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4613 SVPD_ID_TYPE_EUI64; 4614 desc->length = hex2bin(eui, desc->identifier, 16); 4615 desc->length = desc->length > 12 ? 16 : 4616 (desc->length > 8 ? 12 : 8); 4617 len -= 16 - desc->length; 4618 } 4619 if (naa != NULL) { 4620 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4621 desc->length); 4622 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4623 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4624 SVPD_ID_TYPE_NAA; 4625 desc->length = hex2bin(naa, desc->identifier, 16); 4626 desc->length = desc->length > 8 ? 16 : 8; 4627 len -= 16 - desc->length; 4628 } 4629 lun->lun_devid->len = len; 4630 4631 mtx_lock(&ctl_softc->ctl_lock); 4632 /* 4633 * See if the caller requested a particular LUN number. If so, see 4634 * if it is available. Otherwise, allocate the first available LUN. 4635 */ 4636 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4637 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4638 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4639 mtx_unlock(&ctl_softc->ctl_lock); 4640 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4641 printf("ctl: requested LUN ID %d is higher " 4642 "than CTL_MAX_LUNS - 1 (%d)\n", 4643 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4644 } else { 4645 /* 4646 * XXX KDM return an error, or just assign 4647 * another LUN ID in this case?? 4648 */ 4649 printf("ctl: requested LUN ID %d is already " 4650 "in use\n", be_lun->req_lun_id); 4651 } 4652 if (lun->flags & CTL_LUN_MALLOCED) 4653 free(lun, M_CTL); 4654 be_lun->lun_config_status(be_lun->be_lun, 4655 CTL_LUN_CONFIG_FAILURE); 4656 return (ENOSPC); 4657 } 4658 lun_number = be_lun->req_lun_id; 4659 } else { 4660 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4661 if (lun_number == -1) { 4662 mtx_unlock(&ctl_softc->ctl_lock); 4663 printf("ctl: can't allocate LUN, out of LUNs\n"); 4664 if (lun->flags & CTL_LUN_MALLOCED) 4665 free(lun, M_CTL); 4666 be_lun->lun_config_status(be_lun->be_lun, 4667 CTL_LUN_CONFIG_FAILURE); 4668 return (ENOSPC); 4669 } 4670 } 4671 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4672 4673 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4674 lun->lun = lun_number; 4675 lun->be_lun = be_lun; 4676 /* 4677 * The processor LUN is always enabled. Disk LUNs come on line 4678 * disabled, and must be enabled by the backend. 4679 */ 4680 lun->flags |= CTL_LUN_DISABLED; 4681 lun->backend = be_lun->be; 4682 be_lun->ctl_lun = lun; 4683 be_lun->lun_id = lun_number; 4684 atomic_add_int(&be_lun->be->num_luns, 1); 4685 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4686 lun->flags |= CTL_LUN_OFFLINE; 4687 4688 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4689 lun->flags |= CTL_LUN_STOPPED; 4690 4691 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4692 lun->flags |= CTL_LUN_INOPERABLE; 4693 4694 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4695 lun->flags |= CTL_LUN_PRIMARY_SC; 4696 4697 lun->ctl_softc = ctl_softc; 4698#ifdef CTL_TIME_IO 4699 lun->last_busy = getsbinuptime(); 4700#endif 4701 TAILQ_INIT(&lun->ooa_queue); 4702 TAILQ_INIT(&lun->blocked_queue); 4703 STAILQ_INIT(&lun->error_list); 4704 ctl_tpc_lun_init(lun); 4705 4706 /* 4707 * Initialize the mode and log page index. 4708 */ 4709 ctl_init_page_index(lun); 4710 ctl_init_log_page_index(lun); 4711 4712 /* 4713 * Now, before we insert this lun on the lun list, set the lun 4714 * inventory changed UA for all other luns. 4715 */ 4716 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4717 mtx_lock(&nlun->lun_lock); 4718 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4719 mtx_unlock(&nlun->lun_lock); 4720 } 4721 4722 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4723 4724 ctl_softc->ctl_luns[lun_number] = lun; 4725 4726 ctl_softc->num_luns++; 4727 4728 /* Setup statistics gathering */ 4729 lun->stats.device_type = be_lun->lun_type; 4730 lun->stats.lun_number = lun_number; 4731 if (lun->stats.device_type == T_DIRECT) 4732 lun->stats.blocksize = be_lun->blocksize; 4733 else 4734 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4735 for (i = 0;i < CTL_MAX_PORTS;i++) 4736 lun->stats.ports[i].targ_port = i; 4737 4738 mtx_unlock(&ctl_softc->ctl_lock); 4739 4740 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4741 return (0); 4742} 4743 4744/* 4745 * Delete a LUN. 4746 * Assumptions: 4747 * - LUN has already been marked invalid and any pending I/O has been taken 4748 * care of. 4749 */ 4750static int 4751ctl_free_lun(struct ctl_lun *lun) 4752{ 4753 struct ctl_softc *softc; 4754 struct ctl_lun *nlun; 4755 int i; 4756 4757 softc = lun->ctl_softc; 4758 4759 mtx_assert(&softc->ctl_lock, MA_OWNED); 4760 4761 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4762 4763 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4764 4765 softc->ctl_luns[lun->lun] = NULL; 4766 4767 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4768 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4769 4770 softc->num_luns--; 4771 4772 /* 4773 * Tell the backend to free resources, if this LUN has a backend. 4774 */ 4775 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4776 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4777 4778 ctl_tpc_lun_shutdown(lun); 4779 mtx_destroy(&lun->lun_lock); 4780 free(lun->lun_devid, M_CTL); 4781 for (i = 0; i < CTL_MAX_PORTS; i++) 4782 free(lun->pending_ua[i], M_CTL); 4783 for (i = 0; i < CTL_MAX_PORTS; i++) 4784 free(lun->pr_keys[i], M_CTL); 4785 free(lun->write_buffer, M_CTL); 4786 if (lun->flags & CTL_LUN_MALLOCED) 4787 free(lun, M_CTL); 4788 4789 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4790 mtx_lock(&nlun->lun_lock); 4791 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4792 mtx_unlock(&nlun->lun_lock); 4793 } 4794 4795 return (0); 4796} 4797 4798static void 4799ctl_create_lun(struct ctl_be_lun *be_lun) 4800{ 4801 4802 /* 4803 * ctl_alloc_lun() should handle all potential failure cases. 4804 */ 4805 ctl_alloc_lun(control_softc, NULL, be_lun); 4806} 4807 4808int 4809ctl_add_lun(struct ctl_be_lun *be_lun) 4810{ 4811 struct ctl_softc *softc = control_softc; 4812 4813 mtx_lock(&softc->ctl_lock); 4814 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4815 mtx_unlock(&softc->ctl_lock); 4816 wakeup(&softc->pending_lun_queue); 4817 4818 return (0); 4819} 4820 4821int 4822ctl_enable_lun(struct ctl_be_lun *be_lun) 4823{ 4824 struct ctl_softc *softc; 4825 struct ctl_port *port, *nport; 4826 struct ctl_lun *lun; 4827 int retval; 4828 4829 lun = (struct ctl_lun *)be_lun->ctl_lun; 4830 softc = lun->ctl_softc; 4831 4832 mtx_lock(&softc->ctl_lock); 4833 mtx_lock(&lun->lun_lock); 4834 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4835 /* 4836 * eh? Why did we get called if the LUN is already 4837 * enabled? 4838 */ 4839 mtx_unlock(&lun->lun_lock); 4840 mtx_unlock(&softc->ctl_lock); 4841 return (0); 4842 } 4843 lun->flags &= ~CTL_LUN_DISABLED; 4844 mtx_unlock(&lun->lun_lock); 4845 4846 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) { 4847 nport = STAILQ_NEXT(port, links); 4848 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4849 port->lun_map != NULL || port->lun_enable == NULL) 4850 continue; 4851 4852 /* 4853 * Drop the lock while we call the FETD's enable routine. 4854 * This can lead to a callback into CTL (at least in the 4855 * case of the internal initiator frontend. 4856 */ 4857 mtx_unlock(&softc->ctl_lock); 4858 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4859 mtx_lock(&softc->ctl_lock); 4860 if (retval != 0) { 4861 printf("%s: FETD %s port %d returned error " 4862 "%d for lun_enable on lun %jd\n", 4863 __func__, port->port_name, port->targ_port, 4864 retval, (intmax_t)lun->lun); 4865 } 4866 } 4867 4868 mtx_unlock(&softc->ctl_lock); 4869 ctl_isc_announce_lun(lun); 4870 4871 return (0); 4872} 4873 4874int 4875ctl_disable_lun(struct ctl_be_lun *be_lun) 4876{ 4877 struct ctl_softc *softc; 4878 struct ctl_port *port; 4879 struct ctl_lun *lun; 4880 int retval; 4881 4882 lun = (struct ctl_lun *)be_lun->ctl_lun; 4883 softc = lun->ctl_softc; 4884 4885 mtx_lock(&softc->ctl_lock); 4886 mtx_lock(&lun->lun_lock); 4887 if (lun->flags & CTL_LUN_DISABLED) { 4888 mtx_unlock(&lun->lun_lock); 4889 mtx_unlock(&softc->ctl_lock); 4890 return (0); 4891 } 4892 lun->flags |= CTL_LUN_DISABLED; 4893 mtx_unlock(&lun->lun_lock); 4894 4895 STAILQ_FOREACH(port, &softc->port_list, links) { 4896 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4897 port->lun_map != NULL || port->lun_disable == NULL) 4898 continue; 4899 4900 /* 4901 * Drop the lock before we call the frontend's disable 4902 * routine, to avoid lock order reversals. 4903 * 4904 * XXX KDM what happens if the frontend list changes while 4905 * we're traversing it? It's unlikely, but should be handled. 4906 */ 4907 mtx_unlock(&softc->ctl_lock); 4908 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4909 mtx_lock(&softc->ctl_lock); 4910 if (retval != 0) { 4911 printf("%s: FETD %s port %d returned error " 4912 "%d for lun_disable on lun %jd\n", 4913 __func__, port->port_name, port->targ_port, 4914 retval, (intmax_t)lun->lun); 4915 } 4916 } 4917 4918 mtx_unlock(&softc->ctl_lock); 4919 ctl_isc_announce_lun(lun); 4920 4921 return (0); 4922} 4923 4924int 4925ctl_start_lun(struct ctl_be_lun *be_lun) 4926{ 4927 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4928 4929 mtx_lock(&lun->lun_lock); 4930 lun->flags &= ~CTL_LUN_STOPPED; 4931 mtx_unlock(&lun->lun_lock); 4932 return (0); 4933} 4934 4935int 4936ctl_stop_lun(struct ctl_be_lun *be_lun) 4937{ 4938 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4939 4940 mtx_lock(&lun->lun_lock); 4941 lun->flags |= CTL_LUN_STOPPED; 4942 mtx_unlock(&lun->lun_lock); 4943 return (0); 4944} 4945 4946int 4947ctl_lun_offline(struct ctl_be_lun *be_lun) 4948{ 4949 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4950 4951 mtx_lock(&lun->lun_lock); 4952 lun->flags |= CTL_LUN_OFFLINE; 4953 mtx_unlock(&lun->lun_lock); 4954 return (0); 4955} 4956 4957int 4958ctl_lun_online(struct ctl_be_lun *be_lun) 4959{ 4960 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4961 4962 mtx_lock(&lun->lun_lock); 4963 lun->flags &= ~CTL_LUN_OFFLINE; 4964 mtx_unlock(&lun->lun_lock); 4965 return (0); 4966} 4967 4968int 4969ctl_lun_primary(struct ctl_be_lun *be_lun) 4970{ 4971 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4972 4973 mtx_lock(&lun->lun_lock); 4974 lun->flags |= CTL_LUN_PRIMARY_SC; 4975 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4976 mtx_unlock(&lun->lun_lock); 4977 ctl_isc_announce_lun(lun); 4978 return (0); 4979} 4980 4981int 4982ctl_lun_secondary(struct ctl_be_lun *be_lun) 4983{ 4984 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4985 4986 mtx_lock(&lun->lun_lock); 4987 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4988 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4989 mtx_unlock(&lun->lun_lock); 4990 ctl_isc_announce_lun(lun); 4991 return (0); 4992} 4993 4994int 4995ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4996{ 4997 struct ctl_softc *softc; 4998 struct ctl_lun *lun; 4999 5000 lun = (struct ctl_lun *)be_lun->ctl_lun; 5001 softc = lun->ctl_softc; 5002 5003 mtx_lock(&lun->lun_lock); 5004 5005 /* 5006 * The LUN needs to be disabled before it can be marked invalid. 5007 */ 5008 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 5009 mtx_unlock(&lun->lun_lock); 5010 return (-1); 5011 } 5012 /* 5013 * Mark the LUN invalid. 5014 */ 5015 lun->flags |= CTL_LUN_INVALID; 5016 5017 /* 5018 * If there is nothing in the OOA queue, go ahead and free the LUN. 5019 * If we have something in the OOA queue, we'll free it when the 5020 * last I/O completes. 5021 */ 5022 if (TAILQ_EMPTY(&lun->ooa_queue)) { 5023 mtx_unlock(&lun->lun_lock); 5024 mtx_lock(&softc->ctl_lock); 5025 ctl_free_lun(lun); 5026 mtx_unlock(&softc->ctl_lock); 5027 } else 5028 mtx_unlock(&lun->lun_lock); 5029 5030 return (0); 5031} 5032 5033int 5034ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5035{ 5036 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5037 5038 mtx_lock(&lun->lun_lock); 5039 lun->flags |= CTL_LUN_INOPERABLE; 5040 mtx_unlock(&lun->lun_lock); 5041 return (0); 5042} 5043 5044int 5045ctl_lun_operable(struct ctl_be_lun *be_lun) 5046{ 5047 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5048 5049 mtx_lock(&lun->lun_lock); 5050 lun->flags &= ~CTL_LUN_INOPERABLE; 5051 mtx_unlock(&lun->lun_lock); 5052 return (0); 5053} 5054 5055void 5056ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5057{ 5058 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5059 union ctl_ha_msg msg; 5060 5061 mtx_lock(&lun->lun_lock); 5062 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED); 5063 mtx_unlock(&lun->lun_lock); 5064 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 5065 /* Send msg to other side. */ 5066 bzero(&msg.ua, sizeof(msg.ua)); 5067 msg.hdr.msg_type = CTL_MSG_UA; 5068 msg.hdr.nexus.initid = -1; 5069 msg.hdr.nexus.targ_port = -1; 5070 msg.hdr.nexus.targ_lun = lun->lun; 5071 msg.hdr.nexus.targ_mapped_lun = lun->lun; 5072 msg.ua.ua_all = 1; 5073 msg.ua.ua_set = 1; 5074 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGED; 5075 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 5076 M_WAITOK); 5077 } 5078} 5079 5080/* 5081 * Backend "memory move is complete" callback for requests that never 5082 * make it down to say RAIDCore's configuration code. 5083 */ 5084int 5085ctl_config_move_done(union ctl_io *io) 5086{ 5087 int retval; 5088 5089 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5090 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5091 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5092 5093 if ((io->io_hdr.port_status != 0) && 5094 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5095 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5096 /* 5097 * For hardware error sense keys, the sense key 5098 * specific value is defined to be a retry count, 5099 * but we use it to pass back an internal FETD 5100 * error code. XXX KDM Hopefully the FETD is only 5101 * using 16 bits for an error code, since that's 5102 * all the space we have in the sks field. 5103 */ 5104 ctl_set_internal_failure(&io->scsiio, 5105 /*sks_valid*/ 1, 5106 /*retry_count*/ 5107 io->io_hdr.port_status); 5108 } 5109 5110 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5111 ctl_data_print(io); 5112 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5113 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5114 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5115 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5116 /* 5117 * XXX KDM just assuming a single pointer here, and not a 5118 * S/G list. If we start using S/G lists for config data, 5119 * we'll need to know how to clean them up here as well. 5120 */ 5121 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5122 free(io->scsiio.kern_data_ptr, M_CTL); 5123 ctl_done(io); 5124 retval = CTL_RETVAL_COMPLETE; 5125 } else { 5126 /* 5127 * XXX KDM now we need to continue data movement. Some 5128 * options: 5129 * - call ctl_scsiio() again? We don't do this for data 5130 * writes, because for those at least we know ahead of 5131 * time where the write will go and how long it is. For 5132 * config writes, though, that information is largely 5133 * contained within the write itself, thus we need to 5134 * parse out the data again. 5135 * 5136 * - Call some other function once the data is in? 5137 */ 5138 5139 /* 5140 * XXX KDM call ctl_scsiio() again for now, and check flag 5141 * bits to see whether we're allocated or not. 5142 */ 5143 retval = ctl_scsiio(&io->scsiio); 5144 } 5145 return (retval); 5146} 5147 5148/* 5149 * This gets called by a backend driver when it is done with a 5150 * data_submit method. 5151 */ 5152void 5153ctl_data_submit_done(union ctl_io *io) 5154{ 5155 /* 5156 * If the IO_CONT flag is set, we need to call the supplied 5157 * function to continue processing the I/O, instead of completing 5158 * the I/O just yet. 5159 * 5160 * If there is an error, though, we don't want to keep processing. 5161 * Instead, just send status back to the initiator. 5162 */ 5163 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5164 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5165 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5166 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5167 io->scsiio.io_cont(io); 5168 return; 5169 } 5170 ctl_done(io); 5171} 5172 5173/* 5174 * This gets called by a backend driver when it is done with a 5175 * configuration write. 5176 */ 5177void 5178ctl_config_write_done(union ctl_io *io) 5179{ 5180 uint8_t *buf; 5181 5182 /* 5183 * If the IO_CONT flag is set, we need to call the supplied 5184 * function to continue processing the I/O, instead of completing 5185 * the I/O just yet. 5186 * 5187 * If there is an error, though, we don't want to keep processing. 5188 * Instead, just send status back to the initiator. 5189 */ 5190 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5191 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5192 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5193 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5194 io->scsiio.io_cont(io); 5195 return; 5196 } 5197 /* 5198 * Since a configuration write can be done for commands that actually 5199 * have data allocated, like write buffer, and commands that have 5200 * no data, like start/stop unit, we need to check here. 5201 */ 5202 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5203 buf = io->scsiio.kern_data_ptr; 5204 else 5205 buf = NULL; 5206 ctl_done(io); 5207 if (buf) 5208 free(buf, M_CTL); 5209} 5210 5211void 5212ctl_config_read_done(union ctl_io *io) 5213{ 5214 uint8_t *buf; 5215 5216 /* 5217 * If there is some error -- we are done, skip data transfer. 5218 */ 5219 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5220 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5221 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5222 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5223 buf = io->scsiio.kern_data_ptr; 5224 else 5225 buf = NULL; 5226 ctl_done(io); 5227 if (buf) 5228 free(buf, M_CTL); 5229 return; 5230 } 5231 5232 /* 5233 * If the IO_CONT flag is set, we need to call the supplied 5234 * function to continue processing the I/O, instead of completing 5235 * the I/O just yet. 5236 */ 5237 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5238 io->scsiio.io_cont(io); 5239 return; 5240 } 5241 5242 ctl_datamove(io); 5243} 5244 5245/* 5246 * SCSI release command. 5247 */ 5248int 5249ctl_scsi_release(struct ctl_scsiio *ctsio) 5250{ 5251 int length, longid, thirdparty_id, resv_id; 5252 struct ctl_lun *lun; 5253 uint32_t residx; 5254 5255 length = 0; 5256 resv_id = 0; 5257 5258 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5259 5260 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5261 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5262 5263 switch (ctsio->cdb[0]) { 5264 case RELEASE_10: { 5265 struct scsi_release_10 *cdb; 5266 5267 cdb = (struct scsi_release_10 *)ctsio->cdb; 5268 5269 if (cdb->byte2 & SR10_LONGID) 5270 longid = 1; 5271 else 5272 thirdparty_id = cdb->thirdparty_id; 5273 5274 resv_id = cdb->resv_id; 5275 length = scsi_2btoul(cdb->length); 5276 break; 5277 } 5278 } 5279 5280 5281 /* 5282 * XXX KDM right now, we only support LUN reservation. We don't 5283 * support 3rd party reservations, or extent reservations, which 5284 * might actually need the parameter list. If we've gotten this 5285 * far, we've got a LUN reservation. Anything else got kicked out 5286 * above. So, according to SPC, ignore the length. 5287 */ 5288 length = 0; 5289 5290 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5291 && (length > 0)) { 5292 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5293 ctsio->kern_data_len = length; 5294 ctsio->kern_total_len = length; 5295 ctsio->kern_data_resid = 0; 5296 ctsio->kern_rel_offset = 0; 5297 ctsio->kern_sg_entries = 0; 5298 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5299 ctsio->be_move_done = ctl_config_move_done; 5300 ctl_datamove((union ctl_io *)ctsio); 5301 5302 return (CTL_RETVAL_COMPLETE); 5303 } 5304 5305 if (length > 0) 5306 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5307 5308 mtx_lock(&lun->lun_lock); 5309 5310 /* 5311 * According to SPC, it is not an error for an intiator to attempt 5312 * to release a reservation on a LUN that isn't reserved, or that 5313 * is reserved by another initiator. The reservation can only be 5314 * released, though, by the initiator who made it or by one of 5315 * several reset type events. 5316 */ 5317 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5318 lun->flags &= ~CTL_LUN_RESERVED; 5319 5320 mtx_unlock(&lun->lun_lock); 5321 5322 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5323 free(ctsio->kern_data_ptr, M_CTL); 5324 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5325 } 5326 5327 ctl_set_success(ctsio); 5328 ctl_done((union ctl_io *)ctsio); 5329 return (CTL_RETVAL_COMPLETE); 5330} 5331 5332int 5333ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5334{ 5335 int extent, thirdparty, longid; 5336 int resv_id, length; 5337 uint64_t thirdparty_id; 5338 struct ctl_lun *lun; 5339 uint32_t residx; 5340 5341 extent = 0; 5342 thirdparty = 0; 5343 longid = 0; 5344 resv_id = 0; 5345 length = 0; 5346 thirdparty_id = 0; 5347 5348 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5349 5350 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5351 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5352 5353 switch (ctsio->cdb[0]) { 5354 case RESERVE_10: { 5355 struct scsi_reserve_10 *cdb; 5356 5357 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5358 5359 if (cdb->byte2 & SR10_LONGID) 5360 longid = 1; 5361 else 5362 thirdparty_id = cdb->thirdparty_id; 5363 5364 resv_id = cdb->resv_id; 5365 length = scsi_2btoul(cdb->length); 5366 break; 5367 } 5368 } 5369 5370 /* 5371 * XXX KDM right now, we only support LUN reservation. We don't 5372 * support 3rd party reservations, or extent reservations, which 5373 * might actually need the parameter list. If we've gotten this 5374 * far, we've got a LUN reservation. Anything else got kicked out 5375 * above. So, according to SPC, ignore the length. 5376 */ 5377 length = 0; 5378 5379 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5380 && (length > 0)) { 5381 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5382 ctsio->kern_data_len = length; 5383 ctsio->kern_total_len = length; 5384 ctsio->kern_data_resid = 0; 5385 ctsio->kern_rel_offset = 0; 5386 ctsio->kern_sg_entries = 0; 5387 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5388 ctsio->be_move_done = ctl_config_move_done; 5389 ctl_datamove((union ctl_io *)ctsio); 5390 5391 return (CTL_RETVAL_COMPLETE); 5392 } 5393 5394 if (length > 0) 5395 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5396 5397 mtx_lock(&lun->lun_lock); 5398 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5399 ctl_set_reservation_conflict(ctsio); 5400 goto bailout; 5401 } 5402 5403 lun->flags |= CTL_LUN_RESERVED; 5404 lun->res_idx = residx; 5405 5406 ctl_set_success(ctsio); 5407 5408bailout: 5409 mtx_unlock(&lun->lun_lock); 5410 5411 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5412 free(ctsio->kern_data_ptr, M_CTL); 5413 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5414 } 5415 5416 ctl_done((union ctl_io *)ctsio); 5417 return (CTL_RETVAL_COMPLETE); 5418} 5419 5420int 5421ctl_start_stop(struct ctl_scsiio *ctsio) 5422{ 5423 struct scsi_start_stop_unit *cdb; 5424 struct ctl_lun *lun; 5425 int retval; 5426 5427 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5428 5429 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5430 retval = 0; 5431 5432 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5433 5434 /* 5435 * XXX KDM 5436 * We don't support the immediate bit on a stop unit. In order to 5437 * do that, we would need to code up a way to know that a stop is 5438 * pending, and hold off any new commands until it completes, one 5439 * way or another. Then we could accept or reject those commands 5440 * depending on its status. We would almost need to do the reverse 5441 * of what we do below for an immediate start -- return the copy of 5442 * the ctl_io to the FETD with status to send to the host (and to 5443 * free the copy!) and then free the original I/O once the stop 5444 * actually completes. That way, the OOA queue mechanism can work 5445 * to block commands that shouldn't proceed. Another alternative 5446 * would be to put the copy in the queue in place of the original, 5447 * and return the original back to the caller. That could be 5448 * slightly safer.. 5449 */ 5450 if ((cdb->byte2 & SSS_IMMED) 5451 && ((cdb->how & SSS_START) == 0)) { 5452 ctl_set_invalid_field(ctsio, 5453 /*sks_valid*/ 1, 5454 /*command*/ 1, 5455 /*field*/ 1, 5456 /*bit_valid*/ 1, 5457 /*bit*/ 0); 5458 ctl_done((union ctl_io *)ctsio); 5459 return (CTL_RETVAL_COMPLETE); 5460 } 5461 5462 if ((lun->flags & CTL_LUN_PR_RESERVED) 5463 && ((cdb->how & SSS_START)==0)) { 5464 uint32_t residx; 5465 5466 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5467 if (ctl_get_prkey(lun, residx) == 0 5468 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5469 5470 ctl_set_reservation_conflict(ctsio); 5471 ctl_done((union ctl_io *)ctsio); 5472 return (CTL_RETVAL_COMPLETE); 5473 } 5474 } 5475 5476 /* 5477 * If there is no backend on this device, we can't start or stop 5478 * it. In theory we shouldn't get any start/stop commands in the 5479 * first place at this level if the LUN doesn't have a backend. 5480 * That should get stopped by the command decode code. 5481 */ 5482 if (lun->backend == NULL) { 5483 ctl_set_invalid_opcode(ctsio); 5484 ctl_done((union ctl_io *)ctsio); 5485 return (CTL_RETVAL_COMPLETE); 5486 } 5487 5488 /* 5489 * XXX KDM Copan-specific offline behavior. 5490 * Figure out a reasonable way to port this? 5491 */ 5492#ifdef NEEDTOPORT 5493 mtx_lock(&lun->lun_lock); 5494 5495 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5496 && (lun->flags & CTL_LUN_OFFLINE)) { 5497 /* 5498 * If the LUN is offline, and the on/offline bit isn't set, 5499 * reject the start or stop. Otherwise, let it through. 5500 */ 5501 mtx_unlock(&lun->lun_lock); 5502 ctl_set_lun_not_ready(ctsio); 5503 ctl_done((union ctl_io *)ctsio); 5504 } else { 5505 mtx_unlock(&lun->lun_lock); 5506#endif /* NEEDTOPORT */ 5507 /* 5508 * This could be a start or a stop when we're online, 5509 * or a stop/offline or start/online. A start or stop when 5510 * we're offline is covered in the case above. 5511 */ 5512 /* 5513 * In the non-immediate case, we send the request to 5514 * the backend and return status to the user when 5515 * it is done. 5516 * 5517 * In the immediate case, we allocate a new ctl_io 5518 * to hold a copy of the request, and send that to 5519 * the backend. We then set good status on the 5520 * user's request and return it immediately. 5521 */ 5522 if (cdb->byte2 & SSS_IMMED) { 5523 union ctl_io *new_io; 5524 5525 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5526 ctl_copy_io((union ctl_io *)ctsio, new_io); 5527 retval = lun->backend->config_write(new_io); 5528 ctl_set_success(ctsio); 5529 ctl_done((union ctl_io *)ctsio); 5530 } else { 5531 retval = lun->backend->config_write( 5532 (union ctl_io *)ctsio); 5533 } 5534#ifdef NEEDTOPORT 5535 } 5536#endif 5537 return (retval); 5538} 5539 5540/* 5541 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5542 * we don't really do anything with the LBA and length fields if the user 5543 * passes them in. Instead we'll just flush out the cache for the entire 5544 * LUN. 5545 */ 5546int 5547ctl_sync_cache(struct ctl_scsiio *ctsio) 5548{ 5549 struct ctl_lun *lun; 5550 struct ctl_softc *softc; 5551 struct ctl_lba_len_flags *lbalen; 5552 uint64_t starting_lba; 5553 uint32_t block_count; 5554 int retval; 5555 uint8_t byte2; 5556 5557 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5558 5559 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5560 softc = lun->ctl_softc; 5561 retval = 0; 5562 5563 switch (ctsio->cdb[0]) { 5564 case SYNCHRONIZE_CACHE: { 5565 struct scsi_sync_cache *cdb; 5566 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5567 5568 starting_lba = scsi_4btoul(cdb->begin_lba); 5569 block_count = scsi_2btoul(cdb->lb_count); 5570 byte2 = cdb->byte2; 5571 break; 5572 } 5573 case SYNCHRONIZE_CACHE_16: { 5574 struct scsi_sync_cache_16 *cdb; 5575 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5576 5577 starting_lba = scsi_8btou64(cdb->begin_lba); 5578 block_count = scsi_4btoul(cdb->lb_count); 5579 byte2 = cdb->byte2; 5580 break; 5581 } 5582 default: 5583 ctl_set_invalid_opcode(ctsio); 5584 ctl_done((union ctl_io *)ctsio); 5585 goto bailout; 5586 break; /* NOTREACHED */ 5587 } 5588 5589 /* 5590 * We check the LBA and length, but don't do anything with them. 5591 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5592 * get flushed. This check will just help satisfy anyone who wants 5593 * to see an error for an out of range LBA. 5594 */ 5595 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5596 ctl_set_lba_out_of_range(ctsio); 5597 ctl_done((union ctl_io *)ctsio); 5598 goto bailout; 5599 } 5600 5601 /* 5602 * If this LUN has no backend, we can't flush the cache anyway. 5603 */ 5604 if (lun->backend == NULL) { 5605 ctl_set_invalid_opcode(ctsio); 5606 ctl_done((union ctl_io *)ctsio); 5607 goto bailout; 5608 } 5609 5610 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5611 lbalen->lba = starting_lba; 5612 lbalen->len = block_count; 5613 lbalen->flags = byte2; 5614 5615 /* 5616 * Check to see whether we're configured to send the SYNCHRONIZE 5617 * CACHE command directly to the back end. 5618 */ 5619 mtx_lock(&lun->lun_lock); 5620 if ((softc->flags & CTL_FLAG_REAL_SYNC) 5621 && (++(lun->sync_count) >= lun->sync_interval)) { 5622 lun->sync_count = 0; 5623 mtx_unlock(&lun->lun_lock); 5624 retval = lun->backend->config_write((union ctl_io *)ctsio); 5625 } else { 5626 mtx_unlock(&lun->lun_lock); 5627 ctl_set_success(ctsio); 5628 ctl_done((union ctl_io *)ctsio); 5629 } 5630 5631bailout: 5632 5633 return (retval); 5634} 5635 5636int 5637ctl_format(struct ctl_scsiio *ctsio) 5638{ 5639 struct scsi_format *cdb; 5640 struct ctl_lun *lun; 5641 int length, defect_list_len; 5642 5643 CTL_DEBUG_PRINT(("ctl_format\n")); 5644 5645 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5646 5647 cdb = (struct scsi_format *)ctsio->cdb; 5648 5649 length = 0; 5650 if (cdb->byte2 & SF_FMTDATA) { 5651 if (cdb->byte2 & SF_LONGLIST) 5652 length = sizeof(struct scsi_format_header_long); 5653 else 5654 length = sizeof(struct scsi_format_header_short); 5655 } 5656 5657 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5658 && (length > 0)) { 5659 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5660 ctsio->kern_data_len = length; 5661 ctsio->kern_total_len = length; 5662 ctsio->kern_data_resid = 0; 5663 ctsio->kern_rel_offset = 0; 5664 ctsio->kern_sg_entries = 0; 5665 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5666 ctsio->be_move_done = ctl_config_move_done; 5667 ctl_datamove((union ctl_io *)ctsio); 5668 5669 return (CTL_RETVAL_COMPLETE); 5670 } 5671 5672 defect_list_len = 0; 5673 5674 if (cdb->byte2 & SF_FMTDATA) { 5675 if (cdb->byte2 & SF_LONGLIST) { 5676 struct scsi_format_header_long *header; 5677 5678 header = (struct scsi_format_header_long *) 5679 ctsio->kern_data_ptr; 5680 5681 defect_list_len = scsi_4btoul(header->defect_list_len); 5682 if (defect_list_len != 0) { 5683 ctl_set_invalid_field(ctsio, 5684 /*sks_valid*/ 1, 5685 /*command*/ 0, 5686 /*field*/ 2, 5687 /*bit_valid*/ 0, 5688 /*bit*/ 0); 5689 goto bailout; 5690 } 5691 } else { 5692 struct scsi_format_header_short *header; 5693 5694 header = (struct scsi_format_header_short *) 5695 ctsio->kern_data_ptr; 5696 5697 defect_list_len = scsi_2btoul(header->defect_list_len); 5698 if (defect_list_len != 0) { 5699 ctl_set_invalid_field(ctsio, 5700 /*sks_valid*/ 1, 5701 /*command*/ 0, 5702 /*field*/ 2, 5703 /*bit_valid*/ 0, 5704 /*bit*/ 0); 5705 goto bailout; 5706 } 5707 } 5708 } 5709 5710 /* 5711 * The format command will clear out the "Medium format corrupted" 5712 * status if set by the configuration code. That status is really 5713 * just a way to notify the host that we have lost the media, and 5714 * get them to issue a command that will basically make them think 5715 * they're blowing away the media. 5716 */ 5717 mtx_lock(&lun->lun_lock); 5718 lun->flags &= ~CTL_LUN_INOPERABLE; 5719 mtx_unlock(&lun->lun_lock); 5720 5721 ctl_set_success(ctsio); 5722bailout: 5723 5724 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5725 free(ctsio->kern_data_ptr, M_CTL); 5726 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5727 } 5728 5729 ctl_done((union ctl_io *)ctsio); 5730 return (CTL_RETVAL_COMPLETE); 5731} 5732 5733int 5734ctl_read_buffer(struct ctl_scsiio *ctsio) 5735{ 5736 struct ctl_lun *lun; 5737 uint64_t buffer_offset; 5738 uint32_t len; 5739 uint8_t byte2; 5740 static uint8_t descr[4]; 5741 static uint8_t echo_descr[4] = { 0 }; 5742 5743 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5744 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5745 switch (ctsio->cdb[0]) { 5746 case READ_BUFFER: { 5747 struct scsi_read_buffer *cdb; 5748 5749 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5750 buffer_offset = scsi_3btoul(cdb->offset); 5751 len = scsi_3btoul(cdb->length); 5752 byte2 = cdb->byte2; 5753 break; 5754 } 5755 case READ_BUFFER_16: { 5756 struct scsi_read_buffer_16 *cdb; 5757 5758 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5759 buffer_offset = scsi_8btou64(cdb->offset); 5760 len = scsi_4btoul(cdb->length); 5761 byte2 = cdb->byte2; 5762 break; 5763 } 5764 default: /* This shouldn't happen. */ 5765 ctl_set_invalid_opcode(ctsio); 5766 ctl_done((union ctl_io *)ctsio); 5767 return (CTL_RETVAL_COMPLETE); 5768 } 5769 5770 if ((byte2 & RWB_MODE) != RWB_MODE_DATA && 5771 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5772 (byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5773 ctl_set_invalid_field(ctsio, 5774 /*sks_valid*/ 1, 5775 /*command*/ 1, 5776 /*field*/ 1, 5777 /*bit_valid*/ 1, 5778 /*bit*/ 4); 5779 ctl_done((union ctl_io *)ctsio); 5780 return (CTL_RETVAL_COMPLETE); 5781 } 5782 5783 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5784 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5785 ctl_set_invalid_field(ctsio, 5786 /*sks_valid*/ 1, 5787 /*command*/ 1, 5788 /*field*/ 6, 5789 /*bit_valid*/ 0, 5790 /*bit*/ 0); 5791 ctl_done((union ctl_io *)ctsio); 5792 return (CTL_RETVAL_COMPLETE); 5793 } 5794 5795 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5796 descr[0] = 0; 5797 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5798 ctsio->kern_data_ptr = descr; 5799 len = min(len, sizeof(descr)); 5800 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5801 ctsio->kern_data_ptr = echo_descr; 5802 len = min(len, sizeof(echo_descr)); 5803 } else { 5804 if (lun->write_buffer == NULL) { 5805 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5806 M_CTL, M_WAITOK); 5807 } 5808 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5809 } 5810 ctsio->kern_data_len = len; 5811 ctsio->kern_total_len = len; 5812 ctsio->kern_data_resid = 0; 5813 ctsio->kern_rel_offset = 0; 5814 ctsio->kern_sg_entries = 0; 5815 ctl_set_success(ctsio); 5816 ctsio->be_move_done = ctl_config_move_done; 5817 ctl_datamove((union ctl_io *)ctsio); 5818 return (CTL_RETVAL_COMPLETE); 5819} 5820 5821int 5822ctl_write_buffer(struct ctl_scsiio *ctsio) 5823{ 5824 struct scsi_write_buffer *cdb; 5825 struct ctl_lun *lun; 5826 int buffer_offset, len; 5827 5828 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5829 5830 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5831 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5832 5833 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5834 ctl_set_invalid_field(ctsio, 5835 /*sks_valid*/ 1, 5836 /*command*/ 1, 5837 /*field*/ 1, 5838 /*bit_valid*/ 1, 5839 /*bit*/ 4); 5840 ctl_done((union ctl_io *)ctsio); 5841 return (CTL_RETVAL_COMPLETE); 5842 } 5843 5844 len = scsi_3btoul(cdb->length); 5845 buffer_offset = scsi_3btoul(cdb->offset); 5846 5847 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5848 ctl_set_invalid_field(ctsio, 5849 /*sks_valid*/ 1, 5850 /*command*/ 1, 5851 /*field*/ 6, 5852 /*bit_valid*/ 0, 5853 /*bit*/ 0); 5854 ctl_done((union ctl_io *)ctsio); 5855 return (CTL_RETVAL_COMPLETE); 5856 } 5857 5858 /* 5859 * If we've got a kernel request that hasn't been malloced yet, 5860 * malloc it and tell the caller the data buffer is here. 5861 */ 5862 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5863 if (lun->write_buffer == NULL) { 5864 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5865 M_CTL, M_WAITOK); 5866 } 5867 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5868 ctsio->kern_data_len = len; 5869 ctsio->kern_total_len = len; 5870 ctsio->kern_data_resid = 0; 5871 ctsio->kern_rel_offset = 0; 5872 ctsio->kern_sg_entries = 0; 5873 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5874 ctsio->be_move_done = ctl_config_move_done; 5875 ctl_datamove((union ctl_io *)ctsio); 5876 5877 return (CTL_RETVAL_COMPLETE); 5878 } 5879 5880 ctl_set_success(ctsio); 5881 ctl_done((union ctl_io *)ctsio); 5882 return (CTL_RETVAL_COMPLETE); 5883} 5884 5885int 5886ctl_write_same(struct ctl_scsiio *ctsio) 5887{ 5888 struct ctl_lun *lun; 5889 struct ctl_lba_len_flags *lbalen; 5890 uint64_t lba; 5891 uint32_t num_blocks; 5892 int len, retval; 5893 uint8_t byte2; 5894 5895 retval = CTL_RETVAL_COMPLETE; 5896 5897 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5898 5899 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5900 5901 switch (ctsio->cdb[0]) { 5902 case WRITE_SAME_10: { 5903 struct scsi_write_same_10 *cdb; 5904 5905 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5906 5907 lba = scsi_4btoul(cdb->addr); 5908 num_blocks = scsi_2btoul(cdb->length); 5909 byte2 = cdb->byte2; 5910 break; 5911 } 5912 case WRITE_SAME_16: { 5913 struct scsi_write_same_16 *cdb; 5914 5915 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5916 5917 lba = scsi_8btou64(cdb->addr); 5918 num_blocks = scsi_4btoul(cdb->length); 5919 byte2 = cdb->byte2; 5920 break; 5921 } 5922 default: 5923 /* 5924 * We got a command we don't support. This shouldn't 5925 * happen, commands should be filtered out above us. 5926 */ 5927 ctl_set_invalid_opcode(ctsio); 5928 ctl_done((union ctl_io *)ctsio); 5929 5930 return (CTL_RETVAL_COMPLETE); 5931 break; /* NOTREACHED */ 5932 } 5933 5934 /* ANCHOR flag can be used only together with UNMAP */ 5935 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5936 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5937 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5938 ctl_done((union ctl_io *)ctsio); 5939 return (CTL_RETVAL_COMPLETE); 5940 } 5941 5942 /* 5943 * The first check is to make sure we're in bounds, the second 5944 * check is to catch wrap-around problems. If the lba + num blocks 5945 * is less than the lba, then we've wrapped around and the block 5946 * range is invalid anyway. 5947 */ 5948 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5949 || ((lba + num_blocks) < lba)) { 5950 ctl_set_lba_out_of_range(ctsio); 5951 ctl_done((union ctl_io *)ctsio); 5952 return (CTL_RETVAL_COMPLETE); 5953 } 5954 5955 /* Zero number of blocks means "to the last logical block" */ 5956 if (num_blocks == 0) { 5957 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5958 ctl_set_invalid_field(ctsio, 5959 /*sks_valid*/ 0, 5960 /*command*/ 1, 5961 /*field*/ 0, 5962 /*bit_valid*/ 0, 5963 /*bit*/ 0); 5964 ctl_done((union ctl_io *)ctsio); 5965 return (CTL_RETVAL_COMPLETE); 5966 } 5967 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5968 } 5969 5970 len = lun->be_lun->blocksize; 5971 5972 /* 5973 * If we've got a kernel request that hasn't been malloced yet, 5974 * malloc it and tell the caller the data buffer is here. 5975 */ 5976 if ((byte2 & SWS_NDOB) == 0 && 5977 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5978 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5979 ctsio->kern_data_len = len; 5980 ctsio->kern_total_len = len; 5981 ctsio->kern_data_resid = 0; 5982 ctsio->kern_rel_offset = 0; 5983 ctsio->kern_sg_entries = 0; 5984 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5985 ctsio->be_move_done = ctl_config_move_done; 5986 ctl_datamove((union ctl_io *)ctsio); 5987 5988 return (CTL_RETVAL_COMPLETE); 5989 } 5990 5991 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5992 lbalen->lba = lba; 5993 lbalen->len = num_blocks; 5994 lbalen->flags = byte2; 5995 retval = lun->backend->config_write((union ctl_io *)ctsio); 5996 5997 return (retval); 5998} 5999 6000int 6001ctl_unmap(struct ctl_scsiio *ctsio) 6002{ 6003 struct ctl_lun *lun; 6004 struct scsi_unmap *cdb; 6005 struct ctl_ptr_len_flags *ptrlen; 6006 struct scsi_unmap_header *hdr; 6007 struct scsi_unmap_desc *buf, *end, *endnz, *range; 6008 uint64_t lba; 6009 uint32_t num_blocks; 6010 int len, retval; 6011 uint8_t byte2; 6012 6013 retval = CTL_RETVAL_COMPLETE; 6014 6015 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6016 6017 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6018 cdb = (struct scsi_unmap *)ctsio->cdb; 6019 6020 len = scsi_2btoul(cdb->length); 6021 byte2 = cdb->byte2; 6022 6023 /* 6024 * If we've got a kernel request that hasn't been malloced yet, 6025 * malloc it and tell the caller the data buffer is here. 6026 */ 6027 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6028 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6029 ctsio->kern_data_len = len; 6030 ctsio->kern_total_len = len; 6031 ctsio->kern_data_resid = 0; 6032 ctsio->kern_rel_offset = 0; 6033 ctsio->kern_sg_entries = 0; 6034 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6035 ctsio->be_move_done = ctl_config_move_done; 6036 ctl_datamove((union ctl_io *)ctsio); 6037 6038 return (CTL_RETVAL_COMPLETE); 6039 } 6040 6041 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6042 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6043 if (len < sizeof (*hdr) || 6044 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6045 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6046 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6047 ctl_set_invalid_field(ctsio, 6048 /*sks_valid*/ 0, 6049 /*command*/ 0, 6050 /*field*/ 0, 6051 /*bit_valid*/ 0, 6052 /*bit*/ 0); 6053 goto done; 6054 } 6055 len = scsi_2btoul(hdr->desc_length); 6056 buf = (struct scsi_unmap_desc *)(hdr + 1); 6057 end = buf + len / sizeof(*buf); 6058 6059 endnz = buf; 6060 for (range = buf; range < end; range++) { 6061 lba = scsi_8btou64(range->lba); 6062 num_blocks = scsi_4btoul(range->length); 6063 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6064 || ((lba + num_blocks) < lba)) { 6065 ctl_set_lba_out_of_range(ctsio); 6066 ctl_done((union ctl_io *)ctsio); 6067 return (CTL_RETVAL_COMPLETE); 6068 } 6069 if (num_blocks != 0) 6070 endnz = range + 1; 6071 } 6072 6073 /* 6074 * Block backend can not handle zero last range. 6075 * Filter it out and return if there is nothing left. 6076 */ 6077 len = (uint8_t *)endnz - (uint8_t *)buf; 6078 if (len == 0) { 6079 ctl_set_success(ctsio); 6080 goto done; 6081 } 6082 6083 mtx_lock(&lun->lun_lock); 6084 ptrlen = (struct ctl_ptr_len_flags *) 6085 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6086 ptrlen->ptr = (void *)buf; 6087 ptrlen->len = len; 6088 ptrlen->flags = byte2; 6089 ctl_check_blocked(lun); 6090 mtx_unlock(&lun->lun_lock); 6091 6092 retval = lun->backend->config_write((union ctl_io *)ctsio); 6093 return (retval); 6094 6095done: 6096 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 6097 free(ctsio->kern_data_ptr, M_CTL); 6098 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 6099 } 6100 ctl_done((union ctl_io *)ctsio); 6101 return (CTL_RETVAL_COMPLETE); 6102} 6103 6104/* 6105 * Note that this function currently doesn't actually do anything inside 6106 * CTL to enforce things if the DQue bit is turned on. 6107 * 6108 * Also note that this function can't be used in the default case, because 6109 * the DQue bit isn't set in the changeable mask for the control mode page 6110 * anyway. This is just here as an example for how to implement a page 6111 * handler, and a placeholder in case we want to allow the user to turn 6112 * tagged queueing on and off. 6113 * 6114 * The D_SENSE bit handling is functional, however, and will turn 6115 * descriptor sense on and off for a given LUN. 6116 */ 6117int 6118ctl_control_page_handler(struct ctl_scsiio *ctsio, 6119 struct ctl_page_index *page_index, uint8_t *page_ptr) 6120{ 6121 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6122 struct ctl_lun *lun; 6123 int set_ua; 6124 uint32_t initidx; 6125 6126 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6127 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6128 set_ua = 0; 6129 6130 user_cp = (struct scsi_control_page *)page_ptr; 6131 current_cp = (struct scsi_control_page *) 6132 (page_index->page_data + (page_index->page_len * 6133 CTL_PAGE_CURRENT)); 6134 saved_cp = (struct scsi_control_page *) 6135 (page_index->page_data + (page_index->page_len * 6136 CTL_PAGE_SAVED)); 6137 6138 mtx_lock(&lun->lun_lock); 6139 if (((current_cp->rlec & SCP_DSENSE) == 0) 6140 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6141 /* 6142 * Descriptor sense is currently turned off and the user 6143 * wants to turn it on. 6144 */ 6145 current_cp->rlec |= SCP_DSENSE; 6146 saved_cp->rlec |= SCP_DSENSE; 6147 lun->flags |= CTL_LUN_SENSE_DESC; 6148 set_ua = 1; 6149 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6150 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6151 /* 6152 * Descriptor sense is currently turned on, and the user 6153 * wants to turn it off. 6154 */ 6155 current_cp->rlec &= ~SCP_DSENSE; 6156 saved_cp->rlec &= ~SCP_DSENSE; 6157 lun->flags &= ~CTL_LUN_SENSE_DESC; 6158 set_ua = 1; 6159 } 6160 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6161 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6162 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6163 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6164 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6165 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6166 set_ua = 1; 6167 } 6168 if ((current_cp->eca_and_aen & SCP_SWP) != 6169 (user_cp->eca_and_aen & SCP_SWP)) { 6170 current_cp->eca_and_aen &= ~SCP_SWP; 6171 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6172 saved_cp->eca_and_aen &= ~SCP_SWP; 6173 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6174 set_ua = 1; 6175 } 6176 if (set_ua != 0) 6177 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6178 mtx_unlock(&lun->lun_lock); 6179 if (set_ua) { 6180 ctl_isc_announce_mode(lun, 6181 ctl_get_initindex(&ctsio->io_hdr.nexus), 6182 page_index->page_code, page_index->subpage); 6183 } 6184 return (0); 6185} 6186 6187int 6188ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6189 struct ctl_page_index *page_index, uint8_t *page_ptr) 6190{ 6191 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6192 struct ctl_lun *lun; 6193 int set_ua; 6194 uint32_t initidx; 6195 6196 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6197 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6198 set_ua = 0; 6199 6200 user_cp = (struct scsi_caching_page *)page_ptr; 6201 current_cp = (struct scsi_caching_page *) 6202 (page_index->page_data + (page_index->page_len * 6203 CTL_PAGE_CURRENT)); 6204 saved_cp = (struct scsi_caching_page *) 6205 (page_index->page_data + (page_index->page_len * 6206 CTL_PAGE_SAVED)); 6207 6208 mtx_lock(&lun->lun_lock); 6209 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6210 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6211 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6212 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6213 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6214 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6215 set_ua = 1; 6216 } 6217 if (set_ua != 0) 6218 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6219 mtx_unlock(&lun->lun_lock); 6220 if (set_ua) { 6221 ctl_isc_announce_mode(lun, 6222 ctl_get_initindex(&ctsio->io_hdr.nexus), 6223 page_index->page_code, page_index->subpage); 6224 } 6225 return (0); 6226} 6227 6228int 6229ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6230 struct ctl_page_index *page_index, 6231 uint8_t *page_ptr) 6232{ 6233 uint8_t *c; 6234 int i; 6235 6236 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6237 ctl_time_io_secs = 6238 (c[0] << 8) | 6239 (c[1] << 0) | 6240 0; 6241 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6242 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6243 printf("page data:"); 6244 for (i=0; i<8; i++) 6245 printf(" %.2x",page_ptr[i]); 6246 printf("\n"); 6247 return (0); 6248} 6249 6250int 6251ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6252 struct ctl_page_index *page_index, 6253 int pc) 6254{ 6255 struct copan_debugconf_subpage *page; 6256 6257 page = (struct copan_debugconf_subpage *)page_index->page_data + 6258 (page_index->page_len * pc); 6259 6260 switch (pc) { 6261 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6262 case SMS_PAGE_CTRL_DEFAULT >> 6: 6263 case SMS_PAGE_CTRL_SAVED >> 6: 6264 /* 6265 * We don't update the changable or default bits for this page. 6266 */ 6267 break; 6268 case SMS_PAGE_CTRL_CURRENT >> 6: 6269 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6270 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6271 break; 6272 default: 6273#ifdef NEEDTOPORT 6274 EPRINT(0, "Invalid PC %d!!", pc); 6275#endif /* NEEDTOPORT */ 6276 break; 6277 } 6278 return (0); 6279} 6280 6281 6282static int 6283ctl_do_mode_select(union ctl_io *io) 6284{ 6285 struct scsi_mode_page_header *page_header; 6286 struct ctl_page_index *page_index; 6287 struct ctl_scsiio *ctsio; 6288 int control_dev, page_len; 6289 int page_len_offset, page_len_size; 6290 union ctl_modepage_info *modepage_info; 6291 struct ctl_lun *lun; 6292 int *len_left, *len_used; 6293 int retval, i; 6294 6295 ctsio = &io->scsiio; 6296 page_index = NULL; 6297 page_len = 0; 6298 retval = CTL_RETVAL_COMPLETE; 6299 6300 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6301 6302 if (lun->be_lun->lun_type != T_DIRECT) 6303 control_dev = 1; 6304 else 6305 control_dev = 0; 6306 6307 modepage_info = (union ctl_modepage_info *) 6308 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6309 len_left = &modepage_info->header.len_left; 6310 len_used = &modepage_info->header.len_used; 6311 6312do_next_page: 6313 6314 page_header = (struct scsi_mode_page_header *) 6315 (ctsio->kern_data_ptr + *len_used); 6316 6317 if (*len_left == 0) { 6318 free(ctsio->kern_data_ptr, M_CTL); 6319 ctl_set_success(ctsio); 6320 ctl_done((union ctl_io *)ctsio); 6321 return (CTL_RETVAL_COMPLETE); 6322 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6323 6324 free(ctsio->kern_data_ptr, M_CTL); 6325 ctl_set_param_len_error(ctsio); 6326 ctl_done((union ctl_io *)ctsio); 6327 return (CTL_RETVAL_COMPLETE); 6328 6329 } else if ((page_header->page_code & SMPH_SPF) 6330 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6331 6332 free(ctsio->kern_data_ptr, M_CTL); 6333 ctl_set_param_len_error(ctsio); 6334 ctl_done((union ctl_io *)ctsio); 6335 return (CTL_RETVAL_COMPLETE); 6336 } 6337 6338 6339 /* 6340 * XXX KDM should we do something with the block descriptor? 6341 */ 6342 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6343 6344 if ((control_dev != 0) 6345 && (lun->mode_pages.index[i].page_flags & 6346 CTL_PAGE_FLAG_DISK_ONLY)) 6347 continue; 6348 6349 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6350 (page_header->page_code & SMPH_PC_MASK)) 6351 continue; 6352 6353 /* 6354 * If neither page has a subpage code, then we've got a 6355 * match. 6356 */ 6357 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6358 && ((page_header->page_code & SMPH_SPF) == 0)) { 6359 page_index = &lun->mode_pages.index[i]; 6360 page_len = page_header->page_length; 6361 break; 6362 } 6363 6364 /* 6365 * If both pages have subpages, then the subpage numbers 6366 * have to match. 6367 */ 6368 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6369 && (page_header->page_code & SMPH_SPF)) { 6370 struct scsi_mode_page_header_sp *sph; 6371 6372 sph = (struct scsi_mode_page_header_sp *)page_header; 6373 6374 if (lun->mode_pages.index[i].subpage == 6375 sph->subpage) { 6376 page_index = &lun->mode_pages.index[i]; 6377 page_len = scsi_2btoul(sph->page_length); 6378 break; 6379 } 6380 } 6381 } 6382 6383 /* 6384 * If we couldn't find the page, or if we don't have a mode select 6385 * handler for it, send back an error to the user. 6386 */ 6387 if ((page_index == NULL) 6388 || (page_index->select_handler == NULL)) { 6389 ctl_set_invalid_field(ctsio, 6390 /*sks_valid*/ 1, 6391 /*command*/ 0, 6392 /*field*/ *len_used, 6393 /*bit_valid*/ 0, 6394 /*bit*/ 0); 6395 free(ctsio->kern_data_ptr, M_CTL); 6396 ctl_done((union ctl_io *)ctsio); 6397 return (CTL_RETVAL_COMPLETE); 6398 } 6399 6400 if (page_index->page_code & SMPH_SPF) { 6401 page_len_offset = 2; 6402 page_len_size = 2; 6403 } else { 6404 page_len_size = 1; 6405 page_len_offset = 1; 6406 } 6407 6408 /* 6409 * If the length the initiator gives us isn't the one we specify in 6410 * the mode page header, or if they didn't specify enough data in 6411 * the CDB to avoid truncating this page, kick out the request. 6412 */ 6413 if ((page_len != (page_index->page_len - page_len_offset - 6414 page_len_size)) 6415 || (*len_left < page_index->page_len)) { 6416 6417 6418 ctl_set_invalid_field(ctsio, 6419 /*sks_valid*/ 1, 6420 /*command*/ 0, 6421 /*field*/ *len_used + page_len_offset, 6422 /*bit_valid*/ 0, 6423 /*bit*/ 0); 6424 free(ctsio->kern_data_ptr, M_CTL); 6425 ctl_done((union ctl_io *)ctsio); 6426 return (CTL_RETVAL_COMPLETE); 6427 } 6428 6429 /* 6430 * Run through the mode page, checking to make sure that the bits 6431 * the user changed are actually legal for him to change. 6432 */ 6433 for (i = 0; i < page_index->page_len; i++) { 6434 uint8_t *user_byte, *change_mask, *current_byte; 6435 int bad_bit; 6436 int j; 6437 6438 user_byte = (uint8_t *)page_header + i; 6439 change_mask = page_index->page_data + 6440 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6441 current_byte = page_index->page_data + 6442 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6443 6444 /* 6445 * Check to see whether the user set any bits in this byte 6446 * that he is not allowed to set. 6447 */ 6448 if ((*user_byte & ~(*change_mask)) == 6449 (*current_byte & ~(*change_mask))) 6450 continue; 6451 6452 /* 6453 * Go through bit by bit to determine which one is illegal. 6454 */ 6455 bad_bit = 0; 6456 for (j = 7; j >= 0; j--) { 6457 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6458 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6459 bad_bit = i; 6460 break; 6461 } 6462 } 6463 ctl_set_invalid_field(ctsio, 6464 /*sks_valid*/ 1, 6465 /*command*/ 0, 6466 /*field*/ *len_used + i, 6467 /*bit_valid*/ 1, 6468 /*bit*/ bad_bit); 6469 free(ctsio->kern_data_ptr, M_CTL); 6470 ctl_done((union ctl_io *)ctsio); 6471 return (CTL_RETVAL_COMPLETE); 6472 } 6473 6474 /* 6475 * Decrement these before we call the page handler, since we may 6476 * end up getting called back one way or another before the handler 6477 * returns to this context. 6478 */ 6479 *len_left -= page_index->page_len; 6480 *len_used += page_index->page_len; 6481 6482 retval = page_index->select_handler(ctsio, page_index, 6483 (uint8_t *)page_header); 6484 6485 /* 6486 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6487 * wait until this queued command completes to finish processing 6488 * the mode page. If it returns anything other than 6489 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6490 * already set the sense information, freed the data pointer, and 6491 * completed the io for us. 6492 */ 6493 if (retval != CTL_RETVAL_COMPLETE) 6494 goto bailout_no_done; 6495 6496 /* 6497 * If the initiator sent us more than one page, parse the next one. 6498 */ 6499 if (*len_left > 0) 6500 goto do_next_page; 6501 6502 ctl_set_success(ctsio); 6503 free(ctsio->kern_data_ptr, M_CTL); 6504 ctl_done((union ctl_io *)ctsio); 6505 6506bailout_no_done: 6507 6508 return (CTL_RETVAL_COMPLETE); 6509 6510} 6511 6512int 6513ctl_mode_select(struct ctl_scsiio *ctsio) 6514{ 6515 int param_len, pf, sp; 6516 int header_size, bd_len; 6517 int len_left, len_used; 6518 struct ctl_page_index *page_index; 6519 struct ctl_lun *lun; 6520 int control_dev, page_len; 6521 union ctl_modepage_info *modepage_info; 6522 int retval; 6523 6524 pf = 0; 6525 sp = 0; 6526 page_len = 0; 6527 len_used = 0; 6528 len_left = 0; 6529 retval = 0; 6530 bd_len = 0; 6531 page_index = NULL; 6532 6533 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6534 6535 if (lun->be_lun->lun_type != T_DIRECT) 6536 control_dev = 1; 6537 else 6538 control_dev = 0; 6539 6540 switch (ctsio->cdb[0]) { 6541 case MODE_SELECT_6: { 6542 struct scsi_mode_select_6 *cdb; 6543 6544 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6545 6546 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6547 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6548 6549 param_len = cdb->length; 6550 header_size = sizeof(struct scsi_mode_header_6); 6551 break; 6552 } 6553 case MODE_SELECT_10: { 6554 struct scsi_mode_select_10 *cdb; 6555 6556 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6557 6558 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6559 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6560 6561 param_len = scsi_2btoul(cdb->length); 6562 header_size = sizeof(struct scsi_mode_header_10); 6563 break; 6564 } 6565 default: 6566 ctl_set_invalid_opcode(ctsio); 6567 ctl_done((union ctl_io *)ctsio); 6568 return (CTL_RETVAL_COMPLETE); 6569 break; /* NOTREACHED */ 6570 } 6571 6572 /* 6573 * From SPC-3: 6574 * "A parameter list length of zero indicates that the Data-Out Buffer 6575 * shall be empty. This condition shall not be considered as an error." 6576 */ 6577 if (param_len == 0) { 6578 ctl_set_success(ctsio); 6579 ctl_done((union ctl_io *)ctsio); 6580 return (CTL_RETVAL_COMPLETE); 6581 } 6582 6583 /* 6584 * Since we'll hit this the first time through, prior to 6585 * allocation, we don't need to free a data buffer here. 6586 */ 6587 if (param_len < header_size) { 6588 ctl_set_param_len_error(ctsio); 6589 ctl_done((union ctl_io *)ctsio); 6590 return (CTL_RETVAL_COMPLETE); 6591 } 6592 6593 /* 6594 * Allocate the data buffer and grab the user's data. In theory, 6595 * we shouldn't have to sanity check the parameter list length here 6596 * because the maximum size is 64K. We should be able to malloc 6597 * that much without too many problems. 6598 */ 6599 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6600 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6601 ctsio->kern_data_len = param_len; 6602 ctsio->kern_total_len = param_len; 6603 ctsio->kern_data_resid = 0; 6604 ctsio->kern_rel_offset = 0; 6605 ctsio->kern_sg_entries = 0; 6606 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6607 ctsio->be_move_done = ctl_config_move_done; 6608 ctl_datamove((union ctl_io *)ctsio); 6609 6610 return (CTL_RETVAL_COMPLETE); 6611 } 6612 6613 switch (ctsio->cdb[0]) { 6614 case MODE_SELECT_6: { 6615 struct scsi_mode_header_6 *mh6; 6616 6617 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6618 bd_len = mh6->blk_desc_len; 6619 break; 6620 } 6621 case MODE_SELECT_10: { 6622 struct scsi_mode_header_10 *mh10; 6623 6624 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6625 bd_len = scsi_2btoul(mh10->blk_desc_len); 6626 break; 6627 } 6628 default: 6629 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6630 break; 6631 } 6632 6633 if (param_len < (header_size + bd_len)) { 6634 free(ctsio->kern_data_ptr, M_CTL); 6635 ctl_set_param_len_error(ctsio); 6636 ctl_done((union ctl_io *)ctsio); 6637 return (CTL_RETVAL_COMPLETE); 6638 } 6639 6640 /* 6641 * Set the IO_CONT flag, so that if this I/O gets passed to 6642 * ctl_config_write_done(), it'll get passed back to 6643 * ctl_do_mode_select() for further processing, or completion if 6644 * we're all done. 6645 */ 6646 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6647 ctsio->io_cont = ctl_do_mode_select; 6648 6649 modepage_info = (union ctl_modepage_info *) 6650 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6651 6652 memset(modepage_info, 0, sizeof(*modepage_info)); 6653 6654 len_left = param_len - header_size - bd_len; 6655 len_used = header_size + bd_len; 6656 6657 modepage_info->header.len_left = len_left; 6658 modepage_info->header.len_used = len_used; 6659 6660 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6661} 6662 6663int 6664ctl_mode_sense(struct ctl_scsiio *ctsio) 6665{ 6666 struct ctl_lun *lun; 6667 int pc, page_code, dbd, llba, subpage; 6668 int alloc_len, page_len, header_len, total_len; 6669 struct scsi_mode_block_descr *block_desc; 6670 struct ctl_page_index *page_index; 6671 int control_dev; 6672 6673 dbd = 0; 6674 llba = 0; 6675 block_desc = NULL; 6676 page_index = NULL; 6677 6678 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6679 6680 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6681 6682 if (lun->be_lun->lun_type != T_DIRECT) 6683 control_dev = 1; 6684 else 6685 control_dev = 0; 6686 6687 switch (ctsio->cdb[0]) { 6688 case MODE_SENSE_6: { 6689 struct scsi_mode_sense_6 *cdb; 6690 6691 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6692 6693 header_len = sizeof(struct scsi_mode_hdr_6); 6694 if (cdb->byte2 & SMS_DBD) 6695 dbd = 1; 6696 else 6697 header_len += sizeof(struct scsi_mode_block_descr); 6698 6699 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6700 page_code = cdb->page & SMS_PAGE_CODE; 6701 subpage = cdb->subpage; 6702 alloc_len = cdb->length; 6703 break; 6704 } 6705 case MODE_SENSE_10: { 6706 struct scsi_mode_sense_10 *cdb; 6707 6708 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6709 6710 header_len = sizeof(struct scsi_mode_hdr_10); 6711 6712 if (cdb->byte2 & SMS_DBD) 6713 dbd = 1; 6714 else 6715 header_len += sizeof(struct scsi_mode_block_descr); 6716 if (cdb->byte2 & SMS10_LLBAA) 6717 llba = 1; 6718 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6719 page_code = cdb->page & SMS_PAGE_CODE; 6720 subpage = cdb->subpage; 6721 alloc_len = scsi_2btoul(cdb->length); 6722 break; 6723 } 6724 default: 6725 ctl_set_invalid_opcode(ctsio); 6726 ctl_done((union ctl_io *)ctsio); 6727 return (CTL_RETVAL_COMPLETE); 6728 break; /* NOTREACHED */ 6729 } 6730 6731 /* 6732 * We have to make a first pass through to calculate the size of 6733 * the pages that match the user's query. Then we allocate enough 6734 * memory to hold it, and actually copy the data into the buffer. 6735 */ 6736 switch (page_code) { 6737 case SMS_ALL_PAGES_PAGE: { 6738 int i; 6739 6740 page_len = 0; 6741 6742 /* 6743 * At the moment, values other than 0 and 0xff here are 6744 * reserved according to SPC-3. 6745 */ 6746 if ((subpage != SMS_SUBPAGE_PAGE_0) 6747 && (subpage != SMS_SUBPAGE_ALL)) { 6748 ctl_set_invalid_field(ctsio, 6749 /*sks_valid*/ 1, 6750 /*command*/ 1, 6751 /*field*/ 3, 6752 /*bit_valid*/ 0, 6753 /*bit*/ 0); 6754 ctl_done((union ctl_io *)ctsio); 6755 return (CTL_RETVAL_COMPLETE); 6756 } 6757 6758 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6759 if ((control_dev != 0) 6760 && (lun->mode_pages.index[i].page_flags & 6761 CTL_PAGE_FLAG_DISK_ONLY)) 6762 continue; 6763 6764 /* 6765 * We don't use this subpage if the user didn't 6766 * request all subpages. 6767 */ 6768 if ((lun->mode_pages.index[i].subpage != 0) 6769 && (subpage == SMS_SUBPAGE_PAGE_0)) 6770 continue; 6771 6772#if 0 6773 printf("found page %#x len %d\n", 6774 lun->mode_pages.index[i].page_code & 6775 SMPH_PC_MASK, 6776 lun->mode_pages.index[i].page_len); 6777#endif 6778 page_len += lun->mode_pages.index[i].page_len; 6779 } 6780 break; 6781 } 6782 default: { 6783 int i; 6784 6785 page_len = 0; 6786 6787 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6788 /* Look for the right page code */ 6789 if ((lun->mode_pages.index[i].page_code & 6790 SMPH_PC_MASK) != page_code) 6791 continue; 6792 6793 /* Look for the right subpage or the subpage wildcard*/ 6794 if ((lun->mode_pages.index[i].subpage != subpage) 6795 && (subpage != SMS_SUBPAGE_ALL)) 6796 continue; 6797 6798 /* Make sure the page is supported for this dev type */ 6799 if ((control_dev != 0) 6800 && (lun->mode_pages.index[i].page_flags & 6801 CTL_PAGE_FLAG_DISK_ONLY)) 6802 continue; 6803 6804#if 0 6805 printf("found page %#x len %d\n", 6806 lun->mode_pages.index[i].page_code & 6807 SMPH_PC_MASK, 6808 lun->mode_pages.index[i].page_len); 6809#endif 6810 6811 page_len += lun->mode_pages.index[i].page_len; 6812 } 6813 6814 if (page_len == 0) { 6815 ctl_set_invalid_field(ctsio, 6816 /*sks_valid*/ 1, 6817 /*command*/ 1, 6818 /*field*/ 2, 6819 /*bit_valid*/ 1, 6820 /*bit*/ 5); 6821 ctl_done((union ctl_io *)ctsio); 6822 return (CTL_RETVAL_COMPLETE); 6823 } 6824 break; 6825 } 6826 } 6827 6828 total_len = header_len + page_len; 6829#if 0 6830 printf("header_len = %d, page_len = %d, total_len = %d\n", 6831 header_len, page_len, total_len); 6832#endif 6833 6834 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6835 ctsio->kern_sg_entries = 0; 6836 ctsio->kern_data_resid = 0; 6837 ctsio->kern_rel_offset = 0; 6838 if (total_len < alloc_len) { 6839 ctsio->residual = alloc_len - total_len; 6840 ctsio->kern_data_len = total_len; 6841 ctsio->kern_total_len = total_len; 6842 } else { 6843 ctsio->residual = 0; 6844 ctsio->kern_data_len = alloc_len; 6845 ctsio->kern_total_len = alloc_len; 6846 } 6847 6848 switch (ctsio->cdb[0]) { 6849 case MODE_SENSE_6: { 6850 struct scsi_mode_hdr_6 *header; 6851 6852 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6853 6854 header->datalen = MIN(total_len - 1, 254); 6855 if (control_dev == 0) { 6856 header->dev_specific = 0x10; /* DPOFUA */ 6857 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6858 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6859 .eca_and_aen & SCP_SWP) != 0) 6860 header->dev_specific |= 0x80; /* WP */ 6861 } 6862 if (dbd) 6863 header->block_descr_len = 0; 6864 else 6865 header->block_descr_len = 6866 sizeof(struct scsi_mode_block_descr); 6867 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6868 break; 6869 } 6870 case MODE_SENSE_10: { 6871 struct scsi_mode_hdr_10 *header; 6872 int datalen; 6873 6874 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6875 6876 datalen = MIN(total_len - 2, 65533); 6877 scsi_ulto2b(datalen, header->datalen); 6878 if (control_dev == 0) { 6879 header->dev_specific = 0x10; /* DPOFUA */ 6880 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6881 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6882 .eca_and_aen & SCP_SWP) != 0) 6883 header->dev_specific |= 0x80; /* WP */ 6884 } 6885 if (dbd) 6886 scsi_ulto2b(0, header->block_descr_len); 6887 else 6888 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6889 header->block_descr_len); 6890 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6891 break; 6892 } 6893 default: 6894 panic("invalid CDB type %#x", ctsio->cdb[0]); 6895 break; /* NOTREACHED */ 6896 } 6897 6898 /* 6899 * If we've got a disk, use its blocksize in the block 6900 * descriptor. Otherwise, just set it to 0. 6901 */ 6902 if (dbd == 0) { 6903 if (control_dev == 0) 6904 scsi_ulto3b(lun->be_lun->blocksize, 6905 block_desc->block_len); 6906 else 6907 scsi_ulto3b(0, block_desc->block_len); 6908 } 6909 6910 switch (page_code) { 6911 case SMS_ALL_PAGES_PAGE: { 6912 int i, data_used; 6913 6914 data_used = header_len; 6915 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6916 struct ctl_page_index *page_index; 6917 6918 page_index = &lun->mode_pages.index[i]; 6919 6920 if ((control_dev != 0) 6921 && (page_index->page_flags & 6922 CTL_PAGE_FLAG_DISK_ONLY)) 6923 continue; 6924 6925 /* 6926 * We don't use this subpage if the user didn't 6927 * request all subpages. We already checked (above) 6928 * to make sure the user only specified a subpage 6929 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6930 */ 6931 if ((page_index->subpage != 0) 6932 && (subpage == SMS_SUBPAGE_PAGE_0)) 6933 continue; 6934 6935 /* 6936 * Call the handler, if it exists, to update the 6937 * page to the latest values. 6938 */ 6939 if (page_index->sense_handler != NULL) 6940 page_index->sense_handler(ctsio, page_index,pc); 6941 6942 memcpy(ctsio->kern_data_ptr + data_used, 6943 page_index->page_data + 6944 (page_index->page_len * pc), 6945 page_index->page_len); 6946 data_used += page_index->page_len; 6947 } 6948 break; 6949 } 6950 default: { 6951 int i, data_used; 6952 6953 data_used = header_len; 6954 6955 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6956 struct ctl_page_index *page_index; 6957 6958 page_index = &lun->mode_pages.index[i]; 6959 6960 /* Look for the right page code */ 6961 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6962 continue; 6963 6964 /* Look for the right subpage or the subpage wildcard*/ 6965 if ((page_index->subpage != subpage) 6966 && (subpage != SMS_SUBPAGE_ALL)) 6967 continue; 6968 6969 /* Make sure the page is supported for this dev type */ 6970 if ((control_dev != 0) 6971 && (page_index->page_flags & 6972 CTL_PAGE_FLAG_DISK_ONLY)) 6973 continue; 6974 6975 /* 6976 * Call the handler, if it exists, to update the 6977 * page to the latest values. 6978 */ 6979 if (page_index->sense_handler != NULL) 6980 page_index->sense_handler(ctsio, page_index,pc); 6981 6982 memcpy(ctsio->kern_data_ptr + data_used, 6983 page_index->page_data + 6984 (page_index->page_len * pc), 6985 page_index->page_len); 6986 data_used += page_index->page_len; 6987 } 6988 break; 6989 } 6990 } 6991 6992 ctl_set_success(ctsio); 6993 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6994 ctsio->be_move_done = ctl_config_move_done; 6995 ctl_datamove((union ctl_io *)ctsio); 6996 return (CTL_RETVAL_COMPLETE); 6997} 6998 6999int 7000ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 7001 struct ctl_page_index *page_index, 7002 int pc) 7003{ 7004 struct ctl_lun *lun; 7005 struct scsi_log_param_header *phdr; 7006 uint8_t *data; 7007 uint64_t val; 7008 7009 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7010 data = page_index->page_data; 7011 7012 if (lun->backend->lun_attr != NULL && 7013 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 7014 != UINT64_MAX) { 7015 phdr = (struct scsi_log_param_header *)data; 7016 scsi_ulto2b(0x0001, phdr->param_code); 7017 phdr->param_control = SLP_LBIN | SLP_LP; 7018 phdr->param_len = 8; 7019 data = (uint8_t *)(phdr + 1); 7020 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 7021 data[4] = 0x02; /* per-pool */ 7022 data += phdr->param_len; 7023 } 7024 7025 if (lun->backend->lun_attr != NULL && 7026 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 7027 != UINT64_MAX) { 7028 phdr = (struct scsi_log_param_header *)data; 7029 scsi_ulto2b(0x0002, phdr->param_code); 7030 phdr->param_control = SLP_LBIN | SLP_LP; 7031 phdr->param_len = 8; 7032 data = (uint8_t *)(phdr + 1); 7033 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 7034 data[4] = 0x01; /* per-LUN */ 7035 data += phdr->param_len; 7036 } 7037 7038 if (lun->backend->lun_attr != NULL && 7039 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 7040 != UINT64_MAX) { 7041 phdr = (struct scsi_log_param_header *)data; 7042 scsi_ulto2b(0x00f1, phdr->param_code); 7043 phdr->param_control = SLP_LBIN | SLP_LP; 7044 phdr->param_len = 8; 7045 data = (uint8_t *)(phdr + 1); 7046 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 7047 data[4] = 0x02; /* per-pool */ 7048 data += phdr->param_len; 7049 } 7050 7051 if (lun->backend->lun_attr != NULL && 7052 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 7053 != UINT64_MAX) { 7054 phdr = (struct scsi_log_param_header *)data; 7055 scsi_ulto2b(0x00f2, phdr->param_code); 7056 phdr->param_control = SLP_LBIN | SLP_LP; 7057 phdr->param_len = 8; 7058 data = (uint8_t *)(phdr + 1); 7059 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 7060 data[4] = 0x02; /* per-pool */ 7061 data += phdr->param_len; 7062 } 7063 7064 page_index->page_len = data - page_index->page_data; 7065 return (0); 7066} 7067 7068int 7069ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 7070 struct ctl_page_index *page_index, 7071 int pc) 7072{ 7073 struct ctl_lun *lun; 7074 struct stat_page *data; 7075 uint64_t rn, wn, rb, wb; 7076 struct bintime rt, wt; 7077 int i; 7078 7079 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7080 data = (struct stat_page *)page_index->page_data; 7081 7082 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 7083 data->sap.hdr.param_control = SLP_LBIN; 7084 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 7085 sizeof(struct scsi_log_param_header); 7086 rn = wn = rb = wb = 0; 7087 bintime_clear(&rt); 7088 bintime_clear(&wt); 7089 for (i = 0; i < CTL_MAX_PORTS; i++) { 7090 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 7091 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 7092 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 7093 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 7094 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 7095 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 7096 } 7097 scsi_u64to8b(rn, data->sap.read_num); 7098 scsi_u64to8b(wn, data->sap.write_num); 7099 if (lun->stats.blocksize > 0) { 7100 scsi_u64to8b(wb / lun->stats.blocksize, 7101 data->sap.recvieved_lba); 7102 scsi_u64to8b(rb / lun->stats.blocksize, 7103 data->sap.transmitted_lba); 7104 } 7105 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 7106 data->sap.read_int); 7107 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 7108 data->sap.write_int); 7109 scsi_u64to8b(0, data->sap.weighted_num); 7110 scsi_u64to8b(0, data->sap.weighted_int); 7111 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 7112 data->it.hdr.param_control = SLP_LBIN; 7113 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 7114 sizeof(struct scsi_log_param_header); 7115#ifdef CTL_TIME_IO 7116 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 7117#endif 7118 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 7119 data->it.hdr.param_control = SLP_LBIN; 7120 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 7121 sizeof(struct scsi_log_param_header); 7122 scsi_ulto4b(3, data->ti.exponent); 7123 scsi_ulto4b(1, data->ti.integer); 7124 7125 page_index->page_len = sizeof(*data); 7126 return (0); 7127} 7128 7129int 7130ctl_log_sense(struct ctl_scsiio *ctsio) 7131{ 7132 struct ctl_lun *lun; 7133 int i, pc, page_code, subpage; 7134 int alloc_len, total_len; 7135 struct ctl_page_index *page_index; 7136 struct scsi_log_sense *cdb; 7137 struct scsi_log_header *header; 7138 7139 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 7140 7141 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7142 cdb = (struct scsi_log_sense *)ctsio->cdb; 7143 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 7144 page_code = cdb->page & SLS_PAGE_CODE; 7145 subpage = cdb->subpage; 7146 alloc_len = scsi_2btoul(cdb->length); 7147 7148 page_index = NULL; 7149 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 7150 page_index = &lun->log_pages.index[i]; 7151 7152 /* Look for the right page code */ 7153 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 7154 continue; 7155 7156 /* Look for the right subpage or the subpage wildcard*/ 7157 if (page_index->subpage != subpage) 7158 continue; 7159 7160 break; 7161 } 7162 if (i >= CTL_NUM_LOG_PAGES) { 7163 ctl_set_invalid_field(ctsio, 7164 /*sks_valid*/ 1, 7165 /*command*/ 1, 7166 /*field*/ 2, 7167 /*bit_valid*/ 0, 7168 /*bit*/ 0); 7169 ctl_done((union ctl_io *)ctsio); 7170 return (CTL_RETVAL_COMPLETE); 7171 } 7172 7173 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 7174 7175 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7176 ctsio->kern_sg_entries = 0; 7177 ctsio->kern_data_resid = 0; 7178 ctsio->kern_rel_offset = 0; 7179 if (total_len < alloc_len) { 7180 ctsio->residual = alloc_len - total_len; 7181 ctsio->kern_data_len = total_len; 7182 ctsio->kern_total_len = total_len; 7183 } else { 7184 ctsio->residual = 0; 7185 ctsio->kern_data_len = alloc_len; 7186 ctsio->kern_total_len = alloc_len; 7187 } 7188 7189 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7190 header->page = page_index->page_code; 7191 if (page_index->subpage) { 7192 header->page |= SL_SPF; 7193 header->subpage = page_index->subpage; 7194 } 7195 scsi_ulto2b(page_index->page_len, header->datalen); 7196 7197 /* 7198 * Call the handler, if it exists, to update the 7199 * page to the latest values. 7200 */ 7201 if (page_index->sense_handler != NULL) 7202 page_index->sense_handler(ctsio, page_index, pc); 7203 7204 memcpy(header + 1, page_index->page_data, page_index->page_len); 7205 7206 ctl_set_success(ctsio); 7207 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7208 ctsio->be_move_done = ctl_config_move_done; 7209 ctl_datamove((union ctl_io *)ctsio); 7210 return (CTL_RETVAL_COMPLETE); 7211} 7212 7213int 7214ctl_read_capacity(struct ctl_scsiio *ctsio) 7215{ 7216 struct scsi_read_capacity *cdb; 7217 struct scsi_read_capacity_data *data; 7218 struct ctl_lun *lun; 7219 uint32_t lba; 7220 7221 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7222 7223 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7224 7225 lba = scsi_4btoul(cdb->addr); 7226 if (((cdb->pmi & SRC_PMI) == 0) 7227 && (lba != 0)) { 7228 ctl_set_invalid_field(/*ctsio*/ ctsio, 7229 /*sks_valid*/ 1, 7230 /*command*/ 1, 7231 /*field*/ 2, 7232 /*bit_valid*/ 0, 7233 /*bit*/ 0); 7234 ctl_done((union ctl_io *)ctsio); 7235 return (CTL_RETVAL_COMPLETE); 7236 } 7237 7238 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7239 7240 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7241 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7242 ctsio->residual = 0; 7243 ctsio->kern_data_len = sizeof(*data); 7244 ctsio->kern_total_len = sizeof(*data); 7245 ctsio->kern_data_resid = 0; 7246 ctsio->kern_rel_offset = 0; 7247 ctsio->kern_sg_entries = 0; 7248 7249 /* 7250 * If the maximum LBA is greater than 0xfffffffe, the user must 7251 * issue a SERVICE ACTION IN (16) command, with the read capacity 7252 * serivce action set. 7253 */ 7254 if (lun->be_lun->maxlba > 0xfffffffe) 7255 scsi_ulto4b(0xffffffff, data->addr); 7256 else 7257 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7258 7259 /* 7260 * XXX KDM this may not be 512 bytes... 7261 */ 7262 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7263 7264 ctl_set_success(ctsio); 7265 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7266 ctsio->be_move_done = ctl_config_move_done; 7267 ctl_datamove((union ctl_io *)ctsio); 7268 return (CTL_RETVAL_COMPLETE); 7269} 7270 7271int 7272ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7273{ 7274 struct scsi_read_capacity_16 *cdb; 7275 struct scsi_read_capacity_data_long *data; 7276 struct ctl_lun *lun; 7277 uint64_t lba; 7278 uint32_t alloc_len; 7279 7280 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7281 7282 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7283 7284 alloc_len = scsi_4btoul(cdb->alloc_len); 7285 lba = scsi_8btou64(cdb->addr); 7286 7287 if ((cdb->reladr & SRC16_PMI) 7288 && (lba != 0)) { 7289 ctl_set_invalid_field(/*ctsio*/ ctsio, 7290 /*sks_valid*/ 1, 7291 /*command*/ 1, 7292 /*field*/ 2, 7293 /*bit_valid*/ 0, 7294 /*bit*/ 0); 7295 ctl_done((union ctl_io *)ctsio); 7296 return (CTL_RETVAL_COMPLETE); 7297 } 7298 7299 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7300 7301 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7302 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7303 7304 if (sizeof(*data) < alloc_len) { 7305 ctsio->residual = alloc_len - sizeof(*data); 7306 ctsio->kern_data_len = sizeof(*data); 7307 ctsio->kern_total_len = sizeof(*data); 7308 } else { 7309 ctsio->residual = 0; 7310 ctsio->kern_data_len = alloc_len; 7311 ctsio->kern_total_len = alloc_len; 7312 } 7313 ctsio->kern_data_resid = 0; 7314 ctsio->kern_rel_offset = 0; 7315 ctsio->kern_sg_entries = 0; 7316 7317 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7318 /* XXX KDM this may not be 512 bytes... */ 7319 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7320 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7321 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7322 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7323 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7324 7325 ctl_set_success(ctsio); 7326 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7327 ctsio->be_move_done = ctl_config_move_done; 7328 ctl_datamove((union ctl_io *)ctsio); 7329 return (CTL_RETVAL_COMPLETE); 7330} 7331 7332int 7333ctl_get_lba_status(struct ctl_scsiio *ctsio) 7334{ 7335 struct scsi_get_lba_status *cdb; 7336 struct scsi_get_lba_status_data *data; 7337 struct ctl_lun *lun; 7338 struct ctl_lba_len_flags *lbalen; 7339 uint64_t lba; 7340 uint32_t alloc_len, total_len; 7341 int retval; 7342 7343 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7344 7345 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7346 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7347 lba = scsi_8btou64(cdb->addr); 7348 alloc_len = scsi_4btoul(cdb->alloc_len); 7349 7350 if (lba > lun->be_lun->maxlba) { 7351 ctl_set_lba_out_of_range(ctsio); 7352 ctl_done((union ctl_io *)ctsio); 7353 return (CTL_RETVAL_COMPLETE); 7354 } 7355 7356 total_len = sizeof(*data) + sizeof(data->descr[0]); 7357 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7358 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7359 7360 if (total_len < alloc_len) { 7361 ctsio->residual = alloc_len - total_len; 7362 ctsio->kern_data_len = total_len; 7363 ctsio->kern_total_len = total_len; 7364 } else { 7365 ctsio->residual = 0; 7366 ctsio->kern_data_len = alloc_len; 7367 ctsio->kern_total_len = alloc_len; 7368 } 7369 ctsio->kern_data_resid = 0; 7370 ctsio->kern_rel_offset = 0; 7371 ctsio->kern_sg_entries = 0; 7372 7373 /* Fill dummy data in case backend can't tell anything. */ 7374 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7375 scsi_u64to8b(lba, data->descr[0].addr); 7376 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7377 data->descr[0].length); 7378 data->descr[0].status = 0; /* Mapped or unknown. */ 7379 7380 ctl_set_success(ctsio); 7381 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7382 ctsio->be_move_done = ctl_config_move_done; 7383 7384 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7385 lbalen->lba = lba; 7386 lbalen->len = total_len; 7387 lbalen->flags = 0; 7388 retval = lun->backend->config_read((union ctl_io *)ctsio); 7389 return (CTL_RETVAL_COMPLETE); 7390} 7391 7392int 7393ctl_read_defect(struct ctl_scsiio *ctsio) 7394{ 7395 struct scsi_read_defect_data_10 *ccb10; 7396 struct scsi_read_defect_data_12 *ccb12; 7397 struct scsi_read_defect_data_hdr_10 *data10; 7398 struct scsi_read_defect_data_hdr_12 *data12; 7399 uint32_t alloc_len, data_len; 7400 uint8_t format; 7401 7402 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7403 7404 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7405 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7406 format = ccb10->format; 7407 alloc_len = scsi_2btoul(ccb10->alloc_length); 7408 data_len = sizeof(*data10); 7409 } else { 7410 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7411 format = ccb12->format; 7412 alloc_len = scsi_4btoul(ccb12->alloc_length); 7413 data_len = sizeof(*data12); 7414 } 7415 if (alloc_len == 0) { 7416 ctl_set_success(ctsio); 7417 ctl_done((union ctl_io *)ctsio); 7418 return (CTL_RETVAL_COMPLETE); 7419 } 7420 7421 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7422 if (data_len < alloc_len) { 7423 ctsio->residual = alloc_len - data_len; 7424 ctsio->kern_data_len = data_len; 7425 ctsio->kern_total_len = data_len; 7426 } else { 7427 ctsio->residual = 0; 7428 ctsio->kern_data_len = alloc_len; 7429 ctsio->kern_total_len = alloc_len; 7430 } 7431 ctsio->kern_data_resid = 0; 7432 ctsio->kern_rel_offset = 0; 7433 ctsio->kern_sg_entries = 0; 7434 7435 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7436 data10 = (struct scsi_read_defect_data_hdr_10 *) 7437 ctsio->kern_data_ptr; 7438 data10->format = format; 7439 scsi_ulto2b(0, data10->length); 7440 } else { 7441 data12 = (struct scsi_read_defect_data_hdr_12 *) 7442 ctsio->kern_data_ptr; 7443 data12->format = format; 7444 scsi_ulto2b(0, data12->generation); 7445 scsi_ulto4b(0, data12->length); 7446 } 7447 7448 ctl_set_success(ctsio); 7449 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7450 ctsio->be_move_done = ctl_config_move_done; 7451 ctl_datamove((union ctl_io *)ctsio); 7452 return (CTL_RETVAL_COMPLETE); 7453} 7454 7455int 7456ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7457{ 7458 struct scsi_maintenance_in *cdb; 7459 int retval; 7460 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os; 7461 int num_target_port_groups, num_target_ports; 7462 struct ctl_lun *lun; 7463 struct ctl_softc *softc; 7464 struct ctl_port *port; 7465 struct scsi_target_group_data *rtg_ptr; 7466 struct scsi_target_group_data_extended *rtg_ext_ptr; 7467 struct scsi_target_port_group_descriptor *tpg_desc; 7468 7469 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7470 7471 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7472 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7473 softc = lun->ctl_softc; 7474 7475 retval = CTL_RETVAL_COMPLETE; 7476 7477 switch (cdb->byte2 & STG_PDF_MASK) { 7478 case STG_PDF_LENGTH: 7479 ext = 0; 7480 break; 7481 case STG_PDF_EXTENDED: 7482 ext = 1; 7483 break; 7484 default: 7485 ctl_set_invalid_field(/*ctsio*/ ctsio, 7486 /*sks_valid*/ 1, 7487 /*command*/ 1, 7488 /*field*/ 2, 7489 /*bit_valid*/ 1, 7490 /*bit*/ 5); 7491 ctl_done((union ctl_io *)ctsio); 7492 return(retval); 7493 } 7494 7495 if (softc->is_single) 7496 num_target_port_groups = 1; 7497 else 7498 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7499 num_target_ports = 0; 7500 mtx_lock(&softc->ctl_lock); 7501 STAILQ_FOREACH(port, &softc->port_list, links) { 7502 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7503 continue; 7504 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7505 continue; 7506 num_target_ports++; 7507 } 7508 mtx_unlock(&softc->ctl_lock); 7509 7510 if (ext) 7511 total_len = sizeof(struct scsi_target_group_data_extended); 7512 else 7513 total_len = sizeof(struct scsi_target_group_data); 7514 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7515 num_target_port_groups + 7516 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7517 7518 alloc_len = scsi_4btoul(cdb->length); 7519 7520 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7521 7522 ctsio->kern_sg_entries = 0; 7523 7524 if (total_len < alloc_len) { 7525 ctsio->residual = alloc_len - total_len; 7526 ctsio->kern_data_len = total_len; 7527 ctsio->kern_total_len = total_len; 7528 } else { 7529 ctsio->residual = 0; 7530 ctsio->kern_data_len = alloc_len; 7531 ctsio->kern_total_len = alloc_len; 7532 } 7533 ctsio->kern_data_resid = 0; 7534 ctsio->kern_rel_offset = 0; 7535 7536 if (ext) { 7537 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7538 ctsio->kern_data_ptr; 7539 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7540 rtg_ext_ptr->format_type = 0x10; 7541 rtg_ext_ptr->implicit_transition_time = 0; 7542 tpg_desc = &rtg_ext_ptr->groups[0]; 7543 } else { 7544 rtg_ptr = (struct scsi_target_group_data *) 7545 ctsio->kern_data_ptr; 7546 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7547 tpg_desc = &rtg_ptr->groups[0]; 7548 } 7549 7550 mtx_lock(&softc->ctl_lock); 7551 pg = softc->port_min / softc->port_cnt; 7552 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7553 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7554 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7555 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7556 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7557 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7558 else 7559 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7560 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7561 os = gs; 7562 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7563 } else 7564 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7565 for (g = 0; g < num_target_port_groups; g++) { 7566 tpg_desc->pref_state = (g == pg) ? gs : os; 7567 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7568 TPG_U_SUP | TPG_T_SUP; 7569 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7570 tpg_desc->status = TPG_IMPLICIT; 7571 pc = 0; 7572 STAILQ_FOREACH(port, &softc->port_list, links) { 7573 if (port->targ_port < g * softc->port_cnt || 7574 port->targ_port >= (g + 1) * softc->port_cnt) 7575 continue; 7576 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7577 continue; 7578 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7579 continue; 7580 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7581 relative_target_port_identifier); 7582 pc++; 7583 } 7584 tpg_desc->target_port_count = pc; 7585 tpg_desc = (struct scsi_target_port_group_descriptor *) 7586 &tpg_desc->descriptors[pc]; 7587 } 7588 mtx_unlock(&softc->ctl_lock); 7589 7590 ctl_set_success(ctsio); 7591 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7592 ctsio->be_move_done = ctl_config_move_done; 7593 ctl_datamove((union ctl_io *)ctsio); 7594 return(retval); 7595} 7596 7597int 7598ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7599{ 7600 struct ctl_lun *lun; 7601 struct scsi_report_supported_opcodes *cdb; 7602 const struct ctl_cmd_entry *entry, *sentry; 7603 struct scsi_report_supported_opcodes_all *all; 7604 struct scsi_report_supported_opcodes_descr *descr; 7605 struct scsi_report_supported_opcodes_one *one; 7606 int retval; 7607 int alloc_len, total_len; 7608 int opcode, service_action, i, j, num; 7609 7610 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7611 7612 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7613 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7614 7615 retval = CTL_RETVAL_COMPLETE; 7616 7617 opcode = cdb->requested_opcode; 7618 service_action = scsi_2btoul(cdb->requested_service_action); 7619 switch (cdb->options & RSO_OPTIONS_MASK) { 7620 case RSO_OPTIONS_ALL: 7621 num = 0; 7622 for (i = 0; i < 256; i++) { 7623 entry = &ctl_cmd_table[i]; 7624 if (entry->flags & CTL_CMD_FLAG_SA5) { 7625 for (j = 0; j < 32; j++) { 7626 sentry = &((const struct ctl_cmd_entry *) 7627 entry->execute)[j]; 7628 if (ctl_cmd_applicable( 7629 lun->be_lun->lun_type, sentry)) 7630 num++; 7631 } 7632 } else { 7633 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7634 entry)) 7635 num++; 7636 } 7637 } 7638 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7639 num * sizeof(struct scsi_report_supported_opcodes_descr); 7640 break; 7641 case RSO_OPTIONS_OC: 7642 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7643 ctl_set_invalid_field(/*ctsio*/ ctsio, 7644 /*sks_valid*/ 1, 7645 /*command*/ 1, 7646 /*field*/ 2, 7647 /*bit_valid*/ 1, 7648 /*bit*/ 2); 7649 ctl_done((union ctl_io *)ctsio); 7650 return (CTL_RETVAL_COMPLETE); 7651 } 7652 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7653 break; 7654 case RSO_OPTIONS_OC_SA: 7655 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7656 service_action >= 32) { 7657 ctl_set_invalid_field(/*ctsio*/ ctsio, 7658 /*sks_valid*/ 1, 7659 /*command*/ 1, 7660 /*field*/ 2, 7661 /*bit_valid*/ 1, 7662 /*bit*/ 2); 7663 ctl_done((union ctl_io *)ctsio); 7664 return (CTL_RETVAL_COMPLETE); 7665 } 7666 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7667 break; 7668 default: 7669 ctl_set_invalid_field(/*ctsio*/ ctsio, 7670 /*sks_valid*/ 1, 7671 /*command*/ 1, 7672 /*field*/ 2, 7673 /*bit_valid*/ 1, 7674 /*bit*/ 2); 7675 ctl_done((union ctl_io *)ctsio); 7676 return (CTL_RETVAL_COMPLETE); 7677 } 7678 7679 alloc_len = scsi_4btoul(cdb->length); 7680 7681 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7682 7683 ctsio->kern_sg_entries = 0; 7684 7685 if (total_len < alloc_len) { 7686 ctsio->residual = alloc_len - total_len; 7687 ctsio->kern_data_len = total_len; 7688 ctsio->kern_total_len = total_len; 7689 } else { 7690 ctsio->residual = 0; 7691 ctsio->kern_data_len = alloc_len; 7692 ctsio->kern_total_len = alloc_len; 7693 } 7694 ctsio->kern_data_resid = 0; 7695 ctsio->kern_rel_offset = 0; 7696 7697 switch (cdb->options & RSO_OPTIONS_MASK) { 7698 case RSO_OPTIONS_ALL: 7699 all = (struct scsi_report_supported_opcodes_all *) 7700 ctsio->kern_data_ptr; 7701 num = 0; 7702 for (i = 0; i < 256; i++) { 7703 entry = &ctl_cmd_table[i]; 7704 if (entry->flags & CTL_CMD_FLAG_SA5) { 7705 for (j = 0; j < 32; j++) { 7706 sentry = &((const struct ctl_cmd_entry *) 7707 entry->execute)[j]; 7708 if (!ctl_cmd_applicable( 7709 lun->be_lun->lun_type, sentry)) 7710 continue; 7711 descr = &all->descr[num++]; 7712 descr->opcode = i; 7713 scsi_ulto2b(j, descr->service_action); 7714 descr->flags = RSO_SERVACTV; 7715 scsi_ulto2b(sentry->length, 7716 descr->cdb_length); 7717 } 7718 } else { 7719 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7720 entry)) 7721 continue; 7722 descr = &all->descr[num++]; 7723 descr->opcode = i; 7724 scsi_ulto2b(0, descr->service_action); 7725 descr->flags = 0; 7726 scsi_ulto2b(entry->length, descr->cdb_length); 7727 } 7728 } 7729 scsi_ulto4b( 7730 num * sizeof(struct scsi_report_supported_opcodes_descr), 7731 all->length); 7732 break; 7733 case RSO_OPTIONS_OC: 7734 one = (struct scsi_report_supported_opcodes_one *) 7735 ctsio->kern_data_ptr; 7736 entry = &ctl_cmd_table[opcode]; 7737 goto fill_one; 7738 case RSO_OPTIONS_OC_SA: 7739 one = (struct scsi_report_supported_opcodes_one *) 7740 ctsio->kern_data_ptr; 7741 entry = &ctl_cmd_table[opcode]; 7742 entry = &((const struct ctl_cmd_entry *) 7743 entry->execute)[service_action]; 7744fill_one: 7745 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7746 one->support = 3; 7747 scsi_ulto2b(entry->length, one->cdb_length); 7748 one->cdb_usage[0] = opcode; 7749 memcpy(&one->cdb_usage[1], entry->usage, 7750 entry->length - 1); 7751 } else 7752 one->support = 1; 7753 break; 7754 } 7755 7756 ctl_set_success(ctsio); 7757 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7758 ctsio->be_move_done = ctl_config_move_done; 7759 ctl_datamove((union ctl_io *)ctsio); 7760 return(retval); 7761} 7762 7763int 7764ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7765{ 7766 struct scsi_report_supported_tmf *cdb; 7767 struct scsi_report_supported_tmf_data *data; 7768 int retval; 7769 int alloc_len, total_len; 7770 7771 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7772 7773 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7774 7775 retval = CTL_RETVAL_COMPLETE; 7776 7777 total_len = sizeof(struct scsi_report_supported_tmf_data); 7778 alloc_len = scsi_4btoul(cdb->length); 7779 7780 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7781 7782 ctsio->kern_sg_entries = 0; 7783 7784 if (total_len < alloc_len) { 7785 ctsio->residual = alloc_len - total_len; 7786 ctsio->kern_data_len = total_len; 7787 ctsio->kern_total_len = total_len; 7788 } else { 7789 ctsio->residual = 0; 7790 ctsio->kern_data_len = alloc_len; 7791 ctsio->kern_total_len = alloc_len; 7792 } 7793 ctsio->kern_data_resid = 0; 7794 ctsio->kern_rel_offset = 0; 7795 7796 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7797 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7798 RST_TRS; 7799 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7800 7801 ctl_set_success(ctsio); 7802 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7803 ctsio->be_move_done = ctl_config_move_done; 7804 ctl_datamove((union ctl_io *)ctsio); 7805 return (retval); 7806} 7807 7808int 7809ctl_report_timestamp(struct ctl_scsiio *ctsio) 7810{ 7811 struct scsi_report_timestamp *cdb; 7812 struct scsi_report_timestamp_data *data; 7813 struct timeval tv; 7814 int64_t timestamp; 7815 int retval; 7816 int alloc_len, total_len; 7817 7818 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7819 7820 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7821 7822 retval = CTL_RETVAL_COMPLETE; 7823 7824 total_len = sizeof(struct scsi_report_timestamp_data); 7825 alloc_len = scsi_4btoul(cdb->length); 7826 7827 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7828 7829 ctsio->kern_sg_entries = 0; 7830 7831 if (total_len < alloc_len) { 7832 ctsio->residual = alloc_len - total_len; 7833 ctsio->kern_data_len = total_len; 7834 ctsio->kern_total_len = total_len; 7835 } else { 7836 ctsio->residual = 0; 7837 ctsio->kern_data_len = alloc_len; 7838 ctsio->kern_total_len = alloc_len; 7839 } 7840 ctsio->kern_data_resid = 0; 7841 ctsio->kern_rel_offset = 0; 7842 7843 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7844 scsi_ulto2b(sizeof(*data) - 2, data->length); 7845 data->origin = RTS_ORIG_OUTSIDE; 7846 getmicrotime(&tv); 7847 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7848 scsi_ulto4b(timestamp >> 16, data->timestamp); 7849 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7850 7851 ctl_set_success(ctsio); 7852 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7853 ctsio->be_move_done = ctl_config_move_done; 7854 ctl_datamove((union ctl_io *)ctsio); 7855 return (retval); 7856} 7857 7858int 7859ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7860{ 7861 struct scsi_per_res_in *cdb; 7862 int alloc_len, total_len = 0; 7863 /* struct scsi_per_res_in_rsrv in_data; */ 7864 struct ctl_lun *lun; 7865 struct ctl_softc *softc; 7866 uint64_t key; 7867 7868 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7869 7870 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7871 7872 alloc_len = scsi_2btoul(cdb->length); 7873 7874 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7875 softc = lun->ctl_softc; 7876 7877retry: 7878 mtx_lock(&lun->lun_lock); 7879 switch (cdb->action) { 7880 case SPRI_RK: /* read keys */ 7881 total_len = sizeof(struct scsi_per_res_in_keys) + 7882 lun->pr_key_count * 7883 sizeof(struct scsi_per_res_key); 7884 break; 7885 case SPRI_RR: /* read reservation */ 7886 if (lun->flags & CTL_LUN_PR_RESERVED) 7887 total_len = sizeof(struct scsi_per_res_in_rsrv); 7888 else 7889 total_len = sizeof(struct scsi_per_res_in_header); 7890 break; 7891 case SPRI_RC: /* report capabilities */ 7892 total_len = sizeof(struct scsi_per_res_cap); 7893 break; 7894 case SPRI_RS: /* read full status */ 7895 total_len = sizeof(struct scsi_per_res_in_header) + 7896 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7897 lun->pr_key_count; 7898 break; 7899 default: 7900 panic("Invalid PR type %x", cdb->action); 7901 } 7902 mtx_unlock(&lun->lun_lock); 7903 7904 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7905 7906 if (total_len < alloc_len) { 7907 ctsio->residual = alloc_len - total_len; 7908 ctsio->kern_data_len = total_len; 7909 ctsio->kern_total_len = total_len; 7910 } else { 7911 ctsio->residual = 0; 7912 ctsio->kern_data_len = alloc_len; 7913 ctsio->kern_total_len = alloc_len; 7914 } 7915 7916 ctsio->kern_data_resid = 0; 7917 ctsio->kern_rel_offset = 0; 7918 ctsio->kern_sg_entries = 0; 7919 7920 mtx_lock(&lun->lun_lock); 7921 switch (cdb->action) { 7922 case SPRI_RK: { // read keys 7923 struct scsi_per_res_in_keys *res_keys; 7924 int i, key_count; 7925 7926 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7927 7928 /* 7929 * We had to drop the lock to allocate our buffer, which 7930 * leaves time for someone to come in with another 7931 * persistent reservation. (That is unlikely, though, 7932 * since this should be the only persistent reservation 7933 * command active right now.) 7934 */ 7935 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7936 (lun->pr_key_count * 7937 sizeof(struct scsi_per_res_key)))){ 7938 mtx_unlock(&lun->lun_lock); 7939 free(ctsio->kern_data_ptr, M_CTL); 7940 printf("%s: reservation length changed, retrying\n", 7941 __func__); 7942 goto retry; 7943 } 7944 7945 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7946 7947 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7948 lun->pr_key_count, res_keys->header.length); 7949 7950 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7951 if ((key = ctl_get_prkey(lun, i)) == 0) 7952 continue; 7953 7954 /* 7955 * We used lun->pr_key_count to calculate the 7956 * size to allocate. If it turns out the number of 7957 * initiators with the registered flag set is 7958 * larger than that (i.e. they haven't been kept in 7959 * sync), we've got a problem. 7960 */ 7961 if (key_count >= lun->pr_key_count) { 7962#ifdef NEEDTOPORT 7963 csevent_log(CSC_CTL | CSC_SHELF_SW | 7964 CTL_PR_ERROR, 7965 csevent_LogType_Fault, 7966 csevent_AlertLevel_Yellow, 7967 csevent_FRU_ShelfController, 7968 csevent_FRU_Firmware, 7969 csevent_FRU_Unknown, 7970 "registered keys %d >= key " 7971 "count %d", key_count, 7972 lun->pr_key_count); 7973#endif 7974 key_count++; 7975 continue; 7976 } 7977 scsi_u64to8b(key, res_keys->keys[key_count].key); 7978 key_count++; 7979 } 7980 break; 7981 } 7982 case SPRI_RR: { // read reservation 7983 struct scsi_per_res_in_rsrv *res; 7984 int tmp_len, header_only; 7985 7986 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7987 7988 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7989 7990 if (lun->flags & CTL_LUN_PR_RESERVED) 7991 { 7992 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7993 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7994 res->header.length); 7995 header_only = 0; 7996 } else { 7997 tmp_len = sizeof(struct scsi_per_res_in_header); 7998 scsi_ulto4b(0, res->header.length); 7999 header_only = 1; 8000 } 8001 8002 /* 8003 * We had to drop the lock to allocate our buffer, which 8004 * leaves time for someone to come in with another 8005 * persistent reservation. (That is unlikely, though, 8006 * since this should be the only persistent reservation 8007 * command active right now.) 8008 */ 8009 if (tmp_len != total_len) { 8010 mtx_unlock(&lun->lun_lock); 8011 free(ctsio->kern_data_ptr, M_CTL); 8012 printf("%s: reservation status changed, retrying\n", 8013 __func__); 8014 goto retry; 8015 } 8016 8017 /* 8018 * No reservation held, so we're done. 8019 */ 8020 if (header_only != 0) 8021 break; 8022 8023 /* 8024 * If the registration is an All Registrants type, the key 8025 * is 0, since it doesn't really matter. 8026 */ 8027 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8028 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 8029 res->data.reservation); 8030 } 8031 res->data.scopetype = lun->res_type; 8032 break; 8033 } 8034 case SPRI_RC: //report capabilities 8035 { 8036 struct scsi_per_res_cap *res_cap; 8037 uint16_t type_mask; 8038 8039 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 8040 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 8041 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 8042 type_mask = SPRI_TM_WR_EX_AR | 8043 SPRI_TM_EX_AC_RO | 8044 SPRI_TM_WR_EX_RO | 8045 SPRI_TM_EX_AC | 8046 SPRI_TM_WR_EX | 8047 SPRI_TM_EX_AC_AR; 8048 scsi_ulto2b(type_mask, res_cap->type_mask); 8049 break; 8050 } 8051 case SPRI_RS: { // read full status 8052 struct scsi_per_res_in_full *res_status; 8053 struct scsi_per_res_in_full_desc *res_desc; 8054 struct ctl_port *port; 8055 int i, len; 8056 8057 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 8058 8059 /* 8060 * We had to drop the lock to allocate our buffer, which 8061 * leaves time for someone to come in with another 8062 * persistent reservation. (That is unlikely, though, 8063 * since this should be the only persistent reservation 8064 * command active right now.) 8065 */ 8066 if (total_len < (sizeof(struct scsi_per_res_in_header) + 8067 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 8068 lun->pr_key_count)){ 8069 mtx_unlock(&lun->lun_lock); 8070 free(ctsio->kern_data_ptr, M_CTL); 8071 printf("%s: reservation length changed, retrying\n", 8072 __func__); 8073 goto retry; 8074 } 8075 8076 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 8077 8078 res_desc = &res_status->desc[0]; 8079 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8080 if ((key = ctl_get_prkey(lun, i)) == 0) 8081 continue; 8082 8083 scsi_u64to8b(key, res_desc->res_key.key); 8084 if ((lun->flags & CTL_LUN_PR_RESERVED) && 8085 (lun->pr_res_idx == i || 8086 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 8087 res_desc->flags = SPRI_FULL_R_HOLDER; 8088 res_desc->scopetype = lun->res_type; 8089 } 8090 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 8091 res_desc->rel_trgt_port_id); 8092 len = 0; 8093 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 8094 if (port != NULL) 8095 len = ctl_create_iid(port, 8096 i % CTL_MAX_INIT_PER_PORT, 8097 res_desc->transport_id); 8098 scsi_ulto4b(len, res_desc->additional_length); 8099 res_desc = (struct scsi_per_res_in_full_desc *) 8100 &res_desc->transport_id[len]; 8101 } 8102 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 8103 res_status->header.length); 8104 break; 8105 } 8106 default: 8107 /* 8108 * This is a bug, because we just checked for this above, 8109 * and should have returned an error. 8110 */ 8111 panic("Invalid PR type %x", cdb->action); 8112 break; /* NOTREACHED */ 8113 } 8114 mtx_unlock(&lun->lun_lock); 8115 8116 ctl_set_success(ctsio); 8117 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8118 ctsio->be_move_done = ctl_config_move_done; 8119 ctl_datamove((union ctl_io *)ctsio); 8120 return (CTL_RETVAL_COMPLETE); 8121} 8122 8123/* 8124 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 8125 * it should return. 8126 */ 8127static int 8128ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 8129 uint64_t sa_res_key, uint8_t type, uint32_t residx, 8130 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 8131 struct scsi_per_res_out_parms* param) 8132{ 8133 union ctl_ha_msg persis_io; 8134 int i; 8135 8136 mtx_lock(&lun->lun_lock); 8137 if (sa_res_key == 0) { 8138 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8139 /* validate scope and type */ 8140 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8141 SPR_LU_SCOPE) { 8142 mtx_unlock(&lun->lun_lock); 8143 ctl_set_invalid_field(/*ctsio*/ ctsio, 8144 /*sks_valid*/ 1, 8145 /*command*/ 1, 8146 /*field*/ 2, 8147 /*bit_valid*/ 1, 8148 /*bit*/ 4); 8149 ctl_done((union ctl_io *)ctsio); 8150 return (1); 8151 } 8152 8153 if (type>8 || type==2 || type==4 || type==0) { 8154 mtx_unlock(&lun->lun_lock); 8155 ctl_set_invalid_field(/*ctsio*/ ctsio, 8156 /*sks_valid*/ 1, 8157 /*command*/ 1, 8158 /*field*/ 2, 8159 /*bit_valid*/ 1, 8160 /*bit*/ 0); 8161 ctl_done((union ctl_io *)ctsio); 8162 return (1); 8163 } 8164 8165 /* 8166 * Unregister everybody else and build UA for 8167 * them 8168 */ 8169 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8170 if (i == residx || ctl_get_prkey(lun, i) == 0) 8171 continue; 8172 8173 ctl_clr_prkey(lun, i); 8174 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8175 } 8176 lun->pr_key_count = 1; 8177 lun->res_type = type; 8178 if (lun->res_type != SPR_TYPE_WR_EX_AR 8179 && lun->res_type != SPR_TYPE_EX_AC_AR) 8180 lun->pr_res_idx = residx; 8181 lun->PRGeneration++; 8182 mtx_unlock(&lun->lun_lock); 8183 8184 /* send msg to other side */ 8185 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8186 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8187 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8188 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8189 persis_io.pr.pr_info.res_type = type; 8190 memcpy(persis_io.pr.pr_info.sa_res_key, 8191 param->serv_act_res_key, 8192 sizeof(param->serv_act_res_key)); 8193 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8194 sizeof(persis_io.pr), M_WAITOK); 8195 } else { 8196 /* not all registrants */ 8197 mtx_unlock(&lun->lun_lock); 8198 free(ctsio->kern_data_ptr, M_CTL); 8199 ctl_set_invalid_field(ctsio, 8200 /*sks_valid*/ 1, 8201 /*command*/ 0, 8202 /*field*/ 8, 8203 /*bit_valid*/ 0, 8204 /*bit*/ 0); 8205 ctl_done((union ctl_io *)ctsio); 8206 return (1); 8207 } 8208 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8209 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8210 int found = 0; 8211 8212 if (res_key == sa_res_key) { 8213 /* special case */ 8214 /* 8215 * The spec implies this is not good but doesn't 8216 * say what to do. There are two choices either 8217 * generate a res conflict or check condition 8218 * with illegal field in parameter data. Since 8219 * that is what is done when the sa_res_key is 8220 * zero I'll take that approach since this has 8221 * to do with the sa_res_key. 8222 */ 8223 mtx_unlock(&lun->lun_lock); 8224 free(ctsio->kern_data_ptr, M_CTL); 8225 ctl_set_invalid_field(ctsio, 8226 /*sks_valid*/ 1, 8227 /*command*/ 0, 8228 /*field*/ 8, 8229 /*bit_valid*/ 0, 8230 /*bit*/ 0); 8231 ctl_done((union ctl_io *)ctsio); 8232 return (1); 8233 } 8234 8235 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8236 if (ctl_get_prkey(lun, i) != sa_res_key) 8237 continue; 8238 8239 found = 1; 8240 ctl_clr_prkey(lun, i); 8241 lun->pr_key_count--; 8242 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8243 } 8244 if (!found) { 8245 mtx_unlock(&lun->lun_lock); 8246 free(ctsio->kern_data_ptr, M_CTL); 8247 ctl_set_reservation_conflict(ctsio); 8248 ctl_done((union ctl_io *)ctsio); 8249 return (CTL_RETVAL_COMPLETE); 8250 } 8251 lun->PRGeneration++; 8252 mtx_unlock(&lun->lun_lock); 8253 8254 /* send msg to other side */ 8255 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8256 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8257 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8258 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8259 persis_io.pr.pr_info.res_type = type; 8260 memcpy(persis_io.pr.pr_info.sa_res_key, 8261 param->serv_act_res_key, 8262 sizeof(param->serv_act_res_key)); 8263 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8264 sizeof(persis_io.pr), M_WAITOK); 8265 } else { 8266 /* Reserved but not all registrants */ 8267 /* sa_res_key is res holder */ 8268 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 8269 /* validate scope and type */ 8270 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8271 SPR_LU_SCOPE) { 8272 mtx_unlock(&lun->lun_lock); 8273 ctl_set_invalid_field(/*ctsio*/ ctsio, 8274 /*sks_valid*/ 1, 8275 /*command*/ 1, 8276 /*field*/ 2, 8277 /*bit_valid*/ 1, 8278 /*bit*/ 4); 8279 ctl_done((union ctl_io *)ctsio); 8280 return (1); 8281 } 8282 8283 if (type>8 || type==2 || type==4 || type==0) { 8284 mtx_unlock(&lun->lun_lock); 8285 ctl_set_invalid_field(/*ctsio*/ ctsio, 8286 /*sks_valid*/ 1, 8287 /*command*/ 1, 8288 /*field*/ 2, 8289 /*bit_valid*/ 1, 8290 /*bit*/ 0); 8291 ctl_done((union ctl_io *)ctsio); 8292 return (1); 8293 } 8294 8295 /* 8296 * Do the following: 8297 * if sa_res_key != res_key remove all 8298 * registrants w/sa_res_key and generate UA 8299 * for these registrants(Registrations 8300 * Preempted) if it wasn't an exclusive 8301 * reservation generate UA(Reservations 8302 * Preempted) for all other registered nexuses 8303 * if the type has changed. Establish the new 8304 * reservation and holder. If res_key and 8305 * sa_res_key are the same do the above 8306 * except don't unregister the res holder. 8307 */ 8308 8309 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8310 if (i == residx || ctl_get_prkey(lun, i) == 0) 8311 continue; 8312 8313 if (sa_res_key == ctl_get_prkey(lun, i)) { 8314 ctl_clr_prkey(lun, i); 8315 lun->pr_key_count--; 8316 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8317 } else if (type != lun->res_type 8318 && (lun->res_type == SPR_TYPE_WR_EX_RO 8319 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8320 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8321 } 8322 } 8323 lun->res_type = type; 8324 if (lun->res_type != SPR_TYPE_WR_EX_AR 8325 && lun->res_type != SPR_TYPE_EX_AC_AR) 8326 lun->pr_res_idx = residx; 8327 else 8328 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8329 lun->PRGeneration++; 8330 mtx_unlock(&lun->lun_lock); 8331 8332 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8333 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8334 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8335 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8336 persis_io.pr.pr_info.res_type = type; 8337 memcpy(persis_io.pr.pr_info.sa_res_key, 8338 param->serv_act_res_key, 8339 sizeof(param->serv_act_res_key)); 8340 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8341 sizeof(persis_io.pr), M_WAITOK); 8342 } else { 8343 /* 8344 * sa_res_key is not the res holder just 8345 * remove registrants 8346 */ 8347 int found=0; 8348 8349 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8350 if (sa_res_key != ctl_get_prkey(lun, i)) 8351 continue; 8352 8353 found = 1; 8354 ctl_clr_prkey(lun, i); 8355 lun->pr_key_count--; 8356 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8357 } 8358 8359 if (!found) { 8360 mtx_unlock(&lun->lun_lock); 8361 free(ctsio->kern_data_ptr, M_CTL); 8362 ctl_set_reservation_conflict(ctsio); 8363 ctl_done((union ctl_io *)ctsio); 8364 return (1); 8365 } 8366 lun->PRGeneration++; 8367 mtx_unlock(&lun->lun_lock); 8368 8369 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8370 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8371 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8372 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8373 persis_io.pr.pr_info.res_type = type; 8374 memcpy(persis_io.pr.pr_info.sa_res_key, 8375 param->serv_act_res_key, 8376 sizeof(param->serv_act_res_key)); 8377 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8378 sizeof(persis_io.pr), M_WAITOK); 8379 } 8380 } 8381 return (0); 8382} 8383 8384static void 8385ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8386{ 8387 uint64_t sa_res_key; 8388 int i; 8389 8390 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8391 8392 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8393 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8394 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8395 if (sa_res_key == 0) { 8396 /* 8397 * Unregister everybody else and build UA for 8398 * them 8399 */ 8400 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8401 if (i == msg->pr.pr_info.residx || 8402 ctl_get_prkey(lun, i) == 0) 8403 continue; 8404 8405 ctl_clr_prkey(lun, i); 8406 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8407 } 8408 8409 lun->pr_key_count = 1; 8410 lun->res_type = msg->pr.pr_info.res_type; 8411 if (lun->res_type != SPR_TYPE_WR_EX_AR 8412 && lun->res_type != SPR_TYPE_EX_AC_AR) 8413 lun->pr_res_idx = msg->pr.pr_info.residx; 8414 } else { 8415 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8416 if (sa_res_key == ctl_get_prkey(lun, i)) 8417 continue; 8418 8419 ctl_clr_prkey(lun, i); 8420 lun->pr_key_count--; 8421 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8422 } 8423 } 8424 } else { 8425 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8426 if (i == msg->pr.pr_info.residx || 8427 ctl_get_prkey(lun, i) == 0) 8428 continue; 8429 8430 if (sa_res_key == ctl_get_prkey(lun, i)) { 8431 ctl_clr_prkey(lun, i); 8432 lun->pr_key_count--; 8433 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8434 } else if (msg->pr.pr_info.res_type != lun->res_type 8435 && (lun->res_type == SPR_TYPE_WR_EX_RO 8436 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8437 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8438 } 8439 } 8440 lun->res_type = msg->pr.pr_info.res_type; 8441 if (lun->res_type != SPR_TYPE_WR_EX_AR 8442 && lun->res_type != SPR_TYPE_EX_AC_AR) 8443 lun->pr_res_idx = msg->pr.pr_info.residx; 8444 else 8445 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8446 } 8447 lun->PRGeneration++; 8448 8449} 8450 8451 8452int 8453ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8454{ 8455 int retval; 8456 u_int32_t param_len; 8457 struct scsi_per_res_out *cdb; 8458 struct ctl_lun *lun; 8459 struct scsi_per_res_out_parms* param; 8460 struct ctl_softc *softc; 8461 uint32_t residx; 8462 uint64_t res_key, sa_res_key, key; 8463 uint8_t type; 8464 union ctl_ha_msg persis_io; 8465 int i; 8466 8467 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8468 8469 retval = CTL_RETVAL_COMPLETE; 8470 8471 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8472 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8473 softc = lun->ctl_softc; 8474 8475 /* 8476 * We only support whole-LUN scope. The scope & type are ignored for 8477 * register, register and ignore existing key and clear. 8478 * We sometimes ignore scope and type on preempts too!! 8479 * Verify reservation type here as well. 8480 */ 8481 type = cdb->scope_type & SPR_TYPE_MASK; 8482 if ((cdb->action == SPRO_RESERVE) 8483 || (cdb->action == SPRO_RELEASE)) { 8484 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8485 ctl_set_invalid_field(/*ctsio*/ ctsio, 8486 /*sks_valid*/ 1, 8487 /*command*/ 1, 8488 /*field*/ 2, 8489 /*bit_valid*/ 1, 8490 /*bit*/ 4); 8491 ctl_done((union ctl_io *)ctsio); 8492 return (CTL_RETVAL_COMPLETE); 8493 } 8494 8495 if (type>8 || type==2 || type==4 || type==0) { 8496 ctl_set_invalid_field(/*ctsio*/ ctsio, 8497 /*sks_valid*/ 1, 8498 /*command*/ 1, 8499 /*field*/ 2, 8500 /*bit_valid*/ 1, 8501 /*bit*/ 0); 8502 ctl_done((union ctl_io *)ctsio); 8503 return (CTL_RETVAL_COMPLETE); 8504 } 8505 } 8506 8507 param_len = scsi_4btoul(cdb->length); 8508 8509 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8510 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8511 ctsio->kern_data_len = param_len; 8512 ctsio->kern_total_len = param_len; 8513 ctsio->kern_data_resid = 0; 8514 ctsio->kern_rel_offset = 0; 8515 ctsio->kern_sg_entries = 0; 8516 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8517 ctsio->be_move_done = ctl_config_move_done; 8518 ctl_datamove((union ctl_io *)ctsio); 8519 8520 return (CTL_RETVAL_COMPLETE); 8521 } 8522 8523 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8524 8525 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8526 res_key = scsi_8btou64(param->res_key.key); 8527 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8528 8529 /* 8530 * Validate the reservation key here except for SPRO_REG_IGNO 8531 * This must be done for all other service actions 8532 */ 8533 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8534 mtx_lock(&lun->lun_lock); 8535 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8536 if (res_key != key) { 8537 /* 8538 * The current key passed in doesn't match 8539 * the one the initiator previously 8540 * registered. 8541 */ 8542 mtx_unlock(&lun->lun_lock); 8543 free(ctsio->kern_data_ptr, M_CTL); 8544 ctl_set_reservation_conflict(ctsio); 8545 ctl_done((union ctl_io *)ctsio); 8546 return (CTL_RETVAL_COMPLETE); 8547 } 8548 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8549 /* 8550 * We are not registered 8551 */ 8552 mtx_unlock(&lun->lun_lock); 8553 free(ctsio->kern_data_ptr, M_CTL); 8554 ctl_set_reservation_conflict(ctsio); 8555 ctl_done((union ctl_io *)ctsio); 8556 return (CTL_RETVAL_COMPLETE); 8557 } else if (res_key != 0) { 8558 /* 8559 * We are not registered and trying to register but 8560 * the register key isn't zero. 8561 */ 8562 mtx_unlock(&lun->lun_lock); 8563 free(ctsio->kern_data_ptr, M_CTL); 8564 ctl_set_reservation_conflict(ctsio); 8565 ctl_done((union ctl_io *)ctsio); 8566 return (CTL_RETVAL_COMPLETE); 8567 } 8568 mtx_unlock(&lun->lun_lock); 8569 } 8570 8571 switch (cdb->action & SPRO_ACTION_MASK) { 8572 case SPRO_REGISTER: 8573 case SPRO_REG_IGNO: { 8574 8575#if 0 8576 printf("Registration received\n"); 8577#endif 8578 8579 /* 8580 * We don't support any of these options, as we report in 8581 * the read capabilities request (see 8582 * ctl_persistent_reserve_in(), above). 8583 */ 8584 if ((param->flags & SPR_SPEC_I_PT) 8585 || (param->flags & SPR_ALL_TG_PT) 8586 || (param->flags & SPR_APTPL)) { 8587 int bit_ptr; 8588 8589 if (param->flags & SPR_APTPL) 8590 bit_ptr = 0; 8591 else if (param->flags & SPR_ALL_TG_PT) 8592 bit_ptr = 2; 8593 else /* SPR_SPEC_I_PT */ 8594 bit_ptr = 3; 8595 8596 free(ctsio->kern_data_ptr, M_CTL); 8597 ctl_set_invalid_field(ctsio, 8598 /*sks_valid*/ 1, 8599 /*command*/ 0, 8600 /*field*/ 20, 8601 /*bit_valid*/ 1, 8602 /*bit*/ bit_ptr); 8603 ctl_done((union ctl_io *)ctsio); 8604 return (CTL_RETVAL_COMPLETE); 8605 } 8606 8607 mtx_lock(&lun->lun_lock); 8608 8609 /* 8610 * The initiator wants to clear the 8611 * key/unregister. 8612 */ 8613 if (sa_res_key == 0) { 8614 if ((res_key == 0 8615 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8616 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8617 && ctl_get_prkey(lun, residx) == 0)) { 8618 mtx_unlock(&lun->lun_lock); 8619 goto done; 8620 } 8621 8622 ctl_clr_prkey(lun, residx); 8623 lun->pr_key_count--; 8624 8625 if (residx == lun->pr_res_idx) { 8626 lun->flags &= ~CTL_LUN_PR_RESERVED; 8627 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8628 8629 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8630 || lun->res_type == SPR_TYPE_EX_AC_RO) 8631 && lun->pr_key_count) { 8632 /* 8633 * If the reservation is a registrants 8634 * only type we need to generate a UA 8635 * for other registered inits. The 8636 * sense code should be RESERVATIONS 8637 * RELEASED 8638 */ 8639 8640 for (i = softc->init_min; i < softc->init_max; i++){ 8641 if (ctl_get_prkey(lun, i) == 0) 8642 continue; 8643 ctl_est_ua(lun, i, 8644 CTL_UA_RES_RELEASE); 8645 } 8646 } 8647 lun->res_type = 0; 8648 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8649 if (lun->pr_key_count==0) { 8650 lun->flags &= ~CTL_LUN_PR_RESERVED; 8651 lun->res_type = 0; 8652 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8653 } 8654 } 8655 lun->PRGeneration++; 8656 mtx_unlock(&lun->lun_lock); 8657 8658 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8659 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8660 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8661 persis_io.pr.pr_info.residx = residx; 8662 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8663 sizeof(persis_io.pr), M_WAITOK); 8664 } else /* sa_res_key != 0 */ { 8665 8666 /* 8667 * If we aren't registered currently then increment 8668 * the key count and set the registered flag. 8669 */ 8670 ctl_alloc_prkey(lun, residx); 8671 if (ctl_get_prkey(lun, residx) == 0) 8672 lun->pr_key_count++; 8673 ctl_set_prkey(lun, residx, sa_res_key); 8674 lun->PRGeneration++; 8675 mtx_unlock(&lun->lun_lock); 8676 8677 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8678 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8679 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8680 persis_io.pr.pr_info.residx = residx; 8681 memcpy(persis_io.pr.pr_info.sa_res_key, 8682 param->serv_act_res_key, 8683 sizeof(param->serv_act_res_key)); 8684 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8685 sizeof(persis_io.pr), M_WAITOK); 8686 } 8687 8688 break; 8689 } 8690 case SPRO_RESERVE: 8691#if 0 8692 printf("Reserve executed type %d\n", type); 8693#endif 8694 mtx_lock(&lun->lun_lock); 8695 if (lun->flags & CTL_LUN_PR_RESERVED) { 8696 /* 8697 * if this isn't the reservation holder and it's 8698 * not a "all registrants" type or if the type is 8699 * different then we have a conflict 8700 */ 8701 if ((lun->pr_res_idx != residx 8702 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8703 || lun->res_type != type) { 8704 mtx_unlock(&lun->lun_lock); 8705 free(ctsio->kern_data_ptr, M_CTL); 8706 ctl_set_reservation_conflict(ctsio); 8707 ctl_done((union ctl_io *)ctsio); 8708 return (CTL_RETVAL_COMPLETE); 8709 } 8710 mtx_unlock(&lun->lun_lock); 8711 } else /* create a reservation */ { 8712 /* 8713 * If it's not an "all registrants" type record 8714 * reservation holder 8715 */ 8716 if (type != SPR_TYPE_WR_EX_AR 8717 && type != SPR_TYPE_EX_AC_AR) 8718 lun->pr_res_idx = residx; /* Res holder */ 8719 else 8720 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8721 8722 lun->flags |= CTL_LUN_PR_RESERVED; 8723 lun->res_type = type; 8724 8725 mtx_unlock(&lun->lun_lock); 8726 8727 /* send msg to other side */ 8728 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8729 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8730 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8731 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8732 persis_io.pr.pr_info.res_type = type; 8733 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8734 sizeof(persis_io.pr), M_WAITOK); 8735 } 8736 break; 8737 8738 case SPRO_RELEASE: 8739 mtx_lock(&lun->lun_lock); 8740 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8741 /* No reservation exists return good status */ 8742 mtx_unlock(&lun->lun_lock); 8743 goto done; 8744 } 8745 /* 8746 * Is this nexus a reservation holder? 8747 */ 8748 if (lun->pr_res_idx != residx 8749 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8750 /* 8751 * not a res holder return good status but 8752 * do nothing 8753 */ 8754 mtx_unlock(&lun->lun_lock); 8755 goto done; 8756 } 8757 8758 if (lun->res_type != type) { 8759 mtx_unlock(&lun->lun_lock); 8760 free(ctsio->kern_data_ptr, M_CTL); 8761 ctl_set_illegal_pr_release(ctsio); 8762 ctl_done((union ctl_io *)ctsio); 8763 return (CTL_RETVAL_COMPLETE); 8764 } 8765 8766 /* okay to release */ 8767 lun->flags &= ~CTL_LUN_PR_RESERVED; 8768 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8769 lun->res_type = 0; 8770 8771 /* 8772 * if this isn't an exclusive access 8773 * res generate UA for all other 8774 * registrants. 8775 */ 8776 if (type != SPR_TYPE_EX_AC 8777 && type != SPR_TYPE_WR_EX) { 8778 for (i = softc->init_min; i < softc->init_max; i++) { 8779 if (i == residx || ctl_get_prkey(lun, i) == 0) 8780 continue; 8781 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8782 } 8783 } 8784 mtx_unlock(&lun->lun_lock); 8785 8786 /* Send msg to other side */ 8787 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8788 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8789 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8790 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8791 sizeof(persis_io.pr), M_WAITOK); 8792 break; 8793 8794 case SPRO_CLEAR: 8795 /* send msg to other side */ 8796 8797 mtx_lock(&lun->lun_lock); 8798 lun->flags &= ~CTL_LUN_PR_RESERVED; 8799 lun->res_type = 0; 8800 lun->pr_key_count = 0; 8801 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8802 8803 ctl_clr_prkey(lun, residx); 8804 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8805 if (ctl_get_prkey(lun, i) != 0) { 8806 ctl_clr_prkey(lun, i); 8807 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8808 } 8809 lun->PRGeneration++; 8810 mtx_unlock(&lun->lun_lock); 8811 8812 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8813 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8814 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8815 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8816 sizeof(persis_io.pr), M_WAITOK); 8817 break; 8818 8819 case SPRO_PREEMPT: 8820 case SPRO_PRE_ABO: { 8821 int nretval; 8822 8823 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8824 residx, ctsio, cdb, param); 8825 if (nretval != 0) 8826 return (CTL_RETVAL_COMPLETE); 8827 break; 8828 } 8829 default: 8830 panic("Invalid PR type %x", cdb->action); 8831 } 8832 8833done: 8834 free(ctsio->kern_data_ptr, M_CTL); 8835 ctl_set_success(ctsio); 8836 ctl_done((union ctl_io *)ctsio); 8837 8838 return (retval); 8839} 8840 8841/* 8842 * This routine is for handling a message from the other SC pertaining to 8843 * persistent reserve out. All the error checking will have been done 8844 * so only perorming the action need be done here to keep the two 8845 * in sync. 8846 */ 8847static void 8848ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8849{ 8850 struct ctl_softc *softc = control_softc; 8851 struct ctl_lun *lun; 8852 int i; 8853 uint32_t residx, targ_lun; 8854 8855 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8856 mtx_lock(&softc->ctl_lock); 8857 if ((targ_lun >= CTL_MAX_LUNS) || 8858 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 8859 mtx_unlock(&softc->ctl_lock); 8860 return; 8861 } 8862 mtx_lock(&lun->lun_lock); 8863 mtx_unlock(&softc->ctl_lock); 8864 if (lun->flags & CTL_LUN_DISABLED) { 8865 mtx_unlock(&lun->lun_lock); 8866 return; 8867 } 8868 residx = ctl_get_initindex(&msg->hdr.nexus); 8869 switch(msg->pr.pr_info.action) { 8870 case CTL_PR_REG_KEY: 8871 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8872 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8873 lun->pr_key_count++; 8874 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8875 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8876 lun->PRGeneration++; 8877 break; 8878 8879 case CTL_PR_UNREG_KEY: 8880 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8881 lun->pr_key_count--; 8882 8883 /* XXX Need to see if the reservation has been released */ 8884 /* if so do we need to generate UA? */ 8885 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8886 lun->flags &= ~CTL_LUN_PR_RESERVED; 8887 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8888 8889 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8890 || lun->res_type == SPR_TYPE_EX_AC_RO) 8891 && lun->pr_key_count) { 8892 /* 8893 * If the reservation is a registrants 8894 * only type we need to generate a UA 8895 * for other registered inits. The 8896 * sense code should be RESERVATIONS 8897 * RELEASED 8898 */ 8899 8900 for (i = softc->init_min; i < softc->init_max; i++) { 8901 if (ctl_get_prkey(lun, i) == 0) 8902 continue; 8903 8904 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8905 } 8906 } 8907 lun->res_type = 0; 8908 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8909 if (lun->pr_key_count==0) { 8910 lun->flags &= ~CTL_LUN_PR_RESERVED; 8911 lun->res_type = 0; 8912 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8913 } 8914 } 8915 lun->PRGeneration++; 8916 break; 8917 8918 case CTL_PR_RESERVE: 8919 lun->flags |= CTL_LUN_PR_RESERVED; 8920 lun->res_type = msg->pr.pr_info.res_type; 8921 lun->pr_res_idx = msg->pr.pr_info.residx; 8922 8923 break; 8924 8925 case CTL_PR_RELEASE: 8926 /* 8927 * if this isn't an exclusive access res generate UA for all 8928 * other registrants. 8929 */ 8930 if (lun->res_type != SPR_TYPE_EX_AC 8931 && lun->res_type != SPR_TYPE_WR_EX) { 8932 for (i = softc->init_min; i < softc->init_max; i++) 8933 if (i == residx || ctl_get_prkey(lun, i) == 0) 8934 continue; 8935 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8936 } 8937 8938 lun->flags &= ~CTL_LUN_PR_RESERVED; 8939 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8940 lun->res_type = 0; 8941 break; 8942 8943 case CTL_PR_PREEMPT: 8944 ctl_pro_preempt_other(lun, msg); 8945 break; 8946 case CTL_PR_CLEAR: 8947 lun->flags &= ~CTL_LUN_PR_RESERVED; 8948 lun->res_type = 0; 8949 lun->pr_key_count = 0; 8950 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8951 8952 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8953 if (ctl_get_prkey(lun, i) == 0) 8954 continue; 8955 ctl_clr_prkey(lun, i); 8956 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8957 } 8958 lun->PRGeneration++; 8959 break; 8960 } 8961 8962 mtx_unlock(&lun->lun_lock); 8963} 8964 8965int 8966ctl_read_write(struct ctl_scsiio *ctsio) 8967{ 8968 struct ctl_lun *lun; 8969 struct ctl_lba_len_flags *lbalen; 8970 uint64_t lba; 8971 uint32_t num_blocks; 8972 int flags, retval; 8973 int isread; 8974 8975 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8976 8977 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8978 8979 flags = 0; 8980 retval = CTL_RETVAL_COMPLETE; 8981 8982 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8983 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8984 switch (ctsio->cdb[0]) { 8985 case READ_6: 8986 case WRITE_6: { 8987 struct scsi_rw_6 *cdb; 8988 8989 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8990 8991 lba = scsi_3btoul(cdb->addr); 8992 /* only 5 bits are valid in the most significant address byte */ 8993 lba &= 0x1fffff; 8994 num_blocks = cdb->length; 8995 /* 8996 * This is correct according to SBC-2. 8997 */ 8998 if (num_blocks == 0) 8999 num_blocks = 256; 9000 break; 9001 } 9002 case READ_10: 9003 case WRITE_10: { 9004 struct scsi_rw_10 *cdb; 9005 9006 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9007 if (cdb->byte2 & SRW10_FUA) 9008 flags |= CTL_LLF_FUA; 9009 if (cdb->byte2 & SRW10_DPO) 9010 flags |= CTL_LLF_DPO; 9011 lba = scsi_4btoul(cdb->addr); 9012 num_blocks = scsi_2btoul(cdb->length); 9013 break; 9014 } 9015 case WRITE_VERIFY_10: { 9016 struct scsi_write_verify_10 *cdb; 9017 9018 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9019 flags |= CTL_LLF_FUA; 9020 if (cdb->byte2 & SWV_DPO) 9021 flags |= CTL_LLF_DPO; 9022 lba = scsi_4btoul(cdb->addr); 9023 num_blocks = scsi_2btoul(cdb->length); 9024 break; 9025 } 9026 case READ_12: 9027 case WRITE_12: { 9028 struct scsi_rw_12 *cdb; 9029 9030 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9031 if (cdb->byte2 & SRW12_FUA) 9032 flags |= CTL_LLF_FUA; 9033 if (cdb->byte2 & SRW12_DPO) 9034 flags |= CTL_LLF_DPO; 9035 lba = scsi_4btoul(cdb->addr); 9036 num_blocks = scsi_4btoul(cdb->length); 9037 break; 9038 } 9039 case WRITE_VERIFY_12: { 9040 struct scsi_write_verify_12 *cdb; 9041 9042 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9043 flags |= CTL_LLF_FUA; 9044 if (cdb->byte2 & SWV_DPO) 9045 flags |= CTL_LLF_DPO; 9046 lba = scsi_4btoul(cdb->addr); 9047 num_blocks = scsi_4btoul(cdb->length); 9048 break; 9049 } 9050 case READ_16: 9051 case WRITE_16: { 9052 struct scsi_rw_16 *cdb; 9053 9054 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9055 if (cdb->byte2 & SRW12_FUA) 9056 flags |= CTL_LLF_FUA; 9057 if (cdb->byte2 & SRW12_DPO) 9058 flags |= CTL_LLF_DPO; 9059 lba = scsi_8btou64(cdb->addr); 9060 num_blocks = scsi_4btoul(cdb->length); 9061 break; 9062 } 9063 case WRITE_ATOMIC_16: { 9064 struct scsi_write_atomic_16 *cdb; 9065 9066 if (lun->be_lun->atomicblock == 0) { 9067 ctl_set_invalid_opcode(ctsio); 9068 ctl_done((union ctl_io *)ctsio); 9069 return (CTL_RETVAL_COMPLETE); 9070 } 9071 9072 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 9073 if (cdb->byte2 & SRW12_FUA) 9074 flags |= CTL_LLF_FUA; 9075 if (cdb->byte2 & SRW12_DPO) 9076 flags |= CTL_LLF_DPO; 9077 lba = scsi_8btou64(cdb->addr); 9078 num_blocks = scsi_2btoul(cdb->length); 9079 if (num_blocks > lun->be_lun->atomicblock) { 9080 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 9081 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 9082 /*bit*/ 0); 9083 ctl_done((union ctl_io *)ctsio); 9084 return (CTL_RETVAL_COMPLETE); 9085 } 9086 break; 9087 } 9088 case WRITE_VERIFY_16: { 9089 struct scsi_write_verify_16 *cdb; 9090 9091 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9092 flags |= CTL_LLF_FUA; 9093 if (cdb->byte2 & SWV_DPO) 9094 flags |= CTL_LLF_DPO; 9095 lba = scsi_8btou64(cdb->addr); 9096 num_blocks = scsi_4btoul(cdb->length); 9097 break; 9098 } 9099 default: 9100 /* 9101 * We got a command we don't support. This shouldn't 9102 * happen, commands should be filtered out above us. 9103 */ 9104 ctl_set_invalid_opcode(ctsio); 9105 ctl_done((union ctl_io *)ctsio); 9106 9107 return (CTL_RETVAL_COMPLETE); 9108 break; /* NOTREACHED */ 9109 } 9110 9111 /* 9112 * The first check is to make sure we're in bounds, the second 9113 * check is to catch wrap-around problems. If the lba + num blocks 9114 * is less than the lba, then we've wrapped around and the block 9115 * range is invalid anyway. 9116 */ 9117 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9118 || ((lba + num_blocks) < lba)) { 9119 ctl_set_lba_out_of_range(ctsio); 9120 ctl_done((union ctl_io *)ctsio); 9121 return (CTL_RETVAL_COMPLETE); 9122 } 9123 9124 /* 9125 * According to SBC-3, a transfer length of 0 is not an error. 9126 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9127 * translates to 256 blocks for those commands. 9128 */ 9129 if (num_blocks == 0) { 9130 ctl_set_success(ctsio); 9131 ctl_done((union ctl_io *)ctsio); 9132 return (CTL_RETVAL_COMPLETE); 9133 } 9134 9135 /* Set FUA and/or DPO if caches are disabled. */ 9136 if (isread) { 9137 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9138 SCP_RCD) != 0) 9139 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9140 } else { 9141 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9142 SCP_WCE) == 0) 9143 flags |= CTL_LLF_FUA; 9144 } 9145 9146 lbalen = (struct ctl_lba_len_flags *) 9147 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9148 lbalen->lba = lba; 9149 lbalen->len = num_blocks; 9150 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9151 9152 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9153 ctsio->kern_rel_offset = 0; 9154 9155 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9156 9157 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9158 9159 return (retval); 9160} 9161 9162static int 9163ctl_cnw_cont(union ctl_io *io) 9164{ 9165 struct ctl_scsiio *ctsio; 9166 struct ctl_lun *lun; 9167 struct ctl_lba_len_flags *lbalen; 9168 int retval; 9169 9170 ctsio = &io->scsiio; 9171 ctsio->io_hdr.status = CTL_STATUS_NONE; 9172 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9173 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9174 lbalen = (struct ctl_lba_len_flags *) 9175 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9176 lbalen->flags &= ~CTL_LLF_COMPARE; 9177 lbalen->flags |= CTL_LLF_WRITE; 9178 9179 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9180 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9181 return (retval); 9182} 9183 9184int 9185ctl_cnw(struct ctl_scsiio *ctsio) 9186{ 9187 struct ctl_lun *lun; 9188 struct ctl_lba_len_flags *lbalen; 9189 uint64_t lba; 9190 uint32_t num_blocks; 9191 int flags, retval; 9192 9193 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9194 9195 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9196 9197 flags = 0; 9198 retval = CTL_RETVAL_COMPLETE; 9199 9200 switch (ctsio->cdb[0]) { 9201 case COMPARE_AND_WRITE: { 9202 struct scsi_compare_and_write *cdb; 9203 9204 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9205 if (cdb->byte2 & SRW10_FUA) 9206 flags |= CTL_LLF_FUA; 9207 if (cdb->byte2 & SRW10_DPO) 9208 flags |= CTL_LLF_DPO; 9209 lba = scsi_8btou64(cdb->addr); 9210 num_blocks = cdb->length; 9211 break; 9212 } 9213 default: 9214 /* 9215 * We got a command we don't support. This shouldn't 9216 * happen, commands should be filtered out above us. 9217 */ 9218 ctl_set_invalid_opcode(ctsio); 9219 ctl_done((union ctl_io *)ctsio); 9220 9221 return (CTL_RETVAL_COMPLETE); 9222 break; /* NOTREACHED */ 9223 } 9224 9225 /* 9226 * The first check is to make sure we're in bounds, the second 9227 * check is to catch wrap-around problems. If the lba + num blocks 9228 * is less than the lba, then we've wrapped around and the block 9229 * range is invalid anyway. 9230 */ 9231 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9232 || ((lba + num_blocks) < lba)) { 9233 ctl_set_lba_out_of_range(ctsio); 9234 ctl_done((union ctl_io *)ctsio); 9235 return (CTL_RETVAL_COMPLETE); 9236 } 9237 9238 /* 9239 * According to SBC-3, a transfer length of 0 is not an error. 9240 */ 9241 if (num_blocks == 0) { 9242 ctl_set_success(ctsio); 9243 ctl_done((union ctl_io *)ctsio); 9244 return (CTL_RETVAL_COMPLETE); 9245 } 9246 9247 /* Set FUA if write cache is disabled. */ 9248 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9249 SCP_WCE) == 0) 9250 flags |= CTL_LLF_FUA; 9251 9252 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9253 ctsio->kern_rel_offset = 0; 9254 9255 /* 9256 * Set the IO_CONT flag, so that if this I/O gets passed to 9257 * ctl_data_submit_done(), it'll get passed back to 9258 * ctl_ctl_cnw_cont() for further processing. 9259 */ 9260 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9261 ctsio->io_cont = ctl_cnw_cont; 9262 9263 lbalen = (struct ctl_lba_len_flags *) 9264 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9265 lbalen->lba = lba; 9266 lbalen->len = num_blocks; 9267 lbalen->flags = CTL_LLF_COMPARE | flags; 9268 9269 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9270 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9271 return (retval); 9272} 9273 9274int 9275ctl_verify(struct ctl_scsiio *ctsio) 9276{ 9277 struct ctl_lun *lun; 9278 struct ctl_lba_len_flags *lbalen; 9279 uint64_t lba; 9280 uint32_t num_blocks; 9281 int bytchk, flags; 9282 int retval; 9283 9284 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9285 9286 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9287 9288 bytchk = 0; 9289 flags = CTL_LLF_FUA; 9290 retval = CTL_RETVAL_COMPLETE; 9291 9292 switch (ctsio->cdb[0]) { 9293 case VERIFY_10: { 9294 struct scsi_verify_10 *cdb; 9295 9296 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9297 if (cdb->byte2 & SVFY_BYTCHK) 9298 bytchk = 1; 9299 if (cdb->byte2 & SVFY_DPO) 9300 flags |= CTL_LLF_DPO; 9301 lba = scsi_4btoul(cdb->addr); 9302 num_blocks = scsi_2btoul(cdb->length); 9303 break; 9304 } 9305 case VERIFY_12: { 9306 struct scsi_verify_12 *cdb; 9307 9308 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9309 if (cdb->byte2 & SVFY_BYTCHK) 9310 bytchk = 1; 9311 if (cdb->byte2 & SVFY_DPO) 9312 flags |= CTL_LLF_DPO; 9313 lba = scsi_4btoul(cdb->addr); 9314 num_blocks = scsi_4btoul(cdb->length); 9315 break; 9316 } 9317 case VERIFY_16: { 9318 struct scsi_rw_16 *cdb; 9319 9320 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9321 if (cdb->byte2 & SVFY_BYTCHK) 9322 bytchk = 1; 9323 if (cdb->byte2 & SVFY_DPO) 9324 flags |= CTL_LLF_DPO; 9325 lba = scsi_8btou64(cdb->addr); 9326 num_blocks = scsi_4btoul(cdb->length); 9327 break; 9328 } 9329 default: 9330 /* 9331 * We got a command we don't support. This shouldn't 9332 * happen, commands should be filtered out above us. 9333 */ 9334 ctl_set_invalid_opcode(ctsio); 9335 ctl_done((union ctl_io *)ctsio); 9336 return (CTL_RETVAL_COMPLETE); 9337 } 9338 9339 /* 9340 * The first check is to make sure we're in bounds, the second 9341 * check is to catch wrap-around problems. If the lba + num blocks 9342 * is less than the lba, then we've wrapped around and the block 9343 * range is invalid anyway. 9344 */ 9345 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9346 || ((lba + num_blocks) < lba)) { 9347 ctl_set_lba_out_of_range(ctsio); 9348 ctl_done((union ctl_io *)ctsio); 9349 return (CTL_RETVAL_COMPLETE); 9350 } 9351 9352 /* 9353 * According to SBC-3, a transfer length of 0 is not an error. 9354 */ 9355 if (num_blocks == 0) { 9356 ctl_set_success(ctsio); 9357 ctl_done((union ctl_io *)ctsio); 9358 return (CTL_RETVAL_COMPLETE); 9359 } 9360 9361 lbalen = (struct ctl_lba_len_flags *) 9362 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9363 lbalen->lba = lba; 9364 lbalen->len = num_blocks; 9365 if (bytchk) { 9366 lbalen->flags = CTL_LLF_COMPARE | flags; 9367 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9368 } else { 9369 lbalen->flags = CTL_LLF_VERIFY | flags; 9370 ctsio->kern_total_len = 0; 9371 } 9372 ctsio->kern_rel_offset = 0; 9373 9374 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9375 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9376 return (retval); 9377} 9378 9379int 9380ctl_report_luns(struct ctl_scsiio *ctsio) 9381{ 9382 struct ctl_softc *softc; 9383 struct scsi_report_luns *cdb; 9384 struct scsi_report_luns_data *lun_data; 9385 struct ctl_lun *lun, *request_lun; 9386 struct ctl_port *port; 9387 int num_luns, retval; 9388 uint32_t alloc_len, lun_datalen; 9389 int num_filled; 9390 uint32_t initidx, targ_lun_id, lun_id; 9391 9392 retval = CTL_RETVAL_COMPLETE; 9393 cdb = (struct scsi_report_luns *)ctsio->cdb; 9394 port = ctl_io_port(&ctsio->io_hdr); 9395 softc = port->ctl_softc; 9396 9397 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9398 9399 mtx_lock(&softc->ctl_lock); 9400 num_luns = 0; 9401 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 9402 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 9403 num_luns++; 9404 } 9405 mtx_unlock(&softc->ctl_lock); 9406 9407 switch (cdb->select_report) { 9408 case RPL_REPORT_DEFAULT: 9409 case RPL_REPORT_ALL: 9410 case RPL_REPORT_NONSUBSID: 9411 break; 9412 case RPL_REPORT_WELLKNOWN: 9413 case RPL_REPORT_ADMIN: 9414 case RPL_REPORT_CONGLOM: 9415 num_luns = 0; 9416 break; 9417 default: 9418 ctl_set_invalid_field(ctsio, 9419 /*sks_valid*/ 1, 9420 /*command*/ 1, 9421 /*field*/ 2, 9422 /*bit_valid*/ 0, 9423 /*bit*/ 0); 9424 ctl_done((union ctl_io *)ctsio); 9425 return (retval); 9426 break; /* NOTREACHED */ 9427 } 9428 9429 alloc_len = scsi_4btoul(cdb->length); 9430 /* 9431 * The initiator has to allocate at least 16 bytes for this request, 9432 * so he can at least get the header and the first LUN. Otherwise 9433 * we reject the request (per SPC-3 rev 14, section 6.21). 9434 */ 9435 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9436 sizeof(struct scsi_report_luns_lundata))) { 9437 ctl_set_invalid_field(ctsio, 9438 /*sks_valid*/ 1, 9439 /*command*/ 1, 9440 /*field*/ 6, 9441 /*bit_valid*/ 0, 9442 /*bit*/ 0); 9443 ctl_done((union ctl_io *)ctsio); 9444 return (retval); 9445 } 9446 9447 request_lun = (struct ctl_lun *) 9448 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9449 9450 lun_datalen = sizeof(*lun_data) + 9451 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9452 9453 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9454 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9455 ctsio->kern_sg_entries = 0; 9456 9457 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9458 9459 mtx_lock(&softc->ctl_lock); 9460 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9461 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9462 if (lun_id >= CTL_MAX_LUNS) 9463 continue; 9464 lun = softc->ctl_luns[lun_id]; 9465 if (lun == NULL) 9466 continue; 9467 9468 if (targ_lun_id <= 0xff) { 9469 /* 9470 * Peripheral addressing method, bus number 0. 9471 */ 9472 lun_data->luns[num_filled].lundata[0] = 9473 RPL_LUNDATA_ATYP_PERIPH; 9474 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9475 num_filled++; 9476 } else if (targ_lun_id <= 0x3fff) { 9477 /* 9478 * Flat addressing method. 9479 */ 9480 lun_data->luns[num_filled].lundata[0] = 9481 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9482 lun_data->luns[num_filled].lundata[1] = 9483 (targ_lun_id & 0xff); 9484 num_filled++; 9485 } else if (targ_lun_id <= 0xffffff) { 9486 /* 9487 * Extended flat addressing method. 9488 */ 9489 lun_data->luns[num_filled].lundata[0] = 9490 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9491 scsi_ulto3b(targ_lun_id, 9492 &lun_data->luns[num_filled].lundata[1]); 9493 num_filled++; 9494 } else { 9495 printf("ctl_report_luns: bogus LUN number %jd, " 9496 "skipping\n", (intmax_t)targ_lun_id); 9497 } 9498 /* 9499 * According to SPC-3, rev 14 section 6.21: 9500 * 9501 * "The execution of a REPORT LUNS command to any valid and 9502 * installed logical unit shall clear the REPORTED LUNS DATA 9503 * HAS CHANGED unit attention condition for all logical 9504 * units of that target with respect to the requesting 9505 * initiator. A valid and installed logical unit is one 9506 * having a PERIPHERAL QUALIFIER of 000b in the standard 9507 * INQUIRY data (see 6.4.2)." 9508 * 9509 * If request_lun is NULL, the LUN this report luns command 9510 * was issued to is either disabled or doesn't exist. In that 9511 * case, we shouldn't clear any pending lun change unit 9512 * attention. 9513 */ 9514 if (request_lun != NULL) { 9515 mtx_lock(&lun->lun_lock); 9516 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9517 mtx_unlock(&lun->lun_lock); 9518 } 9519 } 9520 mtx_unlock(&softc->ctl_lock); 9521 9522 /* 9523 * It's quite possible that we've returned fewer LUNs than we allocated 9524 * space for. Trim it. 9525 */ 9526 lun_datalen = sizeof(*lun_data) + 9527 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9528 9529 if (lun_datalen < alloc_len) { 9530 ctsio->residual = alloc_len - lun_datalen; 9531 ctsio->kern_data_len = lun_datalen; 9532 ctsio->kern_total_len = lun_datalen; 9533 } else { 9534 ctsio->residual = 0; 9535 ctsio->kern_data_len = alloc_len; 9536 ctsio->kern_total_len = alloc_len; 9537 } 9538 ctsio->kern_data_resid = 0; 9539 ctsio->kern_rel_offset = 0; 9540 ctsio->kern_sg_entries = 0; 9541 9542 /* 9543 * We set this to the actual data length, regardless of how much 9544 * space we actually have to return results. If the user looks at 9545 * this value, he'll know whether or not he allocated enough space 9546 * and reissue the command if necessary. We don't support well 9547 * known logical units, so if the user asks for that, return none. 9548 */ 9549 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9550 9551 /* 9552 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9553 * this request. 9554 */ 9555 ctl_set_success(ctsio); 9556 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9557 ctsio->be_move_done = ctl_config_move_done; 9558 ctl_datamove((union ctl_io *)ctsio); 9559 return (retval); 9560} 9561 9562int 9563ctl_request_sense(struct ctl_scsiio *ctsio) 9564{ 9565 struct scsi_request_sense *cdb; 9566 struct scsi_sense_data *sense_ptr; 9567 struct ctl_softc *ctl_softc; 9568 struct ctl_lun *lun; 9569 uint32_t initidx; 9570 int have_error; 9571 scsi_sense_data_type sense_format; 9572 ctl_ua_type ua_type; 9573 9574 cdb = (struct scsi_request_sense *)ctsio->cdb; 9575 9576 ctl_softc = control_softc; 9577 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9578 9579 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9580 9581 /* 9582 * Determine which sense format the user wants. 9583 */ 9584 if (cdb->byte2 & SRS_DESC) 9585 sense_format = SSD_TYPE_DESC; 9586 else 9587 sense_format = SSD_TYPE_FIXED; 9588 9589 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9590 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9591 ctsio->kern_sg_entries = 0; 9592 9593 /* 9594 * struct scsi_sense_data, which is currently set to 256 bytes, is 9595 * larger than the largest allowed value for the length field in the 9596 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9597 */ 9598 ctsio->residual = 0; 9599 ctsio->kern_data_len = cdb->length; 9600 ctsio->kern_total_len = cdb->length; 9601 9602 ctsio->kern_data_resid = 0; 9603 ctsio->kern_rel_offset = 0; 9604 ctsio->kern_sg_entries = 0; 9605 9606 /* 9607 * If we don't have a LUN, we don't have any pending sense. 9608 */ 9609 if (lun == NULL) 9610 goto no_sense; 9611 9612 have_error = 0; 9613 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9614 /* 9615 * Check for pending sense, and then for pending unit attentions. 9616 * Pending sense gets returned first, then pending unit attentions. 9617 */ 9618 mtx_lock(&lun->lun_lock); 9619#ifdef CTL_WITH_CA 9620 if (ctl_is_set(lun->have_ca, initidx)) { 9621 scsi_sense_data_type stored_format; 9622 9623 /* 9624 * Check to see which sense format was used for the stored 9625 * sense data. 9626 */ 9627 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9628 9629 /* 9630 * If the user requested a different sense format than the 9631 * one we stored, then we need to convert it to the other 9632 * format. If we're going from descriptor to fixed format 9633 * sense data, we may lose things in translation, depending 9634 * on what options were used. 9635 * 9636 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9637 * for some reason we'll just copy it out as-is. 9638 */ 9639 if ((stored_format == SSD_TYPE_FIXED) 9640 && (sense_format == SSD_TYPE_DESC)) 9641 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9642 &lun->pending_sense[initidx], 9643 (struct scsi_sense_data_desc *)sense_ptr); 9644 else if ((stored_format == SSD_TYPE_DESC) 9645 && (sense_format == SSD_TYPE_FIXED)) 9646 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9647 &lun->pending_sense[initidx], 9648 (struct scsi_sense_data_fixed *)sense_ptr); 9649 else 9650 memcpy(sense_ptr, &lun->pending_sense[initidx], 9651 MIN(sizeof(*sense_ptr), 9652 sizeof(lun->pending_sense[initidx]))); 9653 9654 ctl_clear_mask(lun->have_ca, initidx); 9655 have_error = 1; 9656 } else 9657#endif 9658 { 9659 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9660 if (ua_type != CTL_UA_NONE) 9661 have_error = 1; 9662 if (ua_type == CTL_UA_LUN_CHANGE) { 9663 mtx_unlock(&lun->lun_lock); 9664 mtx_lock(&ctl_softc->ctl_lock); 9665 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type); 9666 mtx_unlock(&ctl_softc->ctl_lock); 9667 mtx_lock(&lun->lun_lock); 9668 } 9669 9670 } 9671 mtx_unlock(&lun->lun_lock); 9672 9673 /* 9674 * We already have a pending error, return it. 9675 */ 9676 if (have_error != 0) { 9677 /* 9678 * We report the SCSI status as OK, since the status of the 9679 * request sense command itself is OK. 9680 * We report 0 for the sense length, because we aren't doing 9681 * autosense in this case. We're reporting sense as 9682 * parameter data. 9683 */ 9684 ctl_set_success(ctsio); 9685 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9686 ctsio->be_move_done = ctl_config_move_done; 9687 ctl_datamove((union ctl_io *)ctsio); 9688 return (CTL_RETVAL_COMPLETE); 9689 } 9690 9691no_sense: 9692 9693 /* 9694 * No sense information to report, so we report that everything is 9695 * okay. 9696 */ 9697 ctl_set_sense_data(sense_ptr, 9698 lun, 9699 sense_format, 9700 /*current_error*/ 1, 9701 /*sense_key*/ SSD_KEY_NO_SENSE, 9702 /*asc*/ 0x00, 9703 /*ascq*/ 0x00, 9704 SSD_ELEM_NONE); 9705 9706 /* 9707 * We report 0 for the sense length, because we aren't doing 9708 * autosense in this case. We're reporting sense as parameter data. 9709 */ 9710 ctl_set_success(ctsio); 9711 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9712 ctsio->be_move_done = ctl_config_move_done; 9713 ctl_datamove((union ctl_io *)ctsio); 9714 return (CTL_RETVAL_COMPLETE); 9715} 9716 9717int 9718ctl_tur(struct ctl_scsiio *ctsio) 9719{ 9720 9721 CTL_DEBUG_PRINT(("ctl_tur\n")); 9722 9723 ctl_set_success(ctsio); 9724 ctl_done((union ctl_io *)ctsio); 9725 9726 return (CTL_RETVAL_COMPLETE); 9727} 9728 9729/* 9730 * SCSI VPD page 0x00, the Supported VPD Pages page. 9731 */ 9732static int 9733ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9734{ 9735 struct scsi_vpd_supported_pages *pages; 9736 int sup_page_size; 9737 struct ctl_lun *lun; 9738 int p; 9739 9740 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9741 9742 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9743 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9744 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9745 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9746 ctsio->kern_sg_entries = 0; 9747 9748 if (sup_page_size < alloc_len) { 9749 ctsio->residual = alloc_len - sup_page_size; 9750 ctsio->kern_data_len = sup_page_size; 9751 ctsio->kern_total_len = sup_page_size; 9752 } else { 9753 ctsio->residual = 0; 9754 ctsio->kern_data_len = alloc_len; 9755 ctsio->kern_total_len = alloc_len; 9756 } 9757 ctsio->kern_data_resid = 0; 9758 ctsio->kern_rel_offset = 0; 9759 ctsio->kern_sg_entries = 0; 9760 9761 /* 9762 * The control device is always connected. The disk device, on the 9763 * other hand, may not be online all the time. Need to change this 9764 * to figure out whether the disk device is actually online or not. 9765 */ 9766 if (lun != NULL) 9767 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9768 lun->be_lun->lun_type; 9769 else 9770 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9771 9772 p = 0; 9773 /* Supported VPD pages */ 9774 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9775 /* Serial Number */ 9776 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9777 /* Device Identification */ 9778 pages->page_list[p++] = SVPD_DEVICE_ID; 9779 /* Extended INQUIRY Data */ 9780 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9781 /* Mode Page Policy */ 9782 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9783 /* SCSI Ports */ 9784 pages->page_list[p++] = SVPD_SCSI_PORTS; 9785 /* Third-party Copy */ 9786 pages->page_list[p++] = SVPD_SCSI_TPC; 9787 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9788 /* Block limits */ 9789 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9790 /* Block Device Characteristics */ 9791 pages->page_list[p++] = SVPD_BDC; 9792 /* Logical Block Provisioning */ 9793 pages->page_list[p++] = SVPD_LBP; 9794 } 9795 pages->length = p; 9796 9797 ctl_set_success(ctsio); 9798 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9799 ctsio->be_move_done = ctl_config_move_done; 9800 ctl_datamove((union ctl_io *)ctsio); 9801 return (CTL_RETVAL_COMPLETE); 9802} 9803 9804/* 9805 * SCSI VPD page 0x80, the Unit Serial Number page. 9806 */ 9807static int 9808ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9809{ 9810 struct scsi_vpd_unit_serial_number *sn_ptr; 9811 struct ctl_lun *lun; 9812 int data_len; 9813 9814 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9815 9816 data_len = 4 + CTL_SN_LEN; 9817 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9818 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9819 if (data_len < alloc_len) { 9820 ctsio->residual = alloc_len - data_len; 9821 ctsio->kern_data_len = data_len; 9822 ctsio->kern_total_len = data_len; 9823 } else { 9824 ctsio->residual = 0; 9825 ctsio->kern_data_len = alloc_len; 9826 ctsio->kern_total_len = alloc_len; 9827 } 9828 ctsio->kern_data_resid = 0; 9829 ctsio->kern_rel_offset = 0; 9830 ctsio->kern_sg_entries = 0; 9831 9832 /* 9833 * The control device is always connected. The disk device, on the 9834 * other hand, may not be online all the time. Need to change this 9835 * to figure out whether the disk device is actually online or not. 9836 */ 9837 if (lun != NULL) 9838 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9839 lun->be_lun->lun_type; 9840 else 9841 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9842 9843 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9844 sn_ptr->length = CTL_SN_LEN; 9845 /* 9846 * If we don't have a LUN, we just leave the serial number as 9847 * all spaces. 9848 */ 9849 if (lun != NULL) { 9850 strncpy((char *)sn_ptr->serial_num, 9851 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9852 } else 9853 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9854 9855 ctl_set_success(ctsio); 9856 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9857 ctsio->be_move_done = ctl_config_move_done; 9858 ctl_datamove((union ctl_io *)ctsio); 9859 return (CTL_RETVAL_COMPLETE); 9860} 9861 9862 9863/* 9864 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9865 */ 9866static int 9867ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9868{ 9869 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9870 struct ctl_lun *lun; 9871 int data_len; 9872 9873 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9874 9875 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9876 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9877 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)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 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9899 lun->be_lun->lun_type; 9900 else 9901 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9902 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9903 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9904 /* 9905 * We support head of queue, ordered and simple tags. 9906 */ 9907 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9908 /* 9909 * Volatile cache supported. 9910 */ 9911 eid_ptr->flags3 = SVPD_EID_V_SUP; 9912 9913 /* 9914 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9915 * attention for a particular IT nexus on all LUNs once we report 9916 * it to that nexus once. This bit is required as of SPC-4. 9917 */ 9918 eid_ptr->flags4 = SVPD_EID_LUICLT; 9919 9920 /* 9921 * XXX KDM in order to correctly answer this, we would need 9922 * information from the SIM to determine how much sense data it 9923 * can send. So this would really be a path inquiry field, most 9924 * likely. This can be set to a maximum of 252 according to SPC-4, 9925 * but the hardware may or may not be able to support that much. 9926 * 0 just means that the maximum sense data length is not reported. 9927 */ 9928 eid_ptr->max_sense_length = 0; 9929 9930 ctl_set_success(ctsio); 9931 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9932 ctsio->be_move_done = ctl_config_move_done; 9933 ctl_datamove((union ctl_io *)ctsio); 9934 return (CTL_RETVAL_COMPLETE); 9935} 9936 9937static int 9938ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9939{ 9940 struct scsi_vpd_mode_page_policy *mpp_ptr; 9941 struct ctl_lun *lun; 9942 int data_len; 9943 9944 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9945 9946 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9947 sizeof(struct scsi_vpd_mode_page_policy_descr); 9948 9949 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9950 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9951 ctsio->kern_sg_entries = 0; 9952 9953 if (data_len < alloc_len) { 9954 ctsio->residual = alloc_len - data_len; 9955 ctsio->kern_data_len = data_len; 9956 ctsio->kern_total_len = data_len; 9957 } else { 9958 ctsio->residual = 0; 9959 ctsio->kern_data_len = alloc_len; 9960 ctsio->kern_total_len = alloc_len; 9961 } 9962 ctsio->kern_data_resid = 0; 9963 ctsio->kern_rel_offset = 0; 9964 ctsio->kern_sg_entries = 0; 9965 9966 /* 9967 * The control device is always connected. The disk device, on the 9968 * other hand, may not be online all the time. 9969 */ 9970 if (lun != NULL) 9971 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9972 lun->be_lun->lun_type; 9973 else 9974 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9975 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9976 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9977 mpp_ptr->descr[0].page_code = 0x3f; 9978 mpp_ptr->descr[0].subpage_code = 0xff; 9979 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9980 9981 ctl_set_success(ctsio); 9982 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9983 ctsio->be_move_done = ctl_config_move_done; 9984 ctl_datamove((union ctl_io *)ctsio); 9985 return (CTL_RETVAL_COMPLETE); 9986} 9987 9988/* 9989 * SCSI VPD page 0x83, the Device Identification page. 9990 */ 9991static int 9992ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9993{ 9994 struct scsi_vpd_device_id *devid_ptr; 9995 struct scsi_vpd_id_descriptor *desc; 9996 struct ctl_softc *softc; 9997 struct ctl_lun *lun; 9998 struct ctl_port *port; 9999 int data_len; 10000 uint8_t proto; 10001 10002 softc = control_softc; 10003 10004 port = ctl_io_port(&ctsio->io_hdr); 10005 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10006 10007 data_len = sizeof(struct scsi_vpd_device_id) + 10008 sizeof(struct scsi_vpd_id_descriptor) + 10009 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 10010 sizeof(struct scsi_vpd_id_descriptor) + 10011 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 10012 if (lun && lun->lun_devid) 10013 data_len += lun->lun_devid->len; 10014 if (port && port->port_devid) 10015 data_len += port->port_devid->len; 10016 if (port && port->target_devid) 10017 data_len += port->target_devid->len; 10018 10019 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10020 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 10021 ctsio->kern_sg_entries = 0; 10022 10023 if (data_len < alloc_len) { 10024 ctsio->residual = alloc_len - data_len; 10025 ctsio->kern_data_len = data_len; 10026 ctsio->kern_total_len = data_len; 10027 } else { 10028 ctsio->residual = 0; 10029 ctsio->kern_data_len = alloc_len; 10030 ctsio->kern_total_len = alloc_len; 10031 } 10032 ctsio->kern_data_resid = 0; 10033 ctsio->kern_rel_offset = 0; 10034 ctsio->kern_sg_entries = 0; 10035 10036 /* 10037 * The control device is always connected. The disk device, on the 10038 * other hand, may not be online all the time. 10039 */ 10040 if (lun != NULL) 10041 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10042 lun->be_lun->lun_type; 10043 else 10044 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10045 devid_ptr->page_code = SVPD_DEVICE_ID; 10046 scsi_ulto2b(data_len - 4, devid_ptr->length); 10047 10048 if (port && port->port_type == CTL_PORT_FC) 10049 proto = SCSI_PROTO_FC << 4; 10050 else if (port && port->port_type == CTL_PORT_ISCSI) 10051 proto = SCSI_PROTO_ISCSI << 4; 10052 else 10053 proto = SCSI_PROTO_SPI << 4; 10054 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 10055 10056 /* 10057 * We're using a LUN association here. i.e., this device ID is a 10058 * per-LUN identifier. 10059 */ 10060 if (lun && lun->lun_devid) { 10061 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 10062 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10063 lun->lun_devid->len); 10064 } 10065 10066 /* 10067 * This is for the WWPN which is a port association. 10068 */ 10069 if (port && port->port_devid) { 10070 memcpy(desc, port->port_devid->data, port->port_devid->len); 10071 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10072 port->port_devid->len); 10073 } 10074 10075 /* 10076 * This is for the Relative Target Port(type 4h) identifier 10077 */ 10078 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10079 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10080 SVPD_ID_TYPE_RELTARG; 10081 desc->length = 4; 10082 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 10083 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10084 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 10085 10086 /* 10087 * This is for the Target Port Group(type 5h) identifier 10088 */ 10089 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10090 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10091 SVPD_ID_TYPE_TPORTGRP; 10092 desc->length = 4; 10093 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1, 10094 &desc->identifier[2]); 10095 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10096 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10097 10098 /* 10099 * This is for the Target identifier 10100 */ 10101 if (port && port->target_devid) { 10102 memcpy(desc, port->target_devid->data, port->target_devid->len); 10103 } 10104 10105 ctl_set_success(ctsio); 10106 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10107 ctsio->be_move_done = ctl_config_move_done; 10108 ctl_datamove((union ctl_io *)ctsio); 10109 return (CTL_RETVAL_COMPLETE); 10110} 10111 10112static int 10113ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10114{ 10115 struct ctl_softc *softc = control_softc; 10116 struct scsi_vpd_scsi_ports *sp; 10117 struct scsi_vpd_port_designation *pd; 10118 struct scsi_vpd_port_designation_cont *pdc; 10119 struct ctl_lun *lun; 10120 struct ctl_port *port; 10121 int data_len, num_target_ports, iid_len, id_len; 10122 10123 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10124 10125 num_target_ports = 0; 10126 iid_len = 0; 10127 id_len = 0; 10128 mtx_lock(&softc->ctl_lock); 10129 STAILQ_FOREACH(port, &softc->port_list, links) { 10130 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10131 continue; 10132 if (lun != NULL && 10133 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 10134 continue; 10135 num_target_ports++; 10136 if (port->init_devid) 10137 iid_len += port->init_devid->len; 10138 if (port->port_devid) 10139 id_len += port->port_devid->len; 10140 } 10141 mtx_unlock(&softc->ctl_lock); 10142 10143 data_len = sizeof(struct scsi_vpd_scsi_ports) + 10144 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10145 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10146 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10147 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10148 ctsio->kern_sg_entries = 0; 10149 10150 if (data_len < alloc_len) { 10151 ctsio->residual = alloc_len - data_len; 10152 ctsio->kern_data_len = data_len; 10153 ctsio->kern_total_len = data_len; 10154 } else { 10155 ctsio->residual = 0; 10156 ctsio->kern_data_len = alloc_len; 10157 ctsio->kern_total_len = alloc_len; 10158 } 10159 ctsio->kern_data_resid = 0; 10160 ctsio->kern_rel_offset = 0; 10161 ctsio->kern_sg_entries = 0; 10162 10163 /* 10164 * The control device is always connected. The disk device, on the 10165 * other hand, may not be online all the time. Need to change this 10166 * to figure out whether the disk device is actually online or not. 10167 */ 10168 if (lun != NULL) 10169 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10170 lun->be_lun->lun_type; 10171 else 10172 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10173 10174 sp->page_code = SVPD_SCSI_PORTS; 10175 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10176 sp->page_length); 10177 pd = &sp->design[0]; 10178 10179 mtx_lock(&softc->ctl_lock); 10180 STAILQ_FOREACH(port, &softc->port_list, links) { 10181 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10182 continue; 10183 if (lun != NULL && 10184 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 10185 continue; 10186 scsi_ulto2b(port->targ_port, pd->relative_port_id); 10187 if (port->init_devid) { 10188 iid_len = port->init_devid->len; 10189 memcpy(pd->initiator_transportid, 10190 port->init_devid->data, port->init_devid->len); 10191 } else 10192 iid_len = 0; 10193 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10194 pdc = (struct scsi_vpd_port_designation_cont *) 10195 (&pd->initiator_transportid[iid_len]); 10196 if (port->port_devid) { 10197 id_len = port->port_devid->len; 10198 memcpy(pdc->target_port_descriptors, 10199 port->port_devid->data, port->port_devid->len); 10200 } else 10201 id_len = 0; 10202 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10203 pd = (struct scsi_vpd_port_designation *) 10204 ((uint8_t *)pdc->target_port_descriptors + id_len); 10205 } 10206 mtx_unlock(&softc->ctl_lock); 10207 10208 ctl_set_success(ctsio); 10209 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10210 ctsio->be_move_done = ctl_config_move_done; 10211 ctl_datamove((union ctl_io *)ctsio); 10212 return (CTL_RETVAL_COMPLETE); 10213} 10214 10215static int 10216ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10217{ 10218 struct scsi_vpd_block_limits *bl_ptr; 10219 struct ctl_lun *lun; 10220 int bs; 10221 10222 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10223 10224 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10225 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10226 ctsio->kern_sg_entries = 0; 10227 10228 if (sizeof(*bl_ptr) < alloc_len) { 10229 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10230 ctsio->kern_data_len = sizeof(*bl_ptr); 10231 ctsio->kern_total_len = sizeof(*bl_ptr); 10232 } else { 10233 ctsio->residual = 0; 10234 ctsio->kern_data_len = alloc_len; 10235 ctsio->kern_total_len = alloc_len; 10236 } 10237 ctsio->kern_data_resid = 0; 10238 ctsio->kern_rel_offset = 0; 10239 ctsio->kern_sg_entries = 0; 10240 10241 /* 10242 * The control device is always connected. The disk device, on the 10243 * other hand, may not be online all the time. Need to change this 10244 * to figure out whether the disk device is actually online or not. 10245 */ 10246 if (lun != NULL) 10247 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10248 lun->be_lun->lun_type; 10249 else 10250 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10251 10252 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10253 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10254 bl_ptr->max_cmp_write_len = 0xff; 10255 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10256 if (lun != NULL) { 10257 bs = lun->be_lun->blocksize; 10258 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 10259 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10260 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10261 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10262 if (lun->be_lun->ublockexp != 0) { 10263 scsi_ulto4b((1 << lun->be_lun->ublockexp), 10264 bl_ptr->opt_unmap_grain); 10265 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 10266 bl_ptr->unmap_grain_align); 10267 } 10268 } 10269 scsi_ulto4b(lun->be_lun->atomicblock, 10270 bl_ptr->max_atomic_transfer_length); 10271 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10272 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10273 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 10274 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 10275 } 10276 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10277 10278 ctl_set_success(ctsio); 10279 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10280 ctsio->be_move_done = ctl_config_move_done; 10281 ctl_datamove((union ctl_io *)ctsio); 10282 return (CTL_RETVAL_COMPLETE); 10283} 10284 10285static int 10286ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10287{ 10288 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10289 struct ctl_lun *lun; 10290 const char *value; 10291 u_int i; 10292 10293 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10294 10295 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10296 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10297 ctsio->kern_sg_entries = 0; 10298 10299 if (sizeof(*bdc_ptr) < alloc_len) { 10300 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10301 ctsio->kern_data_len = sizeof(*bdc_ptr); 10302 ctsio->kern_total_len = sizeof(*bdc_ptr); 10303 } else { 10304 ctsio->residual = 0; 10305 ctsio->kern_data_len = alloc_len; 10306 ctsio->kern_total_len = alloc_len; 10307 } 10308 ctsio->kern_data_resid = 0; 10309 ctsio->kern_rel_offset = 0; 10310 ctsio->kern_sg_entries = 0; 10311 10312 /* 10313 * The control device is always connected. The disk device, on the 10314 * other hand, may not be online all the time. Need to change this 10315 * to figure out whether the disk device is actually online or not. 10316 */ 10317 if (lun != NULL) 10318 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10319 lun->be_lun->lun_type; 10320 else 10321 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10322 bdc_ptr->page_code = SVPD_BDC; 10323 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10324 if (lun != NULL && 10325 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10326 i = strtol(value, NULL, 0); 10327 else 10328 i = CTL_DEFAULT_ROTATION_RATE; 10329 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10330 if (lun != NULL && 10331 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10332 i = strtol(value, NULL, 0); 10333 else 10334 i = 0; 10335 bdc_ptr->wab_wac_ff = (i & 0x0f); 10336 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10337 10338 ctl_set_success(ctsio); 10339 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10340 ctsio->be_move_done = ctl_config_move_done; 10341 ctl_datamove((union ctl_io *)ctsio); 10342 return (CTL_RETVAL_COMPLETE); 10343} 10344 10345static int 10346ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10347{ 10348 struct scsi_vpd_logical_block_prov *lbp_ptr; 10349 struct ctl_lun *lun; 10350 10351 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10352 10353 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10354 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10355 ctsio->kern_sg_entries = 0; 10356 10357 if (sizeof(*lbp_ptr) < alloc_len) { 10358 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10359 ctsio->kern_data_len = sizeof(*lbp_ptr); 10360 ctsio->kern_total_len = sizeof(*lbp_ptr); 10361 } else { 10362 ctsio->residual = 0; 10363 ctsio->kern_data_len = alloc_len; 10364 ctsio->kern_total_len = alloc_len; 10365 } 10366 ctsio->kern_data_resid = 0; 10367 ctsio->kern_rel_offset = 0; 10368 ctsio->kern_sg_entries = 0; 10369 10370 /* 10371 * The control device is always connected. The disk device, on the 10372 * other hand, may not be online all the time. Need to change this 10373 * to figure out whether the disk device is actually online or not. 10374 */ 10375 if (lun != NULL) 10376 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10377 lun->be_lun->lun_type; 10378 else 10379 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10380 10381 lbp_ptr->page_code = SVPD_LBP; 10382 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10383 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10384 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10385 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10386 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10387 lbp_ptr->prov_type = SVPD_LBP_THIN; 10388 } 10389 10390 ctl_set_success(ctsio); 10391 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10392 ctsio->be_move_done = ctl_config_move_done; 10393 ctl_datamove((union ctl_io *)ctsio); 10394 return (CTL_RETVAL_COMPLETE); 10395} 10396 10397/* 10398 * INQUIRY with the EVPD bit set. 10399 */ 10400static int 10401ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10402{ 10403 struct ctl_lun *lun; 10404 struct scsi_inquiry *cdb; 10405 int alloc_len, retval; 10406 10407 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10408 cdb = (struct scsi_inquiry *)ctsio->cdb; 10409 alloc_len = scsi_2btoul(cdb->length); 10410 10411 switch (cdb->page_code) { 10412 case SVPD_SUPPORTED_PAGES: 10413 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10414 break; 10415 case SVPD_UNIT_SERIAL_NUMBER: 10416 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10417 break; 10418 case SVPD_DEVICE_ID: 10419 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10420 break; 10421 case SVPD_EXTENDED_INQUIRY_DATA: 10422 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10423 break; 10424 case SVPD_MODE_PAGE_POLICY: 10425 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10426 break; 10427 case SVPD_SCSI_PORTS: 10428 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10429 break; 10430 case SVPD_SCSI_TPC: 10431 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10432 break; 10433 case SVPD_BLOCK_LIMITS: 10434 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10435 goto err; 10436 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10437 break; 10438 case SVPD_BDC: 10439 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10440 goto err; 10441 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10442 break; 10443 case SVPD_LBP: 10444 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10445 goto err; 10446 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10447 break; 10448 default: 10449err: 10450 ctl_set_invalid_field(ctsio, 10451 /*sks_valid*/ 1, 10452 /*command*/ 1, 10453 /*field*/ 2, 10454 /*bit_valid*/ 0, 10455 /*bit*/ 0); 10456 ctl_done((union ctl_io *)ctsio); 10457 retval = CTL_RETVAL_COMPLETE; 10458 break; 10459 } 10460 10461 return (retval); 10462} 10463 10464/* 10465 * Standard INQUIRY data. 10466 */ 10467static int 10468ctl_inquiry_std(struct ctl_scsiio *ctsio) 10469{ 10470 struct scsi_inquiry_data *inq_ptr; 10471 struct scsi_inquiry *cdb; 10472 struct ctl_softc *softc = control_softc; 10473 struct ctl_port *port; 10474 struct ctl_lun *lun; 10475 char *val; 10476 uint32_t alloc_len, data_len; 10477 ctl_port_type port_type; 10478 10479 port = ctl_io_port(&ctsio->io_hdr); 10480 port_type = port->port_type; 10481 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10482 port_type = CTL_PORT_SCSI; 10483 10484 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10485 cdb = (struct scsi_inquiry *)ctsio->cdb; 10486 alloc_len = scsi_2btoul(cdb->length); 10487 10488 /* 10489 * We malloc the full inquiry data size here and fill it 10490 * in. If the user only asks for less, we'll give him 10491 * that much. 10492 */ 10493 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10494 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10495 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10496 ctsio->kern_sg_entries = 0; 10497 ctsio->kern_data_resid = 0; 10498 ctsio->kern_rel_offset = 0; 10499 10500 if (data_len < alloc_len) { 10501 ctsio->residual = alloc_len - data_len; 10502 ctsio->kern_data_len = data_len; 10503 ctsio->kern_total_len = data_len; 10504 } else { 10505 ctsio->residual = 0; 10506 ctsio->kern_data_len = alloc_len; 10507 ctsio->kern_total_len = alloc_len; 10508 } 10509 10510 if (lun != NULL) { 10511 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10512 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10513 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10514 lun->be_lun->lun_type; 10515 } else { 10516 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10517 lun->be_lun->lun_type; 10518 } 10519 } else 10520 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10521 10522 /* RMB in byte 2 is 0 */ 10523 inq_ptr->version = SCSI_REV_SPC4; 10524 10525 /* 10526 * According to SAM-3, even if a device only supports a single 10527 * level of LUN addressing, it should still set the HISUP bit: 10528 * 10529 * 4.9.1 Logical unit numbers overview 10530 * 10531 * All logical unit number formats described in this standard are 10532 * hierarchical in structure even when only a single level in that 10533 * hierarchy is used. The HISUP bit shall be set to one in the 10534 * standard INQUIRY data (see SPC-2) when any logical unit number 10535 * format described in this standard is used. Non-hierarchical 10536 * formats are outside the scope of this standard. 10537 * 10538 * Therefore we set the HiSup bit here. 10539 * 10540 * The reponse format is 2, per SPC-3. 10541 */ 10542 inq_ptr->response_format = SID_HiSup | 2; 10543 10544 inq_ptr->additional_length = data_len - 10545 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10546 CTL_DEBUG_PRINT(("additional_length = %d\n", 10547 inq_ptr->additional_length)); 10548 10549 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10550 /* 16 bit addressing */ 10551 if (port_type == CTL_PORT_SCSI) 10552 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10553 /* XXX set the SID_MultiP bit here if we're actually going to 10554 respond on multiple ports */ 10555 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10556 10557 /* 16 bit data bus, synchronous transfers */ 10558 if (port_type == CTL_PORT_SCSI) 10559 inq_ptr->flags = SID_WBus16 | SID_Sync; 10560 /* 10561 * XXX KDM do we want to support tagged queueing on the control 10562 * device at all? 10563 */ 10564 if ((lun == NULL) 10565 || (lun->be_lun->lun_type != T_PROCESSOR)) 10566 inq_ptr->flags |= SID_CmdQue; 10567 /* 10568 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10569 * We have 8 bytes for the vendor name, and 16 bytes for the device 10570 * name and 4 bytes for the revision. 10571 */ 10572 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10573 "vendor")) == NULL) { 10574 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10575 } else { 10576 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10577 strncpy(inq_ptr->vendor, val, 10578 min(sizeof(inq_ptr->vendor), strlen(val))); 10579 } 10580 if (lun == NULL) { 10581 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10582 sizeof(inq_ptr->product)); 10583 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10584 switch (lun->be_lun->lun_type) { 10585 case T_DIRECT: 10586 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10587 sizeof(inq_ptr->product)); 10588 break; 10589 case T_PROCESSOR: 10590 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10591 sizeof(inq_ptr->product)); 10592 break; 10593 default: 10594 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10595 sizeof(inq_ptr->product)); 10596 break; 10597 } 10598 } else { 10599 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10600 strncpy(inq_ptr->product, val, 10601 min(sizeof(inq_ptr->product), strlen(val))); 10602 } 10603 10604 /* 10605 * XXX make this a macro somewhere so it automatically gets 10606 * incremented when we make changes. 10607 */ 10608 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10609 "revision")) == NULL) { 10610 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10611 } else { 10612 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10613 strncpy(inq_ptr->revision, val, 10614 min(sizeof(inq_ptr->revision), strlen(val))); 10615 } 10616 10617 /* 10618 * For parallel SCSI, we support double transition and single 10619 * transition clocking. We also support QAS (Quick Arbitration 10620 * and Selection) and Information Unit transfers on both the 10621 * control and array devices. 10622 */ 10623 if (port_type == CTL_PORT_SCSI) 10624 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10625 SID_SPI_IUS; 10626 10627 /* SAM-5 (no version claimed) */ 10628 scsi_ulto2b(0x00A0, inq_ptr->version1); 10629 /* SPC-4 (no version claimed) */ 10630 scsi_ulto2b(0x0460, inq_ptr->version2); 10631 if (port_type == CTL_PORT_FC) { 10632 /* FCP-2 ANSI INCITS.350:2003 */ 10633 scsi_ulto2b(0x0917, inq_ptr->version3); 10634 } else if (port_type == CTL_PORT_SCSI) { 10635 /* SPI-4 ANSI INCITS.362:200x */ 10636 scsi_ulto2b(0x0B56, inq_ptr->version3); 10637 } else if (port_type == CTL_PORT_ISCSI) { 10638 /* iSCSI (no version claimed) */ 10639 scsi_ulto2b(0x0960, inq_ptr->version3); 10640 } else if (port_type == CTL_PORT_SAS) { 10641 /* SAS (no version claimed) */ 10642 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10643 } 10644 10645 if (lun == NULL) { 10646 /* SBC-4 (no version claimed) */ 10647 scsi_ulto2b(0x0600, inq_ptr->version4); 10648 } else { 10649 switch (lun->be_lun->lun_type) { 10650 case T_DIRECT: 10651 /* SBC-4 (no version claimed) */ 10652 scsi_ulto2b(0x0600, inq_ptr->version4); 10653 break; 10654 case T_PROCESSOR: 10655 default: 10656 break; 10657 } 10658 } 10659 10660 ctl_set_success(ctsio); 10661 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10662 ctsio->be_move_done = ctl_config_move_done; 10663 ctl_datamove((union ctl_io *)ctsio); 10664 return (CTL_RETVAL_COMPLETE); 10665} 10666 10667int 10668ctl_inquiry(struct ctl_scsiio *ctsio) 10669{ 10670 struct scsi_inquiry *cdb; 10671 int retval; 10672 10673 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10674 10675 cdb = (struct scsi_inquiry *)ctsio->cdb; 10676 if (cdb->byte2 & SI_EVPD) 10677 retval = ctl_inquiry_evpd(ctsio); 10678 else if (cdb->page_code == 0) 10679 retval = ctl_inquiry_std(ctsio); 10680 else { 10681 ctl_set_invalid_field(ctsio, 10682 /*sks_valid*/ 1, 10683 /*command*/ 1, 10684 /*field*/ 2, 10685 /*bit_valid*/ 0, 10686 /*bit*/ 0); 10687 ctl_done((union ctl_io *)ctsio); 10688 return (CTL_RETVAL_COMPLETE); 10689 } 10690 10691 return (retval); 10692} 10693 10694/* 10695 * For known CDB types, parse the LBA and length. 10696 */ 10697static int 10698ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10699{ 10700 if (io->io_hdr.io_type != CTL_IO_SCSI) 10701 return (1); 10702 10703 switch (io->scsiio.cdb[0]) { 10704 case COMPARE_AND_WRITE: { 10705 struct scsi_compare_and_write *cdb; 10706 10707 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10708 10709 *lba = scsi_8btou64(cdb->addr); 10710 *len = cdb->length; 10711 break; 10712 } 10713 case READ_6: 10714 case WRITE_6: { 10715 struct scsi_rw_6 *cdb; 10716 10717 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10718 10719 *lba = scsi_3btoul(cdb->addr); 10720 /* only 5 bits are valid in the most significant address byte */ 10721 *lba &= 0x1fffff; 10722 *len = cdb->length; 10723 break; 10724 } 10725 case READ_10: 10726 case WRITE_10: { 10727 struct scsi_rw_10 *cdb; 10728 10729 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10730 10731 *lba = scsi_4btoul(cdb->addr); 10732 *len = scsi_2btoul(cdb->length); 10733 break; 10734 } 10735 case WRITE_VERIFY_10: { 10736 struct scsi_write_verify_10 *cdb; 10737 10738 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10739 10740 *lba = scsi_4btoul(cdb->addr); 10741 *len = scsi_2btoul(cdb->length); 10742 break; 10743 } 10744 case READ_12: 10745 case WRITE_12: { 10746 struct scsi_rw_12 *cdb; 10747 10748 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10749 10750 *lba = scsi_4btoul(cdb->addr); 10751 *len = scsi_4btoul(cdb->length); 10752 break; 10753 } 10754 case WRITE_VERIFY_12: { 10755 struct scsi_write_verify_12 *cdb; 10756 10757 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10758 10759 *lba = scsi_4btoul(cdb->addr); 10760 *len = scsi_4btoul(cdb->length); 10761 break; 10762 } 10763 case READ_16: 10764 case WRITE_16: { 10765 struct scsi_rw_16 *cdb; 10766 10767 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10768 10769 *lba = scsi_8btou64(cdb->addr); 10770 *len = scsi_4btoul(cdb->length); 10771 break; 10772 } 10773 case WRITE_ATOMIC_16: { 10774 struct scsi_write_atomic_16 *cdb; 10775 10776 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10777 10778 *lba = scsi_8btou64(cdb->addr); 10779 *len = scsi_2btoul(cdb->length); 10780 break; 10781 } 10782 case WRITE_VERIFY_16: { 10783 struct scsi_write_verify_16 *cdb; 10784 10785 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10786 10787 *lba = scsi_8btou64(cdb->addr); 10788 *len = scsi_4btoul(cdb->length); 10789 break; 10790 } 10791 case WRITE_SAME_10: { 10792 struct scsi_write_same_10 *cdb; 10793 10794 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10795 10796 *lba = scsi_4btoul(cdb->addr); 10797 *len = scsi_2btoul(cdb->length); 10798 break; 10799 } 10800 case WRITE_SAME_16: { 10801 struct scsi_write_same_16 *cdb; 10802 10803 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10804 10805 *lba = scsi_8btou64(cdb->addr); 10806 *len = scsi_4btoul(cdb->length); 10807 break; 10808 } 10809 case VERIFY_10: { 10810 struct scsi_verify_10 *cdb; 10811 10812 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10813 10814 *lba = scsi_4btoul(cdb->addr); 10815 *len = scsi_2btoul(cdb->length); 10816 break; 10817 } 10818 case VERIFY_12: { 10819 struct scsi_verify_12 *cdb; 10820 10821 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10822 10823 *lba = scsi_4btoul(cdb->addr); 10824 *len = scsi_4btoul(cdb->length); 10825 break; 10826 } 10827 case VERIFY_16: { 10828 struct scsi_verify_16 *cdb; 10829 10830 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10831 10832 *lba = scsi_8btou64(cdb->addr); 10833 *len = scsi_4btoul(cdb->length); 10834 break; 10835 } 10836 case UNMAP: { 10837 *lba = 0; 10838 *len = UINT64_MAX; 10839 break; 10840 } 10841 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10842 struct scsi_get_lba_status *cdb; 10843 10844 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10845 *lba = scsi_8btou64(cdb->addr); 10846 *len = UINT32_MAX; 10847 break; 10848 } 10849 default: 10850 return (1); 10851 break; /* NOTREACHED */ 10852 } 10853 10854 return (0); 10855} 10856 10857static ctl_action 10858ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10859 bool seq) 10860{ 10861 uint64_t endlba1, endlba2; 10862 10863 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10864 endlba2 = lba2 + len2 - 1; 10865 10866 if ((endlba1 < lba2) || (endlba2 < lba1)) 10867 return (CTL_ACTION_PASS); 10868 else 10869 return (CTL_ACTION_BLOCK); 10870} 10871 10872static int 10873ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10874{ 10875 struct ctl_ptr_len_flags *ptrlen; 10876 struct scsi_unmap_desc *buf, *end, *range; 10877 uint64_t lba; 10878 uint32_t len; 10879 10880 /* If not UNMAP -- go other way. */ 10881 if (io->io_hdr.io_type != CTL_IO_SCSI || 10882 io->scsiio.cdb[0] != UNMAP) 10883 return (CTL_ACTION_ERROR); 10884 10885 /* If UNMAP without data -- block and wait for data. */ 10886 ptrlen = (struct ctl_ptr_len_flags *) 10887 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10888 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10889 ptrlen->ptr == NULL) 10890 return (CTL_ACTION_BLOCK); 10891 10892 /* UNMAP with data -- check for collision. */ 10893 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10894 end = buf + ptrlen->len / sizeof(*buf); 10895 for (range = buf; range < end; range++) { 10896 lba = scsi_8btou64(range->lba); 10897 len = scsi_4btoul(range->length); 10898 if ((lba < lba2 + len2) && (lba + len > lba2)) 10899 return (CTL_ACTION_BLOCK); 10900 } 10901 return (CTL_ACTION_PASS); 10902} 10903 10904static ctl_action 10905ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10906{ 10907 uint64_t lba1, lba2; 10908 uint64_t len1, len2; 10909 int retval; 10910 10911 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10912 return (CTL_ACTION_ERROR); 10913 10914 retval = ctl_extent_check_unmap(io1, lba2, len2); 10915 if (retval != CTL_ACTION_ERROR) 10916 return (retval); 10917 10918 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10919 return (CTL_ACTION_ERROR); 10920 10921 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10922 seq = FALSE; 10923 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10924} 10925 10926static ctl_action 10927ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10928{ 10929 uint64_t lba1, lba2; 10930 uint64_t len1, len2; 10931 10932 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10933 return (CTL_ACTION_PASS); 10934 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10935 return (CTL_ACTION_ERROR); 10936 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10937 return (CTL_ACTION_ERROR); 10938 10939 if (lba1 + len1 == lba2) 10940 return (CTL_ACTION_BLOCK); 10941 return (CTL_ACTION_PASS); 10942} 10943 10944static ctl_action 10945ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10946 union ctl_io *ooa_io) 10947{ 10948 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10949 ctl_serialize_action *serialize_row; 10950 10951 /* 10952 * The initiator attempted multiple untagged commands at the same 10953 * time. Can't do that. 10954 */ 10955 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10956 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10957 && ((pending_io->io_hdr.nexus.targ_port == 10958 ooa_io->io_hdr.nexus.targ_port) 10959 && (pending_io->io_hdr.nexus.initid == 10960 ooa_io->io_hdr.nexus.initid)) 10961 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10962 CTL_FLAG_STATUS_SENT)) == 0)) 10963 return (CTL_ACTION_OVERLAP); 10964 10965 /* 10966 * The initiator attempted to send multiple tagged commands with 10967 * the same ID. (It's fine if different initiators have the same 10968 * tag ID.) 10969 * 10970 * Even if all of those conditions are true, we don't kill the I/O 10971 * if the command ahead of us has been aborted. We won't end up 10972 * sending it to the FETD, and it's perfectly legal to resend a 10973 * command with the same tag number as long as the previous 10974 * instance of this tag number has been aborted somehow. 10975 */ 10976 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10977 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10978 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10979 && ((pending_io->io_hdr.nexus.targ_port == 10980 ooa_io->io_hdr.nexus.targ_port) 10981 && (pending_io->io_hdr.nexus.initid == 10982 ooa_io->io_hdr.nexus.initid)) 10983 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10984 CTL_FLAG_STATUS_SENT)) == 0)) 10985 return (CTL_ACTION_OVERLAP_TAG); 10986 10987 /* 10988 * If we get a head of queue tag, SAM-3 says that we should 10989 * immediately execute it. 10990 * 10991 * What happens if this command would normally block for some other 10992 * reason? e.g. a request sense with a head of queue tag 10993 * immediately after a write. Normally that would block, but this 10994 * will result in its getting executed immediately... 10995 * 10996 * We currently return "pass" instead of "skip", so we'll end up 10997 * going through the rest of the queue to check for overlapped tags. 10998 * 10999 * XXX KDM check for other types of blockage first?? 11000 */ 11001 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11002 return (CTL_ACTION_PASS); 11003 11004 /* 11005 * Ordered tags have to block until all items ahead of them 11006 * have completed. If we get called with an ordered tag, we always 11007 * block, if something else is ahead of us in the queue. 11008 */ 11009 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11010 return (CTL_ACTION_BLOCK); 11011 11012 /* 11013 * Simple tags get blocked until all head of queue and ordered tags 11014 * ahead of them have completed. I'm lumping untagged commands in 11015 * with simple tags here. XXX KDM is that the right thing to do? 11016 */ 11017 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11018 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11019 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11020 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11021 return (CTL_ACTION_BLOCK); 11022 11023 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11024 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11025 11026 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11027 11028 switch (serialize_row[pending_entry->seridx]) { 11029 case CTL_SER_BLOCK: 11030 return (CTL_ACTION_BLOCK); 11031 case CTL_SER_EXTENT: 11032 return (ctl_extent_check(ooa_io, pending_io, 11033 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11034 case CTL_SER_EXTENTOPT: 11035 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11036 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11037 return (ctl_extent_check(ooa_io, pending_io, 11038 (lun->be_lun && 11039 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11040 return (CTL_ACTION_PASS); 11041 case CTL_SER_EXTENTSEQ: 11042 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11043 return (ctl_extent_check_seq(ooa_io, pending_io)); 11044 return (CTL_ACTION_PASS); 11045 case CTL_SER_PASS: 11046 return (CTL_ACTION_PASS); 11047 case CTL_SER_BLOCKOPT: 11048 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11049 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11050 return (CTL_ACTION_BLOCK); 11051 return (CTL_ACTION_PASS); 11052 case CTL_SER_SKIP: 11053 return (CTL_ACTION_SKIP); 11054 default: 11055 panic("invalid serialization value %d", 11056 serialize_row[pending_entry->seridx]); 11057 } 11058 11059 return (CTL_ACTION_ERROR); 11060} 11061 11062/* 11063 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11064 * Assumptions: 11065 * - pending_io is generally either incoming, or on the blocked queue 11066 * - starting I/O is the I/O we want to start the check with. 11067 */ 11068static ctl_action 11069ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11070 union ctl_io *starting_io) 11071{ 11072 union ctl_io *ooa_io; 11073 ctl_action action; 11074 11075 mtx_assert(&lun->lun_lock, MA_OWNED); 11076 11077 /* 11078 * Run back along the OOA queue, starting with the current 11079 * blocked I/O and going through every I/O before it on the 11080 * queue. If starting_io is NULL, we'll just end up returning 11081 * CTL_ACTION_PASS. 11082 */ 11083 for (ooa_io = starting_io; ooa_io != NULL; 11084 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11085 ooa_links)){ 11086 11087 /* 11088 * This routine just checks to see whether 11089 * cur_blocked is blocked by ooa_io, which is ahead 11090 * of it in the queue. It doesn't queue/dequeue 11091 * cur_blocked. 11092 */ 11093 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11094 switch (action) { 11095 case CTL_ACTION_BLOCK: 11096 case CTL_ACTION_OVERLAP: 11097 case CTL_ACTION_OVERLAP_TAG: 11098 case CTL_ACTION_SKIP: 11099 case CTL_ACTION_ERROR: 11100 return (action); 11101 break; /* NOTREACHED */ 11102 case CTL_ACTION_PASS: 11103 break; 11104 default: 11105 panic("invalid action %d", action); 11106 break; /* NOTREACHED */ 11107 } 11108 } 11109 11110 return (CTL_ACTION_PASS); 11111} 11112 11113/* 11114 * Assumptions: 11115 * - An I/O has just completed, and has been removed from the per-LUN OOA 11116 * queue, so some items on the blocked queue may now be unblocked. 11117 */ 11118static int 11119ctl_check_blocked(struct ctl_lun *lun) 11120{ 11121 struct ctl_softc *softc = lun->ctl_softc; 11122 union ctl_io *cur_blocked, *next_blocked; 11123 11124 mtx_assert(&lun->lun_lock, MA_OWNED); 11125 11126 /* 11127 * Run forward from the head of the blocked queue, checking each 11128 * entry against the I/Os prior to it on the OOA queue to see if 11129 * there is still any blockage. 11130 * 11131 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11132 * with our removing a variable on it while it is traversing the 11133 * list. 11134 */ 11135 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11136 cur_blocked != NULL; cur_blocked = next_blocked) { 11137 union ctl_io *prev_ooa; 11138 ctl_action action; 11139 11140 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11141 blocked_links); 11142 11143 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11144 ctl_ooaq, ooa_links); 11145 11146 /* 11147 * If cur_blocked happens to be the first item in the OOA 11148 * queue now, prev_ooa will be NULL, and the action 11149 * returned will just be CTL_ACTION_PASS. 11150 */ 11151 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11152 11153 switch (action) { 11154 case CTL_ACTION_BLOCK: 11155 /* Nothing to do here, still blocked */ 11156 break; 11157 case CTL_ACTION_OVERLAP: 11158 case CTL_ACTION_OVERLAP_TAG: 11159 /* 11160 * This shouldn't happen! In theory we've already 11161 * checked this command for overlap... 11162 */ 11163 break; 11164 case CTL_ACTION_PASS: 11165 case CTL_ACTION_SKIP: { 11166 const struct ctl_cmd_entry *entry; 11167 11168 /* 11169 * The skip case shouldn't happen, this transaction 11170 * should have never made it onto the blocked queue. 11171 */ 11172 /* 11173 * This I/O is no longer blocked, we can remove it 11174 * from the blocked queue. Since this is a TAILQ 11175 * (doubly linked list), we can do O(1) removals 11176 * from any place on the list. 11177 */ 11178 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11179 blocked_links); 11180 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11181 11182 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11183 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11184 /* 11185 * Need to send IO back to original side to 11186 * run 11187 */ 11188 union ctl_ha_msg msg_info; 11189 11190 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11191 msg_info.hdr.original_sc = 11192 cur_blocked->io_hdr.original_sc; 11193 msg_info.hdr.serializing_sc = cur_blocked; 11194 msg_info.hdr.msg_type = CTL_MSG_R2R; 11195 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11196 sizeof(msg_info.hdr), M_NOWAIT); 11197 break; 11198 } 11199 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11200 11201 /* 11202 * Check this I/O for LUN state changes that may 11203 * have happened while this command was blocked. 11204 * The LUN state may have been changed by a command 11205 * ahead of us in the queue, so we need to re-check 11206 * for any states that can be caused by SCSI 11207 * commands. 11208 */ 11209 if (ctl_scsiio_lun_check(lun, entry, 11210 &cur_blocked->scsiio) == 0) { 11211 cur_blocked->io_hdr.flags |= 11212 CTL_FLAG_IS_WAS_ON_RTR; 11213 ctl_enqueue_rtr(cur_blocked); 11214 } else 11215 ctl_done(cur_blocked); 11216 break; 11217 } 11218 default: 11219 /* 11220 * This probably shouldn't happen -- we shouldn't 11221 * get CTL_ACTION_ERROR, or anything else. 11222 */ 11223 break; 11224 } 11225 } 11226 11227 return (CTL_RETVAL_COMPLETE); 11228} 11229 11230/* 11231 * This routine (with one exception) checks LUN flags that can be set by 11232 * commands ahead of us in the OOA queue. These flags have to be checked 11233 * when a command initially comes in, and when we pull a command off the 11234 * blocked queue and are preparing to execute it. The reason we have to 11235 * check these flags for commands on the blocked queue is that the LUN 11236 * state may have been changed by a command ahead of us while we're on the 11237 * blocked queue. 11238 * 11239 * Ordering is somewhat important with these checks, so please pay 11240 * careful attention to the placement of any new checks. 11241 */ 11242static int 11243ctl_scsiio_lun_check(struct ctl_lun *lun, 11244 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11245{ 11246 struct ctl_softc *softc = lun->ctl_softc; 11247 int retval; 11248 uint32_t residx; 11249 11250 retval = 0; 11251 11252 mtx_assert(&lun->lun_lock, MA_OWNED); 11253 11254 /* 11255 * If this shelf is a secondary shelf controller, we may have to 11256 * reject some commands disallowed by HA mode and link state. 11257 */ 11258 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11259 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11260 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11261 ctl_set_lun_unavail(ctsio); 11262 retval = 1; 11263 goto bailout; 11264 } 11265 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11266 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11267 ctl_set_lun_transit(ctsio); 11268 retval = 1; 11269 goto bailout; 11270 } 11271 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11272 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11273 ctl_set_lun_standby(ctsio); 11274 retval = 1; 11275 goto bailout; 11276 } 11277 11278 /* The rest of checks are only done on executing side */ 11279 if (softc->ha_mode == CTL_HA_MODE_XFER) 11280 goto bailout; 11281 } 11282 11283 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11284 if (lun->be_lun && 11285 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11286 ctl_set_hw_write_protected(ctsio); 11287 retval = 1; 11288 goto bailout; 11289 } 11290 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11291 .eca_and_aen & SCP_SWP) != 0) { 11292 ctl_set_sense(ctsio, /*current_error*/ 1, 11293 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11294 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11295 retval = 1; 11296 goto bailout; 11297 } 11298 } 11299 11300 /* 11301 * Check for a reservation conflict. If this command isn't allowed 11302 * even on reserved LUNs, and if this initiator isn't the one who 11303 * reserved us, reject the command with a reservation conflict. 11304 */ 11305 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11306 if ((lun->flags & CTL_LUN_RESERVED) 11307 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11308 if (lun->res_idx != residx) { 11309 ctl_set_reservation_conflict(ctsio); 11310 retval = 1; 11311 goto bailout; 11312 } 11313 } 11314 11315 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11316 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11317 /* No reservation or command is allowed. */; 11318 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11319 (lun->res_type == SPR_TYPE_WR_EX || 11320 lun->res_type == SPR_TYPE_WR_EX_RO || 11321 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11322 /* The command is allowed for Write Exclusive resv. */; 11323 } else { 11324 /* 11325 * if we aren't registered or it's a res holder type 11326 * reservation and this isn't the res holder then set a 11327 * conflict. 11328 */ 11329 if (ctl_get_prkey(lun, residx) == 0 11330 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11331 ctl_set_reservation_conflict(ctsio); 11332 retval = 1; 11333 goto bailout; 11334 } 11335 } 11336 11337 if ((lun->flags & CTL_LUN_OFFLINE) 11338 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0)) { 11339 ctl_set_lun_not_ready(ctsio); 11340 retval = 1; 11341 goto bailout; 11342 } 11343 11344 if ((lun->flags & CTL_LUN_STOPPED) 11345 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11346 /* "Logical unit not ready, initializing cmd. required" */ 11347 ctl_set_lun_stopped(ctsio); 11348 retval = 1; 11349 goto bailout; 11350 } 11351 11352 if ((lun->flags & CTL_LUN_INOPERABLE) 11353 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11354 /* "Medium format corrupted" */ 11355 ctl_set_medium_format_corrupted(ctsio); 11356 retval = 1; 11357 goto bailout; 11358 } 11359 11360bailout: 11361 return (retval); 11362} 11363 11364static void 11365ctl_failover_io(union ctl_io *io, int have_lock) 11366{ 11367 ctl_set_busy(&io->scsiio); 11368 ctl_done(io); 11369} 11370 11371static void 11372ctl_failover_lun(struct ctl_lun *lun) 11373{ 11374 struct ctl_softc *softc = lun->ctl_softc; 11375 struct ctl_io_hdr *io, *next_io; 11376 11377 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", lun->lun)); 11378 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11379 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11380 /* We are master */ 11381 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11382 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11383 io->flags |= CTL_FLAG_ABORT; 11384 io->flags |= CTL_FLAG_FAILOVER; 11385 } else { /* This can be only due to DATAMOVE */ 11386 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11387 io->flags &= ~CTL_FLAG_DMA_INPROG; 11388 io->flags |= CTL_FLAG_IO_ACTIVE; 11389 io->port_status = 31340; 11390 ctl_enqueue_isc((union ctl_io *)io); 11391 } 11392 } 11393 /* We are slave */ 11394 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11395 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11396 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11397 io->flags |= CTL_FLAG_FAILOVER; 11398 } else { 11399 ctl_set_busy(&((union ctl_io *)io)-> 11400 scsiio); 11401 ctl_done((union ctl_io *)io); 11402 } 11403 } 11404 } 11405 } else { /* SERIALIZE modes */ 11406 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11407 next_io) { 11408 /* We are master */ 11409 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11410 TAILQ_REMOVE(&lun->blocked_queue, io, 11411 blocked_links); 11412 io->flags &= ~CTL_FLAG_BLOCKED; 11413 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11414 ctl_free_io((union ctl_io *)io); 11415 } 11416 } 11417 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11418 /* We are master */ 11419 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11420 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11421 ctl_free_io((union ctl_io *)io); 11422 } 11423 /* We are slave */ 11424 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11425 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11426 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11427 ctl_set_busy(&((union ctl_io *)io)-> 11428 scsiio); 11429 ctl_done((union ctl_io *)io); 11430 } 11431 } 11432 } 11433 ctl_check_blocked(lun); 11434 } 11435} 11436 11437static int 11438ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11439{ 11440 struct ctl_lun *lun; 11441 const struct ctl_cmd_entry *entry; 11442 uint32_t initidx, targ_lun; 11443 int retval; 11444 11445 retval = 0; 11446 11447 lun = NULL; 11448 11449 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11450 if ((targ_lun < CTL_MAX_LUNS) 11451 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11452 /* 11453 * If the LUN is invalid, pretend that it doesn't exist. 11454 * It will go away as soon as all pending I/O has been 11455 * completed. 11456 */ 11457 mtx_lock(&lun->lun_lock); 11458 if (lun->flags & CTL_LUN_DISABLED) { 11459 mtx_unlock(&lun->lun_lock); 11460 lun = NULL; 11461 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11462 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11463 } else { 11464 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11465 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11466 lun->be_lun; 11467 11468 /* 11469 * Every I/O goes into the OOA queue for a 11470 * particular LUN, and stays there until completion. 11471 */ 11472#ifdef CTL_TIME_IO 11473 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11474 lun->idle_time += getsbinuptime() - 11475 lun->last_busy; 11476 } 11477#endif 11478 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11479 ooa_links); 11480 } 11481 } else { 11482 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11483 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11484 } 11485 11486 /* Get command entry and return error if it is unsuppotyed. */ 11487 entry = ctl_validate_command(ctsio); 11488 if (entry == NULL) { 11489 if (lun) 11490 mtx_unlock(&lun->lun_lock); 11491 return (retval); 11492 } 11493 11494 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11495 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11496 11497 /* 11498 * Check to see whether we can send this command to LUNs that don't 11499 * exist. This should pretty much only be the case for inquiry 11500 * and request sense. Further checks, below, really require having 11501 * a LUN, so we can't really check the command anymore. Just put 11502 * it on the rtr queue. 11503 */ 11504 if (lun == NULL) { 11505 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11506 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11507 ctl_enqueue_rtr((union ctl_io *)ctsio); 11508 return (retval); 11509 } 11510 11511 ctl_set_unsupported_lun(ctsio); 11512 ctl_done((union ctl_io *)ctsio); 11513 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11514 return (retval); 11515 } else { 11516 /* 11517 * Make sure we support this particular command on this LUN. 11518 * e.g., we don't support writes to the control LUN. 11519 */ 11520 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11521 mtx_unlock(&lun->lun_lock); 11522 ctl_set_invalid_opcode(ctsio); 11523 ctl_done((union ctl_io *)ctsio); 11524 return (retval); 11525 } 11526 } 11527 11528 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11529 11530#ifdef CTL_WITH_CA 11531 /* 11532 * If we've got a request sense, it'll clear the contingent 11533 * allegiance condition. Otherwise, if we have a CA condition for 11534 * this initiator, clear it, because it sent down a command other 11535 * than request sense. 11536 */ 11537 if ((ctsio->cdb[0] != REQUEST_SENSE) 11538 && (ctl_is_set(lun->have_ca, initidx))) 11539 ctl_clear_mask(lun->have_ca, initidx); 11540#endif 11541 11542 /* 11543 * If the command has this flag set, it handles its own unit 11544 * attention reporting, we shouldn't do anything. Otherwise we 11545 * check for any pending unit attentions, and send them back to the 11546 * initiator. We only do this when a command initially comes in, 11547 * not when we pull it off the blocked queue. 11548 * 11549 * According to SAM-3, section 5.3.2, the order that things get 11550 * presented back to the host is basically unit attentions caused 11551 * by some sort of reset event, busy status, reservation conflicts 11552 * or task set full, and finally any other status. 11553 * 11554 * One issue here is that some of the unit attentions we report 11555 * don't fall into the "reset" category (e.g. "reported luns data 11556 * has changed"). So reporting it here, before the reservation 11557 * check, may be technically wrong. I guess the only thing to do 11558 * would be to check for and report the reset events here, and then 11559 * check for the other unit attention types after we check for a 11560 * reservation conflict. 11561 * 11562 * XXX KDM need to fix this 11563 */ 11564 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11565 ctl_ua_type ua_type; 11566 11567 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11568 SSD_TYPE_NONE); 11569 if (ua_type != CTL_UA_NONE) { 11570 mtx_unlock(&lun->lun_lock); 11571 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11572 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11573 ctsio->sense_len = SSD_FULL_SIZE; 11574 ctl_done((union ctl_io *)ctsio); 11575 return (retval); 11576 } 11577 } 11578 11579 11580 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11581 mtx_unlock(&lun->lun_lock); 11582 ctl_done((union ctl_io *)ctsio); 11583 return (retval); 11584 } 11585 11586 /* 11587 * XXX CHD this is where we want to send IO to other side if 11588 * this LUN is secondary on this SC. We will need to make a copy 11589 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11590 * the copy we send as FROM_OTHER. 11591 * We also need to stuff the address of the original IO so we can 11592 * find it easily. Something similar will need be done on the other 11593 * side so when we are done we can find the copy. 11594 */ 11595 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11596 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11597 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11598 union ctl_ha_msg msg_info; 11599 int isc_retval; 11600 11601 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11602 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11603 mtx_unlock(&lun->lun_lock); 11604 11605 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11606 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11607 msg_info.hdr.serializing_sc = NULL; 11608 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11609 msg_info.scsi.tag_num = ctsio->tag_num; 11610 msg_info.scsi.tag_type = ctsio->tag_type; 11611 msg_info.scsi.cdb_len = ctsio->cdb_len; 11612 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11613 11614 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11615 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11616 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11617 ctl_set_busy(ctsio); 11618 ctl_done((union ctl_io *)ctsio); 11619 return (retval); 11620 } 11621 return (retval); 11622 } 11623 11624 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11625 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11626 ctl_ooaq, ooa_links))) { 11627 case CTL_ACTION_BLOCK: 11628 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11629 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11630 blocked_links); 11631 mtx_unlock(&lun->lun_lock); 11632 return (retval); 11633 case CTL_ACTION_PASS: 11634 case CTL_ACTION_SKIP: 11635 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11636 mtx_unlock(&lun->lun_lock); 11637 ctl_enqueue_rtr((union ctl_io *)ctsio); 11638 break; 11639 case CTL_ACTION_OVERLAP: 11640 mtx_unlock(&lun->lun_lock); 11641 ctl_set_overlapped_cmd(ctsio); 11642 ctl_done((union ctl_io *)ctsio); 11643 break; 11644 case CTL_ACTION_OVERLAP_TAG: 11645 mtx_unlock(&lun->lun_lock); 11646 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11647 ctl_done((union ctl_io *)ctsio); 11648 break; 11649 case CTL_ACTION_ERROR: 11650 default: 11651 mtx_unlock(&lun->lun_lock); 11652 ctl_set_internal_failure(ctsio, 11653 /*sks_valid*/ 0, 11654 /*retry_count*/ 0); 11655 ctl_done((union ctl_io *)ctsio); 11656 break; 11657 } 11658 return (retval); 11659} 11660 11661const struct ctl_cmd_entry * 11662ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11663{ 11664 const struct ctl_cmd_entry *entry; 11665 int service_action; 11666 11667 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11668 if (sa) 11669 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11670 if (entry->flags & CTL_CMD_FLAG_SA5) { 11671 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11672 entry = &((const struct ctl_cmd_entry *) 11673 entry->execute)[service_action]; 11674 } 11675 return (entry); 11676} 11677 11678const struct ctl_cmd_entry * 11679ctl_validate_command(struct ctl_scsiio *ctsio) 11680{ 11681 const struct ctl_cmd_entry *entry; 11682 int i, sa; 11683 uint8_t diff; 11684 11685 entry = ctl_get_cmd_entry(ctsio, &sa); 11686 if (entry->execute == NULL) { 11687 if (sa) 11688 ctl_set_invalid_field(ctsio, 11689 /*sks_valid*/ 1, 11690 /*command*/ 1, 11691 /*field*/ 1, 11692 /*bit_valid*/ 1, 11693 /*bit*/ 4); 11694 else 11695 ctl_set_invalid_opcode(ctsio); 11696 ctl_done((union ctl_io *)ctsio); 11697 return (NULL); 11698 } 11699 KASSERT(entry->length > 0, 11700 ("Not defined length for command 0x%02x/0x%02x", 11701 ctsio->cdb[0], ctsio->cdb[1])); 11702 for (i = 1; i < entry->length; i++) { 11703 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11704 if (diff == 0) 11705 continue; 11706 ctl_set_invalid_field(ctsio, 11707 /*sks_valid*/ 1, 11708 /*command*/ 1, 11709 /*field*/ i, 11710 /*bit_valid*/ 1, 11711 /*bit*/ fls(diff) - 1); 11712 ctl_done((union ctl_io *)ctsio); 11713 return (NULL); 11714 } 11715 return (entry); 11716} 11717 11718static int 11719ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11720{ 11721 11722 switch (lun_type) { 11723 case T_PROCESSOR: 11724 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11725 return (0); 11726 break; 11727 case T_DIRECT: 11728 if ((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) 11729 return (0); 11730 break; 11731 default: 11732 return (0); 11733 } 11734 return (1); 11735} 11736 11737static int 11738ctl_scsiio(struct ctl_scsiio *ctsio) 11739{ 11740 int retval; 11741 const struct ctl_cmd_entry *entry; 11742 11743 retval = CTL_RETVAL_COMPLETE; 11744 11745 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11746 11747 entry = ctl_get_cmd_entry(ctsio, NULL); 11748 11749 /* 11750 * If this I/O has been aborted, just send it straight to 11751 * ctl_done() without executing it. 11752 */ 11753 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11754 ctl_done((union ctl_io *)ctsio); 11755 goto bailout; 11756 } 11757 11758 /* 11759 * All the checks should have been handled by ctl_scsiio_precheck(). 11760 * We should be clear now to just execute the I/O. 11761 */ 11762 retval = entry->execute(ctsio); 11763 11764bailout: 11765 return (retval); 11766} 11767 11768/* 11769 * Since we only implement one target right now, a bus reset simply resets 11770 * our single target. 11771 */ 11772static int 11773ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11774{ 11775 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11776} 11777 11778static int 11779ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11780 ctl_ua_type ua_type) 11781{ 11782 struct ctl_port *port; 11783 struct ctl_lun *lun; 11784 int retval; 11785 11786 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11787 union ctl_ha_msg msg_info; 11788 11789 msg_info.hdr.nexus = io->io_hdr.nexus; 11790 if (ua_type==CTL_UA_TARG_RESET) 11791 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11792 else 11793 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11794 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11795 msg_info.hdr.original_sc = NULL; 11796 msg_info.hdr.serializing_sc = NULL; 11797 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11798 sizeof(msg_info.task), M_WAITOK); 11799 } 11800 retval = 0; 11801 11802 mtx_lock(&softc->ctl_lock); 11803 port = ctl_io_port(&io->io_hdr); 11804 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11805 if (port != NULL && 11806 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 11807 continue; 11808 retval += ctl_do_lun_reset(lun, io, ua_type); 11809 } 11810 mtx_unlock(&softc->ctl_lock); 11811 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11812 return (retval); 11813} 11814 11815/* 11816 * The LUN should always be set. The I/O is optional, and is used to 11817 * distinguish between I/Os sent by this initiator, and by other 11818 * initiators. We set unit attention for initiators other than this one. 11819 * SAM-3 is vague on this point. It does say that a unit attention should 11820 * be established for other initiators when a LUN is reset (see section 11821 * 5.7.3), but it doesn't specifically say that the unit attention should 11822 * be established for this particular initiator when a LUN is reset. Here 11823 * is the relevant text, from SAM-3 rev 8: 11824 * 11825 * 5.7.2 When a SCSI initiator port aborts its own tasks 11826 * 11827 * When a SCSI initiator port causes its own task(s) to be aborted, no 11828 * notification that the task(s) have been aborted shall be returned to 11829 * the SCSI initiator port other than the completion response for the 11830 * command or task management function action that caused the task(s) to 11831 * be aborted and notification(s) associated with related effects of the 11832 * action (e.g., a reset unit attention condition). 11833 * 11834 * XXX KDM for now, we're setting unit attention for all initiators. 11835 */ 11836static int 11837ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11838{ 11839 union ctl_io *xio; 11840#if 0 11841 uint32_t initidx; 11842#endif 11843#ifdef CTL_WITH_CA 11844 int i; 11845#endif 11846 11847 mtx_lock(&lun->lun_lock); 11848 /* 11849 * Run through the OOA queue and abort each I/O. 11850 */ 11851 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11852 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11853 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11854 } 11855 11856 /* 11857 * This version sets unit attention for every 11858 */ 11859#if 0 11860 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11861 ctl_est_ua_all(lun, initidx, ua_type); 11862#else 11863 ctl_est_ua_all(lun, -1, ua_type); 11864#endif 11865 11866 /* 11867 * A reset (any kind, really) clears reservations established with 11868 * RESERVE/RELEASE. It does not clear reservations established 11869 * with PERSISTENT RESERVE OUT, but we don't support that at the 11870 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11871 * reservations made with the RESERVE/RELEASE commands, because 11872 * those commands are obsolete in SPC-3. 11873 */ 11874 lun->flags &= ~CTL_LUN_RESERVED; 11875 11876#ifdef CTL_WITH_CA 11877 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11878 ctl_clear_mask(lun->have_ca, i); 11879#endif 11880 mtx_unlock(&lun->lun_lock); 11881 11882 return (0); 11883} 11884 11885static int 11886ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11887{ 11888 struct ctl_lun *lun; 11889 uint32_t targ_lun; 11890 int retval; 11891 11892 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11893 mtx_lock(&softc->ctl_lock); 11894 if ((targ_lun >= CTL_MAX_LUNS) || 11895 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11896 mtx_unlock(&softc->ctl_lock); 11897 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11898 return (1); 11899 } 11900 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 11901 mtx_unlock(&softc->ctl_lock); 11902 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11903 11904 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11905 union ctl_ha_msg msg_info; 11906 11907 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11908 msg_info.hdr.nexus = io->io_hdr.nexus; 11909 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11910 msg_info.hdr.original_sc = NULL; 11911 msg_info.hdr.serializing_sc = NULL; 11912 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11913 sizeof(msg_info.task), M_WAITOK); 11914 } 11915 return (retval); 11916} 11917 11918static void 11919ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11920 int other_sc) 11921{ 11922 union ctl_io *xio; 11923 11924 mtx_assert(&lun->lun_lock, MA_OWNED); 11925 11926 /* 11927 * Run through the OOA queue and attempt to find the given I/O. 11928 * The target port, initiator ID, tag type and tag number have to 11929 * match the values that we got from the initiator. If we have an 11930 * untagged command to abort, simply abort the first untagged command 11931 * we come to. We only allow one untagged command at a time of course. 11932 */ 11933 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11934 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11935 11936 if ((targ_port == UINT32_MAX || 11937 targ_port == xio->io_hdr.nexus.targ_port) && 11938 (init_id == UINT32_MAX || 11939 init_id == xio->io_hdr.nexus.initid)) { 11940 if (targ_port != xio->io_hdr.nexus.targ_port || 11941 init_id != xio->io_hdr.nexus.initid) 11942 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11943 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11944 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11945 union ctl_ha_msg msg_info; 11946 11947 msg_info.hdr.nexus = xio->io_hdr.nexus; 11948 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11949 msg_info.task.tag_num = xio->scsiio.tag_num; 11950 msg_info.task.tag_type = xio->scsiio.tag_type; 11951 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11952 msg_info.hdr.original_sc = NULL; 11953 msg_info.hdr.serializing_sc = NULL; 11954 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11955 sizeof(msg_info.task), M_NOWAIT); 11956 } 11957 } 11958 } 11959} 11960 11961static int 11962ctl_abort_task_set(union ctl_io *io) 11963{ 11964 struct ctl_softc *softc = control_softc; 11965 struct ctl_lun *lun; 11966 uint32_t targ_lun; 11967 11968 /* 11969 * Look up the LUN. 11970 */ 11971 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11972 mtx_lock(&softc->ctl_lock); 11973 if ((targ_lun >= CTL_MAX_LUNS) || 11974 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11975 mtx_unlock(&softc->ctl_lock); 11976 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11977 return (1); 11978 } 11979 11980 mtx_lock(&lun->lun_lock); 11981 mtx_unlock(&softc->ctl_lock); 11982 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11983 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11984 io->io_hdr.nexus.initid, 11985 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11986 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11987 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11988 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11989 } 11990 mtx_unlock(&lun->lun_lock); 11991 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11992 return (0); 11993} 11994 11995static int 11996ctl_i_t_nexus_reset(union ctl_io *io) 11997{ 11998 struct ctl_softc *softc = control_softc; 11999 struct ctl_lun *lun; 12000 uint32_t initidx; 12001 12002 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12003 union ctl_ha_msg msg_info; 12004 12005 msg_info.hdr.nexus = io->io_hdr.nexus; 12006 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 12007 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12008 msg_info.hdr.original_sc = NULL; 12009 msg_info.hdr.serializing_sc = NULL; 12010 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12011 sizeof(msg_info.task), M_WAITOK); 12012 } 12013 12014 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12015 mtx_lock(&softc->ctl_lock); 12016 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12017 mtx_lock(&lun->lun_lock); 12018 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12019 io->io_hdr.nexus.initid, 1); 12020#ifdef CTL_WITH_CA 12021 ctl_clear_mask(lun->have_ca, initidx); 12022#endif 12023 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 12024 lun->flags &= ~CTL_LUN_RESERVED; 12025 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12026 mtx_unlock(&lun->lun_lock); 12027 } 12028 mtx_unlock(&softc->ctl_lock); 12029 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12030 return (0); 12031} 12032 12033static int 12034ctl_abort_task(union ctl_io *io) 12035{ 12036 union ctl_io *xio; 12037 struct ctl_lun *lun; 12038 struct ctl_softc *softc; 12039#if 0 12040 struct sbuf sb; 12041 char printbuf[128]; 12042#endif 12043 int found; 12044 uint32_t targ_lun; 12045 12046 softc = control_softc; 12047 found = 0; 12048 12049 /* 12050 * Look up the LUN. 12051 */ 12052 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12053 mtx_lock(&softc->ctl_lock); 12054 if ((targ_lun >= CTL_MAX_LUNS) || 12055 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12056 mtx_unlock(&softc->ctl_lock); 12057 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12058 return (1); 12059 } 12060 12061#if 0 12062 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12063 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12064#endif 12065 12066 mtx_lock(&lun->lun_lock); 12067 mtx_unlock(&softc->ctl_lock); 12068 /* 12069 * Run through the OOA queue and attempt to find the given I/O. 12070 * The target port, initiator ID, tag type and tag number have to 12071 * match the values that we got from the initiator. If we have an 12072 * untagged command to abort, simply abort the first untagged command 12073 * we come to. We only allow one untagged command at a time of course. 12074 */ 12075 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12076 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12077#if 0 12078 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12079 12080 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12081 lun->lun, xio->scsiio.tag_num, 12082 xio->scsiio.tag_type, 12083 (xio->io_hdr.blocked_links.tqe_prev 12084 == NULL) ? "" : " BLOCKED", 12085 (xio->io_hdr.flags & 12086 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12087 (xio->io_hdr.flags & 12088 CTL_FLAG_ABORT) ? " ABORT" : "", 12089 (xio->io_hdr.flags & 12090 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12091 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12092 sbuf_finish(&sb); 12093 printf("%s\n", sbuf_data(&sb)); 12094#endif 12095 12096 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12097 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12098 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12099 continue; 12100 12101 /* 12102 * If the abort says that the task is untagged, the 12103 * task in the queue must be untagged. Otherwise, 12104 * we just check to see whether the tag numbers 12105 * match. This is because the QLogic firmware 12106 * doesn't pass back the tag type in an abort 12107 * request. 12108 */ 12109#if 0 12110 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12111 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12112 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12113#endif 12114 /* 12115 * XXX KDM we've got problems with FC, because it 12116 * doesn't send down a tag type with aborts. So we 12117 * can only really go by the tag number... 12118 * This may cause problems with parallel SCSI. 12119 * Need to figure that out!! 12120 */ 12121 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12122 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12123 found = 1; 12124 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12125 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12126 union ctl_ha_msg msg_info; 12127 12128 msg_info.hdr.nexus = io->io_hdr.nexus; 12129 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12130 msg_info.task.tag_num = io->taskio.tag_num; 12131 msg_info.task.tag_type = io->taskio.tag_type; 12132 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12133 msg_info.hdr.original_sc = NULL; 12134 msg_info.hdr.serializing_sc = NULL; 12135#if 0 12136 printf("Sent Abort to other side\n"); 12137#endif 12138 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12139 sizeof(msg_info.task), M_NOWAIT); 12140 } 12141#if 0 12142 printf("ctl_abort_task: found I/O to abort\n"); 12143#endif 12144 } 12145 } 12146 mtx_unlock(&lun->lun_lock); 12147 12148 if (found == 0) { 12149 /* 12150 * This isn't really an error. It's entirely possible for 12151 * the abort and command completion to cross on the wire. 12152 * This is more of an informative/diagnostic error. 12153 */ 12154#if 0 12155 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12156 "%u:%u:%u tag %d type %d\n", 12157 io->io_hdr.nexus.initid, 12158 io->io_hdr.nexus.targ_port, 12159 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12160 io->taskio.tag_type); 12161#endif 12162 } 12163 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12164 return (0); 12165} 12166 12167static int 12168ctl_query_task(union ctl_io *io, int task_set) 12169{ 12170 union ctl_io *xio; 12171 struct ctl_lun *lun; 12172 struct ctl_softc *softc; 12173 int found = 0; 12174 uint32_t targ_lun; 12175 12176 softc = control_softc; 12177 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12178 mtx_lock(&softc->ctl_lock); 12179 if ((targ_lun >= CTL_MAX_LUNS) || 12180 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12181 mtx_unlock(&softc->ctl_lock); 12182 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12183 return (1); 12184 } 12185 mtx_lock(&lun->lun_lock); 12186 mtx_unlock(&softc->ctl_lock); 12187 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12188 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12189 12190 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12191 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12192 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12193 continue; 12194 12195 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12196 found = 1; 12197 break; 12198 } 12199 } 12200 mtx_unlock(&lun->lun_lock); 12201 if (found) 12202 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12203 else 12204 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12205 return (0); 12206} 12207 12208static int 12209ctl_query_async_event(union ctl_io *io) 12210{ 12211 struct ctl_lun *lun; 12212 struct ctl_softc *softc; 12213 ctl_ua_type ua; 12214 uint32_t targ_lun, initidx; 12215 12216 softc = control_softc; 12217 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12218 mtx_lock(&softc->ctl_lock); 12219 if ((targ_lun >= CTL_MAX_LUNS) || 12220 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12221 mtx_unlock(&softc->ctl_lock); 12222 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12223 return (1); 12224 } 12225 mtx_lock(&lun->lun_lock); 12226 mtx_unlock(&softc->ctl_lock); 12227 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12228 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12229 mtx_unlock(&lun->lun_lock); 12230 if (ua != CTL_UA_NONE) 12231 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12232 else 12233 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12234 return (0); 12235} 12236 12237static void 12238ctl_run_task(union ctl_io *io) 12239{ 12240 struct ctl_softc *softc = control_softc; 12241 int retval = 1; 12242 12243 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12244 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12245 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12246 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12247 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12248 switch (io->taskio.task_action) { 12249 case CTL_TASK_ABORT_TASK: 12250 retval = ctl_abort_task(io); 12251 break; 12252 case CTL_TASK_ABORT_TASK_SET: 12253 case CTL_TASK_CLEAR_TASK_SET: 12254 retval = ctl_abort_task_set(io); 12255 break; 12256 case CTL_TASK_CLEAR_ACA: 12257 break; 12258 case CTL_TASK_I_T_NEXUS_RESET: 12259 retval = ctl_i_t_nexus_reset(io); 12260 break; 12261 case CTL_TASK_LUN_RESET: 12262 retval = ctl_lun_reset(softc, io); 12263 break; 12264 case CTL_TASK_TARGET_RESET: 12265 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12266 break; 12267 case CTL_TASK_BUS_RESET: 12268 retval = ctl_bus_reset(softc, io); 12269 break; 12270 case CTL_TASK_PORT_LOGIN: 12271 break; 12272 case CTL_TASK_PORT_LOGOUT: 12273 break; 12274 case CTL_TASK_QUERY_TASK: 12275 retval = ctl_query_task(io, 0); 12276 break; 12277 case CTL_TASK_QUERY_TASK_SET: 12278 retval = ctl_query_task(io, 1); 12279 break; 12280 case CTL_TASK_QUERY_ASYNC_EVENT: 12281 retval = ctl_query_async_event(io); 12282 break; 12283 default: 12284 printf("%s: got unknown task management event %d\n", 12285 __func__, io->taskio.task_action); 12286 break; 12287 } 12288 if (retval == 0) 12289 io->io_hdr.status = CTL_SUCCESS; 12290 else 12291 io->io_hdr.status = CTL_ERROR; 12292 ctl_done(io); 12293} 12294 12295/* 12296 * For HA operation. Handle commands that come in from the other 12297 * controller. 12298 */ 12299static void 12300ctl_handle_isc(union ctl_io *io) 12301{ 12302 int free_io; 12303 struct ctl_lun *lun; 12304 struct ctl_softc *softc = control_softc; 12305 uint32_t targ_lun; 12306 12307 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12308 lun = softc->ctl_luns[targ_lun]; 12309 12310 switch (io->io_hdr.msg_type) { 12311 case CTL_MSG_SERIALIZE: 12312 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12313 break; 12314 case CTL_MSG_R2R: { 12315 const struct ctl_cmd_entry *entry; 12316 12317 /* 12318 * This is only used in SER_ONLY mode. 12319 */ 12320 free_io = 0; 12321 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12322 mtx_lock(&lun->lun_lock); 12323 if (ctl_scsiio_lun_check(lun, 12324 entry, (struct ctl_scsiio *)io) != 0) { 12325 mtx_unlock(&lun->lun_lock); 12326 ctl_done(io); 12327 break; 12328 } 12329 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12330 mtx_unlock(&lun->lun_lock); 12331 ctl_enqueue_rtr(io); 12332 break; 12333 } 12334 case CTL_MSG_FINISH_IO: 12335 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12336 free_io = 0; 12337 ctl_done(io); 12338 } else { 12339 free_io = 1; 12340 mtx_lock(&lun->lun_lock); 12341 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12342 ooa_links); 12343 ctl_check_blocked(lun); 12344 mtx_unlock(&lun->lun_lock); 12345 } 12346 break; 12347 case CTL_MSG_PERS_ACTION: 12348 ctl_hndl_per_res_out_on_other_sc( 12349 (union ctl_ha_msg *)&io->presio.pr_msg); 12350 free_io = 1; 12351 break; 12352 case CTL_MSG_BAD_JUJU: 12353 free_io = 0; 12354 ctl_done(io); 12355 break; 12356 case CTL_MSG_DATAMOVE: 12357 /* Only used in XFER mode */ 12358 free_io = 0; 12359 ctl_datamove_remote(io); 12360 break; 12361 case CTL_MSG_DATAMOVE_DONE: 12362 /* Only used in XFER mode */ 12363 free_io = 0; 12364 io->scsiio.be_move_done(io); 12365 break; 12366 case CTL_MSG_FAILOVER: 12367 mtx_lock(&lun->lun_lock); 12368 ctl_failover_lun(lun); 12369 mtx_unlock(&lun->lun_lock); 12370 free_io = 1; 12371 break; 12372 default: 12373 free_io = 1; 12374 printf("%s: Invalid message type %d\n", 12375 __func__, io->io_hdr.msg_type); 12376 break; 12377 } 12378 if (free_io) 12379 ctl_free_io(io); 12380 12381} 12382 12383 12384/* 12385 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12386 * there is no match. 12387 */ 12388static ctl_lun_error_pattern 12389ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12390{ 12391 const struct ctl_cmd_entry *entry; 12392 ctl_lun_error_pattern filtered_pattern, pattern; 12393 12394 pattern = desc->error_pattern; 12395 12396 /* 12397 * XXX KDM we need more data passed into this function to match a 12398 * custom pattern, and we actually need to implement custom pattern 12399 * matching. 12400 */ 12401 if (pattern & CTL_LUN_PAT_CMD) 12402 return (CTL_LUN_PAT_CMD); 12403 12404 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12405 return (CTL_LUN_PAT_ANY); 12406 12407 entry = ctl_get_cmd_entry(ctsio, NULL); 12408 12409 filtered_pattern = entry->pattern & pattern; 12410 12411 /* 12412 * If the user requested specific flags in the pattern (e.g. 12413 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12414 * flags. 12415 * 12416 * If the user did not specify any flags, it doesn't matter whether 12417 * or not the command supports the flags. 12418 */ 12419 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12420 (pattern & ~CTL_LUN_PAT_MASK)) 12421 return (CTL_LUN_PAT_NONE); 12422 12423 /* 12424 * If the user asked for a range check, see if the requested LBA 12425 * range overlaps with this command's LBA range. 12426 */ 12427 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12428 uint64_t lba1; 12429 uint64_t len1; 12430 ctl_action action; 12431 int retval; 12432 12433 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12434 if (retval != 0) 12435 return (CTL_LUN_PAT_NONE); 12436 12437 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12438 desc->lba_range.len, FALSE); 12439 /* 12440 * A "pass" means that the LBA ranges don't overlap, so 12441 * this doesn't match the user's range criteria. 12442 */ 12443 if (action == CTL_ACTION_PASS) 12444 return (CTL_LUN_PAT_NONE); 12445 } 12446 12447 return (filtered_pattern); 12448} 12449 12450static void 12451ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12452{ 12453 struct ctl_error_desc *desc, *desc2; 12454 12455 mtx_assert(&lun->lun_lock, MA_OWNED); 12456 12457 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12458 ctl_lun_error_pattern pattern; 12459 /* 12460 * Check to see whether this particular command matches 12461 * the pattern in the descriptor. 12462 */ 12463 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12464 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12465 continue; 12466 12467 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12468 case CTL_LUN_INJ_ABORTED: 12469 ctl_set_aborted(&io->scsiio); 12470 break; 12471 case CTL_LUN_INJ_MEDIUM_ERR: 12472 ctl_set_medium_error(&io->scsiio, 12473 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12474 CTL_FLAG_DATA_OUT); 12475 break; 12476 case CTL_LUN_INJ_UA: 12477 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12478 * OCCURRED */ 12479 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12480 break; 12481 case CTL_LUN_INJ_CUSTOM: 12482 /* 12483 * We're assuming the user knows what he is doing. 12484 * Just copy the sense information without doing 12485 * checks. 12486 */ 12487 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12488 MIN(sizeof(desc->custom_sense), 12489 sizeof(io->scsiio.sense_data))); 12490 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12491 io->scsiio.sense_len = SSD_FULL_SIZE; 12492 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12493 break; 12494 case CTL_LUN_INJ_NONE: 12495 default: 12496 /* 12497 * If this is an error injection type we don't know 12498 * about, clear the continuous flag (if it is set) 12499 * so it will get deleted below. 12500 */ 12501 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12502 break; 12503 } 12504 /* 12505 * By default, each error injection action is a one-shot 12506 */ 12507 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12508 continue; 12509 12510 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12511 12512 free(desc, M_CTL); 12513 } 12514} 12515 12516#ifdef CTL_IO_DELAY 12517static void 12518ctl_datamove_timer_wakeup(void *arg) 12519{ 12520 union ctl_io *io; 12521 12522 io = (union ctl_io *)arg; 12523 12524 ctl_datamove(io); 12525} 12526#endif /* CTL_IO_DELAY */ 12527 12528void 12529ctl_datamove(union ctl_io *io) 12530{ 12531 struct ctl_lun *lun; 12532 void (*fe_datamove)(union ctl_io *io); 12533 12534 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12535 12536 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12537 12538 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12539#ifdef CTL_TIME_IO 12540 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12541 char str[256]; 12542 char path_str[64]; 12543 struct sbuf sb; 12544 12545 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12546 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12547 12548 sbuf_cat(&sb, path_str); 12549 switch (io->io_hdr.io_type) { 12550 case CTL_IO_SCSI: 12551 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12552 sbuf_printf(&sb, "\n"); 12553 sbuf_cat(&sb, path_str); 12554 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12555 io->scsiio.tag_num, io->scsiio.tag_type); 12556 break; 12557 case CTL_IO_TASK: 12558 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12559 "Tag Type: %d\n", io->taskio.task_action, 12560 io->taskio.tag_num, io->taskio.tag_type); 12561 break; 12562 default: 12563 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12564 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12565 break; 12566 } 12567 sbuf_cat(&sb, path_str); 12568 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12569 (intmax_t)time_uptime - io->io_hdr.start_time); 12570 sbuf_finish(&sb); 12571 printf("%s", sbuf_data(&sb)); 12572 } 12573#endif /* CTL_TIME_IO */ 12574 12575#ifdef CTL_IO_DELAY 12576 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12577 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12578 } else { 12579 if ((lun != NULL) 12580 && (lun->delay_info.datamove_delay > 0)) { 12581 12582 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12583 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12584 callout_reset(&io->io_hdr.delay_callout, 12585 lun->delay_info.datamove_delay * hz, 12586 ctl_datamove_timer_wakeup, io); 12587 if (lun->delay_info.datamove_type == 12588 CTL_DELAY_TYPE_ONESHOT) 12589 lun->delay_info.datamove_delay = 0; 12590 return; 12591 } 12592 } 12593#endif 12594 12595 /* 12596 * This command has been aborted. Set the port status, so we fail 12597 * the data move. 12598 */ 12599 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12600 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12601 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12602 io->io_hdr.nexus.targ_port, 12603 io->io_hdr.nexus.targ_lun); 12604 io->io_hdr.port_status = 31337; 12605 /* 12606 * Note that the backend, in this case, will get the 12607 * callback in its context. In other cases it may get 12608 * called in the frontend's interrupt thread context. 12609 */ 12610 io->scsiio.be_move_done(io); 12611 return; 12612 } 12613 12614 /* Don't confuse frontend with zero length data move. */ 12615 if (io->scsiio.kern_data_len == 0) { 12616 io->scsiio.be_move_done(io); 12617 return; 12618 } 12619 12620 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12621 fe_datamove(io); 12622} 12623 12624static void 12625ctl_send_datamove_done(union ctl_io *io, int have_lock) 12626{ 12627 union ctl_ha_msg msg; 12628#ifdef CTL_TIME_IO 12629 struct bintime cur_bt; 12630#endif 12631 12632 memset(&msg, 0, sizeof(msg)); 12633 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12634 msg.hdr.original_sc = io; 12635 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12636 msg.hdr.nexus = io->io_hdr.nexus; 12637 msg.hdr.status = io->io_hdr.status; 12638 msg.scsi.tag_num = io->scsiio.tag_num; 12639 msg.scsi.tag_type = io->scsiio.tag_type; 12640 msg.scsi.scsi_status = io->scsiio.scsi_status; 12641 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12642 io->scsiio.sense_len); 12643 msg.scsi.sense_len = io->scsiio.sense_len; 12644 msg.scsi.sense_residual = io->scsiio.sense_residual; 12645 msg.scsi.fetd_status = io->io_hdr.port_status; 12646 msg.scsi.residual = io->scsiio.residual; 12647 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12648 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12649 ctl_failover_io(io, /*have_lock*/ have_lock); 12650 return; 12651 } 12652 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12653 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12654 msg.scsi.sense_len, M_WAITOK); 12655 12656#ifdef CTL_TIME_IO 12657 getbintime(&cur_bt); 12658 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12659 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12660 io->io_hdr.num_dmas++; 12661#endif 12662} 12663 12664/* 12665 * The DMA to the remote side is done, now we need to tell the other side 12666 * we're done so it can continue with its data movement. 12667 */ 12668static void 12669ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12670{ 12671 union ctl_io *io; 12672 int i; 12673 12674 io = rq->context; 12675 12676 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12677 printf("%s: ISC DMA write failed with error %d", __func__, 12678 rq->ret); 12679 ctl_set_internal_failure(&io->scsiio, 12680 /*sks_valid*/ 1, 12681 /*retry_count*/ rq->ret); 12682 } 12683 12684 ctl_dt_req_free(rq); 12685 12686 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12687 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12688 free(io->io_hdr.remote_sglist, M_CTL); 12689 io->io_hdr.remote_sglist = NULL; 12690 io->io_hdr.local_sglist = NULL; 12691 12692 /* 12693 * The data is in local and remote memory, so now we need to send 12694 * status (good or back) back to the other side. 12695 */ 12696 ctl_send_datamove_done(io, /*have_lock*/ 0); 12697} 12698 12699/* 12700 * We've moved the data from the host/controller into local memory. Now we 12701 * need to push it over to the remote controller's memory. 12702 */ 12703static int 12704ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12705{ 12706 int retval; 12707 12708 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12709 ctl_datamove_remote_write_cb); 12710 return (retval); 12711} 12712 12713static void 12714ctl_datamove_remote_write(union ctl_io *io) 12715{ 12716 int retval; 12717 void (*fe_datamove)(union ctl_io *io); 12718 12719 /* 12720 * - Get the data from the host/HBA into local memory. 12721 * - DMA memory from the local controller to the remote controller. 12722 * - Send status back to the remote controller. 12723 */ 12724 12725 retval = ctl_datamove_remote_sgl_setup(io); 12726 if (retval != 0) 12727 return; 12728 12729 /* Switch the pointer over so the FETD knows what to do */ 12730 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12731 12732 /* 12733 * Use a custom move done callback, since we need to send completion 12734 * back to the other controller, not to the backend on this side. 12735 */ 12736 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12737 12738 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12739 fe_datamove(io); 12740} 12741 12742static int 12743ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12744{ 12745#if 0 12746 char str[256]; 12747 char path_str[64]; 12748 struct sbuf sb; 12749#endif 12750 int i; 12751 12752 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12753 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12754 free(io->io_hdr.remote_sglist, M_CTL); 12755 io->io_hdr.remote_sglist = NULL; 12756 io->io_hdr.local_sglist = NULL; 12757 12758#if 0 12759 scsi_path_string(io, path_str, sizeof(path_str)); 12760 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12761 sbuf_cat(&sb, path_str); 12762 scsi_command_string(&io->scsiio, NULL, &sb); 12763 sbuf_printf(&sb, "\n"); 12764 sbuf_cat(&sb, path_str); 12765 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12766 io->scsiio.tag_num, io->scsiio.tag_type); 12767 sbuf_cat(&sb, path_str); 12768 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12769 io->io_hdr.flags, io->io_hdr.status); 12770 sbuf_finish(&sb); 12771 printk("%s", sbuf_data(&sb)); 12772#endif 12773 12774 12775 /* 12776 * The read is done, now we need to send status (good or bad) back 12777 * to the other side. 12778 */ 12779 ctl_send_datamove_done(io, /*have_lock*/ 0); 12780 12781 return (0); 12782} 12783 12784static void 12785ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12786{ 12787 union ctl_io *io; 12788 void (*fe_datamove)(union ctl_io *io); 12789 12790 io = rq->context; 12791 12792 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12793 printf("%s: ISC DMA read failed with error %d\n", __func__, 12794 rq->ret); 12795 ctl_set_internal_failure(&io->scsiio, 12796 /*sks_valid*/ 1, 12797 /*retry_count*/ rq->ret); 12798 } 12799 12800 ctl_dt_req_free(rq); 12801 12802 /* Switch the pointer over so the FETD knows what to do */ 12803 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12804 12805 /* 12806 * Use a custom move done callback, since we need to send completion 12807 * back to the other controller, not to the backend on this side. 12808 */ 12809 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12810 12811 /* XXX KDM add checks like the ones in ctl_datamove? */ 12812 12813 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12814 fe_datamove(io); 12815} 12816 12817static int 12818ctl_datamove_remote_sgl_setup(union ctl_io *io) 12819{ 12820 struct ctl_sg_entry *local_sglist; 12821 uint32_t len_to_go; 12822 int retval; 12823 int i; 12824 12825 retval = 0; 12826 local_sglist = io->io_hdr.local_sglist; 12827 len_to_go = io->scsiio.kern_data_len; 12828 12829 /* 12830 * The difficult thing here is that the size of the various 12831 * S/G segments may be different than the size from the 12832 * remote controller. That'll make it harder when DMAing 12833 * the data back to the other side. 12834 */ 12835 for (i = 0; len_to_go > 0; i++) { 12836 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12837 local_sglist[i].addr = 12838 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12839 12840 len_to_go -= local_sglist[i].len; 12841 } 12842 /* 12843 * Reset the number of S/G entries accordingly. The original 12844 * number of S/G entries is available in rem_sg_entries. 12845 */ 12846 io->scsiio.kern_sg_entries = i; 12847 12848#if 0 12849 printf("%s: kern_sg_entries = %d\n", __func__, 12850 io->scsiio.kern_sg_entries); 12851 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12852 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12853 local_sglist[i].addr, local_sglist[i].len); 12854#endif 12855 12856 return (retval); 12857} 12858 12859static int 12860ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12861 ctl_ha_dt_cb callback) 12862{ 12863 struct ctl_ha_dt_req *rq; 12864 struct ctl_sg_entry *remote_sglist, *local_sglist; 12865 uint32_t local_used, remote_used, total_used; 12866 int i, j, isc_ret; 12867 12868 rq = ctl_dt_req_alloc(); 12869 12870 /* 12871 * If we failed to allocate the request, and if the DMA didn't fail 12872 * anyway, set busy status. This is just a resource allocation 12873 * failure. 12874 */ 12875 if ((rq == NULL) 12876 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12877 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12878 ctl_set_busy(&io->scsiio); 12879 12880 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12881 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12882 12883 if (rq != NULL) 12884 ctl_dt_req_free(rq); 12885 12886 /* 12887 * The data move failed. We need to return status back 12888 * to the other controller. No point in trying to DMA 12889 * data to the remote controller. 12890 */ 12891 12892 ctl_send_datamove_done(io, /*have_lock*/ 0); 12893 12894 return (1); 12895 } 12896 12897 local_sglist = io->io_hdr.local_sglist; 12898 remote_sglist = io->io_hdr.remote_sglist; 12899 local_used = 0; 12900 remote_used = 0; 12901 total_used = 0; 12902 12903 /* 12904 * Pull/push the data over the wire from/to the other controller. 12905 * This takes into account the possibility that the local and 12906 * remote sglists may not be identical in terms of the size of 12907 * the elements and the number of elements. 12908 * 12909 * One fundamental assumption here is that the length allocated for 12910 * both the local and remote sglists is identical. Otherwise, we've 12911 * essentially got a coding error of some sort. 12912 */ 12913 isc_ret = CTL_HA_STATUS_SUCCESS; 12914 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12915 uint32_t cur_len; 12916 uint8_t *tmp_ptr; 12917 12918 rq->command = command; 12919 rq->context = io; 12920 12921 /* 12922 * Both pointers should be aligned. But it is possible 12923 * that the allocation length is not. They should both 12924 * also have enough slack left over at the end, though, 12925 * to round up to the next 8 byte boundary. 12926 */ 12927 cur_len = MIN(local_sglist[i].len - local_used, 12928 remote_sglist[j].len - remote_used); 12929 rq->size = cur_len; 12930 12931 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12932 tmp_ptr += local_used; 12933 12934#if 0 12935 /* Use physical addresses when talking to ISC hardware */ 12936 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12937 /* XXX KDM use busdma */ 12938 rq->local = vtophys(tmp_ptr); 12939 } else 12940 rq->local = tmp_ptr; 12941#else 12942 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12943 ("HA does not support BUS_ADDR")); 12944 rq->local = tmp_ptr; 12945#endif 12946 12947 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12948 tmp_ptr += remote_used; 12949 rq->remote = tmp_ptr; 12950 12951 rq->callback = NULL; 12952 12953 local_used += cur_len; 12954 if (local_used >= local_sglist[i].len) { 12955 i++; 12956 local_used = 0; 12957 } 12958 12959 remote_used += cur_len; 12960 if (remote_used >= remote_sglist[j].len) { 12961 j++; 12962 remote_used = 0; 12963 } 12964 total_used += cur_len; 12965 12966 if (total_used >= io->scsiio.kern_data_len) 12967 rq->callback = callback; 12968 12969#if 0 12970 printf("%s: %s: local %p remote %p size %d\n", __func__, 12971 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12972 rq->local, rq->remote, rq->size); 12973#endif 12974 12975 isc_ret = ctl_dt_single(rq); 12976 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12977 break; 12978 } 12979 if (isc_ret != CTL_HA_STATUS_WAIT) { 12980 rq->ret = isc_ret; 12981 callback(rq); 12982 } 12983 12984 return (0); 12985} 12986 12987static void 12988ctl_datamove_remote_read(union ctl_io *io) 12989{ 12990 int retval; 12991 int i; 12992 12993 /* 12994 * This will send an error to the other controller in the case of a 12995 * failure. 12996 */ 12997 retval = ctl_datamove_remote_sgl_setup(io); 12998 if (retval != 0) 12999 return; 13000 13001 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13002 ctl_datamove_remote_read_cb); 13003 if (retval != 0) { 13004 /* 13005 * Make sure we free memory if there was an error.. The 13006 * ctl_datamove_remote_xfer() function will send the 13007 * datamove done message, or call the callback with an 13008 * error if there is a problem. 13009 */ 13010 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13011 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13012 free(io->io_hdr.remote_sglist, M_CTL); 13013 io->io_hdr.remote_sglist = NULL; 13014 io->io_hdr.local_sglist = NULL; 13015 } 13016} 13017 13018/* 13019 * Process a datamove request from the other controller. This is used for 13020 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13021 * first. Once that is complete, the data gets DMAed into the remote 13022 * controller's memory. For reads, we DMA from the remote controller's 13023 * memory into our memory first, and then move it out to the FETD. 13024 */ 13025static void 13026ctl_datamove_remote(union ctl_io *io) 13027{ 13028 13029 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 13030 13031 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13032 ctl_failover_io(io, /*have_lock*/ 0); 13033 return; 13034 } 13035 13036 /* 13037 * Note that we look for an aborted I/O here, but don't do some of 13038 * the other checks that ctl_datamove() normally does. 13039 * We don't need to run the datamove delay code, since that should 13040 * have been done if need be on the other controller. 13041 */ 13042 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13043 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 13044 io->scsiio.tag_num, io->io_hdr.nexus.initid, 13045 io->io_hdr.nexus.targ_port, 13046 io->io_hdr.nexus.targ_lun); 13047 io->io_hdr.port_status = 31338; 13048 ctl_send_datamove_done(io, /*have_lock*/ 0); 13049 return; 13050 } 13051 13052 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 13053 ctl_datamove_remote_write(io); 13054 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 13055 ctl_datamove_remote_read(io); 13056 else { 13057 io->io_hdr.port_status = 31339; 13058 ctl_send_datamove_done(io, /*have_lock*/ 0); 13059 } 13060} 13061 13062static int 13063ctl_process_done(union ctl_io *io) 13064{ 13065 struct ctl_lun *lun; 13066 struct ctl_softc *softc = control_softc; 13067 void (*fe_done)(union ctl_io *io); 13068 union ctl_ha_msg msg; 13069 uint32_t targ_port = io->io_hdr.nexus.targ_port; 13070 13071 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13072 fe_done = softc->ctl_ports[targ_port]->fe_done; 13073 13074#ifdef CTL_TIME_IO 13075 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13076 char str[256]; 13077 char path_str[64]; 13078 struct sbuf sb; 13079 13080 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13081 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13082 13083 sbuf_cat(&sb, path_str); 13084 switch (io->io_hdr.io_type) { 13085 case CTL_IO_SCSI: 13086 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13087 sbuf_printf(&sb, "\n"); 13088 sbuf_cat(&sb, path_str); 13089 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13090 io->scsiio.tag_num, io->scsiio.tag_type); 13091 break; 13092 case CTL_IO_TASK: 13093 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13094 "Tag Type: %d\n", io->taskio.task_action, 13095 io->taskio.tag_num, io->taskio.tag_type); 13096 break; 13097 default: 13098 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13099 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13100 break; 13101 } 13102 sbuf_cat(&sb, path_str); 13103 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13104 (intmax_t)time_uptime - io->io_hdr.start_time); 13105 sbuf_finish(&sb); 13106 printf("%s", sbuf_data(&sb)); 13107 } 13108#endif /* CTL_TIME_IO */ 13109 13110 switch (io->io_hdr.io_type) { 13111 case CTL_IO_SCSI: 13112 break; 13113 case CTL_IO_TASK: 13114 if (ctl_debug & CTL_DEBUG_INFO) 13115 ctl_io_error_print(io, NULL); 13116 fe_done(io); 13117 return (CTL_RETVAL_COMPLETE); 13118 default: 13119 panic("ctl_process_done: invalid io type %d\n", 13120 io->io_hdr.io_type); 13121 break; /* NOTREACHED */ 13122 } 13123 13124 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13125 if (lun == NULL) { 13126 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13127 io->io_hdr.nexus.targ_mapped_lun)); 13128 goto bailout; 13129 } 13130 13131 mtx_lock(&lun->lun_lock); 13132 13133 /* 13134 * Check to see if we have any errors to inject here. We only 13135 * inject errors for commands that don't already have errors set. 13136 */ 13137 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13138 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13139 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13140 ctl_inject_error(lun, io); 13141 13142 /* 13143 * XXX KDM how do we treat commands that aren't completed 13144 * successfully? 13145 * 13146 * XXX KDM should we also track I/O latency? 13147 */ 13148 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13149 io->io_hdr.io_type == CTL_IO_SCSI) { 13150#ifdef CTL_TIME_IO 13151 struct bintime cur_bt; 13152#endif 13153 int type; 13154 13155 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13156 CTL_FLAG_DATA_IN) 13157 type = CTL_STATS_READ; 13158 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13159 CTL_FLAG_DATA_OUT) 13160 type = CTL_STATS_WRITE; 13161 else 13162 type = CTL_STATS_NO_IO; 13163 13164 lun->stats.ports[targ_port].bytes[type] += 13165 io->scsiio.kern_total_len; 13166 lun->stats.ports[targ_port].operations[type]++; 13167#ifdef CTL_TIME_IO 13168 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13169 &io->io_hdr.dma_bt); 13170 lun->stats.ports[targ_port].num_dmas[type] += 13171 io->io_hdr.num_dmas; 13172 getbintime(&cur_bt); 13173 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13174 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13175#endif 13176 } 13177 13178 /* 13179 * Remove this from the OOA queue. 13180 */ 13181 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13182#ifdef CTL_TIME_IO 13183 if (TAILQ_EMPTY(&lun->ooa_queue)) 13184 lun->last_busy = getsbinuptime(); 13185#endif 13186 13187 /* 13188 * Run through the blocked queue on this LUN and see if anything 13189 * has become unblocked, now that this transaction is done. 13190 */ 13191 ctl_check_blocked(lun); 13192 13193 /* 13194 * If the LUN has been invalidated, free it if there is nothing 13195 * left on its OOA queue. 13196 */ 13197 if ((lun->flags & CTL_LUN_INVALID) 13198 && TAILQ_EMPTY(&lun->ooa_queue)) { 13199 mtx_unlock(&lun->lun_lock); 13200 mtx_lock(&softc->ctl_lock); 13201 ctl_free_lun(lun); 13202 mtx_unlock(&softc->ctl_lock); 13203 } else 13204 mtx_unlock(&lun->lun_lock); 13205 13206bailout: 13207 13208 /* 13209 * If this command has been aborted, make sure we set the status 13210 * properly. The FETD is responsible for freeing the I/O and doing 13211 * whatever it needs to do to clean up its state. 13212 */ 13213 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13214 ctl_set_task_aborted(&io->scsiio); 13215 13216 /* 13217 * If enabled, print command error status. 13218 */ 13219 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13220 (ctl_debug & CTL_DEBUG_INFO) != 0) 13221 ctl_io_error_print(io, NULL); 13222 13223 /* 13224 * Tell the FETD or the other shelf controller we're done with this 13225 * command. Note that only SCSI commands get to this point. Task 13226 * management commands are completed above. 13227 */ 13228 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13229 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13230 memset(&msg, 0, sizeof(msg)); 13231 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13232 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13233 msg.hdr.nexus = io->io_hdr.nexus; 13234 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13235 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13236 M_WAITOK); 13237 } 13238 13239 fe_done(io); 13240 return (CTL_RETVAL_COMPLETE); 13241} 13242 13243#ifdef CTL_WITH_CA 13244/* 13245 * Front end should call this if it doesn't do autosense. When the request 13246 * sense comes back in from the initiator, we'll dequeue this and send it. 13247 */ 13248int 13249ctl_queue_sense(union ctl_io *io) 13250{ 13251 struct ctl_lun *lun; 13252 struct ctl_port *port; 13253 struct ctl_softc *softc; 13254 uint32_t initidx, targ_lun; 13255 13256 softc = control_softc; 13257 13258 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13259 13260 /* 13261 * LUN lookup will likely move to the ctl_work_thread() once we 13262 * have our new queueing infrastructure (that doesn't put things on 13263 * a per-LUN queue initially). That is so that we can handle 13264 * things like an INQUIRY to a LUN that we don't have enabled. We 13265 * can't deal with that right now. 13266 */ 13267 mtx_lock(&softc->ctl_lock); 13268 13269 /* 13270 * If we don't have a LUN for this, just toss the sense 13271 * information. 13272 */ 13273 port = ctl_io_port(&ctsio->io_hdr); 13274 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13275 if ((targ_lun < CTL_MAX_LUNS) 13276 && (softc->ctl_luns[targ_lun] != NULL)) 13277 lun = softc->ctl_luns[targ_lun]; 13278 else 13279 goto bailout; 13280 13281 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13282 13283 mtx_lock(&lun->lun_lock); 13284 /* 13285 * Already have CA set for this LUN...toss the sense information. 13286 */ 13287 if (ctl_is_set(lun->have_ca, initidx)) { 13288 mtx_unlock(&lun->lun_lock); 13289 goto bailout; 13290 } 13291 13292 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13293 MIN(sizeof(lun->pending_sense[initidx]), 13294 sizeof(io->scsiio.sense_data))); 13295 ctl_set_mask(lun->have_ca, initidx); 13296 mtx_unlock(&lun->lun_lock); 13297 13298bailout: 13299 mtx_unlock(&softc->ctl_lock); 13300 13301 ctl_free_io(io); 13302 13303 return (CTL_RETVAL_COMPLETE); 13304} 13305#endif 13306 13307/* 13308 * Primary command inlet from frontend ports. All SCSI and task I/O 13309 * requests must go through this function. 13310 */ 13311int 13312ctl_queue(union ctl_io *io) 13313{ 13314 struct ctl_port *port; 13315 13316 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13317 13318#ifdef CTL_TIME_IO 13319 io->io_hdr.start_time = time_uptime; 13320 getbintime(&io->io_hdr.start_bt); 13321#endif /* CTL_TIME_IO */ 13322 13323 /* Map FE-specific LUN ID into global one. */ 13324 port = ctl_io_port(&io->io_hdr); 13325 io->io_hdr.nexus.targ_mapped_lun = 13326 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13327 13328 switch (io->io_hdr.io_type) { 13329 case CTL_IO_SCSI: 13330 case CTL_IO_TASK: 13331 if (ctl_debug & CTL_DEBUG_CDB) 13332 ctl_io_print(io); 13333 ctl_enqueue_incoming(io); 13334 break; 13335 default: 13336 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13337 return (EINVAL); 13338 } 13339 13340 return (CTL_RETVAL_COMPLETE); 13341} 13342 13343#ifdef CTL_IO_DELAY 13344static void 13345ctl_done_timer_wakeup(void *arg) 13346{ 13347 union ctl_io *io; 13348 13349 io = (union ctl_io *)arg; 13350 ctl_done(io); 13351} 13352#endif /* CTL_IO_DELAY */ 13353 13354void 13355ctl_serseq_done(union ctl_io *io) 13356{ 13357 struct ctl_lun *lun; 13358 13359 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13360 if (lun->be_lun == NULL || 13361 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13362 return; 13363 mtx_lock(&lun->lun_lock); 13364 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13365 ctl_check_blocked(lun); 13366 mtx_unlock(&lun->lun_lock); 13367} 13368 13369void 13370ctl_done(union ctl_io *io) 13371{ 13372 13373 /* 13374 * Enable this to catch duplicate completion issues. 13375 */ 13376#if 0 13377 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13378 printf("%s: type %d msg %d cdb %x iptl: " 13379 "%u:%u:%u tag 0x%04x " 13380 "flag %#x status %x\n", 13381 __func__, 13382 io->io_hdr.io_type, 13383 io->io_hdr.msg_type, 13384 io->scsiio.cdb[0], 13385 io->io_hdr.nexus.initid, 13386 io->io_hdr.nexus.targ_port, 13387 io->io_hdr.nexus.targ_lun, 13388 (io->io_hdr.io_type == 13389 CTL_IO_TASK) ? 13390 io->taskio.tag_num : 13391 io->scsiio.tag_num, 13392 io->io_hdr.flags, 13393 io->io_hdr.status); 13394 } else 13395 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13396#endif 13397 13398 /* 13399 * This is an internal copy of an I/O, and should not go through 13400 * the normal done processing logic. 13401 */ 13402 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13403 return; 13404 13405#ifdef CTL_IO_DELAY 13406 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13407 struct ctl_lun *lun; 13408 13409 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13410 13411 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13412 } else { 13413 struct ctl_lun *lun; 13414 13415 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13416 13417 if ((lun != NULL) 13418 && (lun->delay_info.done_delay > 0)) { 13419 13420 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13421 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13422 callout_reset(&io->io_hdr.delay_callout, 13423 lun->delay_info.done_delay * hz, 13424 ctl_done_timer_wakeup, io); 13425 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13426 lun->delay_info.done_delay = 0; 13427 return; 13428 } 13429 } 13430#endif /* CTL_IO_DELAY */ 13431 13432 ctl_enqueue_done(io); 13433} 13434 13435static void 13436ctl_work_thread(void *arg) 13437{ 13438 struct ctl_thread *thr = (struct ctl_thread *)arg; 13439 struct ctl_softc *softc = thr->ctl_softc; 13440 union ctl_io *io; 13441 int retval; 13442 13443 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13444 13445 for (;;) { 13446 retval = 0; 13447 13448 /* 13449 * We handle the queues in this order: 13450 * - ISC 13451 * - done queue (to free up resources, unblock other commands) 13452 * - RtR queue 13453 * - incoming queue 13454 * 13455 * If those queues are empty, we break out of the loop and 13456 * go to sleep. 13457 */ 13458 mtx_lock(&thr->queue_lock); 13459 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13460 if (io != NULL) { 13461 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13462 mtx_unlock(&thr->queue_lock); 13463 ctl_handle_isc(io); 13464 continue; 13465 } 13466 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13467 if (io != NULL) { 13468 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13469 /* clear any blocked commands, call fe_done */ 13470 mtx_unlock(&thr->queue_lock); 13471 retval = ctl_process_done(io); 13472 continue; 13473 } 13474 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13475 if (io != NULL) { 13476 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13477 mtx_unlock(&thr->queue_lock); 13478 if (io->io_hdr.io_type == CTL_IO_TASK) 13479 ctl_run_task(io); 13480 else 13481 ctl_scsiio_precheck(softc, &io->scsiio); 13482 continue; 13483 } 13484 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13485 if (io != NULL) { 13486 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13487 mtx_unlock(&thr->queue_lock); 13488 retval = ctl_scsiio(&io->scsiio); 13489 if (retval != CTL_RETVAL_COMPLETE) 13490 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13491 continue; 13492 } 13493 13494 /* Sleep until we have something to do. */ 13495 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13496 } 13497} 13498 13499static void 13500ctl_lun_thread(void *arg) 13501{ 13502 struct ctl_softc *softc = (struct ctl_softc *)arg; 13503 struct ctl_be_lun *be_lun; 13504 int retval; 13505 13506 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13507 13508 for (;;) { 13509 retval = 0; 13510 mtx_lock(&softc->ctl_lock); 13511 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13512 if (be_lun != NULL) { 13513 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13514 mtx_unlock(&softc->ctl_lock); 13515 ctl_create_lun(be_lun); 13516 continue; 13517 } 13518 13519 /* Sleep until we have something to do. */ 13520 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13521 PDROP | PRIBIO, "-", 0); 13522 } 13523} 13524 13525static void 13526ctl_thresh_thread(void *arg) 13527{ 13528 struct ctl_softc *softc = (struct ctl_softc *)arg; 13529 struct ctl_lun *lun; 13530 struct ctl_be_lun *be_lun; 13531 struct scsi_da_rw_recovery_page *rwpage; 13532 struct ctl_logical_block_provisioning_page *page; 13533 const char *attr; 13534 union ctl_ha_msg msg; 13535 uint64_t thres, val; 13536 int i, e, set; 13537 13538 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13539 13540 for (;;) { 13541 mtx_lock(&softc->ctl_lock); 13542 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13543 be_lun = lun->be_lun; 13544 if ((lun->flags & CTL_LUN_DISABLED) || 13545 (lun->flags & CTL_LUN_OFFLINE) || 13546 lun->backend->lun_attr == NULL) 13547 continue; 13548 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13549 softc->ha_mode == CTL_HA_MODE_XFER) 13550 continue; 13551 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13552 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13553 continue; 13554 e = 0; 13555 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13556 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13557 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13558 continue; 13559 thres = scsi_4btoul(page->descr[i].count); 13560 thres <<= CTL_LBP_EXPONENT; 13561 switch (page->descr[i].resource) { 13562 case 0x01: 13563 attr = "blocksavail"; 13564 break; 13565 case 0x02: 13566 attr = "blocksused"; 13567 break; 13568 case 0xf1: 13569 attr = "poolblocksavail"; 13570 break; 13571 case 0xf2: 13572 attr = "poolblocksused"; 13573 break; 13574 default: 13575 continue; 13576 } 13577 mtx_unlock(&softc->ctl_lock); // XXX 13578 val = lun->backend->lun_attr( 13579 lun->be_lun->be_lun, attr); 13580 mtx_lock(&softc->ctl_lock); 13581 if (val == UINT64_MAX) 13582 continue; 13583 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13584 == SLBPPD_ARMING_INC) 13585 e = (val >= thres); 13586 else 13587 e = (val <= thres); 13588 if (e) 13589 break; 13590 } 13591 mtx_lock(&lun->lun_lock); 13592 if (e) { 13593 scsi_u64to8b((uint8_t *)&page->descr[i] - 13594 (uint8_t *)page, lun->ua_tpt_info); 13595 if (lun->lasttpt == 0 || 13596 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13597 lun->lasttpt = time_uptime; 13598 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13599 set = 1; 13600 } else 13601 set = 0; 13602 } else { 13603 lun->lasttpt = 0; 13604 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13605 set = -1; 13606 } 13607 mtx_unlock(&lun->lun_lock); 13608 if (set != 0 && 13609 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13610 /* Send msg to other side. */ 13611 bzero(&msg.ua, sizeof(msg.ua)); 13612 msg.hdr.msg_type = CTL_MSG_UA; 13613 msg.hdr.nexus.initid = -1; 13614 msg.hdr.nexus.targ_port = -1; 13615 msg.hdr.nexus.targ_lun = lun->lun; 13616 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13617 msg.ua.ua_all = 1; 13618 msg.ua.ua_set = (set > 0); 13619 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13620 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13621 mtx_unlock(&softc->ctl_lock); // XXX 13622 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13623 sizeof(msg.ua), M_WAITOK); 13624 mtx_lock(&softc->ctl_lock); 13625 } 13626 } 13627 mtx_unlock(&softc->ctl_lock); 13628 pause("-", CTL_LBP_PERIOD * hz); 13629 } 13630} 13631 13632static void 13633ctl_enqueue_incoming(union ctl_io *io) 13634{ 13635 struct ctl_softc *softc = control_softc; 13636 struct ctl_thread *thr; 13637 u_int idx; 13638 13639 idx = (io->io_hdr.nexus.targ_port * 127 + 13640 io->io_hdr.nexus.initid) % worker_threads; 13641 thr = &softc->threads[idx]; 13642 mtx_lock(&thr->queue_lock); 13643 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13644 mtx_unlock(&thr->queue_lock); 13645 wakeup(thr); 13646} 13647 13648static void 13649ctl_enqueue_rtr(union ctl_io *io) 13650{ 13651 struct ctl_softc *softc = control_softc; 13652 struct ctl_thread *thr; 13653 13654 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13655 mtx_lock(&thr->queue_lock); 13656 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13657 mtx_unlock(&thr->queue_lock); 13658 wakeup(thr); 13659} 13660 13661static void 13662ctl_enqueue_done(union ctl_io *io) 13663{ 13664 struct ctl_softc *softc = control_softc; 13665 struct ctl_thread *thr; 13666 13667 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13668 mtx_lock(&thr->queue_lock); 13669 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13670 mtx_unlock(&thr->queue_lock); 13671 wakeup(thr); 13672} 13673 13674static void 13675ctl_enqueue_isc(union ctl_io *io) 13676{ 13677 struct ctl_softc *softc = control_softc; 13678 struct ctl_thread *thr; 13679 13680 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13681 mtx_lock(&thr->queue_lock); 13682 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13683 mtx_unlock(&thr->queue_lock); 13684 wakeup(thr); 13685} 13686 13687/* 13688 * vim: ts=8 13689 */ 13690