ctl.c revision 287754
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 287754 2015-09-13 14:04:42Z 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 264const static struct scsi_info_exceptions_page ie_page_default = { 265 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 266 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 267 /*info_flags*/SIEP_FLAGS_DEXCPT, 268 /*mrie*/0, 269 /*interval_timer*/{0, 0, 0, 0}, 270 /*report_count*/{0, 0, 0, 0} 271}; 272 273const static struct scsi_info_exceptions_page ie_page_changeable = { 274 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 275 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 276 /*info_flags*/0, 277 /*mrie*/0, 278 /*interval_timer*/{0, 0, 0, 0}, 279 /*report_count*/{0, 0, 0, 0} 280}; 281 282#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 283 284const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 286 /*subpage_code*/0x02, 287 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 288 /*flags*/0, 289 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 290 /*descr*/{}}, 291 {{/*flags*/0, 292 /*resource*/0x01, 293 /*reserved*/{0, 0}, 294 /*count*/{0, 0, 0, 0}}, 295 {/*flags*/0, 296 /*resource*/0x02, 297 /*reserved*/{0, 0}, 298 /*count*/{0, 0, 0, 0}}, 299 {/*flags*/0, 300 /*resource*/0xf1, 301 /*reserved*/{0, 0}, 302 /*count*/{0, 0, 0, 0}}, 303 {/*flags*/0, 304 /*resource*/0xf2, 305 /*reserved*/{0, 0}, 306 /*count*/{0, 0, 0, 0}} 307 } 308}; 309 310const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 311 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 312 /*subpage_code*/0x02, 313 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 314 /*flags*/0, 315 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 316 /*descr*/{}}, 317 {{/*flags*/0, 318 /*resource*/0, 319 /*reserved*/{0, 0}, 320 /*count*/{0, 0, 0, 0}}, 321 {/*flags*/0, 322 /*resource*/0, 323 /*reserved*/{0, 0}, 324 /*count*/{0, 0, 0, 0}}, 325 {/*flags*/0, 326 /*resource*/0, 327 /*reserved*/{0, 0}, 328 /*count*/{0, 0, 0, 0}}, 329 {/*flags*/0, 330 /*resource*/0, 331 /*reserved*/{0, 0}, 332 /*count*/{0, 0, 0, 0}} 333 } 334}; 335 336SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 337static int worker_threads = -1; 338SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 339 &worker_threads, 1, "Number of worker threads"); 340static int ctl_debug = CTL_DEBUG_NONE; 341SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 342 &ctl_debug, 0, "Enabled debug flags"); 343 344/* 345 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 346 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 347 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 348 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 349 */ 350#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 351 352static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 353 int param); 354static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 355static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 356static int ctl_init(void); 357void ctl_shutdown(void); 358static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 359static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 360static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 361static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 362 struct ctl_ooa *ooa_hdr, 363 struct ctl_ooa_entry *kern_entries); 364static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 365 struct thread *td); 366static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 367 struct ctl_be_lun *be_lun); 368static int ctl_free_lun(struct ctl_lun *lun); 369static void ctl_create_lun(struct ctl_be_lun *be_lun); 370static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 371 372static int ctl_do_mode_select(union ctl_io *io); 373static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 374 uint64_t res_key, uint64_t sa_res_key, 375 uint8_t type, uint32_t residx, 376 struct ctl_scsiio *ctsio, 377 struct scsi_per_res_out *cdb, 378 struct scsi_per_res_out_parms* param); 379static void ctl_pro_preempt_other(struct ctl_lun *lun, 380 union ctl_ha_msg *msg); 381static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 382static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 383static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 384static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 385static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 386static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 387static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 388 int alloc_len); 389static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 390 int alloc_len); 391static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 392static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 393static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 394static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 395static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 396static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 397 bool seq); 398static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 399static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 400 union ctl_io *pending_io, union ctl_io *ooa_io); 401static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 402 union ctl_io *starting_io); 403static int ctl_check_blocked(struct ctl_lun *lun); 404static int ctl_scsiio_lun_check(struct ctl_lun *lun, 405 const struct ctl_cmd_entry *entry, 406 struct ctl_scsiio *ctsio); 407static void ctl_failover_lun(struct ctl_lun *lun); 408static void ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua); 409static void ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, 410 ctl_ua_type ua); 411static void ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua); 412static void ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua); 413static void ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua); 414static void ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 415 ctl_ua_type ua_type); 416static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 417 struct ctl_scsiio *ctsio); 418static int ctl_scsiio(struct ctl_scsiio *ctsio); 419 420static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 421static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 422 ctl_ua_type ua_type); 423static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 424 ctl_ua_type ua_type); 425static int ctl_abort_task(union ctl_io *io); 426static int ctl_abort_task_set(union ctl_io *io); 427static int ctl_i_t_nexus_reset(union ctl_io *io); 428static void ctl_run_task(union ctl_io *io); 429#ifdef CTL_IO_DELAY 430static void ctl_datamove_timer_wakeup(void *arg); 431static void ctl_done_timer_wakeup(void *arg); 432#endif /* CTL_IO_DELAY */ 433 434static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 435static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 436static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 437static void ctl_datamove_remote_write(union ctl_io *io); 438static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 439static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 440static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 441static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 442 ctl_ha_dt_cb callback); 443static void ctl_datamove_remote_read(union ctl_io *io); 444static void ctl_datamove_remote(union ctl_io *io); 445static int ctl_process_done(union ctl_io *io); 446static void ctl_lun_thread(void *arg); 447static void ctl_thresh_thread(void *arg); 448static void ctl_work_thread(void *arg); 449static void ctl_enqueue_incoming(union ctl_io *io); 450static void ctl_enqueue_rtr(union ctl_io *io); 451static void ctl_enqueue_done(union ctl_io *io); 452static void ctl_enqueue_isc(union ctl_io *io); 453static const struct ctl_cmd_entry * 454 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 455static const struct ctl_cmd_entry * 456 ctl_validate_command(struct ctl_scsiio *ctsio); 457static int ctl_cmd_applicable(uint8_t lun_type, 458 const struct ctl_cmd_entry *entry); 459 460static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 461static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 462static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 463static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 464 465/* 466 * Load the serialization table. This isn't very pretty, but is probably 467 * the easiest way to do it. 468 */ 469#include "ctl_ser_table.c" 470 471/* 472 * We only need to define open, close and ioctl routines for this driver. 473 */ 474static struct cdevsw ctl_cdevsw = { 475 .d_version = D_VERSION, 476 .d_flags = 0, 477 .d_open = ctl_open, 478 .d_close = ctl_close, 479 .d_ioctl = ctl_ioctl, 480 .d_name = "ctl", 481}; 482 483 484MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 485 486static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 487 488static moduledata_t ctl_moduledata = { 489 "ctl", 490 ctl_module_event_handler, 491 NULL 492}; 493 494DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 495MODULE_VERSION(ctl, 1); 496 497static struct ctl_frontend ha_frontend = 498{ 499 .name = "ha", 500}; 501 502static void 503ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 504 union ctl_ha_msg *msg_info) 505{ 506 struct ctl_scsiio *ctsio; 507 508 if (msg_info->hdr.original_sc == NULL) { 509 printf("%s: original_sc == NULL!\n", __func__); 510 /* XXX KDM now what? */ 511 return; 512 } 513 514 ctsio = &msg_info->hdr.original_sc->scsiio; 515 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 516 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 517 ctsio->io_hdr.status = msg_info->hdr.status; 518 ctsio->scsi_status = msg_info->scsi.scsi_status; 519 ctsio->sense_len = msg_info->scsi.sense_len; 520 ctsio->sense_residual = msg_info->scsi.sense_residual; 521 ctsio->residual = msg_info->scsi.residual; 522 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 523 msg_info->scsi.sense_len); 524 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 525 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 526 ctl_enqueue_isc((union ctl_io *)ctsio); 527} 528 529static void 530ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 531 union ctl_ha_msg *msg_info) 532{ 533 struct ctl_scsiio *ctsio; 534 535 if (msg_info->hdr.serializing_sc == NULL) { 536 printf("%s: serializing_sc == NULL!\n", __func__); 537 /* XXX KDM now what? */ 538 return; 539 } 540 541 ctsio = &msg_info->hdr.serializing_sc->scsiio; 542 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 543 ctl_enqueue_isc((union ctl_io *)ctsio); 544} 545 546void 547ctl_isc_announce_lun(struct ctl_lun *lun) 548{ 549 struct ctl_softc *softc = lun->ctl_softc; 550 union ctl_ha_msg *msg; 551 struct ctl_ha_msg_lun_pr_key pr_key; 552 int i, k; 553 554 if (softc->ha_link != CTL_HA_LINK_ONLINE) 555 return; 556 mtx_lock(&lun->lun_lock); 557 i = sizeof(msg->lun); 558 if (lun->lun_devid) 559 i += lun->lun_devid->len; 560 i += sizeof(pr_key) * lun->pr_key_count; 561alloc: 562 mtx_unlock(&lun->lun_lock); 563 msg = malloc(i, M_CTL, M_WAITOK); 564 mtx_lock(&lun->lun_lock); 565 k = sizeof(msg->lun); 566 if (lun->lun_devid) 567 k += lun->lun_devid->len; 568 k += sizeof(pr_key) * lun->pr_key_count; 569 if (i < k) { 570 free(msg, M_CTL); 571 i = k; 572 goto alloc; 573 } 574 bzero(&msg->lun, sizeof(msg->lun)); 575 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 576 msg->hdr.nexus.targ_lun = lun->lun; 577 msg->hdr.nexus.targ_mapped_lun = lun->lun; 578 msg->lun.flags = lun->flags; 579 msg->lun.pr_generation = lun->PRGeneration; 580 msg->lun.pr_res_idx = lun->pr_res_idx; 581 msg->lun.pr_res_type = lun->res_type; 582 msg->lun.pr_key_count = lun->pr_key_count; 583 i = 0; 584 if (lun->lun_devid) { 585 msg->lun.lun_devid_len = lun->lun_devid->len; 586 memcpy(&msg->lun.data[i], lun->lun_devid->data, 587 msg->lun.lun_devid_len); 588 i += msg->lun.lun_devid_len; 589 } 590 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 591 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 592 continue; 593 pr_key.pr_iid = k; 594 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 595 i += sizeof(pr_key); 596 } 597 mtx_unlock(&lun->lun_lock); 598 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 599 M_WAITOK); 600 free(msg, M_CTL); 601} 602 603void 604ctl_isc_announce_port(struct ctl_port *port) 605{ 606 struct ctl_softc *softc = control_softc; 607 union ctl_ha_msg *msg; 608 int i; 609 610 if (port->targ_port < softc->port_min || 611 port->targ_port >= softc->port_max || 612 softc->ha_link != CTL_HA_LINK_ONLINE) 613 return; 614 i = sizeof(msg->port) + strlen(port->port_name) + 1; 615 if (port->lun_map) 616 i += sizeof(uint32_t) * CTL_MAX_LUNS; 617 if (port->port_devid) 618 i += port->port_devid->len; 619 if (port->target_devid) 620 i += port->target_devid->len; 621 msg = malloc(i, M_CTL, M_WAITOK); 622 bzero(&msg->port, sizeof(msg->port)); 623 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 624 msg->hdr.nexus.targ_port = port->targ_port; 625 msg->port.port_type = port->port_type; 626 msg->port.physical_port = port->physical_port; 627 msg->port.virtual_port = port->virtual_port; 628 msg->port.status = port->status; 629 i = 0; 630 msg->port.name_len = sprintf(&msg->port.data[i], 631 "%d:%s", softc->ha_id, port->port_name) + 1; 632 i += msg->port.name_len; 633 if (port->lun_map) { 634 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS; 635 memcpy(&msg->port.data[i], port->lun_map, 636 msg->port.lun_map_len); 637 i += msg->port.lun_map_len; 638 } 639 if (port->port_devid) { 640 msg->port.port_devid_len = port->port_devid->len; 641 memcpy(&msg->port.data[i], port->port_devid->data, 642 msg->port.port_devid_len); 643 i += msg->port.port_devid_len; 644 } 645 if (port->target_devid) { 646 msg->port.target_devid_len = port->target_devid->len; 647 memcpy(&msg->port.data[i], port->target_devid->data, 648 msg->port.target_devid_len); 649 i += msg->port.target_devid_len; 650 } 651 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 652 M_WAITOK); 653 free(msg, M_CTL); 654} 655 656static void 657ctl_isc_ha_link_up(struct ctl_softc *softc) 658{ 659 struct ctl_port *port; 660 struct ctl_lun *lun; 661 662 STAILQ_FOREACH(port, &softc->port_list, links) 663 ctl_isc_announce_port(port); 664 STAILQ_FOREACH(lun, &softc->lun_list, links) 665 ctl_isc_announce_lun(lun); 666} 667 668static void 669ctl_isc_ha_link_down(struct ctl_softc *softc) 670{ 671 struct ctl_port *port; 672 struct ctl_lun *lun; 673 union ctl_io *io; 674 675 mtx_lock(&softc->ctl_lock); 676 STAILQ_FOREACH(lun, &softc->lun_list, links) { 677 mtx_lock(&lun->lun_lock); 678 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 679 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 680 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 681 } 682 mtx_unlock(&lun->lun_lock); 683 684 mtx_unlock(&softc->ctl_lock); 685 io = ctl_alloc_io(softc->othersc_pool); 686 mtx_lock(&softc->ctl_lock); 687 ctl_zero_io(io); 688 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 689 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 690 ctl_enqueue_isc(io); 691 } 692 693 STAILQ_FOREACH(port, &softc->port_list, links) { 694 if (port->targ_port >= softc->port_min && 695 port->targ_port < softc->port_max) 696 continue; 697 port->status &= ~CTL_PORT_STATUS_ONLINE; 698 } 699 mtx_unlock(&softc->ctl_lock); 700} 701 702static void 703ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 704{ 705 struct ctl_lun *lun; 706 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 707 708 mtx_lock(&softc->ctl_lock); 709 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS && 710 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) { 711 mtx_lock(&lun->lun_lock); 712 mtx_unlock(&softc->ctl_lock); 713 if (msg->ua.ua_all) { 714 if (msg->ua.ua_set) 715 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 716 else 717 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 718 } else { 719 if (msg->ua.ua_set) 720 ctl_est_ua(lun, iid, msg->ua.ua_type); 721 else 722 ctl_clr_ua(lun, iid, msg->ua.ua_type); 723 } 724 mtx_unlock(&lun->lun_lock); 725 } else 726 mtx_unlock(&softc->ctl_lock); 727} 728 729static void 730ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 731{ 732 struct ctl_lun *lun; 733 struct ctl_ha_msg_lun_pr_key pr_key; 734 int i, k; 735 ctl_lun_flags oflags; 736 uint32_t targ_lun; 737 738 targ_lun = msg->hdr.nexus.targ_mapped_lun; 739 mtx_lock(&softc->ctl_lock); 740 if ((targ_lun >= CTL_MAX_LUNS) || 741 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 742 mtx_unlock(&softc->ctl_lock); 743 return; 744 } 745 mtx_lock(&lun->lun_lock); 746 mtx_unlock(&softc->ctl_lock); 747 if (lun->flags & CTL_LUN_DISABLED) { 748 mtx_unlock(&lun->lun_lock); 749 return; 750 } 751 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 752 if (msg->lun.lun_devid_len != i || (i > 0 && 753 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 754 mtx_unlock(&lun->lun_lock); 755 printf("%s: Received conflicting HA LUN %d\n", 756 __func__, msg->hdr.nexus.targ_lun); 757 return; 758 } else { 759 /* Record whether peer is primary. */ 760 oflags = lun->flags; 761 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 762 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 763 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 764 else 765 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 766 if (oflags != lun->flags) 767 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 768 769 /* If peer is primary and we are not -- use data */ 770 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 771 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 772 lun->PRGeneration = msg->lun.pr_generation; 773 lun->pr_res_idx = msg->lun.pr_res_idx; 774 lun->res_type = msg->lun.pr_res_type; 775 lun->pr_key_count = msg->lun.pr_key_count; 776 for (k = 0; k < CTL_MAX_INITIATORS; k++) 777 ctl_clr_prkey(lun, k); 778 for (k = 0; k < msg->lun.pr_key_count; k++) { 779 memcpy(&pr_key, &msg->lun.data[i], 780 sizeof(pr_key)); 781 ctl_alloc_prkey(lun, pr_key.pr_iid); 782 ctl_set_prkey(lun, pr_key.pr_iid, 783 pr_key.pr_key); 784 i += sizeof(pr_key); 785 } 786 } 787 788 mtx_unlock(&lun->lun_lock); 789 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 790 __func__, msg->hdr.nexus.targ_lun, 791 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 792 "primary" : "secondary")); 793 794 /* If we are primary but peer doesn't know -- notify */ 795 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 796 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 797 ctl_isc_announce_lun(lun); 798 } 799} 800 801static void 802ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 803{ 804 struct ctl_port *port; 805 int i, new; 806 807 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 808 if (port == NULL) { 809 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 810 msg->hdr.nexus.targ_port)); 811 new = 1; 812 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 813 port->frontend = &ha_frontend; 814 port->targ_port = msg->hdr.nexus.targ_port; 815 } else if (port->frontend == &ha_frontend) { 816 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 817 msg->hdr.nexus.targ_port)); 818 new = 0; 819 } else { 820 printf("%s: Received conflicting HA port %d\n", 821 __func__, msg->hdr.nexus.targ_port); 822 return; 823 } 824 port->port_type = msg->port.port_type; 825 port->physical_port = msg->port.physical_port; 826 port->virtual_port = msg->port.virtual_port; 827 port->status = msg->port.status; 828 i = 0; 829 free(port->port_name, M_CTL); 830 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 831 M_CTL); 832 i += msg->port.name_len; 833 if (msg->port.lun_map_len != 0) { 834 if (port->lun_map == NULL) 835 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 836 M_CTL, M_WAITOK); 837 memcpy(port->lun_map, &msg->port.data[i], 838 sizeof(uint32_t) * CTL_MAX_LUNS); 839 i += msg->port.lun_map_len; 840 } else { 841 free(port->lun_map, M_CTL); 842 port->lun_map = NULL; 843 } 844 if (msg->port.port_devid_len != 0) { 845 if (port->port_devid == NULL || 846 port->port_devid->len != msg->port.port_devid_len) { 847 free(port->port_devid, M_CTL); 848 port->port_devid = malloc(sizeof(struct ctl_devid) + 849 msg->port.port_devid_len, M_CTL, M_WAITOK); 850 } 851 memcpy(port->port_devid->data, &msg->port.data[i], 852 msg->port.port_devid_len); 853 port->port_devid->len = msg->port.port_devid_len; 854 i += msg->port.port_devid_len; 855 } else { 856 free(port->port_devid, M_CTL); 857 port->port_devid = NULL; 858 } 859 if (msg->port.target_devid_len != 0) { 860 if (port->target_devid == NULL || 861 port->target_devid->len != msg->port.target_devid_len) { 862 free(port->target_devid, M_CTL); 863 port->target_devid = malloc(sizeof(struct ctl_devid) + 864 msg->port.target_devid_len, M_CTL, M_WAITOK); 865 } 866 memcpy(port->target_devid->data, &msg->port.data[i], 867 msg->port.target_devid_len); 868 port->target_devid->len = msg->port.target_devid_len; 869 i += msg->port.target_devid_len; 870 } else { 871 free(port->port_devid, M_CTL); 872 port->port_devid = NULL; 873 } 874 if (new) { 875 if (ctl_port_register(port) != 0) { 876 printf("%s: ctl_port_register() failed with error\n", 877 __func__); 878 } 879 } 880} 881 882/* 883 * ISC (Inter Shelf Communication) event handler. Events from the HA 884 * subsystem come in here. 885 */ 886static void 887ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 888{ 889 struct ctl_softc *softc; 890 union ctl_io *io; 891 struct ctl_prio *presio; 892 ctl_ha_status isc_status; 893 894 softc = control_softc; 895 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 896 if (event == CTL_HA_EVT_MSG_RECV) { 897 union ctl_ha_msg *msg, msgbuf; 898 899 if (param > sizeof(msgbuf)) 900 msg = malloc(param, M_CTL, M_WAITOK); 901 else 902 msg = &msgbuf; 903 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 904 M_WAITOK); 905 if (isc_status != CTL_HA_STATUS_SUCCESS) { 906 printf("%s: Error receiving message: %d\n", 907 __func__, isc_status); 908 if (msg != &msgbuf) 909 free(msg, M_CTL); 910 return; 911 } 912 913 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 914 switch (msg->hdr.msg_type) { 915 case CTL_MSG_SERIALIZE: 916 io = ctl_alloc_io(softc->othersc_pool); 917 ctl_zero_io(io); 918 // populate ctsio from msg 919 io->io_hdr.io_type = CTL_IO_SCSI; 920 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 921 io->io_hdr.original_sc = msg->hdr.original_sc; 922 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 923 CTL_FLAG_IO_ACTIVE; 924 /* 925 * If we're in serialization-only mode, we don't 926 * want to go through full done processing. Thus 927 * the COPY flag. 928 * 929 * XXX KDM add another flag that is more specific. 930 */ 931 if (softc->ha_mode != CTL_HA_MODE_XFER) 932 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 933 io->io_hdr.nexus = msg->hdr.nexus; 934#if 0 935 printf("port %u, iid %u, lun %u\n", 936 io->io_hdr.nexus.targ_port, 937 io->io_hdr.nexus.initid, 938 io->io_hdr.nexus.targ_lun); 939#endif 940 io->scsiio.tag_num = msg->scsi.tag_num; 941 io->scsiio.tag_type = msg->scsi.tag_type; 942#ifdef CTL_TIME_IO 943 io->io_hdr.start_time = time_uptime; 944 getbintime(&io->io_hdr.start_bt); 945#endif /* CTL_TIME_IO */ 946 io->scsiio.cdb_len = msg->scsi.cdb_len; 947 memcpy(io->scsiio.cdb, msg->scsi.cdb, 948 CTL_MAX_CDBLEN); 949 if (softc->ha_mode == CTL_HA_MODE_XFER) { 950 const struct ctl_cmd_entry *entry; 951 952 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 953 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 954 io->io_hdr.flags |= 955 entry->flags & CTL_FLAG_DATA_MASK; 956 } 957 ctl_enqueue_isc(io); 958 break; 959 960 /* Performed on the Originating SC, XFER mode only */ 961 case CTL_MSG_DATAMOVE: { 962 struct ctl_sg_entry *sgl; 963 int i, j; 964 965 io = msg->hdr.original_sc; 966 if (io == NULL) { 967 printf("%s: original_sc == NULL!\n", __func__); 968 /* XXX KDM do something here */ 969 break; 970 } 971 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 972 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 973 /* 974 * Keep track of this, we need to send it back over 975 * when the datamove is complete. 976 */ 977 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 978 979 if (msg->dt.sg_sequence == 0) { 980 i = msg->dt.kern_sg_entries + 981 io->scsiio.kern_data_len / 982 CTL_HA_DATAMOVE_SEGMENT + 1; 983 sgl = malloc(sizeof(*sgl) * i, M_CTL, 984 M_WAITOK | M_ZERO); 985 io->io_hdr.remote_sglist = sgl; 986 io->io_hdr.local_sglist = 987 &sgl[msg->dt.kern_sg_entries]; 988 989 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 990 991 io->scsiio.kern_sg_entries = 992 msg->dt.kern_sg_entries; 993 io->scsiio.rem_sg_entries = 994 msg->dt.kern_sg_entries; 995 io->scsiio.kern_data_len = 996 msg->dt.kern_data_len; 997 io->scsiio.kern_total_len = 998 msg->dt.kern_total_len; 999 io->scsiio.kern_data_resid = 1000 msg->dt.kern_data_resid; 1001 io->scsiio.kern_rel_offset = 1002 msg->dt.kern_rel_offset; 1003 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1004 io->io_hdr.flags |= msg->dt.flags & 1005 CTL_FLAG_BUS_ADDR; 1006 } else 1007 sgl = (struct ctl_sg_entry *) 1008 io->scsiio.kern_data_ptr; 1009 1010 for (i = msg->dt.sent_sg_entries, j = 0; 1011 i < (msg->dt.sent_sg_entries + 1012 msg->dt.cur_sg_entries); i++, j++) { 1013 sgl[i].addr = msg->dt.sg_list[j].addr; 1014 sgl[i].len = msg->dt.sg_list[j].len; 1015 1016#if 0 1017 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 1018 __func__, 1019 msg->dt.sg_list[j].addr, 1020 msg->dt.sg_list[j].len, 1021 sgl[i].addr, sgl[i].len, j, i); 1022#endif 1023 } 1024 1025 /* 1026 * If this is the last piece of the I/O, we've got 1027 * the full S/G list. Queue processing in the thread. 1028 * Otherwise wait for the next piece. 1029 */ 1030 if (msg->dt.sg_last != 0) 1031 ctl_enqueue_isc(io); 1032 break; 1033 } 1034 /* Performed on the Serializing (primary) SC, XFER mode only */ 1035 case CTL_MSG_DATAMOVE_DONE: { 1036 if (msg->hdr.serializing_sc == NULL) { 1037 printf("%s: serializing_sc == NULL!\n", 1038 __func__); 1039 /* XXX KDM now what? */ 1040 break; 1041 } 1042 /* 1043 * We grab the sense information here in case 1044 * there was a failure, so we can return status 1045 * back to the initiator. 1046 */ 1047 io = msg->hdr.serializing_sc; 1048 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1049 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1050 io->io_hdr.port_status = msg->scsi.fetd_status; 1051 io->scsiio.residual = msg->scsi.residual; 1052 if (msg->hdr.status != CTL_STATUS_NONE) { 1053 io->io_hdr.status = msg->hdr.status; 1054 io->scsiio.scsi_status = msg->scsi.scsi_status; 1055 io->scsiio.sense_len = msg->scsi.sense_len; 1056 io->scsiio.sense_residual =msg->scsi.sense_residual; 1057 memcpy(&io->scsiio.sense_data, 1058 &msg->scsi.sense_data, 1059 msg->scsi.sense_len); 1060 } 1061 ctl_enqueue_isc(io); 1062 break; 1063 } 1064 1065 /* Preformed on Originating SC, SER_ONLY mode */ 1066 case CTL_MSG_R2R: 1067 io = msg->hdr.original_sc; 1068 if (io == NULL) { 1069 printf("%s: original_sc == NULL!\n", 1070 __func__); 1071 break; 1072 } 1073 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1074 io->io_hdr.msg_type = CTL_MSG_R2R; 1075 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1076 ctl_enqueue_isc(io); 1077 break; 1078 1079 /* 1080 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1081 * mode. 1082 * Performed on the Originating (i.e. secondary) SC in XFER 1083 * mode 1084 */ 1085 case CTL_MSG_FINISH_IO: 1086 if (softc->ha_mode == CTL_HA_MODE_XFER) 1087 ctl_isc_handler_finish_xfer(softc, msg); 1088 else 1089 ctl_isc_handler_finish_ser_only(softc, msg); 1090 break; 1091 1092 /* Preformed on Originating SC */ 1093 case CTL_MSG_BAD_JUJU: 1094 io = msg->hdr.original_sc; 1095 if (io == NULL) { 1096 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1097 __func__); 1098 break; 1099 } 1100 ctl_copy_sense_data(msg, io); 1101 /* 1102 * IO should have already been cleaned up on other 1103 * SC so clear this flag so we won't send a message 1104 * back to finish the IO there. 1105 */ 1106 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1107 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1108 1109 /* io = msg->hdr.serializing_sc; */ 1110 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1111 ctl_enqueue_isc(io); 1112 break; 1113 1114 /* Handle resets sent from the other side */ 1115 case CTL_MSG_MANAGE_TASKS: { 1116 struct ctl_taskio *taskio; 1117 taskio = (struct ctl_taskio *)ctl_alloc_io( 1118 softc->othersc_pool); 1119 ctl_zero_io((union ctl_io *)taskio); 1120 taskio->io_hdr.io_type = CTL_IO_TASK; 1121 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1122 taskio->io_hdr.nexus = msg->hdr.nexus; 1123 taskio->task_action = msg->task.task_action; 1124 taskio->tag_num = msg->task.tag_num; 1125 taskio->tag_type = msg->task.tag_type; 1126#ifdef CTL_TIME_IO 1127 taskio->io_hdr.start_time = time_uptime; 1128 getbintime(&taskio->io_hdr.start_bt); 1129#endif /* CTL_TIME_IO */ 1130 ctl_run_task((union ctl_io *)taskio); 1131 break; 1132 } 1133 /* Persistent Reserve action which needs attention */ 1134 case CTL_MSG_PERS_ACTION: 1135 presio = (struct ctl_prio *)ctl_alloc_io( 1136 softc->othersc_pool); 1137 ctl_zero_io((union ctl_io *)presio); 1138 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1139 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1140 presio->io_hdr.nexus = msg->hdr.nexus; 1141 presio->pr_msg = msg->pr; 1142 ctl_enqueue_isc((union ctl_io *)presio); 1143 break; 1144 case CTL_MSG_UA: 1145 ctl_isc_ua(softc, msg, param); 1146 break; 1147 case CTL_MSG_PORT_SYNC: 1148 ctl_isc_port_sync(softc, msg, param); 1149 break; 1150 case CTL_MSG_LUN_SYNC: 1151 ctl_isc_lun_sync(softc, msg, param); 1152 break; 1153 default: 1154 printf("Received HA message of unknown type %d\n", 1155 msg->hdr.msg_type); 1156 break; 1157 } 1158 if (msg != &msgbuf) 1159 free(msg, M_CTL); 1160 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1161 printf("CTL: HA link status changed from %d to %d\n", 1162 softc->ha_link, param); 1163 if (param == softc->ha_link) 1164 return; 1165 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1166 softc->ha_link = param; 1167 ctl_isc_ha_link_down(softc); 1168 } else { 1169 softc->ha_link = param; 1170 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1171 ctl_isc_ha_link_up(softc); 1172 } 1173 return; 1174 } else { 1175 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1176 return; 1177 } 1178} 1179 1180static void 1181ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1182{ 1183 1184 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1185 src->scsi.sense_len); 1186 dest->scsiio.scsi_status = src->scsi.scsi_status; 1187 dest->scsiio.sense_len = src->scsi.sense_len; 1188 dest->io_hdr.status = src->hdr.status; 1189} 1190 1191static void 1192ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1193{ 1194 1195 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1196 src->scsiio.sense_len); 1197 dest->scsi.scsi_status = src->scsiio.scsi_status; 1198 dest->scsi.sense_len = src->scsiio.sense_len; 1199 dest->hdr.status = src->io_hdr.status; 1200} 1201 1202static void 1203ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1204{ 1205 struct ctl_softc *softc = lun->ctl_softc; 1206 ctl_ua_type *pu; 1207 1208 if (initidx < softc->init_min || initidx >= softc->init_max) 1209 return; 1210 mtx_assert(&lun->lun_lock, MA_OWNED); 1211 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1212 if (pu == NULL) 1213 return; 1214 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1215} 1216 1217static void 1218ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1219{ 1220 int i; 1221 1222 mtx_assert(&lun->lun_lock, MA_OWNED); 1223 if (lun->pending_ua[port] == NULL) 1224 return; 1225 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1226 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1227 continue; 1228 lun->pending_ua[port][i] |= ua; 1229 } 1230} 1231 1232static void 1233ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1234{ 1235 struct ctl_softc *softc = lun->ctl_softc; 1236 int i; 1237 1238 mtx_assert(&lun->lun_lock, MA_OWNED); 1239 for (i = softc->port_min; i < softc->port_max; i++) 1240 ctl_est_ua_port(lun, i, except, ua); 1241} 1242 1243static void 1244ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1245{ 1246 struct ctl_softc *softc = lun->ctl_softc; 1247 ctl_ua_type *pu; 1248 1249 if (initidx < softc->init_min || initidx >= softc->init_max) 1250 return; 1251 mtx_assert(&lun->lun_lock, MA_OWNED); 1252 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1253 if (pu == NULL) 1254 return; 1255 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1256} 1257 1258static void 1259ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1260{ 1261 struct ctl_softc *softc = lun->ctl_softc; 1262 int i, j; 1263 1264 mtx_assert(&lun->lun_lock, MA_OWNED); 1265 for (i = softc->port_min; i < softc->port_max; i++) { 1266 if (lun->pending_ua[i] == NULL) 1267 continue; 1268 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1269 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1270 continue; 1271 lun->pending_ua[i][j] &= ~ua; 1272 } 1273 } 1274} 1275 1276static void 1277ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1278 ctl_ua_type ua_type) 1279{ 1280 struct ctl_lun *lun; 1281 1282 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1283 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1284 mtx_lock(&lun->lun_lock); 1285 ctl_clr_ua(lun, initidx, ua_type); 1286 mtx_unlock(&lun->lun_lock); 1287 } 1288} 1289 1290static int 1291ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1292{ 1293 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1294 struct ctl_lun *lun; 1295 struct ctl_lun_req ireq; 1296 int error, value; 1297 1298 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1299 error = sysctl_handle_int(oidp, &value, 0, req); 1300 if ((error != 0) || (req->newptr == NULL)) 1301 return (error); 1302 1303 mtx_lock(&softc->ctl_lock); 1304 if (value == 0) 1305 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1306 else 1307 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1308 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1309 mtx_unlock(&softc->ctl_lock); 1310 bzero(&ireq, sizeof(ireq)); 1311 ireq.reqtype = CTL_LUNREQ_MODIFY; 1312 ireq.reqdata.modify.lun_id = lun->lun; 1313 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1314 curthread); 1315 if (ireq.status != CTL_LUN_OK) { 1316 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1317 __func__, ireq.status, ireq.error_str); 1318 } 1319 mtx_lock(&softc->ctl_lock); 1320 } 1321 mtx_unlock(&softc->ctl_lock); 1322 return (0); 1323} 1324 1325static int 1326ctl_init(void) 1327{ 1328 struct ctl_softc *softc; 1329 void *other_pool; 1330 int i, error, retval; 1331 1332 retval = 0; 1333 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1334 M_WAITOK | M_ZERO); 1335 softc = control_softc; 1336 1337 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1338 "cam/ctl"); 1339 1340 softc->dev->si_drv1 = softc; 1341 1342 sysctl_ctx_init(&softc->sysctl_ctx); 1343 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1344 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1345 CTLFLAG_RD, 0, "CAM Target Layer"); 1346 1347 if (softc->sysctl_tree == NULL) { 1348 printf("%s: unable to allocate sysctl tree\n", __func__); 1349 destroy_dev(softc->dev); 1350 free(control_softc, M_DEVBUF); 1351 control_softc = NULL; 1352 return (ENOMEM); 1353 } 1354 1355 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1356 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1357 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1358 softc->open_count = 0; 1359 1360 /* 1361 * Default to actually sending a SYNCHRONIZE CACHE command down to 1362 * the drive. 1363 */ 1364 softc->flags = CTL_FLAG_REAL_SYNC; 1365 1366 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1367 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1368 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1369 1370 /* 1371 * In Copan's HA scheme, the "master" and "slave" roles are 1372 * figured out through the slot the controller is in. Although it 1373 * is an active/active system, someone has to be in charge. 1374 */ 1375 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1376 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1377 "HA head ID (0 - no HA)"); 1378 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) { 1379 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1380 softc->is_single = 1; 1381 softc->port_cnt = CTL_MAX_PORTS; 1382 softc->port_min = 0; 1383 } else { 1384 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS; 1385 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1386 } 1387 softc->port_max = softc->port_min + softc->port_cnt; 1388 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1389 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1390 1391 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1392 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1393 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1394 1395 STAILQ_INIT(&softc->lun_list); 1396 STAILQ_INIT(&softc->pending_lun_queue); 1397 STAILQ_INIT(&softc->fe_list); 1398 STAILQ_INIT(&softc->port_list); 1399 STAILQ_INIT(&softc->be_list); 1400 ctl_tpc_init(softc); 1401 1402 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1403 &other_pool) != 0) 1404 { 1405 printf("ctl: can't allocate %d entry other SC pool, " 1406 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1407 return (ENOMEM); 1408 } 1409 softc->othersc_pool = other_pool; 1410 1411 if (worker_threads <= 0) 1412 worker_threads = max(1, mp_ncpus / 4); 1413 if (worker_threads > CTL_MAX_THREADS) 1414 worker_threads = CTL_MAX_THREADS; 1415 1416 for (i = 0; i < worker_threads; i++) { 1417 struct ctl_thread *thr = &softc->threads[i]; 1418 1419 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1420 thr->ctl_softc = softc; 1421 STAILQ_INIT(&thr->incoming_queue); 1422 STAILQ_INIT(&thr->rtr_queue); 1423 STAILQ_INIT(&thr->done_queue); 1424 STAILQ_INIT(&thr->isc_queue); 1425 1426 error = kproc_kthread_add(ctl_work_thread, thr, 1427 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1428 if (error != 0) { 1429 printf("error creating CTL work thread!\n"); 1430 ctl_pool_free(other_pool); 1431 return (error); 1432 } 1433 } 1434 error = kproc_kthread_add(ctl_lun_thread, softc, 1435 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1436 if (error != 0) { 1437 printf("error creating CTL lun thread!\n"); 1438 ctl_pool_free(other_pool); 1439 return (error); 1440 } 1441 error = kproc_kthread_add(ctl_thresh_thread, softc, 1442 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1443 if (error != 0) { 1444 printf("error creating CTL threshold thread!\n"); 1445 ctl_pool_free(other_pool); 1446 return (error); 1447 } 1448 1449 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1450 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1451 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1452 1453 if (softc->is_single == 0) { 1454 ctl_frontend_register(&ha_frontend); 1455 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1456 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1457 softc->is_single = 1; 1458 } else 1459 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1460 != CTL_HA_STATUS_SUCCESS) { 1461 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1462 softc->is_single = 1; 1463 } 1464 } 1465 return (0); 1466} 1467 1468void 1469ctl_shutdown(void) 1470{ 1471 struct ctl_softc *softc; 1472 struct ctl_lun *lun, *next_lun; 1473 1474 softc = (struct ctl_softc *)control_softc; 1475 1476 if (softc->is_single == 0) { 1477 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1478 != CTL_HA_STATUS_SUCCESS) { 1479 printf("ctl_shutdown: ctl_ha_msg_deregister failed.\n"); 1480 } 1481 if (ctl_ha_msg_shutdown(softc) != CTL_HA_STATUS_SUCCESS) { 1482 printf("ctl_shutdown: ctl_ha_msg_shutdown failed.\n"); 1483 } 1484 ctl_frontend_deregister(&ha_frontend); 1485 } 1486 1487 mtx_lock(&softc->ctl_lock); 1488 1489 /* 1490 * Free up each LUN. 1491 */ 1492 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1493 next_lun = STAILQ_NEXT(lun, links); 1494 ctl_free_lun(lun); 1495 } 1496 1497 mtx_unlock(&softc->ctl_lock); 1498 1499#if 0 1500 ctl_shutdown_thread(softc->work_thread); 1501 mtx_destroy(&softc->queue_lock); 1502#endif 1503 1504 ctl_tpc_shutdown(softc); 1505 uma_zdestroy(softc->io_zone); 1506 mtx_destroy(&softc->ctl_lock); 1507 1508 destroy_dev(softc->dev); 1509 1510 sysctl_ctx_free(&softc->sysctl_ctx); 1511 1512 free(control_softc, M_DEVBUF); 1513 control_softc = NULL; 1514} 1515 1516static int 1517ctl_module_event_handler(module_t mod, int what, void *arg) 1518{ 1519 1520 switch (what) { 1521 case MOD_LOAD: 1522 return (ctl_init()); 1523 case MOD_UNLOAD: 1524 return (EBUSY); 1525 default: 1526 return (EOPNOTSUPP); 1527 } 1528} 1529 1530/* 1531 * XXX KDM should we do some access checks here? Bump a reference count to 1532 * prevent a CTL module from being unloaded while someone has it open? 1533 */ 1534static int 1535ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1536{ 1537 return (0); 1538} 1539 1540static int 1541ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1542{ 1543 return (0); 1544} 1545 1546/* 1547 * Remove an initiator by port number and initiator ID. 1548 * Returns 0 for success, -1 for failure. 1549 */ 1550int 1551ctl_remove_initiator(struct ctl_port *port, int iid) 1552{ 1553 struct ctl_softc *softc = control_softc; 1554 1555 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1556 1557 if (iid > CTL_MAX_INIT_PER_PORT) { 1558 printf("%s: initiator ID %u > maximun %u!\n", 1559 __func__, iid, CTL_MAX_INIT_PER_PORT); 1560 return (-1); 1561 } 1562 1563 mtx_lock(&softc->ctl_lock); 1564 port->wwpn_iid[iid].in_use--; 1565 port->wwpn_iid[iid].last_use = time_uptime; 1566 mtx_unlock(&softc->ctl_lock); 1567 1568 return (0); 1569} 1570 1571/* 1572 * Add an initiator to the initiator map. 1573 * Returns iid for success, < 0 for failure. 1574 */ 1575int 1576ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1577{ 1578 struct ctl_softc *softc = control_softc; 1579 time_t best_time; 1580 int i, best; 1581 1582 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1583 1584 if (iid >= CTL_MAX_INIT_PER_PORT) { 1585 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1586 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1587 free(name, M_CTL); 1588 return (-1); 1589 } 1590 1591 mtx_lock(&softc->ctl_lock); 1592 1593 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1594 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1595 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1596 iid = i; 1597 break; 1598 } 1599 if (name != NULL && port->wwpn_iid[i].name != NULL && 1600 strcmp(name, port->wwpn_iid[i].name) == 0) { 1601 iid = i; 1602 break; 1603 } 1604 } 1605 } 1606 1607 if (iid < 0) { 1608 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1609 if (port->wwpn_iid[i].in_use == 0 && 1610 port->wwpn_iid[i].wwpn == 0 && 1611 port->wwpn_iid[i].name == NULL) { 1612 iid = i; 1613 break; 1614 } 1615 } 1616 } 1617 1618 if (iid < 0) { 1619 best = -1; 1620 best_time = INT32_MAX; 1621 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1622 if (port->wwpn_iid[i].in_use == 0) { 1623 if (port->wwpn_iid[i].last_use < best_time) { 1624 best = i; 1625 best_time = port->wwpn_iid[i].last_use; 1626 } 1627 } 1628 } 1629 iid = best; 1630 } 1631 1632 if (iid < 0) { 1633 mtx_unlock(&softc->ctl_lock); 1634 free(name, M_CTL); 1635 return (-2); 1636 } 1637 1638 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1639 /* 1640 * This is not an error yet. 1641 */ 1642 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1643#if 0 1644 printf("%s: port %d iid %u WWPN %#jx arrived" 1645 " again\n", __func__, port->targ_port, 1646 iid, (uintmax_t)wwpn); 1647#endif 1648 goto take; 1649 } 1650 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1651 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1652#if 0 1653 printf("%s: port %d iid %u name '%s' arrived" 1654 " again\n", __func__, port->targ_port, 1655 iid, name); 1656#endif 1657 goto take; 1658 } 1659 1660 /* 1661 * This is an error, but what do we do about it? The 1662 * driver is telling us we have a new WWPN for this 1663 * initiator ID, so we pretty much need to use it. 1664 */ 1665 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1666 " but WWPN %#jx '%s' is still at that address\n", 1667 __func__, port->targ_port, iid, wwpn, name, 1668 (uintmax_t)port->wwpn_iid[iid].wwpn, 1669 port->wwpn_iid[iid].name); 1670 1671 /* 1672 * XXX KDM clear have_ca and ua_pending on each LUN for 1673 * this initiator. 1674 */ 1675 } 1676take: 1677 free(port->wwpn_iid[iid].name, M_CTL); 1678 port->wwpn_iid[iid].name = name; 1679 port->wwpn_iid[iid].wwpn = wwpn; 1680 port->wwpn_iid[iid].in_use++; 1681 mtx_unlock(&softc->ctl_lock); 1682 1683 return (iid); 1684} 1685 1686static int 1687ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1688{ 1689 int len; 1690 1691 switch (port->port_type) { 1692 case CTL_PORT_FC: 1693 { 1694 struct scsi_transportid_fcp *id = 1695 (struct scsi_transportid_fcp *)buf; 1696 if (port->wwpn_iid[iid].wwpn == 0) 1697 return (0); 1698 memset(id, 0, sizeof(*id)); 1699 id->format_protocol = SCSI_PROTO_FC; 1700 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1701 return (sizeof(*id)); 1702 } 1703 case CTL_PORT_ISCSI: 1704 { 1705 struct scsi_transportid_iscsi_port *id = 1706 (struct scsi_transportid_iscsi_port *)buf; 1707 if (port->wwpn_iid[iid].name == NULL) 1708 return (0); 1709 memset(id, 0, 256); 1710 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1711 SCSI_PROTO_ISCSI; 1712 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1713 len = roundup2(min(len, 252), 4); 1714 scsi_ulto2b(len, id->additional_length); 1715 return (sizeof(*id) + len); 1716 } 1717 case CTL_PORT_SAS: 1718 { 1719 struct scsi_transportid_sas *id = 1720 (struct scsi_transportid_sas *)buf; 1721 if (port->wwpn_iid[iid].wwpn == 0) 1722 return (0); 1723 memset(id, 0, sizeof(*id)); 1724 id->format_protocol = SCSI_PROTO_SAS; 1725 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1726 return (sizeof(*id)); 1727 } 1728 default: 1729 { 1730 struct scsi_transportid_spi *id = 1731 (struct scsi_transportid_spi *)buf; 1732 memset(id, 0, sizeof(*id)); 1733 id->format_protocol = SCSI_PROTO_SPI; 1734 scsi_ulto2b(iid, id->scsi_addr); 1735 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1736 return (sizeof(*id)); 1737 } 1738 } 1739} 1740 1741/* 1742 * Serialize a command that went down the "wrong" side, and so was sent to 1743 * this controller for execution. The logic is a little different than the 1744 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1745 * sent back to the other side, but in the success case, we execute the 1746 * command on this side (XFER mode) or tell the other side to execute it 1747 * (SER_ONLY mode). 1748 */ 1749static int 1750ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1751{ 1752 struct ctl_softc *softc; 1753 union ctl_ha_msg msg_info; 1754 struct ctl_lun *lun; 1755 const struct ctl_cmd_entry *entry; 1756 int retval = 0; 1757 uint32_t targ_lun; 1758 1759 softc = control_softc; 1760 1761 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1762 mtx_lock(&softc->ctl_lock); 1763 if ((targ_lun < CTL_MAX_LUNS) && 1764 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 1765 mtx_lock(&lun->lun_lock); 1766 mtx_unlock(&softc->ctl_lock); 1767 /* 1768 * If the LUN is invalid, pretend that it doesn't exist. 1769 * It will go away as soon as all pending I/O has been 1770 * completed. 1771 */ 1772 if (lun->flags & CTL_LUN_DISABLED) { 1773 mtx_unlock(&lun->lun_lock); 1774 lun = NULL; 1775 } 1776 } else { 1777 mtx_unlock(&softc->ctl_lock); 1778 lun = NULL; 1779 } 1780 if (lun == NULL) { 1781 /* 1782 * The other node would not send this request to us unless 1783 * received announce that we are primary node for this LUN. 1784 * If this LUN does not exist now, it is probably result of 1785 * a race, so respond to initiator in the most opaque way. 1786 */ 1787 ctl_set_busy(ctsio); 1788 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1789 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1790 msg_info.hdr.serializing_sc = NULL; 1791 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1792 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1793 sizeof(msg_info.scsi), M_WAITOK); 1794 return(1); 1795 } 1796 1797 entry = ctl_get_cmd_entry(ctsio, NULL); 1798 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 1799 mtx_unlock(&lun->lun_lock); 1800 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1801 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1802 msg_info.hdr.serializing_sc = NULL; 1803 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1804 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1805 sizeof(msg_info.scsi), M_WAITOK); 1806 return(1); 1807 } 1808 1809 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 1810 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun; 1811 1812 /* 1813 * Every I/O goes into the OOA queue for a 1814 * particular LUN, and stays there until completion. 1815 */ 1816#ifdef CTL_TIME_IO 1817 if (TAILQ_EMPTY(&lun->ooa_queue)) 1818 lun->idle_time += getsbinuptime() - lun->last_busy; 1819#endif 1820 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1821 1822 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1823 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1824 ooa_links))) { 1825 case CTL_ACTION_BLOCK: 1826 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1827 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1828 blocked_links); 1829 mtx_unlock(&lun->lun_lock); 1830 break; 1831 case CTL_ACTION_PASS: 1832 case CTL_ACTION_SKIP: 1833 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1834 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1835 ctl_enqueue_rtr((union ctl_io *)ctsio); 1836 mtx_unlock(&lun->lun_lock); 1837 } else { 1838 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 1839 mtx_unlock(&lun->lun_lock); 1840 1841 /* send msg back to other side */ 1842 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1843 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1844 msg_info.hdr.msg_type = CTL_MSG_R2R; 1845 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1846 sizeof(msg_info.hdr), M_WAITOK); 1847 } 1848 break; 1849 case CTL_ACTION_OVERLAP: 1850 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1851 mtx_unlock(&lun->lun_lock); 1852 retval = 1; 1853 1854 ctl_set_overlapped_cmd(ctsio); 1855 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1856 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1857 msg_info.hdr.serializing_sc = NULL; 1858 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1859 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1860 sizeof(msg_info.scsi), M_WAITOK); 1861 break; 1862 case CTL_ACTION_OVERLAP_TAG: 1863 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1864 mtx_unlock(&lun->lun_lock); 1865 retval = 1; 1866 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 1867 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1868 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1869 msg_info.hdr.serializing_sc = NULL; 1870 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1871 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1872 sizeof(msg_info.scsi), M_WAITOK); 1873 break; 1874 case CTL_ACTION_ERROR: 1875 default: 1876 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1877 mtx_unlock(&lun->lun_lock); 1878 retval = 1; 1879 1880 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 1881 /*retry_count*/ 0); 1882 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 1883 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1884 msg_info.hdr.serializing_sc = NULL; 1885 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1886 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1887 sizeof(msg_info.scsi), M_WAITOK); 1888 break; 1889 } 1890 return (retval); 1891} 1892 1893/* 1894 * Returns 0 for success, errno for failure. 1895 */ 1896static int 1897ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 1898 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 1899{ 1900 union ctl_io *io; 1901 int retval; 1902 1903 retval = 0; 1904 1905 mtx_lock(&lun->lun_lock); 1906 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 1907 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 1908 ooa_links)) { 1909 struct ctl_ooa_entry *entry; 1910 1911 /* 1912 * If we've got more than we can fit, just count the 1913 * remaining entries. 1914 */ 1915 if (*cur_fill_num >= ooa_hdr->alloc_num) 1916 continue; 1917 1918 entry = &kern_entries[*cur_fill_num]; 1919 1920 entry->tag_num = io->scsiio.tag_num; 1921 entry->lun_num = lun->lun; 1922#ifdef CTL_TIME_IO 1923 entry->start_bt = io->io_hdr.start_bt; 1924#endif 1925 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 1926 entry->cdb_len = io->scsiio.cdb_len; 1927 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 1928 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 1929 1930 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 1931 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 1932 1933 if (io->io_hdr.flags & CTL_FLAG_ABORT) 1934 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 1935 1936 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 1937 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 1938 1939 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 1940 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 1941 } 1942 mtx_unlock(&lun->lun_lock); 1943 1944 return (retval); 1945} 1946 1947static void * 1948ctl_copyin_alloc(void *user_addr, int len, char *error_str, 1949 size_t error_str_len) 1950{ 1951 void *kptr; 1952 1953 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 1954 1955 if (copyin(user_addr, kptr, len) != 0) { 1956 snprintf(error_str, error_str_len, "Error copying %d bytes " 1957 "from user address %p to kernel address %p", len, 1958 user_addr, kptr); 1959 free(kptr, M_CTL); 1960 return (NULL); 1961 } 1962 1963 return (kptr); 1964} 1965 1966static void 1967ctl_free_args(int num_args, struct ctl_be_arg *args) 1968{ 1969 int i; 1970 1971 if (args == NULL) 1972 return; 1973 1974 for (i = 0; i < num_args; i++) { 1975 free(args[i].kname, M_CTL); 1976 free(args[i].kvalue, M_CTL); 1977 } 1978 1979 free(args, M_CTL); 1980} 1981 1982static struct ctl_be_arg * 1983ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 1984 char *error_str, size_t error_str_len) 1985{ 1986 struct ctl_be_arg *args; 1987 int i; 1988 1989 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 1990 error_str, error_str_len); 1991 1992 if (args == NULL) 1993 goto bailout; 1994 1995 for (i = 0; i < num_args; i++) { 1996 args[i].kname = NULL; 1997 args[i].kvalue = NULL; 1998 } 1999 2000 for (i = 0; i < num_args; i++) { 2001 uint8_t *tmpptr; 2002 2003 args[i].kname = ctl_copyin_alloc(args[i].name, 2004 args[i].namelen, error_str, error_str_len); 2005 if (args[i].kname == NULL) 2006 goto bailout; 2007 2008 if (args[i].kname[args[i].namelen - 1] != '\0') { 2009 snprintf(error_str, error_str_len, "Argument %d " 2010 "name is not NUL-terminated", i); 2011 goto bailout; 2012 } 2013 2014 if (args[i].flags & CTL_BEARG_RD) { 2015 tmpptr = ctl_copyin_alloc(args[i].value, 2016 args[i].vallen, error_str, error_str_len); 2017 if (tmpptr == NULL) 2018 goto bailout; 2019 if ((args[i].flags & CTL_BEARG_ASCII) 2020 && (tmpptr[args[i].vallen - 1] != '\0')) { 2021 snprintf(error_str, error_str_len, "Argument " 2022 "%d value is not NUL-terminated", i); 2023 goto bailout; 2024 } 2025 args[i].kvalue = tmpptr; 2026 } else { 2027 args[i].kvalue = malloc(args[i].vallen, 2028 M_CTL, M_WAITOK | M_ZERO); 2029 } 2030 } 2031 2032 return (args); 2033bailout: 2034 2035 ctl_free_args(num_args, args); 2036 2037 return (NULL); 2038} 2039 2040static void 2041ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2042{ 2043 int i; 2044 2045 for (i = 0; i < num_args; i++) { 2046 if (args[i].flags & CTL_BEARG_WR) 2047 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2048 } 2049} 2050 2051/* 2052 * Escape characters that are illegal or not recommended in XML. 2053 */ 2054int 2055ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2056{ 2057 char *end = str + size; 2058 int retval; 2059 2060 retval = 0; 2061 2062 for (; *str && str < end; str++) { 2063 switch (*str) { 2064 case '&': 2065 retval = sbuf_printf(sb, "&"); 2066 break; 2067 case '>': 2068 retval = sbuf_printf(sb, ">"); 2069 break; 2070 case '<': 2071 retval = sbuf_printf(sb, "<"); 2072 break; 2073 default: 2074 retval = sbuf_putc(sb, *str); 2075 break; 2076 } 2077 2078 if (retval != 0) 2079 break; 2080 2081 } 2082 2083 return (retval); 2084} 2085 2086static void 2087ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2088{ 2089 struct scsi_vpd_id_descriptor *desc; 2090 int i; 2091 2092 if (id == NULL || id->len < 4) 2093 return; 2094 desc = (struct scsi_vpd_id_descriptor *)id->data; 2095 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2096 case SVPD_ID_TYPE_T10: 2097 sbuf_printf(sb, "t10."); 2098 break; 2099 case SVPD_ID_TYPE_EUI64: 2100 sbuf_printf(sb, "eui."); 2101 break; 2102 case SVPD_ID_TYPE_NAA: 2103 sbuf_printf(sb, "naa."); 2104 break; 2105 case SVPD_ID_TYPE_SCSI_NAME: 2106 break; 2107 } 2108 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2109 case SVPD_ID_CODESET_BINARY: 2110 for (i = 0; i < desc->length; i++) 2111 sbuf_printf(sb, "%02x", desc->identifier[i]); 2112 break; 2113 case SVPD_ID_CODESET_ASCII: 2114 sbuf_printf(sb, "%.*s", (int)desc->length, 2115 (char *)desc->identifier); 2116 break; 2117 case SVPD_ID_CODESET_UTF8: 2118 sbuf_printf(sb, "%s", (char *)desc->identifier); 2119 break; 2120 } 2121} 2122 2123static int 2124ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2125 struct thread *td) 2126{ 2127 struct ctl_softc *softc; 2128 struct ctl_lun *lun; 2129 int retval; 2130 2131 softc = control_softc; 2132 2133 retval = 0; 2134 2135 switch (cmd) { 2136 case CTL_IO: 2137 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2138 break; 2139 case CTL_ENABLE_PORT: 2140 case CTL_DISABLE_PORT: 2141 case CTL_SET_PORT_WWNS: { 2142 struct ctl_port *port; 2143 struct ctl_port_entry *entry; 2144 2145 entry = (struct ctl_port_entry *)addr; 2146 2147 mtx_lock(&softc->ctl_lock); 2148 STAILQ_FOREACH(port, &softc->port_list, links) { 2149 int action, done; 2150 2151 if (port->targ_port < softc->port_min || 2152 port->targ_port >= softc->port_max) 2153 continue; 2154 2155 action = 0; 2156 done = 0; 2157 if ((entry->port_type == CTL_PORT_NONE) 2158 && (entry->targ_port == port->targ_port)) { 2159 /* 2160 * If the user only wants to enable or 2161 * disable or set WWNs on a specific port, 2162 * do the operation and we're done. 2163 */ 2164 action = 1; 2165 done = 1; 2166 } else if (entry->port_type & port->port_type) { 2167 /* 2168 * Compare the user's type mask with the 2169 * particular frontend type to see if we 2170 * have a match. 2171 */ 2172 action = 1; 2173 done = 0; 2174 2175 /* 2176 * Make sure the user isn't trying to set 2177 * WWNs on multiple ports at the same time. 2178 */ 2179 if (cmd == CTL_SET_PORT_WWNS) { 2180 printf("%s: Can't set WWNs on " 2181 "multiple ports\n", __func__); 2182 retval = EINVAL; 2183 break; 2184 } 2185 } 2186 if (action == 0) 2187 continue; 2188 2189 /* 2190 * XXX KDM we have to drop the lock here, because 2191 * the online/offline operations can potentially 2192 * block. We need to reference count the frontends 2193 * so they can't go away, 2194 */ 2195 if (cmd == CTL_ENABLE_PORT) { 2196 mtx_unlock(&softc->ctl_lock); 2197 ctl_port_online(port); 2198 mtx_lock(&softc->ctl_lock); 2199 } else if (cmd == CTL_DISABLE_PORT) { 2200 mtx_unlock(&softc->ctl_lock); 2201 ctl_port_offline(port); 2202 mtx_lock(&softc->ctl_lock); 2203 } else if (cmd == CTL_SET_PORT_WWNS) { 2204 ctl_port_set_wwns(port, 2205 (entry->flags & CTL_PORT_WWNN_VALID) ? 2206 1 : 0, entry->wwnn, 2207 (entry->flags & CTL_PORT_WWPN_VALID) ? 2208 1 : 0, entry->wwpn); 2209 } 2210 if (done != 0) 2211 break; 2212 } 2213 mtx_unlock(&softc->ctl_lock); 2214 break; 2215 } 2216 case CTL_GET_PORT_LIST: { 2217 struct ctl_port *port; 2218 struct ctl_port_list *list; 2219 int i; 2220 2221 list = (struct ctl_port_list *)addr; 2222 2223 if (list->alloc_len != (list->alloc_num * 2224 sizeof(struct ctl_port_entry))) { 2225 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2226 "alloc_num %u * sizeof(struct ctl_port_entry) " 2227 "%zu\n", __func__, list->alloc_len, 2228 list->alloc_num, sizeof(struct ctl_port_entry)); 2229 retval = EINVAL; 2230 break; 2231 } 2232 list->fill_len = 0; 2233 list->fill_num = 0; 2234 list->dropped_num = 0; 2235 i = 0; 2236 mtx_lock(&softc->ctl_lock); 2237 STAILQ_FOREACH(port, &softc->port_list, links) { 2238 struct ctl_port_entry entry, *list_entry; 2239 2240 if (list->fill_num >= list->alloc_num) { 2241 list->dropped_num++; 2242 continue; 2243 } 2244 2245 entry.port_type = port->port_type; 2246 strlcpy(entry.port_name, port->port_name, 2247 sizeof(entry.port_name)); 2248 entry.targ_port = port->targ_port; 2249 entry.physical_port = port->physical_port; 2250 entry.virtual_port = port->virtual_port; 2251 entry.wwnn = port->wwnn; 2252 entry.wwpn = port->wwpn; 2253 if (port->status & CTL_PORT_STATUS_ONLINE) 2254 entry.online = 1; 2255 else 2256 entry.online = 0; 2257 2258 list_entry = &list->entries[i]; 2259 2260 retval = copyout(&entry, list_entry, sizeof(entry)); 2261 if (retval != 0) { 2262 printf("%s: CTL_GET_PORT_LIST: copyout " 2263 "returned %d\n", __func__, retval); 2264 break; 2265 } 2266 i++; 2267 list->fill_num++; 2268 list->fill_len += sizeof(entry); 2269 } 2270 mtx_unlock(&softc->ctl_lock); 2271 2272 /* 2273 * If this is non-zero, we had a copyout fault, so there's 2274 * probably no point in attempting to set the status inside 2275 * the structure. 2276 */ 2277 if (retval != 0) 2278 break; 2279 2280 if (list->dropped_num > 0) 2281 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2282 else 2283 list->status = CTL_PORT_LIST_OK; 2284 break; 2285 } 2286 case CTL_DUMP_OOA: { 2287 union ctl_io *io; 2288 char printbuf[128]; 2289 struct sbuf sb; 2290 2291 mtx_lock(&softc->ctl_lock); 2292 printf("Dumping OOA queues:\n"); 2293 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2294 mtx_lock(&lun->lun_lock); 2295 for (io = (union ctl_io *)TAILQ_FIRST( 2296 &lun->ooa_queue); io != NULL; 2297 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2298 ooa_links)) { 2299 sbuf_new(&sb, printbuf, sizeof(printbuf), 2300 SBUF_FIXEDLEN); 2301 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2302 (intmax_t)lun->lun, 2303 io->scsiio.tag_num, 2304 (io->io_hdr.flags & 2305 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2306 (io->io_hdr.flags & 2307 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2308 (io->io_hdr.flags & 2309 CTL_FLAG_ABORT) ? " ABORT" : "", 2310 (io->io_hdr.flags & 2311 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2312 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2313 sbuf_finish(&sb); 2314 printf("%s\n", sbuf_data(&sb)); 2315 } 2316 mtx_unlock(&lun->lun_lock); 2317 } 2318 printf("OOA queues dump done\n"); 2319 mtx_unlock(&softc->ctl_lock); 2320 break; 2321 } 2322 case CTL_GET_OOA: { 2323 struct ctl_ooa *ooa_hdr; 2324 struct ctl_ooa_entry *entries; 2325 uint32_t cur_fill_num; 2326 2327 ooa_hdr = (struct ctl_ooa *)addr; 2328 2329 if ((ooa_hdr->alloc_len == 0) 2330 || (ooa_hdr->alloc_num == 0)) { 2331 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2332 "must be non-zero\n", __func__, 2333 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2334 retval = EINVAL; 2335 break; 2336 } 2337 2338 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2339 sizeof(struct ctl_ooa_entry))) { 2340 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2341 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2342 __func__, ooa_hdr->alloc_len, 2343 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2344 retval = EINVAL; 2345 break; 2346 } 2347 2348 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2349 if (entries == NULL) { 2350 printf("%s: could not allocate %d bytes for OOA " 2351 "dump\n", __func__, ooa_hdr->alloc_len); 2352 retval = ENOMEM; 2353 break; 2354 } 2355 2356 mtx_lock(&softc->ctl_lock); 2357 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2358 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2359 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2360 mtx_unlock(&softc->ctl_lock); 2361 free(entries, M_CTL); 2362 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2363 __func__, (uintmax_t)ooa_hdr->lun_num); 2364 retval = EINVAL; 2365 break; 2366 } 2367 2368 cur_fill_num = 0; 2369 2370 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2371 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2372 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2373 ooa_hdr, entries); 2374 if (retval != 0) 2375 break; 2376 } 2377 if (retval != 0) { 2378 mtx_unlock(&softc->ctl_lock); 2379 free(entries, M_CTL); 2380 break; 2381 } 2382 } else { 2383 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2384 2385 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2386 entries); 2387 } 2388 mtx_unlock(&softc->ctl_lock); 2389 2390 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2391 ooa_hdr->fill_len = ooa_hdr->fill_num * 2392 sizeof(struct ctl_ooa_entry); 2393 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2394 if (retval != 0) { 2395 printf("%s: error copying out %d bytes for OOA dump\n", 2396 __func__, ooa_hdr->fill_len); 2397 } 2398 2399 getbintime(&ooa_hdr->cur_bt); 2400 2401 if (cur_fill_num > ooa_hdr->alloc_num) { 2402 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2403 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2404 } else { 2405 ooa_hdr->dropped_num = 0; 2406 ooa_hdr->status = CTL_OOA_OK; 2407 } 2408 2409 free(entries, M_CTL); 2410 break; 2411 } 2412 case CTL_CHECK_OOA: { 2413 union ctl_io *io; 2414 struct ctl_ooa_info *ooa_info; 2415 2416 2417 ooa_info = (struct ctl_ooa_info *)addr; 2418 2419 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2420 ooa_info->status = CTL_OOA_INVALID_LUN; 2421 break; 2422 } 2423 mtx_lock(&softc->ctl_lock); 2424 lun = softc->ctl_luns[ooa_info->lun_id]; 2425 if (lun == NULL) { 2426 mtx_unlock(&softc->ctl_lock); 2427 ooa_info->status = CTL_OOA_INVALID_LUN; 2428 break; 2429 } 2430 mtx_lock(&lun->lun_lock); 2431 mtx_unlock(&softc->ctl_lock); 2432 ooa_info->num_entries = 0; 2433 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2434 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2435 &io->io_hdr, ooa_links)) { 2436 ooa_info->num_entries++; 2437 } 2438 mtx_unlock(&lun->lun_lock); 2439 2440 ooa_info->status = CTL_OOA_SUCCESS; 2441 2442 break; 2443 } 2444 case CTL_DELAY_IO: { 2445 struct ctl_io_delay_info *delay_info; 2446 2447 delay_info = (struct ctl_io_delay_info *)addr; 2448 2449#ifdef CTL_IO_DELAY 2450 mtx_lock(&softc->ctl_lock); 2451 2452 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2453 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2454 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2455 } else { 2456 lun = softc->ctl_luns[delay_info->lun_id]; 2457 mtx_lock(&lun->lun_lock); 2458 2459 delay_info->status = CTL_DELAY_STATUS_OK; 2460 2461 switch (delay_info->delay_type) { 2462 case CTL_DELAY_TYPE_CONT: 2463 break; 2464 case CTL_DELAY_TYPE_ONESHOT: 2465 break; 2466 default: 2467 delay_info->status = 2468 CTL_DELAY_STATUS_INVALID_TYPE; 2469 break; 2470 } 2471 2472 switch (delay_info->delay_loc) { 2473 case CTL_DELAY_LOC_DATAMOVE: 2474 lun->delay_info.datamove_type = 2475 delay_info->delay_type; 2476 lun->delay_info.datamove_delay = 2477 delay_info->delay_secs; 2478 break; 2479 case CTL_DELAY_LOC_DONE: 2480 lun->delay_info.done_type = 2481 delay_info->delay_type; 2482 lun->delay_info.done_delay = 2483 delay_info->delay_secs; 2484 break; 2485 default: 2486 delay_info->status = 2487 CTL_DELAY_STATUS_INVALID_LOC; 2488 break; 2489 } 2490 mtx_unlock(&lun->lun_lock); 2491 } 2492 2493 mtx_unlock(&softc->ctl_lock); 2494#else 2495 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2496#endif /* CTL_IO_DELAY */ 2497 break; 2498 } 2499 case CTL_REALSYNC_SET: { 2500 int *syncstate; 2501 2502 syncstate = (int *)addr; 2503 2504 mtx_lock(&softc->ctl_lock); 2505 switch (*syncstate) { 2506 case 0: 2507 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2508 break; 2509 case 1: 2510 softc->flags |= CTL_FLAG_REAL_SYNC; 2511 break; 2512 default: 2513 retval = EINVAL; 2514 break; 2515 } 2516 mtx_unlock(&softc->ctl_lock); 2517 break; 2518 } 2519 case CTL_REALSYNC_GET: { 2520 int *syncstate; 2521 2522 syncstate = (int*)addr; 2523 2524 mtx_lock(&softc->ctl_lock); 2525 if (softc->flags & CTL_FLAG_REAL_SYNC) 2526 *syncstate = 1; 2527 else 2528 *syncstate = 0; 2529 mtx_unlock(&softc->ctl_lock); 2530 2531 break; 2532 } 2533 case CTL_SETSYNC: 2534 case CTL_GETSYNC: { 2535 struct ctl_sync_info *sync_info; 2536 2537 sync_info = (struct ctl_sync_info *)addr; 2538 2539 mtx_lock(&softc->ctl_lock); 2540 lun = softc->ctl_luns[sync_info->lun_id]; 2541 if (lun == NULL) { 2542 mtx_unlock(&softc->ctl_lock); 2543 sync_info->status = CTL_GS_SYNC_NO_LUN; 2544 break; 2545 } 2546 /* 2547 * Get or set the sync interval. We're not bounds checking 2548 * in the set case, hopefully the user won't do something 2549 * silly. 2550 */ 2551 mtx_lock(&lun->lun_lock); 2552 mtx_unlock(&softc->ctl_lock); 2553 if (cmd == CTL_GETSYNC) 2554 sync_info->sync_interval = lun->sync_interval; 2555 else 2556 lun->sync_interval = sync_info->sync_interval; 2557 mtx_unlock(&lun->lun_lock); 2558 2559 sync_info->status = CTL_GS_SYNC_OK; 2560 2561 break; 2562 } 2563 case CTL_GETSTATS: { 2564 struct ctl_stats *stats; 2565 int i; 2566 2567 stats = (struct ctl_stats *)addr; 2568 2569 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2570 stats->alloc_len) { 2571 stats->status = CTL_SS_NEED_MORE_SPACE; 2572 stats->num_luns = softc->num_luns; 2573 break; 2574 } 2575 /* 2576 * XXX KDM no locking here. If the LUN list changes, 2577 * things can blow up. 2578 */ 2579 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2580 i++, lun = STAILQ_NEXT(lun, links)) { 2581 retval = copyout(&lun->stats, &stats->lun_stats[i], 2582 sizeof(lun->stats)); 2583 if (retval != 0) 2584 break; 2585 } 2586 stats->num_luns = softc->num_luns; 2587 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2588 softc->num_luns; 2589 stats->status = CTL_SS_OK; 2590#ifdef CTL_TIME_IO 2591 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2592#else 2593 stats->flags = CTL_STATS_FLAG_NONE; 2594#endif 2595 getnanouptime(&stats->timestamp); 2596 break; 2597 } 2598 case CTL_ERROR_INJECT: { 2599 struct ctl_error_desc *err_desc, *new_err_desc; 2600 2601 err_desc = (struct ctl_error_desc *)addr; 2602 2603 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2604 M_WAITOK | M_ZERO); 2605 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2606 2607 mtx_lock(&softc->ctl_lock); 2608 lun = softc->ctl_luns[err_desc->lun_id]; 2609 if (lun == NULL) { 2610 mtx_unlock(&softc->ctl_lock); 2611 free(new_err_desc, M_CTL); 2612 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2613 __func__, (uintmax_t)err_desc->lun_id); 2614 retval = EINVAL; 2615 break; 2616 } 2617 mtx_lock(&lun->lun_lock); 2618 mtx_unlock(&softc->ctl_lock); 2619 2620 /* 2621 * We could do some checking here to verify the validity 2622 * of the request, but given the complexity of error 2623 * injection requests, the checking logic would be fairly 2624 * complex. 2625 * 2626 * For now, if the request is invalid, it just won't get 2627 * executed and might get deleted. 2628 */ 2629 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2630 2631 /* 2632 * XXX KDM check to make sure the serial number is unique, 2633 * in case we somehow manage to wrap. That shouldn't 2634 * happen for a very long time, but it's the right thing to 2635 * do. 2636 */ 2637 new_err_desc->serial = lun->error_serial; 2638 err_desc->serial = lun->error_serial; 2639 lun->error_serial++; 2640 2641 mtx_unlock(&lun->lun_lock); 2642 break; 2643 } 2644 case CTL_ERROR_INJECT_DELETE: { 2645 struct ctl_error_desc *delete_desc, *desc, *desc2; 2646 int delete_done; 2647 2648 delete_desc = (struct ctl_error_desc *)addr; 2649 delete_done = 0; 2650 2651 mtx_lock(&softc->ctl_lock); 2652 lun = softc->ctl_luns[delete_desc->lun_id]; 2653 if (lun == NULL) { 2654 mtx_unlock(&softc->ctl_lock); 2655 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2656 __func__, (uintmax_t)delete_desc->lun_id); 2657 retval = EINVAL; 2658 break; 2659 } 2660 mtx_lock(&lun->lun_lock); 2661 mtx_unlock(&softc->ctl_lock); 2662 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2663 if (desc->serial != delete_desc->serial) 2664 continue; 2665 2666 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2667 links); 2668 free(desc, M_CTL); 2669 delete_done = 1; 2670 } 2671 mtx_unlock(&lun->lun_lock); 2672 if (delete_done == 0) { 2673 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2674 "error serial %ju on LUN %u\n", __func__, 2675 delete_desc->serial, delete_desc->lun_id); 2676 retval = EINVAL; 2677 break; 2678 } 2679 break; 2680 } 2681 case CTL_DUMP_STRUCTS: { 2682 int i, j, k; 2683 struct ctl_port *port; 2684 struct ctl_frontend *fe; 2685 2686 mtx_lock(&softc->ctl_lock); 2687 printf("CTL Persistent Reservation information start:\n"); 2688 for (i = 0; i < CTL_MAX_LUNS; i++) { 2689 lun = softc->ctl_luns[i]; 2690 2691 if ((lun == NULL) 2692 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2693 continue; 2694 2695 for (j = 0; j < CTL_MAX_PORTS; j++) { 2696 if (lun->pr_keys[j] == NULL) 2697 continue; 2698 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2699 if (lun->pr_keys[j][k] == 0) 2700 continue; 2701 printf(" LUN %d port %d iid %d key " 2702 "%#jx\n", i, j, k, 2703 (uintmax_t)lun->pr_keys[j][k]); 2704 } 2705 } 2706 } 2707 printf("CTL Persistent Reservation information end\n"); 2708 printf("CTL Ports:\n"); 2709 STAILQ_FOREACH(port, &softc->port_list, links) { 2710 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2711 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2712 port->frontend->name, port->port_type, 2713 port->physical_port, port->virtual_port, 2714 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2715 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2716 if (port->wwpn_iid[j].in_use == 0 && 2717 port->wwpn_iid[j].wwpn == 0 && 2718 port->wwpn_iid[j].name == NULL) 2719 continue; 2720 2721 printf(" iid %u use %d WWPN %#jx '%s'\n", 2722 j, port->wwpn_iid[j].in_use, 2723 (uintmax_t)port->wwpn_iid[j].wwpn, 2724 port->wwpn_iid[j].name); 2725 } 2726 } 2727 printf("CTL Port information end\n"); 2728 mtx_unlock(&softc->ctl_lock); 2729 /* 2730 * XXX KDM calling this without a lock. We'd likely want 2731 * to drop the lock before calling the frontend's dump 2732 * routine anyway. 2733 */ 2734 printf("CTL Frontends:\n"); 2735 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2736 printf(" Frontend '%s'\n", fe->name); 2737 if (fe->fe_dump != NULL) 2738 fe->fe_dump(); 2739 } 2740 printf("CTL Frontend information end\n"); 2741 break; 2742 } 2743 case CTL_LUN_REQ: { 2744 struct ctl_lun_req *lun_req; 2745 struct ctl_backend_driver *backend; 2746 2747 lun_req = (struct ctl_lun_req *)addr; 2748 2749 backend = ctl_backend_find(lun_req->backend); 2750 if (backend == NULL) { 2751 lun_req->status = CTL_LUN_ERROR; 2752 snprintf(lun_req->error_str, 2753 sizeof(lun_req->error_str), 2754 "Backend \"%s\" not found.", 2755 lun_req->backend); 2756 break; 2757 } 2758 if (lun_req->num_be_args > 0) { 2759 lun_req->kern_be_args = ctl_copyin_args( 2760 lun_req->num_be_args, 2761 lun_req->be_args, 2762 lun_req->error_str, 2763 sizeof(lun_req->error_str)); 2764 if (lun_req->kern_be_args == NULL) { 2765 lun_req->status = CTL_LUN_ERROR; 2766 break; 2767 } 2768 } 2769 2770 retval = backend->ioctl(dev, cmd, addr, flag, td); 2771 2772 if (lun_req->num_be_args > 0) { 2773 ctl_copyout_args(lun_req->num_be_args, 2774 lun_req->kern_be_args); 2775 ctl_free_args(lun_req->num_be_args, 2776 lun_req->kern_be_args); 2777 } 2778 break; 2779 } 2780 case CTL_LUN_LIST: { 2781 struct sbuf *sb; 2782 struct ctl_lun_list *list; 2783 struct ctl_option *opt; 2784 2785 list = (struct ctl_lun_list *)addr; 2786 2787 /* 2788 * Allocate a fixed length sbuf here, based on the length 2789 * of the user's buffer. We could allocate an auto-extending 2790 * buffer, and then tell the user how much larger our 2791 * amount of data is than his buffer, but that presents 2792 * some problems: 2793 * 2794 * 1. The sbuf(9) routines use a blocking malloc, and so 2795 * we can't hold a lock while calling them with an 2796 * auto-extending buffer. 2797 * 2798 * 2. There is not currently a LUN reference counting 2799 * mechanism, outside of outstanding transactions on 2800 * the LUN's OOA queue. So a LUN could go away on us 2801 * while we're getting the LUN number, backend-specific 2802 * information, etc. Thus, given the way things 2803 * currently work, we need to hold the CTL lock while 2804 * grabbing LUN information. 2805 * 2806 * So, from the user's standpoint, the best thing to do is 2807 * allocate what he thinks is a reasonable buffer length, 2808 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 2809 * double the buffer length and try again. (And repeat 2810 * that until he succeeds.) 2811 */ 2812 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 2813 if (sb == NULL) { 2814 list->status = CTL_LUN_LIST_ERROR; 2815 snprintf(list->error_str, sizeof(list->error_str), 2816 "Unable to allocate %d bytes for LUN list", 2817 list->alloc_len); 2818 break; 2819 } 2820 2821 sbuf_printf(sb, "<ctllunlist>\n"); 2822 2823 mtx_lock(&softc->ctl_lock); 2824 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2825 mtx_lock(&lun->lun_lock); 2826 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 2827 (uintmax_t)lun->lun); 2828 2829 /* 2830 * Bail out as soon as we see that we've overfilled 2831 * the buffer. 2832 */ 2833 if (retval != 0) 2834 break; 2835 2836 retval = sbuf_printf(sb, "\t<backend_type>%s" 2837 "</backend_type>\n", 2838 (lun->backend == NULL) ? "none" : 2839 lun->backend->name); 2840 2841 if (retval != 0) 2842 break; 2843 2844 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 2845 lun->be_lun->lun_type); 2846 2847 if (retval != 0) 2848 break; 2849 2850 if (lun->backend == NULL) { 2851 retval = sbuf_printf(sb, "</lun>\n"); 2852 if (retval != 0) 2853 break; 2854 continue; 2855 } 2856 2857 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 2858 (lun->be_lun->maxlba > 0) ? 2859 lun->be_lun->maxlba + 1 : 0); 2860 2861 if (retval != 0) 2862 break; 2863 2864 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 2865 lun->be_lun->blocksize); 2866 2867 if (retval != 0) 2868 break; 2869 2870 retval = sbuf_printf(sb, "\t<serial_number>"); 2871 2872 if (retval != 0) 2873 break; 2874 2875 retval = ctl_sbuf_printf_esc(sb, 2876 lun->be_lun->serial_num, 2877 sizeof(lun->be_lun->serial_num)); 2878 2879 if (retval != 0) 2880 break; 2881 2882 retval = sbuf_printf(sb, "</serial_number>\n"); 2883 2884 if (retval != 0) 2885 break; 2886 2887 retval = sbuf_printf(sb, "\t<device_id>"); 2888 2889 if (retval != 0) 2890 break; 2891 2892 retval = ctl_sbuf_printf_esc(sb, 2893 lun->be_lun->device_id, 2894 sizeof(lun->be_lun->device_id)); 2895 2896 if (retval != 0) 2897 break; 2898 2899 retval = sbuf_printf(sb, "</device_id>\n"); 2900 2901 if (retval != 0) 2902 break; 2903 2904 if (lun->backend->lun_info != NULL) { 2905 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 2906 if (retval != 0) 2907 break; 2908 } 2909 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 2910 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 2911 opt->name, opt->value, opt->name); 2912 if (retval != 0) 2913 break; 2914 } 2915 2916 retval = sbuf_printf(sb, "</lun>\n"); 2917 2918 if (retval != 0) 2919 break; 2920 mtx_unlock(&lun->lun_lock); 2921 } 2922 if (lun != NULL) 2923 mtx_unlock(&lun->lun_lock); 2924 mtx_unlock(&softc->ctl_lock); 2925 2926 if ((retval != 0) 2927 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 2928 retval = 0; 2929 sbuf_delete(sb); 2930 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 2931 snprintf(list->error_str, sizeof(list->error_str), 2932 "Out of space, %d bytes is too small", 2933 list->alloc_len); 2934 break; 2935 } 2936 2937 sbuf_finish(sb); 2938 2939 retval = copyout(sbuf_data(sb), list->lun_xml, 2940 sbuf_len(sb) + 1); 2941 2942 list->fill_len = sbuf_len(sb) + 1; 2943 list->status = CTL_LUN_LIST_OK; 2944 sbuf_delete(sb); 2945 break; 2946 } 2947 case CTL_ISCSI: { 2948 struct ctl_iscsi *ci; 2949 struct ctl_frontend *fe; 2950 2951 ci = (struct ctl_iscsi *)addr; 2952 2953 fe = ctl_frontend_find("iscsi"); 2954 if (fe == NULL) { 2955 ci->status = CTL_ISCSI_ERROR; 2956 snprintf(ci->error_str, sizeof(ci->error_str), 2957 "Frontend \"iscsi\" not found."); 2958 break; 2959 } 2960 2961 retval = fe->ioctl(dev, cmd, addr, flag, td); 2962 break; 2963 } 2964 case CTL_PORT_REQ: { 2965 struct ctl_req *req; 2966 struct ctl_frontend *fe; 2967 2968 req = (struct ctl_req *)addr; 2969 2970 fe = ctl_frontend_find(req->driver); 2971 if (fe == NULL) { 2972 req->status = CTL_LUN_ERROR; 2973 snprintf(req->error_str, sizeof(req->error_str), 2974 "Frontend \"%s\" not found.", req->driver); 2975 break; 2976 } 2977 if (req->num_args > 0) { 2978 req->kern_args = ctl_copyin_args(req->num_args, 2979 req->args, req->error_str, sizeof(req->error_str)); 2980 if (req->kern_args == NULL) { 2981 req->status = CTL_LUN_ERROR; 2982 break; 2983 } 2984 } 2985 2986 if (fe->ioctl) 2987 retval = fe->ioctl(dev, cmd, addr, flag, td); 2988 else 2989 retval = ENODEV; 2990 2991 if (req->num_args > 0) { 2992 ctl_copyout_args(req->num_args, req->kern_args); 2993 ctl_free_args(req->num_args, req->kern_args); 2994 } 2995 break; 2996 } 2997 case CTL_PORT_LIST: { 2998 struct sbuf *sb; 2999 struct ctl_port *port; 3000 struct ctl_lun_list *list; 3001 struct ctl_option *opt; 3002 int j; 3003 uint32_t plun; 3004 3005 list = (struct ctl_lun_list *)addr; 3006 3007 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3008 if (sb == NULL) { 3009 list->status = CTL_LUN_LIST_ERROR; 3010 snprintf(list->error_str, sizeof(list->error_str), 3011 "Unable to allocate %d bytes for LUN list", 3012 list->alloc_len); 3013 break; 3014 } 3015 3016 sbuf_printf(sb, "<ctlportlist>\n"); 3017 3018 mtx_lock(&softc->ctl_lock); 3019 STAILQ_FOREACH(port, &softc->port_list, links) { 3020 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3021 (uintmax_t)port->targ_port); 3022 3023 /* 3024 * Bail out as soon as we see that we've overfilled 3025 * the buffer. 3026 */ 3027 if (retval != 0) 3028 break; 3029 3030 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3031 "</frontend_type>\n", port->frontend->name); 3032 if (retval != 0) 3033 break; 3034 3035 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3036 port->port_type); 3037 if (retval != 0) 3038 break; 3039 3040 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3041 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3042 if (retval != 0) 3043 break; 3044 3045 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3046 port->port_name); 3047 if (retval != 0) 3048 break; 3049 3050 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3051 port->physical_port); 3052 if (retval != 0) 3053 break; 3054 3055 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3056 port->virtual_port); 3057 if (retval != 0) 3058 break; 3059 3060 if (port->target_devid != NULL) { 3061 sbuf_printf(sb, "\t<target>"); 3062 ctl_id_sbuf(port->target_devid, sb); 3063 sbuf_printf(sb, "</target>\n"); 3064 } 3065 3066 if (port->port_devid != NULL) { 3067 sbuf_printf(sb, "\t<port>"); 3068 ctl_id_sbuf(port->port_devid, sb); 3069 sbuf_printf(sb, "</port>\n"); 3070 } 3071 3072 if (port->port_info != NULL) { 3073 retval = port->port_info(port->onoff_arg, sb); 3074 if (retval != 0) 3075 break; 3076 } 3077 STAILQ_FOREACH(opt, &port->options, links) { 3078 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3079 opt->name, opt->value, opt->name); 3080 if (retval != 0) 3081 break; 3082 } 3083 3084 if (port->lun_map != NULL) { 3085 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3086 for (j = 0; j < CTL_MAX_LUNS; j++) { 3087 plun = ctl_lun_map_from_port(port, j); 3088 if (plun >= CTL_MAX_LUNS) 3089 continue; 3090 sbuf_printf(sb, 3091 "\t<lun id=\"%u\">%u</lun>\n", 3092 j, plun); 3093 } 3094 } 3095 3096 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3097 if (port->wwpn_iid[j].in_use == 0 || 3098 (port->wwpn_iid[j].wwpn == 0 && 3099 port->wwpn_iid[j].name == NULL)) 3100 continue; 3101 3102 if (port->wwpn_iid[j].name != NULL) 3103 retval = sbuf_printf(sb, 3104 "\t<initiator id=\"%u\">%s</initiator>\n", 3105 j, port->wwpn_iid[j].name); 3106 else 3107 retval = sbuf_printf(sb, 3108 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3109 j, port->wwpn_iid[j].wwpn); 3110 if (retval != 0) 3111 break; 3112 } 3113 if (retval != 0) 3114 break; 3115 3116 retval = sbuf_printf(sb, "</targ_port>\n"); 3117 if (retval != 0) 3118 break; 3119 } 3120 mtx_unlock(&softc->ctl_lock); 3121 3122 if ((retval != 0) 3123 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3124 retval = 0; 3125 sbuf_delete(sb); 3126 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3127 snprintf(list->error_str, sizeof(list->error_str), 3128 "Out of space, %d bytes is too small", 3129 list->alloc_len); 3130 break; 3131 } 3132 3133 sbuf_finish(sb); 3134 3135 retval = copyout(sbuf_data(sb), list->lun_xml, 3136 sbuf_len(sb) + 1); 3137 3138 list->fill_len = sbuf_len(sb) + 1; 3139 list->status = CTL_LUN_LIST_OK; 3140 sbuf_delete(sb); 3141 break; 3142 } 3143 case CTL_LUN_MAP: { 3144 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3145 struct ctl_port *port; 3146 3147 mtx_lock(&softc->ctl_lock); 3148 if (lm->port < softc->port_min || 3149 lm->port >= softc->port_max || 3150 (port = softc->ctl_ports[lm->port]) == NULL) { 3151 mtx_unlock(&softc->ctl_lock); 3152 return (ENXIO); 3153 } 3154 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3155 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 3156 continue; 3157 mtx_lock(&lun->lun_lock); 3158 ctl_est_ua_port(lun, lm->port, -1, CTL_UA_LUN_CHANGE); 3159 mtx_unlock(&lun->lun_lock); 3160 } 3161 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3162 if (lm->plun < CTL_MAX_LUNS) { 3163 if (lm->lun == UINT32_MAX) 3164 retval = ctl_lun_map_unset(port, lm->plun); 3165 else if (lm->lun < CTL_MAX_LUNS && 3166 softc->ctl_luns[lm->lun] != NULL) 3167 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3168 else 3169 return (ENXIO); 3170 } else if (lm->plun == UINT32_MAX) { 3171 if (lm->lun == UINT32_MAX) 3172 retval = ctl_lun_map_deinit(port); 3173 else 3174 retval = ctl_lun_map_init(port); 3175 } else 3176 return (ENXIO); 3177 break; 3178 } 3179 default: { 3180 /* XXX KDM should we fix this? */ 3181#if 0 3182 struct ctl_backend_driver *backend; 3183 unsigned int type; 3184 int found; 3185 3186 found = 0; 3187 3188 /* 3189 * We encode the backend type as the ioctl type for backend 3190 * ioctls. So parse it out here, and then search for a 3191 * backend of this type. 3192 */ 3193 type = _IOC_TYPE(cmd); 3194 3195 STAILQ_FOREACH(backend, &softc->be_list, links) { 3196 if (backend->type == type) { 3197 found = 1; 3198 break; 3199 } 3200 } 3201 if (found == 0) { 3202 printf("ctl: unknown ioctl command %#lx or backend " 3203 "%d\n", cmd, type); 3204 retval = EINVAL; 3205 break; 3206 } 3207 retval = backend->ioctl(dev, cmd, addr, flag, td); 3208#endif 3209 retval = ENOTTY; 3210 break; 3211 } 3212 } 3213 return (retval); 3214} 3215 3216uint32_t 3217ctl_get_initindex(struct ctl_nexus *nexus) 3218{ 3219 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3220} 3221 3222int 3223ctl_lun_map_init(struct ctl_port *port) 3224{ 3225 struct ctl_softc *softc = control_softc; 3226 struct ctl_lun *lun; 3227 uint32_t i; 3228 3229 if (port->lun_map == NULL) 3230 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3231 M_CTL, M_NOWAIT); 3232 if (port->lun_map == NULL) 3233 return (ENOMEM); 3234 for (i = 0; i < CTL_MAX_LUNS; i++) 3235 port->lun_map[i] = UINT32_MAX; 3236 if (port->status & CTL_PORT_STATUS_ONLINE) { 3237 if (port->lun_disable != NULL) { 3238 STAILQ_FOREACH(lun, &softc->lun_list, links) 3239 port->lun_disable(port->targ_lun_arg, lun->lun); 3240 } 3241 ctl_isc_announce_port(port); 3242 } 3243 return (0); 3244} 3245 3246int 3247ctl_lun_map_deinit(struct ctl_port *port) 3248{ 3249 struct ctl_softc *softc = control_softc; 3250 struct ctl_lun *lun; 3251 3252 if (port->lun_map == NULL) 3253 return (0); 3254 free(port->lun_map, M_CTL); 3255 port->lun_map = NULL; 3256 if (port->status & CTL_PORT_STATUS_ONLINE) { 3257 if (port->lun_enable != NULL) { 3258 STAILQ_FOREACH(lun, &softc->lun_list, links) 3259 port->lun_enable(port->targ_lun_arg, lun->lun); 3260 } 3261 ctl_isc_announce_port(port); 3262 } 3263 return (0); 3264} 3265 3266int 3267ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3268{ 3269 int status; 3270 uint32_t old; 3271 3272 if (port->lun_map == NULL) { 3273 status = ctl_lun_map_init(port); 3274 if (status != 0) 3275 return (status); 3276 } 3277 old = port->lun_map[plun]; 3278 port->lun_map[plun] = glun; 3279 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) { 3280 if (port->lun_enable != NULL) 3281 port->lun_enable(port->targ_lun_arg, plun); 3282 ctl_isc_announce_port(port); 3283 } 3284 return (0); 3285} 3286 3287int 3288ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3289{ 3290 uint32_t old; 3291 3292 if (port->lun_map == NULL) 3293 return (0); 3294 old = port->lun_map[plun]; 3295 port->lun_map[plun] = UINT32_MAX; 3296 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) { 3297 if (port->lun_disable != NULL) 3298 port->lun_disable(port->targ_lun_arg, plun); 3299 ctl_isc_announce_port(port); 3300 } 3301 return (0); 3302} 3303 3304uint32_t 3305ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3306{ 3307 3308 if (port == NULL) 3309 return (UINT32_MAX); 3310 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3311 return (lun_id); 3312 return (port->lun_map[lun_id]); 3313} 3314 3315uint32_t 3316ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3317{ 3318 uint32_t i; 3319 3320 if (port == NULL) 3321 return (UINT32_MAX); 3322 if (port->lun_map == NULL) 3323 return (lun_id); 3324 for (i = 0; i < CTL_MAX_LUNS; i++) { 3325 if (port->lun_map[i] == lun_id) 3326 return (i); 3327 } 3328 return (UINT32_MAX); 3329} 3330 3331static struct ctl_port * 3332ctl_io_port(struct ctl_io_hdr *io_hdr) 3333{ 3334 3335 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]); 3336} 3337 3338int 3339ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3340{ 3341 int i; 3342 3343 for (i = first; i < last; i++) { 3344 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3345 return (i); 3346 } 3347 return (-1); 3348} 3349 3350int 3351ctl_set_mask(uint32_t *mask, uint32_t bit) 3352{ 3353 uint32_t chunk, piece; 3354 3355 chunk = bit >> 5; 3356 piece = bit % (sizeof(uint32_t) * 8); 3357 3358 if ((mask[chunk] & (1 << piece)) != 0) 3359 return (-1); 3360 else 3361 mask[chunk] |= (1 << piece); 3362 3363 return (0); 3364} 3365 3366int 3367ctl_clear_mask(uint32_t *mask, uint32_t bit) 3368{ 3369 uint32_t chunk, piece; 3370 3371 chunk = bit >> 5; 3372 piece = bit % (sizeof(uint32_t) * 8); 3373 3374 if ((mask[chunk] & (1 << piece)) == 0) 3375 return (-1); 3376 else 3377 mask[chunk] &= ~(1 << piece); 3378 3379 return (0); 3380} 3381 3382int 3383ctl_is_set(uint32_t *mask, uint32_t bit) 3384{ 3385 uint32_t chunk, piece; 3386 3387 chunk = bit >> 5; 3388 piece = bit % (sizeof(uint32_t) * 8); 3389 3390 if ((mask[chunk] & (1 << piece)) == 0) 3391 return (0); 3392 else 3393 return (1); 3394} 3395 3396static uint64_t 3397ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3398{ 3399 uint64_t *t; 3400 3401 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3402 if (t == NULL) 3403 return (0); 3404 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3405} 3406 3407static void 3408ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3409{ 3410 uint64_t *t; 3411 3412 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3413 if (t == NULL) 3414 return; 3415 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3416} 3417 3418static void 3419ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3420{ 3421 uint64_t *p; 3422 u_int i; 3423 3424 i = residx/CTL_MAX_INIT_PER_PORT; 3425 if (lun->pr_keys[i] != NULL) 3426 return; 3427 mtx_unlock(&lun->lun_lock); 3428 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3429 M_WAITOK | M_ZERO); 3430 mtx_lock(&lun->lun_lock); 3431 if (lun->pr_keys[i] == NULL) 3432 lun->pr_keys[i] = p; 3433 else 3434 free(p, M_CTL); 3435} 3436 3437static void 3438ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3439{ 3440 uint64_t *t; 3441 3442 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3443 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3444 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3445} 3446 3447/* 3448 * ctl_softc, pool_name, total_ctl_io are passed in. 3449 * npool is passed out. 3450 */ 3451int 3452ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3453 uint32_t total_ctl_io, void **npool) 3454{ 3455#ifdef IO_POOLS 3456 struct ctl_io_pool *pool; 3457 3458 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3459 M_NOWAIT | M_ZERO); 3460 if (pool == NULL) 3461 return (ENOMEM); 3462 3463 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3464 pool->ctl_softc = ctl_softc; 3465 pool->zone = uma_zsecond_create(pool->name, NULL, 3466 NULL, NULL, NULL, ctl_softc->io_zone); 3467 /* uma_prealloc(pool->zone, total_ctl_io); */ 3468 3469 *npool = pool; 3470#else 3471 *npool = ctl_softc->io_zone; 3472#endif 3473 return (0); 3474} 3475 3476void 3477ctl_pool_free(struct ctl_io_pool *pool) 3478{ 3479 3480 if (pool == NULL) 3481 return; 3482 3483#ifdef IO_POOLS 3484 uma_zdestroy(pool->zone); 3485 free(pool, M_CTL); 3486#endif 3487} 3488 3489union ctl_io * 3490ctl_alloc_io(void *pool_ref) 3491{ 3492 union ctl_io *io; 3493#ifdef IO_POOLS 3494 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3495 3496 io = uma_zalloc(pool->zone, M_WAITOK); 3497#else 3498 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3499#endif 3500 if (io != NULL) 3501 io->io_hdr.pool = pool_ref; 3502 return (io); 3503} 3504 3505union ctl_io * 3506ctl_alloc_io_nowait(void *pool_ref) 3507{ 3508 union ctl_io *io; 3509#ifdef IO_POOLS 3510 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3511 3512 io = uma_zalloc(pool->zone, M_NOWAIT); 3513#else 3514 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3515#endif 3516 if (io != NULL) 3517 io->io_hdr.pool = pool_ref; 3518 return (io); 3519} 3520 3521void 3522ctl_free_io(union ctl_io *io) 3523{ 3524#ifdef IO_POOLS 3525 struct ctl_io_pool *pool; 3526#endif 3527 3528 if (io == NULL) 3529 return; 3530 3531#ifdef IO_POOLS 3532 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3533 uma_zfree(pool->zone, io); 3534#else 3535 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3536#endif 3537} 3538 3539void 3540ctl_zero_io(union ctl_io *io) 3541{ 3542 void *pool_ref; 3543 3544 if (io == NULL) 3545 return; 3546 3547 /* 3548 * May need to preserve linked list pointers at some point too. 3549 */ 3550 pool_ref = io->io_hdr.pool; 3551 memset(io, 0, sizeof(*io)); 3552 io->io_hdr.pool = pool_ref; 3553} 3554 3555/* 3556 * This routine is currently used for internal copies of ctl_ios that need 3557 * to persist for some reason after we've already returned status to the 3558 * FETD. (Thus the flag set.) 3559 * 3560 * XXX XXX 3561 * Note that this makes a blind copy of all fields in the ctl_io, except 3562 * for the pool reference. This includes any memory that has been 3563 * allocated! That memory will no longer be valid after done has been 3564 * called, so this would be VERY DANGEROUS for command that actually does 3565 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3566 * start and stop commands, which don't transfer any data, so this is not a 3567 * problem. If it is used for anything else, the caller would also need to 3568 * allocate data buffer space and this routine would need to be modified to 3569 * copy the data buffer(s) as well. 3570 */ 3571void 3572ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3573{ 3574 void *pool_ref; 3575 3576 if ((src == NULL) 3577 || (dest == NULL)) 3578 return; 3579 3580 /* 3581 * May need to preserve linked list pointers at some point too. 3582 */ 3583 pool_ref = dest->io_hdr.pool; 3584 3585 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest))); 3586 3587 dest->io_hdr.pool = pool_ref; 3588 /* 3589 * We need to know that this is an internal copy, and doesn't need 3590 * to get passed back to the FETD that allocated it. 3591 */ 3592 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3593} 3594 3595int 3596ctl_expand_number(const char *buf, uint64_t *num) 3597{ 3598 char *endptr; 3599 uint64_t number; 3600 unsigned shift; 3601 3602 number = strtoq(buf, &endptr, 0); 3603 3604 switch (tolower((unsigned char)*endptr)) { 3605 case 'e': 3606 shift = 60; 3607 break; 3608 case 'p': 3609 shift = 50; 3610 break; 3611 case 't': 3612 shift = 40; 3613 break; 3614 case 'g': 3615 shift = 30; 3616 break; 3617 case 'm': 3618 shift = 20; 3619 break; 3620 case 'k': 3621 shift = 10; 3622 break; 3623 case 'b': 3624 case '\0': /* No unit. */ 3625 *num = number; 3626 return (0); 3627 default: 3628 /* Unrecognized unit. */ 3629 return (-1); 3630 } 3631 3632 if ((number << shift) >> shift != number) { 3633 /* Overflow */ 3634 return (-1); 3635 } 3636 *num = number << shift; 3637 return (0); 3638} 3639 3640 3641/* 3642 * This routine could be used in the future to load default and/or saved 3643 * mode page parameters for a particuar lun. 3644 */ 3645static int 3646ctl_init_page_index(struct ctl_lun *lun) 3647{ 3648 int i; 3649 struct ctl_page_index *page_index; 3650 const char *value; 3651 uint64_t ival; 3652 3653 memcpy(&lun->mode_pages.index, page_index_template, 3654 sizeof(page_index_template)); 3655 3656 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3657 3658 page_index = &lun->mode_pages.index[i]; 3659 /* 3660 * If this is a disk-only mode page, there's no point in 3661 * setting it up. For some pages, we have to have some 3662 * basic information about the disk in order to calculate the 3663 * mode page data. 3664 */ 3665 if ((lun->be_lun->lun_type != T_DIRECT) 3666 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3667 continue; 3668 3669 switch (page_index->page_code & SMPH_PC_MASK) { 3670 case SMS_RW_ERROR_RECOVERY_PAGE: { 3671 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3672 panic("subpage is incorrect!"); 3673 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3674 &rw_er_page_default, 3675 sizeof(rw_er_page_default)); 3676 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3677 &rw_er_page_changeable, 3678 sizeof(rw_er_page_changeable)); 3679 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3680 &rw_er_page_default, 3681 sizeof(rw_er_page_default)); 3682 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3683 &rw_er_page_default, 3684 sizeof(rw_er_page_default)); 3685 page_index->page_data = 3686 (uint8_t *)lun->mode_pages.rw_er_page; 3687 break; 3688 } 3689 case SMS_FORMAT_DEVICE_PAGE: { 3690 struct scsi_format_page *format_page; 3691 3692 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3693 panic("subpage is incorrect!"); 3694 3695 /* 3696 * Sectors per track are set above. Bytes per 3697 * sector need to be set here on a per-LUN basis. 3698 */ 3699 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3700 &format_page_default, 3701 sizeof(format_page_default)); 3702 memcpy(&lun->mode_pages.format_page[ 3703 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3704 sizeof(format_page_changeable)); 3705 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3706 &format_page_default, 3707 sizeof(format_page_default)); 3708 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3709 &format_page_default, 3710 sizeof(format_page_default)); 3711 3712 format_page = &lun->mode_pages.format_page[ 3713 CTL_PAGE_CURRENT]; 3714 scsi_ulto2b(lun->be_lun->blocksize, 3715 format_page->bytes_per_sector); 3716 3717 format_page = &lun->mode_pages.format_page[ 3718 CTL_PAGE_DEFAULT]; 3719 scsi_ulto2b(lun->be_lun->blocksize, 3720 format_page->bytes_per_sector); 3721 3722 format_page = &lun->mode_pages.format_page[ 3723 CTL_PAGE_SAVED]; 3724 scsi_ulto2b(lun->be_lun->blocksize, 3725 format_page->bytes_per_sector); 3726 3727 page_index->page_data = 3728 (uint8_t *)lun->mode_pages.format_page; 3729 break; 3730 } 3731 case SMS_RIGID_DISK_PAGE: { 3732 struct scsi_rigid_disk_page *rigid_disk_page; 3733 uint32_t sectors_per_cylinder; 3734 uint64_t cylinders; 3735#ifndef __XSCALE__ 3736 int shift; 3737#endif /* !__XSCALE__ */ 3738 3739 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3740 panic("invalid subpage value %d", 3741 page_index->subpage); 3742 3743 /* 3744 * Rotation rate and sectors per track are set 3745 * above. We calculate the cylinders here based on 3746 * capacity. Due to the number of heads and 3747 * sectors per track we're using, smaller arrays 3748 * may turn out to have 0 cylinders. Linux and 3749 * FreeBSD don't pay attention to these mode pages 3750 * to figure out capacity, but Solaris does. It 3751 * seems to deal with 0 cylinders just fine, and 3752 * works out a fake geometry based on the capacity. 3753 */ 3754 memcpy(&lun->mode_pages.rigid_disk_page[ 3755 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3756 sizeof(rigid_disk_page_default)); 3757 memcpy(&lun->mode_pages.rigid_disk_page[ 3758 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3759 sizeof(rigid_disk_page_changeable)); 3760 3761 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 3762 CTL_DEFAULT_HEADS; 3763 3764 /* 3765 * The divide method here will be more accurate, 3766 * probably, but results in floating point being 3767 * used in the kernel on i386 (__udivdi3()). On the 3768 * XScale, though, __udivdi3() is implemented in 3769 * software. 3770 * 3771 * The shift method for cylinder calculation is 3772 * accurate if sectors_per_cylinder is a power of 3773 * 2. Otherwise it might be slightly off -- you 3774 * might have a bit of a truncation problem. 3775 */ 3776#ifdef __XSCALE__ 3777 cylinders = (lun->be_lun->maxlba + 1) / 3778 sectors_per_cylinder; 3779#else 3780 for (shift = 31; shift > 0; shift--) { 3781 if (sectors_per_cylinder & (1 << shift)) 3782 break; 3783 } 3784 cylinders = (lun->be_lun->maxlba + 1) >> shift; 3785#endif 3786 3787 /* 3788 * We've basically got 3 bytes, or 24 bits for the 3789 * cylinder size in the mode page. If we're over, 3790 * just round down to 2^24. 3791 */ 3792 if (cylinders > 0xffffff) 3793 cylinders = 0xffffff; 3794 3795 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 3796 CTL_PAGE_DEFAULT]; 3797 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 3798 3799 if ((value = ctl_get_opt(&lun->be_lun->options, 3800 "rpm")) != NULL) { 3801 scsi_ulto2b(strtol(value, NULL, 0), 3802 rigid_disk_page->rotation_rate); 3803 } 3804 3805 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 3806 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3807 sizeof(rigid_disk_page_default)); 3808 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 3809 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3810 sizeof(rigid_disk_page_default)); 3811 3812 page_index->page_data = 3813 (uint8_t *)lun->mode_pages.rigid_disk_page; 3814 break; 3815 } 3816 case SMS_CACHING_PAGE: { 3817 struct scsi_caching_page *caching_page; 3818 3819 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3820 panic("invalid subpage value %d", 3821 page_index->subpage); 3822 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 3823 &caching_page_default, 3824 sizeof(caching_page_default)); 3825 memcpy(&lun->mode_pages.caching_page[ 3826 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 3827 sizeof(caching_page_changeable)); 3828 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 3829 &caching_page_default, 3830 sizeof(caching_page_default)); 3831 caching_page = &lun->mode_pages.caching_page[ 3832 CTL_PAGE_SAVED]; 3833 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 3834 if (value != NULL && strcmp(value, "off") == 0) 3835 caching_page->flags1 &= ~SCP_WCE; 3836 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 3837 if (value != NULL && strcmp(value, "off") == 0) 3838 caching_page->flags1 |= SCP_RCD; 3839 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 3840 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 3841 sizeof(caching_page_default)); 3842 page_index->page_data = 3843 (uint8_t *)lun->mode_pages.caching_page; 3844 break; 3845 } 3846 case SMS_CONTROL_MODE_PAGE: { 3847 struct scsi_control_page *control_page; 3848 3849 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3850 panic("invalid subpage value %d", 3851 page_index->subpage); 3852 3853 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 3854 &control_page_default, 3855 sizeof(control_page_default)); 3856 memcpy(&lun->mode_pages.control_page[ 3857 CTL_PAGE_CHANGEABLE], &control_page_changeable, 3858 sizeof(control_page_changeable)); 3859 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 3860 &control_page_default, 3861 sizeof(control_page_default)); 3862 control_page = &lun->mode_pages.control_page[ 3863 CTL_PAGE_SAVED]; 3864 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 3865 if (value != NULL && strcmp(value, "unrestricted") == 0) { 3866 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 3867 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 3868 } 3869 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 3870 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 3871 sizeof(control_page_default)); 3872 page_index->page_data = 3873 (uint8_t *)lun->mode_pages.control_page; 3874 break; 3875 3876 } 3877 case SMS_INFO_EXCEPTIONS_PAGE: { 3878 switch (page_index->subpage) { 3879 case SMS_SUBPAGE_PAGE_0: 3880 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 3881 &ie_page_default, 3882 sizeof(ie_page_default)); 3883 memcpy(&lun->mode_pages.ie_page[ 3884 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 3885 sizeof(ie_page_changeable)); 3886 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 3887 &ie_page_default, 3888 sizeof(ie_page_default)); 3889 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 3890 &ie_page_default, 3891 sizeof(ie_page_default)); 3892 page_index->page_data = 3893 (uint8_t *)lun->mode_pages.ie_page; 3894 break; 3895 case 0x02: { 3896 struct ctl_logical_block_provisioning_page *page; 3897 3898 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 3899 &lbp_page_default, 3900 sizeof(lbp_page_default)); 3901 memcpy(&lun->mode_pages.lbp_page[ 3902 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 3903 sizeof(lbp_page_changeable)); 3904 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 3905 &lbp_page_default, 3906 sizeof(lbp_page_default)); 3907 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 3908 value = ctl_get_opt(&lun->be_lun->options, 3909 "avail-threshold"); 3910 if (value != NULL && 3911 ctl_expand_number(value, &ival) == 0) { 3912 page->descr[0].flags |= SLBPPD_ENABLED | 3913 SLBPPD_ARMING_DEC; 3914 if (lun->be_lun->blocksize) 3915 ival /= lun->be_lun->blocksize; 3916 else 3917 ival /= 512; 3918 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3919 page->descr[0].count); 3920 } 3921 value = ctl_get_opt(&lun->be_lun->options, 3922 "used-threshold"); 3923 if (value != NULL && 3924 ctl_expand_number(value, &ival) == 0) { 3925 page->descr[1].flags |= SLBPPD_ENABLED | 3926 SLBPPD_ARMING_INC; 3927 if (lun->be_lun->blocksize) 3928 ival /= lun->be_lun->blocksize; 3929 else 3930 ival /= 512; 3931 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3932 page->descr[1].count); 3933 } 3934 value = ctl_get_opt(&lun->be_lun->options, 3935 "pool-avail-threshold"); 3936 if (value != NULL && 3937 ctl_expand_number(value, &ival) == 0) { 3938 page->descr[2].flags |= SLBPPD_ENABLED | 3939 SLBPPD_ARMING_DEC; 3940 if (lun->be_lun->blocksize) 3941 ival /= lun->be_lun->blocksize; 3942 else 3943 ival /= 512; 3944 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3945 page->descr[2].count); 3946 } 3947 value = ctl_get_opt(&lun->be_lun->options, 3948 "pool-used-threshold"); 3949 if (value != NULL && 3950 ctl_expand_number(value, &ival) == 0) { 3951 page->descr[3].flags |= SLBPPD_ENABLED | 3952 SLBPPD_ARMING_INC; 3953 if (lun->be_lun->blocksize) 3954 ival /= lun->be_lun->blocksize; 3955 else 3956 ival /= 512; 3957 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3958 page->descr[3].count); 3959 } 3960 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 3961 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 3962 sizeof(lbp_page_default)); 3963 page_index->page_data = 3964 (uint8_t *)lun->mode_pages.lbp_page; 3965 }} 3966 break; 3967 } 3968 case SMS_VENDOR_SPECIFIC_PAGE:{ 3969 switch (page_index->subpage) { 3970 case DBGCNF_SUBPAGE_CODE: { 3971 struct copan_debugconf_subpage *current_page, 3972 *saved_page; 3973 3974 memcpy(&lun->mode_pages.debugconf_subpage[ 3975 CTL_PAGE_CURRENT], 3976 &debugconf_page_default, 3977 sizeof(debugconf_page_default)); 3978 memcpy(&lun->mode_pages.debugconf_subpage[ 3979 CTL_PAGE_CHANGEABLE], 3980 &debugconf_page_changeable, 3981 sizeof(debugconf_page_changeable)); 3982 memcpy(&lun->mode_pages.debugconf_subpage[ 3983 CTL_PAGE_DEFAULT], 3984 &debugconf_page_default, 3985 sizeof(debugconf_page_default)); 3986 memcpy(&lun->mode_pages.debugconf_subpage[ 3987 CTL_PAGE_SAVED], 3988 &debugconf_page_default, 3989 sizeof(debugconf_page_default)); 3990 page_index->page_data = 3991 (uint8_t *)lun->mode_pages.debugconf_subpage; 3992 3993 current_page = (struct copan_debugconf_subpage *) 3994 (page_index->page_data + 3995 (page_index->page_len * 3996 CTL_PAGE_CURRENT)); 3997 saved_page = (struct copan_debugconf_subpage *) 3998 (page_index->page_data + 3999 (page_index->page_len * 4000 CTL_PAGE_SAVED)); 4001 break; 4002 } 4003 default: 4004 panic("invalid subpage value %d", 4005 page_index->subpage); 4006 break; 4007 } 4008 break; 4009 } 4010 default: 4011 panic("invalid page value %d", 4012 page_index->page_code & SMPH_PC_MASK); 4013 break; 4014 } 4015 } 4016 4017 return (CTL_RETVAL_COMPLETE); 4018} 4019 4020static int 4021ctl_init_log_page_index(struct ctl_lun *lun) 4022{ 4023 struct ctl_page_index *page_index; 4024 int i, j, k, prev; 4025 4026 memcpy(&lun->log_pages.index, log_page_index_template, 4027 sizeof(log_page_index_template)); 4028 4029 prev = -1; 4030 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4031 4032 page_index = &lun->log_pages.index[i]; 4033 /* 4034 * If this is a disk-only mode page, there's no point in 4035 * setting it up. For some pages, we have to have some 4036 * basic information about the disk in order to calculate the 4037 * mode page data. 4038 */ 4039 if ((lun->be_lun->lun_type != T_DIRECT) 4040 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4041 continue; 4042 4043 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4044 lun->backend->lun_attr == NULL) 4045 continue; 4046 4047 if (page_index->page_code != prev) { 4048 lun->log_pages.pages_page[j] = page_index->page_code; 4049 prev = page_index->page_code; 4050 j++; 4051 } 4052 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4053 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4054 k++; 4055 } 4056 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4057 lun->log_pages.index[0].page_len = j; 4058 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4059 lun->log_pages.index[1].page_len = k * 2; 4060 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4061 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4062 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4063 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4064 4065 return (CTL_RETVAL_COMPLETE); 4066} 4067 4068static int 4069hex2bin(const char *str, uint8_t *buf, int buf_size) 4070{ 4071 int i; 4072 u_char c; 4073 4074 memset(buf, 0, buf_size); 4075 while (isspace(str[0])) 4076 str++; 4077 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4078 str += 2; 4079 buf_size *= 2; 4080 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4081 c = str[i]; 4082 if (isdigit(c)) 4083 c -= '0'; 4084 else if (isalpha(c)) 4085 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4086 else 4087 break; 4088 if (c >= 16) 4089 break; 4090 if ((i & 1) == 0) 4091 buf[i / 2] |= (c << 4); 4092 else 4093 buf[i / 2] |= c; 4094 } 4095 return ((i + 1) / 2); 4096} 4097 4098/* 4099 * LUN allocation. 4100 * 4101 * Requirements: 4102 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4103 * wants us to allocate the LUN and he can block. 4104 * - ctl_softc is always set 4105 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4106 * 4107 * Returns 0 for success, non-zero (errno) for failure. 4108 */ 4109static int 4110ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4111 struct ctl_be_lun *const be_lun) 4112{ 4113 struct ctl_lun *nlun, *lun; 4114 struct scsi_vpd_id_descriptor *desc; 4115 struct scsi_vpd_id_t10 *t10id; 4116 const char *eui, *naa, *scsiname, *vendor; 4117 int lun_number, i, lun_malloced; 4118 int devidlen, idlen1, idlen2 = 0, len; 4119 4120 if (be_lun == NULL) 4121 return (EINVAL); 4122 4123 /* 4124 * We currently only support Direct Access or Processor LUN types. 4125 */ 4126 switch (be_lun->lun_type) { 4127 case T_DIRECT: 4128 break; 4129 case T_PROCESSOR: 4130 break; 4131 case T_SEQUENTIAL: 4132 case T_CHANGER: 4133 default: 4134 be_lun->lun_config_status(be_lun->be_lun, 4135 CTL_LUN_CONFIG_FAILURE); 4136 break; 4137 } 4138 if (ctl_lun == NULL) { 4139 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4140 lun_malloced = 1; 4141 } else { 4142 lun_malloced = 0; 4143 lun = ctl_lun; 4144 } 4145 4146 memset(lun, 0, sizeof(*lun)); 4147 if (lun_malloced) 4148 lun->flags = CTL_LUN_MALLOCED; 4149 4150 /* Generate LUN ID. */ 4151 devidlen = max(CTL_DEVID_MIN_LEN, 4152 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4153 idlen1 = sizeof(*t10id) + devidlen; 4154 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4155 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4156 if (scsiname != NULL) { 4157 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4158 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4159 } 4160 eui = ctl_get_opt(&be_lun->options, "eui"); 4161 if (eui != NULL) { 4162 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4163 } 4164 naa = ctl_get_opt(&be_lun->options, "naa"); 4165 if (naa != NULL) { 4166 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4167 } 4168 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4169 M_CTL, M_WAITOK | M_ZERO); 4170 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4171 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4172 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4173 desc->length = idlen1; 4174 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4175 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4176 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4177 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4178 } else { 4179 strncpy(t10id->vendor, vendor, 4180 min(sizeof(t10id->vendor), strlen(vendor))); 4181 } 4182 strncpy((char *)t10id->vendor_spec_id, 4183 (char *)be_lun->device_id, devidlen); 4184 if (scsiname != NULL) { 4185 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4186 desc->length); 4187 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4188 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4189 SVPD_ID_TYPE_SCSI_NAME; 4190 desc->length = idlen2; 4191 strlcpy(desc->identifier, scsiname, idlen2); 4192 } 4193 if (eui != NULL) { 4194 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4195 desc->length); 4196 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4197 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4198 SVPD_ID_TYPE_EUI64; 4199 desc->length = hex2bin(eui, desc->identifier, 16); 4200 desc->length = desc->length > 12 ? 16 : 4201 (desc->length > 8 ? 12 : 8); 4202 len -= 16 - desc->length; 4203 } 4204 if (naa != NULL) { 4205 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4206 desc->length); 4207 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4208 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4209 SVPD_ID_TYPE_NAA; 4210 desc->length = hex2bin(naa, desc->identifier, 16); 4211 desc->length = desc->length > 8 ? 16 : 8; 4212 len -= 16 - desc->length; 4213 } 4214 lun->lun_devid->len = len; 4215 4216 mtx_lock(&ctl_softc->ctl_lock); 4217 /* 4218 * See if the caller requested a particular LUN number. If so, see 4219 * if it is available. Otherwise, allocate the first available LUN. 4220 */ 4221 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4222 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4223 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4224 mtx_unlock(&ctl_softc->ctl_lock); 4225 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4226 printf("ctl: requested LUN ID %d is higher " 4227 "than CTL_MAX_LUNS - 1 (%d)\n", 4228 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4229 } else { 4230 /* 4231 * XXX KDM return an error, or just assign 4232 * another LUN ID in this case?? 4233 */ 4234 printf("ctl: requested LUN ID %d is already " 4235 "in use\n", be_lun->req_lun_id); 4236 } 4237 if (lun->flags & CTL_LUN_MALLOCED) 4238 free(lun, M_CTL); 4239 be_lun->lun_config_status(be_lun->be_lun, 4240 CTL_LUN_CONFIG_FAILURE); 4241 return (ENOSPC); 4242 } 4243 lun_number = be_lun->req_lun_id; 4244 } else { 4245 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4246 if (lun_number == -1) { 4247 mtx_unlock(&ctl_softc->ctl_lock); 4248 printf("ctl: can't allocate LUN, out of LUNs\n"); 4249 if (lun->flags & CTL_LUN_MALLOCED) 4250 free(lun, M_CTL); 4251 be_lun->lun_config_status(be_lun->be_lun, 4252 CTL_LUN_CONFIG_FAILURE); 4253 return (ENOSPC); 4254 } 4255 } 4256 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4257 4258 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4259 lun->lun = lun_number; 4260 lun->be_lun = be_lun; 4261 /* 4262 * The processor LUN is always enabled. Disk LUNs come on line 4263 * disabled, and must be enabled by the backend. 4264 */ 4265 lun->flags |= CTL_LUN_DISABLED; 4266 lun->backend = be_lun->be; 4267 be_lun->ctl_lun = lun; 4268 be_lun->lun_id = lun_number; 4269 atomic_add_int(&be_lun->be->num_luns, 1); 4270 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4271 lun->flags |= CTL_LUN_OFFLINE; 4272 4273 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4274 lun->flags |= CTL_LUN_STOPPED; 4275 4276 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4277 lun->flags |= CTL_LUN_INOPERABLE; 4278 4279 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4280 lun->flags |= CTL_LUN_PRIMARY_SC; 4281 4282 lun->ctl_softc = ctl_softc; 4283#ifdef CTL_TIME_IO 4284 lun->last_busy = getsbinuptime(); 4285#endif 4286 TAILQ_INIT(&lun->ooa_queue); 4287 TAILQ_INIT(&lun->blocked_queue); 4288 STAILQ_INIT(&lun->error_list); 4289 ctl_tpc_lun_init(lun); 4290 4291 /* 4292 * Initialize the mode and log page index. 4293 */ 4294 ctl_init_page_index(lun); 4295 ctl_init_log_page_index(lun); 4296 4297 /* 4298 * Now, before we insert this lun on the lun list, set the lun 4299 * inventory changed UA for all other luns. 4300 */ 4301 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4302 mtx_lock(&nlun->lun_lock); 4303 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4304 mtx_unlock(&nlun->lun_lock); 4305 } 4306 4307 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4308 4309 ctl_softc->ctl_luns[lun_number] = lun; 4310 4311 ctl_softc->num_luns++; 4312 4313 /* Setup statistics gathering */ 4314 lun->stats.device_type = be_lun->lun_type; 4315 lun->stats.lun_number = lun_number; 4316 if (lun->stats.device_type == T_DIRECT) 4317 lun->stats.blocksize = be_lun->blocksize; 4318 else 4319 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4320 for (i = 0;i < CTL_MAX_PORTS;i++) 4321 lun->stats.ports[i].targ_port = i; 4322 4323 mtx_unlock(&ctl_softc->ctl_lock); 4324 4325 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4326 return (0); 4327} 4328 4329/* 4330 * Delete a LUN. 4331 * Assumptions: 4332 * - LUN has already been marked invalid and any pending I/O has been taken 4333 * care of. 4334 */ 4335static int 4336ctl_free_lun(struct ctl_lun *lun) 4337{ 4338 struct ctl_softc *softc; 4339 struct ctl_lun *nlun; 4340 int i; 4341 4342 softc = lun->ctl_softc; 4343 4344 mtx_assert(&softc->ctl_lock, MA_OWNED); 4345 4346 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4347 4348 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4349 4350 softc->ctl_luns[lun->lun] = NULL; 4351 4352 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4353 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4354 4355 softc->num_luns--; 4356 4357 /* 4358 * Tell the backend to free resources, if this LUN has a backend. 4359 */ 4360 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4361 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4362 4363 ctl_tpc_lun_shutdown(lun); 4364 mtx_destroy(&lun->lun_lock); 4365 free(lun->lun_devid, M_CTL); 4366 for (i = 0; i < CTL_MAX_PORTS; i++) 4367 free(lun->pending_ua[i], M_CTL); 4368 for (i = 0; i < CTL_MAX_PORTS; i++) 4369 free(lun->pr_keys[i], M_CTL); 4370 free(lun->write_buffer, M_CTL); 4371 if (lun->flags & CTL_LUN_MALLOCED) 4372 free(lun, M_CTL); 4373 4374 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4375 mtx_lock(&nlun->lun_lock); 4376 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4377 mtx_unlock(&nlun->lun_lock); 4378 } 4379 4380 return (0); 4381} 4382 4383static void 4384ctl_create_lun(struct ctl_be_lun *be_lun) 4385{ 4386 struct ctl_softc *softc; 4387 4388 softc = control_softc; 4389 4390 /* 4391 * ctl_alloc_lun() should handle all potential failure cases. 4392 */ 4393 ctl_alloc_lun(softc, NULL, be_lun); 4394} 4395 4396int 4397ctl_add_lun(struct ctl_be_lun *be_lun) 4398{ 4399 struct ctl_softc *softc = control_softc; 4400 4401 mtx_lock(&softc->ctl_lock); 4402 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4403 mtx_unlock(&softc->ctl_lock); 4404 wakeup(&softc->pending_lun_queue); 4405 4406 return (0); 4407} 4408 4409int 4410ctl_enable_lun(struct ctl_be_lun *be_lun) 4411{ 4412 struct ctl_softc *softc; 4413 struct ctl_port *port, *nport; 4414 struct ctl_lun *lun; 4415 int retval; 4416 4417 lun = (struct ctl_lun *)be_lun->ctl_lun; 4418 softc = lun->ctl_softc; 4419 4420 mtx_lock(&softc->ctl_lock); 4421 mtx_lock(&lun->lun_lock); 4422 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4423 /* 4424 * eh? Why did we get called if the LUN is already 4425 * enabled? 4426 */ 4427 mtx_unlock(&lun->lun_lock); 4428 mtx_unlock(&softc->ctl_lock); 4429 return (0); 4430 } 4431 lun->flags &= ~CTL_LUN_DISABLED; 4432 mtx_unlock(&lun->lun_lock); 4433 4434 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) { 4435 nport = STAILQ_NEXT(port, links); 4436 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4437 port->lun_map != NULL || port->lun_enable == NULL) 4438 continue; 4439 4440 /* 4441 * Drop the lock while we call the FETD's enable routine. 4442 * This can lead to a callback into CTL (at least in the 4443 * case of the internal initiator frontend. 4444 */ 4445 mtx_unlock(&softc->ctl_lock); 4446 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4447 mtx_lock(&softc->ctl_lock); 4448 if (retval != 0) { 4449 printf("%s: FETD %s port %d returned error " 4450 "%d for lun_enable on lun %jd\n", 4451 __func__, port->port_name, port->targ_port, 4452 retval, (intmax_t)lun->lun); 4453 } 4454 } 4455 4456 mtx_unlock(&softc->ctl_lock); 4457 ctl_isc_announce_lun(lun); 4458 4459 return (0); 4460} 4461 4462int 4463ctl_disable_lun(struct ctl_be_lun *be_lun) 4464{ 4465 struct ctl_softc *softc; 4466 struct ctl_port *port; 4467 struct ctl_lun *lun; 4468 int retval; 4469 4470 lun = (struct ctl_lun *)be_lun->ctl_lun; 4471 softc = lun->ctl_softc; 4472 4473 mtx_lock(&softc->ctl_lock); 4474 mtx_lock(&lun->lun_lock); 4475 if (lun->flags & CTL_LUN_DISABLED) { 4476 mtx_unlock(&lun->lun_lock); 4477 mtx_unlock(&softc->ctl_lock); 4478 return (0); 4479 } 4480 lun->flags |= CTL_LUN_DISABLED; 4481 mtx_unlock(&lun->lun_lock); 4482 4483 STAILQ_FOREACH(port, &softc->port_list, links) { 4484 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4485 port->lun_map != NULL || port->lun_disable == NULL) 4486 continue; 4487 4488 /* 4489 * Drop the lock before we call the frontend's disable 4490 * routine, to avoid lock order reversals. 4491 * 4492 * XXX KDM what happens if the frontend list changes while 4493 * we're traversing it? It's unlikely, but should be handled. 4494 */ 4495 mtx_unlock(&softc->ctl_lock); 4496 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4497 mtx_lock(&softc->ctl_lock); 4498 if (retval != 0) { 4499 printf("%s: FETD %s port %d returned error " 4500 "%d for lun_disable on lun %jd\n", 4501 __func__, port->port_name, port->targ_port, 4502 retval, (intmax_t)lun->lun); 4503 } 4504 } 4505 4506 mtx_unlock(&softc->ctl_lock); 4507 ctl_isc_announce_lun(lun); 4508 4509 return (0); 4510} 4511 4512int 4513ctl_start_lun(struct ctl_be_lun *be_lun) 4514{ 4515 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4516 4517 mtx_lock(&lun->lun_lock); 4518 lun->flags &= ~CTL_LUN_STOPPED; 4519 mtx_unlock(&lun->lun_lock); 4520 return (0); 4521} 4522 4523int 4524ctl_stop_lun(struct ctl_be_lun *be_lun) 4525{ 4526 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4527 4528 mtx_lock(&lun->lun_lock); 4529 lun->flags |= CTL_LUN_STOPPED; 4530 mtx_unlock(&lun->lun_lock); 4531 return (0); 4532} 4533 4534int 4535ctl_lun_offline(struct ctl_be_lun *be_lun) 4536{ 4537 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4538 4539 mtx_lock(&lun->lun_lock); 4540 lun->flags |= CTL_LUN_OFFLINE; 4541 mtx_unlock(&lun->lun_lock); 4542 return (0); 4543} 4544 4545int 4546ctl_lun_online(struct ctl_be_lun *be_lun) 4547{ 4548 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4549 4550 mtx_lock(&lun->lun_lock); 4551 lun->flags &= ~CTL_LUN_OFFLINE; 4552 mtx_unlock(&lun->lun_lock); 4553 return (0); 4554} 4555 4556int 4557ctl_lun_primary(struct ctl_be_lun *be_lun) 4558{ 4559 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4560 4561 mtx_lock(&lun->lun_lock); 4562 lun->flags |= CTL_LUN_PRIMARY_SC; 4563 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4564 mtx_unlock(&lun->lun_lock); 4565 ctl_isc_announce_lun(lun); 4566 return (0); 4567} 4568 4569int 4570ctl_lun_secondary(struct ctl_be_lun *be_lun) 4571{ 4572 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4573 4574 mtx_lock(&lun->lun_lock); 4575 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4576 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4577 mtx_unlock(&lun->lun_lock); 4578 ctl_isc_announce_lun(lun); 4579 return (0); 4580} 4581 4582int 4583ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4584{ 4585 struct ctl_softc *softc; 4586 struct ctl_lun *lun; 4587 4588 lun = (struct ctl_lun *)be_lun->ctl_lun; 4589 softc = lun->ctl_softc; 4590 4591 mtx_lock(&lun->lun_lock); 4592 4593 /* 4594 * The LUN needs to be disabled before it can be marked invalid. 4595 */ 4596 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4597 mtx_unlock(&lun->lun_lock); 4598 return (-1); 4599 } 4600 /* 4601 * Mark the LUN invalid. 4602 */ 4603 lun->flags |= CTL_LUN_INVALID; 4604 4605 /* 4606 * If there is nothing in the OOA queue, go ahead and free the LUN. 4607 * If we have something in the OOA queue, we'll free it when the 4608 * last I/O completes. 4609 */ 4610 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4611 mtx_unlock(&lun->lun_lock); 4612 mtx_lock(&softc->ctl_lock); 4613 ctl_free_lun(lun); 4614 mtx_unlock(&softc->ctl_lock); 4615 } else 4616 mtx_unlock(&lun->lun_lock); 4617 4618 return (0); 4619} 4620 4621int 4622ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4623{ 4624 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4625 4626 mtx_lock(&lun->lun_lock); 4627 lun->flags |= CTL_LUN_INOPERABLE; 4628 mtx_unlock(&lun->lun_lock); 4629 return (0); 4630} 4631 4632int 4633ctl_lun_operable(struct ctl_be_lun *be_lun) 4634{ 4635 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4636 4637 mtx_lock(&lun->lun_lock); 4638 lun->flags &= ~CTL_LUN_INOPERABLE; 4639 mtx_unlock(&lun->lun_lock); 4640 return (0); 4641} 4642 4643void 4644ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4645{ 4646 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4647 union ctl_ha_msg msg; 4648 4649 mtx_lock(&lun->lun_lock); 4650 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED); 4651 mtx_unlock(&lun->lun_lock); 4652 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4653 /* Send msg to other side. */ 4654 bzero(&msg.ua, sizeof(msg.ua)); 4655 msg.hdr.msg_type = CTL_MSG_UA; 4656 msg.hdr.nexus.initid = -1; 4657 msg.hdr.nexus.targ_port = -1; 4658 msg.hdr.nexus.targ_lun = lun->lun; 4659 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4660 msg.ua.ua_all = 1; 4661 msg.ua.ua_set = 1; 4662 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGED; 4663 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4664 M_WAITOK); 4665 } 4666} 4667 4668/* 4669 * Backend "memory move is complete" callback for requests that never 4670 * make it down to say RAIDCore's configuration code. 4671 */ 4672int 4673ctl_config_move_done(union ctl_io *io) 4674{ 4675 int retval; 4676 4677 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4678 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4679 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 4680 4681 if ((io->io_hdr.port_status != 0) && 4682 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4683 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4684 /* 4685 * For hardware error sense keys, the sense key 4686 * specific value is defined to be a retry count, 4687 * but we use it to pass back an internal FETD 4688 * error code. XXX KDM Hopefully the FETD is only 4689 * using 16 bits for an error code, since that's 4690 * all the space we have in the sks field. 4691 */ 4692 ctl_set_internal_failure(&io->scsiio, 4693 /*sks_valid*/ 1, 4694 /*retry_count*/ 4695 io->io_hdr.port_status); 4696 } 4697 4698 if (ctl_debug & CTL_DEBUG_CDB_DATA) 4699 ctl_data_print(io); 4700 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 4701 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4702 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 4703 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4704 /* 4705 * XXX KDM just assuming a single pointer here, and not a 4706 * S/G list. If we start using S/G lists for config data, 4707 * we'll need to know how to clean them up here as well. 4708 */ 4709 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4710 free(io->scsiio.kern_data_ptr, M_CTL); 4711 ctl_done(io); 4712 retval = CTL_RETVAL_COMPLETE; 4713 } else { 4714 /* 4715 * XXX KDM now we need to continue data movement. Some 4716 * options: 4717 * - call ctl_scsiio() again? We don't do this for data 4718 * writes, because for those at least we know ahead of 4719 * time where the write will go and how long it is. For 4720 * config writes, though, that information is largely 4721 * contained within the write itself, thus we need to 4722 * parse out the data again. 4723 * 4724 * - Call some other function once the data is in? 4725 */ 4726 4727 /* 4728 * XXX KDM call ctl_scsiio() again for now, and check flag 4729 * bits to see whether we're allocated or not. 4730 */ 4731 retval = ctl_scsiio(&io->scsiio); 4732 } 4733 return (retval); 4734} 4735 4736/* 4737 * This gets called by a backend driver when it is done with a 4738 * data_submit method. 4739 */ 4740void 4741ctl_data_submit_done(union ctl_io *io) 4742{ 4743 /* 4744 * If the IO_CONT flag is set, we need to call the supplied 4745 * function to continue processing the I/O, instead of completing 4746 * the I/O just yet. 4747 * 4748 * If there is an error, though, we don't want to keep processing. 4749 * Instead, just send status back to the initiator. 4750 */ 4751 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4752 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4753 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4754 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4755 io->scsiio.io_cont(io); 4756 return; 4757 } 4758 ctl_done(io); 4759} 4760 4761/* 4762 * This gets called by a backend driver when it is done with a 4763 * configuration write. 4764 */ 4765void 4766ctl_config_write_done(union ctl_io *io) 4767{ 4768 uint8_t *buf; 4769 4770 /* 4771 * If the IO_CONT flag is set, we need to call the supplied 4772 * function to continue processing the I/O, instead of completing 4773 * the I/O just yet. 4774 * 4775 * If there is an error, though, we don't want to keep processing. 4776 * Instead, just send status back to the initiator. 4777 */ 4778 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4779 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4780 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4781 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4782 io->scsiio.io_cont(io); 4783 return; 4784 } 4785 /* 4786 * Since a configuration write can be done for commands that actually 4787 * have data allocated, like write buffer, and commands that have 4788 * no data, like start/stop unit, we need to check here. 4789 */ 4790 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4791 buf = io->scsiio.kern_data_ptr; 4792 else 4793 buf = NULL; 4794 ctl_done(io); 4795 if (buf) 4796 free(buf, M_CTL); 4797} 4798 4799void 4800ctl_config_read_done(union ctl_io *io) 4801{ 4802 uint8_t *buf; 4803 4804 /* 4805 * If there is some error -- we are done, skip data transfer. 4806 */ 4807 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 4808 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4809 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 4810 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4811 buf = io->scsiio.kern_data_ptr; 4812 else 4813 buf = NULL; 4814 ctl_done(io); 4815 if (buf) 4816 free(buf, M_CTL); 4817 return; 4818 } 4819 4820 /* 4821 * If the IO_CONT flag is set, we need to call the supplied 4822 * function to continue processing the I/O, instead of completing 4823 * the I/O just yet. 4824 */ 4825 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 4826 io->scsiio.io_cont(io); 4827 return; 4828 } 4829 4830 ctl_datamove(io); 4831} 4832 4833/* 4834 * SCSI release command. 4835 */ 4836int 4837ctl_scsi_release(struct ctl_scsiio *ctsio) 4838{ 4839 int length, longid, thirdparty_id, resv_id; 4840 struct ctl_lun *lun; 4841 uint32_t residx; 4842 4843 length = 0; 4844 resv_id = 0; 4845 4846 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 4847 4848 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 4849 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 4850 4851 switch (ctsio->cdb[0]) { 4852 case RELEASE_10: { 4853 struct scsi_release_10 *cdb; 4854 4855 cdb = (struct scsi_release_10 *)ctsio->cdb; 4856 4857 if (cdb->byte2 & SR10_LONGID) 4858 longid = 1; 4859 else 4860 thirdparty_id = cdb->thirdparty_id; 4861 4862 resv_id = cdb->resv_id; 4863 length = scsi_2btoul(cdb->length); 4864 break; 4865 } 4866 } 4867 4868 4869 /* 4870 * XXX KDM right now, we only support LUN reservation. We don't 4871 * support 3rd party reservations, or extent reservations, which 4872 * might actually need the parameter list. If we've gotten this 4873 * far, we've got a LUN reservation. Anything else got kicked out 4874 * above. So, according to SPC, ignore the length. 4875 */ 4876 length = 0; 4877 4878 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 4879 && (length > 0)) { 4880 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 4881 ctsio->kern_data_len = length; 4882 ctsio->kern_total_len = length; 4883 ctsio->kern_data_resid = 0; 4884 ctsio->kern_rel_offset = 0; 4885 ctsio->kern_sg_entries = 0; 4886 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 4887 ctsio->be_move_done = ctl_config_move_done; 4888 ctl_datamove((union ctl_io *)ctsio); 4889 4890 return (CTL_RETVAL_COMPLETE); 4891 } 4892 4893 if (length > 0) 4894 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 4895 4896 mtx_lock(&lun->lun_lock); 4897 4898 /* 4899 * According to SPC, it is not an error for an intiator to attempt 4900 * to release a reservation on a LUN that isn't reserved, or that 4901 * is reserved by another initiator. The reservation can only be 4902 * released, though, by the initiator who made it or by one of 4903 * several reset type events. 4904 */ 4905 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 4906 lun->flags &= ~CTL_LUN_RESERVED; 4907 4908 mtx_unlock(&lun->lun_lock); 4909 4910 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 4911 free(ctsio->kern_data_ptr, M_CTL); 4912 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 4913 } 4914 4915 ctl_set_success(ctsio); 4916 ctl_done((union ctl_io *)ctsio); 4917 return (CTL_RETVAL_COMPLETE); 4918} 4919 4920int 4921ctl_scsi_reserve(struct ctl_scsiio *ctsio) 4922{ 4923 int extent, thirdparty, longid; 4924 int resv_id, length; 4925 uint64_t thirdparty_id; 4926 struct ctl_lun *lun; 4927 uint32_t residx; 4928 4929 extent = 0; 4930 thirdparty = 0; 4931 longid = 0; 4932 resv_id = 0; 4933 length = 0; 4934 thirdparty_id = 0; 4935 4936 CTL_DEBUG_PRINT(("ctl_reserve\n")); 4937 4938 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 4939 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 4940 4941 switch (ctsio->cdb[0]) { 4942 case RESERVE_10: { 4943 struct scsi_reserve_10 *cdb; 4944 4945 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 4946 4947 if (cdb->byte2 & SR10_LONGID) 4948 longid = 1; 4949 else 4950 thirdparty_id = cdb->thirdparty_id; 4951 4952 resv_id = cdb->resv_id; 4953 length = scsi_2btoul(cdb->length); 4954 break; 4955 } 4956 } 4957 4958 /* 4959 * XXX KDM right now, we only support LUN reservation. We don't 4960 * support 3rd party reservations, or extent reservations, which 4961 * might actually need the parameter list. If we've gotten this 4962 * far, we've got a LUN reservation. Anything else got kicked out 4963 * above. So, according to SPC, ignore the length. 4964 */ 4965 length = 0; 4966 4967 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 4968 && (length > 0)) { 4969 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 4970 ctsio->kern_data_len = length; 4971 ctsio->kern_total_len = length; 4972 ctsio->kern_data_resid = 0; 4973 ctsio->kern_rel_offset = 0; 4974 ctsio->kern_sg_entries = 0; 4975 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 4976 ctsio->be_move_done = ctl_config_move_done; 4977 ctl_datamove((union ctl_io *)ctsio); 4978 4979 return (CTL_RETVAL_COMPLETE); 4980 } 4981 4982 if (length > 0) 4983 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 4984 4985 mtx_lock(&lun->lun_lock); 4986 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 4987 ctl_set_reservation_conflict(ctsio); 4988 goto bailout; 4989 } 4990 4991 lun->flags |= CTL_LUN_RESERVED; 4992 lun->res_idx = residx; 4993 4994 ctl_set_success(ctsio); 4995 4996bailout: 4997 mtx_unlock(&lun->lun_lock); 4998 4999 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5000 free(ctsio->kern_data_ptr, M_CTL); 5001 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5002 } 5003 5004 ctl_done((union ctl_io *)ctsio); 5005 return (CTL_RETVAL_COMPLETE); 5006} 5007 5008int 5009ctl_start_stop(struct ctl_scsiio *ctsio) 5010{ 5011 struct scsi_start_stop_unit *cdb; 5012 struct ctl_lun *lun; 5013 int retval; 5014 5015 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5016 5017 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5018 retval = 0; 5019 5020 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5021 5022 /* 5023 * XXX KDM 5024 * We don't support the immediate bit on a stop unit. In order to 5025 * do that, we would need to code up a way to know that a stop is 5026 * pending, and hold off any new commands until it completes, one 5027 * way or another. Then we could accept or reject those commands 5028 * depending on its status. We would almost need to do the reverse 5029 * of what we do below for an immediate start -- return the copy of 5030 * the ctl_io to the FETD with status to send to the host (and to 5031 * free the copy!) and then free the original I/O once the stop 5032 * actually completes. That way, the OOA queue mechanism can work 5033 * to block commands that shouldn't proceed. Another alternative 5034 * would be to put the copy in the queue in place of the original, 5035 * and return the original back to the caller. That could be 5036 * slightly safer.. 5037 */ 5038 if ((cdb->byte2 & SSS_IMMED) 5039 && ((cdb->how & SSS_START) == 0)) { 5040 ctl_set_invalid_field(ctsio, 5041 /*sks_valid*/ 1, 5042 /*command*/ 1, 5043 /*field*/ 1, 5044 /*bit_valid*/ 1, 5045 /*bit*/ 0); 5046 ctl_done((union ctl_io *)ctsio); 5047 return (CTL_RETVAL_COMPLETE); 5048 } 5049 5050 if ((lun->flags & CTL_LUN_PR_RESERVED) 5051 && ((cdb->how & SSS_START)==0)) { 5052 uint32_t residx; 5053 5054 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5055 if (ctl_get_prkey(lun, residx) == 0 5056 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5057 5058 ctl_set_reservation_conflict(ctsio); 5059 ctl_done((union ctl_io *)ctsio); 5060 return (CTL_RETVAL_COMPLETE); 5061 } 5062 } 5063 5064 /* 5065 * If there is no backend on this device, we can't start or stop 5066 * it. In theory we shouldn't get any start/stop commands in the 5067 * first place at this level if the LUN doesn't have a backend. 5068 * That should get stopped by the command decode code. 5069 */ 5070 if (lun->backend == NULL) { 5071 ctl_set_invalid_opcode(ctsio); 5072 ctl_done((union ctl_io *)ctsio); 5073 return (CTL_RETVAL_COMPLETE); 5074 } 5075 5076 /* 5077 * XXX KDM Copan-specific offline behavior. 5078 * Figure out a reasonable way to port this? 5079 */ 5080#ifdef NEEDTOPORT 5081 mtx_lock(&lun->lun_lock); 5082 5083 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5084 && (lun->flags & CTL_LUN_OFFLINE)) { 5085 /* 5086 * If the LUN is offline, and the on/offline bit isn't set, 5087 * reject the start or stop. Otherwise, let it through. 5088 */ 5089 mtx_unlock(&lun->lun_lock); 5090 ctl_set_lun_not_ready(ctsio); 5091 ctl_done((union ctl_io *)ctsio); 5092 } else { 5093 mtx_unlock(&lun->lun_lock); 5094#endif /* NEEDTOPORT */ 5095 /* 5096 * This could be a start or a stop when we're online, 5097 * or a stop/offline or start/online. A start or stop when 5098 * we're offline is covered in the case above. 5099 */ 5100 /* 5101 * In the non-immediate case, we send the request to 5102 * the backend and return status to the user when 5103 * it is done. 5104 * 5105 * In the immediate case, we allocate a new ctl_io 5106 * to hold a copy of the request, and send that to 5107 * the backend. We then set good status on the 5108 * user's request and return it immediately. 5109 */ 5110 if (cdb->byte2 & SSS_IMMED) { 5111 union ctl_io *new_io; 5112 5113 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5114 ctl_copy_io((union ctl_io *)ctsio, new_io); 5115 retval = lun->backend->config_write(new_io); 5116 ctl_set_success(ctsio); 5117 ctl_done((union ctl_io *)ctsio); 5118 } else { 5119 retval = lun->backend->config_write( 5120 (union ctl_io *)ctsio); 5121 } 5122#ifdef NEEDTOPORT 5123 } 5124#endif 5125 return (retval); 5126} 5127 5128/* 5129 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5130 * we don't really do anything with the LBA and length fields if the user 5131 * passes them in. Instead we'll just flush out the cache for the entire 5132 * LUN. 5133 */ 5134int 5135ctl_sync_cache(struct ctl_scsiio *ctsio) 5136{ 5137 struct ctl_lun *lun; 5138 struct ctl_softc *softc; 5139 struct ctl_lba_len_flags *lbalen; 5140 uint64_t starting_lba; 5141 uint32_t block_count; 5142 int retval; 5143 uint8_t byte2; 5144 5145 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5146 5147 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5148 softc = lun->ctl_softc; 5149 retval = 0; 5150 5151 switch (ctsio->cdb[0]) { 5152 case SYNCHRONIZE_CACHE: { 5153 struct scsi_sync_cache *cdb; 5154 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5155 5156 starting_lba = scsi_4btoul(cdb->begin_lba); 5157 block_count = scsi_2btoul(cdb->lb_count); 5158 byte2 = cdb->byte2; 5159 break; 5160 } 5161 case SYNCHRONIZE_CACHE_16: { 5162 struct scsi_sync_cache_16 *cdb; 5163 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5164 5165 starting_lba = scsi_8btou64(cdb->begin_lba); 5166 block_count = scsi_4btoul(cdb->lb_count); 5167 byte2 = cdb->byte2; 5168 break; 5169 } 5170 default: 5171 ctl_set_invalid_opcode(ctsio); 5172 ctl_done((union ctl_io *)ctsio); 5173 goto bailout; 5174 break; /* NOTREACHED */ 5175 } 5176 5177 /* 5178 * We check the LBA and length, but don't do anything with them. 5179 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5180 * get flushed. This check will just help satisfy anyone who wants 5181 * to see an error for an out of range LBA. 5182 */ 5183 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5184 ctl_set_lba_out_of_range(ctsio); 5185 ctl_done((union ctl_io *)ctsio); 5186 goto bailout; 5187 } 5188 5189 /* 5190 * If this LUN has no backend, we can't flush the cache anyway. 5191 */ 5192 if (lun->backend == NULL) { 5193 ctl_set_invalid_opcode(ctsio); 5194 ctl_done((union ctl_io *)ctsio); 5195 goto bailout; 5196 } 5197 5198 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5199 lbalen->lba = starting_lba; 5200 lbalen->len = block_count; 5201 lbalen->flags = byte2; 5202 5203 /* 5204 * Check to see whether we're configured to send the SYNCHRONIZE 5205 * CACHE command directly to the back end. 5206 */ 5207 mtx_lock(&lun->lun_lock); 5208 if ((softc->flags & CTL_FLAG_REAL_SYNC) 5209 && (++(lun->sync_count) >= lun->sync_interval)) { 5210 lun->sync_count = 0; 5211 mtx_unlock(&lun->lun_lock); 5212 retval = lun->backend->config_write((union ctl_io *)ctsio); 5213 } else { 5214 mtx_unlock(&lun->lun_lock); 5215 ctl_set_success(ctsio); 5216 ctl_done((union ctl_io *)ctsio); 5217 } 5218 5219bailout: 5220 5221 return (retval); 5222} 5223 5224int 5225ctl_format(struct ctl_scsiio *ctsio) 5226{ 5227 struct scsi_format *cdb; 5228 struct ctl_lun *lun; 5229 int length, defect_list_len; 5230 5231 CTL_DEBUG_PRINT(("ctl_format\n")); 5232 5233 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5234 5235 cdb = (struct scsi_format *)ctsio->cdb; 5236 5237 length = 0; 5238 if (cdb->byte2 & SF_FMTDATA) { 5239 if (cdb->byte2 & SF_LONGLIST) 5240 length = sizeof(struct scsi_format_header_long); 5241 else 5242 length = sizeof(struct scsi_format_header_short); 5243 } 5244 5245 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5246 && (length > 0)) { 5247 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5248 ctsio->kern_data_len = length; 5249 ctsio->kern_total_len = length; 5250 ctsio->kern_data_resid = 0; 5251 ctsio->kern_rel_offset = 0; 5252 ctsio->kern_sg_entries = 0; 5253 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5254 ctsio->be_move_done = ctl_config_move_done; 5255 ctl_datamove((union ctl_io *)ctsio); 5256 5257 return (CTL_RETVAL_COMPLETE); 5258 } 5259 5260 defect_list_len = 0; 5261 5262 if (cdb->byte2 & SF_FMTDATA) { 5263 if (cdb->byte2 & SF_LONGLIST) { 5264 struct scsi_format_header_long *header; 5265 5266 header = (struct scsi_format_header_long *) 5267 ctsio->kern_data_ptr; 5268 5269 defect_list_len = scsi_4btoul(header->defect_list_len); 5270 if (defect_list_len != 0) { 5271 ctl_set_invalid_field(ctsio, 5272 /*sks_valid*/ 1, 5273 /*command*/ 0, 5274 /*field*/ 2, 5275 /*bit_valid*/ 0, 5276 /*bit*/ 0); 5277 goto bailout; 5278 } 5279 } else { 5280 struct scsi_format_header_short *header; 5281 5282 header = (struct scsi_format_header_short *) 5283 ctsio->kern_data_ptr; 5284 5285 defect_list_len = scsi_2btoul(header->defect_list_len); 5286 if (defect_list_len != 0) { 5287 ctl_set_invalid_field(ctsio, 5288 /*sks_valid*/ 1, 5289 /*command*/ 0, 5290 /*field*/ 2, 5291 /*bit_valid*/ 0, 5292 /*bit*/ 0); 5293 goto bailout; 5294 } 5295 } 5296 } 5297 5298 /* 5299 * The format command will clear out the "Medium format corrupted" 5300 * status if set by the configuration code. That status is really 5301 * just a way to notify the host that we have lost the media, and 5302 * get them to issue a command that will basically make them think 5303 * they're blowing away the media. 5304 */ 5305 mtx_lock(&lun->lun_lock); 5306 lun->flags &= ~CTL_LUN_INOPERABLE; 5307 mtx_unlock(&lun->lun_lock); 5308 5309 ctl_set_success(ctsio); 5310bailout: 5311 5312 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5313 free(ctsio->kern_data_ptr, M_CTL); 5314 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5315 } 5316 5317 ctl_done((union ctl_io *)ctsio); 5318 return (CTL_RETVAL_COMPLETE); 5319} 5320 5321int 5322ctl_read_buffer(struct ctl_scsiio *ctsio) 5323{ 5324 struct scsi_read_buffer *cdb; 5325 struct ctl_lun *lun; 5326 int buffer_offset, len; 5327 static uint8_t descr[4]; 5328 static uint8_t echo_descr[4] = { 0 }; 5329 5330 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5331 5332 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5333 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5334 5335 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5336 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5337 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5338 ctl_set_invalid_field(ctsio, 5339 /*sks_valid*/ 1, 5340 /*command*/ 1, 5341 /*field*/ 1, 5342 /*bit_valid*/ 1, 5343 /*bit*/ 4); 5344 ctl_done((union ctl_io *)ctsio); 5345 return (CTL_RETVAL_COMPLETE); 5346 } 5347 5348 len = scsi_3btoul(cdb->length); 5349 buffer_offset = scsi_3btoul(cdb->offset); 5350 5351 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5352 ctl_set_invalid_field(ctsio, 5353 /*sks_valid*/ 1, 5354 /*command*/ 1, 5355 /*field*/ 6, 5356 /*bit_valid*/ 0, 5357 /*bit*/ 0); 5358 ctl_done((union ctl_io *)ctsio); 5359 return (CTL_RETVAL_COMPLETE); 5360 } 5361 5362 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5363 descr[0] = 0; 5364 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5365 ctsio->kern_data_ptr = descr; 5366 len = min(len, sizeof(descr)); 5367 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5368 ctsio->kern_data_ptr = echo_descr; 5369 len = min(len, sizeof(echo_descr)); 5370 } else { 5371 if (lun->write_buffer == NULL) { 5372 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5373 M_CTL, M_WAITOK); 5374 } 5375 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5376 } 5377 ctsio->kern_data_len = len; 5378 ctsio->kern_total_len = len; 5379 ctsio->kern_data_resid = 0; 5380 ctsio->kern_rel_offset = 0; 5381 ctsio->kern_sg_entries = 0; 5382 ctl_set_success(ctsio); 5383 ctsio->be_move_done = ctl_config_move_done; 5384 ctl_datamove((union ctl_io *)ctsio); 5385 return (CTL_RETVAL_COMPLETE); 5386} 5387 5388int 5389ctl_write_buffer(struct ctl_scsiio *ctsio) 5390{ 5391 struct scsi_write_buffer *cdb; 5392 struct ctl_lun *lun; 5393 int buffer_offset, len; 5394 5395 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5396 5397 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5398 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5399 5400 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5401 ctl_set_invalid_field(ctsio, 5402 /*sks_valid*/ 1, 5403 /*command*/ 1, 5404 /*field*/ 1, 5405 /*bit_valid*/ 1, 5406 /*bit*/ 4); 5407 ctl_done((union ctl_io *)ctsio); 5408 return (CTL_RETVAL_COMPLETE); 5409 } 5410 5411 len = scsi_3btoul(cdb->length); 5412 buffer_offset = scsi_3btoul(cdb->offset); 5413 5414 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5415 ctl_set_invalid_field(ctsio, 5416 /*sks_valid*/ 1, 5417 /*command*/ 1, 5418 /*field*/ 6, 5419 /*bit_valid*/ 0, 5420 /*bit*/ 0); 5421 ctl_done((union ctl_io *)ctsio); 5422 return (CTL_RETVAL_COMPLETE); 5423 } 5424 5425 /* 5426 * If we've got a kernel request that hasn't been malloced yet, 5427 * malloc it and tell the caller the data buffer is here. 5428 */ 5429 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5430 if (lun->write_buffer == NULL) { 5431 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5432 M_CTL, M_WAITOK); 5433 } 5434 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5435 ctsio->kern_data_len = len; 5436 ctsio->kern_total_len = len; 5437 ctsio->kern_data_resid = 0; 5438 ctsio->kern_rel_offset = 0; 5439 ctsio->kern_sg_entries = 0; 5440 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5441 ctsio->be_move_done = ctl_config_move_done; 5442 ctl_datamove((union ctl_io *)ctsio); 5443 5444 return (CTL_RETVAL_COMPLETE); 5445 } 5446 5447 ctl_set_success(ctsio); 5448 ctl_done((union ctl_io *)ctsio); 5449 return (CTL_RETVAL_COMPLETE); 5450} 5451 5452int 5453ctl_write_same(struct ctl_scsiio *ctsio) 5454{ 5455 struct ctl_lun *lun; 5456 struct ctl_lba_len_flags *lbalen; 5457 uint64_t lba; 5458 uint32_t num_blocks; 5459 int len, retval; 5460 uint8_t byte2; 5461 5462 retval = CTL_RETVAL_COMPLETE; 5463 5464 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5465 5466 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5467 5468 switch (ctsio->cdb[0]) { 5469 case WRITE_SAME_10: { 5470 struct scsi_write_same_10 *cdb; 5471 5472 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5473 5474 lba = scsi_4btoul(cdb->addr); 5475 num_blocks = scsi_2btoul(cdb->length); 5476 byte2 = cdb->byte2; 5477 break; 5478 } 5479 case WRITE_SAME_16: { 5480 struct scsi_write_same_16 *cdb; 5481 5482 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5483 5484 lba = scsi_8btou64(cdb->addr); 5485 num_blocks = scsi_4btoul(cdb->length); 5486 byte2 = cdb->byte2; 5487 break; 5488 } 5489 default: 5490 /* 5491 * We got a command we don't support. This shouldn't 5492 * happen, commands should be filtered out above us. 5493 */ 5494 ctl_set_invalid_opcode(ctsio); 5495 ctl_done((union ctl_io *)ctsio); 5496 5497 return (CTL_RETVAL_COMPLETE); 5498 break; /* NOTREACHED */ 5499 } 5500 5501 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5502 if ((byte2 & SWS_UNMAP) == 0 && 5503 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5504 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5505 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5506 ctl_done((union ctl_io *)ctsio); 5507 return (CTL_RETVAL_COMPLETE); 5508 } 5509 5510 /* 5511 * The first check is to make sure we're in bounds, the second 5512 * check is to catch wrap-around problems. If the lba + num blocks 5513 * is less than the lba, then we've wrapped around and the block 5514 * range is invalid anyway. 5515 */ 5516 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5517 || ((lba + num_blocks) < lba)) { 5518 ctl_set_lba_out_of_range(ctsio); 5519 ctl_done((union ctl_io *)ctsio); 5520 return (CTL_RETVAL_COMPLETE); 5521 } 5522 5523 /* Zero number of blocks means "to the last logical block" */ 5524 if (num_blocks == 0) { 5525 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5526 ctl_set_invalid_field(ctsio, 5527 /*sks_valid*/ 0, 5528 /*command*/ 1, 5529 /*field*/ 0, 5530 /*bit_valid*/ 0, 5531 /*bit*/ 0); 5532 ctl_done((union ctl_io *)ctsio); 5533 return (CTL_RETVAL_COMPLETE); 5534 } 5535 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5536 } 5537 5538 len = lun->be_lun->blocksize; 5539 5540 /* 5541 * If we've got a kernel request that hasn't been malloced yet, 5542 * malloc it and tell the caller the data buffer is here. 5543 */ 5544 if ((byte2 & SWS_NDOB) == 0 && 5545 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5546 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5547 ctsio->kern_data_len = len; 5548 ctsio->kern_total_len = len; 5549 ctsio->kern_data_resid = 0; 5550 ctsio->kern_rel_offset = 0; 5551 ctsio->kern_sg_entries = 0; 5552 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5553 ctsio->be_move_done = ctl_config_move_done; 5554 ctl_datamove((union ctl_io *)ctsio); 5555 5556 return (CTL_RETVAL_COMPLETE); 5557 } 5558 5559 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5560 lbalen->lba = lba; 5561 lbalen->len = num_blocks; 5562 lbalen->flags = byte2; 5563 retval = lun->backend->config_write((union ctl_io *)ctsio); 5564 5565 return (retval); 5566} 5567 5568int 5569ctl_unmap(struct ctl_scsiio *ctsio) 5570{ 5571 struct ctl_lun *lun; 5572 struct scsi_unmap *cdb; 5573 struct ctl_ptr_len_flags *ptrlen; 5574 struct scsi_unmap_header *hdr; 5575 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5576 uint64_t lba; 5577 uint32_t num_blocks; 5578 int len, retval; 5579 uint8_t byte2; 5580 5581 retval = CTL_RETVAL_COMPLETE; 5582 5583 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5584 5585 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5586 cdb = (struct scsi_unmap *)ctsio->cdb; 5587 5588 len = scsi_2btoul(cdb->length); 5589 byte2 = cdb->byte2; 5590 5591 /* 5592 * If we've got a kernel request that hasn't been malloced yet, 5593 * malloc it and tell the caller the data buffer is here. 5594 */ 5595 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5596 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5597 ctsio->kern_data_len = len; 5598 ctsio->kern_total_len = len; 5599 ctsio->kern_data_resid = 0; 5600 ctsio->kern_rel_offset = 0; 5601 ctsio->kern_sg_entries = 0; 5602 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5603 ctsio->be_move_done = ctl_config_move_done; 5604 ctl_datamove((union ctl_io *)ctsio); 5605 5606 return (CTL_RETVAL_COMPLETE); 5607 } 5608 5609 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5610 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5611 if (len < sizeof (*hdr) || 5612 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5613 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5614 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5615 ctl_set_invalid_field(ctsio, 5616 /*sks_valid*/ 0, 5617 /*command*/ 0, 5618 /*field*/ 0, 5619 /*bit_valid*/ 0, 5620 /*bit*/ 0); 5621 goto done; 5622 } 5623 len = scsi_2btoul(hdr->desc_length); 5624 buf = (struct scsi_unmap_desc *)(hdr + 1); 5625 end = buf + len / sizeof(*buf); 5626 5627 endnz = buf; 5628 for (range = buf; range < end; range++) { 5629 lba = scsi_8btou64(range->lba); 5630 num_blocks = scsi_4btoul(range->length); 5631 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5632 || ((lba + num_blocks) < lba)) { 5633 ctl_set_lba_out_of_range(ctsio); 5634 ctl_done((union ctl_io *)ctsio); 5635 return (CTL_RETVAL_COMPLETE); 5636 } 5637 if (num_blocks != 0) 5638 endnz = range + 1; 5639 } 5640 5641 /* 5642 * Block backend can not handle zero last range. 5643 * Filter it out and return if there is nothing left. 5644 */ 5645 len = (uint8_t *)endnz - (uint8_t *)buf; 5646 if (len == 0) { 5647 ctl_set_success(ctsio); 5648 goto done; 5649 } 5650 5651 mtx_lock(&lun->lun_lock); 5652 ptrlen = (struct ctl_ptr_len_flags *) 5653 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5654 ptrlen->ptr = (void *)buf; 5655 ptrlen->len = len; 5656 ptrlen->flags = byte2; 5657 ctl_check_blocked(lun); 5658 mtx_unlock(&lun->lun_lock); 5659 5660 retval = lun->backend->config_write((union ctl_io *)ctsio); 5661 return (retval); 5662 5663done: 5664 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5665 free(ctsio->kern_data_ptr, M_CTL); 5666 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5667 } 5668 ctl_done((union ctl_io *)ctsio); 5669 return (CTL_RETVAL_COMPLETE); 5670} 5671 5672/* 5673 * Note that this function currently doesn't actually do anything inside 5674 * CTL to enforce things if the DQue bit is turned on. 5675 * 5676 * Also note that this function can't be used in the default case, because 5677 * the DQue bit isn't set in the changeable mask for the control mode page 5678 * anyway. This is just here as an example for how to implement a page 5679 * handler, and a placeholder in case we want to allow the user to turn 5680 * tagged queueing on and off. 5681 * 5682 * The D_SENSE bit handling is functional, however, and will turn 5683 * descriptor sense on and off for a given LUN. 5684 */ 5685int 5686ctl_control_page_handler(struct ctl_scsiio *ctsio, 5687 struct ctl_page_index *page_index, uint8_t *page_ptr) 5688{ 5689 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5690 struct ctl_lun *lun; 5691 int set_ua; 5692 uint32_t initidx; 5693 5694 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5695 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5696 set_ua = 0; 5697 5698 user_cp = (struct scsi_control_page *)page_ptr; 5699 current_cp = (struct scsi_control_page *) 5700 (page_index->page_data + (page_index->page_len * 5701 CTL_PAGE_CURRENT)); 5702 saved_cp = (struct scsi_control_page *) 5703 (page_index->page_data + (page_index->page_len * 5704 CTL_PAGE_SAVED)); 5705 5706 mtx_lock(&lun->lun_lock); 5707 if (((current_cp->rlec & SCP_DSENSE) == 0) 5708 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5709 /* 5710 * Descriptor sense is currently turned off and the user 5711 * wants to turn it on. 5712 */ 5713 current_cp->rlec |= SCP_DSENSE; 5714 saved_cp->rlec |= SCP_DSENSE; 5715 lun->flags |= CTL_LUN_SENSE_DESC; 5716 set_ua = 1; 5717 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 5718 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 5719 /* 5720 * Descriptor sense is currently turned on, and the user 5721 * wants to turn it off. 5722 */ 5723 current_cp->rlec &= ~SCP_DSENSE; 5724 saved_cp->rlec &= ~SCP_DSENSE; 5725 lun->flags &= ~CTL_LUN_SENSE_DESC; 5726 set_ua = 1; 5727 } 5728 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 5729 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 5730 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5731 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5732 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5733 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5734 set_ua = 1; 5735 } 5736 if ((current_cp->eca_and_aen & SCP_SWP) != 5737 (user_cp->eca_and_aen & SCP_SWP)) { 5738 current_cp->eca_and_aen &= ~SCP_SWP; 5739 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5740 saved_cp->eca_and_aen &= ~SCP_SWP; 5741 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5742 set_ua = 1; 5743 } 5744 if (set_ua != 0) 5745 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5746 mtx_unlock(&lun->lun_lock); 5747 5748 return (0); 5749} 5750 5751int 5752ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 5753 struct ctl_page_index *page_index, uint8_t *page_ptr) 5754{ 5755 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 5756 struct ctl_lun *lun; 5757 int set_ua; 5758 uint32_t initidx; 5759 5760 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5761 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5762 set_ua = 0; 5763 5764 user_cp = (struct scsi_caching_page *)page_ptr; 5765 current_cp = (struct scsi_caching_page *) 5766 (page_index->page_data + (page_index->page_len * 5767 CTL_PAGE_CURRENT)); 5768 saved_cp = (struct scsi_caching_page *) 5769 (page_index->page_data + (page_index->page_len * 5770 CTL_PAGE_SAVED)); 5771 5772 mtx_lock(&lun->lun_lock); 5773 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 5774 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 5775 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5776 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5777 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5778 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5779 set_ua = 1; 5780 } 5781 if (set_ua != 0) 5782 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5783 mtx_unlock(&lun->lun_lock); 5784 5785 return (0); 5786} 5787 5788int 5789ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 5790 struct ctl_page_index *page_index, 5791 uint8_t *page_ptr) 5792{ 5793 uint8_t *c; 5794 int i; 5795 5796 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 5797 ctl_time_io_secs = 5798 (c[0] << 8) | 5799 (c[1] << 0) | 5800 0; 5801 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 5802 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 5803 printf("page data:"); 5804 for (i=0; i<8; i++) 5805 printf(" %.2x",page_ptr[i]); 5806 printf("\n"); 5807 return (0); 5808} 5809 5810int 5811ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 5812 struct ctl_page_index *page_index, 5813 int pc) 5814{ 5815 struct copan_debugconf_subpage *page; 5816 5817 page = (struct copan_debugconf_subpage *)page_index->page_data + 5818 (page_index->page_len * pc); 5819 5820 switch (pc) { 5821 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 5822 case SMS_PAGE_CTRL_DEFAULT >> 6: 5823 case SMS_PAGE_CTRL_SAVED >> 6: 5824 /* 5825 * We don't update the changable or default bits for this page. 5826 */ 5827 break; 5828 case SMS_PAGE_CTRL_CURRENT >> 6: 5829 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 5830 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 5831 break; 5832 default: 5833#ifdef NEEDTOPORT 5834 EPRINT(0, "Invalid PC %d!!", pc); 5835#endif /* NEEDTOPORT */ 5836 break; 5837 } 5838 return (0); 5839} 5840 5841 5842static int 5843ctl_do_mode_select(union ctl_io *io) 5844{ 5845 struct scsi_mode_page_header *page_header; 5846 struct ctl_page_index *page_index; 5847 struct ctl_scsiio *ctsio; 5848 int control_dev, page_len; 5849 int page_len_offset, page_len_size; 5850 union ctl_modepage_info *modepage_info; 5851 struct ctl_lun *lun; 5852 int *len_left, *len_used; 5853 int retval, i; 5854 5855 ctsio = &io->scsiio; 5856 page_index = NULL; 5857 page_len = 0; 5858 retval = CTL_RETVAL_COMPLETE; 5859 5860 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5861 5862 if (lun->be_lun->lun_type != T_DIRECT) 5863 control_dev = 1; 5864 else 5865 control_dev = 0; 5866 5867 modepage_info = (union ctl_modepage_info *) 5868 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5869 len_left = &modepage_info->header.len_left; 5870 len_used = &modepage_info->header.len_used; 5871 5872do_next_page: 5873 5874 page_header = (struct scsi_mode_page_header *) 5875 (ctsio->kern_data_ptr + *len_used); 5876 5877 if (*len_left == 0) { 5878 free(ctsio->kern_data_ptr, M_CTL); 5879 ctl_set_success(ctsio); 5880 ctl_done((union ctl_io *)ctsio); 5881 return (CTL_RETVAL_COMPLETE); 5882 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5883 5884 free(ctsio->kern_data_ptr, M_CTL); 5885 ctl_set_param_len_error(ctsio); 5886 ctl_done((union ctl_io *)ctsio); 5887 return (CTL_RETVAL_COMPLETE); 5888 5889 } else if ((page_header->page_code & SMPH_SPF) 5890 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5891 5892 free(ctsio->kern_data_ptr, M_CTL); 5893 ctl_set_param_len_error(ctsio); 5894 ctl_done((union ctl_io *)ctsio); 5895 return (CTL_RETVAL_COMPLETE); 5896 } 5897 5898 5899 /* 5900 * XXX KDM should we do something with the block descriptor? 5901 */ 5902 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5903 5904 if ((control_dev != 0) 5905 && (lun->mode_pages.index[i].page_flags & 5906 CTL_PAGE_FLAG_DISK_ONLY)) 5907 continue; 5908 5909 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5910 (page_header->page_code & SMPH_PC_MASK)) 5911 continue; 5912 5913 /* 5914 * If neither page has a subpage code, then we've got a 5915 * match. 5916 */ 5917 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 5918 && ((page_header->page_code & SMPH_SPF) == 0)) { 5919 page_index = &lun->mode_pages.index[i]; 5920 page_len = page_header->page_length; 5921 break; 5922 } 5923 5924 /* 5925 * If both pages have subpages, then the subpage numbers 5926 * have to match. 5927 */ 5928 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 5929 && (page_header->page_code & SMPH_SPF)) { 5930 struct scsi_mode_page_header_sp *sph; 5931 5932 sph = (struct scsi_mode_page_header_sp *)page_header; 5933 5934 if (lun->mode_pages.index[i].subpage == 5935 sph->subpage) { 5936 page_index = &lun->mode_pages.index[i]; 5937 page_len = scsi_2btoul(sph->page_length); 5938 break; 5939 } 5940 } 5941 } 5942 5943 /* 5944 * If we couldn't find the page, or if we don't have a mode select 5945 * handler for it, send back an error to the user. 5946 */ 5947 if ((page_index == NULL) 5948 || (page_index->select_handler == NULL)) { 5949 ctl_set_invalid_field(ctsio, 5950 /*sks_valid*/ 1, 5951 /*command*/ 0, 5952 /*field*/ *len_used, 5953 /*bit_valid*/ 0, 5954 /*bit*/ 0); 5955 free(ctsio->kern_data_ptr, M_CTL); 5956 ctl_done((union ctl_io *)ctsio); 5957 return (CTL_RETVAL_COMPLETE); 5958 } 5959 5960 if (page_index->page_code & SMPH_SPF) { 5961 page_len_offset = 2; 5962 page_len_size = 2; 5963 } else { 5964 page_len_size = 1; 5965 page_len_offset = 1; 5966 } 5967 5968 /* 5969 * If the length the initiator gives us isn't the one we specify in 5970 * the mode page header, or if they didn't specify enough data in 5971 * the CDB to avoid truncating this page, kick out the request. 5972 */ 5973 if ((page_len != (page_index->page_len - page_len_offset - 5974 page_len_size)) 5975 || (*len_left < page_index->page_len)) { 5976 5977 5978 ctl_set_invalid_field(ctsio, 5979 /*sks_valid*/ 1, 5980 /*command*/ 0, 5981 /*field*/ *len_used + page_len_offset, 5982 /*bit_valid*/ 0, 5983 /*bit*/ 0); 5984 free(ctsio->kern_data_ptr, M_CTL); 5985 ctl_done((union ctl_io *)ctsio); 5986 return (CTL_RETVAL_COMPLETE); 5987 } 5988 5989 /* 5990 * Run through the mode page, checking to make sure that the bits 5991 * the user changed are actually legal for him to change. 5992 */ 5993 for (i = 0; i < page_index->page_len; i++) { 5994 uint8_t *user_byte, *change_mask, *current_byte; 5995 int bad_bit; 5996 int j; 5997 5998 user_byte = (uint8_t *)page_header + i; 5999 change_mask = page_index->page_data + 6000 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6001 current_byte = page_index->page_data + 6002 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6003 6004 /* 6005 * Check to see whether the user set any bits in this byte 6006 * that he is not allowed to set. 6007 */ 6008 if ((*user_byte & ~(*change_mask)) == 6009 (*current_byte & ~(*change_mask))) 6010 continue; 6011 6012 /* 6013 * Go through bit by bit to determine which one is illegal. 6014 */ 6015 bad_bit = 0; 6016 for (j = 7; j >= 0; j--) { 6017 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6018 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6019 bad_bit = i; 6020 break; 6021 } 6022 } 6023 ctl_set_invalid_field(ctsio, 6024 /*sks_valid*/ 1, 6025 /*command*/ 0, 6026 /*field*/ *len_used + i, 6027 /*bit_valid*/ 1, 6028 /*bit*/ bad_bit); 6029 free(ctsio->kern_data_ptr, M_CTL); 6030 ctl_done((union ctl_io *)ctsio); 6031 return (CTL_RETVAL_COMPLETE); 6032 } 6033 6034 /* 6035 * Decrement these before we call the page handler, since we may 6036 * end up getting called back one way or another before the handler 6037 * returns to this context. 6038 */ 6039 *len_left -= page_index->page_len; 6040 *len_used += page_index->page_len; 6041 6042 retval = page_index->select_handler(ctsio, page_index, 6043 (uint8_t *)page_header); 6044 6045 /* 6046 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6047 * wait until this queued command completes to finish processing 6048 * the mode page. If it returns anything other than 6049 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6050 * already set the sense information, freed the data pointer, and 6051 * completed the io for us. 6052 */ 6053 if (retval != CTL_RETVAL_COMPLETE) 6054 goto bailout_no_done; 6055 6056 /* 6057 * If the initiator sent us more than one page, parse the next one. 6058 */ 6059 if (*len_left > 0) 6060 goto do_next_page; 6061 6062 ctl_set_success(ctsio); 6063 free(ctsio->kern_data_ptr, M_CTL); 6064 ctl_done((union ctl_io *)ctsio); 6065 6066bailout_no_done: 6067 6068 return (CTL_RETVAL_COMPLETE); 6069 6070} 6071 6072int 6073ctl_mode_select(struct ctl_scsiio *ctsio) 6074{ 6075 int param_len, pf, sp; 6076 int header_size, bd_len; 6077 int len_left, len_used; 6078 struct ctl_page_index *page_index; 6079 struct ctl_lun *lun; 6080 int control_dev, page_len; 6081 union ctl_modepage_info *modepage_info; 6082 int retval; 6083 6084 pf = 0; 6085 sp = 0; 6086 page_len = 0; 6087 len_used = 0; 6088 len_left = 0; 6089 retval = 0; 6090 bd_len = 0; 6091 page_index = NULL; 6092 6093 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6094 6095 if (lun->be_lun->lun_type != T_DIRECT) 6096 control_dev = 1; 6097 else 6098 control_dev = 0; 6099 6100 switch (ctsio->cdb[0]) { 6101 case MODE_SELECT_6: { 6102 struct scsi_mode_select_6 *cdb; 6103 6104 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6105 6106 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6107 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6108 6109 param_len = cdb->length; 6110 header_size = sizeof(struct scsi_mode_header_6); 6111 break; 6112 } 6113 case MODE_SELECT_10: { 6114 struct scsi_mode_select_10 *cdb; 6115 6116 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6117 6118 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6119 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6120 6121 param_len = scsi_2btoul(cdb->length); 6122 header_size = sizeof(struct scsi_mode_header_10); 6123 break; 6124 } 6125 default: 6126 ctl_set_invalid_opcode(ctsio); 6127 ctl_done((union ctl_io *)ctsio); 6128 return (CTL_RETVAL_COMPLETE); 6129 break; /* NOTREACHED */ 6130 } 6131 6132 /* 6133 * From SPC-3: 6134 * "A parameter list length of zero indicates that the Data-Out Buffer 6135 * shall be empty. This condition shall not be considered as an error." 6136 */ 6137 if (param_len == 0) { 6138 ctl_set_success(ctsio); 6139 ctl_done((union ctl_io *)ctsio); 6140 return (CTL_RETVAL_COMPLETE); 6141 } 6142 6143 /* 6144 * Since we'll hit this the first time through, prior to 6145 * allocation, we don't need to free a data buffer here. 6146 */ 6147 if (param_len < header_size) { 6148 ctl_set_param_len_error(ctsio); 6149 ctl_done((union ctl_io *)ctsio); 6150 return (CTL_RETVAL_COMPLETE); 6151 } 6152 6153 /* 6154 * Allocate the data buffer and grab the user's data. In theory, 6155 * we shouldn't have to sanity check the parameter list length here 6156 * because the maximum size is 64K. We should be able to malloc 6157 * that much without too many problems. 6158 */ 6159 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6160 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6161 ctsio->kern_data_len = param_len; 6162 ctsio->kern_total_len = param_len; 6163 ctsio->kern_data_resid = 0; 6164 ctsio->kern_rel_offset = 0; 6165 ctsio->kern_sg_entries = 0; 6166 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6167 ctsio->be_move_done = ctl_config_move_done; 6168 ctl_datamove((union ctl_io *)ctsio); 6169 6170 return (CTL_RETVAL_COMPLETE); 6171 } 6172 6173 switch (ctsio->cdb[0]) { 6174 case MODE_SELECT_6: { 6175 struct scsi_mode_header_6 *mh6; 6176 6177 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6178 bd_len = mh6->blk_desc_len; 6179 break; 6180 } 6181 case MODE_SELECT_10: { 6182 struct scsi_mode_header_10 *mh10; 6183 6184 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6185 bd_len = scsi_2btoul(mh10->blk_desc_len); 6186 break; 6187 } 6188 default: 6189 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6190 break; 6191 } 6192 6193 if (param_len < (header_size + bd_len)) { 6194 free(ctsio->kern_data_ptr, M_CTL); 6195 ctl_set_param_len_error(ctsio); 6196 ctl_done((union ctl_io *)ctsio); 6197 return (CTL_RETVAL_COMPLETE); 6198 } 6199 6200 /* 6201 * Set the IO_CONT flag, so that if this I/O gets passed to 6202 * ctl_config_write_done(), it'll get passed back to 6203 * ctl_do_mode_select() for further processing, or completion if 6204 * we're all done. 6205 */ 6206 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6207 ctsio->io_cont = ctl_do_mode_select; 6208 6209 modepage_info = (union ctl_modepage_info *) 6210 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6211 6212 memset(modepage_info, 0, sizeof(*modepage_info)); 6213 6214 len_left = param_len - header_size - bd_len; 6215 len_used = header_size + bd_len; 6216 6217 modepage_info->header.len_left = len_left; 6218 modepage_info->header.len_used = len_used; 6219 6220 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6221} 6222 6223int 6224ctl_mode_sense(struct ctl_scsiio *ctsio) 6225{ 6226 struct ctl_lun *lun; 6227 int pc, page_code, dbd, llba, subpage; 6228 int alloc_len, page_len, header_len, total_len; 6229 struct scsi_mode_block_descr *block_desc; 6230 struct ctl_page_index *page_index; 6231 int control_dev; 6232 6233 dbd = 0; 6234 llba = 0; 6235 block_desc = NULL; 6236 page_index = NULL; 6237 6238 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6239 6240 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6241 6242 if (lun->be_lun->lun_type != T_DIRECT) 6243 control_dev = 1; 6244 else 6245 control_dev = 0; 6246 6247 switch (ctsio->cdb[0]) { 6248 case MODE_SENSE_6: { 6249 struct scsi_mode_sense_6 *cdb; 6250 6251 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6252 6253 header_len = sizeof(struct scsi_mode_hdr_6); 6254 if (cdb->byte2 & SMS_DBD) 6255 dbd = 1; 6256 else 6257 header_len += sizeof(struct scsi_mode_block_descr); 6258 6259 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6260 page_code = cdb->page & SMS_PAGE_CODE; 6261 subpage = cdb->subpage; 6262 alloc_len = cdb->length; 6263 break; 6264 } 6265 case MODE_SENSE_10: { 6266 struct scsi_mode_sense_10 *cdb; 6267 6268 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6269 6270 header_len = sizeof(struct scsi_mode_hdr_10); 6271 6272 if (cdb->byte2 & SMS_DBD) 6273 dbd = 1; 6274 else 6275 header_len += sizeof(struct scsi_mode_block_descr); 6276 if (cdb->byte2 & SMS10_LLBAA) 6277 llba = 1; 6278 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6279 page_code = cdb->page & SMS_PAGE_CODE; 6280 subpage = cdb->subpage; 6281 alloc_len = scsi_2btoul(cdb->length); 6282 break; 6283 } 6284 default: 6285 ctl_set_invalid_opcode(ctsio); 6286 ctl_done((union ctl_io *)ctsio); 6287 return (CTL_RETVAL_COMPLETE); 6288 break; /* NOTREACHED */ 6289 } 6290 6291 /* 6292 * We have to make a first pass through to calculate the size of 6293 * the pages that match the user's query. Then we allocate enough 6294 * memory to hold it, and actually copy the data into the buffer. 6295 */ 6296 switch (page_code) { 6297 case SMS_ALL_PAGES_PAGE: { 6298 int i; 6299 6300 page_len = 0; 6301 6302 /* 6303 * At the moment, values other than 0 and 0xff here are 6304 * reserved according to SPC-3. 6305 */ 6306 if ((subpage != SMS_SUBPAGE_PAGE_0) 6307 && (subpage != SMS_SUBPAGE_ALL)) { 6308 ctl_set_invalid_field(ctsio, 6309 /*sks_valid*/ 1, 6310 /*command*/ 1, 6311 /*field*/ 3, 6312 /*bit_valid*/ 0, 6313 /*bit*/ 0); 6314 ctl_done((union ctl_io *)ctsio); 6315 return (CTL_RETVAL_COMPLETE); 6316 } 6317 6318 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6319 if ((control_dev != 0) 6320 && (lun->mode_pages.index[i].page_flags & 6321 CTL_PAGE_FLAG_DISK_ONLY)) 6322 continue; 6323 6324 /* 6325 * We don't use this subpage if the user didn't 6326 * request all subpages. 6327 */ 6328 if ((lun->mode_pages.index[i].subpage != 0) 6329 && (subpage == SMS_SUBPAGE_PAGE_0)) 6330 continue; 6331 6332#if 0 6333 printf("found page %#x len %d\n", 6334 lun->mode_pages.index[i].page_code & 6335 SMPH_PC_MASK, 6336 lun->mode_pages.index[i].page_len); 6337#endif 6338 page_len += lun->mode_pages.index[i].page_len; 6339 } 6340 break; 6341 } 6342 default: { 6343 int i; 6344 6345 page_len = 0; 6346 6347 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6348 /* Look for the right page code */ 6349 if ((lun->mode_pages.index[i].page_code & 6350 SMPH_PC_MASK) != page_code) 6351 continue; 6352 6353 /* Look for the right subpage or the subpage wildcard*/ 6354 if ((lun->mode_pages.index[i].subpage != subpage) 6355 && (subpage != SMS_SUBPAGE_ALL)) 6356 continue; 6357 6358 /* Make sure the page is supported for this dev type */ 6359 if ((control_dev != 0) 6360 && (lun->mode_pages.index[i].page_flags & 6361 CTL_PAGE_FLAG_DISK_ONLY)) 6362 continue; 6363 6364#if 0 6365 printf("found page %#x len %d\n", 6366 lun->mode_pages.index[i].page_code & 6367 SMPH_PC_MASK, 6368 lun->mode_pages.index[i].page_len); 6369#endif 6370 6371 page_len += lun->mode_pages.index[i].page_len; 6372 } 6373 6374 if (page_len == 0) { 6375 ctl_set_invalid_field(ctsio, 6376 /*sks_valid*/ 1, 6377 /*command*/ 1, 6378 /*field*/ 2, 6379 /*bit_valid*/ 1, 6380 /*bit*/ 5); 6381 ctl_done((union ctl_io *)ctsio); 6382 return (CTL_RETVAL_COMPLETE); 6383 } 6384 break; 6385 } 6386 } 6387 6388 total_len = header_len + page_len; 6389#if 0 6390 printf("header_len = %d, page_len = %d, total_len = %d\n", 6391 header_len, page_len, total_len); 6392#endif 6393 6394 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6395 ctsio->kern_sg_entries = 0; 6396 ctsio->kern_data_resid = 0; 6397 ctsio->kern_rel_offset = 0; 6398 if (total_len < alloc_len) { 6399 ctsio->residual = alloc_len - total_len; 6400 ctsio->kern_data_len = total_len; 6401 ctsio->kern_total_len = total_len; 6402 } else { 6403 ctsio->residual = 0; 6404 ctsio->kern_data_len = alloc_len; 6405 ctsio->kern_total_len = alloc_len; 6406 } 6407 6408 switch (ctsio->cdb[0]) { 6409 case MODE_SENSE_6: { 6410 struct scsi_mode_hdr_6 *header; 6411 6412 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6413 6414 header->datalen = MIN(total_len - 1, 254); 6415 if (control_dev == 0) { 6416 header->dev_specific = 0x10; /* DPOFUA */ 6417 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6418 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6419 .eca_and_aen & SCP_SWP) != 0) 6420 header->dev_specific |= 0x80; /* WP */ 6421 } 6422 if (dbd) 6423 header->block_descr_len = 0; 6424 else 6425 header->block_descr_len = 6426 sizeof(struct scsi_mode_block_descr); 6427 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6428 break; 6429 } 6430 case MODE_SENSE_10: { 6431 struct scsi_mode_hdr_10 *header; 6432 int datalen; 6433 6434 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6435 6436 datalen = MIN(total_len - 2, 65533); 6437 scsi_ulto2b(datalen, header->datalen); 6438 if (control_dev == 0) { 6439 header->dev_specific = 0x10; /* DPOFUA */ 6440 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6441 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6442 .eca_and_aen & SCP_SWP) != 0) 6443 header->dev_specific |= 0x80; /* WP */ 6444 } 6445 if (dbd) 6446 scsi_ulto2b(0, header->block_descr_len); 6447 else 6448 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6449 header->block_descr_len); 6450 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6451 break; 6452 } 6453 default: 6454 panic("invalid CDB type %#x", ctsio->cdb[0]); 6455 break; /* NOTREACHED */ 6456 } 6457 6458 /* 6459 * If we've got a disk, use its blocksize in the block 6460 * descriptor. Otherwise, just set it to 0. 6461 */ 6462 if (dbd == 0) { 6463 if (control_dev == 0) 6464 scsi_ulto3b(lun->be_lun->blocksize, 6465 block_desc->block_len); 6466 else 6467 scsi_ulto3b(0, block_desc->block_len); 6468 } 6469 6470 switch (page_code) { 6471 case SMS_ALL_PAGES_PAGE: { 6472 int i, data_used; 6473 6474 data_used = header_len; 6475 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6476 struct ctl_page_index *page_index; 6477 6478 page_index = &lun->mode_pages.index[i]; 6479 6480 if ((control_dev != 0) 6481 && (page_index->page_flags & 6482 CTL_PAGE_FLAG_DISK_ONLY)) 6483 continue; 6484 6485 /* 6486 * We don't use this subpage if the user didn't 6487 * request all subpages. We already checked (above) 6488 * to make sure the user only specified a subpage 6489 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6490 */ 6491 if ((page_index->subpage != 0) 6492 && (subpage == SMS_SUBPAGE_PAGE_0)) 6493 continue; 6494 6495 /* 6496 * Call the handler, if it exists, to update the 6497 * page to the latest values. 6498 */ 6499 if (page_index->sense_handler != NULL) 6500 page_index->sense_handler(ctsio, page_index,pc); 6501 6502 memcpy(ctsio->kern_data_ptr + data_used, 6503 page_index->page_data + 6504 (page_index->page_len * pc), 6505 page_index->page_len); 6506 data_used += page_index->page_len; 6507 } 6508 break; 6509 } 6510 default: { 6511 int i, data_used; 6512 6513 data_used = header_len; 6514 6515 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6516 struct ctl_page_index *page_index; 6517 6518 page_index = &lun->mode_pages.index[i]; 6519 6520 /* Look for the right page code */ 6521 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6522 continue; 6523 6524 /* Look for the right subpage or the subpage wildcard*/ 6525 if ((page_index->subpage != subpage) 6526 && (subpage != SMS_SUBPAGE_ALL)) 6527 continue; 6528 6529 /* Make sure the page is supported for this dev type */ 6530 if ((control_dev != 0) 6531 && (page_index->page_flags & 6532 CTL_PAGE_FLAG_DISK_ONLY)) 6533 continue; 6534 6535 /* 6536 * Call the handler, if it exists, to update the 6537 * page to the latest values. 6538 */ 6539 if (page_index->sense_handler != NULL) 6540 page_index->sense_handler(ctsio, page_index,pc); 6541 6542 memcpy(ctsio->kern_data_ptr + data_used, 6543 page_index->page_data + 6544 (page_index->page_len * pc), 6545 page_index->page_len); 6546 data_used += page_index->page_len; 6547 } 6548 break; 6549 } 6550 } 6551 6552 ctl_set_success(ctsio); 6553 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6554 ctsio->be_move_done = ctl_config_move_done; 6555 ctl_datamove((union ctl_io *)ctsio); 6556 return (CTL_RETVAL_COMPLETE); 6557} 6558 6559int 6560ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6561 struct ctl_page_index *page_index, 6562 int pc) 6563{ 6564 struct ctl_lun *lun; 6565 struct scsi_log_param_header *phdr; 6566 uint8_t *data; 6567 uint64_t val; 6568 6569 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6570 data = page_index->page_data; 6571 6572 if (lun->backend->lun_attr != NULL && 6573 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6574 != UINT64_MAX) { 6575 phdr = (struct scsi_log_param_header *)data; 6576 scsi_ulto2b(0x0001, phdr->param_code); 6577 phdr->param_control = SLP_LBIN | SLP_LP; 6578 phdr->param_len = 8; 6579 data = (uint8_t *)(phdr + 1); 6580 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6581 data[4] = 0x02; /* per-pool */ 6582 data += phdr->param_len; 6583 } 6584 6585 if (lun->backend->lun_attr != NULL && 6586 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6587 != UINT64_MAX) { 6588 phdr = (struct scsi_log_param_header *)data; 6589 scsi_ulto2b(0x0002, phdr->param_code); 6590 phdr->param_control = SLP_LBIN | SLP_LP; 6591 phdr->param_len = 8; 6592 data = (uint8_t *)(phdr + 1); 6593 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6594 data[4] = 0x01; /* per-LUN */ 6595 data += phdr->param_len; 6596 } 6597 6598 if (lun->backend->lun_attr != NULL && 6599 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6600 != UINT64_MAX) { 6601 phdr = (struct scsi_log_param_header *)data; 6602 scsi_ulto2b(0x00f1, phdr->param_code); 6603 phdr->param_control = SLP_LBIN | SLP_LP; 6604 phdr->param_len = 8; 6605 data = (uint8_t *)(phdr + 1); 6606 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6607 data[4] = 0x02; /* per-pool */ 6608 data += phdr->param_len; 6609 } 6610 6611 if (lun->backend->lun_attr != NULL && 6612 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6613 != UINT64_MAX) { 6614 phdr = (struct scsi_log_param_header *)data; 6615 scsi_ulto2b(0x00f2, phdr->param_code); 6616 phdr->param_control = SLP_LBIN | SLP_LP; 6617 phdr->param_len = 8; 6618 data = (uint8_t *)(phdr + 1); 6619 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6620 data[4] = 0x02; /* per-pool */ 6621 data += phdr->param_len; 6622 } 6623 6624 page_index->page_len = data - page_index->page_data; 6625 return (0); 6626} 6627 6628int 6629ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6630 struct ctl_page_index *page_index, 6631 int pc) 6632{ 6633 struct ctl_lun *lun; 6634 struct stat_page *data; 6635 uint64_t rn, wn, rb, wb; 6636 struct bintime rt, wt; 6637 int i; 6638 6639 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6640 data = (struct stat_page *)page_index->page_data; 6641 6642 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6643 data->sap.hdr.param_control = SLP_LBIN; 6644 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6645 sizeof(struct scsi_log_param_header); 6646 rn = wn = rb = wb = 0; 6647 bintime_clear(&rt); 6648 bintime_clear(&wt); 6649 for (i = 0; i < CTL_MAX_PORTS; i++) { 6650 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6651 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6652 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6653 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6654 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6655 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6656 } 6657 scsi_u64to8b(rn, data->sap.read_num); 6658 scsi_u64to8b(wn, data->sap.write_num); 6659 if (lun->stats.blocksize > 0) { 6660 scsi_u64to8b(wb / lun->stats.blocksize, 6661 data->sap.recvieved_lba); 6662 scsi_u64to8b(rb / lun->stats.blocksize, 6663 data->sap.transmitted_lba); 6664 } 6665 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6666 data->sap.read_int); 6667 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6668 data->sap.write_int); 6669 scsi_u64to8b(0, data->sap.weighted_num); 6670 scsi_u64to8b(0, data->sap.weighted_int); 6671 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6672 data->it.hdr.param_control = SLP_LBIN; 6673 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6674 sizeof(struct scsi_log_param_header); 6675#ifdef CTL_TIME_IO 6676 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6677#endif 6678 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6679 data->it.hdr.param_control = SLP_LBIN; 6680 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6681 sizeof(struct scsi_log_param_header); 6682 scsi_ulto4b(3, data->ti.exponent); 6683 scsi_ulto4b(1, data->ti.integer); 6684 6685 page_index->page_len = sizeof(*data); 6686 return (0); 6687} 6688 6689int 6690ctl_log_sense(struct ctl_scsiio *ctsio) 6691{ 6692 struct ctl_lun *lun; 6693 int i, pc, page_code, subpage; 6694 int alloc_len, total_len; 6695 struct ctl_page_index *page_index; 6696 struct scsi_log_sense *cdb; 6697 struct scsi_log_header *header; 6698 6699 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6700 6701 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6702 cdb = (struct scsi_log_sense *)ctsio->cdb; 6703 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6704 page_code = cdb->page & SLS_PAGE_CODE; 6705 subpage = cdb->subpage; 6706 alloc_len = scsi_2btoul(cdb->length); 6707 6708 page_index = NULL; 6709 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6710 page_index = &lun->log_pages.index[i]; 6711 6712 /* Look for the right page code */ 6713 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6714 continue; 6715 6716 /* Look for the right subpage or the subpage wildcard*/ 6717 if (page_index->subpage != subpage) 6718 continue; 6719 6720 break; 6721 } 6722 if (i >= CTL_NUM_LOG_PAGES) { 6723 ctl_set_invalid_field(ctsio, 6724 /*sks_valid*/ 1, 6725 /*command*/ 1, 6726 /*field*/ 2, 6727 /*bit_valid*/ 0, 6728 /*bit*/ 0); 6729 ctl_done((union ctl_io *)ctsio); 6730 return (CTL_RETVAL_COMPLETE); 6731 } 6732 6733 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6734 6735 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6736 ctsio->kern_sg_entries = 0; 6737 ctsio->kern_data_resid = 0; 6738 ctsio->kern_rel_offset = 0; 6739 if (total_len < alloc_len) { 6740 ctsio->residual = alloc_len - total_len; 6741 ctsio->kern_data_len = total_len; 6742 ctsio->kern_total_len = total_len; 6743 } else { 6744 ctsio->residual = 0; 6745 ctsio->kern_data_len = alloc_len; 6746 ctsio->kern_total_len = alloc_len; 6747 } 6748 6749 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6750 header->page = page_index->page_code; 6751 if (page_index->subpage) { 6752 header->page |= SL_SPF; 6753 header->subpage = page_index->subpage; 6754 } 6755 scsi_ulto2b(page_index->page_len, header->datalen); 6756 6757 /* 6758 * Call the handler, if it exists, to update the 6759 * page to the latest values. 6760 */ 6761 if (page_index->sense_handler != NULL) 6762 page_index->sense_handler(ctsio, page_index, pc); 6763 6764 memcpy(header + 1, page_index->page_data, page_index->page_len); 6765 6766 ctl_set_success(ctsio); 6767 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6768 ctsio->be_move_done = ctl_config_move_done; 6769 ctl_datamove((union ctl_io *)ctsio); 6770 return (CTL_RETVAL_COMPLETE); 6771} 6772 6773int 6774ctl_read_capacity(struct ctl_scsiio *ctsio) 6775{ 6776 struct scsi_read_capacity *cdb; 6777 struct scsi_read_capacity_data *data; 6778 struct ctl_lun *lun; 6779 uint32_t lba; 6780 6781 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6782 6783 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6784 6785 lba = scsi_4btoul(cdb->addr); 6786 if (((cdb->pmi & SRC_PMI) == 0) 6787 && (lba != 0)) { 6788 ctl_set_invalid_field(/*ctsio*/ ctsio, 6789 /*sks_valid*/ 1, 6790 /*command*/ 1, 6791 /*field*/ 2, 6792 /*bit_valid*/ 0, 6793 /*bit*/ 0); 6794 ctl_done((union ctl_io *)ctsio); 6795 return (CTL_RETVAL_COMPLETE); 6796 } 6797 6798 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6799 6800 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6801 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6802 ctsio->residual = 0; 6803 ctsio->kern_data_len = sizeof(*data); 6804 ctsio->kern_total_len = sizeof(*data); 6805 ctsio->kern_data_resid = 0; 6806 ctsio->kern_rel_offset = 0; 6807 ctsio->kern_sg_entries = 0; 6808 6809 /* 6810 * If the maximum LBA is greater than 0xfffffffe, the user must 6811 * issue a SERVICE ACTION IN (16) command, with the read capacity 6812 * serivce action set. 6813 */ 6814 if (lun->be_lun->maxlba > 0xfffffffe) 6815 scsi_ulto4b(0xffffffff, data->addr); 6816 else 6817 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6818 6819 /* 6820 * XXX KDM this may not be 512 bytes... 6821 */ 6822 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6823 6824 ctl_set_success(ctsio); 6825 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6826 ctsio->be_move_done = ctl_config_move_done; 6827 ctl_datamove((union ctl_io *)ctsio); 6828 return (CTL_RETVAL_COMPLETE); 6829} 6830 6831int 6832ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6833{ 6834 struct scsi_read_capacity_16 *cdb; 6835 struct scsi_read_capacity_data_long *data; 6836 struct ctl_lun *lun; 6837 uint64_t lba; 6838 uint32_t alloc_len; 6839 6840 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6841 6842 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6843 6844 alloc_len = scsi_4btoul(cdb->alloc_len); 6845 lba = scsi_8btou64(cdb->addr); 6846 6847 if ((cdb->reladr & SRC16_PMI) 6848 && (lba != 0)) { 6849 ctl_set_invalid_field(/*ctsio*/ ctsio, 6850 /*sks_valid*/ 1, 6851 /*command*/ 1, 6852 /*field*/ 2, 6853 /*bit_valid*/ 0, 6854 /*bit*/ 0); 6855 ctl_done((union ctl_io *)ctsio); 6856 return (CTL_RETVAL_COMPLETE); 6857 } 6858 6859 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6860 6861 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6862 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6863 6864 if (sizeof(*data) < alloc_len) { 6865 ctsio->residual = alloc_len - sizeof(*data); 6866 ctsio->kern_data_len = sizeof(*data); 6867 ctsio->kern_total_len = sizeof(*data); 6868 } else { 6869 ctsio->residual = 0; 6870 ctsio->kern_data_len = alloc_len; 6871 ctsio->kern_total_len = alloc_len; 6872 } 6873 ctsio->kern_data_resid = 0; 6874 ctsio->kern_rel_offset = 0; 6875 ctsio->kern_sg_entries = 0; 6876 6877 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6878 /* XXX KDM this may not be 512 bytes... */ 6879 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6880 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6881 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6882 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6883 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6884 6885 ctl_set_success(ctsio); 6886 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6887 ctsio->be_move_done = ctl_config_move_done; 6888 ctl_datamove((union ctl_io *)ctsio); 6889 return (CTL_RETVAL_COMPLETE); 6890} 6891 6892int 6893ctl_get_lba_status(struct ctl_scsiio *ctsio) 6894{ 6895 struct scsi_get_lba_status *cdb; 6896 struct scsi_get_lba_status_data *data; 6897 struct ctl_lun *lun; 6898 struct ctl_lba_len_flags *lbalen; 6899 uint64_t lba; 6900 uint32_t alloc_len, total_len; 6901 int retval; 6902 6903 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 6904 6905 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6906 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 6907 lba = scsi_8btou64(cdb->addr); 6908 alloc_len = scsi_4btoul(cdb->alloc_len); 6909 6910 if (lba > lun->be_lun->maxlba) { 6911 ctl_set_lba_out_of_range(ctsio); 6912 ctl_done((union ctl_io *)ctsio); 6913 return (CTL_RETVAL_COMPLETE); 6914 } 6915 6916 total_len = sizeof(*data) + sizeof(data->descr[0]); 6917 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6918 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 6919 6920 if (total_len < alloc_len) { 6921 ctsio->residual = alloc_len - total_len; 6922 ctsio->kern_data_len = total_len; 6923 ctsio->kern_total_len = total_len; 6924 } else { 6925 ctsio->residual = 0; 6926 ctsio->kern_data_len = alloc_len; 6927 ctsio->kern_total_len = alloc_len; 6928 } 6929 ctsio->kern_data_resid = 0; 6930 ctsio->kern_rel_offset = 0; 6931 ctsio->kern_sg_entries = 0; 6932 6933 /* Fill dummy data in case backend can't tell anything. */ 6934 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 6935 scsi_u64to8b(lba, data->descr[0].addr); 6936 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 6937 data->descr[0].length); 6938 data->descr[0].status = 0; /* Mapped or unknown. */ 6939 6940 ctl_set_success(ctsio); 6941 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6942 ctsio->be_move_done = ctl_config_move_done; 6943 6944 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6945 lbalen->lba = lba; 6946 lbalen->len = total_len; 6947 lbalen->flags = 0; 6948 retval = lun->backend->config_read((union ctl_io *)ctsio); 6949 return (CTL_RETVAL_COMPLETE); 6950} 6951 6952int 6953ctl_read_defect(struct ctl_scsiio *ctsio) 6954{ 6955 struct scsi_read_defect_data_10 *ccb10; 6956 struct scsi_read_defect_data_12 *ccb12; 6957 struct scsi_read_defect_data_hdr_10 *data10; 6958 struct scsi_read_defect_data_hdr_12 *data12; 6959 uint32_t alloc_len, data_len; 6960 uint8_t format; 6961 6962 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 6963 6964 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 6965 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 6966 format = ccb10->format; 6967 alloc_len = scsi_2btoul(ccb10->alloc_length); 6968 data_len = sizeof(*data10); 6969 } else { 6970 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 6971 format = ccb12->format; 6972 alloc_len = scsi_4btoul(ccb12->alloc_length); 6973 data_len = sizeof(*data12); 6974 } 6975 if (alloc_len == 0) { 6976 ctl_set_success(ctsio); 6977 ctl_done((union ctl_io *)ctsio); 6978 return (CTL_RETVAL_COMPLETE); 6979 } 6980 6981 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 6982 if (data_len < alloc_len) { 6983 ctsio->residual = alloc_len - data_len; 6984 ctsio->kern_data_len = data_len; 6985 ctsio->kern_total_len = data_len; 6986 } else { 6987 ctsio->residual = 0; 6988 ctsio->kern_data_len = alloc_len; 6989 ctsio->kern_total_len = alloc_len; 6990 } 6991 ctsio->kern_data_resid = 0; 6992 ctsio->kern_rel_offset = 0; 6993 ctsio->kern_sg_entries = 0; 6994 6995 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 6996 data10 = (struct scsi_read_defect_data_hdr_10 *) 6997 ctsio->kern_data_ptr; 6998 data10->format = format; 6999 scsi_ulto2b(0, data10->length); 7000 } else { 7001 data12 = (struct scsi_read_defect_data_hdr_12 *) 7002 ctsio->kern_data_ptr; 7003 data12->format = format; 7004 scsi_ulto2b(0, data12->generation); 7005 scsi_ulto4b(0, data12->length); 7006 } 7007 7008 ctl_set_success(ctsio); 7009 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7010 ctsio->be_move_done = ctl_config_move_done; 7011 ctl_datamove((union ctl_io *)ctsio); 7012 return (CTL_RETVAL_COMPLETE); 7013} 7014 7015int 7016ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7017{ 7018 struct scsi_maintenance_in *cdb; 7019 int retval; 7020 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os; 7021 int num_target_port_groups, num_target_ports; 7022 struct ctl_lun *lun; 7023 struct ctl_softc *softc; 7024 struct ctl_port *port; 7025 struct scsi_target_group_data *rtg_ptr; 7026 struct scsi_target_group_data_extended *rtg_ext_ptr; 7027 struct scsi_target_port_group_descriptor *tpg_desc; 7028 7029 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7030 7031 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7032 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7033 softc = lun->ctl_softc; 7034 7035 retval = CTL_RETVAL_COMPLETE; 7036 7037 switch (cdb->byte2 & STG_PDF_MASK) { 7038 case STG_PDF_LENGTH: 7039 ext = 0; 7040 break; 7041 case STG_PDF_EXTENDED: 7042 ext = 1; 7043 break; 7044 default: 7045 ctl_set_invalid_field(/*ctsio*/ ctsio, 7046 /*sks_valid*/ 1, 7047 /*command*/ 1, 7048 /*field*/ 2, 7049 /*bit_valid*/ 1, 7050 /*bit*/ 5); 7051 ctl_done((union ctl_io *)ctsio); 7052 return(retval); 7053 } 7054 7055 if (softc->is_single) 7056 num_target_port_groups = 1; 7057 else 7058 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7059 num_target_ports = 0; 7060 mtx_lock(&softc->ctl_lock); 7061 STAILQ_FOREACH(port, &softc->port_list, links) { 7062 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7063 continue; 7064 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7065 continue; 7066 num_target_ports++; 7067 } 7068 mtx_unlock(&softc->ctl_lock); 7069 7070 if (ext) 7071 total_len = sizeof(struct scsi_target_group_data_extended); 7072 else 7073 total_len = sizeof(struct scsi_target_group_data); 7074 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7075 num_target_port_groups + 7076 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7077 7078 alloc_len = scsi_4btoul(cdb->length); 7079 7080 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7081 7082 ctsio->kern_sg_entries = 0; 7083 7084 if (total_len < alloc_len) { 7085 ctsio->residual = alloc_len - total_len; 7086 ctsio->kern_data_len = total_len; 7087 ctsio->kern_total_len = total_len; 7088 } else { 7089 ctsio->residual = 0; 7090 ctsio->kern_data_len = alloc_len; 7091 ctsio->kern_total_len = alloc_len; 7092 } 7093 ctsio->kern_data_resid = 0; 7094 ctsio->kern_rel_offset = 0; 7095 7096 if (ext) { 7097 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7098 ctsio->kern_data_ptr; 7099 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7100 rtg_ext_ptr->format_type = 0x10; 7101 rtg_ext_ptr->implicit_transition_time = 0; 7102 tpg_desc = &rtg_ext_ptr->groups[0]; 7103 } else { 7104 rtg_ptr = (struct scsi_target_group_data *) 7105 ctsio->kern_data_ptr; 7106 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7107 tpg_desc = &rtg_ptr->groups[0]; 7108 } 7109 7110 mtx_lock(&softc->ctl_lock); 7111 pg = softc->port_min / softc->port_cnt; 7112 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7113 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7114 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7115 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7116 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7117 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7118 else 7119 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7120 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7121 os = gs; 7122 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7123 } else 7124 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7125 for (g = 0; g < num_target_port_groups; g++) { 7126 tpg_desc->pref_state = (g == pg) ? gs : os; 7127 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7128 TPG_U_SUP | TPG_T_SUP; 7129 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7130 tpg_desc->status = TPG_IMPLICIT; 7131 pc = 0; 7132 STAILQ_FOREACH(port, &softc->port_list, links) { 7133 if (port->targ_port < g * softc->port_cnt || 7134 port->targ_port >= (g + 1) * softc->port_cnt) 7135 continue; 7136 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7137 continue; 7138 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7139 continue; 7140 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7141 relative_target_port_identifier); 7142 pc++; 7143 } 7144 tpg_desc->target_port_count = pc; 7145 tpg_desc = (struct scsi_target_port_group_descriptor *) 7146 &tpg_desc->descriptors[pc]; 7147 } 7148 mtx_unlock(&softc->ctl_lock); 7149 7150 ctl_set_success(ctsio); 7151 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7152 ctsio->be_move_done = ctl_config_move_done; 7153 ctl_datamove((union ctl_io *)ctsio); 7154 return(retval); 7155} 7156 7157int 7158ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7159{ 7160 struct ctl_lun *lun; 7161 struct scsi_report_supported_opcodes *cdb; 7162 const struct ctl_cmd_entry *entry, *sentry; 7163 struct scsi_report_supported_opcodes_all *all; 7164 struct scsi_report_supported_opcodes_descr *descr; 7165 struct scsi_report_supported_opcodes_one *one; 7166 int retval; 7167 int alloc_len, total_len; 7168 int opcode, service_action, i, j, num; 7169 7170 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7171 7172 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7173 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7174 7175 retval = CTL_RETVAL_COMPLETE; 7176 7177 opcode = cdb->requested_opcode; 7178 service_action = scsi_2btoul(cdb->requested_service_action); 7179 switch (cdb->options & RSO_OPTIONS_MASK) { 7180 case RSO_OPTIONS_ALL: 7181 num = 0; 7182 for (i = 0; i < 256; i++) { 7183 entry = &ctl_cmd_table[i]; 7184 if (entry->flags & CTL_CMD_FLAG_SA5) { 7185 for (j = 0; j < 32; j++) { 7186 sentry = &((const struct ctl_cmd_entry *) 7187 entry->execute)[j]; 7188 if (ctl_cmd_applicable( 7189 lun->be_lun->lun_type, sentry)) 7190 num++; 7191 } 7192 } else { 7193 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7194 entry)) 7195 num++; 7196 } 7197 } 7198 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7199 num * sizeof(struct scsi_report_supported_opcodes_descr); 7200 break; 7201 case RSO_OPTIONS_OC: 7202 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7203 ctl_set_invalid_field(/*ctsio*/ ctsio, 7204 /*sks_valid*/ 1, 7205 /*command*/ 1, 7206 /*field*/ 2, 7207 /*bit_valid*/ 1, 7208 /*bit*/ 2); 7209 ctl_done((union ctl_io *)ctsio); 7210 return (CTL_RETVAL_COMPLETE); 7211 } 7212 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7213 break; 7214 case RSO_OPTIONS_OC_SA: 7215 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7216 service_action >= 32) { 7217 ctl_set_invalid_field(/*ctsio*/ ctsio, 7218 /*sks_valid*/ 1, 7219 /*command*/ 1, 7220 /*field*/ 2, 7221 /*bit_valid*/ 1, 7222 /*bit*/ 2); 7223 ctl_done((union ctl_io *)ctsio); 7224 return (CTL_RETVAL_COMPLETE); 7225 } 7226 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7227 break; 7228 default: 7229 ctl_set_invalid_field(/*ctsio*/ ctsio, 7230 /*sks_valid*/ 1, 7231 /*command*/ 1, 7232 /*field*/ 2, 7233 /*bit_valid*/ 1, 7234 /*bit*/ 2); 7235 ctl_done((union ctl_io *)ctsio); 7236 return (CTL_RETVAL_COMPLETE); 7237 } 7238 7239 alloc_len = scsi_4btoul(cdb->length); 7240 7241 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7242 7243 ctsio->kern_sg_entries = 0; 7244 7245 if (total_len < alloc_len) { 7246 ctsio->residual = alloc_len - total_len; 7247 ctsio->kern_data_len = total_len; 7248 ctsio->kern_total_len = total_len; 7249 } else { 7250 ctsio->residual = 0; 7251 ctsio->kern_data_len = alloc_len; 7252 ctsio->kern_total_len = alloc_len; 7253 } 7254 ctsio->kern_data_resid = 0; 7255 ctsio->kern_rel_offset = 0; 7256 7257 switch (cdb->options & RSO_OPTIONS_MASK) { 7258 case RSO_OPTIONS_ALL: 7259 all = (struct scsi_report_supported_opcodes_all *) 7260 ctsio->kern_data_ptr; 7261 num = 0; 7262 for (i = 0; i < 256; i++) { 7263 entry = &ctl_cmd_table[i]; 7264 if (entry->flags & CTL_CMD_FLAG_SA5) { 7265 for (j = 0; j < 32; j++) { 7266 sentry = &((const struct ctl_cmd_entry *) 7267 entry->execute)[j]; 7268 if (!ctl_cmd_applicable( 7269 lun->be_lun->lun_type, sentry)) 7270 continue; 7271 descr = &all->descr[num++]; 7272 descr->opcode = i; 7273 scsi_ulto2b(j, descr->service_action); 7274 descr->flags = RSO_SERVACTV; 7275 scsi_ulto2b(sentry->length, 7276 descr->cdb_length); 7277 } 7278 } else { 7279 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7280 entry)) 7281 continue; 7282 descr = &all->descr[num++]; 7283 descr->opcode = i; 7284 scsi_ulto2b(0, descr->service_action); 7285 descr->flags = 0; 7286 scsi_ulto2b(entry->length, descr->cdb_length); 7287 } 7288 } 7289 scsi_ulto4b( 7290 num * sizeof(struct scsi_report_supported_opcodes_descr), 7291 all->length); 7292 break; 7293 case RSO_OPTIONS_OC: 7294 one = (struct scsi_report_supported_opcodes_one *) 7295 ctsio->kern_data_ptr; 7296 entry = &ctl_cmd_table[opcode]; 7297 goto fill_one; 7298 case RSO_OPTIONS_OC_SA: 7299 one = (struct scsi_report_supported_opcodes_one *) 7300 ctsio->kern_data_ptr; 7301 entry = &ctl_cmd_table[opcode]; 7302 entry = &((const struct ctl_cmd_entry *) 7303 entry->execute)[service_action]; 7304fill_one: 7305 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7306 one->support = 3; 7307 scsi_ulto2b(entry->length, one->cdb_length); 7308 one->cdb_usage[0] = opcode; 7309 memcpy(&one->cdb_usage[1], entry->usage, 7310 entry->length - 1); 7311 } else 7312 one->support = 1; 7313 break; 7314 } 7315 7316 ctl_set_success(ctsio); 7317 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7318 ctsio->be_move_done = ctl_config_move_done; 7319 ctl_datamove((union ctl_io *)ctsio); 7320 return(retval); 7321} 7322 7323int 7324ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7325{ 7326 struct scsi_report_supported_tmf *cdb; 7327 struct scsi_report_supported_tmf_data *data; 7328 int retval; 7329 int alloc_len, total_len; 7330 7331 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7332 7333 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7334 7335 retval = CTL_RETVAL_COMPLETE; 7336 7337 total_len = sizeof(struct scsi_report_supported_tmf_data); 7338 alloc_len = scsi_4btoul(cdb->length); 7339 7340 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7341 7342 ctsio->kern_sg_entries = 0; 7343 7344 if (total_len < alloc_len) { 7345 ctsio->residual = alloc_len - total_len; 7346 ctsio->kern_data_len = total_len; 7347 ctsio->kern_total_len = total_len; 7348 } else { 7349 ctsio->residual = 0; 7350 ctsio->kern_data_len = alloc_len; 7351 ctsio->kern_total_len = alloc_len; 7352 } 7353 ctsio->kern_data_resid = 0; 7354 ctsio->kern_rel_offset = 0; 7355 7356 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7357 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7358 data->byte2 |= RST_ITNRS; 7359 7360 ctl_set_success(ctsio); 7361 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7362 ctsio->be_move_done = ctl_config_move_done; 7363 ctl_datamove((union ctl_io *)ctsio); 7364 return (retval); 7365} 7366 7367int 7368ctl_report_timestamp(struct ctl_scsiio *ctsio) 7369{ 7370 struct scsi_report_timestamp *cdb; 7371 struct scsi_report_timestamp_data *data; 7372 struct timeval tv; 7373 int64_t timestamp; 7374 int retval; 7375 int alloc_len, total_len; 7376 7377 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7378 7379 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7380 7381 retval = CTL_RETVAL_COMPLETE; 7382 7383 total_len = sizeof(struct scsi_report_timestamp_data); 7384 alloc_len = scsi_4btoul(cdb->length); 7385 7386 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7387 7388 ctsio->kern_sg_entries = 0; 7389 7390 if (total_len < alloc_len) { 7391 ctsio->residual = alloc_len - total_len; 7392 ctsio->kern_data_len = total_len; 7393 ctsio->kern_total_len = total_len; 7394 } else { 7395 ctsio->residual = 0; 7396 ctsio->kern_data_len = alloc_len; 7397 ctsio->kern_total_len = alloc_len; 7398 } 7399 ctsio->kern_data_resid = 0; 7400 ctsio->kern_rel_offset = 0; 7401 7402 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7403 scsi_ulto2b(sizeof(*data) - 2, data->length); 7404 data->origin = RTS_ORIG_OUTSIDE; 7405 getmicrotime(&tv); 7406 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7407 scsi_ulto4b(timestamp >> 16, data->timestamp); 7408 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7409 7410 ctl_set_success(ctsio); 7411 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7412 ctsio->be_move_done = ctl_config_move_done; 7413 ctl_datamove((union ctl_io *)ctsio); 7414 return (retval); 7415} 7416 7417int 7418ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7419{ 7420 struct scsi_per_res_in *cdb; 7421 int alloc_len, total_len = 0; 7422 /* struct scsi_per_res_in_rsrv in_data; */ 7423 struct ctl_lun *lun; 7424 struct ctl_softc *softc; 7425 uint64_t key; 7426 7427 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7428 7429 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7430 7431 alloc_len = scsi_2btoul(cdb->length); 7432 7433 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7434 softc = lun->ctl_softc; 7435 7436retry: 7437 mtx_lock(&lun->lun_lock); 7438 switch (cdb->action) { 7439 case SPRI_RK: /* read keys */ 7440 total_len = sizeof(struct scsi_per_res_in_keys) + 7441 lun->pr_key_count * 7442 sizeof(struct scsi_per_res_key); 7443 break; 7444 case SPRI_RR: /* read reservation */ 7445 if (lun->flags & CTL_LUN_PR_RESERVED) 7446 total_len = sizeof(struct scsi_per_res_in_rsrv); 7447 else 7448 total_len = sizeof(struct scsi_per_res_in_header); 7449 break; 7450 case SPRI_RC: /* report capabilities */ 7451 total_len = sizeof(struct scsi_per_res_cap); 7452 break; 7453 case SPRI_RS: /* read full status */ 7454 total_len = sizeof(struct scsi_per_res_in_header) + 7455 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7456 lun->pr_key_count; 7457 break; 7458 default: 7459 panic("Invalid PR type %x", cdb->action); 7460 } 7461 mtx_unlock(&lun->lun_lock); 7462 7463 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7464 7465 if (total_len < alloc_len) { 7466 ctsio->residual = alloc_len - total_len; 7467 ctsio->kern_data_len = total_len; 7468 ctsio->kern_total_len = total_len; 7469 } else { 7470 ctsio->residual = 0; 7471 ctsio->kern_data_len = alloc_len; 7472 ctsio->kern_total_len = alloc_len; 7473 } 7474 7475 ctsio->kern_data_resid = 0; 7476 ctsio->kern_rel_offset = 0; 7477 ctsio->kern_sg_entries = 0; 7478 7479 mtx_lock(&lun->lun_lock); 7480 switch (cdb->action) { 7481 case SPRI_RK: { // read keys 7482 struct scsi_per_res_in_keys *res_keys; 7483 int i, key_count; 7484 7485 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7486 7487 /* 7488 * We had to drop the lock to allocate our buffer, which 7489 * leaves time for someone to come in with another 7490 * persistent reservation. (That is unlikely, though, 7491 * since this should be the only persistent reservation 7492 * command active right now.) 7493 */ 7494 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7495 (lun->pr_key_count * 7496 sizeof(struct scsi_per_res_key)))){ 7497 mtx_unlock(&lun->lun_lock); 7498 free(ctsio->kern_data_ptr, M_CTL); 7499 printf("%s: reservation length changed, retrying\n", 7500 __func__); 7501 goto retry; 7502 } 7503 7504 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7505 7506 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7507 lun->pr_key_count, res_keys->header.length); 7508 7509 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7510 if ((key = ctl_get_prkey(lun, i)) == 0) 7511 continue; 7512 7513 /* 7514 * We used lun->pr_key_count to calculate the 7515 * size to allocate. If it turns out the number of 7516 * initiators with the registered flag set is 7517 * larger than that (i.e. they haven't been kept in 7518 * sync), we've got a problem. 7519 */ 7520 if (key_count >= lun->pr_key_count) { 7521#ifdef NEEDTOPORT 7522 csevent_log(CSC_CTL | CSC_SHELF_SW | 7523 CTL_PR_ERROR, 7524 csevent_LogType_Fault, 7525 csevent_AlertLevel_Yellow, 7526 csevent_FRU_ShelfController, 7527 csevent_FRU_Firmware, 7528 csevent_FRU_Unknown, 7529 "registered keys %d >= key " 7530 "count %d", key_count, 7531 lun->pr_key_count); 7532#endif 7533 key_count++; 7534 continue; 7535 } 7536 scsi_u64to8b(key, res_keys->keys[key_count].key); 7537 key_count++; 7538 } 7539 break; 7540 } 7541 case SPRI_RR: { // read reservation 7542 struct scsi_per_res_in_rsrv *res; 7543 int tmp_len, header_only; 7544 7545 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7546 7547 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7548 7549 if (lun->flags & CTL_LUN_PR_RESERVED) 7550 { 7551 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7552 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7553 res->header.length); 7554 header_only = 0; 7555 } else { 7556 tmp_len = sizeof(struct scsi_per_res_in_header); 7557 scsi_ulto4b(0, res->header.length); 7558 header_only = 1; 7559 } 7560 7561 /* 7562 * We had to drop the lock to allocate our buffer, which 7563 * leaves time for someone to come in with another 7564 * persistent reservation. (That is unlikely, though, 7565 * since this should be the only persistent reservation 7566 * command active right now.) 7567 */ 7568 if (tmp_len != total_len) { 7569 mtx_unlock(&lun->lun_lock); 7570 free(ctsio->kern_data_ptr, M_CTL); 7571 printf("%s: reservation status changed, retrying\n", 7572 __func__); 7573 goto retry; 7574 } 7575 7576 /* 7577 * No reservation held, so we're done. 7578 */ 7579 if (header_only != 0) 7580 break; 7581 7582 /* 7583 * If the registration is an All Registrants type, the key 7584 * is 0, since it doesn't really matter. 7585 */ 7586 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7587 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7588 res->data.reservation); 7589 } 7590 res->data.scopetype = lun->res_type; 7591 break; 7592 } 7593 case SPRI_RC: //report capabilities 7594 { 7595 struct scsi_per_res_cap *res_cap; 7596 uint16_t type_mask; 7597 7598 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7599 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7600 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7601 type_mask = SPRI_TM_WR_EX_AR | 7602 SPRI_TM_EX_AC_RO | 7603 SPRI_TM_WR_EX_RO | 7604 SPRI_TM_EX_AC | 7605 SPRI_TM_WR_EX | 7606 SPRI_TM_EX_AC_AR; 7607 scsi_ulto2b(type_mask, res_cap->type_mask); 7608 break; 7609 } 7610 case SPRI_RS: { // read full status 7611 struct scsi_per_res_in_full *res_status; 7612 struct scsi_per_res_in_full_desc *res_desc; 7613 struct ctl_port *port; 7614 int i, len; 7615 7616 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7617 7618 /* 7619 * We had to drop the lock to allocate our buffer, which 7620 * leaves time for someone to come in with another 7621 * persistent reservation. (That is unlikely, though, 7622 * since this should be the only persistent reservation 7623 * command active right now.) 7624 */ 7625 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7626 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7627 lun->pr_key_count)){ 7628 mtx_unlock(&lun->lun_lock); 7629 free(ctsio->kern_data_ptr, M_CTL); 7630 printf("%s: reservation length changed, retrying\n", 7631 __func__); 7632 goto retry; 7633 } 7634 7635 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7636 7637 res_desc = &res_status->desc[0]; 7638 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7639 if ((key = ctl_get_prkey(lun, i)) == 0) 7640 continue; 7641 7642 scsi_u64to8b(key, res_desc->res_key.key); 7643 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7644 (lun->pr_res_idx == i || 7645 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7646 res_desc->flags = SPRI_FULL_R_HOLDER; 7647 res_desc->scopetype = lun->res_type; 7648 } 7649 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7650 res_desc->rel_trgt_port_id); 7651 len = 0; 7652 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7653 if (port != NULL) 7654 len = ctl_create_iid(port, 7655 i % CTL_MAX_INIT_PER_PORT, 7656 res_desc->transport_id); 7657 scsi_ulto4b(len, res_desc->additional_length); 7658 res_desc = (struct scsi_per_res_in_full_desc *) 7659 &res_desc->transport_id[len]; 7660 } 7661 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7662 res_status->header.length); 7663 break; 7664 } 7665 default: 7666 /* 7667 * This is a bug, because we just checked for this above, 7668 * and should have returned an error. 7669 */ 7670 panic("Invalid PR type %x", cdb->action); 7671 break; /* NOTREACHED */ 7672 } 7673 mtx_unlock(&lun->lun_lock); 7674 7675 ctl_set_success(ctsio); 7676 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7677 ctsio->be_move_done = ctl_config_move_done; 7678 ctl_datamove((union ctl_io *)ctsio); 7679 return (CTL_RETVAL_COMPLETE); 7680} 7681 7682/* 7683 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7684 * it should return. 7685 */ 7686static int 7687ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7688 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7689 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7690 struct scsi_per_res_out_parms* param) 7691{ 7692 union ctl_ha_msg persis_io; 7693 int i; 7694 7695 mtx_lock(&lun->lun_lock); 7696 if (sa_res_key == 0) { 7697 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7698 /* validate scope and type */ 7699 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7700 SPR_LU_SCOPE) { 7701 mtx_unlock(&lun->lun_lock); 7702 ctl_set_invalid_field(/*ctsio*/ ctsio, 7703 /*sks_valid*/ 1, 7704 /*command*/ 1, 7705 /*field*/ 2, 7706 /*bit_valid*/ 1, 7707 /*bit*/ 4); 7708 ctl_done((union ctl_io *)ctsio); 7709 return (1); 7710 } 7711 7712 if (type>8 || type==2 || type==4 || type==0) { 7713 mtx_unlock(&lun->lun_lock); 7714 ctl_set_invalid_field(/*ctsio*/ ctsio, 7715 /*sks_valid*/ 1, 7716 /*command*/ 1, 7717 /*field*/ 2, 7718 /*bit_valid*/ 1, 7719 /*bit*/ 0); 7720 ctl_done((union ctl_io *)ctsio); 7721 return (1); 7722 } 7723 7724 /* 7725 * Unregister everybody else and build UA for 7726 * them 7727 */ 7728 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7729 if (i == residx || ctl_get_prkey(lun, i) == 0) 7730 continue; 7731 7732 ctl_clr_prkey(lun, i); 7733 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7734 } 7735 lun->pr_key_count = 1; 7736 lun->res_type = type; 7737 if (lun->res_type != SPR_TYPE_WR_EX_AR 7738 && lun->res_type != SPR_TYPE_EX_AC_AR) 7739 lun->pr_res_idx = residx; 7740 lun->PRGeneration++; 7741 mtx_unlock(&lun->lun_lock); 7742 7743 /* send msg to other side */ 7744 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7745 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7746 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7747 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7748 persis_io.pr.pr_info.res_type = type; 7749 memcpy(persis_io.pr.pr_info.sa_res_key, 7750 param->serv_act_res_key, 7751 sizeof(param->serv_act_res_key)); 7752 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7753 sizeof(persis_io.pr), M_WAITOK); 7754 } else { 7755 /* not all registrants */ 7756 mtx_unlock(&lun->lun_lock); 7757 free(ctsio->kern_data_ptr, M_CTL); 7758 ctl_set_invalid_field(ctsio, 7759 /*sks_valid*/ 1, 7760 /*command*/ 0, 7761 /*field*/ 8, 7762 /*bit_valid*/ 0, 7763 /*bit*/ 0); 7764 ctl_done((union ctl_io *)ctsio); 7765 return (1); 7766 } 7767 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7768 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7769 int found = 0; 7770 7771 if (res_key == sa_res_key) { 7772 /* special case */ 7773 /* 7774 * The spec implies this is not good but doesn't 7775 * say what to do. There are two choices either 7776 * generate a res conflict or check condition 7777 * with illegal field in parameter data. Since 7778 * that is what is done when the sa_res_key is 7779 * zero I'll take that approach since this has 7780 * to do with the sa_res_key. 7781 */ 7782 mtx_unlock(&lun->lun_lock); 7783 free(ctsio->kern_data_ptr, M_CTL); 7784 ctl_set_invalid_field(ctsio, 7785 /*sks_valid*/ 1, 7786 /*command*/ 0, 7787 /*field*/ 8, 7788 /*bit_valid*/ 0, 7789 /*bit*/ 0); 7790 ctl_done((union ctl_io *)ctsio); 7791 return (1); 7792 } 7793 7794 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7795 if (ctl_get_prkey(lun, i) != sa_res_key) 7796 continue; 7797 7798 found = 1; 7799 ctl_clr_prkey(lun, i); 7800 lun->pr_key_count--; 7801 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7802 } 7803 if (!found) { 7804 mtx_unlock(&lun->lun_lock); 7805 free(ctsio->kern_data_ptr, M_CTL); 7806 ctl_set_reservation_conflict(ctsio); 7807 ctl_done((union ctl_io *)ctsio); 7808 return (CTL_RETVAL_COMPLETE); 7809 } 7810 lun->PRGeneration++; 7811 mtx_unlock(&lun->lun_lock); 7812 7813 /* send msg to other side */ 7814 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7815 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7816 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7817 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7818 persis_io.pr.pr_info.res_type = type; 7819 memcpy(persis_io.pr.pr_info.sa_res_key, 7820 param->serv_act_res_key, 7821 sizeof(param->serv_act_res_key)); 7822 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7823 sizeof(persis_io.pr), M_WAITOK); 7824 } else { 7825 /* Reserved but not all registrants */ 7826 /* sa_res_key is res holder */ 7827 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7828 /* validate scope and type */ 7829 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7830 SPR_LU_SCOPE) { 7831 mtx_unlock(&lun->lun_lock); 7832 ctl_set_invalid_field(/*ctsio*/ ctsio, 7833 /*sks_valid*/ 1, 7834 /*command*/ 1, 7835 /*field*/ 2, 7836 /*bit_valid*/ 1, 7837 /*bit*/ 4); 7838 ctl_done((union ctl_io *)ctsio); 7839 return (1); 7840 } 7841 7842 if (type>8 || type==2 || type==4 || type==0) { 7843 mtx_unlock(&lun->lun_lock); 7844 ctl_set_invalid_field(/*ctsio*/ ctsio, 7845 /*sks_valid*/ 1, 7846 /*command*/ 1, 7847 /*field*/ 2, 7848 /*bit_valid*/ 1, 7849 /*bit*/ 0); 7850 ctl_done((union ctl_io *)ctsio); 7851 return (1); 7852 } 7853 7854 /* 7855 * Do the following: 7856 * if sa_res_key != res_key remove all 7857 * registrants w/sa_res_key and generate UA 7858 * for these registrants(Registrations 7859 * Preempted) if it wasn't an exclusive 7860 * reservation generate UA(Reservations 7861 * Preempted) for all other registered nexuses 7862 * if the type has changed. Establish the new 7863 * reservation and holder. If res_key and 7864 * sa_res_key are the same do the above 7865 * except don't unregister the res holder. 7866 */ 7867 7868 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7869 if (i == residx || ctl_get_prkey(lun, i) == 0) 7870 continue; 7871 7872 if (sa_res_key == ctl_get_prkey(lun, i)) { 7873 ctl_clr_prkey(lun, i); 7874 lun->pr_key_count--; 7875 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7876 } else if (type != lun->res_type 7877 && (lun->res_type == SPR_TYPE_WR_EX_RO 7878 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 7879 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 7880 } 7881 } 7882 lun->res_type = type; 7883 if (lun->res_type != SPR_TYPE_WR_EX_AR 7884 && lun->res_type != SPR_TYPE_EX_AC_AR) 7885 lun->pr_res_idx = residx; 7886 else 7887 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7888 lun->PRGeneration++; 7889 mtx_unlock(&lun->lun_lock); 7890 7891 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7892 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7893 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7894 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7895 persis_io.pr.pr_info.res_type = type; 7896 memcpy(persis_io.pr.pr_info.sa_res_key, 7897 param->serv_act_res_key, 7898 sizeof(param->serv_act_res_key)); 7899 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7900 sizeof(persis_io.pr), M_WAITOK); 7901 } else { 7902 /* 7903 * sa_res_key is not the res holder just 7904 * remove registrants 7905 */ 7906 int found=0; 7907 7908 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7909 if (sa_res_key != ctl_get_prkey(lun, i)) 7910 continue; 7911 7912 found = 1; 7913 ctl_clr_prkey(lun, i); 7914 lun->pr_key_count--; 7915 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7916 } 7917 7918 if (!found) { 7919 mtx_unlock(&lun->lun_lock); 7920 free(ctsio->kern_data_ptr, M_CTL); 7921 ctl_set_reservation_conflict(ctsio); 7922 ctl_done((union ctl_io *)ctsio); 7923 return (1); 7924 } 7925 lun->PRGeneration++; 7926 mtx_unlock(&lun->lun_lock); 7927 7928 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7929 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7930 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7931 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7932 persis_io.pr.pr_info.res_type = type; 7933 memcpy(persis_io.pr.pr_info.sa_res_key, 7934 param->serv_act_res_key, 7935 sizeof(param->serv_act_res_key)); 7936 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7937 sizeof(persis_io.pr), M_WAITOK); 7938 } 7939 } 7940 return (0); 7941} 7942 7943static void 7944ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 7945{ 7946 uint64_t sa_res_key; 7947 int i; 7948 7949 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 7950 7951 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7952 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 7953 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 7954 if (sa_res_key == 0) { 7955 /* 7956 * Unregister everybody else and build UA for 7957 * them 7958 */ 7959 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7960 if (i == msg->pr.pr_info.residx || 7961 ctl_get_prkey(lun, i) == 0) 7962 continue; 7963 7964 ctl_clr_prkey(lun, i); 7965 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7966 } 7967 7968 lun->pr_key_count = 1; 7969 lun->res_type = msg->pr.pr_info.res_type; 7970 if (lun->res_type != SPR_TYPE_WR_EX_AR 7971 && lun->res_type != SPR_TYPE_EX_AC_AR) 7972 lun->pr_res_idx = msg->pr.pr_info.residx; 7973 } else { 7974 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7975 if (sa_res_key == ctl_get_prkey(lun, i)) 7976 continue; 7977 7978 ctl_clr_prkey(lun, i); 7979 lun->pr_key_count--; 7980 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7981 } 7982 } 7983 } else { 7984 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7985 if (i == msg->pr.pr_info.residx || 7986 ctl_get_prkey(lun, i) == 0) 7987 continue; 7988 7989 if (sa_res_key == ctl_get_prkey(lun, i)) { 7990 ctl_clr_prkey(lun, i); 7991 lun->pr_key_count--; 7992 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7993 } else if (msg->pr.pr_info.res_type != lun->res_type 7994 && (lun->res_type == SPR_TYPE_WR_EX_RO 7995 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 7996 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 7997 } 7998 } 7999 lun->res_type = msg->pr.pr_info.res_type; 8000 if (lun->res_type != SPR_TYPE_WR_EX_AR 8001 && lun->res_type != SPR_TYPE_EX_AC_AR) 8002 lun->pr_res_idx = msg->pr.pr_info.residx; 8003 else 8004 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8005 } 8006 lun->PRGeneration++; 8007 8008} 8009 8010 8011int 8012ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8013{ 8014 int retval; 8015 u_int32_t param_len; 8016 struct scsi_per_res_out *cdb; 8017 struct ctl_lun *lun; 8018 struct scsi_per_res_out_parms* param; 8019 struct ctl_softc *softc; 8020 uint32_t residx; 8021 uint64_t res_key, sa_res_key, key; 8022 uint8_t type; 8023 union ctl_ha_msg persis_io; 8024 int i; 8025 8026 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8027 8028 retval = CTL_RETVAL_COMPLETE; 8029 8030 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8031 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8032 softc = lun->ctl_softc; 8033 8034 /* 8035 * We only support whole-LUN scope. The scope & type are ignored for 8036 * register, register and ignore existing key and clear. 8037 * We sometimes ignore scope and type on preempts too!! 8038 * Verify reservation type here as well. 8039 */ 8040 type = cdb->scope_type & SPR_TYPE_MASK; 8041 if ((cdb->action == SPRO_RESERVE) 8042 || (cdb->action == SPRO_RELEASE)) { 8043 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8044 ctl_set_invalid_field(/*ctsio*/ ctsio, 8045 /*sks_valid*/ 1, 8046 /*command*/ 1, 8047 /*field*/ 2, 8048 /*bit_valid*/ 1, 8049 /*bit*/ 4); 8050 ctl_done((union ctl_io *)ctsio); 8051 return (CTL_RETVAL_COMPLETE); 8052 } 8053 8054 if (type>8 || type==2 || type==4 || type==0) { 8055 ctl_set_invalid_field(/*ctsio*/ ctsio, 8056 /*sks_valid*/ 1, 8057 /*command*/ 1, 8058 /*field*/ 2, 8059 /*bit_valid*/ 1, 8060 /*bit*/ 0); 8061 ctl_done((union ctl_io *)ctsio); 8062 return (CTL_RETVAL_COMPLETE); 8063 } 8064 } 8065 8066 param_len = scsi_4btoul(cdb->length); 8067 8068 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8069 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8070 ctsio->kern_data_len = param_len; 8071 ctsio->kern_total_len = param_len; 8072 ctsio->kern_data_resid = 0; 8073 ctsio->kern_rel_offset = 0; 8074 ctsio->kern_sg_entries = 0; 8075 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8076 ctsio->be_move_done = ctl_config_move_done; 8077 ctl_datamove((union ctl_io *)ctsio); 8078 8079 return (CTL_RETVAL_COMPLETE); 8080 } 8081 8082 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8083 8084 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8085 res_key = scsi_8btou64(param->res_key.key); 8086 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8087 8088 /* 8089 * Validate the reservation key here except for SPRO_REG_IGNO 8090 * This must be done for all other service actions 8091 */ 8092 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8093 mtx_lock(&lun->lun_lock); 8094 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8095 if (res_key != key) { 8096 /* 8097 * The current key passed in doesn't match 8098 * the one the initiator previously 8099 * registered. 8100 */ 8101 mtx_unlock(&lun->lun_lock); 8102 free(ctsio->kern_data_ptr, M_CTL); 8103 ctl_set_reservation_conflict(ctsio); 8104 ctl_done((union ctl_io *)ctsio); 8105 return (CTL_RETVAL_COMPLETE); 8106 } 8107 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8108 /* 8109 * We are not registered 8110 */ 8111 mtx_unlock(&lun->lun_lock); 8112 free(ctsio->kern_data_ptr, M_CTL); 8113 ctl_set_reservation_conflict(ctsio); 8114 ctl_done((union ctl_io *)ctsio); 8115 return (CTL_RETVAL_COMPLETE); 8116 } else if (res_key != 0) { 8117 /* 8118 * We are not registered and trying to register but 8119 * the register key isn't zero. 8120 */ 8121 mtx_unlock(&lun->lun_lock); 8122 free(ctsio->kern_data_ptr, M_CTL); 8123 ctl_set_reservation_conflict(ctsio); 8124 ctl_done((union ctl_io *)ctsio); 8125 return (CTL_RETVAL_COMPLETE); 8126 } 8127 mtx_unlock(&lun->lun_lock); 8128 } 8129 8130 switch (cdb->action & SPRO_ACTION_MASK) { 8131 case SPRO_REGISTER: 8132 case SPRO_REG_IGNO: { 8133 8134#if 0 8135 printf("Registration received\n"); 8136#endif 8137 8138 /* 8139 * We don't support any of these options, as we report in 8140 * the read capabilities request (see 8141 * ctl_persistent_reserve_in(), above). 8142 */ 8143 if ((param->flags & SPR_SPEC_I_PT) 8144 || (param->flags & SPR_ALL_TG_PT) 8145 || (param->flags & SPR_APTPL)) { 8146 int bit_ptr; 8147 8148 if (param->flags & SPR_APTPL) 8149 bit_ptr = 0; 8150 else if (param->flags & SPR_ALL_TG_PT) 8151 bit_ptr = 2; 8152 else /* SPR_SPEC_I_PT */ 8153 bit_ptr = 3; 8154 8155 free(ctsio->kern_data_ptr, M_CTL); 8156 ctl_set_invalid_field(ctsio, 8157 /*sks_valid*/ 1, 8158 /*command*/ 0, 8159 /*field*/ 20, 8160 /*bit_valid*/ 1, 8161 /*bit*/ bit_ptr); 8162 ctl_done((union ctl_io *)ctsio); 8163 return (CTL_RETVAL_COMPLETE); 8164 } 8165 8166 mtx_lock(&lun->lun_lock); 8167 8168 /* 8169 * The initiator wants to clear the 8170 * key/unregister. 8171 */ 8172 if (sa_res_key == 0) { 8173 if ((res_key == 0 8174 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8175 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8176 && ctl_get_prkey(lun, residx) == 0)) { 8177 mtx_unlock(&lun->lun_lock); 8178 goto done; 8179 } 8180 8181 ctl_clr_prkey(lun, residx); 8182 lun->pr_key_count--; 8183 8184 if (residx == lun->pr_res_idx) { 8185 lun->flags &= ~CTL_LUN_PR_RESERVED; 8186 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8187 8188 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8189 || lun->res_type == SPR_TYPE_EX_AC_RO) 8190 && lun->pr_key_count) { 8191 /* 8192 * If the reservation is a registrants 8193 * only type we need to generate a UA 8194 * for other registered inits. The 8195 * sense code should be RESERVATIONS 8196 * RELEASED 8197 */ 8198 8199 for (i = softc->init_min; i < softc->init_max; i++){ 8200 if (ctl_get_prkey(lun, i) == 0) 8201 continue; 8202 ctl_est_ua(lun, i, 8203 CTL_UA_RES_RELEASE); 8204 } 8205 } 8206 lun->res_type = 0; 8207 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8208 if (lun->pr_key_count==0) { 8209 lun->flags &= ~CTL_LUN_PR_RESERVED; 8210 lun->res_type = 0; 8211 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8212 } 8213 } 8214 lun->PRGeneration++; 8215 mtx_unlock(&lun->lun_lock); 8216 8217 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8218 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8219 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8220 persis_io.pr.pr_info.residx = residx; 8221 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8222 sizeof(persis_io.pr), M_WAITOK); 8223 } else /* sa_res_key != 0 */ { 8224 8225 /* 8226 * If we aren't registered currently then increment 8227 * the key count and set the registered flag. 8228 */ 8229 ctl_alloc_prkey(lun, residx); 8230 if (ctl_get_prkey(lun, residx) == 0) 8231 lun->pr_key_count++; 8232 ctl_set_prkey(lun, residx, sa_res_key); 8233 lun->PRGeneration++; 8234 mtx_unlock(&lun->lun_lock); 8235 8236 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8237 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8238 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8239 persis_io.pr.pr_info.residx = residx; 8240 memcpy(persis_io.pr.pr_info.sa_res_key, 8241 param->serv_act_res_key, 8242 sizeof(param->serv_act_res_key)); 8243 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8244 sizeof(persis_io.pr), M_WAITOK); 8245 } 8246 8247 break; 8248 } 8249 case SPRO_RESERVE: 8250#if 0 8251 printf("Reserve executed type %d\n", type); 8252#endif 8253 mtx_lock(&lun->lun_lock); 8254 if (lun->flags & CTL_LUN_PR_RESERVED) { 8255 /* 8256 * if this isn't the reservation holder and it's 8257 * not a "all registrants" type or if the type is 8258 * different then we have a conflict 8259 */ 8260 if ((lun->pr_res_idx != residx 8261 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8262 || lun->res_type != type) { 8263 mtx_unlock(&lun->lun_lock); 8264 free(ctsio->kern_data_ptr, M_CTL); 8265 ctl_set_reservation_conflict(ctsio); 8266 ctl_done((union ctl_io *)ctsio); 8267 return (CTL_RETVAL_COMPLETE); 8268 } 8269 mtx_unlock(&lun->lun_lock); 8270 } else /* create a reservation */ { 8271 /* 8272 * If it's not an "all registrants" type record 8273 * reservation holder 8274 */ 8275 if (type != SPR_TYPE_WR_EX_AR 8276 && type != SPR_TYPE_EX_AC_AR) 8277 lun->pr_res_idx = residx; /* Res holder */ 8278 else 8279 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8280 8281 lun->flags |= CTL_LUN_PR_RESERVED; 8282 lun->res_type = type; 8283 8284 mtx_unlock(&lun->lun_lock); 8285 8286 /* send msg to other side */ 8287 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8288 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8289 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8290 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8291 persis_io.pr.pr_info.res_type = type; 8292 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8293 sizeof(persis_io.pr), M_WAITOK); 8294 } 8295 break; 8296 8297 case SPRO_RELEASE: 8298 mtx_lock(&lun->lun_lock); 8299 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8300 /* No reservation exists return good status */ 8301 mtx_unlock(&lun->lun_lock); 8302 goto done; 8303 } 8304 /* 8305 * Is this nexus a reservation holder? 8306 */ 8307 if (lun->pr_res_idx != residx 8308 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8309 /* 8310 * not a res holder return good status but 8311 * do nothing 8312 */ 8313 mtx_unlock(&lun->lun_lock); 8314 goto done; 8315 } 8316 8317 if (lun->res_type != type) { 8318 mtx_unlock(&lun->lun_lock); 8319 free(ctsio->kern_data_ptr, M_CTL); 8320 ctl_set_illegal_pr_release(ctsio); 8321 ctl_done((union ctl_io *)ctsio); 8322 return (CTL_RETVAL_COMPLETE); 8323 } 8324 8325 /* okay to release */ 8326 lun->flags &= ~CTL_LUN_PR_RESERVED; 8327 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8328 lun->res_type = 0; 8329 8330 /* 8331 * if this isn't an exclusive access 8332 * res generate UA for all other 8333 * registrants. 8334 */ 8335 if (type != SPR_TYPE_EX_AC 8336 && type != SPR_TYPE_WR_EX) { 8337 for (i = softc->init_min; i < softc->init_max; i++) { 8338 if (i == residx || ctl_get_prkey(lun, i) == 0) 8339 continue; 8340 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8341 } 8342 } 8343 mtx_unlock(&lun->lun_lock); 8344 8345 /* Send msg to other side */ 8346 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8347 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8348 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8349 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8350 sizeof(persis_io.pr), M_WAITOK); 8351 break; 8352 8353 case SPRO_CLEAR: 8354 /* send msg to other side */ 8355 8356 mtx_lock(&lun->lun_lock); 8357 lun->flags &= ~CTL_LUN_PR_RESERVED; 8358 lun->res_type = 0; 8359 lun->pr_key_count = 0; 8360 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8361 8362 ctl_clr_prkey(lun, residx); 8363 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8364 if (ctl_get_prkey(lun, i) != 0) { 8365 ctl_clr_prkey(lun, i); 8366 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8367 } 8368 lun->PRGeneration++; 8369 mtx_unlock(&lun->lun_lock); 8370 8371 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8372 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8373 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8374 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8375 sizeof(persis_io.pr), M_WAITOK); 8376 break; 8377 8378 case SPRO_PREEMPT: 8379 case SPRO_PRE_ABO: { 8380 int nretval; 8381 8382 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8383 residx, ctsio, cdb, param); 8384 if (nretval != 0) 8385 return (CTL_RETVAL_COMPLETE); 8386 break; 8387 } 8388 default: 8389 panic("Invalid PR type %x", cdb->action); 8390 } 8391 8392done: 8393 free(ctsio->kern_data_ptr, M_CTL); 8394 ctl_set_success(ctsio); 8395 ctl_done((union ctl_io *)ctsio); 8396 8397 return (retval); 8398} 8399 8400/* 8401 * This routine is for handling a message from the other SC pertaining to 8402 * persistent reserve out. All the error checking will have been done 8403 * so only perorming the action need be done here to keep the two 8404 * in sync. 8405 */ 8406static void 8407ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8408{ 8409 struct ctl_lun *lun; 8410 struct ctl_softc *softc; 8411 int i; 8412 uint32_t residx, targ_lun; 8413 8414 softc = control_softc; 8415 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8416 mtx_lock(&softc->ctl_lock); 8417 if ((targ_lun >= CTL_MAX_LUNS) || 8418 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 8419 mtx_unlock(&softc->ctl_lock); 8420 return; 8421 } 8422 mtx_lock(&lun->lun_lock); 8423 mtx_unlock(&softc->ctl_lock); 8424 if (lun->flags & CTL_LUN_DISABLED) { 8425 mtx_unlock(&lun->lun_lock); 8426 return; 8427 } 8428 residx = ctl_get_initindex(&msg->hdr.nexus); 8429 switch(msg->pr.pr_info.action) { 8430 case CTL_PR_REG_KEY: 8431 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8432 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8433 lun->pr_key_count++; 8434 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8435 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8436 lun->PRGeneration++; 8437 break; 8438 8439 case CTL_PR_UNREG_KEY: 8440 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8441 lun->pr_key_count--; 8442 8443 /* XXX Need to see if the reservation has been released */ 8444 /* if so do we need to generate UA? */ 8445 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8446 lun->flags &= ~CTL_LUN_PR_RESERVED; 8447 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8448 8449 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8450 || lun->res_type == SPR_TYPE_EX_AC_RO) 8451 && lun->pr_key_count) { 8452 /* 8453 * If the reservation is a registrants 8454 * only type we need to generate a UA 8455 * for other registered inits. The 8456 * sense code should be RESERVATIONS 8457 * RELEASED 8458 */ 8459 8460 for (i = softc->init_min; i < softc->init_max; i++) { 8461 if (ctl_get_prkey(lun, i) == 0) 8462 continue; 8463 8464 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8465 } 8466 } 8467 lun->res_type = 0; 8468 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8469 if (lun->pr_key_count==0) { 8470 lun->flags &= ~CTL_LUN_PR_RESERVED; 8471 lun->res_type = 0; 8472 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8473 } 8474 } 8475 lun->PRGeneration++; 8476 break; 8477 8478 case CTL_PR_RESERVE: 8479 lun->flags |= CTL_LUN_PR_RESERVED; 8480 lun->res_type = msg->pr.pr_info.res_type; 8481 lun->pr_res_idx = msg->pr.pr_info.residx; 8482 8483 break; 8484 8485 case CTL_PR_RELEASE: 8486 /* 8487 * if this isn't an exclusive access res generate UA for all 8488 * other registrants. 8489 */ 8490 if (lun->res_type != SPR_TYPE_EX_AC 8491 && lun->res_type != SPR_TYPE_WR_EX) { 8492 for (i = softc->init_min; i < softc->init_max; i++) 8493 if (i == residx || ctl_get_prkey(lun, i) == 0) 8494 continue; 8495 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8496 } 8497 8498 lun->flags &= ~CTL_LUN_PR_RESERVED; 8499 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8500 lun->res_type = 0; 8501 break; 8502 8503 case CTL_PR_PREEMPT: 8504 ctl_pro_preempt_other(lun, msg); 8505 break; 8506 case CTL_PR_CLEAR: 8507 lun->flags &= ~CTL_LUN_PR_RESERVED; 8508 lun->res_type = 0; 8509 lun->pr_key_count = 0; 8510 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8511 8512 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8513 if (ctl_get_prkey(lun, i) == 0) 8514 continue; 8515 ctl_clr_prkey(lun, i); 8516 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8517 } 8518 lun->PRGeneration++; 8519 break; 8520 } 8521 8522 mtx_unlock(&lun->lun_lock); 8523} 8524 8525int 8526ctl_read_write(struct ctl_scsiio *ctsio) 8527{ 8528 struct ctl_lun *lun; 8529 struct ctl_lba_len_flags *lbalen; 8530 uint64_t lba; 8531 uint32_t num_blocks; 8532 int flags, retval; 8533 int isread; 8534 8535 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8536 8537 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8538 8539 flags = 0; 8540 retval = CTL_RETVAL_COMPLETE; 8541 8542 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8543 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8544 switch (ctsio->cdb[0]) { 8545 case READ_6: 8546 case WRITE_6: { 8547 struct scsi_rw_6 *cdb; 8548 8549 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8550 8551 lba = scsi_3btoul(cdb->addr); 8552 /* only 5 bits are valid in the most significant address byte */ 8553 lba &= 0x1fffff; 8554 num_blocks = cdb->length; 8555 /* 8556 * This is correct according to SBC-2. 8557 */ 8558 if (num_blocks == 0) 8559 num_blocks = 256; 8560 break; 8561 } 8562 case READ_10: 8563 case WRITE_10: { 8564 struct scsi_rw_10 *cdb; 8565 8566 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8567 if (cdb->byte2 & SRW10_FUA) 8568 flags |= CTL_LLF_FUA; 8569 if (cdb->byte2 & SRW10_DPO) 8570 flags |= CTL_LLF_DPO; 8571 lba = scsi_4btoul(cdb->addr); 8572 num_blocks = scsi_2btoul(cdb->length); 8573 break; 8574 } 8575 case WRITE_VERIFY_10: { 8576 struct scsi_write_verify_10 *cdb; 8577 8578 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8579 flags |= CTL_LLF_FUA; 8580 if (cdb->byte2 & SWV_DPO) 8581 flags |= CTL_LLF_DPO; 8582 lba = scsi_4btoul(cdb->addr); 8583 num_blocks = scsi_2btoul(cdb->length); 8584 break; 8585 } 8586 case READ_12: 8587 case WRITE_12: { 8588 struct scsi_rw_12 *cdb; 8589 8590 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8591 if (cdb->byte2 & SRW12_FUA) 8592 flags |= CTL_LLF_FUA; 8593 if (cdb->byte2 & SRW12_DPO) 8594 flags |= CTL_LLF_DPO; 8595 lba = scsi_4btoul(cdb->addr); 8596 num_blocks = scsi_4btoul(cdb->length); 8597 break; 8598 } 8599 case WRITE_VERIFY_12: { 8600 struct scsi_write_verify_12 *cdb; 8601 8602 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8603 flags |= CTL_LLF_FUA; 8604 if (cdb->byte2 & SWV_DPO) 8605 flags |= CTL_LLF_DPO; 8606 lba = scsi_4btoul(cdb->addr); 8607 num_blocks = scsi_4btoul(cdb->length); 8608 break; 8609 } 8610 case READ_16: 8611 case WRITE_16: { 8612 struct scsi_rw_16 *cdb; 8613 8614 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8615 if (cdb->byte2 & SRW12_FUA) 8616 flags |= CTL_LLF_FUA; 8617 if (cdb->byte2 & SRW12_DPO) 8618 flags |= CTL_LLF_DPO; 8619 lba = scsi_8btou64(cdb->addr); 8620 num_blocks = scsi_4btoul(cdb->length); 8621 break; 8622 } 8623 case WRITE_ATOMIC_16: { 8624 struct scsi_rw_16 *cdb; 8625 8626 if (lun->be_lun->atomicblock == 0) { 8627 ctl_set_invalid_opcode(ctsio); 8628 ctl_done((union ctl_io *)ctsio); 8629 return (CTL_RETVAL_COMPLETE); 8630 } 8631 8632 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8633 if (cdb->byte2 & SRW12_FUA) 8634 flags |= CTL_LLF_FUA; 8635 if (cdb->byte2 & SRW12_DPO) 8636 flags |= CTL_LLF_DPO; 8637 lba = scsi_8btou64(cdb->addr); 8638 num_blocks = scsi_4btoul(cdb->length); 8639 if (num_blocks > lun->be_lun->atomicblock) { 8640 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8641 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8642 /*bit*/ 0); 8643 ctl_done((union ctl_io *)ctsio); 8644 return (CTL_RETVAL_COMPLETE); 8645 } 8646 break; 8647 } 8648 case WRITE_VERIFY_16: { 8649 struct scsi_write_verify_16 *cdb; 8650 8651 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8652 flags |= CTL_LLF_FUA; 8653 if (cdb->byte2 & SWV_DPO) 8654 flags |= CTL_LLF_DPO; 8655 lba = scsi_8btou64(cdb->addr); 8656 num_blocks = scsi_4btoul(cdb->length); 8657 break; 8658 } 8659 default: 8660 /* 8661 * We got a command we don't support. This shouldn't 8662 * happen, commands should be filtered out above us. 8663 */ 8664 ctl_set_invalid_opcode(ctsio); 8665 ctl_done((union ctl_io *)ctsio); 8666 8667 return (CTL_RETVAL_COMPLETE); 8668 break; /* NOTREACHED */ 8669 } 8670 8671 /* 8672 * The first check is to make sure we're in bounds, the second 8673 * check is to catch wrap-around problems. If the lba + num blocks 8674 * is less than the lba, then we've wrapped around and the block 8675 * range is invalid anyway. 8676 */ 8677 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8678 || ((lba + num_blocks) < lba)) { 8679 ctl_set_lba_out_of_range(ctsio); 8680 ctl_done((union ctl_io *)ctsio); 8681 return (CTL_RETVAL_COMPLETE); 8682 } 8683 8684 /* 8685 * According to SBC-3, a transfer length of 0 is not an error. 8686 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8687 * translates to 256 blocks for those commands. 8688 */ 8689 if (num_blocks == 0) { 8690 ctl_set_success(ctsio); 8691 ctl_done((union ctl_io *)ctsio); 8692 return (CTL_RETVAL_COMPLETE); 8693 } 8694 8695 /* Set FUA and/or DPO if caches are disabled. */ 8696 if (isread) { 8697 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8698 SCP_RCD) != 0) 8699 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8700 } else { 8701 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8702 SCP_WCE) == 0) 8703 flags |= CTL_LLF_FUA; 8704 } 8705 8706 lbalen = (struct ctl_lba_len_flags *) 8707 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8708 lbalen->lba = lba; 8709 lbalen->len = num_blocks; 8710 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8711 8712 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8713 ctsio->kern_rel_offset = 0; 8714 8715 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8716 8717 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8718 8719 return (retval); 8720} 8721 8722static int 8723ctl_cnw_cont(union ctl_io *io) 8724{ 8725 struct ctl_scsiio *ctsio; 8726 struct ctl_lun *lun; 8727 struct ctl_lba_len_flags *lbalen; 8728 int retval; 8729 8730 ctsio = &io->scsiio; 8731 ctsio->io_hdr.status = CTL_STATUS_NONE; 8732 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8733 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8734 lbalen = (struct ctl_lba_len_flags *) 8735 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8736 lbalen->flags &= ~CTL_LLF_COMPARE; 8737 lbalen->flags |= CTL_LLF_WRITE; 8738 8739 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8740 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8741 return (retval); 8742} 8743 8744int 8745ctl_cnw(struct ctl_scsiio *ctsio) 8746{ 8747 struct ctl_lun *lun; 8748 struct ctl_lba_len_flags *lbalen; 8749 uint64_t lba; 8750 uint32_t num_blocks; 8751 int flags, retval; 8752 8753 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8754 8755 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8756 8757 flags = 0; 8758 retval = CTL_RETVAL_COMPLETE; 8759 8760 switch (ctsio->cdb[0]) { 8761 case COMPARE_AND_WRITE: { 8762 struct scsi_compare_and_write *cdb; 8763 8764 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8765 if (cdb->byte2 & SRW10_FUA) 8766 flags |= CTL_LLF_FUA; 8767 if (cdb->byte2 & SRW10_DPO) 8768 flags |= CTL_LLF_DPO; 8769 lba = scsi_8btou64(cdb->addr); 8770 num_blocks = cdb->length; 8771 break; 8772 } 8773 default: 8774 /* 8775 * We got a command we don't support. This shouldn't 8776 * happen, commands should be filtered out above us. 8777 */ 8778 ctl_set_invalid_opcode(ctsio); 8779 ctl_done((union ctl_io *)ctsio); 8780 8781 return (CTL_RETVAL_COMPLETE); 8782 break; /* NOTREACHED */ 8783 } 8784 8785 /* 8786 * The first check is to make sure we're in bounds, the second 8787 * check is to catch wrap-around problems. If the lba + num blocks 8788 * is less than the lba, then we've wrapped around and the block 8789 * range is invalid anyway. 8790 */ 8791 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8792 || ((lba + num_blocks) < lba)) { 8793 ctl_set_lba_out_of_range(ctsio); 8794 ctl_done((union ctl_io *)ctsio); 8795 return (CTL_RETVAL_COMPLETE); 8796 } 8797 8798 /* 8799 * According to SBC-3, a transfer length of 0 is not an error. 8800 */ 8801 if (num_blocks == 0) { 8802 ctl_set_success(ctsio); 8803 ctl_done((union ctl_io *)ctsio); 8804 return (CTL_RETVAL_COMPLETE); 8805 } 8806 8807 /* Set FUA if write cache is disabled. */ 8808 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8809 SCP_WCE) == 0) 8810 flags |= CTL_LLF_FUA; 8811 8812 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8813 ctsio->kern_rel_offset = 0; 8814 8815 /* 8816 * Set the IO_CONT flag, so that if this I/O gets passed to 8817 * ctl_data_submit_done(), it'll get passed back to 8818 * ctl_ctl_cnw_cont() for further processing. 8819 */ 8820 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8821 ctsio->io_cont = ctl_cnw_cont; 8822 8823 lbalen = (struct ctl_lba_len_flags *) 8824 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8825 lbalen->lba = lba; 8826 lbalen->len = num_blocks; 8827 lbalen->flags = CTL_LLF_COMPARE | flags; 8828 8829 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8830 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8831 return (retval); 8832} 8833 8834int 8835ctl_verify(struct ctl_scsiio *ctsio) 8836{ 8837 struct ctl_lun *lun; 8838 struct ctl_lba_len_flags *lbalen; 8839 uint64_t lba; 8840 uint32_t num_blocks; 8841 int bytchk, flags; 8842 int retval; 8843 8844 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8845 8846 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8847 8848 bytchk = 0; 8849 flags = CTL_LLF_FUA; 8850 retval = CTL_RETVAL_COMPLETE; 8851 8852 switch (ctsio->cdb[0]) { 8853 case VERIFY_10: { 8854 struct scsi_verify_10 *cdb; 8855 8856 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8857 if (cdb->byte2 & SVFY_BYTCHK) 8858 bytchk = 1; 8859 if (cdb->byte2 & SVFY_DPO) 8860 flags |= CTL_LLF_DPO; 8861 lba = scsi_4btoul(cdb->addr); 8862 num_blocks = scsi_2btoul(cdb->length); 8863 break; 8864 } 8865 case VERIFY_12: { 8866 struct scsi_verify_12 *cdb; 8867 8868 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8869 if (cdb->byte2 & SVFY_BYTCHK) 8870 bytchk = 1; 8871 if (cdb->byte2 & SVFY_DPO) 8872 flags |= CTL_LLF_DPO; 8873 lba = scsi_4btoul(cdb->addr); 8874 num_blocks = scsi_4btoul(cdb->length); 8875 break; 8876 } 8877 case VERIFY_16: { 8878 struct scsi_rw_16 *cdb; 8879 8880 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8881 if (cdb->byte2 & SVFY_BYTCHK) 8882 bytchk = 1; 8883 if (cdb->byte2 & SVFY_DPO) 8884 flags |= CTL_LLF_DPO; 8885 lba = scsi_8btou64(cdb->addr); 8886 num_blocks = scsi_4btoul(cdb->length); 8887 break; 8888 } 8889 default: 8890 /* 8891 * We got a command we don't support. This shouldn't 8892 * happen, commands should be filtered out above us. 8893 */ 8894 ctl_set_invalid_opcode(ctsio); 8895 ctl_done((union ctl_io *)ctsio); 8896 return (CTL_RETVAL_COMPLETE); 8897 } 8898 8899 /* 8900 * The first check is to make sure we're in bounds, the second 8901 * check is to catch wrap-around problems. If the lba + num blocks 8902 * is less than the lba, then we've wrapped around and the block 8903 * range is invalid anyway. 8904 */ 8905 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8906 || ((lba + num_blocks) < lba)) { 8907 ctl_set_lba_out_of_range(ctsio); 8908 ctl_done((union ctl_io *)ctsio); 8909 return (CTL_RETVAL_COMPLETE); 8910 } 8911 8912 /* 8913 * According to SBC-3, a transfer length of 0 is not an error. 8914 */ 8915 if (num_blocks == 0) { 8916 ctl_set_success(ctsio); 8917 ctl_done((union ctl_io *)ctsio); 8918 return (CTL_RETVAL_COMPLETE); 8919 } 8920 8921 lbalen = (struct ctl_lba_len_flags *) 8922 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8923 lbalen->lba = lba; 8924 lbalen->len = num_blocks; 8925 if (bytchk) { 8926 lbalen->flags = CTL_LLF_COMPARE | flags; 8927 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8928 } else { 8929 lbalen->flags = CTL_LLF_VERIFY | flags; 8930 ctsio->kern_total_len = 0; 8931 } 8932 ctsio->kern_rel_offset = 0; 8933 8934 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 8935 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8936 return (retval); 8937} 8938 8939int 8940ctl_report_luns(struct ctl_scsiio *ctsio) 8941{ 8942 struct ctl_softc *softc = control_softc; 8943 struct scsi_report_luns *cdb; 8944 struct scsi_report_luns_data *lun_data; 8945 struct ctl_lun *lun, *request_lun; 8946 struct ctl_port *port; 8947 int num_luns, retval; 8948 uint32_t alloc_len, lun_datalen; 8949 int num_filled, well_known; 8950 uint32_t initidx, targ_lun_id, lun_id; 8951 8952 retval = CTL_RETVAL_COMPLETE; 8953 well_known = 0; 8954 8955 cdb = (struct scsi_report_luns *)ctsio->cdb; 8956 port = ctl_io_port(&ctsio->io_hdr); 8957 8958 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 8959 8960 mtx_lock(&softc->ctl_lock); 8961 num_luns = 0; 8962 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 8963 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 8964 num_luns++; 8965 } 8966 mtx_unlock(&softc->ctl_lock); 8967 8968 switch (cdb->select_report) { 8969 case RPL_REPORT_DEFAULT: 8970 case RPL_REPORT_ALL: 8971 break; 8972 case RPL_REPORT_WELLKNOWN: 8973 well_known = 1; 8974 num_luns = 0; 8975 break; 8976 default: 8977 ctl_set_invalid_field(ctsio, 8978 /*sks_valid*/ 1, 8979 /*command*/ 1, 8980 /*field*/ 2, 8981 /*bit_valid*/ 0, 8982 /*bit*/ 0); 8983 ctl_done((union ctl_io *)ctsio); 8984 return (retval); 8985 break; /* NOTREACHED */ 8986 } 8987 8988 alloc_len = scsi_4btoul(cdb->length); 8989 /* 8990 * The initiator has to allocate at least 16 bytes for this request, 8991 * so he can at least get the header and the first LUN. Otherwise 8992 * we reject the request (per SPC-3 rev 14, section 6.21). 8993 */ 8994 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 8995 sizeof(struct scsi_report_luns_lundata))) { 8996 ctl_set_invalid_field(ctsio, 8997 /*sks_valid*/ 1, 8998 /*command*/ 1, 8999 /*field*/ 6, 9000 /*bit_valid*/ 0, 9001 /*bit*/ 0); 9002 ctl_done((union ctl_io *)ctsio); 9003 return (retval); 9004 } 9005 9006 request_lun = (struct ctl_lun *) 9007 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9008 9009 lun_datalen = sizeof(*lun_data) + 9010 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9011 9012 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9013 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9014 ctsio->kern_sg_entries = 0; 9015 9016 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9017 9018 mtx_lock(&softc->ctl_lock); 9019 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9020 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9021 if (lun_id >= CTL_MAX_LUNS) 9022 continue; 9023 lun = softc->ctl_luns[lun_id]; 9024 if (lun == NULL) 9025 continue; 9026 9027 if (targ_lun_id <= 0xff) { 9028 /* 9029 * Peripheral addressing method, bus number 0. 9030 */ 9031 lun_data->luns[num_filled].lundata[0] = 9032 RPL_LUNDATA_ATYP_PERIPH; 9033 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9034 num_filled++; 9035 } else if (targ_lun_id <= 0x3fff) { 9036 /* 9037 * Flat addressing method. 9038 */ 9039 lun_data->luns[num_filled].lundata[0] = 9040 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9041 lun_data->luns[num_filled].lundata[1] = 9042 (targ_lun_id & 0xff); 9043 num_filled++; 9044 } else if (targ_lun_id <= 0xffffff) { 9045 /* 9046 * Extended flat addressing method. 9047 */ 9048 lun_data->luns[num_filled].lundata[0] = 9049 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9050 scsi_ulto3b(targ_lun_id, 9051 &lun_data->luns[num_filled].lundata[1]); 9052 num_filled++; 9053 } else { 9054 printf("ctl_report_luns: bogus LUN number %jd, " 9055 "skipping\n", (intmax_t)targ_lun_id); 9056 } 9057 /* 9058 * According to SPC-3, rev 14 section 6.21: 9059 * 9060 * "The execution of a REPORT LUNS command to any valid and 9061 * installed logical unit shall clear the REPORTED LUNS DATA 9062 * HAS CHANGED unit attention condition for all logical 9063 * units of that target with respect to the requesting 9064 * initiator. A valid and installed logical unit is one 9065 * having a PERIPHERAL QUALIFIER of 000b in the standard 9066 * INQUIRY data (see 6.4.2)." 9067 * 9068 * If request_lun is NULL, the LUN this report luns command 9069 * was issued to is either disabled or doesn't exist. In that 9070 * case, we shouldn't clear any pending lun change unit 9071 * attention. 9072 */ 9073 if (request_lun != NULL) { 9074 mtx_lock(&lun->lun_lock); 9075 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9076 mtx_unlock(&lun->lun_lock); 9077 } 9078 } 9079 mtx_unlock(&softc->ctl_lock); 9080 9081 /* 9082 * It's quite possible that we've returned fewer LUNs than we allocated 9083 * space for. Trim it. 9084 */ 9085 lun_datalen = sizeof(*lun_data) + 9086 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9087 9088 if (lun_datalen < alloc_len) { 9089 ctsio->residual = alloc_len - lun_datalen; 9090 ctsio->kern_data_len = lun_datalen; 9091 ctsio->kern_total_len = lun_datalen; 9092 } else { 9093 ctsio->residual = 0; 9094 ctsio->kern_data_len = alloc_len; 9095 ctsio->kern_total_len = alloc_len; 9096 } 9097 ctsio->kern_data_resid = 0; 9098 ctsio->kern_rel_offset = 0; 9099 ctsio->kern_sg_entries = 0; 9100 9101 /* 9102 * We set this to the actual data length, regardless of how much 9103 * space we actually have to return results. If the user looks at 9104 * this value, he'll know whether or not he allocated enough space 9105 * and reissue the command if necessary. We don't support well 9106 * known logical units, so if the user asks for that, return none. 9107 */ 9108 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9109 9110 /* 9111 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9112 * this request. 9113 */ 9114 ctl_set_success(ctsio); 9115 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9116 ctsio->be_move_done = ctl_config_move_done; 9117 ctl_datamove((union ctl_io *)ctsio); 9118 return (retval); 9119} 9120 9121int 9122ctl_request_sense(struct ctl_scsiio *ctsio) 9123{ 9124 struct scsi_request_sense *cdb; 9125 struct scsi_sense_data *sense_ptr; 9126 struct ctl_softc *ctl_softc; 9127 struct ctl_lun *lun; 9128 uint32_t initidx; 9129 int have_error; 9130 scsi_sense_data_type sense_format; 9131 ctl_ua_type ua_type; 9132 9133 cdb = (struct scsi_request_sense *)ctsio->cdb; 9134 9135 ctl_softc = control_softc; 9136 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9137 9138 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9139 9140 /* 9141 * Determine which sense format the user wants. 9142 */ 9143 if (cdb->byte2 & SRS_DESC) 9144 sense_format = SSD_TYPE_DESC; 9145 else 9146 sense_format = SSD_TYPE_FIXED; 9147 9148 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9149 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9150 ctsio->kern_sg_entries = 0; 9151 9152 /* 9153 * struct scsi_sense_data, which is currently set to 256 bytes, is 9154 * larger than the largest allowed value for the length field in the 9155 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9156 */ 9157 ctsio->residual = 0; 9158 ctsio->kern_data_len = cdb->length; 9159 ctsio->kern_total_len = cdb->length; 9160 9161 ctsio->kern_data_resid = 0; 9162 ctsio->kern_rel_offset = 0; 9163 ctsio->kern_sg_entries = 0; 9164 9165 /* 9166 * If we don't have a LUN, we don't have any pending sense. 9167 */ 9168 if (lun == NULL) 9169 goto no_sense; 9170 9171 have_error = 0; 9172 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9173 /* 9174 * Check for pending sense, and then for pending unit attentions. 9175 * Pending sense gets returned first, then pending unit attentions. 9176 */ 9177 mtx_lock(&lun->lun_lock); 9178#ifdef CTL_WITH_CA 9179 if (ctl_is_set(lun->have_ca, initidx)) { 9180 scsi_sense_data_type stored_format; 9181 9182 /* 9183 * Check to see which sense format was used for the stored 9184 * sense data. 9185 */ 9186 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9187 9188 /* 9189 * If the user requested a different sense format than the 9190 * one we stored, then we need to convert it to the other 9191 * format. If we're going from descriptor to fixed format 9192 * sense data, we may lose things in translation, depending 9193 * on what options were used. 9194 * 9195 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9196 * for some reason we'll just copy it out as-is. 9197 */ 9198 if ((stored_format == SSD_TYPE_FIXED) 9199 && (sense_format == SSD_TYPE_DESC)) 9200 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9201 &lun->pending_sense[initidx], 9202 (struct scsi_sense_data_desc *)sense_ptr); 9203 else if ((stored_format == SSD_TYPE_DESC) 9204 && (sense_format == SSD_TYPE_FIXED)) 9205 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9206 &lun->pending_sense[initidx], 9207 (struct scsi_sense_data_fixed *)sense_ptr); 9208 else 9209 memcpy(sense_ptr, &lun->pending_sense[initidx], 9210 MIN(sizeof(*sense_ptr), 9211 sizeof(lun->pending_sense[initidx]))); 9212 9213 ctl_clear_mask(lun->have_ca, initidx); 9214 have_error = 1; 9215 } else 9216#endif 9217 { 9218 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9219 if (ua_type != CTL_UA_NONE) 9220 have_error = 1; 9221 if (ua_type == CTL_UA_LUN_CHANGE) { 9222 mtx_unlock(&lun->lun_lock); 9223 mtx_lock(&ctl_softc->ctl_lock); 9224 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type); 9225 mtx_unlock(&ctl_softc->ctl_lock); 9226 mtx_lock(&lun->lun_lock); 9227 } 9228 9229 } 9230 mtx_unlock(&lun->lun_lock); 9231 9232 /* 9233 * We already have a pending error, return it. 9234 */ 9235 if (have_error != 0) { 9236 /* 9237 * We report the SCSI status as OK, since the status of the 9238 * request sense command itself is OK. 9239 * We report 0 for the sense length, because we aren't doing 9240 * autosense in this case. We're reporting sense as 9241 * parameter data. 9242 */ 9243 ctl_set_success(ctsio); 9244 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9245 ctsio->be_move_done = ctl_config_move_done; 9246 ctl_datamove((union ctl_io *)ctsio); 9247 return (CTL_RETVAL_COMPLETE); 9248 } 9249 9250no_sense: 9251 9252 /* 9253 * No sense information to report, so we report that everything is 9254 * okay. 9255 */ 9256 ctl_set_sense_data(sense_ptr, 9257 lun, 9258 sense_format, 9259 /*current_error*/ 1, 9260 /*sense_key*/ SSD_KEY_NO_SENSE, 9261 /*asc*/ 0x00, 9262 /*ascq*/ 0x00, 9263 SSD_ELEM_NONE); 9264 9265 /* 9266 * We report 0 for the sense length, because we aren't doing 9267 * autosense in this case. We're reporting sense as parameter data. 9268 */ 9269 ctl_set_success(ctsio); 9270 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9271 ctsio->be_move_done = ctl_config_move_done; 9272 ctl_datamove((union ctl_io *)ctsio); 9273 return (CTL_RETVAL_COMPLETE); 9274} 9275 9276int 9277ctl_tur(struct ctl_scsiio *ctsio) 9278{ 9279 9280 CTL_DEBUG_PRINT(("ctl_tur\n")); 9281 9282 ctl_set_success(ctsio); 9283 ctl_done((union ctl_io *)ctsio); 9284 9285 return (CTL_RETVAL_COMPLETE); 9286} 9287 9288/* 9289 * SCSI VPD page 0x00, the Supported VPD Pages page. 9290 */ 9291static int 9292ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9293{ 9294 struct scsi_vpd_supported_pages *pages; 9295 int sup_page_size; 9296 struct ctl_lun *lun; 9297 int p; 9298 9299 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9300 9301 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9302 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9303 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9304 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9305 ctsio->kern_sg_entries = 0; 9306 9307 if (sup_page_size < alloc_len) { 9308 ctsio->residual = alloc_len - sup_page_size; 9309 ctsio->kern_data_len = sup_page_size; 9310 ctsio->kern_total_len = sup_page_size; 9311 } else { 9312 ctsio->residual = 0; 9313 ctsio->kern_data_len = alloc_len; 9314 ctsio->kern_total_len = alloc_len; 9315 } 9316 ctsio->kern_data_resid = 0; 9317 ctsio->kern_rel_offset = 0; 9318 ctsio->kern_sg_entries = 0; 9319 9320 /* 9321 * The control device is always connected. The disk device, on the 9322 * other hand, may not be online all the time. Need to change this 9323 * to figure out whether the disk device is actually online or not. 9324 */ 9325 if (lun != NULL) 9326 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9327 lun->be_lun->lun_type; 9328 else 9329 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9330 9331 p = 0; 9332 /* Supported VPD pages */ 9333 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9334 /* Serial Number */ 9335 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9336 /* Device Identification */ 9337 pages->page_list[p++] = SVPD_DEVICE_ID; 9338 /* Extended INQUIRY Data */ 9339 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9340 /* Mode Page Policy */ 9341 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9342 /* SCSI Ports */ 9343 pages->page_list[p++] = SVPD_SCSI_PORTS; 9344 /* Third-party Copy */ 9345 pages->page_list[p++] = SVPD_SCSI_TPC; 9346 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9347 /* Block limits */ 9348 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9349 /* Block Device Characteristics */ 9350 pages->page_list[p++] = SVPD_BDC; 9351 /* Logical Block Provisioning */ 9352 pages->page_list[p++] = SVPD_LBP; 9353 } 9354 pages->length = p; 9355 9356 ctl_set_success(ctsio); 9357 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9358 ctsio->be_move_done = ctl_config_move_done; 9359 ctl_datamove((union ctl_io *)ctsio); 9360 return (CTL_RETVAL_COMPLETE); 9361} 9362 9363/* 9364 * SCSI VPD page 0x80, the Unit Serial Number page. 9365 */ 9366static int 9367ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9368{ 9369 struct scsi_vpd_unit_serial_number *sn_ptr; 9370 struct ctl_lun *lun; 9371 int data_len; 9372 9373 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9374 9375 data_len = 4 + CTL_SN_LEN; 9376 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9377 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9378 if (data_len < alloc_len) { 9379 ctsio->residual = alloc_len - data_len; 9380 ctsio->kern_data_len = data_len; 9381 ctsio->kern_total_len = data_len; 9382 } else { 9383 ctsio->residual = 0; 9384 ctsio->kern_data_len = alloc_len; 9385 ctsio->kern_total_len = alloc_len; 9386 } 9387 ctsio->kern_data_resid = 0; 9388 ctsio->kern_rel_offset = 0; 9389 ctsio->kern_sg_entries = 0; 9390 9391 /* 9392 * The control device is always connected. The disk device, on the 9393 * other hand, may not be online all the time. Need to change this 9394 * to figure out whether the disk device is actually online or not. 9395 */ 9396 if (lun != NULL) 9397 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9398 lun->be_lun->lun_type; 9399 else 9400 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9401 9402 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9403 sn_ptr->length = CTL_SN_LEN; 9404 /* 9405 * If we don't have a LUN, we just leave the serial number as 9406 * all spaces. 9407 */ 9408 if (lun != NULL) { 9409 strncpy((char *)sn_ptr->serial_num, 9410 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9411 } else 9412 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9413 9414 ctl_set_success(ctsio); 9415 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9416 ctsio->be_move_done = ctl_config_move_done; 9417 ctl_datamove((union ctl_io *)ctsio); 9418 return (CTL_RETVAL_COMPLETE); 9419} 9420 9421 9422/* 9423 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9424 */ 9425static int 9426ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9427{ 9428 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9429 struct ctl_lun *lun; 9430 int data_len; 9431 9432 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9433 9434 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9435 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9436 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9437 ctsio->kern_sg_entries = 0; 9438 9439 if (data_len < alloc_len) { 9440 ctsio->residual = alloc_len - data_len; 9441 ctsio->kern_data_len = data_len; 9442 ctsio->kern_total_len = data_len; 9443 } else { 9444 ctsio->residual = 0; 9445 ctsio->kern_data_len = alloc_len; 9446 ctsio->kern_total_len = alloc_len; 9447 } 9448 ctsio->kern_data_resid = 0; 9449 ctsio->kern_rel_offset = 0; 9450 ctsio->kern_sg_entries = 0; 9451 9452 /* 9453 * The control device is always connected. The disk device, on the 9454 * other hand, may not be online all the time. 9455 */ 9456 if (lun != NULL) 9457 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9458 lun->be_lun->lun_type; 9459 else 9460 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9461 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9462 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9463 /* 9464 * We support head of queue, ordered and simple tags. 9465 */ 9466 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9467 /* 9468 * Volatile cache supported. 9469 */ 9470 eid_ptr->flags3 = SVPD_EID_V_SUP; 9471 9472 /* 9473 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9474 * attention for a particular IT nexus on all LUNs once we report 9475 * it to that nexus once. This bit is required as of SPC-4. 9476 */ 9477 eid_ptr->flags4 = SVPD_EID_LUICLT; 9478 9479 /* 9480 * XXX KDM in order to correctly answer this, we would need 9481 * information from the SIM to determine how much sense data it 9482 * can send. So this would really be a path inquiry field, most 9483 * likely. This can be set to a maximum of 252 according to SPC-4, 9484 * but the hardware may or may not be able to support that much. 9485 * 0 just means that the maximum sense data length is not reported. 9486 */ 9487 eid_ptr->max_sense_length = 0; 9488 9489 ctl_set_success(ctsio); 9490 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9491 ctsio->be_move_done = ctl_config_move_done; 9492 ctl_datamove((union ctl_io *)ctsio); 9493 return (CTL_RETVAL_COMPLETE); 9494} 9495 9496static int 9497ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9498{ 9499 struct scsi_vpd_mode_page_policy *mpp_ptr; 9500 struct ctl_lun *lun; 9501 int data_len; 9502 9503 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9504 9505 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9506 sizeof(struct scsi_vpd_mode_page_policy_descr); 9507 9508 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9509 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9510 ctsio->kern_sg_entries = 0; 9511 9512 if (data_len < alloc_len) { 9513 ctsio->residual = alloc_len - data_len; 9514 ctsio->kern_data_len = data_len; 9515 ctsio->kern_total_len = data_len; 9516 } else { 9517 ctsio->residual = 0; 9518 ctsio->kern_data_len = alloc_len; 9519 ctsio->kern_total_len = alloc_len; 9520 } 9521 ctsio->kern_data_resid = 0; 9522 ctsio->kern_rel_offset = 0; 9523 ctsio->kern_sg_entries = 0; 9524 9525 /* 9526 * The control device is always connected. The disk device, on the 9527 * other hand, may not be online all the time. 9528 */ 9529 if (lun != NULL) 9530 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9531 lun->be_lun->lun_type; 9532 else 9533 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9534 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9535 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9536 mpp_ptr->descr[0].page_code = 0x3f; 9537 mpp_ptr->descr[0].subpage_code = 0xff; 9538 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9539 9540 ctl_set_success(ctsio); 9541 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9542 ctsio->be_move_done = ctl_config_move_done; 9543 ctl_datamove((union ctl_io *)ctsio); 9544 return (CTL_RETVAL_COMPLETE); 9545} 9546 9547/* 9548 * SCSI VPD page 0x83, the Device Identification page. 9549 */ 9550static int 9551ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9552{ 9553 struct scsi_vpd_device_id *devid_ptr; 9554 struct scsi_vpd_id_descriptor *desc; 9555 struct ctl_softc *softc; 9556 struct ctl_lun *lun; 9557 struct ctl_port *port; 9558 int data_len; 9559 uint8_t proto; 9560 9561 softc = control_softc; 9562 9563 port = ctl_io_port(&ctsio->io_hdr); 9564 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9565 9566 data_len = sizeof(struct scsi_vpd_device_id) + 9567 sizeof(struct scsi_vpd_id_descriptor) + 9568 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9569 sizeof(struct scsi_vpd_id_descriptor) + 9570 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9571 if (lun && lun->lun_devid) 9572 data_len += lun->lun_devid->len; 9573 if (port && port->port_devid) 9574 data_len += port->port_devid->len; 9575 if (port && port->target_devid) 9576 data_len += port->target_devid->len; 9577 9578 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9579 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9580 ctsio->kern_sg_entries = 0; 9581 9582 if (data_len < alloc_len) { 9583 ctsio->residual = alloc_len - data_len; 9584 ctsio->kern_data_len = data_len; 9585 ctsio->kern_total_len = data_len; 9586 } else { 9587 ctsio->residual = 0; 9588 ctsio->kern_data_len = alloc_len; 9589 ctsio->kern_total_len = alloc_len; 9590 } 9591 ctsio->kern_data_resid = 0; 9592 ctsio->kern_rel_offset = 0; 9593 ctsio->kern_sg_entries = 0; 9594 9595 /* 9596 * The control device is always connected. The disk device, on the 9597 * other hand, may not be online all the time. 9598 */ 9599 if (lun != NULL) 9600 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9601 lun->be_lun->lun_type; 9602 else 9603 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9604 devid_ptr->page_code = SVPD_DEVICE_ID; 9605 scsi_ulto2b(data_len - 4, devid_ptr->length); 9606 9607 if (port && port->port_type == CTL_PORT_FC) 9608 proto = SCSI_PROTO_FC << 4; 9609 else if (port && port->port_type == CTL_PORT_ISCSI) 9610 proto = SCSI_PROTO_ISCSI << 4; 9611 else 9612 proto = SCSI_PROTO_SPI << 4; 9613 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9614 9615 /* 9616 * We're using a LUN association here. i.e., this device ID is a 9617 * per-LUN identifier. 9618 */ 9619 if (lun && lun->lun_devid) { 9620 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9621 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9622 lun->lun_devid->len); 9623 } 9624 9625 /* 9626 * This is for the WWPN which is a port association. 9627 */ 9628 if (port && port->port_devid) { 9629 memcpy(desc, port->port_devid->data, port->port_devid->len); 9630 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9631 port->port_devid->len); 9632 } 9633 9634 /* 9635 * This is for the Relative Target Port(type 4h) identifier 9636 */ 9637 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9638 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9639 SVPD_ID_TYPE_RELTARG; 9640 desc->length = 4; 9641 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9642 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9643 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9644 9645 /* 9646 * This is for the Target Port Group(type 5h) identifier 9647 */ 9648 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9649 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9650 SVPD_ID_TYPE_TPORTGRP; 9651 desc->length = 4; 9652 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1, 9653 &desc->identifier[2]); 9654 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9655 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9656 9657 /* 9658 * This is for the Target identifier 9659 */ 9660 if (port && port->target_devid) { 9661 memcpy(desc, port->target_devid->data, port->target_devid->len); 9662 } 9663 9664 ctl_set_success(ctsio); 9665 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9666 ctsio->be_move_done = ctl_config_move_done; 9667 ctl_datamove((union ctl_io *)ctsio); 9668 return (CTL_RETVAL_COMPLETE); 9669} 9670 9671static int 9672ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9673{ 9674 struct ctl_softc *softc = control_softc; 9675 struct scsi_vpd_scsi_ports *sp; 9676 struct scsi_vpd_port_designation *pd; 9677 struct scsi_vpd_port_designation_cont *pdc; 9678 struct ctl_lun *lun; 9679 struct ctl_port *port; 9680 int data_len, num_target_ports, iid_len, id_len; 9681 9682 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9683 9684 num_target_ports = 0; 9685 iid_len = 0; 9686 id_len = 0; 9687 mtx_lock(&softc->ctl_lock); 9688 STAILQ_FOREACH(port, &softc->port_list, links) { 9689 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9690 continue; 9691 if (lun != NULL && 9692 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9693 continue; 9694 num_target_ports++; 9695 if (port->init_devid) 9696 iid_len += port->init_devid->len; 9697 if (port->port_devid) 9698 id_len += port->port_devid->len; 9699 } 9700 mtx_unlock(&softc->ctl_lock); 9701 9702 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9703 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9704 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9705 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9706 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9707 ctsio->kern_sg_entries = 0; 9708 9709 if (data_len < alloc_len) { 9710 ctsio->residual = alloc_len - data_len; 9711 ctsio->kern_data_len = data_len; 9712 ctsio->kern_total_len = data_len; 9713 } else { 9714 ctsio->residual = 0; 9715 ctsio->kern_data_len = alloc_len; 9716 ctsio->kern_total_len = alloc_len; 9717 } 9718 ctsio->kern_data_resid = 0; 9719 ctsio->kern_rel_offset = 0; 9720 ctsio->kern_sg_entries = 0; 9721 9722 /* 9723 * The control device is always connected. The disk device, on the 9724 * other hand, may not be online all the time. Need to change this 9725 * to figure out whether the disk device is actually online or not. 9726 */ 9727 if (lun != NULL) 9728 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9729 lun->be_lun->lun_type; 9730 else 9731 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9732 9733 sp->page_code = SVPD_SCSI_PORTS; 9734 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9735 sp->page_length); 9736 pd = &sp->design[0]; 9737 9738 mtx_lock(&softc->ctl_lock); 9739 STAILQ_FOREACH(port, &softc->port_list, links) { 9740 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9741 continue; 9742 if (lun != NULL && 9743 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9744 continue; 9745 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9746 if (port->init_devid) { 9747 iid_len = port->init_devid->len; 9748 memcpy(pd->initiator_transportid, 9749 port->init_devid->data, port->init_devid->len); 9750 } else 9751 iid_len = 0; 9752 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9753 pdc = (struct scsi_vpd_port_designation_cont *) 9754 (&pd->initiator_transportid[iid_len]); 9755 if (port->port_devid) { 9756 id_len = port->port_devid->len; 9757 memcpy(pdc->target_port_descriptors, 9758 port->port_devid->data, port->port_devid->len); 9759 } else 9760 id_len = 0; 9761 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9762 pd = (struct scsi_vpd_port_designation *) 9763 ((uint8_t *)pdc->target_port_descriptors + id_len); 9764 } 9765 mtx_unlock(&softc->ctl_lock); 9766 9767 ctl_set_success(ctsio); 9768 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9769 ctsio->be_move_done = ctl_config_move_done; 9770 ctl_datamove((union ctl_io *)ctsio); 9771 return (CTL_RETVAL_COMPLETE); 9772} 9773 9774static int 9775ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9776{ 9777 struct scsi_vpd_block_limits *bl_ptr; 9778 struct ctl_lun *lun; 9779 int bs; 9780 9781 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9782 9783 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9784 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9785 ctsio->kern_sg_entries = 0; 9786 9787 if (sizeof(*bl_ptr) < alloc_len) { 9788 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9789 ctsio->kern_data_len = sizeof(*bl_ptr); 9790 ctsio->kern_total_len = sizeof(*bl_ptr); 9791 } else { 9792 ctsio->residual = 0; 9793 ctsio->kern_data_len = alloc_len; 9794 ctsio->kern_total_len = alloc_len; 9795 } 9796 ctsio->kern_data_resid = 0; 9797 ctsio->kern_rel_offset = 0; 9798 ctsio->kern_sg_entries = 0; 9799 9800 /* 9801 * The control device is always connected. The disk device, on the 9802 * other hand, may not be online all the time. Need to change this 9803 * to figure out whether the disk device is actually online or not. 9804 */ 9805 if (lun != NULL) 9806 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9807 lun->be_lun->lun_type; 9808 else 9809 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9810 9811 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9812 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9813 bl_ptr->max_cmp_write_len = 0xff; 9814 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9815 if (lun != NULL) { 9816 bs = lun->be_lun->blocksize; 9817 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9818 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9819 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 9820 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 9821 if (lun->be_lun->ublockexp != 0) { 9822 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9823 bl_ptr->opt_unmap_grain); 9824 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9825 bl_ptr->unmap_grain_align); 9826 } 9827 } 9828 scsi_ulto4b(lun->be_lun->atomicblock, 9829 bl_ptr->max_atomic_transfer_length); 9830 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9831 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9832 } 9833 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 9834 9835 ctl_set_success(ctsio); 9836 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9837 ctsio->be_move_done = ctl_config_move_done; 9838 ctl_datamove((union ctl_io *)ctsio); 9839 return (CTL_RETVAL_COMPLETE); 9840} 9841 9842static int 9843ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9844{ 9845 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9846 struct ctl_lun *lun; 9847 const char *value; 9848 u_int i; 9849 9850 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9851 9852 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9853 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9854 ctsio->kern_sg_entries = 0; 9855 9856 if (sizeof(*bdc_ptr) < alloc_len) { 9857 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9858 ctsio->kern_data_len = sizeof(*bdc_ptr); 9859 ctsio->kern_total_len = sizeof(*bdc_ptr); 9860 } else { 9861 ctsio->residual = 0; 9862 ctsio->kern_data_len = alloc_len; 9863 ctsio->kern_total_len = alloc_len; 9864 } 9865 ctsio->kern_data_resid = 0; 9866 ctsio->kern_rel_offset = 0; 9867 ctsio->kern_sg_entries = 0; 9868 9869 /* 9870 * The control device is always connected. The disk device, on the 9871 * other hand, may not be online all the time. Need to change this 9872 * to figure out whether the disk device is actually online or not. 9873 */ 9874 if (lun != NULL) 9875 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9876 lun->be_lun->lun_type; 9877 else 9878 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9879 bdc_ptr->page_code = SVPD_BDC; 9880 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9881 if (lun != NULL && 9882 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9883 i = strtol(value, NULL, 0); 9884 else 9885 i = CTL_DEFAULT_ROTATION_RATE; 9886 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9887 if (lun != NULL && 9888 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9889 i = strtol(value, NULL, 0); 9890 else 9891 i = 0; 9892 bdc_ptr->wab_wac_ff = (i & 0x0f); 9893 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9894 9895 ctl_set_success(ctsio); 9896 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9897 ctsio->be_move_done = ctl_config_move_done; 9898 ctl_datamove((union ctl_io *)ctsio); 9899 return (CTL_RETVAL_COMPLETE); 9900} 9901 9902static int 9903ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9904{ 9905 struct scsi_vpd_logical_block_prov *lbp_ptr; 9906 struct ctl_lun *lun; 9907 9908 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9909 9910 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9911 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9912 ctsio->kern_sg_entries = 0; 9913 9914 if (sizeof(*lbp_ptr) < alloc_len) { 9915 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9916 ctsio->kern_data_len = sizeof(*lbp_ptr); 9917 ctsio->kern_total_len = sizeof(*lbp_ptr); 9918 } else { 9919 ctsio->residual = 0; 9920 ctsio->kern_data_len = alloc_len; 9921 ctsio->kern_total_len = alloc_len; 9922 } 9923 ctsio->kern_data_resid = 0; 9924 ctsio->kern_rel_offset = 0; 9925 ctsio->kern_sg_entries = 0; 9926 9927 /* 9928 * The control device is always connected. The disk device, on the 9929 * other hand, may not be online all the time. Need to change this 9930 * to figure out whether the disk device is actually online or not. 9931 */ 9932 if (lun != NULL) 9933 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9934 lun->be_lun->lun_type; 9935 else 9936 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9937 9938 lbp_ptr->page_code = SVPD_LBP; 9939 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9940 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9941 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9942 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9943 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9944 lbp_ptr->prov_type = SVPD_LBP_THIN; 9945 } 9946 9947 ctl_set_success(ctsio); 9948 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9949 ctsio->be_move_done = ctl_config_move_done; 9950 ctl_datamove((union ctl_io *)ctsio); 9951 return (CTL_RETVAL_COMPLETE); 9952} 9953 9954/* 9955 * INQUIRY with the EVPD bit set. 9956 */ 9957static int 9958ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9959{ 9960 struct ctl_lun *lun; 9961 struct scsi_inquiry *cdb; 9962 int alloc_len, retval; 9963 9964 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9965 cdb = (struct scsi_inquiry *)ctsio->cdb; 9966 alloc_len = scsi_2btoul(cdb->length); 9967 9968 switch (cdb->page_code) { 9969 case SVPD_SUPPORTED_PAGES: 9970 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9971 break; 9972 case SVPD_UNIT_SERIAL_NUMBER: 9973 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9974 break; 9975 case SVPD_DEVICE_ID: 9976 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9977 break; 9978 case SVPD_EXTENDED_INQUIRY_DATA: 9979 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 9980 break; 9981 case SVPD_MODE_PAGE_POLICY: 9982 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 9983 break; 9984 case SVPD_SCSI_PORTS: 9985 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 9986 break; 9987 case SVPD_SCSI_TPC: 9988 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 9989 break; 9990 case SVPD_BLOCK_LIMITS: 9991 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9992 goto err; 9993 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 9994 break; 9995 case SVPD_BDC: 9996 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9997 goto err; 9998 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 9999 break; 10000 case SVPD_LBP: 10001 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10002 goto err; 10003 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10004 break; 10005 default: 10006err: 10007 ctl_set_invalid_field(ctsio, 10008 /*sks_valid*/ 1, 10009 /*command*/ 1, 10010 /*field*/ 2, 10011 /*bit_valid*/ 0, 10012 /*bit*/ 0); 10013 ctl_done((union ctl_io *)ctsio); 10014 retval = CTL_RETVAL_COMPLETE; 10015 break; 10016 } 10017 10018 return (retval); 10019} 10020 10021/* 10022 * Standard INQUIRY data. 10023 */ 10024static int 10025ctl_inquiry_std(struct ctl_scsiio *ctsio) 10026{ 10027 struct scsi_inquiry_data *inq_ptr; 10028 struct scsi_inquiry *cdb; 10029 struct ctl_softc *softc; 10030 struct ctl_port *port; 10031 struct ctl_lun *lun; 10032 char *val; 10033 uint32_t alloc_len, data_len; 10034 ctl_port_type port_type; 10035 10036 softc = control_softc; 10037 10038 /* 10039 * Figure out whether we're talking to a Fibre Channel port or not. 10040 * We treat the ioctl front end, and any SCSI adapters, as packetized 10041 * SCSI front ends. 10042 */ 10043 port = ctl_io_port(&ctsio->io_hdr); 10044 if (port != NULL) 10045 port_type = port->port_type; 10046 else 10047 port_type = CTL_PORT_SCSI; 10048 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10049 port_type = CTL_PORT_SCSI; 10050 10051 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10052 cdb = (struct scsi_inquiry *)ctsio->cdb; 10053 alloc_len = scsi_2btoul(cdb->length); 10054 10055 /* 10056 * We malloc the full inquiry data size here and fill it 10057 * in. If the user only asks for less, we'll give him 10058 * that much. 10059 */ 10060 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10061 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10062 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10063 ctsio->kern_sg_entries = 0; 10064 ctsio->kern_data_resid = 0; 10065 ctsio->kern_rel_offset = 0; 10066 10067 if (data_len < alloc_len) { 10068 ctsio->residual = alloc_len - data_len; 10069 ctsio->kern_data_len = data_len; 10070 ctsio->kern_total_len = data_len; 10071 } else { 10072 ctsio->residual = 0; 10073 ctsio->kern_data_len = alloc_len; 10074 ctsio->kern_total_len = alloc_len; 10075 } 10076 10077 if (lun != NULL) { 10078 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10079 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10080 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10081 lun->be_lun->lun_type; 10082 } else { 10083 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10084 lun->be_lun->lun_type; 10085 } 10086 } else 10087 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10088 10089 /* RMB in byte 2 is 0 */ 10090 inq_ptr->version = SCSI_REV_SPC4; 10091 10092 /* 10093 * According to SAM-3, even if a device only supports a single 10094 * level of LUN addressing, it should still set the HISUP bit: 10095 * 10096 * 4.9.1 Logical unit numbers overview 10097 * 10098 * All logical unit number formats described in this standard are 10099 * hierarchical in structure even when only a single level in that 10100 * hierarchy is used. The HISUP bit shall be set to one in the 10101 * standard INQUIRY data (see SPC-2) when any logical unit number 10102 * format described in this standard is used. Non-hierarchical 10103 * formats are outside the scope of this standard. 10104 * 10105 * Therefore we set the HiSup bit here. 10106 * 10107 * The reponse format is 2, per SPC-3. 10108 */ 10109 inq_ptr->response_format = SID_HiSup | 2; 10110 10111 inq_ptr->additional_length = data_len - 10112 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10113 CTL_DEBUG_PRINT(("additional_length = %d\n", 10114 inq_ptr->additional_length)); 10115 10116 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10117 /* 16 bit addressing */ 10118 if (port_type == CTL_PORT_SCSI) 10119 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10120 /* XXX set the SID_MultiP bit here if we're actually going to 10121 respond on multiple ports */ 10122 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10123 10124 /* 16 bit data bus, synchronous transfers */ 10125 if (port_type == CTL_PORT_SCSI) 10126 inq_ptr->flags = SID_WBus16 | SID_Sync; 10127 /* 10128 * XXX KDM do we want to support tagged queueing on the control 10129 * device at all? 10130 */ 10131 if ((lun == NULL) 10132 || (lun->be_lun->lun_type != T_PROCESSOR)) 10133 inq_ptr->flags |= SID_CmdQue; 10134 /* 10135 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10136 * We have 8 bytes for the vendor name, and 16 bytes for the device 10137 * name and 4 bytes for the revision. 10138 */ 10139 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10140 "vendor")) == NULL) { 10141 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10142 } else { 10143 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10144 strncpy(inq_ptr->vendor, val, 10145 min(sizeof(inq_ptr->vendor), strlen(val))); 10146 } 10147 if (lun == NULL) { 10148 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10149 sizeof(inq_ptr->product)); 10150 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10151 switch (lun->be_lun->lun_type) { 10152 case T_DIRECT: 10153 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10154 sizeof(inq_ptr->product)); 10155 break; 10156 case T_PROCESSOR: 10157 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10158 sizeof(inq_ptr->product)); 10159 break; 10160 default: 10161 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10162 sizeof(inq_ptr->product)); 10163 break; 10164 } 10165 } else { 10166 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10167 strncpy(inq_ptr->product, val, 10168 min(sizeof(inq_ptr->product), strlen(val))); 10169 } 10170 10171 /* 10172 * XXX make this a macro somewhere so it automatically gets 10173 * incremented when we make changes. 10174 */ 10175 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10176 "revision")) == NULL) { 10177 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10178 } else { 10179 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10180 strncpy(inq_ptr->revision, val, 10181 min(sizeof(inq_ptr->revision), strlen(val))); 10182 } 10183 10184 /* 10185 * For parallel SCSI, we support double transition and single 10186 * transition clocking. We also support QAS (Quick Arbitration 10187 * and Selection) and Information Unit transfers on both the 10188 * control and array devices. 10189 */ 10190 if (port_type == CTL_PORT_SCSI) 10191 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10192 SID_SPI_IUS; 10193 10194 /* SAM-5 (no version claimed) */ 10195 scsi_ulto2b(0x00A0, inq_ptr->version1); 10196 /* SPC-4 (no version claimed) */ 10197 scsi_ulto2b(0x0460, inq_ptr->version2); 10198 if (port_type == CTL_PORT_FC) { 10199 /* FCP-2 ANSI INCITS.350:2003 */ 10200 scsi_ulto2b(0x0917, inq_ptr->version3); 10201 } else if (port_type == CTL_PORT_SCSI) { 10202 /* SPI-4 ANSI INCITS.362:200x */ 10203 scsi_ulto2b(0x0B56, inq_ptr->version3); 10204 } else if (port_type == CTL_PORT_ISCSI) { 10205 /* iSCSI (no version claimed) */ 10206 scsi_ulto2b(0x0960, inq_ptr->version3); 10207 } else if (port_type == CTL_PORT_SAS) { 10208 /* SAS (no version claimed) */ 10209 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10210 } 10211 10212 if (lun == NULL) { 10213 /* SBC-4 (no version claimed) */ 10214 scsi_ulto2b(0x0600, inq_ptr->version4); 10215 } else { 10216 switch (lun->be_lun->lun_type) { 10217 case T_DIRECT: 10218 /* SBC-4 (no version claimed) */ 10219 scsi_ulto2b(0x0600, inq_ptr->version4); 10220 break; 10221 case T_PROCESSOR: 10222 default: 10223 break; 10224 } 10225 } 10226 10227 ctl_set_success(ctsio); 10228 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10229 ctsio->be_move_done = ctl_config_move_done; 10230 ctl_datamove((union ctl_io *)ctsio); 10231 return (CTL_RETVAL_COMPLETE); 10232} 10233 10234int 10235ctl_inquiry(struct ctl_scsiio *ctsio) 10236{ 10237 struct scsi_inquiry *cdb; 10238 int retval; 10239 10240 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10241 10242 cdb = (struct scsi_inquiry *)ctsio->cdb; 10243 if (cdb->byte2 & SI_EVPD) 10244 retval = ctl_inquiry_evpd(ctsio); 10245 else if (cdb->page_code == 0) 10246 retval = ctl_inquiry_std(ctsio); 10247 else { 10248 ctl_set_invalid_field(ctsio, 10249 /*sks_valid*/ 1, 10250 /*command*/ 1, 10251 /*field*/ 2, 10252 /*bit_valid*/ 0, 10253 /*bit*/ 0); 10254 ctl_done((union ctl_io *)ctsio); 10255 return (CTL_RETVAL_COMPLETE); 10256 } 10257 10258 return (retval); 10259} 10260 10261/* 10262 * For known CDB types, parse the LBA and length. 10263 */ 10264static int 10265ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10266{ 10267 if (io->io_hdr.io_type != CTL_IO_SCSI) 10268 return (1); 10269 10270 switch (io->scsiio.cdb[0]) { 10271 case COMPARE_AND_WRITE: { 10272 struct scsi_compare_and_write *cdb; 10273 10274 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10275 10276 *lba = scsi_8btou64(cdb->addr); 10277 *len = cdb->length; 10278 break; 10279 } 10280 case READ_6: 10281 case WRITE_6: { 10282 struct scsi_rw_6 *cdb; 10283 10284 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10285 10286 *lba = scsi_3btoul(cdb->addr); 10287 /* only 5 bits are valid in the most significant address byte */ 10288 *lba &= 0x1fffff; 10289 *len = cdb->length; 10290 break; 10291 } 10292 case READ_10: 10293 case WRITE_10: { 10294 struct scsi_rw_10 *cdb; 10295 10296 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10297 10298 *lba = scsi_4btoul(cdb->addr); 10299 *len = scsi_2btoul(cdb->length); 10300 break; 10301 } 10302 case WRITE_VERIFY_10: { 10303 struct scsi_write_verify_10 *cdb; 10304 10305 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10306 10307 *lba = scsi_4btoul(cdb->addr); 10308 *len = scsi_2btoul(cdb->length); 10309 break; 10310 } 10311 case READ_12: 10312 case WRITE_12: { 10313 struct scsi_rw_12 *cdb; 10314 10315 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10316 10317 *lba = scsi_4btoul(cdb->addr); 10318 *len = scsi_4btoul(cdb->length); 10319 break; 10320 } 10321 case WRITE_VERIFY_12: { 10322 struct scsi_write_verify_12 *cdb; 10323 10324 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10325 10326 *lba = scsi_4btoul(cdb->addr); 10327 *len = scsi_4btoul(cdb->length); 10328 break; 10329 } 10330 case READ_16: 10331 case WRITE_16: 10332 case WRITE_ATOMIC_16: { 10333 struct scsi_rw_16 *cdb; 10334 10335 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10336 10337 *lba = scsi_8btou64(cdb->addr); 10338 *len = scsi_4btoul(cdb->length); 10339 break; 10340 } 10341 case WRITE_VERIFY_16: { 10342 struct scsi_write_verify_16 *cdb; 10343 10344 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10345 10346 *lba = scsi_8btou64(cdb->addr); 10347 *len = scsi_4btoul(cdb->length); 10348 break; 10349 } 10350 case WRITE_SAME_10: { 10351 struct scsi_write_same_10 *cdb; 10352 10353 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10354 10355 *lba = scsi_4btoul(cdb->addr); 10356 *len = scsi_2btoul(cdb->length); 10357 break; 10358 } 10359 case WRITE_SAME_16: { 10360 struct scsi_write_same_16 *cdb; 10361 10362 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10363 10364 *lba = scsi_8btou64(cdb->addr); 10365 *len = scsi_4btoul(cdb->length); 10366 break; 10367 } 10368 case VERIFY_10: { 10369 struct scsi_verify_10 *cdb; 10370 10371 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10372 10373 *lba = scsi_4btoul(cdb->addr); 10374 *len = scsi_2btoul(cdb->length); 10375 break; 10376 } 10377 case VERIFY_12: { 10378 struct scsi_verify_12 *cdb; 10379 10380 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10381 10382 *lba = scsi_4btoul(cdb->addr); 10383 *len = scsi_4btoul(cdb->length); 10384 break; 10385 } 10386 case VERIFY_16: { 10387 struct scsi_verify_16 *cdb; 10388 10389 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10390 10391 *lba = scsi_8btou64(cdb->addr); 10392 *len = scsi_4btoul(cdb->length); 10393 break; 10394 } 10395 case UNMAP: { 10396 *lba = 0; 10397 *len = UINT64_MAX; 10398 break; 10399 } 10400 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10401 struct scsi_get_lba_status *cdb; 10402 10403 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10404 *lba = scsi_8btou64(cdb->addr); 10405 *len = UINT32_MAX; 10406 break; 10407 } 10408 default: 10409 return (1); 10410 break; /* NOTREACHED */ 10411 } 10412 10413 return (0); 10414} 10415 10416static ctl_action 10417ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10418 bool seq) 10419{ 10420 uint64_t endlba1, endlba2; 10421 10422 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10423 endlba2 = lba2 + len2 - 1; 10424 10425 if ((endlba1 < lba2) || (endlba2 < lba1)) 10426 return (CTL_ACTION_PASS); 10427 else 10428 return (CTL_ACTION_BLOCK); 10429} 10430 10431static int 10432ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10433{ 10434 struct ctl_ptr_len_flags *ptrlen; 10435 struct scsi_unmap_desc *buf, *end, *range; 10436 uint64_t lba; 10437 uint32_t len; 10438 10439 /* If not UNMAP -- go other way. */ 10440 if (io->io_hdr.io_type != CTL_IO_SCSI || 10441 io->scsiio.cdb[0] != UNMAP) 10442 return (CTL_ACTION_ERROR); 10443 10444 /* If UNMAP without data -- block and wait for data. */ 10445 ptrlen = (struct ctl_ptr_len_flags *) 10446 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10447 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10448 ptrlen->ptr == NULL) 10449 return (CTL_ACTION_BLOCK); 10450 10451 /* UNMAP with data -- check for collision. */ 10452 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10453 end = buf + ptrlen->len / sizeof(*buf); 10454 for (range = buf; range < end; range++) { 10455 lba = scsi_8btou64(range->lba); 10456 len = scsi_4btoul(range->length); 10457 if ((lba < lba2 + len2) && (lba + len > lba2)) 10458 return (CTL_ACTION_BLOCK); 10459 } 10460 return (CTL_ACTION_PASS); 10461} 10462 10463static ctl_action 10464ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10465{ 10466 uint64_t lba1, lba2; 10467 uint64_t len1, len2; 10468 int retval; 10469 10470 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10471 return (CTL_ACTION_ERROR); 10472 10473 retval = ctl_extent_check_unmap(io1, lba2, len2); 10474 if (retval != CTL_ACTION_ERROR) 10475 return (retval); 10476 10477 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10478 return (CTL_ACTION_ERROR); 10479 10480 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10481} 10482 10483static ctl_action 10484ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10485{ 10486 uint64_t lba1, lba2; 10487 uint64_t len1, len2; 10488 10489 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10490 return (CTL_ACTION_ERROR); 10491 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10492 return (CTL_ACTION_ERROR); 10493 10494 if (lba1 + len1 == lba2) 10495 return (CTL_ACTION_BLOCK); 10496 return (CTL_ACTION_PASS); 10497} 10498 10499static ctl_action 10500ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10501 union ctl_io *ooa_io) 10502{ 10503 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10504 ctl_serialize_action *serialize_row; 10505 10506 /* 10507 * The initiator attempted multiple untagged commands at the same 10508 * time. Can't do that. 10509 */ 10510 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10511 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10512 && ((pending_io->io_hdr.nexus.targ_port == 10513 ooa_io->io_hdr.nexus.targ_port) 10514 && (pending_io->io_hdr.nexus.initid == 10515 ooa_io->io_hdr.nexus.initid)) 10516 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10517 CTL_FLAG_STATUS_SENT)) == 0)) 10518 return (CTL_ACTION_OVERLAP); 10519 10520 /* 10521 * The initiator attempted to send multiple tagged commands with 10522 * the same ID. (It's fine if different initiators have the same 10523 * tag ID.) 10524 * 10525 * Even if all of those conditions are true, we don't kill the I/O 10526 * if the command ahead of us has been aborted. We won't end up 10527 * sending it to the FETD, and it's perfectly legal to resend a 10528 * command with the same tag number as long as the previous 10529 * instance of this tag number has been aborted somehow. 10530 */ 10531 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10532 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10533 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10534 && ((pending_io->io_hdr.nexus.targ_port == 10535 ooa_io->io_hdr.nexus.targ_port) 10536 && (pending_io->io_hdr.nexus.initid == 10537 ooa_io->io_hdr.nexus.initid)) 10538 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10539 CTL_FLAG_STATUS_SENT)) == 0)) 10540 return (CTL_ACTION_OVERLAP_TAG); 10541 10542 /* 10543 * If we get a head of queue tag, SAM-3 says that we should 10544 * immediately execute it. 10545 * 10546 * What happens if this command would normally block for some other 10547 * reason? e.g. a request sense with a head of queue tag 10548 * immediately after a write. Normally that would block, but this 10549 * will result in its getting executed immediately... 10550 * 10551 * We currently return "pass" instead of "skip", so we'll end up 10552 * going through the rest of the queue to check for overlapped tags. 10553 * 10554 * XXX KDM check for other types of blockage first?? 10555 */ 10556 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10557 return (CTL_ACTION_PASS); 10558 10559 /* 10560 * Ordered tags have to block until all items ahead of them 10561 * have completed. If we get called with an ordered tag, we always 10562 * block, if something else is ahead of us in the queue. 10563 */ 10564 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10565 return (CTL_ACTION_BLOCK); 10566 10567 /* 10568 * Simple tags get blocked until all head of queue and ordered tags 10569 * ahead of them have completed. I'm lumping untagged commands in 10570 * with simple tags here. XXX KDM is that the right thing to do? 10571 */ 10572 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10573 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10574 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10575 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10576 return (CTL_ACTION_BLOCK); 10577 10578 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10579 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10580 10581 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10582 10583 switch (serialize_row[pending_entry->seridx]) { 10584 case CTL_SER_BLOCK: 10585 return (CTL_ACTION_BLOCK); 10586 case CTL_SER_EXTENT: 10587 return (ctl_extent_check(ooa_io, pending_io, 10588 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10589 case CTL_SER_EXTENTOPT: 10590 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10591 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10592 return (ctl_extent_check(ooa_io, pending_io, 10593 (lun->be_lun && 10594 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10595 return (CTL_ACTION_PASS); 10596 case CTL_SER_EXTENTSEQ: 10597 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 10598 return (ctl_extent_check_seq(ooa_io, pending_io)); 10599 return (CTL_ACTION_PASS); 10600 case CTL_SER_PASS: 10601 return (CTL_ACTION_PASS); 10602 case CTL_SER_BLOCKOPT: 10603 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10604 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10605 return (CTL_ACTION_BLOCK); 10606 return (CTL_ACTION_PASS); 10607 case CTL_SER_SKIP: 10608 return (CTL_ACTION_SKIP); 10609 default: 10610 panic("invalid serialization value %d", 10611 serialize_row[pending_entry->seridx]); 10612 } 10613 10614 return (CTL_ACTION_ERROR); 10615} 10616 10617/* 10618 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10619 * Assumptions: 10620 * - pending_io is generally either incoming, or on the blocked queue 10621 * - starting I/O is the I/O we want to start the check with. 10622 */ 10623static ctl_action 10624ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10625 union ctl_io *starting_io) 10626{ 10627 union ctl_io *ooa_io; 10628 ctl_action action; 10629 10630 mtx_assert(&lun->lun_lock, MA_OWNED); 10631 10632 /* 10633 * Run back along the OOA queue, starting with the current 10634 * blocked I/O and going through every I/O before it on the 10635 * queue. If starting_io is NULL, we'll just end up returning 10636 * CTL_ACTION_PASS. 10637 */ 10638 for (ooa_io = starting_io; ooa_io != NULL; 10639 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10640 ooa_links)){ 10641 10642 /* 10643 * This routine just checks to see whether 10644 * cur_blocked is blocked by ooa_io, which is ahead 10645 * of it in the queue. It doesn't queue/dequeue 10646 * cur_blocked. 10647 */ 10648 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10649 switch (action) { 10650 case CTL_ACTION_BLOCK: 10651 case CTL_ACTION_OVERLAP: 10652 case CTL_ACTION_OVERLAP_TAG: 10653 case CTL_ACTION_SKIP: 10654 case CTL_ACTION_ERROR: 10655 return (action); 10656 break; /* NOTREACHED */ 10657 case CTL_ACTION_PASS: 10658 break; 10659 default: 10660 panic("invalid action %d", action); 10661 break; /* NOTREACHED */ 10662 } 10663 } 10664 10665 return (CTL_ACTION_PASS); 10666} 10667 10668/* 10669 * Assumptions: 10670 * - An I/O has just completed, and has been removed from the per-LUN OOA 10671 * queue, so some items on the blocked queue may now be unblocked. 10672 */ 10673static int 10674ctl_check_blocked(struct ctl_lun *lun) 10675{ 10676 struct ctl_softc *softc = lun->ctl_softc; 10677 union ctl_io *cur_blocked, *next_blocked; 10678 10679 mtx_assert(&lun->lun_lock, MA_OWNED); 10680 10681 /* 10682 * Run forward from the head of the blocked queue, checking each 10683 * entry against the I/Os prior to it on the OOA queue to see if 10684 * there is still any blockage. 10685 * 10686 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10687 * with our removing a variable on it while it is traversing the 10688 * list. 10689 */ 10690 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10691 cur_blocked != NULL; cur_blocked = next_blocked) { 10692 union ctl_io *prev_ooa; 10693 ctl_action action; 10694 10695 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10696 blocked_links); 10697 10698 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10699 ctl_ooaq, ooa_links); 10700 10701 /* 10702 * If cur_blocked happens to be the first item in the OOA 10703 * queue now, prev_ooa will be NULL, and the action 10704 * returned will just be CTL_ACTION_PASS. 10705 */ 10706 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10707 10708 switch (action) { 10709 case CTL_ACTION_BLOCK: 10710 /* Nothing to do here, still blocked */ 10711 break; 10712 case CTL_ACTION_OVERLAP: 10713 case CTL_ACTION_OVERLAP_TAG: 10714 /* 10715 * This shouldn't happen! In theory we've already 10716 * checked this command for overlap... 10717 */ 10718 break; 10719 case CTL_ACTION_PASS: 10720 case CTL_ACTION_SKIP: { 10721 const struct ctl_cmd_entry *entry; 10722 10723 /* 10724 * The skip case shouldn't happen, this transaction 10725 * should have never made it onto the blocked queue. 10726 */ 10727 /* 10728 * This I/O is no longer blocked, we can remove it 10729 * from the blocked queue. Since this is a TAILQ 10730 * (doubly linked list), we can do O(1) removals 10731 * from any place on the list. 10732 */ 10733 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10734 blocked_links); 10735 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10736 10737 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 10738 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 10739 /* 10740 * Need to send IO back to original side to 10741 * run 10742 */ 10743 union ctl_ha_msg msg_info; 10744 10745 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 10746 msg_info.hdr.original_sc = 10747 cur_blocked->io_hdr.original_sc; 10748 msg_info.hdr.serializing_sc = cur_blocked; 10749 msg_info.hdr.msg_type = CTL_MSG_R2R; 10750 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 10751 sizeof(msg_info.hdr), M_NOWAIT); 10752 break; 10753 } 10754 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 10755 10756 /* 10757 * Check this I/O for LUN state changes that may 10758 * have happened while this command was blocked. 10759 * The LUN state may have been changed by a command 10760 * ahead of us in the queue, so we need to re-check 10761 * for any states that can be caused by SCSI 10762 * commands. 10763 */ 10764 if (ctl_scsiio_lun_check(lun, entry, 10765 &cur_blocked->scsiio) == 0) { 10766 cur_blocked->io_hdr.flags |= 10767 CTL_FLAG_IS_WAS_ON_RTR; 10768 ctl_enqueue_rtr(cur_blocked); 10769 } else 10770 ctl_done(cur_blocked); 10771 break; 10772 } 10773 default: 10774 /* 10775 * This probably shouldn't happen -- we shouldn't 10776 * get CTL_ACTION_ERROR, or anything else. 10777 */ 10778 break; 10779 } 10780 } 10781 10782 return (CTL_RETVAL_COMPLETE); 10783} 10784 10785/* 10786 * This routine (with one exception) checks LUN flags that can be set by 10787 * commands ahead of us in the OOA queue. These flags have to be checked 10788 * when a command initially comes in, and when we pull a command off the 10789 * blocked queue and are preparing to execute it. The reason we have to 10790 * check these flags for commands on the blocked queue is that the LUN 10791 * state may have been changed by a command ahead of us while we're on the 10792 * blocked queue. 10793 * 10794 * Ordering is somewhat important with these checks, so please pay 10795 * careful attention to the placement of any new checks. 10796 */ 10797static int 10798ctl_scsiio_lun_check(struct ctl_lun *lun, 10799 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 10800{ 10801 struct ctl_softc *softc = lun->ctl_softc; 10802 int retval; 10803 uint32_t residx; 10804 10805 retval = 0; 10806 10807 mtx_assert(&lun->lun_lock, MA_OWNED); 10808 10809 /* 10810 * If this shelf is a secondary shelf controller, we may have to 10811 * reject some commands disallowed by HA mode and link state. 10812 */ 10813 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 10814 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 10815 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 10816 ctl_set_lun_unavail(ctsio); 10817 retval = 1; 10818 goto bailout; 10819 } 10820 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 10821 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 10822 ctl_set_lun_transit(ctsio); 10823 retval = 1; 10824 goto bailout; 10825 } 10826 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 10827 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 10828 ctl_set_lun_standby(ctsio); 10829 retval = 1; 10830 goto bailout; 10831 } 10832 10833 /* The rest of checks are only done on executing side */ 10834 if (softc->ha_mode == CTL_HA_MODE_XFER) 10835 goto bailout; 10836 } 10837 10838 if (entry->pattern & CTL_LUN_PAT_WRITE) { 10839 if (lun->be_lun && 10840 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 10841 ctl_set_sense(ctsio, /*current_error*/ 1, 10842 /*sense_key*/ SSD_KEY_DATA_PROTECT, 10843 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 10844 retval = 1; 10845 goto bailout; 10846 } 10847 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 10848 .eca_and_aen & SCP_SWP) != 0) { 10849 ctl_set_sense(ctsio, /*current_error*/ 1, 10850 /*sense_key*/ SSD_KEY_DATA_PROTECT, 10851 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 10852 retval = 1; 10853 goto bailout; 10854 } 10855 } 10856 10857 /* 10858 * Check for a reservation conflict. If this command isn't allowed 10859 * even on reserved LUNs, and if this initiator isn't the one who 10860 * reserved us, reject the command with a reservation conflict. 10861 */ 10862 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 10863 if ((lun->flags & CTL_LUN_RESERVED) 10864 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 10865 if (lun->res_idx != residx) { 10866 ctl_set_reservation_conflict(ctsio); 10867 retval = 1; 10868 goto bailout; 10869 } 10870 } 10871 10872 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 10873 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 10874 /* No reservation or command is allowed. */; 10875 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 10876 (lun->res_type == SPR_TYPE_WR_EX || 10877 lun->res_type == SPR_TYPE_WR_EX_RO || 10878 lun->res_type == SPR_TYPE_WR_EX_AR)) { 10879 /* The command is allowed for Write Exclusive resv. */; 10880 } else { 10881 /* 10882 * if we aren't registered or it's a res holder type 10883 * reservation and this isn't the res holder then set a 10884 * conflict. 10885 */ 10886 if (ctl_get_prkey(lun, residx) == 0 10887 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 10888 ctl_set_reservation_conflict(ctsio); 10889 retval = 1; 10890 goto bailout; 10891 } 10892 } 10893 10894 if ((lun->flags & CTL_LUN_OFFLINE) 10895 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0)) { 10896 ctl_set_lun_not_ready(ctsio); 10897 retval = 1; 10898 goto bailout; 10899 } 10900 10901 if ((lun->flags & CTL_LUN_STOPPED) 10902 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 10903 /* "Logical unit not ready, initializing cmd. required" */ 10904 ctl_set_lun_stopped(ctsio); 10905 retval = 1; 10906 goto bailout; 10907 } 10908 10909 if ((lun->flags & CTL_LUN_INOPERABLE) 10910 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 10911 /* "Medium format corrupted" */ 10912 ctl_set_medium_format_corrupted(ctsio); 10913 retval = 1; 10914 goto bailout; 10915 } 10916 10917bailout: 10918 return (retval); 10919} 10920 10921static void 10922ctl_failover_io(union ctl_io *io, int have_lock) 10923{ 10924 ctl_set_busy(&io->scsiio); 10925 ctl_done(io); 10926} 10927 10928static void 10929ctl_failover_lun(struct ctl_lun *lun) 10930{ 10931 struct ctl_softc *softc = lun->ctl_softc; 10932 struct ctl_io_hdr *io, *next_io; 10933 10934 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", lun->lun)); 10935 if (softc->ha_mode == CTL_HA_MODE_XFER) { 10936 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 10937 /* We are master */ 10938 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 10939 if (io->flags & CTL_FLAG_IO_ACTIVE) { 10940 io->flags |= CTL_FLAG_ABORT; 10941 } else { /* This can be only due to DATAMOVE */ 10942 io->msg_type = CTL_MSG_DATAMOVE_DONE; 10943 io->flags |= CTL_FLAG_IO_ACTIVE; 10944 io->port_status = 31340; 10945 ctl_enqueue_isc((union ctl_io *)io); 10946 } 10947 } 10948 /* We are slave */ 10949 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 10950 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 10951 if (io->flags & CTL_FLAG_IO_ACTIVE) { 10952 io->flags |= CTL_FLAG_FAILOVER; 10953 } else { 10954 ctl_set_busy(&((union ctl_io *)io)-> 10955 scsiio); 10956 ctl_done((union ctl_io *)io); 10957 } 10958 } 10959 } 10960 } else { /* SERIALIZE modes */ 10961 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 10962 next_io) { 10963 /* We are master */ 10964 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 10965 TAILQ_REMOVE(&lun->blocked_queue, io, 10966 blocked_links); 10967 io->flags &= ~CTL_FLAG_BLOCKED; 10968 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 10969 ctl_free_io((union ctl_io *)io); 10970 } 10971 } 10972 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 10973 /* We are master */ 10974 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 10975 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 10976 ctl_free_io((union ctl_io *)io); 10977 } 10978 /* We are slave */ 10979 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 10980 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 10981 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 10982 ctl_set_busy(&((union ctl_io *)io)-> 10983 scsiio); 10984 ctl_done((union ctl_io *)io); 10985 } 10986 } 10987 } 10988 ctl_check_blocked(lun); 10989 } 10990} 10991 10992static int 10993ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 10994{ 10995 struct ctl_lun *lun; 10996 const struct ctl_cmd_entry *entry; 10997 uint32_t initidx, targ_lun; 10998 int retval; 10999 11000 retval = 0; 11001 11002 lun = NULL; 11003 11004 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11005 if ((targ_lun < CTL_MAX_LUNS) 11006 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11007 /* 11008 * If the LUN is invalid, pretend that it doesn't exist. 11009 * It will go away as soon as all pending I/O has been 11010 * completed. 11011 */ 11012 mtx_lock(&lun->lun_lock); 11013 if (lun->flags & CTL_LUN_DISABLED) { 11014 mtx_unlock(&lun->lun_lock); 11015 lun = NULL; 11016 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11017 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11018 } else { 11019 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11020 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11021 lun->be_lun; 11022 11023 /* 11024 * Every I/O goes into the OOA queue for a 11025 * particular LUN, and stays there until completion. 11026 */ 11027#ifdef CTL_TIME_IO 11028 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11029 lun->idle_time += getsbinuptime() - 11030 lun->last_busy; 11031 } 11032#endif 11033 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11034 ooa_links); 11035 } 11036 } else { 11037 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11038 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11039 } 11040 11041 /* Get command entry and return error if it is unsuppotyed. */ 11042 entry = ctl_validate_command(ctsio); 11043 if (entry == NULL) { 11044 if (lun) 11045 mtx_unlock(&lun->lun_lock); 11046 return (retval); 11047 } 11048 11049 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11050 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11051 11052 /* 11053 * Check to see whether we can send this command to LUNs that don't 11054 * exist. This should pretty much only be the case for inquiry 11055 * and request sense. Further checks, below, really require having 11056 * a LUN, so we can't really check the command anymore. Just put 11057 * it on the rtr queue. 11058 */ 11059 if (lun == NULL) { 11060 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11061 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11062 ctl_enqueue_rtr((union ctl_io *)ctsio); 11063 return (retval); 11064 } 11065 11066 ctl_set_unsupported_lun(ctsio); 11067 ctl_done((union ctl_io *)ctsio); 11068 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11069 return (retval); 11070 } else { 11071 /* 11072 * Make sure we support this particular command on this LUN. 11073 * e.g., we don't support writes to the control LUN. 11074 */ 11075 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11076 mtx_unlock(&lun->lun_lock); 11077 ctl_set_invalid_opcode(ctsio); 11078 ctl_done((union ctl_io *)ctsio); 11079 return (retval); 11080 } 11081 } 11082 11083 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11084 11085#ifdef CTL_WITH_CA 11086 /* 11087 * If we've got a request sense, it'll clear the contingent 11088 * allegiance condition. Otherwise, if we have a CA condition for 11089 * this initiator, clear it, because it sent down a command other 11090 * than request sense. 11091 */ 11092 if ((ctsio->cdb[0] != REQUEST_SENSE) 11093 && (ctl_is_set(lun->have_ca, initidx))) 11094 ctl_clear_mask(lun->have_ca, initidx); 11095#endif 11096 11097 /* 11098 * If the command has this flag set, it handles its own unit 11099 * attention reporting, we shouldn't do anything. Otherwise we 11100 * check for any pending unit attentions, and send them back to the 11101 * initiator. We only do this when a command initially comes in, 11102 * not when we pull it off the blocked queue. 11103 * 11104 * According to SAM-3, section 5.3.2, the order that things get 11105 * presented back to the host is basically unit attentions caused 11106 * by some sort of reset event, busy status, reservation conflicts 11107 * or task set full, and finally any other status. 11108 * 11109 * One issue here is that some of the unit attentions we report 11110 * don't fall into the "reset" category (e.g. "reported luns data 11111 * has changed"). So reporting it here, before the reservation 11112 * check, may be technically wrong. I guess the only thing to do 11113 * would be to check for and report the reset events here, and then 11114 * check for the other unit attention types after we check for a 11115 * reservation conflict. 11116 * 11117 * XXX KDM need to fix this 11118 */ 11119 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11120 ctl_ua_type ua_type; 11121 scsi_sense_data_type sense_format; 11122 11123 if (lun->flags & CTL_LUN_SENSE_DESC) 11124 sense_format = SSD_TYPE_DESC; 11125 else 11126 sense_format = SSD_TYPE_FIXED; 11127 11128 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11129 sense_format); 11130 if (ua_type != CTL_UA_NONE) { 11131 mtx_unlock(&lun->lun_lock); 11132 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11133 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11134 ctsio->sense_len = SSD_FULL_SIZE; 11135 ctl_done((union ctl_io *)ctsio); 11136 return (retval); 11137 } 11138 } 11139 11140 11141 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11142 mtx_unlock(&lun->lun_lock); 11143 ctl_done((union ctl_io *)ctsio); 11144 return (retval); 11145 } 11146 11147 /* 11148 * XXX CHD this is where we want to send IO to other side if 11149 * this LUN is secondary on this SC. We will need to make a copy 11150 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11151 * the copy we send as FROM_OTHER. 11152 * We also need to stuff the address of the original IO so we can 11153 * find it easily. Something similar will need be done on the other 11154 * side so when we are done we can find the copy. 11155 */ 11156 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11157 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0) { 11158 union ctl_ha_msg msg_info; 11159 int isc_retval; 11160 11161 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11162 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11163 mtx_unlock(&lun->lun_lock); 11164 11165 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11166 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11167 msg_info.hdr.serializing_sc = NULL; 11168 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11169 msg_info.scsi.tag_num = ctsio->tag_num; 11170 msg_info.scsi.tag_type = ctsio->tag_type; 11171 msg_info.scsi.cdb_len = ctsio->cdb_len; 11172 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11173 11174 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11175 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11176 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11177 ctl_set_busy(ctsio); 11178 ctl_done((union ctl_io *)ctsio); 11179 return (retval); 11180 } 11181 return (retval); 11182 } 11183 11184 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11185 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11186 ctl_ooaq, ooa_links))) { 11187 case CTL_ACTION_BLOCK: 11188 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11189 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11190 blocked_links); 11191 mtx_unlock(&lun->lun_lock); 11192 return (retval); 11193 case CTL_ACTION_PASS: 11194 case CTL_ACTION_SKIP: 11195 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11196 mtx_unlock(&lun->lun_lock); 11197 ctl_enqueue_rtr((union ctl_io *)ctsio); 11198 break; 11199 case CTL_ACTION_OVERLAP: 11200 mtx_unlock(&lun->lun_lock); 11201 ctl_set_overlapped_cmd(ctsio); 11202 ctl_done((union ctl_io *)ctsio); 11203 break; 11204 case CTL_ACTION_OVERLAP_TAG: 11205 mtx_unlock(&lun->lun_lock); 11206 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11207 ctl_done((union ctl_io *)ctsio); 11208 break; 11209 case CTL_ACTION_ERROR: 11210 default: 11211 mtx_unlock(&lun->lun_lock); 11212 ctl_set_internal_failure(ctsio, 11213 /*sks_valid*/ 0, 11214 /*retry_count*/ 0); 11215 ctl_done((union ctl_io *)ctsio); 11216 break; 11217 } 11218 return (retval); 11219} 11220 11221const struct ctl_cmd_entry * 11222ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11223{ 11224 const struct ctl_cmd_entry *entry; 11225 int service_action; 11226 11227 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11228 if (sa) 11229 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11230 if (entry->flags & CTL_CMD_FLAG_SA5) { 11231 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11232 entry = &((const struct ctl_cmd_entry *) 11233 entry->execute)[service_action]; 11234 } 11235 return (entry); 11236} 11237 11238const struct ctl_cmd_entry * 11239ctl_validate_command(struct ctl_scsiio *ctsio) 11240{ 11241 const struct ctl_cmd_entry *entry; 11242 int i, sa; 11243 uint8_t diff; 11244 11245 entry = ctl_get_cmd_entry(ctsio, &sa); 11246 if (entry->execute == NULL) { 11247 if (sa) 11248 ctl_set_invalid_field(ctsio, 11249 /*sks_valid*/ 1, 11250 /*command*/ 1, 11251 /*field*/ 1, 11252 /*bit_valid*/ 1, 11253 /*bit*/ 4); 11254 else 11255 ctl_set_invalid_opcode(ctsio); 11256 ctl_done((union ctl_io *)ctsio); 11257 return (NULL); 11258 } 11259 KASSERT(entry->length > 0, 11260 ("Not defined length for command 0x%02x/0x%02x", 11261 ctsio->cdb[0], ctsio->cdb[1])); 11262 for (i = 1; i < entry->length; i++) { 11263 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11264 if (diff == 0) 11265 continue; 11266 ctl_set_invalid_field(ctsio, 11267 /*sks_valid*/ 1, 11268 /*command*/ 1, 11269 /*field*/ i, 11270 /*bit_valid*/ 1, 11271 /*bit*/ fls(diff) - 1); 11272 ctl_done((union ctl_io *)ctsio); 11273 return (NULL); 11274 } 11275 return (entry); 11276} 11277 11278static int 11279ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11280{ 11281 11282 switch (lun_type) { 11283 case T_PROCESSOR: 11284 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11285 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11286 return (0); 11287 break; 11288 case T_DIRECT: 11289 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11290 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11291 return (0); 11292 break; 11293 default: 11294 return (0); 11295 } 11296 return (1); 11297} 11298 11299static int 11300ctl_scsiio(struct ctl_scsiio *ctsio) 11301{ 11302 int retval; 11303 const struct ctl_cmd_entry *entry; 11304 11305 retval = CTL_RETVAL_COMPLETE; 11306 11307 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11308 11309 entry = ctl_get_cmd_entry(ctsio, NULL); 11310 11311 /* 11312 * If this I/O has been aborted, just send it straight to 11313 * ctl_done() without executing it. 11314 */ 11315 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11316 ctl_done((union ctl_io *)ctsio); 11317 goto bailout; 11318 } 11319 11320 /* 11321 * All the checks should have been handled by ctl_scsiio_precheck(). 11322 * We should be clear now to just execute the I/O. 11323 */ 11324 retval = entry->execute(ctsio); 11325 11326bailout: 11327 return (retval); 11328} 11329 11330/* 11331 * Since we only implement one target right now, a bus reset simply resets 11332 * our single target. 11333 */ 11334static int 11335ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11336{ 11337 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11338} 11339 11340static int 11341ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11342 ctl_ua_type ua_type) 11343{ 11344 struct ctl_port *port; 11345 struct ctl_lun *lun; 11346 int retval; 11347 11348 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11349 union ctl_ha_msg msg_info; 11350 11351 msg_info.hdr.nexus = io->io_hdr.nexus; 11352 if (ua_type==CTL_UA_TARG_RESET) 11353 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11354 else 11355 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11356 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11357 msg_info.hdr.original_sc = NULL; 11358 msg_info.hdr.serializing_sc = NULL; 11359 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11360 sizeof(msg_info.task), M_WAITOK); 11361 } 11362 retval = 0; 11363 11364 mtx_lock(&softc->ctl_lock); 11365 port = softc->ctl_ports[io->io_hdr.nexus.targ_port]; 11366 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11367 if (port != NULL && 11368 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 11369 continue; 11370 retval += ctl_lun_reset(lun, io, ua_type); 11371 } 11372 mtx_unlock(&softc->ctl_lock); 11373 11374 return (retval); 11375} 11376 11377/* 11378 * The LUN should always be set. The I/O is optional, and is used to 11379 * distinguish between I/Os sent by this initiator, and by other 11380 * initiators. We set unit attention for initiators other than this one. 11381 * SAM-3 is vague on this point. It does say that a unit attention should 11382 * be established for other initiators when a LUN is reset (see section 11383 * 5.7.3), but it doesn't specifically say that the unit attention should 11384 * be established for this particular initiator when a LUN is reset. Here 11385 * is the relevant text, from SAM-3 rev 8: 11386 * 11387 * 5.7.2 When a SCSI initiator port aborts its own tasks 11388 * 11389 * When a SCSI initiator port causes its own task(s) to be aborted, no 11390 * notification that the task(s) have been aborted shall be returned to 11391 * the SCSI initiator port other than the completion response for the 11392 * command or task management function action that caused the task(s) to 11393 * be aborted and notification(s) associated with related effects of the 11394 * action (e.g., a reset unit attention condition). 11395 * 11396 * XXX KDM for now, we're setting unit attention for all initiators. 11397 */ 11398static int 11399ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11400{ 11401 union ctl_io *xio; 11402#if 0 11403 uint32_t initidx; 11404#endif 11405#ifdef CTL_WITH_CA 11406 int i; 11407#endif 11408 11409 mtx_lock(&lun->lun_lock); 11410 /* 11411 * Run through the OOA queue and abort each I/O. 11412 */ 11413 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11414 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11415 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11416 } 11417 11418 /* 11419 * This version sets unit attention for every 11420 */ 11421#if 0 11422 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11423 ctl_est_ua_all(lun, initidx, ua_type); 11424#else 11425 ctl_est_ua_all(lun, -1, ua_type); 11426#endif 11427 11428 /* 11429 * A reset (any kind, really) clears reservations established with 11430 * RESERVE/RELEASE. It does not clear reservations established 11431 * with PERSISTENT RESERVE OUT, but we don't support that at the 11432 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11433 * reservations made with the RESERVE/RELEASE commands, because 11434 * those commands are obsolete in SPC-3. 11435 */ 11436 lun->flags &= ~CTL_LUN_RESERVED; 11437 11438#ifdef CTL_WITH_CA 11439 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11440 ctl_clear_mask(lun->have_ca, i); 11441#endif 11442 mtx_unlock(&lun->lun_lock); 11443 11444 return (0); 11445} 11446 11447static void 11448ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11449 int other_sc) 11450{ 11451 union ctl_io *xio; 11452 11453 mtx_assert(&lun->lun_lock, MA_OWNED); 11454 11455 /* 11456 * Run through the OOA queue and attempt to find the given I/O. 11457 * The target port, initiator ID, tag type and tag number have to 11458 * match the values that we got from the initiator. If we have an 11459 * untagged command to abort, simply abort the first untagged command 11460 * we come to. We only allow one untagged command at a time of course. 11461 */ 11462 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11463 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11464 11465 if ((targ_port == UINT32_MAX || 11466 targ_port == xio->io_hdr.nexus.targ_port) && 11467 (init_id == UINT32_MAX || 11468 init_id == xio->io_hdr.nexus.initid)) { 11469 if (targ_port != xio->io_hdr.nexus.targ_port || 11470 init_id != xio->io_hdr.nexus.initid) 11471 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11472 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11473 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11474 union ctl_ha_msg msg_info; 11475 11476 msg_info.hdr.nexus = xio->io_hdr.nexus; 11477 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11478 msg_info.task.tag_num = xio->scsiio.tag_num; 11479 msg_info.task.tag_type = xio->scsiio.tag_type; 11480 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11481 msg_info.hdr.original_sc = NULL; 11482 msg_info.hdr.serializing_sc = NULL; 11483 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11484 sizeof(msg_info.task), M_NOWAIT); 11485 } 11486 } 11487 } 11488} 11489 11490static int 11491ctl_abort_task_set(union ctl_io *io) 11492{ 11493 struct ctl_softc *softc = control_softc; 11494 struct ctl_lun *lun; 11495 uint32_t targ_lun; 11496 11497 /* 11498 * Look up the LUN. 11499 */ 11500 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11501 mtx_lock(&softc->ctl_lock); 11502 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11503 lun = softc->ctl_luns[targ_lun]; 11504 else { 11505 mtx_unlock(&softc->ctl_lock); 11506 return (1); 11507 } 11508 11509 mtx_lock(&lun->lun_lock); 11510 mtx_unlock(&softc->ctl_lock); 11511 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11512 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11513 io->io_hdr.nexus.initid, 11514 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11515 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11516 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11517 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11518 } 11519 mtx_unlock(&lun->lun_lock); 11520 return (0); 11521} 11522 11523static int 11524ctl_i_t_nexus_reset(union ctl_io *io) 11525{ 11526 struct ctl_softc *softc = control_softc; 11527 struct ctl_lun *lun; 11528 uint32_t initidx; 11529 11530 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11531 union ctl_ha_msg msg_info; 11532 11533 msg_info.hdr.nexus = io->io_hdr.nexus; 11534 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 11535 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11536 msg_info.hdr.original_sc = NULL; 11537 msg_info.hdr.serializing_sc = NULL; 11538 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11539 sizeof(msg_info.task), M_WAITOK); 11540 } 11541 11542 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11543 mtx_lock(&softc->ctl_lock); 11544 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11545 mtx_lock(&lun->lun_lock); 11546 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11547 io->io_hdr.nexus.initid, 1); 11548#ifdef CTL_WITH_CA 11549 ctl_clear_mask(lun->have_ca, initidx); 11550#endif 11551 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 11552 lun->flags &= ~CTL_LUN_RESERVED; 11553 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 11554 mtx_unlock(&lun->lun_lock); 11555 } 11556 mtx_unlock(&softc->ctl_lock); 11557 return (0); 11558} 11559 11560static int 11561ctl_abort_task(union ctl_io *io) 11562{ 11563 union ctl_io *xio; 11564 struct ctl_lun *lun; 11565 struct ctl_softc *softc; 11566#if 0 11567 struct sbuf sb; 11568 char printbuf[128]; 11569#endif 11570 int found; 11571 uint32_t targ_lun; 11572 11573 softc = control_softc; 11574 found = 0; 11575 11576 /* 11577 * Look up the LUN. 11578 */ 11579 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11580 mtx_lock(&softc->ctl_lock); 11581 if ((targ_lun < CTL_MAX_LUNS) 11582 && (softc->ctl_luns[targ_lun] != NULL)) 11583 lun = softc->ctl_luns[targ_lun]; 11584 else { 11585 mtx_unlock(&softc->ctl_lock); 11586 return (1); 11587 } 11588 11589#if 0 11590 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11591 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11592#endif 11593 11594 mtx_lock(&lun->lun_lock); 11595 mtx_unlock(&softc->ctl_lock); 11596 /* 11597 * Run through the OOA queue and attempt to find the given I/O. 11598 * The target port, initiator ID, tag type and tag number have to 11599 * match the values that we got from the initiator. If we have an 11600 * untagged command to abort, simply abort the first untagged command 11601 * we come to. We only allow one untagged command at a time of course. 11602 */ 11603 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11604 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11605#if 0 11606 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11607 11608 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11609 lun->lun, xio->scsiio.tag_num, 11610 xio->scsiio.tag_type, 11611 (xio->io_hdr.blocked_links.tqe_prev 11612 == NULL) ? "" : " BLOCKED", 11613 (xio->io_hdr.flags & 11614 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11615 (xio->io_hdr.flags & 11616 CTL_FLAG_ABORT) ? " ABORT" : "", 11617 (xio->io_hdr.flags & 11618 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11619 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11620 sbuf_finish(&sb); 11621 printf("%s\n", sbuf_data(&sb)); 11622#endif 11623 11624 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 11625 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 11626 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 11627 continue; 11628 11629 /* 11630 * If the abort says that the task is untagged, the 11631 * task in the queue must be untagged. Otherwise, 11632 * we just check to see whether the tag numbers 11633 * match. This is because the QLogic firmware 11634 * doesn't pass back the tag type in an abort 11635 * request. 11636 */ 11637#if 0 11638 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 11639 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 11640 || (xio->scsiio.tag_num == io->taskio.tag_num)) 11641#endif 11642 /* 11643 * XXX KDM we've got problems with FC, because it 11644 * doesn't send down a tag type with aborts. So we 11645 * can only really go by the tag number... 11646 * This may cause problems with parallel SCSI. 11647 * Need to figure that out!! 11648 */ 11649 if (xio->scsiio.tag_num == io->taskio.tag_num) { 11650 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11651 found = 1; 11652 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 11653 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11654 union ctl_ha_msg msg_info; 11655 11656 msg_info.hdr.nexus = io->io_hdr.nexus; 11657 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11658 msg_info.task.tag_num = io->taskio.tag_num; 11659 msg_info.task.tag_type = io->taskio.tag_type; 11660 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11661 msg_info.hdr.original_sc = NULL; 11662 msg_info.hdr.serializing_sc = NULL; 11663#if 0 11664 printf("Sent Abort to other side\n"); 11665#endif 11666 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11667 sizeof(msg_info.task), M_NOWAIT); 11668 } 11669#if 0 11670 printf("ctl_abort_task: found I/O to abort\n"); 11671#endif 11672 } 11673 } 11674 mtx_unlock(&lun->lun_lock); 11675 11676 if (found == 0) { 11677 /* 11678 * This isn't really an error. It's entirely possible for 11679 * the abort and command completion to cross on the wire. 11680 * This is more of an informative/diagnostic error. 11681 */ 11682#if 0 11683 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 11684 "%u:%u:%u tag %d type %d\n", 11685 io->io_hdr.nexus.initid, 11686 io->io_hdr.nexus.targ_port, 11687 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 11688 io->taskio.tag_type); 11689#endif 11690 } 11691 return (0); 11692} 11693 11694static void 11695ctl_run_task(union ctl_io *io) 11696{ 11697 struct ctl_softc *softc = control_softc; 11698 int retval = 1; 11699 const char *task_desc; 11700 11701 CTL_DEBUG_PRINT(("ctl_run_task\n")); 11702 11703 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 11704 ("ctl_run_task: Unextected io_type %d\n", 11705 io->io_hdr.io_type)); 11706 11707 task_desc = ctl_scsi_task_string(&io->taskio); 11708 if (task_desc != NULL) { 11709#ifdef NEEDTOPORT 11710 csevent_log(CSC_CTL | CSC_SHELF_SW | 11711 CTL_TASK_REPORT, 11712 csevent_LogType_Trace, 11713 csevent_Severity_Information, 11714 csevent_AlertLevel_Green, 11715 csevent_FRU_Firmware, 11716 csevent_FRU_Unknown, 11717 "CTL: received task: %s",task_desc); 11718#endif 11719 } else { 11720#ifdef NEEDTOPORT 11721 csevent_log(CSC_CTL | CSC_SHELF_SW | 11722 CTL_TASK_REPORT, 11723 csevent_LogType_Trace, 11724 csevent_Severity_Information, 11725 csevent_AlertLevel_Green, 11726 csevent_FRU_Firmware, 11727 csevent_FRU_Unknown, 11728 "CTL: received unknown task " 11729 "type: %d (%#x)", 11730 io->taskio.task_action, 11731 io->taskio.task_action); 11732#endif 11733 } 11734 switch (io->taskio.task_action) { 11735 case CTL_TASK_ABORT_TASK: 11736 retval = ctl_abort_task(io); 11737 break; 11738 case CTL_TASK_ABORT_TASK_SET: 11739 case CTL_TASK_CLEAR_TASK_SET: 11740 retval = ctl_abort_task_set(io); 11741 break; 11742 case CTL_TASK_CLEAR_ACA: 11743 break; 11744 case CTL_TASK_I_T_NEXUS_RESET: 11745 retval = ctl_i_t_nexus_reset(io); 11746 break; 11747 case CTL_TASK_LUN_RESET: { 11748 struct ctl_lun *lun; 11749 uint32_t targ_lun; 11750 11751 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11752 mtx_lock(&softc->ctl_lock); 11753 if ((targ_lun < CTL_MAX_LUNS) 11754 && (softc->ctl_luns[targ_lun] != NULL)) 11755 lun = softc->ctl_luns[targ_lun]; 11756 else { 11757 mtx_unlock(&softc->ctl_lock); 11758 retval = 1; 11759 break; 11760 } 11761 retval = ctl_lun_reset(lun, io, CTL_UA_LUN_RESET); 11762 mtx_unlock(&softc->ctl_lock); 11763 11764 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11765 union ctl_ha_msg msg_info; 11766 11767 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11768 msg_info.hdr.nexus = io->io_hdr.nexus; 11769 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11770 msg_info.hdr.original_sc = NULL; 11771 msg_info.hdr.serializing_sc = NULL; 11772 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11773 sizeof(msg_info.task), M_WAITOK); 11774 } 11775 break; 11776 } 11777 case CTL_TASK_TARGET_RESET: 11778 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 11779 break; 11780 case CTL_TASK_BUS_RESET: 11781 retval = ctl_bus_reset(softc, io); 11782 break; 11783 case CTL_TASK_PORT_LOGIN: 11784 break; 11785 case CTL_TASK_PORT_LOGOUT: 11786 break; 11787 default: 11788 printf("ctl_run_task: got unknown task management event %d\n", 11789 io->taskio.task_action); 11790 break; 11791 } 11792 if (retval == 0) 11793 io->io_hdr.status = CTL_SUCCESS; 11794 else 11795 io->io_hdr.status = CTL_ERROR; 11796 ctl_done(io); 11797} 11798 11799/* 11800 * For HA operation. Handle commands that come in from the other 11801 * controller. 11802 */ 11803static void 11804ctl_handle_isc(union ctl_io *io) 11805{ 11806 int free_io; 11807 struct ctl_lun *lun; 11808 struct ctl_softc *softc; 11809 uint32_t targ_lun; 11810 11811 softc = control_softc; 11812 11813 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11814 lun = softc->ctl_luns[targ_lun]; 11815 11816 switch (io->io_hdr.msg_type) { 11817 case CTL_MSG_SERIALIZE: 11818 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 11819 break; 11820 case CTL_MSG_R2R: { 11821 const struct ctl_cmd_entry *entry; 11822 11823 /* 11824 * This is only used in SER_ONLY mode. 11825 */ 11826 free_io = 0; 11827 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 11828 mtx_lock(&lun->lun_lock); 11829 if (ctl_scsiio_lun_check(lun, 11830 entry, (struct ctl_scsiio *)io) != 0) { 11831 mtx_unlock(&lun->lun_lock); 11832 ctl_done(io); 11833 break; 11834 } 11835 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11836 mtx_unlock(&lun->lun_lock); 11837 ctl_enqueue_rtr(io); 11838 break; 11839 } 11840 case CTL_MSG_FINISH_IO: 11841 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11842 free_io = 0; 11843 ctl_done(io); 11844 } else { 11845 free_io = 1; 11846 mtx_lock(&lun->lun_lock); 11847 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 11848 ooa_links); 11849 ctl_check_blocked(lun); 11850 mtx_unlock(&lun->lun_lock); 11851 } 11852 break; 11853 case CTL_MSG_PERS_ACTION: 11854 ctl_hndl_per_res_out_on_other_sc( 11855 (union ctl_ha_msg *)&io->presio.pr_msg); 11856 free_io = 1; 11857 break; 11858 case CTL_MSG_BAD_JUJU: 11859 free_io = 0; 11860 ctl_done(io); 11861 break; 11862 case CTL_MSG_DATAMOVE: 11863 /* Only used in XFER mode */ 11864 free_io = 0; 11865 ctl_datamove_remote(io); 11866 break; 11867 case CTL_MSG_DATAMOVE_DONE: 11868 /* Only used in XFER mode */ 11869 free_io = 0; 11870 io->scsiio.be_move_done(io); 11871 break; 11872 case CTL_MSG_FAILOVER: 11873 mtx_lock(&lun->lun_lock); 11874 ctl_failover_lun(lun); 11875 mtx_unlock(&lun->lun_lock); 11876 free_io = 1; 11877 break; 11878 default: 11879 free_io = 1; 11880 printf("%s: Invalid message type %d\n", 11881 __func__, io->io_hdr.msg_type); 11882 break; 11883 } 11884 if (free_io) 11885 ctl_free_io(io); 11886 11887} 11888 11889 11890/* 11891 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 11892 * there is no match. 11893 */ 11894static ctl_lun_error_pattern 11895ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 11896{ 11897 const struct ctl_cmd_entry *entry; 11898 ctl_lun_error_pattern filtered_pattern, pattern; 11899 11900 pattern = desc->error_pattern; 11901 11902 /* 11903 * XXX KDM we need more data passed into this function to match a 11904 * custom pattern, and we actually need to implement custom pattern 11905 * matching. 11906 */ 11907 if (pattern & CTL_LUN_PAT_CMD) 11908 return (CTL_LUN_PAT_CMD); 11909 11910 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 11911 return (CTL_LUN_PAT_ANY); 11912 11913 entry = ctl_get_cmd_entry(ctsio, NULL); 11914 11915 filtered_pattern = entry->pattern & pattern; 11916 11917 /* 11918 * If the user requested specific flags in the pattern (e.g. 11919 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 11920 * flags. 11921 * 11922 * If the user did not specify any flags, it doesn't matter whether 11923 * or not the command supports the flags. 11924 */ 11925 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 11926 (pattern & ~CTL_LUN_PAT_MASK)) 11927 return (CTL_LUN_PAT_NONE); 11928 11929 /* 11930 * If the user asked for a range check, see if the requested LBA 11931 * range overlaps with this command's LBA range. 11932 */ 11933 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 11934 uint64_t lba1; 11935 uint64_t len1; 11936 ctl_action action; 11937 int retval; 11938 11939 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 11940 if (retval != 0) 11941 return (CTL_LUN_PAT_NONE); 11942 11943 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 11944 desc->lba_range.len, FALSE); 11945 /* 11946 * A "pass" means that the LBA ranges don't overlap, so 11947 * this doesn't match the user's range criteria. 11948 */ 11949 if (action == CTL_ACTION_PASS) 11950 return (CTL_LUN_PAT_NONE); 11951 } 11952 11953 return (filtered_pattern); 11954} 11955 11956static void 11957ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 11958{ 11959 struct ctl_error_desc *desc, *desc2; 11960 11961 mtx_assert(&lun->lun_lock, MA_OWNED); 11962 11963 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 11964 ctl_lun_error_pattern pattern; 11965 /* 11966 * Check to see whether this particular command matches 11967 * the pattern in the descriptor. 11968 */ 11969 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 11970 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 11971 continue; 11972 11973 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 11974 case CTL_LUN_INJ_ABORTED: 11975 ctl_set_aborted(&io->scsiio); 11976 break; 11977 case CTL_LUN_INJ_MEDIUM_ERR: 11978 ctl_set_medium_error(&io->scsiio); 11979 break; 11980 case CTL_LUN_INJ_UA: 11981 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 11982 * OCCURRED */ 11983 ctl_set_ua(&io->scsiio, 0x29, 0x00); 11984 break; 11985 case CTL_LUN_INJ_CUSTOM: 11986 /* 11987 * We're assuming the user knows what he is doing. 11988 * Just copy the sense information without doing 11989 * checks. 11990 */ 11991 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 11992 MIN(sizeof(desc->custom_sense), 11993 sizeof(io->scsiio.sense_data))); 11994 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 11995 io->scsiio.sense_len = SSD_FULL_SIZE; 11996 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11997 break; 11998 case CTL_LUN_INJ_NONE: 11999 default: 12000 /* 12001 * If this is an error injection type we don't know 12002 * about, clear the continuous flag (if it is set) 12003 * so it will get deleted below. 12004 */ 12005 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12006 break; 12007 } 12008 /* 12009 * By default, each error injection action is a one-shot 12010 */ 12011 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12012 continue; 12013 12014 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12015 12016 free(desc, M_CTL); 12017 } 12018} 12019 12020#ifdef CTL_IO_DELAY 12021static void 12022ctl_datamove_timer_wakeup(void *arg) 12023{ 12024 union ctl_io *io; 12025 12026 io = (union ctl_io *)arg; 12027 12028 ctl_datamove(io); 12029} 12030#endif /* CTL_IO_DELAY */ 12031 12032void 12033ctl_datamove(union ctl_io *io) 12034{ 12035 void (*fe_datamove)(union ctl_io *io); 12036 12037 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12038 12039 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12040 12041#ifdef CTL_TIME_IO 12042 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12043 char str[256]; 12044 char path_str[64]; 12045 struct sbuf sb; 12046 12047 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12048 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12049 12050 sbuf_cat(&sb, path_str); 12051 switch (io->io_hdr.io_type) { 12052 case CTL_IO_SCSI: 12053 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12054 sbuf_printf(&sb, "\n"); 12055 sbuf_cat(&sb, path_str); 12056 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12057 io->scsiio.tag_num, io->scsiio.tag_type); 12058 break; 12059 case CTL_IO_TASK: 12060 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12061 "Tag Type: %d\n", io->taskio.task_action, 12062 io->taskio.tag_num, io->taskio.tag_type); 12063 break; 12064 default: 12065 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12066 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12067 break; 12068 } 12069 sbuf_cat(&sb, path_str); 12070 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12071 (intmax_t)time_uptime - io->io_hdr.start_time); 12072 sbuf_finish(&sb); 12073 printf("%s", sbuf_data(&sb)); 12074 } 12075#endif /* CTL_TIME_IO */ 12076 12077#ifdef CTL_IO_DELAY 12078 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12079 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12080 } else { 12081 struct ctl_lun *lun; 12082 12083 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12084 if ((lun != NULL) 12085 && (lun->delay_info.datamove_delay > 0)) { 12086 12087 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12088 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12089 callout_reset(&io->io_hdr.delay_callout, 12090 lun->delay_info.datamove_delay * hz, 12091 ctl_datamove_timer_wakeup, io); 12092 if (lun->delay_info.datamove_type == 12093 CTL_DELAY_TYPE_ONESHOT) 12094 lun->delay_info.datamove_delay = 0; 12095 return; 12096 } 12097 } 12098#endif 12099 12100 /* 12101 * This command has been aborted. Set the port status, so we fail 12102 * the data move. 12103 */ 12104 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12105 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12106 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12107 io->io_hdr.nexus.targ_port, 12108 io->io_hdr.nexus.targ_lun); 12109 io->io_hdr.port_status = 31337; 12110 /* 12111 * Note that the backend, in this case, will get the 12112 * callback in its context. In other cases it may get 12113 * called in the frontend's interrupt thread context. 12114 */ 12115 io->scsiio.be_move_done(io); 12116 return; 12117 } 12118 12119 /* Don't confuse frontend with zero length data move. */ 12120 if (io->scsiio.kern_data_len == 0) { 12121 io->scsiio.be_move_done(io); 12122 return; 12123 } 12124 12125 /* 12126 * If we're in XFER mode and this I/O is from the other shelf 12127 * controller, we need to send the DMA to the other side to 12128 * actually transfer the data to/from the host. In serialize only 12129 * mode the transfer happens below CTL and ctl_datamove() is only 12130 * called on the machine that originally received the I/O. 12131 */ 12132 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12133 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12134 union ctl_ha_msg msg; 12135 uint32_t sg_entries_sent; 12136 int do_sg_copy; 12137 int i; 12138 12139 memset(&msg, 0, sizeof(msg)); 12140 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12141 msg.hdr.original_sc = io->io_hdr.original_sc; 12142 msg.hdr.serializing_sc = io; 12143 msg.hdr.nexus = io->io_hdr.nexus; 12144 msg.dt.flags = io->io_hdr.flags; 12145 /* 12146 * We convert everything into a S/G list here. We can't 12147 * pass by reference, only by value between controllers. 12148 * So we can't pass a pointer to the S/G list, only as many 12149 * S/G entries as we can fit in here. If it's possible for 12150 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12151 * then we need to break this up into multiple transfers. 12152 */ 12153 if (io->scsiio.kern_sg_entries == 0) { 12154 msg.dt.kern_sg_entries = 1; 12155#if 0 12156 /* 12157 * Convert to a physical address if this is a 12158 * virtual address. 12159 */ 12160 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12161 msg.dt.sg_list[0].addr = 12162 io->scsiio.kern_data_ptr; 12163 } else { 12164 /* 12165 * XXX KDM use busdma here! 12166 */ 12167 msg.dt.sg_list[0].addr = (void *) 12168 vtophys(io->scsiio.kern_data_ptr); 12169 } 12170#else 12171 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12172 ("HA does not support BUS_ADDR")); 12173 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 12174#endif 12175 12176 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12177 do_sg_copy = 0; 12178 } else { 12179 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12180 do_sg_copy = 1; 12181 } 12182 12183 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12184 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12185 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12186 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12187 msg.dt.sg_sequence = 0; 12188 12189 /* 12190 * Loop until we've sent all of the S/G entries. On the 12191 * other end, we'll recompose these S/G entries into one 12192 * contiguous list before passing it to the 12193 */ 12194 for (sg_entries_sent = 0; sg_entries_sent < 12195 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12196 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/ 12197 sizeof(msg.dt.sg_list[0])), 12198 msg.dt.kern_sg_entries - sg_entries_sent); 12199 12200 if (do_sg_copy != 0) { 12201 struct ctl_sg_entry *sgl; 12202 int j; 12203 12204 sgl = (struct ctl_sg_entry *) 12205 io->scsiio.kern_data_ptr; 12206 /* 12207 * If this is in cached memory, flush the cache 12208 * before we send the DMA request to the other 12209 * controller. We want to do this in either 12210 * the * read or the write case. The read 12211 * case is straightforward. In the write 12212 * case, we want to make sure nothing is 12213 * in the local cache that could overwrite 12214 * the DMAed data. 12215 */ 12216 12217 for (i = sg_entries_sent, j = 0; 12218 i < msg.dt.cur_sg_entries; i++, j++) { 12219#if 0 12220 if ((io->io_hdr.flags & 12221 CTL_FLAG_BUS_ADDR) == 0) { 12222 /* 12223 * XXX KDM use busdma. 12224 */ 12225 msg.dt.sg_list[j].addr =(void *) 12226 vtophys(sgl[i].addr); 12227 } else { 12228 msg.dt.sg_list[j].addr = 12229 sgl[i].addr; 12230 } 12231#else 12232 KASSERT((io->io_hdr.flags & 12233 CTL_FLAG_BUS_ADDR) == 0, 12234 ("HA does not support BUS_ADDR")); 12235 msg.dt.sg_list[j].addr = sgl[i].addr; 12236#endif 12237 msg.dt.sg_list[j].len = sgl[i].len; 12238 } 12239 } 12240 12241 sg_entries_sent += msg.dt.cur_sg_entries; 12242 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12243 msg.dt.sg_last = 1; 12244 else 12245 msg.dt.sg_last = 0; 12246 12247 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12248 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 12249 sizeof(struct ctl_sg_entry)*msg.dt.cur_sg_entries, 12250 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 12251 io->io_hdr.port_status = 31341; 12252 io->scsiio.be_move_done(io); 12253 return; 12254 } 12255 12256 msg.dt.sent_sg_entries = sg_entries_sent; 12257 } 12258 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12259 } else { 12260 12261 /* 12262 * Lookup the fe_datamove() function for this particular 12263 * front end. 12264 */ 12265 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12266 12267 fe_datamove(io); 12268 } 12269} 12270 12271static void 12272ctl_send_datamove_done(union ctl_io *io, int have_lock) 12273{ 12274 union ctl_ha_msg msg; 12275 12276 memset(&msg, 0, sizeof(msg)); 12277 12278 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12279 msg.hdr.original_sc = io; 12280 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12281 msg.hdr.nexus = io->io_hdr.nexus; 12282 msg.hdr.status = io->io_hdr.status; 12283 msg.scsi.tag_num = io->scsiio.tag_num; 12284 msg.scsi.tag_type = io->scsiio.tag_type; 12285 msg.scsi.scsi_status = io->scsiio.scsi_status; 12286 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12287 io->scsiio.sense_len); 12288 msg.scsi.sense_len = io->scsiio.sense_len; 12289 msg.scsi.sense_residual = io->scsiio.sense_residual; 12290 msg.scsi.fetd_status = io->io_hdr.port_status; 12291 msg.scsi.residual = io->scsiio.residual; 12292 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12293 12294 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12295 ctl_failover_io(io, /*have_lock*/ have_lock); 12296 return; 12297 } 12298 12299 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12300 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12301 msg.scsi.sense_len, M_WAITOK); 12302} 12303 12304/* 12305 * The DMA to the remote side is done, now we need to tell the other side 12306 * we're done so it can continue with its data movement. 12307 */ 12308static void 12309ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12310{ 12311 union ctl_io *io; 12312 int i; 12313 12314 io = rq->context; 12315 12316 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12317 printf("%s: ISC DMA write failed with error %d", __func__, 12318 rq->ret); 12319 ctl_set_internal_failure(&io->scsiio, 12320 /*sks_valid*/ 1, 12321 /*retry_count*/ rq->ret); 12322 } 12323 12324 ctl_dt_req_free(rq); 12325 12326 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12327 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12328 free(io->io_hdr.remote_sglist, M_CTL); 12329 io->io_hdr.remote_sglist = NULL; 12330 io->io_hdr.local_sglist = NULL; 12331 12332 /* 12333 * The data is in local and remote memory, so now we need to send 12334 * status (good or back) back to the other side. 12335 */ 12336 ctl_send_datamove_done(io, /*have_lock*/ 0); 12337} 12338 12339/* 12340 * We've moved the data from the host/controller into local memory. Now we 12341 * need to push it over to the remote controller's memory. 12342 */ 12343static int 12344ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12345{ 12346 int retval; 12347 12348 retval = 0; 12349 12350 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12351 ctl_datamove_remote_write_cb); 12352 12353 return (retval); 12354} 12355 12356static void 12357ctl_datamove_remote_write(union ctl_io *io) 12358{ 12359 int retval; 12360 void (*fe_datamove)(union ctl_io *io); 12361 12362 /* 12363 * - Get the data from the host/HBA into local memory. 12364 * - DMA memory from the local controller to the remote controller. 12365 * - Send status back to the remote controller. 12366 */ 12367 12368 retval = ctl_datamove_remote_sgl_setup(io); 12369 if (retval != 0) 12370 return; 12371 12372 /* Switch the pointer over so the FETD knows what to do */ 12373 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12374 12375 /* 12376 * Use a custom move done callback, since we need to send completion 12377 * back to the other controller, not to the backend on this side. 12378 */ 12379 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12380 12381 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12382 12383 fe_datamove(io); 12384 12385 return; 12386 12387} 12388 12389static int 12390ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12391{ 12392#if 0 12393 char str[256]; 12394 char path_str[64]; 12395 struct sbuf sb; 12396#endif 12397 int i; 12398 12399 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12400 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12401 free(io->io_hdr.remote_sglist, M_CTL); 12402 io->io_hdr.remote_sglist = NULL; 12403 io->io_hdr.local_sglist = NULL; 12404 12405#if 0 12406 scsi_path_string(io, path_str, sizeof(path_str)); 12407 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12408 sbuf_cat(&sb, path_str); 12409 scsi_command_string(&io->scsiio, NULL, &sb); 12410 sbuf_printf(&sb, "\n"); 12411 sbuf_cat(&sb, path_str); 12412 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12413 io->scsiio.tag_num, io->scsiio.tag_type); 12414 sbuf_cat(&sb, path_str); 12415 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12416 io->io_hdr.flags, io->io_hdr.status); 12417 sbuf_finish(&sb); 12418 printk("%s", sbuf_data(&sb)); 12419#endif 12420 12421 12422 /* 12423 * The read is done, now we need to send status (good or bad) back 12424 * to the other side. 12425 */ 12426 ctl_send_datamove_done(io, /*have_lock*/ 0); 12427 12428 return (0); 12429} 12430 12431static void 12432ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12433{ 12434 union ctl_io *io; 12435 void (*fe_datamove)(union ctl_io *io); 12436 12437 io = rq->context; 12438 12439 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12440 printf("%s: ISC DMA read failed with error %d\n", __func__, 12441 rq->ret); 12442 ctl_set_internal_failure(&io->scsiio, 12443 /*sks_valid*/ 1, 12444 /*retry_count*/ rq->ret); 12445 } 12446 12447 ctl_dt_req_free(rq); 12448 12449 /* Switch the pointer over so the FETD knows what to do */ 12450 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12451 12452 /* 12453 * Use a custom move done callback, since we need to send completion 12454 * back to the other controller, not to the backend on this side. 12455 */ 12456 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12457 12458 /* XXX KDM add checks like the ones in ctl_datamove? */ 12459 12460 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12461 12462 fe_datamove(io); 12463} 12464 12465static int 12466ctl_datamove_remote_sgl_setup(union ctl_io *io) 12467{ 12468 struct ctl_sg_entry *local_sglist, *remote_sglist; 12469 struct ctl_softc *softc; 12470 uint32_t len_to_go; 12471 int retval; 12472 int i; 12473 12474 retval = 0; 12475 softc = control_softc; 12476 local_sglist = io->io_hdr.local_sglist; 12477 remote_sglist = io->io_hdr.remote_sglist; 12478 len_to_go = io->scsiio.kern_data_len; 12479 12480 /* 12481 * The difficult thing here is that the size of the various 12482 * S/G segments may be different than the size from the 12483 * remote controller. That'll make it harder when DMAing 12484 * the data back to the other side. 12485 */ 12486 for (i = 0; len_to_go > 0; i++) { 12487 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12488 local_sglist[i].addr = 12489 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12490 12491 len_to_go -= local_sglist[i].len; 12492 } 12493 /* 12494 * Reset the number of S/G entries accordingly. The original 12495 * number of S/G entries is available in rem_sg_entries. 12496 */ 12497 io->scsiio.kern_sg_entries = i; 12498 12499#if 0 12500 printf("%s: kern_sg_entries = %d\n", __func__, 12501 io->scsiio.kern_sg_entries); 12502 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12503 printf("%s: sg[%d] = %p, %d\n", __func__, i, 12504 local_sglist[i].addr, local_sglist[i].len); 12505#endif 12506 12507 return (retval); 12508} 12509 12510static int 12511ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12512 ctl_ha_dt_cb callback) 12513{ 12514 struct ctl_ha_dt_req *rq; 12515 struct ctl_sg_entry *remote_sglist, *local_sglist; 12516 uint32_t local_used, remote_used, total_used; 12517 int i, j, isc_ret; 12518 12519 rq = ctl_dt_req_alloc(); 12520 12521 /* 12522 * If we failed to allocate the request, and if the DMA didn't fail 12523 * anyway, set busy status. This is just a resource allocation 12524 * failure. 12525 */ 12526 if ((rq == NULL) 12527 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 12528 ctl_set_busy(&io->scsiio); 12529 12530 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 12531 12532 if (rq != NULL) 12533 ctl_dt_req_free(rq); 12534 12535 /* 12536 * The data move failed. We need to return status back 12537 * to the other controller. No point in trying to DMA 12538 * data to the remote controller. 12539 */ 12540 12541 ctl_send_datamove_done(io, /*have_lock*/ 0); 12542 12543 return (1); 12544 } 12545 12546 local_sglist = io->io_hdr.local_sglist; 12547 remote_sglist = io->io_hdr.remote_sglist; 12548 local_used = 0; 12549 remote_used = 0; 12550 total_used = 0; 12551 12552 /* 12553 * Pull/push the data over the wire from/to the other controller. 12554 * This takes into account the possibility that the local and 12555 * remote sglists may not be identical in terms of the size of 12556 * the elements and the number of elements. 12557 * 12558 * One fundamental assumption here is that the length allocated for 12559 * both the local and remote sglists is identical. Otherwise, we've 12560 * essentially got a coding error of some sort. 12561 */ 12562 isc_ret = CTL_HA_STATUS_SUCCESS; 12563 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12564 uint32_t cur_len; 12565 uint8_t *tmp_ptr; 12566 12567 rq->command = command; 12568 rq->context = io; 12569 12570 /* 12571 * Both pointers should be aligned. But it is possible 12572 * that the allocation length is not. They should both 12573 * also have enough slack left over at the end, though, 12574 * to round up to the next 8 byte boundary. 12575 */ 12576 cur_len = MIN(local_sglist[i].len - local_used, 12577 remote_sglist[j].len - remote_used); 12578 rq->size = cur_len; 12579 12580 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12581 tmp_ptr += local_used; 12582 12583#if 0 12584 /* Use physical addresses when talking to ISC hardware */ 12585 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12586 /* XXX KDM use busdma */ 12587 rq->local = vtophys(tmp_ptr); 12588 } else 12589 rq->local = tmp_ptr; 12590#else 12591 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12592 ("HA does not support BUS_ADDR")); 12593 rq->local = tmp_ptr; 12594#endif 12595 12596 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12597 tmp_ptr += remote_used; 12598 rq->remote = tmp_ptr; 12599 12600 rq->callback = NULL; 12601 12602 local_used += cur_len; 12603 if (local_used >= local_sglist[i].len) { 12604 i++; 12605 local_used = 0; 12606 } 12607 12608 remote_used += cur_len; 12609 if (remote_used >= remote_sglist[j].len) { 12610 j++; 12611 remote_used = 0; 12612 } 12613 total_used += cur_len; 12614 12615 if (total_used >= io->scsiio.kern_data_len) 12616 rq->callback = callback; 12617 12618#if 0 12619 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 12620 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12621 rq->local, rq->remote, rq->size); 12622#endif 12623 12624 isc_ret = ctl_dt_single(rq); 12625 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12626 break; 12627 } 12628 if (isc_ret != CTL_HA_STATUS_WAIT) { 12629 rq->ret = isc_ret; 12630 callback(rq); 12631 } 12632 12633 return (0); 12634} 12635 12636static void 12637ctl_datamove_remote_read(union ctl_io *io) 12638{ 12639 int retval; 12640 int i; 12641 12642 /* 12643 * This will send an error to the other controller in the case of a 12644 * failure. 12645 */ 12646 retval = ctl_datamove_remote_sgl_setup(io); 12647 if (retval != 0) 12648 return; 12649 12650 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12651 ctl_datamove_remote_read_cb); 12652 if (retval != 0) { 12653 /* 12654 * Make sure we free memory if there was an error.. The 12655 * ctl_datamove_remote_xfer() function will send the 12656 * datamove done message, or call the callback with an 12657 * error if there is a problem. 12658 */ 12659 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12660 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12661 free(io->io_hdr.remote_sglist, M_CTL); 12662 io->io_hdr.remote_sglist = NULL; 12663 io->io_hdr.local_sglist = NULL; 12664 } 12665 12666 return; 12667} 12668 12669/* 12670 * Process a datamove request from the other controller. This is used for 12671 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12672 * first. Once that is complete, the data gets DMAed into the remote 12673 * controller's memory. For reads, we DMA from the remote controller's 12674 * memory into our memory first, and then move it out to the FETD. 12675 */ 12676static void 12677ctl_datamove_remote(union ctl_io *io) 12678{ 12679 12680 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12681 12682 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12683 ctl_failover_io(io, /*have_lock*/ 0); 12684 return; 12685 } 12686 12687 /* 12688 * Note that we look for an aborted I/O here, but don't do some of 12689 * the other checks that ctl_datamove() normally does. 12690 * We don't need to run the datamove delay code, since that should 12691 * have been done if need be on the other controller. 12692 */ 12693 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12694 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 12695 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12696 io->io_hdr.nexus.targ_port, 12697 io->io_hdr.nexus.targ_lun); 12698 io->io_hdr.port_status = 31338; 12699 ctl_send_datamove_done(io, /*have_lock*/ 0); 12700 return; 12701 } 12702 12703 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 12704 ctl_datamove_remote_write(io); 12705 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 12706 ctl_datamove_remote_read(io); 12707 else { 12708 io->io_hdr.port_status = 31339; 12709 ctl_send_datamove_done(io, /*have_lock*/ 0); 12710 } 12711} 12712 12713static int 12714ctl_process_done(union ctl_io *io) 12715{ 12716 struct ctl_lun *lun; 12717 struct ctl_softc *softc = control_softc; 12718 void (*fe_done)(union ctl_io *io); 12719 union ctl_ha_msg msg; 12720 uint32_t targ_port = io->io_hdr.nexus.targ_port; 12721 12722 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12723 12724 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) 12725 fe_done = softc->ctl_ports[targ_port]->fe_done; 12726 else 12727 fe_done = NULL; 12728 12729#ifdef CTL_TIME_IO 12730 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12731 char str[256]; 12732 char path_str[64]; 12733 struct sbuf sb; 12734 12735 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12736 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12737 12738 sbuf_cat(&sb, path_str); 12739 switch (io->io_hdr.io_type) { 12740 case CTL_IO_SCSI: 12741 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12742 sbuf_printf(&sb, "\n"); 12743 sbuf_cat(&sb, path_str); 12744 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12745 io->scsiio.tag_num, io->scsiio.tag_type); 12746 break; 12747 case CTL_IO_TASK: 12748 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12749 "Tag Type: %d\n", io->taskio.task_action, 12750 io->taskio.tag_num, io->taskio.tag_type); 12751 break; 12752 default: 12753 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12754 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12755 break; 12756 } 12757 sbuf_cat(&sb, path_str); 12758 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 12759 (intmax_t)time_uptime - io->io_hdr.start_time); 12760 sbuf_finish(&sb); 12761 printf("%s", sbuf_data(&sb)); 12762 } 12763#endif /* CTL_TIME_IO */ 12764 12765 switch (io->io_hdr.io_type) { 12766 case CTL_IO_SCSI: 12767 break; 12768 case CTL_IO_TASK: 12769 if (ctl_debug & CTL_DEBUG_INFO) 12770 ctl_io_error_print(io, NULL); 12771 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 12772 ctl_free_io(io); 12773 else 12774 fe_done(io); 12775 return (CTL_RETVAL_COMPLETE); 12776 default: 12777 panic("ctl_process_done: invalid io type %d\n", 12778 io->io_hdr.io_type); 12779 break; /* NOTREACHED */ 12780 } 12781 12782 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12783 if (lun == NULL) { 12784 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 12785 io->io_hdr.nexus.targ_mapped_lun)); 12786 goto bailout; 12787 } 12788 12789 mtx_lock(&lun->lun_lock); 12790 12791 /* 12792 * Check to see if we have any errors to inject here. We only 12793 * inject errors for commands that don't already have errors set. 12794 */ 12795 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 12796 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 12797 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 12798 ctl_inject_error(lun, io); 12799 12800 /* 12801 * XXX KDM how do we treat commands that aren't completed 12802 * successfully? 12803 * 12804 * XXX KDM should we also track I/O latency? 12805 */ 12806 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 12807 io->io_hdr.io_type == CTL_IO_SCSI) { 12808#ifdef CTL_TIME_IO 12809 struct bintime cur_bt; 12810#endif 12811 int type; 12812 12813 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12814 CTL_FLAG_DATA_IN) 12815 type = CTL_STATS_READ; 12816 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12817 CTL_FLAG_DATA_OUT) 12818 type = CTL_STATS_WRITE; 12819 else 12820 type = CTL_STATS_NO_IO; 12821 12822 lun->stats.ports[targ_port].bytes[type] += 12823 io->scsiio.kern_total_len; 12824 lun->stats.ports[targ_port].operations[type]++; 12825#ifdef CTL_TIME_IO 12826 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 12827 &io->io_hdr.dma_bt); 12828 lun->stats.ports[targ_port].num_dmas[type] += 12829 io->io_hdr.num_dmas; 12830 getbintime(&cur_bt); 12831 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 12832 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 12833#endif 12834 } 12835 12836 /* 12837 * Remove this from the OOA queue. 12838 */ 12839 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12840#ifdef CTL_TIME_IO 12841 if (TAILQ_EMPTY(&lun->ooa_queue)) 12842 lun->last_busy = getsbinuptime(); 12843#endif 12844 12845 /* 12846 * Run through the blocked queue on this LUN and see if anything 12847 * has become unblocked, now that this transaction is done. 12848 */ 12849 ctl_check_blocked(lun); 12850 12851 /* 12852 * If the LUN has been invalidated, free it if there is nothing 12853 * left on its OOA queue. 12854 */ 12855 if ((lun->flags & CTL_LUN_INVALID) 12856 && TAILQ_EMPTY(&lun->ooa_queue)) { 12857 mtx_unlock(&lun->lun_lock); 12858 mtx_lock(&softc->ctl_lock); 12859 ctl_free_lun(lun); 12860 mtx_unlock(&softc->ctl_lock); 12861 } else 12862 mtx_unlock(&lun->lun_lock); 12863 12864bailout: 12865 12866 /* 12867 * If this command has been aborted, make sure we set the status 12868 * properly. The FETD is responsible for freeing the I/O and doing 12869 * whatever it needs to do to clean up its state. 12870 */ 12871 if (io->io_hdr.flags & CTL_FLAG_ABORT) 12872 ctl_set_task_aborted(&io->scsiio); 12873 12874 /* 12875 * If enabled, print command error status. 12876 */ 12877 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 12878 (ctl_debug & CTL_DEBUG_INFO) != 0) 12879 ctl_io_error_print(io, NULL); 12880 12881 /* 12882 * Tell the FETD or the other shelf controller we're done with this 12883 * command. Note that only SCSI commands get to this point. Task 12884 * management commands are completed above. 12885 */ 12886 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 12887 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 12888 memset(&msg, 0, sizeof(msg)); 12889 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 12890 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12891 msg.hdr.nexus = io->io_hdr.nexus; 12892 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12893 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 12894 M_WAITOK); 12895 } 12896 if ((softc->ha_mode == CTL_HA_MODE_XFER) 12897 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12898 memset(&msg, 0, sizeof(msg)); 12899 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 12900 msg.hdr.original_sc = io->io_hdr.original_sc; 12901 msg.hdr.nexus = io->io_hdr.nexus; 12902 msg.hdr.status = io->io_hdr.status; 12903 msg.scsi.scsi_status = io->scsiio.scsi_status; 12904 msg.scsi.tag_num = io->scsiio.tag_num; 12905 msg.scsi.tag_type = io->scsiio.tag_type; 12906 msg.scsi.sense_len = io->scsiio.sense_len; 12907 msg.scsi.sense_residual = io->scsiio.sense_residual; 12908 msg.scsi.residual = io->scsiio.residual; 12909 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12910 io->scsiio.sense_len); 12911 /* 12912 * We copy this whether or not this is an I/O-related 12913 * command. Otherwise, we'd have to go and check to see 12914 * whether it's a read/write command, and it really isn't 12915 * worth it. 12916 */ 12917 memcpy(&msg.scsi.lbalen, 12918 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 12919 sizeof(msg.scsi.lbalen)); 12920 12921 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12922 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12923 msg.scsi.sense_len, M_WAITOK); 12924 ctl_free_io(io); 12925 } else 12926 fe_done(io); 12927 12928 return (CTL_RETVAL_COMPLETE); 12929} 12930 12931#ifdef CTL_WITH_CA 12932/* 12933 * Front end should call this if it doesn't do autosense. When the request 12934 * sense comes back in from the initiator, we'll dequeue this and send it. 12935 */ 12936int 12937ctl_queue_sense(union ctl_io *io) 12938{ 12939 struct ctl_lun *lun; 12940 struct ctl_port *port; 12941 struct ctl_softc *softc; 12942 uint32_t initidx, targ_lun; 12943 12944 softc = control_softc; 12945 12946 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 12947 12948 /* 12949 * LUN lookup will likely move to the ctl_work_thread() once we 12950 * have our new queueing infrastructure (that doesn't put things on 12951 * a per-LUN queue initially). That is so that we can handle 12952 * things like an INQUIRY to a LUN that we don't have enabled. We 12953 * can't deal with that right now. 12954 */ 12955 mtx_lock(&softc->ctl_lock); 12956 12957 /* 12958 * If we don't have a LUN for this, just toss the sense 12959 * information. 12960 */ 12961 port = ctl_io_port(&ctsio->io_hdr); 12962 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 12963 if ((targ_lun < CTL_MAX_LUNS) 12964 && (softc->ctl_luns[targ_lun] != NULL)) 12965 lun = softc->ctl_luns[targ_lun]; 12966 else 12967 goto bailout; 12968 12969 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12970 12971 mtx_lock(&lun->lun_lock); 12972 /* 12973 * Already have CA set for this LUN...toss the sense information. 12974 */ 12975 if (ctl_is_set(lun->have_ca, initidx)) { 12976 mtx_unlock(&lun->lun_lock); 12977 goto bailout; 12978 } 12979 12980 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 12981 MIN(sizeof(lun->pending_sense[initidx]), 12982 sizeof(io->scsiio.sense_data))); 12983 ctl_set_mask(lun->have_ca, initidx); 12984 mtx_unlock(&lun->lun_lock); 12985 12986bailout: 12987 mtx_unlock(&softc->ctl_lock); 12988 12989 ctl_free_io(io); 12990 12991 return (CTL_RETVAL_COMPLETE); 12992} 12993#endif 12994 12995/* 12996 * Primary command inlet from frontend ports. All SCSI and task I/O 12997 * requests must go through this function. 12998 */ 12999int 13000ctl_queue(union ctl_io *io) 13001{ 13002 struct ctl_port *port; 13003 13004 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13005 13006#ifdef CTL_TIME_IO 13007 io->io_hdr.start_time = time_uptime; 13008 getbintime(&io->io_hdr.start_bt); 13009#endif /* CTL_TIME_IO */ 13010 13011 /* Map FE-specific LUN ID into global one. */ 13012 port = ctl_io_port(&io->io_hdr); 13013 io->io_hdr.nexus.targ_mapped_lun = 13014 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13015 13016 switch (io->io_hdr.io_type) { 13017 case CTL_IO_SCSI: 13018 case CTL_IO_TASK: 13019 if (ctl_debug & CTL_DEBUG_CDB) 13020 ctl_io_print(io); 13021 ctl_enqueue_incoming(io); 13022 break; 13023 default: 13024 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13025 return (EINVAL); 13026 } 13027 13028 return (CTL_RETVAL_COMPLETE); 13029} 13030 13031#ifdef CTL_IO_DELAY 13032static void 13033ctl_done_timer_wakeup(void *arg) 13034{ 13035 union ctl_io *io; 13036 13037 io = (union ctl_io *)arg; 13038 ctl_done(io); 13039} 13040#endif /* CTL_IO_DELAY */ 13041 13042void 13043ctl_done(union ctl_io *io) 13044{ 13045 13046 /* 13047 * Enable this to catch duplicate completion issues. 13048 */ 13049#if 0 13050 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13051 printf("%s: type %d msg %d cdb %x iptl: " 13052 "%u:%u:%u tag 0x%04x " 13053 "flag %#x status %x\n", 13054 __func__, 13055 io->io_hdr.io_type, 13056 io->io_hdr.msg_type, 13057 io->scsiio.cdb[0], 13058 io->io_hdr.nexus.initid, 13059 io->io_hdr.nexus.targ_port, 13060 io->io_hdr.nexus.targ_lun, 13061 (io->io_hdr.io_type == 13062 CTL_IO_TASK) ? 13063 io->taskio.tag_num : 13064 io->scsiio.tag_num, 13065 io->io_hdr.flags, 13066 io->io_hdr.status); 13067 } else 13068 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13069#endif 13070 13071 /* 13072 * This is an internal copy of an I/O, and should not go through 13073 * the normal done processing logic. 13074 */ 13075 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13076 return; 13077 13078#ifdef CTL_IO_DELAY 13079 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13080 struct ctl_lun *lun; 13081 13082 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13083 13084 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13085 } else { 13086 struct ctl_lun *lun; 13087 13088 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13089 13090 if ((lun != NULL) 13091 && (lun->delay_info.done_delay > 0)) { 13092 13093 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13094 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13095 callout_reset(&io->io_hdr.delay_callout, 13096 lun->delay_info.done_delay * hz, 13097 ctl_done_timer_wakeup, io); 13098 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13099 lun->delay_info.done_delay = 0; 13100 return; 13101 } 13102 } 13103#endif /* CTL_IO_DELAY */ 13104 13105 ctl_enqueue_done(io); 13106} 13107 13108static void 13109ctl_work_thread(void *arg) 13110{ 13111 struct ctl_thread *thr = (struct ctl_thread *)arg; 13112 struct ctl_softc *softc = thr->ctl_softc; 13113 union ctl_io *io; 13114 int retval; 13115 13116 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13117 13118 for (;;) { 13119 retval = 0; 13120 13121 /* 13122 * We handle the queues in this order: 13123 * - ISC 13124 * - done queue (to free up resources, unblock other commands) 13125 * - RtR queue 13126 * - incoming queue 13127 * 13128 * If those queues are empty, we break out of the loop and 13129 * go to sleep. 13130 */ 13131 mtx_lock(&thr->queue_lock); 13132 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13133 if (io != NULL) { 13134 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13135 mtx_unlock(&thr->queue_lock); 13136 ctl_handle_isc(io); 13137 continue; 13138 } 13139 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13140 if (io != NULL) { 13141 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13142 /* clear any blocked commands, call fe_done */ 13143 mtx_unlock(&thr->queue_lock); 13144 retval = ctl_process_done(io); 13145 continue; 13146 } 13147 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13148 if (io != NULL) { 13149 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13150 mtx_unlock(&thr->queue_lock); 13151 if (io->io_hdr.io_type == CTL_IO_TASK) 13152 ctl_run_task(io); 13153 else 13154 ctl_scsiio_precheck(softc, &io->scsiio); 13155 continue; 13156 } 13157 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13158 if (io != NULL) { 13159 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13160 mtx_unlock(&thr->queue_lock); 13161 retval = ctl_scsiio(&io->scsiio); 13162 if (retval != CTL_RETVAL_COMPLETE) 13163 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13164 continue; 13165 } 13166 13167 /* Sleep until we have something to do. */ 13168 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13169 } 13170} 13171 13172static void 13173ctl_lun_thread(void *arg) 13174{ 13175 struct ctl_softc *softc = (struct ctl_softc *)arg; 13176 struct ctl_be_lun *be_lun; 13177 int retval; 13178 13179 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13180 13181 for (;;) { 13182 retval = 0; 13183 mtx_lock(&softc->ctl_lock); 13184 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13185 if (be_lun != NULL) { 13186 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13187 mtx_unlock(&softc->ctl_lock); 13188 ctl_create_lun(be_lun); 13189 continue; 13190 } 13191 13192 /* Sleep until we have something to do. */ 13193 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13194 PDROP | PRIBIO, "-", 0); 13195 } 13196} 13197 13198static void 13199ctl_thresh_thread(void *arg) 13200{ 13201 struct ctl_softc *softc = (struct ctl_softc *)arg; 13202 struct ctl_lun *lun; 13203 struct ctl_be_lun *be_lun; 13204 struct scsi_da_rw_recovery_page *rwpage; 13205 struct ctl_logical_block_provisioning_page *page; 13206 const char *attr; 13207 union ctl_ha_msg msg; 13208 uint64_t thres, val; 13209 int i, e, set; 13210 13211 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13212 13213 for (;;) { 13214 mtx_lock(&softc->ctl_lock); 13215 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13216 be_lun = lun->be_lun; 13217 if ((lun->flags & CTL_LUN_DISABLED) || 13218 (lun->flags & CTL_LUN_OFFLINE) || 13219 lun->backend->lun_attr == NULL) 13220 continue; 13221 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13222 softc->ha_mode == CTL_HA_MODE_XFER) 13223 continue; 13224 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13225 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13226 continue; 13227 e = 0; 13228 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13229 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13230 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13231 continue; 13232 thres = scsi_4btoul(page->descr[i].count); 13233 thres <<= CTL_LBP_EXPONENT; 13234 switch (page->descr[i].resource) { 13235 case 0x01: 13236 attr = "blocksavail"; 13237 break; 13238 case 0x02: 13239 attr = "blocksused"; 13240 break; 13241 case 0xf1: 13242 attr = "poolblocksavail"; 13243 break; 13244 case 0xf2: 13245 attr = "poolblocksused"; 13246 break; 13247 default: 13248 continue; 13249 } 13250 mtx_unlock(&softc->ctl_lock); // XXX 13251 val = lun->backend->lun_attr( 13252 lun->be_lun->be_lun, attr); 13253 mtx_lock(&softc->ctl_lock); 13254 if (val == UINT64_MAX) 13255 continue; 13256 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13257 == SLBPPD_ARMING_INC) 13258 e |= (val >= thres); 13259 else 13260 e |= (val <= thres); 13261 } 13262 mtx_lock(&lun->lun_lock); 13263 if (e) { 13264 if (lun->lasttpt == 0 || 13265 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13266 lun->lasttpt = time_uptime; 13267 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13268 set = 1; 13269 } else 13270 set = 0; 13271 } else { 13272 lun->lasttpt = 0; 13273 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13274 set = -1; 13275 } 13276 mtx_unlock(&lun->lun_lock); 13277 if (set != 0 && 13278 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13279 /* Send msg to other side. */ 13280 bzero(&msg.ua, sizeof(msg.ua)); 13281 msg.hdr.msg_type = CTL_MSG_UA; 13282 msg.hdr.nexus.initid = -1; 13283 msg.hdr.nexus.targ_port = -1; 13284 msg.hdr.nexus.targ_lun = lun->lun; 13285 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13286 msg.ua.ua_all = 1; 13287 msg.ua.ua_set = (set > 0); 13288 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13289 mtx_unlock(&softc->ctl_lock); // XXX 13290 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13291 sizeof(msg.ua), M_WAITOK); 13292 mtx_lock(&softc->ctl_lock); 13293 } 13294 } 13295 mtx_unlock(&softc->ctl_lock); 13296 pause("-", CTL_LBP_PERIOD * hz); 13297 } 13298} 13299 13300static void 13301ctl_enqueue_incoming(union ctl_io *io) 13302{ 13303 struct ctl_softc *softc = control_softc; 13304 struct ctl_thread *thr; 13305 u_int idx; 13306 13307 idx = (io->io_hdr.nexus.targ_port * 127 + 13308 io->io_hdr.nexus.initid) % worker_threads; 13309 thr = &softc->threads[idx]; 13310 mtx_lock(&thr->queue_lock); 13311 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13312 mtx_unlock(&thr->queue_lock); 13313 wakeup(thr); 13314} 13315 13316static void 13317ctl_enqueue_rtr(union ctl_io *io) 13318{ 13319 struct ctl_softc *softc = control_softc; 13320 struct ctl_thread *thr; 13321 13322 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13323 mtx_lock(&thr->queue_lock); 13324 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13325 mtx_unlock(&thr->queue_lock); 13326 wakeup(thr); 13327} 13328 13329static void 13330ctl_enqueue_done(union ctl_io *io) 13331{ 13332 struct ctl_softc *softc = control_softc; 13333 struct ctl_thread *thr; 13334 13335 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13336 mtx_lock(&thr->queue_lock); 13337 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13338 mtx_unlock(&thr->queue_lock); 13339 wakeup(thr); 13340} 13341 13342static void 13343ctl_enqueue_isc(union ctl_io *io) 13344{ 13345 struct ctl_softc *softc = control_softc; 13346 struct ctl_thread *thr; 13347 13348 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13349 mtx_lock(&thr->queue_lock); 13350 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13351 mtx_unlock(&thr->queue_lock); 13352 wakeup(thr); 13353} 13354 13355/* 13356 * vim: ts=8 13357 */ 13358