ctl.c revision 288727
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: stable/10/sys/cam/ctl/ctl.c 288727 2015-10-05 08:51:20Z 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; 362TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 363SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 364 &worker_threads, 1, "Number of worker threads"); 365static int ctl_debug = CTL_DEBUG_NONE; 366TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug); 367SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 368 &ctl_debug, 0, "Enabled debug flags"); 369 370/* 371 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 372 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 373 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 374 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 375 */ 376#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 377 378#ifdef notyet 379static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 380 int param); 381static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 382#endif 383static int ctl_init(void); 384void ctl_shutdown(void); 385static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 386static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 387static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 388static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 389 struct ctl_ooa *ooa_hdr, 390 struct ctl_ooa_entry *kern_entries); 391static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 392 struct thread *td); 393static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 394 struct ctl_be_lun *be_lun); 395static int ctl_free_lun(struct ctl_lun *lun); 396static void ctl_create_lun(struct ctl_be_lun *be_lun); 397static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 398/** 399static void ctl_failover_change_pages(struct ctl_softc *softc, 400 struct ctl_scsiio *ctsio, int master); 401**/ 402 403static int ctl_do_mode_select(union ctl_io *io); 404static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 405 uint64_t res_key, uint64_t sa_res_key, 406 uint8_t type, uint32_t residx, 407 struct ctl_scsiio *ctsio, 408 struct scsi_per_res_out *cdb, 409 struct scsi_per_res_out_parms* param); 410static void ctl_pro_preempt_other(struct ctl_lun *lun, 411 union ctl_ha_msg *msg); 412static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 413static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 414static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 415static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 416static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 417static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 418static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 419 int alloc_len); 420static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 421 int alloc_len); 422static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 423static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 424static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 425static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 426static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 427static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 428 bool seq); 429static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 430static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 431 union ctl_io *pending_io, union ctl_io *ooa_io); 432static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 433 union ctl_io *starting_io); 434static int ctl_check_blocked(struct ctl_lun *lun); 435static int ctl_scsiio_lun_check(struct ctl_lun *lun, 436 const struct ctl_cmd_entry *entry, 437 struct ctl_scsiio *ctsio); 438//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 439#ifdef notyet 440static void ctl_failover(void); 441#endif 442static void ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 443 ctl_ua_type ua_type); 444static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 445 struct ctl_scsiio *ctsio); 446static int ctl_scsiio(struct ctl_scsiio *ctsio); 447 448static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 449static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 450 ctl_ua_type ua_type); 451static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 452 ctl_ua_type ua_type); 453static int ctl_abort_task(union ctl_io *io); 454static int ctl_abort_task_set(union ctl_io *io); 455static int ctl_i_t_nexus_reset(union ctl_io *io); 456static void ctl_run_task(union ctl_io *io); 457#ifdef CTL_IO_DELAY 458static void ctl_datamove_timer_wakeup(void *arg); 459static void ctl_done_timer_wakeup(void *arg); 460#endif /* CTL_IO_DELAY */ 461 462static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 463static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 464static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 465static void ctl_datamove_remote_write(union ctl_io *io); 466static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 467static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 468static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 469static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 470 ctl_ha_dt_cb callback); 471static void ctl_datamove_remote_read(union ctl_io *io); 472static void ctl_datamove_remote(union ctl_io *io); 473static int ctl_process_done(union ctl_io *io); 474static void ctl_lun_thread(void *arg); 475static void ctl_thresh_thread(void *arg); 476static void ctl_work_thread(void *arg); 477static void ctl_enqueue_incoming(union ctl_io *io); 478static void ctl_enqueue_rtr(union ctl_io *io); 479static void ctl_enqueue_done(union ctl_io *io); 480#ifdef notyet 481static void ctl_enqueue_isc(union ctl_io *io); 482#endif 483static const struct ctl_cmd_entry * 484 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 485static const struct ctl_cmd_entry * 486 ctl_validate_command(struct ctl_scsiio *ctsio); 487static int ctl_cmd_applicable(uint8_t lun_type, 488 const struct ctl_cmd_entry *entry); 489 490/* 491 * Load the serialization table. This isn't very pretty, but is probably 492 * the easiest way to do it. 493 */ 494#include "ctl_ser_table.c" 495 496/* 497 * We only need to define open, close and ioctl routines for this driver. 498 */ 499static struct cdevsw ctl_cdevsw = { 500 .d_version = D_VERSION, 501 .d_flags = 0, 502 .d_open = ctl_open, 503 .d_close = ctl_close, 504 .d_ioctl = ctl_ioctl, 505 .d_name = "ctl", 506}; 507 508 509MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 510 511static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 512 513static moduledata_t ctl_moduledata = { 514 "ctl", 515 ctl_module_event_handler, 516 NULL 517}; 518 519DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 520MODULE_VERSION(ctl, 1); 521 522#ifdef notyet 523static void 524ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 525 union ctl_ha_msg *msg_info) 526{ 527 struct ctl_scsiio *ctsio; 528 529 if (msg_info->hdr.original_sc == NULL) { 530 printf("%s: original_sc == NULL!\n", __func__); 531 /* XXX KDM now what? */ 532 return; 533 } 534 535 ctsio = &msg_info->hdr.original_sc->scsiio; 536 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 537 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 538 ctsio->io_hdr.status = msg_info->hdr.status; 539 ctsio->scsi_status = msg_info->scsi.scsi_status; 540 ctsio->sense_len = msg_info->scsi.sense_len; 541 ctsio->sense_residual = msg_info->scsi.sense_residual; 542 ctsio->residual = msg_info->scsi.residual; 543 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 544 sizeof(ctsio->sense_data)); 545 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 546 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 547 ctl_enqueue_isc((union ctl_io *)ctsio); 548} 549 550static void 551ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 552 union ctl_ha_msg *msg_info) 553{ 554 struct ctl_scsiio *ctsio; 555 556 if (msg_info->hdr.serializing_sc == NULL) { 557 printf("%s: serializing_sc == NULL!\n", __func__); 558 /* XXX KDM now what? */ 559 return; 560 } 561 562 ctsio = &msg_info->hdr.serializing_sc->scsiio; 563#if 0 564 /* 565 * Attempt to catch the situation where an I/O has 566 * been freed, and we're using it again. 567 */ 568 if (ctsio->io_hdr.io_type == 0xff) { 569 union ctl_io *tmp_io; 570 tmp_io = (union ctl_io *)ctsio; 571 printf("%s: %p use after free!\n", __func__, 572 ctsio); 573 printf("%s: type %d msg %d cdb %x iptl: " 574 "%d:%d:%d:%d tag 0x%04x " 575 "flag %#x status %x\n", 576 __func__, 577 tmp_io->io_hdr.io_type, 578 tmp_io->io_hdr.msg_type, 579 tmp_io->scsiio.cdb[0], 580 tmp_io->io_hdr.nexus.initid.id, 581 tmp_io->io_hdr.nexus.targ_port, 582 tmp_io->io_hdr.nexus.targ_target.id, 583 tmp_io->io_hdr.nexus.targ_lun, 584 (tmp_io->io_hdr.io_type == 585 CTL_IO_TASK) ? 586 tmp_io->taskio.tag_num : 587 tmp_io->scsiio.tag_num, 588 tmp_io->io_hdr.flags, 589 tmp_io->io_hdr.status); 590 } 591#endif 592 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 593 ctl_enqueue_isc((union ctl_io *)ctsio); 594} 595 596/* 597 * ISC (Inter Shelf Communication) event handler. Events from the HA 598 * subsystem come in here. 599 */ 600static void 601ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 602{ 603 struct ctl_softc *softc; 604 union ctl_io *io; 605 struct ctl_prio *presio; 606 ctl_ha_status isc_status; 607 608 softc = control_softc; 609 io = NULL; 610 611 612#if 0 613 printf("CTL: Isc Msg event %d\n", event); 614#endif 615 if (event == CTL_HA_EVT_MSG_RECV) { 616 union ctl_ha_msg msg_info; 617 618 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 619 sizeof(msg_info), /*wait*/ 0); 620#if 0 621 printf("CTL: msg_type %d\n", msg_info.msg_type); 622#endif 623 if (isc_status != 0) { 624 printf("Error receiving message, status = %d\n", 625 isc_status); 626 return; 627 } 628 629 switch (msg_info.hdr.msg_type) { 630 case CTL_MSG_SERIALIZE: 631#if 0 632 printf("Serialize\n"); 633#endif 634 io = ctl_alloc_io_nowait(softc->othersc_pool); 635 if (io == NULL) { 636 printf("ctl_isc_event_handler: can't allocate " 637 "ctl_io!\n"); 638 /* Bad Juju */ 639 /* Need to set busy and send msg back */ 640 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 641 msg_info.hdr.status = CTL_SCSI_ERROR; 642 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 643 msg_info.scsi.sense_len = 0; 644 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 645 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 646 } 647 goto bailout; 648 } 649 ctl_zero_io(io); 650 // populate ctsio from msg_info 651 io->io_hdr.io_type = CTL_IO_SCSI; 652 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 653 io->io_hdr.original_sc = msg_info.hdr.original_sc; 654#if 0 655 printf("pOrig %x\n", (int)msg_info.original_sc); 656#endif 657 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 658 CTL_FLAG_IO_ACTIVE; 659 /* 660 * If we're in serialization-only mode, we don't 661 * want to go through full done processing. Thus 662 * the COPY flag. 663 * 664 * XXX KDM add another flag that is more specific. 665 */ 666 if (softc->ha_mode == CTL_HA_MODE_SER_ONLY) 667 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 668 io->io_hdr.nexus = msg_info.hdr.nexus; 669#if 0 670 printf("targ %d, port %d, iid %d, lun %d\n", 671 io->io_hdr.nexus.targ_target.id, 672 io->io_hdr.nexus.targ_port, 673 io->io_hdr.nexus.initid.id, 674 io->io_hdr.nexus.targ_lun); 675#endif 676 io->scsiio.tag_num = msg_info.scsi.tag_num; 677 io->scsiio.tag_type = msg_info.scsi.tag_type; 678 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 679 CTL_MAX_CDBLEN); 680 if (softc->ha_mode == CTL_HA_MODE_XFER) { 681 const struct ctl_cmd_entry *entry; 682 683 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 684 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 685 io->io_hdr.flags |= 686 entry->flags & CTL_FLAG_DATA_MASK; 687 } 688 ctl_enqueue_isc(io); 689 break; 690 691 /* Performed on the Originating SC, XFER mode only */ 692 case CTL_MSG_DATAMOVE: { 693 struct ctl_sg_entry *sgl; 694 int i, j; 695 696 io = msg_info.hdr.original_sc; 697 if (io == NULL) { 698 printf("%s: original_sc == NULL!\n", __func__); 699 /* XXX KDM do something here */ 700 break; 701 } 702 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 703 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 704 /* 705 * Keep track of this, we need to send it back over 706 * when the datamove is complete. 707 */ 708 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 709 710 if (msg_info.dt.sg_sequence == 0) { 711 /* 712 * XXX KDM we use the preallocated S/G list 713 * here, but we'll need to change this to 714 * dynamic allocation if we need larger S/G 715 * lists. 716 */ 717 if (msg_info.dt.kern_sg_entries > 718 sizeof(io->io_hdr.remote_sglist) / 719 sizeof(io->io_hdr.remote_sglist[0])) { 720 printf("%s: number of S/G entries " 721 "needed %u > allocated num %zd\n", 722 __func__, 723 msg_info.dt.kern_sg_entries, 724 sizeof(io->io_hdr.remote_sglist)/ 725 sizeof(io->io_hdr.remote_sglist[0])); 726 727 /* 728 * XXX KDM send a message back to 729 * the other side to shut down the 730 * DMA. The error will come back 731 * through via the normal channel. 732 */ 733 break; 734 } 735 sgl = io->io_hdr.remote_sglist; 736 memset(sgl, 0, 737 sizeof(io->io_hdr.remote_sglist)); 738 739 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 740 741 io->scsiio.kern_sg_entries = 742 msg_info.dt.kern_sg_entries; 743 io->scsiio.rem_sg_entries = 744 msg_info.dt.kern_sg_entries; 745 io->scsiio.kern_data_len = 746 msg_info.dt.kern_data_len; 747 io->scsiio.kern_total_len = 748 msg_info.dt.kern_total_len; 749 io->scsiio.kern_data_resid = 750 msg_info.dt.kern_data_resid; 751 io->scsiio.kern_rel_offset = 752 msg_info.dt.kern_rel_offset; 753 /* 754 * Clear out per-DMA flags. 755 */ 756 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 757 /* 758 * Add per-DMA flags that are set for this 759 * particular DMA request. 760 */ 761 io->io_hdr.flags |= msg_info.dt.flags & 762 CTL_FLAG_RDMA_MASK; 763 } else 764 sgl = (struct ctl_sg_entry *) 765 io->scsiio.kern_data_ptr; 766 767 for (i = msg_info.dt.sent_sg_entries, j = 0; 768 i < (msg_info.dt.sent_sg_entries + 769 msg_info.dt.cur_sg_entries); i++, j++) { 770 sgl[i].addr = msg_info.dt.sg_list[j].addr; 771 sgl[i].len = msg_info.dt.sg_list[j].len; 772 773#if 0 774 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 775 __func__, 776 msg_info.dt.sg_list[j].addr, 777 msg_info.dt.sg_list[j].len, 778 sgl[i].addr, sgl[i].len, j, i); 779#endif 780 } 781#if 0 782 memcpy(&sgl[msg_info.dt.sent_sg_entries], 783 msg_info.dt.sg_list, 784 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 785#endif 786 787 /* 788 * If this is the last piece of the I/O, we've got 789 * the full S/G list. Queue processing in the thread. 790 * Otherwise wait for the next piece. 791 */ 792 if (msg_info.dt.sg_last != 0) 793 ctl_enqueue_isc(io); 794 break; 795 } 796 /* Performed on the Serializing (primary) SC, XFER mode only */ 797 case CTL_MSG_DATAMOVE_DONE: { 798 if (msg_info.hdr.serializing_sc == NULL) { 799 printf("%s: serializing_sc == NULL!\n", 800 __func__); 801 /* XXX KDM now what? */ 802 break; 803 } 804 /* 805 * We grab the sense information here in case 806 * there was a failure, so we can return status 807 * back to the initiator. 808 */ 809 io = msg_info.hdr.serializing_sc; 810 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 811 io->io_hdr.status = msg_info.hdr.status; 812 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 813 io->scsiio.sense_len = msg_info.scsi.sense_len; 814 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 815 io->io_hdr.port_status = msg_info.scsi.fetd_status; 816 io->scsiio.residual = msg_info.scsi.residual; 817 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 818 sizeof(io->scsiio.sense_data)); 819 ctl_enqueue_isc(io); 820 break; 821 } 822 823 /* Preformed on Originating SC, SER_ONLY mode */ 824 case CTL_MSG_R2R: 825 io = msg_info.hdr.original_sc; 826 if (io == NULL) { 827 printf("%s: Major Bummer\n", __func__); 828 return; 829 } else { 830#if 0 831 printf("pOrig %x\n",(int) ctsio); 832#endif 833 } 834 io->io_hdr.msg_type = CTL_MSG_R2R; 835 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 836 ctl_enqueue_isc(io); 837 break; 838 839 /* 840 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 841 * mode. 842 * Performed on the Originating (i.e. secondary) SC in XFER 843 * mode 844 */ 845 case CTL_MSG_FINISH_IO: 846 if (softc->ha_mode == CTL_HA_MODE_XFER) 847 ctl_isc_handler_finish_xfer(softc, 848 &msg_info); 849 else 850 ctl_isc_handler_finish_ser_only(softc, 851 &msg_info); 852 break; 853 854 /* Preformed on Originating SC */ 855 case CTL_MSG_BAD_JUJU: 856 io = msg_info.hdr.original_sc; 857 if (io == NULL) { 858 printf("%s: Bad JUJU!, original_sc is NULL!\n", 859 __func__); 860 break; 861 } 862 ctl_copy_sense_data(&msg_info, io); 863 /* 864 * IO should have already been cleaned up on other 865 * SC so clear this flag so we won't send a message 866 * back to finish the IO there. 867 */ 868 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 869 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 870 871 /* io = msg_info.hdr.serializing_sc; */ 872 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 873 ctl_enqueue_isc(io); 874 break; 875 876 /* Handle resets sent from the other side */ 877 case CTL_MSG_MANAGE_TASKS: { 878 struct ctl_taskio *taskio; 879 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait( 880 softc->othersc_pool); 881 if (taskio == NULL) { 882 printf("ctl_isc_event_handler: can't allocate " 883 "ctl_io!\n"); 884 /* Bad Juju */ 885 /* should I just call the proper reset func 886 here??? */ 887 goto bailout; 888 } 889 ctl_zero_io((union ctl_io *)taskio); 890 taskio->io_hdr.io_type = CTL_IO_TASK; 891 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 892 taskio->io_hdr.nexus = msg_info.hdr.nexus; 893 taskio->task_action = msg_info.task.task_action; 894 taskio->tag_num = msg_info.task.tag_num; 895 taskio->tag_type = msg_info.task.tag_type; 896#ifdef CTL_TIME_IO 897 taskio->io_hdr.start_time = time_uptime; 898 getbintime(&taskio->io_hdr.start_bt); 899#if 0 900 cs_prof_gettime(&taskio->io_hdr.start_ticks); 901#endif 902#endif /* CTL_TIME_IO */ 903 ctl_run_task((union ctl_io *)taskio); 904 break; 905 } 906 /* Persistent Reserve action which needs attention */ 907 case CTL_MSG_PERS_ACTION: 908 presio = (struct ctl_prio *)ctl_alloc_io_nowait( 909 softc->othersc_pool); 910 if (presio == NULL) { 911 printf("ctl_isc_event_handler: can't allocate " 912 "ctl_io!\n"); 913 /* Bad Juju */ 914 /* Need to set busy and send msg back */ 915 goto bailout; 916 } 917 ctl_zero_io((union ctl_io *)presio); 918 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 919 presio->pr_msg = msg_info.pr; 920 ctl_enqueue_isc((union ctl_io *)presio); 921 break; 922 case CTL_MSG_SYNC_FE: 923 rcv_sync_msg = 1; 924 break; 925 default: 926 printf("How did I get here?\n"); 927 } 928 } else if (event == CTL_HA_EVT_MSG_SENT) { 929 if (param != CTL_HA_STATUS_SUCCESS) { 930 printf("Bad status from ctl_ha_msg_send status %d\n", 931 param); 932 } 933 return; 934 } else if (event == CTL_HA_EVT_DISCONNECT) { 935 printf("CTL: Got a disconnect from Isc\n"); 936 return; 937 } else { 938 printf("ctl_isc_event_handler: Unknown event %d\n", event); 939 return; 940 } 941 942bailout: 943 return; 944} 945 946static void 947ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 948{ 949 struct scsi_sense_data *sense; 950 951 sense = &dest->scsiio.sense_data; 952 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 953 dest->scsiio.scsi_status = src->scsi.scsi_status; 954 dest->scsiio.sense_len = src->scsi.sense_len; 955 dest->io_hdr.status = src->hdr.status; 956} 957#endif 958 959static void 960ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 961{ 962 ctl_ua_type *pu; 963 964 mtx_assert(&lun->lun_lock, MA_OWNED); 965 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 966 if (pu == NULL) 967 return; 968 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 969} 970 971static void 972ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 973{ 974 int i, j; 975 976 mtx_assert(&lun->lun_lock, MA_OWNED); 977 for (i = 0; i < CTL_MAX_PORTS; i++) { 978 if (lun->pending_ua[i] == NULL) 979 continue; 980 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 981 if (i * CTL_MAX_INIT_PER_PORT + j == except) 982 continue; 983 lun->pending_ua[i][j] |= ua; 984 } 985 } 986} 987 988static void 989ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 990{ 991 ctl_ua_type *pu; 992 993 mtx_assert(&lun->lun_lock, MA_OWNED); 994 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 995 if (pu == NULL) 996 return; 997 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 998} 999 1000static void 1001ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1002{ 1003 int i, j; 1004 1005 mtx_assert(&lun->lun_lock, MA_OWNED); 1006 for (i = 0; i < CTL_MAX_PORTS; i++) { 1007 if (lun->pending_ua[i] == NULL) 1008 continue; 1009 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1010 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1011 continue; 1012 lun->pending_ua[i][j] &= ~ua; 1013 } 1014 } 1015} 1016 1017static void 1018ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1019 ctl_ua_type ua_type) 1020{ 1021 struct ctl_lun *lun; 1022 1023 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1024 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1025 mtx_lock(&lun->lun_lock); 1026 ctl_clr_ua(lun, initidx, ua_type); 1027 mtx_unlock(&lun->lun_lock); 1028 } 1029} 1030 1031static int 1032ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS) 1033{ 1034 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1035 struct ctl_lun *lun; 1036 int error, value; 1037 1038 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) 1039 value = 0; 1040 else 1041 value = 1; 1042 1043 error = sysctl_handle_int(oidp, &value, 0, req); 1044 if ((error != 0) || (req->newptr == NULL)) 1045 return (error); 1046 1047 mtx_lock(&softc->ctl_lock); 1048 if (value == 0) 1049 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1050 else 1051 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1052 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1053 mtx_lock(&lun->lun_lock); 1054 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1055 mtx_unlock(&lun->lun_lock); 1056 } 1057 mtx_unlock(&softc->ctl_lock); 1058 return (0); 1059} 1060 1061static int 1062ctl_init(void) 1063{ 1064 struct ctl_softc *softc; 1065 void *other_pool; 1066 int i, error, retval; 1067 //int isc_retval; 1068 1069 retval = 0; 1070 ctl_pause_rtr = 0; 1071 rcv_sync_msg = 0; 1072 1073 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1074 M_WAITOK | M_ZERO); 1075 softc = control_softc; 1076 1077 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1078 "cam/ctl"); 1079 1080 softc->dev->si_drv1 = softc; 1081 1082 /* 1083 * By default, return a "bad LUN" peripheral qualifier for unknown 1084 * LUNs. The user can override this default using the tunable or 1085 * sysctl. See the comment in ctl_inquiry_std() for more details. 1086 */ 1087 softc->inquiry_pq_no_lun = 1; 1088 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 1089 &softc->inquiry_pq_no_lun); 1090 sysctl_ctx_init(&softc->sysctl_ctx); 1091 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1092 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1093 CTLFLAG_RD, 0, "CAM Target Layer"); 1094 1095 if (softc->sysctl_tree == NULL) { 1096 printf("%s: unable to allocate sysctl tree\n", __func__); 1097 destroy_dev(softc->dev); 1098 free(control_softc, M_DEVBUF); 1099 control_softc = NULL; 1100 return (ENOMEM); 1101 } 1102 1103 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1104 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1105 "inquiry_pq_no_lun", CTLFLAG_RW, 1106 &softc->inquiry_pq_no_lun, 0, 1107 "Report no lun possible for invalid LUNs"); 1108 1109 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1110 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1111 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1112 softc->open_count = 0; 1113 1114 /* 1115 * Default to actually sending a SYNCHRONIZE CACHE command down to 1116 * the drive. 1117 */ 1118 softc->flags = CTL_FLAG_REAL_SYNC; 1119 1120 /* 1121 * In Copan's HA scheme, the "master" and "slave" roles are 1122 * figured out through the slot the controller is in. Although it 1123 * is an active/active system, someone has to be in charge. 1124 */ 1125 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1126 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1127 "HA head ID (0 - no HA)"); 1128 if (softc->ha_id == 0) { 1129 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1130 softc->is_single = 1; 1131 softc->port_offset = 0; 1132 } else 1133 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS; 1134 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT; 1135 1136 STAILQ_INIT(&softc->lun_list); 1137 STAILQ_INIT(&softc->pending_lun_queue); 1138 STAILQ_INIT(&softc->fe_list); 1139 STAILQ_INIT(&softc->port_list); 1140 STAILQ_INIT(&softc->be_list); 1141 ctl_tpc_init(softc); 1142 1143 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1144 &other_pool) != 0) 1145 { 1146 printf("ctl: can't allocate %d entry other SC pool, " 1147 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1148 return (ENOMEM); 1149 } 1150 softc->othersc_pool = other_pool; 1151 1152 if (worker_threads <= 0) 1153 worker_threads = max(1, mp_ncpus / 4); 1154 if (worker_threads > CTL_MAX_THREADS) 1155 worker_threads = CTL_MAX_THREADS; 1156 1157 for (i = 0; i < worker_threads; i++) { 1158 struct ctl_thread *thr = &softc->threads[i]; 1159 1160 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1161 thr->ctl_softc = softc; 1162 STAILQ_INIT(&thr->incoming_queue); 1163 STAILQ_INIT(&thr->rtr_queue); 1164 STAILQ_INIT(&thr->done_queue); 1165 STAILQ_INIT(&thr->isc_queue); 1166 1167 error = kproc_kthread_add(ctl_work_thread, thr, 1168 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1169 if (error != 0) { 1170 printf("error creating CTL work thread!\n"); 1171 ctl_pool_free(other_pool); 1172 return (error); 1173 } 1174 } 1175 error = kproc_kthread_add(ctl_lun_thread, softc, 1176 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1177 if (error != 0) { 1178 printf("error creating CTL lun thread!\n"); 1179 ctl_pool_free(other_pool); 1180 return (error); 1181 } 1182 error = kproc_kthread_add(ctl_thresh_thread, softc, 1183 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1184 if (error != 0) { 1185 printf("error creating CTL threshold thread!\n"); 1186 ctl_pool_free(other_pool); 1187 return (error); 1188 } 1189 1190 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1191 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN, 1192 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head"); 1193 1194#ifdef CTL_IO_DELAY 1195 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1196 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1197 sizeof(struct callout), CTL_TIMER_BYTES); 1198 return (EINVAL); 1199 } 1200#endif /* CTL_IO_DELAY */ 1201 1202 return (0); 1203} 1204 1205void 1206ctl_shutdown(void) 1207{ 1208 struct ctl_softc *softc; 1209 struct ctl_lun *lun, *next_lun; 1210 1211 softc = (struct ctl_softc *)control_softc; 1212 1213 mtx_lock(&softc->ctl_lock); 1214 1215 /* 1216 * Free up each LUN. 1217 */ 1218 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1219 next_lun = STAILQ_NEXT(lun, links); 1220 ctl_free_lun(lun); 1221 } 1222 1223 mtx_unlock(&softc->ctl_lock); 1224 1225#if 0 1226 ctl_shutdown_thread(softc->work_thread); 1227 mtx_destroy(&softc->queue_lock); 1228#endif 1229 1230 ctl_tpc_shutdown(softc); 1231 uma_zdestroy(softc->io_zone); 1232 mtx_destroy(&softc->ctl_lock); 1233 1234 destroy_dev(softc->dev); 1235 1236 sysctl_ctx_free(&softc->sysctl_ctx); 1237 1238 free(control_softc, M_DEVBUF); 1239 control_softc = NULL; 1240} 1241 1242static int 1243ctl_module_event_handler(module_t mod, int what, void *arg) 1244{ 1245 1246 switch (what) { 1247 case MOD_LOAD: 1248 return (ctl_init()); 1249 case MOD_UNLOAD: 1250 return (EBUSY); 1251 default: 1252 return (EOPNOTSUPP); 1253 } 1254} 1255 1256/* 1257 * XXX KDM should we do some access checks here? Bump a reference count to 1258 * prevent a CTL module from being unloaded while someone has it open? 1259 */ 1260static int 1261ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1262{ 1263 return (0); 1264} 1265 1266static int 1267ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1268{ 1269 return (0); 1270} 1271 1272int 1273ctl_port_enable(ctl_port_type port_type) 1274{ 1275 struct ctl_softc *softc = control_softc; 1276 struct ctl_port *port; 1277 1278 if (softc->is_single == 0) { 1279 union ctl_ha_msg msg_info; 1280 int isc_retval; 1281 1282#if 0 1283 printf("%s: HA mode, synchronizing frontend enable\n", 1284 __func__); 1285#endif 1286 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1287 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1288 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1289 printf("Sync msg send error retval %d\n", isc_retval); 1290 } 1291 if (!rcv_sync_msg) { 1292 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1293 sizeof(msg_info), 1); 1294 } 1295#if 0 1296 printf("CTL:Frontend Enable\n"); 1297 } else { 1298 printf("%s: single mode, skipping frontend synchronization\n", 1299 __func__); 1300#endif 1301 } 1302 1303 STAILQ_FOREACH(port, &softc->port_list, links) { 1304 if (port_type & port->port_type) 1305 { 1306#if 0 1307 printf("port %d\n", port->targ_port); 1308#endif 1309 ctl_port_online(port); 1310 } 1311 } 1312 1313 return (0); 1314} 1315 1316int 1317ctl_port_disable(ctl_port_type port_type) 1318{ 1319 struct ctl_softc *softc; 1320 struct ctl_port *port; 1321 1322 softc = control_softc; 1323 1324 STAILQ_FOREACH(port, &softc->port_list, links) { 1325 if (port_type & port->port_type) 1326 ctl_port_offline(port); 1327 } 1328 1329 return (0); 1330} 1331 1332/* 1333 * Returns 0 for success, 1 for failure. 1334 * Currently the only failure mode is if there aren't enough entries 1335 * allocated. So, in case of a failure, look at num_entries_dropped, 1336 * reallocate and try again. 1337 */ 1338int 1339ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1340 int *num_entries_filled, int *num_entries_dropped, 1341 ctl_port_type port_type, int no_virtual) 1342{ 1343 struct ctl_softc *softc; 1344 struct ctl_port *port; 1345 int entries_dropped, entries_filled; 1346 int retval; 1347 int i; 1348 1349 softc = control_softc; 1350 1351 retval = 0; 1352 entries_filled = 0; 1353 entries_dropped = 0; 1354 1355 i = 0; 1356 mtx_lock(&softc->ctl_lock); 1357 STAILQ_FOREACH(port, &softc->port_list, links) { 1358 struct ctl_port_entry *entry; 1359 1360 if ((port->port_type & port_type) == 0) 1361 continue; 1362 1363 if ((no_virtual != 0) 1364 && (port->virtual_port != 0)) 1365 continue; 1366 1367 if (entries_filled >= num_entries_alloced) { 1368 entries_dropped++; 1369 continue; 1370 } 1371 entry = &entries[i]; 1372 1373 entry->port_type = port->port_type; 1374 strlcpy(entry->port_name, port->port_name, 1375 sizeof(entry->port_name)); 1376 entry->physical_port = port->physical_port; 1377 entry->virtual_port = port->virtual_port; 1378 entry->wwnn = port->wwnn; 1379 entry->wwpn = port->wwpn; 1380 1381 i++; 1382 entries_filled++; 1383 } 1384 1385 mtx_unlock(&softc->ctl_lock); 1386 1387 if (entries_dropped > 0) 1388 retval = 1; 1389 1390 *num_entries_dropped = entries_dropped; 1391 *num_entries_filled = entries_filled; 1392 1393 return (retval); 1394} 1395 1396/* 1397 * Remove an initiator by port number and initiator ID. 1398 * Returns 0 for success, -1 for failure. 1399 */ 1400int 1401ctl_remove_initiator(struct ctl_port *port, int iid) 1402{ 1403 struct ctl_softc *softc = control_softc; 1404 1405 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1406 1407 if (iid > CTL_MAX_INIT_PER_PORT) { 1408 printf("%s: initiator ID %u > maximun %u!\n", 1409 __func__, iid, CTL_MAX_INIT_PER_PORT); 1410 return (-1); 1411 } 1412 1413 mtx_lock(&softc->ctl_lock); 1414 port->wwpn_iid[iid].in_use--; 1415 port->wwpn_iid[iid].last_use = time_uptime; 1416 mtx_unlock(&softc->ctl_lock); 1417 1418 return (0); 1419} 1420 1421/* 1422 * Add an initiator to the initiator map. 1423 * Returns iid for success, < 0 for failure. 1424 */ 1425int 1426ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1427{ 1428 struct ctl_softc *softc = control_softc; 1429 time_t best_time; 1430 int i, best; 1431 1432 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1433 1434 if (iid >= CTL_MAX_INIT_PER_PORT) { 1435 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1436 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1437 free(name, M_CTL); 1438 return (-1); 1439 } 1440 1441 mtx_lock(&softc->ctl_lock); 1442 1443 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1444 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1445 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1446 iid = i; 1447 break; 1448 } 1449 if (name != NULL && port->wwpn_iid[i].name != NULL && 1450 strcmp(name, port->wwpn_iid[i].name) == 0) { 1451 iid = i; 1452 break; 1453 } 1454 } 1455 } 1456 1457 if (iid < 0) { 1458 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1459 if (port->wwpn_iid[i].in_use == 0 && 1460 port->wwpn_iid[i].wwpn == 0 && 1461 port->wwpn_iid[i].name == NULL) { 1462 iid = i; 1463 break; 1464 } 1465 } 1466 } 1467 1468 if (iid < 0) { 1469 best = -1; 1470 best_time = INT32_MAX; 1471 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1472 if (port->wwpn_iid[i].in_use == 0) { 1473 if (port->wwpn_iid[i].last_use < best_time) { 1474 best = i; 1475 best_time = port->wwpn_iid[i].last_use; 1476 } 1477 } 1478 } 1479 iid = best; 1480 } 1481 1482 if (iid < 0) { 1483 mtx_unlock(&softc->ctl_lock); 1484 free(name, M_CTL); 1485 return (-2); 1486 } 1487 1488 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1489 /* 1490 * This is not an error yet. 1491 */ 1492 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1493#if 0 1494 printf("%s: port %d iid %u WWPN %#jx arrived" 1495 " again\n", __func__, port->targ_port, 1496 iid, (uintmax_t)wwpn); 1497#endif 1498 goto take; 1499 } 1500 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1501 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1502#if 0 1503 printf("%s: port %d iid %u name '%s' arrived" 1504 " again\n", __func__, port->targ_port, 1505 iid, name); 1506#endif 1507 goto take; 1508 } 1509 1510 /* 1511 * This is an error, but what do we do about it? The 1512 * driver is telling us we have a new WWPN for this 1513 * initiator ID, so we pretty much need to use it. 1514 */ 1515 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1516 " but WWPN %#jx '%s' is still at that address\n", 1517 __func__, port->targ_port, iid, wwpn, name, 1518 (uintmax_t)port->wwpn_iid[iid].wwpn, 1519 port->wwpn_iid[iid].name); 1520 1521 /* 1522 * XXX KDM clear have_ca and ua_pending on each LUN for 1523 * this initiator. 1524 */ 1525 } 1526take: 1527 free(port->wwpn_iid[iid].name, M_CTL); 1528 port->wwpn_iid[iid].name = name; 1529 port->wwpn_iid[iid].wwpn = wwpn; 1530 port->wwpn_iid[iid].in_use++; 1531 mtx_unlock(&softc->ctl_lock); 1532 1533 return (iid); 1534} 1535 1536static int 1537ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1538{ 1539 int len; 1540 1541 switch (port->port_type) { 1542 case CTL_PORT_FC: 1543 { 1544 struct scsi_transportid_fcp *id = 1545 (struct scsi_transportid_fcp *)buf; 1546 if (port->wwpn_iid[iid].wwpn == 0) 1547 return (0); 1548 memset(id, 0, sizeof(*id)); 1549 id->format_protocol = SCSI_PROTO_FC; 1550 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1551 return (sizeof(*id)); 1552 } 1553 case CTL_PORT_ISCSI: 1554 { 1555 struct scsi_transportid_iscsi_port *id = 1556 (struct scsi_transportid_iscsi_port *)buf; 1557 if (port->wwpn_iid[iid].name == NULL) 1558 return (0); 1559 memset(id, 0, 256); 1560 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1561 SCSI_PROTO_ISCSI; 1562 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1563 len = roundup2(min(len, 252), 4); 1564 scsi_ulto2b(len, id->additional_length); 1565 return (sizeof(*id) + len); 1566 } 1567 case CTL_PORT_SAS: 1568 { 1569 struct scsi_transportid_sas *id = 1570 (struct scsi_transportid_sas *)buf; 1571 if (port->wwpn_iid[iid].wwpn == 0) 1572 return (0); 1573 memset(id, 0, sizeof(*id)); 1574 id->format_protocol = SCSI_PROTO_SAS; 1575 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1576 return (sizeof(*id)); 1577 } 1578 default: 1579 { 1580 struct scsi_transportid_spi *id = 1581 (struct scsi_transportid_spi *)buf; 1582 memset(id, 0, sizeof(*id)); 1583 id->format_protocol = SCSI_PROTO_SPI; 1584 scsi_ulto2b(iid, id->scsi_addr); 1585 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1586 return (sizeof(*id)); 1587 } 1588 } 1589} 1590 1591/* 1592 * Serialize a command that went down the "wrong" side, and so was sent to 1593 * this controller for execution. The logic is a little different than the 1594 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1595 * sent back to the other side, but in the success case, we execute the 1596 * command on this side (XFER mode) or tell the other side to execute it 1597 * (SER_ONLY mode). 1598 */ 1599static int 1600ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1601{ 1602 struct ctl_softc *softc; 1603 union ctl_ha_msg msg_info; 1604 struct ctl_lun *lun; 1605 int retval = 0; 1606 uint32_t targ_lun; 1607 1608 softc = control_softc; 1609 1610 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1611 lun = softc->ctl_luns[targ_lun]; 1612 if (lun==NULL) 1613 { 1614 /* 1615 * Why isn't LUN defined? The other side wouldn't 1616 * send a cmd if the LUN is undefined. 1617 */ 1618 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1619 1620 /* "Logical unit not supported" */ 1621 ctl_set_sense_data(&msg_info.scsi.sense_data, 1622 lun, 1623 /*sense_format*/SSD_TYPE_NONE, 1624 /*current_error*/ 1, 1625 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1626 /*asc*/ 0x25, 1627 /*ascq*/ 0x00, 1628 SSD_ELEM_NONE); 1629 1630 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1631 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1632 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1633 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1634 msg_info.hdr.serializing_sc = NULL; 1635 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1636 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1637 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1638 } 1639 return(1); 1640 1641 } 1642 1643 mtx_lock(&lun->lun_lock); 1644 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1645 1646 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1647 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1648 ooa_links))) { 1649 case CTL_ACTION_BLOCK: 1650 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1651 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1652 blocked_links); 1653 break; 1654 case CTL_ACTION_PASS: 1655 case CTL_ACTION_SKIP: 1656 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1657 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1658 ctl_enqueue_rtr((union ctl_io *)ctsio); 1659 } else { 1660 1661 /* send msg back to other side */ 1662 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1663 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1664 msg_info.hdr.msg_type = CTL_MSG_R2R; 1665#if 0 1666 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1667#endif 1668 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1669 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1670 } 1671 } 1672 break; 1673 case CTL_ACTION_OVERLAP: 1674 /* OVERLAPPED COMMANDS ATTEMPTED */ 1675 ctl_set_sense_data(&msg_info.scsi.sense_data, 1676 lun, 1677 /*sense_format*/SSD_TYPE_NONE, 1678 /*current_error*/ 1, 1679 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1680 /*asc*/ 0x4E, 1681 /*ascq*/ 0x00, 1682 SSD_ELEM_NONE); 1683 1684 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1685 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1686 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1687 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1688 msg_info.hdr.serializing_sc = NULL; 1689 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1690#if 0 1691 printf("BAD JUJU:Major Bummer Overlap\n"); 1692#endif 1693 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1694 retval = 1; 1695 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1696 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1697 } 1698 break; 1699 case CTL_ACTION_OVERLAP_TAG: 1700 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1701 ctl_set_sense_data(&msg_info.scsi.sense_data, 1702 lun, 1703 /*sense_format*/SSD_TYPE_NONE, 1704 /*current_error*/ 1, 1705 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1706 /*asc*/ 0x4D, 1707 /*ascq*/ ctsio->tag_num & 0xff, 1708 SSD_ELEM_NONE); 1709 1710 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1711 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1712 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1713 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1714 msg_info.hdr.serializing_sc = NULL; 1715 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1716#if 0 1717 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1718#endif 1719 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1720 retval = 1; 1721 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1722 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1723 } 1724 break; 1725 case CTL_ACTION_ERROR: 1726 default: 1727 /* "Internal target failure" */ 1728 ctl_set_sense_data(&msg_info.scsi.sense_data, 1729 lun, 1730 /*sense_format*/SSD_TYPE_NONE, 1731 /*current_error*/ 1, 1732 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1733 /*asc*/ 0x44, 1734 /*ascq*/ 0x00, 1735 SSD_ELEM_NONE); 1736 1737 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1738 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1739 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1740 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1741 msg_info.hdr.serializing_sc = NULL; 1742 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1743#if 0 1744 printf("BAD JUJU:Major Bummer HW Error\n"); 1745#endif 1746 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1747 retval = 1; 1748 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1749 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1750 } 1751 break; 1752 } 1753 mtx_unlock(&lun->lun_lock); 1754 return (retval); 1755} 1756 1757/* 1758 * Returns 0 for success, errno for failure. 1759 */ 1760static int 1761ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 1762 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 1763{ 1764 union ctl_io *io; 1765 int retval; 1766 1767 retval = 0; 1768 1769 mtx_lock(&lun->lun_lock); 1770 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 1771 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 1772 ooa_links)) { 1773 struct ctl_ooa_entry *entry; 1774 1775 /* 1776 * If we've got more than we can fit, just count the 1777 * remaining entries. 1778 */ 1779 if (*cur_fill_num >= ooa_hdr->alloc_num) 1780 continue; 1781 1782 entry = &kern_entries[*cur_fill_num]; 1783 1784 entry->tag_num = io->scsiio.tag_num; 1785 entry->lun_num = lun->lun; 1786#ifdef CTL_TIME_IO 1787 entry->start_bt = io->io_hdr.start_bt; 1788#endif 1789 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 1790 entry->cdb_len = io->scsiio.cdb_len; 1791 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 1792 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 1793 1794 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 1795 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 1796 1797 if (io->io_hdr.flags & CTL_FLAG_ABORT) 1798 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 1799 1800 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 1801 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 1802 1803 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 1804 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 1805 } 1806 mtx_unlock(&lun->lun_lock); 1807 1808 return (retval); 1809} 1810 1811static void * 1812ctl_copyin_alloc(void *user_addr, int len, char *error_str, 1813 size_t error_str_len) 1814{ 1815 void *kptr; 1816 1817 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 1818 1819 if (copyin(user_addr, kptr, len) != 0) { 1820 snprintf(error_str, error_str_len, "Error copying %d bytes " 1821 "from user address %p to kernel address %p", len, 1822 user_addr, kptr); 1823 free(kptr, M_CTL); 1824 return (NULL); 1825 } 1826 1827 return (kptr); 1828} 1829 1830static void 1831ctl_free_args(int num_args, struct ctl_be_arg *args) 1832{ 1833 int i; 1834 1835 if (args == NULL) 1836 return; 1837 1838 for (i = 0; i < num_args; i++) { 1839 free(args[i].kname, M_CTL); 1840 free(args[i].kvalue, M_CTL); 1841 } 1842 1843 free(args, M_CTL); 1844} 1845 1846static struct ctl_be_arg * 1847ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 1848 char *error_str, size_t error_str_len) 1849{ 1850 struct ctl_be_arg *args; 1851 int i; 1852 1853 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 1854 error_str, error_str_len); 1855 1856 if (args == NULL) 1857 goto bailout; 1858 1859 for (i = 0; i < num_args; i++) { 1860 args[i].kname = NULL; 1861 args[i].kvalue = NULL; 1862 } 1863 1864 for (i = 0; i < num_args; i++) { 1865 uint8_t *tmpptr; 1866 1867 args[i].kname = ctl_copyin_alloc(args[i].name, 1868 args[i].namelen, error_str, error_str_len); 1869 if (args[i].kname == NULL) 1870 goto bailout; 1871 1872 if (args[i].kname[args[i].namelen - 1] != '\0') { 1873 snprintf(error_str, error_str_len, "Argument %d " 1874 "name is not NUL-terminated", i); 1875 goto bailout; 1876 } 1877 1878 if (args[i].flags & CTL_BEARG_RD) { 1879 tmpptr = ctl_copyin_alloc(args[i].value, 1880 args[i].vallen, error_str, error_str_len); 1881 if (tmpptr == NULL) 1882 goto bailout; 1883 if ((args[i].flags & CTL_BEARG_ASCII) 1884 && (tmpptr[args[i].vallen - 1] != '\0')) { 1885 snprintf(error_str, error_str_len, "Argument " 1886 "%d value is not NUL-terminated", i); 1887 goto bailout; 1888 } 1889 args[i].kvalue = tmpptr; 1890 } else { 1891 args[i].kvalue = malloc(args[i].vallen, 1892 M_CTL, M_WAITOK | M_ZERO); 1893 } 1894 } 1895 1896 return (args); 1897bailout: 1898 1899 ctl_free_args(num_args, args); 1900 1901 return (NULL); 1902} 1903 1904static void 1905ctl_copyout_args(int num_args, struct ctl_be_arg *args) 1906{ 1907 int i; 1908 1909 for (i = 0; i < num_args; i++) { 1910 if (args[i].flags & CTL_BEARG_WR) 1911 copyout(args[i].kvalue, args[i].value, args[i].vallen); 1912 } 1913} 1914 1915/* 1916 * Escape characters that are illegal or not recommended in XML. 1917 */ 1918int 1919ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 1920{ 1921 char *end = str + size; 1922 int retval; 1923 1924 retval = 0; 1925 1926 for (; *str && str < end; str++) { 1927 switch (*str) { 1928 case '&': 1929 retval = sbuf_printf(sb, "&"); 1930 break; 1931 case '>': 1932 retval = sbuf_printf(sb, ">"); 1933 break; 1934 case '<': 1935 retval = sbuf_printf(sb, "<"); 1936 break; 1937 default: 1938 retval = sbuf_putc(sb, *str); 1939 break; 1940 } 1941 1942 if (retval != 0) 1943 break; 1944 1945 } 1946 1947 return (retval); 1948} 1949 1950static void 1951ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 1952{ 1953 struct scsi_vpd_id_descriptor *desc; 1954 int i; 1955 1956 if (id == NULL || id->len < 4) 1957 return; 1958 desc = (struct scsi_vpd_id_descriptor *)id->data; 1959 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 1960 case SVPD_ID_TYPE_T10: 1961 sbuf_printf(sb, "t10."); 1962 break; 1963 case SVPD_ID_TYPE_EUI64: 1964 sbuf_printf(sb, "eui."); 1965 break; 1966 case SVPD_ID_TYPE_NAA: 1967 sbuf_printf(sb, "naa."); 1968 break; 1969 case SVPD_ID_TYPE_SCSI_NAME: 1970 break; 1971 } 1972 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 1973 case SVPD_ID_CODESET_BINARY: 1974 for (i = 0; i < desc->length; i++) 1975 sbuf_printf(sb, "%02x", desc->identifier[i]); 1976 break; 1977 case SVPD_ID_CODESET_ASCII: 1978 sbuf_printf(sb, "%.*s", (int)desc->length, 1979 (char *)desc->identifier); 1980 break; 1981 case SVPD_ID_CODESET_UTF8: 1982 sbuf_printf(sb, "%s", (char *)desc->identifier); 1983 break; 1984 } 1985} 1986 1987static int 1988ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 1989 struct thread *td) 1990{ 1991 struct ctl_softc *softc; 1992 int retval; 1993 1994 softc = control_softc; 1995 1996 retval = 0; 1997 1998 switch (cmd) { 1999 case CTL_IO: 2000 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2001 break; 2002 case CTL_ENABLE_PORT: 2003 case CTL_DISABLE_PORT: 2004 case CTL_SET_PORT_WWNS: { 2005 struct ctl_port *port; 2006 struct ctl_port_entry *entry; 2007 2008 entry = (struct ctl_port_entry *)addr; 2009 2010 mtx_lock(&softc->ctl_lock); 2011 STAILQ_FOREACH(port, &softc->port_list, links) { 2012 int action, done; 2013 2014 action = 0; 2015 done = 0; 2016 2017 if ((entry->port_type == CTL_PORT_NONE) 2018 && (entry->targ_port == port->targ_port)) { 2019 /* 2020 * If the user only wants to enable or 2021 * disable or set WWNs on a specific port, 2022 * do the operation and we're done. 2023 */ 2024 action = 1; 2025 done = 1; 2026 } else if (entry->port_type & port->port_type) { 2027 /* 2028 * Compare the user's type mask with the 2029 * particular frontend type to see if we 2030 * have a match. 2031 */ 2032 action = 1; 2033 done = 0; 2034 2035 /* 2036 * Make sure the user isn't trying to set 2037 * WWNs on multiple ports at the same time. 2038 */ 2039 if (cmd == CTL_SET_PORT_WWNS) { 2040 printf("%s: Can't set WWNs on " 2041 "multiple ports\n", __func__); 2042 retval = EINVAL; 2043 break; 2044 } 2045 } 2046 if (action != 0) { 2047 /* 2048 * XXX KDM we have to drop the lock here, 2049 * because the online/offline operations 2050 * can potentially block. We need to 2051 * reference count the frontends so they 2052 * can't go away, 2053 */ 2054 mtx_unlock(&softc->ctl_lock); 2055 2056 if (cmd == CTL_ENABLE_PORT) { 2057 ctl_port_online(port); 2058 } else if (cmd == CTL_DISABLE_PORT) { 2059 ctl_port_offline(port); 2060 } 2061 2062 mtx_lock(&softc->ctl_lock); 2063 2064 if (cmd == CTL_SET_PORT_WWNS) 2065 ctl_port_set_wwns(port, 2066 (entry->flags & CTL_PORT_WWNN_VALID) ? 2067 1 : 0, entry->wwnn, 2068 (entry->flags & CTL_PORT_WWPN_VALID) ? 2069 1 : 0, entry->wwpn); 2070 } 2071 if (done != 0) 2072 break; 2073 } 2074 mtx_unlock(&softc->ctl_lock); 2075 break; 2076 } 2077 case CTL_GET_PORT_LIST: { 2078 struct ctl_port *port; 2079 struct ctl_port_list *list; 2080 int i; 2081 2082 list = (struct ctl_port_list *)addr; 2083 2084 if (list->alloc_len != (list->alloc_num * 2085 sizeof(struct ctl_port_entry))) { 2086 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2087 "alloc_num %u * sizeof(struct ctl_port_entry) " 2088 "%zu\n", __func__, list->alloc_len, 2089 list->alloc_num, sizeof(struct ctl_port_entry)); 2090 retval = EINVAL; 2091 break; 2092 } 2093 list->fill_len = 0; 2094 list->fill_num = 0; 2095 list->dropped_num = 0; 2096 i = 0; 2097 mtx_lock(&softc->ctl_lock); 2098 STAILQ_FOREACH(port, &softc->port_list, links) { 2099 struct ctl_port_entry entry, *list_entry; 2100 2101 if (list->fill_num >= list->alloc_num) { 2102 list->dropped_num++; 2103 continue; 2104 } 2105 2106 entry.port_type = port->port_type; 2107 strlcpy(entry.port_name, port->port_name, 2108 sizeof(entry.port_name)); 2109 entry.targ_port = port->targ_port; 2110 entry.physical_port = port->physical_port; 2111 entry.virtual_port = port->virtual_port; 2112 entry.wwnn = port->wwnn; 2113 entry.wwpn = port->wwpn; 2114 if (port->status & CTL_PORT_STATUS_ONLINE) 2115 entry.online = 1; 2116 else 2117 entry.online = 0; 2118 2119 list_entry = &list->entries[i]; 2120 2121 retval = copyout(&entry, list_entry, sizeof(entry)); 2122 if (retval != 0) { 2123 printf("%s: CTL_GET_PORT_LIST: copyout " 2124 "returned %d\n", __func__, retval); 2125 break; 2126 } 2127 i++; 2128 list->fill_num++; 2129 list->fill_len += sizeof(entry); 2130 } 2131 mtx_unlock(&softc->ctl_lock); 2132 2133 /* 2134 * If this is non-zero, we had a copyout fault, so there's 2135 * probably no point in attempting to set the status inside 2136 * the structure. 2137 */ 2138 if (retval != 0) 2139 break; 2140 2141 if (list->dropped_num > 0) 2142 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2143 else 2144 list->status = CTL_PORT_LIST_OK; 2145 break; 2146 } 2147 case CTL_DUMP_OOA: { 2148 struct ctl_lun *lun; 2149 union ctl_io *io; 2150 char printbuf[128]; 2151 struct sbuf sb; 2152 2153 mtx_lock(&softc->ctl_lock); 2154 printf("Dumping OOA queues:\n"); 2155 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2156 mtx_lock(&lun->lun_lock); 2157 for (io = (union ctl_io *)TAILQ_FIRST( 2158 &lun->ooa_queue); io != NULL; 2159 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2160 ooa_links)) { 2161 sbuf_new(&sb, printbuf, sizeof(printbuf), 2162 SBUF_FIXEDLEN); 2163 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2164 (intmax_t)lun->lun, 2165 io->scsiio.tag_num, 2166 (io->io_hdr.flags & 2167 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2168 (io->io_hdr.flags & 2169 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2170 (io->io_hdr.flags & 2171 CTL_FLAG_ABORT) ? " ABORT" : "", 2172 (io->io_hdr.flags & 2173 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2174 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2175 sbuf_finish(&sb); 2176 printf("%s\n", sbuf_data(&sb)); 2177 } 2178 mtx_unlock(&lun->lun_lock); 2179 } 2180 printf("OOA queues dump done\n"); 2181 mtx_unlock(&softc->ctl_lock); 2182 break; 2183 } 2184 case CTL_GET_OOA: { 2185 struct ctl_lun *lun; 2186 struct ctl_ooa *ooa_hdr; 2187 struct ctl_ooa_entry *entries; 2188 uint32_t cur_fill_num; 2189 2190 ooa_hdr = (struct ctl_ooa *)addr; 2191 2192 if ((ooa_hdr->alloc_len == 0) 2193 || (ooa_hdr->alloc_num == 0)) { 2194 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2195 "must be non-zero\n", __func__, 2196 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2197 retval = EINVAL; 2198 break; 2199 } 2200 2201 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2202 sizeof(struct ctl_ooa_entry))) { 2203 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2204 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2205 __func__, ooa_hdr->alloc_len, 2206 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2207 retval = EINVAL; 2208 break; 2209 } 2210 2211 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2212 if (entries == NULL) { 2213 printf("%s: could not allocate %d bytes for OOA " 2214 "dump\n", __func__, ooa_hdr->alloc_len); 2215 retval = ENOMEM; 2216 break; 2217 } 2218 2219 mtx_lock(&softc->ctl_lock); 2220 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2221 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2222 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2223 mtx_unlock(&softc->ctl_lock); 2224 free(entries, M_CTL); 2225 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2226 __func__, (uintmax_t)ooa_hdr->lun_num); 2227 retval = EINVAL; 2228 break; 2229 } 2230 2231 cur_fill_num = 0; 2232 2233 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2234 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2235 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2236 ooa_hdr, entries); 2237 if (retval != 0) 2238 break; 2239 } 2240 if (retval != 0) { 2241 mtx_unlock(&softc->ctl_lock); 2242 free(entries, M_CTL); 2243 break; 2244 } 2245 } else { 2246 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2247 2248 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2249 entries); 2250 } 2251 mtx_unlock(&softc->ctl_lock); 2252 2253 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2254 ooa_hdr->fill_len = ooa_hdr->fill_num * 2255 sizeof(struct ctl_ooa_entry); 2256 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2257 if (retval != 0) { 2258 printf("%s: error copying out %d bytes for OOA dump\n", 2259 __func__, ooa_hdr->fill_len); 2260 } 2261 2262 getbintime(&ooa_hdr->cur_bt); 2263 2264 if (cur_fill_num > ooa_hdr->alloc_num) { 2265 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2266 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2267 } else { 2268 ooa_hdr->dropped_num = 0; 2269 ooa_hdr->status = CTL_OOA_OK; 2270 } 2271 2272 free(entries, M_CTL); 2273 break; 2274 } 2275 case CTL_CHECK_OOA: { 2276 union ctl_io *io; 2277 struct ctl_lun *lun; 2278 struct ctl_ooa_info *ooa_info; 2279 2280 2281 ooa_info = (struct ctl_ooa_info *)addr; 2282 2283 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2284 ooa_info->status = CTL_OOA_INVALID_LUN; 2285 break; 2286 } 2287 mtx_lock(&softc->ctl_lock); 2288 lun = softc->ctl_luns[ooa_info->lun_id]; 2289 if (lun == NULL) { 2290 mtx_unlock(&softc->ctl_lock); 2291 ooa_info->status = CTL_OOA_INVALID_LUN; 2292 break; 2293 } 2294 mtx_lock(&lun->lun_lock); 2295 mtx_unlock(&softc->ctl_lock); 2296 ooa_info->num_entries = 0; 2297 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2298 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2299 &io->io_hdr, ooa_links)) { 2300 ooa_info->num_entries++; 2301 } 2302 mtx_unlock(&lun->lun_lock); 2303 2304 ooa_info->status = CTL_OOA_SUCCESS; 2305 2306 break; 2307 } 2308 case CTL_DELAY_IO: { 2309 struct ctl_io_delay_info *delay_info; 2310#ifdef CTL_IO_DELAY 2311 struct ctl_lun *lun; 2312#endif /* CTL_IO_DELAY */ 2313 2314 delay_info = (struct ctl_io_delay_info *)addr; 2315 2316#ifdef CTL_IO_DELAY 2317 mtx_lock(&softc->ctl_lock); 2318 2319 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2320 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2321 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2322 } else { 2323 lun = softc->ctl_luns[delay_info->lun_id]; 2324 mtx_lock(&lun->lun_lock); 2325 2326 delay_info->status = CTL_DELAY_STATUS_OK; 2327 2328 switch (delay_info->delay_type) { 2329 case CTL_DELAY_TYPE_CONT: 2330 break; 2331 case CTL_DELAY_TYPE_ONESHOT: 2332 break; 2333 default: 2334 delay_info->status = 2335 CTL_DELAY_STATUS_INVALID_TYPE; 2336 break; 2337 } 2338 2339 switch (delay_info->delay_loc) { 2340 case CTL_DELAY_LOC_DATAMOVE: 2341 lun->delay_info.datamove_type = 2342 delay_info->delay_type; 2343 lun->delay_info.datamove_delay = 2344 delay_info->delay_secs; 2345 break; 2346 case CTL_DELAY_LOC_DONE: 2347 lun->delay_info.done_type = 2348 delay_info->delay_type; 2349 lun->delay_info.done_delay = 2350 delay_info->delay_secs; 2351 break; 2352 default: 2353 delay_info->status = 2354 CTL_DELAY_STATUS_INVALID_LOC; 2355 break; 2356 } 2357 mtx_unlock(&lun->lun_lock); 2358 } 2359 2360 mtx_unlock(&softc->ctl_lock); 2361#else 2362 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2363#endif /* CTL_IO_DELAY */ 2364 break; 2365 } 2366 case CTL_REALSYNC_SET: { 2367 int *syncstate; 2368 2369 syncstate = (int *)addr; 2370 2371 mtx_lock(&softc->ctl_lock); 2372 switch (*syncstate) { 2373 case 0: 2374 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2375 break; 2376 case 1: 2377 softc->flags |= CTL_FLAG_REAL_SYNC; 2378 break; 2379 default: 2380 retval = EINVAL; 2381 break; 2382 } 2383 mtx_unlock(&softc->ctl_lock); 2384 break; 2385 } 2386 case CTL_REALSYNC_GET: { 2387 int *syncstate; 2388 2389 syncstate = (int*)addr; 2390 2391 mtx_lock(&softc->ctl_lock); 2392 if (softc->flags & CTL_FLAG_REAL_SYNC) 2393 *syncstate = 1; 2394 else 2395 *syncstate = 0; 2396 mtx_unlock(&softc->ctl_lock); 2397 2398 break; 2399 } 2400 case CTL_SETSYNC: 2401 case CTL_GETSYNC: { 2402 struct ctl_sync_info *sync_info; 2403 struct ctl_lun *lun; 2404 2405 sync_info = (struct ctl_sync_info *)addr; 2406 2407 mtx_lock(&softc->ctl_lock); 2408 lun = softc->ctl_luns[sync_info->lun_id]; 2409 if (lun == NULL) { 2410 mtx_unlock(&softc->ctl_lock); 2411 sync_info->status = CTL_GS_SYNC_NO_LUN; 2412 } 2413 /* 2414 * Get or set the sync interval. We're not bounds checking 2415 * in the set case, hopefully the user won't do something 2416 * silly. 2417 */ 2418 mtx_lock(&lun->lun_lock); 2419 mtx_unlock(&softc->ctl_lock); 2420 if (cmd == CTL_GETSYNC) 2421 sync_info->sync_interval = lun->sync_interval; 2422 else 2423 lun->sync_interval = sync_info->sync_interval; 2424 mtx_unlock(&lun->lun_lock); 2425 2426 sync_info->status = CTL_GS_SYNC_OK; 2427 2428 break; 2429 } 2430 case CTL_GETSTATS: { 2431 struct ctl_stats *stats; 2432 struct ctl_lun *lun; 2433 int i; 2434 2435 stats = (struct ctl_stats *)addr; 2436 2437 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2438 stats->alloc_len) { 2439 stats->status = CTL_SS_NEED_MORE_SPACE; 2440 stats->num_luns = softc->num_luns; 2441 break; 2442 } 2443 /* 2444 * XXX KDM no locking here. If the LUN list changes, 2445 * things can blow up. 2446 */ 2447 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2448 i++, lun = STAILQ_NEXT(lun, links)) { 2449 retval = copyout(&lun->stats, &stats->lun_stats[i], 2450 sizeof(lun->stats)); 2451 if (retval != 0) 2452 break; 2453 } 2454 stats->num_luns = softc->num_luns; 2455 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2456 softc->num_luns; 2457 stats->status = CTL_SS_OK; 2458#ifdef CTL_TIME_IO 2459 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2460#else 2461 stats->flags = CTL_STATS_FLAG_NONE; 2462#endif 2463 getnanouptime(&stats->timestamp); 2464 break; 2465 } 2466 case CTL_ERROR_INJECT: { 2467 struct ctl_error_desc *err_desc, *new_err_desc; 2468 struct ctl_lun *lun; 2469 2470 err_desc = (struct ctl_error_desc *)addr; 2471 2472 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2473 M_WAITOK | M_ZERO); 2474 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2475 2476 mtx_lock(&softc->ctl_lock); 2477 lun = softc->ctl_luns[err_desc->lun_id]; 2478 if (lun == NULL) { 2479 mtx_unlock(&softc->ctl_lock); 2480 free(new_err_desc, M_CTL); 2481 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2482 __func__, (uintmax_t)err_desc->lun_id); 2483 retval = EINVAL; 2484 break; 2485 } 2486 mtx_lock(&lun->lun_lock); 2487 mtx_unlock(&softc->ctl_lock); 2488 2489 /* 2490 * We could do some checking here to verify the validity 2491 * of the request, but given the complexity of error 2492 * injection requests, the checking logic would be fairly 2493 * complex. 2494 * 2495 * For now, if the request is invalid, it just won't get 2496 * executed and might get deleted. 2497 */ 2498 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2499 2500 /* 2501 * XXX KDM check to make sure the serial number is unique, 2502 * in case we somehow manage to wrap. That shouldn't 2503 * happen for a very long time, but it's the right thing to 2504 * do. 2505 */ 2506 new_err_desc->serial = lun->error_serial; 2507 err_desc->serial = lun->error_serial; 2508 lun->error_serial++; 2509 2510 mtx_unlock(&lun->lun_lock); 2511 break; 2512 } 2513 case CTL_ERROR_INJECT_DELETE: { 2514 struct ctl_error_desc *delete_desc, *desc, *desc2; 2515 struct ctl_lun *lun; 2516 int delete_done; 2517 2518 delete_desc = (struct ctl_error_desc *)addr; 2519 delete_done = 0; 2520 2521 mtx_lock(&softc->ctl_lock); 2522 lun = softc->ctl_luns[delete_desc->lun_id]; 2523 if (lun == NULL) { 2524 mtx_unlock(&softc->ctl_lock); 2525 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2526 __func__, (uintmax_t)delete_desc->lun_id); 2527 retval = EINVAL; 2528 break; 2529 } 2530 mtx_lock(&lun->lun_lock); 2531 mtx_unlock(&softc->ctl_lock); 2532 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2533 if (desc->serial != delete_desc->serial) 2534 continue; 2535 2536 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2537 links); 2538 free(desc, M_CTL); 2539 delete_done = 1; 2540 } 2541 mtx_unlock(&lun->lun_lock); 2542 if (delete_done == 0) { 2543 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2544 "error serial %ju on LUN %u\n", __func__, 2545 delete_desc->serial, delete_desc->lun_id); 2546 retval = EINVAL; 2547 break; 2548 } 2549 break; 2550 } 2551 case CTL_DUMP_STRUCTS: { 2552 int i, j, k; 2553 struct ctl_port *port; 2554 struct ctl_frontend *fe; 2555 2556 mtx_lock(&softc->ctl_lock); 2557 printf("CTL Persistent Reservation information start:\n"); 2558 for (i = 0; i < CTL_MAX_LUNS; i++) { 2559 struct ctl_lun *lun; 2560 2561 lun = softc->ctl_luns[i]; 2562 2563 if ((lun == NULL) 2564 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2565 continue; 2566 2567 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2568 if (lun->pr_keys[j] == NULL) 2569 continue; 2570 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2571 if (lun->pr_keys[j][k] == 0) 2572 continue; 2573 printf(" LUN %d port %d iid %d key " 2574 "%#jx\n", i, j, k, 2575 (uintmax_t)lun->pr_keys[j][k]); 2576 } 2577 } 2578 } 2579 printf("CTL Persistent Reservation information end\n"); 2580 printf("CTL Ports:\n"); 2581 STAILQ_FOREACH(port, &softc->port_list, links) { 2582 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2583 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2584 port->frontend->name, port->port_type, 2585 port->physical_port, port->virtual_port, 2586 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2587 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2588 if (port->wwpn_iid[j].in_use == 0 && 2589 port->wwpn_iid[j].wwpn == 0 && 2590 port->wwpn_iid[j].name == NULL) 2591 continue; 2592 2593 printf(" iid %u use %d WWPN %#jx '%s'\n", 2594 j, port->wwpn_iid[j].in_use, 2595 (uintmax_t)port->wwpn_iid[j].wwpn, 2596 port->wwpn_iid[j].name); 2597 } 2598 } 2599 printf("CTL Port information end\n"); 2600 mtx_unlock(&softc->ctl_lock); 2601 /* 2602 * XXX KDM calling this without a lock. We'd likely want 2603 * to drop the lock before calling the frontend's dump 2604 * routine anyway. 2605 */ 2606 printf("CTL Frontends:\n"); 2607 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2608 printf(" Frontend '%s'\n", fe->name); 2609 if (fe->fe_dump != NULL) 2610 fe->fe_dump(); 2611 } 2612 printf("CTL Frontend information end\n"); 2613 break; 2614 } 2615 case CTL_LUN_REQ: { 2616 struct ctl_lun_req *lun_req; 2617 struct ctl_backend_driver *backend; 2618 2619 lun_req = (struct ctl_lun_req *)addr; 2620 2621 backend = ctl_backend_find(lun_req->backend); 2622 if (backend == NULL) { 2623 lun_req->status = CTL_LUN_ERROR; 2624 snprintf(lun_req->error_str, 2625 sizeof(lun_req->error_str), 2626 "Backend \"%s\" not found.", 2627 lun_req->backend); 2628 break; 2629 } 2630 if (lun_req->num_be_args > 0) { 2631 lun_req->kern_be_args = ctl_copyin_args( 2632 lun_req->num_be_args, 2633 lun_req->be_args, 2634 lun_req->error_str, 2635 sizeof(lun_req->error_str)); 2636 if (lun_req->kern_be_args == NULL) { 2637 lun_req->status = CTL_LUN_ERROR; 2638 break; 2639 } 2640 } 2641 2642 retval = backend->ioctl(dev, cmd, addr, flag, td); 2643 2644 if (lun_req->num_be_args > 0) { 2645 ctl_copyout_args(lun_req->num_be_args, 2646 lun_req->kern_be_args); 2647 ctl_free_args(lun_req->num_be_args, 2648 lun_req->kern_be_args); 2649 } 2650 break; 2651 } 2652 case CTL_LUN_LIST: { 2653 struct sbuf *sb; 2654 struct ctl_lun *lun; 2655 struct ctl_lun_list *list; 2656 struct ctl_option *opt; 2657 2658 list = (struct ctl_lun_list *)addr; 2659 2660 /* 2661 * Allocate a fixed length sbuf here, based on the length 2662 * of the user's buffer. We could allocate an auto-extending 2663 * buffer, and then tell the user how much larger our 2664 * amount of data is than his buffer, but that presents 2665 * some problems: 2666 * 2667 * 1. The sbuf(9) routines use a blocking malloc, and so 2668 * we can't hold a lock while calling them with an 2669 * auto-extending buffer. 2670 * 2671 * 2. There is not currently a LUN reference counting 2672 * mechanism, outside of outstanding transactions on 2673 * the LUN's OOA queue. So a LUN could go away on us 2674 * while we're getting the LUN number, backend-specific 2675 * information, etc. Thus, given the way things 2676 * currently work, we need to hold the CTL lock while 2677 * grabbing LUN information. 2678 * 2679 * So, from the user's standpoint, the best thing to do is 2680 * allocate what he thinks is a reasonable buffer length, 2681 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 2682 * double the buffer length and try again. (And repeat 2683 * that until he succeeds.) 2684 */ 2685 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 2686 if (sb == NULL) { 2687 list->status = CTL_LUN_LIST_ERROR; 2688 snprintf(list->error_str, sizeof(list->error_str), 2689 "Unable to allocate %d bytes for LUN list", 2690 list->alloc_len); 2691 break; 2692 } 2693 2694 sbuf_printf(sb, "<ctllunlist>\n"); 2695 2696 mtx_lock(&softc->ctl_lock); 2697 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2698 mtx_lock(&lun->lun_lock); 2699 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 2700 (uintmax_t)lun->lun); 2701 2702 /* 2703 * Bail out as soon as we see that we've overfilled 2704 * the buffer. 2705 */ 2706 if (retval != 0) 2707 break; 2708 2709 retval = sbuf_printf(sb, "\t<backend_type>%s" 2710 "</backend_type>\n", 2711 (lun->backend == NULL) ? "none" : 2712 lun->backend->name); 2713 2714 if (retval != 0) 2715 break; 2716 2717 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 2718 lun->be_lun->lun_type); 2719 2720 if (retval != 0) 2721 break; 2722 2723 if (lun->backend == NULL) { 2724 retval = sbuf_printf(sb, "</lun>\n"); 2725 if (retval != 0) 2726 break; 2727 continue; 2728 } 2729 2730 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 2731 (lun->be_lun->maxlba > 0) ? 2732 lun->be_lun->maxlba + 1 : 0); 2733 2734 if (retval != 0) 2735 break; 2736 2737 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 2738 lun->be_lun->blocksize); 2739 2740 if (retval != 0) 2741 break; 2742 2743 retval = sbuf_printf(sb, "\t<serial_number>"); 2744 2745 if (retval != 0) 2746 break; 2747 2748 retval = ctl_sbuf_printf_esc(sb, 2749 lun->be_lun->serial_num, 2750 sizeof(lun->be_lun->serial_num)); 2751 2752 if (retval != 0) 2753 break; 2754 2755 retval = sbuf_printf(sb, "</serial_number>\n"); 2756 2757 if (retval != 0) 2758 break; 2759 2760 retval = sbuf_printf(sb, "\t<device_id>"); 2761 2762 if (retval != 0) 2763 break; 2764 2765 retval = ctl_sbuf_printf_esc(sb, 2766 lun->be_lun->device_id, 2767 sizeof(lun->be_lun->device_id)); 2768 2769 if (retval != 0) 2770 break; 2771 2772 retval = sbuf_printf(sb, "</device_id>\n"); 2773 2774 if (retval != 0) 2775 break; 2776 2777 if (lun->backend->lun_info != NULL) { 2778 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 2779 if (retval != 0) 2780 break; 2781 } 2782 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 2783 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 2784 opt->name, opt->value, opt->name); 2785 if (retval != 0) 2786 break; 2787 } 2788 2789 retval = sbuf_printf(sb, "</lun>\n"); 2790 2791 if (retval != 0) 2792 break; 2793 mtx_unlock(&lun->lun_lock); 2794 } 2795 if (lun != NULL) 2796 mtx_unlock(&lun->lun_lock); 2797 mtx_unlock(&softc->ctl_lock); 2798 2799 if ((retval != 0) 2800 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 2801 retval = 0; 2802 sbuf_delete(sb); 2803 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 2804 snprintf(list->error_str, sizeof(list->error_str), 2805 "Out of space, %d bytes is too small", 2806 list->alloc_len); 2807 break; 2808 } 2809 2810 sbuf_finish(sb); 2811 2812 retval = copyout(sbuf_data(sb), list->lun_xml, 2813 sbuf_len(sb) + 1); 2814 2815 list->fill_len = sbuf_len(sb) + 1; 2816 list->status = CTL_LUN_LIST_OK; 2817 sbuf_delete(sb); 2818 break; 2819 } 2820 case CTL_ISCSI: { 2821 struct ctl_iscsi *ci; 2822 struct ctl_frontend *fe; 2823 2824 ci = (struct ctl_iscsi *)addr; 2825 2826 fe = ctl_frontend_find("iscsi"); 2827 if (fe == NULL) { 2828 ci->status = CTL_ISCSI_ERROR; 2829 snprintf(ci->error_str, sizeof(ci->error_str), 2830 "Frontend \"iscsi\" not found."); 2831 break; 2832 } 2833 2834 retval = fe->ioctl(dev, cmd, addr, flag, td); 2835 break; 2836 } 2837 case CTL_PORT_REQ: { 2838 struct ctl_req *req; 2839 struct ctl_frontend *fe; 2840 2841 req = (struct ctl_req *)addr; 2842 2843 fe = ctl_frontend_find(req->driver); 2844 if (fe == NULL) { 2845 req->status = CTL_LUN_ERROR; 2846 snprintf(req->error_str, sizeof(req->error_str), 2847 "Frontend \"%s\" not found.", req->driver); 2848 break; 2849 } 2850 if (req->num_args > 0) { 2851 req->kern_args = ctl_copyin_args(req->num_args, 2852 req->args, req->error_str, sizeof(req->error_str)); 2853 if (req->kern_args == NULL) { 2854 req->status = CTL_LUN_ERROR; 2855 break; 2856 } 2857 } 2858 2859 retval = fe->ioctl(dev, cmd, addr, flag, td); 2860 2861 if (req->num_args > 0) { 2862 ctl_copyout_args(req->num_args, req->kern_args); 2863 ctl_free_args(req->num_args, req->kern_args); 2864 } 2865 break; 2866 } 2867 case CTL_PORT_LIST: { 2868 struct sbuf *sb; 2869 struct ctl_port *port; 2870 struct ctl_lun_list *list; 2871 struct ctl_option *opt; 2872 int j; 2873 uint32_t plun; 2874 2875 list = (struct ctl_lun_list *)addr; 2876 2877 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 2878 if (sb == NULL) { 2879 list->status = CTL_LUN_LIST_ERROR; 2880 snprintf(list->error_str, sizeof(list->error_str), 2881 "Unable to allocate %d bytes for LUN list", 2882 list->alloc_len); 2883 break; 2884 } 2885 2886 sbuf_printf(sb, "<ctlportlist>\n"); 2887 2888 mtx_lock(&softc->ctl_lock); 2889 STAILQ_FOREACH(port, &softc->port_list, links) { 2890 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 2891 (uintmax_t)port->targ_port); 2892 2893 /* 2894 * Bail out as soon as we see that we've overfilled 2895 * the buffer. 2896 */ 2897 if (retval != 0) 2898 break; 2899 2900 retval = sbuf_printf(sb, "\t<frontend_type>%s" 2901 "</frontend_type>\n", port->frontend->name); 2902 if (retval != 0) 2903 break; 2904 2905 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 2906 port->port_type); 2907 if (retval != 0) 2908 break; 2909 2910 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 2911 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 2912 if (retval != 0) 2913 break; 2914 2915 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 2916 port->port_name); 2917 if (retval != 0) 2918 break; 2919 2920 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 2921 port->physical_port); 2922 if (retval != 0) 2923 break; 2924 2925 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 2926 port->virtual_port); 2927 if (retval != 0) 2928 break; 2929 2930 if (port->target_devid != NULL) { 2931 sbuf_printf(sb, "\t<target>"); 2932 ctl_id_sbuf(port->target_devid, sb); 2933 sbuf_printf(sb, "</target>\n"); 2934 } 2935 2936 if (port->port_devid != NULL) { 2937 sbuf_printf(sb, "\t<port>"); 2938 ctl_id_sbuf(port->port_devid, sb); 2939 sbuf_printf(sb, "</port>\n"); 2940 } 2941 2942 if (port->port_info != NULL) { 2943 retval = port->port_info(port->onoff_arg, sb); 2944 if (retval != 0) 2945 break; 2946 } 2947 STAILQ_FOREACH(opt, &port->options, links) { 2948 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 2949 opt->name, opt->value, opt->name); 2950 if (retval != 0) 2951 break; 2952 } 2953 2954 if (port->lun_map != NULL) { 2955 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 2956 for (j = 0; j < CTL_MAX_LUNS; j++) { 2957 plun = ctl_lun_map_from_port(port, j); 2958 if (plun >= CTL_MAX_LUNS) 2959 continue; 2960 sbuf_printf(sb, 2961 "\t<lun id=\"%u\">%u</lun>\n", 2962 j, plun); 2963 } 2964 } 2965 2966 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2967 if (port->wwpn_iid[j].in_use == 0 || 2968 (port->wwpn_iid[j].wwpn == 0 && 2969 port->wwpn_iid[j].name == NULL)) 2970 continue; 2971 2972 if (port->wwpn_iid[j].name != NULL) 2973 retval = sbuf_printf(sb, 2974 "\t<initiator id=\"%u\">%s</initiator>\n", 2975 j, port->wwpn_iid[j].name); 2976 else 2977 retval = sbuf_printf(sb, 2978 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 2979 j, port->wwpn_iid[j].wwpn); 2980 if (retval != 0) 2981 break; 2982 } 2983 if (retval != 0) 2984 break; 2985 2986 retval = sbuf_printf(sb, "</targ_port>\n"); 2987 if (retval != 0) 2988 break; 2989 } 2990 mtx_unlock(&softc->ctl_lock); 2991 2992 if ((retval != 0) 2993 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 2994 retval = 0; 2995 sbuf_delete(sb); 2996 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 2997 snprintf(list->error_str, sizeof(list->error_str), 2998 "Out of space, %d bytes is too small", 2999 list->alloc_len); 3000 break; 3001 } 3002 3003 sbuf_finish(sb); 3004 3005 retval = copyout(sbuf_data(sb), list->lun_xml, 3006 sbuf_len(sb) + 1); 3007 3008 list->fill_len = sbuf_len(sb) + 1; 3009 list->status = CTL_LUN_LIST_OK; 3010 sbuf_delete(sb); 3011 break; 3012 } 3013 case CTL_LUN_MAP: { 3014 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3015 struct ctl_port *port; 3016 3017 mtx_lock(&softc->ctl_lock); 3018 if (lm->port >= CTL_MAX_PORTS || 3019 (port = softc->ctl_ports[lm->port]) == NULL) { 3020 mtx_unlock(&softc->ctl_lock); 3021 return (ENXIO); 3022 } 3023 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3024 if (lm->plun < CTL_MAX_LUNS) { 3025 if (lm->lun == UINT32_MAX) 3026 retval = ctl_lun_map_unset(port, lm->plun); 3027 else if (lm->lun < CTL_MAX_LUNS && 3028 softc->ctl_luns[lm->lun] != NULL) 3029 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3030 else 3031 return (ENXIO); 3032 } else if (lm->plun == UINT32_MAX) { 3033 if (lm->lun == UINT32_MAX) 3034 retval = ctl_lun_map_deinit(port); 3035 else 3036 retval = ctl_lun_map_init(port); 3037 } else 3038 return (ENXIO); 3039 break; 3040 } 3041 default: { 3042 /* XXX KDM should we fix this? */ 3043#if 0 3044 struct ctl_backend_driver *backend; 3045 unsigned int type; 3046 int found; 3047 3048 found = 0; 3049 3050 /* 3051 * We encode the backend type as the ioctl type for backend 3052 * ioctls. So parse it out here, and then search for a 3053 * backend of this type. 3054 */ 3055 type = _IOC_TYPE(cmd); 3056 3057 STAILQ_FOREACH(backend, &softc->be_list, links) { 3058 if (backend->type == type) { 3059 found = 1; 3060 break; 3061 } 3062 } 3063 if (found == 0) { 3064 printf("ctl: unknown ioctl command %#lx or backend " 3065 "%d\n", cmd, type); 3066 retval = EINVAL; 3067 break; 3068 } 3069 retval = backend->ioctl(dev, cmd, addr, flag, td); 3070#endif 3071 retval = ENOTTY; 3072 break; 3073 } 3074 } 3075 return (retval); 3076} 3077 3078uint32_t 3079ctl_get_initindex(struct ctl_nexus *nexus) 3080{ 3081 if (nexus->targ_port < CTL_MAX_PORTS) 3082 return (nexus->initid.id + 3083 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3084 else 3085 return (nexus->initid.id + 3086 ((nexus->targ_port - CTL_MAX_PORTS) * 3087 CTL_MAX_INIT_PER_PORT)); 3088} 3089 3090uint32_t 3091ctl_get_resindex(struct ctl_nexus *nexus) 3092{ 3093 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3094} 3095 3096uint32_t 3097ctl_port_idx(int port_num) 3098{ 3099 if (port_num < CTL_MAX_PORTS) 3100 return(port_num); 3101 else 3102 return(port_num - CTL_MAX_PORTS); 3103} 3104 3105int 3106ctl_lun_map_init(struct ctl_port *port) 3107{ 3108 struct ctl_softc *softc = control_softc; 3109 struct ctl_lun *lun; 3110 uint32_t i; 3111 3112 if (port->lun_map == NULL) 3113 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3114 M_CTL, M_NOWAIT); 3115 if (port->lun_map == NULL) 3116 return (ENOMEM); 3117 for (i = 0; i < CTL_MAX_LUNS; i++) 3118 port->lun_map[i] = UINT32_MAX; 3119 if (port->status & CTL_PORT_STATUS_ONLINE && 3120 port->lun_disable != NULL) { 3121 STAILQ_FOREACH(lun, &softc->lun_list, links) 3122 port->lun_disable(port->targ_lun_arg, lun->lun); 3123 } 3124 return (0); 3125} 3126 3127int 3128ctl_lun_map_deinit(struct ctl_port *port) 3129{ 3130 struct ctl_softc *softc = control_softc; 3131 struct ctl_lun *lun; 3132 3133 if (port->lun_map == NULL) 3134 return (0); 3135 free(port->lun_map, M_CTL); 3136 port->lun_map = NULL; 3137 if (port->status & CTL_PORT_STATUS_ONLINE && 3138 port->lun_enable != NULL) { 3139 STAILQ_FOREACH(lun, &softc->lun_list, links) 3140 port->lun_enable(port->targ_lun_arg, lun->lun); 3141 } 3142 return (0); 3143} 3144 3145int 3146ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3147{ 3148 int status; 3149 uint32_t old; 3150 3151 if (port->lun_map == NULL) { 3152 status = ctl_lun_map_init(port); 3153 if (status != 0) 3154 return (status); 3155 } 3156 old = port->lun_map[plun]; 3157 port->lun_map[plun] = glun; 3158 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS && 3159 port->lun_enable != NULL) 3160 port->lun_enable(port->targ_lun_arg, plun); 3161 return (0); 3162} 3163 3164int 3165ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3166{ 3167 uint32_t old; 3168 3169 if (port->lun_map == NULL) 3170 return (0); 3171 old = port->lun_map[plun]; 3172 port->lun_map[plun] = UINT32_MAX; 3173 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS && 3174 port->lun_disable != NULL) 3175 port->lun_disable(port->targ_lun_arg, plun); 3176 return (0); 3177} 3178 3179uint32_t 3180ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3181{ 3182 3183 if (port == NULL) 3184 return (UINT32_MAX); 3185 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3186 return (lun_id); 3187 return (port->lun_map[lun_id]); 3188} 3189 3190uint32_t 3191ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3192{ 3193 uint32_t i; 3194 3195 if (port == NULL) 3196 return (UINT32_MAX); 3197 if (port->lun_map == NULL) 3198 return (lun_id); 3199 for (i = 0; i < CTL_MAX_LUNS; i++) { 3200 if (port->lun_map[i] == lun_id) 3201 return (i); 3202 } 3203 return (UINT32_MAX); 3204} 3205 3206static struct ctl_port * 3207ctl_io_port(struct ctl_io_hdr *io_hdr) 3208{ 3209 int port_num; 3210 3211 port_num = io_hdr->nexus.targ_port; 3212 return (control_softc->ctl_ports[ctl_port_idx(port_num)]); 3213} 3214 3215/* 3216 * Note: This only works for bitmask sizes that are at least 32 bits, and 3217 * that are a power of 2. 3218 */ 3219int 3220ctl_ffz(uint32_t *mask, uint32_t size) 3221{ 3222 uint32_t num_chunks, num_pieces; 3223 int i, j; 3224 3225 num_chunks = (size >> 5); 3226 if (num_chunks == 0) 3227 num_chunks++; 3228 num_pieces = MIN((sizeof(uint32_t) * 8), size); 3229 3230 for (i = 0; i < num_chunks; i++) { 3231 for (j = 0; j < num_pieces; j++) { 3232 if ((mask[i] & (1 << j)) == 0) 3233 return ((i << 5) + j); 3234 } 3235 } 3236 3237 return (-1); 3238} 3239 3240int 3241ctl_set_mask(uint32_t *mask, uint32_t bit) 3242{ 3243 uint32_t chunk, piece; 3244 3245 chunk = bit >> 5; 3246 piece = bit % (sizeof(uint32_t) * 8); 3247 3248 if ((mask[chunk] & (1 << piece)) != 0) 3249 return (-1); 3250 else 3251 mask[chunk] |= (1 << piece); 3252 3253 return (0); 3254} 3255 3256int 3257ctl_clear_mask(uint32_t *mask, uint32_t bit) 3258{ 3259 uint32_t chunk, piece; 3260 3261 chunk = bit >> 5; 3262 piece = bit % (sizeof(uint32_t) * 8); 3263 3264 if ((mask[chunk] & (1 << piece)) == 0) 3265 return (-1); 3266 else 3267 mask[chunk] &= ~(1 << piece); 3268 3269 return (0); 3270} 3271 3272int 3273ctl_is_set(uint32_t *mask, uint32_t bit) 3274{ 3275 uint32_t chunk, piece; 3276 3277 chunk = bit >> 5; 3278 piece = bit % (sizeof(uint32_t) * 8); 3279 3280 if ((mask[chunk] & (1 << piece)) == 0) 3281 return (0); 3282 else 3283 return (1); 3284} 3285 3286static uint64_t 3287ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3288{ 3289 uint64_t *t; 3290 3291 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3292 if (t == NULL) 3293 return (0); 3294 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3295} 3296 3297static void 3298ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3299{ 3300 uint64_t *t; 3301 3302 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3303 if (t == NULL) 3304 return; 3305 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3306} 3307 3308static void 3309ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3310{ 3311 uint64_t *p; 3312 u_int i; 3313 3314 i = residx/CTL_MAX_INIT_PER_PORT; 3315 if (lun->pr_keys[i] != NULL) 3316 return; 3317 mtx_unlock(&lun->lun_lock); 3318 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3319 M_WAITOK | M_ZERO); 3320 mtx_lock(&lun->lun_lock); 3321 if (lun->pr_keys[i] == NULL) 3322 lun->pr_keys[i] = p; 3323 else 3324 free(p, M_CTL); 3325} 3326 3327static void 3328ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3329{ 3330 uint64_t *t; 3331 3332 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3333 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3334 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3335} 3336 3337/* 3338 * ctl_softc, pool_name, total_ctl_io are passed in. 3339 * npool is passed out. 3340 */ 3341int 3342ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3343 uint32_t total_ctl_io, void **npool) 3344{ 3345#ifdef IO_POOLS 3346 struct ctl_io_pool *pool; 3347 3348 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3349 M_NOWAIT | M_ZERO); 3350 if (pool == NULL) 3351 return (ENOMEM); 3352 3353 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3354 pool->ctl_softc = ctl_softc; 3355 pool->zone = uma_zsecond_create(pool->name, NULL, 3356 NULL, NULL, NULL, ctl_softc->io_zone); 3357 /* uma_prealloc(pool->zone, total_ctl_io); */ 3358 3359 *npool = pool; 3360#else 3361 *npool = ctl_softc->io_zone; 3362#endif 3363 return (0); 3364} 3365 3366void 3367ctl_pool_free(struct ctl_io_pool *pool) 3368{ 3369 3370 if (pool == NULL) 3371 return; 3372 3373#ifdef IO_POOLS 3374 uma_zdestroy(pool->zone); 3375 free(pool, M_CTL); 3376#endif 3377} 3378 3379union ctl_io * 3380ctl_alloc_io(void *pool_ref) 3381{ 3382 union ctl_io *io; 3383#ifdef IO_POOLS 3384 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3385 3386 io = uma_zalloc(pool->zone, M_WAITOK); 3387#else 3388 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3389#endif 3390 if (io != NULL) 3391 io->io_hdr.pool = pool_ref; 3392 return (io); 3393} 3394 3395union ctl_io * 3396ctl_alloc_io_nowait(void *pool_ref) 3397{ 3398 union ctl_io *io; 3399#ifdef IO_POOLS 3400 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3401 3402 io = uma_zalloc(pool->zone, M_NOWAIT); 3403#else 3404 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3405#endif 3406 if (io != NULL) 3407 io->io_hdr.pool = pool_ref; 3408 return (io); 3409} 3410 3411void 3412ctl_free_io(union ctl_io *io) 3413{ 3414#ifdef IO_POOLS 3415 struct ctl_io_pool *pool; 3416#endif 3417 3418 if (io == NULL) 3419 return; 3420 3421#ifdef IO_POOLS 3422 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3423 uma_zfree(pool->zone, io); 3424#else 3425 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3426#endif 3427} 3428 3429void 3430ctl_zero_io(union ctl_io *io) 3431{ 3432 void *pool_ref; 3433 3434 if (io == NULL) 3435 return; 3436 3437 /* 3438 * May need to preserve linked list pointers at some point too. 3439 */ 3440 pool_ref = io->io_hdr.pool; 3441 memset(io, 0, sizeof(*io)); 3442 io->io_hdr.pool = pool_ref; 3443} 3444 3445/* 3446 * This routine is currently used for internal copies of ctl_ios that need 3447 * to persist for some reason after we've already returned status to the 3448 * FETD. (Thus the flag set.) 3449 * 3450 * XXX XXX 3451 * Note that this makes a blind copy of all fields in the ctl_io, except 3452 * for the pool reference. This includes any memory that has been 3453 * allocated! That memory will no longer be valid after done has been 3454 * called, so this would be VERY DANGEROUS for command that actually does 3455 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3456 * start and stop commands, which don't transfer any data, so this is not a 3457 * problem. If it is used for anything else, the caller would also need to 3458 * allocate data buffer space and this routine would need to be modified to 3459 * copy the data buffer(s) as well. 3460 */ 3461void 3462ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3463{ 3464 void *pool_ref; 3465 3466 if ((src == NULL) 3467 || (dest == NULL)) 3468 return; 3469 3470 /* 3471 * May need to preserve linked list pointers at some point too. 3472 */ 3473 pool_ref = dest->io_hdr.pool; 3474 3475 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest))); 3476 3477 dest->io_hdr.pool = pool_ref; 3478 /* 3479 * We need to know that this is an internal copy, and doesn't need 3480 * to get passed back to the FETD that allocated it. 3481 */ 3482 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3483} 3484 3485int 3486ctl_expand_number(const char *buf, uint64_t *num) 3487{ 3488 char *endptr; 3489 uint64_t number; 3490 unsigned shift; 3491 3492 number = strtoq(buf, &endptr, 0); 3493 3494 switch (tolower((unsigned char)*endptr)) { 3495 case 'e': 3496 shift = 60; 3497 break; 3498 case 'p': 3499 shift = 50; 3500 break; 3501 case 't': 3502 shift = 40; 3503 break; 3504 case 'g': 3505 shift = 30; 3506 break; 3507 case 'm': 3508 shift = 20; 3509 break; 3510 case 'k': 3511 shift = 10; 3512 break; 3513 case 'b': 3514 case '\0': /* No unit. */ 3515 *num = number; 3516 return (0); 3517 default: 3518 /* Unrecognized unit. */ 3519 return (-1); 3520 } 3521 3522 if ((number << shift) >> shift != number) { 3523 /* Overflow */ 3524 return (-1); 3525 } 3526 *num = number << shift; 3527 return (0); 3528} 3529 3530 3531/* 3532 * This routine could be used in the future to load default and/or saved 3533 * mode page parameters for a particuar lun. 3534 */ 3535static int 3536ctl_init_page_index(struct ctl_lun *lun) 3537{ 3538 int i; 3539 struct ctl_page_index *page_index; 3540 const char *value; 3541 uint64_t ival; 3542 3543 memcpy(&lun->mode_pages.index, page_index_template, 3544 sizeof(page_index_template)); 3545 3546 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3547 3548 page_index = &lun->mode_pages.index[i]; 3549 /* 3550 * If this is a disk-only mode page, there's no point in 3551 * setting it up. For some pages, we have to have some 3552 * basic information about the disk in order to calculate the 3553 * mode page data. 3554 */ 3555 if ((lun->be_lun->lun_type != T_DIRECT) 3556 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3557 continue; 3558 3559 switch (page_index->page_code & SMPH_PC_MASK) { 3560 case SMS_RW_ERROR_RECOVERY_PAGE: { 3561 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3562 panic("subpage is incorrect!"); 3563 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3564 &rw_er_page_default, 3565 sizeof(rw_er_page_default)); 3566 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3567 &rw_er_page_changeable, 3568 sizeof(rw_er_page_changeable)); 3569 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3570 &rw_er_page_default, 3571 sizeof(rw_er_page_default)); 3572 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3573 &rw_er_page_default, 3574 sizeof(rw_er_page_default)); 3575 page_index->page_data = 3576 (uint8_t *)lun->mode_pages.rw_er_page; 3577 break; 3578 } 3579 case SMS_FORMAT_DEVICE_PAGE: { 3580 struct scsi_format_page *format_page; 3581 3582 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3583 panic("subpage is incorrect!"); 3584 3585 /* 3586 * Sectors per track are set above. Bytes per 3587 * sector need to be set here on a per-LUN basis. 3588 */ 3589 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3590 &format_page_default, 3591 sizeof(format_page_default)); 3592 memcpy(&lun->mode_pages.format_page[ 3593 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3594 sizeof(format_page_changeable)); 3595 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3596 &format_page_default, 3597 sizeof(format_page_default)); 3598 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3599 &format_page_default, 3600 sizeof(format_page_default)); 3601 3602 format_page = &lun->mode_pages.format_page[ 3603 CTL_PAGE_CURRENT]; 3604 scsi_ulto2b(lun->be_lun->blocksize, 3605 format_page->bytes_per_sector); 3606 3607 format_page = &lun->mode_pages.format_page[ 3608 CTL_PAGE_DEFAULT]; 3609 scsi_ulto2b(lun->be_lun->blocksize, 3610 format_page->bytes_per_sector); 3611 3612 format_page = &lun->mode_pages.format_page[ 3613 CTL_PAGE_SAVED]; 3614 scsi_ulto2b(lun->be_lun->blocksize, 3615 format_page->bytes_per_sector); 3616 3617 page_index->page_data = 3618 (uint8_t *)lun->mode_pages.format_page; 3619 break; 3620 } 3621 case SMS_RIGID_DISK_PAGE: { 3622 struct scsi_rigid_disk_page *rigid_disk_page; 3623 uint32_t sectors_per_cylinder; 3624 uint64_t cylinders; 3625#ifndef __XSCALE__ 3626 int shift; 3627#endif /* !__XSCALE__ */ 3628 3629 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3630 panic("invalid subpage value %d", 3631 page_index->subpage); 3632 3633 /* 3634 * Rotation rate and sectors per track are set 3635 * above. We calculate the cylinders here based on 3636 * capacity. Due to the number of heads and 3637 * sectors per track we're using, smaller arrays 3638 * may turn out to have 0 cylinders. Linux and 3639 * FreeBSD don't pay attention to these mode pages 3640 * to figure out capacity, but Solaris does. It 3641 * seems to deal with 0 cylinders just fine, and 3642 * works out a fake geometry based on the capacity. 3643 */ 3644 memcpy(&lun->mode_pages.rigid_disk_page[ 3645 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3646 sizeof(rigid_disk_page_default)); 3647 memcpy(&lun->mode_pages.rigid_disk_page[ 3648 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3649 sizeof(rigid_disk_page_changeable)); 3650 3651 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 3652 CTL_DEFAULT_HEADS; 3653 3654 /* 3655 * The divide method here will be more accurate, 3656 * probably, but results in floating point being 3657 * used in the kernel on i386 (__udivdi3()). On the 3658 * XScale, though, __udivdi3() is implemented in 3659 * software. 3660 * 3661 * The shift method for cylinder calculation is 3662 * accurate if sectors_per_cylinder is a power of 3663 * 2. Otherwise it might be slightly off -- you 3664 * might have a bit of a truncation problem. 3665 */ 3666#ifdef __XSCALE__ 3667 cylinders = (lun->be_lun->maxlba + 1) / 3668 sectors_per_cylinder; 3669#else 3670 for (shift = 31; shift > 0; shift--) { 3671 if (sectors_per_cylinder & (1 << shift)) 3672 break; 3673 } 3674 cylinders = (lun->be_lun->maxlba + 1) >> shift; 3675#endif 3676 3677 /* 3678 * We've basically got 3 bytes, or 24 bits for the 3679 * cylinder size in the mode page. If we're over, 3680 * just round down to 2^24. 3681 */ 3682 if (cylinders > 0xffffff) 3683 cylinders = 0xffffff; 3684 3685 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 3686 CTL_PAGE_DEFAULT]; 3687 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 3688 3689 if ((value = ctl_get_opt(&lun->be_lun->options, 3690 "rpm")) != NULL) { 3691 scsi_ulto2b(strtol(value, NULL, 0), 3692 rigid_disk_page->rotation_rate); 3693 } 3694 3695 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 3696 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3697 sizeof(rigid_disk_page_default)); 3698 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 3699 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3700 sizeof(rigid_disk_page_default)); 3701 3702 page_index->page_data = 3703 (uint8_t *)lun->mode_pages.rigid_disk_page; 3704 break; 3705 } 3706 case SMS_CACHING_PAGE: { 3707 struct scsi_caching_page *caching_page; 3708 3709 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3710 panic("invalid subpage value %d", 3711 page_index->subpage); 3712 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 3713 &caching_page_default, 3714 sizeof(caching_page_default)); 3715 memcpy(&lun->mode_pages.caching_page[ 3716 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 3717 sizeof(caching_page_changeable)); 3718 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 3719 &caching_page_default, 3720 sizeof(caching_page_default)); 3721 caching_page = &lun->mode_pages.caching_page[ 3722 CTL_PAGE_SAVED]; 3723 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 3724 if (value != NULL && strcmp(value, "off") == 0) 3725 caching_page->flags1 &= ~SCP_WCE; 3726 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 3727 if (value != NULL && strcmp(value, "off") == 0) 3728 caching_page->flags1 |= SCP_RCD; 3729 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 3730 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 3731 sizeof(caching_page_default)); 3732 page_index->page_data = 3733 (uint8_t *)lun->mode_pages.caching_page; 3734 break; 3735 } 3736 case SMS_CONTROL_MODE_PAGE: { 3737 struct scsi_control_page *control_page; 3738 3739 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3740 panic("invalid subpage value %d", 3741 page_index->subpage); 3742 3743 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 3744 &control_page_default, 3745 sizeof(control_page_default)); 3746 memcpy(&lun->mode_pages.control_page[ 3747 CTL_PAGE_CHANGEABLE], &control_page_changeable, 3748 sizeof(control_page_changeable)); 3749 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 3750 &control_page_default, 3751 sizeof(control_page_default)); 3752 control_page = &lun->mode_pages.control_page[ 3753 CTL_PAGE_SAVED]; 3754 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 3755 if (value != NULL && strcmp(value, "unrestricted") == 0) { 3756 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 3757 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 3758 } 3759 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 3760 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 3761 sizeof(control_page_default)); 3762 page_index->page_data = 3763 (uint8_t *)lun->mode_pages.control_page; 3764 break; 3765 3766 } 3767 case SMS_INFO_EXCEPTIONS_PAGE: { 3768 switch (page_index->subpage) { 3769 case SMS_SUBPAGE_PAGE_0: 3770 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 3771 &ie_page_default, 3772 sizeof(ie_page_default)); 3773 memcpy(&lun->mode_pages.ie_page[ 3774 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 3775 sizeof(ie_page_changeable)); 3776 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 3777 &ie_page_default, 3778 sizeof(ie_page_default)); 3779 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 3780 &ie_page_default, 3781 sizeof(ie_page_default)); 3782 page_index->page_data = 3783 (uint8_t *)lun->mode_pages.ie_page; 3784 break; 3785 case 0x02: { 3786 struct ctl_logical_block_provisioning_page *page; 3787 3788 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 3789 &lbp_page_default, 3790 sizeof(lbp_page_default)); 3791 memcpy(&lun->mode_pages.lbp_page[ 3792 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 3793 sizeof(lbp_page_changeable)); 3794 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 3795 &lbp_page_default, 3796 sizeof(lbp_page_default)); 3797 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 3798 value = ctl_get_opt(&lun->be_lun->options, 3799 "avail-threshold"); 3800 if (value != NULL && 3801 ctl_expand_number(value, &ival) == 0) { 3802 page->descr[0].flags |= SLBPPD_ENABLED | 3803 SLBPPD_ARMING_DEC; 3804 if (lun->be_lun->blocksize) 3805 ival /= lun->be_lun->blocksize; 3806 else 3807 ival /= 512; 3808 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3809 page->descr[0].count); 3810 } 3811 value = ctl_get_opt(&lun->be_lun->options, 3812 "used-threshold"); 3813 if (value != NULL && 3814 ctl_expand_number(value, &ival) == 0) { 3815 page->descr[1].flags |= SLBPPD_ENABLED | 3816 SLBPPD_ARMING_INC; 3817 if (lun->be_lun->blocksize) 3818 ival /= lun->be_lun->blocksize; 3819 else 3820 ival /= 512; 3821 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3822 page->descr[1].count); 3823 } 3824 value = ctl_get_opt(&lun->be_lun->options, 3825 "pool-avail-threshold"); 3826 if (value != NULL && 3827 ctl_expand_number(value, &ival) == 0) { 3828 page->descr[2].flags |= SLBPPD_ENABLED | 3829 SLBPPD_ARMING_DEC; 3830 if (lun->be_lun->blocksize) 3831 ival /= lun->be_lun->blocksize; 3832 else 3833 ival /= 512; 3834 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3835 page->descr[2].count); 3836 } 3837 value = ctl_get_opt(&lun->be_lun->options, 3838 "pool-used-threshold"); 3839 if (value != NULL && 3840 ctl_expand_number(value, &ival) == 0) { 3841 page->descr[3].flags |= SLBPPD_ENABLED | 3842 SLBPPD_ARMING_INC; 3843 if (lun->be_lun->blocksize) 3844 ival /= lun->be_lun->blocksize; 3845 else 3846 ival /= 512; 3847 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 3848 page->descr[3].count); 3849 } 3850 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 3851 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 3852 sizeof(lbp_page_default)); 3853 page_index->page_data = 3854 (uint8_t *)lun->mode_pages.lbp_page; 3855 }} 3856 break; 3857 } 3858 case SMS_VENDOR_SPECIFIC_PAGE:{ 3859 switch (page_index->subpage) { 3860 case DBGCNF_SUBPAGE_CODE: { 3861 struct copan_debugconf_subpage *current_page, 3862 *saved_page; 3863 3864 memcpy(&lun->mode_pages.debugconf_subpage[ 3865 CTL_PAGE_CURRENT], 3866 &debugconf_page_default, 3867 sizeof(debugconf_page_default)); 3868 memcpy(&lun->mode_pages.debugconf_subpage[ 3869 CTL_PAGE_CHANGEABLE], 3870 &debugconf_page_changeable, 3871 sizeof(debugconf_page_changeable)); 3872 memcpy(&lun->mode_pages.debugconf_subpage[ 3873 CTL_PAGE_DEFAULT], 3874 &debugconf_page_default, 3875 sizeof(debugconf_page_default)); 3876 memcpy(&lun->mode_pages.debugconf_subpage[ 3877 CTL_PAGE_SAVED], 3878 &debugconf_page_default, 3879 sizeof(debugconf_page_default)); 3880 page_index->page_data = 3881 (uint8_t *)lun->mode_pages.debugconf_subpage; 3882 3883 current_page = (struct copan_debugconf_subpage *) 3884 (page_index->page_data + 3885 (page_index->page_len * 3886 CTL_PAGE_CURRENT)); 3887 saved_page = (struct copan_debugconf_subpage *) 3888 (page_index->page_data + 3889 (page_index->page_len * 3890 CTL_PAGE_SAVED)); 3891 break; 3892 } 3893 default: 3894 panic("invalid subpage value %d", 3895 page_index->subpage); 3896 break; 3897 } 3898 break; 3899 } 3900 default: 3901 panic("invalid page value %d", 3902 page_index->page_code & SMPH_PC_MASK); 3903 break; 3904 } 3905 } 3906 3907 return (CTL_RETVAL_COMPLETE); 3908} 3909 3910static int 3911ctl_init_log_page_index(struct ctl_lun *lun) 3912{ 3913 struct ctl_page_index *page_index; 3914 int i, j, k, prev; 3915 3916 memcpy(&lun->log_pages.index, log_page_index_template, 3917 sizeof(log_page_index_template)); 3918 3919 prev = -1; 3920 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 3921 3922 page_index = &lun->log_pages.index[i]; 3923 /* 3924 * If this is a disk-only mode page, there's no point in 3925 * setting it up. For some pages, we have to have some 3926 * basic information about the disk in order to calculate the 3927 * mode page data. 3928 */ 3929 if ((lun->be_lun->lun_type != T_DIRECT) 3930 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3931 continue; 3932 3933 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 3934 lun->backend->lun_attr == NULL) 3935 continue; 3936 3937 if (page_index->page_code != prev) { 3938 lun->log_pages.pages_page[j] = page_index->page_code; 3939 prev = page_index->page_code; 3940 j++; 3941 } 3942 lun->log_pages.subpages_page[k*2] = page_index->page_code; 3943 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 3944 k++; 3945 } 3946 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 3947 lun->log_pages.index[0].page_len = j; 3948 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 3949 lun->log_pages.index[1].page_len = k * 2; 3950 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 3951 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 3952 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 3953 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 3954 3955 return (CTL_RETVAL_COMPLETE); 3956} 3957 3958static int 3959hex2bin(const char *str, uint8_t *buf, int buf_size) 3960{ 3961 int i; 3962 u_char c; 3963 3964 memset(buf, 0, buf_size); 3965 while (isspace(str[0])) 3966 str++; 3967 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 3968 str += 2; 3969 buf_size *= 2; 3970 for (i = 0; str[i] != 0 && i < buf_size; i++) { 3971 c = str[i]; 3972 if (isdigit(c)) 3973 c -= '0'; 3974 else if (isalpha(c)) 3975 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 3976 else 3977 break; 3978 if (c >= 16) 3979 break; 3980 if ((i & 1) == 0) 3981 buf[i / 2] |= (c << 4); 3982 else 3983 buf[i / 2] |= c; 3984 } 3985 return ((i + 1) / 2); 3986} 3987 3988/* 3989 * LUN allocation. 3990 * 3991 * Requirements: 3992 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 3993 * wants us to allocate the LUN and he can block. 3994 * - ctl_softc is always set 3995 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 3996 * 3997 * Returns 0 for success, non-zero (errno) for failure. 3998 */ 3999static int 4000ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4001 struct ctl_be_lun *const be_lun) 4002{ 4003 struct ctl_lun *nlun, *lun; 4004 struct scsi_vpd_id_descriptor *desc; 4005 struct scsi_vpd_id_t10 *t10id; 4006 const char *eui, *naa, *scsiname, *vendor; 4007 int lun_number, i, lun_malloced; 4008 int devidlen, idlen1, idlen2 = 0, len; 4009 4010 if (be_lun == NULL) 4011 return (EINVAL); 4012 4013 /* 4014 * We currently only support Direct Access or Processor LUN types. 4015 */ 4016 switch (be_lun->lun_type) { 4017 case T_DIRECT: 4018 break; 4019 case T_PROCESSOR: 4020 break; 4021 case T_SEQUENTIAL: 4022 case T_CHANGER: 4023 default: 4024 be_lun->lun_config_status(be_lun->be_lun, 4025 CTL_LUN_CONFIG_FAILURE); 4026 break; 4027 } 4028 if (ctl_lun == NULL) { 4029 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4030 lun_malloced = 1; 4031 } else { 4032 lun_malloced = 0; 4033 lun = ctl_lun; 4034 } 4035 4036 memset(lun, 0, sizeof(*lun)); 4037 if (lun_malloced) 4038 lun->flags = CTL_LUN_MALLOCED; 4039 4040 /* Generate LUN ID. */ 4041 devidlen = max(CTL_DEVID_MIN_LEN, 4042 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4043 idlen1 = sizeof(*t10id) + devidlen; 4044 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4045 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4046 if (scsiname != NULL) { 4047 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4048 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4049 } 4050 eui = ctl_get_opt(&be_lun->options, "eui"); 4051 if (eui != NULL) { 4052 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4053 } 4054 naa = ctl_get_opt(&be_lun->options, "naa"); 4055 if (naa != NULL) { 4056 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4057 } 4058 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4059 M_CTL, M_WAITOK | M_ZERO); 4060 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4061 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4062 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4063 desc->length = idlen1; 4064 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4065 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4066 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4067 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4068 } else { 4069 strncpy(t10id->vendor, vendor, 4070 min(sizeof(t10id->vendor), strlen(vendor))); 4071 } 4072 strncpy((char *)t10id->vendor_spec_id, 4073 (char *)be_lun->device_id, devidlen); 4074 if (scsiname != NULL) { 4075 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4076 desc->length); 4077 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4078 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4079 SVPD_ID_TYPE_SCSI_NAME; 4080 desc->length = idlen2; 4081 strlcpy(desc->identifier, scsiname, idlen2); 4082 } 4083 if (eui != NULL) { 4084 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4085 desc->length); 4086 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4087 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4088 SVPD_ID_TYPE_EUI64; 4089 desc->length = hex2bin(eui, desc->identifier, 16); 4090 desc->length = desc->length > 12 ? 16 : 4091 (desc->length > 8 ? 12 : 8); 4092 len -= 16 - desc->length; 4093 } 4094 if (naa != NULL) { 4095 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4096 desc->length); 4097 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4098 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4099 SVPD_ID_TYPE_NAA; 4100 desc->length = hex2bin(naa, desc->identifier, 16); 4101 desc->length = desc->length > 8 ? 16 : 8; 4102 len -= 16 - desc->length; 4103 } 4104 lun->lun_devid->len = len; 4105 4106 mtx_lock(&ctl_softc->ctl_lock); 4107 /* 4108 * See if the caller requested a particular LUN number. If so, see 4109 * if it is available. Otherwise, allocate the first available LUN. 4110 */ 4111 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4112 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4113 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4114 mtx_unlock(&ctl_softc->ctl_lock); 4115 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4116 printf("ctl: requested LUN ID %d is higher " 4117 "than CTL_MAX_LUNS - 1 (%d)\n", 4118 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4119 } else { 4120 /* 4121 * XXX KDM return an error, or just assign 4122 * another LUN ID in this case?? 4123 */ 4124 printf("ctl: requested LUN ID %d is already " 4125 "in use\n", be_lun->req_lun_id); 4126 } 4127 if (lun->flags & CTL_LUN_MALLOCED) 4128 free(lun, M_CTL); 4129 be_lun->lun_config_status(be_lun->be_lun, 4130 CTL_LUN_CONFIG_FAILURE); 4131 return (ENOSPC); 4132 } 4133 lun_number = be_lun->req_lun_id; 4134 } else { 4135 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4136 if (lun_number == -1) { 4137 mtx_unlock(&ctl_softc->ctl_lock); 4138 printf("ctl: can't allocate LUN, out of LUNs\n"); 4139 if (lun->flags & CTL_LUN_MALLOCED) 4140 free(lun, M_CTL); 4141 be_lun->lun_config_status(be_lun->be_lun, 4142 CTL_LUN_CONFIG_FAILURE); 4143 return (ENOSPC); 4144 } 4145 } 4146 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4147 4148 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4149 lun->lun = lun_number; 4150 lun->be_lun = be_lun; 4151 /* 4152 * The processor LUN is always enabled. Disk LUNs come on line 4153 * disabled, and must be enabled by the backend. 4154 */ 4155 lun->flags |= CTL_LUN_DISABLED; 4156 lun->backend = be_lun->be; 4157 be_lun->ctl_lun = lun; 4158 be_lun->lun_id = lun_number; 4159 atomic_add_int(&be_lun->be->num_luns, 1); 4160 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4161 lun->flags |= CTL_LUN_OFFLINE; 4162 4163 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4164 lun->flags |= CTL_LUN_STOPPED; 4165 4166 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4167 lun->flags |= CTL_LUN_INOPERABLE; 4168 4169 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4170 lun->flags |= CTL_LUN_PRIMARY_SC; 4171 4172 lun->ctl_softc = ctl_softc; 4173#ifdef CTL_TIME_IO 4174 lun->last_busy = getsbinuptime(); 4175#endif 4176 TAILQ_INIT(&lun->ooa_queue); 4177 TAILQ_INIT(&lun->blocked_queue); 4178 STAILQ_INIT(&lun->error_list); 4179 ctl_tpc_lun_init(lun); 4180 4181 /* 4182 * Initialize the mode and log page index. 4183 */ 4184 ctl_init_page_index(lun); 4185 ctl_init_log_page_index(lun); 4186 4187 /* 4188 * Now, before we insert this lun on the lun list, set the lun 4189 * inventory changed UA for all other luns. 4190 */ 4191 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4192 mtx_lock(&nlun->lun_lock); 4193 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4194 mtx_unlock(&nlun->lun_lock); 4195 } 4196 4197 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4198 4199 ctl_softc->ctl_luns[lun_number] = lun; 4200 4201 ctl_softc->num_luns++; 4202 4203 /* Setup statistics gathering */ 4204 lun->stats.device_type = be_lun->lun_type; 4205 lun->stats.lun_number = lun_number; 4206 if (lun->stats.device_type == T_DIRECT) 4207 lun->stats.blocksize = be_lun->blocksize; 4208 else 4209 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4210 for (i = 0;i < CTL_MAX_PORTS;i++) 4211 lun->stats.ports[i].targ_port = i; 4212 4213 mtx_unlock(&ctl_softc->ctl_lock); 4214 4215 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4216 return (0); 4217} 4218 4219/* 4220 * Delete a LUN. 4221 * Assumptions: 4222 * - LUN has already been marked invalid and any pending I/O has been taken 4223 * care of. 4224 */ 4225static int 4226ctl_free_lun(struct ctl_lun *lun) 4227{ 4228 struct ctl_softc *softc; 4229 struct ctl_lun *nlun; 4230 int i; 4231 4232 softc = lun->ctl_softc; 4233 4234 mtx_assert(&softc->ctl_lock, MA_OWNED); 4235 4236 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4237 4238 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4239 4240 softc->ctl_luns[lun->lun] = NULL; 4241 4242 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4243 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4244 4245 softc->num_luns--; 4246 4247 /* 4248 * Tell the backend to free resources, if this LUN has a backend. 4249 */ 4250 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4251 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4252 4253 ctl_tpc_lun_shutdown(lun); 4254 mtx_destroy(&lun->lun_lock); 4255 free(lun->lun_devid, M_CTL); 4256 for (i = 0; i < CTL_MAX_PORTS; i++) 4257 free(lun->pending_ua[i], M_CTL); 4258 for (i = 0; i < 2 * CTL_MAX_PORTS; i++) 4259 free(lun->pr_keys[i], M_CTL); 4260 free(lun->write_buffer, M_CTL); 4261 if (lun->flags & CTL_LUN_MALLOCED) 4262 free(lun, M_CTL); 4263 4264 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4265 mtx_lock(&nlun->lun_lock); 4266 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4267 mtx_unlock(&nlun->lun_lock); 4268 } 4269 4270 return (0); 4271} 4272 4273static void 4274ctl_create_lun(struct ctl_be_lun *be_lun) 4275{ 4276 struct ctl_softc *softc; 4277 4278 softc = control_softc; 4279 4280 /* 4281 * ctl_alloc_lun() should handle all potential failure cases. 4282 */ 4283 ctl_alloc_lun(softc, NULL, be_lun); 4284} 4285 4286int 4287ctl_add_lun(struct ctl_be_lun *be_lun) 4288{ 4289 struct ctl_softc *softc = control_softc; 4290 4291 mtx_lock(&softc->ctl_lock); 4292 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4293 mtx_unlock(&softc->ctl_lock); 4294 wakeup(&softc->pending_lun_queue); 4295 4296 return (0); 4297} 4298 4299int 4300ctl_enable_lun(struct ctl_be_lun *be_lun) 4301{ 4302 struct ctl_softc *softc; 4303 struct ctl_port *port, *nport; 4304 struct ctl_lun *lun; 4305 int retval; 4306 4307 lun = (struct ctl_lun *)be_lun->ctl_lun; 4308 softc = lun->ctl_softc; 4309 4310 mtx_lock(&softc->ctl_lock); 4311 mtx_lock(&lun->lun_lock); 4312 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4313 /* 4314 * eh? Why did we get called if the LUN is already 4315 * enabled? 4316 */ 4317 mtx_unlock(&lun->lun_lock); 4318 mtx_unlock(&softc->ctl_lock); 4319 return (0); 4320 } 4321 lun->flags &= ~CTL_LUN_DISABLED; 4322 mtx_unlock(&lun->lun_lock); 4323 4324 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) { 4325 nport = STAILQ_NEXT(port, links); 4326 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4327 port->lun_map != NULL || port->lun_enable == NULL) 4328 continue; 4329 4330 /* 4331 * Drop the lock while we call the FETD's enable routine. 4332 * This can lead to a callback into CTL (at least in the 4333 * case of the internal initiator frontend. 4334 */ 4335 mtx_unlock(&softc->ctl_lock); 4336 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4337 mtx_lock(&softc->ctl_lock); 4338 if (retval != 0) { 4339 printf("%s: FETD %s port %d returned error " 4340 "%d for lun_enable on lun %jd\n", 4341 __func__, port->port_name, port->targ_port, 4342 retval, (intmax_t)lun->lun); 4343 } 4344 } 4345 4346 mtx_unlock(&softc->ctl_lock); 4347 4348 return (0); 4349} 4350 4351int 4352ctl_disable_lun(struct ctl_be_lun *be_lun) 4353{ 4354 struct ctl_softc *softc; 4355 struct ctl_port *port; 4356 struct ctl_lun *lun; 4357 int retval; 4358 4359 lun = (struct ctl_lun *)be_lun->ctl_lun; 4360 softc = lun->ctl_softc; 4361 4362 mtx_lock(&softc->ctl_lock); 4363 mtx_lock(&lun->lun_lock); 4364 if (lun->flags & CTL_LUN_DISABLED) { 4365 mtx_unlock(&lun->lun_lock); 4366 mtx_unlock(&softc->ctl_lock); 4367 return (0); 4368 } 4369 lun->flags |= CTL_LUN_DISABLED; 4370 mtx_unlock(&lun->lun_lock); 4371 4372 STAILQ_FOREACH(port, &softc->port_list, links) { 4373 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4374 port->lun_map != NULL || port->lun_disable == NULL) 4375 continue; 4376 4377 /* 4378 * Drop the lock before we call the frontend's disable 4379 * routine, to avoid lock order reversals. 4380 * 4381 * XXX KDM what happens if the frontend list changes while 4382 * we're traversing it? It's unlikely, but should be handled. 4383 */ 4384 mtx_unlock(&softc->ctl_lock); 4385 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4386 mtx_lock(&softc->ctl_lock); 4387 if (retval != 0) { 4388 printf("%s: FETD %s port %d returned error " 4389 "%d for lun_disable on lun %jd\n", 4390 __func__, port->port_name, port->targ_port, 4391 retval, (intmax_t)lun->lun); 4392 } 4393 } 4394 4395 mtx_unlock(&softc->ctl_lock); 4396 4397 return (0); 4398} 4399 4400int 4401ctl_start_lun(struct ctl_be_lun *be_lun) 4402{ 4403 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4404 4405 mtx_lock(&lun->lun_lock); 4406 lun->flags &= ~CTL_LUN_STOPPED; 4407 mtx_unlock(&lun->lun_lock); 4408 return (0); 4409} 4410 4411int 4412ctl_stop_lun(struct ctl_be_lun *be_lun) 4413{ 4414 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4415 4416 mtx_lock(&lun->lun_lock); 4417 lun->flags |= CTL_LUN_STOPPED; 4418 mtx_unlock(&lun->lun_lock); 4419 return (0); 4420} 4421 4422int 4423ctl_lun_offline(struct ctl_be_lun *be_lun) 4424{ 4425 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4426 4427 mtx_lock(&lun->lun_lock); 4428 lun->flags |= CTL_LUN_OFFLINE; 4429 mtx_unlock(&lun->lun_lock); 4430 return (0); 4431} 4432 4433int 4434ctl_lun_online(struct ctl_be_lun *be_lun) 4435{ 4436 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4437 4438 mtx_lock(&lun->lun_lock); 4439 lun->flags &= ~CTL_LUN_OFFLINE; 4440 mtx_unlock(&lun->lun_lock); 4441 return (0); 4442} 4443 4444int 4445ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4446{ 4447 struct ctl_softc *softc; 4448 struct ctl_lun *lun; 4449 4450 lun = (struct ctl_lun *)be_lun->ctl_lun; 4451 softc = lun->ctl_softc; 4452 4453 mtx_lock(&lun->lun_lock); 4454 4455 /* 4456 * The LUN needs to be disabled before it can be marked invalid. 4457 */ 4458 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4459 mtx_unlock(&lun->lun_lock); 4460 return (-1); 4461 } 4462 /* 4463 * Mark the LUN invalid. 4464 */ 4465 lun->flags |= CTL_LUN_INVALID; 4466 4467 /* 4468 * If there is nothing in the OOA queue, go ahead and free the LUN. 4469 * If we have something in the OOA queue, we'll free it when the 4470 * last I/O completes. 4471 */ 4472 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4473 mtx_unlock(&lun->lun_lock); 4474 mtx_lock(&softc->ctl_lock); 4475 ctl_free_lun(lun); 4476 mtx_unlock(&softc->ctl_lock); 4477 } else 4478 mtx_unlock(&lun->lun_lock); 4479 4480 return (0); 4481} 4482 4483int 4484ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4485{ 4486 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4487 4488 mtx_lock(&lun->lun_lock); 4489 lun->flags |= CTL_LUN_INOPERABLE; 4490 mtx_unlock(&lun->lun_lock); 4491 return (0); 4492} 4493 4494int 4495ctl_lun_operable(struct ctl_be_lun *be_lun) 4496{ 4497 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4498 4499 mtx_lock(&lun->lun_lock); 4500 lun->flags &= ~CTL_LUN_INOPERABLE; 4501 mtx_unlock(&lun->lun_lock); 4502 return (0); 4503} 4504 4505void 4506ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4507{ 4508 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4509 4510 mtx_lock(&lun->lun_lock); 4511 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED); 4512 mtx_unlock(&lun->lun_lock); 4513} 4514 4515/* 4516 * Backend "memory move is complete" callback for requests that never 4517 * make it down to say RAIDCore's configuration code. 4518 */ 4519int 4520ctl_config_move_done(union ctl_io *io) 4521{ 4522 int retval; 4523 4524 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4525 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4526 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 4527 4528 if ((io->io_hdr.port_status != 0) && 4529 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4530 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4531 /* 4532 * For hardware error sense keys, the sense key 4533 * specific value is defined to be a retry count, 4534 * but we use it to pass back an internal FETD 4535 * error code. XXX KDM Hopefully the FETD is only 4536 * using 16 bits for an error code, since that's 4537 * all the space we have in the sks field. 4538 */ 4539 ctl_set_internal_failure(&io->scsiio, 4540 /*sks_valid*/ 1, 4541 /*retry_count*/ 4542 io->io_hdr.port_status); 4543 } 4544 4545 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 4546 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4547 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 4548 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4549 /* 4550 * XXX KDM just assuming a single pointer here, and not a 4551 * S/G list. If we start using S/G lists for config data, 4552 * we'll need to know how to clean them up here as well. 4553 */ 4554 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4555 free(io->scsiio.kern_data_ptr, M_CTL); 4556 ctl_done(io); 4557 retval = CTL_RETVAL_COMPLETE; 4558 } else { 4559 /* 4560 * XXX KDM now we need to continue data movement. Some 4561 * options: 4562 * - call ctl_scsiio() again? We don't do this for data 4563 * writes, because for those at least we know ahead of 4564 * time where the write will go and how long it is. For 4565 * config writes, though, that information is largely 4566 * contained within the write itself, thus we need to 4567 * parse out the data again. 4568 * 4569 * - Call some other function once the data is in? 4570 */ 4571 if (ctl_debug & CTL_DEBUG_CDB_DATA) 4572 ctl_data_print(io); 4573 4574 /* 4575 * XXX KDM call ctl_scsiio() again for now, and check flag 4576 * bits to see whether we're allocated or not. 4577 */ 4578 retval = ctl_scsiio(&io->scsiio); 4579 } 4580 return (retval); 4581} 4582 4583/* 4584 * This gets called by a backend driver when it is done with a 4585 * data_submit method. 4586 */ 4587void 4588ctl_data_submit_done(union ctl_io *io) 4589{ 4590 /* 4591 * If the IO_CONT flag is set, we need to call the supplied 4592 * function to continue processing the I/O, instead of completing 4593 * the I/O just yet. 4594 * 4595 * If there is an error, though, we don't want to keep processing. 4596 * Instead, just send status back to the initiator. 4597 */ 4598 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4599 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4600 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4601 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4602 io->scsiio.io_cont(io); 4603 return; 4604 } 4605 ctl_done(io); 4606} 4607 4608/* 4609 * This gets called by a backend driver when it is done with a 4610 * configuration write. 4611 */ 4612void 4613ctl_config_write_done(union ctl_io *io) 4614{ 4615 uint8_t *buf; 4616 4617 /* 4618 * If the IO_CONT flag is set, we need to call the supplied 4619 * function to continue processing the I/O, instead of completing 4620 * the I/O just yet. 4621 * 4622 * If there is an error, though, we don't want to keep processing. 4623 * Instead, just send status back to the initiator. 4624 */ 4625 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4626 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4627 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4628 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4629 io->scsiio.io_cont(io); 4630 return; 4631 } 4632 /* 4633 * Since a configuration write can be done for commands that actually 4634 * have data allocated, like write buffer, and commands that have 4635 * no data, like start/stop unit, we need to check here. 4636 */ 4637 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4638 buf = io->scsiio.kern_data_ptr; 4639 else 4640 buf = NULL; 4641 ctl_done(io); 4642 if (buf) 4643 free(buf, M_CTL); 4644} 4645 4646void 4647ctl_config_read_done(union ctl_io *io) 4648{ 4649 uint8_t *buf; 4650 4651 /* 4652 * If there is some error -- we are done, skip data transfer. 4653 */ 4654 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 4655 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4656 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 4657 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4658 buf = io->scsiio.kern_data_ptr; 4659 else 4660 buf = NULL; 4661 ctl_done(io); 4662 if (buf) 4663 free(buf, M_CTL); 4664 return; 4665 } 4666 4667 /* 4668 * If the IO_CONT flag is set, we need to call the supplied 4669 * function to continue processing the I/O, instead of completing 4670 * the I/O just yet. 4671 */ 4672 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 4673 io->scsiio.io_cont(io); 4674 return; 4675 } 4676 4677 ctl_datamove(io); 4678} 4679 4680/* 4681 * SCSI release command. 4682 */ 4683int 4684ctl_scsi_release(struct ctl_scsiio *ctsio) 4685{ 4686 int length, longid, thirdparty_id, resv_id; 4687 struct ctl_lun *lun; 4688 uint32_t residx; 4689 4690 length = 0; 4691 resv_id = 0; 4692 4693 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 4694 4695 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 4696 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 4697 4698 switch (ctsio->cdb[0]) { 4699 case RELEASE_10: { 4700 struct scsi_release_10 *cdb; 4701 4702 cdb = (struct scsi_release_10 *)ctsio->cdb; 4703 4704 if (cdb->byte2 & SR10_LONGID) 4705 longid = 1; 4706 else 4707 thirdparty_id = cdb->thirdparty_id; 4708 4709 resv_id = cdb->resv_id; 4710 length = scsi_2btoul(cdb->length); 4711 break; 4712 } 4713 } 4714 4715 4716 /* 4717 * XXX KDM right now, we only support LUN reservation. We don't 4718 * support 3rd party reservations, or extent reservations, which 4719 * might actually need the parameter list. If we've gotten this 4720 * far, we've got a LUN reservation. Anything else got kicked out 4721 * above. So, according to SPC, ignore the length. 4722 */ 4723 length = 0; 4724 4725 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 4726 && (length > 0)) { 4727 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 4728 ctsio->kern_data_len = length; 4729 ctsio->kern_total_len = length; 4730 ctsio->kern_data_resid = 0; 4731 ctsio->kern_rel_offset = 0; 4732 ctsio->kern_sg_entries = 0; 4733 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 4734 ctsio->be_move_done = ctl_config_move_done; 4735 ctl_datamove((union ctl_io *)ctsio); 4736 4737 return (CTL_RETVAL_COMPLETE); 4738 } 4739 4740 if (length > 0) 4741 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 4742 4743 mtx_lock(&lun->lun_lock); 4744 4745 /* 4746 * According to SPC, it is not an error for an intiator to attempt 4747 * to release a reservation on a LUN that isn't reserved, or that 4748 * is reserved by another initiator. The reservation can only be 4749 * released, though, by the initiator who made it or by one of 4750 * several reset type events. 4751 */ 4752 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 4753 lun->flags &= ~CTL_LUN_RESERVED; 4754 4755 mtx_unlock(&lun->lun_lock); 4756 4757 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 4758 free(ctsio->kern_data_ptr, M_CTL); 4759 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 4760 } 4761 4762 ctl_set_success(ctsio); 4763 ctl_done((union ctl_io *)ctsio); 4764 return (CTL_RETVAL_COMPLETE); 4765} 4766 4767int 4768ctl_scsi_reserve(struct ctl_scsiio *ctsio) 4769{ 4770 int extent, thirdparty, longid; 4771 int resv_id, length; 4772 uint64_t thirdparty_id; 4773 struct ctl_lun *lun; 4774 uint32_t residx; 4775 4776 extent = 0; 4777 thirdparty = 0; 4778 longid = 0; 4779 resv_id = 0; 4780 length = 0; 4781 thirdparty_id = 0; 4782 4783 CTL_DEBUG_PRINT(("ctl_reserve\n")); 4784 4785 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 4786 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 4787 4788 switch (ctsio->cdb[0]) { 4789 case RESERVE_10: { 4790 struct scsi_reserve_10 *cdb; 4791 4792 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 4793 4794 if (cdb->byte2 & SR10_LONGID) 4795 longid = 1; 4796 else 4797 thirdparty_id = cdb->thirdparty_id; 4798 4799 resv_id = cdb->resv_id; 4800 length = scsi_2btoul(cdb->length); 4801 break; 4802 } 4803 } 4804 4805 /* 4806 * XXX KDM right now, we only support LUN reservation. We don't 4807 * support 3rd party reservations, or extent reservations, which 4808 * might actually need the parameter list. If we've gotten this 4809 * far, we've got a LUN reservation. Anything else got kicked out 4810 * above. So, according to SPC, ignore the length. 4811 */ 4812 length = 0; 4813 4814 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 4815 && (length > 0)) { 4816 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 4817 ctsio->kern_data_len = length; 4818 ctsio->kern_total_len = length; 4819 ctsio->kern_data_resid = 0; 4820 ctsio->kern_rel_offset = 0; 4821 ctsio->kern_sg_entries = 0; 4822 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 4823 ctsio->be_move_done = ctl_config_move_done; 4824 ctl_datamove((union ctl_io *)ctsio); 4825 4826 return (CTL_RETVAL_COMPLETE); 4827 } 4828 4829 if (length > 0) 4830 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 4831 4832 mtx_lock(&lun->lun_lock); 4833 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 4834 ctl_set_reservation_conflict(ctsio); 4835 goto bailout; 4836 } 4837 4838 lun->flags |= CTL_LUN_RESERVED; 4839 lun->res_idx = residx; 4840 4841 ctl_set_success(ctsio); 4842 4843bailout: 4844 mtx_unlock(&lun->lun_lock); 4845 4846 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 4847 free(ctsio->kern_data_ptr, M_CTL); 4848 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 4849 } 4850 4851 ctl_done((union ctl_io *)ctsio); 4852 return (CTL_RETVAL_COMPLETE); 4853} 4854 4855int 4856ctl_start_stop(struct ctl_scsiio *ctsio) 4857{ 4858 struct scsi_start_stop_unit *cdb; 4859 struct ctl_lun *lun; 4860 int retval; 4861 4862 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 4863 4864 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 4865 retval = 0; 4866 4867 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 4868 4869 /* 4870 * XXX KDM 4871 * We don't support the immediate bit on a stop unit. In order to 4872 * do that, we would need to code up a way to know that a stop is 4873 * pending, and hold off any new commands until it completes, one 4874 * way or another. Then we could accept or reject those commands 4875 * depending on its status. We would almost need to do the reverse 4876 * of what we do below for an immediate start -- return the copy of 4877 * the ctl_io to the FETD with status to send to the host (and to 4878 * free the copy!) and then free the original I/O once the stop 4879 * actually completes. That way, the OOA queue mechanism can work 4880 * to block commands that shouldn't proceed. Another alternative 4881 * would be to put the copy in the queue in place of the original, 4882 * and return the original back to the caller. That could be 4883 * slightly safer.. 4884 */ 4885 if ((cdb->byte2 & SSS_IMMED) 4886 && ((cdb->how & SSS_START) == 0)) { 4887 ctl_set_invalid_field(ctsio, 4888 /*sks_valid*/ 1, 4889 /*command*/ 1, 4890 /*field*/ 1, 4891 /*bit_valid*/ 1, 4892 /*bit*/ 0); 4893 ctl_done((union ctl_io *)ctsio); 4894 return (CTL_RETVAL_COMPLETE); 4895 } 4896 4897 if ((lun->flags & CTL_LUN_PR_RESERVED) 4898 && ((cdb->how & SSS_START)==0)) { 4899 uint32_t residx; 4900 4901 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 4902 if (ctl_get_prkey(lun, residx) == 0 4903 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 4904 4905 ctl_set_reservation_conflict(ctsio); 4906 ctl_done((union ctl_io *)ctsio); 4907 return (CTL_RETVAL_COMPLETE); 4908 } 4909 } 4910 4911 /* 4912 * If there is no backend on this device, we can't start or stop 4913 * it. In theory we shouldn't get any start/stop commands in the 4914 * first place at this level if the LUN doesn't have a backend. 4915 * That should get stopped by the command decode code. 4916 */ 4917 if (lun->backend == NULL) { 4918 ctl_set_invalid_opcode(ctsio); 4919 ctl_done((union ctl_io *)ctsio); 4920 return (CTL_RETVAL_COMPLETE); 4921 } 4922 4923 /* 4924 * XXX KDM Copan-specific offline behavior. 4925 * Figure out a reasonable way to port this? 4926 */ 4927#ifdef NEEDTOPORT 4928 mtx_lock(&lun->lun_lock); 4929 4930 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 4931 && (lun->flags & CTL_LUN_OFFLINE)) { 4932 /* 4933 * If the LUN is offline, and the on/offline bit isn't set, 4934 * reject the start or stop. Otherwise, let it through. 4935 */ 4936 mtx_unlock(&lun->lun_lock); 4937 ctl_set_lun_not_ready(ctsio); 4938 ctl_done((union ctl_io *)ctsio); 4939 } else { 4940 mtx_unlock(&lun->lun_lock); 4941#endif /* NEEDTOPORT */ 4942 /* 4943 * This could be a start or a stop when we're online, 4944 * or a stop/offline or start/online. A start or stop when 4945 * we're offline is covered in the case above. 4946 */ 4947 /* 4948 * In the non-immediate case, we send the request to 4949 * the backend and return status to the user when 4950 * it is done. 4951 * 4952 * In the immediate case, we allocate a new ctl_io 4953 * to hold a copy of the request, and send that to 4954 * the backend. We then set good status on the 4955 * user's request and return it immediately. 4956 */ 4957 if (cdb->byte2 & SSS_IMMED) { 4958 union ctl_io *new_io; 4959 4960 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 4961 ctl_copy_io((union ctl_io *)ctsio, new_io); 4962 retval = lun->backend->config_write(new_io); 4963 ctl_set_success(ctsio); 4964 ctl_done((union ctl_io *)ctsio); 4965 } else { 4966 retval = lun->backend->config_write( 4967 (union ctl_io *)ctsio); 4968 } 4969#ifdef NEEDTOPORT 4970 } 4971#endif 4972 return (retval); 4973} 4974 4975/* 4976 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 4977 * we don't really do anything with the LBA and length fields if the user 4978 * passes them in. Instead we'll just flush out the cache for the entire 4979 * LUN. 4980 */ 4981int 4982ctl_sync_cache(struct ctl_scsiio *ctsio) 4983{ 4984 struct ctl_lun *lun; 4985 struct ctl_softc *softc; 4986 struct ctl_lba_len_flags *lbalen; 4987 uint64_t starting_lba; 4988 uint32_t block_count; 4989 int retval; 4990 uint8_t byte2; 4991 4992 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 4993 4994 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 4995 softc = lun->ctl_softc; 4996 retval = 0; 4997 4998 switch (ctsio->cdb[0]) { 4999 case SYNCHRONIZE_CACHE: { 5000 struct scsi_sync_cache *cdb; 5001 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5002 5003 starting_lba = scsi_4btoul(cdb->begin_lba); 5004 block_count = scsi_2btoul(cdb->lb_count); 5005 byte2 = cdb->byte2; 5006 break; 5007 } 5008 case SYNCHRONIZE_CACHE_16: { 5009 struct scsi_sync_cache_16 *cdb; 5010 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5011 5012 starting_lba = scsi_8btou64(cdb->begin_lba); 5013 block_count = scsi_4btoul(cdb->lb_count); 5014 byte2 = cdb->byte2; 5015 break; 5016 } 5017 default: 5018 ctl_set_invalid_opcode(ctsio); 5019 ctl_done((union ctl_io *)ctsio); 5020 goto bailout; 5021 break; /* NOTREACHED */ 5022 } 5023 5024 /* 5025 * We check the LBA and length, but don't do anything with them. 5026 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5027 * get flushed. This check will just help satisfy anyone who wants 5028 * to see an error for an out of range LBA. 5029 */ 5030 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5031 ctl_set_lba_out_of_range(ctsio); 5032 ctl_done((union ctl_io *)ctsio); 5033 goto bailout; 5034 } 5035 5036 /* 5037 * If this LUN has no backend, we can't flush the cache anyway. 5038 */ 5039 if (lun->backend == NULL) { 5040 ctl_set_invalid_opcode(ctsio); 5041 ctl_done((union ctl_io *)ctsio); 5042 goto bailout; 5043 } 5044 5045 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5046 lbalen->lba = starting_lba; 5047 lbalen->len = block_count; 5048 lbalen->flags = byte2; 5049 5050 /* 5051 * Check to see whether we're configured to send the SYNCHRONIZE 5052 * CACHE command directly to the back end. 5053 */ 5054 mtx_lock(&lun->lun_lock); 5055 if ((softc->flags & CTL_FLAG_REAL_SYNC) 5056 && (++(lun->sync_count) >= lun->sync_interval)) { 5057 lun->sync_count = 0; 5058 mtx_unlock(&lun->lun_lock); 5059 retval = lun->backend->config_write((union ctl_io *)ctsio); 5060 } else { 5061 mtx_unlock(&lun->lun_lock); 5062 ctl_set_success(ctsio); 5063 ctl_done((union ctl_io *)ctsio); 5064 } 5065 5066bailout: 5067 5068 return (retval); 5069} 5070 5071int 5072ctl_format(struct ctl_scsiio *ctsio) 5073{ 5074 struct scsi_format *cdb; 5075 struct ctl_lun *lun; 5076 int length, defect_list_len; 5077 5078 CTL_DEBUG_PRINT(("ctl_format\n")); 5079 5080 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5081 5082 cdb = (struct scsi_format *)ctsio->cdb; 5083 5084 length = 0; 5085 if (cdb->byte2 & SF_FMTDATA) { 5086 if (cdb->byte2 & SF_LONGLIST) 5087 length = sizeof(struct scsi_format_header_long); 5088 else 5089 length = sizeof(struct scsi_format_header_short); 5090 } 5091 5092 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5093 && (length > 0)) { 5094 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5095 ctsio->kern_data_len = length; 5096 ctsio->kern_total_len = length; 5097 ctsio->kern_data_resid = 0; 5098 ctsio->kern_rel_offset = 0; 5099 ctsio->kern_sg_entries = 0; 5100 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5101 ctsio->be_move_done = ctl_config_move_done; 5102 ctl_datamove((union ctl_io *)ctsio); 5103 5104 return (CTL_RETVAL_COMPLETE); 5105 } 5106 5107 defect_list_len = 0; 5108 5109 if (cdb->byte2 & SF_FMTDATA) { 5110 if (cdb->byte2 & SF_LONGLIST) { 5111 struct scsi_format_header_long *header; 5112 5113 header = (struct scsi_format_header_long *) 5114 ctsio->kern_data_ptr; 5115 5116 defect_list_len = scsi_4btoul(header->defect_list_len); 5117 if (defect_list_len != 0) { 5118 ctl_set_invalid_field(ctsio, 5119 /*sks_valid*/ 1, 5120 /*command*/ 0, 5121 /*field*/ 2, 5122 /*bit_valid*/ 0, 5123 /*bit*/ 0); 5124 goto bailout; 5125 } 5126 } else { 5127 struct scsi_format_header_short *header; 5128 5129 header = (struct scsi_format_header_short *) 5130 ctsio->kern_data_ptr; 5131 5132 defect_list_len = scsi_2btoul(header->defect_list_len); 5133 if (defect_list_len != 0) { 5134 ctl_set_invalid_field(ctsio, 5135 /*sks_valid*/ 1, 5136 /*command*/ 0, 5137 /*field*/ 2, 5138 /*bit_valid*/ 0, 5139 /*bit*/ 0); 5140 goto bailout; 5141 } 5142 } 5143 } 5144 5145 /* 5146 * The format command will clear out the "Medium format corrupted" 5147 * status if set by the configuration code. That status is really 5148 * just a way to notify the host that we have lost the media, and 5149 * get them to issue a command that will basically make them think 5150 * they're blowing away the media. 5151 */ 5152 mtx_lock(&lun->lun_lock); 5153 lun->flags &= ~CTL_LUN_INOPERABLE; 5154 mtx_unlock(&lun->lun_lock); 5155 5156 ctl_set_success(ctsio); 5157bailout: 5158 5159 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5160 free(ctsio->kern_data_ptr, M_CTL); 5161 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5162 } 5163 5164 ctl_done((union ctl_io *)ctsio); 5165 return (CTL_RETVAL_COMPLETE); 5166} 5167 5168int 5169ctl_read_buffer(struct ctl_scsiio *ctsio) 5170{ 5171 struct scsi_read_buffer *cdb; 5172 struct ctl_lun *lun; 5173 int buffer_offset, len; 5174 static uint8_t descr[4]; 5175 static uint8_t echo_descr[4] = { 0 }; 5176 5177 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5178 5179 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5180 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5181 5182 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5183 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5184 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5185 ctl_set_invalid_field(ctsio, 5186 /*sks_valid*/ 1, 5187 /*command*/ 1, 5188 /*field*/ 1, 5189 /*bit_valid*/ 1, 5190 /*bit*/ 4); 5191 ctl_done((union ctl_io *)ctsio); 5192 return (CTL_RETVAL_COMPLETE); 5193 } 5194 5195 len = scsi_3btoul(cdb->length); 5196 buffer_offset = scsi_3btoul(cdb->offset); 5197 5198 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5199 ctl_set_invalid_field(ctsio, 5200 /*sks_valid*/ 1, 5201 /*command*/ 1, 5202 /*field*/ 6, 5203 /*bit_valid*/ 0, 5204 /*bit*/ 0); 5205 ctl_done((union ctl_io *)ctsio); 5206 return (CTL_RETVAL_COMPLETE); 5207 } 5208 5209 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5210 descr[0] = 0; 5211 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5212 ctsio->kern_data_ptr = descr; 5213 len = min(len, sizeof(descr)); 5214 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5215 ctsio->kern_data_ptr = echo_descr; 5216 len = min(len, sizeof(echo_descr)); 5217 } else { 5218 if (lun->write_buffer == NULL) { 5219 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5220 M_CTL, M_WAITOK); 5221 } 5222 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5223 } 5224 ctsio->kern_data_len = len; 5225 ctsio->kern_total_len = len; 5226 ctsio->kern_data_resid = 0; 5227 ctsio->kern_rel_offset = 0; 5228 ctsio->kern_sg_entries = 0; 5229 ctl_set_success(ctsio); 5230 ctsio->be_move_done = ctl_config_move_done; 5231 ctl_datamove((union ctl_io *)ctsio); 5232 return (CTL_RETVAL_COMPLETE); 5233} 5234 5235int 5236ctl_write_buffer(struct ctl_scsiio *ctsio) 5237{ 5238 struct scsi_write_buffer *cdb; 5239 struct ctl_lun *lun; 5240 int buffer_offset, len; 5241 5242 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5243 5244 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5245 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5246 5247 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5248 ctl_set_invalid_field(ctsio, 5249 /*sks_valid*/ 1, 5250 /*command*/ 1, 5251 /*field*/ 1, 5252 /*bit_valid*/ 1, 5253 /*bit*/ 4); 5254 ctl_done((union ctl_io *)ctsio); 5255 return (CTL_RETVAL_COMPLETE); 5256 } 5257 5258 len = scsi_3btoul(cdb->length); 5259 buffer_offset = scsi_3btoul(cdb->offset); 5260 5261 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5262 ctl_set_invalid_field(ctsio, 5263 /*sks_valid*/ 1, 5264 /*command*/ 1, 5265 /*field*/ 6, 5266 /*bit_valid*/ 0, 5267 /*bit*/ 0); 5268 ctl_done((union ctl_io *)ctsio); 5269 return (CTL_RETVAL_COMPLETE); 5270 } 5271 5272 /* 5273 * If we've got a kernel request that hasn't been malloced yet, 5274 * malloc it and tell the caller the data buffer is here. 5275 */ 5276 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5277 if (lun->write_buffer == NULL) { 5278 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5279 M_CTL, M_WAITOK); 5280 } 5281 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5282 ctsio->kern_data_len = len; 5283 ctsio->kern_total_len = len; 5284 ctsio->kern_data_resid = 0; 5285 ctsio->kern_rel_offset = 0; 5286 ctsio->kern_sg_entries = 0; 5287 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5288 ctsio->be_move_done = ctl_config_move_done; 5289 ctl_datamove((union ctl_io *)ctsio); 5290 5291 return (CTL_RETVAL_COMPLETE); 5292 } 5293 5294 ctl_set_success(ctsio); 5295 ctl_done((union ctl_io *)ctsio); 5296 return (CTL_RETVAL_COMPLETE); 5297} 5298 5299int 5300ctl_write_same(struct ctl_scsiio *ctsio) 5301{ 5302 struct ctl_lun *lun; 5303 struct ctl_lba_len_flags *lbalen; 5304 uint64_t lba; 5305 uint32_t num_blocks; 5306 int len, retval; 5307 uint8_t byte2; 5308 5309 retval = CTL_RETVAL_COMPLETE; 5310 5311 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5312 5313 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5314 5315 switch (ctsio->cdb[0]) { 5316 case WRITE_SAME_10: { 5317 struct scsi_write_same_10 *cdb; 5318 5319 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5320 5321 lba = scsi_4btoul(cdb->addr); 5322 num_blocks = scsi_2btoul(cdb->length); 5323 byte2 = cdb->byte2; 5324 break; 5325 } 5326 case WRITE_SAME_16: { 5327 struct scsi_write_same_16 *cdb; 5328 5329 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5330 5331 lba = scsi_8btou64(cdb->addr); 5332 num_blocks = scsi_4btoul(cdb->length); 5333 byte2 = cdb->byte2; 5334 break; 5335 } 5336 default: 5337 /* 5338 * We got a command we don't support. This shouldn't 5339 * happen, commands should be filtered out above us. 5340 */ 5341 ctl_set_invalid_opcode(ctsio); 5342 ctl_done((union ctl_io *)ctsio); 5343 5344 return (CTL_RETVAL_COMPLETE); 5345 break; /* NOTREACHED */ 5346 } 5347 5348 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5349 if ((byte2 & SWS_UNMAP) == 0 && 5350 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5351 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5352 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5353 ctl_done((union ctl_io *)ctsio); 5354 return (CTL_RETVAL_COMPLETE); 5355 } 5356 5357 /* 5358 * The first check is to make sure we're in bounds, the second 5359 * check is to catch wrap-around problems. If the lba + num blocks 5360 * is less than the lba, then we've wrapped around and the block 5361 * range is invalid anyway. 5362 */ 5363 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5364 || ((lba + num_blocks) < lba)) { 5365 ctl_set_lba_out_of_range(ctsio); 5366 ctl_done((union ctl_io *)ctsio); 5367 return (CTL_RETVAL_COMPLETE); 5368 } 5369 5370 /* Zero number of blocks means "to the last logical block" */ 5371 if (num_blocks == 0) { 5372 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5373 ctl_set_invalid_field(ctsio, 5374 /*sks_valid*/ 0, 5375 /*command*/ 1, 5376 /*field*/ 0, 5377 /*bit_valid*/ 0, 5378 /*bit*/ 0); 5379 ctl_done((union ctl_io *)ctsio); 5380 return (CTL_RETVAL_COMPLETE); 5381 } 5382 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5383 } 5384 5385 len = lun->be_lun->blocksize; 5386 5387 /* 5388 * If we've got a kernel request that hasn't been malloced yet, 5389 * malloc it and tell the caller the data buffer is here. 5390 */ 5391 if ((byte2 & SWS_NDOB) == 0 && 5392 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5393 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5394 ctsio->kern_data_len = len; 5395 ctsio->kern_total_len = len; 5396 ctsio->kern_data_resid = 0; 5397 ctsio->kern_rel_offset = 0; 5398 ctsio->kern_sg_entries = 0; 5399 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5400 ctsio->be_move_done = ctl_config_move_done; 5401 ctl_datamove((union ctl_io *)ctsio); 5402 5403 return (CTL_RETVAL_COMPLETE); 5404 } 5405 5406 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5407 lbalen->lba = lba; 5408 lbalen->len = num_blocks; 5409 lbalen->flags = byte2; 5410 retval = lun->backend->config_write((union ctl_io *)ctsio); 5411 5412 return (retval); 5413} 5414 5415int 5416ctl_unmap(struct ctl_scsiio *ctsio) 5417{ 5418 struct ctl_lun *lun; 5419 struct scsi_unmap *cdb; 5420 struct ctl_ptr_len_flags *ptrlen; 5421 struct scsi_unmap_header *hdr; 5422 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5423 uint64_t lba; 5424 uint32_t num_blocks; 5425 int len, retval; 5426 uint8_t byte2; 5427 5428 retval = CTL_RETVAL_COMPLETE; 5429 5430 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5431 5432 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5433 cdb = (struct scsi_unmap *)ctsio->cdb; 5434 5435 len = scsi_2btoul(cdb->length); 5436 byte2 = cdb->byte2; 5437 5438 /* 5439 * If we've got a kernel request that hasn't been malloced yet, 5440 * malloc it and tell the caller the data buffer is here. 5441 */ 5442 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5443 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5444 ctsio->kern_data_len = len; 5445 ctsio->kern_total_len = len; 5446 ctsio->kern_data_resid = 0; 5447 ctsio->kern_rel_offset = 0; 5448 ctsio->kern_sg_entries = 0; 5449 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5450 ctsio->be_move_done = ctl_config_move_done; 5451 ctl_datamove((union ctl_io *)ctsio); 5452 5453 return (CTL_RETVAL_COMPLETE); 5454 } 5455 5456 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5457 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5458 if (len < sizeof (*hdr) || 5459 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5460 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5461 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5462 ctl_set_invalid_field(ctsio, 5463 /*sks_valid*/ 0, 5464 /*command*/ 0, 5465 /*field*/ 0, 5466 /*bit_valid*/ 0, 5467 /*bit*/ 0); 5468 goto done; 5469 } 5470 len = scsi_2btoul(hdr->desc_length); 5471 buf = (struct scsi_unmap_desc *)(hdr + 1); 5472 end = buf + len / sizeof(*buf); 5473 5474 endnz = buf; 5475 for (range = buf; range < end; range++) { 5476 lba = scsi_8btou64(range->lba); 5477 num_blocks = scsi_4btoul(range->length); 5478 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5479 || ((lba + num_blocks) < lba)) { 5480 ctl_set_lba_out_of_range(ctsio); 5481 ctl_done((union ctl_io *)ctsio); 5482 return (CTL_RETVAL_COMPLETE); 5483 } 5484 if (num_blocks != 0) 5485 endnz = range + 1; 5486 } 5487 5488 /* 5489 * Block backend can not handle zero last range. 5490 * Filter it out and return if there is nothing left. 5491 */ 5492 len = (uint8_t *)endnz - (uint8_t *)buf; 5493 if (len == 0) { 5494 ctl_set_success(ctsio); 5495 goto done; 5496 } 5497 5498 mtx_lock(&lun->lun_lock); 5499 ptrlen = (struct ctl_ptr_len_flags *) 5500 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5501 ptrlen->ptr = (void *)buf; 5502 ptrlen->len = len; 5503 ptrlen->flags = byte2; 5504 ctl_check_blocked(lun); 5505 mtx_unlock(&lun->lun_lock); 5506 5507 retval = lun->backend->config_write((union ctl_io *)ctsio); 5508 return (retval); 5509 5510done: 5511 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5512 free(ctsio->kern_data_ptr, M_CTL); 5513 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5514 } 5515 ctl_done((union ctl_io *)ctsio); 5516 return (CTL_RETVAL_COMPLETE); 5517} 5518 5519/* 5520 * Note that this function currently doesn't actually do anything inside 5521 * CTL to enforce things if the DQue bit is turned on. 5522 * 5523 * Also note that this function can't be used in the default case, because 5524 * the DQue bit isn't set in the changeable mask for the control mode page 5525 * anyway. This is just here as an example for how to implement a page 5526 * handler, and a placeholder in case we want to allow the user to turn 5527 * tagged queueing on and off. 5528 * 5529 * The D_SENSE bit handling is functional, however, and will turn 5530 * descriptor sense on and off for a given LUN. 5531 */ 5532int 5533ctl_control_page_handler(struct ctl_scsiio *ctsio, 5534 struct ctl_page_index *page_index, uint8_t *page_ptr) 5535{ 5536 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5537 struct ctl_lun *lun; 5538 int set_ua; 5539 uint32_t initidx; 5540 5541 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5542 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5543 set_ua = 0; 5544 5545 user_cp = (struct scsi_control_page *)page_ptr; 5546 current_cp = (struct scsi_control_page *) 5547 (page_index->page_data + (page_index->page_len * 5548 CTL_PAGE_CURRENT)); 5549 saved_cp = (struct scsi_control_page *) 5550 (page_index->page_data + (page_index->page_len * 5551 CTL_PAGE_SAVED)); 5552 5553 mtx_lock(&lun->lun_lock); 5554 if (((current_cp->rlec & SCP_DSENSE) == 0) 5555 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5556 /* 5557 * Descriptor sense is currently turned off and the user 5558 * wants to turn it on. 5559 */ 5560 current_cp->rlec |= SCP_DSENSE; 5561 saved_cp->rlec |= SCP_DSENSE; 5562 lun->flags |= CTL_LUN_SENSE_DESC; 5563 set_ua = 1; 5564 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 5565 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 5566 /* 5567 * Descriptor sense is currently turned on, and the user 5568 * wants to turn it off. 5569 */ 5570 current_cp->rlec &= ~SCP_DSENSE; 5571 saved_cp->rlec &= ~SCP_DSENSE; 5572 lun->flags &= ~CTL_LUN_SENSE_DESC; 5573 set_ua = 1; 5574 } 5575 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 5576 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 5577 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5578 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5579 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5580 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5581 set_ua = 1; 5582 } 5583 if ((current_cp->eca_and_aen & SCP_SWP) != 5584 (user_cp->eca_and_aen & SCP_SWP)) { 5585 current_cp->eca_and_aen &= ~SCP_SWP; 5586 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5587 saved_cp->eca_and_aen &= ~SCP_SWP; 5588 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5589 set_ua = 1; 5590 } 5591 if (set_ua != 0) 5592 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5593 mtx_unlock(&lun->lun_lock); 5594 5595 return (0); 5596} 5597 5598int 5599ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 5600 struct ctl_page_index *page_index, uint8_t *page_ptr) 5601{ 5602 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 5603 struct ctl_lun *lun; 5604 int set_ua; 5605 uint32_t initidx; 5606 5607 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5608 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5609 set_ua = 0; 5610 5611 user_cp = (struct scsi_caching_page *)page_ptr; 5612 current_cp = (struct scsi_caching_page *) 5613 (page_index->page_data + (page_index->page_len * 5614 CTL_PAGE_CURRENT)); 5615 saved_cp = (struct scsi_caching_page *) 5616 (page_index->page_data + (page_index->page_len * 5617 CTL_PAGE_SAVED)); 5618 5619 mtx_lock(&lun->lun_lock); 5620 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 5621 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 5622 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5623 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5624 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5625 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5626 set_ua = 1; 5627 } 5628 if (set_ua != 0) 5629 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5630 mtx_unlock(&lun->lun_lock); 5631 5632 return (0); 5633} 5634 5635int 5636ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 5637 struct ctl_page_index *page_index, 5638 uint8_t *page_ptr) 5639{ 5640 uint8_t *c; 5641 int i; 5642 5643 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 5644 ctl_time_io_secs = 5645 (c[0] << 8) | 5646 (c[1] << 0) | 5647 0; 5648 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 5649 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 5650 printf("page data:"); 5651 for (i=0; i<8; i++) 5652 printf(" %.2x",page_ptr[i]); 5653 printf("\n"); 5654 return (0); 5655} 5656 5657int 5658ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 5659 struct ctl_page_index *page_index, 5660 int pc) 5661{ 5662 struct copan_debugconf_subpage *page; 5663 5664 page = (struct copan_debugconf_subpage *)page_index->page_data + 5665 (page_index->page_len * pc); 5666 5667 switch (pc) { 5668 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 5669 case SMS_PAGE_CTRL_DEFAULT >> 6: 5670 case SMS_PAGE_CTRL_SAVED >> 6: 5671 /* 5672 * We don't update the changable or default bits for this page. 5673 */ 5674 break; 5675 case SMS_PAGE_CTRL_CURRENT >> 6: 5676 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 5677 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 5678 break; 5679 default: 5680#ifdef NEEDTOPORT 5681 EPRINT(0, "Invalid PC %d!!", pc); 5682#endif /* NEEDTOPORT */ 5683 break; 5684 } 5685 return (0); 5686} 5687 5688 5689static int 5690ctl_do_mode_select(union ctl_io *io) 5691{ 5692 struct scsi_mode_page_header *page_header; 5693 struct ctl_page_index *page_index; 5694 struct ctl_scsiio *ctsio; 5695 int control_dev, page_len; 5696 int page_len_offset, page_len_size; 5697 union ctl_modepage_info *modepage_info; 5698 struct ctl_lun *lun; 5699 int *len_left, *len_used; 5700 int retval, i; 5701 5702 ctsio = &io->scsiio; 5703 page_index = NULL; 5704 page_len = 0; 5705 retval = CTL_RETVAL_COMPLETE; 5706 5707 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5708 5709 if (lun->be_lun->lun_type != T_DIRECT) 5710 control_dev = 1; 5711 else 5712 control_dev = 0; 5713 5714 modepage_info = (union ctl_modepage_info *) 5715 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5716 len_left = &modepage_info->header.len_left; 5717 len_used = &modepage_info->header.len_used; 5718 5719do_next_page: 5720 5721 page_header = (struct scsi_mode_page_header *) 5722 (ctsio->kern_data_ptr + *len_used); 5723 5724 if (*len_left == 0) { 5725 free(ctsio->kern_data_ptr, M_CTL); 5726 ctl_set_success(ctsio); 5727 ctl_done((union ctl_io *)ctsio); 5728 return (CTL_RETVAL_COMPLETE); 5729 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5730 5731 free(ctsio->kern_data_ptr, M_CTL); 5732 ctl_set_param_len_error(ctsio); 5733 ctl_done((union ctl_io *)ctsio); 5734 return (CTL_RETVAL_COMPLETE); 5735 5736 } else if ((page_header->page_code & SMPH_SPF) 5737 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5738 5739 free(ctsio->kern_data_ptr, M_CTL); 5740 ctl_set_param_len_error(ctsio); 5741 ctl_done((union ctl_io *)ctsio); 5742 return (CTL_RETVAL_COMPLETE); 5743 } 5744 5745 5746 /* 5747 * XXX KDM should we do something with the block descriptor? 5748 */ 5749 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5750 5751 if ((control_dev != 0) 5752 && (lun->mode_pages.index[i].page_flags & 5753 CTL_PAGE_FLAG_DISK_ONLY)) 5754 continue; 5755 5756 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5757 (page_header->page_code & SMPH_PC_MASK)) 5758 continue; 5759 5760 /* 5761 * If neither page has a subpage code, then we've got a 5762 * match. 5763 */ 5764 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 5765 && ((page_header->page_code & SMPH_SPF) == 0)) { 5766 page_index = &lun->mode_pages.index[i]; 5767 page_len = page_header->page_length; 5768 break; 5769 } 5770 5771 /* 5772 * If both pages have subpages, then the subpage numbers 5773 * have to match. 5774 */ 5775 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 5776 && (page_header->page_code & SMPH_SPF)) { 5777 struct scsi_mode_page_header_sp *sph; 5778 5779 sph = (struct scsi_mode_page_header_sp *)page_header; 5780 5781 if (lun->mode_pages.index[i].subpage == 5782 sph->subpage) { 5783 page_index = &lun->mode_pages.index[i]; 5784 page_len = scsi_2btoul(sph->page_length); 5785 break; 5786 } 5787 } 5788 } 5789 5790 /* 5791 * If we couldn't find the page, or if we don't have a mode select 5792 * handler for it, send back an error to the user. 5793 */ 5794 if ((page_index == NULL) 5795 || (page_index->select_handler == NULL)) { 5796 ctl_set_invalid_field(ctsio, 5797 /*sks_valid*/ 1, 5798 /*command*/ 0, 5799 /*field*/ *len_used, 5800 /*bit_valid*/ 0, 5801 /*bit*/ 0); 5802 free(ctsio->kern_data_ptr, M_CTL); 5803 ctl_done((union ctl_io *)ctsio); 5804 return (CTL_RETVAL_COMPLETE); 5805 } 5806 5807 if (page_index->page_code & SMPH_SPF) { 5808 page_len_offset = 2; 5809 page_len_size = 2; 5810 } else { 5811 page_len_size = 1; 5812 page_len_offset = 1; 5813 } 5814 5815 /* 5816 * If the length the initiator gives us isn't the one we specify in 5817 * the mode page header, or if they didn't specify enough data in 5818 * the CDB to avoid truncating this page, kick out the request. 5819 */ 5820 if ((page_len != (page_index->page_len - page_len_offset - 5821 page_len_size)) 5822 || (*len_left < page_index->page_len)) { 5823 5824 5825 ctl_set_invalid_field(ctsio, 5826 /*sks_valid*/ 1, 5827 /*command*/ 0, 5828 /*field*/ *len_used + page_len_offset, 5829 /*bit_valid*/ 0, 5830 /*bit*/ 0); 5831 free(ctsio->kern_data_ptr, M_CTL); 5832 ctl_done((union ctl_io *)ctsio); 5833 return (CTL_RETVAL_COMPLETE); 5834 } 5835 5836 /* 5837 * Run through the mode page, checking to make sure that the bits 5838 * the user changed are actually legal for him to change. 5839 */ 5840 for (i = 0; i < page_index->page_len; i++) { 5841 uint8_t *user_byte, *change_mask, *current_byte; 5842 int bad_bit; 5843 int j; 5844 5845 user_byte = (uint8_t *)page_header + i; 5846 change_mask = page_index->page_data + 5847 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 5848 current_byte = page_index->page_data + 5849 (page_index->page_len * CTL_PAGE_CURRENT) + i; 5850 5851 /* 5852 * Check to see whether the user set any bits in this byte 5853 * that he is not allowed to set. 5854 */ 5855 if ((*user_byte & ~(*change_mask)) == 5856 (*current_byte & ~(*change_mask))) 5857 continue; 5858 5859 /* 5860 * Go through bit by bit to determine which one is illegal. 5861 */ 5862 bad_bit = 0; 5863 for (j = 7; j >= 0; j--) { 5864 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 5865 (((1 << i) & ~(*change_mask)) & *current_byte)) { 5866 bad_bit = i; 5867 break; 5868 } 5869 } 5870 ctl_set_invalid_field(ctsio, 5871 /*sks_valid*/ 1, 5872 /*command*/ 0, 5873 /*field*/ *len_used + i, 5874 /*bit_valid*/ 1, 5875 /*bit*/ bad_bit); 5876 free(ctsio->kern_data_ptr, M_CTL); 5877 ctl_done((union ctl_io *)ctsio); 5878 return (CTL_RETVAL_COMPLETE); 5879 } 5880 5881 /* 5882 * Decrement these before we call the page handler, since we may 5883 * end up getting called back one way or another before the handler 5884 * returns to this context. 5885 */ 5886 *len_left -= page_index->page_len; 5887 *len_used += page_index->page_len; 5888 5889 retval = page_index->select_handler(ctsio, page_index, 5890 (uint8_t *)page_header); 5891 5892 /* 5893 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 5894 * wait until this queued command completes to finish processing 5895 * the mode page. If it returns anything other than 5896 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 5897 * already set the sense information, freed the data pointer, and 5898 * completed the io for us. 5899 */ 5900 if (retval != CTL_RETVAL_COMPLETE) 5901 goto bailout_no_done; 5902 5903 /* 5904 * If the initiator sent us more than one page, parse the next one. 5905 */ 5906 if (*len_left > 0) 5907 goto do_next_page; 5908 5909 ctl_set_success(ctsio); 5910 free(ctsio->kern_data_ptr, M_CTL); 5911 ctl_done((union ctl_io *)ctsio); 5912 5913bailout_no_done: 5914 5915 return (CTL_RETVAL_COMPLETE); 5916 5917} 5918 5919int 5920ctl_mode_select(struct ctl_scsiio *ctsio) 5921{ 5922 int param_len, pf, sp; 5923 int header_size, bd_len; 5924 int len_left, len_used; 5925 struct ctl_page_index *page_index; 5926 struct ctl_lun *lun; 5927 int control_dev, page_len; 5928 union ctl_modepage_info *modepage_info; 5929 int retval; 5930 5931 pf = 0; 5932 sp = 0; 5933 page_len = 0; 5934 len_used = 0; 5935 len_left = 0; 5936 retval = 0; 5937 bd_len = 0; 5938 page_index = NULL; 5939 5940 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5941 5942 if (lun->be_lun->lun_type != T_DIRECT) 5943 control_dev = 1; 5944 else 5945 control_dev = 0; 5946 5947 switch (ctsio->cdb[0]) { 5948 case MODE_SELECT_6: { 5949 struct scsi_mode_select_6 *cdb; 5950 5951 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 5952 5953 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 5954 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 5955 5956 param_len = cdb->length; 5957 header_size = sizeof(struct scsi_mode_header_6); 5958 break; 5959 } 5960 case MODE_SELECT_10: { 5961 struct scsi_mode_select_10 *cdb; 5962 5963 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 5964 5965 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 5966 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 5967 5968 param_len = scsi_2btoul(cdb->length); 5969 header_size = sizeof(struct scsi_mode_header_10); 5970 break; 5971 } 5972 default: 5973 ctl_set_invalid_opcode(ctsio); 5974 ctl_done((union ctl_io *)ctsio); 5975 return (CTL_RETVAL_COMPLETE); 5976 break; /* NOTREACHED */ 5977 } 5978 5979 /* 5980 * From SPC-3: 5981 * "A parameter list length of zero indicates that the Data-Out Buffer 5982 * shall be empty. This condition shall not be considered as an error." 5983 */ 5984 if (param_len == 0) { 5985 ctl_set_success(ctsio); 5986 ctl_done((union ctl_io *)ctsio); 5987 return (CTL_RETVAL_COMPLETE); 5988 } 5989 5990 /* 5991 * Since we'll hit this the first time through, prior to 5992 * allocation, we don't need to free a data buffer here. 5993 */ 5994 if (param_len < header_size) { 5995 ctl_set_param_len_error(ctsio); 5996 ctl_done((union ctl_io *)ctsio); 5997 return (CTL_RETVAL_COMPLETE); 5998 } 5999 6000 /* 6001 * Allocate the data buffer and grab the user's data. In theory, 6002 * we shouldn't have to sanity check the parameter list length here 6003 * because the maximum size is 64K. We should be able to malloc 6004 * that much without too many problems. 6005 */ 6006 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6007 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6008 ctsio->kern_data_len = param_len; 6009 ctsio->kern_total_len = param_len; 6010 ctsio->kern_data_resid = 0; 6011 ctsio->kern_rel_offset = 0; 6012 ctsio->kern_sg_entries = 0; 6013 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6014 ctsio->be_move_done = ctl_config_move_done; 6015 ctl_datamove((union ctl_io *)ctsio); 6016 6017 return (CTL_RETVAL_COMPLETE); 6018 } 6019 6020 switch (ctsio->cdb[0]) { 6021 case MODE_SELECT_6: { 6022 struct scsi_mode_header_6 *mh6; 6023 6024 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6025 bd_len = mh6->blk_desc_len; 6026 break; 6027 } 6028 case MODE_SELECT_10: { 6029 struct scsi_mode_header_10 *mh10; 6030 6031 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6032 bd_len = scsi_2btoul(mh10->blk_desc_len); 6033 break; 6034 } 6035 default: 6036 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6037 break; 6038 } 6039 6040 if (param_len < (header_size + bd_len)) { 6041 free(ctsio->kern_data_ptr, M_CTL); 6042 ctl_set_param_len_error(ctsio); 6043 ctl_done((union ctl_io *)ctsio); 6044 return (CTL_RETVAL_COMPLETE); 6045 } 6046 6047 /* 6048 * Set the IO_CONT flag, so that if this I/O gets passed to 6049 * ctl_config_write_done(), it'll get passed back to 6050 * ctl_do_mode_select() for further processing, or completion if 6051 * we're all done. 6052 */ 6053 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6054 ctsio->io_cont = ctl_do_mode_select; 6055 6056 modepage_info = (union ctl_modepage_info *) 6057 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6058 6059 memset(modepage_info, 0, sizeof(*modepage_info)); 6060 6061 len_left = param_len - header_size - bd_len; 6062 len_used = header_size + bd_len; 6063 6064 modepage_info->header.len_left = len_left; 6065 modepage_info->header.len_used = len_used; 6066 6067 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6068} 6069 6070int 6071ctl_mode_sense(struct ctl_scsiio *ctsio) 6072{ 6073 struct ctl_lun *lun; 6074 int pc, page_code, dbd, llba, subpage; 6075 int alloc_len, page_len, header_len, total_len; 6076 struct scsi_mode_block_descr *block_desc; 6077 struct ctl_page_index *page_index; 6078 int control_dev; 6079 6080 dbd = 0; 6081 llba = 0; 6082 block_desc = NULL; 6083 page_index = NULL; 6084 6085 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6086 6087 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6088 6089 if (lun->be_lun->lun_type != T_DIRECT) 6090 control_dev = 1; 6091 else 6092 control_dev = 0; 6093 6094 switch (ctsio->cdb[0]) { 6095 case MODE_SENSE_6: { 6096 struct scsi_mode_sense_6 *cdb; 6097 6098 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6099 6100 header_len = sizeof(struct scsi_mode_hdr_6); 6101 if (cdb->byte2 & SMS_DBD) 6102 dbd = 1; 6103 else 6104 header_len += sizeof(struct scsi_mode_block_descr); 6105 6106 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6107 page_code = cdb->page & SMS_PAGE_CODE; 6108 subpage = cdb->subpage; 6109 alloc_len = cdb->length; 6110 break; 6111 } 6112 case MODE_SENSE_10: { 6113 struct scsi_mode_sense_10 *cdb; 6114 6115 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6116 6117 header_len = sizeof(struct scsi_mode_hdr_10); 6118 6119 if (cdb->byte2 & SMS_DBD) 6120 dbd = 1; 6121 else 6122 header_len += sizeof(struct scsi_mode_block_descr); 6123 if (cdb->byte2 & SMS10_LLBAA) 6124 llba = 1; 6125 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6126 page_code = cdb->page & SMS_PAGE_CODE; 6127 subpage = cdb->subpage; 6128 alloc_len = scsi_2btoul(cdb->length); 6129 break; 6130 } 6131 default: 6132 ctl_set_invalid_opcode(ctsio); 6133 ctl_done((union ctl_io *)ctsio); 6134 return (CTL_RETVAL_COMPLETE); 6135 break; /* NOTREACHED */ 6136 } 6137 6138 /* 6139 * We have to make a first pass through to calculate the size of 6140 * the pages that match the user's query. Then we allocate enough 6141 * memory to hold it, and actually copy the data into the buffer. 6142 */ 6143 switch (page_code) { 6144 case SMS_ALL_PAGES_PAGE: { 6145 int i; 6146 6147 page_len = 0; 6148 6149 /* 6150 * At the moment, values other than 0 and 0xff here are 6151 * reserved according to SPC-3. 6152 */ 6153 if ((subpage != SMS_SUBPAGE_PAGE_0) 6154 && (subpage != SMS_SUBPAGE_ALL)) { 6155 ctl_set_invalid_field(ctsio, 6156 /*sks_valid*/ 1, 6157 /*command*/ 1, 6158 /*field*/ 3, 6159 /*bit_valid*/ 0, 6160 /*bit*/ 0); 6161 ctl_done((union ctl_io *)ctsio); 6162 return (CTL_RETVAL_COMPLETE); 6163 } 6164 6165 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6166 if ((control_dev != 0) 6167 && (lun->mode_pages.index[i].page_flags & 6168 CTL_PAGE_FLAG_DISK_ONLY)) 6169 continue; 6170 6171 /* 6172 * We don't use this subpage if the user didn't 6173 * request all subpages. 6174 */ 6175 if ((lun->mode_pages.index[i].subpage != 0) 6176 && (subpage == SMS_SUBPAGE_PAGE_0)) 6177 continue; 6178 6179#if 0 6180 printf("found page %#x len %d\n", 6181 lun->mode_pages.index[i].page_code & 6182 SMPH_PC_MASK, 6183 lun->mode_pages.index[i].page_len); 6184#endif 6185 page_len += lun->mode_pages.index[i].page_len; 6186 } 6187 break; 6188 } 6189 default: { 6190 int i; 6191 6192 page_len = 0; 6193 6194 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6195 /* Look for the right page code */ 6196 if ((lun->mode_pages.index[i].page_code & 6197 SMPH_PC_MASK) != page_code) 6198 continue; 6199 6200 /* Look for the right subpage or the subpage wildcard*/ 6201 if ((lun->mode_pages.index[i].subpage != subpage) 6202 && (subpage != SMS_SUBPAGE_ALL)) 6203 continue; 6204 6205 /* Make sure the page is supported for this dev type */ 6206 if ((control_dev != 0) 6207 && (lun->mode_pages.index[i].page_flags & 6208 CTL_PAGE_FLAG_DISK_ONLY)) 6209 continue; 6210 6211#if 0 6212 printf("found page %#x len %d\n", 6213 lun->mode_pages.index[i].page_code & 6214 SMPH_PC_MASK, 6215 lun->mode_pages.index[i].page_len); 6216#endif 6217 6218 page_len += lun->mode_pages.index[i].page_len; 6219 } 6220 6221 if (page_len == 0) { 6222 ctl_set_invalid_field(ctsio, 6223 /*sks_valid*/ 1, 6224 /*command*/ 1, 6225 /*field*/ 2, 6226 /*bit_valid*/ 1, 6227 /*bit*/ 5); 6228 ctl_done((union ctl_io *)ctsio); 6229 return (CTL_RETVAL_COMPLETE); 6230 } 6231 break; 6232 } 6233 } 6234 6235 total_len = header_len + page_len; 6236#if 0 6237 printf("header_len = %d, page_len = %d, total_len = %d\n", 6238 header_len, page_len, total_len); 6239#endif 6240 6241 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6242 ctsio->kern_sg_entries = 0; 6243 ctsio->kern_data_resid = 0; 6244 ctsio->kern_rel_offset = 0; 6245 if (total_len < alloc_len) { 6246 ctsio->residual = alloc_len - total_len; 6247 ctsio->kern_data_len = total_len; 6248 ctsio->kern_total_len = total_len; 6249 } else { 6250 ctsio->residual = 0; 6251 ctsio->kern_data_len = alloc_len; 6252 ctsio->kern_total_len = alloc_len; 6253 } 6254 6255 switch (ctsio->cdb[0]) { 6256 case MODE_SENSE_6: { 6257 struct scsi_mode_hdr_6 *header; 6258 6259 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6260 6261 header->datalen = MIN(total_len - 1, 254); 6262 if (control_dev == 0) { 6263 header->dev_specific = 0x10; /* DPOFUA */ 6264 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6265 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6266 .eca_and_aen & SCP_SWP) != 0) 6267 header->dev_specific |= 0x80; /* WP */ 6268 } 6269 if (dbd) 6270 header->block_descr_len = 0; 6271 else 6272 header->block_descr_len = 6273 sizeof(struct scsi_mode_block_descr); 6274 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6275 break; 6276 } 6277 case MODE_SENSE_10: { 6278 struct scsi_mode_hdr_10 *header; 6279 int datalen; 6280 6281 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6282 6283 datalen = MIN(total_len - 2, 65533); 6284 scsi_ulto2b(datalen, header->datalen); 6285 if (control_dev == 0) { 6286 header->dev_specific = 0x10; /* DPOFUA */ 6287 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6288 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6289 .eca_and_aen & SCP_SWP) != 0) 6290 header->dev_specific |= 0x80; /* WP */ 6291 } 6292 if (dbd) 6293 scsi_ulto2b(0, header->block_descr_len); 6294 else 6295 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6296 header->block_descr_len); 6297 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6298 break; 6299 } 6300 default: 6301 panic("invalid CDB type %#x", ctsio->cdb[0]); 6302 break; /* NOTREACHED */ 6303 } 6304 6305 /* 6306 * If we've got a disk, use its blocksize in the block 6307 * descriptor. Otherwise, just set it to 0. 6308 */ 6309 if (dbd == 0) { 6310 if (control_dev == 0) 6311 scsi_ulto3b(lun->be_lun->blocksize, 6312 block_desc->block_len); 6313 else 6314 scsi_ulto3b(0, block_desc->block_len); 6315 } 6316 6317 switch (page_code) { 6318 case SMS_ALL_PAGES_PAGE: { 6319 int i, data_used; 6320 6321 data_used = header_len; 6322 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6323 struct ctl_page_index *page_index; 6324 6325 page_index = &lun->mode_pages.index[i]; 6326 6327 if ((control_dev != 0) 6328 && (page_index->page_flags & 6329 CTL_PAGE_FLAG_DISK_ONLY)) 6330 continue; 6331 6332 /* 6333 * We don't use this subpage if the user didn't 6334 * request all subpages. We already checked (above) 6335 * to make sure the user only specified a subpage 6336 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6337 */ 6338 if ((page_index->subpage != 0) 6339 && (subpage == SMS_SUBPAGE_PAGE_0)) 6340 continue; 6341 6342 /* 6343 * Call the handler, if it exists, to update the 6344 * page to the latest values. 6345 */ 6346 if (page_index->sense_handler != NULL) 6347 page_index->sense_handler(ctsio, page_index,pc); 6348 6349 memcpy(ctsio->kern_data_ptr + data_used, 6350 page_index->page_data + 6351 (page_index->page_len * pc), 6352 page_index->page_len); 6353 data_used += page_index->page_len; 6354 } 6355 break; 6356 } 6357 default: { 6358 int i, data_used; 6359 6360 data_used = header_len; 6361 6362 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6363 struct ctl_page_index *page_index; 6364 6365 page_index = &lun->mode_pages.index[i]; 6366 6367 /* Look for the right page code */ 6368 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6369 continue; 6370 6371 /* Look for the right subpage or the subpage wildcard*/ 6372 if ((page_index->subpage != subpage) 6373 && (subpage != SMS_SUBPAGE_ALL)) 6374 continue; 6375 6376 /* Make sure the page is supported for this dev type */ 6377 if ((control_dev != 0) 6378 && (page_index->page_flags & 6379 CTL_PAGE_FLAG_DISK_ONLY)) 6380 continue; 6381 6382 /* 6383 * Call the handler, if it exists, to update the 6384 * page to the latest values. 6385 */ 6386 if (page_index->sense_handler != NULL) 6387 page_index->sense_handler(ctsio, page_index,pc); 6388 6389 memcpy(ctsio->kern_data_ptr + data_used, 6390 page_index->page_data + 6391 (page_index->page_len * pc), 6392 page_index->page_len); 6393 data_used += page_index->page_len; 6394 } 6395 break; 6396 } 6397 } 6398 6399 ctl_set_success(ctsio); 6400 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6401 ctsio->be_move_done = ctl_config_move_done; 6402 ctl_datamove((union ctl_io *)ctsio); 6403 return (CTL_RETVAL_COMPLETE); 6404} 6405 6406int 6407ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6408 struct ctl_page_index *page_index, 6409 int pc) 6410{ 6411 struct ctl_lun *lun; 6412 struct scsi_log_param_header *phdr; 6413 uint8_t *data; 6414 uint64_t val; 6415 6416 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6417 data = page_index->page_data; 6418 6419 if (lun->backend->lun_attr != NULL && 6420 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6421 != UINT64_MAX) { 6422 phdr = (struct scsi_log_param_header *)data; 6423 scsi_ulto2b(0x0001, phdr->param_code); 6424 phdr->param_control = SLP_LBIN | SLP_LP; 6425 phdr->param_len = 8; 6426 data = (uint8_t *)(phdr + 1); 6427 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6428 data[4] = 0x02; /* per-pool */ 6429 data += phdr->param_len; 6430 } 6431 6432 if (lun->backend->lun_attr != NULL && 6433 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6434 != UINT64_MAX) { 6435 phdr = (struct scsi_log_param_header *)data; 6436 scsi_ulto2b(0x0002, phdr->param_code); 6437 phdr->param_control = SLP_LBIN | SLP_LP; 6438 phdr->param_len = 8; 6439 data = (uint8_t *)(phdr + 1); 6440 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6441 data[4] = 0x01; /* per-LUN */ 6442 data += phdr->param_len; 6443 } 6444 6445 if (lun->backend->lun_attr != NULL && 6446 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6447 != UINT64_MAX) { 6448 phdr = (struct scsi_log_param_header *)data; 6449 scsi_ulto2b(0x00f1, phdr->param_code); 6450 phdr->param_control = SLP_LBIN | SLP_LP; 6451 phdr->param_len = 8; 6452 data = (uint8_t *)(phdr + 1); 6453 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6454 data[4] = 0x02; /* per-pool */ 6455 data += phdr->param_len; 6456 } 6457 6458 if (lun->backend->lun_attr != NULL && 6459 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6460 != UINT64_MAX) { 6461 phdr = (struct scsi_log_param_header *)data; 6462 scsi_ulto2b(0x00f2, phdr->param_code); 6463 phdr->param_control = SLP_LBIN | SLP_LP; 6464 phdr->param_len = 8; 6465 data = (uint8_t *)(phdr + 1); 6466 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6467 data[4] = 0x02; /* per-pool */ 6468 data += phdr->param_len; 6469 } 6470 6471 page_index->page_len = data - page_index->page_data; 6472 return (0); 6473} 6474 6475int 6476ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6477 struct ctl_page_index *page_index, 6478 int pc) 6479{ 6480 struct ctl_lun *lun; 6481 struct stat_page *data; 6482 uint64_t rn, wn, rb, wb; 6483 struct bintime rt, wt; 6484 int i; 6485 6486 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6487 data = (struct stat_page *)page_index->page_data; 6488 6489 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6490 data->sap.hdr.param_control = SLP_LBIN; 6491 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6492 sizeof(struct scsi_log_param_header); 6493 rn = wn = rb = wb = 0; 6494 bintime_clear(&rt); 6495 bintime_clear(&wt); 6496 for (i = 0; i < CTL_MAX_PORTS; i++) { 6497 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6498 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6499 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6500 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6501 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6502 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6503 } 6504 scsi_u64to8b(rn, data->sap.read_num); 6505 scsi_u64to8b(wn, data->sap.write_num); 6506 if (lun->stats.blocksize > 0) { 6507 scsi_u64to8b(wb / lun->stats.blocksize, 6508 data->sap.recvieved_lba); 6509 scsi_u64to8b(rb / lun->stats.blocksize, 6510 data->sap.transmitted_lba); 6511 } 6512 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6513 data->sap.read_int); 6514 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6515 data->sap.write_int); 6516 scsi_u64to8b(0, data->sap.weighted_num); 6517 scsi_u64to8b(0, data->sap.weighted_int); 6518 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6519 data->it.hdr.param_control = SLP_LBIN; 6520 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6521 sizeof(struct scsi_log_param_header); 6522#ifdef CTL_TIME_IO 6523 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6524#endif 6525 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6526 data->it.hdr.param_control = SLP_LBIN; 6527 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6528 sizeof(struct scsi_log_param_header); 6529 scsi_ulto4b(3, data->ti.exponent); 6530 scsi_ulto4b(1, data->ti.integer); 6531 6532 page_index->page_len = sizeof(*data); 6533 return (0); 6534} 6535 6536int 6537ctl_log_sense(struct ctl_scsiio *ctsio) 6538{ 6539 struct ctl_lun *lun; 6540 int i, pc, page_code, subpage; 6541 int alloc_len, total_len; 6542 struct ctl_page_index *page_index; 6543 struct scsi_log_sense *cdb; 6544 struct scsi_log_header *header; 6545 6546 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6547 6548 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6549 cdb = (struct scsi_log_sense *)ctsio->cdb; 6550 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6551 page_code = cdb->page & SLS_PAGE_CODE; 6552 subpage = cdb->subpage; 6553 alloc_len = scsi_2btoul(cdb->length); 6554 6555 page_index = NULL; 6556 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6557 page_index = &lun->log_pages.index[i]; 6558 6559 /* Look for the right page code */ 6560 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6561 continue; 6562 6563 /* Look for the right subpage or the subpage wildcard*/ 6564 if (page_index->subpage != subpage) 6565 continue; 6566 6567 break; 6568 } 6569 if (i >= CTL_NUM_LOG_PAGES) { 6570 ctl_set_invalid_field(ctsio, 6571 /*sks_valid*/ 1, 6572 /*command*/ 1, 6573 /*field*/ 2, 6574 /*bit_valid*/ 0, 6575 /*bit*/ 0); 6576 ctl_done((union ctl_io *)ctsio); 6577 return (CTL_RETVAL_COMPLETE); 6578 } 6579 6580 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6581 6582 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6583 ctsio->kern_sg_entries = 0; 6584 ctsio->kern_data_resid = 0; 6585 ctsio->kern_rel_offset = 0; 6586 if (total_len < alloc_len) { 6587 ctsio->residual = alloc_len - total_len; 6588 ctsio->kern_data_len = total_len; 6589 ctsio->kern_total_len = total_len; 6590 } else { 6591 ctsio->residual = 0; 6592 ctsio->kern_data_len = alloc_len; 6593 ctsio->kern_total_len = alloc_len; 6594 } 6595 6596 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6597 header->page = page_index->page_code; 6598 if (page_index->subpage) { 6599 header->page |= SL_SPF; 6600 header->subpage = page_index->subpage; 6601 } 6602 scsi_ulto2b(page_index->page_len, header->datalen); 6603 6604 /* 6605 * Call the handler, if it exists, to update the 6606 * page to the latest values. 6607 */ 6608 if (page_index->sense_handler != NULL) 6609 page_index->sense_handler(ctsio, page_index, pc); 6610 6611 memcpy(header + 1, page_index->page_data, page_index->page_len); 6612 6613 ctl_set_success(ctsio); 6614 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6615 ctsio->be_move_done = ctl_config_move_done; 6616 ctl_datamove((union ctl_io *)ctsio); 6617 return (CTL_RETVAL_COMPLETE); 6618} 6619 6620int 6621ctl_read_capacity(struct ctl_scsiio *ctsio) 6622{ 6623 struct scsi_read_capacity *cdb; 6624 struct scsi_read_capacity_data *data; 6625 struct ctl_lun *lun; 6626 uint32_t lba; 6627 6628 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6629 6630 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6631 6632 lba = scsi_4btoul(cdb->addr); 6633 if (((cdb->pmi & SRC_PMI) == 0) 6634 && (lba != 0)) { 6635 ctl_set_invalid_field(/*ctsio*/ ctsio, 6636 /*sks_valid*/ 1, 6637 /*command*/ 1, 6638 /*field*/ 2, 6639 /*bit_valid*/ 0, 6640 /*bit*/ 0); 6641 ctl_done((union ctl_io *)ctsio); 6642 return (CTL_RETVAL_COMPLETE); 6643 } 6644 6645 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6646 6647 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6648 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6649 ctsio->residual = 0; 6650 ctsio->kern_data_len = sizeof(*data); 6651 ctsio->kern_total_len = sizeof(*data); 6652 ctsio->kern_data_resid = 0; 6653 ctsio->kern_rel_offset = 0; 6654 ctsio->kern_sg_entries = 0; 6655 6656 /* 6657 * If the maximum LBA is greater than 0xfffffffe, the user must 6658 * issue a SERVICE ACTION IN (16) command, with the read capacity 6659 * serivce action set. 6660 */ 6661 if (lun->be_lun->maxlba > 0xfffffffe) 6662 scsi_ulto4b(0xffffffff, data->addr); 6663 else 6664 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6665 6666 /* 6667 * XXX KDM this may not be 512 bytes... 6668 */ 6669 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6670 6671 ctl_set_success(ctsio); 6672 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6673 ctsio->be_move_done = ctl_config_move_done; 6674 ctl_datamove((union ctl_io *)ctsio); 6675 return (CTL_RETVAL_COMPLETE); 6676} 6677 6678int 6679ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6680{ 6681 struct scsi_read_capacity_16 *cdb; 6682 struct scsi_read_capacity_data_long *data; 6683 struct ctl_lun *lun; 6684 uint64_t lba; 6685 uint32_t alloc_len; 6686 6687 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6688 6689 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6690 6691 alloc_len = scsi_4btoul(cdb->alloc_len); 6692 lba = scsi_8btou64(cdb->addr); 6693 6694 if ((cdb->reladr & SRC16_PMI) 6695 && (lba != 0)) { 6696 ctl_set_invalid_field(/*ctsio*/ ctsio, 6697 /*sks_valid*/ 1, 6698 /*command*/ 1, 6699 /*field*/ 2, 6700 /*bit_valid*/ 0, 6701 /*bit*/ 0); 6702 ctl_done((union ctl_io *)ctsio); 6703 return (CTL_RETVAL_COMPLETE); 6704 } 6705 6706 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6707 6708 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6709 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6710 6711 if (sizeof(*data) < alloc_len) { 6712 ctsio->residual = alloc_len - sizeof(*data); 6713 ctsio->kern_data_len = sizeof(*data); 6714 ctsio->kern_total_len = sizeof(*data); 6715 } else { 6716 ctsio->residual = 0; 6717 ctsio->kern_data_len = alloc_len; 6718 ctsio->kern_total_len = alloc_len; 6719 } 6720 ctsio->kern_data_resid = 0; 6721 ctsio->kern_rel_offset = 0; 6722 ctsio->kern_sg_entries = 0; 6723 6724 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6725 /* XXX KDM this may not be 512 bytes... */ 6726 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6727 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6728 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6729 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6730 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6731 6732 ctl_set_success(ctsio); 6733 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6734 ctsio->be_move_done = ctl_config_move_done; 6735 ctl_datamove((union ctl_io *)ctsio); 6736 return (CTL_RETVAL_COMPLETE); 6737} 6738 6739int 6740ctl_get_lba_status(struct ctl_scsiio *ctsio) 6741{ 6742 struct scsi_get_lba_status *cdb; 6743 struct scsi_get_lba_status_data *data; 6744 struct ctl_lun *lun; 6745 struct ctl_lba_len_flags *lbalen; 6746 uint64_t lba; 6747 uint32_t alloc_len, total_len; 6748 int retval; 6749 6750 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 6751 6752 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6753 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 6754 lba = scsi_8btou64(cdb->addr); 6755 alloc_len = scsi_4btoul(cdb->alloc_len); 6756 6757 if (lba > lun->be_lun->maxlba) { 6758 ctl_set_lba_out_of_range(ctsio); 6759 ctl_done((union ctl_io *)ctsio); 6760 return (CTL_RETVAL_COMPLETE); 6761 } 6762 6763 total_len = sizeof(*data) + sizeof(data->descr[0]); 6764 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6765 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 6766 6767 if (total_len < alloc_len) { 6768 ctsio->residual = alloc_len - total_len; 6769 ctsio->kern_data_len = total_len; 6770 ctsio->kern_total_len = total_len; 6771 } else { 6772 ctsio->residual = 0; 6773 ctsio->kern_data_len = alloc_len; 6774 ctsio->kern_total_len = alloc_len; 6775 } 6776 ctsio->kern_data_resid = 0; 6777 ctsio->kern_rel_offset = 0; 6778 ctsio->kern_sg_entries = 0; 6779 6780 /* Fill dummy data in case backend can't tell anything. */ 6781 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 6782 scsi_u64to8b(lba, data->descr[0].addr); 6783 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 6784 data->descr[0].length); 6785 data->descr[0].status = 0; /* Mapped or unknown. */ 6786 6787 ctl_set_success(ctsio); 6788 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6789 ctsio->be_move_done = ctl_config_move_done; 6790 6791 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6792 lbalen->lba = lba; 6793 lbalen->len = total_len; 6794 lbalen->flags = 0; 6795 retval = lun->backend->config_read((union ctl_io *)ctsio); 6796 return (CTL_RETVAL_COMPLETE); 6797} 6798 6799int 6800ctl_read_defect(struct ctl_scsiio *ctsio) 6801{ 6802 struct scsi_read_defect_data_10 *ccb10; 6803 struct scsi_read_defect_data_12 *ccb12; 6804 struct scsi_read_defect_data_hdr_10 *data10; 6805 struct scsi_read_defect_data_hdr_12 *data12; 6806 uint32_t alloc_len, data_len; 6807 uint8_t format; 6808 6809 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 6810 6811 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 6812 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 6813 format = ccb10->format; 6814 alloc_len = scsi_2btoul(ccb10->alloc_length); 6815 data_len = sizeof(*data10); 6816 } else { 6817 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 6818 format = ccb12->format; 6819 alloc_len = scsi_4btoul(ccb12->alloc_length); 6820 data_len = sizeof(*data12); 6821 } 6822 if (alloc_len == 0) { 6823 ctl_set_success(ctsio); 6824 ctl_done((union ctl_io *)ctsio); 6825 return (CTL_RETVAL_COMPLETE); 6826 } 6827 6828 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 6829 if (data_len < alloc_len) { 6830 ctsio->residual = alloc_len - data_len; 6831 ctsio->kern_data_len = data_len; 6832 ctsio->kern_total_len = data_len; 6833 } else { 6834 ctsio->residual = 0; 6835 ctsio->kern_data_len = alloc_len; 6836 ctsio->kern_total_len = alloc_len; 6837 } 6838 ctsio->kern_data_resid = 0; 6839 ctsio->kern_rel_offset = 0; 6840 ctsio->kern_sg_entries = 0; 6841 6842 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 6843 data10 = (struct scsi_read_defect_data_hdr_10 *) 6844 ctsio->kern_data_ptr; 6845 data10->format = format; 6846 scsi_ulto2b(0, data10->length); 6847 } else { 6848 data12 = (struct scsi_read_defect_data_hdr_12 *) 6849 ctsio->kern_data_ptr; 6850 data12->format = format; 6851 scsi_ulto2b(0, data12->generation); 6852 scsi_ulto4b(0, data12->length); 6853 } 6854 6855 ctl_set_success(ctsio); 6856 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6857 ctsio->be_move_done = ctl_config_move_done; 6858 ctl_datamove((union ctl_io *)ctsio); 6859 return (CTL_RETVAL_COMPLETE); 6860} 6861 6862int 6863ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 6864{ 6865 struct scsi_maintenance_in *cdb; 6866 int retval; 6867 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 6868 int num_target_port_groups, num_target_ports; 6869 struct ctl_lun *lun; 6870 struct ctl_softc *softc; 6871 struct ctl_port *port; 6872 struct scsi_target_group_data *rtg_ptr; 6873 struct scsi_target_group_data_extended *rtg_ext_ptr; 6874 struct scsi_target_port_group_descriptor *tpg_desc; 6875 6876 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 6877 6878 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 6879 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6880 softc = lun->ctl_softc; 6881 6882 retval = CTL_RETVAL_COMPLETE; 6883 6884 switch (cdb->byte2 & STG_PDF_MASK) { 6885 case STG_PDF_LENGTH: 6886 ext = 0; 6887 break; 6888 case STG_PDF_EXTENDED: 6889 ext = 1; 6890 break; 6891 default: 6892 ctl_set_invalid_field(/*ctsio*/ ctsio, 6893 /*sks_valid*/ 1, 6894 /*command*/ 1, 6895 /*field*/ 2, 6896 /*bit_valid*/ 1, 6897 /*bit*/ 5); 6898 ctl_done((union ctl_io *)ctsio); 6899 return(retval); 6900 } 6901 6902 if (softc->is_single) 6903 num_target_port_groups = 1; 6904 else 6905 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 6906 num_target_ports = 0; 6907 mtx_lock(&softc->ctl_lock); 6908 STAILQ_FOREACH(port, &softc->port_list, links) { 6909 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 6910 continue; 6911 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 6912 continue; 6913 num_target_ports++; 6914 } 6915 mtx_unlock(&softc->ctl_lock); 6916 6917 if (ext) 6918 total_len = sizeof(struct scsi_target_group_data_extended); 6919 else 6920 total_len = sizeof(struct scsi_target_group_data); 6921 total_len += sizeof(struct scsi_target_port_group_descriptor) * 6922 num_target_port_groups + 6923 sizeof(struct scsi_target_port_descriptor) * 6924 num_target_ports * num_target_port_groups; 6925 6926 alloc_len = scsi_4btoul(cdb->length); 6927 6928 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6929 6930 ctsio->kern_sg_entries = 0; 6931 6932 if (total_len < alloc_len) { 6933 ctsio->residual = alloc_len - total_len; 6934 ctsio->kern_data_len = total_len; 6935 ctsio->kern_total_len = total_len; 6936 } else { 6937 ctsio->residual = 0; 6938 ctsio->kern_data_len = alloc_len; 6939 ctsio->kern_total_len = alloc_len; 6940 } 6941 ctsio->kern_data_resid = 0; 6942 ctsio->kern_rel_offset = 0; 6943 6944 if (ext) { 6945 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 6946 ctsio->kern_data_ptr; 6947 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 6948 rtg_ext_ptr->format_type = 0x10; 6949 rtg_ext_ptr->implicit_transition_time = 0; 6950 tpg_desc = &rtg_ext_ptr->groups[0]; 6951 } else { 6952 rtg_ptr = (struct scsi_target_group_data *) 6953 ctsio->kern_data_ptr; 6954 scsi_ulto4b(total_len - 4, rtg_ptr->length); 6955 tpg_desc = &rtg_ptr->groups[0]; 6956 } 6957 6958 mtx_lock(&softc->ctl_lock); 6959 pg = softc->port_offset / CTL_MAX_PORTS; 6960 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 6961 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 6962 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 6963 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 6964 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 6965 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 6966 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 6967 } else { 6968 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 6969 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 6970 } 6971 } else { 6972 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 6973 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 6974 } 6975 for (g = 0; g < num_target_port_groups; g++) { 6976 tpg_desc->pref_state = (g == pg) ? gs : os; 6977 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 6978 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 6979 tpg_desc->status = TPG_IMPLICIT; 6980 pc = 0; 6981 STAILQ_FOREACH(port, &softc->port_list, links) { 6982 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 6983 continue; 6984 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 6985 continue; 6986 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 6987 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 6988 relative_target_port_identifier); 6989 pc++; 6990 } 6991 tpg_desc->target_port_count = pc; 6992 tpg_desc = (struct scsi_target_port_group_descriptor *) 6993 &tpg_desc->descriptors[pc]; 6994 } 6995 mtx_unlock(&softc->ctl_lock); 6996 6997 ctl_set_success(ctsio); 6998 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6999 ctsio->be_move_done = ctl_config_move_done; 7000 ctl_datamove((union ctl_io *)ctsio); 7001 return(retval); 7002} 7003 7004int 7005ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7006{ 7007 struct ctl_lun *lun; 7008 struct scsi_report_supported_opcodes *cdb; 7009 const struct ctl_cmd_entry *entry, *sentry; 7010 struct scsi_report_supported_opcodes_all *all; 7011 struct scsi_report_supported_opcodes_descr *descr; 7012 struct scsi_report_supported_opcodes_one *one; 7013 int retval; 7014 int alloc_len, total_len; 7015 int opcode, service_action, i, j, num; 7016 7017 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7018 7019 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7020 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7021 7022 retval = CTL_RETVAL_COMPLETE; 7023 7024 opcode = cdb->requested_opcode; 7025 service_action = scsi_2btoul(cdb->requested_service_action); 7026 switch (cdb->options & RSO_OPTIONS_MASK) { 7027 case RSO_OPTIONS_ALL: 7028 num = 0; 7029 for (i = 0; i < 256; i++) { 7030 entry = &ctl_cmd_table[i]; 7031 if (entry->flags & CTL_CMD_FLAG_SA5) { 7032 for (j = 0; j < 32; j++) { 7033 sentry = &((const struct ctl_cmd_entry *) 7034 entry->execute)[j]; 7035 if (ctl_cmd_applicable( 7036 lun->be_lun->lun_type, sentry)) 7037 num++; 7038 } 7039 } else { 7040 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7041 entry)) 7042 num++; 7043 } 7044 } 7045 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7046 num * sizeof(struct scsi_report_supported_opcodes_descr); 7047 break; 7048 case RSO_OPTIONS_OC: 7049 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7050 ctl_set_invalid_field(/*ctsio*/ ctsio, 7051 /*sks_valid*/ 1, 7052 /*command*/ 1, 7053 /*field*/ 2, 7054 /*bit_valid*/ 1, 7055 /*bit*/ 2); 7056 ctl_done((union ctl_io *)ctsio); 7057 return (CTL_RETVAL_COMPLETE); 7058 } 7059 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7060 break; 7061 case RSO_OPTIONS_OC_SA: 7062 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7063 service_action >= 32) { 7064 ctl_set_invalid_field(/*ctsio*/ ctsio, 7065 /*sks_valid*/ 1, 7066 /*command*/ 1, 7067 /*field*/ 2, 7068 /*bit_valid*/ 1, 7069 /*bit*/ 2); 7070 ctl_done((union ctl_io *)ctsio); 7071 return (CTL_RETVAL_COMPLETE); 7072 } 7073 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7074 break; 7075 default: 7076 ctl_set_invalid_field(/*ctsio*/ ctsio, 7077 /*sks_valid*/ 1, 7078 /*command*/ 1, 7079 /*field*/ 2, 7080 /*bit_valid*/ 1, 7081 /*bit*/ 2); 7082 ctl_done((union ctl_io *)ctsio); 7083 return (CTL_RETVAL_COMPLETE); 7084 } 7085 7086 alloc_len = scsi_4btoul(cdb->length); 7087 7088 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7089 7090 ctsio->kern_sg_entries = 0; 7091 7092 if (total_len < alloc_len) { 7093 ctsio->residual = alloc_len - total_len; 7094 ctsio->kern_data_len = total_len; 7095 ctsio->kern_total_len = total_len; 7096 } else { 7097 ctsio->residual = 0; 7098 ctsio->kern_data_len = alloc_len; 7099 ctsio->kern_total_len = alloc_len; 7100 } 7101 ctsio->kern_data_resid = 0; 7102 ctsio->kern_rel_offset = 0; 7103 7104 switch (cdb->options & RSO_OPTIONS_MASK) { 7105 case RSO_OPTIONS_ALL: 7106 all = (struct scsi_report_supported_opcodes_all *) 7107 ctsio->kern_data_ptr; 7108 num = 0; 7109 for (i = 0; i < 256; i++) { 7110 entry = &ctl_cmd_table[i]; 7111 if (entry->flags & CTL_CMD_FLAG_SA5) { 7112 for (j = 0; j < 32; j++) { 7113 sentry = &((const struct ctl_cmd_entry *) 7114 entry->execute)[j]; 7115 if (!ctl_cmd_applicable( 7116 lun->be_lun->lun_type, sentry)) 7117 continue; 7118 descr = &all->descr[num++]; 7119 descr->opcode = i; 7120 scsi_ulto2b(j, descr->service_action); 7121 descr->flags = RSO_SERVACTV; 7122 scsi_ulto2b(sentry->length, 7123 descr->cdb_length); 7124 } 7125 } else { 7126 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7127 entry)) 7128 continue; 7129 descr = &all->descr[num++]; 7130 descr->opcode = i; 7131 scsi_ulto2b(0, descr->service_action); 7132 descr->flags = 0; 7133 scsi_ulto2b(entry->length, descr->cdb_length); 7134 } 7135 } 7136 scsi_ulto4b( 7137 num * sizeof(struct scsi_report_supported_opcodes_descr), 7138 all->length); 7139 break; 7140 case RSO_OPTIONS_OC: 7141 one = (struct scsi_report_supported_opcodes_one *) 7142 ctsio->kern_data_ptr; 7143 entry = &ctl_cmd_table[opcode]; 7144 goto fill_one; 7145 case RSO_OPTIONS_OC_SA: 7146 one = (struct scsi_report_supported_opcodes_one *) 7147 ctsio->kern_data_ptr; 7148 entry = &ctl_cmd_table[opcode]; 7149 entry = &((const struct ctl_cmd_entry *) 7150 entry->execute)[service_action]; 7151fill_one: 7152 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7153 one->support = 3; 7154 scsi_ulto2b(entry->length, one->cdb_length); 7155 one->cdb_usage[0] = opcode; 7156 memcpy(&one->cdb_usage[1], entry->usage, 7157 entry->length - 1); 7158 } else 7159 one->support = 1; 7160 break; 7161 } 7162 7163 ctl_set_success(ctsio); 7164 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7165 ctsio->be_move_done = ctl_config_move_done; 7166 ctl_datamove((union ctl_io *)ctsio); 7167 return(retval); 7168} 7169 7170int 7171ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7172{ 7173 struct scsi_report_supported_tmf *cdb; 7174 struct scsi_report_supported_tmf_data *data; 7175 int retval; 7176 int alloc_len, total_len; 7177 7178 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7179 7180 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7181 7182 retval = CTL_RETVAL_COMPLETE; 7183 7184 total_len = sizeof(struct scsi_report_supported_tmf_data); 7185 alloc_len = scsi_4btoul(cdb->length); 7186 7187 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7188 7189 ctsio->kern_sg_entries = 0; 7190 7191 if (total_len < alloc_len) { 7192 ctsio->residual = alloc_len - total_len; 7193 ctsio->kern_data_len = total_len; 7194 ctsio->kern_total_len = total_len; 7195 } else { 7196 ctsio->residual = 0; 7197 ctsio->kern_data_len = alloc_len; 7198 ctsio->kern_total_len = alloc_len; 7199 } 7200 ctsio->kern_data_resid = 0; 7201 ctsio->kern_rel_offset = 0; 7202 7203 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7204 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7205 data->byte2 |= RST_ITNRS; 7206 7207 ctl_set_success(ctsio); 7208 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7209 ctsio->be_move_done = ctl_config_move_done; 7210 ctl_datamove((union ctl_io *)ctsio); 7211 return (retval); 7212} 7213 7214int 7215ctl_report_timestamp(struct ctl_scsiio *ctsio) 7216{ 7217 struct scsi_report_timestamp *cdb; 7218 struct scsi_report_timestamp_data *data; 7219 struct timeval tv; 7220 int64_t timestamp; 7221 int retval; 7222 int alloc_len, total_len; 7223 7224 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7225 7226 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7227 7228 retval = CTL_RETVAL_COMPLETE; 7229 7230 total_len = sizeof(struct scsi_report_timestamp_data); 7231 alloc_len = scsi_4btoul(cdb->length); 7232 7233 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7234 7235 ctsio->kern_sg_entries = 0; 7236 7237 if (total_len < alloc_len) { 7238 ctsio->residual = alloc_len - total_len; 7239 ctsio->kern_data_len = total_len; 7240 ctsio->kern_total_len = total_len; 7241 } else { 7242 ctsio->residual = 0; 7243 ctsio->kern_data_len = alloc_len; 7244 ctsio->kern_total_len = alloc_len; 7245 } 7246 ctsio->kern_data_resid = 0; 7247 ctsio->kern_rel_offset = 0; 7248 7249 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7250 scsi_ulto2b(sizeof(*data) - 2, data->length); 7251 data->origin = RTS_ORIG_OUTSIDE; 7252 getmicrotime(&tv); 7253 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7254 scsi_ulto4b(timestamp >> 16, data->timestamp); 7255 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7256 7257 ctl_set_success(ctsio); 7258 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7259 ctsio->be_move_done = ctl_config_move_done; 7260 ctl_datamove((union ctl_io *)ctsio); 7261 return (retval); 7262} 7263 7264int 7265ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7266{ 7267 struct scsi_per_res_in *cdb; 7268 int alloc_len, total_len = 0; 7269 /* struct scsi_per_res_in_rsrv in_data; */ 7270 struct ctl_lun *lun; 7271 struct ctl_softc *softc; 7272 uint64_t key; 7273 7274 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7275 7276 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7277 7278 alloc_len = scsi_2btoul(cdb->length); 7279 7280 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7281 softc = lun->ctl_softc; 7282 7283retry: 7284 mtx_lock(&lun->lun_lock); 7285 switch (cdb->action) { 7286 case SPRI_RK: /* read keys */ 7287 total_len = sizeof(struct scsi_per_res_in_keys) + 7288 lun->pr_key_count * 7289 sizeof(struct scsi_per_res_key); 7290 break; 7291 case SPRI_RR: /* read reservation */ 7292 if (lun->flags & CTL_LUN_PR_RESERVED) 7293 total_len = sizeof(struct scsi_per_res_in_rsrv); 7294 else 7295 total_len = sizeof(struct scsi_per_res_in_header); 7296 break; 7297 case SPRI_RC: /* report capabilities */ 7298 total_len = sizeof(struct scsi_per_res_cap); 7299 break; 7300 case SPRI_RS: /* read full status */ 7301 total_len = sizeof(struct scsi_per_res_in_header) + 7302 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7303 lun->pr_key_count; 7304 break; 7305 default: 7306 panic("Invalid PR type %x", cdb->action); 7307 } 7308 mtx_unlock(&lun->lun_lock); 7309 7310 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7311 7312 if (total_len < alloc_len) { 7313 ctsio->residual = alloc_len - total_len; 7314 ctsio->kern_data_len = total_len; 7315 ctsio->kern_total_len = total_len; 7316 } else { 7317 ctsio->residual = 0; 7318 ctsio->kern_data_len = alloc_len; 7319 ctsio->kern_total_len = alloc_len; 7320 } 7321 7322 ctsio->kern_data_resid = 0; 7323 ctsio->kern_rel_offset = 0; 7324 ctsio->kern_sg_entries = 0; 7325 7326 mtx_lock(&lun->lun_lock); 7327 switch (cdb->action) { 7328 case SPRI_RK: { // read keys 7329 struct scsi_per_res_in_keys *res_keys; 7330 int i, key_count; 7331 7332 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7333 7334 /* 7335 * We had to drop the lock to allocate our buffer, which 7336 * leaves time for someone to come in with another 7337 * persistent reservation. (That is unlikely, though, 7338 * since this should be the only persistent reservation 7339 * command active right now.) 7340 */ 7341 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7342 (lun->pr_key_count * 7343 sizeof(struct scsi_per_res_key)))){ 7344 mtx_unlock(&lun->lun_lock); 7345 free(ctsio->kern_data_ptr, M_CTL); 7346 printf("%s: reservation length changed, retrying\n", 7347 __func__); 7348 goto retry; 7349 } 7350 7351 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7352 7353 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7354 lun->pr_key_count, res_keys->header.length); 7355 7356 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7357 if ((key = ctl_get_prkey(lun, i)) == 0) 7358 continue; 7359 7360 /* 7361 * We used lun->pr_key_count to calculate the 7362 * size to allocate. If it turns out the number of 7363 * initiators with the registered flag set is 7364 * larger than that (i.e. they haven't been kept in 7365 * sync), we've got a problem. 7366 */ 7367 if (key_count >= lun->pr_key_count) { 7368#ifdef NEEDTOPORT 7369 csevent_log(CSC_CTL | CSC_SHELF_SW | 7370 CTL_PR_ERROR, 7371 csevent_LogType_Fault, 7372 csevent_AlertLevel_Yellow, 7373 csevent_FRU_ShelfController, 7374 csevent_FRU_Firmware, 7375 csevent_FRU_Unknown, 7376 "registered keys %d >= key " 7377 "count %d", key_count, 7378 lun->pr_key_count); 7379#endif 7380 key_count++; 7381 continue; 7382 } 7383 scsi_u64to8b(key, res_keys->keys[key_count].key); 7384 key_count++; 7385 } 7386 break; 7387 } 7388 case SPRI_RR: { // read reservation 7389 struct scsi_per_res_in_rsrv *res; 7390 int tmp_len, header_only; 7391 7392 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7393 7394 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7395 7396 if (lun->flags & CTL_LUN_PR_RESERVED) 7397 { 7398 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7399 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7400 res->header.length); 7401 header_only = 0; 7402 } else { 7403 tmp_len = sizeof(struct scsi_per_res_in_header); 7404 scsi_ulto4b(0, res->header.length); 7405 header_only = 1; 7406 } 7407 7408 /* 7409 * We had to drop the lock to allocate our buffer, which 7410 * leaves time for someone to come in with another 7411 * persistent reservation. (That is unlikely, though, 7412 * since this should be the only persistent reservation 7413 * command active right now.) 7414 */ 7415 if (tmp_len != total_len) { 7416 mtx_unlock(&lun->lun_lock); 7417 free(ctsio->kern_data_ptr, M_CTL); 7418 printf("%s: reservation status changed, retrying\n", 7419 __func__); 7420 goto retry; 7421 } 7422 7423 /* 7424 * No reservation held, so we're done. 7425 */ 7426 if (header_only != 0) 7427 break; 7428 7429 /* 7430 * If the registration is an All Registrants type, the key 7431 * is 0, since it doesn't really matter. 7432 */ 7433 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7434 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7435 res->data.reservation); 7436 } 7437 res->data.scopetype = lun->res_type; 7438 break; 7439 } 7440 case SPRI_RC: //report capabilities 7441 { 7442 struct scsi_per_res_cap *res_cap; 7443 uint16_t type_mask; 7444 7445 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7446 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7447 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7448 type_mask = SPRI_TM_WR_EX_AR | 7449 SPRI_TM_EX_AC_RO | 7450 SPRI_TM_WR_EX_RO | 7451 SPRI_TM_EX_AC | 7452 SPRI_TM_WR_EX | 7453 SPRI_TM_EX_AC_AR; 7454 scsi_ulto2b(type_mask, res_cap->type_mask); 7455 break; 7456 } 7457 case SPRI_RS: { // read full status 7458 struct scsi_per_res_in_full *res_status; 7459 struct scsi_per_res_in_full_desc *res_desc; 7460 struct ctl_port *port; 7461 int i, len; 7462 7463 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7464 7465 /* 7466 * We had to drop the lock to allocate our buffer, which 7467 * leaves time for someone to come in with another 7468 * persistent reservation. (That is unlikely, though, 7469 * since this should be the only persistent reservation 7470 * command active right now.) 7471 */ 7472 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7473 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7474 lun->pr_key_count)){ 7475 mtx_unlock(&lun->lun_lock); 7476 free(ctsio->kern_data_ptr, M_CTL); 7477 printf("%s: reservation length changed, retrying\n", 7478 __func__); 7479 goto retry; 7480 } 7481 7482 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7483 7484 res_desc = &res_status->desc[0]; 7485 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7486 if ((key = ctl_get_prkey(lun, i)) == 0) 7487 continue; 7488 7489 scsi_u64to8b(key, res_desc->res_key.key); 7490 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7491 (lun->pr_res_idx == i || 7492 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7493 res_desc->flags = SPRI_FULL_R_HOLDER; 7494 res_desc->scopetype = lun->res_type; 7495 } 7496 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7497 res_desc->rel_trgt_port_id); 7498 len = 0; 7499 port = softc->ctl_ports[ 7500 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7501 if (port != NULL) 7502 len = ctl_create_iid(port, 7503 i % CTL_MAX_INIT_PER_PORT, 7504 res_desc->transport_id); 7505 scsi_ulto4b(len, res_desc->additional_length); 7506 res_desc = (struct scsi_per_res_in_full_desc *) 7507 &res_desc->transport_id[len]; 7508 } 7509 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7510 res_status->header.length); 7511 break; 7512 } 7513 default: 7514 /* 7515 * This is a bug, because we just checked for this above, 7516 * and should have returned an error. 7517 */ 7518 panic("Invalid PR type %x", cdb->action); 7519 break; /* NOTREACHED */ 7520 } 7521 mtx_unlock(&lun->lun_lock); 7522 7523 ctl_set_success(ctsio); 7524 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7525 ctsio->be_move_done = ctl_config_move_done; 7526 ctl_datamove((union ctl_io *)ctsio); 7527 return (CTL_RETVAL_COMPLETE); 7528} 7529 7530static void 7531ctl_est_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua) 7532{ 7533 int off = lun->ctl_softc->persis_offset; 7534 7535 if (residx >= off && residx < off + CTL_MAX_INITIATORS) 7536 ctl_est_ua(lun, residx - off, ua); 7537} 7538 7539/* 7540 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7541 * it should return. 7542 */ 7543static int 7544ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7545 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7546 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7547 struct scsi_per_res_out_parms* param) 7548{ 7549 union ctl_ha_msg persis_io; 7550 int retval, i; 7551 int isc_retval; 7552 7553 retval = 0; 7554 7555 mtx_lock(&lun->lun_lock); 7556 if (sa_res_key == 0) { 7557 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7558 /* validate scope and type */ 7559 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7560 SPR_LU_SCOPE) { 7561 mtx_unlock(&lun->lun_lock); 7562 ctl_set_invalid_field(/*ctsio*/ ctsio, 7563 /*sks_valid*/ 1, 7564 /*command*/ 1, 7565 /*field*/ 2, 7566 /*bit_valid*/ 1, 7567 /*bit*/ 4); 7568 ctl_done((union ctl_io *)ctsio); 7569 return (1); 7570 } 7571 7572 if (type>8 || type==2 || type==4 || type==0) { 7573 mtx_unlock(&lun->lun_lock); 7574 ctl_set_invalid_field(/*ctsio*/ ctsio, 7575 /*sks_valid*/ 1, 7576 /*command*/ 1, 7577 /*field*/ 2, 7578 /*bit_valid*/ 1, 7579 /*bit*/ 0); 7580 ctl_done((union ctl_io *)ctsio); 7581 return (1); 7582 } 7583 7584 /* 7585 * Unregister everybody else and build UA for 7586 * them 7587 */ 7588 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7589 if (i == residx || ctl_get_prkey(lun, i) == 0) 7590 continue; 7591 7592 ctl_clr_prkey(lun, i); 7593 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 7594 } 7595 lun->pr_key_count = 1; 7596 lun->res_type = type; 7597 if (lun->res_type != SPR_TYPE_WR_EX_AR 7598 && lun->res_type != SPR_TYPE_EX_AC_AR) 7599 lun->pr_res_idx = residx; 7600 7601 /* send msg to other side */ 7602 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7603 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7604 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7605 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7606 persis_io.pr.pr_info.res_type = type; 7607 memcpy(persis_io.pr.pr_info.sa_res_key, 7608 param->serv_act_res_key, 7609 sizeof(param->serv_act_res_key)); 7610 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7611 &persis_io, sizeof(persis_io), 0)) > 7612 CTL_HA_STATUS_SUCCESS) { 7613 printf("CTL:Persis Out error returned " 7614 "from ctl_ha_msg_send %d\n", 7615 isc_retval); 7616 } 7617 } else { 7618 /* not all registrants */ 7619 mtx_unlock(&lun->lun_lock); 7620 free(ctsio->kern_data_ptr, M_CTL); 7621 ctl_set_invalid_field(ctsio, 7622 /*sks_valid*/ 1, 7623 /*command*/ 0, 7624 /*field*/ 8, 7625 /*bit_valid*/ 0, 7626 /*bit*/ 0); 7627 ctl_done((union ctl_io *)ctsio); 7628 return (1); 7629 } 7630 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7631 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7632 int found = 0; 7633 7634 if (res_key == sa_res_key) { 7635 /* special case */ 7636 /* 7637 * The spec implies this is not good but doesn't 7638 * say what to do. There are two choices either 7639 * generate a res conflict or check condition 7640 * with illegal field in parameter data. Since 7641 * that is what is done when the sa_res_key is 7642 * zero I'll take that approach since this has 7643 * to do with the sa_res_key. 7644 */ 7645 mtx_unlock(&lun->lun_lock); 7646 free(ctsio->kern_data_ptr, M_CTL); 7647 ctl_set_invalid_field(ctsio, 7648 /*sks_valid*/ 1, 7649 /*command*/ 0, 7650 /*field*/ 8, 7651 /*bit_valid*/ 0, 7652 /*bit*/ 0); 7653 ctl_done((union ctl_io *)ctsio); 7654 return (1); 7655 } 7656 7657 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7658 if (ctl_get_prkey(lun, i) != sa_res_key) 7659 continue; 7660 7661 found = 1; 7662 ctl_clr_prkey(lun, i); 7663 lun->pr_key_count--; 7664 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 7665 } 7666 if (!found) { 7667 mtx_unlock(&lun->lun_lock); 7668 free(ctsio->kern_data_ptr, M_CTL); 7669 ctl_set_reservation_conflict(ctsio); 7670 ctl_done((union ctl_io *)ctsio); 7671 return (CTL_RETVAL_COMPLETE); 7672 } 7673 /* send msg to other side */ 7674 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7675 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7676 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7677 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7678 persis_io.pr.pr_info.res_type = type; 7679 memcpy(persis_io.pr.pr_info.sa_res_key, 7680 param->serv_act_res_key, 7681 sizeof(param->serv_act_res_key)); 7682 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7683 &persis_io, sizeof(persis_io), 0)) > 7684 CTL_HA_STATUS_SUCCESS) { 7685 printf("CTL:Persis Out error returned from " 7686 "ctl_ha_msg_send %d\n", isc_retval); 7687 } 7688 } else { 7689 /* Reserved but not all registrants */ 7690 /* sa_res_key is res holder */ 7691 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7692 /* validate scope and type */ 7693 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7694 SPR_LU_SCOPE) { 7695 mtx_unlock(&lun->lun_lock); 7696 ctl_set_invalid_field(/*ctsio*/ ctsio, 7697 /*sks_valid*/ 1, 7698 /*command*/ 1, 7699 /*field*/ 2, 7700 /*bit_valid*/ 1, 7701 /*bit*/ 4); 7702 ctl_done((union ctl_io *)ctsio); 7703 return (1); 7704 } 7705 7706 if (type>8 || type==2 || type==4 || type==0) { 7707 mtx_unlock(&lun->lun_lock); 7708 ctl_set_invalid_field(/*ctsio*/ ctsio, 7709 /*sks_valid*/ 1, 7710 /*command*/ 1, 7711 /*field*/ 2, 7712 /*bit_valid*/ 1, 7713 /*bit*/ 0); 7714 ctl_done((union ctl_io *)ctsio); 7715 return (1); 7716 } 7717 7718 /* 7719 * Do the following: 7720 * if sa_res_key != res_key remove all 7721 * registrants w/sa_res_key and generate UA 7722 * for these registrants(Registrations 7723 * Preempted) if it wasn't an exclusive 7724 * reservation generate UA(Reservations 7725 * Preempted) for all other registered nexuses 7726 * if the type has changed. Establish the new 7727 * reservation and holder. If res_key and 7728 * sa_res_key are the same do the above 7729 * except don't unregister the res holder. 7730 */ 7731 7732 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7733 if (i == residx || ctl_get_prkey(lun, i) == 0) 7734 continue; 7735 7736 if (sa_res_key == ctl_get_prkey(lun, i)) { 7737 ctl_clr_prkey(lun, i); 7738 lun->pr_key_count--; 7739 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 7740 } else if (type != lun->res_type 7741 && (lun->res_type == SPR_TYPE_WR_EX_RO 7742 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 7743 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 7744 } 7745 } 7746 lun->res_type = type; 7747 if (lun->res_type != SPR_TYPE_WR_EX_AR 7748 && lun->res_type != SPR_TYPE_EX_AC_AR) 7749 lun->pr_res_idx = residx; 7750 else 7751 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7752 7753 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7754 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7755 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7756 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7757 persis_io.pr.pr_info.res_type = type; 7758 memcpy(persis_io.pr.pr_info.sa_res_key, 7759 param->serv_act_res_key, 7760 sizeof(param->serv_act_res_key)); 7761 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7762 &persis_io, sizeof(persis_io), 0)) > 7763 CTL_HA_STATUS_SUCCESS) { 7764 printf("CTL:Persis Out error returned " 7765 "from ctl_ha_msg_send %d\n", 7766 isc_retval); 7767 } 7768 } else { 7769 /* 7770 * sa_res_key is not the res holder just 7771 * remove registrants 7772 */ 7773 int found=0; 7774 7775 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7776 if (sa_res_key != ctl_get_prkey(lun, i)) 7777 continue; 7778 7779 found = 1; 7780 ctl_clr_prkey(lun, i); 7781 lun->pr_key_count--; 7782 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 7783 } 7784 7785 if (!found) { 7786 mtx_unlock(&lun->lun_lock); 7787 free(ctsio->kern_data_ptr, M_CTL); 7788 ctl_set_reservation_conflict(ctsio); 7789 ctl_done((union ctl_io *)ctsio); 7790 return (1); 7791 } 7792 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7793 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7794 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7795 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7796 persis_io.pr.pr_info.res_type = type; 7797 memcpy(persis_io.pr.pr_info.sa_res_key, 7798 param->serv_act_res_key, 7799 sizeof(param->serv_act_res_key)); 7800 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7801 &persis_io, sizeof(persis_io), 0)) > 7802 CTL_HA_STATUS_SUCCESS) { 7803 printf("CTL:Persis Out error returned " 7804 "from ctl_ha_msg_send %d\n", 7805 isc_retval); 7806 } 7807 } 7808 } 7809 7810 lun->PRGeneration++; 7811 mtx_unlock(&lun->lun_lock); 7812 7813 return (retval); 7814} 7815 7816static void 7817ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 7818{ 7819 uint64_t sa_res_key; 7820 int i; 7821 7822 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 7823 7824 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7825 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 7826 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 7827 if (sa_res_key == 0) { 7828 /* 7829 * Unregister everybody else and build UA for 7830 * them 7831 */ 7832 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7833 if (i == msg->pr.pr_info.residx || 7834 ctl_get_prkey(lun, i) == 0) 7835 continue; 7836 7837 ctl_clr_prkey(lun, i); 7838 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 7839 } 7840 7841 lun->pr_key_count = 1; 7842 lun->res_type = msg->pr.pr_info.res_type; 7843 if (lun->res_type != SPR_TYPE_WR_EX_AR 7844 && lun->res_type != SPR_TYPE_EX_AC_AR) 7845 lun->pr_res_idx = msg->pr.pr_info.residx; 7846 } else { 7847 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7848 if (sa_res_key == ctl_get_prkey(lun, i)) 7849 continue; 7850 7851 ctl_clr_prkey(lun, i); 7852 lun->pr_key_count--; 7853 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 7854 } 7855 } 7856 } else { 7857 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7858 if (i == msg->pr.pr_info.residx || 7859 ctl_get_prkey(lun, i) == 0) 7860 continue; 7861 7862 if (sa_res_key == ctl_get_prkey(lun, i)) { 7863 ctl_clr_prkey(lun, i); 7864 lun->pr_key_count--; 7865 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 7866 } else if (msg->pr.pr_info.res_type != lun->res_type 7867 && (lun->res_type == SPR_TYPE_WR_EX_RO 7868 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 7869 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 7870 } 7871 } 7872 lun->res_type = msg->pr.pr_info.res_type; 7873 if (lun->res_type != SPR_TYPE_WR_EX_AR 7874 && lun->res_type != SPR_TYPE_EX_AC_AR) 7875 lun->pr_res_idx = msg->pr.pr_info.residx; 7876 else 7877 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7878 } 7879 lun->PRGeneration++; 7880 7881} 7882 7883 7884int 7885ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 7886{ 7887 int retval; 7888 int isc_retval; 7889 u_int32_t param_len; 7890 struct scsi_per_res_out *cdb; 7891 struct ctl_lun *lun; 7892 struct scsi_per_res_out_parms* param; 7893 struct ctl_softc *softc; 7894 uint32_t residx; 7895 uint64_t res_key, sa_res_key, key; 7896 uint8_t type; 7897 union ctl_ha_msg persis_io; 7898 int i; 7899 7900 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 7901 7902 retval = CTL_RETVAL_COMPLETE; 7903 7904 cdb = (struct scsi_per_res_out *)ctsio->cdb; 7905 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7906 softc = lun->ctl_softc; 7907 7908 /* 7909 * We only support whole-LUN scope. The scope & type are ignored for 7910 * register, register and ignore existing key and clear. 7911 * We sometimes ignore scope and type on preempts too!! 7912 * Verify reservation type here as well. 7913 */ 7914 type = cdb->scope_type & SPR_TYPE_MASK; 7915 if ((cdb->action == SPRO_RESERVE) 7916 || (cdb->action == SPRO_RELEASE)) { 7917 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 7918 ctl_set_invalid_field(/*ctsio*/ ctsio, 7919 /*sks_valid*/ 1, 7920 /*command*/ 1, 7921 /*field*/ 2, 7922 /*bit_valid*/ 1, 7923 /*bit*/ 4); 7924 ctl_done((union ctl_io *)ctsio); 7925 return (CTL_RETVAL_COMPLETE); 7926 } 7927 7928 if (type>8 || type==2 || type==4 || type==0) { 7929 ctl_set_invalid_field(/*ctsio*/ ctsio, 7930 /*sks_valid*/ 1, 7931 /*command*/ 1, 7932 /*field*/ 2, 7933 /*bit_valid*/ 1, 7934 /*bit*/ 0); 7935 ctl_done((union ctl_io *)ctsio); 7936 return (CTL_RETVAL_COMPLETE); 7937 } 7938 } 7939 7940 param_len = scsi_4btoul(cdb->length); 7941 7942 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 7943 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 7944 ctsio->kern_data_len = param_len; 7945 ctsio->kern_total_len = param_len; 7946 ctsio->kern_data_resid = 0; 7947 ctsio->kern_rel_offset = 0; 7948 ctsio->kern_sg_entries = 0; 7949 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7950 ctsio->be_move_done = ctl_config_move_done; 7951 ctl_datamove((union ctl_io *)ctsio); 7952 7953 return (CTL_RETVAL_COMPLETE); 7954 } 7955 7956 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 7957 7958 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 7959 res_key = scsi_8btou64(param->res_key.key); 7960 sa_res_key = scsi_8btou64(param->serv_act_res_key); 7961 7962 /* 7963 * Validate the reservation key here except for SPRO_REG_IGNO 7964 * This must be done for all other service actions 7965 */ 7966 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 7967 mtx_lock(&lun->lun_lock); 7968 if ((key = ctl_get_prkey(lun, residx)) != 0) { 7969 if (res_key != key) { 7970 /* 7971 * The current key passed in doesn't match 7972 * the one the initiator previously 7973 * registered. 7974 */ 7975 mtx_unlock(&lun->lun_lock); 7976 free(ctsio->kern_data_ptr, M_CTL); 7977 ctl_set_reservation_conflict(ctsio); 7978 ctl_done((union ctl_io *)ctsio); 7979 return (CTL_RETVAL_COMPLETE); 7980 } 7981 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 7982 /* 7983 * We are not registered 7984 */ 7985 mtx_unlock(&lun->lun_lock); 7986 free(ctsio->kern_data_ptr, M_CTL); 7987 ctl_set_reservation_conflict(ctsio); 7988 ctl_done((union ctl_io *)ctsio); 7989 return (CTL_RETVAL_COMPLETE); 7990 } else if (res_key != 0) { 7991 /* 7992 * We are not registered and trying to register but 7993 * the register key isn't zero. 7994 */ 7995 mtx_unlock(&lun->lun_lock); 7996 free(ctsio->kern_data_ptr, M_CTL); 7997 ctl_set_reservation_conflict(ctsio); 7998 ctl_done((union ctl_io *)ctsio); 7999 return (CTL_RETVAL_COMPLETE); 8000 } 8001 mtx_unlock(&lun->lun_lock); 8002 } 8003 8004 switch (cdb->action & SPRO_ACTION_MASK) { 8005 case SPRO_REGISTER: 8006 case SPRO_REG_IGNO: { 8007 8008#if 0 8009 printf("Registration received\n"); 8010#endif 8011 8012 /* 8013 * We don't support any of these options, as we report in 8014 * the read capabilities request (see 8015 * ctl_persistent_reserve_in(), above). 8016 */ 8017 if ((param->flags & SPR_SPEC_I_PT) 8018 || (param->flags & SPR_ALL_TG_PT) 8019 || (param->flags & SPR_APTPL)) { 8020 int bit_ptr; 8021 8022 if (param->flags & SPR_APTPL) 8023 bit_ptr = 0; 8024 else if (param->flags & SPR_ALL_TG_PT) 8025 bit_ptr = 2; 8026 else /* SPR_SPEC_I_PT */ 8027 bit_ptr = 3; 8028 8029 free(ctsio->kern_data_ptr, M_CTL); 8030 ctl_set_invalid_field(ctsio, 8031 /*sks_valid*/ 1, 8032 /*command*/ 0, 8033 /*field*/ 20, 8034 /*bit_valid*/ 1, 8035 /*bit*/ bit_ptr); 8036 ctl_done((union ctl_io *)ctsio); 8037 return (CTL_RETVAL_COMPLETE); 8038 } 8039 8040 mtx_lock(&lun->lun_lock); 8041 8042 /* 8043 * The initiator wants to clear the 8044 * key/unregister. 8045 */ 8046 if (sa_res_key == 0) { 8047 if ((res_key == 0 8048 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8049 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8050 && ctl_get_prkey(lun, residx) == 0)) { 8051 mtx_unlock(&lun->lun_lock); 8052 goto done; 8053 } 8054 8055 ctl_clr_prkey(lun, residx); 8056 lun->pr_key_count--; 8057 8058 if (residx == lun->pr_res_idx) { 8059 lun->flags &= ~CTL_LUN_PR_RESERVED; 8060 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8061 8062 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8063 || lun->res_type == SPR_TYPE_EX_AC_RO) 8064 && lun->pr_key_count) { 8065 /* 8066 * If the reservation is a registrants 8067 * only type we need to generate a UA 8068 * for other registered inits. The 8069 * sense code should be RESERVATIONS 8070 * RELEASED 8071 */ 8072 8073 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8074 if (ctl_get_prkey(lun, i + 8075 softc->persis_offset) == 0) 8076 continue; 8077 ctl_est_ua(lun, i, 8078 CTL_UA_RES_RELEASE); 8079 } 8080 } 8081 lun->res_type = 0; 8082 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8083 if (lun->pr_key_count==0) { 8084 lun->flags &= ~CTL_LUN_PR_RESERVED; 8085 lun->res_type = 0; 8086 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8087 } 8088 } 8089 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8090 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8091 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8092 persis_io.pr.pr_info.residx = residx; 8093 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8094 &persis_io, sizeof(persis_io), 0 )) > 8095 CTL_HA_STATUS_SUCCESS) { 8096 printf("CTL:Persis Out error returned from " 8097 "ctl_ha_msg_send %d\n", isc_retval); 8098 } 8099 } else /* sa_res_key != 0 */ { 8100 8101 /* 8102 * If we aren't registered currently then increment 8103 * the key count and set the registered flag. 8104 */ 8105 ctl_alloc_prkey(lun, residx); 8106 if (ctl_get_prkey(lun, residx) == 0) 8107 lun->pr_key_count++; 8108 ctl_set_prkey(lun, residx, sa_res_key); 8109 8110 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8111 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8112 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8113 persis_io.pr.pr_info.residx = residx; 8114 memcpy(persis_io.pr.pr_info.sa_res_key, 8115 param->serv_act_res_key, 8116 sizeof(param->serv_act_res_key)); 8117 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8118 &persis_io, sizeof(persis_io), 0)) > 8119 CTL_HA_STATUS_SUCCESS) { 8120 printf("CTL:Persis Out error returned from " 8121 "ctl_ha_msg_send %d\n", isc_retval); 8122 } 8123 } 8124 lun->PRGeneration++; 8125 mtx_unlock(&lun->lun_lock); 8126 8127 break; 8128 } 8129 case SPRO_RESERVE: 8130#if 0 8131 printf("Reserve executed type %d\n", type); 8132#endif 8133 mtx_lock(&lun->lun_lock); 8134 if (lun->flags & CTL_LUN_PR_RESERVED) { 8135 /* 8136 * if this isn't the reservation holder and it's 8137 * not a "all registrants" type or if the type is 8138 * different then we have a conflict 8139 */ 8140 if ((lun->pr_res_idx != residx 8141 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8142 || lun->res_type != type) { 8143 mtx_unlock(&lun->lun_lock); 8144 free(ctsio->kern_data_ptr, M_CTL); 8145 ctl_set_reservation_conflict(ctsio); 8146 ctl_done((union ctl_io *)ctsio); 8147 return (CTL_RETVAL_COMPLETE); 8148 } 8149 mtx_unlock(&lun->lun_lock); 8150 } else /* create a reservation */ { 8151 /* 8152 * If it's not an "all registrants" type record 8153 * reservation holder 8154 */ 8155 if (type != SPR_TYPE_WR_EX_AR 8156 && type != SPR_TYPE_EX_AC_AR) 8157 lun->pr_res_idx = residx; /* Res holder */ 8158 else 8159 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8160 8161 lun->flags |= CTL_LUN_PR_RESERVED; 8162 lun->res_type = type; 8163 8164 mtx_unlock(&lun->lun_lock); 8165 8166 /* send msg to other side */ 8167 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8168 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8169 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8170 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8171 persis_io.pr.pr_info.res_type = type; 8172 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8173 &persis_io, sizeof(persis_io), 0)) > 8174 CTL_HA_STATUS_SUCCESS) { 8175 printf("CTL:Persis Out error returned from " 8176 "ctl_ha_msg_send %d\n", isc_retval); 8177 } 8178 } 8179 break; 8180 8181 case SPRO_RELEASE: 8182 mtx_lock(&lun->lun_lock); 8183 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8184 /* No reservation exists return good status */ 8185 mtx_unlock(&lun->lun_lock); 8186 goto done; 8187 } 8188 /* 8189 * Is this nexus a reservation holder? 8190 */ 8191 if (lun->pr_res_idx != residx 8192 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8193 /* 8194 * not a res holder return good status but 8195 * do nothing 8196 */ 8197 mtx_unlock(&lun->lun_lock); 8198 goto done; 8199 } 8200 8201 if (lun->res_type != type) { 8202 mtx_unlock(&lun->lun_lock); 8203 free(ctsio->kern_data_ptr, M_CTL); 8204 ctl_set_illegal_pr_release(ctsio); 8205 ctl_done((union ctl_io *)ctsio); 8206 return (CTL_RETVAL_COMPLETE); 8207 } 8208 8209 /* okay to release */ 8210 lun->flags &= ~CTL_LUN_PR_RESERVED; 8211 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8212 lun->res_type = 0; 8213 8214 /* 8215 * if this isn't an exclusive access 8216 * res generate UA for all other 8217 * registrants. 8218 */ 8219 if (type != SPR_TYPE_EX_AC 8220 && type != SPR_TYPE_WR_EX) { 8221 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8222 if (i == residx || 8223 ctl_get_prkey(lun, 8224 i + softc->persis_offset) == 0) 8225 continue; 8226 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8227 } 8228 } 8229 mtx_unlock(&lun->lun_lock); 8230 /* Send msg to other side */ 8231 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8232 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8233 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8234 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8235 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8236 printf("CTL:Persis Out error returned from " 8237 "ctl_ha_msg_send %d\n", isc_retval); 8238 } 8239 break; 8240 8241 case SPRO_CLEAR: 8242 /* send msg to other side */ 8243 8244 mtx_lock(&lun->lun_lock); 8245 lun->flags &= ~CTL_LUN_PR_RESERVED; 8246 lun->res_type = 0; 8247 lun->pr_key_count = 0; 8248 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8249 8250 ctl_clr_prkey(lun, residx); 8251 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8252 if (ctl_get_prkey(lun, i) != 0) { 8253 ctl_clr_prkey(lun, i); 8254 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8255 } 8256 lun->PRGeneration++; 8257 mtx_unlock(&lun->lun_lock); 8258 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8259 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8260 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8261 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8262 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8263 printf("CTL:Persis Out error returned from " 8264 "ctl_ha_msg_send %d\n", isc_retval); 8265 } 8266 break; 8267 8268 case SPRO_PREEMPT: 8269 case SPRO_PRE_ABO: { 8270 int nretval; 8271 8272 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8273 residx, ctsio, cdb, param); 8274 if (nretval != 0) 8275 return (CTL_RETVAL_COMPLETE); 8276 break; 8277 } 8278 default: 8279 panic("Invalid PR type %x", cdb->action); 8280 } 8281 8282done: 8283 free(ctsio->kern_data_ptr, M_CTL); 8284 ctl_set_success(ctsio); 8285 ctl_done((union ctl_io *)ctsio); 8286 8287 return (retval); 8288} 8289 8290/* 8291 * This routine is for handling a message from the other SC pertaining to 8292 * persistent reserve out. All the error checking will have been done 8293 * so only perorming the action need be done here to keep the two 8294 * in sync. 8295 */ 8296static void 8297ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8298{ 8299 struct ctl_lun *lun; 8300 struct ctl_softc *softc; 8301 int i; 8302 uint32_t targ_lun; 8303 8304 softc = control_softc; 8305 8306 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8307 lun = softc->ctl_luns[targ_lun]; 8308 mtx_lock(&lun->lun_lock); 8309 switch(msg->pr.pr_info.action) { 8310 case CTL_PR_REG_KEY: 8311 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8312 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8313 lun->pr_key_count++; 8314 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8315 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8316 lun->PRGeneration++; 8317 break; 8318 8319 case CTL_PR_UNREG_KEY: 8320 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8321 lun->pr_key_count--; 8322 8323 /* XXX Need to see if the reservation has been released */ 8324 /* if so do we need to generate UA? */ 8325 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8326 lun->flags &= ~CTL_LUN_PR_RESERVED; 8327 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8328 8329 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8330 || lun->res_type == SPR_TYPE_EX_AC_RO) 8331 && lun->pr_key_count) { 8332 /* 8333 * If the reservation is a registrants 8334 * only type we need to generate a UA 8335 * for other registered inits. The 8336 * sense code should be RESERVATIONS 8337 * RELEASED 8338 */ 8339 8340 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8341 if (ctl_get_prkey(lun, i + 8342 softc->persis_offset) == 0) 8343 continue; 8344 8345 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8346 } 8347 } 8348 lun->res_type = 0; 8349 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8350 if (lun->pr_key_count==0) { 8351 lun->flags &= ~CTL_LUN_PR_RESERVED; 8352 lun->res_type = 0; 8353 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8354 } 8355 } 8356 lun->PRGeneration++; 8357 break; 8358 8359 case CTL_PR_RESERVE: 8360 lun->flags |= CTL_LUN_PR_RESERVED; 8361 lun->res_type = msg->pr.pr_info.res_type; 8362 lun->pr_res_idx = msg->pr.pr_info.residx; 8363 8364 break; 8365 8366 case CTL_PR_RELEASE: 8367 /* 8368 * if this isn't an exclusive access res generate UA for all 8369 * other registrants. 8370 */ 8371 if (lun->res_type != SPR_TYPE_EX_AC 8372 && lun->res_type != SPR_TYPE_WR_EX) { 8373 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8374 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0) 8375 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8376 } 8377 8378 lun->flags &= ~CTL_LUN_PR_RESERVED; 8379 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8380 lun->res_type = 0; 8381 break; 8382 8383 case CTL_PR_PREEMPT: 8384 ctl_pro_preempt_other(lun, msg); 8385 break; 8386 case CTL_PR_CLEAR: 8387 lun->flags &= ~CTL_LUN_PR_RESERVED; 8388 lun->res_type = 0; 8389 lun->pr_key_count = 0; 8390 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8391 8392 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8393 if (ctl_get_prkey(lun, i) == 0) 8394 continue; 8395 ctl_clr_prkey(lun, i); 8396 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8397 } 8398 lun->PRGeneration++; 8399 break; 8400 } 8401 8402 mtx_unlock(&lun->lun_lock); 8403} 8404 8405int 8406ctl_read_write(struct ctl_scsiio *ctsio) 8407{ 8408 struct ctl_lun *lun; 8409 struct ctl_lba_len_flags *lbalen; 8410 uint64_t lba; 8411 uint32_t num_blocks; 8412 int flags, retval; 8413 int isread; 8414 8415 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8416 8417 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8418 8419 flags = 0; 8420 retval = CTL_RETVAL_COMPLETE; 8421 8422 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8423 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8424 switch (ctsio->cdb[0]) { 8425 case READ_6: 8426 case WRITE_6: { 8427 struct scsi_rw_6 *cdb; 8428 8429 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8430 8431 lba = scsi_3btoul(cdb->addr); 8432 /* only 5 bits are valid in the most significant address byte */ 8433 lba &= 0x1fffff; 8434 num_blocks = cdb->length; 8435 /* 8436 * This is correct according to SBC-2. 8437 */ 8438 if (num_blocks == 0) 8439 num_blocks = 256; 8440 break; 8441 } 8442 case READ_10: 8443 case WRITE_10: { 8444 struct scsi_rw_10 *cdb; 8445 8446 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8447 if (cdb->byte2 & SRW10_FUA) 8448 flags |= CTL_LLF_FUA; 8449 if (cdb->byte2 & SRW10_DPO) 8450 flags |= CTL_LLF_DPO; 8451 lba = scsi_4btoul(cdb->addr); 8452 num_blocks = scsi_2btoul(cdb->length); 8453 break; 8454 } 8455 case WRITE_VERIFY_10: { 8456 struct scsi_write_verify_10 *cdb; 8457 8458 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8459 flags |= CTL_LLF_FUA; 8460 if (cdb->byte2 & SWV_DPO) 8461 flags |= CTL_LLF_DPO; 8462 lba = scsi_4btoul(cdb->addr); 8463 num_blocks = scsi_2btoul(cdb->length); 8464 break; 8465 } 8466 case READ_12: 8467 case WRITE_12: { 8468 struct scsi_rw_12 *cdb; 8469 8470 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8471 if (cdb->byte2 & SRW12_FUA) 8472 flags |= CTL_LLF_FUA; 8473 if (cdb->byte2 & SRW12_DPO) 8474 flags |= CTL_LLF_DPO; 8475 lba = scsi_4btoul(cdb->addr); 8476 num_blocks = scsi_4btoul(cdb->length); 8477 break; 8478 } 8479 case WRITE_VERIFY_12: { 8480 struct scsi_write_verify_12 *cdb; 8481 8482 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8483 flags |= CTL_LLF_FUA; 8484 if (cdb->byte2 & SWV_DPO) 8485 flags |= CTL_LLF_DPO; 8486 lba = scsi_4btoul(cdb->addr); 8487 num_blocks = scsi_4btoul(cdb->length); 8488 break; 8489 } 8490 case READ_16: 8491 case WRITE_16: { 8492 struct scsi_rw_16 *cdb; 8493 8494 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8495 if (cdb->byte2 & SRW12_FUA) 8496 flags |= CTL_LLF_FUA; 8497 if (cdb->byte2 & SRW12_DPO) 8498 flags |= CTL_LLF_DPO; 8499 lba = scsi_8btou64(cdb->addr); 8500 num_blocks = scsi_4btoul(cdb->length); 8501 break; 8502 } 8503 case WRITE_ATOMIC_16: { 8504 struct scsi_rw_16 *cdb; 8505 8506 if (lun->be_lun->atomicblock == 0) { 8507 ctl_set_invalid_opcode(ctsio); 8508 ctl_done((union ctl_io *)ctsio); 8509 return (CTL_RETVAL_COMPLETE); 8510 } 8511 8512 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8513 if (cdb->byte2 & SRW12_FUA) 8514 flags |= CTL_LLF_FUA; 8515 if (cdb->byte2 & SRW12_DPO) 8516 flags |= CTL_LLF_DPO; 8517 lba = scsi_8btou64(cdb->addr); 8518 num_blocks = scsi_4btoul(cdb->length); 8519 if (num_blocks > lun->be_lun->atomicblock) { 8520 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8521 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8522 /*bit*/ 0); 8523 ctl_done((union ctl_io *)ctsio); 8524 return (CTL_RETVAL_COMPLETE); 8525 } 8526 break; 8527 } 8528 case WRITE_VERIFY_16: { 8529 struct scsi_write_verify_16 *cdb; 8530 8531 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8532 flags |= CTL_LLF_FUA; 8533 if (cdb->byte2 & SWV_DPO) 8534 flags |= CTL_LLF_DPO; 8535 lba = scsi_8btou64(cdb->addr); 8536 num_blocks = scsi_4btoul(cdb->length); 8537 break; 8538 } 8539 default: 8540 /* 8541 * We got a command we don't support. This shouldn't 8542 * happen, commands should be filtered out above us. 8543 */ 8544 ctl_set_invalid_opcode(ctsio); 8545 ctl_done((union ctl_io *)ctsio); 8546 8547 return (CTL_RETVAL_COMPLETE); 8548 break; /* NOTREACHED */ 8549 } 8550 8551 /* 8552 * The first check is to make sure we're in bounds, the second 8553 * check is to catch wrap-around problems. If the lba + num blocks 8554 * is less than the lba, then we've wrapped around and the block 8555 * range is invalid anyway. 8556 */ 8557 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8558 || ((lba + num_blocks) < lba)) { 8559 ctl_set_lba_out_of_range(ctsio); 8560 ctl_done((union ctl_io *)ctsio); 8561 return (CTL_RETVAL_COMPLETE); 8562 } 8563 8564 /* 8565 * According to SBC-3, a transfer length of 0 is not an error. 8566 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8567 * translates to 256 blocks for those commands. 8568 */ 8569 if (num_blocks == 0) { 8570 ctl_set_success(ctsio); 8571 ctl_done((union ctl_io *)ctsio); 8572 return (CTL_RETVAL_COMPLETE); 8573 } 8574 8575 /* Set FUA and/or DPO if caches are disabled. */ 8576 if (isread) { 8577 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8578 SCP_RCD) != 0) 8579 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8580 } else { 8581 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8582 SCP_WCE) == 0) 8583 flags |= CTL_LLF_FUA; 8584 } 8585 8586 lbalen = (struct ctl_lba_len_flags *) 8587 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8588 lbalen->lba = lba; 8589 lbalen->len = num_blocks; 8590 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8591 8592 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8593 ctsio->kern_rel_offset = 0; 8594 8595 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8596 8597 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8598 8599 return (retval); 8600} 8601 8602static int 8603ctl_cnw_cont(union ctl_io *io) 8604{ 8605 struct ctl_scsiio *ctsio; 8606 struct ctl_lun *lun; 8607 struct ctl_lba_len_flags *lbalen; 8608 int retval; 8609 8610 ctsio = &io->scsiio; 8611 ctsio->io_hdr.status = CTL_STATUS_NONE; 8612 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8613 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8614 lbalen = (struct ctl_lba_len_flags *) 8615 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8616 lbalen->flags &= ~CTL_LLF_COMPARE; 8617 lbalen->flags |= CTL_LLF_WRITE; 8618 8619 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8620 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8621 return (retval); 8622} 8623 8624int 8625ctl_cnw(struct ctl_scsiio *ctsio) 8626{ 8627 struct ctl_lun *lun; 8628 struct ctl_lba_len_flags *lbalen; 8629 uint64_t lba; 8630 uint32_t num_blocks; 8631 int flags, retval; 8632 8633 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8634 8635 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8636 8637 flags = 0; 8638 retval = CTL_RETVAL_COMPLETE; 8639 8640 switch (ctsio->cdb[0]) { 8641 case COMPARE_AND_WRITE: { 8642 struct scsi_compare_and_write *cdb; 8643 8644 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8645 if (cdb->byte2 & SRW10_FUA) 8646 flags |= CTL_LLF_FUA; 8647 if (cdb->byte2 & SRW10_DPO) 8648 flags |= CTL_LLF_DPO; 8649 lba = scsi_8btou64(cdb->addr); 8650 num_blocks = cdb->length; 8651 break; 8652 } 8653 default: 8654 /* 8655 * We got a command we don't support. This shouldn't 8656 * happen, commands should be filtered out above us. 8657 */ 8658 ctl_set_invalid_opcode(ctsio); 8659 ctl_done((union ctl_io *)ctsio); 8660 8661 return (CTL_RETVAL_COMPLETE); 8662 break; /* NOTREACHED */ 8663 } 8664 8665 /* 8666 * The first check is to make sure we're in bounds, the second 8667 * check is to catch wrap-around problems. If the lba + num blocks 8668 * is less than the lba, then we've wrapped around and the block 8669 * range is invalid anyway. 8670 */ 8671 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8672 || ((lba + num_blocks) < lba)) { 8673 ctl_set_lba_out_of_range(ctsio); 8674 ctl_done((union ctl_io *)ctsio); 8675 return (CTL_RETVAL_COMPLETE); 8676 } 8677 8678 /* 8679 * According to SBC-3, a transfer length of 0 is not an error. 8680 */ 8681 if (num_blocks == 0) { 8682 ctl_set_success(ctsio); 8683 ctl_done((union ctl_io *)ctsio); 8684 return (CTL_RETVAL_COMPLETE); 8685 } 8686 8687 /* Set FUA if write cache is disabled. */ 8688 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8689 SCP_WCE) == 0) 8690 flags |= CTL_LLF_FUA; 8691 8692 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8693 ctsio->kern_rel_offset = 0; 8694 8695 /* 8696 * Set the IO_CONT flag, so that if this I/O gets passed to 8697 * ctl_data_submit_done(), it'll get passed back to 8698 * ctl_ctl_cnw_cont() for further processing. 8699 */ 8700 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8701 ctsio->io_cont = ctl_cnw_cont; 8702 8703 lbalen = (struct ctl_lba_len_flags *) 8704 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8705 lbalen->lba = lba; 8706 lbalen->len = num_blocks; 8707 lbalen->flags = CTL_LLF_COMPARE | flags; 8708 8709 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8710 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8711 return (retval); 8712} 8713 8714int 8715ctl_verify(struct ctl_scsiio *ctsio) 8716{ 8717 struct ctl_lun *lun; 8718 struct ctl_lba_len_flags *lbalen; 8719 uint64_t lba; 8720 uint32_t num_blocks; 8721 int bytchk, flags; 8722 int retval; 8723 8724 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8725 8726 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8727 8728 bytchk = 0; 8729 flags = CTL_LLF_FUA; 8730 retval = CTL_RETVAL_COMPLETE; 8731 8732 switch (ctsio->cdb[0]) { 8733 case VERIFY_10: { 8734 struct scsi_verify_10 *cdb; 8735 8736 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8737 if (cdb->byte2 & SVFY_BYTCHK) 8738 bytchk = 1; 8739 if (cdb->byte2 & SVFY_DPO) 8740 flags |= CTL_LLF_DPO; 8741 lba = scsi_4btoul(cdb->addr); 8742 num_blocks = scsi_2btoul(cdb->length); 8743 break; 8744 } 8745 case VERIFY_12: { 8746 struct scsi_verify_12 *cdb; 8747 8748 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8749 if (cdb->byte2 & SVFY_BYTCHK) 8750 bytchk = 1; 8751 if (cdb->byte2 & SVFY_DPO) 8752 flags |= CTL_LLF_DPO; 8753 lba = scsi_4btoul(cdb->addr); 8754 num_blocks = scsi_4btoul(cdb->length); 8755 break; 8756 } 8757 case VERIFY_16: { 8758 struct scsi_rw_16 *cdb; 8759 8760 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8761 if (cdb->byte2 & SVFY_BYTCHK) 8762 bytchk = 1; 8763 if (cdb->byte2 & SVFY_DPO) 8764 flags |= CTL_LLF_DPO; 8765 lba = scsi_8btou64(cdb->addr); 8766 num_blocks = scsi_4btoul(cdb->length); 8767 break; 8768 } 8769 default: 8770 /* 8771 * We got a command we don't support. This shouldn't 8772 * happen, commands should be filtered out above us. 8773 */ 8774 ctl_set_invalid_opcode(ctsio); 8775 ctl_done((union ctl_io *)ctsio); 8776 return (CTL_RETVAL_COMPLETE); 8777 } 8778 8779 /* 8780 * The first check is to make sure we're in bounds, the second 8781 * check is to catch wrap-around problems. If the lba + num blocks 8782 * is less than the lba, then we've wrapped around and the block 8783 * range is invalid anyway. 8784 */ 8785 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8786 || ((lba + num_blocks) < lba)) { 8787 ctl_set_lba_out_of_range(ctsio); 8788 ctl_done((union ctl_io *)ctsio); 8789 return (CTL_RETVAL_COMPLETE); 8790 } 8791 8792 /* 8793 * According to SBC-3, a transfer length of 0 is not an error. 8794 */ 8795 if (num_blocks == 0) { 8796 ctl_set_success(ctsio); 8797 ctl_done((union ctl_io *)ctsio); 8798 return (CTL_RETVAL_COMPLETE); 8799 } 8800 8801 lbalen = (struct ctl_lba_len_flags *) 8802 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8803 lbalen->lba = lba; 8804 lbalen->len = num_blocks; 8805 if (bytchk) { 8806 lbalen->flags = CTL_LLF_COMPARE | flags; 8807 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8808 } else { 8809 lbalen->flags = CTL_LLF_VERIFY | flags; 8810 ctsio->kern_total_len = 0; 8811 } 8812 ctsio->kern_rel_offset = 0; 8813 8814 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 8815 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8816 return (retval); 8817} 8818 8819int 8820ctl_report_luns(struct ctl_scsiio *ctsio) 8821{ 8822 struct ctl_softc *softc = control_softc; 8823 struct scsi_report_luns *cdb; 8824 struct scsi_report_luns_data *lun_data; 8825 struct ctl_lun *lun, *request_lun; 8826 struct ctl_port *port; 8827 int num_luns, retval; 8828 uint32_t alloc_len, lun_datalen; 8829 int num_filled, well_known; 8830 uint32_t initidx, targ_lun_id, lun_id; 8831 8832 retval = CTL_RETVAL_COMPLETE; 8833 well_known = 0; 8834 8835 cdb = (struct scsi_report_luns *)ctsio->cdb; 8836 port = ctl_io_port(&ctsio->io_hdr); 8837 8838 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 8839 8840 mtx_lock(&softc->ctl_lock); 8841 num_luns = 0; 8842 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 8843 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 8844 num_luns++; 8845 } 8846 mtx_unlock(&softc->ctl_lock); 8847 8848 switch (cdb->select_report) { 8849 case RPL_REPORT_DEFAULT: 8850 case RPL_REPORT_ALL: 8851 break; 8852 case RPL_REPORT_WELLKNOWN: 8853 well_known = 1; 8854 num_luns = 0; 8855 break; 8856 default: 8857 ctl_set_invalid_field(ctsio, 8858 /*sks_valid*/ 1, 8859 /*command*/ 1, 8860 /*field*/ 2, 8861 /*bit_valid*/ 0, 8862 /*bit*/ 0); 8863 ctl_done((union ctl_io *)ctsio); 8864 return (retval); 8865 break; /* NOTREACHED */ 8866 } 8867 8868 alloc_len = scsi_4btoul(cdb->length); 8869 /* 8870 * The initiator has to allocate at least 16 bytes for this request, 8871 * so he can at least get the header and the first LUN. Otherwise 8872 * we reject the request (per SPC-3 rev 14, section 6.21). 8873 */ 8874 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 8875 sizeof(struct scsi_report_luns_lundata))) { 8876 ctl_set_invalid_field(ctsio, 8877 /*sks_valid*/ 1, 8878 /*command*/ 1, 8879 /*field*/ 6, 8880 /*bit_valid*/ 0, 8881 /*bit*/ 0); 8882 ctl_done((union ctl_io *)ctsio); 8883 return (retval); 8884 } 8885 8886 request_lun = (struct ctl_lun *) 8887 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8888 8889 lun_datalen = sizeof(*lun_data) + 8890 (num_luns * sizeof(struct scsi_report_luns_lundata)); 8891 8892 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 8893 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 8894 ctsio->kern_sg_entries = 0; 8895 8896 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8897 8898 mtx_lock(&softc->ctl_lock); 8899 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 8900 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 8901 if (lun_id >= CTL_MAX_LUNS) 8902 continue; 8903 lun = softc->ctl_luns[lun_id]; 8904 if (lun == NULL) 8905 continue; 8906 8907 if (targ_lun_id <= 0xff) { 8908 /* 8909 * Peripheral addressing method, bus number 0. 8910 */ 8911 lun_data->luns[num_filled].lundata[0] = 8912 RPL_LUNDATA_ATYP_PERIPH; 8913 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 8914 num_filled++; 8915 } else if (targ_lun_id <= 0x3fff) { 8916 /* 8917 * Flat addressing method. 8918 */ 8919 lun_data->luns[num_filled].lundata[0] = 8920 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 8921 lun_data->luns[num_filled].lundata[1] = 8922 (targ_lun_id & 0xff); 8923 num_filled++; 8924 } else if (targ_lun_id <= 0xffffff) { 8925 /* 8926 * Extended flat addressing method. 8927 */ 8928 lun_data->luns[num_filled].lundata[0] = 8929 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 8930 scsi_ulto3b(targ_lun_id, 8931 &lun_data->luns[num_filled].lundata[1]); 8932 num_filled++; 8933 } else { 8934 printf("ctl_report_luns: bogus LUN number %jd, " 8935 "skipping\n", (intmax_t)targ_lun_id); 8936 } 8937 /* 8938 * According to SPC-3, rev 14 section 6.21: 8939 * 8940 * "The execution of a REPORT LUNS command to any valid and 8941 * installed logical unit shall clear the REPORTED LUNS DATA 8942 * HAS CHANGED unit attention condition for all logical 8943 * units of that target with respect to the requesting 8944 * initiator. A valid and installed logical unit is one 8945 * having a PERIPHERAL QUALIFIER of 000b in the standard 8946 * INQUIRY data (see 6.4.2)." 8947 * 8948 * If request_lun is NULL, the LUN this report luns command 8949 * was issued to is either disabled or doesn't exist. In that 8950 * case, we shouldn't clear any pending lun change unit 8951 * attention. 8952 */ 8953 if (request_lun != NULL) { 8954 mtx_lock(&lun->lun_lock); 8955 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 8956 mtx_unlock(&lun->lun_lock); 8957 } 8958 } 8959 mtx_unlock(&softc->ctl_lock); 8960 8961 /* 8962 * It's quite possible that we've returned fewer LUNs than we allocated 8963 * space for. Trim it. 8964 */ 8965 lun_datalen = sizeof(*lun_data) + 8966 (num_filled * sizeof(struct scsi_report_luns_lundata)); 8967 8968 if (lun_datalen < alloc_len) { 8969 ctsio->residual = alloc_len - lun_datalen; 8970 ctsio->kern_data_len = lun_datalen; 8971 ctsio->kern_total_len = lun_datalen; 8972 } else { 8973 ctsio->residual = 0; 8974 ctsio->kern_data_len = alloc_len; 8975 ctsio->kern_total_len = alloc_len; 8976 } 8977 ctsio->kern_data_resid = 0; 8978 ctsio->kern_rel_offset = 0; 8979 ctsio->kern_sg_entries = 0; 8980 8981 /* 8982 * We set this to the actual data length, regardless of how much 8983 * space we actually have to return results. If the user looks at 8984 * this value, he'll know whether or not he allocated enough space 8985 * and reissue the command if necessary. We don't support well 8986 * known logical units, so if the user asks for that, return none. 8987 */ 8988 scsi_ulto4b(lun_datalen - 8, lun_data->length); 8989 8990 /* 8991 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 8992 * this request. 8993 */ 8994 ctl_set_success(ctsio); 8995 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8996 ctsio->be_move_done = ctl_config_move_done; 8997 ctl_datamove((union ctl_io *)ctsio); 8998 return (retval); 8999} 9000 9001int 9002ctl_request_sense(struct ctl_scsiio *ctsio) 9003{ 9004 struct scsi_request_sense *cdb; 9005 struct scsi_sense_data *sense_ptr; 9006 struct ctl_softc *ctl_softc; 9007 struct ctl_lun *lun; 9008 uint32_t initidx; 9009 int have_error; 9010 scsi_sense_data_type sense_format; 9011 ctl_ua_type ua_type; 9012 9013 cdb = (struct scsi_request_sense *)ctsio->cdb; 9014 9015 ctl_softc = control_softc; 9016 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9017 9018 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9019 9020 /* 9021 * Determine which sense format the user wants. 9022 */ 9023 if (cdb->byte2 & SRS_DESC) 9024 sense_format = SSD_TYPE_DESC; 9025 else 9026 sense_format = SSD_TYPE_FIXED; 9027 9028 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9029 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9030 ctsio->kern_sg_entries = 0; 9031 9032 /* 9033 * struct scsi_sense_data, which is currently set to 256 bytes, is 9034 * larger than the largest allowed value for the length field in the 9035 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9036 */ 9037 ctsio->residual = 0; 9038 ctsio->kern_data_len = cdb->length; 9039 ctsio->kern_total_len = cdb->length; 9040 9041 ctsio->kern_data_resid = 0; 9042 ctsio->kern_rel_offset = 0; 9043 ctsio->kern_sg_entries = 0; 9044 9045 /* 9046 * If we don't have a LUN, we don't have any pending sense. 9047 */ 9048 if (lun == NULL) 9049 goto no_sense; 9050 9051 have_error = 0; 9052 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9053 /* 9054 * Check for pending sense, and then for pending unit attentions. 9055 * Pending sense gets returned first, then pending unit attentions. 9056 */ 9057 mtx_lock(&lun->lun_lock); 9058#ifdef CTL_WITH_CA 9059 if (ctl_is_set(lun->have_ca, initidx)) { 9060 scsi_sense_data_type stored_format; 9061 9062 /* 9063 * Check to see which sense format was used for the stored 9064 * sense data. 9065 */ 9066 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9067 9068 /* 9069 * If the user requested a different sense format than the 9070 * one we stored, then we need to convert it to the other 9071 * format. If we're going from descriptor to fixed format 9072 * sense data, we may lose things in translation, depending 9073 * on what options were used. 9074 * 9075 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9076 * for some reason we'll just copy it out as-is. 9077 */ 9078 if ((stored_format == SSD_TYPE_FIXED) 9079 && (sense_format == SSD_TYPE_DESC)) 9080 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9081 &lun->pending_sense[initidx], 9082 (struct scsi_sense_data_desc *)sense_ptr); 9083 else if ((stored_format == SSD_TYPE_DESC) 9084 && (sense_format == SSD_TYPE_FIXED)) 9085 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9086 &lun->pending_sense[initidx], 9087 (struct scsi_sense_data_fixed *)sense_ptr); 9088 else 9089 memcpy(sense_ptr, &lun->pending_sense[initidx], 9090 MIN(sizeof(*sense_ptr), 9091 sizeof(lun->pending_sense[initidx]))); 9092 9093 ctl_clear_mask(lun->have_ca, initidx); 9094 have_error = 1; 9095 } else 9096#endif 9097 { 9098 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9099 if (ua_type != CTL_UA_NONE) 9100 have_error = 1; 9101 if (ua_type == CTL_UA_LUN_CHANGE) { 9102 mtx_unlock(&lun->lun_lock); 9103 mtx_lock(&ctl_softc->ctl_lock); 9104 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type); 9105 mtx_unlock(&ctl_softc->ctl_lock); 9106 mtx_lock(&lun->lun_lock); 9107 } 9108 9109 } 9110 mtx_unlock(&lun->lun_lock); 9111 9112 /* 9113 * We already have a pending error, return it. 9114 */ 9115 if (have_error != 0) { 9116 /* 9117 * We report the SCSI status as OK, since the status of the 9118 * request sense command itself is OK. 9119 * We report 0 for the sense length, because we aren't doing 9120 * autosense in this case. We're reporting sense as 9121 * parameter data. 9122 */ 9123 ctl_set_success(ctsio); 9124 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9125 ctsio->be_move_done = ctl_config_move_done; 9126 ctl_datamove((union ctl_io *)ctsio); 9127 return (CTL_RETVAL_COMPLETE); 9128 } 9129 9130no_sense: 9131 9132 /* 9133 * No sense information to report, so we report that everything is 9134 * okay. 9135 */ 9136 ctl_set_sense_data(sense_ptr, 9137 lun, 9138 sense_format, 9139 /*current_error*/ 1, 9140 /*sense_key*/ SSD_KEY_NO_SENSE, 9141 /*asc*/ 0x00, 9142 /*ascq*/ 0x00, 9143 SSD_ELEM_NONE); 9144 9145 /* 9146 * We report 0 for the sense length, because we aren't doing 9147 * autosense in this case. We're reporting sense as parameter data. 9148 */ 9149 ctl_set_success(ctsio); 9150 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9151 ctsio->be_move_done = ctl_config_move_done; 9152 ctl_datamove((union ctl_io *)ctsio); 9153 return (CTL_RETVAL_COMPLETE); 9154} 9155 9156int 9157ctl_tur(struct ctl_scsiio *ctsio) 9158{ 9159 9160 CTL_DEBUG_PRINT(("ctl_tur\n")); 9161 9162 ctl_set_success(ctsio); 9163 ctl_done((union ctl_io *)ctsio); 9164 9165 return (CTL_RETVAL_COMPLETE); 9166} 9167 9168#ifdef notyet 9169static int 9170ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9171{ 9172 9173} 9174#endif 9175 9176/* 9177 * SCSI VPD page 0x00, the Supported VPD Pages page. 9178 */ 9179static int 9180ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9181{ 9182 struct scsi_vpd_supported_pages *pages; 9183 int sup_page_size; 9184 struct ctl_lun *lun; 9185 int p; 9186 9187 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9188 9189 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9190 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9191 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9192 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9193 ctsio->kern_sg_entries = 0; 9194 9195 if (sup_page_size < alloc_len) { 9196 ctsio->residual = alloc_len - sup_page_size; 9197 ctsio->kern_data_len = sup_page_size; 9198 ctsio->kern_total_len = sup_page_size; 9199 } else { 9200 ctsio->residual = 0; 9201 ctsio->kern_data_len = alloc_len; 9202 ctsio->kern_total_len = alloc_len; 9203 } 9204 ctsio->kern_data_resid = 0; 9205 ctsio->kern_rel_offset = 0; 9206 ctsio->kern_sg_entries = 0; 9207 9208 /* 9209 * The control device is always connected. The disk device, on the 9210 * other hand, may not be online all the time. Need to change this 9211 * to figure out whether the disk device is actually online or not. 9212 */ 9213 if (lun != NULL) 9214 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9215 lun->be_lun->lun_type; 9216 else 9217 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9218 9219 p = 0; 9220 /* Supported VPD pages */ 9221 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9222 /* Serial Number */ 9223 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9224 /* Device Identification */ 9225 pages->page_list[p++] = SVPD_DEVICE_ID; 9226 /* Extended INQUIRY Data */ 9227 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9228 /* Mode Page Policy */ 9229 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9230 /* SCSI Ports */ 9231 pages->page_list[p++] = SVPD_SCSI_PORTS; 9232 /* Third-party Copy */ 9233 pages->page_list[p++] = SVPD_SCSI_TPC; 9234 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9235 /* Block limits */ 9236 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9237 /* Block Device Characteristics */ 9238 pages->page_list[p++] = SVPD_BDC; 9239 /* Logical Block Provisioning */ 9240 pages->page_list[p++] = SVPD_LBP; 9241 } 9242 pages->length = p; 9243 9244 ctl_set_success(ctsio); 9245 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9246 ctsio->be_move_done = ctl_config_move_done; 9247 ctl_datamove((union ctl_io *)ctsio); 9248 return (CTL_RETVAL_COMPLETE); 9249} 9250 9251/* 9252 * SCSI VPD page 0x80, the Unit Serial Number page. 9253 */ 9254static int 9255ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9256{ 9257 struct scsi_vpd_unit_serial_number *sn_ptr; 9258 struct ctl_lun *lun; 9259 int data_len; 9260 9261 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9262 9263 data_len = 4 + CTL_SN_LEN; 9264 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9265 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9266 if (data_len < alloc_len) { 9267 ctsio->residual = alloc_len - data_len; 9268 ctsio->kern_data_len = data_len; 9269 ctsio->kern_total_len = data_len; 9270 } else { 9271 ctsio->residual = 0; 9272 ctsio->kern_data_len = alloc_len; 9273 ctsio->kern_total_len = alloc_len; 9274 } 9275 ctsio->kern_data_resid = 0; 9276 ctsio->kern_rel_offset = 0; 9277 ctsio->kern_sg_entries = 0; 9278 9279 /* 9280 * The control device is always connected. The disk device, on the 9281 * other hand, may not be online all the time. Need to change this 9282 * to figure out whether the disk device is actually online or not. 9283 */ 9284 if (lun != NULL) 9285 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9286 lun->be_lun->lun_type; 9287 else 9288 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9289 9290 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9291 sn_ptr->length = CTL_SN_LEN; 9292 /* 9293 * If we don't have a LUN, we just leave the serial number as 9294 * all spaces. 9295 */ 9296 if (lun != NULL) { 9297 strncpy((char *)sn_ptr->serial_num, 9298 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9299 } else 9300 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9301 9302 ctl_set_success(ctsio); 9303 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9304 ctsio->be_move_done = ctl_config_move_done; 9305 ctl_datamove((union ctl_io *)ctsio); 9306 return (CTL_RETVAL_COMPLETE); 9307} 9308 9309 9310/* 9311 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9312 */ 9313static int 9314ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9315{ 9316 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9317 struct ctl_lun *lun; 9318 int data_len; 9319 9320 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9321 9322 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9323 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9324 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9325 ctsio->kern_sg_entries = 0; 9326 9327 if (data_len < alloc_len) { 9328 ctsio->residual = alloc_len - data_len; 9329 ctsio->kern_data_len = data_len; 9330 ctsio->kern_total_len = data_len; 9331 } else { 9332 ctsio->residual = 0; 9333 ctsio->kern_data_len = alloc_len; 9334 ctsio->kern_total_len = alloc_len; 9335 } 9336 ctsio->kern_data_resid = 0; 9337 ctsio->kern_rel_offset = 0; 9338 ctsio->kern_sg_entries = 0; 9339 9340 /* 9341 * The control device is always connected. The disk device, on the 9342 * other hand, may not be online all the time. 9343 */ 9344 if (lun != NULL) 9345 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9346 lun->be_lun->lun_type; 9347 else 9348 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9349 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9350 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9351 /* 9352 * We support head of queue, ordered and simple tags. 9353 */ 9354 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9355 /* 9356 * Volatile cache supported. 9357 */ 9358 eid_ptr->flags3 = SVPD_EID_V_SUP; 9359 9360 /* 9361 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9362 * attention for a particular IT nexus on all LUNs once we report 9363 * it to that nexus once. This bit is required as of SPC-4. 9364 */ 9365 eid_ptr->flags4 = SVPD_EID_LUICLT; 9366 9367 /* 9368 * XXX KDM in order to correctly answer this, we would need 9369 * information from the SIM to determine how much sense data it 9370 * can send. So this would really be a path inquiry field, most 9371 * likely. This can be set to a maximum of 252 according to SPC-4, 9372 * but the hardware may or may not be able to support that much. 9373 * 0 just means that the maximum sense data length is not reported. 9374 */ 9375 eid_ptr->max_sense_length = 0; 9376 9377 ctl_set_success(ctsio); 9378 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9379 ctsio->be_move_done = ctl_config_move_done; 9380 ctl_datamove((union ctl_io *)ctsio); 9381 return (CTL_RETVAL_COMPLETE); 9382} 9383 9384static int 9385ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9386{ 9387 struct scsi_vpd_mode_page_policy *mpp_ptr; 9388 struct ctl_lun *lun; 9389 int data_len; 9390 9391 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9392 9393 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9394 sizeof(struct scsi_vpd_mode_page_policy_descr); 9395 9396 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9397 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9398 ctsio->kern_sg_entries = 0; 9399 9400 if (data_len < alloc_len) { 9401 ctsio->residual = alloc_len - data_len; 9402 ctsio->kern_data_len = data_len; 9403 ctsio->kern_total_len = data_len; 9404 } else { 9405 ctsio->residual = 0; 9406 ctsio->kern_data_len = alloc_len; 9407 ctsio->kern_total_len = alloc_len; 9408 } 9409 ctsio->kern_data_resid = 0; 9410 ctsio->kern_rel_offset = 0; 9411 ctsio->kern_sg_entries = 0; 9412 9413 /* 9414 * The control device is always connected. The disk device, on the 9415 * other hand, may not be online all the time. 9416 */ 9417 if (lun != NULL) 9418 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9419 lun->be_lun->lun_type; 9420 else 9421 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9422 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9423 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9424 mpp_ptr->descr[0].page_code = 0x3f; 9425 mpp_ptr->descr[0].subpage_code = 0xff; 9426 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9427 9428 ctl_set_success(ctsio); 9429 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9430 ctsio->be_move_done = ctl_config_move_done; 9431 ctl_datamove((union ctl_io *)ctsio); 9432 return (CTL_RETVAL_COMPLETE); 9433} 9434 9435/* 9436 * SCSI VPD page 0x83, the Device Identification page. 9437 */ 9438static int 9439ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9440{ 9441 struct scsi_vpd_device_id *devid_ptr; 9442 struct scsi_vpd_id_descriptor *desc; 9443 struct ctl_softc *softc; 9444 struct ctl_lun *lun; 9445 struct ctl_port *port; 9446 int data_len; 9447 uint8_t proto; 9448 9449 softc = control_softc; 9450 9451 port = softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9452 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9453 9454 data_len = sizeof(struct scsi_vpd_device_id) + 9455 sizeof(struct scsi_vpd_id_descriptor) + 9456 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9457 sizeof(struct scsi_vpd_id_descriptor) + 9458 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9459 if (lun && lun->lun_devid) 9460 data_len += lun->lun_devid->len; 9461 if (port->port_devid) 9462 data_len += port->port_devid->len; 9463 if (port->target_devid) 9464 data_len += port->target_devid->len; 9465 9466 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9467 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9468 ctsio->kern_sg_entries = 0; 9469 9470 if (data_len < alloc_len) { 9471 ctsio->residual = alloc_len - data_len; 9472 ctsio->kern_data_len = data_len; 9473 ctsio->kern_total_len = data_len; 9474 } else { 9475 ctsio->residual = 0; 9476 ctsio->kern_data_len = alloc_len; 9477 ctsio->kern_total_len = alloc_len; 9478 } 9479 ctsio->kern_data_resid = 0; 9480 ctsio->kern_rel_offset = 0; 9481 ctsio->kern_sg_entries = 0; 9482 9483 /* 9484 * The control device is always connected. The disk device, on the 9485 * other hand, may not be online all the time. 9486 */ 9487 if (lun != NULL) 9488 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9489 lun->be_lun->lun_type; 9490 else 9491 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9492 devid_ptr->page_code = SVPD_DEVICE_ID; 9493 scsi_ulto2b(data_len - 4, devid_ptr->length); 9494 9495 if (port->port_type == CTL_PORT_FC) 9496 proto = SCSI_PROTO_FC << 4; 9497 else if (port->port_type == CTL_PORT_ISCSI) 9498 proto = SCSI_PROTO_ISCSI << 4; 9499 else 9500 proto = SCSI_PROTO_SPI << 4; 9501 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9502 9503 /* 9504 * We're using a LUN association here. i.e., this device ID is a 9505 * per-LUN identifier. 9506 */ 9507 if (lun && lun->lun_devid) { 9508 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9509 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9510 lun->lun_devid->len); 9511 } 9512 9513 /* 9514 * This is for the WWPN which is a port association. 9515 */ 9516 if (port->port_devid) { 9517 memcpy(desc, port->port_devid->data, port->port_devid->len); 9518 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9519 port->port_devid->len); 9520 } 9521 9522 /* 9523 * This is for the Relative Target Port(type 4h) identifier 9524 */ 9525 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9526 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9527 SVPD_ID_TYPE_RELTARG; 9528 desc->length = 4; 9529 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9530 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9531 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9532 9533 /* 9534 * This is for the Target Port Group(type 5h) identifier 9535 */ 9536 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9537 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9538 SVPD_ID_TYPE_TPORTGRP; 9539 desc->length = 4; 9540 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9541 &desc->identifier[2]); 9542 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9543 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9544 9545 /* 9546 * This is for the Target identifier 9547 */ 9548 if (port->target_devid) { 9549 memcpy(desc, port->target_devid->data, port->target_devid->len); 9550 } 9551 9552 ctl_set_success(ctsio); 9553 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9554 ctsio->be_move_done = ctl_config_move_done; 9555 ctl_datamove((union ctl_io *)ctsio); 9556 return (CTL_RETVAL_COMPLETE); 9557} 9558 9559static int 9560ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9561{ 9562 struct ctl_softc *softc = control_softc; 9563 struct scsi_vpd_scsi_ports *sp; 9564 struct scsi_vpd_port_designation *pd; 9565 struct scsi_vpd_port_designation_cont *pdc; 9566 struct ctl_lun *lun; 9567 struct ctl_port *port; 9568 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 9569 int num_target_port_groups; 9570 9571 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9572 9573 if (softc->is_single) 9574 num_target_port_groups = 1; 9575 else 9576 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 9577 num_target_ports = 0; 9578 iid_len = 0; 9579 id_len = 0; 9580 mtx_lock(&softc->ctl_lock); 9581 STAILQ_FOREACH(port, &softc->port_list, links) { 9582 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9583 continue; 9584 if (lun != NULL && 9585 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9586 continue; 9587 num_target_ports++; 9588 if (port->init_devid) 9589 iid_len += port->init_devid->len; 9590 if (port->port_devid) 9591 id_len += port->port_devid->len; 9592 } 9593 mtx_unlock(&softc->ctl_lock); 9594 9595 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 9596 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9597 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9598 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9599 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9600 ctsio->kern_sg_entries = 0; 9601 9602 if (data_len < alloc_len) { 9603 ctsio->residual = alloc_len - data_len; 9604 ctsio->kern_data_len = data_len; 9605 ctsio->kern_total_len = data_len; 9606 } else { 9607 ctsio->residual = 0; 9608 ctsio->kern_data_len = alloc_len; 9609 ctsio->kern_total_len = alloc_len; 9610 } 9611 ctsio->kern_data_resid = 0; 9612 ctsio->kern_rel_offset = 0; 9613 ctsio->kern_sg_entries = 0; 9614 9615 /* 9616 * The control device is always connected. The disk device, on the 9617 * other hand, may not be online all the time. Need to change this 9618 * to figure out whether the disk device is actually online or not. 9619 */ 9620 if (lun != NULL) 9621 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9622 lun->be_lun->lun_type; 9623 else 9624 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9625 9626 sp->page_code = SVPD_SCSI_PORTS; 9627 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9628 sp->page_length); 9629 pd = &sp->design[0]; 9630 9631 mtx_lock(&softc->ctl_lock); 9632 pg = softc->port_offset / CTL_MAX_PORTS; 9633 for (g = 0; g < num_target_port_groups; g++) { 9634 STAILQ_FOREACH(port, &softc->port_list, links) { 9635 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9636 continue; 9637 if (lun != NULL && 9638 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9639 continue; 9640 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 9641 scsi_ulto2b(p, pd->relative_port_id); 9642 if (port->init_devid && g == pg) { 9643 iid_len = port->init_devid->len; 9644 memcpy(pd->initiator_transportid, 9645 port->init_devid->data, port->init_devid->len); 9646 } else 9647 iid_len = 0; 9648 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9649 pdc = (struct scsi_vpd_port_designation_cont *) 9650 (&pd->initiator_transportid[iid_len]); 9651 if (port->port_devid && g == pg) { 9652 id_len = port->port_devid->len; 9653 memcpy(pdc->target_port_descriptors, 9654 port->port_devid->data, port->port_devid->len); 9655 } else 9656 id_len = 0; 9657 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9658 pd = (struct scsi_vpd_port_designation *) 9659 ((uint8_t *)pdc->target_port_descriptors + id_len); 9660 } 9661 } 9662 mtx_unlock(&softc->ctl_lock); 9663 9664 ctl_set_success(ctsio); 9665 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9666 ctsio->be_move_done = ctl_config_move_done; 9667 ctl_datamove((union ctl_io *)ctsio); 9668 return (CTL_RETVAL_COMPLETE); 9669} 9670 9671static int 9672ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9673{ 9674 struct scsi_vpd_block_limits *bl_ptr; 9675 struct ctl_lun *lun; 9676 int bs; 9677 9678 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9679 9680 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9681 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9682 ctsio->kern_sg_entries = 0; 9683 9684 if (sizeof(*bl_ptr) < alloc_len) { 9685 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9686 ctsio->kern_data_len = sizeof(*bl_ptr); 9687 ctsio->kern_total_len = sizeof(*bl_ptr); 9688 } else { 9689 ctsio->residual = 0; 9690 ctsio->kern_data_len = alloc_len; 9691 ctsio->kern_total_len = alloc_len; 9692 } 9693 ctsio->kern_data_resid = 0; 9694 ctsio->kern_rel_offset = 0; 9695 ctsio->kern_sg_entries = 0; 9696 9697 /* 9698 * The control device is always connected. The disk device, on the 9699 * other hand, may not be online all the time. Need to change this 9700 * to figure out whether the disk device is actually online or not. 9701 */ 9702 if (lun != NULL) 9703 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9704 lun->be_lun->lun_type; 9705 else 9706 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9707 9708 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9709 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9710 bl_ptr->max_cmp_write_len = 0xff; 9711 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9712 if (lun != NULL) { 9713 bs = lun->be_lun->blocksize; 9714 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9715 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9716 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 9717 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 9718 if (lun->be_lun->ublockexp != 0) { 9719 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9720 bl_ptr->opt_unmap_grain); 9721 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9722 bl_ptr->unmap_grain_align); 9723 } 9724 } 9725 scsi_ulto4b(lun->be_lun->atomicblock, 9726 bl_ptr->max_atomic_transfer_length); 9727 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9728 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9729 } 9730 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 9731 9732 ctl_set_success(ctsio); 9733 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9734 ctsio->be_move_done = ctl_config_move_done; 9735 ctl_datamove((union ctl_io *)ctsio); 9736 return (CTL_RETVAL_COMPLETE); 9737} 9738 9739static int 9740ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9741{ 9742 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9743 struct ctl_lun *lun; 9744 const char *value; 9745 u_int i; 9746 9747 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9748 9749 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9750 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9751 ctsio->kern_sg_entries = 0; 9752 9753 if (sizeof(*bdc_ptr) < alloc_len) { 9754 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9755 ctsio->kern_data_len = sizeof(*bdc_ptr); 9756 ctsio->kern_total_len = sizeof(*bdc_ptr); 9757 } else { 9758 ctsio->residual = 0; 9759 ctsio->kern_data_len = alloc_len; 9760 ctsio->kern_total_len = alloc_len; 9761 } 9762 ctsio->kern_data_resid = 0; 9763 ctsio->kern_rel_offset = 0; 9764 ctsio->kern_sg_entries = 0; 9765 9766 /* 9767 * The control device is always connected. The disk device, on the 9768 * other hand, may not be online all the time. Need to change this 9769 * to figure out whether the disk device is actually online or not. 9770 */ 9771 if (lun != NULL) 9772 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9773 lun->be_lun->lun_type; 9774 else 9775 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9776 bdc_ptr->page_code = SVPD_BDC; 9777 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9778 if (lun != NULL && 9779 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9780 i = strtol(value, NULL, 0); 9781 else 9782 i = CTL_DEFAULT_ROTATION_RATE; 9783 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9784 if (lun != NULL && 9785 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9786 i = strtol(value, NULL, 0); 9787 else 9788 i = 0; 9789 bdc_ptr->wab_wac_ff = (i & 0x0f); 9790 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9791 9792 ctl_set_success(ctsio); 9793 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9794 ctsio->be_move_done = ctl_config_move_done; 9795 ctl_datamove((union ctl_io *)ctsio); 9796 return (CTL_RETVAL_COMPLETE); 9797} 9798 9799static int 9800ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9801{ 9802 struct scsi_vpd_logical_block_prov *lbp_ptr; 9803 struct ctl_lun *lun; 9804 9805 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9806 9807 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9808 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9809 ctsio->kern_sg_entries = 0; 9810 9811 if (sizeof(*lbp_ptr) < alloc_len) { 9812 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9813 ctsio->kern_data_len = sizeof(*lbp_ptr); 9814 ctsio->kern_total_len = sizeof(*lbp_ptr); 9815 } else { 9816 ctsio->residual = 0; 9817 ctsio->kern_data_len = alloc_len; 9818 ctsio->kern_total_len = alloc_len; 9819 } 9820 ctsio->kern_data_resid = 0; 9821 ctsio->kern_rel_offset = 0; 9822 ctsio->kern_sg_entries = 0; 9823 9824 /* 9825 * The control device is always connected. The disk device, on the 9826 * other hand, may not be online all the time. Need to change this 9827 * to figure out whether the disk device is actually online or not. 9828 */ 9829 if (lun != NULL) 9830 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9831 lun->be_lun->lun_type; 9832 else 9833 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9834 9835 lbp_ptr->page_code = SVPD_LBP; 9836 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9837 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9838 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9839 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9840 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9841 lbp_ptr->prov_type = SVPD_LBP_THIN; 9842 } 9843 9844 ctl_set_success(ctsio); 9845 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9846 ctsio->be_move_done = ctl_config_move_done; 9847 ctl_datamove((union ctl_io *)ctsio); 9848 return (CTL_RETVAL_COMPLETE); 9849} 9850 9851/* 9852 * INQUIRY with the EVPD bit set. 9853 */ 9854static int 9855ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9856{ 9857 struct ctl_lun *lun; 9858 struct scsi_inquiry *cdb; 9859 int alloc_len, retval; 9860 9861 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9862 cdb = (struct scsi_inquiry *)ctsio->cdb; 9863 alloc_len = scsi_2btoul(cdb->length); 9864 9865 switch (cdb->page_code) { 9866 case SVPD_SUPPORTED_PAGES: 9867 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9868 break; 9869 case SVPD_UNIT_SERIAL_NUMBER: 9870 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9871 break; 9872 case SVPD_DEVICE_ID: 9873 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9874 break; 9875 case SVPD_EXTENDED_INQUIRY_DATA: 9876 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 9877 break; 9878 case SVPD_MODE_PAGE_POLICY: 9879 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 9880 break; 9881 case SVPD_SCSI_PORTS: 9882 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 9883 break; 9884 case SVPD_SCSI_TPC: 9885 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 9886 break; 9887 case SVPD_BLOCK_LIMITS: 9888 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9889 goto err; 9890 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 9891 break; 9892 case SVPD_BDC: 9893 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9894 goto err; 9895 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 9896 break; 9897 case SVPD_LBP: 9898 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9899 goto err; 9900 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 9901 break; 9902 default: 9903err: 9904 ctl_set_invalid_field(ctsio, 9905 /*sks_valid*/ 1, 9906 /*command*/ 1, 9907 /*field*/ 2, 9908 /*bit_valid*/ 0, 9909 /*bit*/ 0); 9910 ctl_done((union ctl_io *)ctsio); 9911 retval = CTL_RETVAL_COMPLETE; 9912 break; 9913 } 9914 9915 return (retval); 9916} 9917 9918/* 9919 * Standard INQUIRY data. 9920 */ 9921static int 9922ctl_inquiry_std(struct ctl_scsiio *ctsio) 9923{ 9924 struct scsi_inquiry_data *inq_ptr; 9925 struct scsi_inquiry *cdb; 9926 struct ctl_softc *softc; 9927 struct ctl_lun *lun; 9928 char *val; 9929 uint32_t alloc_len, data_len; 9930 ctl_port_type port_type; 9931 9932 softc = control_softc; 9933 9934 /* 9935 * Figure out whether we're talking to a Fibre Channel port or not. 9936 * We treat the ioctl front end, and any SCSI adapters, as packetized 9937 * SCSI front ends. 9938 */ 9939 port_type = softc->ctl_ports[ 9940 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 9941 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 9942 port_type = CTL_PORT_SCSI; 9943 9944 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9945 cdb = (struct scsi_inquiry *)ctsio->cdb; 9946 alloc_len = scsi_2btoul(cdb->length); 9947 9948 /* 9949 * We malloc the full inquiry data size here and fill it 9950 * in. If the user only asks for less, we'll give him 9951 * that much. 9952 */ 9953 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 9954 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9955 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 9956 ctsio->kern_sg_entries = 0; 9957 ctsio->kern_data_resid = 0; 9958 ctsio->kern_rel_offset = 0; 9959 9960 if (data_len < alloc_len) { 9961 ctsio->residual = alloc_len - data_len; 9962 ctsio->kern_data_len = data_len; 9963 ctsio->kern_total_len = data_len; 9964 } else { 9965 ctsio->residual = 0; 9966 ctsio->kern_data_len = alloc_len; 9967 ctsio->kern_total_len = alloc_len; 9968 } 9969 9970 /* 9971 * If we have a LUN configured, report it as connected. Otherwise, 9972 * report that it is offline or no device is supported, depending 9973 * on the value of inquiry_pq_no_lun. 9974 * 9975 * According to the spec (SPC-4 r34), the peripheral qualifier 9976 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 9977 * 9978 * "A peripheral device having the specified peripheral device type 9979 * is not connected to this logical unit. However, the device 9980 * server is capable of supporting the specified peripheral device 9981 * type on this logical unit." 9982 * 9983 * According to the same spec, the peripheral qualifier 9984 * SID_QUAL_BAD_LU (011b) is used in this scenario: 9985 * 9986 * "The device server is not capable of supporting a peripheral 9987 * device on this logical unit. For this peripheral qualifier the 9988 * peripheral device type shall be set to 1Fh. All other peripheral 9989 * device type values are reserved for this peripheral qualifier." 9990 * 9991 * Given the text, it would seem that we probably want to report that 9992 * the LUN is offline here. There is no LUN connected, but we can 9993 * support a LUN at the given LUN number. 9994 * 9995 * In the real world, though, it sounds like things are a little 9996 * different: 9997 * 9998 * - Linux, when presented with a LUN with the offline peripheral 9999 * qualifier, will create an sg driver instance for it. So when 10000 * you attach it to CTL, you wind up with a ton of sg driver 10001 * instances. (One for every LUN that Linux bothered to probe.) 10002 * Linux does this despite the fact that it issues a REPORT LUNs 10003 * to LUN 0 to get the inventory of supported LUNs. 10004 * 10005 * - There is other anecdotal evidence (from Emulex folks) about 10006 * arrays that use the offline peripheral qualifier for LUNs that 10007 * are on the "passive" path in an active/passive array. 10008 * 10009 * So the solution is provide a hopefully reasonable default 10010 * (return bad/no LUN) and allow the user to change the behavior 10011 * with a tunable/sysctl variable. 10012 */ 10013 if (lun != NULL) 10014 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10015 lun->be_lun->lun_type; 10016 else if (softc->inquiry_pq_no_lun == 0) 10017 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10018 else 10019 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10020 10021 /* RMB in byte 2 is 0 */ 10022 inq_ptr->version = SCSI_REV_SPC4; 10023 10024 /* 10025 * According to SAM-3, even if a device only supports a single 10026 * level of LUN addressing, it should still set the HISUP bit: 10027 * 10028 * 4.9.1 Logical unit numbers overview 10029 * 10030 * All logical unit number formats described in this standard are 10031 * hierarchical in structure even when only a single level in that 10032 * hierarchy is used. The HISUP bit shall be set to one in the 10033 * standard INQUIRY data (see SPC-2) when any logical unit number 10034 * format described in this standard is used. Non-hierarchical 10035 * formats are outside the scope of this standard. 10036 * 10037 * Therefore we set the HiSup bit here. 10038 * 10039 * The reponse format is 2, per SPC-3. 10040 */ 10041 inq_ptr->response_format = SID_HiSup | 2; 10042 10043 inq_ptr->additional_length = data_len - 10044 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10045 CTL_DEBUG_PRINT(("additional_length = %d\n", 10046 inq_ptr->additional_length)); 10047 10048 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10049 /* 16 bit addressing */ 10050 if (port_type == CTL_PORT_SCSI) 10051 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10052 /* XXX set the SID_MultiP bit here if we're actually going to 10053 respond on multiple ports */ 10054 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10055 10056 /* 16 bit data bus, synchronous transfers */ 10057 if (port_type == CTL_PORT_SCSI) 10058 inq_ptr->flags = SID_WBus16 | SID_Sync; 10059 /* 10060 * XXX KDM do we want to support tagged queueing on the control 10061 * device at all? 10062 */ 10063 if ((lun == NULL) 10064 || (lun->be_lun->lun_type != T_PROCESSOR)) 10065 inq_ptr->flags |= SID_CmdQue; 10066 /* 10067 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10068 * We have 8 bytes for the vendor name, and 16 bytes for the device 10069 * name and 4 bytes for the revision. 10070 */ 10071 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10072 "vendor")) == NULL) { 10073 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10074 } else { 10075 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10076 strncpy(inq_ptr->vendor, val, 10077 min(sizeof(inq_ptr->vendor), strlen(val))); 10078 } 10079 if (lun == NULL) { 10080 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10081 sizeof(inq_ptr->product)); 10082 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10083 switch (lun->be_lun->lun_type) { 10084 case T_DIRECT: 10085 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10086 sizeof(inq_ptr->product)); 10087 break; 10088 case T_PROCESSOR: 10089 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10090 sizeof(inq_ptr->product)); 10091 break; 10092 default: 10093 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10094 sizeof(inq_ptr->product)); 10095 break; 10096 } 10097 } else { 10098 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10099 strncpy(inq_ptr->product, val, 10100 min(sizeof(inq_ptr->product), strlen(val))); 10101 } 10102 10103 /* 10104 * XXX make this a macro somewhere so it automatically gets 10105 * incremented when we make changes. 10106 */ 10107 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10108 "revision")) == NULL) { 10109 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10110 } else { 10111 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10112 strncpy(inq_ptr->revision, val, 10113 min(sizeof(inq_ptr->revision), strlen(val))); 10114 } 10115 10116 /* 10117 * For parallel SCSI, we support double transition and single 10118 * transition clocking. We also support QAS (Quick Arbitration 10119 * and Selection) and Information Unit transfers on both the 10120 * control and array devices. 10121 */ 10122 if (port_type == CTL_PORT_SCSI) 10123 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10124 SID_SPI_IUS; 10125 10126 /* SAM-5 (no version claimed) */ 10127 scsi_ulto2b(0x00A0, inq_ptr->version1); 10128 /* SPC-4 (no version claimed) */ 10129 scsi_ulto2b(0x0460, inq_ptr->version2); 10130 if (port_type == CTL_PORT_FC) { 10131 /* FCP-2 ANSI INCITS.350:2003 */ 10132 scsi_ulto2b(0x0917, inq_ptr->version3); 10133 } else if (port_type == CTL_PORT_SCSI) { 10134 /* SPI-4 ANSI INCITS.362:200x */ 10135 scsi_ulto2b(0x0B56, inq_ptr->version3); 10136 } else if (port_type == CTL_PORT_ISCSI) { 10137 /* iSCSI (no version claimed) */ 10138 scsi_ulto2b(0x0960, inq_ptr->version3); 10139 } else if (port_type == CTL_PORT_SAS) { 10140 /* SAS (no version claimed) */ 10141 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10142 } 10143 10144 if (lun == NULL) { 10145 /* SBC-4 (no version claimed) */ 10146 scsi_ulto2b(0x0600, inq_ptr->version4); 10147 } else { 10148 switch (lun->be_lun->lun_type) { 10149 case T_DIRECT: 10150 /* SBC-4 (no version claimed) */ 10151 scsi_ulto2b(0x0600, inq_ptr->version4); 10152 break; 10153 case T_PROCESSOR: 10154 default: 10155 break; 10156 } 10157 } 10158 10159 ctl_set_success(ctsio); 10160 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10161 ctsio->be_move_done = ctl_config_move_done; 10162 ctl_datamove((union ctl_io *)ctsio); 10163 return (CTL_RETVAL_COMPLETE); 10164} 10165 10166int 10167ctl_inquiry(struct ctl_scsiio *ctsio) 10168{ 10169 struct scsi_inquiry *cdb; 10170 int retval; 10171 10172 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10173 10174 cdb = (struct scsi_inquiry *)ctsio->cdb; 10175 if (cdb->byte2 & SI_EVPD) 10176 retval = ctl_inquiry_evpd(ctsio); 10177 else if (cdb->page_code == 0) 10178 retval = ctl_inquiry_std(ctsio); 10179 else { 10180 ctl_set_invalid_field(ctsio, 10181 /*sks_valid*/ 1, 10182 /*command*/ 1, 10183 /*field*/ 2, 10184 /*bit_valid*/ 0, 10185 /*bit*/ 0); 10186 ctl_done((union ctl_io *)ctsio); 10187 return (CTL_RETVAL_COMPLETE); 10188 } 10189 10190 return (retval); 10191} 10192 10193/* 10194 * For known CDB types, parse the LBA and length. 10195 */ 10196static int 10197ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10198{ 10199 if (io->io_hdr.io_type != CTL_IO_SCSI) 10200 return (1); 10201 10202 switch (io->scsiio.cdb[0]) { 10203 case COMPARE_AND_WRITE: { 10204 struct scsi_compare_and_write *cdb; 10205 10206 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10207 10208 *lba = scsi_8btou64(cdb->addr); 10209 *len = cdb->length; 10210 break; 10211 } 10212 case READ_6: 10213 case WRITE_6: { 10214 struct scsi_rw_6 *cdb; 10215 10216 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10217 10218 *lba = scsi_3btoul(cdb->addr); 10219 /* only 5 bits are valid in the most significant address byte */ 10220 *lba &= 0x1fffff; 10221 *len = cdb->length; 10222 break; 10223 } 10224 case READ_10: 10225 case WRITE_10: { 10226 struct scsi_rw_10 *cdb; 10227 10228 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10229 10230 *lba = scsi_4btoul(cdb->addr); 10231 *len = scsi_2btoul(cdb->length); 10232 break; 10233 } 10234 case WRITE_VERIFY_10: { 10235 struct scsi_write_verify_10 *cdb; 10236 10237 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10238 10239 *lba = scsi_4btoul(cdb->addr); 10240 *len = scsi_2btoul(cdb->length); 10241 break; 10242 } 10243 case READ_12: 10244 case WRITE_12: { 10245 struct scsi_rw_12 *cdb; 10246 10247 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10248 10249 *lba = scsi_4btoul(cdb->addr); 10250 *len = scsi_4btoul(cdb->length); 10251 break; 10252 } 10253 case WRITE_VERIFY_12: { 10254 struct scsi_write_verify_12 *cdb; 10255 10256 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10257 10258 *lba = scsi_4btoul(cdb->addr); 10259 *len = scsi_4btoul(cdb->length); 10260 break; 10261 } 10262 case READ_16: 10263 case WRITE_16: 10264 case WRITE_ATOMIC_16: { 10265 struct scsi_rw_16 *cdb; 10266 10267 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10268 10269 *lba = scsi_8btou64(cdb->addr); 10270 *len = scsi_4btoul(cdb->length); 10271 break; 10272 } 10273 case WRITE_VERIFY_16: { 10274 struct scsi_write_verify_16 *cdb; 10275 10276 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10277 10278 *lba = scsi_8btou64(cdb->addr); 10279 *len = scsi_4btoul(cdb->length); 10280 break; 10281 } 10282 case WRITE_SAME_10: { 10283 struct scsi_write_same_10 *cdb; 10284 10285 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10286 10287 *lba = scsi_4btoul(cdb->addr); 10288 *len = scsi_2btoul(cdb->length); 10289 break; 10290 } 10291 case WRITE_SAME_16: { 10292 struct scsi_write_same_16 *cdb; 10293 10294 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10295 10296 *lba = scsi_8btou64(cdb->addr); 10297 *len = scsi_4btoul(cdb->length); 10298 break; 10299 } 10300 case VERIFY_10: { 10301 struct scsi_verify_10 *cdb; 10302 10303 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10304 10305 *lba = scsi_4btoul(cdb->addr); 10306 *len = scsi_2btoul(cdb->length); 10307 break; 10308 } 10309 case VERIFY_12: { 10310 struct scsi_verify_12 *cdb; 10311 10312 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10313 10314 *lba = scsi_4btoul(cdb->addr); 10315 *len = scsi_4btoul(cdb->length); 10316 break; 10317 } 10318 case VERIFY_16: { 10319 struct scsi_verify_16 *cdb; 10320 10321 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10322 10323 *lba = scsi_8btou64(cdb->addr); 10324 *len = scsi_4btoul(cdb->length); 10325 break; 10326 } 10327 case UNMAP: { 10328 *lba = 0; 10329 *len = UINT64_MAX; 10330 break; 10331 } 10332 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10333 struct scsi_get_lba_status *cdb; 10334 10335 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10336 *lba = scsi_8btou64(cdb->addr); 10337 *len = UINT32_MAX; 10338 break; 10339 } 10340 default: 10341 return (1); 10342 break; /* NOTREACHED */ 10343 } 10344 10345 return (0); 10346} 10347 10348static ctl_action 10349ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10350 bool seq) 10351{ 10352 uint64_t endlba1, endlba2; 10353 10354 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10355 endlba2 = lba2 + len2 - 1; 10356 10357 if ((endlba1 < lba2) || (endlba2 < lba1)) 10358 return (CTL_ACTION_PASS); 10359 else 10360 return (CTL_ACTION_BLOCK); 10361} 10362 10363static int 10364ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10365{ 10366 struct ctl_ptr_len_flags *ptrlen; 10367 struct scsi_unmap_desc *buf, *end, *range; 10368 uint64_t lba; 10369 uint32_t len; 10370 10371 /* If not UNMAP -- go other way. */ 10372 if (io->io_hdr.io_type != CTL_IO_SCSI || 10373 io->scsiio.cdb[0] != UNMAP) 10374 return (CTL_ACTION_ERROR); 10375 10376 /* If UNMAP without data -- block and wait for data. */ 10377 ptrlen = (struct ctl_ptr_len_flags *) 10378 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10379 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10380 ptrlen->ptr == NULL) 10381 return (CTL_ACTION_BLOCK); 10382 10383 /* UNMAP with data -- check for collision. */ 10384 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10385 end = buf + ptrlen->len / sizeof(*buf); 10386 for (range = buf; range < end; range++) { 10387 lba = scsi_8btou64(range->lba); 10388 len = scsi_4btoul(range->length); 10389 if ((lba < lba2 + len2) && (lba + len > lba2)) 10390 return (CTL_ACTION_BLOCK); 10391 } 10392 return (CTL_ACTION_PASS); 10393} 10394 10395static ctl_action 10396ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10397{ 10398 uint64_t lba1, lba2; 10399 uint64_t len1, len2; 10400 int retval; 10401 10402 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10403 return (CTL_ACTION_ERROR); 10404 10405 retval = ctl_extent_check_unmap(io1, lba2, len2); 10406 if (retval != CTL_ACTION_ERROR) 10407 return (retval); 10408 10409 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10410 return (CTL_ACTION_ERROR); 10411 10412 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10413} 10414 10415static ctl_action 10416ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10417{ 10418 uint64_t lba1, lba2; 10419 uint64_t len1, len2; 10420 10421 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10422 return (CTL_ACTION_ERROR); 10423 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10424 return (CTL_ACTION_ERROR); 10425 10426 if (lba1 + len1 == lba2) 10427 return (CTL_ACTION_BLOCK); 10428 return (CTL_ACTION_PASS); 10429} 10430 10431static ctl_action 10432ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10433 union ctl_io *ooa_io) 10434{ 10435 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10436 ctl_serialize_action *serialize_row; 10437 10438 /* 10439 * The initiator attempted multiple untagged commands at the same 10440 * time. Can't do that. 10441 */ 10442 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10443 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10444 && ((pending_io->io_hdr.nexus.targ_port == 10445 ooa_io->io_hdr.nexus.targ_port) 10446 && (pending_io->io_hdr.nexus.initid.id == 10447 ooa_io->io_hdr.nexus.initid.id)) 10448 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10449 CTL_FLAG_STATUS_SENT)) == 0)) 10450 return (CTL_ACTION_OVERLAP); 10451 10452 /* 10453 * The initiator attempted to send multiple tagged commands with 10454 * the same ID. (It's fine if different initiators have the same 10455 * tag ID.) 10456 * 10457 * Even if all of those conditions are true, we don't kill the I/O 10458 * if the command ahead of us has been aborted. We won't end up 10459 * sending it to the FETD, and it's perfectly legal to resend a 10460 * command with the same tag number as long as the previous 10461 * instance of this tag number has been aborted somehow. 10462 */ 10463 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10464 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10465 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10466 && ((pending_io->io_hdr.nexus.targ_port == 10467 ooa_io->io_hdr.nexus.targ_port) 10468 && (pending_io->io_hdr.nexus.initid.id == 10469 ooa_io->io_hdr.nexus.initid.id)) 10470 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10471 CTL_FLAG_STATUS_SENT)) == 0)) 10472 return (CTL_ACTION_OVERLAP_TAG); 10473 10474 /* 10475 * If we get a head of queue tag, SAM-3 says that we should 10476 * immediately execute it. 10477 * 10478 * What happens if this command would normally block for some other 10479 * reason? e.g. a request sense with a head of queue tag 10480 * immediately after a write. Normally that would block, but this 10481 * will result in its getting executed immediately... 10482 * 10483 * We currently return "pass" instead of "skip", so we'll end up 10484 * going through the rest of the queue to check for overlapped tags. 10485 * 10486 * XXX KDM check for other types of blockage first?? 10487 */ 10488 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10489 return (CTL_ACTION_PASS); 10490 10491 /* 10492 * Ordered tags have to block until all items ahead of them 10493 * have completed. If we get called with an ordered tag, we always 10494 * block, if something else is ahead of us in the queue. 10495 */ 10496 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10497 return (CTL_ACTION_BLOCK); 10498 10499 /* 10500 * Simple tags get blocked until all head of queue and ordered tags 10501 * ahead of them have completed. I'm lumping untagged commands in 10502 * with simple tags here. XXX KDM is that the right thing to do? 10503 */ 10504 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10505 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10506 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10507 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10508 return (CTL_ACTION_BLOCK); 10509 10510 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10511 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10512 10513 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10514 10515 switch (serialize_row[pending_entry->seridx]) { 10516 case CTL_SER_BLOCK: 10517 return (CTL_ACTION_BLOCK); 10518 case CTL_SER_EXTENT: 10519 return (ctl_extent_check(ooa_io, pending_io, 10520 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10521 case CTL_SER_EXTENTOPT: 10522 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10523 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10524 return (ctl_extent_check(ooa_io, pending_io, 10525 (lun->be_lun && 10526 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10527 return (CTL_ACTION_PASS); 10528 case CTL_SER_EXTENTSEQ: 10529 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 10530 return (ctl_extent_check_seq(ooa_io, pending_io)); 10531 return (CTL_ACTION_PASS); 10532 case CTL_SER_PASS: 10533 return (CTL_ACTION_PASS); 10534 case CTL_SER_BLOCKOPT: 10535 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10536 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10537 return (CTL_ACTION_BLOCK); 10538 return (CTL_ACTION_PASS); 10539 case CTL_SER_SKIP: 10540 return (CTL_ACTION_SKIP); 10541 default: 10542 panic("invalid serialization value %d", 10543 serialize_row[pending_entry->seridx]); 10544 } 10545 10546 return (CTL_ACTION_ERROR); 10547} 10548 10549/* 10550 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10551 * Assumptions: 10552 * - pending_io is generally either incoming, or on the blocked queue 10553 * - starting I/O is the I/O we want to start the check with. 10554 */ 10555static ctl_action 10556ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10557 union ctl_io *starting_io) 10558{ 10559 union ctl_io *ooa_io; 10560 ctl_action action; 10561 10562 mtx_assert(&lun->lun_lock, MA_OWNED); 10563 10564 /* 10565 * Run back along the OOA queue, starting with the current 10566 * blocked I/O and going through every I/O before it on the 10567 * queue. If starting_io is NULL, we'll just end up returning 10568 * CTL_ACTION_PASS. 10569 */ 10570 for (ooa_io = starting_io; ooa_io != NULL; 10571 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10572 ooa_links)){ 10573 10574 /* 10575 * This routine just checks to see whether 10576 * cur_blocked is blocked by ooa_io, which is ahead 10577 * of it in the queue. It doesn't queue/dequeue 10578 * cur_blocked. 10579 */ 10580 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10581 switch (action) { 10582 case CTL_ACTION_BLOCK: 10583 case CTL_ACTION_OVERLAP: 10584 case CTL_ACTION_OVERLAP_TAG: 10585 case CTL_ACTION_SKIP: 10586 case CTL_ACTION_ERROR: 10587 return (action); 10588 break; /* NOTREACHED */ 10589 case CTL_ACTION_PASS: 10590 break; 10591 default: 10592 panic("invalid action %d", action); 10593 break; /* NOTREACHED */ 10594 } 10595 } 10596 10597 return (CTL_ACTION_PASS); 10598} 10599 10600/* 10601 * Assumptions: 10602 * - An I/O has just completed, and has been removed from the per-LUN OOA 10603 * queue, so some items on the blocked queue may now be unblocked. 10604 */ 10605static int 10606ctl_check_blocked(struct ctl_lun *lun) 10607{ 10608 union ctl_io *cur_blocked, *next_blocked; 10609 10610 mtx_assert(&lun->lun_lock, MA_OWNED); 10611 10612 /* 10613 * Run forward from the head of the blocked queue, checking each 10614 * entry against the I/Os prior to it on the OOA queue to see if 10615 * there is still any blockage. 10616 * 10617 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10618 * with our removing a variable on it while it is traversing the 10619 * list. 10620 */ 10621 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10622 cur_blocked != NULL; cur_blocked = next_blocked) { 10623 union ctl_io *prev_ooa; 10624 ctl_action action; 10625 10626 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10627 blocked_links); 10628 10629 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10630 ctl_ooaq, ooa_links); 10631 10632 /* 10633 * If cur_blocked happens to be the first item in the OOA 10634 * queue now, prev_ooa will be NULL, and the action 10635 * returned will just be CTL_ACTION_PASS. 10636 */ 10637 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10638 10639 switch (action) { 10640 case CTL_ACTION_BLOCK: 10641 /* Nothing to do here, still blocked */ 10642 break; 10643 case CTL_ACTION_OVERLAP: 10644 case CTL_ACTION_OVERLAP_TAG: 10645 /* 10646 * This shouldn't happen! In theory we've already 10647 * checked this command for overlap... 10648 */ 10649 break; 10650 case CTL_ACTION_PASS: 10651 case CTL_ACTION_SKIP: { 10652 const struct ctl_cmd_entry *entry; 10653 int isc_retval; 10654 10655 /* 10656 * The skip case shouldn't happen, this transaction 10657 * should have never made it onto the blocked queue. 10658 */ 10659 /* 10660 * This I/O is no longer blocked, we can remove it 10661 * from the blocked queue. Since this is a TAILQ 10662 * (doubly linked list), we can do O(1) removals 10663 * from any place on the list. 10664 */ 10665 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10666 blocked_links); 10667 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10668 10669 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10670 /* 10671 * Need to send IO back to original side to 10672 * run 10673 */ 10674 union ctl_ha_msg msg_info; 10675 10676 msg_info.hdr.original_sc = 10677 cur_blocked->io_hdr.original_sc; 10678 msg_info.hdr.serializing_sc = cur_blocked; 10679 msg_info.hdr.msg_type = CTL_MSG_R2R; 10680 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10681 &msg_info, sizeof(msg_info), 0)) > 10682 CTL_HA_STATUS_SUCCESS) { 10683 printf("CTL:Check Blocked error from " 10684 "ctl_ha_msg_send %d\n", 10685 isc_retval); 10686 } 10687 break; 10688 } 10689 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 10690 10691 /* 10692 * Check this I/O for LUN state changes that may 10693 * have happened while this command was blocked. 10694 * The LUN state may have been changed by a command 10695 * ahead of us in the queue, so we need to re-check 10696 * for any states that can be caused by SCSI 10697 * commands. 10698 */ 10699 if (ctl_scsiio_lun_check(lun, entry, 10700 &cur_blocked->scsiio) == 0) { 10701 cur_blocked->io_hdr.flags |= 10702 CTL_FLAG_IS_WAS_ON_RTR; 10703 ctl_enqueue_rtr(cur_blocked); 10704 } else 10705 ctl_done(cur_blocked); 10706 break; 10707 } 10708 default: 10709 /* 10710 * This probably shouldn't happen -- we shouldn't 10711 * get CTL_ACTION_ERROR, or anything else. 10712 */ 10713 break; 10714 } 10715 } 10716 10717 return (CTL_RETVAL_COMPLETE); 10718} 10719 10720/* 10721 * This routine (with one exception) checks LUN flags that can be set by 10722 * commands ahead of us in the OOA queue. These flags have to be checked 10723 * when a command initially comes in, and when we pull a command off the 10724 * blocked queue and are preparing to execute it. The reason we have to 10725 * check these flags for commands on the blocked queue is that the LUN 10726 * state may have been changed by a command ahead of us while we're on the 10727 * blocked queue. 10728 * 10729 * Ordering is somewhat important with these checks, so please pay 10730 * careful attention to the placement of any new checks. 10731 */ 10732static int 10733ctl_scsiio_lun_check(struct ctl_lun *lun, 10734 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 10735{ 10736 struct ctl_softc *softc = lun->ctl_softc; 10737 int retval; 10738 uint32_t residx; 10739 10740 retval = 0; 10741 10742 mtx_assert(&lun->lun_lock, MA_OWNED); 10743 10744 /* 10745 * If this shelf is a secondary shelf controller, we have to reject 10746 * any media access commands. 10747 */ 10748 if ((softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 10749 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 10750 ctl_set_lun_standby(ctsio); 10751 retval = 1; 10752 goto bailout; 10753 } 10754 10755 if (entry->pattern & CTL_LUN_PAT_WRITE) { 10756 if (lun->be_lun && 10757 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 10758 ctl_set_sense(ctsio, /*current_error*/ 1, 10759 /*sense_key*/ SSD_KEY_DATA_PROTECT, 10760 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 10761 retval = 1; 10762 goto bailout; 10763 } 10764 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 10765 .eca_and_aen & SCP_SWP) != 0) { 10766 ctl_set_sense(ctsio, /*current_error*/ 1, 10767 /*sense_key*/ SSD_KEY_DATA_PROTECT, 10768 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 10769 retval = 1; 10770 goto bailout; 10771 } 10772 } 10773 10774 /* 10775 * Check for a reservation conflict. If this command isn't allowed 10776 * even on reserved LUNs, and if this initiator isn't the one who 10777 * reserved us, reject the command with a reservation conflict. 10778 */ 10779 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 10780 if ((lun->flags & CTL_LUN_RESERVED) 10781 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 10782 if (lun->res_idx != residx) { 10783 ctl_set_reservation_conflict(ctsio); 10784 retval = 1; 10785 goto bailout; 10786 } 10787 } 10788 10789 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 10790 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 10791 /* No reservation or command is allowed. */; 10792 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 10793 (lun->res_type == SPR_TYPE_WR_EX || 10794 lun->res_type == SPR_TYPE_WR_EX_RO || 10795 lun->res_type == SPR_TYPE_WR_EX_AR)) { 10796 /* The command is allowed for Write Exclusive resv. */; 10797 } else { 10798 /* 10799 * if we aren't registered or it's a res holder type 10800 * reservation and this isn't the res holder then set a 10801 * conflict. 10802 */ 10803 if (ctl_get_prkey(lun, residx) == 0 10804 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 10805 ctl_set_reservation_conflict(ctsio); 10806 retval = 1; 10807 goto bailout; 10808 } 10809 10810 } 10811 10812 if ((lun->flags & CTL_LUN_OFFLINE) 10813 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 10814 ctl_set_lun_not_ready(ctsio); 10815 retval = 1; 10816 goto bailout; 10817 } 10818 10819 /* 10820 * If the LUN is stopped, see if this particular command is allowed 10821 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 10822 */ 10823 if ((lun->flags & CTL_LUN_STOPPED) 10824 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 10825 /* "Logical unit not ready, initializing cmd. required" */ 10826 ctl_set_lun_stopped(ctsio); 10827 retval = 1; 10828 goto bailout; 10829 } 10830 10831 if ((lun->flags & CTL_LUN_INOPERABLE) 10832 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 10833 /* "Medium format corrupted" */ 10834 ctl_set_medium_format_corrupted(ctsio); 10835 retval = 1; 10836 goto bailout; 10837 } 10838 10839bailout: 10840 return (retval); 10841 10842} 10843 10844static void 10845ctl_failover_io(union ctl_io *io, int have_lock) 10846{ 10847 ctl_set_busy(&io->scsiio); 10848 ctl_done(io); 10849} 10850 10851#ifdef notyet 10852static void 10853ctl_failover(void) 10854{ 10855 struct ctl_lun *lun; 10856 struct ctl_softc *softc; 10857 union ctl_io *next_io, *pending_io; 10858 union ctl_io *io; 10859 int lun_idx; 10860 10861 softc = control_softc; 10862 10863 mtx_lock(&softc->ctl_lock); 10864 /* 10865 * Remove any cmds from the other SC from the rtr queue. These 10866 * will obviously only be for LUNs for which we're the primary. 10867 * We can't send status or get/send data for these commands. 10868 * Since they haven't been executed yet, we can just remove them. 10869 * We'll either abort them or delete them below, depending on 10870 * which HA mode we're in. 10871 */ 10872#ifdef notyet 10873 mtx_lock(&softc->queue_lock); 10874 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); 10875 io != NULL; io = next_io) { 10876 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 10877 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10878 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr, 10879 ctl_io_hdr, links); 10880 } 10881 mtx_unlock(&softc->queue_lock); 10882#endif 10883 10884 for (lun_idx=0; lun_idx < softc->num_luns; lun_idx++) { 10885 lun = softc->ctl_luns[lun_idx]; 10886 if (lun==NULL) 10887 continue; 10888 10889 /* 10890 * Processor LUNs are primary on both sides. 10891 * XXX will this always be true? 10892 */ 10893 if (lun->be_lun->lun_type == T_PROCESSOR) 10894 continue; 10895 10896 if ((lun->flags & CTL_LUN_PRIMARY_SC) 10897 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 10898 printf("FAILOVER: primary lun %d\n", lun_idx); 10899 /* 10900 * Remove all commands from the other SC. First from the 10901 * blocked queue then from the ooa queue. Once we have 10902 * removed them. Call ctl_check_blocked to see if there 10903 * is anything that can run. 10904 */ 10905 for (io = (union ctl_io *)TAILQ_FIRST( 10906 &lun->blocked_queue); io != NULL; io = next_io) { 10907 10908 next_io = (union ctl_io *)TAILQ_NEXT( 10909 &io->io_hdr, blocked_links); 10910 10911 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10912 TAILQ_REMOVE(&lun->blocked_queue, 10913 &io->io_hdr,blocked_links); 10914 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10915 TAILQ_REMOVE(&lun->ooa_queue, 10916 &io->io_hdr, ooa_links); 10917 10918 ctl_free_io(io); 10919 } 10920 } 10921 10922 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10923 io != NULL; io = next_io) { 10924 10925 next_io = (union ctl_io *)TAILQ_NEXT( 10926 &io->io_hdr, ooa_links); 10927 10928 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10929 10930 TAILQ_REMOVE(&lun->ooa_queue, 10931 &io->io_hdr, 10932 ooa_links); 10933 10934 ctl_free_io(io); 10935 } 10936 } 10937 ctl_check_blocked(lun); 10938 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 10939 && (softc->ha_mode == CTL_HA_MODE_XFER)) { 10940 10941 printf("FAILOVER: primary lun %d\n", lun_idx); 10942 /* 10943 * Abort all commands from the other SC. We can't 10944 * send status back for them now. These should get 10945 * cleaned up when they are completed or come out 10946 * for a datamove operation. 10947 */ 10948 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10949 io != NULL; io = next_io) { 10950 next_io = (union ctl_io *)TAILQ_NEXT( 10951 &io->io_hdr, ooa_links); 10952 10953 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10954 io->io_hdr.flags |= CTL_FLAG_ABORT; 10955 } 10956 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 10957 && (softc->ha_mode == CTL_HA_MODE_XFER)) { 10958 10959 printf("FAILOVER: secondary lun %d\n", lun_idx); 10960 10961 lun->flags |= CTL_LUN_PRIMARY_SC; 10962 10963 /* 10964 * We send all I/O that was sent to this controller 10965 * and redirected to the other side back with 10966 * busy status, and have the initiator retry it. 10967 * Figuring out how much data has been transferred, 10968 * etc. and picking up where we left off would be 10969 * very tricky. 10970 * 10971 * XXX KDM need to remove I/O from the blocked 10972 * queue as well! 10973 */ 10974 for (pending_io = (union ctl_io *)TAILQ_FIRST( 10975 &lun->ooa_queue); pending_io != NULL; 10976 pending_io = next_io) { 10977 10978 next_io = (union ctl_io *)TAILQ_NEXT( 10979 &pending_io->io_hdr, ooa_links); 10980 10981 pending_io->io_hdr.flags &= 10982 ~CTL_FLAG_SENT_2OTHER_SC; 10983 10984 if (pending_io->io_hdr.flags & 10985 CTL_FLAG_IO_ACTIVE) { 10986 pending_io->io_hdr.flags |= 10987 CTL_FLAG_FAILOVER; 10988 } else { 10989 ctl_set_busy(&pending_io->scsiio); 10990 ctl_done(pending_io); 10991 } 10992 } 10993 10994 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 10995 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 10996 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 10997 printf("FAILOVER: secondary lun %d\n", lun_idx); 10998 /* 10999 * if the first io on the OOA is not on the RtR queue 11000 * add it. 11001 */ 11002 lun->flags |= CTL_LUN_PRIMARY_SC; 11003 11004 pending_io = (union ctl_io *)TAILQ_FIRST( 11005 &lun->ooa_queue); 11006 if (pending_io==NULL) { 11007 printf("Nothing on OOA queue\n"); 11008 continue; 11009 } 11010 11011 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11012 if ((pending_io->io_hdr.flags & 11013 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11014 pending_io->io_hdr.flags |= 11015 CTL_FLAG_IS_WAS_ON_RTR; 11016 ctl_enqueue_rtr(pending_io); 11017 } 11018#if 0 11019 else 11020 { 11021 printf("Tag 0x%04x is running\n", 11022 pending_io->scsiio.tag_num); 11023 } 11024#endif 11025 11026 next_io = (union ctl_io *)TAILQ_NEXT( 11027 &pending_io->io_hdr, ooa_links); 11028 for (pending_io=next_io; pending_io != NULL; 11029 pending_io = next_io) { 11030 pending_io->io_hdr.flags &= 11031 ~CTL_FLAG_SENT_2OTHER_SC; 11032 next_io = (union ctl_io *)TAILQ_NEXT( 11033 &pending_io->io_hdr, ooa_links); 11034 if (pending_io->io_hdr.flags & 11035 CTL_FLAG_IS_WAS_ON_RTR) { 11036#if 0 11037 printf("Tag 0x%04x is running\n", 11038 pending_io->scsiio.tag_num); 11039#endif 11040 continue; 11041 } 11042 11043 switch (ctl_check_ooa(lun, pending_io, 11044 (union ctl_io *)TAILQ_PREV( 11045 &pending_io->io_hdr, ctl_ooaq, 11046 ooa_links))) { 11047 11048 case CTL_ACTION_BLOCK: 11049 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11050 &pending_io->io_hdr, 11051 blocked_links); 11052 pending_io->io_hdr.flags |= 11053 CTL_FLAG_BLOCKED; 11054 break; 11055 case CTL_ACTION_PASS: 11056 case CTL_ACTION_SKIP: 11057 pending_io->io_hdr.flags |= 11058 CTL_FLAG_IS_WAS_ON_RTR; 11059 ctl_enqueue_rtr(pending_io); 11060 break; 11061 case CTL_ACTION_OVERLAP: 11062 ctl_set_overlapped_cmd( 11063 (struct ctl_scsiio *)pending_io); 11064 ctl_done(pending_io); 11065 break; 11066 case CTL_ACTION_OVERLAP_TAG: 11067 ctl_set_overlapped_tag( 11068 (struct ctl_scsiio *)pending_io, 11069 pending_io->scsiio.tag_num & 0xff); 11070 ctl_done(pending_io); 11071 break; 11072 case CTL_ACTION_ERROR: 11073 default: 11074 ctl_set_internal_failure( 11075 (struct ctl_scsiio *)pending_io, 11076 0, // sks_valid 11077 0); //retry count 11078 ctl_done(pending_io); 11079 break; 11080 } 11081 } 11082 11083 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11084 } else { 11085 panic("Unhandled HA mode failover, LUN flags = %#x, " 11086 "ha_mode = #%x", lun->flags, softc->ha_mode); 11087 } 11088 } 11089 ctl_pause_rtr = 0; 11090 mtx_unlock(&softc->ctl_lock); 11091} 11092#endif 11093 11094static int 11095ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11096{ 11097 struct ctl_lun *lun; 11098 const struct ctl_cmd_entry *entry; 11099 uint32_t initidx, targ_lun; 11100 int retval; 11101 11102 retval = 0; 11103 11104 lun = NULL; 11105 11106 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11107 if ((targ_lun < CTL_MAX_LUNS) 11108 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11109 /* 11110 * If the LUN is invalid, pretend that it doesn't exist. 11111 * It will go away as soon as all pending I/O has been 11112 * completed. 11113 */ 11114 mtx_lock(&lun->lun_lock); 11115 if (lun->flags & CTL_LUN_DISABLED) { 11116 mtx_unlock(&lun->lun_lock); 11117 lun = NULL; 11118 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11119 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11120 } else { 11121 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11122 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11123 lun->be_lun; 11124 if (lun->be_lun->lun_type == T_PROCESSOR) { 11125 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11126 } 11127 11128 /* 11129 * Every I/O goes into the OOA queue for a 11130 * particular LUN, and stays there until completion. 11131 */ 11132#ifdef CTL_TIME_IO 11133 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11134 lun->idle_time += getsbinuptime() - 11135 lun->last_busy; 11136 } 11137#endif 11138 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11139 ooa_links); 11140 } 11141 } else { 11142 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11143 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11144 } 11145 11146 /* Get command entry and return error if it is unsuppotyed. */ 11147 entry = ctl_validate_command(ctsio); 11148 if (entry == NULL) { 11149 if (lun) 11150 mtx_unlock(&lun->lun_lock); 11151 return (retval); 11152 } 11153 11154 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11155 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11156 11157 /* 11158 * Check to see whether we can send this command to LUNs that don't 11159 * exist. This should pretty much only be the case for inquiry 11160 * and request sense. Further checks, below, really require having 11161 * a LUN, so we can't really check the command anymore. Just put 11162 * it on the rtr queue. 11163 */ 11164 if (lun == NULL) { 11165 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11166 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11167 ctl_enqueue_rtr((union ctl_io *)ctsio); 11168 return (retval); 11169 } 11170 11171 ctl_set_unsupported_lun(ctsio); 11172 ctl_done((union ctl_io *)ctsio); 11173 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11174 return (retval); 11175 } else { 11176 /* 11177 * Make sure we support this particular command on this LUN. 11178 * e.g., we don't support writes to the control LUN. 11179 */ 11180 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11181 mtx_unlock(&lun->lun_lock); 11182 ctl_set_invalid_opcode(ctsio); 11183 ctl_done((union ctl_io *)ctsio); 11184 return (retval); 11185 } 11186 } 11187 11188 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11189 11190#ifdef CTL_WITH_CA 11191 /* 11192 * If we've got a request sense, it'll clear the contingent 11193 * allegiance condition. Otherwise, if we have a CA condition for 11194 * this initiator, clear it, because it sent down a command other 11195 * than request sense. 11196 */ 11197 if ((ctsio->cdb[0] != REQUEST_SENSE) 11198 && (ctl_is_set(lun->have_ca, initidx))) 11199 ctl_clear_mask(lun->have_ca, initidx); 11200#endif 11201 11202 /* 11203 * If the command has this flag set, it handles its own unit 11204 * attention reporting, we shouldn't do anything. Otherwise we 11205 * check for any pending unit attentions, and send them back to the 11206 * initiator. We only do this when a command initially comes in, 11207 * not when we pull it off the blocked queue. 11208 * 11209 * According to SAM-3, section 5.3.2, the order that things get 11210 * presented back to the host is basically unit attentions caused 11211 * by some sort of reset event, busy status, reservation conflicts 11212 * or task set full, and finally any other status. 11213 * 11214 * One issue here is that some of the unit attentions we report 11215 * don't fall into the "reset" category (e.g. "reported luns data 11216 * has changed"). So reporting it here, before the reservation 11217 * check, may be technically wrong. I guess the only thing to do 11218 * would be to check for and report the reset events here, and then 11219 * check for the other unit attention types after we check for a 11220 * reservation conflict. 11221 * 11222 * XXX KDM need to fix this 11223 */ 11224 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11225 ctl_ua_type ua_type; 11226 scsi_sense_data_type sense_format; 11227 11228 if (lun->flags & CTL_LUN_SENSE_DESC) 11229 sense_format = SSD_TYPE_DESC; 11230 else 11231 sense_format = SSD_TYPE_FIXED; 11232 11233 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11234 sense_format); 11235 if (ua_type != CTL_UA_NONE) { 11236 mtx_unlock(&lun->lun_lock); 11237 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11238 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11239 ctsio->sense_len = SSD_FULL_SIZE; 11240 ctl_done((union ctl_io *)ctsio); 11241 return (retval); 11242 } 11243 } 11244 11245 11246 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11247 mtx_unlock(&lun->lun_lock); 11248 ctl_done((union ctl_io *)ctsio); 11249 return (retval); 11250 } 11251 11252 /* 11253 * XXX CHD this is where we want to send IO to other side if 11254 * this LUN is secondary on this SC. We will need to make a copy 11255 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11256 * the copy we send as FROM_OTHER. 11257 * We also need to stuff the address of the original IO so we can 11258 * find it easily. Something similar will need be done on the other 11259 * side so when we are done we can find the copy. 11260 */ 11261 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11262 union ctl_ha_msg msg_info; 11263 int isc_retval; 11264 11265 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11266 11267 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11268 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11269#if 0 11270 printf("1. ctsio %p\n", ctsio); 11271#endif 11272 msg_info.hdr.serializing_sc = NULL; 11273 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11274 msg_info.scsi.tag_num = ctsio->tag_num; 11275 msg_info.scsi.tag_type = ctsio->tag_type; 11276 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11277 11278 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11279 11280 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11281 (void *)&msg_info, sizeof(msg_info), 0)) > 11282 CTL_HA_STATUS_SUCCESS) { 11283 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11284 isc_retval); 11285 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11286 } else { 11287#if 0 11288 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11289#endif 11290 } 11291 11292 /* 11293 * XXX KDM this I/O is off the incoming queue, but hasn't 11294 * been inserted on any other queue. We may need to come 11295 * up with a holding queue while we wait for serialization 11296 * so that we have an idea of what we're waiting for from 11297 * the other side. 11298 */ 11299 mtx_unlock(&lun->lun_lock); 11300 return (retval); 11301 } 11302 11303 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11304 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11305 ctl_ooaq, ooa_links))) { 11306 case CTL_ACTION_BLOCK: 11307 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11308 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11309 blocked_links); 11310 mtx_unlock(&lun->lun_lock); 11311 return (retval); 11312 case CTL_ACTION_PASS: 11313 case CTL_ACTION_SKIP: 11314 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11315 mtx_unlock(&lun->lun_lock); 11316 ctl_enqueue_rtr((union ctl_io *)ctsio); 11317 break; 11318 case CTL_ACTION_OVERLAP: 11319 mtx_unlock(&lun->lun_lock); 11320 ctl_set_overlapped_cmd(ctsio); 11321 ctl_done((union ctl_io *)ctsio); 11322 break; 11323 case CTL_ACTION_OVERLAP_TAG: 11324 mtx_unlock(&lun->lun_lock); 11325 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11326 ctl_done((union ctl_io *)ctsio); 11327 break; 11328 case CTL_ACTION_ERROR: 11329 default: 11330 mtx_unlock(&lun->lun_lock); 11331 ctl_set_internal_failure(ctsio, 11332 /*sks_valid*/ 0, 11333 /*retry_count*/ 0); 11334 ctl_done((union ctl_io *)ctsio); 11335 break; 11336 } 11337 return (retval); 11338} 11339 11340const struct ctl_cmd_entry * 11341ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11342{ 11343 const struct ctl_cmd_entry *entry; 11344 int service_action; 11345 11346 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11347 if (sa) 11348 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11349 if (entry->flags & CTL_CMD_FLAG_SA5) { 11350 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11351 entry = &((const struct ctl_cmd_entry *) 11352 entry->execute)[service_action]; 11353 } 11354 return (entry); 11355} 11356 11357const struct ctl_cmd_entry * 11358ctl_validate_command(struct ctl_scsiio *ctsio) 11359{ 11360 const struct ctl_cmd_entry *entry; 11361 int i, sa; 11362 uint8_t diff; 11363 11364 entry = ctl_get_cmd_entry(ctsio, &sa); 11365 if (entry->execute == NULL) { 11366 if (sa) 11367 ctl_set_invalid_field(ctsio, 11368 /*sks_valid*/ 1, 11369 /*command*/ 1, 11370 /*field*/ 1, 11371 /*bit_valid*/ 1, 11372 /*bit*/ 4); 11373 else 11374 ctl_set_invalid_opcode(ctsio); 11375 ctl_done((union ctl_io *)ctsio); 11376 return (NULL); 11377 } 11378 KASSERT(entry->length > 0, 11379 ("Not defined length for command 0x%02x/0x%02x", 11380 ctsio->cdb[0], ctsio->cdb[1])); 11381 for (i = 1; i < entry->length; i++) { 11382 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11383 if (diff == 0) 11384 continue; 11385 ctl_set_invalid_field(ctsio, 11386 /*sks_valid*/ 1, 11387 /*command*/ 1, 11388 /*field*/ i, 11389 /*bit_valid*/ 1, 11390 /*bit*/ fls(diff) - 1); 11391 ctl_done((union ctl_io *)ctsio); 11392 return (NULL); 11393 } 11394 return (entry); 11395} 11396 11397static int 11398ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11399{ 11400 11401 switch (lun_type) { 11402 case T_PROCESSOR: 11403 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11404 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11405 return (0); 11406 break; 11407 case T_DIRECT: 11408 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11409 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11410 return (0); 11411 break; 11412 default: 11413 return (0); 11414 } 11415 return (1); 11416} 11417 11418static int 11419ctl_scsiio(struct ctl_scsiio *ctsio) 11420{ 11421 int retval; 11422 const struct ctl_cmd_entry *entry; 11423 11424 retval = CTL_RETVAL_COMPLETE; 11425 11426 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11427 11428 entry = ctl_get_cmd_entry(ctsio, NULL); 11429 11430 /* 11431 * If this I/O has been aborted, just send it straight to 11432 * ctl_done() without executing it. 11433 */ 11434 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11435 ctl_done((union ctl_io *)ctsio); 11436 goto bailout; 11437 } 11438 11439 /* 11440 * All the checks should have been handled by ctl_scsiio_precheck(). 11441 * We should be clear now to just execute the I/O. 11442 */ 11443 retval = entry->execute(ctsio); 11444 11445bailout: 11446 return (retval); 11447} 11448 11449/* 11450 * Since we only implement one target right now, a bus reset simply resets 11451 * our single target. 11452 */ 11453static int 11454ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11455{ 11456 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11457} 11458 11459static int 11460ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11461 ctl_ua_type ua_type) 11462{ 11463 struct ctl_lun *lun; 11464 int retval; 11465 11466 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11467 union ctl_ha_msg msg_info; 11468 11469 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11470 msg_info.hdr.nexus = io->io_hdr.nexus; 11471 if (ua_type==CTL_UA_TARG_RESET) 11472 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11473 else 11474 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11475 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11476 msg_info.hdr.original_sc = NULL; 11477 msg_info.hdr.serializing_sc = NULL; 11478 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11479 (void *)&msg_info, sizeof(msg_info), 0)) { 11480 } 11481 } 11482 retval = 0; 11483 11484 mtx_lock(&softc->ctl_lock); 11485 STAILQ_FOREACH(lun, &softc->lun_list, links) 11486 retval += ctl_lun_reset(lun, io, ua_type); 11487 mtx_unlock(&softc->ctl_lock); 11488 11489 return (retval); 11490} 11491 11492/* 11493 * The LUN should always be set. The I/O is optional, and is used to 11494 * distinguish between I/Os sent by this initiator, and by other 11495 * initiators. We set unit attention for initiators other than this one. 11496 * SAM-3 is vague on this point. It does say that a unit attention should 11497 * be established for other initiators when a LUN is reset (see section 11498 * 5.7.3), but it doesn't specifically say that the unit attention should 11499 * be established for this particular initiator when a LUN is reset. Here 11500 * is the relevant text, from SAM-3 rev 8: 11501 * 11502 * 5.7.2 When a SCSI initiator port aborts its own tasks 11503 * 11504 * When a SCSI initiator port causes its own task(s) to be aborted, no 11505 * notification that the task(s) have been aborted shall be returned to 11506 * the SCSI initiator port other than the completion response for the 11507 * command or task management function action that caused the task(s) to 11508 * be aborted and notification(s) associated with related effects of the 11509 * action (e.g., a reset unit attention condition). 11510 * 11511 * XXX KDM for now, we're setting unit attention for all initiators. 11512 */ 11513static int 11514ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11515{ 11516 union ctl_io *xio; 11517#if 0 11518 uint32_t initidx; 11519#endif 11520#ifdef CTL_WITH_CA 11521 int i; 11522#endif 11523 11524 mtx_lock(&lun->lun_lock); 11525 /* 11526 * Run through the OOA queue and abort each I/O. 11527 */ 11528 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11529 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11530 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11531 } 11532 11533 /* 11534 * This version sets unit attention for every 11535 */ 11536#if 0 11537 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11538 ctl_est_ua_all(lun, initidx, ua_type); 11539#else 11540 ctl_est_ua_all(lun, -1, ua_type); 11541#endif 11542 11543 /* 11544 * A reset (any kind, really) clears reservations established with 11545 * RESERVE/RELEASE. It does not clear reservations established 11546 * with PERSISTENT RESERVE OUT, but we don't support that at the 11547 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11548 * reservations made with the RESERVE/RELEASE commands, because 11549 * those commands are obsolete in SPC-3. 11550 */ 11551 lun->flags &= ~CTL_LUN_RESERVED; 11552 11553#ifdef CTL_WITH_CA 11554 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11555 ctl_clear_mask(lun->have_ca, i); 11556#endif 11557 mtx_unlock(&lun->lun_lock); 11558 11559 return (0); 11560} 11561 11562static void 11563ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11564 int other_sc) 11565{ 11566 union ctl_io *xio; 11567 11568 mtx_assert(&lun->lun_lock, MA_OWNED); 11569 11570 /* 11571 * Run through the OOA queue and attempt to find the given I/O. 11572 * The target port, initiator ID, tag type and tag number have to 11573 * match the values that we got from the initiator. If we have an 11574 * untagged command to abort, simply abort the first untagged command 11575 * we come to. We only allow one untagged command at a time of course. 11576 */ 11577 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11578 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11579 11580 if ((targ_port == UINT32_MAX || 11581 targ_port == xio->io_hdr.nexus.targ_port) && 11582 (init_id == UINT32_MAX || 11583 init_id == xio->io_hdr.nexus.initid.id)) { 11584 if (targ_port != xio->io_hdr.nexus.targ_port || 11585 init_id != xio->io_hdr.nexus.initid.id) 11586 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11587 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11588 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11589 union ctl_ha_msg msg_info; 11590 11591 msg_info.hdr.nexus = xio->io_hdr.nexus; 11592 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11593 msg_info.task.tag_num = xio->scsiio.tag_num; 11594 msg_info.task.tag_type = xio->scsiio.tag_type; 11595 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11596 msg_info.hdr.original_sc = NULL; 11597 msg_info.hdr.serializing_sc = NULL; 11598 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11599 (void *)&msg_info, sizeof(msg_info), 0); 11600 } 11601 } 11602 } 11603} 11604 11605static int 11606ctl_abort_task_set(union ctl_io *io) 11607{ 11608 struct ctl_softc *softc = control_softc; 11609 struct ctl_lun *lun; 11610 uint32_t targ_lun; 11611 11612 /* 11613 * Look up the LUN. 11614 */ 11615 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11616 mtx_lock(&softc->ctl_lock); 11617 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11618 lun = softc->ctl_luns[targ_lun]; 11619 else { 11620 mtx_unlock(&softc->ctl_lock); 11621 return (1); 11622 } 11623 11624 mtx_lock(&lun->lun_lock); 11625 mtx_unlock(&softc->ctl_lock); 11626 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11627 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11628 io->io_hdr.nexus.initid.id, 11629 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11630 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11631 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11632 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11633 } 11634 mtx_unlock(&lun->lun_lock); 11635 return (0); 11636} 11637 11638static int 11639ctl_i_t_nexus_reset(union ctl_io *io) 11640{ 11641 struct ctl_softc *softc = control_softc; 11642 struct ctl_lun *lun; 11643 uint32_t initidx, residx; 11644 11645 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11646 residx = ctl_get_resindex(&io->io_hdr.nexus); 11647 mtx_lock(&softc->ctl_lock); 11648 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11649 mtx_lock(&lun->lun_lock); 11650 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11651 io->io_hdr.nexus.initid.id, 11652 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11653#ifdef CTL_WITH_CA 11654 ctl_clear_mask(lun->have_ca, initidx); 11655#endif 11656 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 11657 lun->flags &= ~CTL_LUN_RESERVED; 11658 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 11659 mtx_unlock(&lun->lun_lock); 11660 } 11661 mtx_unlock(&softc->ctl_lock); 11662 return (0); 11663} 11664 11665static int 11666ctl_abort_task(union ctl_io *io) 11667{ 11668 union ctl_io *xio; 11669 struct ctl_lun *lun; 11670 struct ctl_softc *softc; 11671#if 0 11672 struct sbuf sb; 11673 char printbuf[128]; 11674#endif 11675 int found; 11676 uint32_t targ_lun; 11677 11678 softc = control_softc; 11679 found = 0; 11680 11681 /* 11682 * Look up the LUN. 11683 */ 11684 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11685 mtx_lock(&softc->ctl_lock); 11686 if ((targ_lun < CTL_MAX_LUNS) 11687 && (softc->ctl_luns[targ_lun] != NULL)) 11688 lun = softc->ctl_luns[targ_lun]; 11689 else { 11690 mtx_unlock(&softc->ctl_lock); 11691 return (1); 11692 } 11693 11694#if 0 11695 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11696 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11697#endif 11698 11699 mtx_lock(&lun->lun_lock); 11700 mtx_unlock(&softc->ctl_lock); 11701 /* 11702 * Run through the OOA queue and attempt to find the given I/O. 11703 * The target port, initiator ID, tag type and tag number have to 11704 * match the values that we got from the initiator. If we have an 11705 * untagged command to abort, simply abort the first untagged command 11706 * we come to. We only allow one untagged command at a time of course. 11707 */ 11708 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11709 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11710#if 0 11711 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11712 11713 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11714 lun->lun, xio->scsiio.tag_num, 11715 xio->scsiio.tag_type, 11716 (xio->io_hdr.blocked_links.tqe_prev 11717 == NULL) ? "" : " BLOCKED", 11718 (xio->io_hdr.flags & 11719 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11720 (xio->io_hdr.flags & 11721 CTL_FLAG_ABORT) ? " ABORT" : "", 11722 (xio->io_hdr.flags & 11723 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11724 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11725 sbuf_finish(&sb); 11726 printf("%s\n", sbuf_data(&sb)); 11727#endif 11728 11729 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 11730 || (xio->io_hdr.nexus.initid.id != io->io_hdr.nexus.initid.id) 11731 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 11732 continue; 11733 11734 /* 11735 * If the abort says that the task is untagged, the 11736 * task in the queue must be untagged. Otherwise, 11737 * we just check to see whether the tag numbers 11738 * match. This is because the QLogic firmware 11739 * doesn't pass back the tag type in an abort 11740 * request. 11741 */ 11742#if 0 11743 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 11744 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 11745 || (xio->scsiio.tag_num == io->taskio.tag_num)) 11746#endif 11747 /* 11748 * XXX KDM we've got problems with FC, because it 11749 * doesn't send down a tag type with aborts. So we 11750 * can only really go by the tag number... 11751 * This may cause problems with parallel SCSI. 11752 * Need to figure that out!! 11753 */ 11754 if (xio->scsiio.tag_num == io->taskio.tag_num) { 11755 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11756 found = 1; 11757 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 11758 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11759 union ctl_ha_msg msg_info; 11760 11761 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11762 msg_info.hdr.nexus = io->io_hdr.nexus; 11763 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11764 msg_info.task.tag_num = io->taskio.tag_num; 11765 msg_info.task.tag_type = io->taskio.tag_type; 11766 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11767 msg_info.hdr.original_sc = NULL; 11768 msg_info.hdr.serializing_sc = NULL; 11769#if 0 11770 printf("Sent Abort to other side\n"); 11771#endif 11772 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11773 (void *)&msg_info, sizeof(msg_info), 0) != 11774 CTL_HA_STATUS_SUCCESS) { 11775 } 11776 } 11777#if 0 11778 printf("ctl_abort_task: found I/O to abort\n"); 11779#endif 11780 } 11781 } 11782 mtx_unlock(&lun->lun_lock); 11783 11784 if (found == 0) { 11785 /* 11786 * This isn't really an error. It's entirely possible for 11787 * the abort and command completion to cross on the wire. 11788 * This is more of an informative/diagnostic error. 11789 */ 11790#if 0 11791 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 11792 "%d:%d:%d:%d tag %d type %d\n", 11793 io->io_hdr.nexus.initid.id, 11794 io->io_hdr.nexus.targ_port, 11795 io->io_hdr.nexus.targ_target.id, 11796 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 11797 io->taskio.tag_type); 11798#endif 11799 } 11800 return (0); 11801} 11802 11803static void 11804ctl_run_task(union ctl_io *io) 11805{ 11806 struct ctl_softc *softc = control_softc; 11807 int retval = 1; 11808 const char *task_desc; 11809 11810 CTL_DEBUG_PRINT(("ctl_run_task\n")); 11811 11812 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 11813 ("ctl_run_task: Unextected io_type %d\n", 11814 io->io_hdr.io_type)); 11815 11816 task_desc = ctl_scsi_task_string(&io->taskio); 11817 if (task_desc != NULL) { 11818#ifdef NEEDTOPORT 11819 csevent_log(CSC_CTL | CSC_SHELF_SW | 11820 CTL_TASK_REPORT, 11821 csevent_LogType_Trace, 11822 csevent_Severity_Information, 11823 csevent_AlertLevel_Green, 11824 csevent_FRU_Firmware, 11825 csevent_FRU_Unknown, 11826 "CTL: received task: %s",task_desc); 11827#endif 11828 } else { 11829#ifdef NEEDTOPORT 11830 csevent_log(CSC_CTL | CSC_SHELF_SW | 11831 CTL_TASK_REPORT, 11832 csevent_LogType_Trace, 11833 csevent_Severity_Information, 11834 csevent_AlertLevel_Green, 11835 csevent_FRU_Firmware, 11836 csevent_FRU_Unknown, 11837 "CTL: received unknown task " 11838 "type: %d (%#x)", 11839 io->taskio.task_action, 11840 io->taskio.task_action); 11841#endif 11842 } 11843 switch (io->taskio.task_action) { 11844 case CTL_TASK_ABORT_TASK: 11845 retval = ctl_abort_task(io); 11846 break; 11847 case CTL_TASK_ABORT_TASK_SET: 11848 case CTL_TASK_CLEAR_TASK_SET: 11849 retval = ctl_abort_task_set(io); 11850 break; 11851 case CTL_TASK_CLEAR_ACA: 11852 break; 11853 case CTL_TASK_I_T_NEXUS_RESET: 11854 retval = ctl_i_t_nexus_reset(io); 11855 break; 11856 case CTL_TASK_LUN_RESET: { 11857 struct ctl_lun *lun; 11858 uint32_t targ_lun; 11859 11860 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11861 mtx_lock(&softc->ctl_lock); 11862 if ((targ_lun < CTL_MAX_LUNS) 11863 && (softc->ctl_luns[targ_lun] != NULL)) 11864 lun = softc->ctl_luns[targ_lun]; 11865 else { 11866 mtx_unlock(&softc->ctl_lock); 11867 retval = 1; 11868 break; 11869 } 11870 11871 if (!(io->io_hdr.flags & 11872 CTL_FLAG_FROM_OTHER_SC)) { 11873 union ctl_ha_msg msg_info; 11874 11875 io->io_hdr.flags |= 11876 CTL_FLAG_SENT_2OTHER_SC; 11877 msg_info.hdr.msg_type = 11878 CTL_MSG_MANAGE_TASKS; 11879 msg_info.hdr.nexus = io->io_hdr.nexus; 11880 msg_info.task.task_action = 11881 CTL_TASK_LUN_RESET; 11882 msg_info.hdr.original_sc = NULL; 11883 msg_info.hdr.serializing_sc = NULL; 11884 if (CTL_HA_STATUS_SUCCESS != 11885 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11886 (void *)&msg_info, 11887 sizeof(msg_info), 0)) { 11888 } 11889 } 11890 11891 retval = ctl_lun_reset(lun, io, 11892 CTL_UA_LUN_RESET); 11893 mtx_unlock(&softc->ctl_lock); 11894 break; 11895 } 11896 case CTL_TASK_TARGET_RESET: 11897 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 11898 break; 11899 case CTL_TASK_BUS_RESET: 11900 retval = ctl_bus_reset(softc, io); 11901 break; 11902 case CTL_TASK_PORT_LOGIN: 11903 break; 11904 case CTL_TASK_PORT_LOGOUT: 11905 break; 11906 default: 11907 printf("ctl_run_task: got unknown task management event %d\n", 11908 io->taskio.task_action); 11909 break; 11910 } 11911 if (retval == 0) 11912 io->io_hdr.status = CTL_SUCCESS; 11913 else 11914 io->io_hdr.status = CTL_ERROR; 11915 ctl_done(io); 11916} 11917 11918/* 11919 * For HA operation. Handle commands that come in from the other 11920 * controller. 11921 */ 11922static void 11923ctl_handle_isc(union ctl_io *io) 11924{ 11925 int free_io; 11926 struct ctl_lun *lun; 11927 struct ctl_softc *softc; 11928 uint32_t targ_lun; 11929 11930 softc = control_softc; 11931 11932 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11933 lun = softc->ctl_luns[targ_lun]; 11934 11935 switch (io->io_hdr.msg_type) { 11936 case CTL_MSG_SERIALIZE: 11937 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 11938 break; 11939 case CTL_MSG_R2R: { 11940 const struct ctl_cmd_entry *entry; 11941 11942 /* 11943 * This is only used in SER_ONLY mode. 11944 */ 11945 free_io = 0; 11946 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 11947 mtx_lock(&lun->lun_lock); 11948 if (ctl_scsiio_lun_check(lun, 11949 entry, (struct ctl_scsiio *)io) != 0) { 11950 mtx_unlock(&lun->lun_lock); 11951 ctl_done(io); 11952 break; 11953 } 11954 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11955 mtx_unlock(&lun->lun_lock); 11956 ctl_enqueue_rtr(io); 11957 break; 11958 } 11959 case CTL_MSG_FINISH_IO: 11960 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11961 free_io = 0; 11962 ctl_done(io); 11963 } else { 11964 free_io = 1; 11965 mtx_lock(&lun->lun_lock); 11966 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 11967 ooa_links); 11968 ctl_check_blocked(lun); 11969 mtx_unlock(&lun->lun_lock); 11970 } 11971 break; 11972 case CTL_MSG_PERS_ACTION: 11973 ctl_hndl_per_res_out_on_other_sc( 11974 (union ctl_ha_msg *)&io->presio.pr_msg); 11975 free_io = 1; 11976 break; 11977 case CTL_MSG_BAD_JUJU: 11978 free_io = 0; 11979 ctl_done(io); 11980 break; 11981 case CTL_MSG_DATAMOVE: 11982 /* Only used in XFER mode */ 11983 free_io = 0; 11984 ctl_datamove_remote(io); 11985 break; 11986 case CTL_MSG_DATAMOVE_DONE: 11987 /* Only used in XFER mode */ 11988 free_io = 0; 11989 io->scsiio.be_move_done(io); 11990 break; 11991 default: 11992 free_io = 1; 11993 printf("%s: Invalid message type %d\n", 11994 __func__, io->io_hdr.msg_type); 11995 break; 11996 } 11997 if (free_io) 11998 ctl_free_io(io); 11999 12000} 12001 12002 12003/* 12004 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12005 * there is no match. 12006 */ 12007static ctl_lun_error_pattern 12008ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12009{ 12010 const struct ctl_cmd_entry *entry; 12011 ctl_lun_error_pattern filtered_pattern, pattern; 12012 12013 pattern = desc->error_pattern; 12014 12015 /* 12016 * XXX KDM we need more data passed into this function to match a 12017 * custom pattern, and we actually need to implement custom pattern 12018 * matching. 12019 */ 12020 if (pattern & CTL_LUN_PAT_CMD) 12021 return (CTL_LUN_PAT_CMD); 12022 12023 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12024 return (CTL_LUN_PAT_ANY); 12025 12026 entry = ctl_get_cmd_entry(ctsio, NULL); 12027 12028 filtered_pattern = entry->pattern & pattern; 12029 12030 /* 12031 * If the user requested specific flags in the pattern (e.g. 12032 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12033 * flags. 12034 * 12035 * If the user did not specify any flags, it doesn't matter whether 12036 * or not the command supports the flags. 12037 */ 12038 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12039 (pattern & ~CTL_LUN_PAT_MASK)) 12040 return (CTL_LUN_PAT_NONE); 12041 12042 /* 12043 * If the user asked for a range check, see if the requested LBA 12044 * range overlaps with this command's LBA range. 12045 */ 12046 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12047 uint64_t lba1; 12048 uint64_t len1; 12049 ctl_action action; 12050 int retval; 12051 12052 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12053 if (retval != 0) 12054 return (CTL_LUN_PAT_NONE); 12055 12056 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12057 desc->lba_range.len, FALSE); 12058 /* 12059 * A "pass" means that the LBA ranges don't overlap, so 12060 * this doesn't match the user's range criteria. 12061 */ 12062 if (action == CTL_ACTION_PASS) 12063 return (CTL_LUN_PAT_NONE); 12064 } 12065 12066 return (filtered_pattern); 12067} 12068 12069static void 12070ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12071{ 12072 struct ctl_error_desc *desc, *desc2; 12073 12074 mtx_assert(&lun->lun_lock, MA_OWNED); 12075 12076 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12077 ctl_lun_error_pattern pattern; 12078 /* 12079 * Check to see whether this particular command matches 12080 * the pattern in the descriptor. 12081 */ 12082 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12083 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12084 continue; 12085 12086 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12087 case CTL_LUN_INJ_ABORTED: 12088 ctl_set_aborted(&io->scsiio); 12089 break; 12090 case CTL_LUN_INJ_MEDIUM_ERR: 12091 ctl_set_medium_error(&io->scsiio); 12092 break; 12093 case CTL_LUN_INJ_UA: 12094 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12095 * OCCURRED */ 12096 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12097 break; 12098 case CTL_LUN_INJ_CUSTOM: 12099 /* 12100 * We're assuming the user knows what he is doing. 12101 * Just copy the sense information without doing 12102 * checks. 12103 */ 12104 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12105 MIN(sizeof(desc->custom_sense), 12106 sizeof(io->scsiio.sense_data))); 12107 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12108 io->scsiio.sense_len = SSD_FULL_SIZE; 12109 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12110 break; 12111 case CTL_LUN_INJ_NONE: 12112 default: 12113 /* 12114 * If this is an error injection type we don't know 12115 * about, clear the continuous flag (if it is set) 12116 * so it will get deleted below. 12117 */ 12118 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12119 break; 12120 } 12121 /* 12122 * By default, each error injection action is a one-shot 12123 */ 12124 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12125 continue; 12126 12127 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12128 12129 free(desc, M_CTL); 12130 } 12131} 12132 12133#ifdef CTL_IO_DELAY 12134static void 12135ctl_datamove_timer_wakeup(void *arg) 12136{ 12137 union ctl_io *io; 12138 12139 io = (union ctl_io *)arg; 12140 12141 ctl_datamove(io); 12142} 12143#endif /* CTL_IO_DELAY */ 12144 12145void 12146ctl_datamove(union ctl_io *io) 12147{ 12148 void (*fe_datamove)(union ctl_io *io); 12149 12150 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12151 12152 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12153 12154#ifdef CTL_TIME_IO 12155 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12156 char str[256]; 12157 char path_str[64]; 12158 struct sbuf sb; 12159 12160 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12161 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12162 12163 sbuf_cat(&sb, path_str); 12164 switch (io->io_hdr.io_type) { 12165 case CTL_IO_SCSI: 12166 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12167 sbuf_printf(&sb, "\n"); 12168 sbuf_cat(&sb, path_str); 12169 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12170 io->scsiio.tag_num, io->scsiio.tag_type); 12171 break; 12172 case CTL_IO_TASK: 12173 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12174 "Tag Type: %d\n", io->taskio.task_action, 12175 io->taskio.tag_num, io->taskio.tag_type); 12176 break; 12177 default: 12178 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12179 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12180 break; 12181 } 12182 sbuf_cat(&sb, path_str); 12183 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12184 (intmax_t)time_uptime - io->io_hdr.start_time); 12185 sbuf_finish(&sb); 12186 printf("%s", sbuf_data(&sb)); 12187 } 12188#endif /* CTL_TIME_IO */ 12189 12190#ifdef CTL_IO_DELAY 12191 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12192 struct ctl_lun *lun; 12193 12194 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12195 12196 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12197 } else { 12198 struct ctl_lun *lun; 12199 12200 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12201 if ((lun != NULL) 12202 && (lun->delay_info.datamove_delay > 0)) { 12203 struct callout *callout; 12204 12205 callout = (struct callout *)&io->io_hdr.timer_bytes; 12206 callout_init(callout, /*mpsafe*/ 1); 12207 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12208 callout_reset(callout, 12209 lun->delay_info.datamove_delay * hz, 12210 ctl_datamove_timer_wakeup, io); 12211 if (lun->delay_info.datamove_type == 12212 CTL_DELAY_TYPE_ONESHOT) 12213 lun->delay_info.datamove_delay = 0; 12214 return; 12215 } 12216 } 12217#endif 12218 12219 /* 12220 * This command has been aborted. Set the port status, so we fail 12221 * the data move. 12222 */ 12223 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12224 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12225 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12226 io->io_hdr.nexus.targ_port, 12227 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12228 io->io_hdr.nexus.targ_lun); 12229 io->io_hdr.port_status = 31337; 12230 /* 12231 * Note that the backend, in this case, will get the 12232 * callback in its context. In other cases it may get 12233 * called in the frontend's interrupt thread context. 12234 */ 12235 io->scsiio.be_move_done(io); 12236 return; 12237 } 12238 12239 /* Don't confuse frontend with zero length data move. */ 12240 if (io->scsiio.kern_data_len == 0) { 12241 io->scsiio.be_move_done(io); 12242 return; 12243 } 12244 12245 /* 12246 * If we're in XFER mode and this I/O is from the other shelf 12247 * controller, we need to send the DMA to the other side to 12248 * actually transfer the data to/from the host. In serialize only 12249 * mode the transfer happens below CTL and ctl_datamove() is only 12250 * called on the machine that originally received the I/O. 12251 */ 12252 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12253 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12254 union ctl_ha_msg msg; 12255 uint32_t sg_entries_sent; 12256 int do_sg_copy; 12257 int i; 12258 12259 memset(&msg, 0, sizeof(msg)); 12260 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12261 msg.hdr.original_sc = io->io_hdr.original_sc; 12262 msg.hdr.serializing_sc = io; 12263 msg.hdr.nexus = io->io_hdr.nexus; 12264 msg.dt.flags = io->io_hdr.flags; 12265 /* 12266 * We convert everything into a S/G list here. We can't 12267 * pass by reference, only by value between controllers. 12268 * So we can't pass a pointer to the S/G list, only as many 12269 * S/G entries as we can fit in here. If it's possible for 12270 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12271 * then we need to break this up into multiple transfers. 12272 */ 12273 if (io->scsiio.kern_sg_entries == 0) { 12274 msg.dt.kern_sg_entries = 1; 12275 /* 12276 * If this is in cached memory, flush the cache 12277 * before we send the DMA request to the other 12278 * controller. We want to do this in either the 12279 * read or the write case. The read case is 12280 * straightforward. In the write case, we want to 12281 * make sure nothing is in the local cache that 12282 * could overwrite the DMAed data. 12283 */ 12284 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12285 /* 12286 * XXX KDM use bus_dmamap_sync() here. 12287 */ 12288 } 12289 12290 /* 12291 * Convert to a physical address if this is a 12292 * virtual address. 12293 */ 12294 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12295 msg.dt.sg_list[0].addr = 12296 io->scsiio.kern_data_ptr; 12297 } else { 12298 /* 12299 * XXX KDM use busdma here! 12300 */ 12301#if 0 12302 msg.dt.sg_list[0].addr = (void *) 12303 vtophys(io->scsiio.kern_data_ptr); 12304#endif 12305 } 12306 12307 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12308 do_sg_copy = 0; 12309 } else { 12310 struct ctl_sg_entry *sgl; 12311 12312 do_sg_copy = 1; 12313 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12314 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12315 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12316 /* 12317 * XXX KDM use bus_dmamap_sync() here. 12318 */ 12319 } 12320 } 12321 12322 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12323 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12324 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12325 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12326 msg.dt.sg_sequence = 0; 12327 12328 /* 12329 * Loop until we've sent all of the S/G entries. On the 12330 * other end, we'll recompose these S/G entries into one 12331 * contiguous list before passing it to the 12332 */ 12333 for (sg_entries_sent = 0; sg_entries_sent < 12334 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12335 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/ 12336 sizeof(msg.dt.sg_list[0])), 12337 msg.dt.kern_sg_entries - sg_entries_sent); 12338 12339 if (do_sg_copy != 0) { 12340 struct ctl_sg_entry *sgl; 12341 int j; 12342 12343 sgl = (struct ctl_sg_entry *) 12344 io->scsiio.kern_data_ptr; 12345 /* 12346 * If this is in cached memory, flush the cache 12347 * before we send the DMA request to the other 12348 * controller. We want to do this in either 12349 * the * read or the write case. The read 12350 * case is straightforward. In the write 12351 * case, we want to make sure nothing is 12352 * in the local cache that could overwrite 12353 * the DMAed data. 12354 */ 12355 12356 for (i = sg_entries_sent, j = 0; 12357 i < msg.dt.cur_sg_entries; i++, j++) { 12358 if ((io->io_hdr.flags & 12359 CTL_FLAG_NO_DATASYNC) == 0) { 12360 /* 12361 * XXX KDM use bus_dmamap_sync() 12362 */ 12363 } 12364 if ((io->io_hdr.flags & 12365 CTL_FLAG_BUS_ADDR) == 0) { 12366 /* 12367 * XXX KDM use busdma. 12368 */ 12369#if 0 12370 msg.dt.sg_list[j].addr =(void *) 12371 vtophys(sgl[i].addr); 12372#endif 12373 } else { 12374 msg.dt.sg_list[j].addr = 12375 sgl[i].addr; 12376 } 12377 msg.dt.sg_list[j].len = sgl[i].len; 12378 } 12379 } 12380 12381 sg_entries_sent += msg.dt.cur_sg_entries; 12382 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12383 msg.dt.sg_last = 1; 12384 else 12385 msg.dt.sg_last = 0; 12386 12387 /* 12388 * XXX KDM drop and reacquire the lock here? 12389 */ 12390 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12391 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12392 /* 12393 * XXX do something here. 12394 */ 12395 } 12396 12397 msg.dt.sent_sg_entries = sg_entries_sent; 12398 } 12399 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12400 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12401 ctl_failover_io(io, /*have_lock*/ 0); 12402 12403 } else { 12404 12405 /* 12406 * Lookup the fe_datamove() function for this particular 12407 * front end. 12408 */ 12409 fe_datamove = 12410 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12411 12412 fe_datamove(io); 12413 } 12414} 12415 12416static void 12417ctl_send_datamove_done(union ctl_io *io, int have_lock) 12418{ 12419 union ctl_ha_msg msg; 12420 int isc_status; 12421 12422 memset(&msg, 0, sizeof(msg)); 12423 12424 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12425 msg.hdr.original_sc = io; 12426 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12427 msg.hdr.nexus = io->io_hdr.nexus; 12428 msg.hdr.status = io->io_hdr.status; 12429 msg.scsi.tag_num = io->scsiio.tag_num; 12430 msg.scsi.tag_type = io->scsiio.tag_type; 12431 msg.scsi.scsi_status = io->scsiio.scsi_status; 12432 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12433 sizeof(io->scsiio.sense_data)); 12434 msg.scsi.sense_len = io->scsiio.sense_len; 12435 msg.scsi.sense_residual = io->scsiio.sense_residual; 12436 msg.scsi.fetd_status = io->io_hdr.port_status; 12437 msg.scsi.residual = io->scsiio.residual; 12438 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12439 12440 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12441 ctl_failover_io(io, /*have_lock*/ have_lock); 12442 return; 12443 } 12444 12445 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12446 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12447 /* XXX do something if this fails */ 12448 } 12449 12450} 12451 12452/* 12453 * The DMA to the remote side is done, now we need to tell the other side 12454 * we're done so it can continue with its data movement. 12455 */ 12456static void 12457ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12458{ 12459 union ctl_io *io; 12460 12461 io = rq->context; 12462 12463 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12464 printf("%s: ISC DMA write failed with error %d", __func__, 12465 rq->ret); 12466 ctl_set_internal_failure(&io->scsiio, 12467 /*sks_valid*/ 1, 12468 /*retry_count*/ rq->ret); 12469 } 12470 12471 ctl_dt_req_free(rq); 12472 12473 /* 12474 * In this case, we had to malloc the memory locally. Free it. 12475 */ 12476 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12477 int i; 12478 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12479 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12480 } 12481 /* 12482 * The data is in local and remote memory, so now we need to send 12483 * status (good or back) back to the other side. 12484 */ 12485 ctl_send_datamove_done(io, /*have_lock*/ 0); 12486} 12487 12488/* 12489 * We've moved the data from the host/controller into local memory. Now we 12490 * need to push it over to the remote controller's memory. 12491 */ 12492static int 12493ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12494{ 12495 int retval; 12496 12497 retval = 0; 12498 12499 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12500 ctl_datamove_remote_write_cb); 12501 12502 return (retval); 12503} 12504 12505static void 12506ctl_datamove_remote_write(union ctl_io *io) 12507{ 12508 int retval; 12509 void (*fe_datamove)(union ctl_io *io); 12510 12511 /* 12512 * - Get the data from the host/HBA into local memory. 12513 * - DMA memory from the local controller to the remote controller. 12514 * - Send status back to the remote controller. 12515 */ 12516 12517 retval = ctl_datamove_remote_sgl_setup(io); 12518 if (retval != 0) 12519 return; 12520 12521 /* Switch the pointer over so the FETD knows what to do */ 12522 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12523 12524 /* 12525 * Use a custom move done callback, since we need to send completion 12526 * back to the other controller, not to the backend on this side. 12527 */ 12528 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12529 12530 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12531 12532 fe_datamove(io); 12533 12534 return; 12535 12536} 12537 12538static int 12539ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12540{ 12541#if 0 12542 char str[256]; 12543 char path_str[64]; 12544 struct sbuf sb; 12545#endif 12546 12547 /* 12548 * In this case, we had to malloc the memory locally. Free it. 12549 */ 12550 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12551 int i; 12552 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12553 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12554 } 12555 12556#if 0 12557 scsi_path_string(io, path_str, sizeof(path_str)); 12558 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12559 sbuf_cat(&sb, path_str); 12560 scsi_command_string(&io->scsiio, NULL, &sb); 12561 sbuf_printf(&sb, "\n"); 12562 sbuf_cat(&sb, path_str); 12563 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12564 io->scsiio.tag_num, io->scsiio.tag_type); 12565 sbuf_cat(&sb, path_str); 12566 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12567 io->io_hdr.flags, io->io_hdr.status); 12568 sbuf_finish(&sb); 12569 printk("%s", sbuf_data(&sb)); 12570#endif 12571 12572 12573 /* 12574 * The read is done, now we need to send status (good or bad) back 12575 * to the other side. 12576 */ 12577 ctl_send_datamove_done(io, /*have_lock*/ 0); 12578 12579 return (0); 12580} 12581 12582static void 12583ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12584{ 12585 union ctl_io *io; 12586 void (*fe_datamove)(union ctl_io *io); 12587 12588 io = rq->context; 12589 12590 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12591 printf("%s: ISC DMA read failed with error %d", __func__, 12592 rq->ret); 12593 ctl_set_internal_failure(&io->scsiio, 12594 /*sks_valid*/ 1, 12595 /*retry_count*/ rq->ret); 12596 } 12597 12598 ctl_dt_req_free(rq); 12599 12600 /* Switch the pointer over so the FETD knows what to do */ 12601 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12602 12603 /* 12604 * Use a custom move done callback, since we need to send completion 12605 * back to the other controller, not to the backend on this side. 12606 */ 12607 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12608 12609 /* XXX KDM add checks like the ones in ctl_datamove? */ 12610 12611 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12612 12613 fe_datamove(io); 12614} 12615 12616static int 12617ctl_datamove_remote_sgl_setup(union ctl_io *io) 12618{ 12619 struct ctl_sg_entry *local_sglist, *remote_sglist; 12620 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12621 struct ctl_softc *softc; 12622 int retval; 12623 int i; 12624 12625 retval = 0; 12626 softc = control_softc; 12627 12628 local_sglist = io->io_hdr.local_sglist; 12629 local_dma_sglist = io->io_hdr.local_dma_sglist; 12630 remote_sglist = io->io_hdr.remote_sglist; 12631 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12632 12633 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12634 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12635 local_sglist[i].len = remote_sglist[i].len; 12636 12637 /* 12638 * XXX Detect the situation where the RS-level I/O 12639 * redirector on the other side has already read the 12640 * data off of the AOR RS on this side, and 12641 * transferred it to remote (mirror) memory on the 12642 * other side. Since we already have the data in 12643 * memory here, we just need to use it. 12644 * 12645 * XXX KDM this can probably be removed once we 12646 * get the cache device code in and take the 12647 * current AOR implementation out. 12648 */ 12649#ifdef NEEDTOPORT 12650 if ((remote_sglist[i].addr >= 12651 (void *)vtophys(softc->mirr->addr)) 12652 && (remote_sglist[i].addr < 12653 ((void *)vtophys(softc->mirr->addr) + 12654 CacheMirrorOffset))) { 12655 local_sglist[i].addr = remote_sglist[i].addr - 12656 CacheMirrorOffset; 12657 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12658 CTL_FLAG_DATA_IN) 12659 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12660 } else { 12661 local_sglist[i].addr = remote_sglist[i].addr + 12662 CacheMirrorOffset; 12663 } 12664#endif 12665#if 0 12666 printf("%s: local %p, remote %p, len %d\n", 12667 __func__, local_sglist[i].addr, 12668 remote_sglist[i].addr, local_sglist[i].len); 12669#endif 12670 } 12671 } else { 12672 uint32_t len_to_go; 12673 12674 /* 12675 * In this case, we don't have automatically allocated 12676 * memory for this I/O on this controller. This typically 12677 * happens with internal CTL I/O -- e.g. inquiry, mode 12678 * sense, etc. Anything coming from RAIDCore will have 12679 * a mirror area available. 12680 */ 12681 len_to_go = io->scsiio.kern_data_len; 12682 12683 /* 12684 * Clear the no datasync flag, we have to use malloced 12685 * buffers. 12686 */ 12687 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 12688 12689 /* 12690 * The difficult thing here is that the size of the various 12691 * S/G segments may be different than the size from the 12692 * remote controller. That'll make it harder when DMAing 12693 * the data back to the other side. 12694 */ 12695 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 12696 sizeof(io->io_hdr.remote_sglist[0])) && 12697 (len_to_go > 0); i++) { 12698 local_sglist[i].len = MIN(len_to_go, 131072); 12699 CTL_SIZE_8B(local_dma_sglist[i].len, 12700 local_sglist[i].len); 12701 local_sglist[i].addr = 12702 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 12703 12704 local_dma_sglist[i].addr = local_sglist[i].addr; 12705 12706 if (local_sglist[i].addr == NULL) { 12707 int j; 12708 12709 printf("malloc failed for %zd bytes!", 12710 local_dma_sglist[i].len); 12711 for (j = 0; j < i; j++) { 12712 free(local_sglist[j].addr, M_CTL); 12713 } 12714 ctl_set_internal_failure(&io->scsiio, 12715 /*sks_valid*/ 1, 12716 /*retry_count*/ 4857); 12717 retval = 1; 12718 goto bailout_error; 12719 12720 } 12721 /* XXX KDM do we need a sync here? */ 12722 12723 len_to_go -= local_sglist[i].len; 12724 } 12725 /* 12726 * Reset the number of S/G entries accordingly. The 12727 * original number of S/G entries is available in 12728 * rem_sg_entries. 12729 */ 12730 io->scsiio.kern_sg_entries = i; 12731 12732#if 0 12733 printf("%s: kern_sg_entries = %d\n", __func__, 12734 io->scsiio.kern_sg_entries); 12735 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12736 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 12737 local_sglist[i].addr, local_sglist[i].len, 12738 local_dma_sglist[i].len); 12739#endif 12740 } 12741 12742 12743 return (retval); 12744 12745bailout_error: 12746 12747 ctl_send_datamove_done(io, /*have_lock*/ 0); 12748 12749 return (retval); 12750} 12751 12752static int 12753ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12754 ctl_ha_dt_cb callback) 12755{ 12756 struct ctl_ha_dt_req *rq; 12757 struct ctl_sg_entry *remote_sglist, *local_sglist; 12758 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 12759 uint32_t local_used, remote_used, total_used; 12760 int retval; 12761 int i, j; 12762 12763 retval = 0; 12764 12765 rq = ctl_dt_req_alloc(); 12766 12767 /* 12768 * If we failed to allocate the request, and if the DMA didn't fail 12769 * anyway, set busy status. This is just a resource allocation 12770 * failure. 12771 */ 12772 if ((rq == NULL) 12773 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 12774 ctl_set_busy(&io->scsiio); 12775 12776 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 12777 12778 if (rq != NULL) 12779 ctl_dt_req_free(rq); 12780 12781 /* 12782 * The data move failed. We need to return status back 12783 * to the other controller. No point in trying to DMA 12784 * data to the remote controller. 12785 */ 12786 12787 ctl_send_datamove_done(io, /*have_lock*/ 0); 12788 12789 retval = 1; 12790 12791 goto bailout; 12792 } 12793 12794 local_sglist = io->io_hdr.local_sglist; 12795 local_dma_sglist = io->io_hdr.local_dma_sglist; 12796 remote_sglist = io->io_hdr.remote_sglist; 12797 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12798 local_used = 0; 12799 remote_used = 0; 12800 total_used = 0; 12801 12802 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 12803 rq->ret = CTL_HA_STATUS_SUCCESS; 12804 rq->context = io; 12805 callback(rq); 12806 goto bailout; 12807 } 12808 12809 /* 12810 * Pull/push the data over the wire from/to the other controller. 12811 * This takes into account the possibility that the local and 12812 * remote sglists may not be identical in terms of the size of 12813 * the elements and the number of elements. 12814 * 12815 * One fundamental assumption here is that the length allocated for 12816 * both the local and remote sglists is identical. Otherwise, we've 12817 * essentially got a coding error of some sort. 12818 */ 12819 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12820 int isc_ret; 12821 uint32_t cur_len, dma_length; 12822 uint8_t *tmp_ptr; 12823 12824 rq->id = CTL_HA_DATA_CTL; 12825 rq->command = command; 12826 rq->context = io; 12827 12828 /* 12829 * Both pointers should be aligned. But it is possible 12830 * that the allocation length is not. They should both 12831 * also have enough slack left over at the end, though, 12832 * to round up to the next 8 byte boundary. 12833 */ 12834 cur_len = MIN(local_sglist[i].len - local_used, 12835 remote_sglist[j].len - remote_used); 12836 12837 /* 12838 * In this case, we have a size issue and need to decrease 12839 * the size, except in the case where we actually have less 12840 * than 8 bytes left. In that case, we need to increase 12841 * the DMA length to get the last bit. 12842 */ 12843 if ((cur_len & 0x7) != 0) { 12844 if (cur_len > 0x7) { 12845 cur_len = cur_len - (cur_len & 0x7); 12846 dma_length = cur_len; 12847 } else { 12848 CTL_SIZE_8B(dma_length, cur_len); 12849 } 12850 12851 } else 12852 dma_length = cur_len; 12853 12854 /* 12855 * If we had to allocate memory for this I/O, instead of using 12856 * the non-cached mirror memory, we'll need to flush the cache 12857 * before trying to DMA to the other controller. 12858 * 12859 * We could end up doing this multiple times for the same 12860 * segment if we have a larger local segment than remote 12861 * segment. That shouldn't be an issue. 12862 */ 12863 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12864 /* 12865 * XXX KDM use bus_dmamap_sync() here. 12866 */ 12867 } 12868 12869 rq->size = dma_length; 12870 12871 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12872 tmp_ptr += local_used; 12873 12874 /* Use physical addresses when talking to ISC hardware */ 12875 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12876 /* XXX KDM use busdma */ 12877#if 0 12878 rq->local = vtophys(tmp_ptr); 12879#endif 12880 } else 12881 rq->local = tmp_ptr; 12882 12883 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12884 tmp_ptr += remote_used; 12885 rq->remote = tmp_ptr; 12886 12887 rq->callback = NULL; 12888 12889 local_used += cur_len; 12890 if (local_used >= local_sglist[i].len) { 12891 i++; 12892 local_used = 0; 12893 } 12894 12895 remote_used += cur_len; 12896 if (remote_used >= remote_sglist[j].len) { 12897 j++; 12898 remote_used = 0; 12899 } 12900 total_used += cur_len; 12901 12902 if (total_used >= io->scsiio.kern_data_len) 12903 rq->callback = callback; 12904 12905 if ((rq->size & 0x7) != 0) { 12906 printf("%s: warning: size %d is not on 8b boundary\n", 12907 __func__, rq->size); 12908 } 12909 if (((uintptr_t)rq->local & 0x7) != 0) { 12910 printf("%s: warning: local %p not on 8b boundary\n", 12911 __func__, rq->local); 12912 } 12913 if (((uintptr_t)rq->remote & 0x7) != 0) { 12914 printf("%s: warning: remote %p not on 8b boundary\n", 12915 __func__, rq->local); 12916 } 12917#if 0 12918 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 12919 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12920 rq->local, rq->remote, rq->size); 12921#endif 12922 12923 isc_ret = ctl_dt_single(rq); 12924 if (isc_ret == CTL_HA_STATUS_WAIT) 12925 continue; 12926 12927 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 12928 rq->ret = CTL_HA_STATUS_SUCCESS; 12929 } else { 12930 rq->ret = isc_ret; 12931 } 12932 callback(rq); 12933 goto bailout; 12934 } 12935 12936bailout: 12937 return (retval); 12938 12939} 12940 12941static void 12942ctl_datamove_remote_read(union ctl_io *io) 12943{ 12944 int retval; 12945 int i; 12946 12947 /* 12948 * This will send an error to the other controller in the case of a 12949 * failure. 12950 */ 12951 retval = ctl_datamove_remote_sgl_setup(io); 12952 if (retval != 0) 12953 return; 12954 12955 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12956 ctl_datamove_remote_read_cb); 12957 if ((retval != 0) 12958 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 12959 /* 12960 * Make sure we free memory if there was an error.. The 12961 * ctl_datamove_remote_xfer() function will send the 12962 * datamove done message, or call the callback with an 12963 * error if there is a problem. 12964 */ 12965 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12966 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12967 } 12968 12969 return; 12970} 12971 12972/* 12973 * Process a datamove request from the other controller. This is used for 12974 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12975 * first. Once that is complete, the data gets DMAed into the remote 12976 * controller's memory. For reads, we DMA from the remote controller's 12977 * memory into our memory first, and then move it out to the FETD. 12978 */ 12979static void 12980ctl_datamove_remote(union ctl_io *io) 12981{ 12982 struct ctl_softc *softc; 12983 12984 softc = control_softc; 12985 12986 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 12987 12988 /* 12989 * Note that we look for an aborted I/O here, but don't do some of 12990 * the other checks that ctl_datamove() normally does. 12991 * We don't need to run the datamove delay code, since that should 12992 * have been done if need be on the other controller. 12993 */ 12994 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12995 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 12996 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 12997 io->io_hdr.nexus.targ_port, 12998 io->io_hdr.nexus.targ_target.id, 12999 io->io_hdr.nexus.targ_lun); 13000 io->io_hdr.port_status = 31338; 13001 ctl_send_datamove_done(io, /*have_lock*/ 0); 13002 return; 13003 } 13004 13005 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13006 ctl_datamove_remote_write(io); 13007 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13008 ctl_datamove_remote_read(io); 13009 } else { 13010 union ctl_ha_msg msg; 13011 struct scsi_sense_data *sense; 13012 uint8_t sks[3]; 13013 int retry_count; 13014 13015 memset(&msg, 0, sizeof(msg)); 13016 13017 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13018 msg.hdr.status = CTL_SCSI_ERROR; 13019 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13020 13021 retry_count = 4243; 13022 13023 sense = &msg.scsi.sense_data; 13024 sks[0] = SSD_SCS_VALID; 13025 sks[1] = (retry_count >> 8) & 0xff; 13026 sks[2] = retry_count & 0xff; 13027 13028 /* "Internal target failure" */ 13029 scsi_set_sense_data(sense, 13030 /*sense_format*/ SSD_TYPE_NONE, 13031 /*current_error*/ 1, 13032 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13033 /*asc*/ 0x44, 13034 /*ascq*/ 0x00, 13035 /*type*/ SSD_ELEM_SKS, 13036 /*size*/ sizeof(sks), 13037 /*data*/ sks, 13038 SSD_ELEM_NONE); 13039 13040 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13041 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13042 ctl_failover_io(io, /*have_lock*/ 1); 13043 return; 13044 } 13045 13046 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13047 CTL_HA_STATUS_SUCCESS) { 13048 /* XXX KDM what to do if this fails? */ 13049 } 13050 return; 13051 } 13052 13053} 13054 13055static int 13056ctl_process_done(union ctl_io *io) 13057{ 13058 struct ctl_lun *lun; 13059 struct ctl_softc *softc = control_softc; 13060 void (*fe_done)(union ctl_io *io); 13061 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13062 13063 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13064 13065 fe_done = softc->ctl_ports[targ_port]->fe_done; 13066 13067#ifdef CTL_TIME_IO 13068 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13069 char str[256]; 13070 char path_str[64]; 13071 struct sbuf sb; 13072 13073 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13074 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13075 13076 sbuf_cat(&sb, path_str); 13077 switch (io->io_hdr.io_type) { 13078 case CTL_IO_SCSI: 13079 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13080 sbuf_printf(&sb, "\n"); 13081 sbuf_cat(&sb, path_str); 13082 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13083 io->scsiio.tag_num, io->scsiio.tag_type); 13084 break; 13085 case CTL_IO_TASK: 13086 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13087 "Tag Type: %d\n", io->taskio.task_action, 13088 io->taskio.tag_num, io->taskio.tag_type); 13089 break; 13090 default: 13091 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13092 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13093 break; 13094 } 13095 sbuf_cat(&sb, path_str); 13096 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13097 (intmax_t)time_uptime - io->io_hdr.start_time); 13098 sbuf_finish(&sb); 13099 printf("%s", sbuf_data(&sb)); 13100 } 13101#endif /* CTL_TIME_IO */ 13102 13103 switch (io->io_hdr.io_type) { 13104 case CTL_IO_SCSI: 13105 break; 13106 case CTL_IO_TASK: 13107 if (ctl_debug & CTL_DEBUG_INFO) 13108 ctl_io_error_print(io, NULL); 13109 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13110 ctl_free_io(io); 13111 else 13112 fe_done(io); 13113 return (CTL_RETVAL_COMPLETE); 13114 default: 13115 panic("ctl_process_done: invalid io type %d\n", 13116 io->io_hdr.io_type); 13117 break; /* NOTREACHED */ 13118 } 13119 13120 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13121 if (lun == NULL) { 13122 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13123 io->io_hdr.nexus.targ_mapped_lun)); 13124 goto bailout; 13125 } 13126 13127 mtx_lock(&lun->lun_lock); 13128 13129 /* 13130 * Check to see if we have any errors to inject here. We only 13131 * inject errors for commands that don't already have errors set. 13132 */ 13133 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13134 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13135 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13136 ctl_inject_error(lun, io); 13137 13138 /* 13139 * XXX KDM how do we treat commands that aren't completed 13140 * successfully? 13141 * 13142 * XXX KDM should we also track I/O latency? 13143 */ 13144 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13145 io->io_hdr.io_type == CTL_IO_SCSI) { 13146#ifdef CTL_TIME_IO 13147 struct bintime cur_bt; 13148#endif 13149 int type; 13150 13151 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13152 CTL_FLAG_DATA_IN) 13153 type = CTL_STATS_READ; 13154 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13155 CTL_FLAG_DATA_OUT) 13156 type = CTL_STATS_WRITE; 13157 else 13158 type = CTL_STATS_NO_IO; 13159 13160 lun->stats.ports[targ_port].bytes[type] += 13161 io->scsiio.kern_total_len; 13162 lun->stats.ports[targ_port].operations[type]++; 13163#ifdef CTL_TIME_IO 13164 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13165 &io->io_hdr.dma_bt); 13166 lun->stats.ports[targ_port].num_dmas[type] += 13167 io->io_hdr.num_dmas; 13168 getbintime(&cur_bt); 13169 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13170 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13171#endif 13172 } 13173 13174 /* 13175 * Remove this from the OOA queue. 13176 */ 13177 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13178#ifdef CTL_TIME_IO 13179 if (TAILQ_EMPTY(&lun->ooa_queue)) 13180 lun->last_busy = getsbinuptime(); 13181#endif 13182 13183 /* 13184 * Run through the blocked queue on this LUN and see if anything 13185 * has become unblocked, now that this transaction is done. 13186 */ 13187 ctl_check_blocked(lun); 13188 13189 /* 13190 * If the LUN has been invalidated, free it if there is nothing 13191 * left on its OOA queue. 13192 */ 13193 if ((lun->flags & CTL_LUN_INVALID) 13194 && TAILQ_EMPTY(&lun->ooa_queue)) { 13195 mtx_unlock(&lun->lun_lock); 13196 mtx_lock(&softc->ctl_lock); 13197 ctl_free_lun(lun); 13198 mtx_unlock(&softc->ctl_lock); 13199 } else 13200 mtx_unlock(&lun->lun_lock); 13201 13202bailout: 13203 13204 /* 13205 * If this command has been aborted, make sure we set the status 13206 * properly. The FETD is responsible for freeing the I/O and doing 13207 * whatever it needs to do to clean up its state. 13208 */ 13209 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13210 ctl_set_task_aborted(&io->scsiio); 13211 13212 /* 13213 * If enabled, print command error status. 13214 */ 13215 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13216 (ctl_debug & CTL_DEBUG_INFO) != 0) 13217 ctl_io_error_print(io, NULL); 13218 13219 /* 13220 * Tell the FETD or the other shelf controller we're done with this 13221 * command. Note that only SCSI commands get to this point. Task 13222 * management commands are completed above. 13223 * 13224 * We only send status to the other controller if we're in XFER 13225 * mode. In SER_ONLY mode, the I/O is done on the controller that 13226 * received the I/O (from CTL's perspective), and so the status is 13227 * generated there. 13228 * 13229 * XXX KDM if we hold the lock here, we could cause a deadlock 13230 * if the frontend comes back in in this context to queue 13231 * something. 13232 */ 13233 if ((softc->ha_mode == CTL_HA_MODE_XFER) 13234 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13235 union ctl_ha_msg msg; 13236 13237 memset(&msg, 0, sizeof(msg)); 13238 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13239 msg.hdr.original_sc = io->io_hdr.original_sc; 13240 msg.hdr.nexus = io->io_hdr.nexus; 13241 msg.hdr.status = io->io_hdr.status; 13242 msg.scsi.scsi_status = io->scsiio.scsi_status; 13243 msg.scsi.tag_num = io->scsiio.tag_num; 13244 msg.scsi.tag_type = io->scsiio.tag_type; 13245 msg.scsi.sense_len = io->scsiio.sense_len; 13246 msg.scsi.sense_residual = io->scsiio.sense_residual; 13247 msg.scsi.residual = io->scsiio.residual; 13248 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13249 sizeof(io->scsiio.sense_data)); 13250 /* 13251 * We copy this whether or not this is an I/O-related 13252 * command. Otherwise, we'd have to go and check to see 13253 * whether it's a read/write command, and it really isn't 13254 * worth it. 13255 */ 13256 memcpy(&msg.scsi.lbalen, 13257 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13258 sizeof(msg.scsi.lbalen)); 13259 13260 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13261 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13262 /* XXX do something here */ 13263 } 13264 13265 ctl_free_io(io); 13266 } else 13267 fe_done(io); 13268 13269 return (CTL_RETVAL_COMPLETE); 13270} 13271 13272#ifdef CTL_WITH_CA 13273/* 13274 * Front end should call this if it doesn't do autosense. When the request 13275 * sense comes back in from the initiator, we'll dequeue this and send it. 13276 */ 13277int 13278ctl_queue_sense(union ctl_io *io) 13279{ 13280 struct ctl_lun *lun; 13281 struct ctl_port *port; 13282 struct ctl_softc *softc; 13283 uint32_t initidx, targ_lun; 13284 13285 softc = control_softc; 13286 13287 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13288 13289 /* 13290 * LUN lookup will likely move to the ctl_work_thread() once we 13291 * have our new queueing infrastructure (that doesn't put things on 13292 * a per-LUN queue initially). That is so that we can handle 13293 * things like an INQUIRY to a LUN that we don't have enabled. We 13294 * can't deal with that right now. 13295 */ 13296 mtx_lock(&softc->ctl_lock); 13297 13298 /* 13299 * If we don't have a LUN for this, just toss the sense 13300 * information. 13301 */ 13302 port = ctl_io_port(&ctsio->io_hdr); 13303 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13304 if ((targ_lun < CTL_MAX_LUNS) 13305 && (softc->ctl_luns[targ_lun] != NULL)) 13306 lun = softc->ctl_luns[targ_lun]; 13307 else 13308 goto bailout; 13309 13310 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13311 13312 mtx_lock(&lun->lun_lock); 13313 /* 13314 * Already have CA set for this LUN...toss the sense information. 13315 */ 13316 if (ctl_is_set(lun->have_ca, initidx)) { 13317 mtx_unlock(&lun->lun_lock); 13318 goto bailout; 13319 } 13320 13321 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13322 MIN(sizeof(lun->pending_sense[initidx]), 13323 sizeof(io->scsiio.sense_data))); 13324 ctl_set_mask(lun->have_ca, initidx); 13325 mtx_unlock(&lun->lun_lock); 13326 13327bailout: 13328 mtx_unlock(&softc->ctl_lock); 13329 13330 ctl_free_io(io); 13331 13332 return (CTL_RETVAL_COMPLETE); 13333} 13334#endif 13335 13336/* 13337 * Primary command inlet from frontend ports. All SCSI and task I/O 13338 * requests must go through this function. 13339 */ 13340int 13341ctl_queue(union ctl_io *io) 13342{ 13343 struct ctl_port *port; 13344 13345 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13346 13347#ifdef CTL_TIME_IO 13348 io->io_hdr.start_time = time_uptime; 13349 getbintime(&io->io_hdr.start_bt); 13350#endif /* CTL_TIME_IO */ 13351 13352 /* Map FE-specific LUN ID into global one. */ 13353 port = ctl_io_port(&io->io_hdr); 13354 io->io_hdr.nexus.targ_mapped_lun = 13355 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13356 13357 switch (io->io_hdr.io_type) { 13358 case CTL_IO_SCSI: 13359 case CTL_IO_TASK: 13360 if (ctl_debug & CTL_DEBUG_CDB) 13361 ctl_io_print(io); 13362 ctl_enqueue_incoming(io); 13363 break; 13364 default: 13365 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13366 return (EINVAL); 13367 } 13368 13369 return (CTL_RETVAL_COMPLETE); 13370} 13371 13372#ifdef CTL_IO_DELAY 13373static void 13374ctl_done_timer_wakeup(void *arg) 13375{ 13376 union ctl_io *io; 13377 13378 io = (union ctl_io *)arg; 13379 ctl_done(io); 13380} 13381#endif /* CTL_IO_DELAY */ 13382 13383void 13384ctl_done(union ctl_io *io) 13385{ 13386 13387 /* 13388 * Enable this to catch duplicate completion issues. 13389 */ 13390#if 0 13391 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13392 printf("%s: type %d msg %d cdb %x iptl: " 13393 "%d:%d:%d:%d tag 0x%04x " 13394 "flag %#x status %x\n", 13395 __func__, 13396 io->io_hdr.io_type, 13397 io->io_hdr.msg_type, 13398 io->scsiio.cdb[0], 13399 io->io_hdr.nexus.initid.id, 13400 io->io_hdr.nexus.targ_port, 13401 io->io_hdr.nexus.targ_target.id, 13402 io->io_hdr.nexus.targ_lun, 13403 (io->io_hdr.io_type == 13404 CTL_IO_TASK) ? 13405 io->taskio.tag_num : 13406 io->scsiio.tag_num, 13407 io->io_hdr.flags, 13408 io->io_hdr.status); 13409 } else 13410 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13411#endif 13412 13413 /* 13414 * This is an internal copy of an I/O, and should not go through 13415 * the normal done processing logic. 13416 */ 13417 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13418 return; 13419 13420 /* 13421 * We need to send a msg to the serializing shelf to finish the IO 13422 * as well. We don't send a finish message to the other shelf if 13423 * this is a task management command. Task management commands 13424 * aren't serialized in the OOA queue, but rather just executed on 13425 * both shelf controllers for commands that originated on that 13426 * controller. 13427 */ 13428 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13429 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13430 union ctl_ha_msg msg_io; 13431 13432 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13433 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13434 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13435 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13436 } 13437 /* continue on to finish IO */ 13438 } 13439#ifdef CTL_IO_DELAY 13440 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13441 struct ctl_lun *lun; 13442 13443 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13444 13445 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13446 } else { 13447 struct ctl_lun *lun; 13448 13449 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13450 13451 if ((lun != NULL) 13452 && (lun->delay_info.done_delay > 0)) { 13453 struct callout *callout; 13454 13455 callout = (struct callout *)&io->io_hdr.timer_bytes; 13456 callout_init(callout, /*mpsafe*/ 1); 13457 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13458 callout_reset(callout, 13459 lun->delay_info.done_delay * hz, 13460 ctl_done_timer_wakeup, io); 13461 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13462 lun->delay_info.done_delay = 0; 13463 return; 13464 } 13465 } 13466#endif /* CTL_IO_DELAY */ 13467 13468 ctl_enqueue_done(io); 13469} 13470 13471int 13472ctl_isc(struct ctl_scsiio *ctsio) 13473{ 13474 struct ctl_lun *lun; 13475 int retval; 13476 13477 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13478 13479 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13480 13481 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13482 13483 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13484 13485 return (retval); 13486} 13487 13488 13489static void 13490ctl_work_thread(void *arg) 13491{ 13492 struct ctl_thread *thr = (struct ctl_thread *)arg; 13493 struct ctl_softc *softc = thr->ctl_softc; 13494 union ctl_io *io; 13495 int retval; 13496 13497 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13498 13499 for (;;) { 13500 retval = 0; 13501 13502 /* 13503 * We handle the queues in this order: 13504 * - ISC 13505 * - done queue (to free up resources, unblock other commands) 13506 * - RtR queue 13507 * - incoming queue 13508 * 13509 * If those queues are empty, we break out of the loop and 13510 * go to sleep. 13511 */ 13512 mtx_lock(&thr->queue_lock); 13513 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13514 if (io != NULL) { 13515 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13516 mtx_unlock(&thr->queue_lock); 13517 ctl_handle_isc(io); 13518 continue; 13519 } 13520 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13521 if (io != NULL) { 13522 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13523 /* clear any blocked commands, call fe_done */ 13524 mtx_unlock(&thr->queue_lock); 13525 retval = ctl_process_done(io); 13526 continue; 13527 } 13528 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13529 if (io != NULL) { 13530 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13531 mtx_unlock(&thr->queue_lock); 13532 if (io->io_hdr.io_type == CTL_IO_TASK) 13533 ctl_run_task(io); 13534 else 13535 ctl_scsiio_precheck(softc, &io->scsiio); 13536 continue; 13537 } 13538 if (!ctl_pause_rtr) { 13539 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13540 if (io != NULL) { 13541 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13542 mtx_unlock(&thr->queue_lock); 13543 retval = ctl_scsiio(&io->scsiio); 13544 if (retval != CTL_RETVAL_COMPLETE) 13545 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13546 continue; 13547 } 13548 } 13549 13550 /* Sleep until we have something to do. */ 13551 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13552 } 13553} 13554 13555static void 13556ctl_lun_thread(void *arg) 13557{ 13558 struct ctl_softc *softc = (struct ctl_softc *)arg; 13559 struct ctl_be_lun *be_lun; 13560 int retval; 13561 13562 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13563 13564 for (;;) { 13565 retval = 0; 13566 mtx_lock(&softc->ctl_lock); 13567 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13568 if (be_lun != NULL) { 13569 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13570 mtx_unlock(&softc->ctl_lock); 13571 ctl_create_lun(be_lun); 13572 continue; 13573 } 13574 13575 /* Sleep until we have something to do. */ 13576 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13577 PDROP | PRIBIO, "-", 0); 13578 } 13579} 13580 13581static void 13582ctl_thresh_thread(void *arg) 13583{ 13584 struct ctl_softc *softc = (struct ctl_softc *)arg; 13585 struct ctl_lun *lun; 13586 struct ctl_be_lun *be_lun; 13587 struct scsi_da_rw_recovery_page *rwpage; 13588 struct ctl_logical_block_provisioning_page *page; 13589 const char *attr; 13590 uint64_t thres, val; 13591 int i, e; 13592 13593 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13594 13595 for (;;) { 13596 mtx_lock(&softc->ctl_lock); 13597 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13598 be_lun = lun->be_lun; 13599 if ((lun->flags & CTL_LUN_DISABLED) || 13600 (lun->flags & CTL_LUN_OFFLINE) || 13601 lun->backend->lun_attr == NULL) 13602 continue; 13603 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13604 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13605 continue; 13606 e = 0; 13607 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13608 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13609 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13610 continue; 13611 thres = scsi_4btoul(page->descr[i].count); 13612 thres <<= CTL_LBP_EXPONENT; 13613 switch (page->descr[i].resource) { 13614 case 0x01: 13615 attr = "blocksavail"; 13616 break; 13617 case 0x02: 13618 attr = "blocksused"; 13619 break; 13620 case 0xf1: 13621 attr = "poolblocksavail"; 13622 break; 13623 case 0xf2: 13624 attr = "poolblocksused"; 13625 break; 13626 default: 13627 continue; 13628 } 13629 mtx_unlock(&softc->ctl_lock); // XXX 13630 val = lun->backend->lun_attr( 13631 lun->be_lun->be_lun, attr); 13632 mtx_lock(&softc->ctl_lock); 13633 if (val == UINT64_MAX) 13634 continue; 13635 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13636 == SLBPPD_ARMING_INC) 13637 e |= (val >= thres); 13638 else 13639 e |= (val <= thres); 13640 } 13641 mtx_lock(&lun->lun_lock); 13642 if (e) { 13643 if (lun->lasttpt == 0 || 13644 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13645 lun->lasttpt = time_uptime; 13646 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13647 } 13648 } else { 13649 lun->lasttpt = 0; 13650 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13651 } 13652 mtx_unlock(&lun->lun_lock); 13653 } 13654 mtx_unlock(&softc->ctl_lock); 13655 pause("-", CTL_LBP_PERIOD * hz); 13656 } 13657} 13658 13659static void 13660ctl_enqueue_incoming(union ctl_io *io) 13661{ 13662 struct ctl_softc *softc = control_softc; 13663 struct ctl_thread *thr; 13664 u_int idx; 13665 13666 idx = (io->io_hdr.nexus.targ_port * 127 + 13667 io->io_hdr.nexus.initid.id) % worker_threads; 13668 thr = &softc->threads[idx]; 13669 mtx_lock(&thr->queue_lock); 13670 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13671 mtx_unlock(&thr->queue_lock); 13672 wakeup(thr); 13673} 13674 13675static void 13676ctl_enqueue_rtr(union ctl_io *io) 13677{ 13678 struct ctl_softc *softc = control_softc; 13679 struct ctl_thread *thr; 13680 13681 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13682 mtx_lock(&thr->queue_lock); 13683 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13684 mtx_unlock(&thr->queue_lock); 13685 wakeup(thr); 13686} 13687 13688static void 13689ctl_enqueue_done(union ctl_io *io) 13690{ 13691 struct ctl_softc *softc = control_softc; 13692 struct ctl_thread *thr; 13693 13694 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13695 mtx_lock(&thr->queue_lock); 13696 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13697 mtx_unlock(&thr->queue_lock); 13698 wakeup(thr); 13699} 13700 13701#ifdef notyet 13702static void 13703ctl_enqueue_isc(union ctl_io *io) 13704{ 13705 struct ctl_softc *softc = control_softc; 13706 struct ctl_thread *thr; 13707 13708 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13709 mtx_lock(&thr->queue_lock); 13710 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13711 mtx_unlock(&thr->queue_lock); 13712 wakeup(thr); 13713} 13714 13715/* Initialization and failover */ 13716 13717void 13718ctl_init_isc_msg(void) 13719{ 13720 printf("CTL: Still calling this thing\n"); 13721} 13722 13723/* 13724 * Init component 13725 * Initializes component into configuration defined by bootMode 13726 * (see hasc-sv.c) 13727 * returns hasc_Status: 13728 * OK 13729 * ERROR - fatal error 13730 */ 13731static ctl_ha_comp_status 13732ctl_isc_init(struct ctl_ha_component *c) 13733{ 13734 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13735 13736 c->status = ret; 13737 return ret; 13738} 13739 13740/* Start component 13741 * Starts component in state requested. If component starts successfully, 13742 * it must set its own state to the requestrd state 13743 * When requested state is HASC_STATE_HA, the component may refine it 13744 * by adding _SLAVE or _MASTER flags. 13745 * Currently allowed state transitions are: 13746 * UNKNOWN->HA - initial startup 13747 * UNKNOWN->SINGLE - initial startup when no parter detected 13748 * HA->SINGLE - failover 13749 * returns ctl_ha_comp_status: 13750 * OK - component successfully started in requested state 13751 * FAILED - could not start the requested state, failover may 13752 * be possible 13753 * ERROR - fatal error detected, no future startup possible 13754 */ 13755static ctl_ha_comp_status 13756ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 13757{ 13758 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13759 13760 printf("%s: go\n", __func__); 13761 13762 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 13763 if (c->state == CTL_HA_STATE_UNKNOWN ) { 13764 control_softc->is_single = 0; 13765 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 13766 != CTL_HA_STATUS_SUCCESS) { 13767 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 13768 ret = CTL_HA_COMP_STATUS_ERROR; 13769 } 13770 } else if (CTL_HA_STATE_IS_HA(c->state) 13771 && CTL_HA_STATE_IS_SINGLE(state)){ 13772 // HA->SINGLE transition 13773 ctl_failover(); 13774 control_softc->is_single = 1; 13775 } else { 13776 printf("ctl_isc_start:Invalid state transition %X->%X\n", 13777 c->state, state); 13778 ret = CTL_HA_COMP_STATUS_ERROR; 13779 } 13780 if (CTL_HA_STATE_IS_SINGLE(state)) 13781 control_softc->is_single = 1; 13782 13783 c->state = state; 13784 c->status = ret; 13785 return ret; 13786} 13787 13788/* 13789 * Quiesce component 13790 * The component must clear any error conditions (set status to OK) and 13791 * prepare itself to another Start call 13792 * returns ctl_ha_comp_status: 13793 * OK 13794 * ERROR 13795 */ 13796static ctl_ha_comp_status 13797ctl_isc_quiesce(struct ctl_ha_component *c) 13798{ 13799 int ret = CTL_HA_COMP_STATUS_OK; 13800 13801 ctl_pause_rtr = 1; 13802 c->status = ret; 13803 return ret; 13804} 13805 13806struct ctl_ha_component ctl_ha_component_ctlisc = 13807{ 13808 .name = "CTL ISC", 13809 .state = CTL_HA_STATE_UNKNOWN, 13810 .init = ctl_isc_init, 13811 .start = ctl_isc_start, 13812 .quiesce = ctl_isc_quiesce 13813}; 13814#endif 13815 13816/* 13817 * vim: ts=8 13818 */ 13819