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: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 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$"); 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 68 #include <cam/cam.h> 69 #include <cam/scsi/scsi_all.h> 70 #include <cam/scsi/scsi_da.h> 71 #include <cam/ctl/ctl_io.h> 72 #include <cam/ctl/ctl.h> 73 #include <cam/ctl/ctl_frontend.h> 74 #include <cam/ctl/ctl_frontend_internal.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 84 struct 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 */ 113 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 123 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 132 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*/0, 141 /*write_retry_count*/0, 142 /*reserved2*/0, 143 /*recovery_time_limit*/{0, 0}, 144 }; 145 146 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 160 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 177 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 193 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 210 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 226 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 242 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 258 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 270 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 282 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 291 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 static struct scsi_logical_block_provisioning_page lbp_page_default = { 301 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 302 /*subpage_code*/0x02, 303 /*page_length*/{0, sizeof(struct scsi_logical_block_provisioning_page) - 4}, 304 /*flags*/0, 305 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 306 /*descr*/{} 307 }; 308 309 static struct scsi_logical_block_provisioning_page lbp_page_changeable = { 310 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 311 /*subpage_code*/0x02, 312 /*page_length*/{0, sizeof(struct scsi_logical_block_provisioning_page) - 4}, 313 /*flags*/0, 314 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 315 /*descr*/{} 316 }; 317 318 /* 319 * XXX KDM move these into the softc. 320 */ 321 static int rcv_sync_msg; 322 static int persis_offset; 323 static uint8_t ctl_pause_rtr; 324 static int ctl_is_single = 1; 325 326 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 327 static int worker_threads = -1; 328 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 329 &worker_threads, 1, "Number of worker threads"); 330 static int ctl_debug = CTL_DEBUG_NONE; 331 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 332 &ctl_debug, 0, "Enabled debug flags"); 333 334 /* 335 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 336 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 337 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 338 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 339 */ 340 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 341 342 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 343 int param); 344 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 345 static int ctl_init(void); 346 void ctl_shutdown(void); 347 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 348 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 349 static void ctl_ioctl_online(void *arg); 350 static void ctl_ioctl_offline(void *arg); 351 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 352 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 353 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 354 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 355 static int ctl_ioctl_submit_wait(union ctl_io *io); 356 static void ctl_ioctl_datamove(union ctl_io *io); 357 static void ctl_ioctl_done(union ctl_io *io); 358 static void ctl_ioctl_hard_startstop_callback(void *arg, 359 struct cfi_metatask *metatask); 360 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 361 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 362 struct ctl_ooa *ooa_hdr, 363 struct ctl_ooa_entry *kern_entries); 364 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 365 struct thread *td); 366 static uint32_t ctl_map_lun(int port_num, uint32_t lun); 367 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 368 #ifdef unused 369 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 370 uint32_t targ_target, uint32_t targ_lun, 371 int can_wait); 372 static void ctl_kfree_io(union ctl_io *io); 373 #endif /* unused */ 374 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 375 struct ctl_be_lun *be_lun, struct ctl_id target_id); 376 static int ctl_free_lun(struct ctl_lun *lun); 377 static void ctl_create_lun(struct ctl_be_lun *be_lun); 378 /** 379 static void ctl_failover_change_pages(struct ctl_softc *softc, 380 struct ctl_scsiio *ctsio, int master); 381 **/ 382 383 static int ctl_do_mode_select(union ctl_io *io); 384 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 385 uint64_t res_key, uint64_t sa_res_key, 386 uint8_t type, uint32_t residx, 387 struct ctl_scsiio *ctsio, 388 struct scsi_per_res_out *cdb, 389 struct scsi_per_res_out_parms* param); 390 static void ctl_pro_preempt_other(struct ctl_lun *lun, 391 union ctl_ha_msg *msg); 392 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 393 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 394 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 395 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 396 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 397 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 398 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 399 int alloc_len); 400 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 401 int alloc_len); 402 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 403 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 404 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 405 static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 406 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 407 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 408 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 409 union ctl_io *pending_io, union ctl_io *ooa_io); 410 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 411 union ctl_io *starting_io); 412 static int ctl_check_blocked(struct ctl_lun *lun); 413 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 414 struct ctl_lun *lun, 415 const struct ctl_cmd_entry *entry, 416 struct ctl_scsiio *ctsio); 417 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 418 static void ctl_failover(void); 419 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 420 struct ctl_scsiio *ctsio); 421 static int ctl_scsiio(struct ctl_scsiio *ctsio); 422 423 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 424 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 425 ctl_ua_type ua_type); 426 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 427 ctl_ua_type ua_type); 428 static int ctl_abort_task(union ctl_io *io); 429 static int ctl_abort_task_set(union ctl_io *io); 430 static int ctl_i_t_nexus_reset(union ctl_io *io); 431 static void ctl_run_task(union ctl_io *io); 432 #ifdef CTL_IO_DELAY 433 static void ctl_datamove_timer_wakeup(void *arg); 434 static void ctl_done_timer_wakeup(void *arg); 435 #endif /* CTL_IO_DELAY */ 436 437 static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 438 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 439 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 440 static void ctl_datamove_remote_write(union ctl_io *io); 441 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 442 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 443 static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 444 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 445 ctl_ha_dt_cb callback); 446 static void ctl_datamove_remote_read(union ctl_io *io); 447 static void ctl_datamove_remote(union ctl_io *io); 448 static int ctl_process_done(union ctl_io *io); 449 static void ctl_lun_thread(void *arg); 450 static void ctl_work_thread(void *arg); 451 static void ctl_enqueue_incoming(union ctl_io *io); 452 static void ctl_enqueue_rtr(union ctl_io *io); 453 static void ctl_enqueue_done(union ctl_io *io); 454 static void ctl_enqueue_isc(union ctl_io *io); 455 static const struct ctl_cmd_entry * 456 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 457 static const struct ctl_cmd_entry * 458 ctl_validate_command(struct ctl_scsiio *ctsio); 459 static int ctl_cmd_applicable(uint8_t lun_type, 460 const struct ctl_cmd_entry *entry); 461 462 /* 463 * Load the serialization table. This isn't very pretty, but is probably 464 * the easiest way to do it. 465 */ 466 #include "ctl_ser_table.c" 467 468 /* 469 * We only need to define open, close and ioctl routines for this driver. 470 */ 471 static struct cdevsw ctl_cdevsw = { 472 .d_version = D_VERSION, 473 .d_flags = 0, 474 .d_open = ctl_open, 475 .d_close = ctl_close, 476 .d_ioctl = ctl_ioctl, 477 .d_name = "ctl", 478 }; 479 480 481 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 482 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 483 484 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 485 486 static moduledata_t ctl_moduledata = { 487 "ctl", 488 ctl_module_event_handler, 489 NULL 490 }; 491 492 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 493 MODULE_VERSION(ctl, 1); 494 495 static struct ctl_frontend ioctl_frontend = 496 { 497 .name = "ioctl", 498 }; 499 500 static void 501 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 502 union ctl_ha_msg *msg_info) 503 { 504 struct ctl_scsiio *ctsio; 505 506 if (msg_info->hdr.original_sc == NULL) { 507 printf("%s: original_sc == NULL!\n", __func__); 508 /* XXX KDM now what? */ 509 return; 510 } 511 512 ctsio = &msg_info->hdr.original_sc->scsiio; 513 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 514 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 515 ctsio->io_hdr.status = msg_info->hdr.status; 516 ctsio->scsi_status = msg_info->scsi.scsi_status; 517 ctsio->sense_len = msg_info->scsi.sense_len; 518 ctsio->sense_residual = msg_info->scsi.sense_residual; 519 ctsio->residual = msg_info->scsi.residual; 520 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 521 sizeof(ctsio->sense_data)); 522 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 523 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 524 ctl_enqueue_isc((union ctl_io *)ctsio); 525 } 526 527 static void 528 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 529 union ctl_ha_msg *msg_info) 530 { 531 struct ctl_scsiio *ctsio; 532 533 if (msg_info->hdr.serializing_sc == NULL) { 534 printf("%s: serializing_sc == NULL!\n", __func__); 535 /* XXX KDM now what? */ 536 return; 537 } 538 539 ctsio = &msg_info->hdr.serializing_sc->scsiio; 540 #if 0 541 /* 542 * Attempt to catch the situation where an I/O has 543 * been freed, and we're using it again. 544 */ 545 if (ctsio->io_hdr.io_type == 0xff) { 546 union ctl_io *tmp_io; 547 tmp_io = (union ctl_io *)ctsio; 548 printf("%s: %p use after free!\n", __func__, 549 ctsio); 550 printf("%s: type %d msg %d cdb %x iptl: " 551 "%d:%d:%d:%d tag 0x%04x " 552 "flag %#x status %x\n", 553 __func__, 554 tmp_io->io_hdr.io_type, 555 tmp_io->io_hdr.msg_type, 556 tmp_io->scsiio.cdb[0], 557 tmp_io->io_hdr.nexus.initid.id, 558 tmp_io->io_hdr.nexus.targ_port, 559 tmp_io->io_hdr.nexus.targ_target.id, 560 tmp_io->io_hdr.nexus.targ_lun, 561 (tmp_io->io_hdr.io_type == 562 CTL_IO_TASK) ? 563 tmp_io->taskio.tag_num : 564 tmp_io->scsiio.tag_num, 565 tmp_io->io_hdr.flags, 566 tmp_io->io_hdr.status); 567 } 568 #endif 569 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 570 ctl_enqueue_isc((union ctl_io *)ctsio); 571 } 572 573 /* 574 * ISC (Inter Shelf Communication) event handler. Events from the HA 575 * subsystem come in here. 576 */ 577 static void 578 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 579 { 580 struct ctl_softc *ctl_softc; 581 union ctl_io *io; 582 struct ctl_prio *presio; 583 ctl_ha_status isc_status; 584 585 ctl_softc = control_softc; 586 io = NULL; 587 588 589 #if 0 590 printf("CTL: Isc Msg event %d\n", event); 591 #endif 592 if (event == CTL_HA_EVT_MSG_RECV) { 593 union ctl_ha_msg msg_info; 594 595 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 596 sizeof(msg_info), /*wait*/ 0); 597 #if 0 598 printf("CTL: msg_type %d\n", msg_info.msg_type); 599 #endif 600 if (isc_status != 0) { 601 printf("Error receiving message, status = %d\n", 602 isc_status); 603 return; 604 } 605 606 switch (msg_info.hdr.msg_type) { 607 case CTL_MSG_SERIALIZE: 608 #if 0 609 printf("Serialize\n"); 610 #endif 611 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 612 if (io == NULL) { 613 printf("ctl_isc_event_handler: can't allocate " 614 "ctl_io!\n"); 615 /* Bad Juju */ 616 /* Need to set busy and send msg back */ 617 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 618 msg_info.hdr.status = CTL_SCSI_ERROR; 619 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 620 msg_info.scsi.sense_len = 0; 621 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 622 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 623 } 624 goto bailout; 625 } 626 ctl_zero_io(io); 627 // populate ctsio from msg_info 628 io->io_hdr.io_type = CTL_IO_SCSI; 629 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 630 io->io_hdr.original_sc = msg_info.hdr.original_sc; 631 #if 0 632 printf("pOrig %x\n", (int)msg_info.original_sc); 633 #endif 634 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 635 CTL_FLAG_IO_ACTIVE; 636 /* 637 * If we're in serialization-only mode, we don't 638 * want to go through full done processing. Thus 639 * the COPY flag. 640 * 641 * XXX KDM add another flag that is more specific. 642 */ 643 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 644 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 645 io->io_hdr.nexus = msg_info.hdr.nexus; 646 #if 0 647 printf("targ %d, port %d, iid %d, lun %d\n", 648 io->io_hdr.nexus.targ_target.id, 649 io->io_hdr.nexus.targ_port, 650 io->io_hdr.nexus.initid.id, 651 io->io_hdr.nexus.targ_lun); 652 #endif 653 io->scsiio.tag_num = msg_info.scsi.tag_num; 654 io->scsiio.tag_type = msg_info.scsi.tag_type; 655 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 656 CTL_MAX_CDBLEN); 657 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 658 const struct ctl_cmd_entry *entry; 659 660 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 661 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 662 io->io_hdr.flags |= 663 entry->flags & CTL_FLAG_DATA_MASK; 664 } 665 ctl_enqueue_isc(io); 666 break; 667 668 /* Performed on the Originating SC, XFER mode only */ 669 case CTL_MSG_DATAMOVE: { 670 struct ctl_sg_entry *sgl; 671 int i, j; 672 673 io = msg_info.hdr.original_sc; 674 if (io == NULL) { 675 printf("%s: original_sc == NULL!\n", __func__); 676 /* XXX KDM do something here */ 677 break; 678 } 679 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 680 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 681 /* 682 * Keep track of this, we need to send it back over 683 * when the datamove is complete. 684 */ 685 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 686 687 if (msg_info.dt.sg_sequence == 0) { 688 /* 689 * XXX KDM we use the preallocated S/G list 690 * here, but we'll need to change this to 691 * dynamic allocation if we need larger S/G 692 * lists. 693 */ 694 if (msg_info.dt.kern_sg_entries > 695 sizeof(io->io_hdr.remote_sglist) / 696 sizeof(io->io_hdr.remote_sglist[0])) { 697 printf("%s: number of S/G entries " 698 "needed %u > allocated num %zd\n", 699 __func__, 700 msg_info.dt.kern_sg_entries, 701 sizeof(io->io_hdr.remote_sglist)/ 702 sizeof(io->io_hdr.remote_sglist[0])); 703 704 /* 705 * XXX KDM send a message back to 706 * the other side to shut down the 707 * DMA. The error will come back 708 * through via the normal channel. 709 */ 710 break; 711 } 712 sgl = io->io_hdr.remote_sglist; 713 memset(sgl, 0, 714 sizeof(io->io_hdr.remote_sglist)); 715 716 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 717 718 io->scsiio.kern_sg_entries = 719 msg_info.dt.kern_sg_entries; 720 io->scsiio.rem_sg_entries = 721 msg_info.dt.kern_sg_entries; 722 io->scsiio.kern_data_len = 723 msg_info.dt.kern_data_len; 724 io->scsiio.kern_total_len = 725 msg_info.dt.kern_total_len; 726 io->scsiio.kern_data_resid = 727 msg_info.dt.kern_data_resid; 728 io->scsiio.kern_rel_offset = 729 msg_info.dt.kern_rel_offset; 730 /* 731 * Clear out per-DMA flags. 732 */ 733 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 734 /* 735 * Add per-DMA flags that are set for this 736 * particular DMA request. 737 */ 738 io->io_hdr.flags |= msg_info.dt.flags & 739 CTL_FLAG_RDMA_MASK; 740 } else 741 sgl = (struct ctl_sg_entry *) 742 io->scsiio.kern_data_ptr; 743 744 for (i = msg_info.dt.sent_sg_entries, j = 0; 745 i < (msg_info.dt.sent_sg_entries + 746 msg_info.dt.cur_sg_entries); i++, j++) { 747 sgl[i].addr = msg_info.dt.sg_list[j].addr; 748 sgl[i].len = msg_info.dt.sg_list[j].len; 749 750 #if 0 751 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 752 __func__, 753 msg_info.dt.sg_list[j].addr, 754 msg_info.dt.sg_list[j].len, 755 sgl[i].addr, sgl[i].len, j, i); 756 #endif 757 } 758 #if 0 759 memcpy(&sgl[msg_info.dt.sent_sg_entries], 760 msg_info.dt.sg_list, 761 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 762 #endif 763 764 /* 765 * If this is the last piece of the I/O, we've got 766 * the full S/G list. Queue processing in the thread. 767 * Otherwise wait for the next piece. 768 */ 769 if (msg_info.dt.sg_last != 0) 770 ctl_enqueue_isc(io); 771 break; 772 } 773 /* Performed on the Serializing (primary) SC, XFER mode only */ 774 case CTL_MSG_DATAMOVE_DONE: { 775 if (msg_info.hdr.serializing_sc == NULL) { 776 printf("%s: serializing_sc == NULL!\n", 777 __func__); 778 /* XXX KDM now what? */ 779 break; 780 } 781 /* 782 * We grab the sense information here in case 783 * there was a failure, so we can return status 784 * back to the initiator. 785 */ 786 io = msg_info.hdr.serializing_sc; 787 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 788 io->io_hdr.status = msg_info.hdr.status; 789 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 790 io->scsiio.sense_len = msg_info.scsi.sense_len; 791 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 792 io->io_hdr.port_status = msg_info.scsi.fetd_status; 793 io->scsiio.residual = msg_info.scsi.residual; 794 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 795 sizeof(io->scsiio.sense_data)); 796 ctl_enqueue_isc(io); 797 break; 798 } 799 800 /* Preformed on Originating SC, SER_ONLY mode */ 801 case CTL_MSG_R2R: 802 io = msg_info.hdr.original_sc; 803 if (io == NULL) { 804 printf("%s: Major Bummer\n", __func__); 805 return; 806 } else { 807 #if 0 808 printf("pOrig %x\n",(int) ctsio); 809 #endif 810 } 811 io->io_hdr.msg_type = CTL_MSG_R2R; 812 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 813 ctl_enqueue_isc(io); 814 break; 815 816 /* 817 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 818 * mode. 819 * Performed on the Originating (i.e. secondary) SC in XFER 820 * mode 821 */ 822 case CTL_MSG_FINISH_IO: 823 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 824 ctl_isc_handler_finish_xfer(ctl_softc, 825 &msg_info); 826 else 827 ctl_isc_handler_finish_ser_only(ctl_softc, 828 &msg_info); 829 break; 830 831 /* Preformed on Originating SC */ 832 case CTL_MSG_BAD_JUJU: 833 io = msg_info.hdr.original_sc; 834 if (io == NULL) { 835 printf("%s: Bad JUJU!, original_sc is NULL!\n", 836 __func__); 837 break; 838 } 839 ctl_copy_sense_data(&msg_info, io); 840 /* 841 * IO should have already been cleaned up on other 842 * SC so clear this flag so we won't send a message 843 * back to finish the IO there. 844 */ 845 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 846 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 847 848 /* io = msg_info.hdr.serializing_sc; */ 849 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 850 ctl_enqueue_isc(io); 851 break; 852 853 /* Handle resets sent from the other side */ 854 case CTL_MSG_MANAGE_TASKS: { 855 struct ctl_taskio *taskio; 856 taskio = (struct ctl_taskio *)ctl_alloc_io( 857 (void *)ctl_softc->othersc_pool); 858 if (taskio == NULL) { 859 printf("ctl_isc_event_handler: can't allocate " 860 "ctl_io!\n"); 861 /* Bad Juju */ 862 /* should I just call the proper reset func 863 here??? */ 864 goto bailout; 865 } 866 ctl_zero_io((union ctl_io *)taskio); 867 taskio->io_hdr.io_type = CTL_IO_TASK; 868 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 869 taskio->io_hdr.nexus = msg_info.hdr.nexus; 870 taskio->task_action = msg_info.task.task_action; 871 taskio->tag_num = msg_info.task.tag_num; 872 taskio->tag_type = msg_info.task.tag_type; 873 #ifdef CTL_TIME_IO 874 taskio->io_hdr.start_time = time_uptime; 875 getbintime(&taskio->io_hdr.start_bt); 876 #if 0 877 cs_prof_gettime(&taskio->io_hdr.start_ticks); 878 #endif 879 #endif /* CTL_TIME_IO */ 880 ctl_run_task((union ctl_io *)taskio); 881 break; 882 } 883 /* Persistent Reserve action which needs attention */ 884 case CTL_MSG_PERS_ACTION: 885 presio = (struct ctl_prio *)ctl_alloc_io( 886 (void *)ctl_softc->othersc_pool); 887 if (presio == NULL) { 888 printf("ctl_isc_event_handler: can't allocate " 889 "ctl_io!\n"); 890 /* Bad Juju */ 891 /* Need to set busy and send msg back */ 892 goto bailout; 893 } 894 ctl_zero_io((union ctl_io *)presio); 895 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 896 presio->pr_msg = msg_info.pr; 897 ctl_enqueue_isc((union ctl_io *)presio); 898 break; 899 case CTL_MSG_SYNC_FE: 900 rcv_sync_msg = 1; 901 break; 902 default: 903 printf("How did I get here?\n"); 904 } 905 } else if (event == CTL_HA_EVT_MSG_SENT) { 906 if (param != CTL_HA_STATUS_SUCCESS) { 907 printf("Bad status from ctl_ha_msg_send status %d\n", 908 param); 909 } 910 return; 911 } else if (event == CTL_HA_EVT_DISCONNECT) { 912 printf("CTL: Got a disconnect from Isc\n"); 913 return; 914 } else { 915 printf("ctl_isc_event_handler: Unknown event %d\n", event); 916 return; 917 } 918 919 bailout: 920 return; 921 } 922 923 static void 924 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 925 { 926 struct scsi_sense_data *sense; 927 928 sense = &dest->scsiio.sense_data; 929 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 930 dest->scsiio.scsi_status = src->scsi.scsi_status; 931 dest->scsiio.sense_len = src->scsi.sense_len; 932 dest->io_hdr.status = src->hdr.status; 933 } 934 935 static int 936 ctl_init(void) 937 { 938 struct ctl_softc *softc; 939 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 940 struct ctl_port *port; 941 uint8_t sc_id =0; 942 int i, error, retval; 943 //int isc_retval; 944 945 retval = 0; 946 ctl_pause_rtr = 0; 947 rcv_sync_msg = 0; 948 949 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 950 M_WAITOK | M_ZERO); 951 softc = control_softc; 952 953 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 954 "cam/ctl"); 955 956 softc->dev->si_drv1 = softc; 957 958 /* 959 * By default, return a "bad LUN" peripheral qualifier for unknown 960 * LUNs. The user can override this default using the tunable or 961 * sysctl. See the comment in ctl_inquiry_std() for more details. 962 */ 963 softc->inquiry_pq_no_lun = 1; 964 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 965 &softc->inquiry_pq_no_lun); 966 sysctl_ctx_init(&softc->sysctl_ctx); 967 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 968 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 969 CTLFLAG_RD, 0, "CAM Target Layer"); 970 971 if (softc->sysctl_tree == NULL) { 972 printf("%s: unable to allocate sysctl tree\n", __func__); 973 destroy_dev(softc->dev); 974 free(control_softc, M_DEVBUF); 975 control_softc = NULL; 976 return (ENOMEM); 977 } 978 979 SYSCTL_ADD_INT(&softc->sysctl_ctx, 980 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 981 "inquiry_pq_no_lun", CTLFLAG_RW, 982 &softc->inquiry_pq_no_lun, 0, 983 "Report no lun possible for invalid LUNs"); 984 985 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 986 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 987 softc->open_count = 0; 988 989 /* 990 * Default to actually sending a SYNCHRONIZE CACHE command down to 991 * the drive. 992 */ 993 softc->flags = CTL_FLAG_REAL_SYNC; 994 995 /* 996 * In Copan's HA scheme, the "master" and "slave" roles are 997 * figured out through the slot the controller is in. Although it 998 * is an active/active system, someone has to be in charge. 999 */ 1000 #ifdef NEEDTOPORT 1001 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1002 #endif 1003 1004 if (sc_id == 0) { 1005 softc->flags |= CTL_FLAG_MASTER_SHELF; 1006 persis_offset = 0; 1007 } else 1008 persis_offset = CTL_MAX_INITIATORS; 1009 1010 /* 1011 * XXX KDM need to figure out where we want to get our target ID 1012 * and WWID. Is it different on each port? 1013 */ 1014 softc->target.id = 0; 1015 softc->target.wwid[0] = 0x12345678; 1016 softc->target.wwid[1] = 0x87654321; 1017 STAILQ_INIT(&softc->lun_list); 1018 STAILQ_INIT(&softc->pending_lun_queue); 1019 STAILQ_INIT(&softc->fe_list); 1020 STAILQ_INIT(&softc->port_list); 1021 STAILQ_INIT(&softc->be_list); 1022 STAILQ_INIT(&softc->io_pools); 1023 ctl_tpc_init(softc); 1024 1025 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1026 &internal_pool)!= 0){ 1027 printf("ctl: can't allocate %d entry internal pool, " 1028 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1029 return (ENOMEM); 1030 } 1031 1032 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1033 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1034 printf("ctl: can't allocate %d entry emergency pool, " 1035 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1036 ctl_pool_free(internal_pool); 1037 return (ENOMEM); 1038 } 1039 1040 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1041 &other_pool) != 0) 1042 { 1043 printf("ctl: can't allocate %d entry other SC pool, " 1044 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1045 ctl_pool_free(internal_pool); 1046 ctl_pool_free(emergency_pool); 1047 return (ENOMEM); 1048 } 1049 1050 softc->internal_pool = internal_pool; 1051 softc->emergency_pool = emergency_pool; 1052 softc->othersc_pool = other_pool; 1053 1054 if (worker_threads <= 0) 1055 worker_threads = max(1, mp_ncpus / 4); 1056 if (worker_threads > CTL_MAX_THREADS) 1057 worker_threads = CTL_MAX_THREADS; 1058 1059 for (i = 0; i < worker_threads; i++) { 1060 struct ctl_thread *thr = &softc->threads[i]; 1061 1062 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1063 thr->ctl_softc = softc; 1064 STAILQ_INIT(&thr->incoming_queue); 1065 STAILQ_INIT(&thr->rtr_queue); 1066 STAILQ_INIT(&thr->done_queue); 1067 STAILQ_INIT(&thr->isc_queue); 1068 1069 error = kproc_kthread_add(ctl_work_thread, thr, 1070 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1071 if (error != 0) { 1072 printf("error creating CTL work thread!\n"); 1073 ctl_pool_free(internal_pool); 1074 ctl_pool_free(emergency_pool); 1075 ctl_pool_free(other_pool); 1076 return (error); 1077 } 1078 } 1079 error = kproc_kthread_add(ctl_lun_thread, softc, 1080 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1081 if (error != 0) { 1082 printf("error creating CTL lun thread!\n"); 1083 ctl_pool_free(internal_pool); 1084 ctl_pool_free(emergency_pool); 1085 ctl_pool_free(other_pool); 1086 return (error); 1087 } 1088 if (bootverbose) 1089 printf("ctl: CAM Target Layer loaded\n"); 1090 1091 /* 1092 * Initialize the ioctl front end. 1093 */ 1094 ctl_frontend_register(&ioctl_frontend); 1095 port = &softc->ioctl_info.port; 1096 port->frontend = &ioctl_frontend; 1097 sprintf(softc->ioctl_info.port_name, "ioctl"); 1098 port->port_type = CTL_PORT_IOCTL; 1099 port->num_requested_ctl_io = 100; 1100 port->port_name = softc->ioctl_info.port_name; 1101 port->port_online = ctl_ioctl_online; 1102 port->port_offline = ctl_ioctl_offline; 1103 port->onoff_arg = &softc->ioctl_info; 1104 port->lun_enable = ctl_ioctl_lun_enable; 1105 port->lun_disable = ctl_ioctl_lun_disable; 1106 port->targ_lun_arg = &softc->ioctl_info; 1107 port->fe_datamove = ctl_ioctl_datamove; 1108 port->fe_done = ctl_ioctl_done; 1109 port->max_targets = 15; 1110 port->max_target_id = 15; 1111 1112 if (ctl_port_register(&softc->ioctl_info.port, 1113 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1114 printf("ctl: ioctl front end registration failed, will " 1115 "continue anyway\n"); 1116 } 1117 1118 #ifdef CTL_IO_DELAY 1119 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1120 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1121 sizeof(struct callout), CTL_TIMER_BYTES); 1122 return (EINVAL); 1123 } 1124 #endif /* CTL_IO_DELAY */ 1125 1126 return (0); 1127 } 1128 1129 void 1130 ctl_shutdown(void) 1131 { 1132 struct ctl_softc *softc; 1133 struct ctl_lun *lun, *next_lun; 1134 struct ctl_io_pool *pool; 1135 1136 softc = (struct ctl_softc *)control_softc; 1137 1138 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1139 printf("ctl: ioctl front end deregistration failed\n"); 1140 1141 mtx_lock(&softc->ctl_lock); 1142 1143 /* 1144 * Free up each LUN. 1145 */ 1146 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1147 next_lun = STAILQ_NEXT(lun, links); 1148 ctl_free_lun(lun); 1149 } 1150 1151 mtx_unlock(&softc->ctl_lock); 1152 1153 ctl_frontend_deregister(&ioctl_frontend); 1154 1155 /* 1156 * This will rip the rug out from under any FETDs or anyone else 1157 * that has a pool allocated. Since we increment our module 1158 * refcount any time someone outside the main CTL module allocates 1159 * a pool, we shouldn't have any problems here. The user won't be 1160 * able to unload the CTL module until client modules have 1161 * successfully unloaded. 1162 */ 1163 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1164 ctl_pool_free(pool); 1165 1166 #if 0 1167 ctl_shutdown_thread(softc->work_thread); 1168 mtx_destroy(&softc->queue_lock); 1169 #endif 1170 1171 ctl_tpc_shutdown(softc); 1172 mtx_destroy(&softc->pool_lock); 1173 mtx_destroy(&softc->ctl_lock); 1174 1175 destroy_dev(softc->dev); 1176 1177 sysctl_ctx_free(&softc->sysctl_ctx); 1178 1179 free(control_softc, M_DEVBUF); 1180 control_softc = NULL; 1181 1182 if (bootverbose) 1183 printf("ctl: CAM Target Layer unloaded\n"); 1184 } 1185 1186 static int 1187 ctl_module_event_handler(module_t mod, int what, void *arg) 1188 { 1189 1190 switch (what) { 1191 case MOD_LOAD: 1192 return (ctl_init()); 1193 case MOD_UNLOAD: 1194 return (EBUSY); 1195 default: 1196 return (EOPNOTSUPP); 1197 } 1198 } 1199 1200 /* 1201 * XXX KDM should we do some access checks here? Bump a reference count to 1202 * prevent a CTL module from being unloaded while someone has it open? 1203 */ 1204 static int 1205 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1206 { 1207 return (0); 1208 } 1209 1210 static int 1211 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1212 { 1213 return (0); 1214 } 1215 1216 int 1217 ctl_port_enable(ctl_port_type port_type) 1218 { 1219 struct ctl_softc *softc; 1220 struct ctl_port *port; 1221 1222 if (ctl_is_single == 0) { 1223 union ctl_ha_msg msg_info; 1224 int isc_retval; 1225 1226 #if 0 1227 printf("%s: HA mode, synchronizing frontend enable\n", 1228 __func__); 1229 #endif 1230 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1231 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1232 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1233 printf("Sync msg send error retval %d\n", isc_retval); 1234 } 1235 if (!rcv_sync_msg) { 1236 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1237 sizeof(msg_info), 1); 1238 } 1239 #if 0 1240 printf("CTL:Frontend Enable\n"); 1241 } else { 1242 printf("%s: single mode, skipping frontend synchronization\n", 1243 __func__); 1244 #endif 1245 } 1246 1247 softc = control_softc; 1248 1249 STAILQ_FOREACH(port, &softc->port_list, links) { 1250 if (port_type & port->port_type) 1251 { 1252 #if 0 1253 printf("port %d\n", port->targ_port); 1254 #endif 1255 ctl_port_online(port); 1256 } 1257 } 1258 1259 return (0); 1260 } 1261 1262 int 1263 ctl_port_disable(ctl_port_type port_type) 1264 { 1265 struct ctl_softc *softc; 1266 struct ctl_port *port; 1267 1268 softc = control_softc; 1269 1270 STAILQ_FOREACH(port, &softc->port_list, links) { 1271 if (port_type & port->port_type) 1272 ctl_port_offline(port); 1273 } 1274 1275 return (0); 1276 } 1277 1278 /* 1279 * Returns 0 for success, 1 for failure. 1280 * Currently the only failure mode is if there aren't enough entries 1281 * allocated. So, in case of a failure, look at num_entries_dropped, 1282 * reallocate and try again. 1283 */ 1284 int 1285 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1286 int *num_entries_filled, int *num_entries_dropped, 1287 ctl_port_type port_type, int no_virtual) 1288 { 1289 struct ctl_softc *softc; 1290 struct ctl_port *port; 1291 int entries_dropped, entries_filled; 1292 int retval; 1293 int i; 1294 1295 softc = control_softc; 1296 1297 retval = 0; 1298 entries_filled = 0; 1299 entries_dropped = 0; 1300 1301 i = 0; 1302 mtx_lock(&softc->ctl_lock); 1303 STAILQ_FOREACH(port, &softc->port_list, links) { 1304 struct ctl_port_entry *entry; 1305 1306 if ((port->port_type & port_type) == 0) 1307 continue; 1308 1309 if ((no_virtual != 0) 1310 && (port->virtual_port != 0)) 1311 continue; 1312 1313 if (entries_filled >= num_entries_alloced) { 1314 entries_dropped++; 1315 continue; 1316 } 1317 entry = &entries[i]; 1318 1319 entry->port_type = port->port_type; 1320 strlcpy(entry->port_name, port->port_name, 1321 sizeof(entry->port_name)); 1322 entry->physical_port = port->physical_port; 1323 entry->virtual_port = port->virtual_port; 1324 entry->wwnn = port->wwnn; 1325 entry->wwpn = port->wwpn; 1326 1327 i++; 1328 entries_filled++; 1329 } 1330 1331 mtx_unlock(&softc->ctl_lock); 1332 1333 if (entries_dropped > 0) 1334 retval = 1; 1335 1336 *num_entries_dropped = entries_dropped; 1337 *num_entries_filled = entries_filled; 1338 1339 return (retval); 1340 } 1341 1342 static void 1343 ctl_ioctl_online(void *arg) 1344 { 1345 struct ctl_ioctl_info *ioctl_info; 1346 1347 ioctl_info = (struct ctl_ioctl_info *)arg; 1348 1349 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1350 } 1351 1352 static void 1353 ctl_ioctl_offline(void *arg) 1354 { 1355 struct ctl_ioctl_info *ioctl_info; 1356 1357 ioctl_info = (struct ctl_ioctl_info *)arg; 1358 1359 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1360 } 1361 1362 /* 1363 * Remove an initiator by port number and initiator ID. 1364 * Returns 0 for success, -1 for failure. 1365 */ 1366 int 1367 ctl_remove_initiator(struct ctl_port *port, int iid) 1368 { 1369 struct ctl_softc *softc = control_softc; 1370 1371 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1372 1373 if (iid > CTL_MAX_INIT_PER_PORT) { 1374 printf("%s: initiator ID %u > maximun %u!\n", 1375 __func__, iid, CTL_MAX_INIT_PER_PORT); 1376 return (-1); 1377 } 1378 1379 mtx_lock(&softc->ctl_lock); 1380 port->wwpn_iid[iid].in_use--; 1381 port->wwpn_iid[iid].last_use = time_uptime; 1382 mtx_unlock(&softc->ctl_lock); 1383 1384 return (0); 1385 } 1386 1387 /* 1388 * Add an initiator to the initiator map. 1389 * Returns iid for success, < 0 for failure. 1390 */ 1391 int 1392 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1393 { 1394 struct ctl_softc *softc = control_softc; 1395 time_t best_time; 1396 int i, best; 1397 1398 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1399 1400 if (iid >= CTL_MAX_INIT_PER_PORT) { 1401 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1402 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1403 free(name, M_CTL); 1404 return (-1); 1405 } 1406 1407 mtx_lock(&softc->ctl_lock); 1408 1409 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1410 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1411 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1412 iid = i; 1413 break; 1414 } 1415 if (name != NULL && port->wwpn_iid[i].name != NULL && 1416 strcmp(name, port->wwpn_iid[i].name) == 0) { 1417 iid = i; 1418 break; 1419 } 1420 } 1421 } 1422 1423 if (iid < 0) { 1424 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1425 if (port->wwpn_iid[i].in_use == 0 && 1426 port->wwpn_iid[i].wwpn == 0 && 1427 port->wwpn_iid[i].name == NULL) { 1428 iid = i; 1429 break; 1430 } 1431 } 1432 } 1433 1434 if (iid < 0) { 1435 best = -1; 1436 best_time = INT32_MAX; 1437 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1438 if (port->wwpn_iid[i].in_use == 0) { 1439 if (port->wwpn_iid[i].last_use < best_time) { 1440 best = i; 1441 best_time = port->wwpn_iid[i].last_use; 1442 } 1443 } 1444 } 1445 iid = best; 1446 } 1447 1448 if (iid < 0) { 1449 mtx_unlock(&softc->ctl_lock); 1450 free(name, M_CTL); 1451 return (-2); 1452 } 1453 1454 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1455 /* 1456 * This is not an error yet. 1457 */ 1458 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1459 #if 0 1460 printf("%s: port %d iid %u WWPN %#jx arrived" 1461 " again\n", __func__, port->targ_port, 1462 iid, (uintmax_t)wwpn); 1463 #endif 1464 goto take; 1465 } 1466 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1467 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1468 #if 0 1469 printf("%s: port %d iid %u name '%s' arrived" 1470 " again\n", __func__, port->targ_port, 1471 iid, name); 1472 #endif 1473 goto take; 1474 } 1475 1476 /* 1477 * This is an error, but what do we do about it? The 1478 * driver is telling us we have a new WWPN for this 1479 * initiator ID, so we pretty much need to use it. 1480 */ 1481 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1482 " but WWPN %#jx '%s' is still at that address\n", 1483 __func__, port->targ_port, iid, wwpn, name, 1484 (uintmax_t)port->wwpn_iid[iid].wwpn, 1485 port->wwpn_iid[iid].name); 1486 1487 /* 1488 * XXX KDM clear have_ca and ua_pending on each LUN for 1489 * this initiator. 1490 */ 1491 } 1492 take: 1493 free(port->wwpn_iid[iid].name, M_CTL); 1494 port->wwpn_iid[iid].name = name; 1495 port->wwpn_iid[iid].wwpn = wwpn; 1496 port->wwpn_iid[iid].in_use++; 1497 mtx_unlock(&softc->ctl_lock); 1498 1499 return (iid); 1500 } 1501 1502 static int 1503 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1504 { 1505 int len; 1506 1507 switch (port->port_type) { 1508 case CTL_PORT_FC: 1509 { 1510 struct scsi_transportid_fcp *id = 1511 (struct scsi_transportid_fcp *)buf; 1512 if (port->wwpn_iid[iid].wwpn == 0) 1513 return (0); 1514 memset(id, 0, sizeof(*id)); 1515 id->format_protocol = SCSI_PROTO_FC; 1516 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1517 return (sizeof(*id)); 1518 } 1519 case CTL_PORT_ISCSI: 1520 { 1521 struct scsi_transportid_iscsi_port *id = 1522 (struct scsi_transportid_iscsi_port *)buf; 1523 if (port->wwpn_iid[iid].name == NULL) 1524 return (0); 1525 memset(id, 0, 256); 1526 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1527 SCSI_PROTO_ISCSI; 1528 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1529 len = roundup2(min(len, 252), 4); 1530 scsi_ulto2b(len, id->additional_length); 1531 return (sizeof(*id) + len); 1532 } 1533 case CTL_PORT_SAS: 1534 { 1535 struct scsi_transportid_sas *id = 1536 (struct scsi_transportid_sas *)buf; 1537 if (port->wwpn_iid[iid].wwpn == 0) 1538 return (0); 1539 memset(id, 0, sizeof(*id)); 1540 id->format_protocol = SCSI_PROTO_SAS; 1541 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1542 return (sizeof(*id)); 1543 } 1544 default: 1545 { 1546 struct scsi_transportid_spi *id = 1547 (struct scsi_transportid_spi *)buf; 1548 memset(id, 0, sizeof(*id)); 1549 id->format_protocol = SCSI_PROTO_SPI; 1550 scsi_ulto2b(iid, id->scsi_addr); 1551 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1552 return (sizeof(*id)); 1553 } 1554 } 1555 } 1556 1557 static int 1558 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1559 { 1560 return (0); 1561 } 1562 1563 static int 1564 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1565 { 1566 return (0); 1567 } 1568 1569 /* 1570 * Data movement routine for the CTL ioctl frontend port. 1571 */ 1572 static int 1573 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1574 { 1575 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1576 struct ctl_sg_entry ext_entry, kern_entry; 1577 int ext_sglen, ext_sg_entries, kern_sg_entries; 1578 int ext_sg_start, ext_offset; 1579 int len_to_copy, len_copied; 1580 int kern_watermark, ext_watermark; 1581 int ext_sglist_malloced; 1582 int i, j; 1583 1584 ext_sglist_malloced = 0; 1585 ext_sg_start = 0; 1586 ext_offset = 0; 1587 1588 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1589 1590 /* 1591 * If this flag is set, fake the data transfer. 1592 */ 1593 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1594 ctsio->ext_data_filled = ctsio->ext_data_len; 1595 goto bailout; 1596 } 1597 1598 /* 1599 * To simplify things here, if we have a single buffer, stick it in 1600 * a S/G entry and just make it a single entry S/G list. 1601 */ 1602 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1603 int len_seen; 1604 1605 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1606 1607 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1608 M_WAITOK); 1609 ext_sglist_malloced = 1; 1610 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1611 ext_sglen) != 0) { 1612 ctl_set_internal_failure(ctsio, 1613 /*sks_valid*/ 0, 1614 /*retry_count*/ 0); 1615 goto bailout; 1616 } 1617 ext_sg_entries = ctsio->ext_sg_entries; 1618 len_seen = 0; 1619 for (i = 0; i < ext_sg_entries; i++) { 1620 if ((len_seen + ext_sglist[i].len) >= 1621 ctsio->ext_data_filled) { 1622 ext_sg_start = i; 1623 ext_offset = ctsio->ext_data_filled - len_seen; 1624 break; 1625 } 1626 len_seen += ext_sglist[i].len; 1627 } 1628 } else { 1629 ext_sglist = &ext_entry; 1630 ext_sglist->addr = ctsio->ext_data_ptr; 1631 ext_sglist->len = ctsio->ext_data_len; 1632 ext_sg_entries = 1; 1633 ext_sg_start = 0; 1634 ext_offset = ctsio->ext_data_filled; 1635 } 1636 1637 if (ctsio->kern_sg_entries > 0) { 1638 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1639 kern_sg_entries = ctsio->kern_sg_entries; 1640 } else { 1641 kern_sglist = &kern_entry; 1642 kern_sglist->addr = ctsio->kern_data_ptr; 1643 kern_sglist->len = ctsio->kern_data_len; 1644 kern_sg_entries = 1; 1645 } 1646 1647 1648 kern_watermark = 0; 1649 ext_watermark = ext_offset; 1650 len_copied = 0; 1651 for (i = ext_sg_start, j = 0; 1652 i < ext_sg_entries && j < kern_sg_entries;) { 1653 uint8_t *ext_ptr, *kern_ptr; 1654 1655 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1656 kern_sglist[j].len - kern_watermark); 1657 1658 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1659 ext_ptr = ext_ptr + ext_watermark; 1660 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1661 /* 1662 * XXX KDM fix this! 1663 */ 1664 panic("need to implement bus address support"); 1665 #if 0 1666 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1667 #endif 1668 } else 1669 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1670 kern_ptr = kern_ptr + kern_watermark; 1671 1672 kern_watermark += len_to_copy; 1673 ext_watermark += len_to_copy; 1674 1675 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1676 CTL_FLAG_DATA_IN) { 1677 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1678 "bytes to user\n", len_to_copy)); 1679 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1680 "to %p\n", kern_ptr, ext_ptr)); 1681 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1682 ctl_set_internal_failure(ctsio, 1683 /*sks_valid*/ 0, 1684 /*retry_count*/ 0); 1685 goto bailout; 1686 } 1687 } else { 1688 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1689 "bytes from user\n", len_to_copy)); 1690 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1691 "to %p\n", ext_ptr, kern_ptr)); 1692 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1693 ctl_set_internal_failure(ctsio, 1694 /*sks_valid*/ 0, 1695 /*retry_count*/0); 1696 goto bailout; 1697 } 1698 } 1699 1700 len_copied += len_to_copy; 1701 1702 if (ext_sglist[i].len == ext_watermark) { 1703 i++; 1704 ext_watermark = 0; 1705 } 1706 1707 if (kern_sglist[j].len == kern_watermark) { 1708 j++; 1709 kern_watermark = 0; 1710 } 1711 } 1712 1713 ctsio->ext_data_filled += len_copied; 1714 1715 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1716 "kern_sg_entries: %d\n", ext_sg_entries, 1717 kern_sg_entries)); 1718 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1719 "kern_data_len = %d\n", ctsio->ext_data_len, 1720 ctsio->kern_data_len)); 1721 1722 1723 /* XXX KDM set residual?? */ 1724 bailout: 1725 1726 if (ext_sglist_malloced != 0) 1727 free(ext_sglist, M_CTL); 1728 1729 return (CTL_RETVAL_COMPLETE); 1730 } 1731 1732 /* 1733 * Serialize a command that went down the "wrong" side, and so was sent to 1734 * this controller for execution. The logic is a little different than the 1735 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1736 * sent back to the other side, but in the success case, we execute the 1737 * command on this side (XFER mode) or tell the other side to execute it 1738 * (SER_ONLY mode). 1739 */ 1740 static int 1741 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1742 { 1743 struct ctl_softc *ctl_softc; 1744 union ctl_ha_msg msg_info; 1745 struct ctl_lun *lun; 1746 int retval = 0; 1747 uint32_t targ_lun; 1748 1749 ctl_softc = control_softc; 1750 1751 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1752 lun = ctl_softc->ctl_luns[targ_lun]; 1753 if (lun==NULL) 1754 { 1755 /* 1756 * Why isn't LUN defined? The other side wouldn't 1757 * send a cmd if the LUN is undefined. 1758 */ 1759 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1760 1761 /* "Logical unit not supported" */ 1762 ctl_set_sense_data(&msg_info.scsi.sense_data, 1763 lun, 1764 /*sense_format*/SSD_TYPE_NONE, 1765 /*current_error*/ 1, 1766 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1767 /*asc*/ 0x25, 1768 /*ascq*/ 0x00, 1769 SSD_ELEM_NONE); 1770 1771 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1772 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1773 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1774 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1775 msg_info.hdr.serializing_sc = NULL; 1776 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1777 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1778 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1779 } 1780 return(1); 1781 1782 } 1783 1784 mtx_lock(&lun->lun_lock); 1785 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1786 1787 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1788 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1789 ooa_links))) { 1790 case CTL_ACTION_BLOCK: 1791 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1792 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1793 blocked_links); 1794 break; 1795 case CTL_ACTION_PASS: 1796 case CTL_ACTION_SKIP: 1797 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1798 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1799 ctl_enqueue_rtr((union ctl_io *)ctsio); 1800 } else { 1801 1802 /* send msg back to other side */ 1803 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1804 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1805 msg_info.hdr.msg_type = CTL_MSG_R2R; 1806 #if 0 1807 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1808 #endif 1809 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1810 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1811 } 1812 } 1813 break; 1814 case CTL_ACTION_OVERLAP: 1815 /* OVERLAPPED COMMANDS ATTEMPTED */ 1816 ctl_set_sense_data(&msg_info.scsi.sense_data, 1817 lun, 1818 /*sense_format*/SSD_TYPE_NONE, 1819 /*current_error*/ 1, 1820 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1821 /*asc*/ 0x4E, 1822 /*ascq*/ 0x00, 1823 SSD_ELEM_NONE); 1824 1825 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1826 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1827 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1828 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1829 msg_info.hdr.serializing_sc = NULL; 1830 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1831 #if 0 1832 printf("BAD JUJU:Major Bummer Overlap\n"); 1833 #endif 1834 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1835 retval = 1; 1836 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1837 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1838 } 1839 break; 1840 case CTL_ACTION_OVERLAP_TAG: 1841 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1842 ctl_set_sense_data(&msg_info.scsi.sense_data, 1843 lun, 1844 /*sense_format*/SSD_TYPE_NONE, 1845 /*current_error*/ 1, 1846 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1847 /*asc*/ 0x4D, 1848 /*ascq*/ ctsio->tag_num & 0xff, 1849 SSD_ELEM_NONE); 1850 1851 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1852 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1853 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1854 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1855 msg_info.hdr.serializing_sc = NULL; 1856 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1857 #if 0 1858 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1859 #endif 1860 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1861 retval = 1; 1862 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1863 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1864 } 1865 break; 1866 case CTL_ACTION_ERROR: 1867 default: 1868 /* "Internal target failure" */ 1869 ctl_set_sense_data(&msg_info.scsi.sense_data, 1870 lun, 1871 /*sense_format*/SSD_TYPE_NONE, 1872 /*current_error*/ 1, 1873 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1874 /*asc*/ 0x44, 1875 /*ascq*/ 0x00, 1876 SSD_ELEM_NONE); 1877 1878 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1879 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1880 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1881 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1882 msg_info.hdr.serializing_sc = NULL; 1883 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1884 #if 0 1885 printf("BAD JUJU:Major Bummer HW Error\n"); 1886 #endif 1887 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1888 retval = 1; 1889 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1890 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1891 } 1892 break; 1893 } 1894 mtx_unlock(&lun->lun_lock); 1895 return (retval); 1896 } 1897 1898 static int 1899 ctl_ioctl_submit_wait(union ctl_io *io) 1900 { 1901 struct ctl_fe_ioctl_params params; 1902 ctl_fe_ioctl_state last_state; 1903 int done, retval; 1904 1905 retval = 0; 1906 1907 bzero(¶ms, sizeof(params)); 1908 1909 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1910 cv_init(¶ms.sem, "ctlioccv"); 1911 params.state = CTL_IOCTL_INPROG; 1912 last_state = params.state; 1913 1914 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1915 1916 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1917 1918 /* This shouldn't happen */ 1919 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1920 return (retval); 1921 1922 done = 0; 1923 1924 do { 1925 mtx_lock(¶ms.ioctl_mtx); 1926 /* 1927 * Check the state here, and don't sleep if the state has 1928 * already changed (i.e. wakeup has already occured, but we 1929 * weren't waiting yet). 1930 */ 1931 if (params.state == last_state) { 1932 /* XXX KDM cv_wait_sig instead? */ 1933 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1934 } 1935 last_state = params.state; 1936 1937 switch (params.state) { 1938 case CTL_IOCTL_INPROG: 1939 /* Why did we wake up? */ 1940 /* XXX KDM error here? */ 1941 mtx_unlock(¶ms.ioctl_mtx); 1942 break; 1943 case CTL_IOCTL_DATAMOVE: 1944 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1945 1946 /* 1947 * change last_state back to INPROG to avoid 1948 * deadlock on subsequent data moves. 1949 */ 1950 params.state = last_state = CTL_IOCTL_INPROG; 1951 1952 mtx_unlock(¶ms.ioctl_mtx); 1953 ctl_ioctl_do_datamove(&io->scsiio); 1954 /* 1955 * Note that in some cases, most notably writes, 1956 * this will queue the I/O and call us back later. 1957 * In other cases, generally reads, this routine 1958 * will immediately call back and wake us up, 1959 * probably using our own context. 1960 */ 1961 io->scsiio.be_move_done(io); 1962 break; 1963 case CTL_IOCTL_DONE: 1964 mtx_unlock(¶ms.ioctl_mtx); 1965 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1966 done = 1; 1967 break; 1968 default: 1969 mtx_unlock(¶ms.ioctl_mtx); 1970 /* XXX KDM error here? */ 1971 break; 1972 } 1973 } while (done == 0); 1974 1975 mtx_destroy(¶ms.ioctl_mtx); 1976 cv_destroy(¶ms.sem); 1977 1978 return (CTL_RETVAL_COMPLETE); 1979 } 1980 1981 static void 1982 ctl_ioctl_datamove(union ctl_io *io) 1983 { 1984 struct ctl_fe_ioctl_params *params; 1985 1986 params = (struct ctl_fe_ioctl_params *) 1987 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1988 1989 mtx_lock(¶ms->ioctl_mtx); 1990 params->state = CTL_IOCTL_DATAMOVE; 1991 cv_broadcast(¶ms->sem); 1992 mtx_unlock(¶ms->ioctl_mtx); 1993 } 1994 1995 static void 1996 ctl_ioctl_done(union ctl_io *io) 1997 { 1998 struct ctl_fe_ioctl_params *params; 1999 2000 params = (struct ctl_fe_ioctl_params *) 2001 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2002 2003 mtx_lock(¶ms->ioctl_mtx); 2004 params->state = CTL_IOCTL_DONE; 2005 cv_broadcast(¶ms->sem); 2006 mtx_unlock(¶ms->ioctl_mtx); 2007 } 2008 2009 static void 2010 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2011 { 2012 struct ctl_fe_ioctl_startstop_info *sd_info; 2013 2014 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2015 2016 sd_info->hs_info.status = metatask->status; 2017 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2018 sd_info->hs_info.luns_complete = 2019 metatask->taskinfo.startstop.luns_complete; 2020 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2021 2022 cv_broadcast(&sd_info->sem); 2023 } 2024 2025 static void 2026 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2027 { 2028 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2029 2030 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2031 2032 mtx_lock(fe_bbr_info->lock); 2033 fe_bbr_info->bbr_info->status = metatask->status; 2034 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2035 fe_bbr_info->wakeup_done = 1; 2036 mtx_unlock(fe_bbr_info->lock); 2037 2038 cv_broadcast(&fe_bbr_info->sem); 2039 } 2040 2041 /* 2042 * Returns 0 for success, errno for failure. 2043 */ 2044 static int 2045 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2046 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2047 { 2048 union ctl_io *io; 2049 int retval; 2050 2051 retval = 0; 2052 2053 mtx_lock(&lun->lun_lock); 2054 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2055 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2056 ooa_links)) { 2057 struct ctl_ooa_entry *entry; 2058 2059 /* 2060 * If we've got more than we can fit, just count the 2061 * remaining entries. 2062 */ 2063 if (*cur_fill_num >= ooa_hdr->alloc_num) 2064 continue; 2065 2066 entry = &kern_entries[*cur_fill_num]; 2067 2068 entry->tag_num = io->scsiio.tag_num; 2069 entry->lun_num = lun->lun; 2070 #ifdef CTL_TIME_IO 2071 entry->start_bt = io->io_hdr.start_bt; 2072 #endif 2073 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2074 entry->cdb_len = io->scsiio.cdb_len; 2075 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2076 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2077 2078 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2079 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2080 2081 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2082 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2083 2084 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2085 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2086 2087 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2088 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2089 } 2090 mtx_unlock(&lun->lun_lock); 2091 2092 return (retval); 2093 } 2094 2095 static void * 2096 ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2097 size_t error_str_len) 2098 { 2099 void *kptr; 2100 2101 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2102 2103 if (copyin(user_addr, kptr, len) != 0) { 2104 snprintf(error_str, error_str_len, "Error copying %d bytes " 2105 "from user address %p to kernel address %p", len, 2106 user_addr, kptr); 2107 free(kptr, M_CTL); 2108 return (NULL); 2109 } 2110 2111 return (kptr); 2112 } 2113 2114 static void 2115 ctl_free_args(int num_args, struct ctl_be_arg *args) 2116 { 2117 int i; 2118 2119 if (args == NULL) 2120 return; 2121 2122 for (i = 0; i < num_args; i++) { 2123 free(args[i].kname, M_CTL); 2124 free(args[i].kvalue, M_CTL); 2125 } 2126 2127 free(args, M_CTL); 2128 } 2129 2130 static struct ctl_be_arg * 2131 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2132 char *error_str, size_t error_str_len) 2133 { 2134 struct ctl_be_arg *args; 2135 int i; 2136 2137 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2138 error_str, error_str_len); 2139 2140 if (args == NULL) 2141 goto bailout; 2142 2143 for (i = 0; i < num_args; i++) { 2144 args[i].kname = NULL; 2145 args[i].kvalue = NULL; 2146 } 2147 2148 for (i = 0; i < num_args; i++) { 2149 uint8_t *tmpptr; 2150 2151 args[i].kname = ctl_copyin_alloc(args[i].name, 2152 args[i].namelen, error_str, error_str_len); 2153 if (args[i].kname == NULL) 2154 goto bailout; 2155 2156 if (args[i].kname[args[i].namelen - 1] != '\0') { 2157 snprintf(error_str, error_str_len, "Argument %d " 2158 "name is not NUL-terminated", i); 2159 goto bailout; 2160 } 2161 2162 if (args[i].flags & CTL_BEARG_RD) { 2163 tmpptr = ctl_copyin_alloc(args[i].value, 2164 args[i].vallen, error_str, error_str_len); 2165 if (tmpptr == NULL) 2166 goto bailout; 2167 if ((args[i].flags & CTL_BEARG_ASCII) 2168 && (tmpptr[args[i].vallen - 1] != '\0')) { 2169 snprintf(error_str, error_str_len, "Argument " 2170 "%d value is not NUL-terminated", i); 2171 goto bailout; 2172 } 2173 args[i].kvalue = tmpptr; 2174 } else { 2175 args[i].kvalue = malloc(args[i].vallen, 2176 M_CTL, M_WAITOK | M_ZERO); 2177 } 2178 } 2179 2180 return (args); 2181 bailout: 2182 2183 ctl_free_args(num_args, args); 2184 2185 return (NULL); 2186 } 2187 2188 static void 2189 ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2190 { 2191 int i; 2192 2193 for (i = 0; i < num_args; i++) { 2194 if (args[i].flags & CTL_BEARG_WR) 2195 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2196 } 2197 } 2198 2199 /* 2200 * Escape characters that are illegal or not recommended in XML. 2201 */ 2202 int 2203 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2204 { 2205 char *end = str + size; 2206 int retval; 2207 2208 retval = 0; 2209 2210 for (; *str && str < end; str++) { 2211 switch (*str) { 2212 case '&': 2213 retval = sbuf_printf(sb, "&"); 2214 break; 2215 case '>': 2216 retval = sbuf_printf(sb, ">"); 2217 break; 2218 case '<': 2219 retval = sbuf_printf(sb, "<"); 2220 break; 2221 default: 2222 retval = sbuf_putc(sb, *str); 2223 break; 2224 } 2225 2226 if (retval != 0) 2227 break; 2228 2229 } 2230 2231 return (retval); 2232 } 2233 2234 static void 2235 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2236 { 2237 struct scsi_vpd_id_descriptor *desc; 2238 int i; 2239 2240 if (id == NULL || id->len < 4) 2241 return; 2242 desc = (struct scsi_vpd_id_descriptor *)id->data; 2243 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2244 case SVPD_ID_TYPE_T10: 2245 sbuf_printf(sb, "t10."); 2246 break; 2247 case SVPD_ID_TYPE_EUI64: 2248 sbuf_printf(sb, "eui."); 2249 break; 2250 case SVPD_ID_TYPE_NAA: 2251 sbuf_printf(sb, "naa."); 2252 break; 2253 case SVPD_ID_TYPE_SCSI_NAME: 2254 break; 2255 } 2256 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2257 case SVPD_ID_CODESET_BINARY: 2258 for (i = 0; i < desc->length; i++) 2259 sbuf_printf(sb, "%02x", desc->identifier[i]); 2260 break; 2261 case SVPD_ID_CODESET_ASCII: 2262 sbuf_printf(sb, "%.*s", (int)desc->length, 2263 (char *)desc->identifier); 2264 break; 2265 case SVPD_ID_CODESET_UTF8: 2266 sbuf_printf(sb, "%s", (char *)desc->identifier); 2267 break; 2268 } 2269 } 2270 2271 static int 2272 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2273 struct thread *td) 2274 { 2275 struct ctl_softc *softc; 2276 int retval; 2277 2278 softc = control_softc; 2279 2280 retval = 0; 2281 2282 switch (cmd) { 2283 case CTL_IO: { 2284 union ctl_io *io; 2285 void *pool_tmp; 2286 2287 /* 2288 * If we haven't been "enabled", don't allow any SCSI I/O 2289 * to this FETD. 2290 */ 2291 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2292 retval = EPERM; 2293 break; 2294 } 2295 2296 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2297 if (io == NULL) { 2298 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2299 retval = ENOSPC; 2300 break; 2301 } 2302 2303 /* 2304 * Need to save the pool reference so it doesn't get 2305 * spammed by the user's ctl_io. 2306 */ 2307 pool_tmp = io->io_hdr.pool; 2308 2309 memcpy(io, (void *)addr, sizeof(*io)); 2310 2311 io->io_hdr.pool = pool_tmp; 2312 /* 2313 * No status yet, so make sure the status is set properly. 2314 */ 2315 io->io_hdr.status = CTL_STATUS_NONE; 2316 2317 /* 2318 * The user sets the initiator ID, target and LUN IDs. 2319 */ 2320 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2321 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2322 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2323 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2324 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2325 2326 retval = ctl_ioctl_submit_wait(io); 2327 2328 if (retval != 0) { 2329 ctl_free_io(io); 2330 break; 2331 } 2332 2333 memcpy((void *)addr, io, sizeof(*io)); 2334 2335 /* return this to our pool */ 2336 ctl_free_io(io); 2337 2338 break; 2339 } 2340 case CTL_ENABLE_PORT: 2341 case CTL_DISABLE_PORT: 2342 case CTL_SET_PORT_WWNS: { 2343 struct ctl_port *port; 2344 struct ctl_port_entry *entry; 2345 2346 entry = (struct ctl_port_entry *)addr; 2347 2348 mtx_lock(&softc->ctl_lock); 2349 STAILQ_FOREACH(port, &softc->port_list, links) { 2350 int action, done; 2351 2352 action = 0; 2353 done = 0; 2354 2355 if ((entry->port_type == CTL_PORT_NONE) 2356 && (entry->targ_port == port->targ_port)) { 2357 /* 2358 * If the user only wants to enable or 2359 * disable or set WWNs on a specific port, 2360 * do the operation and we're done. 2361 */ 2362 action = 1; 2363 done = 1; 2364 } else if (entry->port_type & port->port_type) { 2365 /* 2366 * Compare the user's type mask with the 2367 * particular frontend type to see if we 2368 * have a match. 2369 */ 2370 action = 1; 2371 done = 0; 2372 2373 /* 2374 * Make sure the user isn't trying to set 2375 * WWNs on multiple ports at the same time. 2376 */ 2377 if (cmd == CTL_SET_PORT_WWNS) { 2378 printf("%s: Can't set WWNs on " 2379 "multiple ports\n", __func__); 2380 retval = EINVAL; 2381 break; 2382 } 2383 } 2384 if (action != 0) { 2385 /* 2386 * XXX KDM we have to drop the lock here, 2387 * because the online/offline operations 2388 * can potentially block. We need to 2389 * reference count the frontends so they 2390 * can't go away, 2391 */ 2392 mtx_unlock(&softc->ctl_lock); 2393 2394 if (cmd == CTL_ENABLE_PORT) { 2395 struct ctl_lun *lun; 2396 2397 STAILQ_FOREACH(lun, &softc->lun_list, 2398 links) { 2399 port->lun_enable(port->targ_lun_arg, 2400 lun->target, 2401 lun->lun); 2402 } 2403 2404 ctl_port_online(port); 2405 } else if (cmd == CTL_DISABLE_PORT) { 2406 struct ctl_lun *lun; 2407 2408 ctl_port_offline(port); 2409 2410 STAILQ_FOREACH(lun, &softc->lun_list, 2411 links) { 2412 port->lun_disable( 2413 port->targ_lun_arg, 2414 lun->target, 2415 lun->lun); 2416 } 2417 } 2418 2419 mtx_lock(&softc->ctl_lock); 2420 2421 if (cmd == CTL_SET_PORT_WWNS) 2422 ctl_port_set_wwns(port, 2423 (entry->flags & CTL_PORT_WWNN_VALID) ? 2424 1 : 0, entry->wwnn, 2425 (entry->flags & CTL_PORT_WWPN_VALID) ? 2426 1 : 0, entry->wwpn); 2427 } 2428 if (done != 0) 2429 break; 2430 } 2431 mtx_unlock(&softc->ctl_lock); 2432 break; 2433 } 2434 case CTL_GET_PORT_LIST: { 2435 struct ctl_port *port; 2436 struct ctl_port_list *list; 2437 int i; 2438 2439 list = (struct ctl_port_list *)addr; 2440 2441 if (list->alloc_len != (list->alloc_num * 2442 sizeof(struct ctl_port_entry))) { 2443 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2444 "alloc_num %u * sizeof(struct ctl_port_entry) " 2445 "%zu\n", __func__, list->alloc_len, 2446 list->alloc_num, sizeof(struct ctl_port_entry)); 2447 retval = EINVAL; 2448 break; 2449 } 2450 list->fill_len = 0; 2451 list->fill_num = 0; 2452 list->dropped_num = 0; 2453 i = 0; 2454 mtx_lock(&softc->ctl_lock); 2455 STAILQ_FOREACH(port, &softc->port_list, links) { 2456 struct ctl_port_entry entry, *list_entry; 2457 2458 if (list->fill_num >= list->alloc_num) { 2459 list->dropped_num++; 2460 continue; 2461 } 2462 2463 entry.port_type = port->port_type; 2464 strlcpy(entry.port_name, port->port_name, 2465 sizeof(entry.port_name)); 2466 entry.targ_port = port->targ_port; 2467 entry.physical_port = port->physical_port; 2468 entry.virtual_port = port->virtual_port; 2469 entry.wwnn = port->wwnn; 2470 entry.wwpn = port->wwpn; 2471 if (port->status & CTL_PORT_STATUS_ONLINE) 2472 entry.online = 1; 2473 else 2474 entry.online = 0; 2475 2476 list_entry = &list->entries[i]; 2477 2478 retval = copyout(&entry, list_entry, sizeof(entry)); 2479 if (retval != 0) { 2480 printf("%s: CTL_GET_PORT_LIST: copyout " 2481 "returned %d\n", __func__, retval); 2482 break; 2483 } 2484 i++; 2485 list->fill_num++; 2486 list->fill_len += sizeof(entry); 2487 } 2488 mtx_unlock(&softc->ctl_lock); 2489 2490 /* 2491 * If this is non-zero, we had a copyout fault, so there's 2492 * probably no point in attempting to set the status inside 2493 * the structure. 2494 */ 2495 if (retval != 0) 2496 break; 2497 2498 if (list->dropped_num > 0) 2499 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2500 else 2501 list->status = CTL_PORT_LIST_OK; 2502 break; 2503 } 2504 case CTL_DUMP_OOA: { 2505 struct ctl_lun *lun; 2506 union ctl_io *io; 2507 char printbuf[128]; 2508 struct sbuf sb; 2509 2510 mtx_lock(&softc->ctl_lock); 2511 printf("Dumping OOA queues:\n"); 2512 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2513 mtx_lock(&lun->lun_lock); 2514 for (io = (union ctl_io *)TAILQ_FIRST( 2515 &lun->ooa_queue); io != NULL; 2516 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2517 ooa_links)) { 2518 sbuf_new(&sb, printbuf, sizeof(printbuf), 2519 SBUF_FIXEDLEN); 2520 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2521 (intmax_t)lun->lun, 2522 io->scsiio.tag_num, 2523 (io->io_hdr.flags & 2524 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2525 (io->io_hdr.flags & 2526 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2527 (io->io_hdr.flags & 2528 CTL_FLAG_ABORT) ? " ABORT" : "", 2529 (io->io_hdr.flags & 2530 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2531 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2532 sbuf_finish(&sb); 2533 printf("%s\n", sbuf_data(&sb)); 2534 } 2535 mtx_unlock(&lun->lun_lock); 2536 } 2537 printf("OOA queues dump done\n"); 2538 mtx_unlock(&softc->ctl_lock); 2539 break; 2540 } 2541 case CTL_GET_OOA: { 2542 struct ctl_lun *lun; 2543 struct ctl_ooa *ooa_hdr; 2544 struct ctl_ooa_entry *entries; 2545 uint32_t cur_fill_num; 2546 2547 ooa_hdr = (struct ctl_ooa *)addr; 2548 2549 if ((ooa_hdr->alloc_len == 0) 2550 || (ooa_hdr->alloc_num == 0)) { 2551 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2552 "must be non-zero\n", __func__, 2553 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2554 retval = EINVAL; 2555 break; 2556 } 2557 2558 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2559 sizeof(struct ctl_ooa_entry))) { 2560 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2561 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2562 __func__, ooa_hdr->alloc_len, 2563 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2564 retval = EINVAL; 2565 break; 2566 } 2567 2568 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2569 if (entries == NULL) { 2570 printf("%s: could not allocate %d bytes for OOA " 2571 "dump\n", __func__, ooa_hdr->alloc_len); 2572 retval = ENOMEM; 2573 break; 2574 } 2575 2576 mtx_lock(&softc->ctl_lock); 2577 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2578 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2579 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2580 mtx_unlock(&softc->ctl_lock); 2581 free(entries, M_CTL); 2582 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2583 __func__, (uintmax_t)ooa_hdr->lun_num); 2584 retval = EINVAL; 2585 break; 2586 } 2587 2588 cur_fill_num = 0; 2589 2590 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2591 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2592 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2593 ooa_hdr, entries); 2594 if (retval != 0) 2595 break; 2596 } 2597 if (retval != 0) { 2598 mtx_unlock(&softc->ctl_lock); 2599 free(entries, M_CTL); 2600 break; 2601 } 2602 } else { 2603 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2604 2605 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2606 entries); 2607 } 2608 mtx_unlock(&softc->ctl_lock); 2609 2610 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2611 ooa_hdr->fill_len = ooa_hdr->fill_num * 2612 sizeof(struct ctl_ooa_entry); 2613 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2614 if (retval != 0) { 2615 printf("%s: error copying out %d bytes for OOA dump\n", 2616 __func__, ooa_hdr->fill_len); 2617 } 2618 2619 getbintime(&ooa_hdr->cur_bt); 2620 2621 if (cur_fill_num > ooa_hdr->alloc_num) { 2622 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2623 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2624 } else { 2625 ooa_hdr->dropped_num = 0; 2626 ooa_hdr->status = CTL_OOA_OK; 2627 } 2628 2629 free(entries, M_CTL); 2630 break; 2631 } 2632 case CTL_CHECK_OOA: { 2633 union ctl_io *io; 2634 struct ctl_lun *lun; 2635 struct ctl_ooa_info *ooa_info; 2636 2637 2638 ooa_info = (struct ctl_ooa_info *)addr; 2639 2640 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2641 ooa_info->status = CTL_OOA_INVALID_LUN; 2642 break; 2643 } 2644 mtx_lock(&softc->ctl_lock); 2645 lun = softc->ctl_luns[ooa_info->lun_id]; 2646 if (lun == NULL) { 2647 mtx_unlock(&softc->ctl_lock); 2648 ooa_info->status = CTL_OOA_INVALID_LUN; 2649 break; 2650 } 2651 mtx_lock(&lun->lun_lock); 2652 mtx_unlock(&softc->ctl_lock); 2653 ooa_info->num_entries = 0; 2654 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2655 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2656 &io->io_hdr, ooa_links)) { 2657 ooa_info->num_entries++; 2658 } 2659 mtx_unlock(&lun->lun_lock); 2660 2661 ooa_info->status = CTL_OOA_SUCCESS; 2662 2663 break; 2664 } 2665 case CTL_HARD_START: 2666 case CTL_HARD_STOP: { 2667 struct ctl_fe_ioctl_startstop_info ss_info; 2668 struct cfi_metatask *metatask; 2669 struct mtx hs_mtx; 2670 2671 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2672 2673 cv_init(&ss_info.sem, "hard start/stop cv" ); 2674 2675 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2676 if (metatask == NULL) { 2677 retval = ENOMEM; 2678 mtx_destroy(&hs_mtx); 2679 break; 2680 } 2681 2682 if (cmd == CTL_HARD_START) 2683 metatask->tasktype = CFI_TASK_STARTUP; 2684 else 2685 metatask->tasktype = CFI_TASK_SHUTDOWN; 2686 2687 metatask->callback = ctl_ioctl_hard_startstop_callback; 2688 metatask->callback_arg = &ss_info; 2689 2690 cfi_action(metatask); 2691 2692 /* Wait for the callback */ 2693 mtx_lock(&hs_mtx); 2694 cv_wait_sig(&ss_info.sem, &hs_mtx); 2695 mtx_unlock(&hs_mtx); 2696 2697 /* 2698 * All information has been copied from the metatask by the 2699 * time cv_broadcast() is called, so we free the metatask here. 2700 */ 2701 cfi_free_metatask(metatask); 2702 2703 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2704 2705 mtx_destroy(&hs_mtx); 2706 break; 2707 } 2708 case CTL_BBRREAD: { 2709 struct ctl_bbrread_info *bbr_info; 2710 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2711 struct mtx bbr_mtx; 2712 struct cfi_metatask *metatask; 2713 2714 bbr_info = (struct ctl_bbrread_info *)addr; 2715 2716 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2717 2718 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2719 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2720 2721 fe_bbr_info.bbr_info = bbr_info; 2722 fe_bbr_info.lock = &bbr_mtx; 2723 2724 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2725 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2726 2727 if (metatask == NULL) { 2728 mtx_destroy(&bbr_mtx); 2729 cv_destroy(&fe_bbr_info.sem); 2730 retval = ENOMEM; 2731 break; 2732 } 2733 metatask->tasktype = CFI_TASK_BBRREAD; 2734 metatask->callback = ctl_ioctl_bbrread_callback; 2735 metatask->callback_arg = &fe_bbr_info; 2736 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2737 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2738 metatask->taskinfo.bbrread.len = bbr_info->len; 2739 2740 cfi_action(metatask); 2741 2742 mtx_lock(&bbr_mtx); 2743 while (fe_bbr_info.wakeup_done == 0) 2744 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2745 mtx_unlock(&bbr_mtx); 2746 2747 bbr_info->status = metatask->status; 2748 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2749 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2750 memcpy(&bbr_info->sense_data, 2751 &metatask->taskinfo.bbrread.sense_data, 2752 ctl_min(sizeof(bbr_info->sense_data), 2753 sizeof(metatask->taskinfo.bbrread.sense_data))); 2754 2755 cfi_free_metatask(metatask); 2756 2757 mtx_destroy(&bbr_mtx); 2758 cv_destroy(&fe_bbr_info.sem); 2759 2760 break; 2761 } 2762 case CTL_DELAY_IO: { 2763 struct ctl_io_delay_info *delay_info; 2764 #ifdef CTL_IO_DELAY 2765 struct ctl_lun *lun; 2766 #endif /* CTL_IO_DELAY */ 2767 2768 delay_info = (struct ctl_io_delay_info *)addr; 2769 2770 #ifdef CTL_IO_DELAY 2771 mtx_lock(&softc->ctl_lock); 2772 2773 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2774 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2775 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2776 } else { 2777 lun = softc->ctl_luns[delay_info->lun_id]; 2778 mtx_lock(&lun->lun_lock); 2779 2780 delay_info->status = CTL_DELAY_STATUS_OK; 2781 2782 switch (delay_info->delay_type) { 2783 case CTL_DELAY_TYPE_CONT: 2784 break; 2785 case CTL_DELAY_TYPE_ONESHOT: 2786 break; 2787 default: 2788 delay_info->status = 2789 CTL_DELAY_STATUS_INVALID_TYPE; 2790 break; 2791 } 2792 2793 switch (delay_info->delay_loc) { 2794 case CTL_DELAY_LOC_DATAMOVE: 2795 lun->delay_info.datamove_type = 2796 delay_info->delay_type; 2797 lun->delay_info.datamove_delay = 2798 delay_info->delay_secs; 2799 break; 2800 case CTL_DELAY_LOC_DONE: 2801 lun->delay_info.done_type = 2802 delay_info->delay_type; 2803 lun->delay_info.done_delay = 2804 delay_info->delay_secs; 2805 break; 2806 default: 2807 delay_info->status = 2808 CTL_DELAY_STATUS_INVALID_LOC; 2809 break; 2810 } 2811 mtx_unlock(&lun->lun_lock); 2812 } 2813 2814 mtx_unlock(&softc->ctl_lock); 2815 #else 2816 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2817 #endif /* CTL_IO_DELAY */ 2818 break; 2819 } 2820 case CTL_REALSYNC_SET: { 2821 int *syncstate; 2822 2823 syncstate = (int *)addr; 2824 2825 mtx_lock(&softc->ctl_lock); 2826 switch (*syncstate) { 2827 case 0: 2828 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2829 break; 2830 case 1: 2831 softc->flags |= CTL_FLAG_REAL_SYNC; 2832 break; 2833 default: 2834 retval = EINVAL; 2835 break; 2836 } 2837 mtx_unlock(&softc->ctl_lock); 2838 break; 2839 } 2840 case CTL_REALSYNC_GET: { 2841 int *syncstate; 2842 2843 syncstate = (int*)addr; 2844 2845 mtx_lock(&softc->ctl_lock); 2846 if (softc->flags & CTL_FLAG_REAL_SYNC) 2847 *syncstate = 1; 2848 else 2849 *syncstate = 0; 2850 mtx_unlock(&softc->ctl_lock); 2851 2852 break; 2853 } 2854 case CTL_SETSYNC: 2855 case CTL_GETSYNC: { 2856 struct ctl_sync_info *sync_info; 2857 struct ctl_lun *lun; 2858 2859 sync_info = (struct ctl_sync_info *)addr; 2860 2861 mtx_lock(&softc->ctl_lock); 2862 lun = softc->ctl_luns[sync_info->lun_id]; 2863 if (lun == NULL) { 2864 mtx_unlock(&softc->ctl_lock); 2865 sync_info->status = CTL_GS_SYNC_NO_LUN; 2866 } 2867 /* 2868 * Get or set the sync interval. We're not bounds checking 2869 * in the set case, hopefully the user won't do something 2870 * silly. 2871 */ 2872 mtx_lock(&lun->lun_lock); 2873 mtx_unlock(&softc->ctl_lock); 2874 if (cmd == CTL_GETSYNC) 2875 sync_info->sync_interval = lun->sync_interval; 2876 else 2877 lun->sync_interval = sync_info->sync_interval; 2878 mtx_unlock(&lun->lun_lock); 2879 2880 sync_info->status = CTL_GS_SYNC_OK; 2881 2882 break; 2883 } 2884 case CTL_GETSTATS: { 2885 struct ctl_stats *stats; 2886 struct ctl_lun *lun; 2887 int i; 2888 2889 stats = (struct ctl_stats *)addr; 2890 2891 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2892 stats->alloc_len) { 2893 stats->status = CTL_SS_NEED_MORE_SPACE; 2894 stats->num_luns = softc->num_luns; 2895 break; 2896 } 2897 /* 2898 * XXX KDM no locking here. If the LUN list changes, 2899 * things can blow up. 2900 */ 2901 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2902 i++, lun = STAILQ_NEXT(lun, links)) { 2903 retval = copyout(&lun->stats, &stats->lun_stats[i], 2904 sizeof(lun->stats)); 2905 if (retval != 0) 2906 break; 2907 } 2908 stats->num_luns = softc->num_luns; 2909 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2910 softc->num_luns; 2911 stats->status = CTL_SS_OK; 2912 #ifdef CTL_TIME_IO 2913 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2914 #else 2915 stats->flags = CTL_STATS_FLAG_NONE; 2916 #endif 2917 getnanouptime(&stats->timestamp); 2918 break; 2919 } 2920 case CTL_ERROR_INJECT: { 2921 struct ctl_error_desc *err_desc, *new_err_desc; 2922 struct ctl_lun *lun; 2923 2924 err_desc = (struct ctl_error_desc *)addr; 2925 2926 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2927 M_WAITOK | M_ZERO); 2928 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2929 2930 mtx_lock(&softc->ctl_lock); 2931 lun = softc->ctl_luns[err_desc->lun_id]; 2932 if (lun == NULL) { 2933 mtx_unlock(&softc->ctl_lock); 2934 free(new_err_desc, M_CTL); 2935 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2936 __func__, (uintmax_t)err_desc->lun_id); 2937 retval = EINVAL; 2938 break; 2939 } 2940 mtx_lock(&lun->lun_lock); 2941 mtx_unlock(&softc->ctl_lock); 2942 2943 /* 2944 * We could do some checking here to verify the validity 2945 * of the request, but given the complexity of error 2946 * injection requests, the checking logic would be fairly 2947 * complex. 2948 * 2949 * For now, if the request is invalid, it just won't get 2950 * executed and might get deleted. 2951 */ 2952 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2953 2954 /* 2955 * XXX KDM check to make sure the serial number is unique, 2956 * in case we somehow manage to wrap. That shouldn't 2957 * happen for a very long time, but it's the right thing to 2958 * do. 2959 */ 2960 new_err_desc->serial = lun->error_serial; 2961 err_desc->serial = lun->error_serial; 2962 lun->error_serial++; 2963 2964 mtx_unlock(&lun->lun_lock); 2965 break; 2966 } 2967 case CTL_ERROR_INJECT_DELETE: { 2968 struct ctl_error_desc *delete_desc, *desc, *desc2; 2969 struct ctl_lun *lun; 2970 int delete_done; 2971 2972 delete_desc = (struct ctl_error_desc *)addr; 2973 delete_done = 0; 2974 2975 mtx_lock(&softc->ctl_lock); 2976 lun = softc->ctl_luns[delete_desc->lun_id]; 2977 if (lun == NULL) { 2978 mtx_unlock(&softc->ctl_lock); 2979 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2980 __func__, (uintmax_t)delete_desc->lun_id); 2981 retval = EINVAL; 2982 break; 2983 } 2984 mtx_lock(&lun->lun_lock); 2985 mtx_unlock(&softc->ctl_lock); 2986 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2987 if (desc->serial != delete_desc->serial) 2988 continue; 2989 2990 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2991 links); 2992 free(desc, M_CTL); 2993 delete_done = 1; 2994 } 2995 mtx_unlock(&lun->lun_lock); 2996 if (delete_done == 0) { 2997 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2998 "error serial %ju on LUN %u\n", __func__, 2999 delete_desc->serial, delete_desc->lun_id); 3000 retval = EINVAL; 3001 break; 3002 } 3003 break; 3004 } 3005 case CTL_DUMP_STRUCTS: { 3006 int i, j, k, idx; 3007 struct ctl_port *port; 3008 struct ctl_frontend *fe; 3009 3010 mtx_lock(&softc->ctl_lock); 3011 printf("CTL Persistent Reservation information start:\n"); 3012 for (i = 0; i < CTL_MAX_LUNS; i++) { 3013 struct ctl_lun *lun; 3014 3015 lun = softc->ctl_luns[i]; 3016 3017 if ((lun == NULL) 3018 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3019 continue; 3020 3021 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3022 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3023 idx = j * CTL_MAX_INIT_PER_PORT + k; 3024 if (lun->pr_keys[idx] == 0) 3025 continue; 3026 printf(" LUN %d port %d iid %d key " 3027 "%#jx\n", i, j, k, 3028 (uintmax_t)lun->pr_keys[idx]); 3029 } 3030 } 3031 } 3032 printf("CTL Persistent Reservation information end\n"); 3033 printf("CTL Ports:\n"); 3034 STAILQ_FOREACH(port, &softc->port_list, links) { 3035 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3036 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3037 port->frontend->name, port->port_type, 3038 port->physical_port, port->virtual_port, 3039 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3040 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3041 if (port->wwpn_iid[j].in_use == 0 && 3042 port->wwpn_iid[j].wwpn == 0 && 3043 port->wwpn_iid[j].name == NULL) 3044 continue; 3045 3046 printf(" iid %u use %d WWPN %#jx '%s'\n", 3047 j, port->wwpn_iid[j].in_use, 3048 (uintmax_t)port->wwpn_iid[j].wwpn, 3049 port->wwpn_iid[j].name); 3050 } 3051 } 3052 printf("CTL Port information end\n"); 3053 mtx_unlock(&softc->ctl_lock); 3054 /* 3055 * XXX KDM calling this without a lock. We'd likely want 3056 * to drop the lock before calling the frontend's dump 3057 * routine anyway. 3058 */ 3059 printf("CTL Frontends:\n"); 3060 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3061 printf(" Frontend '%s'\n", fe->name); 3062 if (fe->fe_dump != NULL) 3063 fe->fe_dump(); 3064 } 3065 printf("CTL Frontend information end\n"); 3066 break; 3067 } 3068 case CTL_LUN_REQ: { 3069 struct ctl_lun_req *lun_req; 3070 struct ctl_backend_driver *backend; 3071 3072 lun_req = (struct ctl_lun_req *)addr; 3073 3074 backend = ctl_backend_find(lun_req->backend); 3075 if (backend == NULL) { 3076 lun_req->status = CTL_LUN_ERROR; 3077 snprintf(lun_req->error_str, 3078 sizeof(lun_req->error_str), 3079 "Backend \"%s\" not found.", 3080 lun_req->backend); 3081 break; 3082 } 3083 if (lun_req->num_be_args > 0) { 3084 lun_req->kern_be_args = ctl_copyin_args( 3085 lun_req->num_be_args, 3086 lun_req->be_args, 3087 lun_req->error_str, 3088 sizeof(lun_req->error_str)); 3089 if (lun_req->kern_be_args == NULL) { 3090 lun_req->status = CTL_LUN_ERROR; 3091 break; 3092 } 3093 } 3094 3095 retval = backend->ioctl(dev, cmd, addr, flag, td); 3096 3097 if (lun_req->num_be_args > 0) { 3098 ctl_copyout_args(lun_req->num_be_args, 3099 lun_req->kern_be_args); 3100 ctl_free_args(lun_req->num_be_args, 3101 lun_req->kern_be_args); 3102 } 3103 break; 3104 } 3105 case CTL_LUN_LIST: { 3106 struct sbuf *sb; 3107 struct ctl_lun *lun; 3108 struct ctl_lun_list *list; 3109 struct ctl_option *opt; 3110 3111 list = (struct ctl_lun_list *)addr; 3112 3113 /* 3114 * Allocate a fixed length sbuf here, based on the length 3115 * of the user's buffer. We could allocate an auto-extending 3116 * buffer, and then tell the user how much larger our 3117 * amount of data is than his buffer, but that presents 3118 * some problems: 3119 * 3120 * 1. The sbuf(9) routines use a blocking malloc, and so 3121 * we can't hold a lock while calling them with an 3122 * auto-extending buffer. 3123 * 3124 * 2. There is not currently a LUN reference counting 3125 * mechanism, outside of outstanding transactions on 3126 * the LUN's OOA queue. So a LUN could go away on us 3127 * while we're getting the LUN number, backend-specific 3128 * information, etc. Thus, given the way things 3129 * currently work, we need to hold the CTL lock while 3130 * grabbing LUN information. 3131 * 3132 * So, from the user's standpoint, the best thing to do is 3133 * allocate what he thinks is a reasonable buffer length, 3134 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3135 * double the buffer length and try again. (And repeat 3136 * that until he succeeds.) 3137 */ 3138 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3139 if (sb == NULL) { 3140 list->status = CTL_LUN_LIST_ERROR; 3141 snprintf(list->error_str, sizeof(list->error_str), 3142 "Unable to allocate %d bytes for LUN list", 3143 list->alloc_len); 3144 break; 3145 } 3146 3147 sbuf_printf(sb, "<ctllunlist>\n"); 3148 3149 mtx_lock(&softc->ctl_lock); 3150 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3151 mtx_lock(&lun->lun_lock); 3152 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3153 (uintmax_t)lun->lun); 3154 3155 /* 3156 * Bail out as soon as we see that we've overfilled 3157 * the buffer. 3158 */ 3159 if (retval != 0) 3160 break; 3161 3162 retval = sbuf_printf(sb, "\t<backend_type>%s" 3163 "</backend_type>\n", 3164 (lun->backend == NULL) ? "none" : 3165 lun->backend->name); 3166 3167 if (retval != 0) 3168 break; 3169 3170 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3171 lun->be_lun->lun_type); 3172 3173 if (retval != 0) 3174 break; 3175 3176 if (lun->backend == NULL) { 3177 retval = sbuf_printf(sb, "</lun>\n"); 3178 if (retval != 0) 3179 break; 3180 continue; 3181 } 3182 3183 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3184 (lun->be_lun->maxlba > 0) ? 3185 lun->be_lun->maxlba + 1 : 0); 3186 3187 if (retval != 0) 3188 break; 3189 3190 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3191 lun->be_lun->blocksize); 3192 3193 if (retval != 0) 3194 break; 3195 3196 retval = sbuf_printf(sb, "\t<serial_number>"); 3197 3198 if (retval != 0) 3199 break; 3200 3201 retval = ctl_sbuf_printf_esc(sb, 3202 lun->be_lun->serial_num, 3203 sizeof(lun->be_lun->serial_num)); 3204 3205 if (retval != 0) 3206 break; 3207 3208 retval = sbuf_printf(sb, "</serial_number>\n"); 3209 3210 if (retval != 0) 3211 break; 3212 3213 retval = sbuf_printf(sb, "\t<device_id>"); 3214 3215 if (retval != 0) 3216 break; 3217 3218 retval = ctl_sbuf_printf_esc(sb, 3219 lun->be_lun->device_id, 3220 sizeof(lun->be_lun->device_id)); 3221 3222 if (retval != 0) 3223 break; 3224 3225 retval = sbuf_printf(sb, "</device_id>\n"); 3226 3227 if (retval != 0) 3228 break; 3229 3230 if (lun->backend->lun_info != NULL) { 3231 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3232 if (retval != 0) 3233 break; 3234 } 3235 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3236 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3237 opt->name, opt->value, opt->name); 3238 if (retval != 0) 3239 break; 3240 } 3241 3242 retval = sbuf_printf(sb, "</lun>\n"); 3243 3244 if (retval != 0) 3245 break; 3246 mtx_unlock(&lun->lun_lock); 3247 } 3248 if (lun != NULL) 3249 mtx_unlock(&lun->lun_lock); 3250 mtx_unlock(&softc->ctl_lock); 3251 3252 if ((retval != 0) 3253 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3254 retval = 0; 3255 sbuf_delete(sb); 3256 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3257 snprintf(list->error_str, sizeof(list->error_str), 3258 "Out of space, %d bytes is too small", 3259 list->alloc_len); 3260 break; 3261 } 3262 3263 sbuf_finish(sb); 3264 3265 retval = copyout(sbuf_data(sb), list->lun_xml, 3266 sbuf_len(sb) + 1); 3267 3268 list->fill_len = sbuf_len(sb) + 1; 3269 list->status = CTL_LUN_LIST_OK; 3270 sbuf_delete(sb); 3271 break; 3272 } 3273 case CTL_ISCSI: { 3274 struct ctl_iscsi *ci; 3275 struct ctl_frontend *fe; 3276 3277 ci = (struct ctl_iscsi *)addr; 3278 3279 fe = ctl_frontend_find("iscsi"); 3280 if (fe == NULL) { 3281 ci->status = CTL_ISCSI_ERROR; 3282 snprintf(ci->error_str, sizeof(ci->error_str), 3283 "Frontend \"iscsi\" not found."); 3284 break; 3285 } 3286 3287 retval = fe->ioctl(dev, cmd, addr, flag, td); 3288 break; 3289 } 3290 case CTL_PORT_REQ: { 3291 struct ctl_req *req; 3292 struct ctl_frontend *fe; 3293 3294 req = (struct ctl_req *)addr; 3295 3296 fe = ctl_frontend_find(req->driver); 3297 if (fe == NULL) { 3298 req->status = CTL_LUN_ERROR; 3299 snprintf(req->error_str, sizeof(req->error_str), 3300 "Frontend \"%s\" not found.", req->driver); 3301 break; 3302 } 3303 if (req->num_args > 0) { 3304 req->kern_args = ctl_copyin_args(req->num_args, 3305 req->args, req->error_str, sizeof(req->error_str)); 3306 if (req->kern_args == NULL) { 3307 req->status = CTL_LUN_ERROR; 3308 break; 3309 } 3310 } 3311 3312 retval = fe->ioctl(dev, cmd, addr, flag, td); 3313 3314 if (req->num_args > 0) { 3315 ctl_copyout_args(req->num_args, req->kern_args); 3316 ctl_free_args(req->num_args, req->kern_args); 3317 } 3318 break; 3319 } 3320 case CTL_PORT_LIST: { 3321 struct sbuf *sb; 3322 struct ctl_port *port; 3323 struct ctl_lun_list *list; 3324 struct ctl_option *opt; 3325 int j; 3326 3327 list = (struct ctl_lun_list *)addr; 3328 3329 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3330 if (sb == NULL) { 3331 list->status = CTL_LUN_LIST_ERROR; 3332 snprintf(list->error_str, sizeof(list->error_str), 3333 "Unable to allocate %d bytes for LUN list", 3334 list->alloc_len); 3335 break; 3336 } 3337 3338 sbuf_printf(sb, "<ctlportlist>\n"); 3339 3340 mtx_lock(&softc->ctl_lock); 3341 STAILQ_FOREACH(port, &softc->port_list, links) { 3342 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3343 (uintmax_t)port->targ_port); 3344 3345 /* 3346 * Bail out as soon as we see that we've overfilled 3347 * the buffer. 3348 */ 3349 if (retval != 0) 3350 break; 3351 3352 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3353 "</frontend_type>\n", port->frontend->name); 3354 if (retval != 0) 3355 break; 3356 3357 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3358 port->port_type); 3359 if (retval != 0) 3360 break; 3361 3362 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3363 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3364 if (retval != 0) 3365 break; 3366 3367 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3368 port->port_name); 3369 if (retval != 0) 3370 break; 3371 3372 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3373 port->physical_port); 3374 if (retval != 0) 3375 break; 3376 3377 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3378 port->virtual_port); 3379 if (retval != 0) 3380 break; 3381 3382 if (port->target_devid != NULL) { 3383 sbuf_printf(sb, "\t<target>"); 3384 ctl_id_sbuf(port->target_devid, sb); 3385 sbuf_printf(sb, "</target>\n"); 3386 } 3387 3388 if (port->port_devid != NULL) { 3389 sbuf_printf(sb, "\t<port>"); 3390 ctl_id_sbuf(port->port_devid, sb); 3391 sbuf_printf(sb, "</port>\n"); 3392 } 3393 3394 if (port->port_info != NULL) { 3395 retval = port->port_info(port->onoff_arg, sb); 3396 if (retval != 0) 3397 break; 3398 } 3399 STAILQ_FOREACH(opt, &port->options, links) { 3400 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3401 opt->name, opt->value, opt->name); 3402 if (retval != 0) 3403 break; 3404 } 3405 3406 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3407 if (port->wwpn_iid[j].in_use == 0 || 3408 (port->wwpn_iid[j].wwpn == 0 && 3409 port->wwpn_iid[j].name == NULL)) 3410 continue; 3411 3412 if (port->wwpn_iid[j].name != NULL) 3413 retval = sbuf_printf(sb, 3414 "\t<initiator>%u %s</initiator>\n", 3415 j, port->wwpn_iid[j].name); 3416 else 3417 retval = sbuf_printf(sb, 3418 "\t<initiator>%u naa.%08jx</initiator>\n", 3419 j, port->wwpn_iid[j].wwpn); 3420 if (retval != 0) 3421 break; 3422 } 3423 if (retval != 0) 3424 break; 3425 3426 retval = sbuf_printf(sb, "</targ_port>\n"); 3427 if (retval != 0) 3428 break; 3429 } 3430 mtx_unlock(&softc->ctl_lock); 3431 3432 if ((retval != 0) 3433 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3434 retval = 0; 3435 sbuf_delete(sb); 3436 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3437 snprintf(list->error_str, sizeof(list->error_str), 3438 "Out of space, %d bytes is too small", 3439 list->alloc_len); 3440 break; 3441 } 3442 3443 sbuf_finish(sb); 3444 3445 retval = copyout(sbuf_data(sb), list->lun_xml, 3446 sbuf_len(sb) + 1); 3447 3448 list->fill_len = sbuf_len(sb) + 1; 3449 list->status = CTL_LUN_LIST_OK; 3450 sbuf_delete(sb); 3451 break; 3452 } 3453 default: { 3454 /* XXX KDM should we fix this? */ 3455 #if 0 3456 struct ctl_backend_driver *backend; 3457 unsigned int type; 3458 int found; 3459 3460 found = 0; 3461 3462 /* 3463 * We encode the backend type as the ioctl type for backend 3464 * ioctls. So parse it out here, and then search for a 3465 * backend of this type. 3466 */ 3467 type = _IOC_TYPE(cmd); 3468 3469 STAILQ_FOREACH(backend, &softc->be_list, links) { 3470 if (backend->type == type) { 3471 found = 1; 3472 break; 3473 } 3474 } 3475 if (found == 0) { 3476 printf("ctl: unknown ioctl command %#lx or backend " 3477 "%d\n", cmd, type); 3478 retval = EINVAL; 3479 break; 3480 } 3481 retval = backend->ioctl(dev, cmd, addr, flag, td); 3482 #endif 3483 retval = ENOTTY; 3484 break; 3485 } 3486 } 3487 return (retval); 3488 } 3489 3490 uint32_t 3491 ctl_get_initindex(struct ctl_nexus *nexus) 3492 { 3493 if (nexus->targ_port < CTL_MAX_PORTS) 3494 return (nexus->initid.id + 3495 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3496 else 3497 return (nexus->initid.id + 3498 ((nexus->targ_port - CTL_MAX_PORTS) * 3499 CTL_MAX_INIT_PER_PORT)); 3500 } 3501 3502 uint32_t 3503 ctl_get_resindex(struct ctl_nexus *nexus) 3504 { 3505 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3506 } 3507 3508 uint32_t 3509 ctl_port_idx(int port_num) 3510 { 3511 if (port_num < CTL_MAX_PORTS) 3512 return(port_num); 3513 else 3514 return(port_num - CTL_MAX_PORTS); 3515 } 3516 3517 static uint32_t 3518 ctl_map_lun(int port_num, uint32_t lun_id) 3519 { 3520 struct ctl_port *port; 3521 3522 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3523 if (port == NULL) 3524 return (UINT32_MAX); 3525 if (port->lun_map == NULL) 3526 return (lun_id); 3527 return (port->lun_map(port->targ_lun_arg, lun_id)); 3528 } 3529 3530 static uint32_t 3531 ctl_map_lun_back(int port_num, uint32_t lun_id) 3532 { 3533 struct ctl_port *port; 3534 uint32_t i; 3535 3536 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3537 if (port->lun_map == NULL) 3538 return (lun_id); 3539 for (i = 0; i < CTL_MAX_LUNS; i++) { 3540 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3541 return (i); 3542 } 3543 return (UINT32_MAX); 3544 } 3545 3546 /* 3547 * Note: This only works for bitmask sizes that are at least 32 bits, and 3548 * that are a power of 2. 3549 */ 3550 int 3551 ctl_ffz(uint32_t *mask, uint32_t size) 3552 { 3553 uint32_t num_chunks, num_pieces; 3554 int i, j; 3555 3556 num_chunks = (size >> 5); 3557 if (num_chunks == 0) 3558 num_chunks++; 3559 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3560 3561 for (i = 0; i < num_chunks; i++) { 3562 for (j = 0; j < num_pieces; j++) { 3563 if ((mask[i] & (1 << j)) == 0) 3564 return ((i << 5) + j); 3565 } 3566 } 3567 3568 return (-1); 3569 } 3570 3571 int 3572 ctl_set_mask(uint32_t *mask, uint32_t bit) 3573 { 3574 uint32_t chunk, piece; 3575 3576 chunk = bit >> 5; 3577 piece = bit % (sizeof(uint32_t) * 8); 3578 3579 if ((mask[chunk] & (1 << piece)) != 0) 3580 return (-1); 3581 else 3582 mask[chunk] |= (1 << piece); 3583 3584 return (0); 3585 } 3586 3587 int 3588 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3589 { 3590 uint32_t chunk, piece; 3591 3592 chunk = bit >> 5; 3593 piece = bit % (sizeof(uint32_t) * 8); 3594 3595 if ((mask[chunk] & (1 << piece)) == 0) 3596 return (-1); 3597 else 3598 mask[chunk] &= ~(1 << piece); 3599 3600 return (0); 3601 } 3602 3603 int 3604 ctl_is_set(uint32_t *mask, uint32_t bit) 3605 { 3606 uint32_t chunk, piece; 3607 3608 chunk = bit >> 5; 3609 piece = bit % (sizeof(uint32_t) * 8); 3610 3611 if ((mask[chunk] & (1 << piece)) == 0) 3612 return (0); 3613 else 3614 return (1); 3615 } 3616 3617 #ifdef unused 3618 /* 3619 * The bus, target and lun are optional, they can be filled in later. 3620 * can_wait is used to determine whether we can wait on the malloc or not. 3621 */ 3622 union ctl_io* 3623 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3624 uint32_t targ_lun, int can_wait) 3625 { 3626 union ctl_io *io; 3627 3628 if (can_wait) 3629 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3630 else 3631 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3632 3633 if (io != NULL) { 3634 io->io_hdr.io_type = io_type; 3635 io->io_hdr.targ_port = targ_port; 3636 /* 3637 * XXX KDM this needs to change/go away. We need to move 3638 * to a preallocated pool of ctl_scsiio structures. 3639 */ 3640 io->io_hdr.nexus.targ_target.id = targ_target; 3641 io->io_hdr.nexus.targ_lun = targ_lun; 3642 } 3643 3644 return (io); 3645 } 3646 3647 void 3648 ctl_kfree_io(union ctl_io *io) 3649 { 3650 free(io, M_CTL); 3651 } 3652 #endif /* unused */ 3653 3654 /* 3655 * ctl_softc, pool_type, total_ctl_io are passed in. 3656 * npool is passed out. 3657 */ 3658 int 3659 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3660 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3661 { 3662 uint32_t i; 3663 union ctl_io *cur_io, *next_io; 3664 struct ctl_io_pool *pool; 3665 int retval; 3666 3667 retval = 0; 3668 3669 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3670 M_NOWAIT | M_ZERO); 3671 if (pool == NULL) { 3672 retval = ENOMEM; 3673 goto bailout; 3674 } 3675 3676 pool->type = pool_type; 3677 pool->ctl_softc = ctl_softc; 3678 3679 mtx_lock(&ctl_softc->pool_lock); 3680 pool->id = ctl_softc->cur_pool_id++; 3681 mtx_unlock(&ctl_softc->pool_lock); 3682 3683 pool->flags = CTL_POOL_FLAG_NONE; 3684 pool->refcount = 1; /* Reference for validity. */ 3685 STAILQ_INIT(&pool->free_queue); 3686 3687 /* 3688 * XXX KDM other options here: 3689 * - allocate a page at a time 3690 * - allocate one big chunk of memory. 3691 * Page allocation might work well, but would take a little more 3692 * tracking. 3693 */ 3694 for (i = 0; i < total_ctl_io; i++) { 3695 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3696 M_NOWAIT); 3697 if (cur_io == NULL) { 3698 retval = ENOMEM; 3699 break; 3700 } 3701 cur_io->io_hdr.pool = pool; 3702 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3703 pool->total_ctl_io++; 3704 pool->free_ctl_io++; 3705 } 3706 3707 if (retval != 0) { 3708 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3709 cur_io != NULL; cur_io = next_io) { 3710 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3711 links); 3712 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3713 ctl_io_hdr, links); 3714 free(cur_io, M_CTLIO); 3715 } 3716 3717 free(pool, M_CTL); 3718 goto bailout; 3719 } 3720 mtx_lock(&ctl_softc->pool_lock); 3721 ctl_softc->num_pools++; 3722 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3723 /* 3724 * Increment our usage count if this is an external consumer, so we 3725 * can't get unloaded until the external consumer (most likely a 3726 * FETD) unloads and frees his pool. 3727 * 3728 * XXX KDM will this increment the caller's module use count, or 3729 * mine? 3730 */ 3731 #if 0 3732 if ((pool_type != CTL_POOL_EMERGENCY) 3733 && (pool_type != CTL_POOL_INTERNAL) 3734 && (pool_type != CTL_POOL_4OTHERSC)) 3735 MOD_INC_USE_COUNT; 3736 #endif 3737 3738 mtx_unlock(&ctl_softc->pool_lock); 3739 3740 *npool = pool; 3741 3742 bailout: 3743 3744 return (retval); 3745 } 3746 3747 static int 3748 ctl_pool_acquire(struct ctl_io_pool *pool) 3749 { 3750 3751 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3752 3753 if (pool->flags & CTL_POOL_FLAG_INVALID) 3754 return (EINVAL); 3755 3756 pool->refcount++; 3757 3758 return (0); 3759 } 3760 3761 static void 3762 ctl_pool_release(struct ctl_io_pool *pool) 3763 { 3764 struct ctl_softc *ctl_softc = pool->ctl_softc; 3765 union ctl_io *io; 3766 3767 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3768 3769 if (--pool->refcount != 0) 3770 return; 3771 3772 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3773 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3774 links); 3775 free(io, M_CTLIO); 3776 } 3777 3778 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3779 ctl_softc->num_pools--; 3780 3781 /* 3782 * XXX KDM will this decrement the caller's usage count or mine? 3783 */ 3784 #if 0 3785 if ((pool->type != CTL_POOL_EMERGENCY) 3786 && (pool->type != CTL_POOL_INTERNAL) 3787 && (pool->type != CTL_POOL_4OTHERSC)) 3788 MOD_DEC_USE_COUNT; 3789 #endif 3790 3791 free(pool, M_CTL); 3792 } 3793 3794 void 3795 ctl_pool_free(struct ctl_io_pool *pool) 3796 { 3797 struct ctl_softc *ctl_softc; 3798 3799 if (pool == NULL) 3800 return; 3801 3802 ctl_softc = pool->ctl_softc; 3803 mtx_lock(&ctl_softc->pool_lock); 3804 pool->flags |= CTL_POOL_FLAG_INVALID; 3805 ctl_pool_release(pool); 3806 mtx_unlock(&ctl_softc->pool_lock); 3807 } 3808 3809 /* 3810 * This routine does not block (except for spinlocks of course). 3811 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3812 * possible. 3813 */ 3814 union ctl_io * 3815 ctl_alloc_io(void *pool_ref) 3816 { 3817 union ctl_io *io; 3818 struct ctl_softc *ctl_softc; 3819 struct ctl_io_pool *pool, *npool; 3820 struct ctl_io_pool *emergency_pool; 3821 3822 pool = (struct ctl_io_pool *)pool_ref; 3823 3824 if (pool == NULL) { 3825 printf("%s: pool is NULL\n", __func__); 3826 return (NULL); 3827 } 3828 3829 emergency_pool = NULL; 3830 3831 ctl_softc = pool->ctl_softc; 3832 3833 mtx_lock(&ctl_softc->pool_lock); 3834 /* 3835 * First, try to get the io structure from the user's pool. 3836 */ 3837 if (ctl_pool_acquire(pool) == 0) { 3838 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3839 if (io != NULL) { 3840 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3841 pool->total_allocated++; 3842 pool->free_ctl_io--; 3843 mtx_unlock(&ctl_softc->pool_lock); 3844 return (io); 3845 } else 3846 ctl_pool_release(pool); 3847 } 3848 /* 3849 * If he doesn't have any io structures left, search for an 3850 * emergency pool and grab one from there. 3851 */ 3852 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3853 if (npool->type != CTL_POOL_EMERGENCY) 3854 continue; 3855 3856 if (ctl_pool_acquire(npool) != 0) 3857 continue; 3858 3859 emergency_pool = npool; 3860 3861 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3862 if (io != NULL) { 3863 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3864 npool->total_allocated++; 3865 npool->free_ctl_io--; 3866 mtx_unlock(&ctl_softc->pool_lock); 3867 return (io); 3868 } else 3869 ctl_pool_release(npool); 3870 } 3871 3872 /* Drop the spinlock before we malloc */ 3873 mtx_unlock(&ctl_softc->pool_lock); 3874 3875 /* 3876 * The emergency pool (if it exists) didn't have one, so try an 3877 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3878 */ 3879 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3880 if (io != NULL) { 3881 /* 3882 * If the emergency pool exists but is empty, add this 3883 * ctl_io to its list when it gets freed. 3884 */ 3885 if (emergency_pool != NULL) { 3886 mtx_lock(&ctl_softc->pool_lock); 3887 if (ctl_pool_acquire(emergency_pool) == 0) { 3888 io->io_hdr.pool = emergency_pool; 3889 emergency_pool->total_ctl_io++; 3890 /* 3891 * Need to bump this, otherwise 3892 * total_allocated and total_freed won't 3893 * match when we no longer have anything 3894 * outstanding. 3895 */ 3896 emergency_pool->total_allocated++; 3897 } 3898 mtx_unlock(&ctl_softc->pool_lock); 3899 } else 3900 io->io_hdr.pool = NULL; 3901 } 3902 3903 return (io); 3904 } 3905 3906 void 3907 ctl_free_io(union ctl_io *io) 3908 { 3909 if (io == NULL) 3910 return; 3911 3912 /* 3913 * If this ctl_io has a pool, return it to that pool. 3914 */ 3915 if (io->io_hdr.pool != NULL) { 3916 struct ctl_io_pool *pool; 3917 3918 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3919 mtx_lock(&pool->ctl_softc->pool_lock); 3920 io->io_hdr.io_type = 0xff; 3921 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3922 pool->total_freed++; 3923 pool->free_ctl_io++; 3924 ctl_pool_release(pool); 3925 mtx_unlock(&pool->ctl_softc->pool_lock); 3926 } else { 3927 /* 3928 * Otherwise, just free it. We probably malloced it and 3929 * the emergency pool wasn't available. 3930 */ 3931 free(io, M_CTLIO); 3932 } 3933 3934 } 3935 3936 void 3937 ctl_zero_io(union ctl_io *io) 3938 { 3939 void *pool_ref; 3940 3941 if (io == NULL) 3942 return; 3943 3944 /* 3945 * May need to preserve linked list pointers at some point too. 3946 */ 3947 pool_ref = io->io_hdr.pool; 3948 3949 memset(io, 0, sizeof(*io)); 3950 3951 io->io_hdr.pool = pool_ref; 3952 } 3953 3954 /* 3955 * This routine is currently used for internal copies of ctl_ios that need 3956 * to persist for some reason after we've already returned status to the 3957 * FETD. (Thus the flag set.) 3958 * 3959 * XXX XXX 3960 * Note that this makes a blind copy of all fields in the ctl_io, except 3961 * for the pool reference. This includes any memory that has been 3962 * allocated! That memory will no longer be valid after done has been 3963 * called, so this would be VERY DANGEROUS for command that actually does 3964 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3965 * start and stop commands, which don't transfer any data, so this is not a 3966 * problem. If it is used for anything else, the caller would also need to 3967 * allocate data buffer space and this routine would need to be modified to 3968 * copy the data buffer(s) as well. 3969 */ 3970 void 3971 ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3972 { 3973 void *pool_ref; 3974 3975 if ((src == NULL) 3976 || (dest == NULL)) 3977 return; 3978 3979 /* 3980 * May need to preserve linked list pointers at some point too. 3981 */ 3982 pool_ref = dest->io_hdr.pool; 3983 3984 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3985 3986 dest->io_hdr.pool = pool_ref; 3987 /* 3988 * We need to know that this is an internal copy, and doesn't need 3989 * to get passed back to the FETD that allocated it. 3990 */ 3991 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3992 } 3993 3994 /* 3995 * This routine could be used in the future to load default and/or saved 3996 * mode page parameters for a particuar lun. 3997 */ 3998 static int 3999 ctl_init_page_index(struct ctl_lun *lun) 4000 { 4001 int i; 4002 struct ctl_page_index *page_index; 4003 const char *value; 4004 4005 memcpy(&lun->mode_pages.index, page_index_template, 4006 sizeof(page_index_template)); 4007 4008 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4009 4010 page_index = &lun->mode_pages.index[i]; 4011 /* 4012 * If this is a disk-only mode page, there's no point in 4013 * setting it up. For some pages, we have to have some 4014 * basic information about the disk in order to calculate the 4015 * mode page data. 4016 */ 4017 if ((lun->be_lun->lun_type != T_DIRECT) 4018 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4019 continue; 4020 4021 switch (page_index->page_code & SMPH_PC_MASK) { 4022 case SMS_RW_ERROR_RECOVERY_PAGE: { 4023 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4024 panic("subpage is incorrect!"); 4025 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4026 &rw_er_page_default, 4027 sizeof(rw_er_page_default)); 4028 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4029 &rw_er_page_changeable, 4030 sizeof(rw_er_page_changeable)); 4031 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4032 &rw_er_page_default, 4033 sizeof(rw_er_page_default)); 4034 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4035 &rw_er_page_default, 4036 sizeof(rw_er_page_default)); 4037 page_index->page_data = 4038 (uint8_t *)lun->mode_pages.rw_er_page; 4039 break; 4040 } 4041 case SMS_FORMAT_DEVICE_PAGE: { 4042 struct scsi_format_page *format_page; 4043 4044 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4045 panic("subpage is incorrect!"); 4046 4047 /* 4048 * Sectors per track are set above. Bytes per 4049 * sector need to be set here on a per-LUN basis. 4050 */ 4051 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4052 &format_page_default, 4053 sizeof(format_page_default)); 4054 memcpy(&lun->mode_pages.format_page[ 4055 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4056 sizeof(format_page_changeable)); 4057 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4058 &format_page_default, 4059 sizeof(format_page_default)); 4060 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4061 &format_page_default, 4062 sizeof(format_page_default)); 4063 4064 format_page = &lun->mode_pages.format_page[ 4065 CTL_PAGE_CURRENT]; 4066 scsi_ulto2b(lun->be_lun->blocksize, 4067 format_page->bytes_per_sector); 4068 4069 format_page = &lun->mode_pages.format_page[ 4070 CTL_PAGE_DEFAULT]; 4071 scsi_ulto2b(lun->be_lun->blocksize, 4072 format_page->bytes_per_sector); 4073 4074 format_page = &lun->mode_pages.format_page[ 4075 CTL_PAGE_SAVED]; 4076 scsi_ulto2b(lun->be_lun->blocksize, 4077 format_page->bytes_per_sector); 4078 4079 page_index->page_data = 4080 (uint8_t *)lun->mode_pages.format_page; 4081 break; 4082 } 4083 case SMS_RIGID_DISK_PAGE: { 4084 struct scsi_rigid_disk_page *rigid_disk_page; 4085 uint32_t sectors_per_cylinder; 4086 uint64_t cylinders; 4087 #ifndef __XSCALE__ 4088 int shift; 4089 #endif /* !__XSCALE__ */ 4090 4091 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4092 panic("invalid subpage value %d", 4093 page_index->subpage); 4094 4095 /* 4096 * Rotation rate and sectors per track are set 4097 * above. We calculate the cylinders here based on 4098 * capacity. Due to the number of heads and 4099 * sectors per track we're using, smaller arrays 4100 * may turn out to have 0 cylinders. Linux and 4101 * FreeBSD don't pay attention to these mode pages 4102 * to figure out capacity, but Solaris does. It 4103 * seems to deal with 0 cylinders just fine, and 4104 * works out a fake geometry based on the capacity. 4105 */ 4106 memcpy(&lun->mode_pages.rigid_disk_page[ 4107 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4108 sizeof(rigid_disk_page_default)); 4109 memcpy(&lun->mode_pages.rigid_disk_page[ 4110 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4111 sizeof(rigid_disk_page_changeable)); 4112 memcpy(&lun->mode_pages.rigid_disk_page[ 4113 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4114 sizeof(rigid_disk_page_default)); 4115 memcpy(&lun->mode_pages.rigid_disk_page[ 4116 CTL_PAGE_SAVED], &rigid_disk_page_default, 4117 sizeof(rigid_disk_page_default)); 4118 4119 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4120 CTL_DEFAULT_HEADS; 4121 4122 /* 4123 * The divide method here will be more accurate, 4124 * probably, but results in floating point being 4125 * used in the kernel on i386 (__udivdi3()). On the 4126 * XScale, though, __udivdi3() is implemented in 4127 * software. 4128 * 4129 * The shift method for cylinder calculation is 4130 * accurate if sectors_per_cylinder is a power of 4131 * 2. Otherwise it might be slightly off -- you 4132 * might have a bit of a truncation problem. 4133 */ 4134 #ifdef __XSCALE__ 4135 cylinders = (lun->be_lun->maxlba + 1) / 4136 sectors_per_cylinder; 4137 #else 4138 for (shift = 31; shift > 0; shift--) { 4139 if (sectors_per_cylinder & (1 << shift)) 4140 break; 4141 } 4142 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4143 #endif 4144 4145 /* 4146 * We've basically got 3 bytes, or 24 bits for the 4147 * cylinder size in the mode page. If we're over, 4148 * just round down to 2^24. 4149 */ 4150 if (cylinders > 0xffffff) 4151 cylinders = 0xffffff; 4152 4153 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4154 CTL_PAGE_CURRENT]; 4155 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4156 4157 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4158 CTL_PAGE_DEFAULT]; 4159 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4160 4161 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4162 CTL_PAGE_SAVED]; 4163 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4164 4165 page_index->page_data = 4166 (uint8_t *)lun->mode_pages.rigid_disk_page; 4167 break; 4168 } 4169 case SMS_CACHING_PAGE: { 4170 struct scsi_caching_page *caching_page; 4171 4172 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4173 panic("invalid subpage value %d", 4174 page_index->subpage); 4175 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4176 &caching_page_default, 4177 sizeof(caching_page_default)); 4178 memcpy(&lun->mode_pages.caching_page[ 4179 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4180 sizeof(caching_page_changeable)); 4181 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4182 &caching_page_default, 4183 sizeof(caching_page_default)); 4184 caching_page = &lun->mode_pages.caching_page[ 4185 CTL_PAGE_SAVED]; 4186 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4187 if (value != NULL && strcmp(value, "off") == 0) 4188 caching_page->flags1 &= ~SCP_WCE; 4189 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4190 if (value != NULL && strcmp(value, "off") == 0) 4191 caching_page->flags1 |= SCP_RCD; 4192 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4193 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4194 sizeof(caching_page_default)); 4195 page_index->page_data = 4196 (uint8_t *)lun->mode_pages.caching_page; 4197 break; 4198 } 4199 case SMS_CONTROL_MODE_PAGE: { 4200 struct scsi_control_page *control_page; 4201 4202 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4203 panic("invalid subpage value %d", 4204 page_index->subpage); 4205 4206 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4207 &control_page_default, 4208 sizeof(control_page_default)); 4209 memcpy(&lun->mode_pages.control_page[ 4210 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4211 sizeof(control_page_changeable)); 4212 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4213 &control_page_default, 4214 sizeof(control_page_default)); 4215 control_page = &lun->mode_pages.control_page[ 4216 CTL_PAGE_SAVED]; 4217 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4218 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4219 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4220 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4221 } 4222 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4223 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4224 sizeof(control_page_default)); 4225 page_index->page_data = 4226 (uint8_t *)lun->mode_pages.control_page; 4227 break; 4228 4229 } 4230 case SMS_INFO_EXCEPTIONS_PAGE: { 4231 switch (page_index->subpage) { 4232 case SMS_SUBPAGE_PAGE_0: 4233 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4234 &ie_page_default, 4235 sizeof(ie_page_default)); 4236 memcpy(&lun->mode_pages.ie_page[ 4237 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4238 sizeof(ie_page_changeable)); 4239 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4240 &ie_page_default, 4241 sizeof(ie_page_default)); 4242 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4243 &ie_page_default, 4244 sizeof(ie_page_default)); 4245 page_index->page_data = 4246 (uint8_t *)lun->mode_pages.ie_page; 4247 break; 4248 case 0x02: 4249 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4250 &lbp_page_default, 4251 sizeof(lbp_page_default)); 4252 memcpy(&lun->mode_pages.lbp_page[ 4253 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4254 sizeof(lbp_page_changeable)); 4255 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4256 &lbp_page_default, 4257 sizeof(lbp_page_default)); 4258 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4259 &lbp_page_default, 4260 sizeof(lbp_page_default)); 4261 page_index->page_data = 4262 (uint8_t *)lun->mode_pages.lbp_page; 4263 } 4264 break; 4265 } 4266 case SMS_VENDOR_SPECIFIC_PAGE:{ 4267 switch (page_index->subpage) { 4268 case DBGCNF_SUBPAGE_CODE: { 4269 struct copan_debugconf_subpage *current_page, 4270 *saved_page; 4271 4272 memcpy(&lun->mode_pages.debugconf_subpage[ 4273 CTL_PAGE_CURRENT], 4274 &debugconf_page_default, 4275 sizeof(debugconf_page_default)); 4276 memcpy(&lun->mode_pages.debugconf_subpage[ 4277 CTL_PAGE_CHANGEABLE], 4278 &debugconf_page_changeable, 4279 sizeof(debugconf_page_changeable)); 4280 memcpy(&lun->mode_pages.debugconf_subpage[ 4281 CTL_PAGE_DEFAULT], 4282 &debugconf_page_default, 4283 sizeof(debugconf_page_default)); 4284 memcpy(&lun->mode_pages.debugconf_subpage[ 4285 CTL_PAGE_SAVED], 4286 &debugconf_page_default, 4287 sizeof(debugconf_page_default)); 4288 page_index->page_data = 4289 (uint8_t *)lun->mode_pages.debugconf_subpage; 4290 4291 current_page = (struct copan_debugconf_subpage *) 4292 (page_index->page_data + 4293 (page_index->page_len * 4294 CTL_PAGE_CURRENT)); 4295 saved_page = (struct copan_debugconf_subpage *) 4296 (page_index->page_data + 4297 (page_index->page_len * 4298 CTL_PAGE_SAVED)); 4299 break; 4300 } 4301 default: 4302 panic("invalid subpage value %d", 4303 page_index->subpage); 4304 break; 4305 } 4306 break; 4307 } 4308 default: 4309 panic("invalid page value %d", 4310 page_index->page_code & SMPH_PC_MASK); 4311 break; 4312 } 4313 } 4314 4315 return (CTL_RETVAL_COMPLETE); 4316 } 4317 4318 static int 4319 ctl_init_log_page_index(struct ctl_lun *lun) 4320 { 4321 struct ctl_page_index *page_index; 4322 int i, j, prev; 4323 4324 memcpy(&lun->log_pages.index, log_page_index_template, 4325 sizeof(log_page_index_template)); 4326 4327 prev = -1; 4328 for (i = 0, j = 0; i < CTL_NUM_LOG_PAGES; i++) { 4329 4330 page_index = &lun->log_pages.index[i]; 4331 /* 4332 * If this is a disk-only mode page, there's no point in 4333 * setting it up. For some pages, we have to have some 4334 * basic information about the disk in order to calculate the 4335 * mode page data. 4336 */ 4337 if ((lun->be_lun->lun_type != T_DIRECT) 4338 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4339 continue; 4340 4341 if (page_index->page_code != prev) { 4342 lun->log_pages.pages_page[j] = page_index->page_code; 4343 prev = page_index->page_code; 4344 j++; 4345 } 4346 lun->log_pages.subpages_page[i*2] = page_index->page_code; 4347 lun->log_pages.subpages_page[i*2+1] = page_index->subpage; 4348 } 4349 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4350 lun->log_pages.index[0].page_len = j; 4351 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4352 lun->log_pages.index[1].page_len = i * 2; 4353 4354 return (CTL_RETVAL_COMPLETE); 4355 } 4356 4357 static int 4358 hex2bin(const char *str, uint8_t *buf, int buf_size) 4359 { 4360 int i; 4361 u_char c; 4362 4363 memset(buf, 0, buf_size); 4364 while (isspace(str[0])) 4365 str++; 4366 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4367 str += 2; 4368 buf_size *= 2; 4369 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4370 c = str[i]; 4371 if (isdigit(c)) 4372 c -= '0'; 4373 else if (isalpha(c)) 4374 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4375 else 4376 break; 4377 if (c >= 16) 4378 break; 4379 if ((i & 1) == 0) 4380 buf[i / 2] |= (c << 4); 4381 else 4382 buf[i / 2] |= c; 4383 } 4384 return ((i + 1) / 2); 4385 } 4386 4387 /* 4388 * LUN allocation. 4389 * 4390 * Requirements: 4391 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4392 * wants us to allocate the LUN and he can block. 4393 * - ctl_softc is always set 4394 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4395 * 4396 * Returns 0 for success, non-zero (errno) for failure. 4397 */ 4398 static int 4399 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4400 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4401 { 4402 struct ctl_lun *nlun, *lun; 4403 struct ctl_port *port; 4404 struct scsi_vpd_id_descriptor *desc; 4405 struct scsi_vpd_id_t10 *t10id; 4406 const char *eui, *naa, *scsiname, *vendor, *value; 4407 int lun_number, i, lun_malloced; 4408 int devidlen, idlen1, idlen2 = 0, len; 4409 4410 if (be_lun == NULL) 4411 return (EINVAL); 4412 4413 /* 4414 * We currently only support Direct Access or Processor LUN types. 4415 */ 4416 switch (be_lun->lun_type) { 4417 case T_DIRECT: 4418 break; 4419 case T_PROCESSOR: 4420 break; 4421 case T_SEQUENTIAL: 4422 case T_CHANGER: 4423 default: 4424 be_lun->lun_config_status(be_lun->be_lun, 4425 CTL_LUN_CONFIG_FAILURE); 4426 break; 4427 } 4428 if (ctl_lun == NULL) { 4429 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4430 lun_malloced = 1; 4431 } else { 4432 lun_malloced = 0; 4433 lun = ctl_lun; 4434 } 4435 4436 memset(lun, 0, sizeof(*lun)); 4437 if (lun_malloced) 4438 lun->flags = CTL_LUN_MALLOCED; 4439 4440 /* Generate LUN ID. */ 4441 devidlen = max(CTL_DEVID_MIN_LEN, 4442 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4443 idlen1 = sizeof(*t10id) + devidlen; 4444 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4445 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4446 if (scsiname != NULL) { 4447 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4448 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4449 } 4450 eui = ctl_get_opt(&be_lun->options, "eui"); 4451 if (eui != NULL) { 4452 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4453 } 4454 naa = ctl_get_opt(&be_lun->options, "naa"); 4455 if (naa != NULL) { 4456 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4457 } 4458 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4459 M_CTL, M_WAITOK | M_ZERO); 4460 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4461 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4462 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4463 desc->length = idlen1; 4464 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4465 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4466 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4467 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4468 } else { 4469 strncpy(t10id->vendor, vendor, 4470 min(sizeof(t10id->vendor), strlen(vendor))); 4471 } 4472 strncpy((char *)t10id->vendor_spec_id, 4473 (char *)be_lun->device_id, devidlen); 4474 if (scsiname != NULL) { 4475 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4476 desc->length); 4477 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4478 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4479 SVPD_ID_TYPE_SCSI_NAME; 4480 desc->length = idlen2; 4481 strlcpy(desc->identifier, scsiname, idlen2); 4482 } 4483 if (eui != NULL) { 4484 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4485 desc->length); 4486 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4487 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4488 SVPD_ID_TYPE_EUI64; 4489 desc->length = hex2bin(eui, desc->identifier, 16); 4490 desc->length = desc->length > 12 ? 16 : 4491 (desc->length > 8 ? 12 : 8); 4492 len -= 16 - desc->length; 4493 } 4494 if (naa != NULL) { 4495 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4496 desc->length); 4497 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4498 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4499 SVPD_ID_TYPE_NAA; 4500 desc->length = hex2bin(naa, desc->identifier, 16); 4501 desc->length = desc->length > 8 ? 16 : 8; 4502 len -= 16 - desc->length; 4503 } 4504 lun->lun_devid->len = len; 4505 4506 mtx_lock(&ctl_softc->ctl_lock); 4507 /* 4508 * See if the caller requested a particular LUN number. If so, see 4509 * if it is available. Otherwise, allocate the first available LUN. 4510 */ 4511 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4512 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4513 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4514 mtx_unlock(&ctl_softc->ctl_lock); 4515 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4516 printf("ctl: requested LUN ID %d is higher " 4517 "than CTL_MAX_LUNS - 1 (%d)\n", 4518 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4519 } else { 4520 /* 4521 * XXX KDM return an error, or just assign 4522 * another LUN ID in this case?? 4523 */ 4524 printf("ctl: requested LUN ID %d is already " 4525 "in use\n", be_lun->req_lun_id); 4526 } 4527 if (lun->flags & CTL_LUN_MALLOCED) 4528 free(lun, M_CTL); 4529 be_lun->lun_config_status(be_lun->be_lun, 4530 CTL_LUN_CONFIG_FAILURE); 4531 return (ENOSPC); 4532 } 4533 lun_number = be_lun->req_lun_id; 4534 } else { 4535 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4536 if (lun_number == -1) { 4537 mtx_unlock(&ctl_softc->ctl_lock); 4538 printf("ctl: can't allocate LUN on target %ju, out of " 4539 "LUNs\n", (uintmax_t)target_id.id); 4540 if (lun->flags & CTL_LUN_MALLOCED) 4541 free(lun, M_CTL); 4542 be_lun->lun_config_status(be_lun->be_lun, 4543 CTL_LUN_CONFIG_FAILURE); 4544 return (ENOSPC); 4545 } 4546 } 4547 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4548 4549 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4550 lun->target = target_id; 4551 lun->lun = lun_number; 4552 lun->be_lun = be_lun; 4553 /* 4554 * The processor LUN is always enabled. Disk LUNs come on line 4555 * disabled, and must be enabled by the backend. 4556 */ 4557 lun->flags |= CTL_LUN_DISABLED; 4558 lun->backend = be_lun->be; 4559 be_lun->ctl_lun = lun; 4560 be_lun->lun_id = lun_number; 4561 atomic_add_int(&be_lun->be->num_luns, 1); 4562 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4563 lun->flags |= CTL_LUN_OFFLINE; 4564 4565 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4566 lun->flags |= CTL_LUN_STOPPED; 4567 4568 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4569 lun->flags |= CTL_LUN_INOPERABLE; 4570 4571 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4572 lun->flags |= CTL_LUN_PRIMARY_SC; 4573 4574 value = ctl_get_opt(&be_lun->options, "readonly"); 4575 if (value != NULL && strcmp(value, "on") == 0) 4576 lun->flags |= CTL_LUN_READONLY; 4577 4578 lun->ctl_softc = ctl_softc; 4579 TAILQ_INIT(&lun->ooa_queue); 4580 TAILQ_INIT(&lun->blocked_queue); 4581 STAILQ_INIT(&lun->error_list); 4582 ctl_tpc_lun_init(lun); 4583 4584 /* 4585 * Initialize the mode and log page index. 4586 */ 4587 ctl_init_page_index(lun); 4588 ctl_init_log_page_index(lun); 4589 4590 /* 4591 * Set the poweron UA for all initiators on this LUN only. 4592 */ 4593 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4594 lun->pending_ua[i] = CTL_UA_POWERON; 4595 4596 /* 4597 * Now, before we insert this lun on the lun list, set the lun 4598 * inventory changed UA for all other luns. 4599 */ 4600 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4601 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4602 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4603 } 4604 } 4605 4606 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4607 4608 ctl_softc->ctl_luns[lun_number] = lun; 4609 4610 ctl_softc->num_luns++; 4611 4612 /* Setup statistics gathering */ 4613 lun->stats.device_type = be_lun->lun_type; 4614 lun->stats.lun_number = lun_number; 4615 if (lun->stats.device_type == T_DIRECT) 4616 lun->stats.blocksize = be_lun->blocksize; 4617 else 4618 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4619 for (i = 0;i < CTL_MAX_PORTS;i++) 4620 lun->stats.ports[i].targ_port = i; 4621 4622 mtx_unlock(&ctl_softc->ctl_lock); 4623 4624 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4625 4626 /* 4627 * Run through each registered FETD and bring it online if it isn't 4628 * already. Enable the target ID if it hasn't been enabled, and 4629 * enable this particular LUN. 4630 */ 4631 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4632 int retval; 4633 4634 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4635 if (retval != 0) { 4636 printf("ctl_alloc_lun: FETD %s port %d returned error " 4637 "%d for lun_enable on target %ju lun %d\n", 4638 port->port_name, port->targ_port, retval, 4639 (uintmax_t)target_id.id, lun_number); 4640 } else 4641 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4642 } 4643 return (0); 4644 } 4645 4646 /* 4647 * Delete a LUN. 4648 * Assumptions: 4649 * - LUN has already been marked invalid and any pending I/O has been taken 4650 * care of. 4651 */ 4652 static int 4653 ctl_free_lun(struct ctl_lun *lun) 4654 { 4655 struct ctl_softc *softc; 4656 #if 0 4657 struct ctl_port *port; 4658 #endif 4659 struct ctl_lun *nlun; 4660 int i; 4661 4662 softc = lun->ctl_softc; 4663 4664 mtx_assert(&softc->ctl_lock, MA_OWNED); 4665 4666 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4667 4668 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4669 4670 softc->ctl_luns[lun->lun] = NULL; 4671 4672 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4673 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4674 4675 softc->num_luns--; 4676 4677 /* 4678 * XXX KDM this scheme only works for a single target/multiple LUN 4679 * setup. It needs to be revamped for a multiple target scheme. 4680 * 4681 * XXX KDM this results in port->lun_disable() getting called twice, 4682 * once when ctl_disable_lun() is called, and a second time here. 4683 * We really need to re-think the LUN disable semantics. There 4684 * should probably be several steps/levels to LUN removal: 4685 * - disable 4686 * - invalidate 4687 * - free 4688 * 4689 * Right now we only have a disable method when communicating to 4690 * the front end ports, at least for individual LUNs. 4691 */ 4692 #if 0 4693 STAILQ_FOREACH(port, &softc->port_list, links) { 4694 int retval; 4695 4696 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4697 lun->lun); 4698 if (retval != 0) { 4699 printf("ctl_free_lun: FETD %s port %d returned error " 4700 "%d for lun_disable on target %ju lun %jd\n", 4701 port->port_name, port->targ_port, retval, 4702 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4703 } 4704 4705 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4706 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4707 4708 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4709 if (retval != 0) { 4710 printf("ctl_free_lun: FETD %s port %d " 4711 "returned error %d for targ_disable on " 4712 "target %ju\n", port->port_name, 4713 port->targ_port, retval, 4714 (uintmax_t)lun->target.id); 4715 } else 4716 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4717 4718 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4719 continue; 4720 4721 #if 0 4722 port->port_offline(port->onoff_arg); 4723 port->status &= ~CTL_PORT_STATUS_ONLINE; 4724 #endif 4725 } 4726 } 4727 #endif 4728 4729 /* 4730 * Tell the backend to free resources, if this LUN has a backend. 4731 */ 4732 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4733 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4734 4735 ctl_tpc_lun_shutdown(lun); 4736 mtx_destroy(&lun->lun_lock); 4737 free(lun->lun_devid, M_CTL); 4738 free(lun->write_buffer, M_CTL); 4739 if (lun->flags & CTL_LUN_MALLOCED) 4740 free(lun, M_CTL); 4741 4742 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4743 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4744 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4745 } 4746 } 4747 4748 return (0); 4749 } 4750 4751 static void 4752 ctl_create_lun(struct ctl_be_lun *be_lun) 4753 { 4754 struct ctl_softc *ctl_softc; 4755 4756 ctl_softc = control_softc; 4757 4758 /* 4759 * ctl_alloc_lun() should handle all potential failure cases. 4760 */ 4761 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4762 } 4763 4764 int 4765 ctl_add_lun(struct ctl_be_lun *be_lun) 4766 { 4767 struct ctl_softc *ctl_softc = control_softc; 4768 4769 mtx_lock(&ctl_softc->ctl_lock); 4770 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4771 mtx_unlock(&ctl_softc->ctl_lock); 4772 wakeup(&ctl_softc->pending_lun_queue); 4773 4774 return (0); 4775 } 4776 4777 int 4778 ctl_enable_lun(struct ctl_be_lun *be_lun) 4779 { 4780 struct ctl_softc *ctl_softc; 4781 struct ctl_port *port, *nport; 4782 struct ctl_lun *lun; 4783 int retval; 4784 4785 ctl_softc = control_softc; 4786 4787 lun = (struct ctl_lun *)be_lun->ctl_lun; 4788 4789 mtx_lock(&ctl_softc->ctl_lock); 4790 mtx_lock(&lun->lun_lock); 4791 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4792 /* 4793 * eh? Why did we get called if the LUN is already 4794 * enabled? 4795 */ 4796 mtx_unlock(&lun->lun_lock); 4797 mtx_unlock(&ctl_softc->ctl_lock); 4798 return (0); 4799 } 4800 lun->flags &= ~CTL_LUN_DISABLED; 4801 mtx_unlock(&lun->lun_lock); 4802 4803 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4804 nport = STAILQ_NEXT(port, links); 4805 4806 /* 4807 * Drop the lock while we call the FETD's enable routine. 4808 * This can lead to a callback into CTL (at least in the 4809 * case of the internal initiator frontend. 4810 */ 4811 mtx_unlock(&ctl_softc->ctl_lock); 4812 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4813 mtx_lock(&ctl_softc->ctl_lock); 4814 if (retval != 0) { 4815 printf("%s: FETD %s port %d returned error " 4816 "%d for lun_enable on target %ju lun %jd\n", 4817 __func__, port->port_name, port->targ_port, retval, 4818 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4819 } 4820 #if 0 4821 else { 4822 /* NOTE: TODO: why does lun enable affect port status? */ 4823 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4824 } 4825 #endif 4826 } 4827 4828 mtx_unlock(&ctl_softc->ctl_lock); 4829 4830 return (0); 4831 } 4832 4833 int 4834 ctl_disable_lun(struct ctl_be_lun *be_lun) 4835 { 4836 struct ctl_softc *ctl_softc; 4837 struct ctl_port *port; 4838 struct ctl_lun *lun; 4839 int retval; 4840 4841 ctl_softc = control_softc; 4842 4843 lun = (struct ctl_lun *)be_lun->ctl_lun; 4844 4845 mtx_lock(&ctl_softc->ctl_lock); 4846 mtx_lock(&lun->lun_lock); 4847 if (lun->flags & CTL_LUN_DISABLED) { 4848 mtx_unlock(&lun->lun_lock); 4849 mtx_unlock(&ctl_softc->ctl_lock); 4850 return (0); 4851 } 4852 lun->flags |= CTL_LUN_DISABLED; 4853 mtx_unlock(&lun->lun_lock); 4854 4855 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4856 mtx_unlock(&ctl_softc->ctl_lock); 4857 /* 4858 * Drop the lock before we call the frontend's disable 4859 * routine, to avoid lock order reversals. 4860 * 4861 * XXX KDM what happens if the frontend list changes while 4862 * we're traversing it? It's unlikely, but should be handled. 4863 */ 4864 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4865 lun->lun); 4866 mtx_lock(&ctl_softc->ctl_lock); 4867 if (retval != 0) { 4868 printf("ctl_alloc_lun: FETD %s port %d returned error " 4869 "%d for lun_disable on target %ju lun %jd\n", 4870 port->port_name, port->targ_port, retval, 4871 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4872 } 4873 } 4874 4875 mtx_unlock(&ctl_softc->ctl_lock); 4876 4877 return (0); 4878 } 4879 4880 int 4881 ctl_start_lun(struct ctl_be_lun *be_lun) 4882 { 4883 struct ctl_softc *ctl_softc; 4884 struct ctl_lun *lun; 4885 4886 ctl_softc = control_softc; 4887 4888 lun = (struct ctl_lun *)be_lun->ctl_lun; 4889 4890 mtx_lock(&lun->lun_lock); 4891 lun->flags &= ~CTL_LUN_STOPPED; 4892 mtx_unlock(&lun->lun_lock); 4893 4894 return (0); 4895 } 4896 4897 int 4898 ctl_stop_lun(struct ctl_be_lun *be_lun) 4899 { 4900 struct ctl_softc *ctl_softc; 4901 struct ctl_lun *lun; 4902 4903 ctl_softc = control_softc; 4904 4905 lun = (struct ctl_lun *)be_lun->ctl_lun; 4906 4907 mtx_lock(&lun->lun_lock); 4908 lun->flags |= CTL_LUN_STOPPED; 4909 mtx_unlock(&lun->lun_lock); 4910 4911 return (0); 4912 } 4913 4914 int 4915 ctl_lun_offline(struct ctl_be_lun *be_lun) 4916 { 4917 struct ctl_softc *ctl_softc; 4918 struct ctl_lun *lun; 4919 4920 ctl_softc = control_softc; 4921 4922 lun = (struct ctl_lun *)be_lun->ctl_lun; 4923 4924 mtx_lock(&lun->lun_lock); 4925 lun->flags |= CTL_LUN_OFFLINE; 4926 mtx_unlock(&lun->lun_lock); 4927 4928 return (0); 4929 } 4930 4931 int 4932 ctl_lun_online(struct ctl_be_lun *be_lun) 4933 { 4934 struct ctl_softc *ctl_softc; 4935 struct ctl_lun *lun; 4936 4937 ctl_softc = control_softc; 4938 4939 lun = (struct ctl_lun *)be_lun->ctl_lun; 4940 4941 mtx_lock(&lun->lun_lock); 4942 lun->flags &= ~CTL_LUN_OFFLINE; 4943 mtx_unlock(&lun->lun_lock); 4944 4945 return (0); 4946 } 4947 4948 int 4949 ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4950 { 4951 struct ctl_softc *ctl_softc; 4952 struct ctl_lun *lun; 4953 4954 ctl_softc = control_softc; 4955 4956 lun = (struct ctl_lun *)be_lun->ctl_lun; 4957 4958 mtx_lock(&lun->lun_lock); 4959 4960 /* 4961 * The LUN needs to be disabled before it can be marked invalid. 4962 */ 4963 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4964 mtx_unlock(&lun->lun_lock); 4965 return (-1); 4966 } 4967 /* 4968 * Mark the LUN invalid. 4969 */ 4970 lun->flags |= CTL_LUN_INVALID; 4971 4972 /* 4973 * If there is nothing in the OOA queue, go ahead and free the LUN. 4974 * If we have something in the OOA queue, we'll free it when the 4975 * last I/O completes. 4976 */ 4977 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4978 mtx_unlock(&lun->lun_lock); 4979 mtx_lock(&ctl_softc->ctl_lock); 4980 ctl_free_lun(lun); 4981 mtx_unlock(&ctl_softc->ctl_lock); 4982 } else 4983 mtx_unlock(&lun->lun_lock); 4984 4985 return (0); 4986 } 4987 4988 int 4989 ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4990 { 4991 struct ctl_softc *ctl_softc; 4992 struct ctl_lun *lun; 4993 4994 ctl_softc = control_softc; 4995 lun = (struct ctl_lun *)be_lun->ctl_lun; 4996 4997 mtx_lock(&lun->lun_lock); 4998 lun->flags |= CTL_LUN_INOPERABLE; 4999 mtx_unlock(&lun->lun_lock); 5000 5001 return (0); 5002 } 5003 5004 int 5005 ctl_lun_operable(struct ctl_be_lun *be_lun) 5006 { 5007 struct ctl_softc *ctl_softc; 5008 struct ctl_lun *lun; 5009 5010 ctl_softc = control_softc; 5011 lun = (struct ctl_lun *)be_lun->ctl_lun; 5012 5013 mtx_lock(&lun->lun_lock); 5014 lun->flags &= ~CTL_LUN_INOPERABLE; 5015 mtx_unlock(&lun->lun_lock); 5016 5017 return (0); 5018 } 5019 5020 void 5021 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5022 { 5023 struct ctl_lun *lun; 5024 struct ctl_softc *softc; 5025 int i; 5026 5027 softc = control_softc; 5028 5029 lun = (struct ctl_lun *)be_lun->ctl_lun; 5030 5031 mtx_lock(&lun->lun_lock); 5032 5033 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5034 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5035 5036 mtx_unlock(&lun->lun_lock); 5037 } 5038 5039 /* 5040 * Backend "memory move is complete" callback for requests that never 5041 * make it down to say RAIDCore's configuration code. 5042 */ 5043 int 5044 ctl_config_move_done(union ctl_io *io) 5045 { 5046 int retval; 5047 5048 retval = CTL_RETVAL_COMPLETE; 5049 5050 5051 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5052 /* 5053 * XXX KDM this shouldn't happen, but what if it does? 5054 */ 5055 if (io->io_hdr.io_type != CTL_IO_SCSI) 5056 panic("I/O type isn't CTL_IO_SCSI!"); 5057 5058 if ((io->io_hdr.port_status == 0) 5059 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5060 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5061 io->io_hdr.status = CTL_SUCCESS; 5062 else if ((io->io_hdr.port_status != 0) 5063 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5064 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5065 /* 5066 * For hardware error sense keys, the sense key 5067 * specific value is defined to be a retry count, 5068 * but we use it to pass back an internal FETD 5069 * error code. XXX KDM Hopefully the FETD is only 5070 * using 16 bits for an error code, since that's 5071 * all the space we have in the sks field. 5072 */ 5073 ctl_set_internal_failure(&io->scsiio, 5074 /*sks_valid*/ 1, 5075 /*retry_count*/ 5076 io->io_hdr.port_status); 5077 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5078 free(io->scsiio.kern_data_ptr, M_CTL); 5079 ctl_done(io); 5080 goto bailout; 5081 } 5082 5083 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5084 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5085 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5086 /* 5087 * XXX KDM just assuming a single pointer here, and not a 5088 * S/G list. If we start using S/G lists for config data, 5089 * we'll need to know how to clean them up here as well. 5090 */ 5091 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5092 free(io->scsiio.kern_data_ptr, M_CTL); 5093 /* Hopefully the user has already set the status... */ 5094 ctl_done(io); 5095 } else { 5096 /* 5097 * XXX KDM now we need to continue data movement. Some 5098 * options: 5099 * - call ctl_scsiio() again? We don't do this for data 5100 * writes, because for those at least we know ahead of 5101 * time where the write will go and how long it is. For 5102 * config writes, though, that information is largely 5103 * contained within the write itself, thus we need to 5104 * parse out the data again. 5105 * 5106 * - Call some other function once the data is in? 5107 */ 5108 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5109 ctl_data_print(io); 5110 5111 /* 5112 * XXX KDM call ctl_scsiio() again for now, and check flag 5113 * bits to see whether we're allocated or not. 5114 */ 5115 retval = ctl_scsiio(&io->scsiio); 5116 } 5117 bailout: 5118 return (retval); 5119 } 5120 5121 /* 5122 * This gets called by a backend driver when it is done with a 5123 * data_submit method. 5124 */ 5125 void 5126 ctl_data_submit_done(union ctl_io *io) 5127 { 5128 /* 5129 * If the IO_CONT flag is set, we need to call the supplied 5130 * function to continue processing the I/O, instead of completing 5131 * the I/O just yet. 5132 * 5133 * If there is an error, though, we don't want to keep processing. 5134 * Instead, just send status back to the initiator. 5135 */ 5136 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5137 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5138 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5139 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5140 io->scsiio.io_cont(io); 5141 return; 5142 } 5143 ctl_done(io); 5144 } 5145 5146 /* 5147 * This gets called by a backend driver when it is done with a 5148 * configuration write. 5149 */ 5150 void 5151 ctl_config_write_done(union ctl_io *io) 5152 { 5153 uint8_t *buf; 5154 5155 /* 5156 * If the IO_CONT flag is set, we need to call the supplied 5157 * function to continue processing the I/O, instead of completing 5158 * the I/O just yet. 5159 * 5160 * If there is an error, though, we don't want to keep processing. 5161 * Instead, just send status back to the initiator. 5162 */ 5163 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5164 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5165 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5166 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5167 io->scsiio.io_cont(io); 5168 return; 5169 } 5170 /* 5171 * Since a configuration write can be done for commands that actually 5172 * have data allocated, like write buffer, and commands that have 5173 * no data, like start/stop unit, we need to check here. 5174 */ 5175 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5176 buf = io->scsiio.kern_data_ptr; 5177 else 5178 buf = NULL; 5179 ctl_done(io); 5180 if (buf) 5181 free(buf, M_CTL); 5182 } 5183 5184 /* 5185 * SCSI release command. 5186 */ 5187 int 5188 ctl_scsi_release(struct ctl_scsiio *ctsio) 5189 { 5190 int length, longid, thirdparty_id, resv_id; 5191 struct ctl_softc *ctl_softc; 5192 struct ctl_lun *lun; 5193 uint32_t residx; 5194 5195 length = 0; 5196 resv_id = 0; 5197 5198 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5199 5200 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5201 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5202 ctl_softc = control_softc; 5203 5204 switch (ctsio->cdb[0]) { 5205 case RELEASE_10: { 5206 struct scsi_release_10 *cdb; 5207 5208 cdb = (struct scsi_release_10 *)ctsio->cdb; 5209 5210 if (cdb->byte2 & SR10_LONGID) 5211 longid = 1; 5212 else 5213 thirdparty_id = cdb->thirdparty_id; 5214 5215 resv_id = cdb->resv_id; 5216 length = scsi_2btoul(cdb->length); 5217 break; 5218 } 5219 } 5220 5221 5222 /* 5223 * XXX KDM right now, we only support LUN reservation. We don't 5224 * support 3rd party reservations, or extent reservations, which 5225 * might actually need the parameter list. If we've gotten this 5226 * far, we've got a LUN reservation. Anything else got kicked out 5227 * above. So, according to SPC, ignore the length. 5228 */ 5229 length = 0; 5230 5231 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5232 && (length > 0)) { 5233 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5234 ctsio->kern_data_len = length; 5235 ctsio->kern_total_len = length; 5236 ctsio->kern_data_resid = 0; 5237 ctsio->kern_rel_offset = 0; 5238 ctsio->kern_sg_entries = 0; 5239 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5240 ctsio->be_move_done = ctl_config_move_done; 5241 ctl_datamove((union ctl_io *)ctsio); 5242 5243 return (CTL_RETVAL_COMPLETE); 5244 } 5245 5246 if (length > 0) 5247 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5248 5249 mtx_lock(&lun->lun_lock); 5250 5251 /* 5252 * According to SPC, it is not an error for an intiator to attempt 5253 * to release a reservation on a LUN that isn't reserved, or that 5254 * is reserved by another initiator. The reservation can only be 5255 * released, though, by the initiator who made it or by one of 5256 * several reset type events. 5257 */ 5258 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5259 lun->flags &= ~CTL_LUN_RESERVED; 5260 5261 mtx_unlock(&lun->lun_lock); 5262 5263 ctsio->scsi_status = SCSI_STATUS_OK; 5264 ctsio->io_hdr.status = CTL_SUCCESS; 5265 5266 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5267 free(ctsio->kern_data_ptr, M_CTL); 5268 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5269 } 5270 5271 ctl_done((union ctl_io *)ctsio); 5272 return (CTL_RETVAL_COMPLETE); 5273 } 5274 5275 int 5276 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5277 { 5278 int extent, thirdparty, longid; 5279 int resv_id, length; 5280 uint64_t thirdparty_id; 5281 struct ctl_softc *ctl_softc; 5282 struct ctl_lun *lun; 5283 uint32_t residx; 5284 5285 extent = 0; 5286 thirdparty = 0; 5287 longid = 0; 5288 resv_id = 0; 5289 length = 0; 5290 thirdparty_id = 0; 5291 5292 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5293 5294 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5295 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5296 ctl_softc = control_softc; 5297 5298 switch (ctsio->cdb[0]) { 5299 case RESERVE_10: { 5300 struct scsi_reserve_10 *cdb; 5301 5302 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5303 5304 if (cdb->byte2 & SR10_LONGID) 5305 longid = 1; 5306 else 5307 thirdparty_id = cdb->thirdparty_id; 5308 5309 resv_id = cdb->resv_id; 5310 length = scsi_2btoul(cdb->length); 5311 break; 5312 } 5313 } 5314 5315 /* 5316 * XXX KDM right now, we only support LUN reservation. We don't 5317 * support 3rd party reservations, or extent reservations, which 5318 * might actually need the parameter list. If we've gotten this 5319 * far, we've got a LUN reservation. Anything else got kicked out 5320 * above. So, according to SPC, ignore the length. 5321 */ 5322 length = 0; 5323 5324 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5325 && (length > 0)) { 5326 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5327 ctsio->kern_data_len = length; 5328 ctsio->kern_total_len = length; 5329 ctsio->kern_data_resid = 0; 5330 ctsio->kern_rel_offset = 0; 5331 ctsio->kern_sg_entries = 0; 5332 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5333 ctsio->be_move_done = ctl_config_move_done; 5334 ctl_datamove((union ctl_io *)ctsio); 5335 5336 return (CTL_RETVAL_COMPLETE); 5337 } 5338 5339 if (length > 0) 5340 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5341 5342 mtx_lock(&lun->lun_lock); 5343 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5344 ctl_set_reservation_conflict(ctsio); 5345 goto bailout; 5346 } 5347 5348 lun->flags |= CTL_LUN_RESERVED; 5349 lun->res_idx = residx; 5350 5351 ctsio->scsi_status = SCSI_STATUS_OK; 5352 ctsio->io_hdr.status = CTL_SUCCESS; 5353 5354 bailout: 5355 mtx_unlock(&lun->lun_lock); 5356 5357 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5358 free(ctsio->kern_data_ptr, M_CTL); 5359 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5360 } 5361 5362 ctl_done((union ctl_io *)ctsio); 5363 return (CTL_RETVAL_COMPLETE); 5364 } 5365 5366 int 5367 ctl_start_stop(struct ctl_scsiio *ctsio) 5368 { 5369 struct scsi_start_stop_unit *cdb; 5370 struct ctl_lun *lun; 5371 struct ctl_softc *ctl_softc; 5372 int retval; 5373 5374 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5375 5376 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5377 ctl_softc = control_softc; 5378 retval = 0; 5379 5380 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5381 5382 /* 5383 * XXX KDM 5384 * We don't support the immediate bit on a stop unit. In order to 5385 * do that, we would need to code up a way to know that a stop is 5386 * pending, and hold off any new commands until it completes, one 5387 * way or another. Then we could accept or reject those commands 5388 * depending on its status. We would almost need to do the reverse 5389 * of what we do below for an immediate start -- return the copy of 5390 * the ctl_io to the FETD with status to send to the host (and to 5391 * free the copy!) and then free the original I/O once the stop 5392 * actually completes. That way, the OOA queue mechanism can work 5393 * to block commands that shouldn't proceed. Another alternative 5394 * would be to put the copy in the queue in place of the original, 5395 * and return the original back to the caller. That could be 5396 * slightly safer.. 5397 */ 5398 if ((cdb->byte2 & SSS_IMMED) 5399 && ((cdb->how & SSS_START) == 0)) { 5400 ctl_set_invalid_field(ctsio, 5401 /*sks_valid*/ 1, 5402 /*command*/ 1, 5403 /*field*/ 1, 5404 /*bit_valid*/ 1, 5405 /*bit*/ 0); 5406 ctl_done((union ctl_io *)ctsio); 5407 return (CTL_RETVAL_COMPLETE); 5408 } 5409 5410 if ((lun->flags & CTL_LUN_PR_RESERVED) 5411 && ((cdb->how & SSS_START)==0)) { 5412 uint32_t residx; 5413 5414 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5415 if (lun->pr_keys[residx] == 0 5416 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5417 5418 ctl_set_reservation_conflict(ctsio); 5419 ctl_done((union ctl_io *)ctsio); 5420 return (CTL_RETVAL_COMPLETE); 5421 } 5422 } 5423 5424 /* 5425 * If there is no backend on this device, we can't start or stop 5426 * it. In theory we shouldn't get any start/stop commands in the 5427 * first place at this level if the LUN doesn't have a backend. 5428 * That should get stopped by the command decode code. 5429 */ 5430 if (lun->backend == NULL) { 5431 ctl_set_invalid_opcode(ctsio); 5432 ctl_done((union ctl_io *)ctsio); 5433 return (CTL_RETVAL_COMPLETE); 5434 } 5435 5436 /* 5437 * XXX KDM Copan-specific offline behavior. 5438 * Figure out a reasonable way to port this? 5439 */ 5440 #ifdef NEEDTOPORT 5441 mtx_lock(&lun->lun_lock); 5442 5443 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5444 && (lun->flags & CTL_LUN_OFFLINE)) { 5445 /* 5446 * If the LUN is offline, and the on/offline bit isn't set, 5447 * reject the start or stop. Otherwise, let it through. 5448 */ 5449 mtx_unlock(&lun->lun_lock); 5450 ctl_set_lun_not_ready(ctsio); 5451 ctl_done((union ctl_io *)ctsio); 5452 } else { 5453 mtx_unlock(&lun->lun_lock); 5454 #endif /* NEEDTOPORT */ 5455 /* 5456 * This could be a start or a stop when we're online, 5457 * or a stop/offline or start/online. A start or stop when 5458 * we're offline is covered in the case above. 5459 */ 5460 /* 5461 * In the non-immediate case, we send the request to 5462 * the backend and return status to the user when 5463 * it is done. 5464 * 5465 * In the immediate case, we allocate a new ctl_io 5466 * to hold a copy of the request, and send that to 5467 * the backend. We then set good status on the 5468 * user's request and return it immediately. 5469 */ 5470 if (cdb->byte2 & SSS_IMMED) { 5471 union ctl_io *new_io; 5472 5473 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5474 if (new_io == NULL) { 5475 ctl_set_busy(ctsio); 5476 ctl_done((union ctl_io *)ctsio); 5477 } else { 5478 ctl_copy_io((union ctl_io *)ctsio, 5479 new_io); 5480 retval = lun->backend->config_write(new_io); 5481 ctl_set_success(ctsio); 5482 ctl_done((union ctl_io *)ctsio); 5483 } 5484 } else { 5485 retval = lun->backend->config_write( 5486 (union ctl_io *)ctsio); 5487 } 5488 #ifdef NEEDTOPORT 5489 } 5490 #endif 5491 return (retval); 5492 } 5493 5494 /* 5495 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5496 * we don't really do anything with the LBA and length fields if the user 5497 * passes them in. Instead we'll just flush out the cache for the entire 5498 * LUN. 5499 */ 5500 int 5501 ctl_sync_cache(struct ctl_scsiio *ctsio) 5502 { 5503 struct ctl_lun *lun; 5504 struct ctl_softc *ctl_softc; 5505 uint64_t starting_lba; 5506 uint32_t block_count; 5507 int retval; 5508 5509 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5510 5511 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5512 ctl_softc = control_softc; 5513 retval = 0; 5514 5515 switch (ctsio->cdb[0]) { 5516 case SYNCHRONIZE_CACHE: { 5517 struct scsi_sync_cache *cdb; 5518 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5519 5520 starting_lba = scsi_4btoul(cdb->begin_lba); 5521 block_count = scsi_2btoul(cdb->lb_count); 5522 break; 5523 } 5524 case SYNCHRONIZE_CACHE_16: { 5525 struct scsi_sync_cache_16 *cdb; 5526 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5527 5528 starting_lba = scsi_8btou64(cdb->begin_lba); 5529 block_count = scsi_4btoul(cdb->lb_count); 5530 break; 5531 } 5532 default: 5533 ctl_set_invalid_opcode(ctsio); 5534 ctl_done((union ctl_io *)ctsio); 5535 goto bailout; 5536 break; /* NOTREACHED */ 5537 } 5538 5539 /* 5540 * We check the LBA and length, but don't do anything with them. 5541 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5542 * get flushed. This check will just help satisfy anyone who wants 5543 * to see an error for an out of range LBA. 5544 */ 5545 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5546 ctl_set_lba_out_of_range(ctsio); 5547 ctl_done((union ctl_io *)ctsio); 5548 goto bailout; 5549 } 5550 5551 /* 5552 * If this LUN has no backend, we can't flush the cache anyway. 5553 */ 5554 if (lun->backend == NULL) { 5555 ctl_set_invalid_opcode(ctsio); 5556 ctl_done((union ctl_io *)ctsio); 5557 goto bailout; 5558 } 5559 5560 /* 5561 * Check to see whether we're configured to send the SYNCHRONIZE 5562 * CACHE command directly to the back end. 5563 */ 5564 mtx_lock(&lun->lun_lock); 5565 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5566 && (++(lun->sync_count) >= lun->sync_interval)) { 5567 lun->sync_count = 0; 5568 mtx_unlock(&lun->lun_lock); 5569 retval = lun->backend->config_write((union ctl_io *)ctsio); 5570 } else { 5571 mtx_unlock(&lun->lun_lock); 5572 ctl_set_success(ctsio); 5573 ctl_done((union ctl_io *)ctsio); 5574 } 5575 5576 bailout: 5577 5578 return (retval); 5579 } 5580 5581 int 5582 ctl_format(struct ctl_scsiio *ctsio) 5583 { 5584 struct scsi_format *cdb; 5585 struct ctl_lun *lun; 5586 struct ctl_softc *ctl_softc; 5587 int length, defect_list_len; 5588 5589 CTL_DEBUG_PRINT(("ctl_format\n")); 5590 5591 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5592 ctl_softc = control_softc; 5593 5594 cdb = (struct scsi_format *)ctsio->cdb; 5595 5596 length = 0; 5597 if (cdb->byte2 & SF_FMTDATA) { 5598 if (cdb->byte2 & SF_LONGLIST) 5599 length = sizeof(struct scsi_format_header_long); 5600 else 5601 length = sizeof(struct scsi_format_header_short); 5602 } 5603 5604 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5605 && (length > 0)) { 5606 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5607 ctsio->kern_data_len = length; 5608 ctsio->kern_total_len = length; 5609 ctsio->kern_data_resid = 0; 5610 ctsio->kern_rel_offset = 0; 5611 ctsio->kern_sg_entries = 0; 5612 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5613 ctsio->be_move_done = ctl_config_move_done; 5614 ctl_datamove((union ctl_io *)ctsio); 5615 5616 return (CTL_RETVAL_COMPLETE); 5617 } 5618 5619 defect_list_len = 0; 5620 5621 if (cdb->byte2 & SF_FMTDATA) { 5622 if (cdb->byte2 & SF_LONGLIST) { 5623 struct scsi_format_header_long *header; 5624 5625 header = (struct scsi_format_header_long *) 5626 ctsio->kern_data_ptr; 5627 5628 defect_list_len = scsi_4btoul(header->defect_list_len); 5629 if (defect_list_len != 0) { 5630 ctl_set_invalid_field(ctsio, 5631 /*sks_valid*/ 1, 5632 /*command*/ 0, 5633 /*field*/ 2, 5634 /*bit_valid*/ 0, 5635 /*bit*/ 0); 5636 goto bailout; 5637 } 5638 } else { 5639 struct scsi_format_header_short *header; 5640 5641 header = (struct scsi_format_header_short *) 5642 ctsio->kern_data_ptr; 5643 5644 defect_list_len = scsi_2btoul(header->defect_list_len); 5645 if (defect_list_len != 0) { 5646 ctl_set_invalid_field(ctsio, 5647 /*sks_valid*/ 1, 5648 /*command*/ 0, 5649 /*field*/ 2, 5650 /*bit_valid*/ 0, 5651 /*bit*/ 0); 5652 goto bailout; 5653 } 5654 } 5655 } 5656 5657 /* 5658 * The format command will clear out the "Medium format corrupted" 5659 * status if set by the configuration code. That status is really 5660 * just a way to notify the host that we have lost the media, and 5661 * get them to issue a command that will basically make them think 5662 * they're blowing away the media. 5663 */ 5664 mtx_lock(&lun->lun_lock); 5665 lun->flags &= ~CTL_LUN_INOPERABLE; 5666 mtx_unlock(&lun->lun_lock); 5667 5668 ctsio->scsi_status = SCSI_STATUS_OK; 5669 ctsio->io_hdr.status = CTL_SUCCESS; 5670 bailout: 5671 5672 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5673 free(ctsio->kern_data_ptr, M_CTL); 5674 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5675 } 5676 5677 ctl_done((union ctl_io *)ctsio); 5678 return (CTL_RETVAL_COMPLETE); 5679 } 5680 5681 int 5682 ctl_read_buffer(struct ctl_scsiio *ctsio) 5683 { 5684 struct scsi_read_buffer *cdb; 5685 struct ctl_lun *lun; 5686 int buffer_offset, len; 5687 static uint8_t descr[4]; 5688 static uint8_t echo_descr[4] = { 0 }; 5689 5690 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5691 5692 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5693 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5694 5695 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5696 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5697 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5698 ctl_set_invalid_field(ctsio, 5699 /*sks_valid*/ 1, 5700 /*command*/ 1, 5701 /*field*/ 1, 5702 /*bit_valid*/ 1, 5703 /*bit*/ 4); 5704 ctl_done((union ctl_io *)ctsio); 5705 return (CTL_RETVAL_COMPLETE); 5706 } 5707 5708 len = scsi_3btoul(cdb->length); 5709 buffer_offset = scsi_3btoul(cdb->offset); 5710 5711 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5712 ctl_set_invalid_field(ctsio, 5713 /*sks_valid*/ 1, 5714 /*command*/ 1, 5715 /*field*/ 6, 5716 /*bit_valid*/ 0, 5717 /*bit*/ 0); 5718 ctl_done((union ctl_io *)ctsio); 5719 return (CTL_RETVAL_COMPLETE); 5720 } 5721 5722 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5723 descr[0] = 0; 5724 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5725 ctsio->kern_data_ptr = descr; 5726 len = min(len, sizeof(descr)); 5727 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5728 ctsio->kern_data_ptr = echo_descr; 5729 len = min(len, sizeof(echo_descr)); 5730 } else { 5731 if (lun->write_buffer == NULL) { 5732 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5733 M_CTL, M_WAITOK); 5734 } 5735 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5736 } 5737 ctsio->kern_data_len = len; 5738 ctsio->kern_total_len = len; 5739 ctsio->kern_data_resid = 0; 5740 ctsio->kern_rel_offset = 0; 5741 ctsio->kern_sg_entries = 0; 5742 ctsio->be_move_done = ctl_config_move_done; 5743 ctl_datamove((union ctl_io *)ctsio); 5744 5745 return (CTL_RETVAL_COMPLETE); 5746 } 5747 5748 int 5749 ctl_write_buffer(struct ctl_scsiio *ctsio) 5750 { 5751 struct scsi_write_buffer *cdb; 5752 struct ctl_lun *lun; 5753 int buffer_offset, len; 5754 5755 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5756 5757 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5758 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5759 5760 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5761 ctl_set_invalid_field(ctsio, 5762 /*sks_valid*/ 1, 5763 /*command*/ 1, 5764 /*field*/ 1, 5765 /*bit_valid*/ 1, 5766 /*bit*/ 4); 5767 ctl_done((union ctl_io *)ctsio); 5768 return (CTL_RETVAL_COMPLETE); 5769 } 5770 5771 len = scsi_3btoul(cdb->length); 5772 buffer_offset = scsi_3btoul(cdb->offset); 5773 5774 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5775 ctl_set_invalid_field(ctsio, 5776 /*sks_valid*/ 1, 5777 /*command*/ 1, 5778 /*field*/ 6, 5779 /*bit_valid*/ 0, 5780 /*bit*/ 0); 5781 ctl_done((union ctl_io *)ctsio); 5782 return (CTL_RETVAL_COMPLETE); 5783 } 5784 5785 /* 5786 * If we've got a kernel request that hasn't been malloced yet, 5787 * malloc it and tell the caller the data buffer is here. 5788 */ 5789 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5790 if (lun->write_buffer == NULL) { 5791 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5792 M_CTL, M_WAITOK); 5793 } 5794 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5795 ctsio->kern_data_len = len; 5796 ctsio->kern_total_len = len; 5797 ctsio->kern_data_resid = 0; 5798 ctsio->kern_rel_offset = 0; 5799 ctsio->kern_sg_entries = 0; 5800 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5801 ctsio->be_move_done = ctl_config_move_done; 5802 ctl_datamove((union ctl_io *)ctsio); 5803 5804 return (CTL_RETVAL_COMPLETE); 5805 } 5806 5807 ctl_done((union ctl_io *)ctsio); 5808 5809 return (CTL_RETVAL_COMPLETE); 5810 } 5811 5812 int 5813 ctl_write_same(struct ctl_scsiio *ctsio) 5814 { 5815 struct ctl_lun *lun; 5816 struct ctl_lba_len_flags *lbalen; 5817 uint64_t lba; 5818 uint32_t num_blocks; 5819 int len, retval; 5820 uint8_t byte2; 5821 5822 retval = CTL_RETVAL_COMPLETE; 5823 5824 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5825 5826 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5827 5828 switch (ctsio->cdb[0]) { 5829 case WRITE_SAME_10: { 5830 struct scsi_write_same_10 *cdb; 5831 5832 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5833 5834 lba = scsi_4btoul(cdb->addr); 5835 num_blocks = scsi_2btoul(cdb->length); 5836 byte2 = cdb->byte2; 5837 break; 5838 } 5839 case WRITE_SAME_16: { 5840 struct scsi_write_same_16 *cdb; 5841 5842 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5843 5844 lba = scsi_8btou64(cdb->addr); 5845 num_blocks = scsi_4btoul(cdb->length); 5846 byte2 = cdb->byte2; 5847 break; 5848 } 5849 default: 5850 /* 5851 * We got a command we don't support. This shouldn't 5852 * happen, commands should be filtered out above us. 5853 */ 5854 ctl_set_invalid_opcode(ctsio); 5855 ctl_done((union ctl_io *)ctsio); 5856 5857 return (CTL_RETVAL_COMPLETE); 5858 break; /* NOTREACHED */ 5859 } 5860 5861 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5862 if ((byte2 & SWS_UNMAP) == 0 && 5863 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5864 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5865 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5866 ctl_done((union ctl_io *)ctsio); 5867 return (CTL_RETVAL_COMPLETE); 5868 } 5869 5870 /* 5871 * The first check is to make sure we're in bounds, the second 5872 * check is to catch wrap-around problems. If the lba + num blocks 5873 * is less than the lba, then we've wrapped around and the block 5874 * range is invalid anyway. 5875 */ 5876 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5877 || ((lba + num_blocks) < lba)) { 5878 ctl_set_lba_out_of_range(ctsio); 5879 ctl_done((union ctl_io *)ctsio); 5880 return (CTL_RETVAL_COMPLETE); 5881 } 5882 5883 /* Zero number of blocks means "to the last logical block" */ 5884 if (num_blocks == 0) { 5885 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5886 ctl_set_invalid_field(ctsio, 5887 /*sks_valid*/ 0, 5888 /*command*/ 1, 5889 /*field*/ 0, 5890 /*bit_valid*/ 0, 5891 /*bit*/ 0); 5892 ctl_done((union ctl_io *)ctsio); 5893 return (CTL_RETVAL_COMPLETE); 5894 } 5895 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5896 } 5897 5898 len = lun->be_lun->blocksize; 5899 5900 /* 5901 * If we've got a kernel request that hasn't been malloced yet, 5902 * malloc it and tell the caller the data buffer is here. 5903 */ 5904 if ((byte2 & SWS_NDOB) == 0 && 5905 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5906 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5907 ctsio->kern_data_len = len; 5908 ctsio->kern_total_len = len; 5909 ctsio->kern_data_resid = 0; 5910 ctsio->kern_rel_offset = 0; 5911 ctsio->kern_sg_entries = 0; 5912 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5913 ctsio->be_move_done = ctl_config_move_done; 5914 ctl_datamove((union ctl_io *)ctsio); 5915 5916 return (CTL_RETVAL_COMPLETE); 5917 } 5918 5919 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5920 lbalen->lba = lba; 5921 lbalen->len = num_blocks; 5922 lbalen->flags = byte2; 5923 retval = lun->backend->config_write((union ctl_io *)ctsio); 5924 5925 return (retval); 5926 } 5927 5928 int 5929 ctl_unmap(struct ctl_scsiio *ctsio) 5930 { 5931 struct ctl_lun *lun; 5932 struct scsi_unmap *cdb; 5933 struct ctl_ptr_len_flags *ptrlen; 5934 struct scsi_unmap_header *hdr; 5935 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5936 uint64_t lba; 5937 uint32_t num_blocks; 5938 int len, retval; 5939 uint8_t byte2; 5940 5941 retval = CTL_RETVAL_COMPLETE; 5942 5943 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5944 5945 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5946 cdb = (struct scsi_unmap *)ctsio->cdb; 5947 5948 len = scsi_2btoul(cdb->length); 5949 byte2 = cdb->byte2; 5950 5951 /* 5952 * If we've got a kernel request that hasn't been malloced yet, 5953 * malloc it and tell the caller the data buffer is here. 5954 */ 5955 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5956 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5957 ctsio->kern_data_len = len; 5958 ctsio->kern_total_len = len; 5959 ctsio->kern_data_resid = 0; 5960 ctsio->kern_rel_offset = 0; 5961 ctsio->kern_sg_entries = 0; 5962 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5963 ctsio->be_move_done = ctl_config_move_done; 5964 ctl_datamove((union ctl_io *)ctsio); 5965 5966 return (CTL_RETVAL_COMPLETE); 5967 } 5968 5969 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5970 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5971 if (len < sizeof (*hdr) || 5972 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5973 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5974 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5975 ctl_set_invalid_field(ctsio, 5976 /*sks_valid*/ 0, 5977 /*command*/ 0, 5978 /*field*/ 0, 5979 /*bit_valid*/ 0, 5980 /*bit*/ 0); 5981 ctl_done((union ctl_io *)ctsio); 5982 return (CTL_RETVAL_COMPLETE); 5983 } 5984 len = scsi_2btoul(hdr->desc_length); 5985 buf = (struct scsi_unmap_desc *)(hdr + 1); 5986 end = buf + len / sizeof(*buf); 5987 5988 endnz = buf; 5989 for (range = buf; range < end; range++) { 5990 lba = scsi_8btou64(range->lba); 5991 num_blocks = scsi_4btoul(range->length); 5992 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5993 || ((lba + num_blocks) < lba)) { 5994 ctl_set_lba_out_of_range(ctsio); 5995 ctl_done((union ctl_io *)ctsio); 5996 return (CTL_RETVAL_COMPLETE); 5997 } 5998 if (num_blocks != 0) 5999 endnz = range + 1; 6000 } 6001 6002 /* 6003 * Block backend can not handle zero last range. 6004 * Filter it out and return if there is nothing left. 6005 */ 6006 len = (uint8_t *)endnz - (uint8_t *)buf; 6007 if (len == 0) { 6008 ctl_set_success(ctsio); 6009 ctl_done((union ctl_io *)ctsio); 6010 return (CTL_RETVAL_COMPLETE); 6011 } 6012 6013 mtx_lock(&lun->lun_lock); 6014 ptrlen = (struct ctl_ptr_len_flags *) 6015 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6016 ptrlen->ptr = (void *)buf; 6017 ptrlen->len = len; 6018 ptrlen->flags = byte2; 6019 ctl_check_blocked(lun); 6020 mtx_unlock(&lun->lun_lock); 6021 6022 retval = lun->backend->config_write((union ctl_io *)ctsio); 6023 return (retval); 6024 } 6025 6026 /* 6027 * Note that this function currently doesn't actually do anything inside 6028 * CTL to enforce things if the DQue bit is turned on. 6029 * 6030 * Also note that this function can't be used in the default case, because 6031 * the DQue bit isn't set in the changeable mask for the control mode page 6032 * anyway. This is just here as an example for how to implement a page 6033 * handler, and a placeholder in case we want to allow the user to turn 6034 * tagged queueing on and off. 6035 * 6036 * The D_SENSE bit handling is functional, however, and will turn 6037 * descriptor sense on and off for a given LUN. 6038 */ 6039 int 6040 ctl_control_page_handler(struct ctl_scsiio *ctsio, 6041 struct ctl_page_index *page_index, uint8_t *page_ptr) 6042 { 6043 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6044 struct ctl_lun *lun; 6045 struct ctl_softc *softc; 6046 int set_ua; 6047 uint32_t initidx; 6048 6049 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6050 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6051 set_ua = 0; 6052 6053 user_cp = (struct scsi_control_page *)page_ptr; 6054 current_cp = (struct scsi_control_page *) 6055 (page_index->page_data + (page_index->page_len * 6056 CTL_PAGE_CURRENT)); 6057 saved_cp = (struct scsi_control_page *) 6058 (page_index->page_data + (page_index->page_len * 6059 CTL_PAGE_SAVED)); 6060 6061 softc = control_softc; 6062 6063 mtx_lock(&lun->lun_lock); 6064 if (((current_cp->rlec & SCP_DSENSE) == 0) 6065 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6066 /* 6067 * Descriptor sense is currently turned off and the user 6068 * wants to turn it on. 6069 */ 6070 current_cp->rlec |= SCP_DSENSE; 6071 saved_cp->rlec |= SCP_DSENSE; 6072 lun->flags |= CTL_LUN_SENSE_DESC; 6073 set_ua = 1; 6074 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6075 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6076 /* 6077 * Descriptor sense is currently turned on, and the user 6078 * wants to turn it off. 6079 */ 6080 current_cp->rlec &= ~SCP_DSENSE; 6081 saved_cp->rlec &= ~SCP_DSENSE; 6082 lun->flags &= ~CTL_LUN_SENSE_DESC; 6083 set_ua = 1; 6084 } 6085 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6086 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6087 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6088 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6089 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6090 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6091 set_ua = 1; 6092 } 6093 if ((current_cp->eca_and_aen & SCP_SWP) != 6094 (user_cp->eca_and_aen & SCP_SWP)) { 6095 current_cp->eca_and_aen &= ~SCP_SWP; 6096 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6097 saved_cp->eca_and_aen &= ~SCP_SWP; 6098 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6099 set_ua = 1; 6100 } 6101 if (set_ua != 0) { 6102 int i; 6103 /* 6104 * Let other initiators know that the mode 6105 * parameters for this LUN have changed. 6106 */ 6107 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6108 if (i == initidx) 6109 continue; 6110 6111 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6112 } 6113 } 6114 mtx_unlock(&lun->lun_lock); 6115 6116 return (0); 6117 } 6118 6119 int 6120 ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6121 struct ctl_page_index *page_index, uint8_t *page_ptr) 6122 { 6123 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6124 struct ctl_lun *lun; 6125 int set_ua; 6126 uint32_t initidx; 6127 6128 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6129 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6130 set_ua = 0; 6131 6132 user_cp = (struct scsi_caching_page *)page_ptr; 6133 current_cp = (struct scsi_caching_page *) 6134 (page_index->page_data + (page_index->page_len * 6135 CTL_PAGE_CURRENT)); 6136 saved_cp = (struct scsi_caching_page *) 6137 (page_index->page_data + (page_index->page_len * 6138 CTL_PAGE_SAVED)); 6139 6140 mtx_lock(&lun->lun_lock); 6141 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6142 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6143 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6144 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6145 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6146 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6147 set_ua = 1; 6148 } 6149 if (set_ua != 0) { 6150 int i; 6151 /* 6152 * Let other initiators know that the mode 6153 * parameters for this LUN have changed. 6154 */ 6155 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6156 if (i == initidx) 6157 continue; 6158 6159 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6160 } 6161 } 6162 mtx_unlock(&lun->lun_lock); 6163 6164 return (0); 6165 } 6166 6167 int 6168 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6169 struct ctl_page_index *page_index, 6170 uint8_t *page_ptr) 6171 { 6172 uint8_t *c; 6173 int i; 6174 6175 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6176 ctl_time_io_secs = 6177 (c[0] << 8) | 6178 (c[1] << 0) | 6179 0; 6180 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6181 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6182 printf("page data:"); 6183 for (i=0; i<8; i++) 6184 printf(" %.2x",page_ptr[i]); 6185 printf("\n"); 6186 return (0); 6187 } 6188 6189 int 6190 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6191 struct ctl_page_index *page_index, 6192 int pc) 6193 { 6194 struct copan_debugconf_subpage *page; 6195 6196 page = (struct copan_debugconf_subpage *)page_index->page_data + 6197 (page_index->page_len * pc); 6198 6199 switch (pc) { 6200 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6201 case SMS_PAGE_CTRL_DEFAULT >> 6: 6202 case SMS_PAGE_CTRL_SAVED >> 6: 6203 /* 6204 * We don't update the changable or default bits for this page. 6205 */ 6206 break; 6207 case SMS_PAGE_CTRL_CURRENT >> 6: 6208 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6209 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6210 break; 6211 default: 6212 #ifdef NEEDTOPORT 6213 EPRINT(0, "Invalid PC %d!!", pc); 6214 #endif /* NEEDTOPORT */ 6215 break; 6216 } 6217 return (0); 6218 } 6219 6220 6221 static int 6222 ctl_do_mode_select(union ctl_io *io) 6223 { 6224 struct scsi_mode_page_header *page_header; 6225 struct ctl_page_index *page_index; 6226 struct ctl_scsiio *ctsio; 6227 int control_dev, page_len; 6228 int page_len_offset, page_len_size; 6229 union ctl_modepage_info *modepage_info; 6230 struct ctl_lun *lun; 6231 int *len_left, *len_used; 6232 int retval, i; 6233 6234 ctsio = &io->scsiio; 6235 page_index = NULL; 6236 page_len = 0; 6237 retval = CTL_RETVAL_COMPLETE; 6238 6239 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6240 6241 if (lun->be_lun->lun_type != T_DIRECT) 6242 control_dev = 1; 6243 else 6244 control_dev = 0; 6245 6246 modepage_info = (union ctl_modepage_info *) 6247 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6248 len_left = &modepage_info->header.len_left; 6249 len_used = &modepage_info->header.len_used; 6250 6251 do_next_page: 6252 6253 page_header = (struct scsi_mode_page_header *) 6254 (ctsio->kern_data_ptr + *len_used); 6255 6256 if (*len_left == 0) { 6257 free(ctsio->kern_data_ptr, M_CTL); 6258 ctl_set_success(ctsio); 6259 ctl_done((union ctl_io *)ctsio); 6260 return (CTL_RETVAL_COMPLETE); 6261 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6262 6263 free(ctsio->kern_data_ptr, M_CTL); 6264 ctl_set_param_len_error(ctsio); 6265 ctl_done((union ctl_io *)ctsio); 6266 return (CTL_RETVAL_COMPLETE); 6267 6268 } else if ((page_header->page_code & SMPH_SPF) 6269 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6270 6271 free(ctsio->kern_data_ptr, M_CTL); 6272 ctl_set_param_len_error(ctsio); 6273 ctl_done((union ctl_io *)ctsio); 6274 return (CTL_RETVAL_COMPLETE); 6275 } 6276 6277 6278 /* 6279 * XXX KDM should we do something with the block descriptor? 6280 */ 6281 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6282 6283 if ((control_dev != 0) 6284 && (lun->mode_pages.index[i].page_flags & 6285 CTL_PAGE_FLAG_DISK_ONLY)) 6286 continue; 6287 6288 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6289 (page_header->page_code & SMPH_PC_MASK)) 6290 continue; 6291 6292 /* 6293 * If neither page has a subpage code, then we've got a 6294 * match. 6295 */ 6296 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6297 && ((page_header->page_code & SMPH_SPF) == 0)) { 6298 page_index = &lun->mode_pages.index[i]; 6299 page_len = page_header->page_length; 6300 break; 6301 } 6302 6303 /* 6304 * If both pages have subpages, then the subpage numbers 6305 * have to match. 6306 */ 6307 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6308 && (page_header->page_code & SMPH_SPF)) { 6309 struct scsi_mode_page_header_sp *sph; 6310 6311 sph = (struct scsi_mode_page_header_sp *)page_header; 6312 6313 if (lun->mode_pages.index[i].subpage == 6314 sph->subpage) { 6315 page_index = &lun->mode_pages.index[i]; 6316 page_len = scsi_2btoul(sph->page_length); 6317 break; 6318 } 6319 } 6320 } 6321 6322 /* 6323 * If we couldn't find the page, or if we don't have a mode select 6324 * handler for it, send back an error to the user. 6325 */ 6326 if ((page_index == NULL) 6327 || (page_index->select_handler == NULL)) { 6328 ctl_set_invalid_field(ctsio, 6329 /*sks_valid*/ 1, 6330 /*command*/ 0, 6331 /*field*/ *len_used, 6332 /*bit_valid*/ 0, 6333 /*bit*/ 0); 6334 free(ctsio->kern_data_ptr, M_CTL); 6335 ctl_done((union ctl_io *)ctsio); 6336 return (CTL_RETVAL_COMPLETE); 6337 } 6338 6339 if (page_index->page_code & SMPH_SPF) { 6340 page_len_offset = 2; 6341 page_len_size = 2; 6342 } else { 6343 page_len_size = 1; 6344 page_len_offset = 1; 6345 } 6346 6347 /* 6348 * If the length the initiator gives us isn't the one we specify in 6349 * the mode page header, or if they didn't specify enough data in 6350 * the CDB to avoid truncating this page, kick out the request. 6351 */ 6352 if ((page_len != (page_index->page_len - page_len_offset - 6353 page_len_size)) 6354 || (*len_left < page_index->page_len)) { 6355 6356 6357 ctl_set_invalid_field(ctsio, 6358 /*sks_valid*/ 1, 6359 /*command*/ 0, 6360 /*field*/ *len_used + page_len_offset, 6361 /*bit_valid*/ 0, 6362 /*bit*/ 0); 6363 free(ctsio->kern_data_ptr, M_CTL); 6364 ctl_done((union ctl_io *)ctsio); 6365 return (CTL_RETVAL_COMPLETE); 6366 } 6367 6368 /* 6369 * Run through the mode page, checking to make sure that the bits 6370 * the user changed are actually legal for him to change. 6371 */ 6372 for (i = 0; i < page_index->page_len; i++) { 6373 uint8_t *user_byte, *change_mask, *current_byte; 6374 int bad_bit; 6375 int j; 6376 6377 user_byte = (uint8_t *)page_header + i; 6378 change_mask = page_index->page_data + 6379 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6380 current_byte = page_index->page_data + 6381 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6382 6383 /* 6384 * Check to see whether the user set any bits in this byte 6385 * that he is not allowed to set. 6386 */ 6387 if ((*user_byte & ~(*change_mask)) == 6388 (*current_byte & ~(*change_mask))) 6389 continue; 6390 6391 /* 6392 * Go through bit by bit to determine which one is illegal. 6393 */ 6394 bad_bit = 0; 6395 for (j = 7; j >= 0; j--) { 6396 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6397 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6398 bad_bit = i; 6399 break; 6400 } 6401 } 6402 ctl_set_invalid_field(ctsio, 6403 /*sks_valid*/ 1, 6404 /*command*/ 0, 6405 /*field*/ *len_used + i, 6406 /*bit_valid*/ 1, 6407 /*bit*/ bad_bit); 6408 free(ctsio->kern_data_ptr, M_CTL); 6409 ctl_done((union ctl_io *)ctsio); 6410 return (CTL_RETVAL_COMPLETE); 6411 } 6412 6413 /* 6414 * Decrement these before we call the page handler, since we may 6415 * end up getting called back one way or another before the handler 6416 * returns to this context. 6417 */ 6418 *len_left -= page_index->page_len; 6419 *len_used += page_index->page_len; 6420 6421 retval = page_index->select_handler(ctsio, page_index, 6422 (uint8_t *)page_header); 6423 6424 /* 6425 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6426 * wait until this queued command completes to finish processing 6427 * the mode page. If it returns anything other than 6428 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6429 * already set the sense information, freed the data pointer, and 6430 * completed the io for us. 6431 */ 6432 if (retval != CTL_RETVAL_COMPLETE) 6433 goto bailout_no_done; 6434 6435 /* 6436 * If the initiator sent us more than one page, parse the next one. 6437 */ 6438 if (*len_left > 0) 6439 goto do_next_page; 6440 6441 ctl_set_success(ctsio); 6442 free(ctsio->kern_data_ptr, M_CTL); 6443 ctl_done((union ctl_io *)ctsio); 6444 6445 bailout_no_done: 6446 6447 return (CTL_RETVAL_COMPLETE); 6448 6449 } 6450 6451 int 6452 ctl_mode_select(struct ctl_scsiio *ctsio) 6453 { 6454 int param_len, pf, sp; 6455 int header_size, bd_len; 6456 int len_left, len_used; 6457 struct ctl_page_index *page_index; 6458 struct ctl_lun *lun; 6459 int control_dev, page_len; 6460 union ctl_modepage_info *modepage_info; 6461 int retval; 6462 6463 pf = 0; 6464 sp = 0; 6465 page_len = 0; 6466 len_used = 0; 6467 len_left = 0; 6468 retval = 0; 6469 bd_len = 0; 6470 page_index = NULL; 6471 6472 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6473 6474 if (lun->be_lun->lun_type != T_DIRECT) 6475 control_dev = 1; 6476 else 6477 control_dev = 0; 6478 6479 switch (ctsio->cdb[0]) { 6480 case MODE_SELECT_6: { 6481 struct scsi_mode_select_6 *cdb; 6482 6483 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6484 6485 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6486 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6487 6488 param_len = cdb->length; 6489 header_size = sizeof(struct scsi_mode_header_6); 6490 break; 6491 } 6492 case MODE_SELECT_10: { 6493 struct scsi_mode_select_10 *cdb; 6494 6495 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6496 6497 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6498 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6499 6500 param_len = scsi_2btoul(cdb->length); 6501 header_size = sizeof(struct scsi_mode_header_10); 6502 break; 6503 } 6504 default: 6505 ctl_set_invalid_opcode(ctsio); 6506 ctl_done((union ctl_io *)ctsio); 6507 return (CTL_RETVAL_COMPLETE); 6508 break; /* NOTREACHED */ 6509 } 6510 6511 /* 6512 * From SPC-3: 6513 * "A parameter list length of zero indicates that the Data-Out Buffer 6514 * shall be empty. This condition shall not be considered as an error." 6515 */ 6516 if (param_len == 0) { 6517 ctl_set_success(ctsio); 6518 ctl_done((union ctl_io *)ctsio); 6519 return (CTL_RETVAL_COMPLETE); 6520 } 6521 6522 /* 6523 * Since we'll hit this the first time through, prior to 6524 * allocation, we don't need to free a data buffer here. 6525 */ 6526 if (param_len < header_size) { 6527 ctl_set_param_len_error(ctsio); 6528 ctl_done((union ctl_io *)ctsio); 6529 return (CTL_RETVAL_COMPLETE); 6530 } 6531 6532 /* 6533 * Allocate the data buffer and grab the user's data. In theory, 6534 * we shouldn't have to sanity check the parameter list length here 6535 * because the maximum size is 64K. We should be able to malloc 6536 * that much without too many problems. 6537 */ 6538 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6539 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6540 ctsio->kern_data_len = param_len; 6541 ctsio->kern_total_len = param_len; 6542 ctsio->kern_data_resid = 0; 6543 ctsio->kern_rel_offset = 0; 6544 ctsio->kern_sg_entries = 0; 6545 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6546 ctsio->be_move_done = ctl_config_move_done; 6547 ctl_datamove((union ctl_io *)ctsio); 6548 6549 return (CTL_RETVAL_COMPLETE); 6550 } 6551 6552 switch (ctsio->cdb[0]) { 6553 case MODE_SELECT_6: { 6554 struct scsi_mode_header_6 *mh6; 6555 6556 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6557 bd_len = mh6->blk_desc_len; 6558 break; 6559 } 6560 case MODE_SELECT_10: { 6561 struct scsi_mode_header_10 *mh10; 6562 6563 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6564 bd_len = scsi_2btoul(mh10->blk_desc_len); 6565 break; 6566 } 6567 default: 6568 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6569 break; 6570 } 6571 6572 if (param_len < (header_size + bd_len)) { 6573 free(ctsio->kern_data_ptr, M_CTL); 6574 ctl_set_param_len_error(ctsio); 6575 ctl_done((union ctl_io *)ctsio); 6576 return (CTL_RETVAL_COMPLETE); 6577 } 6578 6579 /* 6580 * Set the IO_CONT flag, so that if this I/O gets passed to 6581 * ctl_config_write_done(), it'll get passed back to 6582 * ctl_do_mode_select() for further processing, or completion if 6583 * we're all done. 6584 */ 6585 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6586 ctsio->io_cont = ctl_do_mode_select; 6587 6588 modepage_info = (union ctl_modepage_info *) 6589 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6590 6591 memset(modepage_info, 0, sizeof(*modepage_info)); 6592 6593 len_left = param_len - header_size - bd_len; 6594 len_used = header_size + bd_len; 6595 6596 modepage_info->header.len_left = len_left; 6597 modepage_info->header.len_used = len_used; 6598 6599 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6600 } 6601 6602 int 6603 ctl_mode_sense(struct ctl_scsiio *ctsio) 6604 { 6605 struct ctl_lun *lun; 6606 int pc, page_code, dbd, llba, subpage; 6607 int alloc_len, page_len, header_len, total_len; 6608 struct scsi_mode_block_descr *block_desc; 6609 struct ctl_page_index *page_index; 6610 int control_dev; 6611 6612 dbd = 0; 6613 llba = 0; 6614 block_desc = NULL; 6615 page_index = NULL; 6616 6617 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6618 6619 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6620 6621 if (lun->be_lun->lun_type != T_DIRECT) 6622 control_dev = 1; 6623 else 6624 control_dev = 0; 6625 6626 switch (ctsio->cdb[0]) { 6627 case MODE_SENSE_6: { 6628 struct scsi_mode_sense_6 *cdb; 6629 6630 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6631 6632 header_len = sizeof(struct scsi_mode_hdr_6); 6633 if (cdb->byte2 & SMS_DBD) 6634 dbd = 1; 6635 else 6636 header_len += sizeof(struct scsi_mode_block_descr); 6637 6638 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6639 page_code = cdb->page & SMS_PAGE_CODE; 6640 subpage = cdb->subpage; 6641 alloc_len = cdb->length; 6642 break; 6643 } 6644 case MODE_SENSE_10: { 6645 struct scsi_mode_sense_10 *cdb; 6646 6647 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6648 6649 header_len = sizeof(struct scsi_mode_hdr_10); 6650 6651 if (cdb->byte2 & SMS_DBD) 6652 dbd = 1; 6653 else 6654 header_len += sizeof(struct scsi_mode_block_descr); 6655 if (cdb->byte2 & SMS10_LLBAA) 6656 llba = 1; 6657 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6658 page_code = cdb->page & SMS_PAGE_CODE; 6659 subpage = cdb->subpage; 6660 alloc_len = scsi_2btoul(cdb->length); 6661 break; 6662 } 6663 default: 6664 ctl_set_invalid_opcode(ctsio); 6665 ctl_done((union ctl_io *)ctsio); 6666 return (CTL_RETVAL_COMPLETE); 6667 break; /* NOTREACHED */ 6668 } 6669 6670 /* 6671 * We have to make a first pass through to calculate the size of 6672 * the pages that match the user's query. Then we allocate enough 6673 * memory to hold it, and actually copy the data into the buffer. 6674 */ 6675 switch (page_code) { 6676 case SMS_ALL_PAGES_PAGE: { 6677 int i; 6678 6679 page_len = 0; 6680 6681 /* 6682 * At the moment, values other than 0 and 0xff here are 6683 * reserved according to SPC-3. 6684 */ 6685 if ((subpage != SMS_SUBPAGE_PAGE_0) 6686 && (subpage != SMS_SUBPAGE_ALL)) { 6687 ctl_set_invalid_field(ctsio, 6688 /*sks_valid*/ 1, 6689 /*command*/ 1, 6690 /*field*/ 3, 6691 /*bit_valid*/ 0, 6692 /*bit*/ 0); 6693 ctl_done((union ctl_io *)ctsio); 6694 return (CTL_RETVAL_COMPLETE); 6695 } 6696 6697 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6698 if ((control_dev != 0) 6699 && (lun->mode_pages.index[i].page_flags & 6700 CTL_PAGE_FLAG_DISK_ONLY)) 6701 continue; 6702 6703 /* 6704 * We don't use this subpage if the user didn't 6705 * request all subpages. 6706 */ 6707 if ((lun->mode_pages.index[i].subpage != 0) 6708 && (subpage == SMS_SUBPAGE_PAGE_0)) 6709 continue; 6710 6711 #if 0 6712 printf("found page %#x len %d\n", 6713 lun->mode_pages.index[i].page_code & 6714 SMPH_PC_MASK, 6715 lun->mode_pages.index[i].page_len); 6716 #endif 6717 page_len += lun->mode_pages.index[i].page_len; 6718 } 6719 break; 6720 } 6721 default: { 6722 int i; 6723 6724 page_len = 0; 6725 6726 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6727 /* Look for the right page code */ 6728 if ((lun->mode_pages.index[i].page_code & 6729 SMPH_PC_MASK) != page_code) 6730 continue; 6731 6732 /* Look for the right subpage or the subpage wildcard*/ 6733 if ((lun->mode_pages.index[i].subpage != subpage) 6734 && (subpage != SMS_SUBPAGE_ALL)) 6735 continue; 6736 6737 /* Make sure the page is supported for this dev type */ 6738 if ((control_dev != 0) 6739 && (lun->mode_pages.index[i].page_flags & 6740 CTL_PAGE_FLAG_DISK_ONLY)) 6741 continue; 6742 6743 #if 0 6744 printf("found page %#x len %d\n", 6745 lun->mode_pages.index[i].page_code & 6746 SMPH_PC_MASK, 6747 lun->mode_pages.index[i].page_len); 6748 #endif 6749 6750 page_len += lun->mode_pages.index[i].page_len; 6751 } 6752 6753 if (page_len == 0) { 6754 ctl_set_invalid_field(ctsio, 6755 /*sks_valid*/ 1, 6756 /*command*/ 1, 6757 /*field*/ 2, 6758 /*bit_valid*/ 1, 6759 /*bit*/ 5); 6760 ctl_done((union ctl_io *)ctsio); 6761 return (CTL_RETVAL_COMPLETE); 6762 } 6763 break; 6764 } 6765 } 6766 6767 total_len = header_len + page_len; 6768 #if 0 6769 printf("header_len = %d, page_len = %d, total_len = %d\n", 6770 header_len, page_len, total_len); 6771 #endif 6772 6773 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6774 ctsio->kern_sg_entries = 0; 6775 ctsio->kern_data_resid = 0; 6776 ctsio->kern_rel_offset = 0; 6777 if (total_len < alloc_len) { 6778 ctsio->residual = alloc_len - total_len; 6779 ctsio->kern_data_len = total_len; 6780 ctsio->kern_total_len = total_len; 6781 } else { 6782 ctsio->residual = 0; 6783 ctsio->kern_data_len = alloc_len; 6784 ctsio->kern_total_len = alloc_len; 6785 } 6786 6787 switch (ctsio->cdb[0]) { 6788 case MODE_SENSE_6: { 6789 struct scsi_mode_hdr_6 *header; 6790 6791 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6792 6793 header->datalen = ctl_min(total_len - 1, 254); 6794 if (control_dev == 0) { 6795 header->dev_specific = 0x10; /* DPOFUA */ 6796 if ((lun->flags & CTL_LUN_READONLY) || 6797 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6798 .eca_and_aen & SCP_SWP) != 0) 6799 header->dev_specific |= 0x80; /* WP */ 6800 } 6801 if (dbd) 6802 header->block_descr_len = 0; 6803 else 6804 header->block_descr_len = 6805 sizeof(struct scsi_mode_block_descr); 6806 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6807 break; 6808 } 6809 case MODE_SENSE_10: { 6810 struct scsi_mode_hdr_10 *header; 6811 int datalen; 6812 6813 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6814 6815 datalen = ctl_min(total_len - 2, 65533); 6816 scsi_ulto2b(datalen, header->datalen); 6817 if (control_dev == 0) { 6818 header->dev_specific = 0x10; /* DPOFUA */ 6819 if ((lun->flags & CTL_LUN_READONLY) || 6820 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6821 .eca_and_aen & SCP_SWP) != 0) 6822 header->dev_specific |= 0x80; /* WP */ 6823 } 6824 if (dbd) 6825 scsi_ulto2b(0, header->block_descr_len); 6826 else 6827 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6828 header->block_descr_len); 6829 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6830 break; 6831 } 6832 default: 6833 panic("invalid CDB type %#x", ctsio->cdb[0]); 6834 break; /* NOTREACHED */ 6835 } 6836 6837 /* 6838 * If we've got a disk, use its blocksize in the block 6839 * descriptor. Otherwise, just set it to 0. 6840 */ 6841 if (dbd == 0) { 6842 if (control_dev == 0) 6843 scsi_ulto3b(lun->be_lun->blocksize, 6844 block_desc->block_len); 6845 else 6846 scsi_ulto3b(0, block_desc->block_len); 6847 } 6848 6849 switch (page_code) { 6850 case SMS_ALL_PAGES_PAGE: { 6851 int i, data_used; 6852 6853 data_used = header_len; 6854 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6855 struct ctl_page_index *page_index; 6856 6857 page_index = &lun->mode_pages.index[i]; 6858 6859 if ((control_dev != 0) 6860 && (page_index->page_flags & 6861 CTL_PAGE_FLAG_DISK_ONLY)) 6862 continue; 6863 6864 /* 6865 * We don't use this subpage if the user didn't 6866 * request all subpages. We already checked (above) 6867 * to make sure the user only specified a subpage 6868 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6869 */ 6870 if ((page_index->subpage != 0) 6871 && (subpage == SMS_SUBPAGE_PAGE_0)) 6872 continue; 6873 6874 /* 6875 * Call the handler, if it exists, to update the 6876 * page to the latest values. 6877 */ 6878 if (page_index->sense_handler != NULL) 6879 page_index->sense_handler(ctsio, page_index,pc); 6880 6881 memcpy(ctsio->kern_data_ptr + data_used, 6882 page_index->page_data + 6883 (page_index->page_len * pc), 6884 page_index->page_len); 6885 data_used += page_index->page_len; 6886 } 6887 break; 6888 } 6889 default: { 6890 int i, data_used; 6891 6892 data_used = header_len; 6893 6894 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6895 struct ctl_page_index *page_index; 6896 6897 page_index = &lun->mode_pages.index[i]; 6898 6899 /* Look for the right page code */ 6900 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6901 continue; 6902 6903 /* Look for the right subpage or the subpage wildcard*/ 6904 if ((page_index->subpage != subpage) 6905 && (subpage != SMS_SUBPAGE_ALL)) 6906 continue; 6907 6908 /* Make sure the page is supported for this dev type */ 6909 if ((control_dev != 0) 6910 && (page_index->page_flags & 6911 CTL_PAGE_FLAG_DISK_ONLY)) 6912 continue; 6913 6914 /* 6915 * Call the handler, if it exists, to update the 6916 * page to the latest values. 6917 */ 6918 if (page_index->sense_handler != NULL) 6919 page_index->sense_handler(ctsio, page_index,pc); 6920 6921 memcpy(ctsio->kern_data_ptr + data_used, 6922 page_index->page_data + 6923 (page_index->page_len * pc), 6924 page_index->page_len); 6925 data_used += page_index->page_len; 6926 } 6927 break; 6928 } 6929 } 6930 6931 ctsio->scsi_status = SCSI_STATUS_OK; 6932 6933 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6934 ctsio->be_move_done = ctl_config_move_done; 6935 ctl_datamove((union ctl_io *)ctsio); 6936 6937 return (CTL_RETVAL_COMPLETE); 6938 } 6939 6940 int 6941 ctl_log_sense(struct ctl_scsiio *ctsio) 6942 { 6943 struct ctl_lun *lun; 6944 int i, pc, page_code, subpage; 6945 int alloc_len, total_len; 6946 struct ctl_page_index *page_index; 6947 struct scsi_log_sense *cdb; 6948 struct scsi_log_header *header; 6949 6950 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6951 6952 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6953 cdb = (struct scsi_log_sense *)ctsio->cdb; 6954 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6955 page_code = cdb->page & SLS_PAGE_CODE; 6956 subpage = cdb->subpage; 6957 alloc_len = scsi_2btoul(cdb->length); 6958 6959 page_index = NULL; 6960 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6961 page_index = &lun->log_pages.index[i]; 6962 6963 /* Look for the right page code */ 6964 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6965 continue; 6966 6967 /* Look for the right subpage or the subpage wildcard*/ 6968 if (page_index->subpage != subpage) 6969 continue; 6970 6971 break; 6972 } 6973 if (i >= CTL_NUM_LOG_PAGES) { 6974 ctl_set_invalid_field(ctsio, 6975 /*sks_valid*/ 1, 6976 /*command*/ 1, 6977 /*field*/ 2, 6978 /*bit_valid*/ 0, 6979 /*bit*/ 0); 6980 ctl_done((union ctl_io *)ctsio); 6981 return (CTL_RETVAL_COMPLETE); 6982 } 6983 6984 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6985 6986 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6987 ctsio->kern_sg_entries = 0; 6988 ctsio->kern_data_resid = 0; 6989 ctsio->kern_rel_offset = 0; 6990 if (total_len < alloc_len) { 6991 ctsio->residual = alloc_len - total_len; 6992 ctsio->kern_data_len = total_len; 6993 ctsio->kern_total_len = total_len; 6994 } else { 6995 ctsio->residual = 0; 6996 ctsio->kern_data_len = alloc_len; 6997 ctsio->kern_total_len = alloc_len; 6998 } 6999 7000 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7001 header->page = page_index->page_code; 7002 if (page_index->subpage) { 7003 header->page |= SL_SPF; 7004 header->subpage = page_index->subpage; 7005 } 7006 scsi_ulto2b(page_index->page_len, header->datalen); 7007 7008 /* 7009 * Call the handler, if it exists, to update the 7010 * page to the latest values. 7011 */ 7012 if (page_index->sense_handler != NULL) 7013 page_index->sense_handler(ctsio, page_index, pc); 7014 7015 memcpy(header + 1, page_index->page_data, page_index->page_len); 7016 7017 ctsio->scsi_status = SCSI_STATUS_OK; 7018 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7019 ctsio->be_move_done = ctl_config_move_done; 7020 ctl_datamove((union ctl_io *)ctsio); 7021 7022 return (CTL_RETVAL_COMPLETE); 7023 } 7024 7025 int 7026 ctl_read_capacity(struct ctl_scsiio *ctsio) 7027 { 7028 struct scsi_read_capacity *cdb; 7029 struct scsi_read_capacity_data *data; 7030 struct ctl_lun *lun; 7031 uint32_t lba; 7032 7033 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7034 7035 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7036 7037 lba = scsi_4btoul(cdb->addr); 7038 if (((cdb->pmi & SRC_PMI) == 0) 7039 && (lba != 0)) { 7040 ctl_set_invalid_field(/*ctsio*/ ctsio, 7041 /*sks_valid*/ 1, 7042 /*command*/ 1, 7043 /*field*/ 2, 7044 /*bit_valid*/ 0, 7045 /*bit*/ 0); 7046 ctl_done((union ctl_io *)ctsio); 7047 return (CTL_RETVAL_COMPLETE); 7048 } 7049 7050 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7051 7052 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7053 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7054 ctsio->residual = 0; 7055 ctsio->kern_data_len = sizeof(*data); 7056 ctsio->kern_total_len = sizeof(*data); 7057 ctsio->kern_data_resid = 0; 7058 ctsio->kern_rel_offset = 0; 7059 ctsio->kern_sg_entries = 0; 7060 7061 /* 7062 * If the maximum LBA is greater than 0xfffffffe, the user must 7063 * issue a SERVICE ACTION IN (16) command, with the read capacity 7064 * serivce action set. 7065 */ 7066 if (lun->be_lun->maxlba > 0xfffffffe) 7067 scsi_ulto4b(0xffffffff, data->addr); 7068 else 7069 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7070 7071 /* 7072 * XXX KDM this may not be 512 bytes... 7073 */ 7074 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7075 7076 ctsio->scsi_status = SCSI_STATUS_OK; 7077 7078 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7079 ctsio->be_move_done = ctl_config_move_done; 7080 ctl_datamove((union ctl_io *)ctsio); 7081 7082 return (CTL_RETVAL_COMPLETE); 7083 } 7084 7085 int 7086 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7087 { 7088 struct scsi_read_capacity_16 *cdb; 7089 struct scsi_read_capacity_data_long *data; 7090 struct ctl_lun *lun; 7091 uint64_t lba; 7092 uint32_t alloc_len; 7093 7094 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7095 7096 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7097 7098 alloc_len = scsi_4btoul(cdb->alloc_len); 7099 lba = scsi_8btou64(cdb->addr); 7100 7101 if ((cdb->reladr & SRC16_PMI) 7102 && (lba != 0)) { 7103 ctl_set_invalid_field(/*ctsio*/ ctsio, 7104 /*sks_valid*/ 1, 7105 /*command*/ 1, 7106 /*field*/ 2, 7107 /*bit_valid*/ 0, 7108 /*bit*/ 0); 7109 ctl_done((union ctl_io *)ctsio); 7110 return (CTL_RETVAL_COMPLETE); 7111 } 7112 7113 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7114 7115 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7116 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7117 7118 if (sizeof(*data) < alloc_len) { 7119 ctsio->residual = alloc_len - sizeof(*data); 7120 ctsio->kern_data_len = sizeof(*data); 7121 ctsio->kern_total_len = sizeof(*data); 7122 } else { 7123 ctsio->residual = 0; 7124 ctsio->kern_data_len = alloc_len; 7125 ctsio->kern_total_len = alloc_len; 7126 } 7127 ctsio->kern_data_resid = 0; 7128 ctsio->kern_rel_offset = 0; 7129 ctsio->kern_sg_entries = 0; 7130 7131 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7132 /* XXX KDM this may not be 512 bytes... */ 7133 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7134 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7135 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7136 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7137 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7138 7139 ctsio->scsi_status = SCSI_STATUS_OK; 7140 7141 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7142 ctsio->be_move_done = ctl_config_move_done; 7143 ctl_datamove((union ctl_io *)ctsio); 7144 7145 return (CTL_RETVAL_COMPLETE); 7146 } 7147 7148 int 7149 ctl_read_defect(struct ctl_scsiio *ctsio) 7150 { 7151 struct scsi_read_defect_data_10 *ccb10; 7152 struct scsi_read_defect_data_12 *ccb12; 7153 struct scsi_read_defect_data_hdr_10 *data10; 7154 struct scsi_read_defect_data_hdr_12 *data12; 7155 struct ctl_lun *lun; 7156 uint32_t alloc_len, data_len; 7157 uint8_t format; 7158 7159 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7160 7161 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7162 7163 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7164 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7165 format = ccb10->format; 7166 alloc_len = scsi_2btoul(ccb10->alloc_length); 7167 data_len = sizeof(*data10); 7168 } else { 7169 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7170 format = ccb12->format; 7171 alloc_len = scsi_4btoul(ccb12->alloc_length); 7172 data_len = sizeof(*data12); 7173 } 7174 if (alloc_len == 0) { 7175 ctl_set_success(ctsio); 7176 ctl_done((union ctl_io *)ctsio); 7177 return (CTL_RETVAL_COMPLETE); 7178 } 7179 7180 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7181 if (data_len < alloc_len) { 7182 ctsio->residual = alloc_len - data_len; 7183 ctsio->kern_data_len = data_len; 7184 ctsio->kern_total_len = data_len; 7185 } else { 7186 ctsio->residual = 0; 7187 ctsio->kern_data_len = alloc_len; 7188 ctsio->kern_total_len = alloc_len; 7189 } 7190 ctsio->kern_data_resid = 0; 7191 ctsio->kern_rel_offset = 0; 7192 ctsio->kern_sg_entries = 0; 7193 7194 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7195 data10 = (struct scsi_read_defect_data_hdr_10 *) 7196 ctsio->kern_data_ptr; 7197 data10->format = format; 7198 scsi_ulto2b(0, data10->length); 7199 } else { 7200 data12 = (struct scsi_read_defect_data_hdr_12 *) 7201 ctsio->kern_data_ptr; 7202 data12->format = format; 7203 scsi_ulto2b(0, data12->generation); 7204 scsi_ulto4b(0, data12->length); 7205 } 7206 7207 ctsio->scsi_status = SCSI_STATUS_OK; 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 (CTL_RETVAL_COMPLETE); 7212 } 7213 7214 int 7215 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7216 { 7217 struct scsi_maintenance_in *cdb; 7218 int retval; 7219 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7220 int num_target_port_groups, num_target_ports, single; 7221 struct ctl_lun *lun; 7222 struct ctl_softc *softc; 7223 struct ctl_port *port; 7224 struct scsi_target_group_data *rtg_ptr; 7225 struct scsi_target_group_data_extended *rtg_ext_ptr; 7226 struct scsi_target_port_group_descriptor *tpg_desc; 7227 7228 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7229 7230 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7231 softc = control_softc; 7232 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7233 7234 retval = CTL_RETVAL_COMPLETE; 7235 7236 switch (cdb->byte2 & STG_PDF_MASK) { 7237 case STG_PDF_LENGTH: 7238 ext = 0; 7239 break; 7240 case STG_PDF_EXTENDED: 7241 ext = 1; 7242 break; 7243 default: 7244 ctl_set_invalid_field(/*ctsio*/ ctsio, 7245 /*sks_valid*/ 1, 7246 /*command*/ 1, 7247 /*field*/ 2, 7248 /*bit_valid*/ 1, 7249 /*bit*/ 5); 7250 ctl_done((union ctl_io *)ctsio); 7251 return(retval); 7252 } 7253 7254 single = ctl_is_single; 7255 if (single) 7256 num_target_port_groups = 1; 7257 else 7258 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7259 num_target_ports = 0; 7260 mtx_lock(&softc->ctl_lock); 7261 STAILQ_FOREACH(port, &softc->port_list, links) { 7262 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7263 continue; 7264 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7265 continue; 7266 num_target_ports++; 7267 } 7268 mtx_unlock(&softc->ctl_lock); 7269 7270 if (ext) 7271 total_len = sizeof(struct scsi_target_group_data_extended); 7272 else 7273 total_len = sizeof(struct scsi_target_group_data); 7274 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7275 num_target_port_groups + 7276 sizeof(struct scsi_target_port_descriptor) * 7277 num_target_ports * num_target_port_groups; 7278 7279 alloc_len = scsi_4btoul(cdb->length); 7280 7281 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7282 7283 ctsio->kern_sg_entries = 0; 7284 7285 if (total_len < alloc_len) { 7286 ctsio->residual = alloc_len - total_len; 7287 ctsio->kern_data_len = total_len; 7288 ctsio->kern_total_len = total_len; 7289 } else { 7290 ctsio->residual = 0; 7291 ctsio->kern_data_len = alloc_len; 7292 ctsio->kern_total_len = alloc_len; 7293 } 7294 ctsio->kern_data_resid = 0; 7295 ctsio->kern_rel_offset = 0; 7296 7297 if (ext) { 7298 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7299 ctsio->kern_data_ptr; 7300 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7301 rtg_ext_ptr->format_type = 0x10; 7302 rtg_ext_ptr->implicit_transition_time = 0; 7303 tpg_desc = &rtg_ext_ptr->groups[0]; 7304 } else { 7305 rtg_ptr = (struct scsi_target_group_data *) 7306 ctsio->kern_data_ptr; 7307 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7308 tpg_desc = &rtg_ptr->groups[0]; 7309 } 7310 7311 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7312 mtx_lock(&softc->ctl_lock); 7313 for (g = 0; g < num_target_port_groups; g++) { 7314 if (g == pg) 7315 tpg_desc->pref_state = TPG_PRIMARY | 7316 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7317 else 7318 tpg_desc->pref_state = 7319 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7320 tpg_desc->support = TPG_AO_SUP; 7321 if (!single) 7322 tpg_desc->support |= TPG_AN_SUP; 7323 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7324 tpg_desc->status = TPG_IMPLICIT; 7325 pc = 0; 7326 STAILQ_FOREACH(port, &softc->port_list, links) { 7327 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7328 continue; 7329 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7330 CTL_MAX_LUNS) 7331 continue; 7332 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7333 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7334 relative_target_port_identifier); 7335 pc++; 7336 } 7337 tpg_desc->target_port_count = pc; 7338 tpg_desc = (struct scsi_target_port_group_descriptor *) 7339 &tpg_desc->descriptors[pc]; 7340 } 7341 mtx_unlock(&softc->ctl_lock); 7342 7343 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7344 ctsio->be_move_done = ctl_config_move_done; 7345 7346 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7347 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7348 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7349 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7350 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7351 7352 ctl_datamove((union ctl_io *)ctsio); 7353 return(retval); 7354 } 7355 7356 int 7357 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7358 { 7359 struct ctl_lun *lun; 7360 struct scsi_report_supported_opcodes *cdb; 7361 const struct ctl_cmd_entry *entry, *sentry; 7362 struct scsi_report_supported_opcodes_all *all; 7363 struct scsi_report_supported_opcodes_descr *descr; 7364 struct scsi_report_supported_opcodes_one *one; 7365 int retval; 7366 int alloc_len, total_len; 7367 int opcode, service_action, i, j, num; 7368 7369 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7370 7371 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7372 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7373 7374 retval = CTL_RETVAL_COMPLETE; 7375 7376 opcode = cdb->requested_opcode; 7377 service_action = scsi_2btoul(cdb->requested_service_action); 7378 switch (cdb->options & RSO_OPTIONS_MASK) { 7379 case RSO_OPTIONS_ALL: 7380 num = 0; 7381 for (i = 0; i < 256; i++) { 7382 entry = &ctl_cmd_table[i]; 7383 if (entry->flags & CTL_CMD_FLAG_SA5) { 7384 for (j = 0; j < 32; j++) { 7385 sentry = &((const struct ctl_cmd_entry *) 7386 entry->execute)[j]; 7387 if (ctl_cmd_applicable( 7388 lun->be_lun->lun_type, sentry)) 7389 num++; 7390 } 7391 } else { 7392 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7393 entry)) 7394 num++; 7395 } 7396 } 7397 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7398 num * sizeof(struct scsi_report_supported_opcodes_descr); 7399 break; 7400 case RSO_OPTIONS_OC: 7401 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7402 ctl_set_invalid_field(/*ctsio*/ ctsio, 7403 /*sks_valid*/ 1, 7404 /*command*/ 1, 7405 /*field*/ 2, 7406 /*bit_valid*/ 1, 7407 /*bit*/ 2); 7408 ctl_done((union ctl_io *)ctsio); 7409 return (CTL_RETVAL_COMPLETE); 7410 } 7411 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7412 break; 7413 case RSO_OPTIONS_OC_SA: 7414 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7415 service_action >= 32) { 7416 ctl_set_invalid_field(/*ctsio*/ ctsio, 7417 /*sks_valid*/ 1, 7418 /*command*/ 1, 7419 /*field*/ 2, 7420 /*bit_valid*/ 1, 7421 /*bit*/ 2); 7422 ctl_done((union ctl_io *)ctsio); 7423 return (CTL_RETVAL_COMPLETE); 7424 } 7425 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7426 break; 7427 default: 7428 ctl_set_invalid_field(/*ctsio*/ ctsio, 7429 /*sks_valid*/ 1, 7430 /*command*/ 1, 7431 /*field*/ 2, 7432 /*bit_valid*/ 1, 7433 /*bit*/ 2); 7434 ctl_done((union ctl_io *)ctsio); 7435 return (CTL_RETVAL_COMPLETE); 7436 } 7437 7438 alloc_len = scsi_4btoul(cdb->length); 7439 7440 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7441 7442 ctsio->kern_sg_entries = 0; 7443 7444 if (total_len < alloc_len) { 7445 ctsio->residual = alloc_len - total_len; 7446 ctsio->kern_data_len = total_len; 7447 ctsio->kern_total_len = total_len; 7448 } else { 7449 ctsio->residual = 0; 7450 ctsio->kern_data_len = alloc_len; 7451 ctsio->kern_total_len = alloc_len; 7452 } 7453 ctsio->kern_data_resid = 0; 7454 ctsio->kern_rel_offset = 0; 7455 7456 switch (cdb->options & RSO_OPTIONS_MASK) { 7457 case RSO_OPTIONS_ALL: 7458 all = (struct scsi_report_supported_opcodes_all *) 7459 ctsio->kern_data_ptr; 7460 num = 0; 7461 for (i = 0; i < 256; i++) { 7462 entry = &ctl_cmd_table[i]; 7463 if (entry->flags & CTL_CMD_FLAG_SA5) { 7464 for (j = 0; j < 32; j++) { 7465 sentry = &((const struct ctl_cmd_entry *) 7466 entry->execute)[j]; 7467 if (!ctl_cmd_applicable( 7468 lun->be_lun->lun_type, sentry)) 7469 continue; 7470 descr = &all->descr[num++]; 7471 descr->opcode = i; 7472 scsi_ulto2b(j, descr->service_action); 7473 descr->flags = RSO_SERVACTV; 7474 scsi_ulto2b(sentry->length, 7475 descr->cdb_length); 7476 } 7477 } else { 7478 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7479 entry)) 7480 continue; 7481 descr = &all->descr[num++]; 7482 descr->opcode = i; 7483 scsi_ulto2b(0, descr->service_action); 7484 descr->flags = 0; 7485 scsi_ulto2b(entry->length, descr->cdb_length); 7486 } 7487 } 7488 scsi_ulto4b( 7489 num * sizeof(struct scsi_report_supported_opcodes_descr), 7490 all->length); 7491 break; 7492 case RSO_OPTIONS_OC: 7493 one = (struct scsi_report_supported_opcodes_one *) 7494 ctsio->kern_data_ptr; 7495 entry = &ctl_cmd_table[opcode]; 7496 goto fill_one; 7497 case RSO_OPTIONS_OC_SA: 7498 one = (struct scsi_report_supported_opcodes_one *) 7499 ctsio->kern_data_ptr; 7500 entry = &ctl_cmd_table[opcode]; 7501 entry = &((const struct ctl_cmd_entry *) 7502 entry->execute)[service_action]; 7503 fill_one: 7504 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7505 one->support = 3; 7506 scsi_ulto2b(entry->length, one->cdb_length); 7507 one->cdb_usage[0] = opcode; 7508 memcpy(&one->cdb_usage[1], entry->usage, 7509 entry->length - 1); 7510 } else 7511 one->support = 1; 7512 break; 7513 } 7514 7515 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7516 ctsio->be_move_done = ctl_config_move_done; 7517 7518 ctl_datamove((union ctl_io *)ctsio); 7519 return(retval); 7520 } 7521 7522 int 7523 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7524 { 7525 struct ctl_lun *lun; 7526 struct scsi_report_supported_tmf *cdb; 7527 struct scsi_report_supported_tmf_data *data; 7528 int retval; 7529 int alloc_len, total_len; 7530 7531 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7532 7533 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7534 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7535 7536 retval = CTL_RETVAL_COMPLETE; 7537 7538 total_len = sizeof(struct scsi_report_supported_tmf_data); 7539 alloc_len = scsi_4btoul(cdb->length); 7540 7541 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7542 7543 ctsio->kern_sg_entries = 0; 7544 7545 if (total_len < alloc_len) { 7546 ctsio->residual = alloc_len - total_len; 7547 ctsio->kern_data_len = total_len; 7548 ctsio->kern_total_len = total_len; 7549 } else { 7550 ctsio->residual = 0; 7551 ctsio->kern_data_len = alloc_len; 7552 ctsio->kern_total_len = alloc_len; 7553 } 7554 ctsio->kern_data_resid = 0; 7555 ctsio->kern_rel_offset = 0; 7556 7557 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7558 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7559 data->byte2 |= RST_ITNRS; 7560 7561 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7562 ctsio->be_move_done = ctl_config_move_done; 7563 7564 ctl_datamove((union ctl_io *)ctsio); 7565 return (retval); 7566 } 7567 7568 int 7569 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7570 { 7571 struct ctl_lun *lun; 7572 struct scsi_report_timestamp *cdb; 7573 struct scsi_report_timestamp_data *data; 7574 struct timeval tv; 7575 int64_t timestamp; 7576 int retval; 7577 int alloc_len, total_len; 7578 7579 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7580 7581 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7582 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7583 7584 retval = CTL_RETVAL_COMPLETE; 7585 7586 total_len = sizeof(struct scsi_report_timestamp_data); 7587 alloc_len = scsi_4btoul(cdb->length); 7588 7589 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7590 7591 ctsio->kern_sg_entries = 0; 7592 7593 if (total_len < alloc_len) { 7594 ctsio->residual = alloc_len - total_len; 7595 ctsio->kern_data_len = total_len; 7596 ctsio->kern_total_len = total_len; 7597 } else { 7598 ctsio->residual = 0; 7599 ctsio->kern_data_len = alloc_len; 7600 ctsio->kern_total_len = alloc_len; 7601 } 7602 ctsio->kern_data_resid = 0; 7603 ctsio->kern_rel_offset = 0; 7604 7605 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7606 scsi_ulto2b(sizeof(*data) - 2, data->length); 7607 data->origin = RTS_ORIG_OUTSIDE; 7608 getmicrotime(&tv); 7609 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7610 scsi_ulto4b(timestamp >> 16, data->timestamp); 7611 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7612 7613 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7614 ctsio->be_move_done = ctl_config_move_done; 7615 7616 ctl_datamove((union ctl_io *)ctsio); 7617 return (retval); 7618 } 7619 7620 int 7621 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7622 { 7623 struct scsi_per_res_in *cdb; 7624 int alloc_len, total_len = 0; 7625 /* struct scsi_per_res_in_rsrv in_data; */ 7626 struct ctl_lun *lun; 7627 struct ctl_softc *softc; 7628 7629 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7630 7631 softc = control_softc; 7632 7633 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7634 7635 alloc_len = scsi_2btoul(cdb->length); 7636 7637 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7638 7639 retry: 7640 mtx_lock(&lun->lun_lock); 7641 switch (cdb->action) { 7642 case SPRI_RK: /* read keys */ 7643 total_len = sizeof(struct scsi_per_res_in_keys) + 7644 lun->pr_key_count * 7645 sizeof(struct scsi_per_res_key); 7646 break; 7647 case SPRI_RR: /* read reservation */ 7648 if (lun->flags & CTL_LUN_PR_RESERVED) 7649 total_len = sizeof(struct scsi_per_res_in_rsrv); 7650 else 7651 total_len = sizeof(struct scsi_per_res_in_header); 7652 break; 7653 case SPRI_RC: /* report capabilities */ 7654 total_len = sizeof(struct scsi_per_res_cap); 7655 break; 7656 case SPRI_RS: /* read full status */ 7657 total_len = sizeof(struct scsi_per_res_in_header) + 7658 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7659 lun->pr_key_count; 7660 break; 7661 default: 7662 panic("Invalid PR type %x", cdb->action); 7663 } 7664 mtx_unlock(&lun->lun_lock); 7665 7666 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7667 7668 if (total_len < alloc_len) { 7669 ctsio->residual = alloc_len - total_len; 7670 ctsio->kern_data_len = total_len; 7671 ctsio->kern_total_len = total_len; 7672 } else { 7673 ctsio->residual = 0; 7674 ctsio->kern_data_len = alloc_len; 7675 ctsio->kern_total_len = alloc_len; 7676 } 7677 7678 ctsio->kern_data_resid = 0; 7679 ctsio->kern_rel_offset = 0; 7680 ctsio->kern_sg_entries = 0; 7681 7682 mtx_lock(&lun->lun_lock); 7683 switch (cdb->action) { 7684 case SPRI_RK: { // read keys 7685 struct scsi_per_res_in_keys *res_keys; 7686 int i, key_count; 7687 7688 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7689 7690 /* 7691 * We had to drop the lock to allocate our buffer, which 7692 * leaves time for someone to come in with another 7693 * persistent reservation. (That is unlikely, though, 7694 * since this should be the only persistent reservation 7695 * command active right now.) 7696 */ 7697 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7698 (lun->pr_key_count * 7699 sizeof(struct scsi_per_res_key)))){ 7700 mtx_unlock(&lun->lun_lock); 7701 free(ctsio->kern_data_ptr, M_CTL); 7702 printf("%s: reservation length changed, retrying\n", 7703 __func__); 7704 goto retry; 7705 } 7706 7707 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7708 7709 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7710 lun->pr_key_count, res_keys->header.length); 7711 7712 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7713 if (lun->pr_keys[i] == 0) 7714 continue; 7715 7716 /* 7717 * We used lun->pr_key_count to calculate the 7718 * size to allocate. If it turns out the number of 7719 * initiators with the registered flag set is 7720 * larger than that (i.e. they haven't been kept in 7721 * sync), we've got a problem. 7722 */ 7723 if (key_count >= lun->pr_key_count) { 7724 #ifdef NEEDTOPORT 7725 csevent_log(CSC_CTL | CSC_SHELF_SW | 7726 CTL_PR_ERROR, 7727 csevent_LogType_Fault, 7728 csevent_AlertLevel_Yellow, 7729 csevent_FRU_ShelfController, 7730 csevent_FRU_Firmware, 7731 csevent_FRU_Unknown, 7732 "registered keys %d >= key " 7733 "count %d", key_count, 7734 lun->pr_key_count); 7735 #endif 7736 key_count++; 7737 continue; 7738 } 7739 scsi_u64to8b(lun->pr_keys[i], 7740 res_keys->keys[key_count].key); 7741 key_count++; 7742 } 7743 break; 7744 } 7745 case SPRI_RR: { // read reservation 7746 struct scsi_per_res_in_rsrv *res; 7747 int tmp_len, header_only; 7748 7749 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7750 7751 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7752 7753 if (lun->flags & CTL_LUN_PR_RESERVED) 7754 { 7755 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7756 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7757 res->header.length); 7758 header_only = 0; 7759 } else { 7760 tmp_len = sizeof(struct scsi_per_res_in_header); 7761 scsi_ulto4b(0, res->header.length); 7762 header_only = 1; 7763 } 7764 7765 /* 7766 * We had to drop the lock to allocate our buffer, which 7767 * leaves time for someone to come in with another 7768 * persistent reservation. (That is unlikely, though, 7769 * since this should be the only persistent reservation 7770 * command active right now.) 7771 */ 7772 if (tmp_len != total_len) { 7773 mtx_unlock(&lun->lun_lock); 7774 free(ctsio->kern_data_ptr, M_CTL); 7775 printf("%s: reservation status changed, retrying\n", 7776 __func__); 7777 goto retry; 7778 } 7779 7780 /* 7781 * No reservation held, so we're done. 7782 */ 7783 if (header_only != 0) 7784 break; 7785 7786 /* 7787 * If the registration is an All Registrants type, the key 7788 * is 0, since it doesn't really matter. 7789 */ 7790 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7791 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx], 7792 res->data.reservation); 7793 } 7794 res->data.scopetype = lun->res_type; 7795 break; 7796 } 7797 case SPRI_RC: //report capabilities 7798 { 7799 struct scsi_per_res_cap *res_cap; 7800 uint16_t type_mask; 7801 7802 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7803 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7804 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7805 type_mask = SPRI_TM_WR_EX_AR | 7806 SPRI_TM_EX_AC_RO | 7807 SPRI_TM_WR_EX_RO | 7808 SPRI_TM_EX_AC | 7809 SPRI_TM_WR_EX | 7810 SPRI_TM_EX_AC_AR; 7811 scsi_ulto2b(type_mask, res_cap->type_mask); 7812 break; 7813 } 7814 case SPRI_RS: { // read full status 7815 struct scsi_per_res_in_full *res_status; 7816 struct scsi_per_res_in_full_desc *res_desc; 7817 struct ctl_port *port; 7818 int i, len; 7819 7820 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7821 7822 /* 7823 * We had to drop the lock to allocate our buffer, which 7824 * leaves time for someone to come in with another 7825 * persistent reservation. (That is unlikely, though, 7826 * since this should be the only persistent reservation 7827 * command active right now.) 7828 */ 7829 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7830 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7831 lun->pr_key_count)){ 7832 mtx_unlock(&lun->lun_lock); 7833 free(ctsio->kern_data_ptr, M_CTL); 7834 printf("%s: reservation length changed, retrying\n", 7835 __func__); 7836 goto retry; 7837 } 7838 7839 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7840 7841 res_desc = &res_status->desc[0]; 7842 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7843 if (lun->pr_keys[i] == 0) 7844 continue; 7845 7846 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key); 7847 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7848 (lun->pr_res_idx == i || 7849 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7850 res_desc->flags = SPRI_FULL_R_HOLDER; 7851 res_desc->scopetype = lun->res_type; 7852 } 7853 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7854 res_desc->rel_trgt_port_id); 7855 len = 0; 7856 port = softc->ctl_ports[ 7857 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7858 if (port != NULL) 7859 len = ctl_create_iid(port, 7860 i % CTL_MAX_INIT_PER_PORT, 7861 res_desc->transport_id); 7862 scsi_ulto4b(len, res_desc->additional_length); 7863 res_desc = (struct scsi_per_res_in_full_desc *) 7864 &res_desc->transport_id[len]; 7865 } 7866 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7867 res_status->header.length); 7868 break; 7869 } 7870 default: 7871 /* 7872 * This is a bug, because we just checked for this above, 7873 * and should have returned an error. 7874 */ 7875 panic("Invalid PR type %x", cdb->action); 7876 break; /* NOTREACHED */ 7877 } 7878 mtx_unlock(&lun->lun_lock); 7879 7880 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7881 ctsio->be_move_done = ctl_config_move_done; 7882 7883 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7884 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7885 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7886 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7887 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7888 7889 ctl_datamove((union ctl_io *)ctsio); 7890 7891 return (CTL_RETVAL_COMPLETE); 7892 } 7893 7894 /* 7895 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7896 * it should return. 7897 */ 7898 static int 7899 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7900 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7901 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7902 struct scsi_per_res_out_parms* param) 7903 { 7904 union ctl_ha_msg persis_io; 7905 int retval, i; 7906 int isc_retval; 7907 7908 retval = 0; 7909 7910 mtx_lock(&lun->lun_lock); 7911 if (sa_res_key == 0) { 7912 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7913 /* validate scope and type */ 7914 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7915 SPR_LU_SCOPE) { 7916 mtx_unlock(&lun->lun_lock); 7917 ctl_set_invalid_field(/*ctsio*/ ctsio, 7918 /*sks_valid*/ 1, 7919 /*command*/ 1, 7920 /*field*/ 2, 7921 /*bit_valid*/ 1, 7922 /*bit*/ 4); 7923 ctl_done((union ctl_io *)ctsio); 7924 return (1); 7925 } 7926 7927 if (type>8 || type==2 || type==4 || type==0) { 7928 mtx_unlock(&lun->lun_lock); 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 (1); 7937 } 7938 7939 /* 7940 * Unregister everybody else and build UA for 7941 * them 7942 */ 7943 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7944 if (i == residx || lun->pr_keys[i] == 0) 7945 continue; 7946 7947 if (!persis_offset 7948 && i <CTL_MAX_INITIATORS) 7949 lun->pending_ua[i] |= 7950 CTL_UA_REG_PREEMPT; 7951 else if (persis_offset 7952 && i >= persis_offset) 7953 lun->pending_ua[i-persis_offset] |= 7954 CTL_UA_REG_PREEMPT; 7955 lun->pr_keys[i] = 0; 7956 } 7957 lun->pr_key_count = 1; 7958 lun->res_type = type; 7959 if (lun->res_type != SPR_TYPE_WR_EX_AR 7960 && lun->res_type != SPR_TYPE_EX_AC_AR) 7961 lun->pr_res_idx = residx; 7962 7963 /* send msg to other side */ 7964 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7965 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7966 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7967 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7968 persis_io.pr.pr_info.res_type = type; 7969 memcpy(persis_io.pr.pr_info.sa_res_key, 7970 param->serv_act_res_key, 7971 sizeof(param->serv_act_res_key)); 7972 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7973 &persis_io, sizeof(persis_io), 0)) > 7974 CTL_HA_STATUS_SUCCESS) { 7975 printf("CTL:Persis Out error returned " 7976 "from ctl_ha_msg_send %d\n", 7977 isc_retval); 7978 } 7979 } else { 7980 /* not all registrants */ 7981 mtx_unlock(&lun->lun_lock); 7982 free(ctsio->kern_data_ptr, M_CTL); 7983 ctl_set_invalid_field(ctsio, 7984 /*sks_valid*/ 1, 7985 /*command*/ 0, 7986 /*field*/ 8, 7987 /*bit_valid*/ 0, 7988 /*bit*/ 0); 7989 ctl_done((union ctl_io *)ctsio); 7990 return (1); 7991 } 7992 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7993 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7994 int found = 0; 7995 7996 if (res_key == sa_res_key) { 7997 /* special case */ 7998 /* 7999 * The spec implies this is not good but doesn't 8000 * say what to do. There are two choices either 8001 * generate a res conflict or check condition 8002 * with illegal field in parameter data. Since 8003 * that is what is done when the sa_res_key is 8004 * zero I'll take that approach since this has 8005 * to do with the sa_res_key. 8006 */ 8007 mtx_unlock(&lun->lun_lock); 8008 free(ctsio->kern_data_ptr, M_CTL); 8009 ctl_set_invalid_field(ctsio, 8010 /*sks_valid*/ 1, 8011 /*command*/ 0, 8012 /*field*/ 8, 8013 /*bit_valid*/ 0, 8014 /*bit*/ 0); 8015 ctl_done((union ctl_io *)ctsio); 8016 return (1); 8017 } 8018 8019 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8020 if (lun->pr_keys[i] != sa_res_key) 8021 continue; 8022 8023 found = 1; 8024 lun->pr_keys[i] = 0; 8025 lun->pr_key_count--; 8026 8027 if (!persis_offset && i < CTL_MAX_INITIATORS) 8028 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8029 else if (persis_offset && i >= persis_offset) 8030 lun->pending_ua[i-persis_offset] |= 8031 CTL_UA_REG_PREEMPT; 8032 } 8033 if (!found) { 8034 mtx_unlock(&lun->lun_lock); 8035 free(ctsio->kern_data_ptr, M_CTL); 8036 ctl_set_reservation_conflict(ctsio); 8037 ctl_done((union ctl_io *)ctsio); 8038 return (CTL_RETVAL_COMPLETE); 8039 } 8040 /* send msg to other side */ 8041 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8042 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8043 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8044 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8045 persis_io.pr.pr_info.res_type = type; 8046 memcpy(persis_io.pr.pr_info.sa_res_key, 8047 param->serv_act_res_key, 8048 sizeof(param->serv_act_res_key)); 8049 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8050 &persis_io, sizeof(persis_io), 0)) > 8051 CTL_HA_STATUS_SUCCESS) { 8052 printf("CTL:Persis Out error returned from " 8053 "ctl_ha_msg_send %d\n", isc_retval); 8054 } 8055 } else { 8056 /* Reserved but not all registrants */ 8057 /* sa_res_key is res holder */ 8058 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) { 8059 /* validate scope and type */ 8060 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8061 SPR_LU_SCOPE) { 8062 mtx_unlock(&lun->lun_lock); 8063 ctl_set_invalid_field(/*ctsio*/ ctsio, 8064 /*sks_valid*/ 1, 8065 /*command*/ 1, 8066 /*field*/ 2, 8067 /*bit_valid*/ 1, 8068 /*bit*/ 4); 8069 ctl_done((union ctl_io *)ctsio); 8070 return (1); 8071 } 8072 8073 if (type>8 || type==2 || type==4 || type==0) { 8074 mtx_unlock(&lun->lun_lock); 8075 ctl_set_invalid_field(/*ctsio*/ ctsio, 8076 /*sks_valid*/ 1, 8077 /*command*/ 1, 8078 /*field*/ 2, 8079 /*bit_valid*/ 1, 8080 /*bit*/ 0); 8081 ctl_done((union ctl_io *)ctsio); 8082 return (1); 8083 } 8084 8085 /* 8086 * Do the following: 8087 * if sa_res_key != res_key remove all 8088 * registrants w/sa_res_key and generate UA 8089 * for these registrants(Registrations 8090 * Preempted) if it wasn't an exclusive 8091 * reservation generate UA(Reservations 8092 * Preempted) for all other registered nexuses 8093 * if the type has changed. Establish the new 8094 * reservation and holder. If res_key and 8095 * sa_res_key are the same do the above 8096 * except don't unregister the res holder. 8097 */ 8098 8099 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8100 if (i == residx || lun->pr_keys[i] == 0) 8101 continue; 8102 8103 if (sa_res_key == lun->pr_keys[i]) { 8104 lun->pr_keys[i] = 0; 8105 lun->pr_key_count--; 8106 8107 if (!persis_offset 8108 && i < CTL_MAX_INITIATORS) 8109 lun->pending_ua[i] |= 8110 CTL_UA_REG_PREEMPT; 8111 else if (persis_offset 8112 && i >= persis_offset) 8113 lun->pending_ua[i-persis_offset] |= 8114 CTL_UA_REG_PREEMPT; 8115 } else if (type != lun->res_type 8116 && (lun->res_type == SPR_TYPE_WR_EX_RO 8117 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8118 if (!persis_offset 8119 && i < CTL_MAX_INITIATORS) 8120 lun->pending_ua[i] |= 8121 CTL_UA_RES_RELEASE; 8122 else if (persis_offset 8123 && i >= persis_offset) 8124 lun->pending_ua[ 8125 i-persis_offset] |= 8126 CTL_UA_RES_RELEASE; 8127 } 8128 } 8129 lun->res_type = type; 8130 if (lun->res_type != SPR_TYPE_WR_EX_AR 8131 && lun->res_type != SPR_TYPE_EX_AC_AR) 8132 lun->pr_res_idx = residx; 8133 else 8134 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8135 8136 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8137 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8138 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8139 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8140 persis_io.pr.pr_info.res_type = type; 8141 memcpy(persis_io.pr.pr_info.sa_res_key, 8142 param->serv_act_res_key, 8143 sizeof(param->serv_act_res_key)); 8144 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8145 &persis_io, sizeof(persis_io), 0)) > 8146 CTL_HA_STATUS_SUCCESS) { 8147 printf("CTL:Persis Out error returned " 8148 "from ctl_ha_msg_send %d\n", 8149 isc_retval); 8150 } 8151 } else { 8152 /* 8153 * sa_res_key is not the res holder just 8154 * remove registrants 8155 */ 8156 int found=0; 8157 8158 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8159 if (sa_res_key != lun->pr_keys[i]) 8160 continue; 8161 8162 found = 1; 8163 lun->pr_keys[i] = 0; 8164 lun->pr_key_count--; 8165 8166 if (!persis_offset 8167 && i < CTL_MAX_INITIATORS) 8168 lun->pending_ua[i] |= 8169 CTL_UA_REG_PREEMPT; 8170 else if (persis_offset 8171 && i >= persis_offset) 8172 lun->pending_ua[i-persis_offset] |= 8173 CTL_UA_REG_PREEMPT; 8174 } 8175 8176 if (!found) { 8177 mtx_unlock(&lun->lun_lock); 8178 free(ctsio->kern_data_ptr, M_CTL); 8179 ctl_set_reservation_conflict(ctsio); 8180 ctl_done((union ctl_io *)ctsio); 8181 return (1); 8182 } 8183 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8184 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8185 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8186 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8187 persis_io.pr.pr_info.res_type = type; 8188 memcpy(persis_io.pr.pr_info.sa_res_key, 8189 param->serv_act_res_key, 8190 sizeof(param->serv_act_res_key)); 8191 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8192 &persis_io, sizeof(persis_io), 0)) > 8193 CTL_HA_STATUS_SUCCESS) { 8194 printf("CTL:Persis Out error returned " 8195 "from ctl_ha_msg_send %d\n", 8196 isc_retval); 8197 } 8198 } 8199 } 8200 8201 lun->PRGeneration++; 8202 mtx_unlock(&lun->lun_lock); 8203 8204 return (retval); 8205 } 8206 8207 static void 8208 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8209 { 8210 uint64_t sa_res_key; 8211 int i; 8212 8213 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8214 8215 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8216 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8217 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) { 8218 if (sa_res_key == 0) { 8219 /* 8220 * Unregister everybody else and build UA for 8221 * them 8222 */ 8223 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8224 if (i == msg->pr.pr_info.residx || 8225 lun->pr_keys[i] == 0) 8226 continue; 8227 8228 if (!persis_offset 8229 && i < CTL_MAX_INITIATORS) 8230 lun->pending_ua[i] |= 8231 CTL_UA_REG_PREEMPT; 8232 else if (persis_offset && i >= persis_offset) 8233 lun->pending_ua[i - persis_offset] |= 8234 CTL_UA_REG_PREEMPT; 8235 lun->pr_keys[i] = 0; 8236 } 8237 8238 lun->pr_key_count = 1; 8239 lun->res_type = msg->pr.pr_info.res_type; 8240 if (lun->res_type != SPR_TYPE_WR_EX_AR 8241 && lun->res_type != SPR_TYPE_EX_AC_AR) 8242 lun->pr_res_idx = msg->pr.pr_info.residx; 8243 } else { 8244 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8245 if (sa_res_key == lun->pr_keys[i]) 8246 continue; 8247 8248 lun->pr_keys[i] = 0; 8249 lun->pr_key_count--; 8250 8251 if (!persis_offset 8252 && i < persis_offset) 8253 lun->pending_ua[i] |= 8254 CTL_UA_REG_PREEMPT; 8255 else if (persis_offset 8256 && i >= persis_offset) 8257 lun->pending_ua[i - persis_offset] |= 8258 CTL_UA_REG_PREEMPT; 8259 } 8260 } 8261 } else { 8262 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8263 if (i == msg->pr.pr_info.residx || 8264 lun->pr_keys[i] == 0) 8265 continue; 8266 8267 if (sa_res_key == lun->pr_keys[i]) { 8268 lun->pr_keys[i] = 0; 8269 lun->pr_key_count--; 8270 if (!persis_offset 8271 && i < CTL_MAX_INITIATORS) 8272 lun->pending_ua[i] |= 8273 CTL_UA_REG_PREEMPT; 8274 else if (persis_offset 8275 && i >= persis_offset) 8276 lun->pending_ua[i - persis_offset] |= 8277 CTL_UA_REG_PREEMPT; 8278 } else if (msg->pr.pr_info.res_type != lun->res_type 8279 && (lun->res_type == SPR_TYPE_WR_EX_RO 8280 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8281 if (!persis_offset 8282 && i < persis_offset) 8283 lun->pending_ua[i] |= 8284 CTL_UA_RES_RELEASE; 8285 else if (persis_offset 8286 && i >= persis_offset) 8287 lun->pending_ua[i - persis_offset] |= 8288 CTL_UA_RES_RELEASE; 8289 } 8290 } 8291 lun->res_type = msg->pr.pr_info.res_type; 8292 if (lun->res_type != SPR_TYPE_WR_EX_AR 8293 && lun->res_type != SPR_TYPE_EX_AC_AR) 8294 lun->pr_res_idx = msg->pr.pr_info.residx; 8295 else 8296 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8297 } 8298 lun->PRGeneration++; 8299 8300 } 8301 8302 8303 int 8304 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8305 { 8306 int retval; 8307 int isc_retval; 8308 u_int32_t param_len; 8309 struct scsi_per_res_out *cdb; 8310 struct ctl_lun *lun; 8311 struct scsi_per_res_out_parms* param; 8312 struct ctl_softc *softc; 8313 uint32_t residx; 8314 uint64_t res_key, sa_res_key; 8315 uint8_t type; 8316 union ctl_ha_msg persis_io; 8317 int i; 8318 8319 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8320 8321 retval = CTL_RETVAL_COMPLETE; 8322 8323 softc = control_softc; 8324 8325 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8326 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8327 8328 /* 8329 * We only support whole-LUN scope. The scope & type are ignored for 8330 * register, register and ignore existing key and clear. 8331 * We sometimes ignore scope and type on preempts too!! 8332 * Verify reservation type here as well. 8333 */ 8334 type = cdb->scope_type & SPR_TYPE_MASK; 8335 if ((cdb->action == SPRO_RESERVE) 8336 || (cdb->action == SPRO_RELEASE)) { 8337 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8338 ctl_set_invalid_field(/*ctsio*/ ctsio, 8339 /*sks_valid*/ 1, 8340 /*command*/ 1, 8341 /*field*/ 2, 8342 /*bit_valid*/ 1, 8343 /*bit*/ 4); 8344 ctl_done((union ctl_io *)ctsio); 8345 return (CTL_RETVAL_COMPLETE); 8346 } 8347 8348 if (type>8 || type==2 || type==4 || type==0) { 8349 ctl_set_invalid_field(/*ctsio*/ ctsio, 8350 /*sks_valid*/ 1, 8351 /*command*/ 1, 8352 /*field*/ 2, 8353 /*bit_valid*/ 1, 8354 /*bit*/ 0); 8355 ctl_done((union ctl_io *)ctsio); 8356 return (CTL_RETVAL_COMPLETE); 8357 } 8358 } 8359 8360 param_len = scsi_4btoul(cdb->length); 8361 8362 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8363 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8364 ctsio->kern_data_len = param_len; 8365 ctsio->kern_total_len = param_len; 8366 ctsio->kern_data_resid = 0; 8367 ctsio->kern_rel_offset = 0; 8368 ctsio->kern_sg_entries = 0; 8369 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8370 ctsio->be_move_done = ctl_config_move_done; 8371 ctl_datamove((union ctl_io *)ctsio); 8372 8373 return (CTL_RETVAL_COMPLETE); 8374 } 8375 8376 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8377 8378 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8379 res_key = scsi_8btou64(param->res_key.key); 8380 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8381 8382 /* 8383 * Validate the reservation key here except for SPRO_REG_IGNO 8384 * This must be done for all other service actions 8385 */ 8386 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8387 mtx_lock(&lun->lun_lock); 8388 if (lun->pr_keys[residx] != 0) { 8389 if (res_key != lun->pr_keys[residx]) { 8390 /* 8391 * The current key passed in doesn't match 8392 * the one the initiator previously 8393 * registered. 8394 */ 8395 mtx_unlock(&lun->lun_lock); 8396 free(ctsio->kern_data_ptr, M_CTL); 8397 ctl_set_reservation_conflict(ctsio); 8398 ctl_done((union ctl_io *)ctsio); 8399 return (CTL_RETVAL_COMPLETE); 8400 } 8401 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8402 /* 8403 * We are not registered 8404 */ 8405 mtx_unlock(&lun->lun_lock); 8406 free(ctsio->kern_data_ptr, M_CTL); 8407 ctl_set_reservation_conflict(ctsio); 8408 ctl_done((union ctl_io *)ctsio); 8409 return (CTL_RETVAL_COMPLETE); 8410 } else if (res_key != 0) { 8411 /* 8412 * We are not registered and trying to register but 8413 * the register key isn't zero. 8414 */ 8415 mtx_unlock(&lun->lun_lock); 8416 free(ctsio->kern_data_ptr, M_CTL); 8417 ctl_set_reservation_conflict(ctsio); 8418 ctl_done((union ctl_io *)ctsio); 8419 return (CTL_RETVAL_COMPLETE); 8420 } 8421 mtx_unlock(&lun->lun_lock); 8422 } 8423 8424 switch (cdb->action & SPRO_ACTION_MASK) { 8425 case SPRO_REGISTER: 8426 case SPRO_REG_IGNO: { 8427 8428 #if 0 8429 printf("Registration received\n"); 8430 #endif 8431 8432 /* 8433 * We don't support any of these options, as we report in 8434 * the read capabilities request (see 8435 * ctl_persistent_reserve_in(), above). 8436 */ 8437 if ((param->flags & SPR_SPEC_I_PT) 8438 || (param->flags & SPR_ALL_TG_PT) 8439 || (param->flags & SPR_APTPL)) { 8440 int bit_ptr; 8441 8442 if (param->flags & SPR_APTPL) 8443 bit_ptr = 0; 8444 else if (param->flags & SPR_ALL_TG_PT) 8445 bit_ptr = 2; 8446 else /* SPR_SPEC_I_PT */ 8447 bit_ptr = 3; 8448 8449 free(ctsio->kern_data_ptr, M_CTL); 8450 ctl_set_invalid_field(ctsio, 8451 /*sks_valid*/ 1, 8452 /*command*/ 0, 8453 /*field*/ 20, 8454 /*bit_valid*/ 1, 8455 /*bit*/ bit_ptr); 8456 ctl_done((union ctl_io *)ctsio); 8457 return (CTL_RETVAL_COMPLETE); 8458 } 8459 8460 mtx_lock(&lun->lun_lock); 8461 8462 /* 8463 * The initiator wants to clear the 8464 * key/unregister. 8465 */ 8466 if (sa_res_key == 0) { 8467 if ((res_key == 0 8468 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8469 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8470 && lun->pr_keys[residx] == 0)) { 8471 mtx_unlock(&lun->lun_lock); 8472 goto done; 8473 } 8474 8475 lun->pr_keys[residx] = 0; 8476 lun->pr_key_count--; 8477 8478 if (residx == lun->pr_res_idx) { 8479 lun->flags &= ~CTL_LUN_PR_RESERVED; 8480 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8481 8482 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8483 || lun->res_type == SPR_TYPE_EX_AC_RO) 8484 && lun->pr_key_count) { 8485 /* 8486 * If the reservation is a registrants 8487 * only type we need to generate a UA 8488 * for other registered inits. The 8489 * sense code should be RESERVATIONS 8490 * RELEASED 8491 */ 8492 8493 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8494 if (lun->pr_keys[ 8495 i + persis_offset] == 0) 8496 continue; 8497 lun->pending_ua[i] |= 8498 CTL_UA_RES_RELEASE; 8499 } 8500 } 8501 lun->res_type = 0; 8502 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8503 if (lun->pr_key_count==0) { 8504 lun->flags &= ~CTL_LUN_PR_RESERVED; 8505 lun->res_type = 0; 8506 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8507 } 8508 } 8509 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8510 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8511 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8512 persis_io.pr.pr_info.residx = residx; 8513 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8514 &persis_io, sizeof(persis_io), 0 )) > 8515 CTL_HA_STATUS_SUCCESS) { 8516 printf("CTL:Persis Out error returned from " 8517 "ctl_ha_msg_send %d\n", isc_retval); 8518 } 8519 } else /* sa_res_key != 0 */ { 8520 8521 /* 8522 * If we aren't registered currently then increment 8523 * the key count and set the registered flag. 8524 */ 8525 if (lun->pr_keys[residx] == 0) 8526 lun->pr_key_count++; 8527 lun->pr_keys[residx] = sa_res_key; 8528 8529 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8530 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8531 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8532 persis_io.pr.pr_info.residx = residx; 8533 memcpy(persis_io.pr.pr_info.sa_res_key, 8534 param->serv_act_res_key, 8535 sizeof(param->serv_act_res_key)); 8536 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8537 &persis_io, sizeof(persis_io), 0)) > 8538 CTL_HA_STATUS_SUCCESS) { 8539 printf("CTL:Persis Out error returned from " 8540 "ctl_ha_msg_send %d\n", isc_retval); 8541 } 8542 } 8543 lun->PRGeneration++; 8544 mtx_unlock(&lun->lun_lock); 8545 8546 break; 8547 } 8548 case SPRO_RESERVE: 8549 #if 0 8550 printf("Reserve executed type %d\n", type); 8551 #endif 8552 mtx_lock(&lun->lun_lock); 8553 if (lun->flags & CTL_LUN_PR_RESERVED) { 8554 /* 8555 * if this isn't the reservation holder and it's 8556 * not a "all registrants" type or if the type is 8557 * different then we have a conflict 8558 */ 8559 if ((lun->pr_res_idx != residx 8560 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8561 || lun->res_type != type) { 8562 mtx_unlock(&lun->lun_lock); 8563 free(ctsio->kern_data_ptr, M_CTL); 8564 ctl_set_reservation_conflict(ctsio); 8565 ctl_done((union ctl_io *)ctsio); 8566 return (CTL_RETVAL_COMPLETE); 8567 } 8568 mtx_unlock(&lun->lun_lock); 8569 } else /* create a reservation */ { 8570 /* 8571 * If it's not an "all registrants" type record 8572 * reservation holder 8573 */ 8574 if (type != SPR_TYPE_WR_EX_AR 8575 && type != SPR_TYPE_EX_AC_AR) 8576 lun->pr_res_idx = residx; /* Res holder */ 8577 else 8578 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8579 8580 lun->flags |= CTL_LUN_PR_RESERVED; 8581 lun->res_type = type; 8582 8583 mtx_unlock(&lun->lun_lock); 8584 8585 /* send msg to other side */ 8586 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8587 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8588 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8589 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8590 persis_io.pr.pr_info.res_type = type; 8591 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8592 &persis_io, sizeof(persis_io), 0)) > 8593 CTL_HA_STATUS_SUCCESS) { 8594 printf("CTL:Persis Out error returned from " 8595 "ctl_ha_msg_send %d\n", isc_retval); 8596 } 8597 } 8598 break; 8599 8600 case SPRO_RELEASE: 8601 mtx_lock(&lun->lun_lock); 8602 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8603 /* No reservation exists return good status */ 8604 mtx_unlock(&lun->lun_lock); 8605 goto done; 8606 } 8607 /* 8608 * Is this nexus a reservation holder? 8609 */ 8610 if (lun->pr_res_idx != residx 8611 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8612 /* 8613 * not a res holder return good status but 8614 * do nothing 8615 */ 8616 mtx_unlock(&lun->lun_lock); 8617 goto done; 8618 } 8619 8620 if (lun->res_type != type) { 8621 mtx_unlock(&lun->lun_lock); 8622 free(ctsio->kern_data_ptr, M_CTL); 8623 ctl_set_illegal_pr_release(ctsio); 8624 ctl_done((union ctl_io *)ctsio); 8625 return (CTL_RETVAL_COMPLETE); 8626 } 8627 8628 /* okay to release */ 8629 lun->flags &= ~CTL_LUN_PR_RESERVED; 8630 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8631 lun->res_type = 0; 8632 8633 /* 8634 * if this isn't an exclusive access 8635 * res generate UA for all other 8636 * registrants. 8637 */ 8638 if (type != SPR_TYPE_EX_AC 8639 && type != SPR_TYPE_WR_EX) { 8640 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8641 if (i == residx || 8642 lun->pr_keys[i + persis_offset] == 0) 8643 continue; 8644 lun->pending_ua[i] |= CTL_UA_RES_RELEASE; 8645 } 8646 } 8647 mtx_unlock(&lun->lun_lock); 8648 /* Send msg to other side */ 8649 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8650 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8651 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8652 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8653 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8654 printf("CTL:Persis Out error returned from " 8655 "ctl_ha_msg_send %d\n", isc_retval); 8656 } 8657 break; 8658 8659 case SPRO_CLEAR: 8660 /* send msg to other side */ 8661 8662 mtx_lock(&lun->lun_lock); 8663 lun->flags &= ~CTL_LUN_PR_RESERVED; 8664 lun->res_type = 0; 8665 lun->pr_key_count = 0; 8666 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8667 8668 lun->pr_keys[residx] = 0; 8669 8670 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8671 if (lun->pr_keys[i] != 0) { 8672 if (!persis_offset && i < CTL_MAX_INITIATORS) 8673 lun->pending_ua[i] |= 8674 CTL_UA_RES_PREEMPT; 8675 else if (persis_offset && i >= persis_offset) 8676 lun->pending_ua[i-persis_offset] |= 8677 CTL_UA_RES_PREEMPT; 8678 8679 lun->pr_keys[i] = 0; 8680 } 8681 lun->PRGeneration++; 8682 mtx_unlock(&lun->lun_lock); 8683 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8684 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8685 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8686 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8687 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8688 printf("CTL:Persis Out error returned from " 8689 "ctl_ha_msg_send %d\n", isc_retval); 8690 } 8691 break; 8692 8693 case SPRO_PREEMPT: { 8694 int nretval; 8695 8696 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8697 residx, ctsio, cdb, param); 8698 if (nretval != 0) 8699 return (CTL_RETVAL_COMPLETE); 8700 break; 8701 } 8702 default: 8703 panic("Invalid PR type %x", cdb->action); 8704 } 8705 8706 done: 8707 free(ctsio->kern_data_ptr, M_CTL); 8708 ctl_set_success(ctsio); 8709 ctl_done((union ctl_io *)ctsio); 8710 8711 return (retval); 8712 } 8713 8714 /* 8715 * This routine is for handling a message from the other SC pertaining to 8716 * persistent reserve out. All the error checking will have been done 8717 * so only perorming the action need be done here to keep the two 8718 * in sync. 8719 */ 8720 static void 8721 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8722 { 8723 struct ctl_lun *lun; 8724 struct ctl_softc *softc; 8725 int i; 8726 uint32_t targ_lun; 8727 8728 softc = control_softc; 8729 8730 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8731 lun = softc->ctl_luns[targ_lun]; 8732 mtx_lock(&lun->lun_lock); 8733 switch(msg->pr.pr_info.action) { 8734 case CTL_PR_REG_KEY: 8735 if (lun->pr_keys[msg->pr.pr_info.residx] == 0) 8736 lun->pr_key_count++; 8737 lun->pr_keys[msg->pr.pr_info.residx] = 8738 scsi_8btou64(msg->pr.pr_info.sa_res_key); 8739 lun->PRGeneration++; 8740 break; 8741 8742 case CTL_PR_UNREG_KEY: 8743 lun->pr_keys[msg->pr.pr_info.residx] = 0; 8744 lun->pr_key_count--; 8745 8746 /* XXX Need to see if the reservation has been released */ 8747 /* if so do we need to generate UA? */ 8748 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8749 lun->flags &= ~CTL_LUN_PR_RESERVED; 8750 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8751 8752 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8753 || lun->res_type == SPR_TYPE_EX_AC_RO) 8754 && lun->pr_key_count) { 8755 /* 8756 * If the reservation is a registrants 8757 * only type we need to generate a UA 8758 * for other registered inits. The 8759 * sense code should be RESERVATIONS 8760 * RELEASED 8761 */ 8762 8763 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8764 if (lun->pr_keys[i+ 8765 persis_offset] == 0) 8766 continue; 8767 8768 lun->pending_ua[i] |= 8769 CTL_UA_RES_RELEASE; 8770 } 8771 } 8772 lun->res_type = 0; 8773 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8774 if (lun->pr_key_count==0) { 8775 lun->flags &= ~CTL_LUN_PR_RESERVED; 8776 lun->res_type = 0; 8777 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8778 } 8779 } 8780 lun->PRGeneration++; 8781 break; 8782 8783 case CTL_PR_RESERVE: 8784 lun->flags |= CTL_LUN_PR_RESERVED; 8785 lun->res_type = msg->pr.pr_info.res_type; 8786 lun->pr_res_idx = msg->pr.pr_info.residx; 8787 8788 break; 8789 8790 case CTL_PR_RELEASE: 8791 /* 8792 * if this isn't an exclusive access res generate UA for all 8793 * other registrants. 8794 */ 8795 if (lun->res_type != SPR_TYPE_EX_AC 8796 && lun->res_type != SPR_TYPE_WR_EX) { 8797 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8798 if (lun->pr_keys[i+persis_offset] != 0) 8799 lun->pending_ua[i] |= 8800 CTL_UA_RES_RELEASE; 8801 } 8802 8803 lun->flags &= ~CTL_LUN_PR_RESERVED; 8804 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8805 lun->res_type = 0; 8806 break; 8807 8808 case CTL_PR_PREEMPT: 8809 ctl_pro_preempt_other(lun, msg); 8810 break; 8811 case CTL_PR_CLEAR: 8812 lun->flags &= ~CTL_LUN_PR_RESERVED; 8813 lun->res_type = 0; 8814 lun->pr_key_count = 0; 8815 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8816 8817 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8818 if (lun->pr_keys[i] == 0) 8819 continue; 8820 if (!persis_offset 8821 && i < CTL_MAX_INITIATORS) 8822 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 8823 else if (persis_offset 8824 && i >= persis_offset) 8825 lun->pending_ua[i-persis_offset] |= 8826 CTL_UA_RES_PREEMPT; 8827 lun->pr_keys[i] = 0; 8828 } 8829 lun->PRGeneration++; 8830 break; 8831 } 8832 8833 mtx_unlock(&lun->lun_lock); 8834 } 8835 8836 int 8837 ctl_read_write(struct ctl_scsiio *ctsio) 8838 { 8839 struct ctl_lun *lun; 8840 struct ctl_lba_len_flags *lbalen; 8841 uint64_t lba; 8842 uint32_t num_blocks; 8843 int flags, retval; 8844 int isread; 8845 8846 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8847 8848 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8849 8850 flags = 0; 8851 retval = CTL_RETVAL_COMPLETE; 8852 8853 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8854 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8855 switch (ctsio->cdb[0]) { 8856 case READ_6: 8857 case WRITE_6: { 8858 struct scsi_rw_6 *cdb; 8859 8860 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8861 8862 lba = scsi_3btoul(cdb->addr); 8863 /* only 5 bits are valid in the most significant address byte */ 8864 lba &= 0x1fffff; 8865 num_blocks = cdb->length; 8866 /* 8867 * This is correct according to SBC-2. 8868 */ 8869 if (num_blocks == 0) 8870 num_blocks = 256; 8871 break; 8872 } 8873 case READ_10: 8874 case WRITE_10: { 8875 struct scsi_rw_10 *cdb; 8876 8877 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8878 if (cdb->byte2 & SRW10_FUA) 8879 flags |= CTL_LLF_FUA; 8880 if (cdb->byte2 & SRW10_DPO) 8881 flags |= CTL_LLF_DPO; 8882 lba = scsi_4btoul(cdb->addr); 8883 num_blocks = scsi_2btoul(cdb->length); 8884 break; 8885 } 8886 case WRITE_VERIFY_10: { 8887 struct scsi_write_verify_10 *cdb; 8888 8889 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8890 flags |= CTL_LLF_FUA; 8891 if (cdb->byte2 & SWV_DPO) 8892 flags |= CTL_LLF_DPO; 8893 lba = scsi_4btoul(cdb->addr); 8894 num_blocks = scsi_2btoul(cdb->length); 8895 break; 8896 } 8897 case READ_12: 8898 case WRITE_12: { 8899 struct scsi_rw_12 *cdb; 8900 8901 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8902 if (cdb->byte2 & SRW12_FUA) 8903 flags |= CTL_LLF_FUA; 8904 if (cdb->byte2 & SRW12_DPO) 8905 flags |= CTL_LLF_DPO; 8906 lba = scsi_4btoul(cdb->addr); 8907 num_blocks = scsi_4btoul(cdb->length); 8908 break; 8909 } 8910 case WRITE_VERIFY_12: { 8911 struct scsi_write_verify_12 *cdb; 8912 8913 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8914 flags |= CTL_LLF_FUA; 8915 if (cdb->byte2 & SWV_DPO) 8916 flags |= CTL_LLF_DPO; 8917 lba = scsi_4btoul(cdb->addr); 8918 num_blocks = scsi_4btoul(cdb->length); 8919 break; 8920 } 8921 case READ_16: 8922 case WRITE_16: { 8923 struct scsi_rw_16 *cdb; 8924 8925 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8926 if (cdb->byte2 & SRW12_FUA) 8927 flags |= CTL_LLF_FUA; 8928 if (cdb->byte2 & SRW12_DPO) 8929 flags |= CTL_LLF_DPO; 8930 lba = scsi_8btou64(cdb->addr); 8931 num_blocks = scsi_4btoul(cdb->length); 8932 break; 8933 } 8934 case WRITE_ATOMIC_16: { 8935 struct scsi_rw_16 *cdb; 8936 8937 if (lun->be_lun->atomicblock == 0) { 8938 ctl_set_invalid_opcode(ctsio); 8939 ctl_done((union ctl_io *)ctsio); 8940 return (CTL_RETVAL_COMPLETE); 8941 } 8942 8943 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8944 if (cdb->byte2 & SRW12_FUA) 8945 flags |= CTL_LLF_FUA; 8946 if (cdb->byte2 & SRW12_DPO) 8947 flags |= CTL_LLF_DPO; 8948 lba = scsi_8btou64(cdb->addr); 8949 num_blocks = scsi_4btoul(cdb->length); 8950 if (num_blocks > lun->be_lun->atomicblock) { 8951 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8952 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8953 /*bit*/ 0); 8954 ctl_done((union ctl_io *)ctsio); 8955 return (CTL_RETVAL_COMPLETE); 8956 } 8957 break; 8958 } 8959 case WRITE_VERIFY_16: { 8960 struct scsi_write_verify_16 *cdb; 8961 8962 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8963 flags |= CTL_LLF_FUA; 8964 if (cdb->byte2 & SWV_DPO) 8965 flags |= CTL_LLF_DPO; 8966 lba = scsi_8btou64(cdb->addr); 8967 num_blocks = scsi_4btoul(cdb->length); 8968 break; 8969 } 8970 default: 8971 /* 8972 * We got a command we don't support. This shouldn't 8973 * happen, commands should be filtered out above us. 8974 */ 8975 ctl_set_invalid_opcode(ctsio); 8976 ctl_done((union ctl_io *)ctsio); 8977 8978 return (CTL_RETVAL_COMPLETE); 8979 break; /* NOTREACHED */ 8980 } 8981 8982 /* 8983 * The first check is to make sure we're in bounds, the second 8984 * check is to catch wrap-around problems. If the lba + num blocks 8985 * is less than the lba, then we've wrapped around and the block 8986 * range is invalid anyway. 8987 */ 8988 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8989 || ((lba + num_blocks) < lba)) { 8990 ctl_set_lba_out_of_range(ctsio); 8991 ctl_done((union ctl_io *)ctsio); 8992 return (CTL_RETVAL_COMPLETE); 8993 } 8994 8995 /* 8996 * According to SBC-3, a transfer length of 0 is not an error. 8997 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8998 * translates to 256 blocks for those commands. 8999 */ 9000 if (num_blocks == 0) { 9001 ctl_set_success(ctsio); 9002 ctl_done((union ctl_io *)ctsio); 9003 return (CTL_RETVAL_COMPLETE); 9004 } 9005 9006 /* Set FUA and/or DPO if caches are disabled. */ 9007 if (isread) { 9008 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9009 SCP_RCD) != 0) 9010 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9011 } else { 9012 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9013 SCP_WCE) == 0) 9014 flags |= CTL_LLF_FUA; 9015 } 9016 9017 lbalen = (struct ctl_lba_len_flags *) 9018 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9019 lbalen->lba = lba; 9020 lbalen->len = num_blocks; 9021 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9022 9023 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9024 ctsio->kern_rel_offset = 0; 9025 9026 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9027 9028 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9029 9030 return (retval); 9031 } 9032 9033 static int 9034 ctl_cnw_cont(union ctl_io *io) 9035 { 9036 struct ctl_scsiio *ctsio; 9037 struct ctl_lun *lun; 9038 struct ctl_lba_len_flags *lbalen; 9039 int retval; 9040 9041 ctsio = &io->scsiio; 9042 ctsio->io_hdr.status = CTL_STATUS_NONE; 9043 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9044 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9045 lbalen = (struct ctl_lba_len_flags *) 9046 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9047 lbalen->flags &= ~CTL_LLF_COMPARE; 9048 lbalen->flags |= CTL_LLF_WRITE; 9049 9050 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9051 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9052 return (retval); 9053 } 9054 9055 int 9056 ctl_cnw(struct ctl_scsiio *ctsio) 9057 { 9058 struct ctl_lun *lun; 9059 struct ctl_lba_len_flags *lbalen; 9060 uint64_t lba; 9061 uint32_t num_blocks; 9062 int flags, retval; 9063 9064 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9065 9066 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9067 9068 flags = 0; 9069 retval = CTL_RETVAL_COMPLETE; 9070 9071 switch (ctsio->cdb[0]) { 9072 case COMPARE_AND_WRITE: { 9073 struct scsi_compare_and_write *cdb; 9074 9075 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9076 if (cdb->byte2 & SRW10_FUA) 9077 flags |= CTL_LLF_FUA; 9078 if (cdb->byte2 & SRW10_DPO) 9079 flags |= CTL_LLF_DPO; 9080 lba = scsi_8btou64(cdb->addr); 9081 num_blocks = cdb->length; 9082 break; 9083 } 9084 default: 9085 /* 9086 * We got a command we don't support. This shouldn't 9087 * happen, commands should be filtered out above us. 9088 */ 9089 ctl_set_invalid_opcode(ctsio); 9090 ctl_done((union ctl_io *)ctsio); 9091 9092 return (CTL_RETVAL_COMPLETE); 9093 break; /* NOTREACHED */ 9094 } 9095 9096 /* 9097 * The first check is to make sure we're in bounds, the second 9098 * check is to catch wrap-around problems. If the lba + num blocks 9099 * is less than the lba, then we've wrapped around and the block 9100 * range is invalid anyway. 9101 */ 9102 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9103 || ((lba + num_blocks) < lba)) { 9104 ctl_set_lba_out_of_range(ctsio); 9105 ctl_done((union ctl_io *)ctsio); 9106 return (CTL_RETVAL_COMPLETE); 9107 } 9108 9109 /* 9110 * According to SBC-3, a transfer length of 0 is not an error. 9111 */ 9112 if (num_blocks == 0) { 9113 ctl_set_success(ctsio); 9114 ctl_done((union ctl_io *)ctsio); 9115 return (CTL_RETVAL_COMPLETE); 9116 } 9117 9118 /* Set FUA if write cache is disabled. */ 9119 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9120 SCP_WCE) == 0) 9121 flags |= CTL_LLF_FUA; 9122 9123 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9124 ctsio->kern_rel_offset = 0; 9125 9126 /* 9127 * Set the IO_CONT flag, so that if this I/O gets passed to 9128 * ctl_data_submit_done(), it'll get passed back to 9129 * ctl_ctl_cnw_cont() for further processing. 9130 */ 9131 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9132 ctsio->io_cont = ctl_cnw_cont; 9133 9134 lbalen = (struct ctl_lba_len_flags *) 9135 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9136 lbalen->lba = lba; 9137 lbalen->len = num_blocks; 9138 lbalen->flags = CTL_LLF_COMPARE | flags; 9139 9140 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9141 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9142 return (retval); 9143 } 9144 9145 int 9146 ctl_verify(struct ctl_scsiio *ctsio) 9147 { 9148 struct ctl_lun *lun; 9149 struct ctl_lba_len_flags *lbalen; 9150 uint64_t lba; 9151 uint32_t num_blocks; 9152 int bytchk, flags; 9153 int retval; 9154 9155 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9156 9157 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9158 9159 bytchk = 0; 9160 flags = CTL_LLF_FUA; 9161 retval = CTL_RETVAL_COMPLETE; 9162 9163 switch (ctsio->cdb[0]) { 9164 case VERIFY_10: { 9165 struct scsi_verify_10 *cdb; 9166 9167 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9168 if (cdb->byte2 & SVFY_BYTCHK) 9169 bytchk = 1; 9170 if (cdb->byte2 & SVFY_DPO) 9171 flags |= CTL_LLF_DPO; 9172 lba = scsi_4btoul(cdb->addr); 9173 num_blocks = scsi_2btoul(cdb->length); 9174 break; 9175 } 9176 case VERIFY_12: { 9177 struct scsi_verify_12 *cdb; 9178 9179 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9180 if (cdb->byte2 & SVFY_BYTCHK) 9181 bytchk = 1; 9182 if (cdb->byte2 & SVFY_DPO) 9183 flags |= CTL_LLF_DPO; 9184 lba = scsi_4btoul(cdb->addr); 9185 num_blocks = scsi_4btoul(cdb->length); 9186 break; 9187 } 9188 case VERIFY_16: { 9189 struct scsi_rw_16 *cdb; 9190 9191 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9192 if (cdb->byte2 & SVFY_BYTCHK) 9193 bytchk = 1; 9194 if (cdb->byte2 & SVFY_DPO) 9195 flags |= CTL_LLF_DPO; 9196 lba = scsi_8btou64(cdb->addr); 9197 num_blocks = scsi_4btoul(cdb->length); 9198 break; 9199 } 9200 default: 9201 /* 9202 * We got a command we don't support. This shouldn't 9203 * happen, commands should be filtered out above us. 9204 */ 9205 ctl_set_invalid_opcode(ctsio); 9206 ctl_done((union ctl_io *)ctsio); 9207 return (CTL_RETVAL_COMPLETE); 9208 } 9209 9210 /* 9211 * The first check is to make sure we're in bounds, the second 9212 * check is to catch wrap-around problems. If the lba + num blocks 9213 * is less than the lba, then we've wrapped around and the block 9214 * range is invalid anyway. 9215 */ 9216 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9217 || ((lba + num_blocks) < lba)) { 9218 ctl_set_lba_out_of_range(ctsio); 9219 ctl_done((union ctl_io *)ctsio); 9220 return (CTL_RETVAL_COMPLETE); 9221 } 9222 9223 /* 9224 * According to SBC-3, a transfer length of 0 is not an error. 9225 */ 9226 if (num_blocks == 0) { 9227 ctl_set_success(ctsio); 9228 ctl_done((union ctl_io *)ctsio); 9229 return (CTL_RETVAL_COMPLETE); 9230 } 9231 9232 lbalen = (struct ctl_lba_len_flags *) 9233 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9234 lbalen->lba = lba; 9235 lbalen->len = num_blocks; 9236 if (bytchk) { 9237 lbalen->flags = CTL_LLF_COMPARE | flags; 9238 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9239 } else { 9240 lbalen->flags = CTL_LLF_VERIFY | flags; 9241 ctsio->kern_total_len = 0; 9242 } 9243 ctsio->kern_rel_offset = 0; 9244 9245 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9246 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9247 return (retval); 9248 } 9249 9250 int 9251 ctl_report_luns(struct ctl_scsiio *ctsio) 9252 { 9253 struct scsi_report_luns *cdb; 9254 struct scsi_report_luns_data *lun_data; 9255 struct ctl_lun *lun, *request_lun; 9256 int num_luns, retval; 9257 uint32_t alloc_len, lun_datalen; 9258 int num_filled, well_known; 9259 uint32_t initidx, targ_lun_id, lun_id; 9260 9261 retval = CTL_RETVAL_COMPLETE; 9262 well_known = 0; 9263 9264 cdb = (struct scsi_report_luns *)ctsio->cdb; 9265 9266 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9267 9268 mtx_lock(&control_softc->ctl_lock); 9269 num_luns = control_softc->num_luns; 9270 mtx_unlock(&control_softc->ctl_lock); 9271 9272 switch (cdb->select_report) { 9273 case RPL_REPORT_DEFAULT: 9274 case RPL_REPORT_ALL: 9275 break; 9276 case RPL_REPORT_WELLKNOWN: 9277 well_known = 1; 9278 num_luns = 0; 9279 break; 9280 default: 9281 ctl_set_invalid_field(ctsio, 9282 /*sks_valid*/ 1, 9283 /*command*/ 1, 9284 /*field*/ 2, 9285 /*bit_valid*/ 0, 9286 /*bit*/ 0); 9287 ctl_done((union ctl_io *)ctsio); 9288 return (retval); 9289 break; /* NOTREACHED */ 9290 } 9291 9292 alloc_len = scsi_4btoul(cdb->length); 9293 /* 9294 * The initiator has to allocate at least 16 bytes for this request, 9295 * so he can at least get the header and the first LUN. Otherwise 9296 * we reject the request (per SPC-3 rev 14, section 6.21). 9297 */ 9298 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9299 sizeof(struct scsi_report_luns_lundata))) { 9300 ctl_set_invalid_field(ctsio, 9301 /*sks_valid*/ 1, 9302 /*command*/ 1, 9303 /*field*/ 6, 9304 /*bit_valid*/ 0, 9305 /*bit*/ 0); 9306 ctl_done((union ctl_io *)ctsio); 9307 return (retval); 9308 } 9309 9310 request_lun = (struct ctl_lun *) 9311 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9312 9313 lun_datalen = sizeof(*lun_data) + 9314 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9315 9316 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9317 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9318 ctsio->kern_sg_entries = 0; 9319 9320 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9321 9322 mtx_lock(&control_softc->ctl_lock); 9323 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9324 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9325 if (lun_id >= CTL_MAX_LUNS) 9326 continue; 9327 lun = control_softc->ctl_luns[lun_id]; 9328 if (lun == NULL) 9329 continue; 9330 9331 if (targ_lun_id <= 0xff) { 9332 /* 9333 * Peripheral addressing method, bus number 0. 9334 */ 9335 lun_data->luns[num_filled].lundata[0] = 9336 RPL_LUNDATA_ATYP_PERIPH; 9337 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9338 num_filled++; 9339 } else if (targ_lun_id <= 0x3fff) { 9340 /* 9341 * Flat addressing method. 9342 */ 9343 lun_data->luns[num_filled].lundata[0] = 9344 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9345 lun_data->luns[num_filled].lundata[1] = 9346 (targ_lun_id & 0xff); 9347 num_filled++; 9348 } else if (targ_lun_id <= 0xffffff) { 9349 /* 9350 * Extended flat addressing method. 9351 */ 9352 lun_data->luns[num_filled].lundata[0] = 9353 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9354 scsi_ulto3b(targ_lun_id, 9355 &lun_data->luns[num_filled].lundata[1]); 9356 num_filled++; 9357 } else { 9358 printf("ctl_report_luns: bogus LUN number %jd, " 9359 "skipping\n", (intmax_t)targ_lun_id); 9360 } 9361 /* 9362 * According to SPC-3, rev 14 section 6.21: 9363 * 9364 * "The execution of a REPORT LUNS command to any valid and 9365 * installed logical unit shall clear the REPORTED LUNS DATA 9366 * HAS CHANGED unit attention condition for all logical 9367 * units of that target with respect to the requesting 9368 * initiator. A valid and installed logical unit is one 9369 * having a PERIPHERAL QUALIFIER of 000b in the standard 9370 * INQUIRY data (see 6.4.2)." 9371 * 9372 * If request_lun is NULL, the LUN this report luns command 9373 * was issued to is either disabled or doesn't exist. In that 9374 * case, we shouldn't clear any pending lun change unit 9375 * attention. 9376 */ 9377 if (request_lun != NULL) { 9378 mtx_lock(&lun->lun_lock); 9379 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9380 mtx_unlock(&lun->lun_lock); 9381 } 9382 } 9383 mtx_unlock(&control_softc->ctl_lock); 9384 9385 /* 9386 * It's quite possible that we've returned fewer LUNs than we allocated 9387 * space for. Trim it. 9388 */ 9389 lun_datalen = sizeof(*lun_data) + 9390 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9391 9392 if (lun_datalen < alloc_len) { 9393 ctsio->residual = alloc_len - lun_datalen; 9394 ctsio->kern_data_len = lun_datalen; 9395 ctsio->kern_total_len = lun_datalen; 9396 } else { 9397 ctsio->residual = 0; 9398 ctsio->kern_data_len = alloc_len; 9399 ctsio->kern_total_len = alloc_len; 9400 } 9401 ctsio->kern_data_resid = 0; 9402 ctsio->kern_rel_offset = 0; 9403 ctsio->kern_sg_entries = 0; 9404 9405 /* 9406 * We set this to the actual data length, regardless of how much 9407 * space we actually have to return results. If the user looks at 9408 * this value, he'll know whether or not he allocated enough space 9409 * and reissue the command if necessary. We don't support well 9410 * known logical units, so if the user asks for that, return none. 9411 */ 9412 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9413 9414 /* 9415 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9416 * this request. 9417 */ 9418 ctsio->scsi_status = SCSI_STATUS_OK; 9419 9420 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9421 ctsio->be_move_done = ctl_config_move_done; 9422 ctl_datamove((union ctl_io *)ctsio); 9423 9424 return (retval); 9425 } 9426 9427 int 9428 ctl_request_sense(struct ctl_scsiio *ctsio) 9429 { 9430 struct scsi_request_sense *cdb; 9431 struct scsi_sense_data *sense_ptr; 9432 struct ctl_lun *lun; 9433 uint32_t initidx; 9434 int have_error; 9435 scsi_sense_data_type sense_format; 9436 9437 cdb = (struct scsi_request_sense *)ctsio->cdb; 9438 9439 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9440 9441 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9442 9443 /* 9444 * Determine which sense format the user wants. 9445 */ 9446 if (cdb->byte2 & SRS_DESC) 9447 sense_format = SSD_TYPE_DESC; 9448 else 9449 sense_format = SSD_TYPE_FIXED; 9450 9451 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9452 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9453 ctsio->kern_sg_entries = 0; 9454 9455 /* 9456 * struct scsi_sense_data, which is currently set to 256 bytes, is 9457 * larger than the largest allowed value for the length field in the 9458 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9459 */ 9460 ctsio->residual = 0; 9461 ctsio->kern_data_len = cdb->length; 9462 ctsio->kern_total_len = cdb->length; 9463 9464 ctsio->kern_data_resid = 0; 9465 ctsio->kern_rel_offset = 0; 9466 ctsio->kern_sg_entries = 0; 9467 9468 /* 9469 * If we don't have a LUN, we don't have any pending sense. 9470 */ 9471 if (lun == NULL) 9472 goto no_sense; 9473 9474 have_error = 0; 9475 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9476 /* 9477 * Check for pending sense, and then for pending unit attentions. 9478 * Pending sense gets returned first, then pending unit attentions. 9479 */ 9480 mtx_lock(&lun->lun_lock); 9481 #ifdef CTL_WITH_CA 9482 if (ctl_is_set(lun->have_ca, initidx)) { 9483 scsi_sense_data_type stored_format; 9484 9485 /* 9486 * Check to see which sense format was used for the stored 9487 * sense data. 9488 */ 9489 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9490 9491 /* 9492 * If the user requested a different sense format than the 9493 * one we stored, then we need to convert it to the other 9494 * format. If we're going from descriptor to fixed format 9495 * sense data, we may lose things in translation, depending 9496 * on what options were used. 9497 * 9498 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9499 * for some reason we'll just copy it out as-is. 9500 */ 9501 if ((stored_format == SSD_TYPE_FIXED) 9502 && (sense_format == SSD_TYPE_DESC)) 9503 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9504 &lun->pending_sense[initidx], 9505 (struct scsi_sense_data_desc *)sense_ptr); 9506 else if ((stored_format == SSD_TYPE_DESC) 9507 && (sense_format == SSD_TYPE_FIXED)) 9508 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9509 &lun->pending_sense[initidx], 9510 (struct scsi_sense_data_fixed *)sense_ptr); 9511 else 9512 memcpy(sense_ptr, &lun->pending_sense[initidx], 9513 ctl_min(sizeof(*sense_ptr), 9514 sizeof(lun->pending_sense[initidx]))); 9515 9516 ctl_clear_mask(lun->have_ca, initidx); 9517 have_error = 1; 9518 } else 9519 #endif 9520 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9521 ctl_ua_type ua_type; 9522 9523 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9524 sense_ptr, sense_format); 9525 if (ua_type != CTL_UA_NONE) 9526 have_error = 1; 9527 } 9528 mtx_unlock(&lun->lun_lock); 9529 9530 /* 9531 * We already have a pending error, return it. 9532 */ 9533 if (have_error != 0) { 9534 /* 9535 * We report the SCSI status as OK, since the status of the 9536 * request sense command itself is OK. 9537 */ 9538 ctsio->scsi_status = SCSI_STATUS_OK; 9539 9540 /* 9541 * We report 0 for the sense length, because we aren't doing 9542 * autosense in this case. We're reporting sense as 9543 * parameter data. 9544 */ 9545 ctsio->sense_len = 0; 9546 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9547 ctsio->be_move_done = ctl_config_move_done; 9548 ctl_datamove((union ctl_io *)ctsio); 9549 9550 return (CTL_RETVAL_COMPLETE); 9551 } 9552 9553 no_sense: 9554 9555 /* 9556 * No sense information to report, so we report that everything is 9557 * okay. 9558 */ 9559 ctl_set_sense_data(sense_ptr, 9560 lun, 9561 sense_format, 9562 /*current_error*/ 1, 9563 /*sense_key*/ SSD_KEY_NO_SENSE, 9564 /*asc*/ 0x00, 9565 /*ascq*/ 0x00, 9566 SSD_ELEM_NONE); 9567 9568 ctsio->scsi_status = SCSI_STATUS_OK; 9569 9570 /* 9571 * We report 0 for the sense length, because we aren't doing 9572 * autosense in this case. We're reporting sense as parameter data. 9573 */ 9574 ctsio->sense_len = 0; 9575 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9576 ctsio->be_move_done = ctl_config_move_done; 9577 ctl_datamove((union ctl_io *)ctsio); 9578 9579 return (CTL_RETVAL_COMPLETE); 9580 } 9581 9582 int 9583 ctl_tur(struct ctl_scsiio *ctsio) 9584 { 9585 struct ctl_lun *lun; 9586 9587 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9588 9589 CTL_DEBUG_PRINT(("ctl_tur\n")); 9590 9591 if (lun == NULL) 9592 return (EINVAL); 9593 9594 ctsio->scsi_status = SCSI_STATUS_OK; 9595 ctsio->io_hdr.status = CTL_SUCCESS; 9596 9597 ctl_done((union ctl_io *)ctsio); 9598 9599 return (CTL_RETVAL_COMPLETE); 9600 } 9601 9602 #ifdef notyet 9603 static int 9604 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9605 { 9606 9607 } 9608 #endif 9609 9610 static int 9611 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9612 { 9613 struct scsi_vpd_supported_pages *pages; 9614 int sup_page_size; 9615 struct ctl_lun *lun; 9616 9617 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9618 9619 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9620 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9621 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9622 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9623 ctsio->kern_sg_entries = 0; 9624 9625 if (sup_page_size < alloc_len) { 9626 ctsio->residual = alloc_len - sup_page_size; 9627 ctsio->kern_data_len = sup_page_size; 9628 ctsio->kern_total_len = sup_page_size; 9629 } else { 9630 ctsio->residual = 0; 9631 ctsio->kern_data_len = alloc_len; 9632 ctsio->kern_total_len = alloc_len; 9633 } 9634 ctsio->kern_data_resid = 0; 9635 ctsio->kern_rel_offset = 0; 9636 ctsio->kern_sg_entries = 0; 9637 9638 /* 9639 * The control device is always connected. The disk device, on the 9640 * other hand, may not be online all the time. Need to change this 9641 * to figure out whether the disk device is actually online or not. 9642 */ 9643 if (lun != NULL) 9644 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9645 lun->be_lun->lun_type; 9646 else 9647 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9648 9649 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9650 /* Supported VPD pages */ 9651 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9652 /* Serial Number */ 9653 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9654 /* Device Identification */ 9655 pages->page_list[2] = SVPD_DEVICE_ID; 9656 /* Extended INQUIRY Data */ 9657 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9658 /* Mode Page Policy */ 9659 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9660 /* SCSI Ports */ 9661 pages->page_list[5] = SVPD_SCSI_PORTS; 9662 /* Third-party Copy */ 9663 pages->page_list[6] = SVPD_SCSI_TPC; 9664 /* Block limits */ 9665 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9666 /* Block Device Characteristics */ 9667 pages->page_list[8] = SVPD_BDC; 9668 /* Logical Block Provisioning */ 9669 pages->page_list[9] = SVPD_LBP; 9670 9671 ctsio->scsi_status = SCSI_STATUS_OK; 9672 9673 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9674 ctsio->be_move_done = ctl_config_move_done; 9675 ctl_datamove((union ctl_io *)ctsio); 9676 9677 return (CTL_RETVAL_COMPLETE); 9678 } 9679 9680 static int 9681 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9682 { 9683 struct scsi_vpd_unit_serial_number *sn_ptr; 9684 struct ctl_lun *lun; 9685 int data_len; 9686 9687 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9688 9689 data_len = 4 + CTL_SN_LEN; 9690 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9691 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9692 if (data_len < alloc_len) { 9693 ctsio->residual = alloc_len - data_len; 9694 ctsio->kern_data_len = data_len; 9695 ctsio->kern_total_len = data_len; 9696 } else { 9697 ctsio->residual = 0; 9698 ctsio->kern_data_len = alloc_len; 9699 ctsio->kern_total_len = alloc_len; 9700 } 9701 ctsio->kern_data_resid = 0; 9702 ctsio->kern_rel_offset = 0; 9703 ctsio->kern_sg_entries = 0; 9704 9705 /* 9706 * The control device is always connected. The disk device, on the 9707 * other hand, may not be online all the time. Need to change this 9708 * to figure out whether the disk device is actually online or not. 9709 */ 9710 if (lun != NULL) 9711 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9712 lun->be_lun->lun_type; 9713 else 9714 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9715 9716 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9717 sn_ptr->length = CTL_SN_LEN; 9718 /* 9719 * If we don't have a LUN, we just leave the serial number as 9720 * all spaces. 9721 */ 9722 if (lun != NULL) { 9723 strncpy((char *)sn_ptr->serial_num, 9724 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9725 } else 9726 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9727 ctsio->scsi_status = SCSI_STATUS_OK; 9728 9729 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9730 ctsio->be_move_done = ctl_config_move_done; 9731 ctl_datamove((union ctl_io *)ctsio); 9732 9733 return (CTL_RETVAL_COMPLETE); 9734 } 9735 9736 9737 static int 9738 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9739 { 9740 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9741 struct ctl_lun *lun; 9742 int data_len; 9743 9744 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9745 9746 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9747 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9748 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9749 ctsio->kern_sg_entries = 0; 9750 9751 if (data_len < alloc_len) { 9752 ctsio->residual = alloc_len - data_len; 9753 ctsio->kern_data_len = data_len; 9754 ctsio->kern_total_len = data_len; 9755 } else { 9756 ctsio->residual = 0; 9757 ctsio->kern_data_len = alloc_len; 9758 ctsio->kern_total_len = alloc_len; 9759 } 9760 ctsio->kern_data_resid = 0; 9761 ctsio->kern_rel_offset = 0; 9762 ctsio->kern_sg_entries = 0; 9763 9764 /* 9765 * The control device is always connected. The disk device, on the 9766 * other hand, may not be online all the time. 9767 */ 9768 if (lun != NULL) 9769 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9770 lun->be_lun->lun_type; 9771 else 9772 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9773 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9774 eid_ptr->page_length = data_len - 4; 9775 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9776 eid_ptr->flags3 = SVPD_EID_V_SUP; 9777 9778 ctsio->scsi_status = SCSI_STATUS_OK; 9779 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9780 ctsio->be_move_done = ctl_config_move_done; 9781 ctl_datamove((union ctl_io *)ctsio); 9782 9783 return (CTL_RETVAL_COMPLETE); 9784 } 9785 9786 static int 9787 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9788 { 9789 struct scsi_vpd_mode_page_policy *mpp_ptr; 9790 struct ctl_lun *lun; 9791 int data_len; 9792 9793 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9794 9795 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9796 sizeof(struct scsi_vpd_mode_page_policy_descr); 9797 9798 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9799 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9800 ctsio->kern_sg_entries = 0; 9801 9802 if (data_len < alloc_len) { 9803 ctsio->residual = alloc_len - data_len; 9804 ctsio->kern_data_len = data_len; 9805 ctsio->kern_total_len = data_len; 9806 } else { 9807 ctsio->residual = 0; 9808 ctsio->kern_data_len = alloc_len; 9809 ctsio->kern_total_len = alloc_len; 9810 } 9811 ctsio->kern_data_resid = 0; 9812 ctsio->kern_rel_offset = 0; 9813 ctsio->kern_sg_entries = 0; 9814 9815 /* 9816 * The control device is always connected. The disk device, on the 9817 * other hand, may not be online all the time. 9818 */ 9819 if (lun != NULL) 9820 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9821 lun->be_lun->lun_type; 9822 else 9823 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9824 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9825 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9826 mpp_ptr->descr[0].page_code = 0x3f; 9827 mpp_ptr->descr[0].subpage_code = 0xff; 9828 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9829 9830 ctsio->scsi_status = SCSI_STATUS_OK; 9831 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9832 ctsio->be_move_done = ctl_config_move_done; 9833 ctl_datamove((union ctl_io *)ctsio); 9834 9835 return (CTL_RETVAL_COMPLETE); 9836 } 9837 9838 static int 9839 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9840 { 9841 struct scsi_vpd_device_id *devid_ptr; 9842 struct scsi_vpd_id_descriptor *desc; 9843 struct ctl_softc *ctl_softc; 9844 struct ctl_lun *lun; 9845 struct ctl_port *port; 9846 int data_len; 9847 uint8_t proto; 9848 9849 ctl_softc = control_softc; 9850 9851 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9852 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9853 9854 data_len = sizeof(struct scsi_vpd_device_id) + 9855 sizeof(struct scsi_vpd_id_descriptor) + 9856 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9857 sizeof(struct scsi_vpd_id_descriptor) + 9858 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9859 if (lun && lun->lun_devid) 9860 data_len += lun->lun_devid->len; 9861 if (port->port_devid) 9862 data_len += port->port_devid->len; 9863 if (port->target_devid) 9864 data_len += port->target_devid->len; 9865 9866 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9867 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9868 ctsio->kern_sg_entries = 0; 9869 9870 if (data_len < alloc_len) { 9871 ctsio->residual = alloc_len - data_len; 9872 ctsio->kern_data_len = data_len; 9873 ctsio->kern_total_len = data_len; 9874 } else { 9875 ctsio->residual = 0; 9876 ctsio->kern_data_len = alloc_len; 9877 ctsio->kern_total_len = alloc_len; 9878 } 9879 ctsio->kern_data_resid = 0; 9880 ctsio->kern_rel_offset = 0; 9881 ctsio->kern_sg_entries = 0; 9882 9883 /* 9884 * The control device is always connected. The disk device, on the 9885 * other hand, may not be online all the time. 9886 */ 9887 if (lun != NULL) 9888 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9889 lun->be_lun->lun_type; 9890 else 9891 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9892 devid_ptr->page_code = SVPD_DEVICE_ID; 9893 scsi_ulto2b(data_len - 4, devid_ptr->length); 9894 9895 if (port->port_type == CTL_PORT_FC) 9896 proto = SCSI_PROTO_FC << 4; 9897 else if (port->port_type == CTL_PORT_ISCSI) 9898 proto = SCSI_PROTO_ISCSI << 4; 9899 else 9900 proto = SCSI_PROTO_SPI << 4; 9901 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9902 9903 /* 9904 * We're using a LUN association here. i.e., this device ID is a 9905 * per-LUN identifier. 9906 */ 9907 if (lun && lun->lun_devid) { 9908 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9909 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9910 lun->lun_devid->len); 9911 } 9912 9913 /* 9914 * This is for the WWPN which is a port association. 9915 */ 9916 if (port->port_devid) { 9917 memcpy(desc, port->port_devid->data, port->port_devid->len); 9918 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9919 port->port_devid->len); 9920 } 9921 9922 /* 9923 * This is for the Relative Target Port(type 4h) identifier 9924 */ 9925 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9926 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9927 SVPD_ID_TYPE_RELTARG; 9928 desc->length = 4; 9929 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9930 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9931 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9932 9933 /* 9934 * This is for the Target Port Group(type 5h) identifier 9935 */ 9936 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9937 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9938 SVPD_ID_TYPE_TPORTGRP; 9939 desc->length = 4; 9940 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9941 &desc->identifier[2]); 9942 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9943 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9944 9945 /* 9946 * This is for the Target identifier 9947 */ 9948 if (port->target_devid) { 9949 memcpy(desc, port->target_devid->data, port->target_devid->len); 9950 } 9951 9952 ctsio->scsi_status = SCSI_STATUS_OK; 9953 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9954 ctsio->be_move_done = ctl_config_move_done; 9955 ctl_datamove((union ctl_io *)ctsio); 9956 9957 return (CTL_RETVAL_COMPLETE); 9958 } 9959 9960 static int 9961 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9962 { 9963 struct ctl_softc *softc = control_softc; 9964 struct scsi_vpd_scsi_ports *sp; 9965 struct scsi_vpd_port_designation *pd; 9966 struct scsi_vpd_port_designation_cont *pdc; 9967 struct ctl_lun *lun; 9968 struct ctl_port *port; 9969 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 9970 int num_target_port_groups, single; 9971 9972 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9973 9974 single = ctl_is_single; 9975 if (single) 9976 num_target_port_groups = 1; 9977 else 9978 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 9979 num_target_ports = 0; 9980 iid_len = 0; 9981 id_len = 0; 9982 mtx_lock(&softc->ctl_lock); 9983 STAILQ_FOREACH(port, &softc->port_list, links) { 9984 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9985 continue; 9986 if (lun != NULL && 9987 ctl_map_lun_back(port->targ_port, lun->lun) >= 9988 CTL_MAX_LUNS) 9989 continue; 9990 num_target_ports++; 9991 if (port->init_devid) 9992 iid_len += port->init_devid->len; 9993 if (port->port_devid) 9994 id_len += port->port_devid->len; 9995 } 9996 mtx_unlock(&softc->ctl_lock); 9997 9998 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 9999 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10000 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10001 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10002 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10003 ctsio->kern_sg_entries = 0; 10004 10005 if (data_len < alloc_len) { 10006 ctsio->residual = alloc_len - data_len; 10007 ctsio->kern_data_len = data_len; 10008 ctsio->kern_total_len = data_len; 10009 } else { 10010 ctsio->residual = 0; 10011 ctsio->kern_data_len = alloc_len; 10012 ctsio->kern_total_len = alloc_len; 10013 } 10014 ctsio->kern_data_resid = 0; 10015 ctsio->kern_rel_offset = 0; 10016 ctsio->kern_sg_entries = 0; 10017 10018 /* 10019 * The control device is always connected. The disk device, on the 10020 * other hand, may not be online all the time. Need to change this 10021 * to figure out whether the disk device is actually online or not. 10022 */ 10023 if (lun != NULL) 10024 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10025 lun->be_lun->lun_type; 10026 else 10027 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10028 10029 sp->page_code = SVPD_SCSI_PORTS; 10030 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10031 sp->page_length); 10032 pd = &sp->design[0]; 10033 10034 mtx_lock(&softc->ctl_lock); 10035 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10036 pg = 0; 10037 else 10038 pg = 1; 10039 for (g = 0; g < num_target_port_groups; g++) { 10040 STAILQ_FOREACH(port, &softc->port_list, links) { 10041 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10042 continue; 10043 if (lun != NULL && 10044 ctl_map_lun_back(port->targ_port, lun->lun) >= 10045 CTL_MAX_LUNS) 10046 continue; 10047 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10048 scsi_ulto2b(p, pd->relative_port_id); 10049 if (port->init_devid && g == pg) { 10050 iid_len = port->init_devid->len; 10051 memcpy(pd->initiator_transportid, 10052 port->init_devid->data, port->init_devid->len); 10053 } else 10054 iid_len = 0; 10055 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10056 pdc = (struct scsi_vpd_port_designation_cont *) 10057 (&pd->initiator_transportid[iid_len]); 10058 if (port->port_devid && g == pg) { 10059 id_len = port->port_devid->len; 10060 memcpy(pdc->target_port_descriptors, 10061 port->port_devid->data, port->port_devid->len); 10062 } else 10063 id_len = 0; 10064 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10065 pd = (struct scsi_vpd_port_designation *) 10066 ((uint8_t *)pdc->target_port_descriptors + id_len); 10067 } 10068 } 10069 mtx_unlock(&softc->ctl_lock); 10070 10071 ctsio->scsi_status = SCSI_STATUS_OK; 10072 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10073 ctsio->be_move_done = ctl_config_move_done; 10074 ctl_datamove((union ctl_io *)ctsio); 10075 10076 return (CTL_RETVAL_COMPLETE); 10077 } 10078 10079 static int 10080 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10081 { 10082 struct scsi_vpd_block_limits *bl_ptr; 10083 struct ctl_lun *lun; 10084 int bs; 10085 10086 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10087 10088 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10089 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10090 ctsio->kern_sg_entries = 0; 10091 10092 if (sizeof(*bl_ptr) < alloc_len) { 10093 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10094 ctsio->kern_data_len = sizeof(*bl_ptr); 10095 ctsio->kern_total_len = sizeof(*bl_ptr); 10096 } else { 10097 ctsio->residual = 0; 10098 ctsio->kern_data_len = alloc_len; 10099 ctsio->kern_total_len = alloc_len; 10100 } 10101 ctsio->kern_data_resid = 0; 10102 ctsio->kern_rel_offset = 0; 10103 ctsio->kern_sg_entries = 0; 10104 10105 /* 10106 * The control device is always connected. The disk device, on the 10107 * other hand, may not be online all the time. Need to change this 10108 * to figure out whether the disk device is actually online or not. 10109 */ 10110 if (lun != NULL) 10111 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10112 lun->be_lun->lun_type; 10113 else 10114 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10115 10116 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10117 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10118 bl_ptr->max_cmp_write_len = 0xff; 10119 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10120 if (lun != NULL) { 10121 bs = lun->be_lun->blocksize; 10122 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10123 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10124 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10125 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10126 if (lun->be_lun->pblockexp != 0) { 10127 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10128 bl_ptr->opt_unmap_grain); 10129 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10130 bl_ptr->unmap_grain_align); 10131 } 10132 } 10133 scsi_ulto4b(lun->be_lun->atomicblock, 10134 bl_ptr->max_atomic_transfer_length); 10135 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10136 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10137 } 10138 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10139 10140 ctsio->scsi_status = SCSI_STATUS_OK; 10141 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10142 ctsio->be_move_done = ctl_config_move_done; 10143 ctl_datamove((union ctl_io *)ctsio); 10144 10145 return (CTL_RETVAL_COMPLETE); 10146 } 10147 10148 static int 10149 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10150 { 10151 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10152 struct ctl_lun *lun; 10153 const char *value; 10154 u_int i; 10155 10156 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10157 10158 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10159 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10160 ctsio->kern_sg_entries = 0; 10161 10162 if (sizeof(*bdc_ptr) < alloc_len) { 10163 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10164 ctsio->kern_data_len = sizeof(*bdc_ptr); 10165 ctsio->kern_total_len = sizeof(*bdc_ptr); 10166 } else { 10167 ctsio->residual = 0; 10168 ctsio->kern_data_len = alloc_len; 10169 ctsio->kern_total_len = alloc_len; 10170 } 10171 ctsio->kern_data_resid = 0; 10172 ctsio->kern_rel_offset = 0; 10173 ctsio->kern_sg_entries = 0; 10174 10175 /* 10176 * The control device is always connected. The disk device, on the 10177 * other hand, may not be online all the time. Need to change this 10178 * to figure out whether the disk device is actually online or not. 10179 */ 10180 if (lun != NULL) 10181 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10182 lun->be_lun->lun_type; 10183 else 10184 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10185 bdc_ptr->page_code = SVPD_BDC; 10186 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10187 if (lun != NULL && 10188 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10189 i = strtol(value, NULL, 0); 10190 else 10191 i = SVPD_NON_ROTATING; 10192 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10193 if (lun != NULL && 10194 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10195 i = strtol(value, NULL, 0); 10196 else 10197 i = 0; 10198 bdc_ptr->wab_wac_ff = (i & 0x0f); 10199 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10200 10201 ctsio->scsi_status = SCSI_STATUS_OK; 10202 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10203 ctsio->be_move_done = ctl_config_move_done; 10204 ctl_datamove((union ctl_io *)ctsio); 10205 10206 return (CTL_RETVAL_COMPLETE); 10207 } 10208 10209 static int 10210 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10211 { 10212 struct scsi_vpd_logical_block_prov *lbp_ptr; 10213 struct ctl_lun *lun; 10214 10215 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10216 10217 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10218 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10219 ctsio->kern_sg_entries = 0; 10220 10221 if (sizeof(*lbp_ptr) < alloc_len) { 10222 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10223 ctsio->kern_data_len = sizeof(*lbp_ptr); 10224 ctsio->kern_total_len = sizeof(*lbp_ptr); 10225 } else { 10226 ctsio->residual = 0; 10227 ctsio->kern_data_len = alloc_len; 10228 ctsio->kern_total_len = alloc_len; 10229 } 10230 ctsio->kern_data_resid = 0; 10231 ctsio->kern_rel_offset = 0; 10232 ctsio->kern_sg_entries = 0; 10233 10234 /* 10235 * The control device is always connected. The disk device, on the 10236 * other hand, may not be online all the time. Need to change this 10237 * to figure out whether the disk device is actually online or not. 10238 */ 10239 if (lun != NULL) 10240 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10241 lun->be_lun->lun_type; 10242 else 10243 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10244 10245 lbp_ptr->page_code = SVPD_LBP; 10246 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10247 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10248 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10249 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10250 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10251 } 10252 10253 ctsio->scsi_status = SCSI_STATUS_OK; 10254 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10255 ctsio->be_move_done = ctl_config_move_done; 10256 ctl_datamove((union ctl_io *)ctsio); 10257 10258 return (CTL_RETVAL_COMPLETE); 10259 } 10260 10261 static int 10262 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10263 { 10264 struct scsi_inquiry *cdb; 10265 struct ctl_lun *lun; 10266 int alloc_len, retval; 10267 10268 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10269 cdb = (struct scsi_inquiry *)ctsio->cdb; 10270 10271 retval = CTL_RETVAL_COMPLETE; 10272 10273 alloc_len = scsi_2btoul(cdb->length); 10274 10275 switch (cdb->page_code) { 10276 case SVPD_SUPPORTED_PAGES: 10277 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10278 break; 10279 case SVPD_UNIT_SERIAL_NUMBER: 10280 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10281 break; 10282 case SVPD_DEVICE_ID: 10283 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10284 break; 10285 case SVPD_EXTENDED_INQUIRY_DATA: 10286 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10287 break; 10288 case SVPD_MODE_PAGE_POLICY: 10289 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10290 break; 10291 case SVPD_SCSI_PORTS: 10292 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10293 break; 10294 case SVPD_SCSI_TPC: 10295 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10296 break; 10297 case SVPD_BLOCK_LIMITS: 10298 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10299 break; 10300 case SVPD_BDC: 10301 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10302 break; 10303 case SVPD_LBP: 10304 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10305 break; 10306 default: 10307 ctl_set_invalid_field(ctsio, 10308 /*sks_valid*/ 1, 10309 /*command*/ 1, 10310 /*field*/ 2, 10311 /*bit_valid*/ 0, 10312 /*bit*/ 0); 10313 ctl_done((union ctl_io *)ctsio); 10314 retval = CTL_RETVAL_COMPLETE; 10315 break; 10316 } 10317 10318 return (retval); 10319 } 10320 10321 static int 10322 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10323 { 10324 struct scsi_inquiry_data *inq_ptr; 10325 struct scsi_inquiry *cdb; 10326 struct ctl_softc *ctl_softc; 10327 struct ctl_lun *lun; 10328 char *val; 10329 uint32_t alloc_len, data_len; 10330 ctl_port_type port_type; 10331 10332 ctl_softc = control_softc; 10333 10334 /* 10335 * Figure out whether we're talking to a Fibre Channel port or not. 10336 * We treat the ioctl front end, and any SCSI adapters, as packetized 10337 * SCSI front ends. 10338 */ 10339 port_type = ctl_softc->ctl_ports[ 10340 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10341 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10342 port_type = CTL_PORT_SCSI; 10343 10344 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10345 cdb = (struct scsi_inquiry *)ctsio->cdb; 10346 alloc_len = scsi_2btoul(cdb->length); 10347 10348 /* 10349 * We malloc the full inquiry data size here and fill it 10350 * in. If the user only asks for less, we'll give him 10351 * that much. 10352 */ 10353 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10354 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10355 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10356 ctsio->kern_sg_entries = 0; 10357 ctsio->kern_data_resid = 0; 10358 ctsio->kern_rel_offset = 0; 10359 10360 if (data_len < alloc_len) { 10361 ctsio->residual = alloc_len - data_len; 10362 ctsio->kern_data_len = data_len; 10363 ctsio->kern_total_len = data_len; 10364 } else { 10365 ctsio->residual = 0; 10366 ctsio->kern_data_len = alloc_len; 10367 ctsio->kern_total_len = alloc_len; 10368 } 10369 10370 /* 10371 * If we have a LUN configured, report it as connected. Otherwise, 10372 * report that it is offline or no device is supported, depending 10373 * on the value of inquiry_pq_no_lun. 10374 * 10375 * According to the spec (SPC-4 r34), the peripheral qualifier 10376 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10377 * 10378 * "A peripheral device having the specified peripheral device type 10379 * is not connected to this logical unit. However, the device 10380 * server is capable of supporting the specified peripheral device 10381 * type on this logical unit." 10382 * 10383 * According to the same spec, the peripheral qualifier 10384 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10385 * 10386 * "The device server is not capable of supporting a peripheral 10387 * device on this logical unit. For this peripheral qualifier the 10388 * peripheral device type shall be set to 1Fh. All other peripheral 10389 * device type values are reserved for this peripheral qualifier." 10390 * 10391 * Given the text, it would seem that we probably want to report that 10392 * the LUN is offline here. There is no LUN connected, but we can 10393 * support a LUN at the given LUN number. 10394 * 10395 * In the real world, though, it sounds like things are a little 10396 * different: 10397 * 10398 * - Linux, when presented with a LUN with the offline peripheral 10399 * qualifier, will create an sg driver instance for it. So when 10400 * you attach it to CTL, you wind up with a ton of sg driver 10401 * instances. (One for every LUN that Linux bothered to probe.) 10402 * Linux does this despite the fact that it issues a REPORT LUNs 10403 * to LUN 0 to get the inventory of supported LUNs. 10404 * 10405 * - There is other anecdotal evidence (from Emulex folks) about 10406 * arrays that use the offline peripheral qualifier for LUNs that 10407 * are on the "passive" path in an active/passive array. 10408 * 10409 * So the solution is provide a hopefully reasonable default 10410 * (return bad/no LUN) and allow the user to change the behavior 10411 * with a tunable/sysctl variable. 10412 */ 10413 if (lun != NULL) 10414 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10415 lun->be_lun->lun_type; 10416 else if (ctl_softc->inquiry_pq_no_lun == 0) 10417 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10418 else 10419 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10420 10421 /* RMB in byte 2 is 0 */ 10422 inq_ptr->version = SCSI_REV_SPC4; 10423 10424 /* 10425 * According to SAM-3, even if a device only supports a single 10426 * level of LUN addressing, it should still set the HISUP bit: 10427 * 10428 * 4.9.1 Logical unit numbers overview 10429 * 10430 * All logical unit number formats described in this standard are 10431 * hierarchical in structure even when only a single level in that 10432 * hierarchy is used. The HISUP bit shall be set to one in the 10433 * standard INQUIRY data (see SPC-2) when any logical unit number 10434 * format described in this standard is used. Non-hierarchical 10435 * formats are outside the scope of this standard. 10436 * 10437 * Therefore we set the HiSup bit here. 10438 * 10439 * The reponse format is 2, per SPC-3. 10440 */ 10441 inq_ptr->response_format = SID_HiSup | 2; 10442 10443 inq_ptr->additional_length = data_len - 10444 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10445 CTL_DEBUG_PRINT(("additional_length = %d\n", 10446 inq_ptr->additional_length)); 10447 10448 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10449 /* 16 bit addressing */ 10450 if (port_type == CTL_PORT_SCSI) 10451 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10452 /* XXX set the SID_MultiP bit here if we're actually going to 10453 respond on multiple ports */ 10454 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10455 10456 /* 16 bit data bus, synchronous transfers */ 10457 if (port_type == CTL_PORT_SCSI) 10458 inq_ptr->flags = SID_WBus16 | SID_Sync; 10459 /* 10460 * XXX KDM do we want to support tagged queueing on the control 10461 * device at all? 10462 */ 10463 if ((lun == NULL) 10464 || (lun->be_lun->lun_type != T_PROCESSOR)) 10465 inq_ptr->flags |= SID_CmdQue; 10466 /* 10467 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10468 * We have 8 bytes for the vendor name, and 16 bytes for the device 10469 * name and 4 bytes for the revision. 10470 */ 10471 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10472 "vendor")) == NULL) { 10473 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10474 } else { 10475 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10476 strncpy(inq_ptr->vendor, val, 10477 min(sizeof(inq_ptr->vendor), strlen(val))); 10478 } 10479 if (lun == NULL) { 10480 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10481 sizeof(inq_ptr->product)); 10482 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10483 switch (lun->be_lun->lun_type) { 10484 case T_DIRECT: 10485 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10486 sizeof(inq_ptr->product)); 10487 break; 10488 case T_PROCESSOR: 10489 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10490 sizeof(inq_ptr->product)); 10491 break; 10492 default: 10493 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10494 sizeof(inq_ptr->product)); 10495 break; 10496 } 10497 } else { 10498 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10499 strncpy(inq_ptr->product, val, 10500 min(sizeof(inq_ptr->product), strlen(val))); 10501 } 10502 10503 /* 10504 * XXX make this a macro somewhere so it automatically gets 10505 * incremented when we make changes. 10506 */ 10507 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10508 "revision")) == NULL) { 10509 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10510 } else { 10511 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10512 strncpy(inq_ptr->revision, val, 10513 min(sizeof(inq_ptr->revision), strlen(val))); 10514 } 10515 10516 /* 10517 * For parallel SCSI, we support double transition and single 10518 * transition clocking. We also support QAS (Quick Arbitration 10519 * and Selection) and Information Unit transfers on both the 10520 * control and array devices. 10521 */ 10522 if (port_type == CTL_PORT_SCSI) 10523 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10524 SID_SPI_IUS; 10525 10526 /* SAM-5 (no version claimed) */ 10527 scsi_ulto2b(0x00A0, inq_ptr->version1); 10528 /* SPC-4 (no version claimed) */ 10529 scsi_ulto2b(0x0460, inq_ptr->version2); 10530 if (port_type == CTL_PORT_FC) { 10531 /* FCP-2 ANSI INCITS.350:2003 */ 10532 scsi_ulto2b(0x0917, inq_ptr->version3); 10533 } else if (port_type == CTL_PORT_SCSI) { 10534 /* SPI-4 ANSI INCITS.362:200x */ 10535 scsi_ulto2b(0x0B56, inq_ptr->version3); 10536 } else if (port_type == CTL_PORT_ISCSI) { 10537 /* iSCSI (no version claimed) */ 10538 scsi_ulto2b(0x0960, inq_ptr->version3); 10539 } else if (port_type == CTL_PORT_SAS) { 10540 /* SAS (no version claimed) */ 10541 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10542 } 10543 10544 if (lun == NULL) { 10545 /* SBC-4 (no version claimed) */ 10546 scsi_ulto2b(0x0600, inq_ptr->version4); 10547 } else { 10548 switch (lun->be_lun->lun_type) { 10549 case T_DIRECT: 10550 /* SBC-4 (no version claimed) */ 10551 scsi_ulto2b(0x0600, inq_ptr->version4); 10552 break; 10553 case T_PROCESSOR: 10554 default: 10555 break; 10556 } 10557 } 10558 10559 ctsio->scsi_status = SCSI_STATUS_OK; 10560 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10561 ctsio->be_move_done = ctl_config_move_done; 10562 ctl_datamove((union ctl_io *)ctsio); 10563 return (CTL_RETVAL_COMPLETE); 10564 } 10565 10566 int 10567 ctl_inquiry(struct ctl_scsiio *ctsio) 10568 { 10569 struct scsi_inquiry *cdb; 10570 int retval; 10571 10572 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10573 10574 cdb = (struct scsi_inquiry *)ctsio->cdb; 10575 if (cdb->byte2 & SI_EVPD) 10576 retval = ctl_inquiry_evpd(ctsio); 10577 else if (cdb->page_code == 0) 10578 retval = ctl_inquiry_std(ctsio); 10579 else { 10580 ctl_set_invalid_field(ctsio, 10581 /*sks_valid*/ 1, 10582 /*command*/ 1, 10583 /*field*/ 2, 10584 /*bit_valid*/ 0, 10585 /*bit*/ 0); 10586 ctl_done((union ctl_io *)ctsio); 10587 return (CTL_RETVAL_COMPLETE); 10588 } 10589 10590 return (retval); 10591 } 10592 10593 /* 10594 * For known CDB types, parse the LBA and length. 10595 */ 10596 static int 10597 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10598 { 10599 if (io->io_hdr.io_type != CTL_IO_SCSI) 10600 return (1); 10601 10602 switch (io->scsiio.cdb[0]) { 10603 case COMPARE_AND_WRITE: { 10604 struct scsi_compare_and_write *cdb; 10605 10606 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10607 10608 *lba = scsi_8btou64(cdb->addr); 10609 *len = cdb->length; 10610 break; 10611 } 10612 case READ_6: 10613 case WRITE_6: { 10614 struct scsi_rw_6 *cdb; 10615 10616 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10617 10618 *lba = scsi_3btoul(cdb->addr); 10619 /* only 5 bits are valid in the most significant address byte */ 10620 *lba &= 0x1fffff; 10621 *len = cdb->length; 10622 break; 10623 } 10624 case READ_10: 10625 case WRITE_10: { 10626 struct scsi_rw_10 *cdb; 10627 10628 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10629 10630 *lba = scsi_4btoul(cdb->addr); 10631 *len = scsi_2btoul(cdb->length); 10632 break; 10633 } 10634 case WRITE_VERIFY_10: { 10635 struct scsi_write_verify_10 *cdb; 10636 10637 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10638 10639 *lba = scsi_4btoul(cdb->addr); 10640 *len = scsi_2btoul(cdb->length); 10641 break; 10642 } 10643 case READ_12: 10644 case WRITE_12: { 10645 struct scsi_rw_12 *cdb; 10646 10647 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10648 10649 *lba = scsi_4btoul(cdb->addr); 10650 *len = scsi_4btoul(cdb->length); 10651 break; 10652 } 10653 case WRITE_VERIFY_12: { 10654 struct scsi_write_verify_12 *cdb; 10655 10656 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10657 10658 *lba = scsi_4btoul(cdb->addr); 10659 *len = scsi_4btoul(cdb->length); 10660 break; 10661 } 10662 case READ_16: 10663 case WRITE_16: 10664 case WRITE_ATOMIC_16: { 10665 struct scsi_rw_16 *cdb; 10666 10667 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10668 10669 *lba = scsi_8btou64(cdb->addr); 10670 *len = scsi_4btoul(cdb->length); 10671 break; 10672 } 10673 case WRITE_VERIFY_16: { 10674 struct scsi_write_verify_16 *cdb; 10675 10676 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10677 10678 *lba = scsi_8btou64(cdb->addr); 10679 *len = scsi_4btoul(cdb->length); 10680 break; 10681 } 10682 case WRITE_SAME_10: { 10683 struct scsi_write_same_10 *cdb; 10684 10685 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10686 10687 *lba = scsi_4btoul(cdb->addr); 10688 *len = scsi_2btoul(cdb->length); 10689 break; 10690 } 10691 case WRITE_SAME_16: { 10692 struct scsi_write_same_16 *cdb; 10693 10694 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10695 10696 *lba = scsi_8btou64(cdb->addr); 10697 *len = scsi_4btoul(cdb->length); 10698 break; 10699 } 10700 case VERIFY_10: { 10701 struct scsi_verify_10 *cdb; 10702 10703 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10704 10705 *lba = scsi_4btoul(cdb->addr); 10706 *len = scsi_2btoul(cdb->length); 10707 break; 10708 } 10709 case VERIFY_12: { 10710 struct scsi_verify_12 *cdb; 10711 10712 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10713 10714 *lba = scsi_4btoul(cdb->addr); 10715 *len = scsi_4btoul(cdb->length); 10716 break; 10717 } 10718 case VERIFY_16: { 10719 struct scsi_verify_16 *cdb; 10720 10721 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10722 10723 *lba = scsi_8btou64(cdb->addr); 10724 *len = scsi_4btoul(cdb->length); 10725 break; 10726 } 10727 case UNMAP: { 10728 *lba = 0; 10729 *len = UINT64_MAX; 10730 break; 10731 } 10732 default: 10733 return (1); 10734 break; /* NOTREACHED */ 10735 } 10736 10737 return (0); 10738 } 10739 10740 static ctl_action 10741 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10742 { 10743 uint64_t endlba1, endlba2; 10744 10745 endlba1 = lba1 + len1 - 1; 10746 endlba2 = lba2 + len2 - 1; 10747 10748 if ((endlba1 < lba2) 10749 || (endlba2 < lba1)) 10750 return (CTL_ACTION_PASS); 10751 else 10752 return (CTL_ACTION_BLOCK); 10753 } 10754 10755 static int 10756 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10757 { 10758 struct ctl_ptr_len_flags *ptrlen; 10759 struct scsi_unmap_desc *buf, *end, *range; 10760 uint64_t lba; 10761 uint32_t len; 10762 10763 /* If not UNMAP -- go other way. */ 10764 if (io->io_hdr.io_type != CTL_IO_SCSI || 10765 io->scsiio.cdb[0] != UNMAP) 10766 return (CTL_ACTION_ERROR); 10767 10768 /* If UNMAP without data -- block and wait for data. */ 10769 ptrlen = (struct ctl_ptr_len_flags *) 10770 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10771 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10772 ptrlen->ptr == NULL) 10773 return (CTL_ACTION_BLOCK); 10774 10775 /* UNMAP with data -- check for collision. */ 10776 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10777 end = buf + ptrlen->len / sizeof(*buf); 10778 for (range = buf; range < end; range++) { 10779 lba = scsi_8btou64(range->lba); 10780 len = scsi_4btoul(range->length); 10781 if ((lba < lba2 + len2) && (lba + len > lba2)) 10782 return (CTL_ACTION_BLOCK); 10783 } 10784 return (CTL_ACTION_PASS); 10785 } 10786 10787 static ctl_action 10788 ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10789 { 10790 uint64_t lba1, lba2; 10791 uint64_t len1, len2; 10792 int retval; 10793 10794 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10795 return (CTL_ACTION_ERROR); 10796 10797 retval = ctl_extent_check_unmap(io2, lba1, len1); 10798 if (retval != CTL_ACTION_ERROR) 10799 return (retval); 10800 10801 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10802 return (CTL_ACTION_ERROR); 10803 10804 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10805 } 10806 10807 static ctl_action 10808 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10809 union ctl_io *ooa_io) 10810 { 10811 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10812 ctl_serialize_action *serialize_row; 10813 10814 /* 10815 * The initiator attempted multiple untagged commands at the same 10816 * time. Can't do that. 10817 */ 10818 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10819 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10820 && ((pending_io->io_hdr.nexus.targ_port == 10821 ooa_io->io_hdr.nexus.targ_port) 10822 && (pending_io->io_hdr.nexus.initid.id == 10823 ooa_io->io_hdr.nexus.initid.id)) 10824 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10825 return (CTL_ACTION_OVERLAP); 10826 10827 /* 10828 * The initiator attempted to send multiple tagged commands with 10829 * the same ID. (It's fine if different initiators have the same 10830 * tag ID.) 10831 * 10832 * Even if all of those conditions are true, we don't kill the I/O 10833 * if the command ahead of us has been aborted. We won't end up 10834 * sending it to the FETD, and it's perfectly legal to resend a 10835 * command with the same tag number as long as the previous 10836 * instance of this tag number has been aborted somehow. 10837 */ 10838 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10839 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10840 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10841 && ((pending_io->io_hdr.nexus.targ_port == 10842 ooa_io->io_hdr.nexus.targ_port) 10843 && (pending_io->io_hdr.nexus.initid.id == 10844 ooa_io->io_hdr.nexus.initid.id)) 10845 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10846 return (CTL_ACTION_OVERLAP_TAG); 10847 10848 /* 10849 * If we get a head of queue tag, SAM-3 says that we should 10850 * immediately execute it. 10851 * 10852 * What happens if this command would normally block for some other 10853 * reason? e.g. a request sense with a head of queue tag 10854 * immediately after a write. Normally that would block, but this 10855 * will result in its getting executed immediately... 10856 * 10857 * We currently return "pass" instead of "skip", so we'll end up 10858 * going through the rest of the queue to check for overlapped tags. 10859 * 10860 * XXX KDM check for other types of blockage first?? 10861 */ 10862 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10863 return (CTL_ACTION_PASS); 10864 10865 /* 10866 * Ordered tags have to block until all items ahead of them 10867 * have completed. If we get called with an ordered tag, we always 10868 * block, if something else is ahead of us in the queue. 10869 */ 10870 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10871 return (CTL_ACTION_BLOCK); 10872 10873 /* 10874 * Simple tags get blocked until all head of queue and ordered tags 10875 * ahead of them have completed. I'm lumping untagged commands in 10876 * with simple tags here. XXX KDM is that the right thing to do? 10877 */ 10878 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10879 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10880 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10881 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10882 return (CTL_ACTION_BLOCK); 10883 10884 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10885 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10886 10887 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10888 10889 switch (serialize_row[pending_entry->seridx]) { 10890 case CTL_SER_BLOCK: 10891 return (CTL_ACTION_BLOCK); 10892 case CTL_SER_EXTENT: 10893 return (ctl_extent_check(pending_io, ooa_io)); 10894 case CTL_SER_EXTENTOPT: 10895 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10896 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10897 return (ctl_extent_check(pending_io, ooa_io)); 10898 /* FALLTHROUGH */ 10899 case CTL_SER_PASS: 10900 return (CTL_ACTION_PASS); 10901 case CTL_SER_BLOCKOPT: 10902 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10903 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10904 return (CTL_ACTION_BLOCK); 10905 return (CTL_ACTION_PASS); 10906 case CTL_SER_SKIP: 10907 return (CTL_ACTION_SKIP); 10908 default: 10909 panic("invalid serialization value %d", 10910 serialize_row[pending_entry->seridx]); 10911 } 10912 10913 return (CTL_ACTION_ERROR); 10914 } 10915 10916 /* 10917 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10918 * Assumptions: 10919 * - pending_io is generally either incoming, or on the blocked queue 10920 * - starting I/O is the I/O we want to start the check with. 10921 */ 10922 static ctl_action 10923 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10924 union ctl_io *starting_io) 10925 { 10926 union ctl_io *ooa_io; 10927 ctl_action action; 10928 10929 mtx_assert(&lun->lun_lock, MA_OWNED); 10930 10931 /* 10932 * Run back along the OOA queue, starting with the current 10933 * blocked I/O and going through every I/O before it on the 10934 * queue. If starting_io is NULL, we'll just end up returning 10935 * CTL_ACTION_PASS. 10936 */ 10937 for (ooa_io = starting_io; ooa_io != NULL; 10938 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10939 ooa_links)){ 10940 10941 /* 10942 * This routine just checks to see whether 10943 * cur_blocked is blocked by ooa_io, which is ahead 10944 * of it in the queue. It doesn't queue/dequeue 10945 * cur_blocked. 10946 */ 10947 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10948 switch (action) { 10949 case CTL_ACTION_BLOCK: 10950 case CTL_ACTION_OVERLAP: 10951 case CTL_ACTION_OVERLAP_TAG: 10952 case CTL_ACTION_SKIP: 10953 case CTL_ACTION_ERROR: 10954 return (action); 10955 break; /* NOTREACHED */ 10956 case CTL_ACTION_PASS: 10957 break; 10958 default: 10959 panic("invalid action %d", action); 10960 break; /* NOTREACHED */ 10961 } 10962 } 10963 10964 return (CTL_ACTION_PASS); 10965 } 10966 10967 /* 10968 * Assumptions: 10969 * - An I/O has just completed, and has been removed from the per-LUN OOA 10970 * queue, so some items on the blocked queue may now be unblocked. 10971 */ 10972 static int 10973 ctl_check_blocked(struct ctl_lun *lun) 10974 { 10975 union ctl_io *cur_blocked, *next_blocked; 10976 10977 mtx_assert(&lun->lun_lock, MA_OWNED); 10978 10979 /* 10980 * Run forward from the head of the blocked queue, checking each 10981 * entry against the I/Os prior to it on the OOA queue to see if 10982 * there is still any blockage. 10983 * 10984 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10985 * with our removing a variable on it while it is traversing the 10986 * list. 10987 */ 10988 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10989 cur_blocked != NULL; cur_blocked = next_blocked) { 10990 union ctl_io *prev_ooa; 10991 ctl_action action; 10992 10993 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10994 blocked_links); 10995 10996 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10997 ctl_ooaq, ooa_links); 10998 10999 /* 11000 * If cur_blocked happens to be the first item in the OOA 11001 * queue now, prev_ooa will be NULL, and the action 11002 * returned will just be CTL_ACTION_PASS. 11003 */ 11004 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11005 11006 switch (action) { 11007 case CTL_ACTION_BLOCK: 11008 /* Nothing to do here, still blocked */ 11009 break; 11010 case CTL_ACTION_OVERLAP: 11011 case CTL_ACTION_OVERLAP_TAG: 11012 /* 11013 * This shouldn't happen! In theory we've already 11014 * checked this command for overlap... 11015 */ 11016 break; 11017 case CTL_ACTION_PASS: 11018 case CTL_ACTION_SKIP: { 11019 struct ctl_softc *softc; 11020 const struct ctl_cmd_entry *entry; 11021 uint32_t initidx; 11022 int isc_retval; 11023 11024 /* 11025 * The skip case shouldn't happen, this transaction 11026 * should have never made it onto the blocked queue. 11027 */ 11028 /* 11029 * This I/O is no longer blocked, we can remove it 11030 * from the blocked queue. Since this is a TAILQ 11031 * (doubly linked list), we can do O(1) removals 11032 * from any place on the list. 11033 */ 11034 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11035 blocked_links); 11036 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11037 11038 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11039 /* 11040 * Need to send IO back to original side to 11041 * run 11042 */ 11043 union ctl_ha_msg msg_info; 11044 11045 msg_info.hdr.original_sc = 11046 cur_blocked->io_hdr.original_sc; 11047 msg_info.hdr.serializing_sc = cur_blocked; 11048 msg_info.hdr.msg_type = CTL_MSG_R2R; 11049 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11050 &msg_info, sizeof(msg_info), 0)) > 11051 CTL_HA_STATUS_SUCCESS) { 11052 printf("CTL:Check Blocked error from " 11053 "ctl_ha_msg_send %d\n", 11054 isc_retval); 11055 } 11056 break; 11057 } 11058 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11059 softc = control_softc; 11060 11061 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11062 11063 /* 11064 * Check this I/O for LUN state changes that may 11065 * have happened while this command was blocked. 11066 * The LUN state may have been changed by a command 11067 * ahead of us in the queue, so we need to re-check 11068 * for any states that can be caused by SCSI 11069 * commands. 11070 */ 11071 if (ctl_scsiio_lun_check(softc, lun, entry, 11072 &cur_blocked->scsiio) == 0) { 11073 cur_blocked->io_hdr.flags |= 11074 CTL_FLAG_IS_WAS_ON_RTR; 11075 ctl_enqueue_rtr(cur_blocked); 11076 } else 11077 ctl_done(cur_blocked); 11078 break; 11079 } 11080 default: 11081 /* 11082 * This probably shouldn't happen -- we shouldn't 11083 * get CTL_ACTION_ERROR, or anything else. 11084 */ 11085 break; 11086 } 11087 } 11088 11089 return (CTL_RETVAL_COMPLETE); 11090 } 11091 11092 /* 11093 * This routine (with one exception) checks LUN flags that can be set by 11094 * commands ahead of us in the OOA queue. These flags have to be checked 11095 * when a command initially comes in, and when we pull a command off the 11096 * blocked queue and are preparing to execute it. The reason we have to 11097 * check these flags for commands on the blocked queue is that the LUN 11098 * state may have been changed by a command ahead of us while we're on the 11099 * blocked queue. 11100 * 11101 * Ordering is somewhat important with these checks, so please pay 11102 * careful attention to the placement of any new checks. 11103 */ 11104 static int 11105 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11106 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11107 { 11108 int retval; 11109 uint32_t residx; 11110 11111 retval = 0; 11112 11113 mtx_assert(&lun->lun_lock, MA_OWNED); 11114 11115 /* 11116 * If this shelf is a secondary shelf controller, we have to reject 11117 * any media access commands. 11118 */ 11119 #if 0 11120 /* No longer needed for HA */ 11121 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11122 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11123 ctl_set_lun_standby(ctsio); 11124 retval = 1; 11125 goto bailout; 11126 } 11127 #endif 11128 11129 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11130 if (lun->flags & CTL_LUN_READONLY) { 11131 ctl_set_sense(ctsio, /*current_error*/ 1, 11132 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11133 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11134 retval = 1; 11135 goto bailout; 11136 } 11137 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11138 .eca_and_aen & SCP_SWP) != 0) { 11139 ctl_set_sense(ctsio, /*current_error*/ 1, 11140 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11141 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11142 retval = 1; 11143 goto bailout; 11144 } 11145 } 11146 11147 /* 11148 * Check for a reservation conflict. If this command isn't allowed 11149 * even on reserved LUNs, and if this initiator isn't the one who 11150 * reserved us, reject the command with a reservation conflict. 11151 */ 11152 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11153 if ((lun->flags & CTL_LUN_RESERVED) 11154 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11155 if (lun->res_idx != residx) { 11156 ctl_set_reservation_conflict(ctsio); 11157 retval = 1; 11158 goto bailout; 11159 } 11160 } 11161 11162 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11163 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11164 /* No reservation or command is allowed. */; 11165 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11166 (lun->res_type == SPR_TYPE_WR_EX || 11167 lun->res_type == SPR_TYPE_WR_EX_RO || 11168 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11169 /* The command is allowed for Write Exclusive resv. */; 11170 } else { 11171 /* 11172 * if we aren't registered or it's a res holder type 11173 * reservation and this isn't the res holder then set a 11174 * conflict. 11175 */ 11176 if (lun->pr_keys[residx] == 0 11177 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11178 ctl_set_reservation_conflict(ctsio); 11179 retval = 1; 11180 goto bailout; 11181 } 11182 11183 } 11184 11185 if ((lun->flags & CTL_LUN_OFFLINE) 11186 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11187 ctl_set_lun_not_ready(ctsio); 11188 retval = 1; 11189 goto bailout; 11190 } 11191 11192 /* 11193 * If the LUN is stopped, see if this particular command is allowed 11194 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11195 */ 11196 if ((lun->flags & CTL_LUN_STOPPED) 11197 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11198 /* "Logical unit not ready, initializing cmd. required" */ 11199 ctl_set_lun_stopped(ctsio); 11200 retval = 1; 11201 goto bailout; 11202 } 11203 11204 if ((lun->flags & CTL_LUN_INOPERABLE) 11205 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11206 /* "Medium format corrupted" */ 11207 ctl_set_medium_format_corrupted(ctsio); 11208 retval = 1; 11209 goto bailout; 11210 } 11211 11212 bailout: 11213 return (retval); 11214 11215 } 11216 11217 static void 11218 ctl_failover_io(union ctl_io *io, int have_lock) 11219 { 11220 ctl_set_busy(&io->scsiio); 11221 ctl_done(io); 11222 } 11223 11224 static void 11225 ctl_failover(void) 11226 { 11227 struct ctl_lun *lun; 11228 struct ctl_softc *ctl_softc; 11229 union ctl_io *next_io, *pending_io; 11230 union ctl_io *io; 11231 int lun_idx; 11232 int i; 11233 11234 ctl_softc = control_softc; 11235 11236 mtx_lock(&ctl_softc->ctl_lock); 11237 /* 11238 * Remove any cmds from the other SC from the rtr queue. These 11239 * will obviously only be for LUNs for which we're the primary. 11240 * We can't send status or get/send data for these commands. 11241 * Since they haven't been executed yet, we can just remove them. 11242 * We'll either abort them or delete them below, depending on 11243 * which HA mode we're in. 11244 */ 11245 #ifdef notyet 11246 mtx_lock(&ctl_softc->queue_lock); 11247 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11248 io != NULL; io = next_io) { 11249 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11250 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11251 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11252 ctl_io_hdr, links); 11253 } 11254 mtx_unlock(&ctl_softc->queue_lock); 11255 #endif 11256 11257 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11258 lun = ctl_softc->ctl_luns[lun_idx]; 11259 if (lun==NULL) 11260 continue; 11261 11262 /* 11263 * Processor LUNs are primary on both sides. 11264 * XXX will this always be true? 11265 */ 11266 if (lun->be_lun->lun_type == T_PROCESSOR) 11267 continue; 11268 11269 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11270 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11271 printf("FAILOVER: primary lun %d\n", lun_idx); 11272 /* 11273 * Remove all commands from the other SC. First from the 11274 * blocked queue then from the ooa queue. Once we have 11275 * removed them. Call ctl_check_blocked to see if there 11276 * is anything that can run. 11277 */ 11278 for (io = (union ctl_io *)TAILQ_FIRST( 11279 &lun->blocked_queue); io != NULL; io = next_io) { 11280 11281 next_io = (union ctl_io *)TAILQ_NEXT( 11282 &io->io_hdr, blocked_links); 11283 11284 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11285 TAILQ_REMOVE(&lun->blocked_queue, 11286 &io->io_hdr,blocked_links); 11287 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11288 TAILQ_REMOVE(&lun->ooa_queue, 11289 &io->io_hdr, ooa_links); 11290 11291 ctl_free_io(io); 11292 } 11293 } 11294 11295 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11296 io != NULL; io = next_io) { 11297 11298 next_io = (union ctl_io *)TAILQ_NEXT( 11299 &io->io_hdr, ooa_links); 11300 11301 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11302 11303 TAILQ_REMOVE(&lun->ooa_queue, 11304 &io->io_hdr, 11305 ooa_links); 11306 11307 ctl_free_io(io); 11308 } 11309 } 11310 ctl_check_blocked(lun); 11311 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11312 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11313 11314 printf("FAILOVER: primary lun %d\n", lun_idx); 11315 /* 11316 * Abort all commands from the other SC. We can't 11317 * send status back for them now. These should get 11318 * cleaned up when they are completed or come out 11319 * for a datamove operation. 11320 */ 11321 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11322 io != NULL; io = next_io) { 11323 next_io = (union ctl_io *)TAILQ_NEXT( 11324 &io->io_hdr, ooa_links); 11325 11326 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11327 io->io_hdr.flags |= CTL_FLAG_ABORT; 11328 } 11329 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11330 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11331 11332 printf("FAILOVER: secondary lun %d\n", lun_idx); 11333 11334 lun->flags |= CTL_LUN_PRIMARY_SC; 11335 11336 /* 11337 * We send all I/O that was sent to this controller 11338 * and redirected to the other side back with 11339 * busy status, and have the initiator retry it. 11340 * Figuring out how much data has been transferred, 11341 * etc. and picking up where we left off would be 11342 * very tricky. 11343 * 11344 * XXX KDM need to remove I/O from the blocked 11345 * queue as well! 11346 */ 11347 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11348 &lun->ooa_queue); pending_io != NULL; 11349 pending_io = next_io) { 11350 11351 next_io = (union ctl_io *)TAILQ_NEXT( 11352 &pending_io->io_hdr, ooa_links); 11353 11354 pending_io->io_hdr.flags &= 11355 ~CTL_FLAG_SENT_2OTHER_SC; 11356 11357 if (pending_io->io_hdr.flags & 11358 CTL_FLAG_IO_ACTIVE) { 11359 pending_io->io_hdr.flags |= 11360 CTL_FLAG_FAILOVER; 11361 } else { 11362 ctl_set_busy(&pending_io->scsiio); 11363 ctl_done(pending_io); 11364 } 11365 } 11366 11367 /* 11368 * Build Unit Attention 11369 */ 11370 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11371 lun->pending_ua[i] |= 11372 CTL_UA_ASYM_ACC_CHANGE; 11373 } 11374 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11375 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11376 printf("FAILOVER: secondary lun %d\n", lun_idx); 11377 /* 11378 * if the first io on the OOA is not on the RtR queue 11379 * add it. 11380 */ 11381 lun->flags |= CTL_LUN_PRIMARY_SC; 11382 11383 pending_io = (union ctl_io *)TAILQ_FIRST( 11384 &lun->ooa_queue); 11385 if (pending_io==NULL) { 11386 printf("Nothing on OOA queue\n"); 11387 continue; 11388 } 11389 11390 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11391 if ((pending_io->io_hdr.flags & 11392 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11393 pending_io->io_hdr.flags |= 11394 CTL_FLAG_IS_WAS_ON_RTR; 11395 ctl_enqueue_rtr(pending_io); 11396 } 11397 #if 0 11398 else 11399 { 11400 printf("Tag 0x%04x is running\n", 11401 pending_io->scsiio.tag_num); 11402 } 11403 #endif 11404 11405 next_io = (union ctl_io *)TAILQ_NEXT( 11406 &pending_io->io_hdr, ooa_links); 11407 for (pending_io=next_io; pending_io != NULL; 11408 pending_io = next_io) { 11409 pending_io->io_hdr.flags &= 11410 ~CTL_FLAG_SENT_2OTHER_SC; 11411 next_io = (union ctl_io *)TAILQ_NEXT( 11412 &pending_io->io_hdr, ooa_links); 11413 if (pending_io->io_hdr.flags & 11414 CTL_FLAG_IS_WAS_ON_RTR) { 11415 #if 0 11416 printf("Tag 0x%04x is running\n", 11417 pending_io->scsiio.tag_num); 11418 #endif 11419 continue; 11420 } 11421 11422 switch (ctl_check_ooa(lun, pending_io, 11423 (union ctl_io *)TAILQ_PREV( 11424 &pending_io->io_hdr, ctl_ooaq, 11425 ooa_links))) { 11426 11427 case CTL_ACTION_BLOCK: 11428 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11429 &pending_io->io_hdr, 11430 blocked_links); 11431 pending_io->io_hdr.flags |= 11432 CTL_FLAG_BLOCKED; 11433 break; 11434 case CTL_ACTION_PASS: 11435 case CTL_ACTION_SKIP: 11436 pending_io->io_hdr.flags |= 11437 CTL_FLAG_IS_WAS_ON_RTR; 11438 ctl_enqueue_rtr(pending_io); 11439 break; 11440 case CTL_ACTION_OVERLAP: 11441 ctl_set_overlapped_cmd( 11442 (struct ctl_scsiio *)pending_io); 11443 ctl_done(pending_io); 11444 break; 11445 case CTL_ACTION_OVERLAP_TAG: 11446 ctl_set_overlapped_tag( 11447 (struct ctl_scsiio *)pending_io, 11448 pending_io->scsiio.tag_num & 0xff); 11449 ctl_done(pending_io); 11450 break; 11451 case CTL_ACTION_ERROR: 11452 default: 11453 ctl_set_internal_failure( 11454 (struct ctl_scsiio *)pending_io, 11455 0, // sks_valid 11456 0); //retry count 11457 ctl_done(pending_io); 11458 break; 11459 } 11460 } 11461 11462 /* 11463 * Build Unit Attention 11464 */ 11465 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11466 lun->pending_ua[i] |= 11467 CTL_UA_ASYM_ACC_CHANGE; 11468 } 11469 } else { 11470 panic("Unhandled HA mode failover, LUN flags = %#x, " 11471 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11472 } 11473 } 11474 ctl_pause_rtr = 0; 11475 mtx_unlock(&ctl_softc->ctl_lock); 11476 } 11477 11478 static int 11479 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11480 { 11481 struct ctl_lun *lun; 11482 const struct ctl_cmd_entry *entry; 11483 uint32_t initidx, targ_lun; 11484 int retval; 11485 11486 retval = 0; 11487 11488 lun = NULL; 11489 11490 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11491 if ((targ_lun < CTL_MAX_LUNS) 11492 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11493 lun = ctl_softc->ctl_luns[targ_lun]; 11494 /* 11495 * If the LUN is invalid, pretend that it doesn't exist. 11496 * It will go away as soon as all pending I/O has been 11497 * completed. 11498 */ 11499 if (lun->flags & CTL_LUN_DISABLED) { 11500 lun = NULL; 11501 } else { 11502 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11503 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11504 lun->be_lun; 11505 if (lun->be_lun->lun_type == T_PROCESSOR) { 11506 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11507 } 11508 11509 /* 11510 * Every I/O goes into the OOA queue for a 11511 * particular LUN, and stays there until completion. 11512 */ 11513 mtx_lock(&lun->lun_lock); 11514 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11515 ooa_links); 11516 } 11517 } else { 11518 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11519 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11520 } 11521 11522 /* Get command entry and return error if it is unsuppotyed. */ 11523 entry = ctl_validate_command(ctsio); 11524 if (entry == NULL) { 11525 if (lun) 11526 mtx_unlock(&lun->lun_lock); 11527 return (retval); 11528 } 11529 11530 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11531 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11532 11533 /* 11534 * Check to see whether we can send this command to LUNs that don't 11535 * exist. This should pretty much only be the case for inquiry 11536 * and request sense. Further checks, below, really require having 11537 * a LUN, so we can't really check the command anymore. Just put 11538 * it on the rtr queue. 11539 */ 11540 if (lun == NULL) { 11541 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11542 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11543 ctl_enqueue_rtr((union ctl_io *)ctsio); 11544 return (retval); 11545 } 11546 11547 ctl_set_unsupported_lun(ctsio); 11548 ctl_done((union ctl_io *)ctsio); 11549 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11550 return (retval); 11551 } else { 11552 /* 11553 * Make sure we support this particular command on this LUN. 11554 * e.g., we don't support writes to the control LUN. 11555 */ 11556 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11557 mtx_unlock(&lun->lun_lock); 11558 ctl_set_invalid_opcode(ctsio); 11559 ctl_done((union ctl_io *)ctsio); 11560 return (retval); 11561 } 11562 } 11563 11564 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11565 11566 #ifdef CTL_WITH_CA 11567 /* 11568 * If we've got a request sense, it'll clear the contingent 11569 * allegiance condition. Otherwise, if we have a CA condition for 11570 * this initiator, clear it, because it sent down a command other 11571 * than request sense. 11572 */ 11573 if ((ctsio->cdb[0] != REQUEST_SENSE) 11574 && (ctl_is_set(lun->have_ca, initidx))) 11575 ctl_clear_mask(lun->have_ca, initidx); 11576 #endif 11577 11578 /* 11579 * If the command has this flag set, it handles its own unit 11580 * attention reporting, we shouldn't do anything. Otherwise we 11581 * check for any pending unit attentions, and send them back to the 11582 * initiator. We only do this when a command initially comes in, 11583 * not when we pull it off the blocked queue. 11584 * 11585 * According to SAM-3, section 5.3.2, the order that things get 11586 * presented back to the host is basically unit attentions caused 11587 * by some sort of reset event, busy status, reservation conflicts 11588 * or task set full, and finally any other status. 11589 * 11590 * One issue here is that some of the unit attentions we report 11591 * don't fall into the "reset" category (e.g. "reported luns data 11592 * has changed"). So reporting it here, before the reservation 11593 * check, may be technically wrong. I guess the only thing to do 11594 * would be to check for and report the reset events here, and then 11595 * check for the other unit attention types after we check for a 11596 * reservation conflict. 11597 * 11598 * XXX KDM need to fix this 11599 */ 11600 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11601 ctl_ua_type ua_type; 11602 11603 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11604 scsi_sense_data_type sense_format; 11605 11606 if (lun != NULL) 11607 sense_format = (lun->flags & 11608 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11609 SSD_TYPE_FIXED; 11610 else 11611 sense_format = SSD_TYPE_FIXED; 11612 11613 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11614 &ctsio->sense_data, sense_format); 11615 if (ua_type != CTL_UA_NONE) { 11616 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11617 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11618 CTL_AUTOSENSE; 11619 ctsio->sense_len = SSD_FULL_SIZE; 11620 mtx_unlock(&lun->lun_lock); 11621 ctl_done((union ctl_io *)ctsio); 11622 return (retval); 11623 } 11624 } 11625 } 11626 11627 11628 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11629 mtx_unlock(&lun->lun_lock); 11630 ctl_done((union ctl_io *)ctsio); 11631 return (retval); 11632 } 11633 11634 /* 11635 * XXX CHD this is where we want to send IO to other side if 11636 * this LUN is secondary on this SC. We will need to make a copy 11637 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11638 * the copy we send as FROM_OTHER. 11639 * We also need to stuff the address of the original IO so we can 11640 * find it easily. Something similar will need be done on the other 11641 * side so when we are done we can find the copy. 11642 */ 11643 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11644 union ctl_ha_msg msg_info; 11645 int isc_retval; 11646 11647 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11648 11649 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11650 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11651 #if 0 11652 printf("1. ctsio %p\n", ctsio); 11653 #endif 11654 msg_info.hdr.serializing_sc = NULL; 11655 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11656 msg_info.scsi.tag_num = ctsio->tag_num; 11657 msg_info.scsi.tag_type = ctsio->tag_type; 11658 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11659 11660 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11661 11662 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11663 (void *)&msg_info, sizeof(msg_info), 0)) > 11664 CTL_HA_STATUS_SUCCESS) { 11665 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11666 isc_retval); 11667 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11668 } else { 11669 #if 0 11670 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11671 #endif 11672 } 11673 11674 /* 11675 * XXX KDM this I/O is off the incoming queue, but hasn't 11676 * been inserted on any other queue. We may need to come 11677 * up with a holding queue while we wait for serialization 11678 * so that we have an idea of what we're waiting for from 11679 * the other side. 11680 */ 11681 mtx_unlock(&lun->lun_lock); 11682 return (retval); 11683 } 11684 11685 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11686 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11687 ctl_ooaq, ooa_links))) { 11688 case CTL_ACTION_BLOCK: 11689 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11690 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11691 blocked_links); 11692 mtx_unlock(&lun->lun_lock); 11693 return (retval); 11694 case CTL_ACTION_PASS: 11695 case CTL_ACTION_SKIP: 11696 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11697 mtx_unlock(&lun->lun_lock); 11698 ctl_enqueue_rtr((union ctl_io *)ctsio); 11699 break; 11700 case CTL_ACTION_OVERLAP: 11701 mtx_unlock(&lun->lun_lock); 11702 ctl_set_overlapped_cmd(ctsio); 11703 ctl_done((union ctl_io *)ctsio); 11704 break; 11705 case CTL_ACTION_OVERLAP_TAG: 11706 mtx_unlock(&lun->lun_lock); 11707 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11708 ctl_done((union ctl_io *)ctsio); 11709 break; 11710 case CTL_ACTION_ERROR: 11711 default: 11712 mtx_unlock(&lun->lun_lock); 11713 ctl_set_internal_failure(ctsio, 11714 /*sks_valid*/ 0, 11715 /*retry_count*/ 0); 11716 ctl_done((union ctl_io *)ctsio); 11717 break; 11718 } 11719 return (retval); 11720 } 11721 11722 const struct ctl_cmd_entry * 11723 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11724 { 11725 const struct ctl_cmd_entry *entry; 11726 int service_action; 11727 11728 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11729 if (sa) 11730 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11731 if (entry->flags & CTL_CMD_FLAG_SA5) { 11732 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11733 entry = &((const struct ctl_cmd_entry *) 11734 entry->execute)[service_action]; 11735 } 11736 return (entry); 11737 } 11738 11739 const struct ctl_cmd_entry * 11740 ctl_validate_command(struct ctl_scsiio *ctsio) 11741 { 11742 const struct ctl_cmd_entry *entry; 11743 int i, sa; 11744 uint8_t diff; 11745 11746 entry = ctl_get_cmd_entry(ctsio, &sa); 11747 if (entry->execute == NULL) { 11748 if (sa) 11749 ctl_set_invalid_field(ctsio, 11750 /*sks_valid*/ 1, 11751 /*command*/ 1, 11752 /*field*/ 1, 11753 /*bit_valid*/ 1, 11754 /*bit*/ 4); 11755 else 11756 ctl_set_invalid_opcode(ctsio); 11757 ctl_done((union ctl_io *)ctsio); 11758 return (NULL); 11759 } 11760 KASSERT(entry->length > 0, 11761 ("Not defined length for command 0x%02x/0x%02x", 11762 ctsio->cdb[0], ctsio->cdb[1])); 11763 for (i = 1; i < entry->length; i++) { 11764 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11765 if (diff == 0) 11766 continue; 11767 ctl_set_invalid_field(ctsio, 11768 /*sks_valid*/ 1, 11769 /*command*/ 1, 11770 /*field*/ i, 11771 /*bit_valid*/ 1, 11772 /*bit*/ fls(diff) - 1); 11773 ctl_done((union ctl_io *)ctsio); 11774 return (NULL); 11775 } 11776 return (entry); 11777 } 11778 11779 static int 11780 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11781 { 11782 11783 switch (lun_type) { 11784 case T_PROCESSOR: 11785 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11786 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11787 return (0); 11788 break; 11789 case T_DIRECT: 11790 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11791 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11792 return (0); 11793 break; 11794 default: 11795 return (0); 11796 } 11797 return (1); 11798 } 11799 11800 static int 11801 ctl_scsiio(struct ctl_scsiio *ctsio) 11802 { 11803 int retval; 11804 const struct ctl_cmd_entry *entry; 11805 11806 retval = CTL_RETVAL_COMPLETE; 11807 11808 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11809 11810 entry = ctl_get_cmd_entry(ctsio, NULL); 11811 11812 /* 11813 * If this I/O has been aborted, just send it straight to 11814 * ctl_done() without executing it. 11815 */ 11816 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11817 ctl_done((union ctl_io *)ctsio); 11818 goto bailout; 11819 } 11820 11821 /* 11822 * All the checks should have been handled by ctl_scsiio_precheck(). 11823 * We should be clear now to just execute the I/O. 11824 */ 11825 retval = entry->execute(ctsio); 11826 11827 bailout: 11828 return (retval); 11829 } 11830 11831 /* 11832 * Since we only implement one target right now, a bus reset simply resets 11833 * our single target. 11834 */ 11835 static int 11836 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11837 { 11838 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11839 } 11840 11841 static int 11842 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11843 ctl_ua_type ua_type) 11844 { 11845 struct ctl_lun *lun; 11846 int retval; 11847 11848 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11849 union ctl_ha_msg msg_info; 11850 11851 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11852 msg_info.hdr.nexus = io->io_hdr.nexus; 11853 if (ua_type==CTL_UA_TARG_RESET) 11854 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11855 else 11856 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11857 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11858 msg_info.hdr.original_sc = NULL; 11859 msg_info.hdr.serializing_sc = NULL; 11860 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11861 (void *)&msg_info, sizeof(msg_info), 0)) { 11862 } 11863 } 11864 retval = 0; 11865 11866 mtx_lock(&ctl_softc->ctl_lock); 11867 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11868 retval += ctl_lun_reset(lun, io, ua_type); 11869 mtx_unlock(&ctl_softc->ctl_lock); 11870 11871 return (retval); 11872 } 11873 11874 /* 11875 * The LUN should always be set. The I/O is optional, and is used to 11876 * distinguish between I/Os sent by this initiator, and by other 11877 * initiators. We set unit attention for initiators other than this one. 11878 * SAM-3 is vague on this point. It does say that a unit attention should 11879 * be established for other initiators when a LUN is reset (see section 11880 * 5.7.3), but it doesn't specifically say that the unit attention should 11881 * be established for this particular initiator when a LUN is reset. Here 11882 * is the relevant text, from SAM-3 rev 8: 11883 * 11884 * 5.7.2 When a SCSI initiator port aborts its own tasks 11885 * 11886 * When a SCSI initiator port causes its own task(s) to be aborted, no 11887 * notification that the task(s) have been aborted shall be returned to 11888 * the SCSI initiator port other than the completion response for the 11889 * command or task management function action that caused the task(s) to 11890 * be aborted and notification(s) associated with related effects of the 11891 * action (e.g., a reset unit attention condition). 11892 * 11893 * XXX KDM for now, we're setting unit attention for all initiators. 11894 */ 11895 static int 11896 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11897 { 11898 union ctl_io *xio; 11899 #if 0 11900 uint32_t initindex; 11901 #endif 11902 int i; 11903 11904 mtx_lock(&lun->lun_lock); 11905 /* 11906 * Run through the OOA queue and abort each I/O. 11907 */ 11908 #if 0 11909 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11910 #endif 11911 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11912 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11913 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11914 } 11915 11916 /* 11917 * This version sets unit attention for every 11918 */ 11919 #if 0 11920 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11921 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11922 if (initindex == i) 11923 continue; 11924 lun->pending_ua[i] |= ua_type; 11925 } 11926 #endif 11927 11928 /* 11929 * A reset (any kind, really) clears reservations established with 11930 * RESERVE/RELEASE. It does not clear reservations established 11931 * with PERSISTENT RESERVE OUT, but we don't support that at the 11932 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11933 * reservations made with the RESERVE/RELEASE commands, because 11934 * those commands are obsolete in SPC-3. 11935 */ 11936 lun->flags &= ~CTL_LUN_RESERVED; 11937 11938 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11939 #ifdef CTL_WITH_CA 11940 ctl_clear_mask(lun->have_ca, i); 11941 #endif 11942 lun->pending_ua[i] |= ua_type; 11943 } 11944 mtx_unlock(&lun->lun_lock); 11945 11946 return (0); 11947 } 11948 11949 static void 11950 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11951 int other_sc) 11952 { 11953 union ctl_io *xio; 11954 11955 mtx_assert(&lun->lun_lock, MA_OWNED); 11956 11957 /* 11958 * Run through the OOA queue and attempt to find the given I/O. 11959 * The target port, initiator ID, tag type and tag number have to 11960 * match the values that we got from the initiator. If we have an 11961 * untagged command to abort, simply abort the first untagged command 11962 * we come to. We only allow one untagged command at a time of course. 11963 */ 11964 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11965 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11966 11967 if ((targ_port == UINT32_MAX || 11968 targ_port == xio->io_hdr.nexus.targ_port) && 11969 (init_id == UINT32_MAX || 11970 init_id == xio->io_hdr.nexus.initid.id)) { 11971 if (targ_port != xio->io_hdr.nexus.targ_port || 11972 init_id != xio->io_hdr.nexus.initid.id) 11973 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11974 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11975 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11976 union ctl_ha_msg msg_info; 11977 11978 msg_info.hdr.nexus = xio->io_hdr.nexus; 11979 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11980 msg_info.task.tag_num = xio->scsiio.tag_num; 11981 msg_info.task.tag_type = xio->scsiio.tag_type; 11982 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11983 msg_info.hdr.original_sc = NULL; 11984 msg_info.hdr.serializing_sc = NULL; 11985 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11986 (void *)&msg_info, sizeof(msg_info), 0); 11987 } 11988 } 11989 } 11990 } 11991 11992 static int 11993 ctl_abort_task_set(union ctl_io *io) 11994 { 11995 struct ctl_softc *softc = control_softc; 11996 struct ctl_lun *lun; 11997 uint32_t targ_lun; 11998 11999 /* 12000 * Look up the LUN. 12001 */ 12002 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12003 mtx_lock(&softc->ctl_lock); 12004 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12005 lun = softc->ctl_luns[targ_lun]; 12006 else { 12007 mtx_unlock(&softc->ctl_lock); 12008 return (1); 12009 } 12010 12011 mtx_lock(&lun->lun_lock); 12012 mtx_unlock(&softc->ctl_lock); 12013 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12014 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12015 io->io_hdr.nexus.initid.id, 12016 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12017 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12018 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12019 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12020 } 12021 mtx_unlock(&lun->lun_lock); 12022 return (0); 12023 } 12024 12025 static int 12026 ctl_i_t_nexus_reset(union ctl_io *io) 12027 { 12028 struct ctl_softc *softc = control_softc; 12029 struct ctl_lun *lun; 12030 uint32_t initindex, residx; 12031 12032 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12033 residx = ctl_get_resindex(&io->io_hdr.nexus); 12034 mtx_lock(&softc->ctl_lock); 12035 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12036 mtx_lock(&lun->lun_lock); 12037 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12038 io->io_hdr.nexus.initid.id, 12039 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12040 #ifdef CTL_WITH_CA 12041 ctl_clear_mask(lun->have_ca, initindex); 12042 #endif 12043 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12044 lun->flags &= ~CTL_LUN_RESERVED; 12045 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12046 mtx_unlock(&lun->lun_lock); 12047 } 12048 mtx_unlock(&softc->ctl_lock); 12049 return (0); 12050 } 12051 12052 static int 12053 ctl_abort_task(union ctl_io *io) 12054 { 12055 union ctl_io *xio; 12056 struct ctl_lun *lun; 12057 struct ctl_softc *ctl_softc; 12058 #if 0 12059 struct sbuf sb; 12060 char printbuf[128]; 12061 #endif 12062 int found; 12063 uint32_t targ_lun; 12064 12065 ctl_softc = control_softc; 12066 found = 0; 12067 12068 /* 12069 * Look up the LUN. 12070 */ 12071 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12072 mtx_lock(&ctl_softc->ctl_lock); 12073 if ((targ_lun < CTL_MAX_LUNS) 12074 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12075 lun = ctl_softc->ctl_luns[targ_lun]; 12076 else { 12077 mtx_unlock(&ctl_softc->ctl_lock); 12078 return (1); 12079 } 12080 12081 #if 0 12082 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12083 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12084 #endif 12085 12086 mtx_lock(&lun->lun_lock); 12087 mtx_unlock(&ctl_softc->ctl_lock); 12088 /* 12089 * Run through the OOA queue and attempt to find the given I/O. 12090 * The target port, initiator ID, tag type and tag number have to 12091 * match the values that we got from the initiator. If we have an 12092 * untagged command to abort, simply abort the first untagged command 12093 * we come to. We only allow one untagged command at a time of course. 12094 */ 12095 #if 0 12096 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12097 #endif 12098 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12099 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12100 #if 0 12101 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12102 12103 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12104 lun->lun, xio->scsiio.tag_num, 12105 xio->scsiio.tag_type, 12106 (xio->io_hdr.blocked_links.tqe_prev 12107 == NULL) ? "" : " BLOCKED", 12108 (xio->io_hdr.flags & 12109 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12110 (xio->io_hdr.flags & 12111 CTL_FLAG_ABORT) ? " ABORT" : "", 12112 (xio->io_hdr.flags & 12113 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12114 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12115 sbuf_finish(&sb); 12116 printf("%s\n", sbuf_data(&sb)); 12117 #endif 12118 12119 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12120 && (xio->io_hdr.nexus.initid.id == 12121 io->io_hdr.nexus.initid.id)) { 12122 /* 12123 * If the abort says that the task is untagged, the 12124 * task in the queue must be untagged. Otherwise, 12125 * we just check to see whether the tag numbers 12126 * match. This is because the QLogic firmware 12127 * doesn't pass back the tag type in an abort 12128 * request. 12129 */ 12130 #if 0 12131 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12132 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12133 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12134 #endif 12135 /* 12136 * XXX KDM we've got problems with FC, because it 12137 * doesn't send down a tag type with aborts. So we 12138 * can only really go by the tag number... 12139 * This may cause problems with parallel SCSI. 12140 * Need to figure that out!! 12141 */ 12142 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12143 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12144 found = 1; 12145 if ((io->io_hdr.flags & 12146 CTL_FLAG_FROM_OTHER_SC) == 0 && 12147 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12148 union ctl_ha_msg msg_info; 12149 12150 io->io_hdr.flags |= 12151 CTL_FLAG_SENT_2OTHER_SC; 12152 msg_info.hdr.nexus = io->io_hdr.nexus; 12153 msg_info.task.task_action = 12154 CTL_TASK_ABORT_TASK; 12155 msg_info.task.tag_num = 12156 io->taskio.tag_num; 12157 msg_info.task.tag_type = 12158 io->taskio.tag_type; 12159 msg_info.hdr.msg_type = 12160 CTL_MSG_MANAGE_TASKS; 12161 msg_info.hdr.original_sc = NULL; 12162 msg_info.hdr.serializing_sc = NULL; 12163 #if 0 12164 printf("Sent Abort to other side\n"); 12165 #endif 12166 if (CTL_HA_STATUS_SUCCESS != 12167 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12168 (void *)&msg_info, 12169 sizeof(msg_info), 0)) { 12170 } 12171 } 12172 #if 0 12173 printf("ctl_abort_task: found I/O to abort\n"); 12174 #endif 12175 break; 12176 } 12177 } 12178 } 12179 mtx_unlock(&lun->lun_lock); 12180 12181 if (found == 0) { 12182 /* 12183 * This isn't really an error. It's entirely possible for 12184 * the abort and command completion to cross on the wire. 12185 * This is more of an informative/diagnostic error. 12186 */ 12187 #if 0 12188 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12189 "%d:%d:%d:%d tag %d type %d\n", 12190 io->io_hdr.nexus.initid.id, 12191 io->io_hdr.nexus.targ_port, 12192 io->io_hdr.nexus.targ_target.id, 12193 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12194 io->taskio.tag_type); 12195 #endif 12196 } 12197 return (0); 12198 } 12199 12200 static void 12201 ctl_run_task(union ctl_io *io) 12202 { 12203 struct ctl_softc *ctl_softc = control_softc; 12204 int retval = 1; 12205 const char *task_desc; 12206 12207 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12208 12209 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12210 ("ctl_run_task: Unextected io_type %d\n", 12211 io->io_hdr.io_type)); 12212 12213 task_desc = ctl_scsi_task_string(&io->taskio); 12214 if (task_desc != NULL) { 12215 #ifdef NEEDTOPORT 12216 csevent_log(CSC_CTL | CSC_SHELF_SW | 12217 CTL_TASK_REPORT, 12218 csevent_LogType_Trace, 12219 csevent_Severity_Information, 12220 csevent_AlertLevel_Green, 12221 csevent_FRU_Firmware, 12222 csevent_FRU_Unknown, 12223 "CTL: received task: %s",task_desc); 12224 #endif 12225 } else { 12226 #ifdef NEEDTOPORT 12227 csevent_log(CSC_CTL | CSC_SHELF_SW | 12228 CTL_TASK_REPORT, 12229 csevent_LogType_Trace, 12230 csevent_Severity_Information, 12231 csevent_AlertLevel_Green, 12232 csevent_FRU_Firmware, 12233 csevent_FRU_Unknown, 12234 "CTL: received unknown task " 12235 "type: %d (%#x)", 12236 io->taskio.task_action, 12237 io->taskio.task_action); 12238 #endif 12239 } 12240 switch (io->taskio.task_action) { 12241 case CTL_TASK_ABORT_TASK: 12242 retval = ctl_abort_task(io); 12243 break; 12244 case CTL_TASK_ABORT_TASK_SET: 12245 case CTL_TASK_CLEAR_TASK_SET: 12246 retval = ctl_abort_task_set(io); 12247 break; 12248 case CTL_TASK_CLEAR_ACA: 12249 break; 12250 case CTL_TASK_I_T_NEXUS_RESET: 12251 retval = ctl_i_t_nexus_reset(io); 12252 break; 12253 case CTL_TASK_LUN_RESET: { 12254 struct ctl_lun *lun; 12255 uint32_t targ_lun; 12256 12257 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12258 mtx_lock(&ctl_softc->ctl_lock); 12259 if ((targ_lun < CTL_MAX_LUNS) 12260 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12261 lun = ctl_softc->ctl_luns[targ_lun]; 12262 else { 12263 mtx_unlock(&ctl_softc->ctl_lock); 12264 retval = 1; 12265 break; 12266 } 12267 12268 if (!(io->io_hdr.flags & 12269 CTL_FLAG_FROM_OTHER_SC)) { 12270 union ctl_ha_msg msg_info; 12271 12272 io->io_hdr.flags |= 12273 CTL_FLAG_SENT_2OTHER_SC; 12274 msg_info.hdr.msg_type = 12275 CTL_MSG_MANAGE_TASKS; 12276 msg_info.hdr.nexus = io->io_hdr.nexus; 12277 msg_info.task.task_action = 12278 CTL_TASK_LUN_RESET; 12279 msg_info.hdr.original_sc = NULL; 12280 msg_info.hdr.serializing_sc = NULL; 12281 if (CTL_HA_STATUS_SUCCESS != 12282 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12283 (void *)&msg_info, 12284 sizeof(msg_info), 0)) { 12285 } 12286 } 12287 12288 retval = ctl_lun_reset(lun, io, 12289 CTL_UA_LUN_RESET); 12290 mtx_unlock(&ctl_softc->ctl_lock); 12291 break; 12292 } 12293 case CTL_TASK_TARGET_RESET: 12294 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12295 break; 12296 case CTL_TASK_BUS_RESET: 12297 retval = ctl_bus_reset(ctl_softc, io); 12298 break; 12299 case CTL_TASK_PORT_LOGIN: 12300 break; 12301 case CTL_TASK_PORT_LOGOUT: 12302 break; 12303 default: 12304 printf("ctl_run_task: got unknown task management event %d\n", 12305 io->taskio.task_action); 12306 break; 12307 } 12308 if (retval == 0) 12309 io->io_hdr.status = CTL_SUCCESS; 12310 else 12311 io->io_hdr.status = CTL_ERROR; 12312 ctl_done(io); 12313 } 12314 12315 /* 12316 * For HA operation. Handle commands that come in from the other 12317 * controller. 12318 */ 12319 static void 12320 ctl_handle_isc(union ctl_io *io) 12321 { 12322 int free_io; 12323 struct ctl_lun *lun; 12324 struct ctl_softc *ctl_softc; 12325 uint32_t targ_lun; 12326 12327 ctl_softc = control_softc; 12328 12329 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12330 lun = ctl_softc->ctl_luns[targ_lun]; 12331 12332 switch (io->io_hdr.msg_type) { 12333 case CTL_MSG_SERIALIZE: 12334 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12335 break; 12336 case CTL_MSG_R2R: { 12337 const struct ctl_cmd_entry *entry; 12338 12339 /* 12340 * This is only used in SER_ONLY mode. 12341 */ 12342 free_io = 0; 12343 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12344 mtx_lock(&lun->lun_lock); 12345 if (ctl_scsiio_lun_check(ctl_softc, lun, 12346 entry, (struct ctl_scsiio *)io) != 0) { 12347 mtx_unlock(&lun->lun_lock); 12348 ctl_done(io); 12349 break; 12350 } 12351 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12352 mtx_unlock(&lun->lun_lock); 12353 ctl_enqueue_rtr(io); 12354 break; 12355 } 12356 case CTL_MSG_FINISH_IO: 12357 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12358 free_io = 0; 12359 ctl_done(io); 12360 } else { 12361 free_io = 1; 12362 mtx_lock(&lun->lun_lock); 12363 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12364 ooa_links); 12365 ctl_check_blocked(lun); 12366 mtx_unlock(&lun->lun_lock); 12367 } 12368 break; 12369 case CTL_MSG_PERS_ACTION: 12370 ctl_hndl_per_res_out_on_other_sc( 12371 (union ctl_ha_msg *)&io->presio.pr_msg); 12372 free_io = 1; 12373 break; 12374 case CTL_MSG_BAD_JUJU: 12375 free_io = 0; 12376 ctl_done(io); 12377 break; 12378 case CTL_MSG_DATAMOVE: 12379 /* Only used in XFER mode */ 12380 free_io = 0; 12381 ctl_datamove_remote(io); 12382 break; 12383 case CTL_MSG_DATAMOVE_DONE: 12384 /* Only used in XFER mode */ 12385 free_io = 0; 12386 io->scsiio.be_move_done(io); 12387 break; 12388 default: 12389 free_io = 1; 12390 printf("%s: Invalid message type %d\n", 12391 __func__, io->io_hdr.msg_type); 12392 break; 12393 } 12394 if (free_io) 12395 ctl_free_io(io); 12396 12397 } 12398 12399 12400 /* 12401 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12402 * there is no match. 12403 */ 12404 static ctl_lun_error_pattern 12405 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12406 { 12407 const struct ctl_cmd_entry *entry; 12408 ctl_lun_error_pattern filtered_pattern, pattern; 12409 12410 pattern = desc->error_pattern; 12411 12412 /* 12413 * XXX KDM we need more data passed into this function to match a 12414 * custom pattern, and we actually need to implement custom pattern 12415 * matching. 12416 */ 12417 if (pattern & CTL_LUN_PAT_CMD) 12418 return (CTL_LUN_PAT_CMD); 12419 12420 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12421 return (CTL_LUN_PAT_ANY); 12422 12423 entry = ctl_get_cmd_entry(ctsio, NULL); 12424 12425 filtered_pattern = entry->pattern & pattern; 12426 12427 /* 12428 * If the user requested specific flags in the pattern (e.g. 12429 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12430 * flags. 12431 * 12432 * If the user did not specify any flags, it doesn't matter whether 12433 * or not the command supports the flags. 12434 */ 12435 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12436 (pattern & ~CTL_LUN_PAT_MASK)) 12437 return (CTL_LUN_PAT_NONE); 12438 12439 /* 12440 * If the user asked for a range check, see if the requested LBA 12441 * range overlaps with this command's LBA range. 12442 */ 12443 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12444 uint64_t lba1; 12445 uint64_t len1; 12446 ctl_action action; 12447 int retval; 12448 12449 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12450 if (retval != 0) 12451 return (CTL_LUN_PAT_NONE); 12452 12453 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12454 desc->lba_range.len); 12455 /* 12456 * A "pass" means that the LBA ranges don't overlap, so 12457 * this doesn't match the user's range criteria. 12458 */ 12459 if (action == CTL_ACTION_PASS) 12460 return (CTL_LUN_PAT_NONE); 12461 } 12462 12463 return (filtered_pattern); 12464 } 12465 12466 static void 12467 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12468 { 12469 struct ctl_error_desc *desc, *desc2; 12470 12471 mtx_assert(&lun->lun_lock, MA_OWNED); 12472 12473 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12474 ctl_lun_error_pattern pattern; 12475 /* 12476 * Check to see whether this particular command matches 12477 * the pattern in the descriptor. 12478 */ 12479 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12480 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12481 continue; 12482 12483 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12484 case CTL_LUN_INJ_ABORTED: 12485 ctl_set_aborted(&io->scsiio); 12486 break; 12487 case CTL_LUN_INJ_MEDIUM_ERR: 12488 ctl_set_medium_error(&io->scsiio); 12489 break; 12490 case CTL_LUN_INJ_UA: 12491 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12492 * OCCURRED */ 12493 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12494 break; 12495 case CTL_LUN_INJ_CUSTOM: 12496 /* 12497 * We're assuming the user knows what he is doing. 12498 * Just copy the sense information without doing 12499 * checks. 12500 */ 12501 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12502 ctl_min(sizeof(desc->custom_sense), 12503 sizeof(io->scsiio.sense_data))); 12504 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12505 io->scsiio.sense_len = SSD_FULL_SIZE; 12506 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12507 break; 12508 case CTL_LUN_INJ_NONE: 12509 default: 12510 /* 12511 * If this is an error injection type we don't know 12512 * about, clear the continuous flag (if it is set) 12513 * so it will get deleted below. 12514 */ 12515 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12516 break; 12517 } 12518 /* 12519 * By default, each error injection action is a one-shot 12520 */ 12521 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12522 continue; 12523 12524 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12525 12526 free(desc, M_CTL); 12527 } 12528 } 12529 12530 #ifdef CTL_IO_DELAY 12531 static void 12532 ctl_datamove_timer_wakeup(void *arg) 12533 { 12534 union ctl_io *io; 12535 12536 io = (union ctl_io *)arg; 12537 12538 ctl_datamove(io); 12539 } 12540 #endif /* CTL_IO_DELAY */ 12541 12542 void 12543 ctl_datamove(union ctl_io *io) 12544 { 12545 void (*fe_datamove)(union ctl_io *io); 12546 12547 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12548 12549 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12550 12551 #ifdef CTL_TIME_IO 12552 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12553 char str[256]; 12554 char path_str[64]; 12555 struct sbuf sb; 12556 12557 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12558 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12559 12560 sbuf_cat(&sb, path_str); 12561 switch (io->io_hdr.io_type) { 12562 case CTL_IO_SCSI: 12563 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12564 sbuf_printf(&sb, "\n"); 12565 sbuf_cat(&sb, path_str); 12566 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12567 io->scsiio.tag_num, io->scsiio.tag_type); 12568 break; 12569 case CTL_IO_TASK: 12570 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12571 "Tag Type: %d\n", io->taskio.task_action, 12572 io->taskio.tag_num, io->taskio.tag_type); 12573 break; 12574 default: 12575 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12576 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12577 break; 12578 } 12579 sbuf_cat(&sb, path_str); 12580 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12581 (intmax_t)time_uptime - io->io_hdr.start_time); 12582 sbuf_finish(&sb); 12583 printf("%s", sbuf_data(&sb)); 12584 } 12585 #endif /* CTL_TIME_IO */ 12586 12587 #ifdef CTL_IO_DELAY 12588 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12589 struct ctl_lun *lun; 12590 12591 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12592 12593 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12594 } else { 12595 struct ctl_lun *lun; 12596 12597 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12598 if ((lun != NULL) 12599 && (lun->delay_info.datamove_delay > 0)) { 12600 struct callout *callout; 12601 12602 callout = (struct callout *)&io->io_hdr.timer_bytes; 12603 callout_init(callout, /*mpsafe*/ 1); 12604 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12605 callout_reset(callout, 12606 lun->delay_info.datamove_delay * hz, 12607 ctl_datamove_timer_wakeup, io); 12608 if (lun->delay_info.datamove_type == 12609 CTL_DELAY_TYPE_ONESHOT) 12610 lun->delay_info.datamove_delay = 0; 12611 return; 12612 } 12613 } 12614 #endif 12615 12616 /* 12617 * This command has been aborted. Set the port status, so we fail 12618 * the data move. 12619 */ 12620 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12621 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12622 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12623 io->io_hdr.nexus.targ_port, 12624 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12625 io->io_hdr.nexus.targ_lun); 12626 io->io_hdr.port_status = 31337; 12627 /* 12628 * Note that the backend, in this case, will get the 12629 * callback in its context. In other cases it may get 12630 * called in the frontend's interrupt thread context. 12631 */ 12632 io->scsiio.be_move_done(io); 12633 return; 12634 } 12635 12636 /* Don't confuse frontend with zero length data move. */ 12637 if (io->scsiio.kern_data_len == 0) { 12638 io->scsiio.be_move_done(io); 12639 return; 12640 } 12641 12642 /* 12643 * If we're in XFER mode and this I/O is from the other shelf 12644 * controller, we need to send the DMA to the other side to 12645 * actually transfer the data to/from the host. In serialize only 12646 * mode the transfer happens below CTL and ctl_datamove() is only 12647 * called on the machine that originally received the I/O. 12648 */ 12649 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12650 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12651 union ctl_ha_msg msg; 12652 uint32_t sg_entries_sent; 12653 int do_sg_copy; 12654 int i; 12655 12656 memset(&msg, 0, sizeof(msg)); 12657 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12658 msg.hdr.original_sc = io->io_hdr.original_sc; 12659 msg.hdr.serializing_sc = io; 12660 msg.hdr.nexus = io->io_hdr.nexus; 12661 msg.dt.flags = io->io_hdr.flags; 12662 /* 12663 * We convert everything into a S/G list here. We can't 12664 * pass by reference, only by value between controllers. 12665 * So we can't pass a pointer to the S/G list, only as many 12666 * S/G entries as we can fit in here. If it's possible for 12667 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12668 * then we need to break this up into multiple transfers. 12669 */ 12670 if (io->scsiio.kern_sg_entries == 0) { 12671 msg.dt.kern_sg_entries = 1; 12672 /* 12673 * If this is in cached memory, flush the cache 12674 * before we send the DMA request to the other 12675 * controller. We want to do this in either the 12676 * read or the write case. The read case is 12677 * straightforward. In the write case, we want to 12678 * make sure nothing is in the local cache that 12679 * could overwrite the DMAed data. 12680 */ 12681 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12682 /* 12683 * XXX KDM use bus_dmamap_sync() here. 12684 */ 12685 } 12686 12687 /* 12688 * Convert to a physical address if this is a 12689 * virtual address. 12690 */ 12691 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12692 msg.dt.sg_list[0].addr = 12693 io->scsiio.kern_data_ptr; 12694 } else { 12695 /* 12696 * XXX KDM use busdma here! 12697 */ 12698 #if 0 12699 msg.dt.sg_list[0].addr = (void *) 12700 vtophys(io->scsiio.kern_data_ptr); 12701 #endif 12702 } 12703 12704 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12705 do_sg_copy = 0; 12706 } else { 12707 struct ctl_sg_entry *sgl; 12708 12709 do_sg_copy = 1; 12710 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12711 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12712 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12713 /* 12714 * XXX KDM use bus_dmamap_sync() here. 12715 */ 12716 } 12717 } 12718 12719 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12720 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12721 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12722 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12723 msg.dt.sg_sequence = 0; 12724 12725 /* 12726 * Loop until we've sent all of the S/G entries. On the 12727 * other end, we'll recompose these S/G entries into one 12728 * contiguous list before passing it to the 12729 */ 12730 for (sg_entries_sent = 0; sg_entries_sent < 12731 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12732 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12733 sizeof(msg.dt.sg_list[0])), 12734 msg.dt.kern_sg_entries - sg_entries_sent); 12735 12736 if (do_sg_copy != 0) { 12737 struct ctl_sg_entry *sgl; 12738 int j; 12739 12740 sgl = (struct ctl_sg_entry *) 12741 io->scsiio.kern_data_ptr; 12742 /* 12743 * If this is in cached memory, flush the cache 12744 * before we send the DMA request to the other 12745 * controller. We want to do this in either 12746 * the * read or the write case. The read 12747 * case is straightforward. In the write 12748 * case, we want to make sure nothing is 12749 * in the local cache that could overwrite 12750 * the DMAed data. 12751 */ 12752 12753 for (i = sg_entries_sent, j = 0; 12754 i < msg.dt.cur_sg_entries; i++, j++) { 12755 if ((io->io_hdr.flags & 12756 CTL_FLAG_NO_DATASYNC) == 0) { 12757 /* 12758 * XXX KDM use bus_dmamap_sync() 12759 */ 12760 } 12761 if ((io->io_hdr.flags & 12762 CTL_FLAG_BUS_ADDR) == 0) { 12763 /* 12764 * XXX KDM use busdma. 12765 */ 12766 #if 0 12767 msg.dt.sg_list[j].addr =(void *) 12768 vtophys(sgl[i].addr); 12769 #endif 12770 } else { 12771 msg.dt.sg_list[j].addr = 12772 sgl[i].addr; 12773 } 12774 msg.dt.sg_list[j].len = sgl[i].len; 12775 } 12776 } 12777 12778 sg_entries_sent += msg.dt.cur_sg_entries; 12779 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12780 msg.dt.sg_last = 1; 12781 else 12782 msg.dt.sg_last = 0; 12783 12784 /* 12785 * XXX KDM drop and reacquire the lock here? 12786 */ 12787 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12788 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12789 /* 12790 * XXX do something here. 12791 */ 12792 } 12793 12794 msg.dt.sent_sg_entries = sg_entries_sent; 12795 } 12796 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12797 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12798 ctl_failover_io(io, /*have_lock*/ 0); 12799 12800 } else { 12801 12802 /* 12803 * Lookup the fe_datamove() function for this particular 12804 * front end. 12805 */ 12806 fe_datamove = 12807 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12808 12809 fe_datamove(io); 12810 } 12811 } 12812 12813 static void 12814 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12815 { 12816 union ctl_ha_msg msg; 12817 int isc_status; 12818 12819 memset(&msg, 0, sizeof(msg)); 12820 12821 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12822 msg.hdr.original_sc = io; 12823 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12824 msg.hdr.nexus = io->io_hdr.nexus; 12825 msg.hdr.status = io->io_hdr.status; 12826 msg.scsi.tag_num = io->scsiio.tag_num; 12827 msg.scsi.tag_type = io->scsiio.tag_type; 12828 msg.scsi.scsi_status = io->scsiio.scsi_status; 12829 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12830 sizeof(io->scsiio.sense_data)); 12831 msg.scsi.sense_len = io->scsiio.sense_len; 12832 msg.scsi.sense_residual = io->scsiio.sense_residual; 12833 msg.scsi.fetd_status = io->io_hdr.port_status; 12834 msg.scsi.residual = io->scsiio.residual; 12835 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12836 12837 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12838 ctl_failover_io(io, /*have_lock*/ have_lock); 12839 return; 12840 } 12841 12842 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12843 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12844 /* XXX do something if this fails */ 12845 } 12846 12847 } 12848 12849 /* 12850 * The DMA to the remote side is done, now we need to tell the other side 12851 * we're done so it can continue with its data movement. 12852 */ 12853 static void 12854 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12855 { 12856 union ctl_io *io; 12857 12858 io = rq->context; 12859 12860 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12861 printf("%s: ISC DMA write failed with error %d", __func__, 12862 rq->ret); 12863 ctl_set_internal_failure(&io->scsiio, 12864 /*sks_valid*/ 1, 12865 /*retry_count*/ rq->ret); 12866 } 12867 12868 ctl_dt_req_free(rq); 12869 12870 /* 12871 * In this case, we had to malloc the memory locally. Free it. 12872 */ 12873 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12874 int i; 12875 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12876 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12877 } 12878 /* 12879 * The data is in local and remote memory, so now we need to send 12880 * status (good or back) back to the other side. 12881 */ 12882 ctl_send_datamove_done(io, /*have_lock*/ 0); 12883 } 12884 12885 /* 12886 * We've moved the data from the host/controller into local memory. Now we 12887 * need to push it over to the remote controller's memory. 12888 */ 12889 static int 12890 ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12891 { 12892 int retval; 12893 12894 retval = 0; 12895 12896 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12897 ctl_datamove_remote_write_cb); 12898 12899 return (retval); 12900 } 12901 12902 static void 12903 ctl_datamove_remote_write(union ctl_io *io) 12904 { 12905 int retval; 12906 void (*fe_datamove)(union ctl_io *io); 12907 12908 /* 12909 * - Get the data from the host/HBA into local memory. 12910 * - DMA memory from the local controller to the remote controller. 12911 * - Send status back to the remote controller. 12912 */ 12913 12914 retval = ctl_datamove_remote_sgl_setup(io); 12915 if (retval != 0) 12916 return; 12917 12918 /* Switch the pointer over so the FETD knows what to do */ 12919 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12920 12921 /* 12922 * Use a custom move done callback, since we need to send completion 12923 * back to the other controller, not to the backend on this side. 12924 */ 12925 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12926 12927 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12928 12929 fe_datamove(io); 12930 12931 return; 12932 12933 } 12934 12935 static int 12936 ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12937 { 12938 #if 0 12939 char str[256]; 12940 char path_str[64]; 12941 struct sbuf sb; 12942 #endif 12943 12944 /* 12945 * In this case, we had to malloc the memory locally. Free it. 12946 */ 12947 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12948 int i; 12949 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12950 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12951 } 12952 12953 #if 0 12954 scsi_path_string(io, path_str, sizeof(path_str)); 12955 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12956 sbuf_cat(&sb, path_str); 12957 scsi_command_string(&io->scsiio, NULL, &sb); 12958 sbuf_printf(&sb, "\n"); 12959 sbuf_cat(&sb, path_str); 12960 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12961 io->scsiio.tag_num, io->scsiio.tag_type); 12962 sbuf_cat(&sb, path_str); 12963 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12964 io->io_hdr.flags, io->io_hdr.status); 12965 sbuf_finish(&sb); 12966 printk("%s", sbuf_data(&sb)); 12967 #endif 12968 12969 12970 /* 12971 * The read is done, now we need to send status (good or bad) back 12972 * to the other side. 12973 */ 12974 ctl_send_datamove_done(io, /*have_lock*/ 0); 12975 12976 return (0); 12977 } 12978 12979 static void 12980 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12981 { 12982 union ctl_io *io; 12983 void (*fe_datamove)(union ctl_io *io); 12984 12985 io = rq->context; 12986 12987 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12988 printf("%s: ISC DMA read failed with error %d", __func__, 12989 rq->ret); 12990 ctl_set_internal_failure(&io->scsiio, 12991 /*sks_valid*/ 1, 12992 /*retry_count*/ rq->ret); 12993 } 12994 12995 ctl_dt_req_free(rq); 12996 12997 /* Switch the pointer over so the FETD knows what to do */ 12998 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12999 13000 /* 13001 * Use a custom move done callback, since we need to send completion 13002 * back to the other controller, not to the backend on this side. 13003 */ 13004 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13005 13006 /* XXX KDM add checks like the ones in ctl_datamove? */ 13007 13008 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13009 13010 fe_datamove(io); 13011 } 13012 13013 static int 13014 ctl_datamove_remote_sgl_setup(union ctl_io *io) 13015 { 13016 struct ctl_sg_entry *local_sglist, *remote_sglist; 13017 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13018 struct ctl_softc *softc; 13019 int retval; 13020 int i; 13021 13022 retval = 0; 13023 softc = control_softc; 13024 13025 local_sglist = io->io_hdr.local_sglist; 13026 local_dma_sglist = io->io_hdr.local_dma_sglist; 13027 remote_sglist = io->io_hdr.remote_sglist; 13028 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13029 13030 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13031 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13032 local_sglist[i].len = remote_sglist[i].len; 13033 13034 /* 13035 * XXX Detect the situation where the RS-level I/O 13036 * redirector on the other side has already read the 13037 * data off of the AOR RS on this side, and 13038 * transferred it to remote (mirror) memory on the 13039 * other side. Since we already have the data in 13040 * memory here, we just need to use it. 13041 * 13042 * XXX KDM this can probably be removed once we 13043 * get the cache device code in and take the 13044 * current AOR implementation out. 13045 */ 13046 #ifdef NEEDTOPORT 13047 if ((remote_sglist[i].addr >= 13048 (void *)vtophys(softc->mirr->addr)) 13049 && (remote_sglist[i].addr < 13050 ((void *)vtophys(softc->mirr->addr) + 13051 CacheMirrorOffset))) { 13052 local_sglist[i].addr = remote_sglist[i].addr - 13053 CacheMirrorOffset; 13054 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13055 CTL_FLAG_DATA_IN) 13056 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13057 } else { 13058 local_sglist[i].addr = remote_sglist[i].addr + 13059 CacheMirrorOffset; 13060 } 13061 #endif 13062 #if 0 13063 printf("%s: local %p, remote %p, len %d\n", 13064 __func__, local_sglist[i].addr, 13065 remote_sglist[i].addr, local_sglist[i].len); 13066 #endif 13067 } 13068 } else { 13069 uint32_t len_to_go; 13070 13071 /* 13072 * In this case, we don't have automatically allocated 13073 * memory for this I/O on this controller. This typically 13074 * happens with internal CTL I/O -- e.g. inquiry, mode 13075 * sense, etc. Anything coming from RAIDCore will have 13076 * a mirror area available. 13077 */ 13078 len_to_go = io->scsiio.kern_data_len; 13079 13080 /* 13081 * Clear the no datasync flag, we have to use malloced 13082 * buffers. 13083 */ 13084 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13085 13086 /* 13087 * The difficult thing here is that the size of the various 13088 * S/G segments may be different than the size from the 13089 * remote controller. That'll make it harder when DMAing 13090 * the data back to the other side. 13091 */ 13092 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13093 sizeof(io->io_hdr.remote_sglist[0])) && 13094 (len_to_go > 0); i++) { 13095 local_sglist[i].len = ctl_min(len_to_go, 131072); 13096 CTL_SIZE_8B(local_dma_sglist[i].len, 13097 local_sglist[i].len); 13098 local_sglist[i].addr = 13099 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13100 13101 local_dma_sglist[i].addr = local_sglist[i].addr; 13102 13103 if (local_sglist[i].addr == NULL) { 13104 int j; 13105 13106 printf("malloc failed for %zd bytes!", 13107 local_dma_sglist[i].len); 13108 for (j = 0; j < i; j++) { 13109 free(local_sglist[j].addr, M_CTL); 13110 } 13111 ctl_set_internal_failure(&io->scsiio, 13112 /*sks_valid*/ 1, 13113 /*retry_count*/ 4857); 13114 retval = 1; 13115 goto bailout_error; 13116 13117 } 13118 /* XXX KDM do we need a sync here? */ 13119 13120 len_to_go -= local_sglist[i].len; 13121 } 13122 /* 13123 * Reset the number of S/G entries accordingly. The 13124 * original number of S/G entries is available in 13125 * rem_sg_entries. 13126 */ 13127 io->scsiio.kern_sg_entries = i; 13128 13129 #if 0 13130 printf("%s: kern_sg_entries = %d\n", __func__, 13131 io->scsiio.kern_sg_entries); 13132 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13133 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13134 local_sglist[i].addr, local_sglist[i].len, 13135 local_dma_sglist[i].len); 13136 #endif 13137 } 13138 13139 13140 return (retval); 13141 13142 bailout_error: 13143 13144 ctl_send_datamove_done(io, /*have_lock*/ 0); 13145 13146 return (retval); 13147 } 13148 13149 static int 13150 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13151 ctl_ha_dt_cb callback) 13152 { 13153 struct ctl_ha_dt_req *rq; 13154 struct ctl_sg_entry *remote_sglist, *local_sglist; 13155 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13156 uint32_t local_used, remote_used, total_used; 13157 int retval; 13158 int i, j; 13159 13160 retval = 0; 13161 13162 rq = ctl_dt_req_alloc(); 13163 13164 /* 13165 * If we failed to allocate the request, and if the DMA didn't fail 13166 * anyway, set busy status. This is just a resource allocation 13167 * failure. 13168 */ 13169 if ((rq == NULL) 13170 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13171 ctl_set_busy(&io->scsiio); 13172 13173 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13174 13175 if (rq != NULL) 13176 ctl_dt_req_free(rq); 13177 13178 /* 13179 * The data move failed. We need to return status back 13180 * to the other controller. No point in trying to DMA 13181 * data to the remote controller. 13182 */ 13183 13184 ctl_send_datamove_done(io, /*have_lock*/ 0); 13185 13186 retval = 1; 13187 13188 goto bailout; 13189 } 13190 13191 local_sglist = io->io_hdr.local_sglist; 13192 local_dma_sglist = io->io_hdr.local_dma_sglist; 13193 remote_sglist = io->io_hdr.remote_sglist; 13194 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13195 local_used = 0; 13196 remote_used = 0; 13197 total_used = 0; 13198 13199 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13200 rq->ret = CTL_HA_STATUS_SUCCESS; 13201 rq->context = io; 13202 callback(rq); 13203 goto bailout; 13204 } 13205 13206 /* 13207 * Pull/push the data over the wire from/to the other controller. 13208 * This takes into account the possibility that the local and 13209 * remote sglists may not be identical in terms of the size of 13210 * the elements and the number of elements. 13211 * 13212 * One fundamental assumption here is that the length allocated for 13213 * both the local and remote sglists is identical. Otherwise, we've 13214 * essentially got a coding error of some sort. 13215 */ 13216 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13217 int isc_ret; 13218 uint32_t cur_len, dma_length; 13219 uint8_t *tmp_ptr; 13220 13221 rq->id = CTL_HA_DATA_CTL; 13222 rq->command = command; 13223 rq->context = io; 13224 13225 /* 13226 * Both pointers should be aligned. But it is possible 13227 * that the allocation length is not. They should both 13228 * also have enough slack left over at the end, though, 13229 * to round up to the next 8 byte boundary. 13230 */ 13231 cur_len = ctl_min(local_sglist[i].len - local_used, 13232 remote_sglist[j].len - remote_used); 13233 13234 /* 13235 * In this case, we have a size issue and need to decrease 13236 * the size, except in the case where we actually have less 13237 * than 8 bytes left. In that case, we need to increase 13238 * the DMA length to get the last bit. 13239 */ 13240 if ((cur_len & 0x7) != 0) { 13241 if (cur_len > 0x7) { 13242 cur_len = cur_len - (cur_len & 0x7); 13243 dma_length = cur_len; 13244 } else { 13245 CTL_SIZE_8B(dma_length, cur_len); 13246 } 13247 13248 } else 13249 dma_length = cur_len; 13250 13251 /* 13252 * If we had to allocate memory for this I/O, instead of using 13253 * the non-cached mirror memory, we'll need to flush the cache 13254 * before trying to DMA to the other controller. 13255 * 13256 * We could end up doing this multiple times for the same 13257 * segment if we have a larger local segment than remote 13258 * segment. That shouldn't be an issue. 13259 */ 13260 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13261 /* 13262 * XXX KDM use bus_dmamap_sync() here. 13263 */ 13264 } 13265 13266 rq->size = dma_length; 13267 13268 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13269 tmp_ptr += local_used; 13270 13271 /* Use physical addresses when talking to ISC hardware */ 13272 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13273 /* XXX KDM use busdma */ 13274 #if 0 13275 rq->local = vtophys(tmp_ptr); 13276 #endif 13277 } else 13278 rq->local = tmp_ptr; 13279 13280 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13281 tmp_ptr += remote_used; 13282 rq->remote = tmp_ptr; 13283 13284 rq->callback = NULL; 13285 13286 local_used += cur_len; 13287 if (local_used >= local_sglist[i].len) { 13288 i++; 13289 local_used = 0; 13290 } 13291 13292 remote_used += cur_len; 13293 if (remote_used >= remote_sglist[j].len) { 13294 j++; 13295 remote_used = 0; 13296 } 13297 total_used += cur_len; 13298 13299 if (total_used >= io->scsiio.kern_data_len) 13300 rq->callback = callback; 13301 13302 if ((rq->size & 0x7) != 0) { 13303 printf("%s: warning: size %d is not on 8b boundary\n", 13304 __func__, rq->size); 13305 } 13306 if (((uintptr_t)rq->local & 0x7) != 0) { 13307 printf("%s: warning: local %p not on 8b boundary\n", 13308 __func__, rq->local); 13309 } 13310 if (((uintptr_t)rq->remote & 0x7) != 0) { 13311 printf("%s: warning: remote %p not on 8b boundary\n", 13312 __func__, rq->local); 13313 } 13314 #if 0 13315 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13316 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13317 rq->local, rq->remote, rq->size); 13318 #endif 13319 13320 isc_ret = ctl_dt_single(rq); 13321 if (isc_ret == CTL_HA_STATUS_WAIT) 13322 continue; 13323 13324 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13325 rq->ret = CTL_HA_STATUS_SUCCESS; 13326 } else { 13327 rq->ret = isc_ret; 13328 } 13329 callback(rq); 13330 goto bailout; 13331 } 13332 13333 bailout: 13334 return (retval); 13335 13336 } 13337 13338 static void 13339 ctl_datamove_remote_read(union ctl_io *io) 13340 { 13341 int retval; 13342 int i; 13343 13344 /* 13345 * This will send an error to the other controller in the case of a 13346 * failure. 13347 */ 13348 retval = ctl_datamove_remote_sgl_setup(io); 13349 if (retval != 0) 13350 return; 13351 13352 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13353 ctl_datamove_remote_read_cb); 13354 if ((retval != 0) 13355 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13356 /* 13357 * Make sure we free memory if there was an error.. The 13358 * ctl_datamove_remote_xfer() function will send the 13359 * datamove done message, or call the callback with an 13360 * error if there is a problem. 13361 */ 13362 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13363 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13364 } 13365 13366 return; 13367 } 13368 13369 /* 13370 * Process a datamove request from the other controller. This is used for 13371 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13372 * first. Once that is complete, the data gets DMAed into the remote 13373 * controller's memory. For reads, we DMA from the remote controller's 13374 * memory into our memory first, and then move it out to the FETD. 13375 */ 13376 static void 13377 ctl_datamove_remote(union ctl_io *io) 13378 { 13379 struct ctl_softc *softc; 13380 13381 softc = control_softc; 13382 13383 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13384 13385 /* 13386 * Note that we look for an aborted I/O here, but don't do some of 13387 * the other checks that ctl_datamove() normally does. 13388 * We don't need to run the datamove delay code, since that should 13389 * have been done if need be on the other controller. 13390 */ 13391 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13392 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13393 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13394 io->io_hdr.nexus.targ_port, 13395 io->io_hdr.nexus.targ_target.id, 13396 io->io_hdr.nexus.targ_lun); 13397 io->io_hdr.port_status = 31338; 13398 ctl_send_datamove_done(io, /*have_lock*/ 0); 13399 return; 13400 } 13401 13402 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13403 ctl_datamove_remote_write(io); 13404 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13405 ctl_datamove_remote_read(io); 13406 } else { 13407 union ctl_ha_msg msg; 13408 struct scsi_sense_data *sense; 13409 uint8_t sks[3]; 13410 int retry_count; 13411 13412 memset(&msg, 0, sizeof(msg)); 13413 13414 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13415 msg.hdr.status = CTL_SCSI_ERROR; 13416 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13417 13418 retry_count = 4243; 13419 13420 sense = &msg.scsi.sense_data; 13421 sks[0] = SSD_SCS_VALID; 13422 sks[1] = (retry_count >> 8) & 0xff; 13423 sks[2] = retry_count & 0xff; 13424 13425 /* "Internal target failure" */ 13426 scsi_set_sense_data(sense, 13427 /*sense_format*/ SSD_TYPE_NONE, 13428 /*current_error*/ 1, 13429 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13430 /*asc*/ 0x44, 13431 /*ascq*/ 0x00, 13432 /*type*/ SSD_ELEM_SKS, 13433 /*size*/ sizeof(sks), 13434 /*data*/ sks, 13435 SSD_ELEM_NONE); 13436 13437 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13438 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13439 ctl_failover_io(io, /*have_lock*/ 1); 13440 return; 13441 } 13442 13443 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13444 CTL_HA_STATUS_SUCCESS) { 13445 /* XXX KDM what to do if this fails? */ 13446 } 13447 return; 13448 } 13449 13450 } 13451 13452 static int 13453 ctl_process_done(union ctl_io *io) 13454 { 13455 struct ctl_lun *lun; 13456 struct ctl_softc *ctl_softc; 13457 void (*fe_done)(union ctl_io *io); 13458 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13459 13460 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13461 13462 fe_done = 13463 control_softc->ctl_ports[targ_port]->fe_done; 13464 13465 #ifdef CTL_TIME_IO 13466 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13467 char str[256]; 13468 char path_str[64]; 13469 struct sbuf sb; 13470 13471 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13472 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13473 13474 sbuf_cat(&sb, path_str); 13475 switch (io->io_hdr.io_type) { 13476 case CTL_IO_SCSI: 13477 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13478 sbuf_printf(&sb, "\n"); 13479 sbuf_cat(&sb, path_str); 13480 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13481 io->scsiio.tag_num, io->scsiio.tag_type); 13482 break; 13483 case CTL_IO_TASK: 13484 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13485 "Tag Type: %d\n", io->taskio.task_action, 13486 io->taskio.tag_num, io->taskio.tag_type); 13487 break; 13488 default: 13489 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13490 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13491 break; 13492 } 13493 sbuf_cat(&sb, path_str); 13494 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13495 (intmax_t)time_uptime - io->io_hdr.start_time); 13496 sbuf_finish(&sb); 13497 printf("%s", sbuf_data(&sb)); 13498 } 13499 #endif /* CTL_TIME_IO */ 13500 13501 switch (io->io_hdr.io_type) { 13502 case CTL_IO_SCSI: 13503 break; 13504 case CTL_IO_TASK: 13505 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13506 ctl_io_error_print(io, NULL); 13507 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13508 ctl_free_io(io); 13509 else 13510 fe_done(io); 13511 return (CTL_RETVAL_COMPLETE); 13512 default: 13513 panic("ctl_process_done: invalid io type %d\n", 13514 io->io_hdr.io_type); 13515 break; /* NOTREACHED */ 13516 } 13517 13518 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13519 if (lun == NULL) { 13520 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13521 io->io_hdr.nexus.targ_mapped_lun)); 13522 fe_done(io); 13523 goto bailout; 13524 } 13525 ctl_softc = lun->ctl_softc; 13526 13527 mtx_lock(&lun->lun_lock); 13528 13529 /* 13530 * Check to see if we have any errors to inject here. We only 13531 * inject errors for commands that don't already have errors set. 13532 */ 13533 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13534 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13535 ctl_inject_error(lun, io); 13536 13537 /* 13538 * XXX KDM how do we treat commands that aren't completed 13539 * successfully? 13540 * 13541 * XXX KDM should we also track I/O latency? 13542 */ 13543 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13544 io->io_hdr.io_type == CTL_IO_SCSI) { 13545 #ifdef CTL_TIME_IO 13546 struct bintime cur_bt; 13547 #endif 13548 int type; 13549 13550 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13551 CTL_FLAG_DATA_IN) 13552 type = CTL_STATS_READ; 13553 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13554 CTL_FLAG_DATA_OUT) 13555 type = CTL_STATS_WRITE; 13556 else 13557 type = CTL_STATS_NO_IO; 13558 13559 lun->stats.ports[targ_port].bytes[type] += 13560 io->scsiio.kern_total_len; 13561 lun->stats.ports[targ_port].operations[type]++; 13562 #ifdef CTL_TIME_IO 13563 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13564 &io->io_hdr.dma_bt); 13565 lun->stats.ports[targ_port].num_dmas[type] += 13566 io->io_hdr.num_dmas; 13567 getbintime(&cur_bt); 13568 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13569 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13570 #endif 13571 } 13572 13573 /* 13574 * Remove this from the OOA queue. 13575 */ 13576 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13577 13578 /* 13579 * Run through the blocked queue on this LUN and see if anything 13580 * has become unblocked, now that this transaction is done. 13581 */ 13582 ctl_check_blocked(lun); 13583 13584 /* 13585 * If the LUN has been invalidated, free it if there is nothing 13586 * left on its OOA queue. 13587 */ 13588 if ((lun->flags & CTL_LUN_INVALID) 13589 && TAILQ_EMPTY(&lun->ooa_queue)) { 13590 mtx_unlock(&lun->lun_lock); 13591 mtx_lock(&ctl_softc->ctl_lock); 13592 ctl_free_lun(lun); 13593 mtx_unlock(&ctl_softc->ctl_lock); 13594 } else 13595 mtx_unlock(&lun->lun_lock); 13596 13597 /* 13598 * If this command has been aborted, make sure we set the status 13599 * properly. The FETD is responsible for freeing the I/O and doing 13600 * whatever it needs to do to clean up its state. 13601 */ 13602 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13603 ctl_set_task_aborted(&io->scsiio); 13604 13605 /* 13606 * If enabled, print command error status. 13607 * We don't print UAs unless debugging was enabled explicitly. 13608 */ 13609 do { 13610 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13611 break; 13612 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13613 break; 13614 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13615 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13616 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13617 int error_code, sense_key, asc, ascq; 13618 13619 scsi_extract_sense_len(&io->scsiio.sense_data, 13620 io->scsiio.sense_len, &error_code, &sense_key, 13621 &asc, &ascq, /*show_errors*/ 0); 13622 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13623 break; 13624 } 13625 13626 ctl_io_error_print(io, NULL); 13627 } while (0); 13628 13629 /* 13630 * Tell the FETD or the other shelf controller we're done with this 13631 * command. Note that only SCSI commands get to this point. Task 13632 * management commands are completed above. 13633 * 13634 * We only send status to the other controller if we're in XFER 13635 * mode. In SER_ONLY mode, the I/O is done on the controller that 13636 * received the I/O (from CTL's perspective), and so the status is 13637 * generated there. 13638 * 13639 * XXX KDM if we hold the lock here, we could cause a deadlock 13640 * if the frontend comes back in in this context to queue 13641 * something. 13642 */ 13643 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13644 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13645 union ctl_ha_msg msg; 13646 13647 memset(&msg, 0, sizeof(msg)); 13648 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13649 msg.hdr.original_sc = io->io_hdr.original_sc; 13650 msg.hdr.nexus = io->io_hdr.nexus; 13651 msg.hdr.status = io->io_hdr.status; 13652 msg.scsi.scsi_status = io->scsiio.scsi_status; 13653 msg.scsi.tag_num = io->scsiio.tag_num; 13654 msg.scsi.tag_type = io->scsiio.tag_type; 13655 msg.scsi.sense_len = io->scsiio.sense_len; 13656 msg.scsi.sense_residual = io->scsiio.sense_residual; 13657 msg.scsi.residual = io->scsiio.residual; 13658 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13659 sizeof(io->scsiio.sense_data)); 13660 /* 13661 * We copy this whether or not this is an I/O-related 13662 * command. Otherwise, we'd have to go and check to see 13663 * whether it's a read/write command, and it really isn't 13664 * worth it. 13665 */ 13666 memcpy(&msg.scsi.lbalen, 13667 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13668 sizeof(msg.scsi.lbalen)); 13669 13670 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13671 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13672 /* XXX do something here */ 13673 } 13674 13675 ctl_free_io(io); 13676 } else 13677 fe_done(io); 13678 13679 bailout: 13680 13681 return (CTL_RETVAL_COMPLETE); 13682 } 13683 13684 #ifdef CTL_WITH_CA 13685 /* 13686 * Front end should call this if it doesn't do autosense. When the request 13687 * sense comes back in from the initiator, we'll dequeue this and send it. 13688 */ 13689 int 13690 ctl_queue_sense(union ctl_io *io) 13691 { 13692 struct ctl_lun *lun; 13693 struct ctl_softc *ctl_softc; 13694 uint32_t initidx, targ_lun; 13695 13696 ctl_softc = control_softc; 13697 13698 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13699 13700 /* 13701 * LUN lookup will likely move to the ctl_work_thread() once we 13702 * have our new queueing infrastructure (that doesn't put things on 13703 * a per-LUN queue initially). That is so that we can handle 13704 * things like an INQUIRY to a LUN that we don't have enabled. We 13705 * can't deal with that right now. 13706 */ 13707 mtx_lock(&ctl_softc->ctl_lock); 13708 13709 /* 13710 * If we don't have a LUN for this, just toss the sense 13711 * information. 13712 */ 13713 targ_lun = io->io_hdr.nexus.targ_lun; 13714 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13715 if ((targ_lun < CTL_MAX_LUNS) 13716 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13717 lun = ctl_softc->ctl_luns[targ_lun]; 13718 else 13719 goto bailout; 13720 13721 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13722 13723 mtx_lock(&lun->lun_lock); 13724 /* 13725 * Already have CA set for this LUN...toss the sense information. 13726 */ 13727 if (ctl_is_set(lun->have_ca, initidx)) { 13728 mtx_unlock(&lun->lun_lock); 13729 goto bailout; 13730 } 13731 13732 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13733 ctl_min(sizeof(lun->pending_sense[initidx]), 13734 sizeof(io->scsiio.sense_data))); 13735 ctl_set_mask(lun->have_ca, initidx); 13736 mtx_unlock(&lun->lun_lock); 13737 13738 bailout: 13739 mtx_unlock(&ctl_softc->ctl_lock); 13740 13741 ctl_free_io(io); 13742 13743 return (CTL_RETVAL_COMPLETE); 13744 } 13745 #endif 13746 13747 /* 13748 * Primary command inlet from frontend ports. All SCSI and task I/O 13749 * requests must go through this function. 13750 */ 13751 int 13752 ctl_queue(union ctl_io *io) 13753 { 13754 struct ctl_softc *ctl_softc; 13755 13756 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13757 13758 ctl_softc = control_softc; 13759 13760 #ifdef CTL_TIME_IO 13761 io->io_hdr.start_time = time_uptime; 13762 getbintime(&io->io_hdr.start_bt); 13763 #endif /* CTL_TIME_IO */ 13764 13765 /* Map FE-specific LUN ID into global one. */ 13766 io->io_hdr.nexus.targ_mapped_lun = 13767 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13768 13769 switch (io->io_hdr.io_type) { 13770 case CTL_IO_SCSI: 13771 case CTL_IO_TASK: 13772 if (ctl_debug & CTL_DEBUG_CDB) 13773 ctl_io_print(io); 13774 ctl_enqueue_incoming(io); 13775 break; 13776 default: 13777 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13778 return (EINVAL); 13779 } 13780 13781 return (CTL_RETVAL_COMPLETE); 13782 } 13783 13784 #ifdef CTL_IO_DELAY 13785 static void 13786 ctl_done_timer_wakeup(void *arg) 13787 { 13788 union ctl_io *io; 13789 13790 io = (union ctl_io *)arg; 13791 ctl_done(io); 13792 } 13793 #endif /* CTL_IO_DELAY */ 13794 13795 void 13796 ctl_done(union ctl_io *io) 13797 { 13798 struct ctl_softc *ctl_softc; 13799 13800 ctl_softc = control_softc; 13801 13802 /* 13803 * Enable this to catch duplicate completion issues. 13804 */ 13805 #if 0 13806 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13807 printf("%s: type %d msg %d cdb %x iptl: " 13808 "%d:%d:%d:%d tag 0x%04x " 13809 "flag %#x status %x\n", 13810 __func__, 13811 io->io_hdr.io_type, 13812 io->io_hdr.msg_type, 13813 io->scsiio.cdb[0], 13814 io->io_hdr.nexus.initid.id, 13815 io->io_hdr.nexus.targ_port, 13816 io->io_hdr.nexus.targ_target.id, 13817 io->io_hdr.nexus.targ_lun, 13818 (io->io_hdr.io_type == 13819 CTL_IO_TASK) ? 13820 io->taskio.tag_num : 13821 io->scsiio.tag_num, 13822 io->io_hdr.flags, 13823 io->io_hdr.status); 13824 } else 13825 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13826 #endif 13827 13828 /* 13829 * This is an internal copy of an I/O, and should not go through 13830 * the normal done processing logic. 13831 */ 13832 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13833 return; 13834 13835 /* 13836 * We need to send a msg to the serializing shelf to finish the IO 13837 * as well. We don't send a finish message to the other shelf if 13838 * this is a task management command. Task management commands 13839 * aren't serialized in the OOA queue, but rather just executed on 13840 * both shelf controllers for commands that originated on that 13841 * controller. 13842 */ 13843 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13844 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13845 union ctl_ha_msg msg_io; 13846 13847 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13848 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13849 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13850 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13851 } 13852 /* continue on to finish IO */ 13853 } 13854 #ifdef CTL_IO_DELAY 13855 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13856 struct ctl_lun *lun; 13857 13858 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13859 13860 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13861 } else { 13862 struct ctl_lun *lun; 13863 13864 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13865 13866 if ((lun != NULL) 13867 && (lun->delay_info.done_delay > 0)) { 13868 struct callout *callout; 13869 13870 callout = (struct callout *)&io->io_hdr.timer_bytes; 13871 callout_init(callout, /*mpsafe*/ 1); 13872 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13873 callout_reset(callout, 13874 lun->delay_info.done_delay * hz, 13875 ctl_done_timer_wakeup, io); 13876 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13877 lun->delay_info.done_delay = 0; 13878 return; 13879 } 13880 } 13881 #endif /* CTL_IO_DELAY */ 13882 13883 ctl_enqueue_done(io); 13884 } 13885 13886 int 13887 ctl_isc(struct ctl_scsiio *ctsio) 13888 { 13889 struct ctl_lun *lun; 13890 int retval; 13891 13892 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13893 13894 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13895 13896 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13897 13898 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13899 13900 return (retval); 13901 } 13902 13903 13904 static void 13905 ctl_work_thread(void *arg) 13906 { 13907 struct ctl_thread *thr = (struct ctl_thread *)arg; 13908 struct ctl_softc *softc = thr->ctl_softc; 13909 union ctl_io *io; 13910 int retval; 13911 13912 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13913 13914 for (;;) { 13915 retval = 0; 13916 13917 /* 13918 * We handle the queues in this order: 13919 * - ISC 13920 * - done queue (to free up resources, unblock other commands) 13921 * - RtR queue 13922 * - incoming queue 13923 * 13924 * If those queues are empty, we break out of the loop and 13925 * go to sleep. 13926 */ 13927 mtx_lock(&thr->queue_lock); 13928 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13929 if (io != NULL) { 13930 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13931 mtx_unlock(&thr->queue_lock); 13932 ctl_handle_isc(io); 13933 continue; 13934 } 13935 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13936 if (io != NULL) { 13937 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13938 /* clear any blocked commands, call fe_done */ 13939 mtx_unlock(&thr->queue_lock); 13940 retval = ctl_process_done(io); 13941 continue; 13942 } 13943 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13944 if (io != NULL) { 13945 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13946 mtx_unlock(&thr->queue_lock); 13947 if (io->io_hdr.io_type == CTL_IO_TASK) 13948 ctl_run_task(io); 13949 else 13950 ctl_scsiio_precheck(softc, &io->scsiio); 13951 continue; 13952 } 13953 if (!ctl_pause_rtr) { 13954 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13955 if (io != NULL) { 13956 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13957 mtx_unlock(&thr->queue_lock); 13958 retval = ctl_scsiio(&io->scsiio); 13959 if (retval != CTL_RETVAL_COMPLETE) 13960 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13961 continue; 13962 } 13963 } 13964 13965 /* Sleep until we have something to do. */ 13966 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13967 } 13968 } 13969 13970 static void 13971 ctl_lun_thread(void *arg) 13972 { 13973 struct ctl_softc *softc = (struct ctl_softc *)arg; 13974 struct ctl_be_lun *be_lun; 13975 int retval; 13976 13977 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13978 13979 for (;;) { 13980 retval = 0; 13981 mtx_lock(&softc->ctl_lock); 13982 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13983 if (be_lun != NULL) { 13984 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13985 mtx_unlock(&softc->ctl_lock); 13986 ctl_create_lun(be_lun); 13987 continue; 13988 } 13989 13990 /* Sleep until we have something to do. */ 13991 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13992 PDROP | PRIBIO, "-", 0); 13993 } 13994 } 13995 13996 static void 13997 ctl_enqueue_incoming(union ctl_io *io) 13998 { 13999 struct ctl_softc *softc = control_softc; 14000 struct ctl_thread *thr; 14001 u_int idx; 14002 14003 idx = (io->io_hdr.nexus.targ_port * 127 + 14004 io->io_hdr.nexus.initid.id) % worker_threads; 14005 thr = &softc->threads[idx]; 14006 mtx_lock(&thr->queue_lock); 14007 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14008 mtx_unlock(&thr->queue_lock); 14009 wakeup(thr); 14010 } 14011 14012 static void 14013 ctl_enqueue_rtr(union ctl_io *io) 14014 { 14015 struct ctl_softc *softc = control_softc; 14016 struct ctl_thread *thr; 14017 14018 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14019 mtx_lock(&thr->queue_lock); 14020 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14021 mtx_unlock(&thr->queue_lock); 14022 wakeup(thr); 14023 } 14024 14025 static void 14026 ctl_enqueue_done(union ctl_io *io) 14027 { 14028 struct ctl_softc *softc = control_softc; 14029 struct ctl_thread *thr; 14030 14031 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14032 mtx_lock(&thr->queue_lock); 14033 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14034 mtx_unlock(&thr->queue_lock); 14035 wakeup(thr); 14036 } 14037 14038 static void 14039 ctl_enqueue_isc(union ctl_io *io) 14040 { 14041 struct ctl_softc *softc = control_softc; 14042 struct ctl_thread *thr; 14043 14044 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14045 mtx_lock(&thr->queue_lock); 14046 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14047 mtx_unlock(&thr->queue_lock); 14048 wakeup(thr); 14049 } 14050 14051 /* Initialization and failover */ 14052 14053 void 14054 ctl_init_isc_msg(void) 14055 { 14056 printf("CTL: Still calling this thing\n"); 14057 } 14058 14059 /* 14060 * Init component 14061 * Initializes component into configuration defined by bootMode 14062 * (see hasc-sv.c) 14063 * returns hasc_Status: 14064 * OK 14065 * ERROR - fatal error 14066 */ 14067 static ctl_ha_comp_status 14068 ctl_isc_init(struct ctl_ha_component *c) 14069 { 14070 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14071 14072 c->status = ret; 14073 return ret; 14074 } 14075 14076 /* Start component 14077 * Starts component in state requested. If component starts successfully, 14078 * it must set its own state to the requestrd state 14079 * When requested state is HASC_STATE_HA, the component may refine it 14080 * by adding _SLAVE or _MASTER flags. 14081 * Currently allowed state transitions are: 14082 * UNKNOWN->HA - initial startup 14083 * UNKNOWN->SINGLE - initial startup when no parter detected 14084 * HA->SINGLE - failover 14085 * returns ctl_ha_comp_status: 14086 * OK - component successfully started in requested state 14087 * FAILED - could not start the requested state, failover may 14088 * be possible 14089 * ERROR - fatal error detected, no future startup possible 14090 */ 14091 static ctl_ha_comp_status 14092 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14093 { 14094 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14095 14096 printf("%s: go\n", __func__); 14097 14098 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14099 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14100 ctl_is_single = 0; 14101 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14102 != CTL_HA_STATUS_SUCCESS) { 14103 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14104 ret = CTL_HA_COMP_STATUS_ERROR; 14105 } 14106 } else if (CTL_HA_STATE_IS_HA(c->state) 14107 && CTL_HA_STATE_IS_SINGLE(state)){ 14108 // HA->SINGLE transition 14109 ctl_failover(); 14110 ctl_is_single = 1; 14111 } else { 14112 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14113 c->state, state); 14114 ret = CTL_HA_COMP_STATUS_ERROR; 14115 } 14116 if (CTL_HA_STATE_IS_SINGLE(state)) 14117 ctl_is_single = 1; 14118 14119 c->state = state; 14120 c->status = ret; 14121 return ret; 14122 } 14123 14124 /* 14125 * Quiesce component 14126 * The component must clear any error conditions (set status to OK) and 14127 * prepare itself to another Start call 14128 * returns ctl_ha_comp_status: 14129 * OK 14130 * ERROR 14131 */ 14132 static ctl_ha_comp_status 14133 ctl_isc_quiesce(struct ctl_ha_component *c) 14134 { 14135 int ret = CTL_HA_COMP_STATUS_OK; 14136 14137 ctl_pause_rtr = 1; 14138 c->status = ret; 14139 return ret; 14140 } 14141 14142 struct ctl_ha_component ctl_ha_component_ctlisc = 14143 { 14144 .name = "CTL ISC", 14145 .state = CTL_HA_STATE_UNKNOWN, 14146 .init = ctl_isc_init, 14147 .start = ctl_isc_start, 14148 .quiesce = ctl_isc_quiesce 14149 }; 14150 14151 /* 14152 * vim: ts=8 14153 */ 14154