ctl.c revision 286806
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id$ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl.c 286806 2015-08-15 13:34:38Z mav $"); 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/ctype.h> 50#include <sys/kernel.h> 51#include <sys/types.h> 52#include <sys/kthread.h> 53#include <sys/bio.h> 54#include <sys/fcntl.h> 55#include <sys/lock.h> 56#include <sys/module.h> 57#include <sys/mutex.h> 58#include <sys/condvar.h> 59#include <sys/malloc.h> 60#include <sys/conf.h> 61#include <sys/ioccom.h> 62#include <sys/queue.h> 63#include <sys/sbuf.h> 64#include <sys/smp.h> 65#include <sys/endian.h> 66#include <sys/sysctl.h> 67#include <vm/uma.h> 68 69#include <cam/cam.h> 70#include <cam/scsi/scsi_all.h> 71#include <cam/scsi/scsi_da.h> 72#include <cam/ctl/ctl_io.h> 73#include <cam/ctl/ctl.h> 74#include <cam/ctl/ctl_frontend.h> 75#include <cam/ctl/ctl_util.h> 76#include <cam/ctl/ctl_backend.h> 77#include <cam/ctl/ctl_ioctl.h> 78#include <cam/ctl/ctl_ha.h> 79#include <cam/ctl/ctl_private.h> 80#include <cam/ctl/ctl_debug.h> 81#include <cam/ctl/ctl_scsi_all.h> 82#include <cam/ctl/ctl_error.h> 83 84struct ctl_softc *control_softc = NULL; 85 86/* 87 * Size and alignment macros needed for Copan-specific HA hardware. These 88 * can go away when the HA code is re-written, and uses busdma for any 89 * hardware. 90 */ 91#define CTL_ALIGN_8B(target, source, type) \ 92 if (((uint32_t)source & 0x7) != 0) \ 93 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 94 else \ 95 target = (type)source; 96 97#define CTL_SIZE_8B(target, size) \ 98 if ((size & 0x7) != 0) \ 99 target = size + (0x8 - (size & 0x7)); \ 100 else \ 101 target = size; 102 103#define CTL_ALIGN_8B_MARGIN 16 104 105/* 106 * Template mode pages. 107 */ 108 109/* 110 * Note that these are default values only. The actual values will be 111 * filled in when the user does a mode sense. 112 */ 113const static struct copan_debugconf_subpage debugconf_page_default = { 114 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 115 DBGCNF_SUBPAGE_CODE, /* subpage */ 116 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 117 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 118 DBGCNF_VERSION, /* page_version */ 119 {CTL_TIME_IO_DEFAULT_SECS>>8, 120 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 121}; 122 123const static struct copan_debugconf_subpage debugconf_page_changeable = { 124 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 125 DBGCNF_SUBPAGE_CODE, /* subpage */ 126 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 127 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 128 0, /* page_version */ 129 {0xff,0xff}, /* ctl_time_io_secs */ 130}; 131 132const static struct scsi_da_rw_recovery_page rw_er_page_default = { 133 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 134 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 135 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 136 /*read_retry_count*/0, 137 /*correction_span*/0, 138 /*head_offset_count*/0, 139 /*data_strobe_offset_cnt*/0, 140 /*byte8*/SMS_RWER_LBPERE, 141 /*write_retry_count*/0, 142 /*reserved2*/0, 143 /*recovery_time_limit*/{0, 0}, 144}; 145 146const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 147 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 148 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 149 /*byte3*/0, 150 /*read_retry_count*/0, 151 /*correction_span*/0, 152 /*head_offset_count*/0, 153 /*data_strobe_offset_cnt*/0, 154 /*byte8*/0, 155 /*write_retry_count*/0, 156 /*reserved2*/0, 157 /*recovery_time_limit*/{0, 0}, 158}; 159 160const static struct scsi_format_page format_page_default = { 161 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 162 /*page_length*/sizeof(struct scsi_format_page) - 2, 163 /*tracks_per_zone*/ {0, 0}, 164 /*alt_sectors_per_zone*/ {0, 0}, 165 /*alt_tracks_per_zone*/ {0, 0}, 166 /*alt_tracks_per_lun*/ {0, 0}, 167 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 168 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 169 /*bytes_per_sector*/ {0, 0}, 170 /*interleave*/ {0, 0}, 171 /*track_skew*/ {0, 0}, 172 /*cylinder_skew*/ {0, 0}, 173 /*flags*/ SFP_HSEC, 174 /*reserved*/ {0, 0, 0} 175}; 176 177const static struct scsi_format_page format_page_changeable = { 178 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 179 /*page_length*/sizeof(struct scsi_format_page) - 2, 180 /*tracks_per_zone*/ {0, 0}, 181 /*alt_sectors_per_zone*/ {0, 0}, 182 /*alt_tracks_per_zone*/ {0, 0}, 183 /*alt_tracks_per_lun*/ {0, 0}, 184 /*sectors_per_track*/ {0, 0}, 185 /*bytes_per_sector*/ {0, 0}, 186 /*interleave*/ {0, 0}, 187 /*track_skew*/ {0, 0}, 188 /*cylinder_skew*/ {0, 0}, 189 /*flags*/ 0, 190 /*reserved*/ {0, 0, 0} 191}; 192 193const static struct scsi_rigid_disk_page rigid_disk_page_default = { 194 /*page_code*/SMS_RIGID_DISK_PAGE, 195 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 196 /*cylinders*/ {0, 0, 0}, 197 /*heads*/ CTL_DEFAULT_HEADS, 198 /*start_write_precomp*/ {0, 0, 0}, 199 /*start_reduced_current*/ {0, 0, 0}, 200 /*step_rate*/ {0, 0}, 201 /*landing_zone_cylinder*/ {0, 0, 0}, 202 /*rpl*/ SRDP_RPL_DISABLED, 203 /*rotational_offset*/ 0, 204 /*reserved1*/ 0, 205 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 206 CTL_DEFAULT_ROTATION_RATE & 0xff}, 207 /*reserved2*/ {0, 0} 208}; 209 210const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 211 /*page_code*/SMS_RIGID_DISK_PAGE, 212 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 213 /*cylinders*/ {0, 0, 0}, 214 /*heads*/ 0, 215 /*start_write_precomp*/ {0, 0, 0}, 216 /*start_reduced_current*/ {0, 0, 0}, 217 /*step_rate*/ {0, 0}, 218 /*landing_zone_cylinder*/ {0, 0, 0}, 219 /*rpl*/ 0, 220 /*rotational_offset*/ 0, 221 /*reserved1*/ 0, 222 /*rotation_rate*/ {0, 0}, 223 /*reserved2*/ {0, 0} 224}; 225 226const static struct scsi_caching_page caching_page_default = { 227 /*page_code*/SMS_CACHING_PAGE, 228 /*page_length*/sizeof(struct scsi_caching_page) - 2, 229 /*flags1*/ SCP_DISC | SCP_WCE, 230 /*ret_priority*/ 0, 231 /*disable_pf_transfer_len*/ {0xff, 0xff}, 232 /*min_prefetch*/ {0, 0}, 233 /*max_prefetch*/ {0xff, 0xff}, 234 /*max_pf_ceiling*/ {0xff, 0xff}, 235 /*flags2*/ 0, 236 /*cache_segments*/ 0, 237 /*cache_seg_size*/ {0, 0}, 238 /*reserved*/ 0, 239 /*non_cache_seg_size*/ {0, 0, 0} 240}; 241 242const static struct scsi_caching_page caching_page_changeable = { 243 /*page_code*/SMS_CACHING_PAGE, 244 /*page_length*/sizeof(struct scsi_caching_page) - 2, 245 /*flags1*/ SCP_WCE | SCP_RCD, 246 /*ret_priority*/ 0, 247 /*disable_pf_transfer_len*/ {0, 0}, 248 /*min_prefetch*/ {0, 0}, 249 /*max_prefetch*/ {0, 0}, 250 /*max_pf_ceiling*/ {0, 0}, 251 /*flags2*/ 0, 252 /*cache_segments*/ 0, 253 /*cache_seg_size*/ {0, 0}, 254 /*reserved*/ 0, 255 /*non_cache_seg_size*/ {0, 0, 0} 256}; 257 258const static struct scsi_control_page control_page_default = { 259 /*page_code*/SMS_CONTROL_MODE_PAGE, 260 /*page_length*/sizeof(struct scsi_control_page) - 2, 261 /*rlec*/0, 262 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 263 /*eca_and_aen*/0, 264 /*flags4*/SCP_TAS, 265 /*aen_holdoff_period*/{0, 0}, 266 /*busy_timeout_period*/{0, 0}, 267 /*extended_selftest_completion_time*/{0, 0} 268}; 269 270const static struct scsi_control_page control_page_changeable = { 271 /*page_code*/SMS_CONTROL_MODE_PAGE, 272 /*page_length*/sizeof(struct scsi_control_page) - 2, 273 /*rlec*/SCP_DSENSE, 274 /*queue_flags*/SCP_QUEUE_ALG_MASK, 275 /*eca_and_aen*/SCP_SWP, 276 /*flags4*/0, 277 /*aen_holdoff_period*/{0, 0}, 278 /*busy_timeout_period*/{0, 0}, 279 /*extended_selftest_completion_time*/{0, 0} 280}; 281 282const static struct scsi_info_exceptions_page ie_page_default = { 283 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 284 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 285 /*info_flags*/SIEP_FLAGS_DEXCPT, 286 /*mrie*/0, 287 /*interval_timer*/{0, 0, 0, 0}, 288 /*report_count*/{0, 0, 0, 0} 289}; 290 291const static struct scsi_info_exceptions_page ie_page_changeable = { 292 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 293 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 294 /*info_flags*/0, 295 /*mrie*/0, 296 /*interval_timer*/{0, 0, 0, 0}, 297 /*report_count*/{0, 0, 0, 0} 298}; 299 300#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 301 302const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 303 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 304 /*subpage_code*/0x02, 305 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 306 /*flags*/0, 307 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 308 /*descr*/{}}, 309 {{/*flags*/0, 310 /*resource*/0x01, 311 /*reserved*/{0, 0}, 312 /*count*/{0, 0, 0, 0}}, 313 {/*flags*/0, 314 /*resource*/0x02, 315 /*reserved*/{0, 0}, 316 /*count*/{0, 0, 0, 0}}, 317 {/*flags*/0, 318 /*resource*/0xf1, 319 /*reserved*/{0, 0}, 320 /*count*/{0, 0, 0, 0}}, 321 {/*flags*/0, 322 /*resource*/0xf2, 323 /*reserved*/{0, 0}, 324 /*count*/{0, 0, 0, 0}} 325 } 326}; 327 328const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 329 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 330 /*subpage_code*/0x02, 331 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 332 /*flags*/0, 333 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 334 /*descr*/{}}, 335 {{/*flags*/0, 336 /*resource*/0, 337 /*reserved*/{0, 0}, 338 /*count*/{0, 0, 0, 0}}, 339 {/*flags*/0, 340 /*resource*/0, 341 /*reserved*/{0, 0}, 342 /*count*/{0, 0, 0, 0}}, 343 {/*flags*/0, 344 /*resource*/0, 345 /*reserved*/{0, 0}, 346 /*count*/{0, 0, 0, 0}}, 347 {/*flags*/0, 348 /*resource*/0, 349 /*reserved*/{0, 0}, 350 /*count*/{0, 0, 0, 0}} 351 } 352}; 353 354/* 355 * XXX KDM move these into the softc. 356 */ 357static int rcv_sync_msg; 358static uint8_t ctl_pause_rtr; 359 360SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 361static int worker_threads = -1; 362SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 363 &worker_threads, 1, "Number of worker threads"); 364static int ctl_debug = CTL_DEBUG_NONE; 365SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 366 &ctl_debug, 0, "Enabled debug flags"); 367 368/* 369 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 370 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 371 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 372 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 373 */ 374#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 375 376#ifdef notyet 377static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 378 int param); 379static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 380#endif 381static int ctl_init(void); 382void ctl_shutdown(void); 383static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 384static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 385static void ctl_ioctl_online(void *arg); 386static void ctl_ioctl_offline(void *arg); 387static int ctl_ioctl_lun_enable(void *arg, int lun_id); 388static int ctl_ioctl_lun_disable(void *arg, int lun_id); 389static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 390static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 391static int ctl_ioctl_submit_wait(union ctl_io *io); 392static void ctl_ioctl_datamove(union ctl_io *io); 393static void ctl_ioctl_done(union ctl_io *io); 394static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 395 struct ctl_ooa *ooa_hdr, 396 struct ctl_ooa_entry *kern_entries); 397static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 398 struct thread *td); 399static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 400 struct ctl_be_lun *be_lun); 401static int ctl_free_lun(struct ctl_lun *lun); 402static void ctl_create_lun(struct ctl_be_lun *be_lun); 403static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 404/** 405static void ctl_failover_change_pages(struct ctl_softc *softc, 406 struct ctl_scsiio *ctsio, int master); 407**/ 408 409static int ctl_do_mode_select(union ctl_io *io); 410static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 411 uint64_t res_key, uint64_t sa_res_key, 412 uint8_t type, uint32_t residx, 413 struct ctl_scsiio *ctsio, 414 struct scsi_per_res_out *cdb, 415 struct scsi_per_res_out_parms* param); 416static void ctl_pro_preempt_other(struct ctl_lun *lun, 417 union ctl_ha_msg *msg); 418static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 419static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 420static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 421static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 422static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 423static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 424static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 425 int alloc_len); 426static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 427 int alloc_len); 428static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 429static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 430static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 431static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 432static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 433static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 434 bool seq); 435static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 436static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 437 union ctl_io *pending_io, union ctl_io *ooa_io); 438static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 439 union ctl_io *starting_io); 440static int ctl_check_blocked(struct ctl_lun *lun); 441static int ctl_scsiio_lun_check(struct ctl_lun *lun, 442 const struct ctl_cmd_entry *entry, 443 struct ctl_scsiio *ctsio); 444//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 445#ifdef notyet 446static void ctl_failover(void); 447#endif 448static void ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx, 449 ctl_ua_type ua_type); 450static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 451 struct ctl_scsiio *ctsio); 452static int ctl_scsiio(struct ctl_scsiio *ctsio); 453 454static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 455static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 456 ctl_ua_type ua_type); 457static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 458 ctl_ua_type ua_type); 459static int ctl_abort_task(union ctl_io *io); 460static int ctl_abort_task_set(union ctl_io *io); 461static int ctl_i_t_nexus_reset(union ctl_io *io); 462static void ctl_run_task(union ctl_io *io); 463#ifdef CTL_IO_DELAY 464static void ctl_datamove_timer_wakeup(void *arg); 465static void ctl_done_timer_wakeup(void *arg); 466#endif /* CTL_IO_DELAY */ 467 468static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 469static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 470static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 471static void ctl_datamove_remote_write(union ctl_io *io); 472static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 473static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 474static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 475static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 476 ctl_ha_dt_cb callback); 477static void ctl_datamove_remote_read(union ctl_io *io); 478static void ctl_datamove_remote(union ctl_io *io); 479static int ctl_process_done(union ctl_io *io); 480static void ctl_lun_thread(void *arg); 481static void ctl_thresh_thread(void *arg); 482static void ctl_work_thread(void *arg); 483static void ctl_enqueue_incoming(union ctl_io *io); 484static void ctl_enqueue_rtr(union ctl_io *io); 485static void ctl_enqueue_done(union ctl_io *io); 486#ifdef notyet 487static void ctl_enqueue_isc(union ctl_io *io); 488#endif 489static const struct ctl_cmd_entry * 490 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 491static const struct ctl_cmd_entry * 492 ctl_validate_command(struct ctl_scsiio *ctsio); 493static int ctl_cmd_applicable(uint8_t lun_type, 494 const struct ctl_cmd_entry *entry); 495 496/* 497 * Load the serialization table. This isn't very pretty, but is probably 498 * the easiest way to do it. 499 */ 500#include "ctl_ser_table.c" 501 502/* 503 * We only need to define open, close and ioctl routines for this driver. 504 */ 505static struct cdevsw ctl_cdevsw = { 506 .d_version = D_VERSION, 507 .d_flags = 0, 508 .d_open = ctl_open, 509 .d_close = ctl_close, 510 .d_ioctl = ctl_ioctl, 511 .d_name = "ctl", 512}; 513 514 515MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 516 517static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 518 519static moduledata_t ctl_moduledata = { 520 "ctl", 521 ctl_module_event_handler, 522 NULL 523}; 524 525DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 526MODULE_VERSION(ctl, 1); 527 528static struct ctl_frontend ioctl_frontend = 529{ 530 .name = "ioctl", 531}; 532 533#ifdef notyet 534static void 535ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 536 union ctl_ha_msg *msg_info) 537{ 538 struct ctl_scsiio *ctsio; 539 540 if (msg_info->hdr.original_sc == NULL) { 541 printf("%s: original_sc == NULL!\n", __func__); 542 /* XXX KDM now what? */ 543 return; 544 } 545 546 ctsio = &msg_info->hdr.original_sc->scsiio; 547 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 548 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 549 ctsio->io_hdr.status = msg_info->hdr.status; 550 ctsio->scsi_status = msg_info->scsi.scsi_status; 551 ctsio->sense_len = msg_info->scsi.sense_len; 552 ctsio->sense_residual = msg_info->scsi.sense_residual; 553 ctsio->residual = msg_info->scsi.residual; 554 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 555 sizeof(ctsio->sense_data)); 556 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 557 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 558 ctl_enqueue_isc((union ctl_io *)ctsio); 559} 560 561static void 562ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 563 union ctl_ha_msg *msg_info) 564{ 565 struct ctl_scsiio *ctsio; 566 567 if (msg_info->hdr.serializing_sc == NULL) { 568 printf("%s: serializing_sc == NULL!\n", __func__); 569 /* XXX KDM now what? */ 570 return; 571 } 572 573 ctsio = &msg_info->hdr.serializing_sc->scsiio; 574#if 0 575 /* 576 * Attempt to catch the situation where an I/O has 577 * been freed, and we're using it again. 578 */ 579 if (ctsio->io_hdr.io_type == 0xff) { 580 union ctl_io *tmp_io; 581 tmp_io = (union ctl_io *)ctsio; 582 printf("%s: %p use after free!\n", __func__, 583 ctsio); 584 printf("%s: type %d msg %d cdb %x iptl: " 585 "%d:%d:%d:%d tag 0x%04x " 586 "flag %#x status %x\n", 587 __func__, 588 tmp_io->io_hdr.io_type, 589 tmp_io->io_hdr.msg_type, 590 tmp_io->scsiio.cdb[0], 591 tmp_io->io_hdr.nexus.initid.id, 592 tmp_io->io_hdr.nexus.targ_port, 593 tmp_io->io_hdr.nexus.targ_target.id, 594 tmp_io->io_hdr.nexus.targ_lun, 595 (tmp_io->io_hdr.io_type == 596 CTL_IO_TASK) ? 597 tmp_io->taskio.tag_num : 598 tmp_io->scsiio.tag_num, 599 tmp_io->io_hdr.flags, 600 tmp_io->io_hdr.status); 601 } 602#endif 603 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 604 ctl_enqueue_isc((union ctl_io *)ctsio); 605} 606 607/* 608 * ISC (Inter Shelf Communication) event handler. Events from the HA 609 * subsystem come in here. 610 */ 611static void 612ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 613{ 614 struct ctl_softc *softc; 615 union ctl_io *io; 616 struct ctl_prio *presio; 617 ctl_ha_status isc_status; 618 619 softc = control_softc; 620 io = NULL; 621 622 623#if 0 624 printf("CTL: Isc Msg event %d\n", event); 625#endif 626 if (event == CTL_HA_EVT_MSG_RECV) { 627 union ctl_ha_msg msg_info; 628 629 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 630 sizeof(msg_info), /*wait*/ 0); 631#if 0 632 printf("CTL: msg_type %d\n", msg_info.msg_type); 633#endif 634 if (isc_status != 0) { 635 printf("Error receiving message, status = %d\n", 636 isc_status); 637 return; 638 } 639 640 switch (msg_info.hdr.msg_type) { 641 case CTL_MSG_SERIALIZE: 642#if 0 643 printf("Serialize\n"); 644#endif 645 io = ctl_alloc_io_nowait(softc->othersc_pool); 646 if (io == NULL) { 647 printf("ctl_isc_event_handler: can't allocate " 648 "ctl_io!\n"); 649 /* Bad Juju */ 650 /* Need to set busy and send msg back */ 651 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 652 msg_info.hdr.status = CTL_SCSI_ERROR; 653 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 654 msg_info.scsi.sense_len = 0; 655 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 656 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 657 } 658 goto bailout; 659 } 660 ctl_zero_io(io); 661 // populate ctsio from msg_info 662 io->io_hdr.io_type = CTL_IO_SCSI; 663 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 664 io->io_hdr.original_sc = msg_info.hdr.original_sc; 665#if 0 666 printf("pOrig %x\n", (int)msg_info.original_sc); 667#endif 668 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 669 CTL_FLAG_IO_ACTIVE; 670 /* 671 * If we're in serialization-only mode, we don't 672 * want to go through full done processing. Thus 673 * the COPY flag. 674 * 675 * XXX KDM add another flag that is more specific. 676 */ 677 if (softc->ha_mode == CTL_HA_MODE_SER_ONLY) 678 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 679 io->io_hdr.nexus = msg_info.hdr.nexus; 680#if 0 681 printf("targ %d, port %d, iid %d, lun %d\n", 682 io->io_hdr.nexus.targ_target.id, 683 io->io_hdr.nexus.targ_port, 684 io->io_hdr.nexus.initid.id, 685 io->io_hdr.nexus.targ_lun); 686#endif 687 io->scsiio.tag_num = msg_info.scsi.tag_num; 688 io->scsiio.tag_type = msg_info.scsi.tag_type; 689 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 690 CTL_MAX_CDBLEN); 691 if (softc->ha_mode == CTL_HA_MODE_XFER) { 692 const struct ctl_cmd_entry *entry; 693 694 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 695 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 696 io->io_hdr.flags |= 697 entry->flags & CTL_FLAG_DATA_MASK; 698 } 699 ctl_enqueue_isc(io); 700 break; 701 702 /* Performed on the Originating SC, XFER mode only */ 703 case CTL_MSG_DATAMOVE: { 704 struct ctl_sg_entry *sgl; 705 int i, j; 706 707 io = msg_info.hdr.original_sc; 708 if (io == NULL) { 709 printf("%s: original_sc == NULL!\n", __func__); 710 /* XXX KDM do something here */ 711 break; 712 } 713 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 714 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 715 /* 716 * Keep track of this, we need to send it back over 717 * when the datamove is complete. 718 */ 719 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 720 721 if (msg_info.dt.sg_sequence == 0) { 722 /* 723 * XXX KDM we use the preallocated S/G list 724 * here, but we'll need to change this to 725 * dynamic allocation if we need larger S/G 726 * lists. 727 */ 728 if (msg_info.dt.kern_sg_entries > 729 sizeof(io->io_hdr.remote_sglist) / 730 sizeof(io->io_hdr.remote_sglist[0])) { 731 printf("%s: number of S/G entries " 732 "needed %u > allocated num %zd\n", 733 __func__, 734 msg_info.dt.kern_sg_entries, 735 sizeof(io->io_hdr.remote_sglist)/ 736 sizeof(io->io_hdr.remote_sglist[0])); 737 738 /* 739 * XXX KDM send a message back to 740 * the other side to shut down the 741 * DMA. The error will come back 742 * through via the normal channel. 743 */ 744 break; 745 } 746 sgl = io->io_hdr.remote_sglist; 747 memset(sgl, 0, 748 sizeof(io->io_hdr.remote_sglist)); 749 750 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 751 752 io->scsiio.kern_sg_entries = 753 msg_info.dt.kern_sg_entries; 754 io->scsiio.rem_sg_entries = 755 msg_info.dt.kern_sg_entries; 756 io->scsiio.kern_data_len = 757 msg_info.dt.kern_data_len; 758 io->scsiio.kern_total_len = 759 msg_info.dt.kern_total_len; 760 io->scsiio.kern_data_resid = 761 msg_info.dt.kern_data_resid; 762 io->scsiio.kern_rel_offset = 763 msg_info.dt.kern_rel_offset; 764 /* 765 * Clear out per-DMA flags. 766 */ 767 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 768 /* 769 * Add per-DMA flags that are set for this 770 * particular DMA request. 771 */ 772 io->io_hdr.flags |= msg_info.dt.flags & 773 CTL_FLAG_RDMA_MASK; 774 } else 775 sgl = (struct ctl_sg_entry *) 776 io->scsiio.kern_data_ptr; 777 778 for (i = msg_info.dt.sent_sg_entries, j = 0; 779 i < (msg_info.dt.sent_sg_entries + 780 msg_info.dt.cur_sg_entries); i++, j++) { 781 sgl[i].addr = msg_info.dt.sg_list[j].addr; 782 sgl[i].len = msg_info.dt.sg_list[j].len; 783 784#if 0 785 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 786 __func__, 787 msg_info.dt.sg_list[j].addr, 788 msg_info.dt.sg_list[j].len, 789 sgl[i].addr, sgl[i].len, j, i); 790#endif 791 } 792#if 0 793 memcpy(&sgl[msg_info.dt.sent_sg_entries], 794 msg_info.dt.sg_list, 795 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 796#endif 797 798 /* 799 * If this is the last piece of the I/O, we've got 800 * the full S/G list. Queue processing in the thread. 801 * Otherwise wait for the next piece. 802 */ 803 if (msg_info.dt.sg_last != 0) 804 ctl_enqueue_isc(io); 805 break; 806 } 807 /* Performed on the Serializing (primary) SC, XFER mode only */ 808 case CTL_MSG_DATAMOVE_DONE: { 809 if (msg_info.hdr.serializing_sc == NULL) { 810 printf("%s: serializing_sc == NULL!\n", 811 __func__); 812 /* XXX KDM now what? */ 813 break; 814 } 815 /* 816 * We grab the sense information here in case 817 * there was a failure, so we can return status 818 * back to the initiator. 819 */ 820 io = msg_info.hdr.serializing_sc; 821 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 822 io->io_hdr.status = msg_info.hdr.status; 823 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 824 io->scsiio.sense_len = msg_info.scsi.sense_len; 825 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 826 io->io_hdr.port_status = msg_info.scsi.fetd_status; 827 io->scsiio.residual = msg_info.scsi.residual; 828 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 829 sizeof(io->scsiio.sense_data)); 830 ctl_enqueue_isc(io); 831 break; 832 } 833 834 /* Preformed on Originating SC, SER_ONLY mode */ 835 case CTL_MSG_R2R: 836 io = msg_info.hdr.original_sc; 837 if (io == NULL) { 838 printf("%s: Major Bummer\n", __func__); 839 return; 840 } else { 841#if 0 842 printf("pOrig %x\n",(int) ctsio); 843#endif 844 } 845 io->io_hdr.msg_type = CTL_MSG_R2R; 846 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 847 ctl_enqueue_isc(io); 848 break; 849 850 /* 851 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 852 * mode. 853 * Performed on the Originating (i.e. secondary) SC in XFER 854 * mode 855 */ 856 case CTL_MSG_FINISH_IO: 857 if (softc->ha_mode == CTL_HA_MODE_XFER) 858 ctl_isc_handler_finish_xfer(softc, 859 &msg_info); 860 else 861 ctl_isc_handler_finish_ser_only(softc, 862 &msg_info); 863 break; 864 865 /* Preformed on Originating SC */ 866 case CTL_MSG_BAD_JUJU: 867 io = msg_info.hdr.original_sc; 868 if (io == NULL) { 869 printf("%s: Bad JUJU!, original_sc is NULL!\n", 870 __func__); 871 break; 872 } 873 ctl_copy_sense_data(&msg_info, io); 874 /* 875 * IO should have already been cleaned up on other 876 * SC so clear this flag so we won't send a message 877 * back to finish the IO there. 878 */ 879 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 880 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 881 882 /* io = msg_info.hdr.serializing_sc; */ 883 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 884 ctl_enqueue_isc(io); 885 break; 886 887 /* Handle resets sent from the other side */ 888 case CTL_MSG_MANAGE_TASKS: { 889 struct ctl_taskio *taskio; 890 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait( 891 softc->othersc_pool); 892 if (taskio == NULL) { 893 printf("ctl_isc_event_handler: can't allocate " 894 "ctl_io!\n"); 895 /* Bad Juju */ 896 /* should I just call the proper reset func 897 here??? */ 898 goto bailout; 899 } 900 ctl_zero_io((union ctl_io *)taskio); 901 taskio->io_hdr.io_type = CTL_IO_TASK; 902 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 903 taskio->io_hdr.nexus = msg_info.hdr.nexus; 904 taskio->task_action = msg_info.task.task_action; 905 taskio->tag_num = msg_info.task.tag_num; 906 taskio->tag_type = msg_info.task.tag_type; 907#ifdef CTL_TIME_IO 908 taskio->io_hdr.start_time = time_uptime; 909 getbintime(&taskio->io_hdr.start_bt); 910#if 0 911 cs_prof_gettime(&taskio->io_hdr.start_ticks); 912#endif 913#endif /* CTL_TIME_IO */ 914 ctl_run_task((union ctl_io *)taskio); 915 break; 916 } 917 /* Persistent Reserve action which needs attention */ 918 case CTL_MSG_PERS_ACTION: 919 presio = (struct ctl_prio *)ctl_alloc_io_nowait( 920 softc->othersc_pool); 921 if (presio == NULL) { 922 printf("ctl_isc_event_handler: can't allocate " 923 "ctl_io!\n"); 924 /* Bad Juju */ 925 /* Need to set busy and send msg back */ 926 goto bailout; 927 } 928 ctl_zero_io((union ctl_io *)presio); 929 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 930 presio->pr_msg = msg_info.pr; 931 ctl_enqueue_isc((union ctl_io *)presio); 932 break; 933 case CTL_MSG_SYNC_FE: 934 rcv_sync_msg = 1; 935 break; 936 default: 937 printf("How did I get here?\n"); 938 } 939 } else if (event == CTL_HA_EVT_MSG_SENT) { 940 if (param != CTL_HA_STATUS_SUCCESS) { 941 printf("Bad status from ctl_ha_msg_send status %d\n", 942 param); 943 } 944 return; 945 } else if (event == CTL_HA_EVT_DISCONNECT) { 946 printf("CTL: Got a disconnect from Isc\n"); 947 return; 948 } else { 949 printf("ctl_isc_event_handler: Unknown event %d\n", event); 950 return; 951 } 952 953bailout: 954 return; 955} 956 957static void 958ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 959{ 960 struct scsi_sense_data *sense; 961 962 sense = &dest->scsiio.sense_data; 963 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 964 dest->scsiio.scsi_status = src->scsi.scsi_status; 965 dest->scsiio.sense_len = src->scsi.sense_len; 966 dest->io_hdr.status = src->hdr.status; 967} 968#endif 969 970static void 971ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 972{ 973 ctl_ua_type *pu; 974 975 mtx_assert(&lun->lun_lock, MA_OWNED); 976 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 977 if (pu == NULL) 978 return; 979 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 980} 981 982static void 983ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 984{ 985 int i, j; 986 987 mtx_assert(&lun->lun_lock, MA_OWNED); 988 for (i = 0; i < CTL_MAX_PORTS; i++) { 989 if (lun->pending_ua[i] == NULL) 990 continue; 991 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 992 if (i * CTL_MAX_INIT_PER_PORT + j == except) 993 continue; 994 lun->pending_ua[i][j] |= ua; 995 } 996 } 997} 998 999static void 1000ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1001{ 1002 ctl_ua_type *pu; 1003 1004 mtx_assert(&lun->lun_lock, MA_OWNED); 1005 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1006 if (pu == NULL) 1007 return; 1008 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1009} 1010 1011static void 1012ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1013{ 1014 int i, j; 1015 1016 mtx_assert(&lun->lun_lock, MA_OWNED); 1017 for (i = 0; i < CTL_MAX_PORTS; i++) { 1018 if (lun->pending_ua[i] == NULL) 1019 continue; 1020 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1021 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1022 continue; 1023 lun->pending_ua[i][j] &= ~ua; 1024 } 1025 } 1026} 1027 1028static int 1029ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS) 1030{ 1031 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1032 struct ctl_lun *lun; 1033 int error, value; 1034 1035 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) 1036 value = 0; 1037 else 1038 value = 1; 1039 1040 error = sysctl_handle_int(oidp, &value, 0, req); 1041 if ((error != 0) || (req->newptr == NULL)) 1042 return (error); 1043 1044 mtx_lock(&softc->ctl_lock); 1045 if (value == 0) 1046 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1047 else 1048 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1049 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1050 mtx_lock(&lun->lun_lock); 1051 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1052 mtx_unlock(&lun->lun_lock); 1053 } 1054 mtx_unlock(&softc->ctl_lock); 1055 return (0); 1056} 1057 1058static int 1059ctl_init(void) 1060{ 1061 struct ctl_softc *softc; 1062 void *other_pool; 1063 struct ctl_port *port; 1064 int i, error, retval; 1065 //int isc_retval; 1066 1067 retval = 0; 1068 ctl_pause_rtr = 0; 1069 rcv_sync_msg = 0; 1070 1071 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1072 M_WAITOK | M_ZERO); 1073 softc = control_softc; 1074 1075 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1076 "cam/ctl"); 1077 1078 softc->dev->si_drv1 = softc; 1079 1080 /* 1081 * By default, return a "bad LUN" peripheral qualifier for unknown 1082 * LUNs. The user can override this default using the tunable or 1083 * sysctl. See the comment in ctl_inquiry_std() for more details. 1084 */ 1085 softc->inquiry_pq_no_lun = 1; 1086 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 1087 &softc->inquiry_pq_no_lun); 1088 sysctl_ctx_init(&softc->sysctl_ctx); 1089 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1090 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1091 CTLFLAG_RD, 0, "CAM Target Layer"); 1092 1093 if (softc->sysctl_tree == NULL) { 1094 printf("%s: unable to allocate sysctl tree\n", __func__); 1095 destroy_dev(softc->dev); 1096 free(control_softc, M_DEVBUF); 1097 control_softc = NULL; 1098 return (ENOMEM); 1099 } 1100 1101 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1102 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1103 "inquiry_pq_no_lun", CTLFLAG_RW, 1104 &softc->inquiry_pq_no_lun, 0, 1105 "Report no lun possible for invalid LUNs"); 1106 1107 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1108 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1109 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1110 softc->open_count = 0; 1111 1112 /* 1113 * Default to actually sending a SYNCHRONIZE CACHE command down to 1114 * the drive. 1115 */ 1116 softc->flags = CTL_FLAG_REAL_SYNC; 1117 1118 /* 1119 * In Copan's HA scheme, the "master" and "slave" roles are 1120 * figured out through the slot the controller is in. Although it 1121 * is an active/active system, someone has to be in charge. 1122 */ 1123 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1124 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1125 "HA head ID (0 - no HA)"); 1126 if (softc->ha_id == 0) { 1127 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1128 softc->is_single = 1; 1129 softc->port_offset = 0; 1130 } else 1131 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS; 1132 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT; 1133 1134 STAILQ_INIT(&softc->lun_list); 1135 STAILQ_INIT(&softc->pending_lun_queue); 1136 STAILQ_INIT(&softc->fe_list); 1137 STAILQ_INIT(&softc->port_list); 1138 STAILQ_INIT(&softc->be_list); 1139 ctl_tpc_init(softc); 1140 1141 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1142 &other_pool) != 0) 1143 { 1144 printf("ctl: can't allocate %d entry other SC pool, " 1145 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1146 return (ENOMEM); 1147 } 1148 softc->othersc_pool = other_pool; 1149 1150 if (worker_threads <= 0) 1151 worker_threads = max(1, mp_ncpus / 4); 1152 if (worker_threads > CTL_MAX_THREADS) 1153 worker_threads = CTL_MAX_THREADS; 1154 1155 for (i = 0; i < worker_threads; i++) { 1156 struct ctl_thread *thr = &softc->threads[i]; 1157 1158 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1159 thr->ctl_softc = softc; 1160 STAILQ_INIT(&thr->incoming_queue); 1161 STAILQ_INIT(&thr->rtr_queue); 1162 STAILQ_INIT(&thr->done_queue); 1163 STAILQ_INIT(&thr->isc_queue); 1164 1165 error = kproc_kthread_add(ctl_work_thread, thr, 1166 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1167 if (error != 0) { 1168 printf("error creating CTL work thread!\n"); 1169 ctl_pool_free(other_pool); 1170 return (error); 1171 } 1172 } 1173 error = kproc_kthread_add(ctl_lun_thread, softc, 1174 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1175 if (error != 0) { 1176 printf("error creating CTL lun thread!\n"); 1177 ctl_pool_free(other_pool); 1178 return (error); 1179 } 1180 error = kproc_kthread_add(ctl_thresh_thread, softc, 1181 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1182 if (error != 0) { 1183 printf("error creating CTL threshold thread!\n"); 1184 ctl_pool_free(other_pool); 1185 return (error); 1186 } 1187 1188 /* 1189 * Initialize the ioctl front end. 1190 */ 1191 ctl_frontend_register(&ioctl_frontend); 1192 port = &softc->ioctl_info.port; 1193 port->frontend = &ioctl_frontend; 1194 sprintf(softc->ioctl_info.port_name, "ioctl"); 1195 port->port_type = CTL_PORT_IOCTL; 1196 port->num_requested_ctl_io = 100; 1197 port->port_name = softc->ioctl_info.port_name; 1198 port->port_online = ctl_ioctl_online; 1199 port->port_offline = ctl_ioctl_offline; 1200 port->onoff_arg = &softc->ioctl_info; 1201 port->lun_enable = ctl_ioctl_lun_enable; 1202 port->lun_disable = ctl_ioctl_lun_disable; 1203 port->targ_lun_arg = &softc->ioctl_info; 1204 port->fe_datamove = ctl_ioctl_datamove; 1205 port->fe_done = ctl_ioctl_done; 1206 port->max_targets = 15; 1207 port->max_target_id = 15; 1208 1209 if (ctl_port_register(&softc->ioctl_info.port) != 0) { 1210 printf("ctl: ioctl front end registration failed, will " 1211 "continue anyway\n"); 1212 } 1213 1214 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1215 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN, 1216 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head"); 1217 1218#ifdef CTL_IO_DELAY 1219 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1220 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1221 sizeof(struct callout), CTL_TIMER_BYTES); 1222 return (EINVAL); 1223 } 1224#endif /* CTL_IO_DELAY */ 1225 1226 return (0); 1227} 1228 1229void 1230ctl_shutdown(void) 1231{ 1232 struct ctl_softc *softc; 1233 struct ctl_lun *lun, *next_lun; 1234 1235 softc = (struct ctl_softc *)control_softc; 1236 1237 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1238 printf("ctl: ioctl front end deregistration failed\n"); 1239 1240 mtx_lock(&softc->ctl_lock); 1241 1242 /* 1243 * Free up each LUN. 1244 */ 1245 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1246 next_lun = STAILQ_NEXT(lun, links); 1247 ctl_free_lun(lun); 1248 } 1249 1250 mtx_unlock(&softc->ctl_lock); 1251 1252 ctl_frontend_deregister(&ioctl_frontend); 1253 1254#if 0 1255 ctl_shutdown_thread(softc->work_thread); 1256 mtx_destroy(&softc->queue_lock); 1257#endif 1258 1259 ctl_tpc_shutdown(softc); 1260 uma_zdestroy(softc->io_zone); 1261 mtx_destroy(&softc->ctl_lock); 1262 1263 destroy_dev(softc->dev); 1264 1265 sysctl_ctx_free(&softc->sysctl_ctx); 1266 1267 free(control_softc, M_DEVBUF); 1268 control_softc = NULL; 1269} 1270 1271static int 1272ctl_module_event_handler(module_t mod, int what, void *arg) 1273{ 1274 1275 switch (what) { 1276 case MOD_LOAD: 1277 return (ctl_init()); 1278 case MOD_UNLOAD: 1279 return (EBUSY); 1280 default: 1281 return (EOPNOTSUPP); 1282 } 1283} 1284 1285/* 1286 * XXX KDM should we do some access checks here? Bump a reference count to 1287 * prevent a CTL module from being unloaded while someone has it open? 1288 */ 1289static int 1290ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1291{ 1292 return (0); 1293} 1294 1295static int 1296ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1297{ 1298 return (0); 1299} 1300 1301int 1302ctl_port_enable(ctl_port_type port_type) 1303{ 1304 struct ctl_softc *softc = control_softc; 1305 struct ctl_port *port; 1306 1307 if (softc->is_single == 0) { 1308 union ctl_ha_msg msg_info; 1309 int isc_retval; 1310 1311#if 0 1312 printf("%s: HA mode, synchronizing frontend enable\n", 1313 __func__); 1314#endif 1315 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1316 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1317 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1318 printf("Sync msg send error retval %d\n", isc_retval); 1319 } 1320 if (!rcv_sync_msg) { 1321 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1322 sizeof(msg_info), 1); 1323 } 1324#if 0 1325 printf("CTL:Frontend Enable\n"); 1326 } else { 1327 printf("%s: single mode, skipping frontend synchronization\n", 1328 __func__); 1329#endif 1330 } 1331 1332 STAILQ_FOREACH(port, &softc->port_list, links) { 1333 if (port_type & port->port_type) 1334 { 1335#if 0 1336 printf("port %d\n", port->targ_port); 1337#endif 1338 ctl_port_online(port); 1339 } 1340 } 1341 1342 return (0); 1343} 1344 1345int 1346ctl_port_disable(ctl_port_type port_type) 1347{ 1348 struct ctl_softc *softc; 1349 struct ctl_port *port; 1350 1351 softc = control_softc; 1352 1353 STAILQ_FOREACH(port, &softc->port_list, links) { 1354 if (port_type & port->port_type) 1355 ctl_port_offline(port); 1356 } 1357 1358 return (0); 1359} 1360 1361/* 1362 * Returns 0 for success, 1 for failure. 1363 * Currently the only failure mode is if there aren't enough entries 1364 * allocated. So, in case of a failure, look at num_entries_dropped, 1365 * reallocate and try again. 1366 */ 1367int 1368ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1369 int *num_entries_filled, int *num_entries_dropped, 1370 ctl_port_type port_type, int no_virtual) 1371{ 1372 struct ctl_softc *softc; 1373 struct ctl_port *port; 1374 int entries_dropped, entries_filled; 1375 int retval; 1376 int i; 1377 1378 softc = control_softc; 1379 1380 retval = 0; 1381 entries_filled = 0; 1382 entries_dropped = 0; 1383 1384 i = 0; 1385 mtx_lock(&softc->ctl_lock); 1386 STAILQ_FOREACH(port, &softc->port_list, links) { 1387 struct ctl_port_entry *entry; 1388 1389 if ((port->port_type & port_type) == 0) 1390 continue; 1391 1392 if ((no_virtual != 0) 1393 && (port->virtual_port != 0)) 1394 continue; 1395 1396 if (entries_filled >= num_entries_alloced) { 1397 entries_dropped++; 1398 continue; 1399 } 1400 entry = &entries[i]; 1401 1402 entry->port_type = port->port_type; 1403 strlcpy(entry->port_name, port->port_name, 1404 sizeof(entry->port_name)); 1405 entry->physical_port = port->physical_port; 1406 entry->virtual_port = port->virtual_port; 1407 entry->wwnn = port->wwnn; 1408 entry->wwpn = port->wwpn; 1409 1410 i++; 1411 entries_filled++; 1412 } 1413 1414 mtx_unlock(&softc->ctl_lock); 1415 1416 if (entries_dropped > 0) 1417 retval = 1; 1418 1419 *num_entries_dropped = entries_dropped; 1420 *num_entries_filled = entries_filled; 1421 1422 return (retval); 1423} 1424 1425static void 1426ctl_ioctl_online(void *arg) 1427{ 1428 struct ctl_ioctl_info *ioctl_info; 1429 1430 ioctl_info = (struct ctl_ioctl_info *)arg; 1431 1432 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1433} 1434 1435static void 1436ctl_ioctl_offline(void *arg) 1437{ 1438 struct ctl_ioctl_info *ioctl_info; 1439 1440 ioctl_info = (struct ctl_ioctl_info *)arg; 1441 1442 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1443} 1444 1445/* 1446 * Remove an initiator by port number and initiator ID. 1447 * Returns 0 for success, -1 for failure. 1448 */ 1449int 1450ctl_remove_initiator(struct ctl_port *port, int iid) 1451{ 1452 struct ctl_softc *softc = control_softc; 1453 1454 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1455 1456 if (iid > CTL_MAX_INIT_PER_PORT) { 1457 printf("%s: initiator ID %u > maximun %u!\n", 1458 __func__, iid, CTL_MAX_INIT_PER_PORT); 1459 return (-1); 1460 } 1461 1462 mtx_lock(&softc->ctl_lock); 1463 port->wwpn_iid[iid].in_use--; 1464 port->wwpn_iid[iid].last_use = time_uptime; 1465 mtx_unlock(&softc->ctl_lock); 1466 1467 return (0); 1468} 1469 1470/* 1471 * Add an initiator to the initiator map. 1472 * Returns iid for success, < 0 for failure. 1473 */ 1474int 1475ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1476{ 1477 struct ctl_softc *softc = control_softc; 1478 time_t best_time; 1479 int i, best; 1480 1481 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1482 1483 if (iid >= CTL_MAX_INIT_PER_PORT) { 1484 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1485 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1486 free(name, M_CTL); 1487 return (-1); 1488 } 1489 1490 mtx_lock(&softc->ctl_lock); 1491 1492 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1493 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1494 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1495 iid = i; 1496 break; 1497 } 1498 if (name != NULL && port->wwpn_iid[i].name != NULL && 1499 strcmp(name, port->wwpn_iid[i].name) == 0) { 1500 iid = i; 1501 break; 1502 } 1503 } 1504 } 1505 1506 if (iid < 0) { 1507 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1508 if (port->wwpn_iid[i].in_use == 0 && 1509 port->wwpn_iid[i].wwpn == 0 && 1510 port->wwpn_iid[i].name == NULL) { 1511 iid = i; 1512 break; 1513 } 1514 } 1515 } 1516 1517 if (iid < 0) { 1518 best = -1; 1519 best_time = INT32_MAX; 1520 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1521 if (port->wwpn_iid[i].in_use == 0) { 1522 if (port->wwpn_iid[i].last_use < best_time) { 1523 best = i; 1524 best_time = port->wwpn_iid[i].last_use; 1525 } 1526 } 1527 } 1528 iid = best; 1529 } 1530 1531 if (iid < 0) { 1532 mtx_unlock(&softc->ctl_lock); 1533 free(name, M_CTL); 1534 return (-2); 1535 } 1536 1537 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1538 /* 1539 * This is not an error yet. 1540 */ 1541 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1542#if 0 1543 printf("%s: port %d iid %u WWPN %#jx arrived" 1544 " again\n", __func__, port->targ_port, 1545 iid, (uintmax_t)wwpn); 1546#endif 1547 goto take; 1548 } 1549 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1550 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1551#if 0 1552 printf("%s: port %d iid %u name '%s' arrived" 1553 " again\n", __func__, port->targ_port, 1554 iid, name); 1555#endif 1556 goto take; 1557 } 1558 1559 /* 1560 * This is an error, but what do we do about it? The 1561 * driver is telling us we have a new WWPN for this 1562 * initiator ID, so we pretty much need to use it. 1563 */ 1564 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1565 " but WWPN %#jx '%s' is still at that address\n", 1566 __func__, port->targ_port, iid, wwpn, name, 1567 (uintmax_t)port->wwpn_iid[iid].wwpn, 1568 port->wwpn_iid[iid].name); 1569 1570 /* 1571 * XXX KDM clear have_ca and ua_pending on each LUN for 1572 * this initiator. 1573 */ 1574 } 1575take: 1576 free(port->wwpn_iid[iid].name, M_CTL); 1577 port->wwpn_iid[iid].name = name; 1578 port->wwpn_iid[iid].wwpn = wwpn; 1579 port->wwpn_iid[iid].in_use++; 1580 mtx_unlock(&softc->ctl_lock); 1581 1582 return (iid); 1583} 1584 1585static int 1586ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1587{ 1588 int len; 1589 1590 switch (port->port_type) { 1591 case CTL_PORT_FC: 1592 { 1593 struct scsi_transportid_fcp *id = 1594 (struct scsi_transportid_fcp *)buf; 1595 if (port->wwpn_iid[iid].wwpn == 0) 1596 return (0); 1597 memset(id, 0, sizeof(*id)); 1598 id->format_protocol = SCSI_PROTO_FC; 1599 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1600 return (sizeof(*id)); 1601 } 1602 case CTL_PORT_ISCSI: 1603 { 1604 struct scsi_transportid_iscsi_port *id = 1605 (struct scsi_transportid_iscsi_port *)buf; 1606 if (port->wwpn_iid[iid].name == NULL) 1607 return (0); 1608 memset(id, 0, 256); 1609 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1610 SCSI_PROTO_ISCSI; 1611 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1612 len = roundup2(min(len, 252), 4); 1613 scsi_ulto2b(len, id->additional_length); 1614 return (sizeof(*id) + len); 1615 } 1616 case CTL_PORT_SAS: 1617 { 1618 struct scsi_transportid_sas *id = 1619 (struct scsi_transportid_sas *)buf; 1620 if (port->wwpn_iid[iid].wwpn == 0) 1621 return (0); 1622 memset(id, 0, sizeof(*id)); 1623 id->format_protocol = SCSI_PROTO_SAS; 1624 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1625 return (sizeof(*id)); 1626 } 1627 default: 1628 { 1629 struct scsi_transportid_spi *id = 1630 (struct scsi_transportid_spi *)buf; 1631 memset(id, 0, sizeof(*id)); 1632 id->format_protocol = SCSI_PROTO_SPI; 1633 scsi_ulto2b(iid, id->scsi_addr); 1634 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1635 return (sizeof(*id)); 1636 } 1637 } 1638} 1639 1640static int 1641ctl_ioctl_lun_enable(void *arg, int lun_id) 1642{ 1643 return (0); 1644} 1645 1646static int 1647ctl_ioctl_lun_disable(void *arg, int lun_id) 1648{ 1649 return (0); 1650} 1651 1652/* 1653 * Data movement routine for the CTL ioctl frontend port. 1654 */ 1655static int 1656ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1657{ 1658 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1659 struct ctl_sg_entry ext_entry, kern_entry; 1660 int ext_sglen, ext_sg_entries, kern_sg_entries; 1661 int ext_sg_start, ext_offset; 1662 int len_to_copy, len_copied; 1663 int kern_watermark, ext_watermark; 1664 int ext_sglist_malloced; 1665 int i, j; 1666 1667 ext_sglist_malloced = 0; 1668 ext_sg_start = 0; 1669 ext_offset = 0; 1670 1671 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1672 1673 /* 1674 * If this flag is set, fake the data transfer. 1675 */ 1676 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1677 ctsio->ext_data_filled = ctsio->ext_data_len; 1678 goto bailout; 1679 } 1680 1681 /* 1682 * To simplify things here, if we have a single buffer, stick it in 1683 * a S/G entry and just make it a single entry S/G list. 1684 */ 1685 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1686 int len_seen; 1687 1688 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1689 1690 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1691 M_WAITOK); 1692 ext_sglist_malloced = 1; 1693 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1694 ext_sglen) != 0) { 1695 ctl_set_internal_failure(ctsio, 1696 /*sks_valid*/ 0, 1697 /*retry_count*/ 0); 1698 goto bailout; 1699 } 1700 ext_sg_entries = ctsio->ext_sg_entries; 1701 len_seen = 0; 1702 for (i = 0; i < ext_sg_entries; i++) { 1703 if ((len_seen + ext_sglist[i].len) >= 1704 ctsio->ext_data_filled) { 1705 ext_sg_start = i; 1706 ext_offset = ctsio->ext_data_filled - len_seen; 1707 break; 1708 } 1709 len_seen += ext_sglist[i].len; 1710 } 1711 } else { 1712 ext_sglist = &ext_entry; 1713 ext_sglist->addr = ctsio->ext_data_ptr; 1714 ext_sglist->len = ctsio->ext_data_len; 1715 ext_sg_entries = 1; 1716 ext_sg_start = 0; 1717 ext_offset = ctsio->ext_data_filled; 1718 } 1719 1720 if (ctsio->kern_sg_entries > 0) { 1721 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1722 kern_sg_entries = ctsio->kern_sg_entries; 1723 } else { 1724 kern_sglist = &kern_entry; 1725 kern_sglist->addr = ctsio->kern_data_ptr; 1726 kern_sglist->len = ctsio->kern_data_len; 1727 kern_sg_entries = 1; 1728 } 1729 1730 1731 kern_watermark = 0; 1732 ext_watermark = ext_offset; 1733 len_copied = 0; 1734 for (i = ext_sg_start, j = 0; 1735 i < ext_sg_entries && j < kern_sg_entries;) { 1736 uint8_t *ext_ptr, *kern_ptr; 1737 1738 len_to_copy = MIN(ext_sglist[i].len - ext_watermark, 1739 kern_sglist[j].len - kern_watermark); 1740 1741 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1742 ext_ptr = ext_ptr + ext_watermark; 1743 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1744 /* 1745 * XXX KDM fix this! 1746 */ 1747 panic("need to implement bus address support"); 1748#if 0 1749 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1750#endif 1751 } else 1752 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1753 kern_ptr = kern_ptr + kern_watermark; 1754 1755 kern_watermark += len_to_copy; 1756 ext_watermark += len_to_copy; 1757 1758 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1759 CTL_FLAG_DATA_IN) { 1760 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1761 "bytes to user\n", len_to_copy)); 1762 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1763 "to %p\n", kern_ptr, ext_ptr)); 1764 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1765 ctl_set_internal_failure(ctsio, 1766 /*sks_valid*/ 0, 1767 /*retry_count*/ 0); 1768 goto bailout; 1769 } 1770 } else { 1771 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1772 "bytes from user\n", len_to_copy)); 1773 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1774 "to %p\n", ext_ptr, kern_ptr)); 1775 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1776 ctl_set_internal_failure(ctsio, 1777 /*sks_valid*/ 0, 1778 /*retry_count*/0); 1779 goto bailout; 1780 } 1781 } 1782 1783 len_copied += len_to_copy; 1784 1785 if (ext_sglist[i].len == ext_watermark) { 1786 i++; 1787 ext_watermark = 0; 1788 } 1789 1790 if (kern_sglist[j].len == kern_watermark) { 1791 j++; 1792 kern_watermark = 0; 1793 } 1794 } 1795 1796 ctsio->ext_data_filled += len_copied; 1797 1798 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1799 "kern_sg_entries: %d\n", ext_sg_entries, 1800 kern_sg_entries)); 1801 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1802 "kern_data_len = %d\n", ctsio->ext_data_len, 1803 ctsio->kern_data_len)); 1804 1805 1806 /* XXX KDM set residual?? */ 1807bailout: 1808 1809 if (ext_sglist_malloced != 0) 1810 free(ext_sglist, M_CTL); 1811 1812 return (CTL_RETVAL_COMPLETE); 1813} 1814 1815/* 1816 * Serialize a command that went down the "wrong" side, and so was sent to 1817 * this controller for execution. The logic is a little different than the 1818 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1819 * sent back to the other side, but in the success case, we execute the 1820 * command on this side (XFER mode) or tell the other side to execute it 1821 * (SER_ONLY mode). 1822 */ 1823static int 1824ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1825{ 1826 struct ctl_softc *softc; 1827 union ctl_ha_msg msg_info; 1828 struct ctl_lun *lun; 1829 int retval = 0; 1830 uint32_t targ_lun; 1831 1832 softc = control_softc; 1833 1834 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1835 lun = softc->ctl_luns[targ_lun]; 1836 if (lun==NULL) 1837 { 1838 /* 1839 * Why isn't LUN defined? The other side wouldn't 1840 * send a cmd if the LUN is undefined. 1841 */ 1842 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1843 1844 /* "Logical unit not supported" */ 1845 ctl_set_sense_data(&msg_info.scsi.sense_data, 1846 lun, 1847 /*sense_format*/SSD_TYPE_NONE, 1848 /*current_error*/ 1, 1849 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1850 /*asc*/ 0x25, 1851 /*ascq*/ 0x00, 1852 SSD_ELEM_NONE); 1853 1854 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1855 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1856 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1857 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1858 msg_info.hdr.serializing_sc = NULL; 1859 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1860 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1861 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1862 } 1863 return(1); 1864 1865 } 1866 1867 mtx_lock(&lun->lun_lock); 1868 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1869 1870 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1871 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1872 ooa_links))) { 1873 case CTL_ACTION_BLOCK: 1874 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1875 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1876 blocked_links); 1877 break; 1878 case CTL_ACTION_PASS: 1879 case CTL_ACTION_SKIP: 1880 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1881 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1882 ctl_enqueue_rtr((union ctl_io *)ctsio); 1883 } else { 1884 1885 /* send msg back to other side */ 1886 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1887 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1888 msg_info.hdr.msg_type = CTL_MSG_R2R; 1889#if 0 1890 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1891#endif 1892 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1893 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1894 } 1895 } 1896 break; 1897 case CTL_ACTION_OVERLAP: 1898 /* OVERLAPPED COMMANDS ATTEMPTED */ 1899 ctl_set_sense_data(&msg_info.scsi.sense_data, 1900 lun, 1901 /*sense_format*/SSD_TYPE_NONE, 1902 /*current_error*/ 1, 1903 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1904 /*asc*/ 0x4E, 1905 /*ascq*/ 0x00, 1906 SSD_ELEM_NONE); 1907 1908 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1909 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1910 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1911 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1912 msg_info.hdr.serializing_sc = NULL; 1913 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1914#if 0 1915 printf("BAD JUJU:Major Bummer Overlap\n"); 1916#endif 1917 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1918 retval = 1; 1919 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1920 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1921 } 1922 break; 1923 case CTL_ACTION_OVERLAP_TAG: 1924 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1925 ctl_set_sense_data(&msg_info.scsi.sense_data, 1926 lun, 1927 /*sense_format*/SSD_TYPE_NONE, 1928 /*current_error*/ 1, 1929 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1930 /*asc*/ 0x4D, 1931 /*ascq*/ ctsio->tag_num & 0xff, 1932 SSD_ELEM_NONE); 1933 1934 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1935 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1936 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1937 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1938 msg_info.hdr.serializing_sc = NULL; 1939 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1940#if 0 1941 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1942#endif 1943 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1944 retval = 1; 1945 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1946 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1947 } 1948 break; 1949 case CTL_ACTION_ERROR: 1950 default: 1951 /* "Internal target failure" */ 1952 ctl_set_sense_data(&msg_info.scsi.sense_data, 1953 lun, 1954 /*sense_format*/SSD_TYPE_NONE, 1955 /*current_error*/ 1, 1956 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1957 /*asc*/ 0x44, 1958 /*ascq*/ 0x00, 1959 SSD_ELEM_NONE); 1960 1961 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1962 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1963 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1964 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1965 msg_info.hdr.serializing_sc = NULL; 1966 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1967#if 0 1968 printf("BAD JUJU:Major Bummer HW Error\n"); 1969#endif 1970 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1971 retval = 1; 1972 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1973 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1974 } 1975 break; 1976 } 1977 mtx_unlock(&lun->lun_lock); 1978 return (retval); 1979} 1980 1981static int 1982ctl_ioctl_submit_wait(union ctl_io *io) 1983{ 1984 struct ctl_fe_ioctl_params params; 1985 ctl_fe_ioctl_state last_state; 1986 int done, retval; 1987 1988 retval = 0; 1989 1990 bzero(¶ms, sizeof(params)); 1991 1992 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1993 cv_init(¶ms.sem, "ctlioccv"); 1994 params.state = CTL_IOCTL_INPROG; 1995 last_state = params.state; 1996 1997 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1998 1999 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 2000 2001 /* This shouldn't happen */ 2002 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 2003 return (retval); 2004 2005 done = 0; 2006 2007 do { 2008 mtx_lock(¶ms.ioctl_mtx); 2009 /* 2010 * Check the state here, and don't sleep if the state has 2011 * already changed (i.e. wakeup has already occured, but we 2012 * weren't waiting yet). 2013 */ 2014 if (params.state == last_state) { 2015 /* XXX KDM cv_wait_sig instead? */ 2016 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 2017 } 2018 last_state = params.state; 2019 2020 switch (params.state) { 2021 case CTL_IOCTL_INPROG: 2022 /* Why did we wake up? */ 2023 /* XXX KDM error here? */ 2024 mtx_unlock(¶ms.ioctl_mtx); 2025 break; 2026 case CTL_IOCTL_DATAMOVE: 2027 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 2028 2029 /* 2030 * change last_state back to INPROG to avoid 2031 * deadlock on subsequent data moves. 2032 */ 2033 params.state = last_state = CTL_IOCTL_INPROG; 2034 2035 mtx_unlock(¶ms.ioctl_mtx); 2036 ctl_ioctl_do_datamove(&io->scsiio); 2037 /* 2038 * Note that in some cases, most notably writes, 2039 * this will queue the I/O and call us back later. 2040 * In other cases, generally reads, this routine 2041 * will immediately call back and wake us up, 2042 * probably using our own context. 2043 */ 2044 io->scsiio.be_move_done(io); 2045 break; 2046 case CTL_IOCTL_DONE: 2047 mtx_unlock(¶ms.ioctl_mtx); 2048 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 2049 done = 1; 2050 break; 2051 default: 2052 mtx_unlock(¶ms.ioctl_mtx); 2053 /* XXX KDM error here? */ 2054 break; 2055 } 2056 } while (done == 0); 2057 2058 mtx_destroy(¶ms.ioctl_mtx); 2059 cv_destroy(¶ms.sem); 2060 2061 return (CTL_RETVAL_COMPLETE); 2062} 2063 2064static void 2065ctl_ioctl_datamove(union ctl_io *io) 2066{ 2067 struct ctl_fe_ioctl_params *params; 2068 2069 params = (struct ctl_fe_ioctl_params *) 2070 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2071 2072 mtx_lock(¶ms->ioctl_mtx); 2073 params->state = CTL_IOCTL_DATAMOVE; 2074 cv_broadcast(¶ms->sem); 2075 mtx_unlock(¶ms->ioctl_mtx); 2076} 2077 2078static void 2079ctl_ioctl_done(union ctl_io *io) 2080{ 2081 struct ctl_fe_ioctl_params *params; 2082 2083 params = (struct ctl_fe_ioctl_params *) 2084 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2085 2086 mtx_lock(¶ms->ioctl_mtx); 2087 params->state = CTL_IOCTL_DONE; 2088 cv_broadcast(¶ms->sem); 2089 mtx_unlock(¶ms->ioctl_mtx); 2090} 2091 2092/* 2093 * Returns 0 for success, errno for failure. 2094 */ 2095static int 2096ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2097 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2098{ 2099 union ctl_io *io; 2100 int retval; 2101 2102 retval = 0; 2103 2104 mtx_lock(&lun->lun_lock); 2105 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2106 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2107 ooa_links)) { 2108 struct ctl_ooa_entry *entry; 2109 2110 /* 2111 * If we've got more than we can fit, just count the 2112 * remaining entries. 2113 */ 2114 if (*cur_fill_num >= ooa_hdr->alloc_num) 2115 continue; 2116 2117 entry = &kern_entries[*cur_fill_num]; 2118 2119 entry->tag_num = io->scsiio.tag_num; 2120 entry->lun_num = lun->lun; 2121#ifdef CTL_TIME_IO 2122 entry->start_bt = io->io_hdr.start_bt; 2123#endif 2124 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2125 entry->cdb_len = io->scsiio.cdb_len; 2126 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2127 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2128 2129 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2130 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2131 2132 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2133 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2134 2135 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2136 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2137 2138 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2139 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2140 } 2141 mtx_unlock(&lun->lun_lock); 2142 2143 return (retval); 2144} 2145 2146static void * 2147ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2148 size_t error_str_len) 2149{ 2150 void *kptr; 2151 2152 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2153 2154 if (copyin(user_addr, kptr, len) != 0) { 2155 snprintf(error_str, error_str_len, "Error copying %d bytes " 2156 "from user address %p to kernel address %p", len, 2157 user_addr, kptr); 2158 free(kptr, M_CTL); 2159 return (NULL); 2160 } 2161 2162 return (kptr); 2163} 2164 2165static void 2166ctl_free_args(int num_args, struct ctl_be_arg *args) 2167{ 2168 int i; 2169 2170 if (args == NULL) 2171 return; 2172 2173 for (i = 0; i < num_args; i++) { 2174 free(args[i].kname, M_CTL); 2175 free(args[i].kvalue, M_CTL); 2176 } 2177 2178 free(args, M_CTL); 2179} 2180 2181static struct ctl_be_arg * 2182ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2183 char *error_str, size_t error_str_len) 2184{ 2185 struct ctl_be_arg *args; 2186 int i; 2187 2188 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2189 error_str, error_str_len); 2190 2191 if (args == NULL) 2192 goto bailout; 2193 2194 for (i = 0; i < num_args; i++) { 2195 args[i].kname = NULL; 2196 args[i].kvalue = NULL; 2197 } 2198 2199 for (i = 0; i < num_args; i++) { 2200 uint8_t *tmpptr; 2201 2202 args[i].kname = ctl_copyin_alloc(args[i].name, 2203 args[i].namelen, error_str, error_str_len); 2204 if (args[i].kname == NULL) 2205 goto bailout; 2206 2207 if (args[i].kname[args[i].namelen - 1] != '\0') { 2208 snprintf(error_str, error_str_len, "Argument %d " 2209 "name is not NUL-terminated", i); 2210 goto bailout; 2211 } 2212 2213 if (args[i].flags & CTL_BEARG_RD) { 2214 tmpptr = ctl_copyin_alloc(args[i].value, 2215 args[i].vallen, error_str, error_str_len); 2216 if (tmpptr == NULL) 2217 goto bailout; 2218 if ((args[i].flags & CTL_BEARG_ASCII) 2219 && (tmpptr[args[i].vallen - 1] != '\0')) { 2220 snprintf(error_str, error_str_len, "Argument " 2221 "%d value is not NUL-terminated", i); 2222 goto bailout; 2223 } 2224 args[i].kvalue = tmpptr; 2225 } else { 2226 args[i].kvalue = malloc(args[i].vallen, 2227 M_CTL, M_WAITOK | M_ZERO); 2228 } 2229 } 2230 2231 return (args); 2232bailout: 2233 2234 ctl_free_args(num_args, args); 2235 2236 return (NULL); 2237} 2238 2239static void 2240ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2241{ 2242 int i; 2243 2244 for (i = 0; i < num_args; i++) { 2245 if (args[i].flags & CTL_BEARG_WR) 2246 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2247 } 2248} 2249 2250/* 2251 * Escape characters that are illegal or not recommended in XML. 2252 */ 2253int 2254ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2255{ 2256 char *end = str + size; 2257 int retval; 2258 2259 retval = 0; 2260 2261 for (; *str && str < end; str++) { 2262 switch (*str) { 2263 case '&': 2264 retval = sbuf_printf(sb, "&"); 2265 break; 2266 case '>': 2267 retval = sbuf_printf(sb, ">"); 2268 break; 2269 case '<': 2270 retval = sbuf_printf(sb, "<"); 2271 break; 2272 default: 2273 retval = sbuf_putc(sb, *str); 2274 break; 2275 } 2276 2277 if (retval != 0) 2278 break; 2279 2280 } 2281 2282 return (retval); 2283} 2284 2285static void 2286ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2287{ 2288 struct scsi_vpd_id_descriptor *desc; 2289 int i; 2290 2291 if (id == NULL || id->len < 4) 2292 return; 2293 desc = (struct scsi_vpd_id_descriptor *)id->data; 2294 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2295 case SVPD_ID_TYPE_T10: 2296 sbuf_printf(sb, "t10."); 2297 break; 2298 case SVPD_ID_TYPE_EUI64: 2299 sbuf_printf(sb, "eui."); 2300 break; 2301 case SVPD_ID_TYPE_NAA: 2302 sbuf_printf(sb, "naa."); 2303 break; 2304 case SVPD_ID_TYPE_SCSI_NAME: 2305 break; 2306 } 2307 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2308 case SVPD_ID_CODESET_BINARY: 2309 for (i = 0; i < desc->length; i++) 2310 sbuf_printf(sb, "%02x", desc->identifier[i]); 2311 break; 2312 case SVPD_ID_CODESET_ASCII: 2313 sbuf_printf(sb, "%.*s", (int)desc->length, 2314 (char *)desc->identifier); 2315 break; 2316 case SVPD_ID_CODESET_UTF8: 2317 sbuf_printf(sb, "%s", (char *)desc->identifier); 2318 break; 2319 } 2320} 2321 2322static int 2323ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2324 struct thread *td) 2325{ 2326 struct ctl_softc *softc; 2327 int retval; 2328 2329 softc = control_softc; 2330 2331 retval = 0; 2332 2333 switch (cmd) { 2334 case CTL_IO: { 2335 union ctl_io *io; 2336 void *pool_tmp; 2337 2338 /* 2339 * If we haven't been "enabled", don't allow any SCSI I/O 2340 * to this FETD. 2341 */ 2342 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2343 retval = EPERM; 2344 break; 2345 } 2346 2347 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2348 2349 /* 2350 * Need to save the pool reference so it doesn't get 2351 * spammed by the user's ctl_io. 2352 */ 2353 pool_tmp = io->io_hdr.pool; 2354 memcpy(io, (void *)addr, sizeof(*io)); 2355 io->io_hdr.pool = pool_tmp; 2356 2357 /* 2358 * No status yet, so make sure the status is set properly. 2359 */ 2360 io->io_hdr.status = CTL_STATUS_NONE; 2361 2362 /* 2363 * The user sets the initiator ID, target and LUN IDs. 2364 */ 2365 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2366 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2367 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2368 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2369 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2370 2371 retval = ctl_ioctl_submit_wait(io); 2372 2373 if (retval != 0) { 2374 ctl_free_io(io); 2375 break; 2376 } 2377 2378 memcpy((void *)addr, io, sizeof(*io)); 2379 2380 /* return this to our pool */ 2381 ctl_free_io(io); 2382 2383 break; 2384 } 2385 case CTL_ENABLE_PORT: 2386 case CTL_DISABLE_PORT: 2387 case CTL_SET_PORT_WWNS: { 2388 struct ctl_port *port; 2389 struct ctl_port_entry *entry; 2390 2391 entry = (struct ctl_port_entry *)addr; 2392 2393 mtx_lock(&softc->ctl_lock); 2394 STAILQ_FOREACH(port, &softc->port_list, links) { 2395 int action, done; 2396 2397 action = 0; 2398 done = 0; 2399 2400 if ((entry->port_type == CTL_PORT_NONE) 2401 && (entry->targ_port == port->targ_port)) { 2402 /* 2403 * If the user only wants to enable or 2404 * disable or set WWNs on a specific port, 2405 * do the operation and we're done. 2406 */ 2407 action = 1; 2408 done = 1; 2409 } else if (entry->port_type & port->port_type) { 2410 /* 2411 * Compare the user's type mask with the 2412 * particular frontend type to see if we 2413 * have a match. 2414 */ 2415 action = 1; 2416 done = 0; 2417 2418 /* 2419 * Make sure the user isn't trying to set 2420 * WWNs on multiple ports at the same time. 2421 */ 2422 if (cmd == CTL_SET_PORT_WWNS) { 2423 printf("%s: Can't set WWNs on " 2424 "multiple ports\n", __func__); 2425 retval = EINVAL; 2426 break; 2427 } 2428 } 2429 if (action != 0) { 2430 /* 2431 * XXX KDM we have to drop the lock here, 2432 * because the online/offline operations 2433 * can potentially block. We need to 2434 * reference count the frontends so they 2435 * can't go away, 2436 */ 2437 mtx_unlock(&softc->ctl_lock); 2438 2439 if (cmd == CTL_ENABLE_PORT) { 2440 ctl_port_online(port); 2441 } else if (cmd == CTL_DISABLE_PORT) { 2442 ctl_port_offline(port); 2443 } 2444 2445 mtx_lock(&softc->ctl_lock); 2446 2447 if (cmd == CTL_SET_PORT_WWNS) 2448 ctl_port_set_wwns(port, 2449 (entry->flags & CTL_PORT_WWNN_VALID) ? 2450 1 : 0, entry->wwnn, 2451 (entry->flags & CTL_PORT_WWPN_VALID) ? 2452 1 : 0, entry->wwpn); 2453 } 2454 if (done != 0) 2455 break; 2456 } 2457 mtx_unlock(&softc->ctl_lock); 2458 break; 2459 } 2460 case CTL_GET_PORT_LIST: { 2461 struct ctl_port *port; 2462 struct ctl_port_list *list; 2463 int i; 2464 2465 list = (struct ctl_port_list *)addr; 2466 2467 if (list->alloc_len != (list->alloc_num * 2468 sizeof(struct ctl_port_entry))) { 2469 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2470 "alloc_num %u * sizeof(struct ctl_port_entry) " 2471 "%zu\n", __func__, list->alloc_len, 2472 list->alloc_num, sizeof(struct ctl_port_entry)); 2473 retval = EINVAL; 2474 break; 2475 } 2476 list->fill_len = 0; 2477 list->fill_num = 0; 2478 list->dropped_num = 0; 2479 i = 0; 2480 mtx_lock(&softc->ctl_lock); 2481 STAILQ_FOREACH(port, &softc->port_list, links) { 2482 struct ctl_port_entry entry, *list_entry; 2483 2484 if (list->fill_num >= list->alloc_num) { 2485 list->dropped_num++; 2486 continue; 2487 } 2488 2489 entry.port_type = port->port_type; 2490 strlcpy(entry.port_name, port->port_name, 2491 sizeof(entry.port_name)); 2492 entry.targ_port = port->targ_port; 2493 entry.physical_port = port->physical_port; 2494 entry.virtual_port = port->virtual_port; 2495 entry.wwnn = port->wwnn; 2496 entry.wwpn = port->wwpn; 2497 if (port->status & CTL_PORT_STATUS_ONLINE) 2498 entry.online = 1; 2499 else 2500 entry.online = 0; 2501 2502 list_entry = &list->entries[i]; 2503 2504 retval = copyout(&entry, list_entry, sizeof(entry)); 2505 if (retval != 0) { 2506 printf("%s: CTL_GET_PORT_LIST: copyout " 2507 "returned %d\n", __func__, retval); 2508 break; 2509 } 2510 i++; 2511 list->fill_num++; 2512 list->fill_len += sizeof(entry); 2513 } 2514 mtx_unlock(&softc->ctl_lock); 2515 2516 /* 2517 * If this is non-zero, we had a copyout fault, so there's 2518 * probably no point in attempting to set the status inside 2519 * the structure. 2520 */ 2521 if (retval != 0) 2522 break; 2523 2524 if (list->dropped_num > 0) 2525 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2526 else 2527 list->status = CTL_PORT_LIST_OK; 2528 break; 2529 } 2530 case CTL_DUMP_OOA: { 2531 struct ctl_lun *lun; 2532 union ctl_io *io; 2533 char printbuf[128]; 2534 struct sbuf sb; 2535 2536 mtx_lock(&softc->ctl_lock); 2537 printf("Dumping OOA queues:\n"); 2538 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2539 mtx_lock(&lun->lun_lock); 2540 for (io = (union ctl_io *)TAILQ_FIRST( 2541 &lun->ooa_queue); io != NULL; 2542 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2543 ooa_links)) { 2544 sbuf_new(&sb, printbuf, sizeof(printbuf), 2545 SBUF_FIXEDLEN); 2546 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2547 (intmax_t)lun->lun, 2548 io->scsiio.tag_num, 2549 (io->io_hdr.flags & 2550 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2551 (io->io_hdr.flags & 2552 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2553 (io->io_hdr.flags & 2554 CTL_FLAG_ABORT) ? " ABORT" : "", 2555 (io->io_hdr.flags & 2556 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2557 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2558 sbuf_finish(&sb); 2559 printf("%s\n", sbuf_data(&sb)); 2560 } 2561 mtx_unlock(&lun->lun_lock); 2562 } 2563 printf("OOA queues dump done\n"); 2564 mtx_unlock(&softc->ctl_lock); 2565 break; 2566 } 2567 case CTL_GET_OOA: { 2568 struct ctl_lun *lun; 2569 struct ctl_ooa *ooa_hdr; 2570 struct ctl_ooa_entry *entries; 2571 uint32_t cur_fill_num; 2572 2573 ooa_hdr = (struct ctl_ooa *)addr; 2574 2575 if ((ooa_hdr->alloc_len == 0) 2576 || (ooa_hdr->alloc_num == 0)) { 2577 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2578 "must be non-zero\n", __func__, 2579 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2580 retval = EINVAL; 2581 break; 2582 } 2583 2584 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2585 sizeof(struct ctl_ooa_entry))) { 2586 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2587 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2588 __func__, ooa_hdr->alloc_len, 2589 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2590 retval = EINVAL; 2591 break; 2592 } 2593 2594 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2595 if (entries == NULL) { 2596 printf("%s: could not allocate %d bytes for OOA " 2597 "dump\n", __func__, ooa_hdr->alloc_len); 2598 retval = ENOMEM; 2599 break; 2600 } 2601 2602 mtx_lock(&softc->ctl_lock); 2603 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2604 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2605 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2606 mtx_unlock(&softc->ctl_lock); 2607 free(entries, M_CTL); 2608 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2609 __func__, (uintmax_t)ooa_hdr->lun_num); 2610 retval = EINVAL; 2611 break; 2612 } 2613 2614 cur_fill_num = 0; 2615 2616 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2617 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2618 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2619 ooa_hdr, entries); 2620 if (retval != 0) 2621 break; 2622 } 2623 if (retval != 0) { 2624 mtx_unlock(&softc->ctl_lock); 2625 free(entries, M_CTL); 2626 break; 2627 } 2628 } else { 2629 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2630 2631 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2632 entries); 2633 } 2634 mtx_unlock(&softc->ctl_lock); 2635 2636 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2637 ooa_hdr->fill_len = ooa_hdr->fill_num * 2638 sizeof(struct ctl_ooa_entry); 2639 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2640 if (retval != 0) { 2641 printf("%s: error copying out %d bytes for OOA dump\n", 2642 __func__, ooa_hdr->fill_len); 2643 } 2644 2645 getbintime(&ooa_hdr->cur_bt); 2646 2647 if (cur_fill_num > ooa_hdr->alloc_num) { 2648 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2649 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2650 } else { 2651 ooa_hdr->dropped_num = 0; 2652 ooa_hdr->status = CTL_OOA_OK; 2653 } 2654 2655 free(entries, M_CTL); 2656 break; 2657 } 2658 case CTL_CHECK_OOA: { 2659 union ctl_io *io; 2660 struct ctl_lun *lun; 2661 struct ctl_ooa_info *ooa_info; 2662 2663 2664 ooa_info = (struct ctl_ooa_info *)addr; 2665 2666 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2667 ooa_info->status = CTL_OOA_INVALID_LUN; 2668 break; 2669 } 2670 mtx_lock(&softc->ctl_lock); 2671 lun = softc->ctl_luns[ooa_info->lun_id]; 2672 if (lun == NULL) { 2673 mtx_unlock(&softc->ctl_lock); 2674 ooa_info->status = CTL_OOA_INVALID_LUN; 2675 break; 2676 } 2677 mtx_lock(&lun->lun_lock); 2678 mtx_unlock(&softc->ctl_lock); 2679 ooa_info->num_entries = 0; 2680 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2681 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2682 &io->io_hdr, ooa_links)) { 2683 ooa_info->num_entries++; 2684 } 2685 mtx_unlock(&lun->lun_lock); 2686 2687 ooa_info->status = CTL_OOA_SUCCESS; 2688 2689 break; 2690 } 2691 case CTL_DELAY_IO: { 2692 struct ctl_io_delay_info *delay_info; 2693#ifdef CTL_IO_DELAY 2694 struct ctl_lun *lun; 2695#endif /* CTL_IO_DELAY */ 2696 2697 delay_info = (struct ctl_io_delay_info *)addr; 2698 2699#ifdef CTL_IO_DELAY 2700 mtx_lock(&softc->ctl_lock); 2701 2702 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2703 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2704 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2705 } else { 2706 lun = softc->ctl_luns[delay_info->lun_id]; 2707 mtx_lock(&lun->lun_lock); 2708 2709 delay_info->status = CTL_DELAY_STATUS_OK; 2710 2711 switch (delay_info->delay_type) { 2712 case CTL_DELAY_TYPE_CONT: 2713 break; 2714 case CTL_DELAY_TYPE_ONESHOT: 2715 break; 2716 default: 2717 delay_info->status = 2718 CTL_DELAY_STATUS_INVALID_TYPE; 2719 break; 2720 } 2721 2722 switch (delay_info->delay_loc) { 2723 case CTL_DELAY_LOC_DATAMOVE: 2724 lun->delay_info.datamove_type = 2725 delay_info->delay_type; 2726 lun->delay_info.datamove_delay = 2727 delay_info->delay_secs; 2728 break; 2729 case CTL_DELAY_LOC_DONE: 2730 lun->delay_info.done_type = 2731 delay_info->delay_type; 2732 lun->delay_info.done_delay = 2733 delay_info->delay_secs; 2734 break; 2735 default: 2736 delay_info->status = 2737 CTL_DELAY_STATUS_INVALID_LOC; 2738 break; 2739 } 2740 mtx_unlock(&lun->lun_lock); 2741 } 2742 2743 mtx_unlock(&softc->ctl_lock); 2744#else 2745 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2746#endif /* CTL_IO_DELAY */ 2747 break; 2748 } 2749 case CTL_REALSYNC_SET: { 2750 int *syncstate; 2751 2752 syncstate = (int *)addr; 2753 2754 mtx_lock(&softc->ctl_lock); 2755 switch (*syncstate) { 2756 case 0: 2757 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2758 break; 2759 case 1: 2760 softc->flags |= CTL_FLAG_REAL_SYNC; 2761 break; 2762 default: 2763 retval = EINVAL; 2764 break; 2765 } 2766 mtx_unlock(&softc->ctl_lock); 2767 break; 2768 } 2769 case CTL_REALSYNC_GET: { 2770 int *syncstate; 2771 2772 syncstate = (int*)addr; 2773 2774 mtx_lock(&softc->ctl_lock); 2775 if (softc->flags & CTL_FLAG_REAL_SYNC) 2776 *syncstate = 1; 2777 else 2778 *syncstate = 0; 2779 mtx_unlock(&softc->ctl_lock); 2780 2781 break; 2782 } 2783 case CTL_SETSYNC: 2784 case CTL_GETSYNC: { 2785 struct ctl_sync_info *sync_info; 2786 struct ctl_lun *lun; 2787 2788 sync_info = (struct ctl_sync_info *)addr; 2789 2790 mtx_lock(&softc->ctl_lock); 2791 lun = softc->ctl_luns[sync_info->lun_id]; 2792 if (lun == NULL) { 2793 mtx_unlock(&softc->ctl_lock); 2794 sync_info->status = CTL_GS_SYNC_NO_LUN; 2795 } 2796 /* 2797 * Get or set the sync interval. We're not bounds checking 2798 * in the set case, hopefully the user won't do something 2799 * silly. 2800 */ 2801 mtx_lock(&lun->lun_lock); 2802 mtx_unlock(&softc->ctl_lock); 2803 if (cmd == CTL_GETSYNC) 2804 sync_info->sync_interval = lun->sync_interval; 2805 else 2806 lun->sync_interval = sync_info->sync_interval; 2807 mtx_unlock(&lun->lun_lock); 2808 2809 sync_info->status = CTL_GS_SYNC_OK; 2810 2811 break; 2812 } 2813 case CTL_GETSTATS: { 2814 struct ctl_stats *stats; 2815 struct ctl_lun *lun; 2816 int i; 2817 2818 stats = (struct ctl_stats *)addr; 2819 2820 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2821 stats->alloc_len) { 2822 stats->status = CTL_SS_NEED_MORE_SPACE; 2823 stats->num_luns = softc->num_luns; 2824 break; 2825 } 2826 /* 2827 * XXX KDM no locking here. If the LUN list changes, 2828 * things can blow up. 2829 */ 2830 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2831 i++, lun = STAILQ_NEXT(lun, links)) { 2832 retval = copyout(&lun->stats, &stats->lun_stats[i], 2833 sizeof(lun->stats)); 2834 if (retval != 0) 2835 break; 2836 } 2837 stats->num_luns = softc->num_luns; 2838 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2839 softc->num_luns; 2840 stats->status = CTL_SS_OK; 2841#ifdef CTL_TIME_IO 2842 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2843#else 2844 stats->flags = CTL_STATS_FLAG_NONE; 2845#endif 2846 getnanouptime(&stats->timestamp); 2847 break; 2848 } 2849 case CTL_ERROR_INJECT: { 2850 struct ctl_error_desc *err_desc, *new_err_desc; 2851 struct ctl_lun *lun; 2852 2853 err_desc = (struct ctl_error_desc *)addr; 2854 2855 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2856 M_WAITOK | M_ZERO); 2857 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2858 2859 mtx_lock(&softc->ctl_lock); 2860 lun = softc->ctl_luns[err_desc->lun_id]; 2861 if (lun == NULL) { 2862 mtx_unlock(&softc->ctl_lock); 2863 free(new_err_desc, M_CTL); 2864 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2865 __func__, (uintmax_t)err_desc->lun_id); 2866 retval = EINVAL; 2867 break; 2868 } 2869 mtx_lock(&lun->lun_lock); 2870 mtx_unlock(&softc->ctl_lock); 2871 2872 /* 2873 * We could do some checking here to verify the validity 2874 * of the request, but given the complexity of error 2875 * injection requests, the checking logic would be fairly 2876 * complex. 2877 * 2878 * For now, if the request is invalid, it just won't get 2879 * executed and might get deleted. 2880 */ 2881 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2882 2883 /* 2884 * XXX KDM check to make sure the serial number is unique, 2885 * in case we somehow manage to wrap. That shouldn't 2886 * happen for a very long time, but it's the right thing to 2887 * do. 2888 */ 2889 new_err_desc->serial = lun->error_serial; 2890 err_desc->serial = lun->error_serial; 2891 lun->error_serial++; 2892 2893 mtx_unlock(&lun->lun_lock); 2894 break; 2895 } 2896 case CTL_ERROR_INJECT_DELETE: { 2897 struct ctl_error_desc *delete_desc, *desc, *desc2; 2898 struct ctl_lun *lun; 2899 int delete_done; 2900 2901 delete_desc = (struct ctl_error_desc *)addr; 2902 delete_done = 0; 2903 2904 mtx_lock(&softc->ctl_lock); 2905 lun = softc->ctl_luns[delete_desc->lun_id]; 2906 if (lun == NULL) { 2907 mtx_unlock(&softc->ctl_lock); 2908 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2909 __func__, (uintmax_t)delete_desc->lun_id); 2910 retval = EINVAL; 2911 break; 2912 } 2913 mtx_lock(&lun->lun_lock); 2914 mtx_unlock(&softc->ctl_lock); 2915 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2916 if (desc->serial != delete_desc->serial) 2917 continue; 2918 2919 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2920 links); 2921 free(desc, M_CTL); 2922 delete_done = 1; 2923 } 2924 mtx_unlock(&lun->lun_lock); 2925 if (delete_done == 0) { 2926 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2927 "error serial %ju on LUN %u\n", __func__, 2928 delete_desc->serial, delete_desc->lun_id); 2929 retval = EINVAL; 2930 break; 2931 } 2932 break; 2933 } 2934 case CTL_DUMP_STRUCTS: { 2935 int i, j, k; 2936 struct ctl_port *port; 2937 struct ctl_frontend *fe; 2938 2939 mtx_lock(&softc->ctl_lock); 2940 printf("CTL Persistent Reservation information start:\n"); 2941 for (i = 0; i < CTL_MAX_LUNS; i++) { 2942 struct ctl_lun *lun; 2943 2944 lun = softc->ctl_luns[i]; 2945 2946 if ((lun == NULL) 2947 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2948 continue; 2949 2950 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2951 if (lun->pr_keys[j] == NULL) 2952 continue; 2953 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2954 if (lun->pr_keys[j][k] == 0) 2955 continue; 2956 printf(" LUN %d port %d iid %d key " 2957 "%#jx\n", i, j, k, 2958 (uintmax_t)lun->pr_keys[j][k]); 2959 } 2960 } 2961 } 2962 printf("CTL Persistent Reservation information end\n"); 2963 printf("CTL Ports:\n"); 2964 STAILQ_FOREACH(port, &softc->port_list, links) { 2965 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2966 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2967 port->frontend->name, port->port_type, 2968 port->physical_port, port->virtual_port, 2969 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2970 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2971 if (port->wwpn_iid[j].in_use == 0 && 2972 port->wwpn_iid[j].wwpn == 0 && 2973 port->wwpn_iid[j].name == NULL) 2974 continue; 2975 2976 printf(" iid %u use %d WWPN %#jx '%s'\n", 2977 j, port->wwpn_iid[j].in_use, 2978 (uintmax_t)port->wwpn_iid[j].wwpn, 2979 port->wwpn_iid[j].name); 2980 } 2981 } 2982 printf("CTL Port information end\n"); 2983 mtx_unlock(&softc->ctl_lock); 2984 /* 2985 * XXX KDM calling this without a lock. We'd likely want 2986 * to drop the lock before calling the frontend's dump 2987 * routine anyway. 2988 */ 2989 printf("CTL Frontends:\n"); 2990 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2991 printf(" Frontend '%s'\n", fe->name); 2992 if (fe->fe_dump != NULL) 2993 fe->fe_dump(); 2994 } 2995 printf("CTL Frontend information end\n"); 2996 break; 2997 } 2998 case CTL_LUN_REQ: { 2999 struct ctl_lun_req *lun_req; 3000 struct ctl_backend_driver *backend; 3001 3002 lun_req = (struct ctl_lun_req *)addr; 3003 3004 backend = ctl_backend_find(lun_req->backend); 3005 if (backend == NULL) { 3006 lun_req->status = CTL_LUN_ERROR; 3007 snprintf(lun_req->error_str, 3008 sizeof(lun_req->error_str), 3009 "Backend \"%s\" not found.", 3010 lun_req->backend); 3011 break; 3012 } 3013 if (lun_req->num_be_args > 0) { 3014 lun_req->kern_be_args = ctl_copyin_args( 3015 lun_req->num_be_args, 3016 lun_req->be_args, 3017 lun_req->error_str, 3018 sizeof(lun_req->error_str)); 3019 if (lun_req->kern_be_args == NULL) { 3020 lun_req->status = CTL_LUN_ERROR; 3021 break; 3022 } 3023 } 3024 3025 retval = backend->ioctl(dev, cmd, addr, flag, td); 3026 3027 if (lun_req->num_be_args > 0) { 3028 ctl_copyout_args(lun_req->num_be_args, 3029 lun_req->kern_be_args); 3030 ctl_free_args(lun_req->num_be_args, 3031 lun_req->kern_be_args); 3032 } 3033 break; 3034 } 3035 case CTL_LUN_LIST: { 3036 struct sbuf *sb; 3037 struct ctl_lun *lun; 3038 struct ctl_lun_list *list; 3039 struct ctl_option *opt; 3040 3041 list = (struct ctl_lun_list *)addr; 3042 3043 /* 3044 * Allocate a fixed length sbuf here, based on the length 3045 * of the user's buffer. We could allocate an auto-extending 3046 * buffer, and then tell the user how much larger our 3047 * amount of data is than his buffer, but that presents 3048 * some problems: 3049 * 3050 * 1. The sbuf(9) routines use a blocking malloc, and so 3051 * we can't hold a lock while calling them with an 3052 * auto-extending buffer. 3053 * 3054 * 2. There is not currently a LUN reference counting 3055 * mechanism, outside of outstanding transactions on 3056 * the LUN's OOA queue. So a LUN could go away on us 3057 * while we're getting the LUN number, backend-specific 3058 * information, etc. Thus, given the way things 3059 * currently work, we need to hold the CTL lock while 3060 * grabbing LUN information. 3061 * 3062 * So, from the user's standpoint, the best thing to do is 3063 * allocate what he thinks is a reasonable buffer length, 3064 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3065 * double the buffer length and try again. (And repeat 3066 * that until he succeeds.) 3067 */ 3068 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3069 if (sb == NULL) { 3070 list->status = CTL_LUN_LIST_ERROR; 3071 snprintf(list->error_str, sizeof(list->error_str), 3072 "Unable to allocate %d bytes for LUN list", 3073 list->alloc_len); 3074 break; 3075 } 3076 3077 sbuf_printf(sb, "<ctllunlist>\n"); 3078 3079 mtx_lock(&softc->ctl_lock); 3080 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3081 mtx_lock(&lun->lun_lock); 3082 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3083 (uintmax_t)lun->lun); 3084 3085 /* 3086 * Bail out as soon as we see that we've overfilled 3087 * the buffer. 3088 */ 3089 if (retval != 0) 3090 break; 3091 3092 retval = sbuf_printf(sb, "\t<backend_type>%s" 3093 "</backend_type>\n", 3094 (lun->backend == NULL) ? "none" : 3095 lun->backend->name); 3096 3097 if (retval != 0) 3098 break; 3099 3100 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3101 lun->be_lun->lun_type); 3102 3103 if (retval != 0) 3104 break; 3105 3106 if (lun->backend == NULL) { 3107 retval = sbuf_printf(sb, "</lun>\n"); 3108 if (retval != 0) 3109 break; 3110 continue; 3111 } 3112 3113 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3114 (lun->be_lun->maxlba > 0) ? 3115 lun->be_lun->maxlba + 1 : 0); 3116 3117 if (retval != 0) 3118 break; 3119 3120 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3121 lun->be_lun->blocksize); 3122 3123 if (retval != 0) 3124 break; 3125 3126 retval = sbuf_printf(sb, "\t<serial_number>"); 3127 3128 if (retval != 0) 3129 break; 3130 3131 retval = ctl_sbuf_printf_esc(sb, 3132 lun->be_lun->serial_num, 3133 sizeof(lun->be_lun->serial_num)); 3134 3135 if (retval != 0) 3136 break; 3137 3138 retval = sbuf_printf(sb, "</serial_number>\n"); 3139 3140 if (retval != 0) 3141 break; 3142 3143 retval = sbuf_printf(sb, "\t<device_id>"); 3144 3145 if (retval != 0) 3146 break; 3147 3148 retval = ctl_sbuf_printf_esc(sb, 3149 lun->be_lun->device_id, 3150 sizeof(lun->be_lun->device_id)); 3151 3152 if (retval != 0) 3153 break; 3154 3155 retval = sbuf_printf(sb, "</device_id>\n"); 3156 3157 if (retval != 0) 3158 break; 3159 3160 if (lun->backend->lun_info != NULL) { 3161 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3162 if (retval != 0) 3163 break; 3164 } 3165 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3166 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3167 opt->name, opt->value, opt->name); 3168 if (retval != 0) 3169 break; 3170 } 3171 3172 retval = sbuf_printf(sb, "</lun>\n"); 3173 3174 if (retval != 0) 3175 break; 3176 mtx_unlock(&lun->lun_lock); 3177 } 3178 if (lun != NULL) 3179 mtx_unlock(&lun->lun_lock); 3180 mtx_unlock(&softc->ctl_lock); 3181 3182 if ((retval != 0) 3183 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3184 retval = 0; 3185 sbuf_delete(sb); 3186 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3187 snprintf(list->error_str, sizeof(list->error_str), 3188 "Out of space, %d bytes is too small", 3189 list->alloc_len); 3190 break; 3191 } 3192 3193 sbuf_finish(sb); 3194 3195 retval = copyout(sbuf_data(sb), list->lun_xml, 3196 sbuf_len(sb) + 1); 3197 3198 list->fill_len = sbuf_len(sb) + 1; 3199 list->status = CTL_LUN_LIST_OK; 3200 sbuf_delete(sb); 3201 break; 3202 } 3203 case CTL_ISCSI: { 3204 struct ctl_iscsi *ci; 3205 struct ctl_frontend *fe; 3206 3207 ci = (struct ctl_iscsi *)addr; 3208 3209 fe = ctl_frontend_find("iscsi"); 3210 if (fe == NULL) { 3211 ci->status = CTL_ISCSI_ERROR; 3212 snprintf(ci->error_str, sizeof(ci->error_str), 3213 "Frontend \"iscsi\" not found."); 3214 break; 3215 } 3216 3217 retval = fe->ioctl(dev, cmd, addr, flag, td); 3218 break; 3219 } 3220 case CTL_PORT_REQ: { 3221 struct ctl_req *req; 3222 struct ctl_frontend *fe; 3223 3224 req = (struct ctl_req *)addr; 3225 3226 fe = ctl_frontend_find(req->driver); 3227 if (fe == NULL) { 3228 req->status = CTL_LUN_ERROR; 3229 snprintf(req->error_str, sizeof(req->error_str), 3230 "Frontend \"%s\" not found.", req->driver); 3231 break; 3232 } 3233 if (req->num_args > 0) { 3234 req->kern_args = ctl_copyin_args(req->num_args, 3235 req->args, req->error_str, sizeof(req->error_str)); 3236 if (req->kern_args == NULL) { 3237 req->status = CTL_LUN_ERROR; 3238 break; 3239 } 3240 } 3241 3242 retval = fe->ioctl(dev, cmd, addr, flag, td); 3243 3244 if (req->num_args > 0) { 3245 ctl_copyout_args(req->num_args, req->kern_args); 3246 ctl_free_args(req->num_args, req->kern_args); 3247 } 3248 break; 3249 } 3250 case CTL_PORT_LIST: { 3251 struct sbuf *sb; 3252 struct ctl_port *port; 3253 struct ctl_lun_list *list; 3254 struct ctl_option *opt; 3255 int j; 3256 uint32_t plun; 3257 3258 list = (struct ctl_lun_list *)addr; 3259 3260 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3261 if (sb == NULL) { 3262 list->status = CTL_LUN_LIST_ERROR; 3263 snprintf(list->error_str, sizeof(list->error_str), 3264 "Unable to allocate %d bytes for LUN list", 3265 list->alloc_len); 3266 break; 3267 } 3268 3269 sbuf_printf(sb, "<ctlportlist>\n"); 3270 3271 mtx_lock(&softc->ctl_lock); 3272 STAILQ_FOREACH(port, &softc->port_list, links) { 3273 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3274 (uintmax_t)port->targ_port); 3275 3276 /* 3277 * Bail out as soon as we see that we've overfilled 3278 * the buffer. 3279 */ 3280 if (retval != 0) 3281 break; 3282 3283 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3284 "</frontend_type>\n", port->frontend->name); 3285 if (retval != 0) 3286 break; 3287 3288 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3289 port->port_type); 3290 if (retval != 0) 3291 break; 3292 3293 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3294 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3295 if (retval != 0) 3296 break; 3297 3298 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3299 port->port_name); 3300 if (retval != 0) 3301 break; 3302 3303 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3304 port->physical_port); 3305 if (retval != 0) 3306 break; 3307 3308 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3309 port->virtual_port); 3310 if (retval != 0) 3311 break; 3312 3313 if (port->target_devid != NULL) { 3314 sbuf_printf(sb, "\t<target>"); 3315 ctl_id_sbuf(port->target_devid, sb); 3316 sbuf_printf(sb, "</target>\n"); 3317 } 3318 3319 if (port->port_devid != NULL) { 3320 sbuf_printf(sb, "\t<port>"); 3321 ctl_id_sbuf(port->port_devid, sb); 3322 sbuf_printf(sb, "</port>\n"); 3323 } 3324 3325 if (port->port_info != NULL) { 3326 retval = port->port_info(port->onoff_arg, sb); 3327 if (retval != 0) 3328 break; 3329 } 3330 STAILQ_FOREACH(opt, &port->options, links) { 3331 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3332 opt->name, opt->value, opt->name); 3333 if (retval != 0) 3334 break; 3335 } 3336 3337 if (port->lun_map != NULL) { 3338 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3339 for (j = 0; j < CTL_MAX_LUNS; j++) { 3340 plun = ctl_lun_map_from_port(port, j); 3341 if (plun >= CTL_MAX_LUNS) 3342 continue; 3343 sbuf_printf(sb, 3344 "\t<lun id=\"%u\">%u</lun>\n", 3345 j, plun); 3346 } 3347 } 3348 3349 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3350 if (port->wwpn_iid[j].in_use == 0 || 3351 (port->wwpn_iid[j].wwpn == 0 && 3352 port->wwpn_iid[j].name == NULL)) 3353 continue; 3354 3355 if (port->wwpn_iid[j].name != NULL) 3356 retval = sbuf_printf(sb, 3357 "\t<initiator id=\"%u\">%s</initiator>\n", 3358 j, port->wwpn_iid[j].name); 3359 else 3360 retval = sbuf_printf(sb, 3361 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3362 j, port->wwpn_iid[j].wwpn); 3363 if (retval != 0) 3364 break; 3365 } 3366 if (retval != 0) 3367 break; 3368 3369 retval = sbuf_printf(sb, "</targ_port>\n"); 3370 if (retval != 0) 3371 break; 3372 } 3373 mtx_unlock(&softc->ctl_lock); 3374 3375 if ((retval != 0) 3376 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3377 retval = 0; 3378 sbuf_delete(sb); 3379 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3380 snprintf(list->error_str, sizeof(list->error_str), 3381 "Out of space, %d bytes is too small", 3382 list->alloc_len); 3383 break; 3384 } 3385 3386 sbuf_finish(sb); 3387 3388 retval = copyout(sbuf_data(sb), list->lun_xml, 3389 sbuf_len(sb) + 1); 3390 3391 list->fill_len = sbuf_len(sb) + 1; 3392 list->status = CTL_LUN_LIST_OK; 3393 sbuf_delete(sb); 3394 break; 3395 } 3396 case CTL_LUN_MAP: { 3397 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3398 struct ctl_port *port; 3399 3400 mtx_lock(&softc->ctl_lock); 3401 if (lm->port >= CTL_MAX_PORTS || 3402 (port = softc->ctl_ports[lm->port]) == NULL) { 3403 mtx_unlock(&softc->ctl_lock); 3404 return (ENXIO); 3405 } 3406 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3407 if (lm->plun < CTL_MAX_LUNS) { 3408 if (lm->lun == UINT32_MAX) 3409 retval = ctl_lun_map_unset(port, lm->plun); 3410 else if (lm->lun < CTL_MAX_LUNS && 3411 softc->ctl_luns[lm->lun] != NULL) 3412 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3413 else 3414 return (ENXIO); 3415 } else if (lm->plun == UINT32_MAX) { 3416 if (lm->lun == UINT32_MAX) 3417 retval = ctl_lun_map_deinit(port); 3418 else 3419 retval = ctl_lun_map_init(port); 3420 } else 3421 return (ENXIO); 3422 break; 3423 } 3424 default: { 3425 /* XXX KDM should we fix this? */ 3426#if 0 3427 struct ctl_backend_driver *backend; 3428 unsigned int type; 3429 int found; 3430 3431 found = 0; 3432 3433 /* 3434 * We encode the backend type as the ioctl type for backend 3435 * ioctls. So parse it out here, and then search for a 3436 * backend of this type. 3437 */ 3438 type = _IOC_TYPE(cmd); 3439 3440 STAILQ_FOREACH(backend, &softc->be_list, links) { 3441 if (backend->type == type) { 3442 found = 1; 3443 break; 3444 } 3445 } 3446 if (found == 0) { 3447 printf("ctl: unknown ioctl command %#lx or backend " 3448 "%d\n", cmd, type); 3449 retval = EINVAL; 3450 break; 3451 } 3452 retval = backend->ioctl(dev, cmd, addr, flag, td); 3453#endif 3454 retval = ENOTTY; 3455 break; 3456 } 3457 } 3458 return (retval); 3459} 3460 3461uint32_t 3462ctl_get_initindex(struct ctl_nexus *nexus) 3463{ 3464 if (nexus->targ_port < CTL_MAX_PORTS) 3465 return (nexus->initid.id + 3466 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3467 else 3468 return (nexus->initid.id + 3469 ((nexus->targ_port - CTL_MAX_PORTS) * 3470 CTL_MAX_INIT_PER_PORT)); 3471} 3472 3473uint32_t 3474ctl_get_resindex(struct ctl_nexus *nexus) 3475{ 3476 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3477} 3478 3479uint32_t 3480ctl_port_idx(int port_num) 3481{ 3482 if (port_num < CTL_MAX_PORTS) 3483 return(port_num); 3484 else 3485 return(port_num - CTL_MAX_PORTS); 3486} 3487 3488int 3489ctl_lun_map_init(struct ctl_port *port) 3490{ 3491 struct ctl_softc *softc = control_softc; 3492 struct ctl_lun *lun; 3493 uint32_t i; 3494 3495 if (port->lun_map == NULL) 3496 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3497 M_CTL, M_NOWAIT); 3498 if (port->lun_map == NULL) 3499 return (ENOMEM); 3500 for (i = 0; i < CTL_MAX_LUNS; i++) 3501 port->lun_map[i] = UINT32_MAX; 3502 if (port->status & CTL_PORT_STATUS_ONLINE) { 3503 STAILQ_FOREACH(lun, &softc->lun_list, links) 3504 port->lun_disable(port->targ_lun_arg, lun->lun); 3505 } 3506 return (0); 3507} 3508 3509int 3510ctl_lun_map_deinit(struct ctl_port *port) 3511{ 3512 struct ctl_softc *softc = control_softc; 3513 struct ctl_lun *lun; 3514 3515 if (port->lun_map == NULL) 3516 return (0); 3517 free(port->lun_map, M_CTL); 3518 port->lun_map = NULL; 3519 if (port->status & CTL_PORT_STATUS_ONLINE) { 3520 STAILQ_FOREACH(lun, &softc->lun_list, links) 3521 port->lun_enable(port->targ_lun_arg, lun->lun); 3522 } 3523 return (0); 3524} 3525 3526int 3527ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3528{ 3529 int status; 3530 uint32_t old; 3531 3532 if (port->lun_map == NULL) { 3533 status = ctl_lun_map_init(port); 3534 if (status != 0) 3535 return (status); 3536 } 3537 old = port->lun_map[plun]; 3538 port->lun_map[plun] = glun; 3539 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) 3540 port->lun_enable(port->targ_lun_arg, plun); 3541 return (0); 3542} 3543 3544int 3545ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3546{ 3547 uint32_t old; 3548 3549 if (port->lun_map == NULL) 3550 return (0); 3551 old = port->lun_map[plun]; 3552 port->lun_map[plun] = UINT32_MAX; 3553 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) 3554 port->lun_disable(port->targ_lun_arg, plun); 3555 return (0); 3556} 3557 3558uint32_t 3559ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3560{ 3561 3562 if (port == NULL) 3563 return (UINT32_MAX); 3564 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3565 return (lun_id); 3566 return (port->lun_map[lun_id]); 3567} 3568 3569uint32_t 3570ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3571{ 3572 uint32_t i; 3573 3574 if (port == NULL) 3575 return (UINT32_MAX); 3576 if (port->lun_map == NULL) 3577 return (lun_id); 3578 for (i = 0; i < CTL_MAX_LUNS; i++) { 3579 if (port->lun_map[i] == lun_id) 3580 return (i); 3581 } 3582 return (UINT32_MAX); 3583} 3584 3585static struct ctl_port * 3586ctl_io_port(struct ctl_io_hdr *io_hdr) 3587{ 3588 int port_num; 3589 3590 port_num = io_hdr->nexus.targ_port; 3591 return (control_softc->ctl_ports[ctl_port_idx(port_num)]); 3592} 3593 3594/* 3595 * Note: This only works for bitmask sizes that are at least 32 bits, and 3596 * that are a power of 2. 3597 */ 3598int 3599ctl_ffz(uint32_t *mask, uint32_t size) 3600{ 3601 uint32_t num_chunks, num_pieces; 3602 int i, j; 3603 3604 num_chunks = (size >> 5); 3605 if (num_chunks == 0) 3606 num_chunks++; 3607 num_pieces = MIN((sizeof(uint32_t) * 8), size); 3608 3609 for (i = 0; i < num_chunks; i++) { 3610 for (j = 0; j < num_pieces; j++) { 3611 if ((mask[i] & (1 << j)) == 0) 3612 return ((i << 5) + j); 3613 } 3614 } 3615 3616 return (-1); 3617} 3618 3619int 3620ctl_set_mask(uint32_t *mask, uint32_t bit) 3621{ 3622 uint32_t chunk, piece; 3623 3624 chunk = bit >> 5; 3625 piece = bit % (sizeof(uint32_t) * 8); 3626 3627 if ((mask[chunk] & (1 << piece)) != 0) 3628 return (-1); 3629 else 3630 mask[chunk] |= (1 << piece); 3631 3632 return (0); 3633} 3634 3635int 3636ctl_clear_mask(uint32_t *mask, uint32_t bit) 3637{ 3638 uint32_t chunk, piece; 3639 3640 chunk = bit >> 5; 3641 piece = bit % (sizeof(uint32_t) * 8); 3642 3643 if ((mask[chunk] & (1 << piece)) == 0) 3644 return (-1); 3645 else 3646 mask[chunk] &= ~(1 << piece); 3647 3648 return (0); 3649} 3650 3651int 3652ctl_is_set(uint32_t *mask, uint32_t bit) 3653{ 3654 uint32_t chunk, piece; 3655 3656 chunk = bit >> 5; 3657 piece = bit % (sizeof(uint32_t) * 8); 3658 3659 if ((mask[chunk] & (1 << piece)) == 0) 3660 return (0); 3661 else 3662 return (1); 3663} 3664 3665static uint64_t 3666ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3667{ 3668 uint64_t *t; 3669 3670 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3671 if (t == NULL) 3672 return (0); 3673 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3674} 3675 3676static void 3677ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3678{ 3679 uint64_t *t; 3680 3681 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3682 if (t == NULL) 3683 return; 3684 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3685} 3686 3687static void 3688ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3689{ 3690 uint64_t *p; 3691 u_int i; 3692 3693 i = residx/CTL_MAX_INIT_PER_PORT; 3694 if (lun->pr_keys[i] != NULL) 3695 return; 3696 mtx_unlock(&lun->lun_lock); 3697 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3698 M_WAITOK | M_ZERO); 3699 mtx_lock(&lun->lun_lock); 3700 if (lun->pr_keys[i] == NULL) 3701 lun->pr_keys[i] = p; 3702 else 3703 free(p, M_CTL); 3704} 3705 3706static void 3707ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3708{ 3709 uint64_t *t; 3710 3711 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3712 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3713 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3714} 3715 3716/* 3717 * ctl_softc, pool_name, total_ctl_io are passed in. 3718 * npool is passed out. 3719 */ 3720int 3721ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3722 uint32_t total_ctl_io, void **npool) 3723{ 3724#ifdef IO_POOLS 3725 struct ctl_io_pool *pool; 3726 3727 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3728 M_NOWAIT | M_ZERO); 3729 if (pool == NULL) 3730 return (ENOMEM); 3731 3732 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3733 pool->ctl_softc = ctl_softc; 3734 pool->zone = uma_zsecond_create(pool->name, NULL, 3735 NULL, NULL, NULL, ctl_softc->io_zone); 3736 /* uma_prealloc(pool->zone, total_ctl_io); */ 3737 3738 *npool = pool; 3739#else 3740 *npool = ctl_softc->io_zone; 3741#endif 3742 return (0); 3743} 3744 3745void 3746ctl_pool_free(struct ctl_io_pool *pool) 3747{ 3748 3749 if (pool == NULL) 3750 return; 3751 3752#ifdef IO_POOLS 3753 uma_zdestroy(pool->zone); 3754 free(pool, M_CTL); 3755#endif 3756} 3757 3758union ctl_io * 3759ctl_alloc_io(void *pool_ref) 3760{ 3761 union ctl_io *io; 3762#ifdef IO_POOLS 3763 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3764 3765 io = uma_zalloc(pool->zone, M_WAITOK); 3766#else 3767 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3768#endif 3769 if (io != NULL) 3770 io->io_hdr.pool = pool_ref; 3771 return (io); 3772} 3773 3774union ctl_io * 3775ctl_alloc_io_nowait(void *pool_ref) 3776{ 3777 union ctl_io *io; 3778#ifdef IO_POOLS 3779 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3780 3781 io = uma_zalloc(pool->zone, M_NOWAIT); 3782#else 3783 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3784#endif 3785 if (io != NULL) 3786 io->io_hdr.pool = pool_ref; 3787 return (io); 3788} 3789 3790void 3791ctl_free_io(union ctl_io *io) 3792{ 3793#ifdef IO_POOLS 3794 struct ctl_io_pool *pool; 3795#endif 3796 3797 if (io == NULL) 3798 return; 3799 3800#ifdef IO_POOLS 3801 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3802 uma_zfree(pool->zone, io); 3803#else 3804 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3805#endif 3806} 3807 3808void 3809ctl_zero_io(union ctl_io *io) 3810{ 3811 void *pool_ref; 3812 3813 if (io == NULL) 3814 return; 3815 3816 /* 3817 * May need to preserve linked list pointers at some point too. 3818 */ 3819 pool_ref = io->io_hdr.pool; 3820 memset(io, 0, sizeof(*io)); 3821 io->io_hdr.pool = pool_ref; 3822} 3823 3824/* 3825 * This routine is currently used for internal copies of ctl_ios that need 3826 * to persist for some reason after we've already returned status to the 3827 * FETD. (Thus the flag set.) 3828 * 3829 * XXX XXX 3830 * Note that this makes a blind copy of all fields in the ctl_io, except 3831 * for the pool reference. This includes any memory that has been 3832 * allocated! That memory will no longer be valid after done has been 3833 * called, so this would be VERY DANGEROUS for command that actually does 3834 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3835 * start and stop commands, which don't transfer any data, so this is not a 3836 * problem. If it is used for anything else, the caller would also need to 3837 * allocate data buffer space and this routine would need to be modified to 3838 * copy the data buffer(s) as well. 3839 */ 3840void 3841ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3842{ 3843 void *pool_ref; 3844 3845 if ((src == NULL) 3846 || (dest == NULL)) 3847 return; 3848 3849 /* 3850 * May need to preserve linked list pointers at some point too. 3851 */ 3852 pool_ref = dest->io_hdr.pool; 3853 3854 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest))); 3855 3856 dest->io_hdr.pool = pool_ref; 3857 /* 3858 * We need to know that this is an internal copy, and doesn't need 3859 * to get passed back to the FETD that allocated it. 3860 */ 3861 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3862} 3863 3864int 3865ctl_expand_number(const char *buf, uint64_t *num) 3866{ 3867 char *endptr; 3868 uint64_t number; 3869 unsigned shift; 3870 3871 number = strtoq(buf, &endptr, 0); 3872 3873 switch (tolower((unsigned char)*endptr)) { 3874 case 'e': 3875 shift = 60; 3876 break; 3877 case 'p': 3878 shift = 50; 3879 break; 3880 case 't': 3881 shift = 40; 3882 break; 3883 case 'g': 3884 shift = 30; 3885 break; 3886 case 'm': 3887 shift = 20; 3888 break; 3889 case 'k': 3890 shift = 10; 3891 break; 3892 case 'b': 3893 case '\0': /* No unit. */ 3894 *num = number; 3895 return (0); 3896 default: 3897 /* Unrecognized unit. */ 3898 return (-1); 3899 } 3900 3901 if ((number << shift) >> shift != number) { 3902 /* Overflow */ 3903 return (-1); 3904 } 3905 *num = number << shift; 3906 return (0); 3907} 3908 3909 3910/* 3911 * This routine could be used in the future to load default and/or saved 3912 * mode page parameters for a particuar lun. 3913 */ 3914static int 3915ctl_init_page_index(struct ctl_lun *lun) 3916{ 3917 int i; 3918 struct ctl_page_index *page_index; 3919 const char *value; 3920 uint64_t ival; 3921 3922 memcpy(&lun->mode_pages.index, page_index_template, 3923 sizeof(page_index_template)); 3924 3925 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3926 3927 page_index = &lun->mode_pages.index[i]; 3928 /* 3929 * If this is a disk-only mode page, there's no point in 3930 * setting it up. For some pages, we have to have some 3931 * basic information about the disk in order to calculate the 3932 * mode page data. 3933 */ 3934 if ((lun->be_lun->lun_type != T_DIRECT) 3935 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3936 continue; 3937 3938 switch (page_index->page_code & SMPH_PC_MASK) { 3939 case SMS_RW_ERROR_RECOVERY_PAGE: { 3940 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3941 panic("subpage is incorrect!"); 3942 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3943 &rw_er_page_default, 3944 sizeof(rw_er_page_default)); 3945 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3946 &rw_er_page_changeable, 3947 sizeof(rw_er_page_changeable)); 3948 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3949 &rw_er_page_default, 3950 sizeof(rw_er_page_default)); 3951 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3952 &rw_er_page_default, 3953 sizeof(rw_er_page_default)); 3954 page_index->page_data = 3955 (uint8_t *)lun->mode_pages.rw_er_page; 3956 break; 3957 } 3958 case SMS_FORMAT_DEVICE_PAGE: { 3959 struct scsi_format_page *format_page; 3960 3961 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3962 panic("subpage is incorrect!"); 3963 3964 /* 3965 * Sectors per track are set above. Bytes per 3966 * sector need to be set here on a per-LUN basis. 3967 */ 3968 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3969 &format_page_default, 3970 sizeof(format_page_default)); 3971 memcpy(&lun->mode_pages.format_page[ 3972 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3973 sizeof(format_page_changeable)); 3974 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3975 &format_page_default, 3976 sizeof(format_page_default)); 3977 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3978 &format_page_default, 3979 sizeof(format_page_default)); 3980 3981 format_page = &lun->mode_pages.format_page[ 3982 CTL_PAGE_CURRENT]; 3983 scsi_ulto2b(lun->be_lun->blocksize, 3984 format_page->bytes_per_sector); 3985 3986 format_page = &lun->mode_pages.format_page[ 3987 CTL_PAGE_DEFAULT]; 3988 scsi_ulto2b(lun->be_lun->blocksize, 3989 format_page->bytes_per_sector); 3990 3991 format_page = &lun->mode_pages.format_page[ 3992 CTL_PAGE_SAVED]; 3993 scsi_ulto2b(lun->be_lun->blocksize, 3994 format_page->bytes_per_sector); 3995 3996 page_index->page_data = 3997 (uint8_t *)lun->mode_pages.format_page; 3998 break; 3999 } 4000 case SMS_RIGID_DISK_PAGE: { 4001 struct scsi_rigid_disk_page *rigid_disk_page; 4002 uint32_t sectors_per_cylinder; 4003 uint64_t cylinders; 4004#ifndef __XSCALE__ 4005 int shift; 4006#endif /* !__XSCALE__ */ 4007 4008 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4009 panic("invalid subpage value %d", 4010 page_index->subpage); 4011 4012 /* 4013 * Rotation rate and sectors per track are set 4014 * above. We calculate the cylinders here based on 4015 * capacity. Due to the number of heads and 4016 * sectors per track we're using, smaller arrays 4017 * may turn out to have 0 cylinders. Linux and 4018 * FreeBSD don't pay attention to these mode pages 4019 * to figure out capacity, but Solaris does. It 4020 * seems to deal with 0 cylinders just fine, and 4021 * works out a fake geometry based on the capacity. 4022 */ 4023 memcpy(&lun->mode_pages.rigid_disk_page[ 4024 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4025 sizeof(rigid_disk_page_default)); 4026 memcpy(&lun->mode_pages.rigid_disk_page[ 4027 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4028 sizeof(rigid_disk_page_changeable)); 4029 4030 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4031 CTL_DEFAULT_HEADS; 4032 4033 /* 4034 * The divide method here will be more accurate, 4035 * probably, but results in floating point being 4036 * used in the kernel on i386 (__udivdi3()). On the 4037 * XScale, though, __udivdi3() is implemented in 4038 * software. 4039 * 4040 * The shift method for cylinder calculation is 4041 * accurate if sectors_per_cylinder is a power of 4042 * 2. Otherwise it might be slightly off -- you 4043 * might have a bit of a truncation problem. 4044 */ 4045#ifdef __XSCALE__ 4046 cylinders = (lun->be_lun->maxlba + 1) / 4047 sectors_per_cylinder; 4048#else 4049 for (shift = 31; shift > 0; shift--) { 4050 if (sectors_per_cylinder & (1 << shift)) 4051 break; 4052 } 4053 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4054#endif 4055 4056 /* 4057 * We've basically got 3 bytes, or 24 bits for the 4058 * cylinder size in the mode page. If we're over, 4059 * just round down to 2^24. 4060 */ 4061 if (cylinders > 0xffffff) 4062 cylinders = 0xffffff; 4063 4064 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4065 CTL_PAGE_DEFAULT]; 4066 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4067 4068 if ((value = ctl_get_opt(&lun->be_lun->options, 4069 "rpm")) != NULL) { 4070 scsi_ulto2b(strtol(value, NULL, 0), 4071 rigid_disk_page->rotation_rate); 4072 } 4073 4074 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4075 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4076 sizeof(rigid_disk_page_default)); 4077 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4078 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4079 sizeof(rigid_disk_page_default)); 4080 4081 page_index->page_data = 4082 (uint8_t *)lun->mode_pages.rigid_disk_page; 4083 break; 4084 } 4085 case SMS_CACHING_PAGE: { 4086 struct scsi_caching_page *caching_page; 4087 4088 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4089 panic("invalid subpage value %d", 4090 page_index->subpage); 4091 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4092 &caching_page_default, 4093 sizeof(caching_page_default)); 4094 memcpy(&lun->mode_pages.caching_page[ 4095 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4096 sizeof(caching_page_changeable)); 4097 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4098 &caching_page_default, 4099 sizeof(caching_page_default)); 4100 caching_page = &lun->mode_pages.caching_page[ 4101 CTL_PAGE_SAVED]; 4102 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4103 if (value != NULL && strcmp(value, "off") == 0) 4104 caching_page->flags1 &= ~SCP_WCE; 4105 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4106 if (value != NULL && strcmp(value, "off") == 0) 4107 caching_page->flags1 |= SCP_RCD; 4108 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4109 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4110 sizeof(caching_page_default)); 4111 page_index->page_data = 4112 (uint8_t *)lun->mode_pages.caching_page; 4113 break; 4114 } 4115 case SMS_CONTROL_MODE_PAGE: { 4116 struct scsi_control_page *control_page; 4117 4118 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4119 panic("invalid subpage value %d", 4120 page_index->subpage); 4121 4122 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4123 &control_page_default, 4124 sizeof(control_page_default)); 4125 memcpy(&lun->mode_pages.control_page[ 4126 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4127 sizeof(control_page_changeable)); 4128 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4129 &control_page_default, 4130 sizeof(control_page_default)); 4131 control_page = &lun->mode_pages.control_page[ 4132 CTL_PAGE_SAVED]; 4133 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4134 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4135 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4136 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4137 } 4138 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4139 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4140 sizeof(control_page_default)); 4141 page_index->page_data = 4142 (uint8_t *)lun->mode_pages.control_page; 4143 break; 4144 4145 } 4146 case SMS_INFO_EXCEPTIONS_PAGE: { 4147 switch (page_index->subpage) { 4148 case SMS_SUBPAGE_PAGE_0: 4149 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4150 &ie_page_default, 4151 sizeof(ie_page_default)); 4152 memcpy(&lun->mode_pages.ie_page[ 4153 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4154 sizeof(ie_page_changeable)); 4155 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4156 &ie_page_default, 4157 sizeof(ie_page_default)); 4158 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4159 &ie_page_default, 4160 sizeof(ie_page_default)); 4161 page_index->page_data = 4162 (uint8_t *)lun->mode_pages.ie_page; 4163 break; 4164 case 0x02: { 4165 struct ctl_logical_block_provisioning_page *page; 4166 4167 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4168 &lbp_page_default, 4169 sizeof(lbp_page_default)); 4170 memcpy(&lun->mode_pages.lbp_page[ 4171 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4172 sizeof(lbp_page_changeable)); 4173 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4174 &lbp_page_default, 4175 sizeof(lbp_page_default)); 4176 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4177 value = ctl_get_opt(&lun->be_lun->options, 4178 "avail-threshold"); 4179 if (value != NULL && 4180 ctl_expand_number(value, &ival) == 0) { 4181 page->descr[0].flags |= SLBPPD_ENABLED | 4182 SLBPPD_ARMING_DEC; 4183 if (lun->be_lun->blocksize) 4184 ival /= lun->be_lun->blocksize; 4185 else 4186 ival /= 512; 4187 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4188 page->descr[0].count); 4189 } 4190 value = ctl_get_opt(&lun->be_lun->options, 4191 "used-threshold"); 4192 if (value != NULL && 4193 ctl_expand_number(value, &ival) == 0) { 4194 page->descr[1].flags |= SLBPPD_ENABLED | 4195 SLBPPD_ARMING_INC; 4196 if (lun->be_lun->blocksize) 4197 ival /= lun->be_lun->blocksize; 4198 else 4199 ival /= 512; 4200 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4201 page->descr[1].count); 4202 } 4203 value = ctl_get_opt(&lun->be_lun->options, 4204 "pool-avail-threshold"); 4205 if (value != NULL && 4206 ctl_expand_number(value, &ival) == 0) { 4207 page->descr[2].flags |= SLBPPD_ENABLED | 4208 SLBPPD_ARMING_DEC; 4209 if (lun->be_lun->blocksize) 4210 ival /= lun->be_lun->blocksize; 4211 else 4212 ival /= 512; 4213 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4214 page->descr[2].count); 4215 } 4216 value = ctl_get_opt(&lun->be_lun->options, 4217 "pool-used-threshold"); 4218 if (value != NULL && 4219 ctl_expand_number(value, &ival) == 0) { 4220 page->descr[3].flags |= SLBPPD_ENABLED | 4221 SLBPPD_ARMING_INC; 4222 if (lun->be_lun->blocksize) 4223 ival /= lun->be_lun->blocksize; 4224 else 4225 ival /= 512; 4226 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4227 page->descr[3].count); 4228 } 4229 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4230 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4231 sizeof(lbp_page_default)); 4232 page_index->page_data = 4233 (uint8_t *)lun->mode_pages.lbp_page; 4234 }} 4235 break; 4236 } 4237 case SMS_VENDOR_SPECIFIC_PAGE:{ 4238 switch (page_index->subpage) { 4239 case DBGCNF_SUBPAGE_CODE: { 4240 struct copan_debugconf_subpage *current_page, 4241 *saved_page; 4242 4243 memcpy(&lun->mode_pages.debugconf_subpage[ 4244 CTL_PAGE_CURRENT], 4245 &debugconf_page_default, 4246 sizeof(debugconf_page_default)); 4247 memcpy(&lun->mode_pages.debugconf_subpage[ 4248 CTL_PAGE_CHANGEABLE], 4249 &debugconf_page_changeable, 4250 sizeof(debugconf_page_changeable)); 4251 memcpy(&lun->mode_pages.debugconf_subpage[ 4252 CTL_PAGE_DEFAULT], 4253 &debugconf_page_default, 4254 sizeof(debugconf_page_default)); 4255 memcpy(&lun->mode_pages.debugconf_subpage[ 4256 CTL_PAGE_SAVED], 4257 &debugconf_page_default, 4258 sizeof(debugconf_page_default)); 4259 page_index->page_data = 4260 (uint8_t *)lun->mode_pages.debugconf_subpage; 4261 4262 current_page = (struct copan_debugconf_subpage *) 4263 (page_index->page_data + 4264 (page_index->page_len * 4265 CTL_PAGE_CURRENT)); 4266 saved_page = (struct copan_debugconf_subpage *) 4267 (page_index->page_data + 4268 (page_index->page_len * 4269 CTL_PAGE_SAVED)); 4270 break; 4271 } 4272 default: 4273 panic("invalid subpage value %d", 4274 page_index->subpage); 4275 break; 4276 } 4277 break; 4278 } 4279 default: 4280 panic("invalid page value %d", 4281 page_index->page_code & SMPH_PC_MASK); 4282 break; 4283 } 4284 } 4285 4286 return (CTL_RETVAL_COMPLETE); 4287} 4288 4289static int 4290ctl_init_log_page_index(struct ctl_lun *lun) 4291{ 4292 struct ctl_page_index *page_index; 4293 int i, j, k, prev; 4294 4295 memcpy(&lun->log_pages.index, log_page_index_template, 4296 sizeof(log_page_index_template)); 4297 4298 prev = -1; 4299 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4300 4301 page_index = &lun->log_pages.index[i]; 4302 /* 4303 * If this is a disk-only mode page, there's no point in 4304 * setting it up. For some pages, we have to have some 4305 * basic information about the disk in order to calculate the 4306 * mode page data. 4307 */ 4308 if ((lun->be_lun->lun_type != T_DIRECT) 4309 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4310 continue; 4311 4312 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4313 lun->backend->lun_attr == NULL) 4314 continue; 4315 4316 if (page_index->page_code != prev) { 4317 lun->log_pages.pages_page[j] = page_index->page_code; 4318 prev = page_index->page_code; 4319 j++; 4320 } 4321 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4322 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4323 k++; 4324 } 4325 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4326 lun->log_pages.index[0].page_len = j; 4327 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4328 lun->log_pages.index[1].page_len = k * 2; 4329 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4330 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4331 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4332 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4333 4334 return (CTL_RETVAL_COMPLETE); 4335} 4336 4337static int 4338hex2bin(const char *str, uint8_t *buf, int buf_size) 4339{ 4340 int i; 4341 u_char c; 4342 4343 memset(buf, 0, buf_size); 4344 while (isspace(str[0])) 4345 str++; 4346 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4347 str += 2; 4348 buf_size *= 2; 4349 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4350 c = str[i]; 4351 if (isdigit(c)) 4352 c -= '0'; 4353 else if (isalpha(c)) 4354 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4355 else 4356 break; 4357 if (c >= 16) 4358 break; 4359 if ((i & 1) == 0) 4360 buf[i / 2] |= (c << 4); 4361 else 4362 buf[i / 2] |= c; 4363 } 4364 return ((i + 1) / 2); 4365} 4366 4367/* 4368 * LUN allocation. 4369 * 4370 * Requirements: 4371 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4372 * wants us to allocate the LUN and he can block. 4373 * - ctl_softc is always set 4374 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4375 * 4376 * Returns 0 for success, non-zero (errno) for failure. 4377 */ 4378static int 4379ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4380 struct ctl_be_lun *const be_lun) 4381{ 4382 struct ctl_lun *nlun, *lun; 4383 struct scsi_vpd_id_descriptor *desc; 4384 struct scsi_vpd_id_t10 *t10id; 4385 const char *eui, *naa, *scsiname, *vendor, *value; 4386 int lun_number, i, lun_malloced; 4387 int devidlen, idlen1, idlen2 = 0, len; 4388 4389 if (be_lun == NULL) 4390 return (EINVAL); 4391 4392 /* 4393 * We currently only support Direct Access or Processor LUN types. 4394 */ 4395 switch (be_lun->lun_type) { 4396 case T_DIRECT: 4397 break; 4398 case T_PROCESSOR: 4399 break; 4400 case T_SEQUENTIAL: 4401 case T_CHANGER: 4402 default: 4403 be_lun->lun_config_status(be_lun->be_lun, 4404 CTL_LUN_CONFIG_FAILURE); 4405 break; 4406 } 4407 if (ctl_lun == NULL) { 4408 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4409 lun_malloced = 1; 4410 } else { 4411 lun_malloced = 0; 4412 lun = ctl_lun; 4413 } 4414 4415 memset(lun, 0, sizeof(*lun)); 4416 if (lun_malloced) 4417 lun->flags = CTL_LUN_MALLOCED; 4418 4419 /* Generate LUN ID. */ 4420 devidlen = max(CTL_DEVID_MIN_LEN, 4421 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4422 idlen1 = sizeof(*t10id) + devidlen; 4423 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4424 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4425 if (scsiname != NULL) { 4426 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4427 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4428 } 4429 eui = ctl_get_opt(&be_lun->options, "eui"); 4430 if (eui != NULL) { 4431 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4432 } 4433 naa = ctl_get_opt(&be_lun->options, "naa"); 4434 if (naa != NULL) { 4435 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4436 } 4437 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4438 M_CTL, M_WAITOK | M_ZERO); 4439 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4440 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4441 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4442 desc->length = idlen1; 4443 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4444 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4445 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4446 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4447 } else { 4448 strncpy(t10id->vendor, vendor, 4449 min(sizeof(t10id->vendor), strlen(vendor))); 4450 } 4451 strncpy((char *)t10id->vendor_spec_id, 4452 (char *)be_lun->device_id, devidlen); 4453 if (scsiname != NULL) { 4454 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4455 desc->length); 4456 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4457 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4458 SVPD_ID_TYPE_SCSI_NAME; 4459 desc->length = idlen2; 4460 strlcpy(desc->identifier, scsiname, idlen2); 4461 } 4462 if (eui != NULL) { 4463 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4464 desc->length); 4465 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4466 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4467 SVPD_ID_TYPE_EUI64; 4468 desc->length = hex2bin(eui, desc->identifier, 16); 4469 desc->length = desc->length > 12 ? 16 : 4470 (desc->length > 8 ? 12 : 8); 4471 len -= 16 - desc->length; 4472 } 4473 if (naa != NULL) { 4474 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4475 desc->length); 4476 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4477 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4478 SVPD_ID_TYPE_NAA; 4479 desc->length = hex2bin(naa, desc->identifier, 16); 4480 desc->length = desc->length > 8 ? 16 : 8; 4481 len -= 16 - desc->length; 4482 } 4483 lun->lun_devid->len = len; 4484 4485 mtx_lock(&ctl_softc->ctl_lock); 4486 /* 4487 * See if the caller requested a particular LUN number. If so, see 4488 * if it is available. Otherwise, allocate the first available LUN. 4489 */ 4490 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4491 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4492 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4493 mtx_unlock(&ctl_softc->ctl_lock); 4494 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4495 printf("ctl: requested LUN ID %d is higher " 4496 "than CTL_MAX_LUNS - 1 (%d)\n", 4497 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4498 } else { 4499 /* 4500 * XXX KDM return an error, or just assign 4501 * another LUN ID in this case?? 4502 */ 4503 printf("ctl: requested LUN ID %d is already " 4504 "in use\n", be_lun->req_lun_id); 4505 } 4506 if (lun->flags & CTL_LUN_MALLOCED) 4507 free(lun, M_CTL); 4508 be_lun->lun_config_status(be_lun->be_lun, 4509 CTL_LUN_CONFIG_FAILURE); 4510 return (ENOSPC); 4511 } 4512 lun_number = be_lun->req_lun_id; 4513 } else { 4514 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4515 if (lun_number == -1) { 4516 mtx_unlock(&ctl_softc->ctl_lock); 4517 printf("ctl: can't allocate LUN, out of LUNs\n"); 4518 if (lun->flags & CTL_LUN_MALLOCED) 4519 free(lun, M_CTL); 4520 be_lun->lun_config_status(be_lun->be_lun, 4521 CTL_LUN_CONFIG_FAILURE); 4522 return (ENOSPC); 4523 } 4524 } 4525 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4526 4527 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4528 lun->lun = lun_number; 4529 lun->be_lun = be_lun; 4530 /* 4531 * The processor LUN is always enabled. Disk LUNs come on line 4532 * disabled, and must be enabled by the backend. 4533 */ 4534 lun->flags |= CTL_LUN_DISABLED; 4535 lun->backend = be_lun->be; 4536 be_lun->ctl_lun = lun; 4537 be_lun->lun_id = lun_number; 4538 atomic_add_int(&be_lun->be->num_luns, 1); 4539 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4540 lun->flags |= CTL_LUN_OFFLINE; 4541 4542 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4543 lun->flags |= CTL_LUN_STOPPED; 4544 4545 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4546 lun->flags |= CTL_LUN_INOPERABLE; 4547 4548 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4549 lun->flags |= CTL_LUN_PRIMARY_SC; 4550 4551 value = ctl_get_opt(&be_lun->options, "readonly"); 4552 if (value != NULL && strcmp(value, "on") == 0) 4553 lun->flags |= CTL_LUN_READONLY; 4554 4555 lun->serseq = CTL_LUN_SERSEQ_OFF; 4556 if (be_lun->flags & CTL_LUN_FLAG_SERSEQ_READ) 4557 lun->serseq = CTL_LUN_SERSEQ_READ; 4558 value = ctl_get_opt(&be_lun->options, "serseq"); 4559 if (value != NULL && strcmp(value, "on") == 0) 4560 lun->serseq = CTL_LUN_SERSEQ_ON; 4561 else if (value != NULL && strcmp(value, "read") == 0) 4562 lun->serseq = CTL_LUN_SERSEQ_READ; 4563 else if (value != NULL && strcmp(value, "off") == 0) 4564 lun->serseq = CTL_LUN_SERSEQ_OFF; 4565 4566 lun->ctl_softc = ctl_softc; 4567#ifdef CTL_TIME_IO 4568 lun->last_busy = getsbinuptime(); 4569#endif 4570 TAILQ_INIT(&lun->ooa_queue); 4571 TAILQ_INIT(&lun->blocked_queue); 4572 STAILQ_INIT(&lun->error_list); 4573 ctl_tpc_lun_init(lun); 4574 4575 /* 4576 * Initialize the mode and log page index. 4577 */ 4578 ctl_init_page_index(lun); 4579 ctl_init_log_page_index(lun); 4580 4581 /* 4582 * Now, before we insert this lun on the lun list, set the lun 4583 * inventory changed UA for all other luns. 4584 */ 4585 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4586 mtx_lock(&nlun->lun_lock); 4587 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4588 mtx_unlock(&nlun->lun_lock); 4589 } 4590 4591 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4592 4593 ctl_softc->ctl_luns[lun_number] = lun; 4594 4595 ctl_softc->num_luns++; 4596 4597 /* Setup statistics gathering */ 4598 lun->stats.device_type = be_lun->lun_type; 4599 lun->stats.lun_number = lun_number; 4600 if (lun->stats.device_type == T_DIRECT) 4601 lun->stats.blocksize = be_lun->blocksize; 4602 else 4603 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4604 for (i = 0;i < CTL_MAX_PORTS;i++) 4605 lun->stats.ports[i].targ_port = i; 4606 4607 mtx_unlock(&ctl_softc->ctl_lock); 4608 4609 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4610 return (0); 4611} 4612 4613/* 4614 * Delete a LUN. 4615 * Assumptions: 4616 * - LUN has already been marked invalid and any pending I/O has been taken 4617 * care of. 4618 */ 4619static int 4620ctl_free_lun(struct ctl_lun *lun) 4621{ 4622 struct ctl_softc *softc; 4623 struct ctl_lun *nlun; 4624 int i; 4625 4626 softc = lun->ctl_softc; 4627 4628 mtx_assert(&softc->ctl_lock, MA_OWNED); 4629 4630 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4631 4632 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4633 4634 softc->ctl_luns[lun->lun] = NULL; 4635 4636 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4637 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4638 4639 softc->num_luns--; 4640 4641 /* 4642 * Tell the backend to free resources, if this LUN has a backend. 4643 */ 4644 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4645 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4646 4647 ctl_tpc_lun_shutdown(lun); 4648 mtx_destroy(&lun->lun_lock); 4649 free(lun->lun_devid, M_CTL); 4650 for (i = 0; i < CTL_MAX_PORTS; i++) 4651 free(lun->pending_ua[i], M_CTL); 4652 for (i = 0; i < 2 * CTL_MAX_PORTS; i++) 4653 free(lun->pr_keys[i], M_CTL); 4654 free(lun->write_buffer, M_CTL); 4655 if (lun->flags & CTL_LUN_MALLOCED) 4656 free(lun, M_CTL); 4657 4658 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4659 mtx_lock(&nlun->lun_lock); 4660 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4661 mtx_unlock(&nlun->lun_lock); 4662 } 4663 4664 return (0); 4665} 4666 4667static void 4668ctl_create_lun(struct ctl_be_lun *be_lun) 4669{ 4670 struct ctl_softc *softc; 4671 4672 softc = control_softc; 4673 4674 /* 4675 * ctl_alloc_lun() should handle all potential failure cases. 4676 */ 4677 ctl_alloc_lun(softc, NULL, be_lun); 4678} 4679 4680int 4681ctl_add_lun(struct ctl_be_lun *be_lun) 4682{ 4683 struct ctl_softc *softc = control_softc; 4684 4685 mtx_lock(&softc->ctl_lock); 4686 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4687 mtx_unlock(&softc->ctl_lock); 4688 wakeup(&softc->pending_lun_queue); 4689 4690 return (0); 4691} 4692 4693int 4694ctl_enable_lun(struct ctl_be_lun *be_lun) 4695{ 4696 struct ctl_softc *softc; 4697 struct ctl_port *port, *nport; 4698 struct ctl_lun *lun; 4699 int retval; 4700 4701 lun = (struct ctl_lun *)be_lun->ctl_lun; 4702 softc = lun->ctl_softc; 4703 4704 mtx_lock(&softc->ctl_lock); 4705 mtx_lock(&lun->lun_lock); 4706 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4707 /* 4708 * eh? Why did we get called if the LUN is already 4709 * enabled? 4710 */ 4711 mtx_unlock(&lun->lun_lock); 4712 mtx_unlock(&softc->ctl_lock); 4713 return (0); 4714 } 4715 lun->flags &= ~CTL_LUN_DISABLED; 4716 mtx_unlock(&lun->lun_lock); 4717 4718 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) { 4719 nport = STAILQ_NEXT(port, links); 4720 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4721 port->lun_map != NULL) 4722 continue; 4723 4724 /* 4725 * Drop the lock while we call the FETD's enable routine. 4726 * This can lead to a callback into CTL (at least in the 4727 * case of the internal initiator frontend. 4728 */ 4729 mtx_unlock(&softc->ctl_lock); 4730 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4731 mtx_lock(&softc->ctl_lock); 4732 if (retval != 0) { 4733 printf("%s: FETD %s port %d returned error " 4734 "%d for lun_enable on lun %jd\n", 4735 __func__, port->port_name, port->targ_port, 4736 retval, (intmax_t)lun->lun); 4737 } 4738 } 4739 4740 mtx_unlock(&softc->ctl_lock); 4741 4742 return (0); 4743} 4744 4745int 4746ctl_disable_lun(struct ctl_be_lun *be_lun) 4747{ 4748 struct ctl_softc *softc; 4749 struct ctl_port *port; 4750 struct ctl_lun *lun; 4751 int retval; 4752 4753 lun = (struct ctl_lun *)be_lun->ctl_lun; 4754 softc = lun->ctl_softc; 4755 4756 mtx_lock(&softc->ctl_lock); 4757 mtx_lock(&lun->lun_lock); 4758 if (lun->flags & CTL_LUN_DISABLED) { 4759 mtx_unlock(&lun->lun_lock); 4760 mtx_unlock(&softc->ctl_lock); 4761 return (0); 4762 } 4763 lun->flags |= CTL_LUN_DISABLED; 4764 mtx_unlock(&lun->lun_lock); 4765 4766 STAILQ_FOREACH(port, &softc->port_list, links) { 4767 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4768 port->lun_map != NULL) 4769 continue; 4770 mtx_unlock(&softc->ctl_lock); 4771 /* 4772 * Drop the lock before we call the frontend's disable 4773 * routine, to avoid lock order reversals. 4774 * 4775 * XXX KDM what happens if the frontend list changes while 4776 * we're traversing it? It's unlikely, but should be handled. 4777 */ 4778 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4779 mtx_lock(&softc->ctl_lock); 4780 if (retval != 0) { 4781 printf("%s: FETD %s port %d returned error " 4782 "%d for lun_disable on lun %jd\n", 4783 __func__, port->port_name, port->targ_port, 4784 retval, (intmax_t)lun->lun); 4785 } 4786 } 4787 4788 mtx_unlock(&softc->ctl_lock); 4789 4790 return (0); 4791} 4792 4793int 4794ctl_start_lun(struct ctl_be_lun *be_lun) 4795{ 4796 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4797 4798 mtx_lock(&lun->lun_lock); 4799 lun->flags &= ~CTL_LUN_STOPPED; 4800 mtx_unlock(&lun->lun_lock); 4801 return (0); 4802} 4803 4804int 4805ctl_stop_lun(struct ctl_be_lun *be_lun) 4806{ 4807 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4808 4809 mtx_lock(&lun->lun_lock); 4810 lun->flags |= CTL_LUN_STOPPED; 4811 mtx_unlock(&lun->lun_lock); 4812 return (0); 4813} 4814 4815int 4816ctl_lun_offline(struct ctl_be_lun *be_lun) 4817{ 4818 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4819 4820 mtx_lock(&lun->lun_lock); 4821 lun->flags |= CTL_LUN_OFFLINE; 4822 mtx_unlock(&lun->lun_lock); 4823 return (0); 4824} 4825 4826int 4827ctl_lun_online(struct ctl_be_lun *be_lun) 4828{ 4829 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4830 4831 mtx_lock(&lun->lun_lock); 4832 lun->flags &= ~CTL_LUN_OFFLINE; 4833 mtx_unlock(&lun->lun_lock); 4834 return (0); 4835} 4836 4837int 4838ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4839{ 4840 struct ctl_softc *softc; 4841 struct ctl_lun *lun; 4842 4843 lun = (struct ctl_lun *)be_lun->ctl_lun; 4844 softc = lun->ctl_softc; 4845 4846 mtx_lock(&lun->lun_lock); 4847 4848 /* 4849 * The LUN needs to be disabled before it can be marked invalid. 4850 */ 4851 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4852 mtx_unlock(&lun->lun_lock); 4853 return (-1); 4854 } 4855 /* 4856 * Mark the LUN invalid. 4857 */ 4858 lun->flags |= CTL_LUN_INVALID; 4859 4860 /* 4861 * If there is nothing in the OOA queue, go ahead and free the LUN. 4862 * If we have something in the OOA queue, we'll free it when the 4863 * last I/O completes. 4864 */ 4865 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4866 mtx_unlock(&lun->lun_lock); 4867 mtx_lock(&softc->ctl_lock); 4868 ctl_free_lun(lun); 4869 mtx_unlock(&softc->ctl_lock); 4870 } else 4871 mtx_unlock(&lun->lun_lock); 4872 4873 return (0); 4874} 4875 4876int 4877ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4878{ 4879 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4880 4881 mtx_lock(&lun->lun_lock); 4882 lun->flags |= CTL_LUN_INOPERABLE; 4883 mtx_unlock(&lun->lun_lock); 4884 return (0); 4885} 4886 4887int 4888ctl_lun_operable(struct ctl_be_lun *be_lun) 4889{ 4890 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4891 4892 mtx_lock(&lun->lun_lock); 4893 lun->flags &= ~CTL_LUN_INOPERABLE; 4894 mtx_unlock(&lun->lun_lock); 4895 return (0); 4896} 4897 4898void 4899ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4900{ 4901 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4902 4903 mtx_lock(&lun->lun_lock); 4904 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED); 4905 mtx_unlock(&lun->lun_lock); 4906} 4907 4908/* 4909 * Backend "memory move is complete" callback for requests that never 4910 * make it down to say RAIDCore's configuration code. 4911 */ 4912int 4913ctl_config_move_done(union ctl_io *io) 4914{ 4915 int retval; 4916 4917 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4918 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4919 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 4920 4921 if ((io->io_hdr.port_status != 0) && 4922 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4923 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4924 /* 4925 * For hardware error sense keys, the sense key 4926 * specific value is defined to be a retry count, 4927 * but we use it to pass back an internal FETD 4928 * error code. XXX KDM Hopefully the FETD is only 4929 * using 16 bits for an error code, since that's 4930 * all the space we have in the sks field. 4931 */ 4932 ctl_set_internal_failure(&io->scsiio, 4933 /*sks_valid*/ 1, 4934 /*retry_count*/ 4935 io->io_hdr.port_status); 4936 } 4937 4938 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 4939 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4940 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 4941 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4942 /* 4943 * XXX KDM just assuming a single pointer here, and not a 4944 * S/G list. If we start using S/G lists for config data, 4945 * we'll need to know how to clean them up here as well. 4946 */ 4947 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4948 free(io->scsiio.kern_data_ptr, M_CTL); 4949 ctl_done(io); 4950 retval = CTL_RETVAL_COMPLETE; 4951 } else { 4952 /* 4953 * XXX KDM now we need to continue data movement. Some 4954 * options: 4955 * - call ctl_scsiio() again? We don't do this for data 4956 * writes, because for those at least we know ahead of 4957 * time where the write will go and how long it is. For 4958 * config writes, though, that information is largely 4959 * contained within the write itself, thus we need to 4960 * parse out the data again. 4961 * 4962 * - Call some other function once the data is in? 4963 */ 4964 if (ctl_debug & CTL_DEBUG_CDB_DATA) 4965 ctl_data_print(io); 4966 4967 /* 4968 * XXX KDM call ctl_scsiio() again for now, and check flag 4969 * bits to see whether we're allocated or not. 4970 */ 4971 retval = ctl_scsiio(&io->scsiio); 4972 } 4973 return (retval); 4974} 4975 4976/* 4977 * This gets called by a backend driver when it is done with a 4978 * data_submit method. 4979 */ 4980void 4981ctl_data_submit_done(union ctl_io *io) 4982{ 4983 /* 4984 * If the IO_CONT flag is set, we need to call the supplied 4985 * function to continue processing the I/O, instead of completing 4986 * the I/O just yet. 4987 * 4988 * If there is an error, though, we don't want to keep processing. 4989 * Instead, just send status back to the initiator. 4990 */ 4991 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4992 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4993 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4994 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4995 io->scsiio.io_cont(io); 4996 return; 4997 } 4998 ctl_done(io); 4999} 5000 5001/* 5002 * This gets called by a backend driver when it is done with a 5003 * configuration write. 5004 */ 5005void 5006ctl_config_write_done(union ctl_io *io) 5007{ 5008 uint8_t *buf; 5009 5010 /* 5011 * If the IO_CONT flag is set, we need to call the supplied 5012 * function to continue processing the I/O, instead of completing 5013 * the I/O just yet. 5014 * 5015 * If there is an error, though, we don't want to keep processing. 5016 * Instead, just send status back to the initiator. 5017 */ 5018 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5019 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5020 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5021 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5022 io->scsiio.io_cont(io); 5023 return; 5024 } 5025 /* 5026 * Since a configuration write can be done for commands that actually 5027 * have data allocated, like write buffer, and commands that have 5028 * no data, like start/stop unit, we need to check here. 5029 */ 5030 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5031 buf = io->scsiio.kern_data_ptr; 5032 else 5033 buf = NULL; 5034 ctl_done(io); 5035 if (buf) 5036 free(buf, M_CTL); 5037} 5038 5039void 5040ctl_config_read_done(union ctl_io *io) 5041{ 5042 uint8_t *buf; 5043 5044 /* 5045 * If there is some error -- we are done, skip data transfer. 5046 */ 5047 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5048 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5049 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5050 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5051 buf = io->scsiio.kern_data_ptr; 5052 else 5053 buf = NULL; 5054 ctl_done(io); 5055 if (buf) 5056 free(buf, M_CTL); 5057 return; 5058 } 5059 5060 /* 5061 * If the IO_CONT flag is set, we need to call the supplied 5062 * function to continue processing the I/O, instead of completing 5063 * the I/O just yet. 5064 */ 5065 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5066 io->scsiio.io_cont(io); 5067 return; 5068 } 5069 5070 ctl_datamove(io); 5071} 5072 5073/* 5074 * SCSI release command. 5075 */ 5076int 5077ctl_scsi_release(struct ctl_scsiio *ctsio) 5078{ 5079 int length, longid, thirdparty_id, resv_id; 5080 struct ctl_lun *lun; 5081 uint32_t residx; 5082 5083 length = 0; 5084 resv_id = 0; 5085 5086 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5087 5088 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5089 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5090 5091 switch (ctsio->cdb[0]) { 5092 case RELEASE_10: { 5093 struct scsi_release_10 *cdb; 5094 5095 cdb = (struct scsi_release_10 *)ctsio->cdb; 5096 5097 if (cdb->byte2 & SR10_LONGID) 5098 longid = 1; 5099 else 5100 thirdparty_id = cdb->thirdparty_id; 5101 5102 resv_id = cdb->resv_id; 5103 length = scsi_2btoul(cdb->length); 5104 break; 5105 } 5106 } 5107 5108 5109 /* 5110 * XXX KDM right now, we only support LUN reservation. We don't 5111 * support 3rd party reservations, or extent reservations, which 5112 * might actually need the parameter list. If we've gotten this 5113 * far, we've got a LUN reservation. Anything else got kicked out 5114 * above. So, according to SPC, ignore the length. 5115 */ 5116 length = 0; 5117 5118 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5119 && (length > 0)) { 5120 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5121 ctsio->kern_data_len = length; 5122 ctsio->kern_total_len = length; 5123 ctsio->kern_data_resid = 0; 5124 ctsio->kern_rel_offset = 0; 5125 ctsio->kern_sg_entries = 0; 5126 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5127 ctsio->be_move_done = ctl_config_move_done; 5128 ctl_datamove((union ctl_io *)ctsio); 5129 5130 return (CTL_RETVAL_COMPLETE); 5131 } 5132 5133 if (length > 0) 5134 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5135 5136 mtx_lock(&lun->lun_lock); 5137 5138 /* 5139 * According to SPC, it is not an error for an intiator to attempt 5140 * to release a reservation on a LUN that isn't reserved, or that 5141 * is reserved by another initiator. The reservation can only be 5142 * released, though, by the initiator who made it or by one of 5143 * several reset type events. 5144 */ 5145 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5146 lun->flags &= ~CTL_LUN_RESERVED; 5147 5148 mtx_unlock(&lun->lun_lock); 5149 5150 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5151 free(ctsio->kern_data_ptr, M_CTL); 5152 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5153 } 5154 5155 ctl_set_success(ctsio); 5156 ctl_done((union ctl_io *)ctsio); 5157 return (CTL_RETVAL_COMPLETE); 5158} 5159 5160int 5161ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5162{ 5163 int extent, thirdparty, longid; 5164 int resv_id, length; 5165 uint64_t thirdparty_id; 5166 struct ctl_lun *lun; 5167 uint32_t residx; 5168 5169 extent = 0; 5170 thirdparty = 0; 5171 longid = 0; 5172 resv_id = 0; 5173 length = 0; 5174 thirdparty_id = 0; 5175 5176 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5177 5178 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5179 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5180 5181 switch (ctsio->cdb[0]) { 5182 case RESERVE_10: { 5183 struct scsi_reserve_10 *cdb; 5184 5185 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5186 5187 if (cdb->byte2 & SR10_LONGID) 5188 longid = 1; 5189 else 5190 thirdparty_id = cdb->thirdparty_id; 5191 5192 resv_id = cdb->resv_id; 5193 length = scsi_2btoul(cdb->length); 5194 break; 5195 } 5196 } 5197 5198 /* 5199 * XXX KDM right now, we only support LUN reservation. We don't 5200 * support 3rd party reservations, or extent reservations, which 5201 * might actually need the parameter list. If we've gotten this 5202 * far, we've got a LUN reservation. Anything else got kicked out 5203 * above. So, according to SPC, ignore the length. 5204 */ 5205 length = 0; 5206 5207 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5208 && (length > 0)) { 5209 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5210 ctsio->kern_data_len = length; 5211 ctsio->kern_total_len = length; 5212 ctsio->kern_data_resid = 0; 5213 ctsio->kern_rel_offset = 0; 5214 ctsio->kern_sg_entries = 0; 5215 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5216 ctsio->be_move_done = ctl_config_move_done; 5217 ctl_datamove((union ctl_io *)ctsio); 5218 5219 return (CTL_RETVAL_COMPLETE); 5220 } 5221 5222 if (length > 0) 5223 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5224 5225 mtx_lock(&lun->lun_lock); 5226 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5227 ctl_set_reservation_conflict(ctsio); 5228 goto bailout; 5229 } 5230 5231 lun->flags |= CTL_LUN_RESERVED; 5232 lun->res_idx = residx; 5233 5234 ctl_set_success(ctsio); 5235 5236bailout: 5237 mtx_unlock(&lun->lun_lock); 5238 5239 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5240 free(ctsio->kern_data_ptr, M_CTL); 5241 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5242 } 5243 5244 ctl_done((union ctl_io *)ctsio); 5245 return (CTL_RETVAL_COMPLETE); 5246} 5247 5248int 5249ctl_start_stop(struct ctl_scsiio *ctsio) 5250{ 5251 struct scsi_start_stop_unit *cdb; 5252 struct ctl_lun *lun; 5253 int retval; 5254 5255 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5256 5257 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5258 retval = 0; 5259 5260 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5261 5262 /* 5263 * XXX KDM 5264 * We don't support the immediate bit on a stop unit. In order to 5265 * do that, we would need to code up a way to know that a stop is 5266 * pending, and hold off any new commands until it completes, one 5267 * way or another. Then we could accept or reject those commands 5268 * depending on its status. We would almost need to do the reverse 5269 * of what we do below for an immediate start -- return the copy of 5270 * the ctl_io to the FETD with status to send to the host (and to 5271 * free the copy!) and then free the original I/O once the stop 5272 * actually completes. That way, the OOA queue mechanism can work 5273 * to block commands that shouldn't proceed. Another alternative 5274 * would be to put the copy in the queue in place of the original, 5275 * and return the original back to the caller. That could be 5276 * slightly safer.. 5277 */ 5278 if ((cdb->byte2 & SSS_IMMED) 5279 && ((cdb->how & SSS_START) == 0)) { 5280 ctl_set_invalid_field(ctsio, 5281 /*sks_valid*/ 1, 5282 /*command*/ 1, 5283 /*field*/ 1, 5284 /*bit_valid*/ 1, 5285 /*bit*/ 0); 5286 ctl_done((union ctl_io *)ctsio); 5287 return (CTL_RETVAL_COMPLETE); 5288 } 5289 5290 if ((lun->flags & CTL_LUN_PR_RESERVED) 5291 && ((cdb->how & SSS_START)==0)) { 5292 uint32_t residx; 5293 5294 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5295 if (ctl_get_prkey(lun, residx) == 0 5296 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5297 5298 ctl_set_reservation_conflict(ctsio); 5299 ctl_done((union ctl_io *)ctsio); 5300 return (CTL_RETVAL_COMPLETE); 5301 } 5302 } 5303 5304 /* 5305 * If there is no backend on this device, we can't start or stop 5306 * it. In theory we shouldn't get any start/stop commands in the 5307 * first place at this level if the LUN doesn't have a backend. 5308 * That should get stopped by the command decode code. 5309 */ 5310 if (lun->backend == NULL) { 5311 ctl_set_invalid_opcode(ctsio); 5312 ctl_done((union ctl_io *)ctsio); 5313 return (CTL_RETVAL_COMPLETE); 5314 } 5315 5316 /* 5317 * XXX KDM Copan-specific offline behavior. 5318 * Figure out a reasonable way to port this? 5319 */ 5320#ifdef NEEDTOPORT 5321 mtx_lock(&lun->lun_lock); 5322 5323 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5324 && (lun->flags & CTL_LUN_OFFLINE)) { 5325 /* 5326 * If the LUN is offline, and the on/offline bit isn't set, 5327 * reject the start or stop. Otherwise, let it through. 5328 */ 5329 mtx_unlock(&lun->lun_lock); 5330 ctl_set_lun_not_ready(ctsio); 5331 ctl_done((union ctl_io *)ctsio); 5332 } else { 5333 mtx_unlock(&lun->lun_lock); 5334#endif /* NEEDTOPORT */ 5335 /* 5336 * This could be a start or a stop when we're online, 5337 * or a stop/offline or start/online. A start or stop when 5338 * we're offline is covered in the case above. 5339 */ 5340 /* 5341 * In the non-immediate case, we send the request to 5342 * the backend and return status to the user when 5343 * it is done. 5344 * 5345 * In the immediate case, we allocate a new ctl_io 5346 * to hold a copy of the request, and send that to 5347 * the backend. We then set good status on the 5348 * user's request and return it immediately. 5349 */ 5350 if (cdb->byte2 & SSS_IMMED) { 5351 union ctl_io *new_io; 5352 5353 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5354 ctl_copy_io((union ctl_io *)ctsio, new_io); 5355 retval = lun->backend->config_write(new_io); 5356 ctl_set_success(ctsio); 5357 ctl_done((union ctl_io *)ctsio); 5358 } else { 5359 retval = lun->backend->config_write( 5360 (union ctl_io *)ctsio); 5361 } 5362#ifdef NEEDTOPORT 5363 } 5364#endif 5365 return (retval); 5366} 5367 5368/* 5369 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5370 * we don't really do anything with the LBA and length fields if the user 5371 * passes them in. Instead we'll just flush out the cache for the entire 5372 * LUN. 5373 */ 5374int 5375ctl_sync_cache(struct ctl_scsiio *ctsio) 5376{ 5377 struct ctl_lun *lun; 5378 struct ctl_softc *softc; 5379 struct ctl_lba_len_flags *lbalen; 5380 uint64_t starting_lba; 5381 uint32_t block_count; 5382 int retval; 5383 uint8_t byte2; 5384 5385 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5386 5387 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5388 softc = lun->ctl_softc; 5389 retval = 0; 5390 5391 switch (ctsio->cdb[0]) { 5392 case SYNCHRONIZE_CACHE: { 5393 struct scsi_sync_cache *cdb; 5394 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5395 5396 starting_lba = scsi_4btoul(cdb->begin_lba); 5397 block_count = scsi_2btoul(cdb->lb_count); 5398 byte2 = cdb->byte2; 5399 break; 5400 } 5401 case SYNCHRONIZE_CACHE_16: { 5402 struct scsi_sync_cache_16 *cdb; 5403 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5404 5405 starting_lba = scsi_8btou64(cdb->begin_lba); 5406 block_count = scsi_4btoul(cdb->lb_count); 5407 byte2 = cdb->byte2; 5408 break; 5409 } 5410 default: 5411 ctl_set_invalid_opcode(ctsio); 5412 ctl_done((union ctl_io *)ctsio); 5413 goto bailout; 5414 break; /* NOTREACHED */ 5415 } 5416 5417 /* 5418 * We check the LBA and length, but don't do anything with them. 5419 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5420 * get flushed. This check will just help satisfy anyone who wants 5421 * to see an error for an out of range LBA. 5422 */ 5423 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5424 ctl_set_lba_out_of_range(ctsio); 5425 ctl_done((union ctl_io *)ctsio); 5426 goto bailout; 5427 } 5428 5429 /* 5430 * If this LUN has no backend, we can't flush the cache anyway. 5431 */ 5432 if (lun->backend == NULL) { 5433 ctl_set_invalid_opcode(ctsio); 5434 ctl_done((union ctl_io *)ctsio); 5435 goto bailout; 5436 } 5437 5438 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5439 lbalen->lba = starting_lba; 5440 lbalen->len = block_count; 5441 lbalen->flags = byte2; 5442 5443 /* 5444 * Check to see whether we're configured to send the SYNCHRONIZE 5445 * CACHE command directly to the back end. 5446 */ 5447 mtx_lock(&lun->lun_lock); 5448 if ((softc->flags & CTL_FLAG_REAL_SYNC) 5449 && (++(lun->sync_count) >= lun->sync_interval)) { 5450 lun->sync_count = 0; 5451 mtx_unlock(&lun->lun_lock); 5452 retval = lun->backend->config_write((union ctl_io *)ctsio); 5453 } else { 5454 mtx_unlock(&lun->lun_lock); 5455 ctl_set_success(ctsio); 5456 ctl_done((union ctl_io *)ctsio); 5457 } 5458 5459bailout: 5460 5461 return (retval); 5462} 5463 5464int 5465ctl_format(struct ctl_scsiio *ctsio) 5466{ 5467 struct scsi_format *cdb; 5468 struct ctl_lun *lun; 5469 int length, defect_list_len; 5470 5471 CTL_DEBUG_PRINT(("ctl_format\n")); 5472 5473 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5474 5475 cdb = (struct scsi_format *)ctsio->cdb; 5476 5477 length = 0; 5478 if (cdb->byte2 & SF_FMTDATA) { 5479 if (cdb->byte2 & SF_LONGLIST) 5480 length = sizeof(struct scsi_format_header_long); 5481 else 5482 length = sizeof(struct scsi_format_header_short); 5483 } 5484 5485 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5486 && (length > 0)) { 5487 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5488 ctsio->kern_data_len = length; 5489 ctsio->kern_total_len = length; 5490 ctsio->kern_data_resid = 0; 5491 ctsio->kern_rel_offset = 0; 5492 ctsio->kern_sg_entries = 0; 5493 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5494 ctsio->be_move_done = ctl_config_move_done; 5495 ctl_datamove((union ctl_io *)ctsio); 5496 5497 return (CTL_RETVAL_COMPLETE); 5498 } 5499 5500 defect_list_len = 0; 5501 5502 if (cdb->byte2 & SF_FMTDATA) { 5503 if (cdb->byte2 & SF_LONGLIST) { 5504 struct scsi_format_header_long *header; 5505 5506 header = (struct scsi_format_header_long *) 5507 ctsio->kern_data_ptr; 5508 5509 defect_list_len = scsi_4btoul(header->defect_list_len); 5510 if (defect_list_len != 0) { 5511 ctl_set_invalid_field(ctsio, 5512 /*sks_valid*/ 1, 5513 /*command*/ 0, 5514 /*field*/ 2, 5515 /*bit_valid*/ 0, 5516 /*bit*/ 0); 5517 goto bailout; 5518 } 5519 } else { 5520 struct scsi_format_header_short *header; 5521 5522 header = (struct scsi_format_header_short *) 5523 ctsio->kern_data_ptr; 5524 5525 defect_list_len = scsi_2btoul(header->defect_list_len); 5526 if (defect_list_len != 0) { 5527 ctl_set_invalid_field(ctsio, 5528 /*sks_valid*/ 1, 5529 /*command*/ 0, 5530 /*field*/ 2, 5531 /*bit_valid*/ 0, 5532 /*bit*/ 0); 5533 goto bailout; 5534 } 5535 } 5536 } 5537 5538 /* 5539 * The format command will clear out the "Medium format corrupted" 5540 * status if set by the configuration code. That status is really 5541 * just a way to notify the host that we have lost the media, and 5542 * get them to issue a command that will basically make them think 5543 * they're blowing away the media. 5544 */ 5545 mtx_lock(&lun->lun_lock); 5546 lun->flags &= ~CTL_LUN_INOPERABLE; 5547 mtx_unlock(&lun->lun_lock); 5548 5549 ctl_set_success(ctsio); 5550bailout: 5551 5552 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5553 free(ctsio->kern_data_ptr, M_CTL); 5554 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5555 } 5556 5557 ctl_done((union ctl_io *)ctsio); 5558 return (CTL_RETVAL_COMPLETE); 5559} 5560 5561int 5562ctl_read_buffer(struct ctl_scsiio *ctsio) 5563{ 5564 struct scsi_read_buffer *cdb; 5565 struct ctl_lun *lun; 5566 int buffer_offset, len; 5567 static uint8_t descr[4]; 5568 static uint8_t echo_descr[4] = { 0 }; 5569 5570 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5571 5572 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5573 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5574 5575 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5576 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5577 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5578 ctl_set_invalid_field(ctsio, 5579 /*sks_valid*/ 1, 5580 /*command*/ 1, 5581 /*field*/ 1, 5582 /*bit_valid*/ 1, 5583 /*bit*/ 4); 5584 ctl_done((union ctl_io *)ctsio); 5585 return (CTL_RETVAL_COMPLETE); 5586 } 5587 5588 len = scsi_3btoul(cdb->length); 5589 buffer_offset = scsi_3btoul(cdb->offset); 5590 5591 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5592 ctl_set_invalid_field(ctsio, 5593 /*sks_valid*/ 1, 5594 /*command*/ 1, 5595 /*field*/ 6, 5596 /*bit_valid*/ 0, 5597 /*bit*/ 0); 5598 ctl_done((union ctl_io *)ctsio); 5599 return (CTL_RETVAL_COMPLETE); 5600 } 5601 5602 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5603 descr[0] = 0; 5604 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5605 ctsio->kern_data_ptr = descr; 5606 len = min(len, sizeof(descr)); 5607 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5608 ctsio->kern_data_ptr = echo_descr; 5609 len = min(len, sizeof(echo_descr)); 5610 } else { 5611 if (lun->write_buffer == NULL) { 5612 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5613 M_CTL, M_WAITOK); 5614 } 5615 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5616 } 5617 ctsio->kern_data_len = len; 5618 ctsio->kern_total_len = len; 5619 ctsio->kern_data_resid = 0; 5620 ctsio->kern_rel_offset = 0; 5621 ctsio->kern_sg_entries = 0; 5622 ctl_set_success(ctsio); 5623 ctsio->be_move_done = ctl_config_move_done; 5624 ctl_datamove((union ctl_io *)ctsio); 5625 return (CTL_RETVAL_COMPLETE); 5626} 5627 5628int 5629ctl_write_buffer(struct ctl_scsiio *ctsio) 5630{ 5631 struct scsi_write_buffer *cdb; 5632 struct ctl_lun *lun; 5633 int buffer_offset, len; 5634 5635 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5636 5637 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5638 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5639 5640 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5641 ctl_set_invalid_field(ctsio, 5642 /*sks_valid*/ 1, 5643 /*command*/ 1, 5644 /*field*/ 1, 5645 /*bit_valid*/ 1, 5646 /*bit*/ 4); 5647 ctl_done((union ctl_io *)ctsio); 5648 return (CTL_RETVAL_COMPLETE); 5649 } 5650 5651 len = scsi_3btoul(cdb->length); 5652 buffer_offset = scsi_3btoul(cdb->offset); 5653 5654 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5655 ctl_set_invalid_field(ctsio, 5656 /*sks_valid*/ 1, 5657 /*command*/ 1, 5658 /*field*/ 6, 5659 /*bit_valid*/ 0, 5660 /*bit*/ 0); 5661 ctl_done((union ctl_io *)ctsio); 5662 return (CTL_RETVAL_COMPLETE); 5663 } 5664 5665 /* 5666 * If we've got a kernel request that hasn't been malloced yet, 5667 * malloc it and tell the caller the data buffer is here. 5668 */ 5669 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5670 if (lun->write_buffer == NULL) { 5671 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5672 M_CTL, M_WAITOK); 5673 } 5674 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5675 ctsio->kern_data_len = len; 5676 ctsio->kern_total_len = len; 5677 ctsio->kern_data_resid = 0; 5678 ctsio->kern_rel_offset = 0; 5679 ctsio->kern_sg_entries = 0; 5680 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5681 ctsio->be_move_done = ctl_config_move_done; 5682 ctl_datamove((union ctl_io *)ctsio); 5683 5684 return (CTL_RETVAL_COMPLETE); 5685 } 5686 5687 ctl_set_success(ctsio); 5688 ctl_done((union ctl_io *)ctsio); 5689 return (CTL_RETVAL_COMPLETE); 5690} 5691 5692int 5693ctl_write_same(struct ctl_scsiio *ctsio) 5694{ 5695 struct ctl_lun *lun; 5696 struct ctl_lba_len_flags *lbalen; 5697 uint64_t lba; 5698 uint32_t num_blocks; 5699 int len, retval; 5700 uint8_t byte2; 5701 5702 retval = CTL_RETVAL_COMPLETE; 5703 5704 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5705 5706 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5707 5708 switch (ctsio->cdb[0]) { 5709 case WRITE_SAME_10: { 5710 struct scsi_write_same_10 *cdb; 5711 5712 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5713 5714 lba = scsi_4btoul(cdb->addr); 5715 num_blocks = scsi_2btoul(cdb->length); 5716 byte2 = cdb->byte2; 5717 break; 5718 } 5719 case WRITE_SAME_16: { 5720 struct scsi_write_same_16 *cdb; 5721 5722 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5723 5724 lba = scsi_8btou64(cdb->addr); 5725 num_blocks = scsi_4btoul(cdb->length); 5726 byte2 = cdb->byte2; 5727 break; 5728 } 5729 default: 5730 /* 5731 * We got a command we don't support. This shouldn't 5732 * happen, commands should be filtered out above us. 5733 */ 5734 ctl_set_invalid_opcode(ctsio); 5735 ctl_done((union ctl_io *)ctsio); 5736 5737 return (CTL_RETVAL_COMPLETE); 5738 break; /* NOTREACHED */ 5739 } 5740 5741 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5742 if ((byte2 & SWS_UNMAP) == 0 && 5743 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5744 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5745 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5746 ctl_done((union ctl_io *)ctsio); 5747 return (CTL_RETVAL_COMPLETE); 5748 } 5749 5750 /* 5751 * The first check is to make sure we're in bounds, the second 5752 * check is to catch wrap-around problems. If the lba + num blocks 5753 * is less than the lba, then we've wrapped around and the block 5754 * range is invalid anyway. 5755 */ 5756 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5757 || ((lba + num_blocks) < lba)) { 5758 ctl_set_lba_out_of_range(ctsio); 5759 ctl_done((union ctl_io *)ctsio); 5760 return (CTL_RETVAL_COMPLETE); 5761 } 5762 5763 /* Zero number of blocks means "to the last logical block" */ 5764 if (num_blocks == 0) { 5765 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5766 ctl_set_invalid_field(ctsio, 5767 /*sks_valid*/ 0, 5768 /*command*/ 1, 5769 /*field*/ 0, 5770 /*bit_valid*/ 0, 5771 /*bit*/ 0); 5772 ctl_done((union ctl_io *)ctsio); 5773 return (CTL_RETVAL_COMPLETE); 5774 } 5775 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5776 } 5777 5778 len = lun->be_lun->blocksize; 5779 5780 /* 5781 * If we've got a kernel request that hasn't been malloced yet, 5782 * malloc it and tell the caller the data buffer is here. 5783 */ 5784 if ((byte2 & SWS_NDOB) == 0 && 5785 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5786 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5787 ctsio->kern_data_len = len; 5788 ctsio->kern_total_len = len; 5789 ctsio->kern_data_resid = 0; 5790 ctsio->kern_rel_offset = 0; 5791 ctsio->kern_sg_entries = 0; 5792 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5793 ctsio->be_move_done = ctl_config_move_done; 5794 ctl_datamove((union ctl_io *)ctsio); 5795 5796 return (CTL_RETVAL_COMPLETE); 5797 } 5798 5799 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5800 lbalen->lba = lba; 5801 lbalen->len = num_blocks; 5802 lbalen->flags = byte2; 5803 retval = lun->backend->config_write((union ctl_io *)ctsio); 5804 5805 return (retval); 5806} 5807 5808int 5809ctl_unmap(struct ctl_scsiio *ctsio) 5810{ 5811 struct ctl_lun *lun; 5812 struct scsi_unmap *cdb; 5813 struct ctl_ptr_len_flags *ptrlen; 5814 struct scsi_unmap_header *hdr; 5815 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5816 uint64_t lba; 5817 uint32_t num_blocks; 5818 int len, retval; 5819 uint8_t byte2; 5820 5821 retval = CTL_RETVAL_COMPLETE; 5822 5823 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5824 5825 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5826 cdb = (struct scsi_unmap *)ctsio->cdb; 5827 5828 len = scsi_2btoul(cdb->length); 5829 byte2 = cdb->byte2; 5830 5831 /* 5832 * If we've got a kernel request that hasn't been malloced yet, 5833 * malloc it and tell the caller the data buffer is here. 5834 */ 5835 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5836 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5837 ctsio->kern_data_len = len; 5838 ctsio->kern_total_len = len; 5839 ctsio->kern_data_resid = 0; 5840 ctsio->kern_rel_offset = 0; 5841 ctsio->kern_sg_entries = 0; 5842 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5843 ctsio->be_move_done = ctl_config_move_done; 5844 ctl_datamove((union ctl_io *)ctsio); 5845 5846 return (CTL_RETVAL_COMPLETE); 5847 } 5848 5849 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5850 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5851 if (len < sizeof (*hdr) || 5852 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5853 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5854 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5855 ctl_set_invalid_field(ctsio, 5856 /*sks_valid*/ 0, 5857 /*command*/ 0, 5858 /*field*/ 0, 5859 /*bit_valid*/ 0, 5860 /*bit*/ 0); 5861 goto done; 5862 } 5863 len = scsi_2btoul(hdr->desc_length); 5864 buf = (struct scsi_unmap_desc *)(hdr + 1); 5865 end = buf + len / sizeof(*buf); 5866 5867 endnz = buf; 5868 for (range = buf; range < end; range++) { 5869 lba = scsi_8btou64(range->lba); 5870 num_blocks = scsi_4btoul(range->length); 5871 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5872 || ((lba + num_blocks) < lba)) { 5873 ctl_set_lba_out_of_range(ctsio); 5874 ctl_done((union ctl_io *)ctsio); 5875 return (CTL_RETVAL_COMPLETE); 5876 } 5877 if (num_blocks != 0) 5878 endnz = range + 1; 5879 } 5880 5881 /* 5882 * Block backend can not handle zero last range. 5883 * Filter it out and return if there is nothing left. 5884 */ 5885 len = (uint8_t *)endnz - (uint8_t *)buf; 5886 if (len == 0) { 5887 ctl_set_success(ctsio); 5888 goto done; 5889 } 5890 5891 mtx_lock(&lun->lun_lock); 5892 ptrlen = (struct ctl_ptr_len_flags *) 5893 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5894 ptrlen->ptr = (void *)buf; 5895 ptrlen->len = len; 5896 ptrlen->flags = byte2; 5897 ctl_check_blocked(lun); 5898 mtx_unlock(&lun->lun_lock); 5899 5900 retval = lun->backend->config_write((union ctl_io *)ctsio); 5901 return (retval); 5902 5903done: 5904 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5905 free(ctsio->kern_data_ptr, M_CTL); 5906 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5907 } 5908 ctl_done((union ctl_io *)ctsio); 5909 return (CTL_RETVAL_COMPLETE); 5910} 5911 5912/* 5913 * Note that this function currently doesn't actually do anything inside 5914 * CTL to enforce things if the DQue bit is turned on. 5915 * 5916 * Also note that this function can't be used in the default case, because 5917 * the DQue bit isn't set in the changeable mask for the control mode page 5918 * anyway. This is just here as an example for how to implement a page 5919 * handler, and a placeholder in case we want to allow the user to turn 5920 * tagged queueing on and off. 5921 * 5922 * The D_SENSE bit handling is functional, however, and will turn 5923 * descriptor sense on and off for a given LUN. 5924 */ 5925int 5926ctl_control_page_handler(struct ctl_scsiio *ctsio, 5927 struct ctl_page_index *page_index, uint8_t *page_ptr) 5928{ 5929 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5930 struct ctl_lun *lun; 5931 int set_ua; 5932 uint32_t initidx; 5933 5934 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5935 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5936 set_ua = 0; 5937 5938 user_cp = (struct scsi_control_page *)page_ptr; 5939 current_cp = (struct scsi_control_page *) 5940 (page_index->page_data + (page_index->page_len * 5941 CTL_PAGE_CURRENT)); 5942 saved_cp = (struct scsi_control_page *) 5943 (page_index->page_data + (page_index->page_len * 5944 CTL_PAGE_SAVED)); 5945 5946 mtx_lock(&lun->lun_lock); 5947 if (((current_cp->rlec & SCP_DSENSE) == 0) 5948 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5949 /* 5950 * Descriptor sense is currently turned off and the user 5951 * wants to turn it on. 5952 */ 5953 current_cp->rlec |= SCP_DSENSE; 5954 saved_cp->rlec |= SCP_DSENSE; 5955 lun->flags |= CTL_LUN_SENSE_DESC; 5956 set_ua = 1; 5957 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 5958 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 5959 /* 5960 * Descriptor sense is currently turned on, and the user 5961 * wants to turn it off. 5962 */ 5963 current_cp->rlec &= ~SCP_DSENSE; 5964 saved_cp->rlec &= ~SCP_DSENSE; 5965 lun->flags &= ~CTL_LUN_SENSE_DESC; 5966 set_ua = 1; 5967 } 5968 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 5969 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 5970 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5971 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5972 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5973 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5974 set_ua = 1; 5975 } 5976 if ((current_cp->eca_and_aen & SCP_SWP) != 5977 (user_cp->eca_and_aen & SCP_SWP)) { 5978 current_cp->eca_and_aen &= ~SCP_SWP; 5979 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5980 saved_cp->eca_and_aen &= ~SCP_SWP; 5981 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5982 set_ua = 1; 5983 } 5984 if (set_ua != 0) 5985 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5986 mtx_unlock(&lun->lun_lock); 5987 5988 return (0); 5989} 5990 5991int 5992ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 5993 struct ctl_page_index *page_index, uint8_t *page_ptr) 5994{ 5995 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 5996 struct ctl_lun *lun; 5997 int set_ua; 5998 uint32_t initidx; 5999 6000 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6001 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6002 set_ua = 0; 6003 6004 user_cp = (struct scsi_caching_page *)page_ptr; 6005 current_cp = (struct scsi_caching_page *) 6006 (page_index->page_data + (page_index->page_len * 6007 CTL_PAGE_CURRENT)); 6008 saved_cp = (struct scsi_caching_page *) 6009 (page_index->page_data + (page_index->page_len * 6010 CTL_PAGE_SAVED)); 6011 6012 mtx_lock(&lun->lun_lock); 6013 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6014 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6015 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6016 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6017 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6018 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6019 set_ua = 1; 6020 } 6021 if (set_ua != 0) 6022 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6023 mtx_unlock(&lun->lun_lock); 6024 6025 return (0); 6026} 6027 6028int 6029ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6030 struct ctl_page_index *page_index, 6031 uint8_t *page_ptr) 6032{ 6033 uint8_t *c; 6034 int i; 6035 6036 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6037 ctl_time_io_secs = 6038 (c[0] << 8) | 6039 (c[1] << 0) | 6040 0; 6041 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6042 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6043 printf("page data:"); 6044 for (i=0; i<8; i++) 6045 printf(" %.2x",page_ptr[i]); 6046 printf("\n"); 6047 return (0); 6048} 6049 6050int 6051ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6052 struct ctl_page_index *page_index, 6053 int pc) 6054{ 6055 struct copan_debugconf_subpage *page; 6056 6057 page = (struct copan_debugconf_subpage *)page_index->page_data + 6058 (page_index->page_len * pc); 6059 6060 switch (pc) { 6061 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6062 case SMS_PAGE_CTRL_DEFAULT >> 6: 6063 case SMS_PAGE_CTRL_SAVED >> 6: 6064 /* 6065 * We don't update the changable or default bits for this page. 6066 */ 6067 break; 6068 case SMS_PAGE_CTRL_CURRENT >> 6: 6069 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6070 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6071 break; 6072 default: 6073#ifdef NEEDTOPORT 6074 EPRINT(0, "Invalid PC %d!!", pc); 6075#endif /* NEEDTOPORT */ 6076 break; 6077 } 6078 return (0); 6079} 6080 6081 6082static int 6083ctl_do_mode_select(union ctl_io *io) 6084{ 6085 struct scsi_mode_page_header *page_header; 6086 struct ctl_page_index *page_index; 6087 struct ctl_scsiio *ctsio; 6088 int control_dev, page_len; 6089 int page_len_offset, page_len_size; 6090 union ctl_modepage_info *modepage_info; 6091 struct ctl_lun *lun; 6092 int *len_left, *len_used; 6093 int retval, i; 6094 6095 ctsio = &io->scsiio; 6096 page_index = NULL; 6097 page_len = 0; 6098 retval = CTL_RETVAL_COMPLETE; 6099 6100 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6101 6102 if (lun->be_lun->lun_type != T_DIRECT) 6103 control_dev = 1; 6104 else 6105 control_dev = 0; 6106 6107 modepage_info = (union ctl_modepage_info *) 6108 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6109 len_left = &modepage_info->header.len_left; 6110 len_used = &modepage_info->header.len_used; 6111 6112do_next_page: 6113 6114 page_header = (struct scsi_mode_page_header *) 6115 (ctsio->kern_data_ptr + *len_used); 6116 6117 if (*len_left == 0) { 6118 free(ctsio->kern_data_ptr, M_CTL); 6119 ctl_set_success(ctsio); 6120 ctl_done((union ctl_io *)ctsio); 6121 return (CTL_RETVAL_COMPLETE); 6122 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6123 6124 free(ctsio->kern_data_ptr, M_CTL); 6125 ctl_set_param_len_error(ctsio); 6126 ctl_done((union ctl_io *)ctsio); 6127 return (CTL_RETVAL_COMPLETE); 6128 6129 } else if ((page_header->page_code & SMPH_SPF) 6130 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6131 6132 free(ctsio->kern_data_ptr, M_CTL); 6133 ctl_set_param_len_error(ctsio); 6134 ctl_done((union ctl_io *)ctsio); 6135 return (CTL_RETVAL_COMPLETE); 6136 } 6137 6138 6139 /* 6140 * XXX KDM should we do something with the block descriptor? 6141 */ 6142 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6143 6144 if ((control_dev != 0) 6145 && (lun->mode_pages.index[i].page_flags & 6146 CTL_PAGE_FLAG_DISK_ONLY)) 6147 continue; 6148 6149 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6150 (page_header->page_code & SMPH_PC_MASK)) 6151 continue; 6152 6153 /* 6154 * If neither page has a subpage code, then we've got a 6155 * match. 6156 */ 6157 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6158 && ((page_header->page_code & SMPH_SPF) == 0)) { 6159 page_index = &lun->mode_pages.index[i]; 6160 page_len = page_header->page_length; 6161 break; 6162 } 6163 6164 /* 6165 * If both pages have subpages, then the subpage numbers 6166 * have to match. 6167 */ 6168 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6169 && (page_header->page_code & SMPH_SPF)) { 6170 struct scsi_mode_page_header_sp *sph; 6171 6172 sph = (struct scsi_mode_page_header_sp *)page_header; 6173 6174 if (lun->mode_pages.index[i].subpage == 6175 sph->subpage) { 6176 page_index = &lun->mode_pages.index[i]; 6177 page_len = scsi_2btoul(sph->page_length); 6178 break; 6179 } 6180 } 6181 } 6182 6183 /* 6184 * If we couldn't find the page, or if we don't have a mode select 6185 * handler for it, send back an error to the user. 6186 */ 6187 if ((page_index == NULL) 6188 || (page_index->select_handler == NULL)) { 6189 ctl_set_invalid_field(ctsio, 6190 /*sks_valid*/ 1, 6191 /*command*/ 0, 6192 /*field*/ *len_used, 6193 /*bit_valid*/ 0, 6194 /*bit*/ 0); 6195 free(ctsio->kern_data_ptr, M_CTL); 6196 ctl_done((union ctl_io *)ctsio); 6197 return (CTL_RETVAL_COMPLETE); 6198 } 6199 6200 if (page_index->page_code & SMPH_SPF) { 6201 page_len_offset = 2; 6202 page_len_size = 2; 6203 } else { 6204 page_len_size = 1; 6205 page_len_offset = 1; 6206 } 6207 6208 /* 6209 * If the length the initiator gives us isn't the one we specify in 6210 * the mode page header, or if they didn't specify enough data in 6211 * the CDB to avoid truncating this page, kick out the request. 6212 */ 6213 if ((page_len != (page_index->page_len - page_len_offset - 6214 page_len_size)) 6215 || (*len_left < page_index->page_len)) { 6216 6217 6218 ctl_set_invalid_field(ctsio, 6219 /*sks_valid*/ 1, 6220 /*command*/ 0, 6221 /*field*/ *len_used + page_len_offset, 6222 /*bit_valid*/ 0, 6223 /*bit*/ 0); 6224 free(ctsio->kern_data_ptr, M_CTL); 6225 ctl_done((union ctl_io *)ctsio); 6226 return (CTL_RETVAL_COMPLETE); 6227 } 6228 6229 /* 6230 * Run through the mode page, checking to make sure that the bits 6231 * the user changed are actually legal for him to change. 6232 */ 6233 for (i = 0; i < page_index->page_len; i++) { 6234 uint8_t *user_byte, *change_mask, *current_byte; 6235 int bad_bit; 6236 int j; 6237 6238 user_byte = (uint8_t *)page_header + i; 6239 change_mask = page_index->page_data + 6240 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6241 current_byte = page_index->page_data + 6242 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6243 6244 /* 6245 * Check to see whether the user set any bits in this byte 6246 * that he is not allowed to set. 6247 */ 6248 if ((*user_byte & ~(*change_mask)) == 6249 (*current_byte & ~(*change_mask))) 6250 continue; 6251 6252 /* 6253 * Go through bit by bit to determine which one is illegal. 6254 */ 6255 bad_bit = 0; 6256 for (j = 7; j >= 0; j--) { 6257 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6258 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6259 bad_bit = i; 6260 break; 6261 } 6262 } 6263 ctl_set_invalid_field(ctsio, 6264 /*sks_valid*/ 1, 6265 /*command*/ 0, 6266 /*field*/ *len_used + i, 6267 /*bit_valid*/ 1, 6268 /*bit*/ bad_bit); 6269 free(ctsio->kern_data_ptr, M_CTL); 6270 ctl_done((union ctl_io *)ctsio); 6271 return (CTL_RETVAL_COMPLETE); 6272 } 6273 6274 /* 6275 * Decrement these before we call the page handler, since we may 6276 * end up getting called back one way or another before the handler 6277 * returns to this context. 6278 */ 6279 *len_left -= page_index->page_len; 6280 *len_used += page_index->page_len; 6281 6282 retval = page_index->select_handler(ctsio, page_index, 6283 (uint8_t *)page_header); 6284 6285 /* 6286 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6287 * wait until this queued command completes to finish processing 6288 * the mode page. If it returns anything other than 6289 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6290 * already set the sense information, freed the data pointer, and 6291 * completed the io for us. 6292 */ 6293 if (retval != CTL_RETVAL_COMPLETE) 6294 goto bailout_no_done; 6295 6296 /* 6297 * If the initiator sent us more than one page, parse the next one. 6298 */ 6299 if (*len_left > 0) 6300 goto do_next_page; 6301 6302 ctl_set_success(ctsio); 6303 free(ctsio->kern_data_ptr, M_CTL); 6304 ctl_done((union ctl_io *)ctsio); 6305 6306bailout_no_done: 6307 6308 return (CTL_RETVAL_COMPLETE); 6309 6310} 6311 6312int 6313ctl_mode_select(struct ctl_scsiio *ctsio) 6314{ 6315 int param_len, pf, sp; 6316 int header_size, bd_len; 6317 int len_left, len_used; 6318 struct ctl_page_index *page_index; 6319 struct ctl_lun *lun; 6320 int control_dev, page_len; 6321 union ctl_modepage_info *modepage_info; 6322 int retval; 6323 6324 pf = 0; 6325 sp = 0; 6326 page_len = 0; 6327 len_used = 0; 6328 len_left = 0; 6329 retval = 0; 6330 bd_len = 0; 6331 page_index = NULL; 6332 6333 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6334 6335 if (lun->be_lun->lun_type != T_DIRECT) 6336 control_dev = 1; 6337 else 6338 control_dev = 0; 6339 6340 switch (ctsio->cdb[0]) { 6341 case MODE_SELECT_6: { 6342 struct scsi_mode_select_6 *cdb; 6343 6344 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6345 6346 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6347 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6348 6349 param_len = cdb->length; 6350 header_size = sizeof(struct scsi_mode_header_6); 6351 break; 6352 } 6353 case MODE_SELECT_10: { 6354 struct scsi_mode_select_10 *cdb; 6355 6356 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6357 6358 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6359 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6360 6361 param_len = scsi_2btoul(cdb->length); 6362 header_size = sizeof(struct scsi_mode_header_10); 6363 break; 6364 } 6365 default: 6366 ctl_set_invalid_opcode(ctsio); 6367 ctl_done((union ctl_io *)ctsio); 6368 return (CTL_RETVAL_COMPLETE); 6369 break; /* NOTREACHED */ 6370 } 6371 6372 /* 6373 * From SPC-3: 6374 * "A parameter list length of zero indicates that the Data-Out Buffer 6375 * shall be empty. This condition shall not be considered as an error." 6376 */ 6377 if (param_len == 0) { 6378 ctl_set_success(ctsio); 6379 ctl_done((union ctl_io *)ctsio); 6380 return (CTL_RETVAL_COMPLETE); 6381 } 6382 6383 /* 6384 * Since we'll hit this the first time through, prior to 6385 * allocation, we don't need to free a data buffer here. 6386 */ 6387 if (param_len < header_size) { 6388 ctl_set_param_len_error(ctsio); 6389 ctl_done((union ctl_io *)ctsio); 6390 return (CTL_RETVAL_COMPLETE); 6391 } 6392 6393 /* 6394 * Allocate the data buffer and grab the user's data. In theory, 6395 * we shouldn't have to sanity check the parameter list length here 6396 * because the maximum size is 64K. We should be able to malloc 6397 * that much without too many problems. 6398 */ 6399 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6400 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6401 ctsio->kern_data_len = param_len; 6402 ctsio->kern_total_len = param_len; 6403 ctsio->kern_data_resid = 0; 6404 ctsio->kern_rel_offset = 0; 6405 ctsio->kern_sg_entries = 0; 6406 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6407 ctsio->be_move_done = ctl_config_move_done; 6408 ctl_datamove((union ctl_io *)ctsio); 6409 6410 return (CTL_RETVAL_COMPLETE); 6411 } 6412 6413 switch (ctsio->cdb[0]) { 6414 case MODE_SELECT_6: { 6415 struct scsi_mode_header_6 *mh6; 6416 6417 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6418 bd_len = mh6->blk_desc_len; 6419 break; 6420 } 6421 case MODE_SELECT_10: { 6422 struct scsi_mode_header_10 *mh10; 6423 6424 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6425 bd_len = scsi_2btoul(mh10->blk_desc_len); 6426 break; 6427 } 6428 default: 6429 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6430 break; 6431 } 6432 6433 if (param_len < (header_size + bd_len)) { 6434 free(ctsio->kern_data_ptr, M_CTL); 6435 ctl_set_param_len_error(ctsio); 6436 ctl_done((union ctl_io *)ctsio); 6437 return (CTL_RETVAL_COMPLETE); 6438 } 6439 6440 /* 6441 * Set the IO_CONT flag, so that if this I/O gets passed to 6442 * ctl_config_write_done(), it'll get passed back to 6443 * ctl_do_mode_select() for further processing, or completion if 6444 * we're all done. 6445 */ 6446 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6447 ctsio->io_cont = ctl_do_mode_select; 6448 6449 modepage_info = (union ctl_modepage_info *) 6450 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6451 6452 memset(modepage_info, 0, sizeof(*modepage_info)); 6453 6454 len_left = param_len - header_size - bd_len; 6455 len_used = header_size + bd_len; 6456 6457 modepage_info->header.len_left = len_left; 6458 modepage_info->header.len_used = len_used; 6459 6460 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6461} 6462 6463int 6464ctl_mode_sense(struct ctl_scsiio *ctsio) 6465{ 6466 struct ctl_lun *lun; 6467 int pc, page_code, dbd, llba, subpage; 6468 int alloc_len, page_len, header_len, total_len; 6469 struct scsi_mode_block_descr *block_desc; 6470 struct ctl_page_index *page_index; 6471 int control_dev; 6472 6473 dbd = 0; 6474 llba = 0; 6475 block_desc = NULL; 6476 page_index = NULL; 6477 6478 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6479 6480 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6481 6482 if (lun->be_lun->lun_type != T_DIRECT) 6483 control_dev = 1; 6484 else 6485 control_dev = 0; 6486 6487 switch (ctsio->cdb[0]) { 6488 case MODE_SENSE_6: { 6489 struct scsi_mode_sense_6 *cdb; 6490 6491 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6492 6493 header_len = sizeof(struct scsi_mode_hdr_6); 6494 if (cdb->byte2 & SMS_DBD) 6495 dbd = 1; 6496 else 6497 header_len += sizeof(struct scsi_mode_block_descr); 6498 6499 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6500 page_code = cdb->page & SMS_PAGE_CODE; 6501 subpage = cdb->subpage; 6502 alloc_len = cdb->length; 6503 break; 6504 } 6505 case MODE_SENSE_10: { 6506 struct scsi_mode_sense_10 *cdb; 6507 6508 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6509 6510 header_len = sizeof(struct scsi_mode_hdr_10); 6511 6512 if (cdb->byte2 & SMS_DBD) 6513 dbd = 1; 6514 else 6515 header_len += sizeof(struct scsi_mode_block_descr); 6516 if (cdb->byte2 & SMS10_LLBAA) 6517 llba = 1; 6518 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6519 page_code = cdb->page & SMS_PAGE_CODE; 6520 subpage = cdb->subpage; 6521 alloc_len = scsi_2btoul(cdb->length); 6522 break; 6523 } 6524 default: 6525 ctl_set_invalid_opcode(ctsio); 6526 ctl_done((union ctl_io *)ctsio); 6527 return (CTL_RETVAL_COMPLETE); 6528 break; /* NOTREACHED */ 6529 } 6530 6531 /* 6532 * We have to make a first pass through to calculate the size of 6533 * the pages that match the user's query. Then we allocate enough 6534 * memory to hold it, and actually copy the data into the buffer. 6535 */ 6536 switch (page_code) { 6537 case SMS_ALL_PAGES_PAGE: { 6538 int i; 6539 6540 page_len = 0; 6541 6542 /* 6543 * At the moment, values other than 0 and 0xff here are 6544 * reserved according to SPC-3. 6545 */ 6546 if ((subpage != SMS_SUBPAGE_PAGE_0) 6547 && (subpage != SMS_SUBPAGE_ALL)) { 6548 ctl_set_invalid_field(ctsio, 6549 /*sks_valid*/ 1, 6550 /*command*/ 1, 6551 /*field*/ 3, 6552 /*bit_valid*/ 0, 6553 /*bit*/ 0); 6554 ctl_done((union ctl_io *)ctsio); 6555 return (CTL_RETVAL_COMPLETE); 6556 } 6557 6558 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6559 if ((control_dev != 0) 6560 && (lun->mode_pages.index[i].page_flags & 6561 CTL_PAGE_FLAG_DISK_ONLY)) 6562 continue; 6563 6564 /* 6565 * We don't use this subpage if the user didn't 6566 * request all subpages. 6567 */ 6568 if ((lun->mode_pages.index[i].subpage != 0) 6569 && (subpage == SMS_SUBPAGE_PAGE_0)) 6570 continue; 6571 6572#if 0 6573 printf("found page %#x len %d\n", 6574 lun->mode_pages.index[i].page_code & 6575 SMPH_PC_MASK, 6576 lun->mode_pages.index[i].page_len); 6577#endif 6578 page_len += lun->mode_pages.index[i].page_len; 6579 } 6580 break; 6581 } 6582 default: { 6583 int i; 6584 6585 page_len = 0; 6586 6587 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6588 /* Look for the right page code */ 6589 if ((lun->mode_pages.index[i].page_code & 6590 SMPH_PC_MASK) != page_code) 6591 continue; 6592 6593 /* Look for the right subpage or the subpage wildcard*/ 6594 if ((lun->mode_pages.index[i].subpage != subpage) 6595 && (subpage != SMS_SUBPAGE_ALL)) 6596 continue; 6597 6598 /* Make sure the page is supported for this dev type */ 6599 if ((control_dev != 0) 6600 && (lun->mode_pages.index[i].page_flags & 6601 CTL_PAGE_FLAG_DISK_ONLY)) 6602 continue; 6603 6604#if 0 6605 printf("found page %#x len %d\n", 6606 lun->mode_pages.index[i].page_code & 6607 SMPH_PC_MASK, 6608 lun->mode_pages.index[i].page_len); 6609#endif 6610 6611 page_len += lun->mode_pages.index[i].page_len; 6612 } 6613 6614 if (page_len == 0) { 6615 ctl_set_invalid_field(ctsio, 6616 /*sks_valid*/ 1, 6617 /*command*/ 1, 6618 /*field*/ 2, 6619 /*bit_valid*/ 1, 6620 /*bit*/ 5); 6621 ctl_done((union ctl_io *)ctsio); 6622 return (CTL_RETVAL_COMPLETE); 6623 } 6624 break; 6625 } 6626 } 6627 6628 total_len = header_len + page_len; 6629#if 0 6630 printf("header_len = %d, page_len = %d, total_len = %d\n", 6631 header_len, page_len, total_len); 6632#endif 6633 6634 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6635 ctsio->kern_sg_entries = 0; 6636 ctsio->kern_data_resid = 0; 6637 ctsio->kern_rel_offset = 0; 6638 if (total_len < alloc_len) { 6639 ctsio->residual = alloc_len - total_len; 6640 ctsio->kern_data_len = total_len; 6641 ctsio->kern_total_len = total_len; 6642 } else { 6643 ctsio->residual = 0; 6644 ctsio->kern_data_len = alloc_len; 6645 ctsio->kern_total_len = alloc_len; 6646 } 6647 6648 switch (ctsio->cdb[0]) { 6649 case MODE_SENSE_6: { 6650 struct scsi_mode_hdr_6 *header; 6651 6652 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6653 6654 header->datalen = MIN(total_len - 1, 254); 6655 if (control_dev == 0) { 6656 header->dev_specific = 0x10; /* DPOFUA */ 6657 if ((lun->flags & CTL_LUN_READONLY) || 6658 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6659 .eca_and_aen & SCP_SWP) != 0) 6660 header->dev_specific |= 0x80; /* WP */ 6661 } 6662 if (dbd) 6663 header->block_descr_len = 0; 6664 else 6665 header->block_descr_len = 6666 sizeof(struct scsi_mode_block_descr); 6667 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6668 break; 6669 } 6670 case MODE_SENSE_10: { 6671 struct scsi_mode_hdr_10 *header; 6672 int datalen; 6673 6674 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6675 6676 datalen = MIN(total_len - 2, 65533); 6677 scsi_ulto2b(datalen, header->datalen); 6678 if (control_dev == 0) { 6679 header->dev_specific = 0x10; /* DPOFUA */ 6680 if ((lun->flags & CTL_LUN_READONLY) || 6681 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6682 .eca_and_aen & SCP_SWP) != 0) 6683 header->dev_specific |= 0x80; /* WP */ 6684 } 6685 if (dbd) 6686 scsi_ulto2b(0, header->block_descr_len); 6687 else 6688 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6689 header->block_descr_len); 6690 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6691 break; 6692 } 6693 default: 6694 panic("invalid CDB type %#x", ctsio->cdb[0]); 6695 break; /* NOTREACHED */ 6696 } 6697 6698 /* 6699 * If we've got a disk, use its blocksize in the block 6700 * descriptor. Otherwise, just set it to 0. 6701 */ 6702 if (dbd == 0) { 6703 if (control_dev == 0) 6704 scsi_ulto3b(lun->be_lun->blocksize, 6705 block_desc->block_len); 6706 else 6707 scsi_ulto3b(0, block_desc->block_len); 6708 } 6709 6710 switch (page_code) { 6711 case SMS_ALL_PAGES_PAGE: { 6712 int i, data_used; 6713 6714 data_used = header_len; 6715 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6716 struct ctl_page_index *page_index; 6717 6718 page_index = &lun->mode_pages.index[i]; 6719 6720 if ((control_dev != 0) 6721 && (page_index->page_flags & 6722 CTL_PAGE_FLAG_DISK_ONLY)) 6723 continue; 6724 6725 /* 6726 * We don't use this subpage if the user didn't 6727 * request all subpages. We already checked (above) 6728 * to make sure the user only specified a subpage 6729 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6730 */ 6731 if ((page_index->subpage != 0) 6732 && (subpage == SMS_SUBPAGE_PAGE_0)) 6733 continue; 6734 6735 /* 6736 * Call the handler, if it exists, to update the 6737 * page to the latest values. 6738 */ 6739 if (page_index->sense_handler != NULL) 6740 page_index->sense_handler(ctsio, page_index,pc); 6741 6742 memcpy(ctsio->kern_data_ptr + data_used, 6743 page_index->page_data + 6744 (page_index->page_len * pc), 6745 page_index->page_len); 6746 data_used += page_index->page_len; 6747 } 6748 break; 6749 } 6750 default: { 6751 int i, data_used; 6752 6753 data_used = header_len; 6754 6755 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6756 struct ctl_page_index *page_index; 6757 6758 page_index = &lun->mode_pages.index[i]; 6759 6760 /* Look for the right page code */ 6761 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6762 continue; 6763 6764 /* Look for the right subpage or the subpage wildcard*/ 6765 if ((page_index->subpage != subpage) 6766 && (subpage != SMS_SUBPAGE_ALL)) 6767 continue; 6768 6769 /* Make sure the page is supported for this dev type */ 6770 if ((control_dev != 0) 6771 && (page_index->page_flags & 6772 CTL_PAGE_FLAG_DISK_ONLY)) 6773 continue; 6774 6775 /* 6776 * Call the handler, if it exists, to update the 6777 * page to the latest values. 6778 */ 6779 if (page_index->sense_handler != NULL) 6780 page_index->sense_handler(ctsio, page_index,pc); 6781 6782 memcpy(ctsio->kern_data_ptr + data_used, 6783 page_index->page_data + 6784 (page_index->page_len * pc), 6785 page_index->page_len); 6786 data_used += page_index->page_len; 6787 } 6788 break; 6789 } 6790 } 6791 6792 ctl_set_success(ctsio); 6793 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6794 ctsio->be_move_done = ctl_config_move_done; 6795 ctl_datamove((union ctl_io *)ctsio); 6796 return (CTL_RETVAL_COMPLETE); 6797} 6798 6799int 6800ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6801 struct ctl_page_index *page_index, 6802 int pc) 6803{ 6804 struct ctl_lun *lun; 6805 struct scsi_log_param_header *phdr; 6806 uint8_t *data; 6807 uint64_t val; 6808 6809 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6810 data = page_index->page_data; 6811 6812 if (lun->backend->lun_attr != NULL && 6813 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6814 != UINT64_MAX) { 6815 phdr = (struct scsi_log_param_header *)data; 6816 scsi_ulto2b(0x0001, phdr->param_code); 6817 phdr->param_control = SLP_LBIN | SLP_LP; 6818 phdr->param_len = 8; 6819 data = (uint8_t *)(phdr + 1); 6820 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6821 data[4] = 0x02; /* per-pool */ 6822 data += phdr->param_len; 6823 } 6824 6825 if (lun->backend->lun_attr != NULL && 6826 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6827 != UINT64_MAX) { 6828 phdr = (struct scsi_log_param_header *)data; 6829 scsi_ulto2b(0x0002, phdr->param_code); 6830 phdr->param_control = SLP_LBIN | SLP_LP; 6831 phdr->param_len = 8; 6832 data = (uint8_t *)(phdr + 1); 6833 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6834 data[4] = 0x01; /* per-LUN */ 6835 data += phdr->param_len; 6836 } 6837 6838 if (lun->backend->lun_attr != NULL && 6839 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6840 != UINT64_MAX) { 6841 phdr = (struct scsi_log_param_header *)data; 6842 scsi_ulto2b(0x00f1, phdr->param_code); 6843 phdr->param_control = SLP_LBIN | SLP_LP; 6844 phdr->param_len = 8; 6845 data = (uint8_t *)(phdr + 1); 6846 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6847 data[4] = 0x02; /* per-pool */ 6848 data += phdr->param_len; 6849 } 6850 6851 if (lun->backend->lun_attr != NULL && 6852 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6853 != UINT64_MAX) { 6854 phdr = (struct scsi_log_param_header *)data; 6855 scsi_ulto2b(0x00f2, phdr->param_code); 6856 phdr->param_control = SLP_LBIN | SLP_LP; 6857 phdr->param_len = 8; 6858 data = (uint8_t *)(phdr + 1); 6859 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6860 data[4] = 0x02; /* per-pool */ 6861 data += phdr->param_len; 6862 } 6863 6864 page_index->page_len = data - page_index->page_data; 6865 return (0); 6866} 6867 6868int 6869ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6870 struct ctl_page_index *page_index, 6871 int pc) 6872{ 6873 struct ctl_lun *lun; 6874 struct stat_page *data; 6875 uint64_t rn, wn, rb, wb; 6876 struct bintime rt, wt; 6877 int i; 6878 6879 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6880 data = (struct stat_page *)page_index->page_data; 6881 6882 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6883 data->sap.hdr.param_control = SLP_LBIN; 6884 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6885 sizeof(struct scsi_log_param_header); 6886 rn = wn = rb = wb = 0; 6887 bintime_clear(&rt); 6888 bintime_clear(&wt); 6889 for (i = 0; i < CTL_MAX_PORTS; i++) { 6890 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6891 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6892 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6893 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6894 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6895 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6896 } 6897 scsi_u64to8b(rn, data->sap.read_num); 6898 scsi_u64to8b(wn, data->sap.write_num); 6899 if (lun->stats.blocksize > 0) { 6900 scsi_u64to8b(wb / lun->stats.blocksize, 6901 data->sap.recvieved_lba); 6902 scsi_u64to8b(rb / lun->stats.blocksize, 6903 data->sap.transmitted_lba); 6904 } 6905 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6906 data->sap.read_int); 6907 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6908 data->sap.write_int); 6909 scsi_u64to8b(0, data->sap.weighted_num); 6910 scsi_u64to8b(0, data->sap.weighted_int); 6911 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6912 data->it.hdr.param_control = SLP_LBIN; 6913 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6914 sizeof(struct scsi_log_param_header); 6915#ifdef CTL_TIME_IO 6916 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6917#endif 6918 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6919 data->it.hdr.param_control = SLP_LBIN; 6920 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6921 sizeof(struct scsi_log_param_header); 6922 scsi_ulto4b(3, data->ti.exponent); 6923 scsi_ulto4b(1, data->ti.integer); 6924 6925 page_index->page_len = sizeof(*data); 6926 return (0); 6927} 6928 6929int 6930ctl_log_sense(struct ctl_scsiio *ctsio) 6931{ 6932 struct ctl_lun *lun; 6933 int i, pc, page_code, subpage; 6934 int alloc_len, total_len; 6935 struct ctl_page_index *page_index; 6936 struct scsi_log_sense *cdb; 6937 struct scsi_log_header *header; 6938 6939 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6940 6941 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6942 cdb = (struct scsi_log_sense *)ctsio->cdb; 6943 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6944 page_code = cdb->page & SLS_PAGE_CODE; 6945 subpage = cdb->subpage; 6946 alloc_len = scsi_2btoul(cdb->length); 6947 6948 page_index = NULL; 6949 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6950 page_index = &lun->log_pages.index[i]; 6951 6952 /* Look for the right page code */ 6953 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6954 continue; 6955 6956 /* Look for the right subpage or the subpage wildcard*/ 6957 if (page_index->subpage != subpage) 6958 continue; 6959 6960 break; 6961 } 6962 if (i >= CTL_NUM_LOG_PAGES) { 6963 ctl_set_invalid_field(ctsio, 6964 /*sks_valid*/ 1, 6965 /*command*/ 1, 6966 /*field*/ 2, 6967 /*bit_valid*/ 0, 6968 /*bit*/ 0); 6969 ctl_done((union ctl_io *)ctsio); 6970 return (CTL_RETVAL_COMPLETE); 6971 } 6972 6973 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6974 6975 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6976 ctsio->kern_sg_entries = 0; 6977 ctsio->kern_data_resid = 0; 6978 ctsio->kern_rel_offset = 0; 6979 if (total_len < alloc_len) { 6980 ctsio->residual = alloc_len - total_len; 6981 ctsio->kern_data_len = total_len; 6982 ctsio->kern_total_len = total_len; 6983 } else { 6984 ctsio->residual = 0; 6985 ctsio->kern_data_len = alloc_len; 6986 ctsio->kern_total_len = alloc_len; 6987 } 6988 6989 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6990 header->page = page_index->page_code; 6991 if (page_index->subpage) { 6992 header->page |= SL_SPF; 6993 header->subpage = page_index->subpage; 6994 } 6995 scsi_ulto2b(page_index->page_len, header->datalen); 6996 6997 /* 6998 * Call the handler, if it exists, to update the 6999 * page to the latest values. 7000 */ 7001 if (page_index->sense_handler != NULL) 7002 page_index->sense_handler(ctsio, page_index, pc); 7003 7004 memcpy(header + 1, page_index->page_data, page_index->page_len); 7005 7006 ctl_set_success(ctsio); 7007 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7008 ctsio->be_move_done = ctl_config_move_done; 7009 ctl_datamove((union ctl_io *)ctsio); 7010 return (CTL_RETVAL_COMPLETE); 7011} 7012 7013int 7014ctl_read_capacity(struct ctl_scsiio *ctsio) 7015{ 7016 struct scsi_read_capacity *cdb; 7017 struct scsi_read_capacity_data *data; 7018 struct ctl_lun *lun; 7019 uint32_t lba; 7020 7021 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7022 7023 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7024 7025 lba = scsi_4btoul(cdb->addr); 7026 if (((cdb->pmi & SRC_PMI) == 0) 7027 && (lba != 0)) { 7028 ctl_set_invalid_field(/*ctsio*/ ctsio, 7029 /*sks_valid*/ 1, 7030 /*command*/ 1, 7031 /*field*/ 2, 7032 /*bit_valid*/ 0, 7033 /*bit*/ 0); 7034 ctl_done((union ctl_io *)ctsio); 7035 return (CTL_RETVAL_COMPLETE); 7036 } 7037 7038 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7039 7040 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7041 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7042 ctsio->residual = 0; 7043 ctsio->kern_data_len = sizeof(*data); 7044 ctsio->kern_total_len = sizeof(*data); 7045 ctsio->kern_data_resid = 0; 7046 ctsio->kern_rel_offset = 0; 7047 ctsio->kern_sg_entries = 0; 7048 7049 /* 7050 * If the maximum LBA is greater than 0xfffffffe, the user must 7051 * issue a SERVICE ACTION IN (16) command, with the read capacity 7052 * serivce action set. 7053 */ 7054 if (lun->be_lun->maxlba > 0xfffffffe) 7055 scsi_ulto4b(0xffffffff, data->addr); 7056 else 7057 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7058 7059 /* 7060 * XXX KDM this may not be 512 bytes... 7061 */ 7062 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7063 7064 ctl_set_success(ctsio); 7065 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7066 ctsio->be_move_done = ctl_config_move_done; 7067 ctl_datamove((union ctl_io *)ctsio); 7068 return (CTL_RETVAL_COMPLETE); 7069} 7070 7071int 7072ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7073{ 7074 struct scsi_read_capacity_16 *cdb; 7075 struct scsi_read_capacity_data_long *data; 7076 struct ctl_lun *lun; 7077 uint64_t lba; 7078 uint32_t alloc_len; 7079 7080 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7081 7082 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7083 7084 alloc_len = scsi_4btoul(cdb->alloc_len); 7085 lba = scsi_8btou64(cdb->addr); 7086 7087 if ((cdb->reladr & SRC16_PMI) 7088 && (lba != 0)) { 7089 ctl_set_invalid_field(/*ctsio*/ ctsio, 7090 /*sks_valid*/ 1, 7091 /*command*/ 1, 7092 /*field*/ 2, 7093 /*bit_valid*/ 0, 7094 /*bit*/ 0); 7095 ctl_done((union ctl_io *)ctsio); 7096 return (CTL_RETVAL_COMPLETE); 7097 } 7098 7099 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7100 7101 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7102 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7103 7104 if (sizeof(*data) < alloc_len) { 7105 ctsio->residual = alloc_len - sizeof(*data); 7106 ctsio->kern_data_len = sizeof(*data); 7107 ctsio->kern_total_len = sizeof(*data); 7108 } else { 7109 ctsio->residual = 0; 7110 ctsio->kern_data_len = alloc_len; 7111 ctsio->kern_total_len = alloc_len; 7112 } 7113 ctsio->kern_data_resid = 0; 7114 ctsio->kern_rel_offset = 0; 7115 ctsio->kern_sg_entries = 0; 7116 7117 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7118 /* XXX KDM this may not be 512 bytes... */ 7119 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7120 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7121 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7122 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7123 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7124 7125 ctl_set_success(ctsio); 7126 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7127 ctsio->be_move_done = ctl_config_move_done; 7128 ctl_datamove((union ctl_io *)ctsio); 7129 return (CTL_RETVAL_COMPLETE); 7130} 7131 7132int 7133ctl_get_lba_status(struct ctl_scsiio *ctsio) 7134{ 7135 struct scsi_get_lba_status *cdb; 7136 struct scsi_get_lba_status_data *data; 7137 struct ctl_lun *lun; 7138 struct ctl_lba_len_flags *lbalen; 7139 uint64_t lba; 7140 uint32_t alloc_len, total_len; 7141 int retval; 7142 7143 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7144 7145 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7146 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7147 lba = scsi_8btou64(cdb->addr); 7148 alloc_len = scsi_4btoul(cdb->alloc_len); 7149 7150 if (lba > lun->be_lun->maxlba) { 7151 ctl_set_lba_out_of_range(ctsio); 7152 ctl_done((union ctl_io *)ctsio); 7153 return (CTL_RETVAL_COMPLETE); 7154 } 7155 7156 total_len = sizeof(*data) + sizeof(data->descr[0]); 7157 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7158 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7159 7160 if (total_len < alloc_len) { 7161 ctsio->residual = alloc_len - total_len; 7162 ctsio->kern_data_len = total_len; 7163 ctsio->kern_total_len = total_len; 7164 } else { 7165 ctsio->residual = 0; 7166 ctsio->kern_data_len = alloc_len; 7167 ctsio->kern_total_len = alloc_len; 7168 } 7169 ctsio->kern_data_resid = 0; 7170 ctsio->kern_rel_offset = 0; 7171 ctsio->kern_sg_entries = 0; 7172 7173 /* Fill dummy data in case backend can't tell anything. */ 7174 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7175 scsi_u64to8b(lba, data->descr[0].addr); 7176 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7177 data->descr[0].length); 7178 data->descr[0].status = 0; /* Mapped or unknown. */ 7179 7180 ctl_set_success(ctsio); 7181 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7182 ctsio->be_move_done = ctl_config_move_done; 7183 7184 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7185 lbalen->lba = lba; 7186 lbalen->len = total_len; 7187 lbalen->flags = 0; 7188 retval = lun->backend->config_read((union ctl_io *)ctsio); 7189 return (CTL_RETVAL_COMPLETE); 7190} 7191 7192int 7193ctl_read_defect(struct ctl_scsiio *ctsio) 7194{ 7195 struct scsi_read_defect_data_10 *ccb10; 7196 struct scsi_read_defect_data_12 *ccb12; 7197 struct scsi_read_defect_data_hdr_10 *data10; 7198 struct scsi_read_defect_data_hdr_12 *data12; 7199 uint32_t alloc_len, data_len; 7200 uint8_t format; 7201 7202 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7203 7204 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7205 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7206 format = ccb10->format; 7207 alloc_len = scsi_2btoul(ccb10->alloc_length); 7208 data_len = sizeof(*data10); 7209 } else { 7210 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7211 format = ccb12->format; 7212 alloc_len = scsi_4btoul(ccb12->alloc_length); 7213 data_len = sizeof(*data12); 7214 } 7215 if (alloc_len == 0) { 7216 ctl_set_success(ctsio); 7217 ctl_done((union ctl_io *)ctsio); 7218 return (CTL_RETVAL_COMPLETE); 7219 } 7220 7221 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7222 if (data_len < alloc_len) { 7223 ctsio->residual = alloc_len - data_len; 7224 ctsio->kern_data_len = data_len; 7225 ctsio->kern_total_len = data_len; 7226 } else { 7227 ctsio->residual = 0; 7228 ctsio->kern_data_len = alloc_len; 7229 ctsio->kern_total_len = alloc_len; 7230 } 7231 ctsio->kern_data_resid = 0; 7232 ctsio->kern_rel_offset = 0; 7233 ctsio->kern_sg_entries = 0; 7234 7235 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7236 data10 = (struct scsi_read_defect_data_hdr_10 *) 7237 ctsio->kern_data_ptr; 7238 data10->format = format; 7239 scsi_ulto2b(0, data10->length); 7240 } else { 7241 data12 = (struct scsi_read_defect_data_hdr_12 *) 7242 ctsio->kern_data_ptr; 7243 data12->format = format; 7244 scsi_ulto2b(0, data12->generation); 7245 scsi_ulto4b(0, data12->length); 7246 } 7247 7248 ctl_set_success(ctsio); 7249 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7250 ctsio->be_move_done = ctl_config_move_done; 7251 ctl_datamove((union ctl_io *)ctsio); 7252 return (CTL_RETVAL_COMPLETE); 7253} 7254 7255int 7256ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7257{ 7258 struct scsi_maintenance_in *cdb; 7259 int retval; 7260 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 7261 int num_target_port_groups, num_target_ports; 7262 struct ctl_lun *lun; 7263 struct ctl_softc *softc; 7264 struct ctl_port *port; 7265 struct scsi_target_group_data *rtg_ptr; 7266 struct scsi_target_group_data_extended *rtg_ext_ptr; 7267 struct scsi_target_port_group_descriptor *tpg_desc; 7268 7269 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7270 7271 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7272 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7273 softc = lun->ctl_softc; 7274 7275 retval = CTL_RETVAL_COMPLETE; 7276 7277 switch (cdb->byte2 & STG_PDF_MASK) { 7278 case STG_PDF_LENGTH: 7279 ext = 0; 7280 break; 7281 case STG_PDF_EXTENDED: 7282 ext = 1; 7283 break; 7284 default: 7285 ctl_set_invalid_field(/*ctsio*/ ctsio, 7286 /*sks_valid*/ 1, 7287 /*command*/ 1, 7288 /*field*/ 2, 7289 /*bit_valid*/ 1, 7290 /*bit*/ 5); 7291 ctl_done((union ctl_io *)ctsio); 7292 return(retval); 7293 } 7294 7295 if (softc->is_single) 7296 num_target_port_groups = 1; 7297 else 7298 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7299 num_target_ports = 0; 7300 mtx_lock(&softc->ctl_lock); 7301 STAILQ_FOREACH(port, &softc->port_list, links) { 7302 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7303 continue; 7304 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7305 continue; 7306 num_target_ports++; 7307 } 7308 mtx_unlock(&softc->ctl_lock); 7309 7310 if (ext) 7311 total_len = sizeof(struct scsi_target_group_data_extended); 7312 else 7313 total_len = sizeof(struct scsi_target_group_data); 7314 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7315 num_target_port_groups + 7316 sizeof(struct scsi_target_port_descriptor) * 7317 num_target_ports * num_target_port_groups; 7318 7319 alloc_len = scsi_4btoul(cdb->length); 7320 7321 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7322 7323 ctsio->kern_sg_entries = 0; 7324 7325 if (total_len < alloc_len) { 7326 ctsio->residual = alloc_len - total_len; 7327 ctsio->kern_data_len = total_len; 7328 ctsio->kern_total_len = total_len; 7329 } else { 7330 ctsio->residual = 0; 7331 ctsio->kern_data_len = alloc_len; 7332 ctsio->kern_total_len = alloc_len; 7333 } 7334 ctsio->kern_data_resid = 0; 7335 ctsio->kern_rel_offset = 0; 7336 7337 if (ext) { 7338 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7339 ctsio->kern_data_ptr; 7340 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7341 rtg_ext_ptr->format_type = 0x10; 7342 rtg_ext_ptr->implicit_transition_time = 0; 7343 tpg_desc = &rtg_ext_ptr->groups[0]; 7344 } else { 7345 rtg_ptr = (struct scsi_target_group_data *) 7346 ctsio->kern_data_ptr; 7347 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7348 tpg_desc = &rtg_ptr->groups[0]; 7349 } 7350 7351 mtx_lock(&softc->ctl_lock); 7352 pg = softc->port_offset / CTL_MAX_PORTS; 7353 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 7354 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 7355 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7356 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7357 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 7358 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7359 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7360 } else { 7361 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7362 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7363 } 7364 } else { 7365 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7366 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7367 } 7368 for (g = 0; g < num_target_port_groups; g++) { 7369 tpg_desc->pref_state = (g == pg) ? gs : os; 7370 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 7371 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7372 tpg_desc->status = TPG_IMPLICIT; 7373 pc = 0; 7374 STAILQ_FOREACH(port, &softc->port_list, links) { 7375 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7376 continue; 7377 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7378 continue; 7379 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7380 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7381 relative_target_port_identifier); 7382 pc++; 7383 } 7384 tpg_desc->target_port_count = pc; 7385 tpg_desc = (struct scsi_target_port_group_descriptor *) 7386 &tpg_desc->descriptors[pc]; 7387 } 7388 mtx_unlock(&softc->ctl_lock); 7389 7390 ctl_set_success(ctsio); 7391 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7392 ctsio->be_move_done = ctl_config_move_done; 7393 ctl_datamove((union ctl_io *)ctsio); 7394 return(retval); 7395} 7396 7397int 7398ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7399{ 7400 struct ctl_lun *lun; 7401 struct scsi_report_supported_opcodes *cdb; 7402 const struct ctl_cmd_entry *entry, *sentry; 7403 struct scsi_report_supported_opcodes_all *all; 7404 struct scsi_report_supported_opcodes_descr *descr; 7405 struct scsi_report_supported_opcodes_one *one; 7406 int retval; 7407 int alloc_len, total_len; 7408 int opcode, service_action, i, j, num; 7409 7410 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7411 7412 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7413 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7414 7415 retval = CTL_RETVAL_COMPLETE; 7416 7417 opcode = cdb->requested_opcode; 7418 service_action = scsi_2btoul(cdb->requested_service_action); 7419 switch (cdb->options & RSO_OPTIONS_MASK) { 7420 case RSO_OPTIONS_ALL: 7421 num = 0; 7422 for (i = 0; i < 256; i++) { 7423 entry = &ctl_cmd_table[i]; 7424 if (entry->flags & CTL_CMD_FLAG_SA5) { 7425 for (j = 0; j < 32; j++) { 7426 sentry = &((const struct ctl_cmd_entry *) 7427 entry->execute)[j]; 7428 if (ctl_cmd_applicable( 7429 lun->be_lun->lun_type, sentry)) 7430 num++; 7431 } 7432 } else { 7433 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7434 entry)) 7435 num++; 7436 } 7437 } 7438 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7439 num * sizeof(struct scsi_report_supported_opcodes_descr); 7440 break; 7441 case RSO_OPTIONS_OC: 7442 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7443 ctl_set_invalid_field(/*ctsio*/ ctsio, 7444 /*sks_valid*/ 1, 7445 /*command*/ 1, 7446 /*field*/ 2, 7447 /*bit_valid*/ 1, 7448 /*bit*/ 2); 7449 ctl_done((union ctl_io *)ctsio); 7450 return (CTL_RETVAL_COMPLETE); 7451 } 7452 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7453 break; 7454 case RSO_OPTIONS_OC_SA: 7455 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7456 service_action >= 32) { 7457 ctl_set_invalid_field(/*ctsio*/ ctsio, 7458 /*sks_valid*/ 1, 7459 /*command*/ 1, 7460 /*field*/ 2, 7461 /*bit_valid*/ 1, 7462 /*bit*/ 2); 7463 ctl_done((union ctl_io *)ctsio); 7464 return (CTL_RETVAL_COMPLETE); 7465 } 7466 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7467 break; 7468 default: 7469 ctl_set_invalid_field(/*ctsio*/ ctsio, 7470 /*sks_valid*/ 1, 7471 /*command*/ 1, 7472 /*field*/ 2, 7473 /*bit_valid*/ 1, 7474 /*bit*/ 2); 7475 ctl_done((union ctl_io *)ctsio); 7476 return (CTL_RETVAL_COMPLETE); 7477 } 7478 7479 alloc_len = scsi_4btoul(cdb->length); 7480 7481 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7482 7483 ctsio->kern_sg_entries = 0; 7484 7485 if (total_len < alloc_len) { 7486 ctsio->residual = alloc_len - total_len; 7487 ctsio->kern_data_len = total_len; 7488 ctsio->kern_total_len = total_len; 7489 } else { 7490 ctsio->residual = 0; 7491 ctsio->kern_data_len = alloc_len; 7492 ctsio->kern_total_len = alloc_len; 7493 } 7494 ctsio->kern_data_resid = 0; 7495 ctsio->kern_rel_offset = 0; 7496 7497 switch (cdb->options & RSO_OPTIONS_MASK) { 7498 case RSO_OPTIONS_ALL: 7499 all = (struct scsi_report_supported_opcodes_all *) 7500 ctsio->kern_data_ptr; 7501 num = 0; 7502 for (i = 0; i < 256; i++) { 7503 entry = &ctl_cmd_table[i]; 7504 if (entry->flags & CTL_CMD_FLAG_SA5) { 7505 for (j = 0; j < 32; j++) { 7506 sentry = &((const struct ctl_cmd_entry *) 7507 entry->execute)[j]; 7508 if (!ctl_cmd_applicable( 7509 lun->be_lun->lun_type, sentry)) 7510 continue; 7511 descr = &all->descr[num++]; 7512 descr->opcode = i; 7513 scsi_ulto2b(j, descr->service_action); 7514 descr->flags = RSO_SERVACTV; 7515 scsi_ulto2b(sentry->length, 7516 descr->cdb_length); 7517 } 7518 } else { 7519 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7520 entry)) 7521 continue; 7522 descr = &all->descr[num++]; 7523 descr->opcode = i; 7524 scsi_ulto2b(0, descr->service_action); 7525 descr->flags = 0; 7526 scsi_ulto2b(entry->length, descr->cdb_length); 7527 } 7528 } 7529 scsi_ulto4b( 7530 num * sizeof(struct scsi_report_supported_opcodes_descr), 7531 all->length); 7532 break; 7533 case RSO_OPTIONS_OC: 7534 one = (struct scsi_report_supported_opcodes_one *) 7535 ctsio->kern_data_ptr; 7536 entry = &ctl_cmd_table[opcode]; 7537 goto fill_one; 7538 case RSO_OPTIONS_OC_SA: 7539 one = (struct scsi_report_supported_opcodes_one *) 7540 ctsio->kern_data_ptr; 7541 entry = &ctl_cmd_table[opcode]; 7542 entry = &((const struct ctl_cmd_entry *) 7543 entry->execute)[service_action]; 7544fill_one: 7545 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7546 one->support = 3; 7547 scsi_ulto2b(entry->length, one->cdb_length); 7548 one->cdb_usage[0] = opcode; 7549 memcpy(&one->cdb_usage[1], entry->usage, 7550 entry->length - 1); 7551 } else 7552 one->support = 1; 7553 break; 7554 } 7555 7556 ctl_set_success(ctsio); 7557 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7558 ctsio->be_move_done = ctl_config_move_done; 7559 ctl_datamove((union ctl_io *)ctsio); 7560 return(retval); 7561} 7562 7563int 7564ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7565{ 7566 struct scsi_report_supported_tmf *cdb; 7567 struct scsi_report_supported_tmf_data *data; 7568 int retval; 7569 int alloc_len, total_len; 7570 7571 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7572 7573 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7574 7575 retval = CTL_RETVAL_COMPLETE; 7576 7577 total_len = sizeof(struct scsi_report_supported_tmf_data); 7578 alloc_len = scsi_4btoul(cdb->length); 7579 7580 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7581 7582 ctsio->kern_sg_entries = 0; 7583 7584 if (total_len < alloc_len) { 7585 ctsio->residual = alloc_len - total_len; 7586 ctsio->kern_data_len = total_len; 7587 ctsio->kern_total_len = total_len; 7588 } else { 7589 ctsio->residual = 0; 7590 ctsio->kern_data_len = alloc_len; 7591 ctsio->kern_total_len = alloc_len; 7592 } 7593 ctsio->kern_data_resid = 0; 7594 ctsio->kern_rel_offset = 0; 7595 7596 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7597 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7598 data->byte2 |= RST_ITNRS; 7599 7600 ctl_set_success(ctsio); 7601 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7602 ctsio->be_move_done = ctl_config_move_done; 7603 ctl_datamove((union ctl_io *)ctsio); 7604 return (retval); 7605} 7606 7607int 7608ctl_report_timestamp(struct ctl_scsiio *ctsio) 7609{ 7610 struct scsi_report_timestamp *cdb; 7611 struct scsi_report_timestamp_data *data; 7612 struct timeval tv; 7613 int64_t timestamp; 7614 int retval; 7615 int alloc_len, total_len; 7616 7617 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7618 7619 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7620 7621 retval = CTL_RETVAL_COMPLETE; 7622 7623 total_len = sizeof(struct scsi_report_timestamp_data); 7624 alloc_len = scsi_4btoul(cdb->length); 7625 7626 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7627 7628 ctsio->kern_sg_entries = 0; 7629 7630 if (total_len < alloc_len) { 7631 ctsio->residual = alloc_len - total_len; 7632 ctsio->kern_data_len = total_len; 7633 ctsio->kern_total_len = total_len; 7634 } else { 7635 ctsio->residual = 0; 7636 ctsio->kern_data_len = alloc_len; 7637 ctsio->kern_total_len = alloc_len; 7638 } 7639 ctsio->kern_data_resid = 0; 7640 ctsio->kern_rel_offset = 0; 7641 7642 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7643 scsi_ulto2b(sizeof(*data) - 2, data->length); 7644 data->origin = RTS_ORIG_OUTSIDE; 7645 getmicrotime(&tv); 7646 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7647 scsi_ulto4b(timestamp >> 16, data->timestamp); 7648 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7649 7650 ctl_set_success(ctsio); 7651 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7652 ctsio->be_move_done = ctl_config_move_done; 7653 ctl_datamove((union ctl_io *)ctsio); 7654 return (retval); 7655} 7656 7657int 7658ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7659{ 7660 struct scsi_per_res_in *cdb; 7661 int alloc_len, total_len = 0; 7662 /* struct scsi_per_res_in_rsrv in_data; */ 7663 struct ctl_lun *lun; 7664 struct ctl_softc *softc; 7665 uint64_t key; 7666 7667 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7668 7669 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7670 7671 alloc_len = scsi_2btoul(cdb->length); 7672 7673 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7674 softc = lun->ctl_softc; 7675 7676retry: 7677 mtx_lock(&lun->lun_lock); 7678 switch (cdb->action) { 7679 case SPRI_RK: /* read keys */ 7680 total_len = sizeof(struct scsi_per_res_in_keys) + 7681 lun->pr_key_count * 7682 sizeof(struct scsi_per_res_key); 7683 break; 7684 case SPRI_RR: /* read reservation */ 7685 if (lun->flags & CTL_LUN_PR_RESERVED) 7686 total_len = sizeof(struct scsi_per_res_in_rsrv); 7687 else 7688 total_len = sizeof(struct scsi_per_res_in_header); 7689 break; 7690 case SPRI_RC: /* report capabilities */ 7691 total_len = sizeof(struct scsi_per_res_cap); 7692 break; 7693 case SPRI_RS: /* read full status */ 7694 total_len = sizeof(struct scsi_per_res_in_header) + 7695 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7696 lun->pr_key_count; 7697 break; 7698 default: 7699 panic("Invalid PR type %x", cdb->action); 7700 } 7701 mtx_unlock(&lun->lun_lock); 7702 7703 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7704 7705 if (total_len < alloc_len) { 7706 ctsio->residual = alloc_len - total_len; 7707 ctsio->kern_data_len = total_len; 7708 ctsio->kern_total_len = total_len; 7709 } else { 7710 ctsio->residual = 0; 7711 ctsio->kern_data_len = alloc_len; 7712 ctsio->kern_total_len = alloc_len; 7713 } 7714 7715 ctsio->kern_data_resid = 0; 7716 ctsio->kern_rel_offset = 0; 7717 ctsio->kern_sg_entries = 0; 7718 7719 mtx_lock(&lun->lun_lock); 7720 switch (cdb->action) { 7721 case SPRI_RK: { // read keys 7722 struct scsi_per_res_in_keys *res_keys; 7723 int i, key_count; 7724 7725 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7726 7727 /* 7728 * We had to drop the lock to allocate our buffer, which 7729 * leaves time for someone to come in with another 7730 * persistent reservation. (That is unlikely, though, 7731 * since this should be the only persistent reservation 7732 * command active right now.) 7733 */ 7734 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7735 (lun->pr_key_count * 7736 sizeof(struct scsi_per_res_key)))){ 7737 mtx_unlock(&lun->lun_lock); 7738 free(ctsio->kern_data_ptr, M_CTL); 7739 printf("%s: reservation length changed, retrying\n", 7740 __func__); 7741 goto retry; 7742 } 7743 7744 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7745 7746 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7747 lun->pr_key_count, res_keys->header.length); 7748 7749 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7750 if ((key = ctl_get_prkey(lun, i)) == 0) 7751 continue; 7752 7753 /* 7754 * We used lun->pr_key_count to calculate the 7755 * size to allocate. If it turns out the number of 7756 * initiators with the registered flag set is 7757 * larger than that (i.e. they haven't been kept in 7758 * sync), we've got a problem. 7759 */ 7760 if (key_count >= lun->pr_key_count) { 7761#ifdef NEEDTOPORT 7762 csevent_log(CSC_CTL | CSC_SHELF_SW | 7763 CTL_PR_ERROR, 7764 csevent_LogType_Fault, 7765 csevent_AlertLevel_Yellow, 7766 csevent_FRU_ShelfController, 7767 csevent_FRU_Firmware, 7768 csevent_FRU_Unknown, 7769 "registered keys %d >= key " 7770 "count %d", key_count, 7771 lun->pr_key_count); 7772#endif 7773 key_count++; 7774 continue; 7775 } 7776 scsi_u64to8b(key, res_keys->keys[key_count].key); 7777 key_count++; 7778 } 7779 break; 7780 } 7781 case SPRI_RR: { // read reservation 7782 struct scsi_per_res_in_rsrv *res; 7783 int tmp_len, header_only; 7784 7785 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7786 7787 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7788 7789 if (lun->flags & CTL_LUN_PR_RESERVED) 7790 { 7791 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7792 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7793 res->header.length); 7794 header_only = 0; 7795 } else { 7796 tmp_len = sizeof(struct scsi_per_res_in_header); 7797 scsi_ulto4b(0, res->header.length); 7798 header_only = 1; 7799 } 7800 7801 /* 7802 * We had to drop the lock to allocate our buffer, which 7803 * leaves time for someone to come in with another 7804 * persistent reservation. (That is unlikely, though, 7805 * since this should be the only persistent reservation 7806 * command active right now.) 7807 */ 7808 if (tmp_len != total_len) { 7809 mtx_unlock(&lun->lun_lock); 7810 free(ctsio->kern_data_ptr, M_CTL); 7811 printf("%s: reservation status changed, retrying\n", 7812 __func__); 7813 goto retry; 7814 } 7815 7816 /* 7817 * No reservation held, so we're done. 7818 */ 7819 if (header_only != 0) 7820 break; 7821 7822 /* 7823 * If the registration is an All Registrants type, the key 7824 * is 0, since it doesn't really matter. 7825 */ 7826 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7827 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7828 res->data.reservation); 7829 } 7830 res->data.scopetype = lun->res_type; 7831 break; 7832 } 7833 case SPRI_RC: //report capabilities 7834 { 7835 struct scsi_per_res_cap *res_cap; 7836 uint16_t type_mask; 7837 7838 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7839 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7840 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7841 type_mask = SPRI_TM_WR_EX_AR | 7842 SPRI_TM_EX_AC_RO | 7843 SPRI_TM_WR_EX_RO | 7844 SPRI_TM_EX_AC | 7845 SPRI_TM_WR_EX | 7846 SPRI_TM_EX_AC_AR; 7847 scsi_ulto2b(type_mask, res_cap->type_mask); 7848 break; 7849 } 7850 case SPRI_RS: { // read full status 7851 struct scsi_per_res_in_full *res_status; 7852 struct scsi_per_res_in_full_desc *res_desc; 7853 struct ctl_port *port; 7854 int i, len; 7855 7856 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7857 7858 /* 7859 * We had to drop the lock to allocate our buffer, which 7860 * leaves time for someone to come in with another 7861 * persistent reservation. (That is unlikely, though, 7862 * since this should be the only persistent reservation 7863 * command active right now.) 7864 */ 7865 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7866 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7867 lun->pr_key_count)){ 7868 mtx_unlock(&lun->lun_lock); 7869 free(ctsio->kern_data_ptr, M_CTL); 7870 printf("%s: reservation length changed, retrying\n", 7871 __func__); 7872 goto retry; 7873 } 7874 7875 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7876 7877 res_desc = &res_status->desc[0]; 7878 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7879 if ((key = ctl_get_prkey(lun, i)) == 0) 7880 continue; 7881 7882 scsi_u64to8b(key, res_desc->res_key.key); 7883 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7884 (lun->pr_res_idx == i || 7885 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7886 res_desc->flags = SPRI_FULL_R_HOLDER; 7887 res_desc->scopetype = lun->res_type; 7888 } 7889 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7890 res_desc->rel_trgt_port_id); 7891 len = 0; 7892 port = softc->ctl_ports[ 7893 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7894 if (port != NULL) 7895 len = ctl_create_iid(port, 7896 i % CTL_MAX_INIT_PER_PORT, 7897 res_desc->transport_id); 7898 scsi_ulto4b(len, res_desc->additional_length); 7899 res_desc = (struct scsi_per_res_in_full_desc *) 7900 &res_desc->transport_id[len]; 7901 } 7902 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7903 res_status->header.length); 7904 break; 7905 } 7906 default: 7907 /* 7908 * This is a bug, because we just checked for this above, 7909 * and should have returned an error. 7910 */ 7911 panic("Invalid PR type %x", cdb->action); 7912 break; /* NOTREACHED */ 7913 } 7914 mtx_unlock(&lun->lun_lock); 7915 7916 ctl_set_success(ctsio); 7917 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7918 ctsio->be_move_done = ctl_config_move_done; 7919 ctl_datamove((union ctl_io *)ctsio); 7920 return (CTL_RETVAL_COMPLETE); 7921} 7922 7923static void 7924ctl_est_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua) 7925{ 7926 int off = lun->ctl_softc->persis_offset; 7927 7928 if (residx >= off && residx < off + CTL_MAX_INITIATORS) 7929 ctl_est_ua(lun, residx - off, ua); 7930} 7931 7932/* 7933 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7934 * it should return. 7935 */ 7936static int 7937ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7938 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7939 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7940 struct scsi_per_res_out_parms* param) 7941{ 7942 union ctl_ha_msg persis_io; 7943 int retval, i; 7944 int isc_retval; 7945 7946 retval = 0; 7947 7948 mtx_lock(&lun->lun_lock); 7949 if (sa_res_key == 0) { 7950 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7951 /* validate scope and type */ 7952 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7953 SPR_LU_SCOPE) { 7954 mtx_unlock(&lun->lun_lock); 7955 ctl_set_invalid_field(/*ctsio*/ ctsio, 7956 /*sks_valid*/ 1, 7957 /*command*/ 1, 7958 /*field*/ 2, 7959 /*bit_valid*/ 1, 7960 /*bit*/ 4); 7961 ctl_done((union ctl_io *)ctsio); 7962 return (1); 7963 } 7964 7965 if (type>8 || type==2 || type==4 || type==0) { 7966 mtx_unlock(&lun->lun_lock); 7967 ctl_set_invalid_field(/*ctsio*/ ctsio, 7968 /*sks_valid*/ 1, 7969 /*command*/ 1, 7970 /*field*/ 2, 7971 /*bit_valid*/ 1, 7972 /*bit*/ 0); 7973 ctl_done((union ctl_io *)ctsio); 7974 return (1); 7975 } 7976 7977 /* 7978 * Unregister everybody else and build UA for 7979 * them 7980 */ 7981 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7982 if (i == residx || ctl_get_prkey(lun, i) == 0) 7983 continue; 7984 7985 ctl_clr_prkey(lun, i); 7986 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 7987 } 7988 lun->pr_key_count = 1; 7989 lun->res_type = type; 7990 if (lun->res_type != SPR_TYPE_WR_EX_AR 7991 && lun->res_type != SPR_TYPE_EX_AC_AR) 7992 lun->pr_res_idx = residx; 7993 7994 /* send msg to other side */ 7995 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7996 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7997 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7998 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7999 persis_io.pr.pr_info.res_type = type; 8000 memcpy(persis_io.pr.pr_info.sa_res_key, 8001 param->serv_act_res_key, 8002 sizeof(param->serv_act_res_key)); 8003 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8004 &persis_io, sizeof(persis_io), 0)) > 8005 CTL_HA_STATUS_SUCCESS) { 8006 printf("CTL:Persis Out error returned " 8007 "from ctl_ha_msg_send %d\n", 8008 isc_retval); 8009 } 8010 } else { 8011 /* not all registrants */ 8012 mtx_unlock(&lun->lun_lock); 8013 free(ctsio->kern_data_ptr, M_CTL); 8014 ctl_set_invalid_field(ctsio, 8015 /*sks_valid*/ 1, 8016 /*command*/ 0, 8017 /*field*/ 8, 8018 /*bit_valid*/ 0, 8019 /*bit*/ 0); 8020 ctl_done((union ctl_io *)ctsio); 8021 return (1); 8022 } 8023 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8024 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8025 int found = 0; 8026 8027 if (res_key == sa_res_key) { 8028 /* special case */ 8029 /* 8030 * The spec implies this is not good but doesn't 8031 * say what to do. There are two choices either 8032 * generate a res conflict or check condition 8033 * with illegal field in parameter data. Since 8034 * that is what is done when the sa_res_key is 8035 * zero I'll take that approach since this has 8036 * to do with the sa_res_key. 8037 */ 8038 mtx_unlock(&lun->lun_lock); 8039 free(ctsio->kern_data_ptr, M_CTL); 8040 ctl_set_invalid_field(ctsio, 8041 /*sks_valid*/ 1, 8042 /*command*/ 0, 8043 /*field*/ 8, 8044 /*bit_valid*/ 0, 8045 /*bit*/ 0); 8046 ctl_done((union ctl_io *)ctsio); 8047 return (1); 8048 } 8049 8050 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8051 if (ctl_get_prkey(lun, i) != sa_res_key) 8052 continue; 8053 8054 found = 1; 8055 ctl_clr_prkey(lun, i); 8056 lun->pr_key_count--; 8057 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8058 } 8059 if (!found) { 8060 mtx_unlock(&lun->lun_lock); 8061 free(ctsio->kern_data_ptr, M_CTL); 8062 ctl_set_reservation_conflict(ctsio); 8063 ctl_done((union ctl_io *)ctsio); 8064 return (CTL_RETVAL_COMPLETE); 8065 } 8066 /* send msg to other side */ 8067 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8068 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8069 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8070 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8071 persis_io.pr.pr_info.res_type = type; 8072 memcpy(persis_io.pr.pr_info.sa_res_key, 8073 param->serv_act_res_key, 8074 sizeof(param->serv_act_res_key)); 8075 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8076 &persis_io, sizeof(persis_io), 0)) > 8077 CTL_HA_STATUS_SUCCESS) { 8078 printf("CTL:Persis Out error returned from " 8079 "ctl_ha_msg_send %d\n", isc_retval); 8080 } 8081 } else { 8082 /* Reserved but not all registrants */ 8083 /* sa_res_key is res holder */ 8084 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 8085 /* validate scope and type */ 8086 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8087 SPR_LU_SCOPE) { 8088 mtx_unlock(&lun->lun_lock); 8089 ctl_set_invalid_field(/*ctsio*/ ctsio, 8090 /*sks_valid*/ 1, 8091 /*command*/ 1, 8092 /*field*/ 2, 8093 /*bit_valid*/ 1, 8094 /*bit*/ 4); 8095 ctl_done((union ctl_io *)ctsio); 8096 return (1); 8097 } 8098 8099 if (type>8 || type==2 || type==4 || type==0) { 8100 mtx_unlock(&lun->lun_lock); 8101 ctl_set_invalid_field(/*ctsio*/ ctsio, 8102 /*sks_valid*/ 1, 8103 /*command*/ 1, 8104 /*field*/ 2, 8105 /*bit_valid*/ 1, 8106 /*bit*/ 0); 8107 ctl_done((union ctl_io *)ctsio); 8108 return (1); 8109 } 8110 8111 /* 8112 * Do the following: 8113 * if sa_res_key != res_key remove all 8114 * registrants w/sa_res_key and generate UA 8115 * for these registrants(Registrations 8116 * Preempted) if it wasn't an exclusive 8117 * reservation generate UA(Reservations 8118 * Preempted) for all other registered nexuses 8119 * if the type has changed. Establish the new 8120 * reservation and holder. If res_key and 8121 * sa_res_key are the same do the above 8122 * except don't unregister the res holder. 8123 */ 8124 8125 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8126 if (i == residx || ctl_get_prkey(lun, i) == 0) 8127 continue; 8128 8129 if (sa_res_key == ctl_get_prkey(lun, i)) { 8130 ctl_clr_prkey(lun, i); 8131 lun->pr_key_count--; 8132 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8133 } else if (type != lun->res_type 8134 && (lun->res_type == SPR_TYPE_WR_EX_RO 8135 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8136 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 8137 } 8138 } 8139 lun->res_type = type; 8140 if (lun->res_type != SPR_TYPE_WR_EX_AR 8141 && lun->res_type != SPR_TYPE_EX_AC_AR) 8142 lun->pr_res_idx = residx; 8143 else 8144 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8145 8146 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8147 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8148 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8149 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8150 persis_io.pr.pr_info.res_type = type; 8151 memcpy(persis_io.pr.pr_info.sa_res_key, 8152 param->serv_act_res_key, 8153 sizeof(param->serv_act_res_key)); 8154 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8155 &persis_io, sizeof(persis_io), 0)) > 8156 CTL_HA_STATUS_SUCCESS) { 8157 printf("CTL:Persis Out error returned " 8158 "from ctl_ha_msg_send %d\n", 8159 isc_retval); 8160 } 8161 } else { 8162 /* 8163 * sa_res_key is not the res holder just 8164 * remove registrants 8165 */ 8166 int found=0; 8167 8168 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8169 if (sa_res_key != ctl_get_prkey(lun, i)) 8170 continue; 8171 8172 found = 1; 8173 ctl_clr_prkey(lun, i); 8174 lun->pr_key_count--; 8175 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8176 } 8177 8178 if (!found) { 8179 mtx_unlock(&lun->lun_lock); 8180 free(ctsio->kern_data_ptr, M_CTL); 8181 ctl_set_reservation_conflict(ctsio); 8182 ctl_done((union ctl_io *)ctsio); 8183 return (1); 8184 } 8185 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8186 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8187 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8188 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8189 persis_io.pr.pr_info.res_type = type; 8190 memcpy(persis_io.pr.pr_info.sa_res_key, 8191 param->serv_act_res_key, 8192 sizeof(param->serv_act_res_key)); 8193 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8194 &persis_io, sizeof(persis_io), 0)) > 8195 CTL_HA_STATUS_SUCCESS) { 8196 printf("CTL:Persis Out error returned " 8197 "from ctl_ha_msg_send %d\n", 8198 isc_retval); 8199 } 8200 } 8201 } 8202 8203 lun->PRGeneration++; 8204 mtx_unlock(&lun->lun_lock); 8205 8206 return (retval); 8207} 8208 8209static void 8210ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8211{ 8212 uint64_t sa_res_key; 8213 int i; 8214 8215 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8216 8217 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8218 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8219 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8220 if (sa_res_key == 0) { 8221 /* 8222 * Unregister everybody else and build UA for 8223 * them 8224 */ 8225 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8226 if (i == msg->pr.pr_info.residx || 8227 ctl_get_prkey(lun, i) == 0) 8228 continue; 8229 8230 ctl_clr_prkey(lun, i); 8231 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8232 } 8233 8234 lun->pr_key_count = 1; 8235 lun->res_type = msg->pr.pr_info.res_type; 8236 if (lun->res_type != SPR_TYPE_WR_EX_AR 8237 && lun->res_type != SPR_TYPE_EX_AC_AR) 8238 lun->pr_res_idx = msg->pr.pr_info.residx; 8239 } else { 8240 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8241 if (sa_res_key == ctl_get_prkey(lun, i)) 8242 continue; 8243 8244 ctl_clr_prkey(lun, i); 8245 lun->pr_key_count--; 8246 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8247 } 8248 } 8249 } else { 8250 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8251 if (i == msg->pr.pr_info.residx || 8252 ctl_get_prkey(lun, i) == 0) 8253 continue; 8254 8255 if (sa_res_key == ctl_get_prkey(lun, i)) { 8256 ctl_clr_prkey(lun, i); 8257 lun->pr_key_count--; 8258 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8259 } else if (msg->pr.pr_info.res_type != lun->res_type 8260 && (lun->res_type == SPR_TYPE_WR_EX_RO 8261 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8262 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 8263 } 8264 } 8265 lun->res_type = msg->pr.pr_info.res_type; 8266 if (lun->res_type != SPR_TYPE_WR_EX_AR 8267 && lun->res_type != SPR_TYPE_EX_AC_AR) 8268 lun->pr_res_idx = msg->pr.pr_info.residx; 8269 else 8270 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8271 } 8272 lun->PRGeneration++; 8273 8274} 8275 8276 8277int 8278ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8279{ 8280 int retval; 8281 int isc_retval; 8282 u_int32_t param_len; 8283 struct scsi_per_res_out *cdb; 8284 struct ctl_lun *lun; 8285 struct scsi_per_res_out_parms* param; 8286 struct ctl_softc *softc; 8287 uint32_t residx; 8288 uint64_t res_key, sa_res_key, key; 8289 uint8_t type; 8290 union ctl_ha_msg persis_io; 8291 int i; 8292 8293 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8294 8295 retval = CTL_RETVAL_COMPLETE; 8296 8297 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8298 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8299 softc = lun->ctl_softc; 8300 8301 /* 8302 * We only support whole-LUN scope. The scope & type are ignored for 8303 * register, register and ignore existing key and clear. 8304 * We sometimes ignore scope and type on preempts too!! 8305 * Verify reservation type here as well. 8306 */ 8307 type = cdb->scope_type & SPR_TYPE_MASK; 8308 if ((cdb->action == SPRO_RESERVE) 8309 || (cdb->action == SPRO_RELEASE)) { 8310 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8311 ctl_set_invalid_field(/*ctsio*/ ctsio, 8312 /*sks_valid*/ 1, 8313 /*command*/ 1, 8314 /*field*/ 2, 8315 /*bit_valid*/ 1, 8316 /*bit*/ 4); 8317 ctl_done((union ctl_io *)ctsio); 8318 return (CTL_RETVAL_COMPLETE); 8319 } 8320 8321 if (type>8 || type==2 || type==4 || type==0) { 8322 ctl_set_invalid_field(/*ctsio*/ ctsio, 8323 /*sks_valid*/ 1, 8324 /*command*/ 1, 8325 /*field*/ 2, 8326 /*bit_valid*/ 1, 8327 /*bit*/ 0); 8328 ctl_done((union ctl_io *)ctsio); 8329 return (CTL_RETVAL_COMPLETE); 8330 } 8331 } 8332 8333 param_len = scsi_4btoul(cdb->length); 8334 8335 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8336 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8337 ctsio->kern_data_len = param_len; 8338 ctsio->kern_total_len = param_len; 8339 ctsio->kern_data_resid = 0; 8340 ctsio->kern_rel_offset = 0; 8341 ctsio->kern_sg_entries = 0; 8342 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8343 ctsio->be_move_done = ctl_config_move_done; 8344 ctl_datamove((union ctl_io *)ctsio); 8345 8346 return (CTL_RETVAL_COMPLETE); 8347 } 8348 8349 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8350 8351 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8352 res_key = scsi_8btou64(param->res_key.key); 8353 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8354 8355 /* 8356 * Validate the reservation key here except for SPRO_REG_IGNO 8357 * This must be done for all other service actions 8358 */ 8359 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8360 mtx_lock(&lun->lun_lock); 8361 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8362 if (res_key != key) { 8363 /* 8364 * The current key passed in doesn't match 8365 * the one the initiator previously 8366 * registered. 8367 */ 8368 mtx_unlock(&lun->lun_lock); 8369 free(ctsio->kern_data_ptr, M_CTL); 8370 ctl_set_reservation_conflict(ctsio); 8371 ctl_done((union ctl_io *)ctsio); 8372 return (CTL_RETVAL_COMPLETE); 8373 } 8374 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8375 /* 8376 * We are not registered 8377 */ 8378 mtx_unlock(&lun->lun_lock); 8379 free(ctsio->kern_data_ptr, M_CTL); 8380 ctl_set_reservation_conflict(ctsio); 8381 ctl_done((union ctl_io *)ctsio); 8382 return (CTL_RETVAL_COMPLETE); 8383 } else if (res_key != 0) { 8384 /* 8385 * We are not registered and trying to register but 8386 * the register key isn't zero. 8387 */ 8388 mtx_unlock(&lun->lun_lock); 8389 free(ctsio->kern_data_ptr, M_CTL); 8390 ctl_set_reservation_conflict(ctsio); 8391 ctl_done((union ctl_io *)ctsio); 8392 return (CTL_RETVAL_COMPLETE); 8393 } 8394 mtx_unlock(&lun->lun_lock); 8395 } 8396 8397 switch (cdb->action & SPRO_ACTION_MASK) { 8398 case SPRO_REGISTER: 8399 case SPRO_REG_IGNO: { 8400 8401#if 0 8402 printf("Registration received\n"); 8403#endif 8404 8405 /* 8406 * We don't support any of these options, as we report in 8407 * the read capabilities request (see 8408 * ctl_persistent_reserve_in(), above). 8409 */ 8410 if ((param->flags & SPR_SPEC_I_PT) 8411 || (param->flags & SPR_ALL_TG_PT) 8412 || (param->flags & SPR_APTPL)) { 8413 int bit_ptr; 8414 8415 if (param->flags & SPR_APTPL) 8416 bit_ptr = 0; 8417 else if (param->flags & SPR_ALL_TG_PT) 8418 bit_ptr = 2; 8419 else /* SPR_SPEC_I_PT */ 8420 bit_ptr = 3; 8421 8422 free(ctsio->kern_data_ptr, M_CTL); 8423 ctl_set_invalid_field(ctsio, 8424 /*sks_valid*/ 1, 8425 /*command*/ 0, 8426 /*field*/ 20, 8427 /*bit_valid*/ 1, 8428 /*bit*/ bit_ptr); 8429 ctl_done((union ctl_io *)ctsio); 8430 return (CTL_RETVAL_COMPLETE); 8431 } 8432 8433 mtx_lock(&lun->lun_lock); 8434 8435 /* 8436 * The initiator wants to clear the 8437 * key/unregister. 8438 */ 8439 if (sa_res_key == 0) { 8440 if ((res_key == 0 8441 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8442 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8443 && ctl_get_prkey(lun, residx) == 0)) { 8444 mtx_unlock(&lun->lun_lock); 8445 goto done; 8446 } 8447 8448 ctl_clr_prkey(lun, residx); 8449 lun->pr_key_count--; 8450 8451 if (residx == lun->pr_res_idx) { 8452 lun->flags &= ~CTL_LUN_PR_RESERVED; 8453 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8454 8455 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8456 || lun->res_type == SPR_TYPE_EX_AC_RO) 8457 && lun->pr_key_count) { 8458 /* 8459 * If the reservation is a registrants 8460 * only type we need to generate a UA 8461 * for other registered inits. The 8462 * sense code should be RESERVATIONS 8463 * RELEASED 8464 */ 8465 8466 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8467 if (ctl_get_prkey(lun, i + 8468 softc->persis_offset) == 0) 8469 continue; 8470 ctl_est_ua(lun, i, 8471 CTL_UA_RES_RELEASE); 8472 } 8473 } 8474 lun->res_type = 0; 8475 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8476 if (lun->pr_key_count==0) { 8477 lun->flags &= ~CTL_LUN_PR_RESERVED; 8478 lun->res_type = 0; 8479 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8480 } 8481 } 8482 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8483 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8484 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8485 persis_io.pr.pr_info.residx = residx; 8486 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8487 &persis_io, sizeof(persis_io), 0 )) > 8488 CTL_HA_STATUS_SUCCESS) { 8489 printf("CTL:Persis Out error returned from " 8490 "ctl_ha_msg_send %d\n", isc_retval); 8491 } 8492 } else /* sa_res_key != 0 */ { 8493 8494 /* 8495 * If we aren't registered currently then increment 8496 * the key count and set the registered flag. 8497 */ 8498 ctl_alloc_prkey(lun, residx); 8499 if (ctl_get_prkey(lun, residx) == 0) 8500 lun->pr_key_count++; 8501 ctl_set_prkey(lun, residx, sa_res_key); 8502 8503 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8504 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8505 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8506 persis_io.pr.pr_info.residx = residx; 8507 memcpy(persis_io.pr.pr_info.sa_res_key, 8508 param->serv_act_res_key, 8509 sizeof(param->serv_act_res_key)); 8510 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8511 &persis_io, sizeof(persis_io), 0)) > 8512 CTL_HA_STATUS_SUCCESS) { 8513 printf("CTL:Persis Out error returned from " 8514 "ctl_ha_msg_send %d\n", isc_retval); 8515 } 8516 } 8517 lun->PRGeneration++; 8518 mtx_unlock(&lun->lun_lock); 8519 8520 break; 8521 } 8522 case SPRO_RESERVE: 8523#if 0 8524 printf("Reserve executed type %d\n", type); 8525#endif 8526 mtx_lock(&lun->lun_lock); 8527 if (lun->flags & CTL_LUN_PR_RESERVED) { 8528 /* 8529 * if this isn't the reservation holder and it's 8530 * not a "all registrants" type or if the type is 8531 * different then we have a conflict 8532 */ 8533 if ((lun->pr_res_idx != residx 8534 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8535 || lun->res_type != type) { 8536 mtx_unlock(&lun->lun_lock); 8537 free(ctsio->kern_data_ptr, M_CTL); 8538 ctl_set_reservation_conflict(ctsio); 8539 ctl_done((union ctl_io *)ctsio); 8540 return (CTL_RETVAL_COMPLETE); 8541 } 8542 mtx_unlock(&lun->lun_lock); 8543 } else /* create a reservation */ { 8544 /* 8545 * If it's not an "all registrants" type record 8546 * reservation holder 8547 */ 8548 if (type != SPR_TYPE_WR_EX_AR 8549 && type != SPR_TYPE_EX_AC_AR) 8550 lun->pr_res_idx = residx; /* Res holder */ 8551 else 8552 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8553 8554 lun->flags |= CTL_LUN_PR_RESERVED; 8555 lun->res_type = type; 8556 8557 mtx_unlock(&lun->lun_lock); 8558 8559 /* send msg to other side */ 8560 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8561 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8562 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8563 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8564 persis_io.pr.pr_info.res_type = type; 8565 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8566 &persis_io, sizeof(persis_io), 0)) > 8567 CTL_HA_STATUS_SUCCESS) { 8568 printf("CTL:Persis Out error returned from " 8569 "ctl_ha_msg_send %d\n", isc_retval); 8570 } 8571 } 8572 break; 8573 8574 case SPRO_RELEASE: 8575 mtx_lock(&lun->lun_lock); 8576 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8577 /* No reservation exists return good status */ 8578 mtx_unlock(&lun->lun_lock); 8579 goto done; 8580 } 8581 /* 8582 * Is this nexus a reservation holder? 8583 */ 8584 if (lun->pr_res_idx != residx 8585 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8586 /* 8587 * not a res holder return good status but 8588 * do nothing 8589 */ 8590 mtx_unlock(&lun->lun_lock); 8591 goto done; 8592 } 8593 8594 if (lun->res_type != type) { 8595 mtx_unlock(&lun->lun_lock); 8596 free(ctsio->kern_data_ptr, M_CTL); 8597 ctl_set_illegal_pr_release(ctsio); 8598 ctl_done((union ctl_io *)ctsio); 8599 return (CTL_RETVAL_COMPLETE); 8600 } 8601 8602 /* okay to release */ 8603 lun->flags &= ~CTL_LUN_PR_RESERVED; 8604 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8605 lun->res_type = 0; 8606 8607 /* 8608 * if this isn't an exclusive access 8609 * res generate UA for all other 8610 * registrants. 8611 */ 8612 if (type != SPR_TYPE_EX_AC 8613 && type != SPR_TYPE_WR_EX) { 8614 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8615 if (i == residx || 8616 ctl_get_prkey(lun, 8617 i + softc->persis_offset) == 0) 8618 continue; 8619 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8620 } 8621 } 8622 mtx_unlock(&lun->lun_lock); 8623 /* Send msg to other side */ 8624 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8625 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8626 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8627 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8628 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8629 printf("CTL:Persis Out error returned from " 8630 "ctl_ha_msg_send %d\n", isc_retval); 8631 } 8632 break; 8633 8634 case SPRO_CLEAR: 8635 /* send msg to other side */ 8636 8637 mtx_lock(&lun->lun_lock); 8638 lun->flags &= ~CTL_LUN_PR_RESERVED; 8639 lun->res_type = 0; 8640 lun->pr_key_count = 0; 8641 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8642 8643 ctl_clr_prkey(lun, residx); 8644 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8645 if (ctl_get_prkey(lun, i) != 0) { 8646 ctl_clr_prkey(lun, i); 8647 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8648 } 8649 lun->PRGeneration++; 8650 mtx_unlock(&lun->lun_lock); 8651 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8652 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8653 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8654 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8655 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8656 printf("CTL:Persis Out error returned from " 8657 "ctl_ha_msg_send %d\n", isc_retval); 8658 } 8659 break; 8660 8661 case SPRO_PREEMPT: 8662 case SPRO_PRE_ABO: { 8663 int nretval; 8664 8665 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8666 residx, ctsio, cdb, param); 8667 if (nretval != 0) 8668 return (CTL_RETVAL_COMPLETE); 8669 break; 8670 } 8671 default: 8672 panic("Invalid PR type %x", cdb->action); 8673 } 8674 8675done: 8676 free(ctsio->kern_data_ptr, M_CTL); 8677 ctl_set_success(ctsio); 8678 ctl_done((union ctl_io *)ctsio); 8679 8680 return (retval); 8681} 8682 8683/* 8684 * This routine is for handling a message from the other SC pertaining to 8685 * persistent reserve out. All the error checking will have been done 8686 * so only perorming the action need be done here to keep the two 8687 * in sync. 8688 */ 8689static void 8690ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8691{ 8692 struct ctl_lun *lun; 8693 struct ctl_softc *softc; 8694 int i; 8695 uint32_t targ_lun; 8696 8697 softc = control_softc; 8698 8699 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8700 lun = softc->ctl_luns[targ_lun]; 8701 mtx_lock(&lun->lun_lock); 8702 switch(msg->pr.pr_info.action) { 8703 case CTL_PR_REG_KEY: 8704 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8705 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8706 lun->pr_key_count++; 8707 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8708 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8709 lun->PRGeneration++; 8710 break; 8711 8712 case CTL_PR_UNREG_KEY: 8713 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8714 lun->pr_key_count--; 8715 8716 /* XXX Need to see if the reservation has been released */ 8717 /* if so do we need to generate UA? */ 8718 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8719 lun->flags &= ~CTL_LUN_PR_RESERVED; 8720 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8721 8722 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8723 || lun->res_type == SPR_TYPE_EX_AC_RO) 8724 && lun->pr_key_count) { 8725 /* 8726 * If the reservation is a registrants 8727 * only type we need to generate a UA 8728 * for other registered inits. The 8729 * sense code should be RESERVATIONS 8730 * RELEASED 8731 */ 8732 8733 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8734 if (ctl_get_prkey(lun, i + 8735 softc->persis_offset) == 0) 8736 continue; 8737 8738 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8739 } 8740 } 8741 lun->res_type = 0; 8742 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8743 if (lun->pr_key_count==0) { 8744 lun->flags &= ~CTL_LUN_PR_RESERVED; 8745 lun->res_type = 0; 8746 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8747 } 8748 } 8749 lun->PRGeneration++; 8750 break; 8751 8752 case CTL_PR_RESERVE: 8753 lun->flags |= CTL_LUN_PR_RESERVED; 8754 lun->res_type = msg->pr.pr_info.res_type; 8755 lun->pr_res_idx = msg->pr.pr_info.residx; 8756 8757 break; 8758 8759 case CTL_PR_RELEASE: 8760 /* 8761 * if this isn't an exclusive access res generate UA for all 8762 * other registrants. 8763 */ 8764 if (lun->res_type != SPR_TYPE_EX_AC 8765 && lun->res_type != SPR_TYPE_WR_EX) { 8766 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8767 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0) 8768 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8769 } 8770 8771 lun->flags &= ~CTL_LUN_PR_RESERVED; 8772 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8773 lun->res_type = 0; 8774 break; 8775 8776 case CTL_PR_PREEMPT: 8777 ctl_pro_preempt_other(lun, msg); 8778 break; 8779 case CTL_PR_CLEAR: 8780 lun->flags &= ~CTL_LUN_PR_RESERVED; 8781 lun->res_type = 0; 8782 lun->pr_key_count = 0; 8783 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8784 8785 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8786 if (ctl_get_prkey(lun, i) == 0) 8787 continue; 8788 ctl_clr_prkey(lun, i); 8789 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8790 } 8791 lun->PRGeneration++; 8792 break; 8793 } 8794 8795 mtx_unlock(&lun->lun_lock); 8796} 8797 8798int 8799ctl_read_write(struct ctl_scsiio *ctsio) 8800{ 8801 struct ctl_lun *lun; 8802 struct ctl_lba_len_flags *lbalen; 8803 uint64_t lba; 8804 uint32_t num_blocks; 8805 int flags, retval; 8806 int isread; 8807 8808 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8809 8810 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8811 8812 flags = 0; 8813 retval = CTL_RETVAL_COMPLETE; 8814 8815 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8816 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8817 switch (ctsio->cdb[0]) { 8818 case READ_6: 8819 case WRITE_6: { 8820 struct scsi_rw_6 *cdb; 8821 8822 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8823 8824 lba = scsi_3btoul(cdb->addr); 8825 /* only 5 bits are valid in the most significant address byte */ 8826 lba &= 0x1fffff; 8827 num_blocks = cdb->length; 8828 /* 8829 * This is correct according to SBC-2. 8830 */ 8831 if (num_blocks == 0) 8832 num_blocks = 256; 8833 break; 8834 } 8835 case READ_10: 8836 case WRITE_10: { 8837 struct scsi_rw_10 *cdb; 8838 8839 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8840 if (cdb->byte2 & SRW10_FUA) 8841 flags |= CTL_LLF_FUA; 8842 if (cdb->byte2 & SRW10_DPO) 8843 flags |= CTL_LLF_DPO; 8844 lba = scsi_4btoul(cdb->addr); 8845 num_blocks = scsi_2btoul(cdb->length); 8846 break; 8847 } 8848 case WRITE_VERIFY_10: { 8849 struct scsi_write_verify_10 *cdb; 8850 8851 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8852 flags |= CTL_LLF_FUA; 8853 if (cdb->byte2 & SWV_DPO) 8854 flags |= CTL_LLF_DPO; 8855 lba = scsi_4btoul(cdb->addr); 8856 num_blocks = scsi_2btoul(cdb->length); 8857 break; 8858 } 8859 case READ_12: 8860 case WRITE_12: { 8861 struct scsi_rw_12 *cdb; 8862 8863 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8864 if (cdb->byte2 & SRW12_FUA) 8865 flags |= CTL_LLF_FUA; 8866 if (cdb->byte2 & SRW12_DPO) 8867 flags |= CTL_LLF_DPO; 8868 lba = scsi_4btoul(cdb->addr); 8869 num_blocks = scsi_4btoul(cdb->length); 8870 break; 8871 } 8872 case WRITE_VERIFY_12: { 8873 struct scsi_write_verify_12 *cdb; 8874 8875 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8876 flags |= CTL_LLF_FUA; 8877 if (cdb->byte2 & SWV_DPO) 8878 flags |= CTL_LLF_DPO; 8879 lba = scsi_4btoul(cdb->addr); 8880 num_blocks = scsi_4btoul(cdb->length); 8881 break; 8882 } 8883 case READ_16: 8884 case WRITE_16: { 8885 struct scsi_rw_16 *cdb; 8886 8887 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8888 if (cdb->byte2 & SRW12_FUA) 8889 flags |= CTL_LLF_FUA; 8890 if (cdb->byte2 & SRW12_DPO) 8891 flags |= CTL_LLF_DPO; 8892 lba = scsi_8btou64(cdb->addr); 8893 num_blocks = scsi_4btoul(cdb->length); 8894 break; 8895 } 8896 case WRITE_ATOMIC_16: { 8897 struct scsi_rw_16 *cdb; 8898 8899 if (lun->be_lun->atomicblock == 0) { 8900 ctl_set_invalid_opcode(ctsio); 8901 ctl_done((union ctl_io *)ctsio); 8902 return (CTL_RETVAL_COMPLETE); 8903 } 8904 8905 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8906 if (cdb->byte2 & SRW12_FUA) 8907 flags |= CTL_LLF_FUA; 8908 if (cdb->byte2 & SRW12_DPO) 8909 flags |= CTL_LLF_DPO; 8910 lba = scsi_8btou64(cdb->addr); 8911 num_blocks = scsi_4btoul(cdb->length); 8912 if (num_blocks > lun->be_lun->atomicblock) { 8913 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8914 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8915 /*bit*/ 0); 8916 ctl_done((union ctl_io *)ctsio); 8917 return (CTL_RETVAL_COMPLETE); 8918 } 8919 break; 8920 } 8921 case WRITE_VERIFY_16: { 8922 struct scsi_write_verify_16 *cdb; 8923 8924 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8925 flags |= CTL_LLF_FUA; 8926 if (cdb->byte2 & SWV_DPO) 8927 flags |= CTL_LLF_DPO; 8928 lba = scsi_8btou64(cdb->addr); 8929 num_blocks = scsi_4btoul(cdb->length); 8930 break; 8931 } 8932 default: 8933 /* 8934 * We got a command we don't support. This shouldn't 8935 * happen, commands should be filtered out above us. 8936 */ 8937 ctl_set_invalid_opcode(ctsio); 8938 ctl_done((union ctl_io *)ctsio); 8939 8940 return (CTL_RETVAL_COMPLETE); 8941 break; /* NOTREACHED */ 8942 } 8943 8944 /* 8945 * The first check is to make sure we're in bounds, the second 8946 * check is to catch wrap-around problems. If the lba + num blocks 8947 * is less than the lba, then we've wrapped around and the block 8948 * range is invalid anyway. 8949 */ 8950 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8951 || ((lba + num_blocks) < lba)) { 8952 ctl_set_lba_out_of_range(ctsio); 8953 ctl_done((union ctl_io *)ctsio); 8954 return (CTL_RETVAL_COMPLETE); 8955 } 8956 8957 /* 8958 * According to SBC-3, a transfer length of 0 is not an error. 8959 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8960 * translates to 256 blocks for those commands. 8961 */ 8962 if (num_blocks == 0) { 8963 ctl_set_success(ctsio); 8964 ctl_done((union ctl_io *)ctsio); 8965 return (CTL_RETVAL_COMPLETE); 8966 } 8967 8968 /* Set FUA and/or DPO if caches are disabled. */ 8969 if (isread) { 8970 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8971 SCP_RCD) != 0) 8972 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8973 } else { 8974 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8975 SCP_WCE) == 0) 8976 flags |= CTL_LLF_FUA; 8977 } 8978 8979 lbalen = (struct ctl_lba_len_flags *) 8980 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8981 lbalen->lba = lba; 8982 lbalen->len = num_blocks; 8983 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8984 8985 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8986 ctsio->kern_rel_offset = 0; 8987 8988 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8989 8990 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8991 8992 return (retval); 8993} 8994 8995static int 8996ctl_cnw_cont(union ctl_io *io) 8997{ 8998 struct ctl_scsiio *ctsio; 8999 struct ctl_lun *lun; 9000 struct ctl_lba_len_flags *lbalen; 9001 int retval; 9002 9003 ctsio = &io->scsiio; 9004 ctsio->io_hdr.status = CTL_STATUS_NONE; 9005 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9006 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9007 lbalen = (struct ctl_lba_len_flags *) 9008 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9009 lbalen->flags &= ~CTL_LLF_COMPARE; 9010 lbalen->flags |= CTL_LLF_WRITE; 9011 9012 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9013 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9014 return (retval); 9015} 9016 9017int 9018ctl_cnw(struct ctl_scsiio *ctsio) 9019{ 9020 struct ctl_lun *lun; 9021 struct ctl_lba_len_flags *lbalen; 9022 uint64_t lba; 9023 uint32_t num_blocks; 9024 int flags, retval; 9025 9026 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9027 9028 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9029 9030 flags = 0; 9031 retval = CTL_RETVAL_COMPLETE; 9032 9033 switch (ctsio->cdb[0]) { 9034 case COMPARE_AND_WRITE: { 9035 struct scsi_compare_and_write *cdb; 9036 9037 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9038 if (cdb->byte2 & SRW10_FUA) 9039 flags |= CTL_LLF_FUA; 9040 if (cdb->byte2 & SRW10_DPO) 9041 flags |= CTL_LLF_DPO; 9042 lba = scsi_8btou64(cdb->addr); 9043 num_blocks = cdb->length; 9044 break; 9045 } 9046 default: 9047 /* 9048 * We got a command we don't support. This shouldn't 9049 * happen, commands should be filtered out above us. 9050 */ 9051 ctl_set_invalid_opcode(ctsio); 9052 ctl_done((union ctl_io *)ctsio); 9053 9054 return (CTL_RETVAL_COMPLETE); 9055 break; /* NOTREACHED */ 9056 } 9057 9058 /* 9059 * The first check is to make sure we're in bounds, the second 9060 * check is to catch wrap-around problems. If the lba + num blocks 9061 * is less than the lba, then we've wrapped around and the block 9062 * range is invalid anyway. 9063 */ 9064 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9065 || ((lba + num_blocks) < lba)) { 9066 ctl_set_lba_out_of_range(ctsio); 9067 ctl_done((union ctl_io *)ctsio); 9068 return (CTL_RETVAL_COMPLETE); 9069 } 9070 9071 /* 9072 * According to SBC-3, a transfer length of 0 is not an error. 9073 */ 9074 if (num_blocks == 0) { 9075 ctl_set_success(ctsio); 9076 ctl_done((union ctl_io *)ctsio); 9077 return (CTL_RETVAL_COMPLETE); 9078 } 9079 9080 /* Set FUA if write cache is disabled. */ 9081 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9082 SCP_WCE) == 0) 9083 flags |= CTL_LLF_FUA; 9084 9085 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9086 ctsio->kern_rel_offset = 0; 9087 9088 /* 9089 * Set the IO_CONT flag, so that if this I/O gets passed to 9090 * ctl_data_submit_done(), it'll get passed back to 9091 * ctl_ctl_cnw_cont() for further processing. 9092 */ 9093 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9094 ctsio->io_cont = ctl_cnw_cont; 9095 9096 lbalen = (struct ctl_lba_len_flags *) 9097 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9098 lbalen->lba = lba; 9099 lbalen->len = num_blocks; 9100 lbalen->flags = CTL_LLF_COMPARE | flags; 9101 9102 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9103 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9104 return (retval); 9105} 9106 9107int 9108ctl_verify(struct ctl_scsiio *ctsio) 9109{ 9110 struct ctl_lun *lun; 9111 struct ctl_lba_len_flags *lbalen; 9112 uint64_t lba; 9113 uint32_t num_blocks; 9114 int bytchk, flags; 9115 int retval; 9116 9117 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9118 9119 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9120 9121 bytchk = 0; 9122 flags = CTL_LLF_FUA; 9123 retval = CTL_RETVAL_COMPLETE; 9124 9125 switch (ctsio->cdb[0]) { 9126 case VERIFY_10: { 9127 struct scsi_verify_10 *cdb; 9128 9129 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9130 if (cdb->byte2 & SVFY_BYTCHK) 9131 bytchk = 1; 9132 if (cdb->byte2 & SVFY_DPO) 9133 flags |= CTL_LLF_DPO; 9134 lba = scsi_4btoul(cdb->addr); 9135 num_blocks = scsi_2btoul(cdb->length); 9136 break; 9137 } 9138 case VERIFY_12: { 9139 struct scsi_verify_12 *cdb; 9140 9141 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9142 if (cdb->byte2 & SVFY_BYTCHK) 9143 bytchk = 1; 9144 if (cdb->byte2 & SVFY_DPO) 9145 flags |= CTL_LLF_DPO; 9146 lba = scsi_4btoul(cdb->addr); 9147 num_blocks = scsi_4btoul(cdb->length); 9148 break; 9149 } 9150 case VERIFY_16: { 9151 struct scsi_rw_16 *cdb; 9152 9153 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9154 if (cdb->byte2 & SVFY_BYTCHK) 9155 bytchk = 1; 9156 if (cdb->byte2 & SVFY_DPO) 9157 flags |= CTL_LLF_DPO; 9158 lba = scsi_8btou64(cdb->addr); 9159 num_blocks = scsi_4btoul(cdb->length); 9160 break; 9161 } 9162 default: 9163 /* 9164 * We got a command we don't support. This shouldn't 9165 * happen, commands should be filtered out above us. 9166 */ 9167 ctl_set_invalid_opcode(ctsio); 9168 ctl_done((union ctl_io *)ctsio); 9169 return (CTL_RETVAL_COMPLETE); 9170 } 9171 9172 /* 9173 * The first check is to make sure we're in bounds, the second 9174 * check is to catch wrap-around problems. If the lba + num blocks 9175 * is less than the lba, then we've wrapped around and the block 9176 * range is invalid anyway. 9177 */ 9178 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9179 || ((lba + num_blocks) < lba)) { 9180 ctl_set_lba_out_of_range(ctsio); 9181 ctl_done((union ctl_io *)ctsio); 9182 return (CTL_RETVAL_COMPLETE); 9183 } 9184 9185 /* 9186 * According to SBC-3, a transfer length of 0 is not an error. 9187 */ 9188 if (num_blocks == 0) { 9189 ctl_set_success(ctsio); 9190 ctl_done((union ctl_io *)ctsio); 9191 return (CTL_RETVAL_COMPLETE); 9192 } 9193 9194 lbalen = (struct ctl_lba_len_flags *) 9195 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9196 lbalen->lba = lba; 9197 lbalen->len = num_blocks; 9198 if (bytchk) { 9199 lbalen->flags = CTL_LLF_COMPARE | flags; 9200 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9201 } else { 9202 lbalen->flags = CTL_LLF_VERIFY | flags; 9203 ctsio->kern_total_len = 0; 9204 } 9205 ctsio->kern_rel_offset = 0; 9206 9207 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9208 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9209 return (retval); 9210} 9211 9212int 9213ctl_report_luns(struct ctl_scsiio *ctsio) 9214{ 9215 struct ctl_softc *softc = control_softc; 9216 struct scsi_report_luns *cdb; 9217 struct scsi_report_luns_data *lun_data; 9218 struct ctl_lun *lun, *request_lun; 9219 struct ctl_port *port; 9220 int num_luns, retval; 9221 uint32_t alloc_len, lun_datalen; 9222 int num_filled, well_known; 9223 uint32_t initidx, targ_lun_id, lun_id; 9224 9225 retval = CTL_RETVAL_COMPLETE; 9226 well_known = 0; 9227 9228 cdb = (struct scsi_report_luns *)ctsio->cdb; 9229 port = ctl_io_port(&ctsio->io_hdr); 9230 9231 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9232 9233 mtx_lock(&softc->ctl_lock); 9234 num_luns = 0; 9235 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 9236 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 9237 num_luns++; 9238 } 9239 mtx_unlock(&softc->ctl_lock); 9240 9241 switch (cdb->select_report) { 9242 case RPL_REPORT_DEFAULT: 9243 case RPL_REPORT_ALL: 9244 break; 9245 case RPL_REPORT_WELLKNOWN: 9246 well_known = 1; 9247 num_luns = 0; 9248 break; 9249 default: 9250 ctl_set_invalid_field(ctsio, 9251 /*sks_valid*/ 1, 9252 /*command*/ 1, 9253 /*field*/ 2, 9254 /*bit_valid*/ 0, 9255 /*bit*/ 0); 9256 ctl_done((union ctl_io *)ctsio); 9257 return (retval); 9258 break; /* NOTREACHED */ 9259 } 9260 9261 alloc_len = scsi_4btoul(cdb->length); 9262 /* 9263 * The initiator has to allocate at least 16 bytes for this request, 9264 * so he can at least get the header and the first LUN. Otherwise 9265 * we reject the request (per SPC-3 rev 14, section 6.21). 9266 */ 9267 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9268 sizeof(struct scsi_report_luns_lundata))) { 9269 ctl_set_invalid_field(ctsio, 9270 /*sks_valid*/ 1, 9271 /*command*/ 1, 9272 /*field*/ 6, 9273 /*bit_valid*/ 0, 9274 /*bit*/ 0); 9275 ctl_done((union ctl_io *)ctsio); 9276 return (retval); 9277 } 9278 9279 request_lun = (struct ctl_lun *) 9280 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9281 9282 lun_datalen = sizeof(*lun_data) + 9283 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9284 9285 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9286 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9287 ctsio->kern_sg_entries = 0; 9288 9289 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9290 9291 mtx_lock(&softc->ctl_lock); 9292 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9293 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9294 if (lun_id >= CTL_MAX_LUNS) 9295 continue; 9296 lun = softc->ctl_luns[lun_id]; 9297 if (lun == NULL) 9298 continue; 9299 9300 if (targ_lun_id <= 0xff) { 9301 /* 9302 * Peripheral addressing method, bus number 0. 9303 */ 9304 lun_data->luns[num_filled].lundata[0] = 9305 RPL_LUNDATA_ATYP_PERIPH; 9306 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9307 num_filled++; 9308 } else if (targ_lun_id <= 0x3fff) { 9309 /* 9310 * Flat addressing method. 9311 */ 9312 lun_data->luns[num_filled].lundata[0] = 9313 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9314 lun_data->luns[num_filled].lundata[1] = 9315 (targ_lun_id & 0xff); 9316 num_filled++; 9317 } else if (targ_lun_id <= 0xffffff) { 9318 /* 9319 * Extended flat addressing method. 9320 */ 9321 lun_data->luns[num_filled].lundata[0] = 9322 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9323 scsi_ulto3b(targ_lun_id, 9324 &lun_data->luns[num_filled].lundata[1]); 9325 num_filled++; 9326 } else { 9327 printf("ctl_report_luns: bogus LUN number %jd, " 9328 "skipping\n", (intmax_t)targ_lun_id); 9329 } 9330 /* 9331 * According to SPC-3, rev 14 section 6.21: 9332 * 9333 * "The execution of a REPORT LUNS command to any valid and 9334 * installed logical unit shall clear the REPORTED LUNS DATA 9335 * HAS CHANGED unit attention condition for all logical 9336 * units of that target with respect to the requesting 9337 * initiator. A valid and installed logical unit is one 9338 * having a PERIPHERAL QUALIFIER of 000b in the standard 9339 * INQUIRY data (see 6.4.2)." 9340 * 9341 * If request_lun is NULL, the LUN this report luns command 9342 * was issued to is either disabled or doesn't exist. In that 9343 * case, we shouldn't clear any pending lun change unit 9344 * attention. 9345 */ 9346 if (request_lun != NULL) { 9347 mtx_lock(&lun->lun_lock); 9348 ctl_clr_ua(lun, initidx, CTL_UA_RES_RELEASE); 9349 mtx_unlock(&lun->lun_lock); 9350 } 9351 } 9352 mtx_unlock(&softc->ctl_lock); 9353 9354 /* 9355 * It's quite possible that we've returned fewer LUNs than we allocated 9356 * space for. Trim it. 9357 */ 9358 lun_datalen = sizeof(*lun_data) + 9359 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9360 9361 if (lun_datalen < alloc_len) { 9362 ctsio->residual = alloc_len - lun_datalen; 9363 ctsio->kern_data_len = lun_datalen; 9364 ctsio->kern_total_len = lun_datalen; 9365 } else { 9366 ctsio->residual = 0; 9367 ctsio->kern_data_len = alloc_len; 9368 ctsio->kern_total_len = alloc_len; 9369 } 9370 ctsio->kern_data_resid = 0; 9371 ctsio->kern_rel_offset = 0; 9372 ctsio->kern_sg_entries = 0; 9373 9374 /* 9375 * We set this to the actual data length, regardless of how much 9376 * space we actually have to return results. If the user looks at 9377 * this value, he'll know whether or not he allocated enough space 9378 * and reissue the command if necessary. We don't support well 9379 * known logical units, so if the user asks for that, return none. 9380 */ 9381 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9382 9383 /* 9384 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9385 * this request. 9386 */ 9387 ctl_set_success(ctsio); 9388 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9389 ctsio->be_move_done = ctl_config_move_done; 9390 ctl_datamove((union ctl_io *)ctsio); 9391 return (retval); 9392} 9393 9394int 9395ctl_request_sense(struct ctl_scsiio *ctsio) 9396{ 9397 struct scsi_request_sense *cdb; 9398 struct scsi_sense_data *sense_ptr; 9399 struct ctl_softc *ctl_softc; 9400 struct ctl_lun *lun; 9401 uint32_t initidx; 9402 int have_error; 9403 scsi_sense_data_type sense_format; 9404 ctl_ua_type ua_type; 9405 9406 cdb = (struct scsi_request_sense *)ctsio->cdb; 9407 9408 ctl_softc = control_softc; 9409 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9410 9411 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9412 9413 /* 9414 * Determine which sense format the user wants. 9415 */ 9416 if (cdb->byte2 & SRS_DESC) 9417 sense_format = SSD_TYPE_DESC; 9418 else 9419 sense_format = SSD_TYPE_FIXED; 9420 9421 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9422 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9423 ctsio->kern_sg_entries = 0; 9424 9425 /* 9426 * struct scsi_sense_data, which is currently set to 256 bytes, is 9427 * larger than the largest allowed value for the length field in the 9428 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9429 */ 9430 ctsio->residual = 0; 9431 ctsio->kern_data_len = cdb->length; 9432 ctsio->kern_total_len = cdb->length; 9433 9434 ctsio->kern_data_resid = 0; 9435 ctsio->kern_rel_offset = 0; 9436 ctsio->kern_sg_entries = 0; 9437 9438 /* 9439 * If we don't have a LUN, we don't have any pending sense. 9440 */ 9441 if (lun == NULL) 9442 goto no_sense; 9443 9444 have_error = 0; 9445 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9446 /* 9447 * Check for pending sense, and then for pending unit attentions. 9448 * Pending sense gets returned first, then pending unit attentions. 9449 */ 9450 mtx_lock(&lun->lun_lock); 9451#ifdef CTL_WITH_CA 9452 if (ctl_is_set(lun->have_ca, initidx)) { 9453 scsi_sense_data_type stored_format; 9454 9455 /* 9456 * Check to see which sense format was used for the stored 9457 * sense data. 9458 */ 9459 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9460 9461 /* 9462 * If the user requested a different sense format than the 9463 * one we stored, then we need to convert it to the other 9464 * format. If we're going from descriptor to fixed format 9465 * sense data, we may lose things in translation, depending 9466 * on what options were used. 9467 * 9468 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9469 * for some reason we'll just copy it out as-is. 9470 */ 9471 if ((stored_format == SSD_TYPE_FIXED) 9472 && (sense_format == SSD_TYPE_DESC)) 9473 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9474 &lun->pending_sense[initidx], 9475 (struct scsi_sense_data_desc *)sense_ptr); 9476 else if ((stored_format == SSD_TYPE_DESC) 9477 && (sense_format == SSD_TYPE_FIXED)) 9478 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9479 &lun->pending_sense[initidx], 9480 (struct scsi_sense_data_fixed *)sense_ptr); 9481 else 9482 memcpy(sense_ptr, &lun->pending_sense[initidx], 9483 MIN(sizeof(*sense_ptr), 9484 sizeof(lun->pending_sense[initidx]))); 9485 9486 ctl_clear_mask(lun->have_ca, initidx); 9487 have_error = 1; 9488 } else 9489#endif 9490 { 9491 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9492 if (ua_type != CTL_UA_NONE) 9493 have_error = 1; 9494 if (ua_type == CTL_UA_LUN_CHANGE) { 9495 mtx_unlock(&lun->lun_lock); 9496 mtx_lock(&ctl_softc->ctl_lock); 9497 ctl_clear_ua(ctl_softc, initidx, ua_type); 9498 mtx_unlock(&ctl_softc->ctl_lock); 9499 mtx_lock(&lun->lun_lock); 9500 } 9501 9502 } 9503 mtx_unlock(&lun->lun_lock); 9504 9505 /* 9506 * We already have a pending error, return it. 9507 */ 9508 if (have_error != 0) { 9509 /* 9510 * We report the SCSI status as OK, since the status of the 9511 * request sense command itself is OK. 9512 * We report 0 for the sense length, because we aren't doing 9513 * autosense in this case. We're reporting sense as 9514 * parameter data. 9515 */ 9516 ctl_set_success(ctsio); 9517 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9518 ctsio->be_move_done = ctl_config_move_done; 9519 ctl_datamove((union ctl_io *)ctsio); 9520 return (CTL_RETVAL_COMPLETE); 9521 } 9522 9523no_sense: 9524 9525 /* 9526 * No sense information to report, so we report that everything is 9527 * okay. 9528 */ 9529 ctl_set_sense_data(sense_ptr, 9530 lun, 9531 sense_format, 9532 /*current_error*/ 1, 9533 /*sense_key*/ SSD_KEY_NO_SENSE, 9534 /*asc*/ 0x00, 9535 /*ascq*/ 0x00, 9536 SSD_ELEM_NONE); 9537 9538 /* 9539 * We report 0 for the sense length, because we aren't doing 9540 * autosense in this case. We're reporting sense as parameter data. 9541 */ 9542 ctl_set_success(ctsio); 9543 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9544 ctsio->be_move_done = ctl_config_move_done; 9545 ctl_datamove((union ctl_io *)ctsio); 9546 return (CTL_RETVAL_COMPLETE); 9547} 9548 9549int 9550ctl_tur(struct ctl_scsiio *ctsio) 9551{ 9552 9553 CTL_DEBUG_PRINT(("ctl_tur\n")); 9554 9555 ctl_set_success(ctsio); 9556 ctl_done((union ctl_io *)ctsio); 9557 9558 return (CTL_RETVAL_COMPLETE); 9559} 9560 9561#ifdef notyet 9562static int 9563ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9564{ 9565 9566} 9567#endif 9568 9569/* 9570 * SCSI VPD page 0x00, the Supported VPD Pages page. 9571 */ 9572static int 9573ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9574{ 9575 struct scsi_vpd_supported_pages *pages; 9576 int sup_page_size; 9577 struct ctl_lun *lun; 9578 int p; 9579 9580 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9581 9582 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9583 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9584 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9585 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9586 ctsio->kern_sg_entries = 0; 9587 9588 if (sup_page_size < alloc_len) { 9589 ctsio->residual = alloc_len - sup_page_size; 9590 ctsio->kern_data_len = sup_page_size; 9591 ctsio->kern_total_len = sup_page_size; 9592 } else { 9593 ctsio->residual = 0; 9594 ctsio->kern_data_len = alloc_len; 9595 ctsio->kern_total_len = alloc_len; 9596 } 9597 ctsio->kern_data_resid = 0; 9598 ctsio->kern_rel_offset = 0; 9599 ctsio->kern_sg_entries = 0; 9600 9601 /* 9602 * The control device is always connected. The disk device, on the 9603 * other hand, may not be online all the time. Need to change this 9604 * to figure out whether the disk device is actually online or not. 9605 */ 9606 if (lun != NULL) 9607 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9608 lun->be_lun->lun_type; 9609 else 9610 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9611 9612 p = 0; 9613 /* Supported VPD pages */ 9614 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9615 /* Serial Number */ 9616 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9617 /* Device Identification */ 9618 pages->page_list[p++] = SVPD_DEVICE_ID; 9619 /* Extended INQUIRY Data */ 9620 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9621 /* Mode Page Policy */ 9622 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9623 /* SCSI Ports */ 9624 pages->page_list[p++] = SVPD_SCSI_PORTS; 9625 /* Third-party Copy */ 9626 pages->page_list[p++] = SVPD_SCSI_TPC; 9627 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9628 /* Block limits */ 9629 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9630 /* Block Device Characteristics */ 9631 pages->page_list[p++] = SVPD_BDC; 9632 /* Logical Block Provisioning */ 9633 pages->page_list[p++] = SVPD_LBP; 9634 } 9635 pages->length = p; 9636 9637 ctl_set_success(ctsio); 9638 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9639 ctsio->be_move_done = ctl_config_move_done; 9640 ctl_datamove((union ctl_io *)ctsio); 9641 return (CTL_RETVAL_COMPLETE); 9642} 9643 9644/* 9645 * SCSI VPD page 0x80, the Unit Serial Number page. 9646 */ 9647static int 9648ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9649{ 9650 struct scsi_vpd_unit_serial_number *sn_ptr; 9651 struct ctl_lun *lun; 9652 int data_len; 9653 9654 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9655 9656 data_len = 4 + CTL_SN_LEN; 9657 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9658 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9659 if (data_len < alloc_len) { 9660 ctsio->residual = alloc_len - data_len; 9661 ctsio->kern_data_len = data_len; 9662 ctsio->kern_total_len = data_len; 9663 } else { 9664 ctsio->residual = 0; 9665 ctsio->kern_data_len = alloc_len; 9666 ctsio->kern_total_len = alloc_len; 9667 } 9668 ctsio->kern_data_resid = 0; 9669 ctsio->kern_rel_offset = 0; 9670 ctsio->kern_sg_entries = 0; 9671 9672 /* 9673 * The control device is always connected. The disk device, on the 9674 * other hand, may not be online all the time. Need to change this 9675 * to figure out whether the disk device is actually online or not. 9676 */ 9677 if (lun != NULL) 9678 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9679 lun->be_lun->lun_type; 9680 else 9681 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9682 9683 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9684 sn_ptr->length = CTL_SN_LEN; 9685 /* 9686 * If we don't have a LUN, we just leave the serial number as 9687 * all spaces. 9688 */ 9689 if (lun != NULL) { 9690 strncpy((char *)sn_ptr->serial_num, 9691 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9692 } else 9693 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9694 9695 ctl_set_success(ctsio); 9696 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9697 ctsio->be_move_done = ctl_config_move_done; 9698 ctl_datamove((union ctl_io *)ctsio); 9699 return (CTL_RETVAL_COMPLETE); 9700} 9701 9702 9703/* 9704 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9705 */ 9706static int 9707ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9708{ 9709 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9710 struct ctl_lun *lun; 9711 int data_len; 9712 9713 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9714 9715 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9716 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9717 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9718 ctsio->kern_sg_entries = 0; 9719 9720 if (data_len < alloc_len) { 9721 ctsio->residual = alloc_len - data_len; 9722 ctsio->kern_data_len = data_len; 9723 ctsio->kern_total_len = data_len; 9724 } else { 9725 ctsio->residual = 0; 9726 ctsio->kern_data_len = alloc_len; 9727 ctsio->kern_total_len = alloc_len; 9728 } 9729 ctsio->kern_data_resid = 0; 9730 ctsio->kern_rel_offset = 0; 9731 ctsio->kern_sg_entries = 0; 9732 9733 /* 9734 * The control device is always connected. The disk device, on the 9735 * other hand, may not be online all the time. 9736 */ 9737 if (lun != NULL) 9738 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9739 lun->be_lun->lun_type; 9740 else 9741 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9742 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9743 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9744 /* 9745 * We support head of queue, ordered and simple tags. 9746 */ 9747 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9748 /* 9749 * Volatile cache supported. 9750 */ 9751 eid_ptr->flags3 = SVPD_EID_V_SUP; 9752 9753 /* 9754 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9755 * attention for a particular IT nexus on all LUNs once we report 9756 * it to that nexus once. This bit is required as of SPC-4. 9757 */ 9758 eid_ptr->flags4 = SVPD_EID_LUICLT; 9759 9760 /* 9761 * XXX KDM in order to correctly answer this, we would need 9762 * information from the SIM to determine how much sense data it 9763 * can send. So this would really be a path inquiry field, most 9764 * likely. This can be set to a maximum of 252 according to SPC-4, 9765 * but the hardware may or may not be able to support that much. 9766 * 0 just means that the maximum sense data length is not reported. 9767 */ 9768 eid_ptr->max_sense_length = 0; 9769 9770 ctl_set_success(ctsio); 9771 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9772 ctsio->be_move_done = ctl_config_move_done; 9773 ctl_datamove((union ctl_io *)ctsio); 9774 return (CTL_RETVAL_COMPLETE); 9775} 9776 9777static int 9778ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9779{ 9780 struct scsi_vpd_mode_page_policy *mpp_ptr; 9781 struct ctl_lun *lun; 9782 int data_len; 9783 9784 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9785 9786 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9787 sizeof(struct scsi_vpd_mode_page_policy_descr); 9788 9789 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9790 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9791 ctsio->kern_sg_entries = 0; 9792 9793 if (data_len < alloc_len) { 9794 ctsio->residual = alloc_len - data_len; 9795 ctsio->kern_data_len = data_len; 9796 ctsio->kern_total_len = data_len; 9797 } else { 9798 ctsio->residual = 0; 9799 ctsio->kern_data_len = alloc_len; 9800 ctsio->kern_total_len = alloc_len; 9801 } 9802 ctsio->kern_data_resid = 0; 9803 ctsio->kern_rel_offset = 0; 9804 ctsio->kern_sg_entries = 0; 9805 9806 /* 9807 * The control device is always connected. The disk device, on the 9808 * other hand, may not be online all the time. 9809 */ 9810 if (lun != NULL) 9811 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9812 lun->be_lun->lun_type; 9813 else 9814 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9815 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9816 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9817 mpp_ptr->descr[0].page_code = 0x3f; 9818 mpp_ptr->descr[0].subpage_code = 0xff; 9819 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9820 9821 ctl_set_success(ctsio); 9822 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9823 ctsio->be_move_done = ctl_config_move_done; 9824 ctl_datamove((union ctl_io *)ctsio); 9825 return (CTL_RETVAL_COMPLETE); 9826} 9827 9828/* 9829 * SCSI VPD page 0x83, the Device Identification page. 9830 */ 9831static int 9832ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9833{ 9834 struct scsi_vpd_device_id *devid_ptr; 9835 struct scsi_vpd_id_descriptor *desc; 9836 struct ctl_softc *softc; 9837 struct ctl_lun *lun; 9838 struct ctl_port *port; 9839 int data_len; 9840 uint8_t proto; 9841 9842 softc = control_softc; 9843 9844 port = softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9845 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9846 9847 data_len = sizeof(struct scsi_vpd_device_id) + 9848 sizeof(struct scsi_vpd_id_descriptor) + 9849 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9850 sizeof(struct scsi_vpd_id_descriptor) + 9851 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9852 if (lun && lun->lun_devid) 9853 data_len += lun->lun_devid->len; 9854 if (port->port_devid) 9855 data_len += port->port_devid->len; 9856 if (port->target_devid) 9857 data_len += port->target_devid->len; 9858 9859 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9860 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9861 ctsio->kern_sg_entries = 0; 9862 9863 if (data_len < alloc_len) { 9864 ctsio->residual = alloc_len - data_len; 9865 ctsio->kern_data_len = data_len; 9866 ctsio->kern_total_len = data_len; 9867 } else { 9868 ctsio->residual = 0; 9869 ctsio->kern_data_len = alloc_len; 9870 ctsio->kern_total_len = alloc_len; 9871 } 9872 ctsio->kern_data_resid = 0; 9873 ctsio->kern_rel_offset = 0; 9874 ctsio->kern_sg_entries = 0; 9875 9876 /* 9877 * The control device is always connected. The disk device, on the 9878 * other hand, may not be online all the time. 9879 */ 9880 if (lun != NULL) 9881 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9882 lun->be_lun->lun_type; 9883 else 9884 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9885 devid_ptr->page_code = SVPD_DEVICE_ID; 9886 scsi_ulto2b(data_len - 4, devid_ptr->length); 9887 9888 if (port->port_type == CTL_PORT_FC) 9889 proto = SCSI_PROTO_FC << 4; 9890 else if (port->port_type == CTL_PORT_ISCSI) 9891 proto = SCSI_PROTO_ISCSI << 4; 9892 else 9893 proto = SCSI_PROTO_SPI << 4; 9894 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9895 9896 /* 9897 * We're using a LUN association here. i.e., this device ID is a 9898 * per-LUN identifier. 9899 */ 9900 if (lun && lun->lun_devid) { 9901 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9902 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9903 lun->lun_devid->len); 9904 } 9905 9906 /* 9907 * This is for the WWPN which is a port association. 9908 */ 9909 if (port->port_devid) { 9910 memcpy(desc, port->port_devid->data, port->port_devid->len); 9911 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9912 port->port_devid->len); 9913 } 9914 9915 /* 9916 * This is for the Relative Target Port(type 4h) identifier 9917 */ 9918 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9919 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9920 SVPD_ID_TYPE_RELTARG; 9921 desc->length = 4; 9922 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9923 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9924 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9925 9926 /* 9927 * This is for the Target Port Group(type 5h) identifier 9928 */ 9929 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9930 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9931 SVPD_ID_TYPE_TPORTGRP; 9932 desc->length = 4; 9933 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9934 &desc->identifier[2]); 9935 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9936 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9937 9938 /* 9939 * This is for the Target identifier 9940 */ 9941 if (port->target_devid) { 9942 memcpy(desc, port->target_devid->data, port->target_devid->len); 9943 } 9944 9945 ctl_set_success(ctsio); 9946 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9947 ctsio->be_move_done = ctl_config_move_done; 9948 ctl_datamove((union ctl_io *)ctsio); 9949 return (CTL_RETVAL_COMPLETE); 9950} 9951 9952static int 9953ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9954{ 9955 struct ctl_softc *softc = control_softc; 9956 struct scsi_vpd_scsi_ports *sp; 9957 struct scsi_vpd_port_designation *pd; 9958 struct scsi_vpd_port_designation_cont *pdc; 9959 struct ctl_lun *lun; 9960 struct ctl_port *port; 9961 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 9962 int num_target_port_groups; 9963 9964 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9965 9966 if (softc->is_single) 9967 num_target_port_groups = 1; 9968 else 9969 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 9970 num_target_ports = 0; 9971 iid_len = 0; 9972 id_len = 0; 9973 mtx_lock(&softc->ctl_lock); 9974 STAILQ_FOREACH(port, &softc->port_list, links) { 9975 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9976 continue; 9977 if (lun != NULL && 9978 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9979 continue; 9980 num_target_ports++; 9981 if (port->init_devid) 9982 iid_len += port->init_devid->len; 9983 if (port->port_devid) 9984 id_len += port->port_devid->len; 9985 } 9986 mtx_unlock(&softc->ctl_lock); 9987 9988 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 9989 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9990 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9991 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9992 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9993 ctsio->kern_sg_entries = 0; 9994 9995 if (data_len < alloc_len) { 9996 ctsio->residual = alloc_len - data_len; 9997 ctsio->kern_data_len = data_len; 9998 ctsio->kern_total_len = data_len; 9999 } else { 10000 ctsio->residual = 0; 10001 ctsio->kern_data_len = alloc_len; 10002 ctsio->kern_total_len = alloc_len; 10003 } 10004 ctsio->kern_data_resid = 0; 10005 ctsio->kern_rel_offset = 0; 10006 ctsio->kern_sg_entries = 0; 10007 10008 /* 10009 * The control device is always connected. The disk device, on the 10010 * other hand, may not be online all the time. Need to change this 10011 * to figure out whether the disk device is actually online or not. 10012 */ 10013 if (lun != NULL) 10014 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10015 lun->be_lun->lun_type; 10016 else 10017 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10018 10019 sp->page_code = SVPD_SCSI_PORTS; 10020 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10021 sp->page_length); 10022 pd = &sp->design[0]; 10023 10024 mtx_lock(&softc->ctl_lock); 10025 pg = softc->port_offset / CTL_MAX_PORTS; 10026 for (g = 0; g < num_target_port_groups; g++) { 10027 STAILQ_FOREACH(port, &softc->port_list, links) { 10028 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10029 continue; 10030 if (lun != NULL && 10031 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 10032 continue; 10033 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10034 scsi_ulto2b(p, pd->relative_port_id); 10035 if (port->init_devid && g == pg) { 10036 iid_len = port->init_devid->len; 10037 memcpy(pd->initiator_transportid, 10038 port->init_devid->data, port->init_devid->len); 10039 } else 10040 iid_len = 0; 10041 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10042 pdc = (struct scsi_vpd_port_designation_cont *) 10043 (&pd->initiator_transportid[iid_len]); 10044 if (port->port_devid && g == pg) { 10045 id_len = port->port_devid->len; 10046 memcpy(pdc->target_port_descriptors, 10047 port->port_devid->data, port->port_devid->len); 10048 } else 10049 id_len = 0; 10050 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10051 pd = (struct scsi_vpd_port_designation *) 10052 ((uint8_t *)pdc->target_port_descriptors + id_len); 10053 } 10054 } 10055 mtx_unlock(&softc->ctl_lock); 10056 10057 ctl_set_success(ctsio); 10058 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10059 ctsio->be_move_done = ctl_config_move_done; 10060 ctl_datamove((union ctl_io *)ctsio); 10061 return (CTL_RETVAL_COMPLETE); 10062} 10063 10064static int 10065ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10066{ 10067 struct scsi_vpd_block_limits *bl_ptr; 10068 struct ctl_lun *lun; 10069 int bs; 10070 10071 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10072 10073 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10074 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10075 ctsio->kern_sg_entries = 0; 10076 10077 if (sizeof(*bl_ptr) < alloc_len) { 10078 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10079 ctsio->kern_data_len = sizeof(*bl_ptr); 10080 ctsio->kern_total_len = sizeof(*bl_ptr); 10081 } else { 10082 ctsio->residual = 0; 10083 ctsio->kern_data_len = alloc_len; 10084 ctsio->kern_total_len = alloc_len; 10085 } 10086 ctsio->kern_data_resid = 0; 10087 ctsio->kern_rel_offset = 0; 10088 ctsio->kern_sg_entries = 0; 10089 10090 /* 10091 * The control device is always connected. The disk device, on the 10092 * other hand, may not be online all the time. Need to change this 10093 * to figure out whether the disk device is actually online or not. 10094 */ 10095 if (lun != NULL) 10096 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10097 lun->be_lun->lun_type; 10098 else 10099 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10100 10101 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10102 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10103 bl_ptr->max_cmp_write_len = 0xff; 10104 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10105 if (lun != NULL) { 10106 bs = lun->be_lun->blocksize; 10107 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 10108 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10109 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10110 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10111 if (lun->be_lun->ublockexp != 0) { 10112 scsi_ulto4b((1 << lun->be_lun->ublockexp), 10113 bl_ptr->opt_unmap_grain); 10114 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 10115 bl_ptr->unmap_grain_align); 10116 } 10117 } 10118 scsi_ulto4b(lun->be_lun->atomicblock, 10119 bl_ptr->max_atomic_transfer_length); 10120 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10121 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10122 } 10123 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10124 10125 ctl_set_success(ctsio); 10126 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10127 ctsio->be_move_done = ctl_config_move_done; 10128 ctl_datamove((union ctl_io *)ctsio); 10129 return (CTL_RETVAL_COMPLETE); 10130} 10131 10132static int 10133ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10134{ 10135 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10136 struct ctl_lun *lun; 10137 const char *value; 10138 u_int i; 10139 10140 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10141 10142 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10143 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10144 ctsio->kern_sg_entries = 0; 10145 10146 if (sizeof(*bdc_ptr) < alloc_len) { 10147 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10148 ctsio->kern_data_len = sizeof(*bdc_ptr); 10149 ctsio->kern_total_len = sizeof(*bdc_ptr); 10150 } else { 10151 ctsio->residual = 0; 10152 ctsio->kern_data_len = alloc_len; 10153 ctsio->kern_total_len = alloc_len; 10154 } 10155 ctsio->kern_data_resid = 0; 10156 ctsio->kern_rel_offset = 0; 10157 ctsio->kern_sg_entries = 0; 10158 10159 /* 10160 * The control device is always connected. The disk device, on the 10161 * other hand, may not be online all the time. Need to change this 10162 * to figure out whether the disk device is actually online or not. 10163 */ 10164 if (lun != NULL) 10165 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10166 lun->be_lun->lun_type; 10167 else 10168 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10169 bdc_ptr->page_code = SVPD_BDC; 10170 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10171 if (lun != NULL && 10172 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10173 i = strtol(value, NULL, 0); 10174 else 10175 i = CTL_DEFAULT_ROTATION_RATE; 10176 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10177 if (lun != NULL && 10178 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10179 i = strtol(value, NULL, 0); 10180 else 10181 i = 0; 10182 bdc_ptr->wab_wac_ff = (i & 0x0f); 10183 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10184 10185 ctl_set_success(ctsio); 10186 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10187 ctsio->be_move_done = ctl_config_move_done; 10188 ctl_datamove((union ctl_io *)ctsio); 10189 return (CTL_RETVAL_COMPLETE); 10190} 10191 10192static int 10193ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10194{ 10195 struct scsi_vpd_logical_block_prov *lbp_ptr; 10196 struct ctl_lun *lun; 10197 10198 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10199 10200 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10201 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10202 ctsio->kern_sg_entries = 0; 10203 10204 if (sizeof(*lbp_ptr) < alloc_len) { 10205 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10206 ctsio->kern_data_len = sizeof(*lbp_ptr); 10207 ctsio->kern_total_len = sizeof(*lbp_ptr); 10208 } else { 10209 ctsio->residual = 0; 10210 ctsio->kern_data_len = alloc_len; 10211 ctsio->kern_total_len = alloc_len; 10212 } 10213 ctsio->kern_data_resid = 0; 10214 ctsio->kern_rel_offset = 0; 10215 ctsio->kern_sg_entries = 0; 10216 10217 /* 10218 * The control device is always connected. The disk device, on the 10219 * other hand, may not be online all the time. Need to change this 10220 * to figure out whether the disk device is actually online or not. 10221 */ 10222 if (lun != NULL) 10223 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10224 lun->be_lun->lun_type; 10225 else 10226 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10227 10228 lbp_ptr->page_code = SVPD_LBP; 10229 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10230 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10231 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10232 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10233 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10234 lbp_ptr->prov_type = SVPD_LBP_THIN; 10235 } 10236 10237 ctl_set_success(ctsio); 10238 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10239 ctsio->be_move_done = ctl_config_move_done; 10240 ctl_datamove((union ctl_io *)ctsio); 10241 return (CTL_RETVAL_COMPLETE); 10242} 10243 10244/* 10245 * INQUIRY with the EVPD bit set. 10246 */ 10247static int 10248ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10249{ 10250 struct ctl_lun *lun; 10251 struct scsi_inquiry *cdb; 10252 int alloc_len, retval; 10253 10254 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10255 cdb = (struct scsi_inquiry *)ctsio->cdb; 10256 alloc_len = scsi_2btoul(cdb->length); 10257 10258 switch (cdb->page_code) { 10259 case SVPD_SUPPORTED_PAGES: 10260 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10261 break; 10262 case SVPD_UNIT_SERIAL_NUMBER: 10263 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10264 break; 10265 case SVPD_DEVICE_ID: 10266 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10267 break; 10268 case SVPD_EXTENDED_INQUIRY_DATA: 10269 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10270 break; 10271 case SVPD_MODE_PAGE_POLICY: 10272 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10273 break; 10274 case SVPD_SCSI_PORTS: 10275 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10276 break; 10277 case SVPD_SCSI_TPC: 10278 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10279 break; 10280 case SVPD_BLOCK_LIMITS: 10281 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10282 goto err; 10283 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10284 break; 10285 case SVPD_BDC: 10286 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10287 goto err; 10288 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10289 break; 10290 case SVPD_LBP: 10291 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10292 goto err; 10293 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10294 break; 10295 default: 10296err: 10297 ctl_set_invalid_field(ctsio, 10298 /*sks_valid*/ 1, 10299 /*command*/ 1, 10300 /*field*/ 2, 10301 /*bit_valid*/ 0, 10302 /*bit*/ 0); 10303 ctl_done((union ctl_io *)ctsio); 10304 retval = CTL_RETVAL_COMPLETE; 10305 break; 10306 } 10307 10308 return (retval); 10309} 10310 10311/* 10312 * Standard INQUIRY data. 10313 */ 10314static int 10315ctl_inquiry_std(struct ctl_scsiio *ctsio) 10316{ 10317 struct scsi_inquiry_data *inq_ptr; 10318 struct scsi_inquiry *cdb; 10319 struct ctl_softc *softc; 10320 struct ctl_lun *lun; 10321 char *val; 10322 uint32_t alloc_len, data_len; 10323 ctl_port_type port_type; 10324 10325 softc = control_softc; 10326 10327 /* 10328 * Figure out whether we're talking to a Fibre Channel port or not. 10329 * We treat the ioctl front end, and any SCSI adapters, as packetized 10330 * SCSI front ends. 10331 */ 10332 port_type = softc->ctl_ports[ 10333 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10334 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10335 port_type = CTL_PORT_SCSI; 10336 10337 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10338 cdb = (struct scsi_inquiry *)ctsio->cdb; 10339 alloc_len = scsi_2btoul(cdb->length); 10340 10341 /* 10342 * We malloc the full inquiry data size here and fill it 10343 * in. If the user only asks for less, we'll give him 10344 * that much. 10345 */ 10346 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10347 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10348 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10349 ctsio->kern_sg_entries = 0; 10350 ctsio->kern_data_resid = 0; 10351 ctsio->kern_rel_offset = 0; 10352 10353 if (data_len < alloc_len) { 10354 ctsio->residual = alloc_len - data_len; 10355 ctsio->kern_data_len = data_len; 10356 ctsio->kern_total_len = data_len; 10357 } else { 10358 ctsio->residual = 0; 10359 ctsio->kern_data_len = alloc_len; 10360 ctsio->kern_total_len = alloc_len; 10361 } 10362 10363 /* 10364 * If we have a LUN configured, report it as connected. Otherwise, 10365 * report that it is offline or no device is supported, depending 10366 * on the value of inquiry_pq_no_lun. 10367 * 10368 * According to the spec (SPC-4 r34), the peripheral qualifier 10369 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10370 * 10371 * "A peripheral device having the specified peripheral device type 10372 * is not connected to this logical unit. However, the device 10373 * server is capable of supporting the specified peripheral device 10374 * type on this logical unit." 10375 * 10376 * According to the same spec, the peripheral qualifier 10377 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10378 * 10379 * "The device server is not capable of supporting a peripheral 10380 * device on this logical unit. For this peripheral qualifier the 10381 * peripheral device type shall be set to 1Fh. All other peripheral 10382 * device type values are reserved for this peripheral qualifier." 10383 * 10384 * Given the text, it would seem that we probably want to report that 10385 * the LUN is offline here. There is no LUN connected, but we can 10386 * support a LUN at the given LUN number. 10387 * 10388 * In the real world, though, it sounds like things are a little 10389 * different: 10390 * 10391 * - Linux, when presented with a LUN with the offline peripheral 10392 * qualifier, will create an sg driver instance for it. So when 10393 * you attach it to CTL, you wind up with a ton of sg driver 10394 * instances. (One for every LUN that Linux bothered to probe.) 10395 * Linux does this despite the fact that it issues a REPORT LUNs 10396 * to LUN 0 to get the inventory of supported LUNs. 10397 * 10398 * - There is other anecdotal evidence (from Emulex folks) about 10399 * arrays that use the offline peripheral qualifier for LUNs that 10400 * are on the "passive" path in an active/passive array. 10401 * 10402 * So the solution is provide a hopefully reasonable default 10403 * (return bad/no LUN) and allow the user to change the behavior 10404 * with a tunable/sysctl variable. 10405 */ 10406 if (lun != NULL) 10407 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10408 lun->be_lun->lun_type; 10409 else if (softc->inquiry_pq_no_lun == 0) 10410 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10411 else 10412 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10413 10414 /* RMB in byte 2 is 0 */ 10415 inq_ptr->version = SCSI_REV_SPC4; 10416 10417 /* 10418 * According to SAM-3, even if a device only supports a single 10419 * level of LUN addressing, it should still set the HISUP bit: 10420 * 10421 * 4.9.1 Logical unit numbers overview 10422 * 10423 * All logical unit number formats described in this standard are 10424 * hierarchical in structure even when only a single level in that 10425 * hierarchy is used. The HISUP bit shall be set to one in the 10426 * standard INQUIRY data (see SPC-2) when any logical unit number 10427 * format described in this standard is used. Non-hierarchical 10428 * formats are outside the scope of this standard. 10429 * 10430 * Therefore we set the HiSup bit here. 10431 * 10432 * The reponse format is 2, per SPC-3. 10433 */ 10434 inq_ptr->response_format = SID_HiSup | 2; 10435 10436 inq_ptr->additional_length = data_len - 10437 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10438 CTL_DEBUG_PRINT(("additional_length = %d\n", 10439 inq_ptr->additional_length)); 10440 10441 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10442 /* 16 bit addressing */ 10443 if (port_type == CTL_PORT_SCSI) 10444 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10445 /* XXX set the SID_MultiP bit here if we're actually going to 10446 respond on multiple ports */ 10447 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10448 10449 /* 16 bit data bus, synchronous transfers */ 10450 if (port_type == CTL_PORT_SCSI) 10451 inq_ptr->flags = SID_WBus16 | SID_Sync; 10452 /* 10453 * XXX KDM do we want to support tagged queueing on the control 10454 * device at all? 10455 */ 10456 if ((lun == NULL) 10457 || (lun->be_lun->lun_type != T_PROCESSOR)) 10458 inq_ptr->flags |= SID_CmdQue; 10459 /* 10460 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10461 * We have 8 bytes for the vendor name, and 16 bytes for the device 10462 * name and 4 bytes for the revision. 10463 */ 10464 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10465 "vendor")) == NULL) { 10466 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10467 } else { 10468 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10469 strncpy(inq_ptr->vendor, val, 10470 min(sizeof(inq_ptr->vendor), strlen(val))); 10471 } 10472 if (lun == NULL) { 10473 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10474 sizeof(inq_ptr->product)); 10475 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10476 switch (lun->be_lun->lun_type) { 10477 case T_DIRECT: 10478 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10479 sizeof(inq_ptr->product)); 10480 break; 10481 case T_PROCESSOR: 10482 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10483 sizeof(inq_ptr->product)); 10484 break; 10485 default: 10486 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10487 sizeof(inq_ptr->product)); 10488 break; 10489 } 10490 } else { 10491 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10492 strncpy(inq_ptr->product, val, 10493 min(sizeof(inq_ptr->product), strlen(val))); 10494 } 10495 10496 /* 10497 * XXX make this a macro somewhere so it automatically gets 10498 * incremented when we make changes. 10499 */ 10500 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10501 "revision")) == NULL) { 10502 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10503 } else { 10504 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10505 strncpy(inq_ptr->revision, val, 10506 min(sizeof(inq_ptr->revision), strlen(val))); 10507 } 10508 10509 /* 10510 * For parallel SCSI, we support double transition and single 10511 * transition clocking. We also support QAS (Quick Arbitration 10512 * and Selection) and Information Unit transfers on both the 10513 * control and array devices. 10514 */ 10515 if (port_type == CTL_PORT_SCSI) 10516 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10517 SID_SPI_IUS; 10518 10519 /* SAM-5 (no version claimed) */ 10520 scsi_ulto2b(0x00A0, inq_ptr->version1); 10521 /* SPC-4 (no version claimed) */ 10522 scsi_ulto2b(0x0460, inq_ptr->version2); 10523 if (port_type == CTL_PORT_FC) { 10524 /* FCP-2 ANSI INCITS.350:2003 */ 10525 scsi_ulto2b(0x0917, inq_ptr->version3); 10526 } else if (port_type == CTL_PORT_SCSI) { 10527 /* SPI-4 ANSI INCITS.362:200x */ 10528 scsi_ulto2b(0x0B56, inq_ptr->version3); 10529 } else if (port_type == CTL_PORT_ISCSI) { 10530 /* iSCSI (no version claimed) */ 10531 scsi_ulto2b(0x0960, inq_ptr->version3); 10532 } else if (port_type == CTL_PORT_SAS) { 10533 /* SAS (no version claimed) */ 10534 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10535 } 10536 10537 if (lun == NULL) { 10538 /* SBC-4 (no version claimed) */ 10539 scsi_ulto2b(0x0600, inq_ptr->version4); 10540 } else { 10541 switch (lun->be_lun->lun_type) { 10542 case T_DIRECT: 10543 /* SBC-4 (no version claimed) */ 10544 scsi_ulto2b(0x0600, inq_ptr->version4); 10545 break; 10546 case T_PROCESSOR: 10547 default: 10548 break; 10549 } 10550 } 10551 10552 ctl_set_success(ctsio); 10553 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10554 ctsio->be_move_done = ctl_config_move_done; 10555 ctl_datamove((union ctl_io *)ctsio); 10556 return (CTL_RETVAL_COMPLETE); 10557} 10558 10559int 10560ctl_inquiry(struct ctl_scsiio *ctsio) 10561{ 10562 struct scsi_inquiry *cdb; 10563 int retval; 10564 10565 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10566 10567 cdb = (struct scsi_inquiry *)ctsio->cdb; 10568 if (cdb->byte2 & SI_EVPD) 10569 retval = ctl_inquiry_evpd(ctsio); 10570 else if (cdb->page_code == 0) 10571 retval = ctl_inquiry_std(ctsio); 10572 else { 10573 ctl_set_invalid_field(ctsio, 10574 /*sks_valid*/ 1, 10575 /*command*/ 1, 10576 /*field*/ 2, 10577 /*bit_valid*/ 0, 10578 /*bit*/ 0); 10579 ctl_done((union ctl_io *)ctsio); 10580 return (CTL_RETVAL_COMPLETE); 10581 } 10582 10583 return (retval); 10584} 10585 10586/* 10587 * For known CDB types, parse the LBA and length. 10588 */ 10589static int 10590ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10591{ 10592 if (io->io_hdr.io_type != CTL_IO_SCSI) 10593 return (1); 10594 10595 switch (io->scsiio.cdb[0]) { 10596 case COMPARE_AND_WRITE: { 10597 struct scsi_compare_and_write *cdb; 10598 10599 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10600 10601 *lba = scsi_8btou64(cdb->addr); 10602 *len = cdb->length; 10603 break; 10604 } 10605 case READ_6: 10606 case WRITE_6: { 10607 struct scsi_rw_6 *cdb; 10608 10609 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10610 10611 *lba = scsi_3btoul(cdb->addr); 10612 /* only 5 bits are valid in the most significant address byte */ 10613 *lba &= 0x1fffff; 10614 *len = cdb->length; 10615 break; 10616 } 10617 case READ_10: 10618 case WRITE_10: { 10619 struct scsi_rw_10 *cdb; 10620 10621 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10622 10623 *lba = scsi_4btoul(cdb->addr); 10624 *len = scsi_2btoul(cdb->length); 10625 break; 10626 } 10627 case WRITE_VERIFY_10: { 10628 struct scsi_write_verify_10 *cdb; 10629 10630 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10631 10632 *lba = scsi_4btoul(cdb->addr); 10633 *len = scsi_2btoul(cdb->length); 10634 break; 10635 } 10636 case READ_12: 10637 case WRITE_12: { 10638 struct scsi_rw_12 *cdb; 10639 10640 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10641 10642 *lba = scsi_4btoul(cdb->addr); 10643 *len = scsi_4btoul(cdb->length); 10644 break; 10645 } 10646 case WRITE_VERIFY_12: { 10647 struct scsi_write_verify_12 *cdb; 10648 10649 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10650 10651 *lba = scsi_4btoul(cdb->addr); 10652 *len = scsi_4btoul(cdb->length); 10653 break; 10654 } 10655 case READ_16: 10656 case WRITE_16: 10657 case WRITE_ATOMIC_16: { 10658 struct scsi_rw_16 *cdb; 10659 10660 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10661 10662 *lba = scsi_8btou64(cdb->addr); 10663 *len = scsi_4btoul(cdb->length); 10664 break; 10665 } 10666 case WRITE_VERIFY_16: { 10667 struct scsi_write_verify_16 *cdb; 10668 10669 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10670 10671 *lba = scsi_8btou64(cdb->addr); 10672 *len = scsi_4btoul(cdb->length); 10673 break; 10674 } 10675 case WRITE_SAME_10: { 10676 struct scsi_write_same_10 *cdb; 10677 10678 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10679 10680 *lba = scsi_4btoul(cdb->addr); 10681 *len = scsi_2btoul(cdb->length); 10682 break; 10683 } 10684 case WRITE_SAME_16: { 10685 struct scsi_write_same_16 *cdb; 10686 10687 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10688 10689 *lba = scsi_8btou64(cdb->addr); 10690 *len = scsi_4btoul(cdb->length); 10691 break; 10692 } 10693 case VERIFY_10: { 10694 struct scsi_verify_10 *cdb; 10695 10696 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10697 10698 *lba = scsi_4btoul(cdb->addr); 10699 *len = scsi_2btoul(cdb->length); 10700 break; 10701 } 10702 case VERIFY_12: { 10703 struct scsi_verify_12 *cdb; 10704 10705 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10706 10707 *lba = scsi_4btoul(cdb->addr); 10708 *len = scsi_4btoul(cdb->length); 10709 break; 10710 } 10711 case VERIFY_16: { 10712 struct scsi_verify_16 *cdb; 10713 10714 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10715 10716 *lba = scsi_8btou64(cdb->addr); 10717 *len = scsi_4btoul(cdb->length); 10718 break; 10719 } 10720 case UNMAP: { 10721 *lba = 0; 10722 *len = UINT64_MAX; 10723 break; 10724 } 10725 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10726 struct scsi_get_lba_status *cdb; 10727 10728 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10729 *lba = scsi_8btou64(cdb->addr); 10730 *len = UINT32_MAX; 10731 break; 10732 } 10733 default: 10734 return (1); 10735 break; /* NOTREACHED */ 10736 } 10737 10738 return (0); 10739} 10740 10741static ctl_action 10742ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10743 bool seq) 10744{ 10745 uint64_t endlba1, endlba2; 10746 10747 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10748 endlba2 = lba2 + len2 - 1; 10749 10750 if ((endlba1 < lba2) || (endlba2 < lba1)) 10751 return (CTL_ACTION_PASS); 10752 else 10753 return (CTL_ACTION_BLOCK); 10754} 10755 10756static int 10757ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10758{ 10759 struct ctl_ptr_len_flags *ptrlen; 10760 struct scsi_unmap_desc *buf, *end, *range; 10761 uint64_t lba; 10762 uint32_t len; 10763 10764 /* If not UNMAP -- go other way. */ 10765 if (io->io_hdr.io_type != CTL_IO_SCSI || 10766 io->scsiio.cdb[0] != UNMAP) 10767 return (CTL_ACTION_ERROR); 10768 10769 /* If UNMAP without data -- block and wait for data. */ 10770 ptrlen = (struct ctl_ptr_len_flags *) 10771 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10772 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10773 ptrlen->ptr == NULL) 10774 return (CTL_ACTION_BLOCK); 10775 10776 /* UNMAP with data -- check for collision. */ 10777 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10778 end = buf + ptrlen->len / sizeof(*buf); 10779 for (range = buf; range < end; range++) { 10780 lba = scsi_8btou64(range->lba); 10781 len = scsi_4btoul(range->length); 10782 if ((lba < lba2 + len2) && (lba + len > lba2)) 10783 return (CTL_ACTION_BLOCK); 10784 } 10785 return (CTL_ACTION_PASS); 10786} 10787 10788static ctl_action 10789ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10790{ 10791 uint64_t lba1, lba2; 10792 uint64_t len1, len2; 10793 int retval; 10794 10795 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10796 return (CTL_ACTION_ERROR); 10797 10798 retval = ctl_extent_check_unmap(io1, lba2, len2); 10799 if (retval != CTL_ACTION_ERROR) 10800 return (retval); 10801 10802 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10803 return (CTL_ACTION_ERROR); 10804 10805 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10806} 10807 10808static ctl_action 10809ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10810{ 10811 uint64_t lba1, lba2; 10812 uint64_t len1, len2; 10813 10814 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10815 return (CTL_ACTION_ERROR); 10816 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10817 return (CTL_ACTION_ERROR); 10818 10819 if (lba1 + len1 == lba2) 10820 return (CTL_ACTION_BLOCK); 10821 return (CTL_ACTION_PASS); 10822} 10823 10824static ctl_action 10825ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10826 union ctl_io *ooa_io) 10827{ 10828 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10829 ctl_serialize_action *serialize_row; 10830 10831 /* 10832 * The initiator attempted multiple untagged commands at the same 10833 * time. Can't do that. 10834 */ 10835 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10836 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10837 && ((pending_io->io_hdr.nexus.targ_port == 10838 ooa_io->io_hdr.nexus.targ_port) 10839 && (pending_io->io_hdr.nexus.initid.id == 10840 ooa_io->io_hdr.nexus.initid.id)) 10841 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10842 CTL_FLAG_STATUS_SENT)) == 0)) 10843 return (CTL_ACTION_OVERLAP); 10844 10845 /* 10846 * The initiator attempted to send multiple tagged commands with 10847 * the same ID. (It's fine if different initiators have the same 10848 * tag ID.) 10849 * 10850 * Even if all of those conditions are true, we don't kill the I/O 10851 * if the command ahead of us has been aborted. We won't end up 10852 * sending it to the FETD, and it's perfectly legal to resend a 10853 * command with the same tag number as long as the previous 10854 * instance of this tag number has been aborted somehow. 10855 */ 10856 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10857 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10858 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10859 && ((pending_io->io_hdr.nexus.targ_port == 10860 ooa_io->io_hdr.nexus.targ_port) 10861 && (pending_io->io_hdr.nexus.initid.id == 10862 ooa_io->io_hdr.nexus.initid.id)) 10863 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10864 CTL_FLAG_STATUS_SENT)) == 0)) 10865 return (CTL_ACTION_OVERLAP_TAG); 10866 10867 /* 10868 * If we get a head of queue tag, SAM-3 says that we should 10869 * immediately execute it. 10870 * 10871 * What happens if this command would normally block for some other 10872 * reason? e.g. a request sense with a head of queue tag 10873 * immediately after a write. Normally that would block, but this 10874 * will result in its getting executed immediately... 10875 * 10876 * We currently return "pass" instead of "skip", so we'll end up 10877 * going through the rest of the queue to check for overlapped tags. 10878 * 10879 * XXX KDM check for other types of blockage first?? 10880 */ 10881 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10882 return (CTL_ACTION_PASS); 10883 10884 /* 10885 * Ordered tags have to block until all items ahead of them 10886 * have completed. If we get called with an ordered tag, we always 10887 * block, if something else is ahead of us in the queue. 10888 */ 10889 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10890 return (CTL_ACTION_BLOCK); 10891 10892 /* 10893 * Simple tags get blocked until all head of queue and ordered tags 10894 * ahead of them have completed. I'm lumping untagged commands in 10895 * with simple tags here. XXX KDM is that the right thing to do? 10896 */ 10897 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10898 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10899 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10900 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10901 return (CTL_ACTION_BLOCK); 10902 10903 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10904 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10905 10906 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10907 10908 switch (serialize_row[pending_entry->seridx]) { 10909 case CTL_SER_BLOCK: 10910 return (CTL_ACTION_BLOCK); 10911 case CTL_SER_EXTENT: 10912 return (ctl_extent_check(ooa_io, pending_io, 10913 (lun->serseq == CTL_LUN_SERSEQ_ON))); 10914 case CTL_SER_EXTENTOPT: 10915 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10916 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10917 return (ctl_extent_check(ooa_io, pending_io, 10918 (lun->serseq == CTL_LUN_SERSEQ_ON))); 10919 return (CTL_ACTION_PASS); 10920 case CTL_SER_EXTENTSEQ: 10921 if (lun->serseq != CTL_LUN_SERSEQ_OFF) 10922 return (ctl_extent_check_seq(ooa_io, pending_io)); 10923 return (CTL_ACTION_PASS); 10924 case CTL_SER_PASS: 10925 return (CTL_ACTION_PASS); 10926 case CTL_SER_BLOCKOPT: 10927 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10928 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10929 return (CTL_ACTION_BLOCK); 10930 return (CTL_ACTION_PASS); 10931 case CTL_SER_SKIP: 10932 return (CTL_ACTION_SKIP); 10933 default: 10934 panic("invalid serialization value %d", 10935 serialize_row[pending_entry->seridx]); 10936 } 10937 10938 return (CTL_ACTION_ERROR); 10939} 10940 10941/* 10942 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10943 * Assumptions: 10944 * - pending_io is generally either incoming, or on the blocked queue 10945 * - starting I/O is the I/O we want to start the check with. 10946 */ 10947static ctl_action 10948ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10949 union ctl_io *starting_io) 10950{ 10951 union ctl_io *ooa_io; 10952 ctl_action action; 10953 10954 mtx_assert(&lun->lun_lock, MA_OWNED); 10955 10956 /* 10957 * Run back along the OOA queue, starting with the current 10958 * blocked I/O and going through every I/O before it on the 10959 * queue. If starting_io is NULL, we'll just end up returning 10960 * CTL_ACTION_PASS. 10961 */ 10962 for (ooa_io = starting_io; ooa_io != NULL; 10963 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10964 ooa_links)){ 10965 10966 /* 10967 * This routine just checks to see whether 10968 * cur_blocked is blocked by ooa_io, which is ahead 10969 * of it in the queue. It doesn't queue/dequeue 10970 * cur_blocked. 10971 */ 10972 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10973 switch (action) { 10974 case CTL_ACTION_BLOCK: 10975 case CTL_ACTION_OVERLAP: 10976 case CTL_ACTION_OVERLAP_TAG: 10977 case CTL_ACTION_SKIP: 10978 case CTL_ACTION_ERROR: 10979 return (action); 10980 break; /* NOTREACHED */ 10981 case CTL_ACTION_PASS: 10982 break; 10983 default: 10984 panic("invalid action %d", action); 10985 break; /* NOTREACHED */ 10986 } 10987 } 10988 10989 return (CTL_ACTION_PASS); 10990} 10991 10992/* 10993 * Assumptions: 10994 * - An I/O has just completed, and has been removed from the per-LUN OOA 10995 * queue, so some items on the blocked queue may now be unblocked. 10996 */ 10997static int 10998ctl_check_blocked(struct ctl_lun *lun) 10999{ 11000 union ctl_io *cur_blocked, *next_blocked; 11001 11002 mtx_assert(&lun->lun_lock, MA_OWNED); 11003 11004 /* 11005 * Run forward from the head of the blocked queue, checking each 11006 * entry against the I/Os prior to it on the OOA queue to see if 11007 * there is still any blockage. 11008 * 11009 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11010 * with our removing a variable on it while it is traversing the 11011 * list. 11012 */ 11013 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11014 cur_blocked != NULL; cur_blocked = next_blocked) { 11015 union ctl_io *prev_ooa; 11016 ctl_action action; 11017 11018 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11019 blocked_links); 11020 11021 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11022 ctl_ooaq, ooa_links); 11023 11024 /* 11025 * If cur_blocked happens to be the first item in the OOA 11026 * queue now, prev_ooa will be NULL, and the action 11027 * returned will just be CTL_ACTION_PASS. 11028 */ 11029 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11030 11031 switch (action) { 11032 case CTL_ACTION_BLOCK: 11033 /* Nothing to do here, still blocked */ 11034 break; 11035 case CTL_ACTION_OVERLAP: 11036 case CTL_ACTION_OVERLAP_TAG: 11037 /* 11038 * This shouldn't happen! In theory we've already 11039 * checked this command for overlap... 11040 */ 11041 break; 11042 case CTL_ACTION_PASS: 11043 case CTL_ACTION_SKIP: { 11044 const struct ctl_cmd_entry *entry; 11045 int isc_retval; 11046 11047 /* 11048 * The skip case shouldn't happen, this transaction 11049 * should have never made it onto the blocked queue. 11050 */ 11051 /* 11052 * This I/O is no longer blocked, we can remove it 11053 * from the blocked queue. Since this is a TAILQ 11054 * (doubly linked list), we can do O(1) removals 11055 * from any place on the list. 11056 */ 11057 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11058 blocked_links); 11059 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11060 11061 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11062 /* 11063 * Need to send IO back to original side to 11064 * run 11065 */ 11066 union ctl_ha_msg msg_info; 11067 11068 msg_info.hdr.original_sc = 11069 cur_blocked->io_hdr.original_sc; 11070 msg_info.hdr.serializing_sc = cur_blocked; 11071 msg_info.hdr.msg_type = CTL_MSG_R2R; 11072 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11073 &msg_info, sizeof(msg_info), 0)) > 11074 CTL_HA_STATUS_SUCCESS) { 11075 printf("CTL:Check Blocked error from " 11076 "ctl_ha_msg_send %d\n", 11077 isc_retval); 11078 } 11079 break; 11080 } 11081 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11082 11083 /* 11084 * Check this I/O for LUN state changes that may 11085 * have happened while this command was blocked. 11086 * The LUN state may have been changed by a command 11087 * ahead of us in the queue, so we need to re-check 11088 * for any states that can be caused by SCSI 11089 * commands. 11090 */ 11091 if (ctl_scsiio_lun_check(lun, entry, 11092 &cur_blocked->scsiio) == 0) { 11093 cur_blocked->io_hdr.flags |= 11094 CTL_FLAG_IS_WAS_ON_RTR; 11095 ctl_enqueue_rtr(cur_blocked); 11096 } else 11097 ctl_done(cur_blocked); 11098 break; 11099 } 11100 default: 11101 /* 11102 * This probably shouldn't happen -- we shouldn't 11103 * get CTL_ACTION_ERROR, or anything else. 11104 */ 11105 break; 11106 } 11107 } 11108 11109 return (CTL_RETVAL_COMPLETE); 11110} 11111 11112/* 11113 * This routine (with one exception) checks LUN flags that can be set by 11114 * commands ahead of us in the OOA queue. These flags have to be checked 11115 * when a command initially comes in, and when we pull a command off the 11116 * blocked queue and are preparing to execute it. The reason we have to 11117 * check these flags for commands on the blocked queue is that the LUN 11118 * state may have been changed by a command ahead of us while we're on the 11119 * blocked queue. 11120 * 11121 * Ordering is somewhat important with these checks, so please pay 11122 * careful attention to the placement of any new checks. 11123 */ 11124static int 11125ctl_scsiio_lun_check(struct ctl_lun *lun, 11126 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11127{ 11128 struct ctl_softc *softc = lun->ctl_softc; 11129 int retval; 11130 uint32_t residx; 11131 11132 retval = 0; 11133 11134 mtx_assert(&lun->lun_lock, MA_OWNED); 11135 11136 /* 11137 * If this shelf is a secondary shelf controller, we have to reject 11138 * any media access commands. 11139 */ 11140 if ((softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 11141 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 11142 ctl_set_lun_standby(ctsio); 11143 retval = 1; 11144 goto bailout; 11145 } 11146 11147 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11148 if (lun->flags & CTL_LUN_READONLY) { 11149 ctl_set_sense(ctsio, /*current_error*/ 1, 11150 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11151 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11152 retval = 1; 11153 goto bailout; 11154 } 11155 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11156 .eca_and_aen & SCP_SWP) != 0) { 11157 ctl_set_sense(ctsio, /*current_error*/ 1, 11158 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11159 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11160 retval = 1; 11161 goto bailout; 11162 } 11163 } 11164 11165 /* 11166 * Check for a reservation conflict. If this command isn't allowed 11167 * even on reserved LUNs, and if this initiator isn't the one who 11168 * reserved us, reject the command with a reservation conflict. 11169 */ 11170 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11171 if ((lun->flags & CTL_LUN_RESERVED) 11172 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11173 if (lun->res_idx != residx) { 11174 ctl_set_reservation_conflict(ctsio); 11175 retval = 1; 11176 goto bailout; 11177 } 11178 } 11179 11180 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11181 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11182 /* No reservation or command is allowed. */; 11183 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11184 (lun->res_type == SPR_TYPE_WR_EX || 11185 lun->res_type == SPR_TYPE_WR_EX_RO || 11186 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11187 /* The command is allowed for Write Exclusive resv. */; 11188 } else { 11189 /* 11190 * if we aren't registered or it's a res holder type 11191 * reservation and this isn't the res holder then set a 11192 * conflict. 11193 */ 11194 if (ctl_get_prkey(lun, residx) == 0 11195 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11196 ctl_set_reservation_conflict(ctsio); 11197 retval = 1; 11198 goto bailout; 11199 } 11200 11201 } 11202 11203 if ((lun->flags & CTL_LUN_OFFLINE) 11204 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11205 ctl_set_lun_not_ready(ctsio); 11206 retval = 1; 11207 goto bailout; 11208 } 11209 11210 /* 11211 * If the LUN is stopped, see if this particular command is allowed 11212 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11213 */ 11214 if ((lun->flags & CTL_LUN_STOPPED) 11215 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11216 /* "Logical unit not ready, initializing cmd. required" */ 11217 ctl_set_lun_stopped(ctsio); 11218 retval = 1; 11219 goto bailout; 11220 } 11221 11222 if ((lun->flags & CTL_LUN_INOPERABLE) 11223 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11224 /* "Medium format corrupted" */ 11225 ctl_set_medium_format_corrupted(ctsio); 11226 retval = 1; 11227 goto bailout; 11228 } 11229 11230bailout: 11231 return (retval); 11232 11233} 11234 11235static void 11236ctl_failover_io(union ctl_io *io, int have_lock) 11237{ 11238 ctl_set_busy(&io->scsiio); 11239 ctl_done(io); 11240} 11241 11242#ifdef notyet 11243static void 11244ctl_failover(void) 11245{ 11246 struct ctl_lun *lun; 11247 struct ctl_softc *softc; 11248 union ctl_io *next_io, *pending_io; 11249 union ctl_io *io; 11250 int lun_idx; 11251 11252 softc = control_softc; 11253 11254 mtx_lock(&softc->ctl_lock); 11255 /* 11256 * Remove any cmds from the other SC from the rtr queue. These 11257 * will obviously only be for LUNs for which we're the primary. 11258 * We can't send status or get/send data for these commands. 11259 * Since they haven't been executed yet, we can just remove them. 11260 * We'll either abort them or delete them below, depending on 11261 * which HA mode we're in. 11262 */ 11263#ifdef notyet 11264 mtx_lock(&softc->queue_lock); 11265 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); 11266 io != NULL; io = next_io) { 11267 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11268 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11269 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr, 11270 ctl_io_hdr, links); 11271 } 11272 mtx_unlock(&softc->queue_lock); 11273#endif 11274 11275 for (lun_idx=0; lun_idx < softc->num_luns; lun_idx++) { 11276 lun = softc->ctl_luns[lun_idx]; 11277 if (lun==NULL) 11278 continue; 11279 11280 /* 11281 * Processor LUNs are primary on both sides. 11282 * XXX will this always be true? 11283 */ 11284 if (lun->be_lun->lun_type == T_PROCESSOR) 11285 continue; 11286 11287 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11288 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11289 printf("FAILOVER: primary lun %d\n", lun_idx); 11290 /* 11291 * Remove all commands from the other SC. First from the 11292 * blocked queue then from the ooa queue. Once we have 11293 * removed them. Call ctl_check_blocked to see if there 11294 * is anything that can run. 11295 */ 11296 for (io = (union ctl_io *)TAILQ_FIRST( 11297 &lun->blocked_queue); io != NULL; io = next_io) { 11298 11299 next_io = (union ctl_io *)TAILQ_NEXT( 11300 &io->io_hdr, blocked_links); 11301 11302 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11303 TAILQ_REMOVE(&lun->blocked_queue, 11304 &io->io_hdr,blocked_links); 11305 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11306 TAILQ_REMOVE(&lun->ooa_queue, 11307 &io->io_hdr, ooa_links); 11308 11309 ctl_free_io(io); 11310 } 11311 } 11312 11313 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11314 io != NULL; io = next_io) { 11315 11316 next_io = (union ctl_io *)TAILQ_NEXT( 11317 &io->io_hdr, ooa_links); 11318 11319 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11320 11321 TAILQ_REMOVE(&lun->ooa_queue, 11322 &io->io_hdr, 11323 ooa_links); 11324 11325 ctl_free_io(io); 11326 } 11327 } 11328 ctl_check_blocked(lun); 11329 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11330 && (softc->ha_mode == CTL_HA_MODE_XFER)) { 11331 11332 printf("FAILOVER: primary lun %d\n", lun_idx); 11333 /* 11334 * Abort all commands from the other SC. We can't 11335 * send status back for them now. These should get 11336 * cleaned up when they are completed or come out 11337 * for a datamove operation. 11338 */ 11339 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11340 io != NULL; io = next_io) { 11341 next_io = (union ctl_io *)TAILQ_NEXT( 11342 &io->io_hdr, ooa_links); 11343 11344 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11345 io->io_hdr.flags |= CTL_FLAG_ABORT; 11346 } 11347 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11348 && (softc->ha_mode == CTL_HA_MODE_XFER)) { 11349 11350 printf("FAILOVER: secondary lun %d\n", lun_idx); 11351 11352 lun->flags |= CTL_LUN_PRIMARY_SC; 11353 11354 /* 11355 * We send all I/O that was sent to this controller 11356 * and redirected to the other side back with 11357 * busy status, and have the initiator retry it. 11358 * Figuring out how much data has been transferred, 11359 * etc. and picking up where we left off would be 11360 * very tricky. 11361 * 11362 * XXX KDM need to remove I/O from the blocked 11363 * queue as well! 11364 */ 11365 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11366 &lun->ooa_queue); pending_io != NULL; 11367 pending_io = next_io) { 11368 11369 next_io = (union ctl_io *)TAILQ_NEXT( 11370 &pending_io->io_hdr, ooa_links); 11371 11372 pending_io->io_hdr.flags &= 11373 ~CTL_FLAG_SENT_2OTHER_SC; 11374 11375 if (pending_io->io_hdr.flags & 11376 CTL_FLAG_IO_ACTIVE) { 11377 pending_io->io_hdr.flags |= 11378 CTL_FLAG_FAILOVER; 11379 } else { 11380 ctl_set_busy(&pending_io->scsiio); 11381 ctl_done(pending_io); 11382 } 11383 } 11384 11385 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11386 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11387 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11388 printf("FAILOVER: secondary lun %d\n", lun_idx); 11389 /* 11390 * if the first io on the OOA is not on the RtR queue 11391 * add it. 11392 */ 11393 lun->flags |= CTL_LUN_PRIMARY_SC; 11394 11395 pending_io = (union ctl_io *)TAILQ_FIRST( 11396 &lun->ooa_queue); 11397 if (pending_io==NULL) { 11398 printf("Nothing on OOA queue\n"); 11399 continue; 11400 } 11401 11402 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11403 if ((pending_io->io_hdr.flags & 11404 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11405 pending_io->io_hdr.flags |= 11406 CTL_FLAG_IS_WAS_ON_RTR; 11407 ctl_enqueue_rtr(pending_io); 11408 } 11409#if 0 11410 else 11411 { 11412 printf("Tag 0x%04x is running\n", 11413 pending_io->scsiio.tag_num); 11414 } 11415#endif 11416 11417 next_io = (union ctl_io *)TAILQ_NEXT( 11418 &pending_io->io_hdr, ooa_links); 11419 for (pending_io=next_io; pending_io != NULL; 11420 pending_io = next_io) { 11421 pending_io->io_hdr.flags &= 11422 ~CTL_FLAG_SENT_2OTHER_SC; 11423 next_io = (union ctl_io *)TAILQ_NEXT( 11424 &pending_io->io_hdr, ooa_links); 11425 if (pending_io->io_hdr.flags & 11426 CTL_FLAG_IS_WAS_ON_RTR) { 11427#if 0 11428 printf("Tag 0x%04x is running\n", 11429 pending_io->scsiio.tag_num); 11430#endif 11431 continue; 11432 } 11433 11434 switch (ctl_check_ooa(lun, pending_io, 11435 (union ctl_io *)TAILQ_PREV( 11436 &pending_io->io_hdr, ctl_ooaq, 11437 ooa_links))) { 11438 11439 case CTL_ACTION_BLOCK: 11440 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11441 &pending_io->io_hdr, 11442 blocked_links); 11443 pending_io->io_hdr.flags |= 11444 CTL_FLAG_BLOCKED; 11445 break; 11446 case CTL_ACTION_PASS: 11447 case CTL_ACTION_SKIP: 11448 pending_io->io_hdr.flags |= 11449 CTL_FLAG_IS_WAS_ON_RTR; 11450 ctl_enqueue_rtr(pending_io); 11451 break; 11452 case CTL_ACTION_OVERLAP: 11453 ctl_set_overlapped_cmd( 11454 (struct ctl_scsiio *)pending_io); 11455 ctl_done(pending_io); 11456 break; 11457 case CTL_ACTION_OVERLAP_TAG: 11458 ctl_set_overlapped_tag( 11459 (struct ctl_scsiio *)pending_io, 11460 pending_io->scsiio.tag_num & 0xff); 11461 ctl_done(pending_io); 11462 break; 11463 case CTL_ACTION_ERROR: 11464 default: 11465 ctl_set_internal_failure( 11466 (struct ctl_scsiio *)pending_io, 11467 0, // sks_valid 11468 0); //retry count 11469 ctl_done(pending_io); 11470 break; 11471 } 11472 } 11473 11474 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11475 } else { 11476 panic("Unhandled HA mode failover, LUN flags = %#x, " 11477 "ha_mode = #%x", lun->flags, softc->ha_mode); 11478 } 11479 } 11480 ctl_pause_rtr = 0; 11481 mtx_unlock(&softc->ctl_lock); 11482} 11483#endif 11484 11485static void 11486ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx, 11487 ctl_ua_type ua_type) 11488{ 11489 struct ctl_lun *lun; 11490 ctl_ua_type *pu; 11491 11492 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 11493 11494 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 11495 mtx_lock(&lun->lun_lock); 11496 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 11497 if (pu != NULL) 11498 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua_type; 11499 mtx_unlock(&lun->lun_lock); 11500 } 11501} 11502 11503static int 11504ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11505{ 11506 struct ctl_lun *lun; 11507 const struct ctl_cmd_entry *entry; 11508 uint32_t initidx, targ_lun; 11509 int retval; 11510 11511 retval = 0; 11512 11513 lun = NULL; 11514 11515 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11516 if ((targ_lun < CTL_MAX_LUNS) 11517 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11518 /* 11519 * If the LUN is invalid, pretend that it doesn't exist. 11520 * It will go away as soon as all pending I/O has been 11521 * completed. 11522 */ 11523 mtx_lock(&lun->lun_lock); 11524 if (lun->flags & CTL_LUN_DISABLED) { 11525 mtx_unlock(&lun->lun_lock); 11526 lun = NULL; 11527 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11528 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11529 } else { 11530 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11531 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11532 lun->be_lun; 11533 if (lun->be_lun->lun_type == T_PROCESSOR) { 11534 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11535 } 11536 11537 /* 11538 * Every I/O goes into the OOA queue for a 11539 * particular LUN, and stays there until completion. 11540 */ 11541#ifdef CTL_TIME_IO 11542 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11543 lun->idle_time += getsbinuptime() - 11544 lun->last_busy; 11545 } 11546#endif 11547 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11548 ooa_links); 11549 } 11550 } else { 11551 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11552 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11553 } 11554 11555 /* Get command entry and return error if it is unsuppotyed. */ 11556 entry = ctl_validate_command(ctsio); 11557 if (entry == NULL) { 11558 if (lun) 11559 mtx_unlock(&lun->lun_lock); 11560 return (retval); 11561 } 11562 11563 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11564 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11565 11566 /* 11567 * Check to see whether we can send this command to LUNs that don't 11568 * exist. This should pretty much only be the case for inquiry 11569 * and request sense. Further checks, below, really require having 11570 * a LUN, so we can't really check the command anymore. Just put 11571 * it on the rtr queue. 11572 */ 11573 if (lun == NULL) { 11574 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11575 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11576 ctl_enqueue_rtr((union ctl_io *)ctsio); 11577 return (retval); 11578 } 11579 11580 ctl_set_unsupported_lun(ctsio); 11581 ctl_done((union ctl_io *)ctsio); 11582 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11583 return (retval); 11584 } else { 11585 /* 11586 * Make sure we support this particular command on this LUN. 11587 * e.g., we don't support writes to the control LUN. 11588 */ 11589 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11590 mtx_unlock(&lun->lun_lock); 11591 ctl_set_invalid_opcode(ctsio); 11592 ctl_done((union ctl_io *)ctsio); 11593 return (retval); 11594 } 11595 } 11596 11597 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11598 11599#ifdef CTL_WITH_CA 11600 /* 11601 * If we've got a request sense, it'll clear the contingent 11602 * allegiance condition. Otherwise, if we have a CA condition for 11603 * this initiator, clear it, because it sent down a command other 11604 * than request sense. 11605 */ 11606 if ((ctsio->cdb[0] != REQUEST_SENSE) 11607 && (ctl_is_set(lun->have_ca, initidx))) 11608 ctl_clear_mask(lun->have_ca, initidx); 11609#endif 11610 11611 /* 11612 * If the command has this flag set, it handles its own unit 11613 * attention reporting, we shouldn't do anything. Otherwise we 11614 * check for any pending unit attentions, and send them back to the 11615 * initiator. We only do this when a command initially comes in, 11616 * not when we pull it off the blocked queue. 11617 * 11618 * According to SAM-3, section 5.3.2, the order that things get 11619 * presented back to the host is basically unit attentions caused 11620 * by some sort of reset event, busy status, reservation conflicts 11621 * or task set full, and finally any other status. 11622 * 11623 * One issue here is that some of the unit attentions we report 11624 * don't fall into the "reset" category (e.g. "reported luns data 11625 * has changed"). So reporting it here, before the reservation 11626 * check, may be technically wrong. I guess the only thing to do 11627 * would be to check for and report the reset events here, and then 11628 * check for the other unit attention types after we check for a 11629 * reservation conflict. 11630 * 11631 * XXX KDM need to fix this 11632 */ 11633 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11634 ctl_ua_type ua_type; 11635 scsi_sense_data_type sense_format; 11636 11637 if (lun->flags & CTL_LUN_SENSE_DESC) 11638 sense_format = SSD_TYPE_DESC; 11639 else 11640 sense_format = SSD_TYPE_FIXED; 11641 11642 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11643 sense_format); 11644 if (ua_type != CTL_UA_NONE) { 11645 mtx_unlock(&lun->lun_lock); 11646 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11647 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11648 ctsio->sense_len = SSD_FULL_SIZE; 11649 ctl_done((union ctl_io *)ctsio); 11650 return (retval); 11651 } 11652 } 11653 11654 11655 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11656 mtx_unlock(&lun->lun_lock); 11657 ctl_done((union ctl_io *)ctsio); 11658 return (retval); 11659 } 11660 11661 /* 11662 * XXX CHD this is where we want to send IO to other side if 11663 * this LUN is secondary on this SC. We will need to make a copy 11664 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11665 * the copy we send as FROM_OTHER. 11666 * We also need to stuff the address of the original IO so we can 11667 * find it easily. Something similar will need be done on the other 11668 * side so when we are done we can find the copy. 11669 */ 11670 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11671 union ctl_ha_msg msg_info; 11672 int isc_retval; 11673 11674 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11675 11676 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11677 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11678#if 0 11679 printf("1. ctsio %p\n", ctsio); 11680#endif 11681 msg_info.hdr.serializing_sc = NULL; 11682 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11683 msg_info.scsi.tag_num = ctsio->tag_num; 11684 msg_info.scsi.tag_type = ctsio->tag_type; 11685 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11686 11687 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11688 11689 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11690 (void *)&msg_info, sizeof(msg_info), 0)) > 11691 CTL_HA_STATUS_SUCCESS) { 11692 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11693 isc_retval); 11694 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11695 } else { 11696#if 0 11697 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11698#endif 11699 } 11700 11701 /* 11702 * XXX KDM this I/O is off the incoming queue, but hasn't 11703 * been inserted on any other queue. We may need to come 11704 * up with a holding queue while we wait for serialization 11705 * so that we have an idea of what we're waiting for from 11706 * the other side. 11707 */ 11708 mtx_unlock(&lun->lun_lock); 11709 return (retval); 11710 } 11711 11712 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11713 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11714 ctl_ooaq, ooa_links))) { 11715 case CTL_ACTION_BLOCK: 11716 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11717 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11718 blocked_links); 11719 mtx_unlock(&lun->lun_lock); 11720 return (retval); 11721 case CTL_ACTION_PASS: 11722 case CTL_ACTION_SKIP: 11723 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11724 mtx_unlock(&lun->lun_lock); 11725 ctl_enqueue_rtr((union ctl_io *)ctsio); 11726 break; 11727 case CTL_ACTION_OVERLAP: 11728 mtx_unlock(&lun->lun_lock); 11729 ctl_set_overlapped_cmd(ctsio); 11730 ctl_done((union ctl_io *)ctsio); 11731 break; 11732 case CTL_ACTION_OVERLAP_TAG: 11733 mtx_unlock(&lun->lun_lock); 11734 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11735 ctl_done((union ctl_io *)ctsio); 11736 break; 11737 case CTL_ACTION_ERROR: 11738 default: 11739 mtx_unlock(&lun->lun_lock); 11740 ctl_set_internal_failure(ctsio, 11741 /*sks_valid*/ 0, 11742 /*retry_count*/ 0); 11743 ctl_done((union ctl_io *)ctsio); 11744 break; 11745 } 11746 return (retval); 11747} 11748 11749const struct ctl_cmd_entry * 11750ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11751{ 11752 const struct ctl_cmd_entry *entry; 11753 int service_action; 11754 11755 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11756 if (sa) 11757 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11758 if (entry->flags & CTL_CMD_FLAG_SA5) { 11759 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11760 entry = &((const struct ctl_cmd_entry *) 11761 entry->execute)[service_action]; 11762 } 11763 return (entry); 11764} 11765 11766const struct ctl_cmd_entry * 11767ctl_validate_command(struct ctl_scsiio *ctsio) 11768{ 11769 const struct ctl_cmd_entry *entry; 11770 int i, sa; 11771 uint8_t diff; 11772 11773 entry = ctl_get_cmd_entry(ctsio, &sa); 11774 if (entry->execute == NULL) { 11775 if (sa) 11776 ctl_set_invalid_field(ctsio, 11777 /*sks_valid*/ 1, 11778 /*command*/ 1, 11779 /*field*/ 1, 11780 /*bit_valid*/ 1, 11781 /*bit*/ 4); 11782 else 11783 ctl_set_invalid_opcode(ctsio); 11784 ctl_done((union ctl_io *)ctsio); 11785 return (NULL); 11786 } 11787 KASSERT(entry->length > 0, 11788 ("Not defined length for command 0x%02x/0x%02x", 11789 ctsio->cdb[0], ctsio->cdb[1])); 11790 for (i = 1; i < entry->length; i++) { 11791 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11792 if (diff == 0) 11793 continue; 11794 ctl_set_invalid_field(ctsio, 11795 /*sks_valid*/ 1, 11796 /*command*/ 1, 11797 /*field*/ i, 11798 /*bit_valid*/ 1, 11799 /*bit*/ fls(diff) - 1); 11800 ctl_done((union ctl_io *)ctsio); 11801 return (NULL); 11802 } 11803 return (entry); 11804} 11805 11806static int 11807ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11808{ 11809 11810 switch (lun_type) { 11811 case T_PROCESSOR: 11812 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11813 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11814 return (0); 11815 break; 11816 case T_DIRECT: 11817 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11818 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11819 return (0); 11820 break; 11821 default: 11822 return (0); 11823 } 11824 return (1); 11825} 11826 11827static int 11828ctl_scsiio(struct ctl_scsiio *ctsio) 11829{ 11830 int retval; 11831 const struct ctl_cmd_entry *entry; 11832 11833 retval = CTL_RETVAL_COMPLETE; 11834 11835 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11836 11837 entry = ctl_get_cmd_entry(ctsio, NULL); 11838 11839 /* 11840 * If this I/O has been aborted, just send it straight to 11841 * ctl_done() without executing it. 11842 */ 11843 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11844 ctl_done((union ctl_io *)ctsio); 11845 goto bailout; 11846 } 11847 11848 /* 11849 * All the checks should have been handled by ctl_scsiio_precheck(). 11850 * We should be clear now to just execute the I/O. 11851 */ 11852 retval = entry->execute(ctsio); 11853 11854bailout: 11855 return (retval); 11856} 11857 11858/* 11859 * Since we only implement one target right now, a bus reset simply resets 11860 * our single target. 11861 */ 11862static int 11863ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11864{ 11865 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11866} 11867 11868static int 11869ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11870 ctl_ua_type ua_type) 11871{ 11872 struct ctl_lun *lun; 11873 int retval; 11874 11875 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11876 union ctl_ha_msg msg_info; 11877 11878 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11879 msg_info.hdr.nexus = io->io_hdr.nexus; 11880 if (ua_type==CTL_UA_TARG_RESET) 11881 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11882 else 11883 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11884 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11885 msg_info.hdr.original_sc = NULL; 11886 msg_info.hdr.serializing_sc = NULL; 11887 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11888 (void *)&msg_info, sizeof(msg_info), 0)) { 11889 } 11890 } 11891 retval = 0; 11892 11893 mtx_lock(&softc->ctl_lock); 11894 STAILQ_FOREACH(lun, &softc->lun_list, links) 11895 retval += ctl_lun_reset(lun, io, ua_type); 11896 mtx_unlock(&softc->ctl_lock); 11897 11898 return (retval); 11899} 11900 11901/* 11902 * The LUN should always be set. The I/O is optional, and is used to 11903 * distinguish between I/Os sent by this initiator, and by other 11904 * initiators. We set unit attention for initiators other than this one. 11905 * SAM-3 is vague on this point. It does say that a unit attention should 11906 * be established for other initiators when a LUN is reset (see section 11907 * 5.7.3), but it doesn't specifically say that the unit attention should 11908 * be established for this particular initiator when a LUN is reset. Here 11909 * is the relevant text, from SAM-3 rev 8: 11910 * 11911 * 5.7.2 When a SCSI initiator port aborts its own tasks 11912 * 11913 * When a SCSI initiator port causes its own task(s) to be aborted, no 11914 * notification that the task(s) have been aborted shall be returned to 11915 * the SCSI initiator port other than the completion response for the 11916 * command or task management function action that caused the task(s) to 11917 * be aborted and notification(s) associated with related effects of the 11918 * action (e.g., a reset unit attention condition). 11919 * 11920 * XXX KDM for now, we're setting unit attention for all initiators. 11921 */ 11922static int 11923ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11924{ 11925 union ctl_io *xio; 11926#if 0 11927 uint32_t initidx; 11928#endif 11929#ifdef CTL_WITH_CA 11930 int i; 11931#endif 11932 11933 mtx_lock(&lun->lun_lock); 11934 /* 11935 * Run through the OOA queue and abort each I/O. 11936 */ 11937 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11938 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11939 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11940 } 11941 11942 /* 11943 * This version sets unit attention for every 11944 */ 11945#if 0 11946 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11947 ctl_est_ua_all(lun, initidx, ua_type); 11948#else 11949 ctl_est_ua_all(lun, -1, ua_type); 11950#endif 11951 11952 /* 11953 * A reset (any kind, really) clears reservations established with 11954 * RESERVE/RELEASE. It does not clear reservations established 11955 * with PERSISTENT RESERVE OUT, but we don't support that at the 11956 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11957 * reservations made with the RESERVE/RELEASE commands, because 11958 * those commands are obsolete in SPC-3. 11959 */ 11960 lun->flags &= ~CTL_LUN_RESERVED; 11961 11962#ifdef CTL_WITH_CA 11963 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11964 ctl_clear_mask(lun->have_ca, i); 11965#endif 11966 mtx_unlock(&lun->lun_lock); 11967 11968 return (0); 11969} 11970 11971static void 11972ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11973 int other_sc) 11974{ 11975 union ctl_io *xio; 11976 11977 mtx_assert(&lun->lun_lock, MA_OWNED); 11978 11979 /* 11980 * Run through the OOA queue and attempt to find the given I/O. 11981 * The target port, initiator ID, tag type and tag number have to 11982 * match the values that we got from the initiator. If we have an 11983 * untagged command to abort, simply abort the first untagged command 11984 * we come to. We only allow one untagged command at a time of course. 11985 */ 11986 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11987 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11988 11989 if ((targ_port == UINT32_MAX || 11990 targ_port == xio->io_hdr.nexus.targ_port) && 11991 (init_id == UINT32_MAX || 11992 init_id == xio->io_hdr.nexus.initid.id)) { 11993 if (targ_port != xio->io_hdr.nexus.targ_port || 11994 init_id != xio->io_hdr.nexus.initid.id) 11995 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11996 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11997 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11998 union ctl_ha_msg msg_info; 11999 12000 msg_info.hdr.nexus = xio->io_hdr.nexus; 12001 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12002 msg_info.task.tag_num = xio->scsiio.tag_num; 12003 msg_info.task.tag_type = xio->scsiio.tag_type; 12004 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12005 msg_info.hdr.original_sc = NULL; 12006 msg_info.hdr.serializing_sc = NULL; 12007 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12008 (void *)&msg_info, sizeof(msg_info), 0); 12009 } 12010 } 12011 } 12012} 12013 12014static int 12015ctl_abort_task_set(union ctl_io *io) 12016{ 12017 struct ctl_softc *softc = control_softc; 12018 struct ctl_lun *lun; 12019 uint32_t targ_lun; 12020 12021 /* 12022 * Look up the LUN. 12023 */ 12024 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12025 mtx_lock(&softc->ctl_lock); 12026 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12027 lun = softc->ctl_luns[targ_lun]; 12028 else { 12029 mtx_unlock(&softc->ctl_lock); 12030 return (1); 12031 } 12032 12033 mtx_lock(&lun->lun_lock); 12034 mtx_unlock(&softc->ctl_lock); 12035 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12036 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12037 io->io_hdr.nexus.initid.id, 12038 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12039 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12040 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12041 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12042 } 12043 mtx_unlock(&lun->lun_lock); 12044 return (0); 12045} 12046 12047static int 12048ctl_i_t_nexus_reset(union ctl_io *io) 12049{ 12050 struct ctl_softc *softc = control_softc; 12051 struct ctl_lun *lun; 12052 uint32_t initidx, residx; 12053 12054 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12055 residx = ctl_get_resindex(&io->io_hdr.nexus); 12056 mtx_lock(&softc->ctl_lock); 12057 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12058 mtx_lock(&lun->lun_lock); 12059 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12060 io->io_hdr.nexus.initid.id, 12061 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12062#ifdef CTL_WITH_CA 12063 ctl_clear_mask(lun->have_ca, initidx); 12064#endif 12065 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12066 lun->flags &= ~CTL_LUN_RESERVED; 12067 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12068 mtx_unlock(&lun->lun_lock); 12069 } 12070 mtx_unlock(&softc->ctl_lock); 12071 return (0); 12072} 12073 12074static int 12075ctl_abort_task(union ctl_io *io) 12076{ 12077 union ctl_io *xio; 12078 struct ctl_lun *lun; 12079 struct ctl_softc *softc; 12080#if 0 12081 struct sbuf sb; 12082 char printbuf[128]; 12083#endif 12084 int found; 12085 uint32_t targ_lun; 12086 12087 softc = control_softc; 12088 found = 0; 12089 12090 /* 12091 * Look up the LUN. 12092 */ 12093 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12094 mtx_lock(&softc->ctl_lock); 12095 if ((targ_lun < CTL_MAX_LUNS) 12096 && (softc->ctl_luns[targ_lun] != NULL)) 12097 lun = softc->ctl_luns[targ_lun]; 12098 else { 12099 mtx_unlock(&softc->ctl_lock); 12100 return (1); 12101 } 12102 12103#if 0 12104 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12105 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12106#endif 12107 12108 mtx_lock(&lun->lun_lock); 12109 mtx_unlock(&softc->ctl_lock); 12110 /* 12111 * Run through the OOA queue and attempt to find the given I/O. 12112 * The target port, initiator ID, tag type and tag number have to 12113 * match the values that we got from the initiator. If we have an 12114 * untagged command to abort, simply abort the first untagged command 12115 * we come to. We only allow one untagged command at a time of course. 12116 */ 12117 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12118 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12119#if 0 12120 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12121 12122 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12123 lun->lun, xio->scsiio.tag_num, 12124 xio->scsiio.tag_type, 12125 (xio->io_hdr.blocked_links.tqe_prev 12126 == NULL) ? "" : " BLOCKED", 12127 (xio->io_hdr.flags & 12128 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12129 (xio->io_hdr.flags & 12130 CTL_FLAG_ABORT) ? " ABORT" : "", 12131 (xio->io_hdr.flags & 12132 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12133 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12134 sbuf_finish(&sb); 12135 printf("%s\n", sbuf_data(&sb)); 12136#endif 12137 12138 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12139 || (xio->io_hdr.nexus.initid.id != io->io_hdr.nexus.initid.id) 12140 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12141 continue; 12142 12143 /* 12144 * If the abort says that the task is untagged, the 12145 * task in the queue must be untagged. Otherwise, 12146 * we just check to see whether the tag numbers 12147 * match. This is because the QLogic firmware 12148 * doesn't pass back the tag type in an abort 12149 * request. 12150 */ 12151#if 0 12152 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12153 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12154 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12155#endif 12156 /* 12157 * XXX KDM we've got problems with FC, because it 12158 * doesn't send down a tag type with aborts. So we 12159 * can only really go by the tag number... 12160 * This may cause problems with parallel SCSI. 12161 * Need to figure that out!! 12162 */ 12163 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12164 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12165 found = 1; 12166 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12167 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12168 union ctl_ha_msg msg_info; 12169 12170 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12171 msg_info.hdr.nexus = io->io_hdr.nexus; 12172 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12173 msg_info.task.tag_num = io->taskio.tag_num; 12174 msg_info.task.tag_type = io->taskio.tag_type; 12175 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12176 msg_info.hdr.original_sc = NULL; 12177 msg_info.hdr.serializing_sc = NULL; 12178#if 0 12179 printf("Sent Abort to other side\n"); 12180#endif 12181 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12182 (void *)&msg_info, sizeof(msg_info), 0) != 12183 CTL_HA_STATUS_SUCCESS) { 12184 } 12185 } 12186#if 0 12187 printf("ctl_abort_task: found I/O to abort\n"); 12188#endif 12189 } 12190 } 12191 mtx_unlock(&lun->lun_lock); 12192 12193 if (found == 0) { 12194 /* 12195 * This isn't really an error. It's entirely possible for 12196 * the abort and command completion to cross on the wire. 12197 * This is more of an informative/diagnostic error. 12198 */ 12199#if 0 12200 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12201 "%d:%d:%d:%d tag %d type %d\n", 12202 io->io_hdr.nexus.initid.id, 12203 io->io_hdr.nexus.targ_port, 12204 io->io_hdr.nexus.targ_target.id, 12205 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12206 io->taskio.tag_type); 12207#endif 12208 } 12209 return (0); 12210} 12211 12212static void 12213ctl_run_task(union ctl_io *io) 12214{ 12215 struct ctl_softc *softc = control_softc; 12216 int retval = 1; 12217 const char *task_desc; 12218 12219 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12220 12221 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12222 ("ctl_run_task: Unextected io_type %d\n", 12223 io->io_hdr.io_type)); 12224 12225 task_desc = ctl_scsi_task_string(&io->taskio); 12226 if (task_desc != NULL) { 12227#ifdef NEEDTOPORT 12228 csevent_log(CSC_CTL | CSC_SHELF_SW | 12229 CTL_TASK_REPORT, 12230 csevent_LogType_Trace, 12231 csevent_Severity_Information, 12232 csevent_AlertLevel_Green, 12233 csevent_FRU_Firmware, 12234 csevent_FRU_Unknown, 12235 "CTL: received task: %s",task_desc); 12236#endif 12237 } else { 12238#ifdef NEEDTOPORT 12239 csevent_log(CSC_CTL | CSC_SHELF_SW | 12240 CTL_TASK_REPORT, 12241 csevent_LogType_Trace, 12242 csevent_Severity_Information, 12243 csevent_AlertLevel_Green, 12244 csevent_FRU_Firmware, 12245 csevent_FRU_Unknown, 12246 "CTL: received unknown task " 12247 "type: %d (%#x)", 12248 io->taskio.task_action, 12249 io->taskio.task_action); 12250#endif 12251 } 12252 switch (io->taskio.task_action) { 12253 case CTL_TASK_ABORT_TASK: 12254 retval = ctl_abort_task(io); 12255 break; 12256 case CTL_TASK_ABORT_TASK_SET: 12257 case CTL_TASK_CLEAR_TASK_SET: 12258 retval = ctl_abort_task_set(io); 12259 break; 12260 case CTL_TASK_CLEAR_ACA: 12261 break; 12262 case CTL_TASK_I_T_NEXUS_RESET: 12263 retval = ctl_i_t_nexus_reset(io); 12264 break; 12265 case CTL_TASK_LUN_RESET: { 12266 struct ctl_lun *lun; 12267 uint32_t targ_lun; 12268 12269 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12270 mtx_lock(&softc->ctl_lock); 12271 if ((targ_lun < CTL_MAX_LUNS) 12272 && (softc->ctl_luns[targ_lun] != NULL)) 12273 lun = softc->ctl_luns[targ_lun]; 12274 else { 12275 mtx_unlock(&softc->ctl_lock); 12276 retval = 1; 12277 break; 12278 } 12279 12280 if (!(io->io_hdr.flags & 12281 CTL_FLAG_FROM_OTHER_SC)) { 12282 union ctl_ha_msg msg_info; 12283 12284 io->io_hdr.flags |= 12285 CTL_FLAG_SENT_2OTHER_SC; 12286 msg_info.hdr.msg_type = 12287 CTL_MSG_MANAGE_TASKS; 12288 msg_info.hdr.nexus = io->io_hdr.nexus; 12289 msg_info.task.task_action = 12290 CTL_TASK_LUN_RESET; 12291 msg_info.hdr.original_sc = NULL; 12292 msg_info.hdr.serializing_sc = NULL; 12293 if (CTL_HA_STATUS_SUCCESS != 12294 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12295 (void *)&msg_info, 12296 sizeof(msg_info), 0)) { 12297 } 12298 } 12299 12300 retval = ctl_lun_reset(lun, io, 12301 CTL_UA_LUN_RESET); 12302 mtx_unlock(&softc->ctl_lock); 12303 break; 12304 } 12305 case CTL_TASK_TARGET_RESET: 12306 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12307 break; 12308 case CTL_TASK_BUS_RESET: 12309 retval = ctl_bus_reset(softc, io); 12310 break; 12311 case CTL_TASK_PORT_LOGIN: 12312 break; 12313 case CTL_TASK_PORT_LOGOUT: 12314 break; 12315 default: 12316 printf("ctl_run_task: got unknown task management event %d\n", 12317 io->taskio.task_action); 12318 break; 12319 } 12320 if (retval == 0) 12321 io->io_hdr.status = CTL_SUCCESS; 12322 else 12323 io->io_hdr.status = CTL_ERROR; 12324 ctl_done(io); 12325} 12326 12327/* 12328 * For HA operation. Handle commands that come in from the other 12329 * controller. 12330 */ 12331static void 12332ctl_handle_isc(union ctl_io *io) 12333{ 12334 int free_io; 12335 struct ctl_lun *lun; 12336 struct ctl_softc *softc; 12337 uint32_t targ_lun; 12338 12339 softc = control_softc; 12340 12341 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12342 lun = softc->ctl_luns[targ_lun]; 12343 12344 switch (io->io_hdr.msg_type) { 12345 case CTL_MSG_SERIALIZE: 12346 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12347 break; 12348 case CTL_MSG_R2R: { 12349 const struct ctl_cmd_entry *entry; 12350 12351 /* 12352 * This is only used in SER_ONLY mode. 12353 */ 12354 free_io = 0; 12355 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12356 mtx_lock(&lun->lun_lock); 12357 if (ctl_scsiio_lun_check(lun, 12358 entry, (struct ctl_scsiio *)io) != 0) { 12359 mtx_unlock(&lun->lun_lock); 12360 ctl_done(io); 12361 break; 12362 } 12363 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12364 mtx_unlock(&lun->lun_lock); 12365 ctl_enqueue_rtr(io); 12366 break; 12367 } 12368 case CTL_MSG_FINISH_IO: 12369 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12370 free_io = 0; 12371 ctl_done(io); 12372 } else { 12373 free_io = 1; 12374 mtx_lock(&lun->lun_lock); 12375 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12376 ooa_links); 12377 ctl_check_blocked(lun); 12378 mtx_unlock(&lun->lun_lock); 12379 } 12380 break; 12381 case CTL_MSG_PERS_ACTION: 12382 ctl_hndl_per_res_out_on_other_sc( 12383 (union ctl_ha_msg *)&io->presio.pr_msg); 12384 free_io = 1; 12385 break; 12386 case CTL_MSG_BAD_JUJU: 12387 free_io = 0; 12388 ctl_done(io); 12389 break; 12390 case CTL_MSG_DATAMOVE: 12391 /* Only used in XFER mode */ 12392 free_io = 0; 12393 ctl_datamove_remote(io); 12394 break; 12395 case CTL_MSG_DATAMOVE_DONE: 12396 /* Only used in XFER mode */ 12397 free_io = 0; 12398 io->scsiio.be_move_done(io); 12399 break; 12400 default: 12401 free_io = 1; 12402 printf("%s: Invalid message type %d\n", 12403 __func__, io->io_hdr.msg_type); 12404 break; 12405 } 12406 if (free_io) 12407 ctl_free_io(io); 12408 12409} 12410 12411 12412/* 12413 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12414 * there is no match. 12415 */ 12416static ctl_lun_error_pattern 12417ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12418{ 12419 const struct ctl_cmd_entry *entry; 12420 ctl_lun_error_pattern filtered_pattern, pattern; 12421 12422 pattern = desc->error_pattern; 12423 12424 /* 12425 * XXX KDM we need more data passed into this function to match a 12426 * custom pattern, and we actually need to implement custom pattern 12427 * matching. 12428 */ 12429 if (pattern & CTL_LUN_PAT_CMD) 12430 return (CTL_LUN_PAT_CMD); 12431 12432 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12433 return (CTL_LUN_PAT_ANY); 12434 12435 entry = ctl_get_cmd_entry(ctsio, NULL); 12436 12437 filtered_pattern = entry->pattern & pattern; 12438 12439 /* 12440 * If the user requested specific flags in the pattern (e.g. 12441 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12442 * flags. 12443 * 12444 * If the user did not specify any flags, it doesn't matter whether 12445 * or not the command supports the flags. 12446 */ 12447 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12448 (pattern & ~CTL_LUN_PAT_MASK)) 12449 return (CTL_LUN_PAT_NONE); 12450 12451 /* 12452 * If the user asked for a range check, see if the requested LBA 12453 * range overlaps with this command's LBA range. 12454 */ 12455 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12456 uint64_t lba1; 12457 uint64_t len1; 12458 ctl_action action; 12459 int retval; 12460 12461 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12462 if (retval != 0) 12463 return (CTL_LUN_PAT_NONE); 12464 12465 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12466 desc->lba_range.len, FALSE); 12467 /* 12468 * A "pass" means that the LBA ranges don't overlap, so 12469 * this doesn't match the user's range criteria. 12470 */ 12471 if (action == CTL_ACTION_PASS) 12472 return (CTL_LUN_PAT_NONE); 12473 } 12474 12475 return (filtered_pattern); 12476} 12477 12478static void 12479ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12480{ 12481 struct ctl_error_desc *desc, *desc2; 12482 12483 mtx_assert(&lun->lun_lock, MA_OWNED); 12484 12485 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12486 ctl_lun_error_pattern pattern; 12487 /* 12488 * Check to see whether this particular command matches 12489 * the pattern in the descriptor. 12490 */ 12491 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12492 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12493 continue; 12494 12495 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12496 case CTL_LUN_INJ_ABORTED: 12497 ctl_set_aborted(&io->scsiio); 12498 break; 12499 case CTL_LUN_INJ_MEDIUM_ERR: 12500 ctl_set_medium_error(&io->scsiio); 12501 break; 12502 case CTL_LUN_INJ_UA: 12503 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12504 * OCCURRED */ 12505 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12506 break; 12507 case CTL_LUN_INJ_CUSTOM: 12508 /* 12509 * We're assuming the user knows what he is doing. 12510 * Just copy the sense information without doing 12511 * checks. 12512 */ 12513 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12514 MIN(sizeof(desc->custom_sense), 12515 sizeof(io->scsiio.sense_data))); 12516 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12517 io->scsiio.sense_len = SSD_FULL_SIZE; 12518 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12519 break; 12520 case CTL_LUN_INJ_NONE: 12521 default: 12522 /* 12523 * If this is an error injection type we don't know 12524 * about, clear the continuous flag (if it is set) 12525 * so it will get deleted below. 12526 */ 12527 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12528 break; 12529 } 12530 /* 12531 * By default, each error injection action is a one-shot 12532 */ 12533 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12534 continue; 12535 12536 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12537 12538 free(desc, M_CTL); 12539 } 12540} 12541 12542#ifdef CTL_IO_DELAY 12543static void 12544ctl_datamove_timer_wakeup(void *arg) 12545{ 12546 union ctl_io *io; 12547 12548 io = (union ctl_io *)arg; 12549 12550 ctl_datamove(io); 12551} 12552#endif /* CTL_IO_DELAY */ 12553 12554void 12555ctl_datamove(union ctl_io *io) 12556{ 12557 void (*fe_datamove)(union ctl_io *io); 12558 12559 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12560 12561 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12562 12563#ifdef CTL_TIME_IO 12564 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12565 char str[256]; 12566 char path_str[64]; 12567 struct sbuf sb; 12568 12569 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12570 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12571 12572 sbuf_cat(&sb, path_str); 12573 switch (io->io_hdr.io_type) { 12574 case CTL_IO_SCSI: 12575 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12576 sbuf_printf(&sb, "\n"); 12577 sbuf_cat(&sb, path_str); 12578 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12579 io->scsiio.tag_num, io->scsiio.tag_type); 12580 break; 12581 case CTL_IO_TASK: 12582 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12583 "Tag Type: %d\n", io->taskio.task_action, 12584 io->taskio.tag_num, io->taskio.tag_type); 12585 break; 12586 default: 12587 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12588 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12589 break; 12590 } 12591 sbuf_cat(&sb, path_str); 12592 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12593 (intmax_t)time_uptime - io->io_hdr.start_time); 12594 sbuf_finish(&sb); 12595 printf("%s", sbuf_data(&sb)); 12596 } 12597#endif /* CTL_TIME_IO */ 12598 12599#ifdef CTL_IO_DELAY 12600 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12601 struct ctl_lun *lun; 12602 12603 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12604 12605 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12606 } else { 12607 struct ctl_lun *lun; 12608 12609 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12610 if ((lun != NULL) 12611 && (lun->delay_info.datamove_delay > 0)) { 12612 struct callout *callout; 12613 12614 callout = (struct callout *)&io->io_hdr.timer_bytes; 12615 callout_init(callout, /*mpsafe*/ 1); 12616 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12617 callout_reset(callout, 12618 lun->delay_info.datamove_delay * hz, 12619 ctl_datamove_timer_wakeup, io); 12620 if (lun->delay_info.datamove_type == 12621 CTL_DELAY_TYPE_ONESHOT) 12622 lun->delay_info.datamove_delay = 0; 12623 return; 12624 } 12625 } 12626#endif 12627 12628 /* 12629 * This command has been aborted. Set the port status, so we fail 12630 * the data move. 12631 */ 12632 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12633 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12634 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12635 io->io_hdr.nexus.targ_port, 12636 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12637 io->io_hdr.nexus.targ_lun); 12638 io->io_hdr.port_status = 31337; 12639 /* 12640 * Note that the backend, in this case, will get the 12641 * callback in its context. In other cases it may get 12642 * called in the frontend's interrupt thread context. 12643 */ 12644 io->scsiio.be_move_done(io); 12645 return; 12646 } 12647 12648 /* Don't confuse frontend with zero length data move. */ 12649 if (io->scsiio.kern_data_len == 0) { 12650 io->scsiio.be_move_done(io); 12651 return; 12652 } 12653 12654 /* 12655 * If we're in XFER mode and this I/O is from the other shelf 12656 * controller, we need to send the DMA to the other side to 12657 * actually transfer the data to/from the host. In serialize only 12658 * mode the transfer happens below CTL and ctl_datamove() is only 12659 * called on the machine that originally received the I/O. 12660 */ 12661 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12662 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12663 union ctl_ha_msg msg; 12664 uint32_t sg_entries_sent; 12665 int do_sg_copy; 12666 int i; 12667 12668 memset(&msg, 0, sizeof(msg)); 12669 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12670 msg.hdr.original_sc = io->io_hdr.original_sc; 12671 msg.hdr.serializing_sc = io; 12672 msg.hdr.nexus = io->io_hdr.nexus; 12673 msg.dt.flags = io->io_hdr.flags; 12674 /* 12675 * We convert everything into a S/G list here. We can't 12676 * pass by reference, only by value between controllers. 12677 * So we can't pass a pointer to the S/G list, only as many 12678 * S/G entries as we can fit in here. If it's possible for 12679 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12680 * then we need to break this up into multiple transfers. 12681 */ 12682 if (io->scsiio.kern_sg_entries == 0) { 12683 msg.dt.kern_sg_entries = 1; 12684 /* 12685 * If this is in cached memory, flush the cache 12686 * before we send the DMA request to the other 12687 * controller. We want to do this in either the 12688 * read or the write case. The read case is 12689 * straightforward. In the write case, we want to 12690 * make sure nothing is in the local cache that 12691 * could overwrite the DMAed data. 12692 */ 12693 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12694 /* 12695 * XXX KDM use bus_dmamap_sync() here. 12696 */ 12697 } 12698 12699 /* 12700 * Convert to a physical address if this is a 12701 * virtual address. 12702 */ 12703 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12704 msg.dt.sg_list[0].addr = 12705 io->scsiio.kern_data_ptr; 12706 } else { 12707 /* 12708 * XXX KDM use busdma here! 12709 */ 12710#if 0 12711 msg.dt.sg_list[0].addr = (void *) 12712 vtophys(io->scsiio.kern_data_ptr); 12713#endif 12714 } 12715 12716 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12717 do_sg_copy = 0; 12718 } else { 12719 struct ctl_sg_entry *sgl; 12720 12721 do_sg_copy = 1; 12722 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12723 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12724 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12725 /* 12726 * XXX KDM use bus_dmamap_sync() here. 12727 */ 12728 } 12729 } 12730 12731 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12732 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12733 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12734 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12735 msg.dt.sg_sequence = 0; 12736 12737 /* 12738 * Loop until we've sent all of the S/G entries. On the 12739 * other end, we'll recompose these S/G entries into one 12740 * contiguous list before passing it to the 12741 */ 12742 for (sg_entries_sent = 0; sg_entries_sent < 12743 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12744 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/ 12745 sizeof(msg.dt.sg_list[0])), 12746 msg.dt.kern_sg_entries - sg_entries_sent); 12747 12748 if (do_sg_copy != 0) { 12749 struct ctl_sg_entry *sgl; 12750 int j; 12751 12752 sgl = (struct ctl_sg_entry *) 12753 io->scsiio.kern_data_ptr; 12754 /* 12755 * If this is in cached memory, flush the cache 12756 * before we send the DMA request to the other 12757 * controller. We want to do this in either 12758 * the * read or the write case. The read 12759 * case is straightforward. In the write 12760 * case, we want to make sure nothing is 12761 * in the local cache that could overwrite 12762 * the DMAed data. 12763 */ 12764 12765 for (i = sg_entries_sent, j = 0; 12766 i < msg.dt.cur_sg_entries; i++, j++) { 12767 if ((io->io_hdr.flags & 12768 CTL_FLAG_NO_DATASYNC) == 0) { 12769 /* 12770 * XXX KDM use bus_dmamap_sync() 12771 */ 12772 } 12773 if ((io->io_hdr.flags & 12774 CTL_FLAG_BUS_ADDR) == 0) { 12775 /* 12776 * XXX KDM use busdma. 12777 */ 12778#if 0 12779 msg.dt.sg_list[j].addr =(void *) 12780 vtophys(sgl[i].addr); 12781#endif 12782 } else { 12783 msg.dt.sg_list[j].addr = 12784 sgl[i].addr; 12785 } 12786 msg.dt.sg_list[j].len = sgl[i].len; 12787 } 12788 } 12789 12790 sg_entries_sent += msg.dt.cur_sg_entries; 12791 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12792 msg.dt.sg_last = 1; 12793 else 12794 msg.dt.sg_last = 0; 12795 12796 /* 12797 * XXX KDM drop and reacquire the lock here? 12798 */ 12799 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12800 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12801 /* 12802 * XXX do something here. 12803 */ 12804 } 12805 12806 msg.dt.sent_sg_entries = sg_entries_sent; 12807 } 12808 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12809 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12810 ctl_failover_io(io, /*have_lock*/ 0); 12811 12812 } else { 12813 12814 /* 12815 * Lookup the fe_datamove() function for this particular 12816 * front end. 12817 */ 12818 fe_datamove = 12819 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12820 12821 fe_datamove(io); 12822 } 12823} 12824 12825static void 12826ctl_send_datamove_done(union ctl_io *io, int have_lock) 12827{ 12828 union ctl_ha_msg msg; 12829 int isc_status; 12830 12831 memset(&msg, 0, sizeof(msg)); 12832 12833 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12834 msg.hdr.original_sc = io; 12835 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12836 msg.hdr.nexus = io->io_hdr.nexus; 12837 msg.hdr.status = io->io_hdr.status; 12838 msg.scsi.tag_num = io->scsiio.tag_num; 12839 msg.scsi.tag_type = io->scsiio.tag_type; 12840 msg.scsi.scsi_status = io->scsiio.scsi_status; 12841 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12842 sizeof(io->scsiio.sense_data)); 12843 msg.scsi.sense_len = io->scsiio.sense_len; 12844 msg.scsi.sense_residual = io->scsiio.sense_residual; 12845 msg.scsi.fetd_status = io->io_hdr.port_status; 12846 msg.scsi.residual = io->scsiio.residual; 12847 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12848 12849 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12850 ctl_failover_io(io, /*have_lock*/ have_lock); 12851 return; 12852 } 12853 12854 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12855 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12856 /* XXX do something if this fails */ 12857 } 12858 12859} 12860 12861/* 12862 * The DMA to the remote side is done, now we need to tell the other side 12863 * we're done so it can continue with its data movement. 12864 */ 12865static void 12866ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12867{ 12868 union ctl_io *io; 12869 12870 io = rq->context; 12871 12872 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12873 printf("%s: ISC DMA write failed with error %d", __func__, 12874 rq->ret); 12875 ctl_set_internal_failure(&io->scsiio, 12876 /*sks_valid*/ 1, 12877 /*retry_count*/ rq->ret); 12878 } 12879 12880 ctl_dt_req_free(rq); 12881 12882 /* 12883 * In this case, we had to malloc the memory locally. Free it. 12884 */ 12885 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12886 int i; 12887 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12888 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12889 } 12890 /* 12891 * The data is in local and remote memory, so now we need to send 12892 * status (good or back) back to the other side. 12893 */ 12894 ctl_send_datamove_done(io, /*have_lock*/ 0); 12895} 12896 12897/* 12898 * We've moved the data from the host/controller into local memory. Now we 12899 * need to push it over to the remote controller's memory. 12900 */ 12901static int 12902ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12903{ 12904 int retval; 12905 12906 retval = 0; 12907 12908 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12909 ctl_datamove_remote_write_cb); 12910 12911 return (retval); 12912} 12913 12914static void 12915ctl_datamove_remote_write(union ctl_io *io) 12916{ 12917 int retval; 12918 void (*fe_datamove)(union ctl_io *io); 12919 12920 /* 12921 * - Get the data from the host/HBA into local memory. 12922 * - DMA memory from the local controller to the remote controller. 12923 * - Send status back to the remote controller. 12924 */ 12925 12926 retval = ctl_datamove_remote_sgl_setup(io); 12927 if (retval != 0) 12928 return; 12929 12930 /* Switch the pointer over so the FETD knows what to do */ 12931 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12932 12933 /* 12934 * Use a custom move done callback, since we need to send completion 12935 * back to the other controller, not to the backend on this side. 12936 */ 12937 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12938 12939 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12940 12941 fe_datamove(io); 12942 12943 return; 12944 12945} 12946 12947static int 12948ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12949{ 12950#if 0 12951 char str[256]; 12952 char path_str[64]; 12953 struct sbuf sb; 12954#endif 12955 12956 /* 12957 * In this case, we had to malloc the memory locally. Free it. 12958 */ 12959 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12960 int i; 12961 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12962 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12963 } 12964 12965#if 0 12966 scsi_path_string(io, path_str, sizeof(path_str)); 12967 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12968 sbuf_cat(&sb, path_str); 12969 scsi_command_string(&io->scsiio, NULL, &sb); 12970 sbuf_printf(&sb, "\n"); 12971 sbuf_cat(&sb, path_str); 12972 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12973 io->scsiio.tag_num, io->scsiio.tag_type); 12974 sbuf_cat(&sb, path_str); 12975 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12976 io->io_hdr.flags, io->io_hdr.status); 12977 sbuf_finish(&sb); 12978 printk("%s", sbuf_data(&sb)); 12979#endif 12980 12981 12982 /* 12983 * The read is done, now we need to send status (good or bad) back 12984 * to the other side. 12985 */ 12986 ctl_send_datamove_done(io, /*have_lock*/ 0); 12987 12988 return (0); 12989} 12990 12991static void 12992ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12993{ 12994 union ctl_io *io; 12995 void (*fe_datamove)(union ctl_io *io); 12996 12997 io = rq->context; 12998 12999 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13000 printf("%s: ISC DMA read failed with error %d", __func__, 13001 rq->ret); 13002 ctl_set_internal_failure(&io->scsiio, 13003 /*sks_valid*/ 1, 13004 /*retry_count*/ rq->ret); 13005 } 13006 13007 ctl_dt_req_free(rq); 13008 13009 /* Switch the pointer over so the FETD knows what to do */ 13010 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13011 13012 /* 13013 * Use a custom move done callback, since we need to send completion 13014 * back to the other controller, not to the backend on this side. 13015 */ 13016 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13017 13018 /* XXX KDM add checks like the ones in ctl_datamove? */ 13019 13020 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13021 13022 fe_datamove(io); 13023} 13024 13025static int 13026ctl_datamove_remote_sgl_setup(union ctl_io *io) 13027{ 13028 struct ctl_sg_entry *local_sglist, *remote_sglist; 13029 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13030 struct ctl_softc *softc; 13031 int retval; 13032 int i; 13033 13034 retval = 0; 13035 softc = control_softc; 13036 13037 local_sglist = io->io_hdr.local_sglist; 13038 local_dma_sglist = io->io_hdr.local_dma_sglist; 13039 remote_sglist = io->io_hdr.remote_sglist; 13040 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13041 13042 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13043 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13044 local_sglist[i].len = remote_sglist[i].len; 13045 13046 /* 13047 * XXX Detect the situation where the RS-level I/O 13048 * redirector on the other side has already read the 13049 * data off of the AOR RS on this side, and 13050 * transferred it to remote (mirror) memory on the 13051 * other side. Since we already have the data in 13052 * memory here, we just need to use it. 13053 * 13054 * XXX KDM this can probably be removed once we 13055 * get the cache device code in and take the 13056 * current AOR implementation out. 13057 */ 13058#ifdef NEEDTOPORT 13059 if ((remote_sglist[i].addr >= 13060 (void *)vtophys(softc->mirr->addr)) 13061 && (remote_sglist[i].addr < 13062 ((void *)vtophys(softc->mirr->addr) + 13063 CacheMirrorOffset))) { 13064 local_sglist[i].addr = remote_sglist[i].addr - 13065 CacheMirrorOffset; 13066 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13067 CTL_FLAG_DATA_IN) 13068 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13069 } else { 13070 local_sglist[i].addr = remote_sglist[i].addr + 13071 CacheMirrorOffset; 13072 } 13073#endif 13074#if 0 13075 printf("%s: local %p, remote %p, len %d\n", 13076 __func__, local_sglist[i].addr, 13077 remote_sglist[i].addr, local_sglist[i].len); 13078#endif 13079 } 13080 } else { 13081 uint32_t len_to_go; 13082 13083 /* 13084 * In this case, we don't have automatically allocated 13085 * memory for this I/O on this controller. This typically 13086 * happens with internal CTL I/O -- e.g. inquiry, mode 13087 * sense, etc. Anything coming from RAIDCore will have 13088 * a mirror area available. 13089 */ 13090 len_to_go = io->scsiio.kern_data_len; 13091 13092 /* 13093 * Clear the no datasync flag, we have to use malloced 13094 * buffers. 13095 */ 13096 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13097 13098 /* 13099 * The difficult thing here is that the size of the various 13100 * S/G segments may be different than the size from the 13101 * remote controller. That'll make it harder when DMAing 13102 * the data back to the other side. 13103 */ 13104 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13105 sizeof(io->io_hdr.remote_sglist[0])) && 13106 (len_to_go > 0); i++) { 13107 local_sglist[i].len = MIN(len_to_go, 131072); 13108 CTL_SIZE_8B(local_dma_sglist[i].len, 13109 local_sglist[i].len); 13110 local_sglist[i].addr = 13111 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13112 13113 local_dma_sglist[i].addr = local_sglist[i].addr; 13114 13115 if (local_sglist[i].addr == NULL) { 13116 int j; 13117 13118 printf("malloc failed for %zd bytes!", 13119 local_dma_sglist[i].len); 13120 for (j = 0; j < i; j++) { 13121 free(local_sglist[j].addr, M_CTL); 13122 } 13123 ctl_set_internal_failure(&io->scsiio, 13124 /*sks_valid*/ 1, 13125 /*retry_count*/ 4857); 13126 retval = 1; 13127 goto bailout_error; 13128 13129 } 13130 /* XXX KDM do we need a sync here? */ 13131 13132 len_to_go -= local_sglist[i].len; 13133 } 13134 /* 13135 * Reset the number of S/G entries accordingly. The 13136 * original number of S/G entries is available in 13137 * rem_sg_entries. 13138 */ 13139 io->scsiio.kern_sg_entries = i; 13140 13141#if 0 13142 printf("%s: kern_sg_entries = %d\n", __func__, 13143 io->scsiio.kern_sg_entries); 13144 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13145 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13146 local_sglist[i].addr, local_sglist[i].len, 13147 local_dma_sglist[i].len); 13148#endif 13149 } 13150 13151 13152 return (retval); 13153 13154bailout_error: 13155 13156 ctl_send_datamove_done(io, /*have_lock*/ 0); 13157 13158 return (retval); 13159} 13160 13161static int 13162ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13163 ctl_ha_dt_cb callback) 13164{ 13165 struct ctl_ha_dt_req *rq; 13166 struct ctl_sg_entry *remote_sglist, *local_sglist; 13167 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13168 uint32_t local_used, remote_used, total_used; 13169 int retval; 13170 int i, j; 13171 13172 retval = 0; 13173 13174 rq = ctl_dt_req_alloc(); 13175 13176 /* 13177 * If we failed to allocate the request, and if the DMA didn't fail 13178 * anyway, set busy status. This is just a resource allocation 13179 * failure. 13180 */ 13181 if ((rq == NULL) 13182 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13183 ctl_set_busy(&io->scsiio); 13184 13185 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13186 13187 if (rq != NULL) 13188 ctl_dt_req_free(rq); 13189 13190 /* 13191 * The data move failed. We need to return status back 13192 * to the other controller. No point in trying to DMA 13193 * data to the remote controller. 13194 */ 13195 13196 ctl_send_datamove_done(io, /*have_lock*/ 0); 13197 13198 retval = 1; 13199 13200 goto bailout; 13201 } 13202 13203 local_sglist = io->io_hdr.local_sglist; 13204 local_dma_sglist = io->io_hdr.local_dma_sglist; 13205 remote_sglist = io->io_hdr.remote_sglist; 13206 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13207 local_used = 0; 13208 remote_used = 0; 13209 total_used = 0; 13210 13211 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13212 rq->ret = CTL_HA_STATUS_SUCCESS; 13213 rq->context = io; 13214 callback(rq); 13215 goto bailout; 13216 } 13217 13218 /* 13219 * Pull/push the data over the wire from/to the other controller. 13220 * This takes into account the possibility that the local and 13221 * remote sglists may not be identical in terms of the size of 13222 * the elements and the number of elements. 13223 * 13224 * One fundamental assumption here is that the length allocated for 13225 * both the local and remote sglists is identical. Otherwise, we've 13226 * essentially got a coding error of some sort. 13227 */ 13228 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13229 int isc_ret; 13230 uint32_t cur_len, dma_length; 13231 uint8_t *tmp_ptr; 13232 13233 rq->id = CTL_HA_DATA_CTL; 13234 rq->command = command; 13235 rq->context = io; 13236 13237 /* 13238 * Both pointers should be aligned. But it is possible 13239 * that the allocation length is not. They should both 13240 * also have enough slack left over at the end, though, 13241 * to round up to the next 8 byte boundary. 13242 */ 13243 cur_len = MIN(local_sglist[i].len - local_used, 13244 remote_sglist[j].len - remote_used); 13245 13246 /* 13247 * In this case, we have a size issue and need to decrease 13248 * the size, except in the case where we actually have less 13249 * than 8 bytes left. In that case, we need to increase 13250 * the DMA length to get the last bit. 13251 */ 13252 if ((cur_len & 0x7) != 0) { 13253 if (cur_len > 0x7) { 13254 cur_len = cur_len - (cur_len & 0x7); 13255 dma_length = cur_len; 13256 } else { 13257 CTL_SIZE_8B(dma_length, cur_len); 13258 } 13259 13260 } else 13261 dma_length = cur_len; 13262 13263 /* 13264 * If we had to allocate memory for this I/O, instead of using 13265 * the non-cached mirror memory, we'll need to flush the cache 13266 * before trying to DMA to the other controller. 13267 * 13268 * We could end up doing this multiple times for the same 13269 * segment if we have a larger local segment than remote 13270 * segment. That shouldn't be an issue. 13271 */ 13272 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13273 /* 13274 * XXX KDM use bus_dmamap_sync() here. 13275 */ 13276 } 13277 13278 rq->size = dma_length; 13279 13280 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13281 tmp_ptr += local_used; 13282 13283 /* Use physical addresses when talking to ISC hardware */ 13284 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13285 /* XXX KDM use busdma */ 13286#if 0 13287 rq->local = vtophys(tmp_ptr); 13288#endif 13289 } else 13290 rq->local = tmp_ptr; 13291 13292 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13293 tmp_ptr += remote_used; 13294 rq->remote = tmp_ptr; 13295 13296 rq->callback = NULL; 13297 13298 local_used += cur_len; 13299 if (local_used >= local_sglist[i].len) { 13300 i++; 13301 local_used = 0; 13302 } 13303 13304 remote_used += cur_len; 13305 if (remote_used >= remote_sglist[j].len) { 13306 j++; 13307 remote_used = 0; 13308 } 13309 total_used += cur_len; 13310 13311 if (total_used >= io->scsiio.kern_data_len) 13312 rq->callback = callback; 13313 13314 if ((rq->size & 0x7) != 0) { 13315 printf("%s: warning: size %d is not on 8b boundary\n", 13316 __func__, rq->size); 13317 } 13318 if (((uintptr_t)rq->local & 0x7) != 0) { 13319 printf("%s: warning: local %p not on 8b boundary\n", 13320 __func__, rq->local); 13321 } 13322 if (((uintptr_t)rq->remote & 0x7) != 0) { 13323 printf("%s: warning: remote %p not on 8b boundary\n", 13324 __func__, rq->local); 13325 } 13326#if 0 13327 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13328 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13329 rq->local, rq->remote, rq->size); 13330#endif 13331 13332 isc_ret = ctl_dt_single(rq); 13333 if (isc_ret == CTL_HA_STATUS_WAIT) 13334 continue; 13335 13336 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13337 rq->ret = CTL_HA_STATUS_SUCCESS; 13338 } else { 13339 rq->ret = isc_ret; 13340 } 13341 callback(rq); 13342 goto bailout; 13343 } 13344 13345bailout: 13346 return (retval); 13347 13348} 13349 13350static void 13351ctl_datamove_remote_read(union ctl_io *io) 13352{ 13353 int retval; 13354 int i; 13355 13356 /* 13357 * This will send an error to the other controller in the case of a 13358 * failure. 13359 */ 13360 retval = ctl_datamove_remote_sgl_setup(io); 13361 if (retval != 0) 13362 return; 13363 13364 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13365 ctl_datamove_remote_read_cb); 13366 if ((retval != 0) 13367 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13368 /* 13369 * Make sure we free memory if there was an error.. The 13370 * ctl_datamove_remote_xfer() function will send the 13371 * datamove done message, or call the callback with an 13372 * error if there is a problem. 13373 */ 13374 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13375 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13376 } 13377 13378 return; 13379} 13380 13381/* 13382 * Process a datamove request from the other controller. This is used for 13383 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13384 * first. Once that is complete, the data gets DMAed into the remote 13385 * controller's memory. For reads, we DMA from the remote controller's 13386 * memory into our memory first, and then move it out to the FETD. 13387 */ 13388static void 13389ctl_datamove_remote(union ctl_io *io) 13390{ 13391 struct ctl_softc *softc; 13392 13393 softc = control_softc; 13394 13395 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13396 13397 /* 13398 * Note that we look for an aborted I/O here, but don't do some of 13399 * the other checks that ctl_datamove() normally does. 13400 * We don't need to run the datamove delay code, since that should 13401 * have been done if need be on the other controller. 13402 */ 13403 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13404 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13405 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13406 io->io_hdr.nexus.targ_port, 13407 io->io_hdr.nexus.targ_target.id, 13408 io->io_hdr.nexus.targ_lun); 13409 io->io_hdr.port_status = 31338; 13410 ctl_send_datamove_done(io, /*have_lock*/ 0); 13411 return; 13412 } 13413 13414 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13415 ctl_datamove_remote_write(io); 13416 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13417 ctl_datamove_remote_read(io); 13418 } else { 13419 union ctl_ha_msg msg; 13420 struct scsi_sense_data *sense; 13421 uint8_t sks[3]; 13422 int retry_count; 13423 13424 memset(&msg, 0, sizeof(msg)); 13425 13426 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13427 msg.hdr.status = CTL_SCSI_ERROR; 13428 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13429 13430 retry_count = 4243; 13431 13432 sense = &msg.scsi.sense_data; 13433 sks[0] = SSD_SCS_VALID; 13434 sks[1] = (retry_count >> 8) & 0xff; 13435 sks[2] = retry_count & 0xff; 13436 13437 /* "Internal target failure" */ 13438 scsi_set_sense_data(sense, 13439 /*sense_format*/ SSD_TYPE_NONE, 13440 /*current_error*/ 1, 13441 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13442 /*asc*/ 0x44, 13443 /*ascq*/ 0x00, 13444 /*type*/ SSD_ELEM_SKS, 13445 /*size*/ sizeof(sks), 13446 /*data*/ sks, 13447 SSD_ELEM_NONE); 13448 13449 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13450 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13451 ctl_failover_io(io, /*have_lock*/ 1); 13452 return; 13453 } 13454 13455 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13456 CTL_HA_STATUS_SUCCESS) { 13457 /* XXX KDM what to do if this fails? */ 13458 } 13459 return; 13460 } 13461 13462} 13463 13464static int 13465ctl_process_done(union ctl_io *io) 13466{ 13467 struct ctl_lun *lun; 13468 struct ctl_softc *softc = control_softc; 13469 void (*fe_done)(union ctl_io *io); 13470 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13471 13472 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13473 13474 fe_done = softc->ctl_ports[targ_port]->fe_done; 13475 13476#ifdef CTL_TIME_IO 13477 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13478 char str[256]; 13479 char path_str[64]; 13480 struct sbuf sb; 13481 13482 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13483 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13484 13485 sbuf_cat(&sb, path_str); 13486 switch (io->io_hdr.io_type) { 13487 case CTL_IO_SCSI: 13488 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13489 sbuf_printf(&sb, "\n"); 13490 sbuf_cat(&sb, path_str); 13491 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13492 io->scsiio.tag_num, io->scsiio.tag_type); 13493 break; 13494 case CTL_IO_TASK: 13495 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13496 "Tag Type: %d\n", io->taskio.task_action, 13497 io->taskio.tag_num, io->taskio.tag_type); 13498 break; 13499 default: 13500 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13501 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13502 break; 13503 } 13504 sbuf_cat(&sb, path_str); 13505 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13506 (intmax_t)time_uptime - io->io_hdr.start_time); 13507 sbuf_finish(&sb); 13508 printf("%s", sbuf_data(&sb)); 13509 } 13510#endif /* CTL_TIME_IO */ 13511 13512 switch (io->io_hdr.io_type) { 13513 case CTL_IO_SCSI: 13514 break; 13515 case CTL_IO_TASK: 13516 if (ctl_debug & CTL_DEBUG_INFO) 13517 ctl_io_error_print(io, NULL); 13518 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13519 ctl_free_io(io); 13520 else 13521 fe_done(io); 13522 return (CTL_RETVAL_COMPLETE); 13523 default: 13524 panic("ctl_process_done: invalid io type %d\n", 13525 io->io_hdr.io_type); 13526 break; /* NOTREACHED */ 13527 } 13528 13529 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13530 if (lun == NULL) { 13531 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13532 io->io_hdr.nexus.targ_mapped_lun)); 13533 goto bailout; 13534 } 13535 13536 mtx_lock(&lun->lun_lock); 13537 13538 /* 13539 * Check to see if we have any errors to inject here. We only 13540 * inject errors for commands that don't already have errors set. 13541 */ 13542 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13543 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13544 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13545 ctl_inject_error(lun, io); 13546 13547 /* 13548 * XXX KDM how do we treat commands that aren't completed 13549 * successfully? 13550 * 13551 * XXX KDM should we also track I/O latency? 13552 */ 13553 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13554 io->io_hdr.io_type == CTL_IO_SCSI) { 13555#ifdef CTL_TIME_IO 13556 struct bintime cur_bt; 13557#endif 13558 int type; 13559 13560 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13561 CTL_FLAG_DATA_IN) 13562 type = CTL_STATS_READ; 13563 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13564 CTL_FLAG_DATA_OUT) 13565 type = CTL_STATS_WRITE; 13566 else 13567 type = CTL_STATS_NO_IO; 13568 13569 lun->stats.ports[targ_port].bytes[type] += 13570 io->scsiio.kern_total_len; 13571 lun->stats.ports[targ_port].operations[type]++; 13572#ifdef CTL_TIME_IO 13573 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13574 &io->io_hdr.dma_bt); 13575 lun->stats.ports[targ_port].num_dmas[type] += 13576 io->io_hdr.num_dmas; 13577 getbintime(&cur_bt); 13578 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13579 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13580#endif 13581 } 13582 13583 /* 13584 * Remove this from the OOA queue. 13585 */ 13586 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13587#ifdef CTL_TIME_IO 13588 if (TAILQ_EMPTY(&lun->ooa_queue)) 13589 lun->last_busy = getsbinuptime(); 13590#endif 13591 13592 /* 13593 * Run through the blocked queue on this LUN and see if anything 13594 * has become unblocked, now that this transaction is done. 13595 */ 13596 ctl_check_blocked(lun); 13597 13598 /* 13599 * If the LUN has been invalidated, free it if there is nothing 13600 * left on its OOA queue. 13601 */ 13602 if ((lun->flags & CTL_LUN_INVALID) 13603 && TAILQ_EMPTY(&lun->ooa_queue)) { 13604 mtx_unlock(&lun->lun_lock); 13605 mtx_lock(&softc->ctl_lock); 13606 ctl_free_lun(lun); 13607 mtx_unlock(&softc->ctl_lock); 13608 } else 13609 mtx_unlock(&lun->lun_lock); 13610 13611bailout: 13612 13613 /* 13614 * If this command has been aborted, make sure we set the status 13615 * properly. The FETD is responsible for freeing the I/O and doing 13616 * whatever it needs to do to clean up its state. 13617 */ 13618 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13619 ctl_set_task_aborted(&io->scsiio); 13620 13621 /* 13622 * If enabled, print command error status. 13623 */ 13624 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13625 (ctl_debug & CTL_DEBUG_INFO) != 0) 13626 ctl_io_error_print(io, NULL); 13627 13628 /* 13629 * Tell the FETD or the other shelf controller we're done with this 13630 * command. Note that only SCSI commands get to this point. Task 13631 * management commands are completed above. 13632 * 13633 * We only send status to the other controller if we're in XFER 13634 * mode. In SER_ONLY mode, the I/O is done on the controller that 13635 * received the I/O (from CTL's perspective), and so the status is 13636 * generated there. 13637 * 13638 * XXX KDM if we hold the lock here, we could cause a deadlock 13639 * if the frontend comes back in in this context to queue 13640 * something. 13641 */ 13642 if ((softc->ha_mode == CTL_HA_MODE_XFER) 13643 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13644 union ctl_ha_msg msg; 13645 13646 memset(&msg, 0, sizeof(msg)); 13647 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13648 msg.hdr.original_sc = io->io_hdr.original_sc; 13649 msg.hdr.nexus = io->io_hdr.nexus; 13650 msg.hdr.status = io->io_hdr.status; 13651 msg.scsi.scsi_status = io->scsiio.scsi_status; 13652 msg.scsi.tag_num = io->scsiio.tag_num; 13653 msg.scsi.tag_type = io->scsiio.tag_type; 13654 msg.scsi.sense_len = io->scsiio.sense_len; 13655 msg.scsi.sense_residual = io->scsiio.sense_residual; 13656 msg.scsi.residual = io->scsiio.residual; 13657 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13658 sizeof(io->scsiio.sense_data)); 13659 /* 13660 * We copy this whether or not this is an I/O-related 13661 * command. Otherwise, we'd have to go and check to see 13662 * whether it's a read/write command, and it really isn't 13663 * worth it. 13664 */ 13665 memcpy(&msg.scsi.lbalen, 13666 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13667 sizeof(msg.scsi.lbalen)); 13668 13669 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13670 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13671 /* XXX do something here */ 13672 } 13673 13674 ctl_free_io(io); 13675 } else 13676 fe_done(io); 13677 13678 return (CTL_RETVAL_COMPLETE); 13679} 13680 13681#ifdef CTL_WITH_CA 13682/* 13683 * Front end should call this if it doesn't do autosense. When the request 13684 * sense comes back in from the initiator, we'll dequeue this and send it. 13685 */ 13686int 13687ctl_queue_sense(union ctl_io *io) 13688{ 13689 struct ctl_lun *lun; 13690 struct ctl_port *port; 13691 struct ctl_softc *softc; 13692 uint32_t initidx, targ_lun; 13693 13694 softc = control_softc; 13695 13696 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13697 13698 /* 13699 * LUN lookup will likely move to the ctl_work_thread() once we 13700 * have our new queueing infrastructure (that doesn't put things on 13701 * a per-LUN queue initially). That is so that we can handle 13702 * things like an INQUIRY to a LUN that we don't have enabled. We 13703 * can't deal with that right now. 13704 */ 13705 mtx_lock(&softc->ctl_lock); 13706 13707 /* 13708 * If we don't have a LUN for this, just toss the sense 13709 * information. 13710 */ 13711 port = ctl_io_port(&ctsio->io_hdr); 13712 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13713 if ((targ_lun < CTL_MAX_LUNS) 13714 && (softc->ctl_luns[targ_lun] != NULL)) 13715 lun = softc->ctl_luns[targ_lun]; 13716 else 13717 goto bailout; 13718 13719 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13720 13721 mtx_lock(&lun->lun_lock); 13722 /* 13723 * Already have CA set for this LUN...toss the sense information. 13724 */ 13725 if (ctl_is_set(lun->have_ca, initidx)) { 13726 mtx_unlock(&lun->lun_lock); 13727 goto bailout; 13728 } 13729 13730 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13731 MIN(sizeof(lun->pending_sense[initidx]), 13732 sizeof(io->scsiio.sense_data))); 13733 ctl_set_mask(lun->have_ca, initidx); 13734 mtx_unlock(&lun->lun_lock); 13735 13736bailout: 13737 mtx_unlock(&softc->ctl_lock); 13738 13739 ctl_free_io(io); 13740 13741 return (CTL_RETVAL_COMPLETE); 13742} 13743#endif 13744 13745/* 13746 * Primary command inlet from frontend ports. All SCSI and task I/O 13747 * requests must go through this function. 13748 */ 13749int 13750ctl_queue(union ctl_io *io) 13751{ 13752 struct ctl_port *port; 13753 13754 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13755 13756#ifdef CTL_TIME_IO 13757 io->io_hdr.start_time = time_uptime; 13758 getbintime(&io->io_hdr.start_bt); 13759#endif /* CTL_TIME_IO */ 13760 13761 /* Map FE-specific LUN ID into global one. */ 13762 port = ctl_io_port(&io->io_hdr); 13763 io->io_hdr.nexus.targ_mapped_lun = 13764 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13765 13766 switch (io->io_hdr.io_type) { 13767 case CTL_IO_SCSI: 13768 case CTL_IO_TASK: 13769 if (ctl_debug & CTL_DEBUG_CDB) 13770 ctl_io_print(io); 13771 ctl_enqueue_incoming(io); 13772 break; 13773 default: 13774 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13775 return (EINVAL); 13776 } 13777 13778 return (CTL_RETVAL_COMPLETE); 13779} 13780 13781#ifdef CTL_IO_DELAY 13782static void 13783ctl_done_timer_wakeup(void *arg) 13784{ 13785 union ctl_io *io; 13786 13787 io = (union ctl_io *)arg; 13788 ctl_done(io); 13789} 13790#endif /* CTL_IO_DELAY */ 13791 13792void 13793ctl_done(union ctl_io *io) 13794{ 13795 13796 /* 13797 * Enable this to catch duplicate completion issues. 13798 */ 13799#if 0 13800 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13801 printf("%s: type %d msg %d cdb %x iptl: " 13802 "%d:%d:%d:%d tag 0x%04x " 13803 "flag %#x status %x\n", 13804 __func__, 13805 io->io_hdr.io_type, 13806 io->io_hdr.msg_type, 13807 io->scsiio.cdb[0], 13808 io->io_hdr.nexus.initid.id, 13809 io->io_hdr.nexus.targ_port, 13810 io->io_hdr.nexus.targ_target.id, 13811 io->io_hdr.nexus.targ_lun, 13812 (io->io_hdr.io_type == 13813 CTL_IO_TASK) ? 13814 io->taskio.tag_num : 13815 io->scsiio.tag_num, 13816 io->io_hdr.flags, 13817 io->io_hdr.status); 13818 } else 13819 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13820#endif 13821 13822 /* 13823 * This is an internal copy of an I/O, and should not go through 13824 * the normal done processing logic. 13825 */ 13826 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13827 return; 13828 13829 /* 13830 * We need to send a msg to the serializing shelf to finish the IO 13831 * as well. We don't send a finish message to the other shelf if 13832 * this is a task management command. Task management commands 13833 * aren't serialized in the OOA queue, but rather just executed on 13834 * both shelf controllers for commands that originated on that 13835 * controller. 13836 */ 13837 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13838 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13839 union ctl_ha_msg msg_io; 13840 13841 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13842 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13843 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13844 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13845 } 13846 /* continue on to finish IO */ 13847 } 13848#ifdef CTL_IO_DELAY 13849 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13850 struct ctl_lun *lun; 13851 13852 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13853 13854 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13855 } else { 13856 struct ctl_lun *lun; 13857 13858 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13859 13860 if ((lun != NULL) 13861 && (lun->delay_info.done_delay > 0)) { 13862 struct callout *callout; 13863 13864 callout = (struct callout *)&io->io_hdr.timer_bytes; 13865 callout_init(callout, /*mpsafe*/ 1); 13866 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13867 callout_reset(callout, 13868 lun->delay_info.done_delay * hz, 13869 ctl_done_timer_wakeup, io); 13870 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13871 lun->delay_info.done_delay = 0; 13872 return; 13873 } 13874 } 13875#endif /* CTL_IO_DELAY */ 13876 13877 ctl_enqueue_done(io); 13878} 13879 13880int 13881ctl_isc(struct ctl_scsiio *ctsio) 13882{ 13883 struct ctl_lun *lun; 13884 int retval; 13885 13886 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13887 13888 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13889 13890 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13891 13892 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13893 13894 return (retval); 13895} 13896 13897 13898static void 13899ctl_work_thread(void *arg) 13900{ 13901 struct ctl_thread *thr = (struct ctl_thread *)arg; 13902 struct ctl_softc *softc = thr->ctl_softc; 13903 union ctl_io *io; 13904 int retval; 13905 13906 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13907 13908 for (;;) { 13909 retval = 0; 13910 13911 /* 13912 * We handle the queues in this order: 13913 * - ISC 13914 * - done queue (to free up resources, unblock other commands) 13915 * - RtR queue 13916 * - incoming queue 13917 * 13918 * If those queues are empty, we break out of the loop and 13919 * go to sleep. 13920 */ 13921 mtx_lock(&thr->queue_lock); 13922 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13923 if (io != NULL) { 13924 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13925 mtx_unlock(&thr->queue_lock); 13926 ctl_handle_isc(io); 13927 continue; 13928 } 13929 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13930 if (io != NULL) { 13931 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13932 /* clear any blocked commands, call fe_done */ 13933 mtx_unlock(&thr->queue_lock); 13934 retval = ctl_process_done(io); 13935 continue; 13936 } 13937 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13938 if (io != NULL) { 13939 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13940 mtx_unlock(&thr->queue_lock); 13941 if (io->io_hdr.io_type == CTL_IO_TASK) 13942 ctl_run_task(io); 13943 else 13944 ctl_scsiio_precheck(softc, &io->scsiio); 13945 continue; 13946 } 13947 if (!ctl_pause_rtr) { 13948 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13949 if (io != NULL) { 13950 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13951 mtx_unlock(&thr->queue_lock); 13952 retval = ctl_scsiio(&io->scsiio); 13953 if (retval != CTL_RETVAL_COMPLETE) 13954 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13955 continue; 13956 } 13957 } 13958 13959 /* Sleep until we have something to do. */ 13960 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13961 } 13962} 13963 13964static void 13965ctl_lun_thread(void *arg) 13966{ 13967 struct ctl_softc *softc = (struct ctl_softc *)arg; 13968 struct ctl_be_lun *be_lun; 13969 int retval; 13970 13971 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13972 13973 for (;;) { 13974 retval = 0; 13975 mtx_lock(&softc->ctl_lock); 13976 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13977 if (be_lun != NULL) { 13978 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13979 mtx_unlock(&softc->ctl_lock); 13980 ctl_create_lun(be_lun); 13981 continue; 13982 } 13983 13984 /* Sleep until we have something to do. */ 13985 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13986 PDROP | PRIBIO, "-", 0); 13987 } 13988} 13989 13990static void 13991ctl_thresh_thread(void *arg) 13992{ 13993 struct ctl_softc *softc = (struct ctl_softc *)arg; 13994 struct ctl_lun *lun; 13995 struct ctl_be_lun *be_lun; 13996 struct scsi_da_rw_recovery_page *rwpage; 13997 struct ctl_logical_block_provisioning_page *page; 13998 const char *attr; 13999 uint64_t thres, val; 14000 int i, e; 14001 14002 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 14003 14004 for (;;) { 14005 mtx_lock(&softc->ctl_lock); 14006 STAILQ_FOREACH(lun, &softc->lun_list, links) { 14007 be_lun = lun->be_lun; 14008 if ((lun->flags & CTL_LUN_DISABLED) || 14009 (lun->flags & CTL_LUN_OFFLINE) || 14010 lun->backend->lun_attr == NULL) 14011 continue; 14012 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 14013 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 14014 continue; 14015 e = 0; 14016 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 14017 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 14018 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 14019 continue; 14020 thres = scsi_4btoul(page->descr[i].count); 14021 thres <<= CTL_LBP_EXPONENT; 14022 switch (page->descr[i].resource) { 14023 case 0x01: 14024 attr = "blocksavail"; 14025 break; 14026 case 0x02: 14027 attr = "blocksused"; 14028 break; 14029 case 0xf1: 14030 attr = "poolblocksavail"; 14031 break; 14032 case 0xf2: 14033 attr = "poolblocksused"; 14034 break; 14035 default: 14036 continue; 14037 } 14038 mtx_unlock(&softc->ctl_lock); // XXX 14039 val = lun->backend->lun_attr( 14040 lun->be_lun->be_lun, attr); 14041 mtx_lock(&softc->ctl_lock); 14042 if (val == UINT64_MAX) 14043 continue; 14044 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 14045 == SLBPPD_ARMING_INC) 14046 e |= (val >= thres); 14047 else 14048 e |= (val <= thres); 14049 } 14050 mtx_lock(&lun->lun_lock); 14051 if (e) { 14052 if (lun->lasttpt == 0 || 14053 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 14054 lun->lasttpt = time_uptime; 14055 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14056 } 14057 } else { 14058 lun->lasttpt = 0; 14059 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14060 } 14061 mtx_unlock(&lun->lun_lock); 14062 } 14063 mtx_unlock(&softc->ctl_lock); 14064 pause("-", CTL_LBP_PERIOD * hz); 14065 } 14066} 14067 14068static void 14069ctl_enqueue_incoming(union ctl_io *io) 14070{ 14071 struct ctl_softc *softc = control_softc; 14072 struct ctl_thread *thr; 14073 u_int idx; 14074 14075 idx = (io->io_hdr.nexus.targ_port * 127 + 14076 io->io_hdr.nexus.initid.id) % worker_threads; 14077 thr = &softc->threads[idx]; 14078 mtx_lock(&thr->queue_lock); 14079 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14080 mtx_unlock(&thr->queue_lock); 14081 wakeup(thr); 14082} 14083 14084static void 14085ctl_enqueue_rtr(union ctl_io *io) 14086{ 14087 struct ctl_softc *softc = control_softc; 14088 struct ctl_thread *thr; 14089 14090 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14091 mtx_lock(&thr->queue_lock); 14092 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14093 mtx_unlock(&thr->queue_lock); 14094 wakeup(thr); 14095} 14096 14097static void 14098ctl_enqueue_done(union ctl_io *io) 14099{ 14100 struct ctl_softc *softc = control_softc; 14101 struct ctl_thread *thr; 14102 14103 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14104 mtx_lock(&thr->queue_lock); 14105 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14106 mtx_unlock(&thr->queue_lock); 14107 wakeup(thr); 14108} 14109 14110#ifdef notyet 14111static void 14112ctl_enqueue_isc(union ctl_io *io) 14113{ 14114 struct ctl_softc *softc = control_softc; 14115 struct ctl_thread *thr; 14116 14117 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14118 mtx_lock(&thr->queue_lock); 14119 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14120 mtx_unlock(&thr->queue_lock); 14121 wakeup(thr); 14122} 14123 14124/* Initialization and failover */ 14125 14126void 14127ctl_init_isc_msg(void) 14128{ 14129 printf("CTL: Still calling this thing\n"); 14130} 14131 14132/* 14133 * Init component 14134 * Initializes component into configuration defined by bootMode 14135 * (see hasc-sv.c) 14136 * returns hasc_Status: 14137 * OK 14138 * ERROR - fatal error 14139 */ 14140static ctl_ha_comp_status 14141ctl_isc_init(struct ctl_ha_component *c) 14142{ 14143 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14144 14145 c->status = ret; 14146 return ret; 14147} 14148 14149/* Start component 14150 * Starts component in state requested. If component starts successfully, 14151 * it must set its own state to the requestrd state 14152 * When requested state is HASC_STATE_HA, the component may refine it 14153 * by adding _SLAVE or _MASTER flags. 14154 * Currently allowed state transitions are: 14155 * UNKNOWN->HA - initial startup 14156 * UNKNOWN->SINGLE - initial startup when no parter detected 14157 * HA->SINGLE - failover 14158 * returns ctl_ha_comp_status: 14159 * OK - component successfully started in requested state 14160 * FAILED - could not start the requested state, failover may 14161 * be possible 14162 * ERROR - fatal error detected, no future startup possible 14163 */ 14164static ctl_ha_comp_status 14165ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14166{ 14167 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14168 14169 printf("%s: go\n", __func__); 14170 14171 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14172 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14173 control_softc->is_single = 0; 14174 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14175 != CTL_HA_STATUS_SUCCESS) { 14176 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14177 ret = CTL_HA_COMP_STATUS_ERROR; 14178 } 14179 } else if (CTL_HA_STATE_IS_HA(c->state) 14180 && CTL_HA_STATE_IS_SINGLE(state)){ 14181 // HA->SINGLE transition 14182 ctl_failover(); 14183 control_softc->is_single = 1; 14184 } else { 14185 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14186 c->state, state); 14187 ret = CTL_HA_COMP_STATUS_ERROR; 14188 } 14189 if (CTL_HA_STATE_IS_SINGLE(state)) 14190 control_softc->is_single = 1; 14191 14192 c->state = state; 14193 c->status = ret; 14194 return ret; 14195} 14196 14197/* 14198 * Quiesce component 14199 * The component must clear any error conditions (set status to OK) and 14200 * prepare itself to another Start call 14201 * returns ctl_ha_comp_status: 14202 * OK 14203 * ERROR 14204 */ 14205static ctl_ha_comp_status 14206ctl_isc_quiesce(struct ctl_ha_component *c) 14207{ 14208 int ret = CTL_HA_COMP_STATUS_OK; 14209 14210 ctl_pause_rtr = 1; 14211 c->status = ret; 14212 return ret; 14213} 14214 14215struct ctl_ha_component ctl_ha_component_ctlisc = 14216{ 14217 .name = "CTL ISC", 14218 .state = CTL_HA_STATE_UNKNOWN, 14219 .init = ctl_isc_init, 14220 .start = ctl_isc_start, 14221 .quiesce = ctl_isc_quiesce 14222}; 14223#endif 14224 14225/* 14226 * vim: ts=8 14227 */ 14228