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/kernel.h> 50 #include <sys/types.h> 51 #include <sys/kthread.h> 52 #include <sys/bio.h> 53 #include <sys/fcntl.h> 54 #include <sys/lock.h> 55 #include <sys/module.h> 56 #include <sys/mutex.h> 57 #include <sys/condvar.h> 58 #include <sys/malloc.h> 59 #include <sys/conf.h> 60 #include <sys/ioccom.h> 61 #include <sys/queue.h> 62 #include <sys/sbuf.h> 63 #include <sys/smp.h> 64 #include <sys/endian.h> 65 #include <sys/sysctl.h> 66 67 #include <cam/cam.h> 68 #include <cam/scsi/scsi_all.h> 69 #include <cam/scsi/scsi_da.h> 70 #include <cam/ctl/ctl_io.h> 71 #include <cam/ctl/ctl.h> 72 #include <cam/ctl/ctl_frontend.h> 73 #include <cam/ctl/ctl_frontend_internal.h> 74 #include <cam/ctl/ctl_util.h> 75 #include <cam/ctl/ctl_backend.h> 76 #include <cam/ctl/ctl_ioctl.h> 77 #include <cam/ctl/ctl_ha.h> 78 #include <cam/ctl/ctl_private.h> 79 #include <cam/ctl/ctl_debug.h> 80 #include <cam/ctl/ctl_scsi_all.h> 81 #include <cam/ctl/ctl_error.h> 82 83 struct ctl_softc *control_softc = NULL; 84 85 /* 86 * Size and alignment macros needed for Copan-specific HA hardware. These 87 * can go away when the HA code is re-written, and uses busdma for any 88 * hardware. 89 */ 90 #define CTL_ALIGN_8B(target, source, type) \ 91 if (((uint32_t)source & 0x7) != 0) \ 92 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 93 else \ 94 target = (type)source; 95 96 #define CTL_SIZE_8B(target, size) \ 97 if ((size & 0x7) != 0) \ 98 target = size + (0x8 - (size & 0x7)); \ 99 else \ 100 target = size; 101 102 #define CTL_ALIGN_8B_MARGIN 16 103 104 /* 105 * Template mode pages. 106 */ 107 108 /* 109 * Note that these are default values only. The actual values will be 110 * filled in when the user does a mode sense. 111 */ 112 static struct copan_debugconf_subpage debugconf_page_default = { 113 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 114 DBGCNF_SUBPAGE_CODE, /* subpage */ 115 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 116 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 117 DBGCNF_VERSION, /* page_version */ 118 {CTL_TIME_IO_DEFAULT_SECS>>8, 119 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 120 }; 121 122 static struct copan_debugconf_subpage debugconf_page_changeable = { 123 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 124 DBGCNF_SUBPAGE_CODE, /* subpage */ 125 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 126 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 127 0, /* page_version */ 128 {0xff,0xff}, /* ctl_time_io_secs */ 129 }; 130 131 static struct scsi_da_rw_recovery_page rw_er_page_default = { 132 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 133 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 134 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 135 /*read_retry_count*/0, 136 /*correction_span*/0, 137 /*head_offset_count*/0, 138 /*data_strobe_offset_cnt*/0, 139 /*byte8*/0, 140 /*write_retry_count*/0, 141 /*reserved2*/0, 142 /*recovery_time_limit*/{0, 0}, 143 }; 144 145 static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 146 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 147 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 148 /*byte3*/0, 149 /*read_retry_count*/0, 150 /*correction_span*/0, 151 /*head_offset_count*/0, 152 /*data_strobe_offset_cnt*/0, 153 /*byte8*/0, 154 /*write_retry_count*/0, 155 /*reserved2*/0, 156 /*recovery_time_limit*/{0, 0}, 157 }; 158 159 static struct scsi_format_page format_page_default = { 160 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 161 /*page_length*/sizeof(struct scsi_format_page) - 2, 162 /*tracks_per_zone*/ {0, 0}, 163 /*alt_sectors_per_zone*/ {0, 0}, 164 /*alt_tracks_per_zone*/ {0, 0}, 165 /*alt_tracks_per_lun*/ {0, 0}, 166 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 167 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 168 /*bytes_per_sector*/ {0, 0}, 169 /*interleave*/ {0, 0}, 170 /*track_skew*/ {0, 0}, 171 /*cylinder_skew*/ {0, 0}, 172 /*flags*/ SFP_HSEC, 173 /*reserved*/ {0, 0, 0} 174 }; 175 176 static struct scsi_format_page format_page_changeable = { 177 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 178 /*page_length*/sizeof(struct scsi_format_page) - 2, 179 /*tracks_per_zone*/ {0, 0}, 180 /*alt_sectors_per_zone*/ {0, 0}, 181 /*alt_tracks_per_zone*/ {0, 0}, 182 /*alt_tracks_per_lun*/ {0, 0}, 183 /*sectors_per_track*/ {0, 0}, 184 /*bytes_per_sector*/ {0, 0}, 185 /*interleave*/ {0, 0}, 186 /*track_skew*/ {0, 0}, 187 /*cylinder_skew*/ {0, 0}, 188 /*flags*/ 0, 189 /*reserved*/ {0, 0, 0} 190 }; 191 192 static struct scsi_rigid_disk_page rigid_disk_page_default = { 193 /*page_code*/SMS_RIGID_DISK_PAGE, 194 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 195 /*cylinders*/ {0, 0, 0}, 196 /*heads*/ CTL_DEFAULT_HEADS, 197 /*start_write_precomp*/ {0, 0, 0}, 198 /*start_reduced_current*/ {0, 0, 0}, 199 /*step_rate*/ {0, 0}, 200 /*landing_zone_cylinder*/ {0, 0, 0}, 201 /*rpl*/ SRDP_RPL_DISABLED, 202 /*rotational_offset*/ 0, 203 /*reserved1*/ 0, 204 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 205 CTL_DEFAULT_ROTATION_RATE & 0xff}, 206 /*reserved2*/ {0, 0} 207 }; 208 209 static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 210 /*page_code*/SMS_RIGID_DISK_PAGE, 211 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 212 /*cylinders*/ {0, 0, 0}, 213 /*heads*/ 0, 214 /*start_write_precomp*/ {0, 0, 0}, 215 /*start_reduced_current*/ {0, 0, 0}, 216 /*step_rate*/ {0, 0}, 217 /*landing_zone_cylinder*/ {0, 0, 0}, 218 /*rpl*/ 0, 219 /*rotational_offset*/ 0, 220 /*reserved1*/ 0, 221 /*rotation_rate*/ {0, 0}, 222 /*reserved2*/ {0, 0} 223 }; 224 225 static struct scsi_caching_page caching_page_default = { 226 /*page_code*/SMS_CACHING_PAGE, 227 /*page_length*/sizeof(struct scsi_caching_page) - 2, 228 /*flags1*/ SCP_DISC | SCP_WCE, 229 /*ret_priority*/ 0, 230 /*disable_pf_transfer_len*/ {0xff, 0xff}, 231 /*min_prefetch*/ {0, 0}, 232 /*max_prefetch*/ {0xff, 0xff}, 233 /*max_pf_ceiling*/ {0xff, 0xff}, 234 /*flags2*/ 0, 235 /*cache_segments*/ 0, 236 /*cache_seg_size*/ {0, 0}, 237 /*reserved*/ 0, 238 /*non_cache_seg_size*/ {0, 0, 0} 239 }; 240 241 static struct scsi_caching_page caching_page_changeable = { 242 /*page_code*/SMS_CACHING_PAGE, 243 /*page_length*/sizeof(struct scsi_caching_page) - 2, 244 /*flags1*/ SCP_WCE | SCP_RCD, 245 /*ret_priority*/ 0, 246 /*disable_pf_transfer_len*/ {0, 0}, 247 /*min_prefetch*/ {0, 0}, 248 /*max_prefetch*/ {0, 0}, 249 /*max_pf_ceiling*/ {0, 0}, 250 /*flags2*/ 0, 251 /*cache_segments*/ 0, 252 /*cache_seg_size*/ {0, 0}, 253 /*reserved*/ 0, 254 /*non_cache_seg_size*/ {0, 0, 0} 255 }; 256 257 static struct scsi_control_page control_page_default = { 258 /*page_code*/SMS_CONTROL_MODE_PAGE, 259 /*page_length*/sizeof(struct scsi_control_page) - 2, 260 /*rlec*/0, 261 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 262 /*eca_and_aen*/0, 263 /*flags4*/SCP_TAS, 264 /*aen_holdoff_period*/{0, 0}, 265 /*busy_timeout_period*/{0, 0}, 266 /*extended_selftest_completion_time*/{0, 0} 267 }; 268 269 static struct scsi_control_page control_page_changeable = { 270 /*page_code*/SMS_CONTROL_MODE_PAGE, 271 /*page_length*/sizeof(struct scsi_control_page) - 2, 272 /*rlec*/SCP_DSENSE, 273 /*queue_flags*/SCP_QUEUE_ALG_MASK, 274 /*eca_and_aen*/SCP_SWP, 275 /*flags4*/0, 276 /*aen_holdoff_period*/{0, 0}, 277 /*busy_timeout_period*/{0, 0}, 278 /*extended_selftest_completion_time*/{0, 0} 279 }; 280 281 static struct scsi_info_exceptions_page ie_page_default = { 282 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 283 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 284 /*info_flags*/SIEP_FLAGS_DEXCPT, 285 /*mrie*/0, 286 /*interval_timer*/{0, 0, 0, 0}, 287 /*report_count*/{0, 0, 0, 0} 288 }; 289 290 static struct scsi_info_exceptions_page ie_page_changeable = { 291 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 292 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 293 /*info_flags*/0, 294 /*mrie*/0, 295 /*interval_timer*/{0, 0, 0, 0}, 296 /*report_count*/{0, 0, 0, 0} 297 }; 298 299 static struct scsi_logical_block_provisioning_page lbp_page_default = { 300 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 301 /*subpage_code*/0x02, 302 /*page_length*/{0, sizeof(struct scsi_logical_block_provisioning_page) - 4}, 303 /*flags*/0, 304 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 305 /*descr*/{} 306 }; 307 308 static struct scsi_logical_block_provisioning_page lbp_page_changeable = { 309 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 310 /*subpage_code*/0x02, 311 /*page_length*/{0, sizeof(struct scsi_logical_block_provisioning_page) - 4}, 312 /*flags*/0, 313 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 314 /*descr*/{} 315 }; 316 317 /* 318 * XXX KDM move these into the softc. 319 */ 320 static int rcv_sync_msg; 321 static int persis_offset; 322 static uint8_t ctl_pause_rtr; 323 static int ctl_is_single = 1; 324 325 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 326 static int worker_threads = -1; 327 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 328 &worker_threads, 1, "Number of worker threads"); 329 static int ctl_debug = CTL_DEBUG_NONE; 330 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 331 &ctl_debug, 0, "Enabled debug flags"); 332 333 /* 334 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 335 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 336 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 337 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 338 */ 339 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 340 341 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 342 int param); 343 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 344 static int ctl_init(void); 345 void ctl_shutdown(void); 346 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 347 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 348 static void ctl_ioctl_online(void *arg); 349 static void ctl_ioctl_offline(void *arg); 350 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 351 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 352 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 353 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 354 static int ctl_ioctl_submit_wait(union ctl_io *io); 355 static void ctl_ioctl_datamove(union ctl_io *io); 356 static void ctl_ioctl_done(union ctl_io *io); 357 static void ctl_ioctl_hard_startstop_callback(void *arg, 358 struct cfi_metatask *metatask); 359 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 360 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 361 struct ctl_ooa *ooa_hdr, 362 struct ctl_ooa_entry *kern_entries); 363 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 364 struct thread *td); 365 static uint32_t ctl_map_lun(int port_num, uint32_t lun); 366 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 367 #ifdef unused 368 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 369 uint32_t targ_target, uint32_t targ_lun, 370 int can_wait); 371 static void ctl_kfree_io(union ctl_io *io); 372 #endif /* unused */ 373 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 374 struct ctl_be_lun *be_lun, struct ctl_id target_id); 375 static int ctl_free_lun(struct ctl_lun *lun); 376 static void ctl_create_lun(struct ctl_be_lun *be_lun); 377 /** 378 static void ctl_failover_change_pages(struct ctl_softc *softc, 379 struct ctl_scsiio *ctsio, int master); 380 **/ 381 382 static int ctl_do_mode_select(union ctl_io *io); 383 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 384 uint64_t res_key, uint64_t sa_res_key, 385 uint8_t type, uint32_t residx, 386 struct ctl_scsiio *ctsio, 387 struct scsi_per_res_out *cdb, 388 struct scsi_per_res_out_parms* param); 389 static void ctl_pro_preempt_other(struct ctl_lun *lun, 390 union ctl_ha_msg *msg); 391 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 392 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 393 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 394 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 395 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 396 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 397 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 398 int alloc_len); 399 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 400 int alloc_len); 401 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 402 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 403 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 404 static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 405 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 406 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 407 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 408 union ctl_io *pending_io, union ctl_io *ooa_io); 409 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 410 union ctl_io *starting_io); 411 static int ctl_check_blocked(struct ctl_lun *lun); 412 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 413 struct ctl_lun *lun, 414 const struct ctl_cmd_entry *entry, 415 struct ctl_scsiio *ctsio); 416 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 417 static void ctl_failover(void); 418 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 419 struct ctl_scsiio *ctsio); 420 static int ctl_scsiio(struct ctl_scsiio *ctsio); 421 422 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 423 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 424 ctl_ua_type ua_type); 425 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 426 ctl_ua_type ua_type); 427 static int ctl_abort_task(union ctl_io *io); 428 static int ctl_abort_task_set(union ctl_io *io); 429 static int ctl_i_t_nexus_reset(union ctl_io *io); 430 static void ctl_run_task(union ctl_io *io); 431 #ifdef CTL_IO_DELAY 432 static void ctl_datamove_timer_wakeup(void *arg); 433 static void ctl_done_timer_wakeup(void *arg); 434 #endif /* CTL_IO_DELAY */ 435 436 static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 437 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 438 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 439 static void ctl_datamove_remote_write(union ctl_io *io); 440 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 441 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 442 static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 443 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 444 ctl_ha_dt_cb callback); 445 static void ctl_datamove_remote_read(union ctl_io *io); 446 static void ctl_datamove_remote(union ctl_io *io); 447 static int ctl_process_done(union ctl_io *io); 448 static void ctl_lun_thread(void *arg); 449 static void ctl_work_thread(void *arg); 450 static void ctl_enqueue_incoming(union ctl_io *io); 451 static void ctl_enqueue_rtr(union ctl_io *io); 452 static void ctl_enqueue_done(union ctl_io *io); 453 static void ctl_enqueue_isc(union ctl_io *io); 454 static const struct ctl_cmd_entry * 455 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 456 static const struct ctl_cmd_entry * 457 ctl_validate_command(struct ctl_scsiio *ctsio); 458 static int ctl_cmd_applicable(uint8_t lun_type, 459 const struct ctl_cmd_entry *entry); 460 461 /* 462 * Load the serialization table. This isn't very pretty, but is probably 463 * the easiest way to do it. 464 */ 465 #include "ctl_ser_table.c" 466 467 /* 468 * We only need to define open, close and ioctl routines for this driver. 469 */ 470 static struct cdevsw ctl_cdevsw = { 471 .d_version = D_VERSION, 472 .d_flags = 0, 473 .d_open = ctl_open, 474 .d_close = ctl_close, 475 .d_ioctl = ctl_ioctl, 476 .d_name = "ctl", 477 }; 478 479 480 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 481 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 482 483 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 484 485 static moduledata_t ctl_moduledata = { 486 "ctl", 487 ctl_module_event_handler, 488 NULL 489 }; 490 491 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 492 MODULE_VERSION(ctl, 1); 493 494 static struct ctl_frontend ioctl_frontend = 495 { 496 .name = "ioctl", 497 }; 498 499 static void 500 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 501 union ctl_ha_msg *msg_info) 502 { 503 struct ctl_scsiio *ctsio; 504 505 if (msg_info->hdr.original_sc == NULL) { 506 printf("%s: original_sc == NULL!\n", __func__); 507 /* XXX KDM now what? */ 508 return; 509 } 510 511 ctsio = &msg_info->hdr.original_sc->scsiio; 512 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 513 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 514 ctsio->io_hdr.status = msg_info->hdr.status; 515 ctsio->scsi_status = msg_info->scsi.scsi_status; 516 ctsio->sense_len = msg_info->scsi.sense_len; 517 ctsio->sense_residual = msg_info->scsi.sense_residual; 518 ctsio->residual = msg_info->scsi.residual; 519 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 520 sizeof(ctsio->sense_data)); 521 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 522 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 523 ctl_enqueue_isc((union ctl_io *)ctsio); 524 } 525 526 static void 527 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 528 union ctl_ha_msg *msg_info) 529 { 530 struct ctl_scsiio *ctsio; 531 532 if (msg_info->hdr.serializing_sc == NULL) { 533 printf("%s: serializing_sc == NULL!\n", __func__); 534 /* XXX KDM now what? */ 535 return; 536 } 537 538 ctsio = &msg_info->hdr.serializing_sc->scsiio; 539 #if 0 540 /* 541 * Attempt to catch the situation where an I/O has 542 * been freed, and we're using it again. 543 */ 544 if (ctsio->io_hdr.io_type == 0xff) { 545 union ctl_io *tmp_io; 546 tmp_io = (union ctl_io *)ctsio; 547 printf("%s: %p use after free!\n", __func__, 548 ctsio); 549 printf("%s: type %d msg %d cdb %x iptl: " 550 "%d:%d:%d:%d tag 0x%04x " 551 "flag %#x status %x\n", 552 __func__, 553 tmp_io->io_hdr.io_type, 554 tmp_io->io_hdr.msg_type, 555 tmp_io->scsiio.cdb[0], 556 tmp_io->io_hdr.nexus.initid.id, 557 tmp_io->io_hdr.nexus.targ_port, 558 tmp_io->io_hdr.nexus.targ_target.id, 559 tmp_io->io_hdr.nexus.targ_lun, 560 (tmp_io->io_hdr.io_type == 561 CTL_IO_TASK) ? 562 tmp_io->taskio.tag_num : 563 tmp_io->scsiio.tag_num, 564 tmp_io->io_hdr.flags, 565 tmp_io->io_hdr.status); 566 } 567 #endif 568 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 569 ctl_enqueue_isc((union ctl_io *)ctsio); 570 } 571 572 /* 573 * ISC (Inter Shelf Communication) event handler. Events from the HA 574 * subsystem come in here. 575 */ 576 static void 577 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 578 { 579 struct ctl_softc *ctl_softc; 580 union ctl_io *io; 581 struct ctl_prio *presio; 582 ctl_ha_status isc_status; 583 584 ctl_softc = control_softc; 585 io = NULL; 586 587 588 #if 0 589 printf("CTL: Isc Msg event %d\n", event); 590 #endif 591 if (event == CTL_HA_EVT_MSG_RECV) { 592 union ctl_ha_msg msg_info; 593 594 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 595 sizeof(msg_info), /*wait*/ 0); 596 #if 0 597 printf("CTL: msg_type %d\n", msg_info.msg_type); 598 #endif 599 if (isc_status != 0) { 600 printf("Error receiving message, status = %d\n", 601 isc_status); 602 return; 603 } 604 605 switch (msg_info.hdr.msg_type) { 606 case CTL_MSG_SERIALIZE: 607 #if 0 608 printf("Serialize\n"); 609 #endif 610 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 611 if (io == NULL) { 612 printf("ctl_isc_event_handler: can't allocate " 613 "ctl_io!\n"); 614 /* Bad Juju */ 615 /* Need to set busy and send msg back */ 616 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 617 msg_info.hdr.status = CTL_SCSI_ERROR; 618 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 619 msg_info.scsi.sense_len = 0; 620 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 621 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 622 } 623 goto bailout; 624 } 625 ctl_zero_io(io); 626 // populate ctsio from msg_info 627 io->io_hdr.io_type = CTL_IO_SCSI; 628 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 629 io->io_hdr.original_sc = msg_info.hdr.original_sc; 630 #if 0 631 printf("pOrig %x\n", (int)msg_info.original_sc); 632 #endif 633 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 634 CTL_FLAG_IO_ACTIVE; 635 /* 636 * If we're in serialization-only mode, we don't 637 * want to go through full done processing. Thus 638 * the COPY flag. 639 * 640 * XXX KDM add another flag that is more specific. 641 */ 642 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 643 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 644 io->io_hdr.nexus = msg_info.hdr.nexus; 645 #if 0 646 printf("targ %d, port %d, iid %d, lun %d\n", 647 io->io_hdr.nexus.targ_target.id, 648 io->io_hdr.nexus.targ_port, 649 io->io_hdr.nexus.initid.id, 650 io->io_hdr.nexus.targ_lun); 651 #endif 652 io->scsiio.tag_num = msg_info.scsi.tag_num; 653 io->scsiio.tag_type = msg_info.scsi.tag_type; 654 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 655 CTL_MAX_CDBLEN); 656 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 657 const struct ctl_cmd_entry *entry; 658 659 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 660 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 661 io->io_hdr.flags |= 662 entry->flags & CTL_FLAG_DATA_MASK; 663 } 664 ctl_enqueue_isc(io); 665 break; 666 667 /* Performed on the Originating SC, XFER mode only */ 668 case CTL_MSG_DATAMOVE: { 669 struct ctl_sg_entry *sgl; 670 int i, j; 671 672 io = msg_info.hdr.original_sc; 673 if (io == NULL) { 674 printf("%s: original_sc == NULL!\n", __func__); 675 /* XXX KDM do something here */ 676 break; 677 } 678 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 679 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 680 /* 681 * Keep track of this, we need to send it back over 682 * when the datamove is complete. 683 */ 684 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 685 686 if (msg_info.dt.sg_sequence == 0) { 687 /* 688 * XXX KDM we use the preallocated S/G list 689 * here, but we'll need to change this to 690 * dynamic allocation if we need larger S/G 691 * lists. 692 */ 693 if (msg_info.dt.kern_sg_entries > 694 sizeof(io->io_hdr.remote_sglist) / 695 sizeof(io->io_hdr.remote_sglist[0])) { 696 printf("%s: number of S/G entries " 697 "needed %u > allocated num %zd\n", 698 __func__, 699 msg_info.dt.kern_sg_entries, 700 sizeof(io->io_hdr.remote_sglist)/ 701 sizeof(io->io_hdr.remote_sglist[0])); 702 703 /* 704 * XXX KDM send a message back to 705 * the other side to shut down the 706 * DMA. The error will come back 707 * through via the normal channel. 708 */ 709 break; 710 } 711 sgl = io->io_hdr.remote_sglist; 712 memset(sgl, 0, 713 sizeof(io->io_hdr.remote_sglist)); 714 715 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 716 717 io->scsiio.kern_sg_entries = 718 msg_info.dt.kern_sg_entries; 719 io->scsiio.rem_sg_entries = 720 msg_info.dt.kern_sg_entries; 721 io->scsiio.kern_data_len = 722 msg_info.dt.kern_data_len; 723 io->scsiio.kern_total_len = 724 msg_info.dt.kern_total_len; 725 io->scsiio.kern_data_resid = 726 msg_info.dt.kern_data_resid; 727 io->scsiio.kern_rel_offset = 728 msg_info.dt.kern_rel_offset; 729 /* 730 * Clear out per-DMA flags. 731 */ 732 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 733 /* 734 * Add per-DMA flags that are set for this 735 * particular DMA request. 736 */ 737 io->io_hdr.flags |= msg_info.dt.flags & 738 CTL_FLAG_RDMA_MASK; 739 } else 740 sgl = (struct ctl_sg_entry *) 741 io->scsiio.kern_data_ptr; 742 743 for (i = msg_info.dt.sent_sg_entries, j = 0; 744 i < (msg_info.dt.sent_sg_entries + 745 msg_info.dt.cur_sg_entries); i++, j++) { 746 sgl[i].addr = msg_info.dt.sg_list[j].addr; 747 sgl[i].len = msg_info.dt.sg_list[j].len; 748 749 #if 0 750 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 751 __func__, 752 msg_info.dt.sg_list[j].addr, 753 msg_info.dt.sg_list[j].len, 754 sgl[i].addr, sgl[i].len, j, i); 755 #endif 756 } 757 #if 0 758 memcpy(&sgl[msg_info.dt.sent_sg_entries], 759 msg_info.dt.sg_list, 760 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 761 #endif 762 763 /* 764 * If this is the last piece of the I/O, we've got 765 * the full S/G list. Queue processing in the thread. 766 * Otherwise wait for the next piece. 767 */ 768 if (msg_info.dt.sg_last != 0) 769 ctl_enqueue_isc(io); 770 break; 771 } 772 /* Performed on the Serializing (primary) SC, XFER mode only */ 773 case CTL_MSG_DATAMOVE_DONE: { 774 if (msg_info.hdr.serializing_sc == NULL) { 775 printf("%s: serializing_sc == NULL!\n", 776 __func__); 777 /* XXX KDM now what? */ 778 break; 779 } 780 /* 781 * We grab the sense information here in case 782 * there was a failure, so we can return status 783 * back to the initiator. 784 */ 785 io = msg_info.hdr.serializing_sc; 786 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 787 io->io_hdr.status = msg_info.hdr.status; 788 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 789 io->scsiio.sense_len = msg_info.scsi.sense_len; 790 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 791 io->io_hdr.port_status = msg_info.scsi.fetd_status; 792 io->scsiio.residual = msg_info.scsi.residual; 793 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 794 sizeof(io->scsiio.sense_data)); 795 ctl_enqueue_isc(io); 796 break; 797 } 798 799 /* Preformed on Originating SC, SER_ONLY mode */ 800 case CTL_MSG_R2R: 801 io = msg_info.hdr.original_sc; 802 if (io == NULL) { 803 printf("%s: Major Bummer\n", __func__); 804 return; 805 } else { 806 #if 0 807 printf("pOrig %x\n",(int) ctsio); 808 #endif 809 } 810 io->io_hdr.msg_type = CTL_MSG_R2R; 811 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 812 ctl_enqueue_isc(io); 813 break; 814 815 /* 816 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 817 * mode. 818 * Performed on the Originating (i.e. secondary) SC in XFER 819 * mode 820 */ 821 case CTL_MSG_FINISH_IO: 822 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 823 ctl_isc_handler_finish_xfer(ctl_softc, 824 &msg_info); 825 else 826 ctl_isc_handler_finish_ser_only(ctl_softc, 827 &msg_info); 828 break; 829 830 /* Preformed on Originating SC */ 831 case CTL_MSG_BAD_JUJU: 832 io = msg_info.hdr.original_sc; 833 if (io == NULL) { 834 printf("%s: Bad JUJU!, original_sc is NULL!\n", 835 __func__); 836 break; 837 } 838 ctl_copy_sense_data(&msg_info, io); 839 /* 840 * IO should have already been cleaned up on other 841 * SC so clear this flag so we won't send a message 842 * back to finish the IO there. 843 */ 844 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 845 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 846 847 /* io = msg_info.hdr.serializing_sc; */ 848 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 849 ctl_enqueue_isc(io); 850 break; 851 852 /* Handle resets sent from the other side */ 853 case CTL_MSG_MANAGE_TASKS: { 854 struct ctl_taskio *taskio; 855 taskio = (struct ctl_taskio *)ctl_alloc_io( 856 (void *)ctl_softc->othersc_pool); 857 if (taskio == NULL) { 858 printf("ctl_isc_event_handler: can't allocate " 859 "ctl_io!\n"); 860 /* Bad Juju */ 861 /* should I just call the proper reset func 862 here??? */ 863 goto bailout; 864 } 865 ctl_zero_io((union ctl_io *)taskio); 866 taskio->io_hdr.io_type = CTL_IO_TASK; 867 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 868 taskio->io_hdr.nexus = msg_info.hdr.nexus; 869 taskio->task_action = msg_info.task.task_action; 870 taskio->tag_num = msg_info.task.tag_num; 871 taskio->tag_type = msg_info.task.tag_type; 872 #ifdef CTL_TIME_IO 873 taskio->io_hdr.start_time = time_uptime; 874 getbintime(&taskio->io_hdr.start_bt); 875 #if 0 876 cs_prof_gettime(&taskio->io_hdr.start_ticks); 877 #endif 878 #endif /* CTL_TIME_IO */ 879 ctl_run_task((union ctl_io *)taskio); 880 break; 881 } 882 /* Persistent Reserve action which needs attention */ 883 case CTL_MSG_PERS_ACTION: 884 presio = (struct ctl_prio *)ctl_alloc_io( 885 (void *)ctl_softc->othersc_pool); 886 if (presio == NULL) { 887 printf("ctl_isc_event_handler: can't allocate " 888 "ctl_io!\n"); 889 /* Bad Juju */ 890 /* Need to set busy and send msg back */ 891 goto bailout; 892 } 893 ctl_zero_io((union ctl_io *)presio); 894 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 895 presio->pr_msg = msg_info.pr; 896 ctl_enqueue_isc((union ctl_io *)presio); 897 break; 898 case CTL_MSG_SYNC_FE: 899 rcv_sync_msg = 1; 900 break; 901 default: 902 printf("How did I get here?\n"); 903 } 904 } else if (event == CTL_HA_EVT_MSG_SENT) { 905 if (param != CTL_HA_STATUS_SUCCESS) { 906 printf("Bad status from ctl_ha_msg_send status %d\n", 907 param); 908 } 909 return; 910 } else if (event == CTL_HA_EVT_DISCONNECT) { 911 printf("CTL: Got a disconnect from Isc\n"); 912 return; 913 } else { 914 printf("ctl_isc_event_handler: Unknown event %d\n", event); 915 return; 916 } 917 918 bailout: 919 return; 920 } 921 922 static void 923 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 924 { 925 struct scsi_sense_data *sense; 926 927 sense = &dest->scsiio.sense_data; 928 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 929 dest->scsiio.scsi_status = src->scsi.scsi_status; 930 dest->scsiio.sense_len = src->scsi.sense_len; 931 dest->io_hdr.status = src->hdr.status; 932 } 933 934 static int 935 ctl_init(void) 936 { 937 struct ctl_softc *softc; 938 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 939 struct ctl_port *port; 940 uint8_t sc_id =0; 941 int i, error, retval; 942 //int isc_retval; 943 944 retval = 0; 945 ctl_pause_rtr = 0; 946 rcv_sync_msg = 0; 947 948 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 949 M_WAITOK | M_ZERO); 950 softc = control_softc; 951 952 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 953 "cam/ctl"); 954 955 softc->dev->si_drv1 = softc; 956 957 /* 958 * By default, return a "bad LUN" peripheral qualifier for unknown 959 * LUNs. The user can override this default using the tunable or 960 * sysctl. See the comment in ctl_inquiry_std() for more details. 961 */ 962 softc->inquiry_pq_no_lun = 1; 963 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 964 &softc->inquiry_pq_no_lun); 965 sysctl_ctx_init(&softc->sysctl_ctx); 966 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 967 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 968 CTLFLAG_RD, 0, "CAM Target Layer"); 969 970 if (softc->sysctl_tree == NULL) { 971 printf("%s: unable to allocate sysctl tree\n", __func__); 972 destroy_dev(softc->dev); 973 free(control_softc, M_DEVBUF); 974 control_softc = NULL; 975 return (ENOMEM); 976 } 977 978 SYSCTL_ADD_INT(&softc->sysctl_ctx, 979 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 980 "inquiry_pq_no_lun", CTLFLAG_RW, 981 &softc->inquiry_pq_no_lun, 0, 982 "Report no lun possible for invalid LUNs"); 983 984 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 985 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 986 softc->open_count = 0; 987 988 /* 989 * Default to actually sending a SYNCHRONIZE CACHE command down to 990 * the drive. 991 */ 992 softc->flags = CTL_FLAG_REAL_SYNC; 993 994 /* 995 * In Copan's HA scheme, the "master" and "slave" roles are 996 * figured out through the slot the controller is in. Although it 997 * is an active/active system, someone has to be in charge. 998 */ 999 #ifdef NEEDTOPORT 1000 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1001 #endif 1002 1003 if (sc_id == 0) { 1004 softc->flags |= CTL_FLAG_MASTER_SHELF; 1005 persis_offset = 0; 1006 } else 1007 persis_offset = CTL_MAX_INITIATORS; 1008 1009 /* 1010 * XXX KDM need to figure out where we want to get our target ID 1011 * and WWID. Is it different on each port? 1012 */ 1013 softc->target.id = 0; 1014 softc->target.wwid[0] = 0x12345678; 1015 softc->target.wwid[1] = 0x87654321; 1016 STAILQ_INIT(&softc->lun_list); 1017 STAILQ_INIT(&softc->pending_lun_queue); 1018 STAILQ_INIT(&softc->fe_list); 1019 STAILQ_INIT(&softc->port_list); 1020 STAILQ_INIT(&softc->be_list); 1021 STAILQ_INIT(&softc->io_pools); 1022 ctl_tpc_init(softc); 1023 1024 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1025 &internal_pool)!= 0){ 1026 printf("ctl: can't allocate %d entry internal pool, " 1027 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1028 return (ENOMEM); 1029 } 1030 1031 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1032 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1033 printf("ctl: can't allocate %d entry emergency pool, " 1034 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1035 ctl_pool_free(internal_pool); 1036 return (ENOMEM); 1037 } 1038 1039 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1040 &other_pool) != 0) 1041 { 1042 printf("ctl: can't allocate %d entry other SC pool, " 1043 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1044 ctl_pool_free(internal_pool); 1045 ctl_pool_free(emergency_pool); 1046 return (ENOMEM); 1047 } 1048 1049 softc->internal_pool = internal_pool; 1050 softc->emergency_pool = emergency_pool; 1051 softc->othersc_pool = other_pool; 1052 1053 if (worker_threads <= 0) 1054 worker_threads = max(1, mp_ncpus / 4); 1055 if (worker_threads > CTL_MAX_THREADS) 1056 worker_threads = CTL_MAX_THREADS; 1057 1058 for (i = 0; i < worker_threads; i++) { 1059 struct ctl_thread *thr = &softc->threads[i]; 1060 1061 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1062 thr->ctl_softc = softc; 1063 STAILQ_INIT(&thr->incoming_queue); 1064 STAILQ_INIT(&thr->rtr_queue); 1065 STAILQ_INIT(&thr->done_queue); 1066 STAILQ_INIT(&thr->isc_queue); 1067 1068 error = kproc_kthread_add(ctl_work_thread, thr, 1069 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1070 if (error != 0) { 1071 printf("error creating CTL work thread!\n"); 1072 ctl_pool_free(internal_pool); 1073 ctl_pool_free(emergency_pool); 1074 ctl_pool_free(other_pool); 1075 return (error); 1076 } 1077 } 1078 error = kproc_kthread_add(ctl_lun_thread, softc, 1079 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1080 if (error != 0) { 1081 printf("error creating CTL lun thread!\n"); 1082 ctl_pool_free(internal_pool); 1083 ctl_pool_free(emergency_pool); 1084 ctl_pool_free(other_pool); 1085 return (error); 1086 } 1087 if (bootverbose) 1088 printf("ctl: CAM Target Layer loaded\n"); 1089 1090 /* 1091 * Initialize the ioctl front end. 1092 */ 1093 ctl_frontend_register(&ioctl_frontend); 1094 port = &softc->ioctl_info.port; 1095 port->frontend = &ioctl_frontend; 1096 sprintf(softc->ioctl_info.port_name, "ioctl"); 1097 port->port_type = CTL_PORT_IOCTL; 1098 port->num_requested_ctl_io = 100; 1099 port->port_name = softc->ioctl_info.port_name; 1100 port->port_online = ctl_ioctl_online; 1101 port->port_offline = ctl_ioctl_offline; 1102 port->onoff_arg = &softc->ioctl_info; 1103 port->lun_enable = ctl_ioctl_lun_enable; 1104 port->lun_disable = ctl_ioctl_lun_disable; 1105 port->targ_lun_arg = &softc->ioctl_info; 1106 port->fe_datamove = ctl_ioctl_datamove; 1107 port->fe_done = ctl_ioctl_done; 1108 port->max_targets = 15; 1109 port->max_target_id = 15; 1110 1111 if (ctl_port_register(&softc->ioctl_info.port, 1112 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1113 printf("ctl: ioctl front end registration failed, will " 1114 "continue anyway\n"); 1115 } 1116 1117 #ifdef CTL_IO_DELAY 1118 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1119 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1120 sizeof(struct callout), CTL_TIMER_BYTES); 1121 return (EINVAL); 1122 } 1123 #endif /* CTL_IO_DELAY */ 1124 1125 return (0); 1126 } 1127 1128 void 1129 ctl_shutdown(void) 1130 { 1131 struct ctl_softc *softc; 1132 struct ctl_lun *lun, *next_lun; 1133 struct ctl_io_pool *pool; 1134 1135 softc = (struct ctl_softc *)control_softc; 1136 1137 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1138 printf("ctl: ioctl front end deregistration failed\n"); 1139 1140 mtx_lock(&softc->ctl_lock); 1141 1142 /* 1143 * Free up each LUN. 1144 */ 1145 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1146 next_lun = STAILQ_NEXT(lun, links); 1147 ctl_free_lun(lun); 1148 } 1149 1150 mtx_unlock(&softc->ctl_lock); 1151 1152 ctl_frontend_deregister(&ioctl_frontend); 1153 1154 /* 1155 * This will rip the rug out from under any FETDs or anyone else 1156 * that has a pool allocated. Since we increment our module 1157 * refcount any time someone outside the main CTL module allocates 1158 * a pool, we shouldn't have any problems here. The user won't be 1159 * able to unload the CTL module until client modules have 1160 * successfully unloaded. 1161 */ 1162 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1163 ctl_pool_free(pool); 1164 1165 #if 0 1166 ctl_shutdown_thread(softc->work_thread); 1167 mtx_destroy(&softc->queue_lock); 1168 #endif 1169 1170 ctl_tpc_shutdown(softc); 1171 mtx_destroy(&softc->pool_lock); 1172 mtx_destroy(&softc->ctl_lock); 1173 1174 destroy_dev(softc->dev); 1175 1176 sysctl_ctx_free(&softc->sysctl_ctx); 1177 1178 free(control_softc, M_DEVBUF); 1179 control_softc = NULL; 1180 1181 if (bootverbose) 1182 printf("ctl: CAM Target Layer unloaded\n"); 1183 } 1184 1185 static int 1186 ctl_module_event_handler(module_t mod, int what, void *arg) 1187 { 1188 1189 switch (what) { 1190 case MOD_LOAD: 1191 return (ctl_init()); 1192 case MOD_UNLOAD: 1193 return (EBUSY); 1194 default: 1195 return (EOPNOTSUPP); 1196 } 1197 } 1198 1199 /* 1200 * XXX KDM should we do some access checks here? Bump a reference count to 1201 * prevent a CTL module from being unloaded while someone has it open? 1202 */ 1203 static int 1204 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1205 { 1206 return (0); 1207 } 1208 1209 static int 1210 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1211 { 1212 return (0); 1213 } 1214 1215 int 1216 ctl_port_enable(ctl_port_type port_type) 1217 { 1218 struct ctl_softc *softc; 1219 struct ctl_port *port; 1220 1221 if (ctl_is_single == 0) { 1222 union ctl_ha_msg msg_info; 1223 int isc_retval; 1224 1225 #if 0 1226 printf("%s: HA mode, synchronizing frontend enable\n", 1227 __func__); 1228 #endif 1229 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1230 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1231 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1232 printf("Sync msg send error retval %d\n", isc_retval); 1233 } 1234 if (!rcv_sync_msg) { 1235 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1236 sizeof(msg_info), 1); 1237 } 1238 #if 0 1239 printf("CTL:Frontend Enable\n"); 1240 } else { 1241 printf("%s: single mode, skipping frontend synchronization\n", 1242 __func__); 1243 #endif 1244 } 1245 1246 softc = control_softc; 1247 1248 STAILQ_FOREACH(port, &softc->port_list, links) { 1249 if (port_type & port->port_type) 1250 { 1251 #if 0 1252 printf("port %d\n", port->targ_port); 1253 #endif 1254 ctl_port_online(port); 1255 } 1256 } 1257 1258 return (0); 1259 } 1260 1261 int 1262 ctl_port_disable(ctl_port_type port_type) 1263 { 1264 struct ctl_softc *softc; 1265 struct ctl_port *port; 1266 1267 softc = control_softc; 1268 1269 STAILQ_FOREACH(port, &softc->port_list, links) { 1270 if (port_type & port->port_type) 1271 ctl_port_offline(port); 1272 } 1273 1274 return (0); 1275 } 1276 1277 /* 1278 * Returns 0 for success, 1 for failure. 1279 * Currently the only failure mode is if there aren't enough entries 1280 * allocated. So, in case of a failure, look at num_entries_dropped, 1281 * reallocate and try again. 1282 */ 1283 int 1284 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1285 int *num_entries_filled, int *num_entries_dropped, 1286 ctl_port_type port_type, int no_virtual) 1287 { 1288 struct ctl_softc *softc; 1289 struct ctl_port *port; 1290 int entries_dropped, entries_filled; 1291 int retval; 1292 int i; 1293 1294 softc = control_softc; 1295 1296 retval = 0; 1297 entries_filled = 0; 1298 entries_dropped = 0; 1299 1300 i = 0; 1301 mtx_lock(&softc->ctl_lock); 1302 STAILQ_FOREACH(port, &softc->port_list, links) { 1303 struct ctl_port_entry *entry; 1304 1305 if ((port->port_type & port_type) == 0) 1306 continue; 1307 1308 if ((no_virtual != 0) 1309 && (port->virtual_port != 0)) 1310 continue; 1311 1312 if (entries_filled >= num_entries_alloced) { 1313 entries_dropped++; 1314 continue; 1315 } 1316 entry = &entries[i]; 1317 1318 entry->port_type = port->port_type; 1319 strlcpy(entry->port_name, port->port_name, 1320 sizeof(entry->port_name)); 1321 entry->physical_port = port->physical_port; 1322 entry->virtual_port = port->virtual_port; 1323 entry->wwnn = port->wwnn; 1324 entry->wwpn = port->wwpn; 1325 1326 i++; 1327 entries_filled++; 1328 } 1329 1330 mtx_unlock(&softc->ctl_lock); 1331 1332 if (entries_dropped > 0) 1333 retval = 1; 1334 1335 *num_entries_dropped = entries_dropped; 1336 *num_entries_filled = entries_filled; 1337 1338 return (retval); 1339 } 1340 1341 static void 1342 ctl_ioctl_online(void *arg) 1343 { 1344 struct ctl_ioctl_info *ioctl_info; 1345 1346 ioctl_info = (struct ctl_ioctl_info *)arg; 1347 1348 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1349 } 1350 1351 static void 1352 ctl_ioctl_offline(void *arg) 1353 { 1354 struct ctl_ioctl_info *ioctl_info; 1355 1356 ioctl_info = (struct ctl_ioctl_info *)arg; 1357 1358 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1359 } 1360 1361 /* 1362 * Remove an initiator by port number and initiator ID. 1363 * Returns 0 for success, -1 for failure. 1364 */ 1365 int 1366 ctl_remove_initiator(struct ctl_port *port, int iid) 1367 { 1368 struct ctl_softc *softc = control_softc; 1369 1370 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1371 1372 if (iid > CTL_MAX_INIT_PER_PORT) { 1373 printf("%s: initiator ID %u > maximun %u!\n", 1374 __func__, iid, CTL_MAX_INIT_PER_PORT); 1375 return (-1); 1376 } 1377 1378 mtx_lock(&softc->ctl_lock); 1379 port->wwpn_iid[iid].in_use--; 1380 port->wwpn_iid[iid].last_use = time_uptime; 1381 mtx_unlock(&softc->ctl_lock); 1382 1383 return (0); 1384 } 1385 1386 /* 1387 * Add an initiator to the initiator map. 1388 * Returns iid for success, < 0 for failure. 1389 */ 1390 int 1391 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1392 { 1393 struct ctl_softc *softc = control_softc; 1394 time_t best_time; 1395 int i, best; 1396 1397 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1398 1399 if (iid >= CTL_MAX_INIT_PER_PORT) { 1400 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1401 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1402 free(name, M_CTL); 1403 return (-1); 1404 } 1405 1406 mtx_lock(&softc->ctl_lock); 1407 1408 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1409 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1410 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1411 iid = i; 1412 break; 1413 } 1414 if (name != NULL && port->wwpn_iid[i].name != NULL && 1415 strcmp(name, port->wwpn_iid[i].name) == 0) { 1416 iid = i; 1417 break; 1418 } 1419 } 1420 } 1421 1422 if (iid < 0) { 1423 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1424 if (port->wwpn_iid[i].in_use == 0 && 1425 port->wwpn_iid[i].wwpn == 0 && 1426 port->wwpn_iid[i].name == NULL) { 1427 iid = i; 1428 break; 1429 } 1430 } 1431 } 1432 1433 if (iid < 0) { 1434 best = -1; 1435 best_time = INT32_MAX; 1436 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1437 if (port->wwpn_iid[i].in_use == 0) { 1438 if (port->wwpn_iid[i].last_use < best_time) { 1439 best = i; 1440 best_time = port->wwpn_iid[i].last_use; 1441 } 1442 } 1443 } 1444 iid = best; 1445 } 1446 1447 if (iid < 0) { 1448 mtx_unlock(&softc->ctl_lock); 1449 free(name, M_CTL); 1450 return (-2); 1451 } 1452 1453 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1454 /* 1455 * This is not an error yet. 1456 */ 1457 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1458 #if 0 1459 printf("%s: port %d iid %u WWPN %#jx arrived" 1460 " again\n", __func__, port->targ_port, 1461 iid, (uintmax_t)wwpn); 1462 #endif 1463 goto take; 1464 } 1465 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1466 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1467 #if 0 1468 printf("%s: port %d iid %u name '%s' arrived" 1469 " again\n", __func__, port->targ_port, 1470 iid, name); 1471 #endif 1472 goto take; 1473 } 1474 1475 /* 1476 * This is an error, but what do we do about it? The 1477 * driver is telling us we have a new WWPN for this 1478 * initiator ID, so we pretty much need to use it. 1479 */ 1480 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1481 " but WWPN %#jx '%s' is still at that address\n", 1482 __func__, port->targ_port, iid, wwpn, name, 1483 (uintmax_t)port->wwpn_iid[iid].wwpn, 1484 port->wwpn_iid[iid].name); 1485 1486 /* 1487 * XXX KDM clear have_ca and ua_pending on each LUN for 1488 * this initiator. 1489 */ 1490 } 1491 take: 1492 free(port->wwpn_iid[iid].name, M_CTL); 1493 port->wwpn_iid[iid].name = name; 1494 port->wwpn_iid[iid].wwpn = wwpn; 1495 port->wwpn_iid[iid].in_use++; 1496 mtx_unlock(&softc->ctl_lock); 1497 1498 return (iid); 1499 } 1500 1501 static int 1502 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1503 { 1504 int len; 1505 1506 switch (port->port_type) { 1507 case CTL_PORT_FC: 1508 { 1509 struct scsi_transportid_fcp *id = 1510 (struct scsi_transportid_fcp *)buf; 1511 if (port->wwpn_iid[iid].wwpn == 0) 1512 return (0); 1513 memset(id, 0, sizeof(*id)); 1514 id->format_protocol = SCSI_PROTO_FC; 1515 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1516 return (sizeof(*id)); 1517 } 1518 case CTL_PORT_ISCSI: 1519 { 1520 struct scsi_transportid_iscsi_port *id = 1521 (struct scsi_transportid_iscsi_port *)buf; 1522 if (port->wwpn_iid[iid].name == NULL) 1523 return (0); 1524 memset(id, 0, 256); 1525 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1526 SCSI_PROTO_ISCSI; 1527 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1528 len = roundup2(min(len, 252), 4); 1529 scsi_ulto2b(len, id->additional_length); 1530 return (sizeof(*id) + len); 1531 } 1532 case CTL_PORT_SAS: 1533 { 1534 struct scsi_transportid_sas *id = 1535 (struct scsi_transportid_sas *)buf; 1536 if (port->wwpn_iid[iid].wwpn == 0) 1537 return (0); 1538 memset(id, 0, sizeof(*id)); 1539 id->format_protocol = SCSI_PROTO_SAS; 1540 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1541 return (sizeof(*id)); 1542 } 1543 default: 1544 { 1545 struct scsi_transportid_spi *id = 1546 (struct scsi_transportid_spi *)buf; 1547 memset(id, 0, sizeof(*id)); 1548 id->format_protocol = SCSI_PROTO_SPI; 1549 scsi_ulto2b(iid, id->scsi_addr); 1550 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1551 return (sizeof(*id)); 1552 } 1553 } 1554 } 1555 1556 static int 1557 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1558 { 1559 return (0); 1560 } 1561 1562 static int 1563 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1564 { 1565 return (0); 1566 } 1567 1568 /* 1569 * Data movement routine for the CTL ioctl frontend port. 1570 */ 1571 static int 1572 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1573 { 1574 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1575 struct ctl_sg_entry ext_entry, kern_entry; 1576 int ext_sglen, ext_sg_entries, kern_sg_entries; 1577 int ext_sg_start, ext_offset; 1578 int len_to_copy, len_copied; 1579 int kern_watermark, ext_watermark; 1580 int ext_sglist_malloced; 1581 int i, j; 1582 1583 ext_sglist_malloced = 0; 1584 ext_sg_start = 0; 1585 ext_offset = 0; 1586 1587 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1588 1589 /* 1590 * If this flag is set, fake the data transfer. 1591 */ 1592 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1593 ctsio->ext_data_filled = ctsio->ext_data_len; 1594 goto bailout; 1595 } 1596 1597 /* 1598 * To simplify things here, if we have a single buffer, stick it in 1599 * a S/G entry and just make it a single entry S/G list. 1600 */ 1601 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1602 int len_seen; 1603 1604 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1605 1606 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1607 M_WAITOK); 1608 ext_sglist_malloced = 1; 1609 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1610 ext_sglen) != 0) { 1611 ctl_set_internal_failure(ctsio, 1612 /*sks_valid*/ 0, 1613 /*retry_count*/ 0); 1614 goto bailout; 1615 } 1616 ext_sg_entries = ctsio->ext_sg_entries; 1617 len_seen = 0; 1618 for (i = 0; i < ext_sg_entries; i++) { 1619 if ((len_seen + ext_sglist[i].len) >= 1620 ctsio->ext_data_filled) { 1621 ext_sg_start = i; 1622 ext_offset = ctsio->ext_data_filled - len_seen; 1623 break; 1624 } 1625 len_seen += ext_sglist[i].len; 1626 } 1627 } else { 1628 ext_sglist = &ext_entry; 1629 ext_sglist->addr = ctsio->ext_data_ptr; 1630 ext_sglist->len = ctsio->ext_data_len; 1631 ext_sg_entries = 1; 1632 ext_sg_start = 0; 1633 ext_offset = ctsio->ext_data_filled; 1634 } 1635 1636 if (ctsio->kern_sg_entries > 0) { 1637 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1638 kern_sg_entries = ctsio->kern_sg_entries; 1639 } else { 1640 kern_sglist = &kern_entry; 1641 kern_sglist->addr = ctsio->kern_data_ptr; 1642 kern_sglist->len = ctsio->kern_data_len; 1643 kern_sg_entries = 1; 1644 } 1645 1646 1647 kern_watermark = 0; 1648 ext_watermark = ext_offset; 1649 len_copied = 0; 1650 for (i = ext_sg_start, j = 0; 1651 i < ext_sg_entries && j < kern_sg_entries;) { 1652 uint8_t *ext_ptr, *kern_ptr; 1653 1654 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1655 kern_sglist[j].len - kern_watermark); 1656 1657 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1658 ext_ptr = ext_ptr + ext_watermark; 1659 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1660 /* 1661 * XXX KDM fix this! 1662 */ 1663 panic("need to implement bus address support"); 1664 #if 0 1665 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1666 #endif 1667 } else 1668 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1669 kern_ptr = kern_ptr + kern_watermark; 1670 1671 kern_watermark += len_to_copy; 1672 ext_watermark += len_to_copy; 1673 1674 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1675 CTL_FLAG_DATA_IN) { 1676 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1677 "bytes to user\n", len_to_copy)); 1678 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1679 "to %p\n", kern_ptr, ext_ptr)); 1680 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1681 ctl_set_internal_failure(ctsio, 1682 /*sks_valid*/ 0, 1683 /*retry_count*/ 0); 1684 goto bailout; 1685 } 1686 } else { 1687 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1688 "bytes from user\n", len_to_copy)); 1689 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1690 "to %p\n", ext_ptr, kern_ptr)); 1691 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1692 ctl_set_internal_failure(ctsio, 1693 /*sks_valid*/ 0, 1694 /*retry_count*/0); 1695 goto bailout; 1696 } 1697 } 1698 1699 len_copied += len_to_copy; 1700 1701 if (ext_sglist[i].len == ext_watermark) { 1702 i++; 1703 ext_watermark = 0; 1704 } 1705 1706 if (kern_sglist[j].len == kern_watermark) { 1707 j++; 1708 kern_watermark = 0; 1709 } 1710 } 1711 1712 ctsio->ext_data_filled += len_copied; 1713 1714 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1715 "kern_sg_entries: %d\n", ext_sg_entries, 1716 kern_sg_entries)); 1717 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1718 "kern_data_len = %d\n", ctsio->ext_data_len, 1719 ctsio->kern_data_len)); 1720 1721 1722 /* XXX KDM set residual?? */ 1723 bailout: 1724 1725 if (ext_sglist_malloced != 0) 1726 free(ext_sglist, M_CTL); 1727 1728 return (CTL_RETVAL_COMPLETE); 1729 } 1730 1731 /* 1732 * Serialize a command that went down the "wrong" side, and so was sent to 1733 * this controller for execution. The logic is a little different than the 1734 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1735 * sent back to the other side, but in the success case, we execute the 1736 * command on this side (XFER mode) or tell the other side to execute it 1737 * (SER_ONLY mode). 1738 */ 1739 static int 1740 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1741 { 1742 struct ctl_softc *ctl_softc; 1743 union ctl_ha_msg msg_info; 1744 struct ctl_lun *lun; 1745 int retval = 0; 1746 uint32_t targ_lun; 1747 1748 ctl_softc = control_softc; 1749 1750 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1751 lun = ctl_softc->ctl_luns[targ_lun]; 1752 if (lun==NULL) 1753 { 1754 /* 1755 * Why isn't LUN defined? The other side wouldn't 1756 * send a cmd if the LUN is undefined. 1757 */ 1758 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1759 1760 /* "Logical unit not supported" */ 1761 ctl_set_sense_data(&msg_info.scsi.sense_data, 1762 lun, 1763 /*sense_format*/SSD_TYPE_NONE, 1764 /*current_error*/ 1, 1765 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1766 /*asc*/ 0x25, 1767 /*ascq*/ 0x00, 1768 SSD_ELEM_NONE); 1769 1770 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1771 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1772 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1773 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1774 msg_info.hdr.serializing_sc = NULL; 1775 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1776 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1777 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1778 } 1779 return(1); 1780 1781 } 1782 1783 mtx_lock(&lun->lun_lock); 1784 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1785 1786 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1787 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1788 ooa_links))) { 1789 case CTL_ACTION_BLOCK: 1790 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1791 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1792 blocked_links); 1793 break; 1794 case CTL_ACTION_PASS: 1795 case CTL_ACTION_SKIP: 1796 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1797 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1798 ctl_enqueue_rtr((union ctl_io *)ctsio); 1799 } else { 1800 1801 /* send msg back to other side */ 1802 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1803 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1804 msg_info.hdr.msg_type = CTL_MSG_R2R; 1805 #if 0 1806 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1807 #endif 1808 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1809 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1810 } 1811 } 1812 break; 1813 case CTL_ACTION_OVERLAP: 1814 /* OVERLAPPED COMMANDS ATTEMPTED */ 1815 ctl_set_sense_data(&msg_info.scsi.sense_data, 1816 lun, 1817 /*sense_format*/SSD_TYPE_NONE, 1818 /*current_error*/ 1, 1819 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1820 /*asc*/ 0x4E, 1821 /*ascq*/ 0x00, 1822 SSD_ELEM_NONE); 1823 1824 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1825 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1826 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1827 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1828 msg_info.hdr.serializing_sc = NULL; 1829 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1830 #if 0 1831 printf("BAD JUJU:Major Bummer Overlap\n"); 1832 #endif 1833 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1834 retval = 1; 1835 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1836 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1837 } 1838 break; 1839 case CTL_ACTION_OVERLAP_TAG: 1840 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1841 ctl_set_sense_data(&msg_info.scsi.sense_data, 1842 lun, 1843 /*sense_format*/SSD_TYPE_NONE, 1844 /*current_error*/ 1, 1845 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1846 /*asc*/ 0x4D, 1847 /*ascq*/ ctsio->tag_num & 0xff, 1848 SSD_ELEM_NONE); 1849 1850 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1851 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1852 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1853 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1854 msg_info.hdr.serializing_sc = NULL; 1855 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1856 #if 0 1857 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1858 #endif 1859 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1860 retval = 1; 1861 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1862 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1863 } 1864 break; 1865 case CTL_ACTION_ERROR: 1866 default: 1867 /* "Internal target failure" */ 1868 ctl_set_sense_data(&msg_info.scsi.sense_data, 1869 lun, 1870 /*sense_format*/SSD_TYPE_NONE, 1871 /*current_error*/ 1, 1872 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1873 /*asc*/ 0x44, 1874 /*ascq*/ 0x00, 1875 SSD_ELEM_NONE); 1876 1877 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1878 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1879 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1880 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1881 msg_info.hdr.serializing_sc = NULL; 1882 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1883 #if 0 1884 printf("BAD JUJU:Major Bummer HW Error\n"); 1885 #endif 1886 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1887 retval = 1; 1888 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1889 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1890 } 1891 break; 1892 } 1893 mtx_unlock(&lun->lun_lock); 1894 return (retval); 1895 } 1896 1897 static int 1898 ctl_ioctl_submit_wait(union ctl_io *io) 1899 { 1900 struct ctl_fe_ioctl_params params; 1901 ctl_fe_ioctl_state last_state; 1902 int done, retval; 1903 1904 retval = 0; 1905 1906 bzero(¶ms, sizeof(params)); 1907 1908 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1909 cv_init(¶ms.sem, "ctlioccv"); 1910 params.state = CTL_IOCTL_INPROG; 1911 last_state = params.state; 1912 1913 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1914 1915 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1916 1917 /* This shouldn't happen */ 1918 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1919 return (retval); 1920 1921 done = 0; 1922 1923 do { 1924 mtx_lock(¶ms.ioctl_mtx); 1925 /* 1926 * Check the state here, and don't sleep if the state has 1927 * already changed (i.e. wakeup has already occured, but we 1928 * weren't waiting yet). 1929 */ 1930 if (params.state == last_state) { 1931 /* XXX KDM cv_wait_sig instead? */ 1932 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1933 } 1934 last_state = params.state; 1935 1936 switch (params.state) { 1937 case CTL_IOCTL_INPROG: 1938 /* Why did we wake up? */ 1939 /* XXX KDM error here? */ 1940 mtx_unlock(¶ms.ioctl_mtx); 1941 break; 1942 case CTL_IOCTL_DATAMOVE: 1943 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1944 1945 /* 1946 * change last_state back to INPROG to avoid 1947 * deadlock on subsequent data moves. 1948 */ 1949 params.state = last_state = CTL_IOCTL_INPROG; 1950 1951 mtx_unlock(¶ms.ioctl_mtx); 1952 ctl_ioctl_do_datamove(&io->scsiio); 1953 /* 1954 * Note that in some cases, most notably writes, 1955 * this will queue the I/O and call us back later. 1956 * In other cases, generally reads, this routine 1957 * will immediately call back and wake us up, 1958 * probably using our own context. 1959 */ 1960 io->scsiio.be_move_done(io); 1961 break; 1962 case CTL_IOCTL_DONE: 1963 mtx_unlock(¶ms.ioctl_mtx); 1964 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1965 done = 1; 1966 break; 1967 default: 1968 mtx_unlock(¶ms.ioctl_mtx); 1969 /* XXX KDM error here? */ 1970 break; 1971 } 1972 } while (done == 0); 1973 1974 mtx_destroy(¶ms.ioctl_mtx); 1975 cv_destroy(¶ms.sem); 1976 1977 return (CTL_RETVAL_COMPLETE); 1978 } 1979 1980 static void 1981 ctl_ioctl_datamove(union ctl_io *io) 1982 { 1983 struct ctl_fe_ioctl_params *params; 1984 1985 params = (struct ctl_fe_ioctl_params *) 1986 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1987 1988 mtx_lock(¶ms->ioctl_mtx); 1989 params->state = CTL_IOCTL_DATAMOVE; 1990 cv_broadcast(¶ms->sem); 1991 mtx_unlock(¶ms->ioctl_mtx); 1992 } 1993 1994 static void 1995 ctl_ioctl_done(union ctl_io *io) 1996 { 1997 struct ctl_fe_ioctl_params *params; 1998 1999 params = (struct ctl_fe_ioctl_params *) 2000 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2001 2002 mtx_lock(¶ms->ioctl_mtx); 2003 params->state = CTL_IOCTL_DONE; 2004 cv_broadcast(¶ms->sem); 2005 mtx_unlock(¶ms->ioctl_mtx); 2006 } 2007 2008 static void 2009 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2010 { 2011 struct ctl_fe_ioctl_startstop_info *sd_info; 2012 2013 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2014 2015 sd_info->hs_info.status = metatask->status; 2016 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2017 sd_info->hs_info.luns_complete = 2018 metatask->taskinfo.startstop.luns_complete; 2019 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2020 2021 cv_broadcast(&sd_info->sem); 2022 } 2023 2024 static void 2025 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2026 { 2027 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2028 2029 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2030 2031 mtx_lock(fe_bbr_info->lock); 2032 fe_bbr_info->bbr_info->status = metatask->status; 2033 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2034 fe_bbr_info->wakeup_done = 1; 2035 mtx_unlock(fe_bbr_info->lock); 2036 2037 cv_broadcast(&fe_bbr_info->sem); 2038 } 2039 2040 /* 2041 * Returns 0 for success, errno for failure. 2042 */ 2043 static int 2044 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2045 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2046 { 2047 union ctl_io *io; 2048 int retval; 2049 2050 retval = 0; 2051 2052 mtx_lock(&lun->lun_lock); 2053 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2054 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2055 ooa_links)) { 2056 struct ctl_ooa_entry *entry; 2057 2058 /* 2059 * If we've got more than we can fit, just count the 2060 * remaining entries. 2061 */ 2062 if (*cur_fill_num >= ooa_hdr->alloc_num) 2063 continue; 2064 2065 entry = &kern_entries[*cur_fill_num]; 2066 2067 entry->tag_num = io->scsiio.tag_num; 2068 entry->lun_num = lun->lun; 2069 #ifdef CTL_TIME_IO 2070 entry->start_bt = io->io_hdr.start_bt; 2071 #endif 2072 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2073 entry->cdb_len = io->scsiio.cdb_len; 2074 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2075 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2076 2077 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2078 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2079 2080 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2081 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2082 2083 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2084 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2085 2086 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2087 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2088 } 2089 mtx_unlock(&lun->lun_lock); 2090 2091 return (retval); 2092 } 2093 2094 static void * 2095 ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2096 size_t error_str_len) 2097 { 2098 void *kptr; 2099 2100 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2101 2102 if (copyin(user_addr, kptr, len) != 0) { 2103 snprintf(error_str, error_str_len, "Error copying %d bytes " 2104 "from user address %p to kernel address %p", len, 2105 user_addr, kptr); 2106 free(kptr, M_CTL); 2107 return (NULL); 2108 } 2109 2110 return (kptr); 2111 } 2112 2113 static void 2114 ctl_free_args(int num_args, struct ctl_be_arg *args) 2115 { 2116 int i; 2117 2118 if (args == NULL) 2119 return; 2120 2121 for (i = 0; i < num_args; i++) { 2122 free(args[i].kname, M_CTL); 2123 free(args[i].kvalue, M_CTL); 2124 } 2125 2126 free(args, M_CTL); 2127 } 2128 2129 static struct ctl_be_arg * 2130 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2131 char *error_str, size_t error_str_len) 2132 { 2133 struct ctl_be_arg *args; 2134 int i; 2135 2136 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2137 error_str, error_str_len); 2138 2139 if (args == NULL) 2140 goto bailout; 2141 2142 for (i = 0; i < num_args; i++) { 2143 args[i].kname = NULL; 2144 args[i].kvalue = NULL; 2145 } 2146 2147 for (i = 0; i < num_args; i++) { 2148 uint8_t *tmpptr; 2149 2150 args[i].kname = ctl_copyin_alloc(args[i].name, 2151 args[i].namelen, error_str, error_str_len); 2152 if (args[i].kname == NULL) 2153 goto bailout; 2154 2155 if (args[i].kname[args[i].namelen - 1] != '\0') { 2156 snprintf(error_str, error_str_len, "Argument %d " 2157 "name is not NUL-terminated", i); 2158 goto bailout; 2159 } 2160 2161 if (args[i].flags & CTL_BEARG_RD) { 2162 tmpptr = ctl_copyin_alloc(args[i].value, 2163 args[i].vallen, error_str, error_str_len); 2164 if (tmpptr == NULL) 2165 goto bailout; 2166 if ((args[i].flags & CTL_BEARG_ASCII) 2167 && (tmpptr[args[i].vallen - 1] != '\0')) { 2168 snprintf(error_str, error_str_len, "Argument " 2169 "%d value is not NUL-terminated", i); 2170 goto bailout; 2171 } 2172 args[i].kvalue = tmpptr; 2173 } else { 2174 args[i].kvalue = malloc(args[i].vallen, 2175 M_CTL, M_WAITOK | M_ZERO); 2176 } 2177 } 2178 2179 return (args); 2180 bailout: 2181 2182 ctl_free_args(num_args, args); 2183 2184 return (NULL); 2185 } 2186 2187 static void 2188 ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2189 { 2190 int i; 2191 2192 for (i = 0; i < num_args; i++) { 2193 if (args[i].flags & CTL_BEARG_WR) 2194 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2195 } 2196 } 2197 2198 /* 2199 * Escape characters that are illegal or not recommended in XML. 2200 */ 2201 int 2202 ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2203 { 2204 int retval; 2205 2206 retval = 0; 2207 2208 for (; *str; str++) { 2209 switch (*str) { 2210 case '&': 2211 retval = sbuf_printf(sb, "&"); 2212 break; 2213 case '>': 2214 retval = sbuf_printf(sb, ">"); 2215 break; 2216 case '<': 2217 retval = sbuf_printf(sb, "<"); 2218 break; 2219 default: 2220 retval = sbuf_putc(sb, *str); 2221 break; 2222 } 2223 2224 if (retval != 0) 2225 break; 2226 2227 } 2228 2229 return (retval); 2230 } 2231 2232 static void 2233 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2234 { 2235 struct scsi_vpd_id_descriptor *desc; 2236 int i; 2237 2238 if (id == NULL || id->len < 4) 2239 return; 2240 desc = (struct scsi_vpd_id_descriptor *)id->data; 2241 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2242 case SVPD_ID_TYPE_T10: 2243 sbuf_printf(sb, "t10."); 2244 break; 2245 case SVPD_ID_TYPE_EUI64: 2246 sbuf_printf(sb, "eui."); 2247 break; 2248 case SVPD_ID_TYPE_NAA: 2249 sbuf_printf(sb, "naa."); 2250 break; 2251 case SVPD_ID_TYPE_SCSI_NAME: 2252 break; 2253 } 2254 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2255 case SVPD_ID_CODESET_BINARY: 2256 for (i = 0; i < desc->length; i++) 2257 sbuf_printf(sb, "%02x", desc->identifier[i]); 2258 break; 2259 case SVPD_ID_CODESET_ASCII: 2260 sbuf_printf(sb, "%.*s", (int)desc->length, 2261 (char *)desc->identifier); 2262 break; 2263 case SVPD_ID_CODESET_UTF8: 2264 sbuf_printf(sb, "%s", (char *)desc->identifier); 2265 break; 2266 } 2267 } 2268 2269 static int 2270 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2271 struct thread *td) 2272 { 2273 struct ctl_softc *softc; 2274 int retval; 2275 2276 softc = control_softc; 2277 2278 retval = 0; 2279 2280 switch (cmd) { 2281 case CTL_IO: { 2282 union ctl_io *io; 2283 void *pool_tmp; 2284 2285 /* 2286 * If we haven't been "enabled", don't allow any SCSI I/O 2287 * to this FETD. 2288 */ 2289 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2290 retval = EPERM; 2291 break; 2292 } 2293 2294 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2295 if (io == NULL) { 2296 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2297 retval = ENOSPC; 2298 break; 2299 } 2300 2301 /* 2302 * Need to save the pool reference so it doesn't get 2303 * spammed by the user's ctl_io. 2304 */ 2305 pool_tmp = io->io_hdr.pool; 2306 2307 memcpy(io, (void *)addr, sizeof(*io)); 2308 2309 io->io_hdr.pool = pool_tmp; 2310 /* 2311 * No status yet, so make sure the status is set properly. 2312 */ 2313 io->io_hdr.status = CTL_STATUS_NONE; 2314 2315 /* 2316 * The user sets the initiator ID, target and LUN IDs. 2317 */ 2318 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2319 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2320 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2321 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2322 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2323 2324 retval = ctl_ioctl_submit_wait(io); 2325 2326 if (retval != 0) { 2327 ctl_free_io(io); 2328 break; 2329 } 2330 2331 memcpy((void *)addr, io, sizeof(*io)); 2332 2333 /* return this to our pool */ 2334 ctl_free_io(io); 2335 2336 break; 2337 } 2338 case CTL_ENABLE_PORT: 2339 case CTL_DISABLE_PORT: 2340 case CTL_SET_PORT_WWNS: { 2341 struct ctl_port *port; 2342 struct ctl_port_entry *entry; 2343 2344 entry = (struct ctl_port_entry *)addr; 2345 2346 mtx_lock(&softc->ctl_lock); 2347 STAILQ_FOREACH(port, &softc->port_list, links) { 2348 int action, done; 2349 2350 action = 0; 2351 done = 0; 2352 2353 if ((entry->port_type == CTL_PORT_NONE) 2354 && (entry->targ_port == port->targ_port)) { 2355 /* 2356 * If the user only wants to enable or 2357 * disable or set WWNs on a specific port, 2358 * do the operation and we're done. 2359 */ 2360 action = 1; 2361 done = 1; 2362 } else if (entry->port_type & port->port_type) { 2363 /* 2364 * Compare the user's type mask with the 2365 * particular frontend type to see if we 2366 * have a match. 2367 */ 2368 action = 1; 2369 done = 0; 2370 2371 /* 2372 * Make sure the user isn't trying to set 2373 * WWNs on multiple ports at the same time. 2374 */ 2375 if (cmd == CTL_SET_PORT_WWNS) { 2376 printf("%s: Can't set WWNs on " 2377 "multiple ports\n", __func__); 2378 retval = EINVAL; 2379 break; 2380 } 2381 } 2382 if (action != 0) { 2383 /* 2384 * XXX KDM we have to drop the lock here, 2385 * because the online/offline operations 2386 * can potentially block. We need to 2387 * reference count the frontends so they 2388 * can't go away, 2389 */ 2390 mtx_unlock(&softc->ctl_lock); 2391 2392 if (cmd == CTL_ENABLE_PORT) { 2393 struct ctl_lun *lun; 2394 2395 STAILQ_FOREACH(lun, &softc->lun_list, 2396 links) { 2397 port->lun_enable(port->targ_lun_arg, 2398 lun->target, 2399 lun->lun); 2400 } 2401 2402 ctl_port_online(port); 2403 } else if (cmd == CTL_DISABLE_PORT) { 2404 struct ctl_lun *lun; 2405 2406 ctl_port_offline(port); 2407 2408 STAILQ_FOREACH(lun, &softc->lun_list, 2409 links) { 2410 port->lun_disable( 2411 port->targ_lun_arg, 2412 lun->target, 2413 lun->lun); 2414 } 2415 } 2416 2417 mtx_lock(&softc->ctl_lock); 2418 2419 if (cmd == CTL_SET_PORT_WWNS) 2420 ctl_port_set_wwns(port, 2421 (entry->flags & CTL_PORT_WWNN_VALID) ? 2422 1 : 0, entry->wwnn, 2423 (entry->flags & CTL_PORT_WWPN_VALID) ? 2424 1 : 0, entry->wwpn); 2425 } 2426 if (done != 0) 2427 break; 2428 } 2429 mtx_unlock(&softc->ctl_lock); 2430 break; 2431 } 2432 case CTL_GET_PORT_LIST: { 2433 struct ctl_port *port; 2434 struct ctl_port_list *list; 2435 int i; 2436 2437 list = (struct ctl_port_list *)addr; 2438 2439 if (list->alloc_len != (list->alloc_num * 2440 sizeof(struct ctl_port_entry))) { 2441 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2442 "alloc_num %u * sizeof(struct ctl_port_entry) " 2443 "%zu\n", __func__, list->alloc_len, 2444 list->alloc_num, sizeof(struct ctl_port_entry)); 2445 retval = EINVAL; 2446 break; 2447 } 2448 list->fill_len = 0; 2449 list->fill_num = 0; 2450 list->dropped_num = 0; 2451 i = 0; 2452 mtx_lock(&softc->ctl_lock); 2453 STAILQ_FOREACH(port, &softc->port_list, links) { 2454 struct ctl_port_entry entry, *list_entry; 2455 2456 if (list->fill_num >= list->alloc_num) { 2457 list->dropped_num++; 2458 continue; 2459 } 2460 2461 entry.port_type = port->port_type; 2462 strlcpy(entry.port_name, port->port_name, 2463 sizeof(entry.port_name)); 2464 entry.targ_port = port->targ_port; 2465 entry.physical_port = port->physical_port; 2466 entry.virtual_port = port->virtual_port; 2467 entry.wwnn = port->wwnn; 2468 entry.wwpn = port->wwpn; 2469 if (port->status & CTL_PORT_STATUS_ONLINE) 2470 entry.online = 1; 2471 else 2472 entry.online = 0; 2473 2474 list_entry = &list->entries[i]; 2475 2476 retval = copyout(&entry, list_entry, sizeof(entry)); 2477 if (retval != 0) { 2478 printf("%s: CTL_GET_PORT_LIST: copyout " 2479 "returned %d\n", __func__, retval); 2480 break; 2481 } 2482 i++; 2483 list->fill_num++; 2484 list->fill_len += sizeof(entry); 2485 } 2486 mtx_unlock(&softc->ctl_lock); 2487 2488 /* 2489 * If this is non-zero, we had a copyout fault, so there's 2490 * probably no point in attempting to set the status inside 2491 * the structure. 2492 */ 2493 if (retval != 0) 2494 break; 2495 2496 if (list->dropped_num > 0) 2497 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2498 else 2499 list->status = CTL_PORT_LIST_OK; 2500 break; 2501 } 2502 case CTL_DUMP_OOA: { 2503 struct ctl_lun *lun; 2504 union ctl_io *io; 2505 char printbuf[128]; 2506 struct sbuf sb; 2507 2508 mtx_lock(&softc->ctl_lock); 2509 printf("Dumping OOA queues:\n"); 2510 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2511 mtx_lock(&lun->lun_lock); 2512 for (io = (union ctl_io *)TAILQ_FIRST( 2513 &lun->ooa_queue); io != NULL; 2514 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2515 ooa_links)) { 2516 sbuf_new(&sb, printbuf, sizeof(printbuf), 2517 SBUF_FIXEDLEN); 2518 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2519 (intmax_t)lun->lun, 2520 io->scsiio.tag_num, 2521 (io->io_hdr.flags & 2522 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2523 (io->io_hdr.flags & 2524 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2525 (io->io_hdr.flags & 2526 CTL_FLAG_ABORT) ? " ABORT" : "", 2527 (io->io_hdr.flags & 2528 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2529 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2530 sbuf_finish(&sb); 2531 printf("%s\n", sbuf_data(&sb)); 2532 } 2533 mtx_unlock(&lun->lun_lock); 2534 } 2535 printf("OOA queues dump done\n"); 2536 mtx_unlock(&softc->ctl_lock); 2537 break; 2538 } 2539 case CTL_GET_OOA: { 2540 struct ctl_lun *lun; 2541 struct ctl_ooa *ooa_hdr; 2542 struct ctl_ooa_entry *entries; 2543 uint32_t cur_fill_num; 2544 2545 ooa_hdr = (struct ctl_ooa *)addr; 2546 2547 if ((ooa_hdr->alloc_len == 0) 2548 || (ooa_hdr->alloc_num == 0)) { 2549 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2550 "must be non-zero\n", __func__, 2551 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2552 retval = EINVAL; 2553 break; 2554 } 2555 2556 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2557 sizeof(struct ctl_ooa_entry))) { 2558 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2559 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2560 __func__, ooa_hdr->alloc_len, 2561 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2562 retval = EINVAL; 2563 break; 2564 } 2565 2566 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2567 if (entries == NULL) { 2568 printf("%s: could not allocate %d bytes for OOA " 2569 "dump\n", __func__, ooa_hdr->alloc_len); 2570 retval = ENOMEM; 2571 break; 2572 } 2573 2574 mtx_lock(&softc->ctl_lock); 2575 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2576 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2577 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2578 mtx_unlock(&softc->ctl_lock); 2579 free(entries, M_CTL); 2580 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2581 __func__, (uintmax_t)ooa_hdr->lun_num); 2582 retval = EINVAL; 2583 break; 2584 } 2585 2586 cur_fill_num = 0; 2587 2588 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2589 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2590 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2591 ooa_hdr, entries); 2592 if (retval != 0) 2593 break; 2594 } 2595 if (retval != 0) { 2596 mtx_unlock(&softc->ctl_lock); 2597 free(entries, M_CTL); 2598 break; 2599 } 2600 } else { 2601 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2602 2603 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2604 entries); 2605 } 2606 mtx_unlock(&softc->ctl_lock); 2607 2608 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2609 ooa_hdr->fill_len = ooa_hdr->fill_num * 2610 sizeof(struct ctl_ooa_entry); 2611 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2612 if (retval != 0) { 2613 printf("%s: error copying out %d bytes for OOA dump\n", 2614 __func__, ooa_hdr->fill_len); 2615 } 2616 2617 getbintime(&ooa_hdr->cur_bt); 2618 2619 if (cur_fill_num > ooa_hdr->alloc_num) { 2620 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2621 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2622 } else { 2623 ooa_hdr->dropped_num = 0; 2624 ooa_hdr->status = CTL_OOA_OK; 2625 } 2626 2627 free(entries, M_CTL); 2628 break; 2629 } 2630 case CTL_CHECK_OOA: { 2631 union ctl_io *io; 2632 struct ctl_lun *lun; 2633 struct ctl_ooa_info *ooa_info; 2634 2635 2636 ooa_info = (struct ctl_ooa_info *)addr; 2637 2638 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2639 ooa_info->status = CTL_OOA_INVALID_LUN; 2640 break; 2641 } 2642 mtx_lock(&softc->ctl_lock); 2643 lun = softc->ctl_luns[ooa_info->lun_id]; 2644 if (lun == NULL) { 2645 mtx_unlock(&softc->ctl_lock); 2646 ooa_info->status = CTL_OOA_INVALID_LUN; 2647 break; 2648 } 2649 mtx_lock(&lun->lun_lock); 2650 mtx_unlock(&softc->ctl_lock); 2651 ooa_info->num_entries = 0; 2652 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2653 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2654 &io->io_hdr, ooa_links)) { 2655 ooa_info->num_entries++; 2656 } 2657 mtx_unlock(&lun->lun_lock); 2658 2659 ooa_info->status = CTL_OOA_SUCCESS; 2660 2661 break; 2662 } 2663 case CTL_HARD_START: 2664 case CTL_HARD_STOP: { 2665 struct ctl_fe_ioctl_startstop_info ss_info; 2666 struct cfi_metatask *metatask; 2667 struct mtx hs_mtx; 2668 2669 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2670 2671 cv_init(&ss_info.sem, "hard start/stop cv" ); 2672 2673 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2674 if (metatask == NULL) { 2675 retval = ENOMEM; 2676 mtx_destroy(&hs_mtx); 2677 break; 2678 } 2679 2680 if (cmd == CTL_HARD_START) 2681 metatask->tasktype = CFI_TASK_STARTUP; 2682 else 2683 metatask->tasktype = CFI_TASK_SHUTDOWN; 2684 2685 metatask->callback = ctl_ioctl_hard_startstop_callback; 2686 metatask->callback_arg = &ss_info; 2687 2688 cfi_action(metatask); 2689 2690 /* Wait for the callback */ 2691 mtx_lock(&hs_mtx); 2692 cv_wait_sig(&ss_info.sem, &hs_mtx); 2693 mtx_unlock(&hs_mtx); 2694 2695 /* 2696 * All information has been copied from the metatask by the 2697 * time cv_broadcast() is called, so we free the metatask here. 2698 */ 2699 cfi_free_metatask(metatask); 2700 2701 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2702 2703 mtx_destroy(&hs_mtx); 2704 break; 2705 } 2706 case CTL_BBRREAD: { 2707 struct ctl_bbrread_info *bbr_info; 2708 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2709 struct mtx bbr_mtx; 2710 struct cfi_metatask *metatask; 2711 2712 bbr_info = (struct ctl_bbrread_info *)addr; 2713 2714 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2715 2716 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2717 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2718 2719 fe_bbr_info.bbr_info = bbr_info; 2720 fe_bbr_info.lock = &bbr_mtx; 2721 2722 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2723 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2724 2725 if (metatask == NULL) { 2726 mtx_destroy(&bbr_mtx); 2727 cv_destroy(&fe_bbr_info.sem); 2728 retval = ENOMEM; 2729 break; 2730 } 2731 metatask->tasktype = CFI_TASK_BBRREAD; 2732 metatask->callback = ctl_ioctl_bbrread_callback; 2733 metatask->callback_arg = &fe_bbr_info; 2734 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2735 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2736 metatask->taskinfo.bbrread.len = bbr_info->len; 2737 2738 cfi_action(metatask); 2739 2740 mtx_lock(&bbr_mtx); 2741 while (fe_bbr_info.wakeup_done == 0) 2742 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2743 mtx_unlock(&bbr_mtx); 2744 2745 bbr_info->status = metatask->status; 2746 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2747 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2748 memcpy(&bbr_info->sense_data, 2749 &metatask->taskinfo.bbrread.sense_data, 2750 ctl_min(sizeof(bbr_info->sense_data), 2751 sizeof(metatask->taskinfo.bbrread.sense_data))); 2752 2753 cfi_free_metatask(metatask); 2754 2755 mtx_destroy(&bbr_mtx); 2756 cv_destroy(&fe_bbr_info.sem); 2757 2758 break; 2759 } 2760 case CTL_DELAY_IO: { 2761 struct ctl_io_delay_info *delay_info; 2762 #ifdef CTL_IO_DELAY 2763 struct ctl_lun *lun; 2764 #endif /* CTL_IO_DELAY */ 2765 2766 delay_info = (struct ctl_io_delay_info *)addr; 2767 2768 #ifdef CTL_IO_DELAY 2769 mtx_lock(&softc->ctl_lock); 2770 2771 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2772 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2773 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2774 } else { 2775 lun = softc->ctl_luns[delay_info->lun_id]; 2776 mtx_lock(&lun->lun_lock); 2777 2778 delay_info->status = CTL_DELAY_STATUS_OK; 2779 2780 switch (delay_info->delay_type) { 2781 case CTL_DELAY_TYPE_CONT: 2782 break; 2783 case CTL_DELAY_TYPE_ONESHOT: 2784 break; 2785 default: 2786 delay_info->status = 2787 CTL_DELAY_STATUS_INVALID_TYPE; 2788 break; 2789 } 2790 2791 switch (delay_info->delay_loc) { 2792 case CTL_DELAY_LOC_DATAMOVE: 2793 lun->delay_info.datamove_type = 2794 delay_info->delay_type; 2795 lun->delay_info.datamove_delay = 2796 delay_info->delay_secs; 2797 break; 2798 case CTL_DELAY_LOC_DONE: 2799 lun->delay_info.done_type = 2800 delay_info->delay_type; 2801 lun->delay_info.done_delay = 2802 delay_info->delay_secs; 2803 break; 2804 default: 2805 delay_info->status = 2806 CTL_DELAY_STATUS_INVALID_LOC; 2807 break; 2808 } 2809 mtx_unlock(&lun->lun_lock); 2810 } 2811 2812 mtx_unlock(&softc->ctl_lock); 2813 #else 2814 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2815 #endif /* CTL_IO_DELAY */ 2816 break; 2817 } 2818 case CTL_REALSYNC_SET: { 2819 int *syncstate; 2820 2821 syncstate = (int *)addr; 2822 2823 mtx_lock(&softc->ctl_lock); 2824 switch (*syncstate) { 2825 case 0: 2826 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2827 break; 2828 case 1: 2829 softc->flags |= CTL_FLAG_REAL_SYNC; 2830 break; 2831 default: 2832 retval = EINVAL; 2833 break; 2834 } 2835 mtx_unlock(&softc->ctl_lock); 2836 break; 2837 } 2838 case CTL_REALSYNC_GET: { 2839 int *syncstate; 2840 2841 syncstate = (int*)addr; 2842 2843 mtx_lock(&softc->ctl_lock); 2844 if (softc->flags & CTL_FLAG_REAL_SYNC) 2845 *syncstate = 1; 2846 else 2847 *syncstate = 0; 2848 mtx_unlock(&softc->ctl_lock); 2849 2850 break; 2851 } 2852 case CTL_SETSYNC: 2853 case CTL_GETSYNC: { 2854 struct ctl_sync_info *sync_info; 2855 struct ctl_lun *lun; 2856 2857 sync_info = (struct ctl_sync_info *)addr; 2858 2859 mtx_lock(&softc->ctl_lock); 2860 lun = softc->ctl_luns[sync_info->lun_id]; 2861 if (lun == NULL) { 2862 mtx_unlock(&softc->ctl_lock); 2863 sync_info->status = CTL_GS_SYNC_NO_LUN; 2864 } 2865 /* 2866 * Get or set the sync interval. We're not bounds checking 2867 * in the set case, hopefully the user won't do something 2868 * silly. 2869 */ 2870 mtx_lock(&lun->lun_lock); 2871 mtx_unlock(&softc->ctl_lock); 2872 if (cmd == CTL_GETSYNC) 2873 sync_info->sync_interval = lun->sync_interval; 2874 else 2875 lun->sync_interval = sync_info->sync_interval; 2876 mtx_unlock(&lun->lun_lock); 2877 2878 sync_info->status = CTL_GS_SYNC_OK; 2879 2880 break; 2881 } 2882 case CTL_GETSTATS: { 2883 struct ctl_stats *stats; 2884 struct ctl_lun *lun; 2885 int i; 2886 2887 stats = (struct ctl_stats *)addr; 2888 2889 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2890 stats->alloc_len) { 2891 stats->status = CTL_SS_NEED_MORE_SPACE; 2892 stats->num_luns = softc->num_luns; 2893 break; 2894 } 2895 /* 2896 * XXX KDM no locking here. If the LUN list changes, 2897 * things can blow up. 2898 */ 2899 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2900 i++, lun = STAILQ_NEXT(lun, links)) { 2901 retval = copyout(&lun->stats, &stats->lun_stats[i], 2902 sizeof(lun->stats)); 2903 if (retval != 0) 2904 break; 2905 } 2906 stats->num_luns = softc->num_luns; 2907 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2908 softc->num_luns; 2909 stats->status = CTL_SS_OK; 2910 #ifdef CTL_TIME_IO 2911 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2912 #else 2913 stats->flags = CTL_STATS_FLAG_NONE; 2914 #endif 2915 getnanouptime(&stats->timestamp); 2916 break; 2917 } 2918 case CTL_ERROR_INJECT: { 2919 struct ctl_error_desc *err_desc, *new_err_desc; 2920 struct ctl_lun *lun; 2921 2922 err_desc = (struct ctl_error_desc *)addr; 2923 2924 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2925 M_WAITOK | M_ZERO); 2926 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2927 2928 mtx_lock(&softc->ctl_lock); 2929 lun = softc->ctl_luns[err_desc->lun_id]; 2930 if (lun == NULL) { 2931 mtx_unlock(&softc->ctl_lock); 2932 free(new_err_desc, M_CTL); 2933 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2934 __func__, (uintmax_t)err_desc->lun_id); 2935 retval = EINVAL; 2936 break; 2937 } 2938 mtx_lock(&lun->lun_lock); 2939 mtx_unlock(&softc->ctl_lock); 2940 2941 /* 2942 * We could do some checking here to verify the validity 2943 * of the request, but given the complexity of error 2944 * injection requests, the checking logic would be fairly 2945 * complex. 2946 * 2947 * For now, if the request is invalid, it just won't get 2948 * executed and might get deleted. 2949 */ 2950 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2951 2952 /* 2953 * XXX KDM check to make sure the serial number is unique, 2954 * in case we somehow manage to wrap. That shouldn't 2955 * happen for a very long time, but it's the right thing to 2956 * do. 2957 */ 2958 new_err_desc->serial = lun->error_serial; 2959 err_desc->serial = lun->error_serial; 2960 lun->error_serial++; 2961 2962 mtx_unlock(&lun->lun_lock); 2963 break; 2964 } 2965 case CTL_ERROR_INJECT_DELETE: { 2966 struct ctl_error_desc *delete_desc, *desc, *desc2; 2967 struct ctl_lun *lun; 2968 int delete_done; 2969 2970 delete_desc = (struct ctl_error_desc *)addr; 2971 delete_done = 0; 2972 2973 mtx_lock(&softc->ctl_lock); 2974 lun = softc->ctl_luns[delete_desc->lun_id]; 2975 if (lun == NULL) { 2976 mtx_unlock(&softc->ctl_lock); 2977 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2978 __func__, (uintmax_t)delete_desc->lun_id); 2979 retval = EINVAL; 2980 break; 2981 } 2982 mtx_lock(&lun->lun_lock); 2983 mtx_unlock(&softc->ctl_lock); 2984 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2985 if (desc->serial != delete_desc->serial) 2986 continue; 2987 2988 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2989 links); 2990 free(desc, M_CTL); 2991 delete_done = 1; 2992 } 2993 mtx_unlock(&lun->lun_lock); 2994 if (delete_done == 0) { 2995 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2996 "error serial %ju on LUN %u\n", __func__, 2997 delete_desc->serial, delete_desc->lun_id); 2998 retval = EINVAL; 2999 break; 3000 } 3001 break; 3002 } 3003 case CTL_DUMP_STRUCTS: { 3004 int i, j, k, idx; 3005 struct ctl_port *port; 3006 struct ctl_frontend *fe; 3007 3008 mtx_lock(&softc->ctl_lock); 3009 printf("CTL Persistent Reservation information start:\n"); 3010 for (i = 0; i < CTL_MAX_LUNS; i++) { 3011 struct ctl_lun *lun; 3012 3013 lun = softc->ctl_luns[i]; 3014 3015 if ((lun == NULL) 3016 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3017 continue; 3018 3019 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3020 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3021 idx = j * CTL_MAX_INIT_PER_PORT + k; 3022 if (lun->pr_keys[idx] == 0) 3023 continue; 3024 printf(" LUN %d port %d iid %d key " 3025 "%#jx\n", i, j, k, 3026 (uintmax_t)lun->pr_keys[idx]); 3027 } 3028 } 3029 } 3030 printf("CTL Persistent Reservation information end\n"); 3031 printf("CTL Ports:\n"); 3032 STAILQ_FOREACH(port, &softc->port_list, links) { 3033 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3034 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3035 port->frontend->name, port->port_type, 3036 port->physical_port, port->virtual_port, 3037 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3038 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3039 if (port->wwpn_iid[j].in_use == 0 && 3040 port->wwpn_iid[j].wwpn == 0 && 3041 port->wwpn_iid[j].name == NULL) 3042 continue; 3043 3044 printf(" iid %u use %d WWPN %#jx '%s'\n", 3045 j, port->wwpn_iid[j].in_use, 3046 (uintmax_t)port->wwpn_iid[j].wwpn, 3047 port->wwpn_iid[j].name); 3048 } 3049 } 3050 printf("CTL Port information end\n"); 3051 mtx_unlock(&softc->ctl_lock); 3052 /* 3053 * XXX KDM calling this without a lock. We'd likely want 3054 * to drop the lock before calling the frontend's dump 3055 * routine anyway. 3056 */ 3057 printf("CTL Frontends:\n"); 3058 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3059 printf(" Frontend '%s'\n", fe->name); 3060 if (fe->fe_dump != NULL) 3061 fe->fe_dump(); 3062 } 3063 printf("CTL Frontend information end\n"); 3064 break; 3065 } 3066 case CTL_LUN_REQ: { 3067 struct ctl_lun_req *lun_req; 3068 struct ctl_backend_driver *backend; 3069 3070 lun_req = (struct ctl_lun_req *)addr; 3071 3072 backend = ctl_backend_find(lun_req->backend); 3073 if (backend == NULL) { 3074 lun_req->status = CTL_LUN_ERROR; 3075 snprintf(lun_req->error_str, 3076 sizeof(lun_req->error_str), 3077 "Backend \"%s\" not found.", 3078 lun_req->backend); 3079 break; 3080 } 3081 if (lun_req->num_be_args > 0) { 3082 lun_req->kern_be_args = ctl_copyin_args( 3083 lun_req->num_be_args, 3084 lun_req->be_args, 3085 lun_req->error_str, 3086 sizeof(lun_req->error_str)); 3087 if (lun_req->kern_be_args == NULL) { 3088 lun_req->status = CTL_LUN_ERROR; 3089 break; 3090 } 3091 } 3092 3093 retval = backend->ioctl(dev, cmd, addr, flag, td); 3094 3095 if (lun_req->num_be_args > 0) { 3096 ctl_copyout_args(lun_req->num_be_args, 3097 lun_req->kern_be_args); 3098 ctl_free_args(lun_req->num_be_args, 3099 lun_req->kern_be_args); 3100 } 3101 break; 3102 } 3103 case CTL_LUN_LIST: { 3104 struct sbuf *sb; 3105 struct ctl_lun *lun; 3106 struct ctl_lun_list *list; 3107 struct ctl_option *opt; 3108 3109 list = (struct ctl_lun_list *)addr; 3110 3111 /* 3112 * Allocate a fixed length sbuf here, based on the length 3113 * of the user's buffer. We could allocate an auto-extending 3114 * buffer, and then tell the user how much larger our 3115 * amount of data is than his buffer, but that presents 3116 * some problems: 3117 * 3118 * 1. The sbuf(9) routines use a blocking malloc, and so 3119 * we can't hold a lock while calling them with an 3120 * auto-extending buffer. 3121 * 3122 * 2. There is not currently a LUN reference counting 3123 * mechanism, outside of outstanding transactions on 3124 * the LUN's OOA queue. So a LUN could go away on us 3125 * while we're getting the LUN number, backend-specific 3126 * information, etc. Thus, given the way things 3127 * currently work, we need to hold the CTL lock while 3128 * grabbing LUN information. 3129 * 3130 * So, from the user's standpoint, the best thing to do is 3131 * allocate what he thinks is a reasonable buffer length, 3132 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3133 * double the buffer length and try again. (And repeat 3134 * that until he succeeds.) 3135 */ 3136 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3137 if (sb == NULL) { 3138 list->status = CTL_LUN_LIST_ERROR; 3139 snprintf(list->error_str, sizeof(list->error_str), 3140 "Unable to allocate %d bytes for LUN list", 3141 list->alloc_len); 3142 break; 3143 } 3144 3145 sbuf_printf(sb, "<ctllunlist>\n"); 3146 3147 mtx_lock(&softc->ctl_lock); 3148 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3149 mtx_lock(&lun->lun_lock); 3150 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3151 (uintmax_t)lun->lun); 3152 3153 /* 3154 * Bail out as soon as we see that we've overfilled 3155 * the buffer. 3156 */ 3157 if (retval != 0) 3158 break; 3159 3160 retval = sbuf_printf(sb, "\t<backend_type>%s" 3161 "</backend_type>\n", 3162 (lun->backend == NULL) ? "none" : 3163 lun->backend->name); 3164 3165 if (retval != 0) 3166 break; 3167 3168 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3169 lun->be_lun->lun_type); 3170 3171 if (retval != 0) 3172 break; 3173 3174 if (lun->backend == NULL) { 3175 retval = sbuf_printf(sb, "</lun>\n"); 3176 if (retval != 0) 3177 break; 3178 continue; 3179 } 3180 3181 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3182 (lun->be_lun->maxlba > 0) ? 3183 lun->be_lun->maxlba + 1 : 0); 3184 3185 if (retval != 0) 3186 break; 3187 3188 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3189 lun->be_lun->blocksize); 3190 3191 if (retval != 0) 3192 break; 3193 3194 retval = sbuf_printf(sb, "\t<serial_number>"); 3195 3196 if (retval != 0) 3197 break; 3198 3199 retval = ctl_sbuf_printf_esc(sb, 3200 lun->be_lun->serial_num); 3201 3202 if (retval != 0) 3203 break; 3204 3205 retval = sbuf_printf(sb, "</serial_number>\n"); 3206 3207 if (retval != 0) 3208 break; 3209 3210 retval = sbuf_printf(sb, "\t<device_id>"); 3211 3212 if (retval != 0) 3213 break; 3214 3215 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3216 3217 if (retval != 0) 3218 break; 3219 3220 retval = sbuf_printf(sb, "</device_id>\n"); 3221 3222 if (retval != 0) 3223 break; 3224 3225 if (lun->backend->lun_info != NULL) { 3226 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3227 if (retval != 0) 3228 break; 3229 } 3230 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3231 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3232 opt->name, opt->value, opt->name); 3233 if (retval != 0) 3234 break; 3235 } 3236 3237 retval = sbuf_printf(sb, "</lun>\n"); 3238 3239 if (retval != 0) 3240 break; 3241 mtx_unlock(&lun->lun_lock); 3242 } 3243 if (lun != NULL) 3244 mtx_unlock(&lun->lun_lock); 3245 mtx_unlock(&softc->ctl_lock); 3246 3247 if ((retval != 0) 3248 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3249 retval = 0; 3250 sbuf_delete(sb); 3251 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3252 snprintf(list->error_str, sizeof(list->error_str), 3253 "Out of space, %d bytes is too small", 3254 list->alloc_len); 3255 break; 3256 } 3257 3258 sbuf_finish(sb); 3259 3260 retval = copyout(sbuf_data(sb), list->lun_xml, 3261 sbuf_len(sb) + 1); 3262 3263 list->fill_len = sbuf_len(sb) + 1; 3264 list->status = CTL_LUN_LIST_OK; 3265 sbuf_delete(sb); 3266 break; 3267 } 3268 case CTL_ISCSI: { 3269 struct ctl_iscsi *ci; 3270 struct ctl_frontend *fe; 3271 3272 ci = (struct ctl_iscsi *)addr; 3273 3274 fe = ctl_frontend_find("iscsi"); 3275 if (fe == NULL) { 3276 ci->status = CTL_ISCSI_ERROR; 3277 snprintf(ci->error_str, sizeof(ci->error_str), 3278 "Frontend \"iscsi\" not found."); 3279 break; 3280 } 3281 3282 retval = fe->ioctl(dev, cmd, addr, flag, td); 3283 break; 3284 } 3285 case CTL_PORT_REQ: { 3286 struct ctl_req *req; 3287 struct ctl_frontend *fe; 3288 3289 req = (struct ctl_req *)addr; 3290 3291 fe = ctl_frontend_find(req->driver); 3292 if (fe == NULL) { 3293 req->status = CTL_LUN_ERROR; 3294 snprintf(req->error_str, sizeof(req->error_str), 3295 "Frontend \"%s\" not found.", req->driver); 3296 break; 3297 } 3298 if (req->num_args > 0) { 3299 req->kern_args = ctl_copyin_args(req->num_args, 3300 req->args, req->error_str, sizeof(req->error_str)); 3301 if (req->kern_args == NULL) { 3302 req->status = CTL_LUN_ERROR; 3303 break; 3304 } 3305 } 3306 3307 retval = fe->ioctl(dev, cmd, addr, flag, td); 3308 3309 if (req->num_args > 0) { 3310 ctl_copyout_args(req->num_args, req->kern_args); 3311 ctl_free_args(req->num_args, req->kern_args); 3312 } 3313 break; 3314 } 3315 case CTL_PORT_LIST: { 3316 struct sbuf *sb; 3317 struct ctl_port *port; 3318 struct ctl_lun_list *list; 3319 struct ctl_option *opt; 3320 int j; 3321 3322 list = (struct ctl_lun_list *)addr; 3323 3324 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3325 if (sb == NULL) { 3326 list->status = CTL_LUN_LIST_ERROR; 3327 snprintf(list->error_str, sizeof(list->error_str), 3328 "Unable to allocate %d bytes for LUN list", 3329 list->alloc_len); 3330 break; 3331 } 3332 3333 sbuf_printf(sb, "<ctlportlist>\n"); 3334 3335 mtx_lock(&softc->ctl_lock); 3336 STAILQ_FOREACH(port, &softc->port_list, links) { 3337 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3338 (uintmax_t)port->targ_port); 3339 3340 /* 3341 * Bail out as soon as we see that we've overfilled 3342 * the buffer. 3343 */ 3344 if (retval != 0) 3345 break; 3346 3347 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3348 "</frontend_type>\n", port->frontend->name); 3349 if (retval != 0) 3350 break; 3351 3352 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3353 port->port_type); 3354 if (retval != 0) 3355 break; 3356 3357 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3358 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3359 if (retval != 0) 3360 break; 3361 3362 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3363 port->port_name); 3364 if (retval != 0) 3365 break; 3366 3367 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3368 port->physical_port); 3369 if (retval != 0) 3370 break; 3371 3372 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3373 port->virtual_port); 3374 if (retval != 0) 3375 break; 3376 3377 if (port->target_devid != NULL) { 3378 sbuf_printf(sb, "\t<target>"); 3379 ctl_id_sbuf(port->target_devid, sb); 3380 sbuf_printf(sb, "</target>\n"); 3381 } 3382 3383 if (port->port_devid != NULL) { 3384 sbuf_printf(sb, "\t<port>"); 3385 ctl_id_sbuf(port->port_devid, sb); 3386 sbuf_printf(sb, "</port>\n"); 3387 } 3388 3389 if (port->port_info != NULL) { 3390 retval = port->port_info(port->onoff_arg, sb); 3391 if (retval != 0) 3392 break; 3393 } 3394 STAILQ_FOREACH(opt, &port->options, links) { 3395 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3396 opt->name, opt->value, opt->name); 3397 if (retval != 0) 3398 break; 3399 } 3400 3401 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3402 if (port->wwpn_iid[j].in_use == 0 || 3403 (port->wwpn_iid[j].wwpn == 0 && 3404 port->wwpn_iid[j].name == NULL)) 3405 continue; 3406 3407 if (port->wwpn_iid[j].name != NULL) 3408 retval = sbuf_printf(sb, 3409 "\t<initiator>%u %s</initiator>\n", 3410 j, port->wwpn_iid[j].name); 3411 else 3412 retval = sbuf_printf(sb, 3413 "\t<initiator>%u naa.%08jx</initiator>\n", 3414 j, port->wwpn_iid[j].wwpn); 3415 if (retval != 0) 3416 break; 3417 } 3418 if (retval != 0) 3419 break; 3420 3421 retval = sbuf_printf(sb, "</targ_port>\n"); 3422 if (retval != 0) 3423 break; 3424 } 3425 mtx_unlock(&softc->ctl_lock); 3426 3427 if ((retval != 0) 3428 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3429 retval = 0; 3430 sbuf_delete(sb); 3431 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3432 snprintf(list->error_str, sizeof(list->error_str), 3433 "Out of space, %d bytes is too small", 3434 list->alloc_len); 3435 break; 3436 } 3437 3438 sbuf_finish(sb); 3439 3440 retval = copyout(sbuf_data(sb), list->lun_xml, 3441 sbuf_len(sb) + 1); 3442 3443 list->fill_len = sbuf_len(sb) + 1; 3444 list->status = CTL_LUN_LIST_OK; 3445 sbuf_delete(sb); 3446 break; 3447 } 3448 default: { 3449 /* XXX KDM should we fix this? */ 3450 #if 0 3451 struct ctl_backend_driver *backend; 3452 unsigned int type; 3453 int found; 3454 3455 found = 0; 3456 3457 /* 3458 * We encode the backend type as the ioctl type for backend 3459 * ioctls. So parse it out here, and then search for a 3460 * backend of this type. 3461 */ 3462 type = _IOC_TYPE(cmd); 3463 3464 STAILQ_FOREACH(backend, &softc->be_list, links) { 3465 if (backend->type == type) { 3466 found = 1; 3467 break; 3468 } 3469 } 3470 if (found == 0) { 3471 printf("ctl: unknown ioctl command %#lx or backend " 3472 "%d\n", cmd, type); 3473 retval = EINVAL; 3474 break; 3475 } 3476 retval = backend->ioctl(dev, cmd, addr, flag, td); 3477 #endif 3478 retval = ENOTTY; 3479 break; 3480 } 3481 } 3482 return (retval); 3483 } 3484 3485 uint32_t 3486 ctl_get_initindex(struct ctl_nexus *nexus) 3487 { 3488 if (nexus->targ_port < CTL_MAX_PORTS) 3489 return (nexus->initid.id + 3490 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3491 else 3492 return (nexus->initid.id + 3493 ((nexus->targ_port - CTL_MAX_PORTS) * 3494 CTL_MAX_INIT_PER_PORT)); 3495 } 3496 3497 uint32_t 3498 ctl_get_resindex(struct ctl_nexus *nexus) 3499 { 3500 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3501 } 3502 3503 uint32_t 3504 ctl_port_idx(int port_num) 3505 { 3506 if (port_num < CTL_MAX_PORTS) 3507 return(port_num); 3508 else 3509 return(port_num - CTL_MAX_PORTS); 3510 } 3511 3512 static uint32_t 3513 ctl_map_lun(int port_num, uint32_t lun_id) 3514 { 3515 struct ctl_port *port; 3516 3517 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3518 if (port == NULL) 3519 return (UINT32_MAX); 3520 if (port->lun_map == NULL) 3521 return (lun_id); 3522 return (port->lun_map(port->targ_lun_arg, lun_id)); 3523 } 3524 3525 static uint32_t 3526 ctl_map_lun_back(int port_num, uint32_t lun_id) 3527 { 3528 struct ctl_port *port; 3529 uint32_t i; 3530 3531 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3532 if (port->lun_map == NULL) 3533 return (lun_id); 3534 for (i = 0; i < CTL_MAX_LUNS; i++) { 3535 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3536 return (i); 3537 } 3538 return (UINT32_MAX); 3539 } 3540 3541 /* 3542 * Note: This only works for bitmask sizes that are at least 32 bits, and 3543 * that are a power of 2. 3544 */ 3545 int 3546 ctl_ffz(uint32_t *mask, uint32_t size) 3547 { 3548 uint32_t num_chunks, num_pieces; 3549 int i, j; 3550 3551 num_chunks = (size >> 5); 3552 if (num_chunks == 0) 3553 num_chunks++; 3554 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3555 3556 for (i = 0; i < num_chunks; i++) { 3557 for (j = 0; j < num_pieces; j++) { 3558 if ((mask[i] & (1 << j)) == 0) 3559 return ((i << 5) + j); 3560 } 3561 } 3562 3563 return (-1); 3564 } 3565 3566 int 3567 ctl_set_mask(uint32_t *mask, uint32_t bit) 3568 { 3569 uint32_t chunk, piece; 3570 3571 chunk = bit >> 5; 3572 piece = bit % (sizeof(uint32_t) * 8); 3573 3574 if ((mask[chunk] & (1 << piece)) != 0) 3575 return (-1); 3576 else 3577 mask[chunk] |= (1 << piece); 3578 3579 return (0); 3580 } 3581 3582 int 3583 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3584 { 3585 uint32_t chunk, piece; 3586 3587 chunk = bit >> 5; 3588 piece = bit % (sizeof(uint32_t) * 8); 3589 3590 if ((mask[chunk] & (1 << piece)) == 0) 3591 return (-1); 3592 else 3593 mask[chunk] &= ~(1 << piece); 3594 3595 return (0); 3596 } 3597 3598 int 3599 ctl_is_set(uint32_t *mask, uint32_t bit) 3600 { 3601 uint32_t chunk, piece; 3602 3603 chunk = bit >> 5; 3604 piece = bit % (sizeof(uint32_t) * 8); 3605 3606 if ((mask[chunk] & (1 << piece)) == 0) 3607 return (0); 3608 else 3609 return (1); 3610 } 3611 3612 #ifdef unused 3613 /* 3614 * The bus, target and lun are optional, they can be filled in later. 3615 * can_wait is used to determine whether we can wait on the malloc or not. 3616 */ 3617 union ctl_io* 3618 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3619 uint32_t targ_lun, int can_wait) 3620 { 3621 union ctl_io *io; 3622 3623 if (can_wait) 3624 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3625 else 3626 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3627 3628 if (io != NULL) { 3629 io->io_hdr.io_type = io_type; 3630 io->io_hdr.targ_port = targ_port; 3631 /* 3632 * XXX KDM this needs to change/go away. We need to move 3633 * to a preallocated pool of ctl_scsiio structures. 3634 */ 3635 io->io_hdr.nexus.targ_target.id = targ_target; 3636 io->io_hdr.nexus.targ_lun = targ_lun; 3637 } 3638 3639 return (io); 3640 } 3641 3642 void 3643 ctl_kfree_io(union ctl_io *io) 3644 { 3645 free(io, M_CTL); 3646 } 3647 #endif /* unused */ 3648 3649 /* 3650 * ctl_softc, pool_type, total_ctl_io are passed in. 3651 * npool is passed out. 3652 */ 3653 int 3654 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3655 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3656 { 3657 uint32_t i; 3658 union ctl_io *cur_io, *next_io; 3659 struct ctl_io_pool *pool; 3660 int retval; 3661 3662 retval = 0; 3663 3664 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3665 M_NOWAIT | M_ZERO); 3666 if (pool == NULL) { 3667 retval = ENOMEM; 3668 goto bailout; 3669 } 3670 3671 pool->type = pool_type; 3672 pool->ctl_softc = ctl_softc; 3673 3674 mtx_lock(&ctl_softc->pool_lock); 3675 pool->id = ctl_softc->cur_pool_id++; 3676 mtx_unlock(&ctl_softc->pool_lock); 3677 3678 pool->flags = CTL_POOL_FLAG_NONE; 3679 pool->refcount = 1; /* Reference for validity. */ 3680 STAILQ_INIT(&pool->free_queue); 3681 3682 /* 3683 * XXX KDM other options here: 3684 * - allocate a page at a time 3685 * - allocate one big chunk of memory. 3686 * Page allocation might work well, but would take a little more 3687 * tracking. 3688 */ 3689 for (i = 0; i < total_ctl_io; i++) { 3690 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3691 M_NOWAIT); 3692 if (cur_io == NULL) { 3693 retval = ENOMEM; 3694 break; 3695 } 3696 cur_io->io_hdr.pool = pool; 3697 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3698 pool->total_ctl_io++; 3699 pool->free_ctl_io++; 3700 } 3701 3702 if (retval != 0) { 3703 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3704 cur_io != NULL; cur_io = next_io) { 3705 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3706 links); 3707 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3708 ctl_io_hdr, links); 3709 free(cur_io, M_CTLIO); 3710 } 3711 3712 free(pool, M_CTL); 3713 goto bailout; 3714 } 3715 mtx_lock(&ctl_softc->pool_lock); 3716 ctl_softc->num_pools++; 3717 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3718 /* 3719 * Increment our usage count if this is an external consumer, so we 3720 * can't get unloaded until the external consumer (most likely a 3721 * FETD) unloads and frees his pool. 3722 * 3723 * XXX KDM will this increment the caller's module use count, or 3724 * mine? 3725 */ 3726 #if 0 3727 if ((pool_type != CTL_POOL_EMERGENCY) 3728 && (pool_type != CTL_POOL_INTERNAL) 3729 && (pool_type != CTL_POOL_4OTHERSC)) 3730 MOD_INC_USE_COUNT; 3731 #endif 3732 3733 mtx_unlock(&ctl_softc->pool_lock); 3734 3735 *npool = pool; 3736 3737 bailout: 3738 3739 return (retval); 3740 } 3741 3742 static int 3743 ctl_pool_acquire(struct ctl_io_pool *pool) 3744 { 3745 3746 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3747 3748 if (pool->flags & CTL_POOL_FLAG_INVALID) 3749 return (EINVAL); 3750 3751 pool->refcount++; 3752 3753 return (0); 3754 } 3755 3756 static void 3757 ctl_pool_release(struct ctl_io_pool *pool) 3758 { 3759 struct ctl_softc *ctl_softc = pool->ctl_softc; 3760 union ctl_io *io; 3761 3762 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3763 3764 if (--pool->refcount != 0) 3765 return; 3766 3767 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3768 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3769 links); 3770 free(io, M_CTLIO); 3771 } 3772 3773 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3774 ctl_softc->num_pools--; 3775 3776 /* 3777 * XXX KDM will this decrement the caller's usage count or mine? 3778 */ 3779 #if 0 3780 if ((pool->type != CTL_POOL_EMERGENCY) 3781 && (pool->type != CTL_POOL_INTERNAL) 3782 && (pool->type != CTL_POOL_4OTHERSC)) 3783 MOD_DEC_USE_COUNT; 3784 #endif 3785 3786 free(pool, M_CTL); 3787 } 3788 3789 void 3790 ctl_pool_free(struct ctl_io_pool *pool) 3791 { 3792 struct ctl_softc *ctl_softc; 3793 3794 if (pool == NULL) 3795 return; 3796 3797 ctl_softc = pool->ctl_softc; 3798 mtx_lock(&ctl_softc->pool_lock); 3799 pool->flags |= CTL_POOL_FLAG_INVALID; 3800 ctl_pool_release(pool); 3801 mtx_unlock(&ctl_softc->pool_lock); 3802 } 3803 3804 /* 3805 * This routine does not block (except for spinlocks of course). 3806 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3807 * possible. 3808 */ 3809 union ctl_io * 3810 ctl_alloc_io(void *pool_ref) 3811 { 3812 union ctl_io *io; 3813 struct ctl_softc *ctl_softc; 3814 struct ctl_io_pool *pool, *npool; 3815 struct ctl_io_pool *emergency_pool; 3816 3817 pool = (struct ctl_io_pool *)pool_ref; 3818 3819 if (pool == NULL) { 3820 printf("%s: pool is NULL\n", __func__); 3821 return (NULL); 3822 } 3823 3824 emergency_pool = NULL; 3825 3826 ctl_softc = pool->ctl_softc; 3827 3828 mtx_lock(&ctl_softc->pool_lock); 3829 /* 3830 * First, try to get the io structure from the user's pool. 3831 */ 3832 if (ctl_pool_acquire(pool) == 0) { 3833 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3834 if (io != NULL) { 3835 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3836 pool->total_allocated++; 3837 pool->free_ctl_io--; 3838 mtx_unlock(&ctl_softc->pool_lock); 3839 return (io); 3840 } else 3841 ctl_pool_release(pool); 3842 } 3843 /* 3844 * If he doesn't have any io structures left, search for an 3845 * emergency pool and grab one from there. 3846 */ 3847 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3848 if (npool->type != CTL_POOL_EMERGENCY) 3849 continue; 3850 3851 if (ctl_pool_acquire(npool) != 0) 3852 continue; 3853 3854 emergency_pool = npool; 3855 3856 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3857 if (io != NULL) { 3858 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3859 npool->total_allocated++; 3860 npool->free_ctl_io--; 3861 mtx_unlock(&ctl_softc->pool_lock); 3862 return (io); 3863 } else 3864 ctl_pool_release(npool); 3865 } 3866 3867 /* Drop the spinlock before we malloc */ 3868 mtx_unlock(&ctl_softc->pool_lock); 3869 3870 /* 3871 * The emergency pool (if it exists) didn't have one, so try an 3872 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3873 */ 3874 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3875 if (io != NULL) { 3876 /* 3877 * If the emergency pool exists but is empty, add this 3878 * ctl_io to its list when it gets freed. 3879 */ 3880 if (emergency_pool != NULL) { 3881 mtx_lock(&ctl_softc->pool_lock); 3882 if (ctl_pool_acquire(emergency_pool) == 0) { 3883 io->io_hdr.pool = emergency_pool; 3884 emergency_pool->total_ctl_io++; 3885 /* 3886 * Need to bump this, otherwise 3887 * total_allocated and total_freed won't 3888 * match when we no longer have anything 3889 * outstanding. 3890 */ 3891 emergency_pool->total_allocated++; 3892 } 3893 mtx_unlock(&ctl_softc->pool_lock); 3894 } else 3895 io->io_hdr.pool = NULL; 3896 } 3897 3898 return (io); 3899 } 3900 3901 void 3902 ctl_free_io(union ctl_io *io) 3903 { 3904 if (io == NULL) 3905 return; 3906 3907 /* 3908 * If this ctl_io has a pool, return it to that pool. 3909 */ 3910 if (io->io_hdr.pool != NULL) { 3911 struct ctl_io_pool *pool; 3912 3913 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3914 mtx_lock(&pool->ctl_softc->pool_lock); 3915 io->io_hdr.io_type = 0xff; 3916 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3917 pool->total_freed++; 3918 pool->free_ctl_io++; 3919 ctl_pool_release(pool); 3920 mtx_unlock(&pool->ctl_softc->pool_lock); 3921 } else { 3922 /* 3923 * Otherwise, just free it. We probably malloced it and 3924 * the emergency pool wasn't available. 3925 */ 3926 free(io, M_CTLIO); 3927 } 3928 3929 } 3930 3931 void 3932 ctl_zero_io(union ctl_io *io) 3933 { 3934 void *pool_ref; 3935 3936 if (io == NULL) 3937 return; 3938 3939 /* 3940 * May need to preserve linked list pointers at some point too. 3941 */ 3942 pool_ref = io->io_hdr.pool; 3943 3944 memset(io, 0, sizeof(*io)); 3945 3946 io->io_hdr.pool = pool_ref; 3947 } 3948 3949 /* 3950 * This routine is currently used for internal copies of ctl_ios that need 3951 * to persist for some reason after we've already returned status to the 3952 * FETD. (Thus the flag set.) 3953 * 3954 * XXX XXX 3955 * Note that this makes a blind copy of all fields in the ctl_io, except 3956 * for the pool reference. This includes any memory that has been 3957 * allocated! That memory will no longer be valid after done has been 3958 * called, so this would be VERY DANGEROUS for command that actually does 3959 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3960 * start and stop commands, which don't transfer any data, so this is not a 3961 * problem. If it is used for anything else, the caller would also need to 3962 * allocate data buffer space and this routine would need to be modified to 3963 * copy the data buffer(s) as well. 3964 */ 3965 void 3966 ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3967 { 3968 void *pool_ref; 3969 3970 if ((src == NULL) 3971 || (dest == NULL)) 3972 return; 3973 3974 /* 3975 * May need to preserve linked list pointers at some point too. 3976 */ 3977 pool_ref = dest->io_hdr.pool; 3978 3979 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3980 3981 dest->io_hdr.pool = pool_ref; 3982 /* 3983 * We need to know that this is an internal copy, and doesn't need 3984 * to get passed back to the FETD that allocated it. 3985 */ 3986 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3987 } 3988 3989 /* 3990 * This routine could be used in the future to load default and/or saved 3991 * mode page parameters for a particuar lun. 3992 */ 3993 static int 3994 ctl_init_page_index(struct ctl_lun *lun) 3995 { 3996 int i; 3997 struct ctl_page_index *page_index; 3998 const char *value; 3999 4000 memcpy(&lun->mode_pages.index, page_index_template, 4001 sizeof(page_index_template)); 4002 4003 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4004 4005 page_index = &lun->mode_pages.index[i]; 4006 /* 4007 * If this is a disk-only mode page, there's no point in 4008 * setting it up. For some pages, we have to have some 4009 * basic information about the disk in order to calculate the 4010 * mode page data. 4011 */ 4012 if ((lun->be_lun->lun_type != T_DIRECT) 4013 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4014 continue; 4015 4016 switch (page_index->page_code & SMPH_PC_MASK) { 4017 case SMS_RW_ERROR_RECOVERY_PAGE: { 4018 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4019 panic("subpage is incorrect!"); 4020 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4021 &rw_er_page_default, 4022 sizeof(rw_er_page_default)); 4023 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4024 &rw_er_page_changeable, 4025 sizeof(rw_er_page_changeable)); 4026 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4027 &rw_er_page_default, 4028 sizeof(rw_er_page_default)); 4029 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4030 &rw_er_page_default, 4031 sizeof(rw_er_page_default)); 4032 page_index->page_data = 4033 (uint8_t *)lun->mode_pages.rw_er_page; 4034 break; 4035 } 4036 case SMS_FORMAT_DEVICE_PAGE: { 4037 struct scsi_format_page *format_page; 4038 4039 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4040 panic("subpage is incorrect!"); 4041 4042 /* 4043 * Sectors per track are set above. Bytes per 4044 * sector need to be set here on a per-LUN basis. 4045 */ 4046 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4047 &format_page_default, 4048 sizeof(format_page_default)); 4049 memcpy(&lun->mode_pages.format_page[ 4050 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4051 sizeof(format_page_changeable)); 4052 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4053 &format_page_default, 4054 sizeof(format_page_default)); 4055 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4056 &format_page_default, 4057 sizeof(format_page_default)); 4058 4059 format_page = &lun->mode_pages.format_page[ 4060 CTL_PAGE_CURRENT]; 4061 scsi_ulto2b(lun->be_lun->blocksize, 4062 format_page->bytes_per_sector); 4063 4064 format_page = &lun->mode_pages.format_page[ 4065 CTL_PAGE_DEFAULT]; 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_SAVED]; 4071 scsi_ulto2b(lun->be_lun->blocksize, 4072 format_page->bytes_per_sector); 4073 4074 page_index->page_data = 4075 (uint8_t *)lun->mode_pages.format_page; 4076 break; 4077 } 4078 case SMS_RIGID_DISK_PAGE: { 4079 struct scsi_rigid_disk_page *rigid_disk_page; 4080 uint32_t sectors_per_cylinder; 4081 uint64_t cylinders; 4082 #ifndef __XSCALE__ 4083 int shift; 4084 #endif /* !__XSCALE__ */ 4085 4086 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4087 panic("invalid subpage value %d", 4088 page_index->subpage); 4089 4090 /* 4091 * Rotation rate and sectors per track are set 4092 * above. We calculate the cylinders here based on 4093 * capacity. Due to the number of heads and 4094 * sectors per track we're using, smaller arrays 4095 * may turn out to have 0 cylinders. Linux and 4096 * FreeBSD don't pay attention to these mode pages 4097 * to figure out capacity, but Solaris does. It 4098 * seems to deal with 0 cylinders just fine, and 4099 * works out a fake geometry based on the capacity. 4100 */ 4101 memcpy(&lun->mode_pages.rigid_disk_page[ 4102 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4103 sizeof(rigid_disk_page_default)); 4104 memcpy(&lun->mode_pages.rigid_disk_page[ 4105 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4106 sizeof(rigid_disk_page_changeable)); 4107 memcpy(&lun->mode_pages.rigid_disk_page[ 4108 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4109 sizeof(rigid_disk_page_default)); 4110 memcpy(&lun->mode_pages.rigid_disk_page[ 4111 CTL_PAGE_SAVED], &rigid_disk_page_default, 4112 sizeof(rigid_disk_page_default)); 4113 4114 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4115 CTL_DEFAULT_HEADS; 4116 4117 /* 4118 * The divide method here will be more accurate, 4119 * probably, but results in floating point being 4120 * used in the kernel on i386 (__udivdi3()). On the 4121 * XScale, though, __udivdi3() is implemented in 4122 * software. 4123 * 4124 * The shift method for cylinder calculation is 4125 * accurate if sectors_per_cylinder is a power of 4126 * 2. Otherwise it might be slightly off -- you 4127 * might have a bit of a truncation problem. 4128 */ 4129 #ifdef __XSCALE__ 4130 cylinders = (lun->be_lun->maxlba + 1) / 4131 sectors_per_cylinder; 4132 #else 4133 for (shift = 31; shift > 0; shift--) { 4134 if (sectors_per_cylinder & (1 << shift)) 4135 break; 4136 } 4137 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4138 #endif 4139 4140 /* 4141 * We've basically got 3 bytes, or 24 bits for the 4142 * cylinder size in the mode page. If we're over, 4143 * just round down to 2^24. 4144 */ 4145 if (cylinders > 0xffffff) 4146 cylinders = 0xffffff; 4147 4148 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4149 CTL_PAGE_CURRENT]; 4150 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4151 4152 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4153 CTL_PAGE_DEFAULT]; 4154 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4155 4156 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4157 CTL_PAGE_SAVED]; 4158 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4159 4160 page_index->page_data = 4161 (uint8_t *)lun->mode_pages.rigid_disk_page; 4162 break; 4163 } 4164 case SMS_CACHING_PAGE: { 4165 struct scsi_caching_page *caching_page; 4166 4167 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4168 panic("invalid subpage value %d", 4169 page_index->subpage); 4170 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4171 &caching_page_default, 4172 sizeof(caching_page_default)); 4173 memcpy(&lun->mode_pages.caching_page[ 4174 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4175 sizeof(caching_page_changeable)); 4176 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4177 &caching_page_default, 4178 sizeof(caching_page_default)); 4179 caching_page = &lun->mode_pages.caching_page[ 4180 CTL_PAGE_SAVED]; 4181 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4182 if (value != NULL && strcmp(value, "off") == 0) 4183 caching_page->flags1 &= ~SCP_WCE; 4184 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4185 if (value != NULL && strcmp(value, "off") == 0) 4186 caching_page->flags1 |= SCP_RCD; 4187 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4188 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4189 sizeof(caching_page_default)); 4190 page_index->page_data = 4191 (uint8_t *)lun->mode_pages.caching_page; 4192 break; 4193 } 4194 case SMS_CONTROL_MODE_PAGE: { 4195 struct scsi_control_page *control_page; 4196 4197 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4198 panic("invalid subpage value %d", 4199 page_index->subpage); 4200 4201 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4202 &control_page_default, 4203 sizeof(control_page_default)); 4204 memcpy(&lun->mode_pages.control_page[ 4205 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4206 sizeof(control_page_changeable)); 4207 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4208 &control_page_default, 4209 sizeof(control_page_default)); 4210 control_page = &lun->mode_pages.control_page[ 4211 CTL_PAGE_SAVED]; 4212 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4213 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4214 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4215 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4216 } 4217 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4218 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4219 sizeof(control_page_default)); 4220 page_index->page_data = 4221 (uint8_t *)lun->mode_pages.control_page; 4222 break; 4223 4224 } 4225 case SMS_INFO_EXCEPTIONS_PAGE: { 4226 switch (page_index->subpage) { 4227 case SMS_SUBPAGE_PAGE_0: 4228 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4229 &ie_page_default, 4230 sizeof(ie_page_default)); 4231 memcpy(&lun->mode_pages.ie_page[ 4232 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4233 sizeof(ie_page_changeable)); 4234 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4235 &ie_page_default, 4236 sizeof(ie_page_default)); 4237 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4238 &ie_page_default, 4239 sizeof(ie_page_default)); 4240 page_index->page_data = 4241 (uint8_t *)lun->mode_pages.ie_page; 4242 break; 4243 case 0x02: 4244 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4245 &lbp_page_default, 4246 sizeof(lbp_page_default)); 4247 memcpy(&lun->mode_pages.lbp_page[ 4248 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4249 sizeof(lbp_page_changeable)); 4250 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4251 &lbp_page_default, 4252 sizeof(lbp_page_default)); 4253 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4254 &lbp_page_default, 4255 sizeof(lbp_page_default)); 4256 page_index->page_data = 4257 (uint8_t *)lun->mode_pages.lbp_page; 4258 } 4259 break; 4260 } 4261 case SMS_VENDOR_SPECIFIC_PAGE:{ 4262 switch (page_index->subpage) { 4263 case DBGCNF_SUBPAGE_CODE: { 4264 struct copan_debugconf_subpage *current_page, 4265 *saved_page; 4266 4267 memcpy(&lun->mode_pages.debugconf_subpage[ 4268 CTL_PAGE_CURRENT], 4269 &debugconf_page_default, 4270 sizeof(debugconf_page_default)); 4271 memcpy(&lun->mode_pages.debugconf_subpage[ 4272 CTL_PAGE_CHANGEABLE], 4273 &debugconf_page_changeable, 4274 sizeof(debugconf_page_changeable)); 4275 memcpy(&lun->mode_pages.debugconf_subpage[ 4276 CTL_PAGE_DEFAULT], 4277 &debugconf_page_default, 4278 sizeof(debugconf_page_default)); 4279 memcpy(&lun->mode_pages.debugconf_subpage[ 4280 CTL_PAGE_SAVED], 4281 &debugconf_page_default, 4282 sizeof(debugconf_page_default)); 4283 page_index->page_data = 4284 (uint8_t *)lun->mode_pages.debugconf_subpage; 4285 4286 current_page = (struct copan_debugconf_subpage *) 4287 (page_index->page_data + 4288 (page_index->page_len * 4289 CTL_PAGE_CURRENT)); 4290 saved_page = (struct copan_debugconf_subpage *) 4291 (page_index->page_data + 4292 (page_index->page_len * 4293 CTL_PAGE_SAVED)); 4294 break; 4295 } 4296 default: 4297 panic("invalid subpage value %d", 4298 page_index->subpage); 4299 break; 4300 } 4301 break; 4302 } 4303 default: 4304 panic("invalid page value %d", 4305 page_index->page_code & SMPH_PC_MASK); 4306 break; 4307 } 4308 } 4309 4310 return (CTL_RETVAL_COMPLETE); 4311 } 4312 4313 static int 4314 ctl_init_log_page_index(struct ctl_lun *lun) 4315 { 4316 struct ctl_page_index *page_index; 4317 int i, j, prev; 4318 4319 memcpy(&lun->log_pages.index, log_page_index_template, 4320 sizeof(log_page_index_template)); 4321 4322 prev = -1; 4323 for (i = 0, j = 0; i < CTL_NUM_LOG_PAGES; i++) { 4324 4325 page_index = &lun->log_pages.index[i]; 4326 /* 4327 * If this is a disk-only mode page, there's no point in 4328 * setting it up. For some pages, we have to have some 4329 * basic information about the disk in order to calculate the 4330 * mode page data. 4331 */ 4332 if ((lun->be_lun->lun_type != T_DIRECT) 4333 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4334 continue; 4335 4336 if (page_index->page_code != prev) { 4337 lun->log_pages.pages_page[j] = page_index->page_code; 4338 prev = page_index->page_code; 4339 j++; 4340 } 4341 lun->log_pages.subpages_page[i*2] = page_index->page_code; 4342 lun->log_pages.subpages_page[i*2+1] = page_index->subpage; 4343 } 4344 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4345 lun->log_pages.index[0].page_len = j; 4346 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4347 lun->log_pages.index[1].page_len = i * 2; 4348 4349 return (CTL_RETVAL_COMPLETE); 4350 } 4351 4352 /* 4353 * LUN allocation. 4354 * 4355 * Requirements: 4356 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4357 * wants us to allocate the LUN and he can block. 4358 * - ctl_softc is always set 4359 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4360 * 4361 * Returns 0 for success, non-zero (errno) for failure. 4362 */ 4363 static int 4364 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4365 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4366 { 4367 struct ctl_lun *nlun, *lun; 4368 struct ctl_port *port; 4369 struct scsi_vpd_id_descriptor *desc; 4370 struct scsi_vpd_id_t10 *t10id; 4371 const char *eui, *naa, *scsiname, *vendor, *value; 4372 int lun_number, i, lun_malloced; 4373 int devidlen, idlen1, idlen2 = 0, len; 4374 4375 if (be_lun == NULL) 4376 return (EINVAL); 4377 4378 /* 4379 * We currently only support Direct Access or Processor LUN types. 4380 */ 4381 switch (be_lun->lun_type) { 4382 case T_DIRECT: 4383 break; 4384 case T_PROCESSOR: 4385 break; 4386 case T_SEQUENTIAL: 4387 case T_CHANGER: 4388 default: 4389 be_lun->lun_config_status(be_lun->be_lun, 4390 CTL_LUN_CONFIG_FAILURE); 4391 break; 4392 } 4393 if (ctl_lun == NULL) { 4394 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4395 lun_malloced = 1; 4396 } else { 4397 lun_malloced = 0; 4398 lun = ctl_lun; 4399 } 4400 4401 memset(lun, 0, sizeof(*lun)); 4402 if (lun_malloced) 4403 lun->flags = CTL_LUN_MALLOCED; 4404 4405 /* Generate LUN ID. */ 4406 devidlen = max(CTL_DEVID_MIN_LEN, 4407 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4408 idlen1 = sizeof(*t10id) + devidlen; 4409 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4410 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4411 if (scsiname != NULL) { 4412 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4413 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4414 } 4415 eui = ctl_get_opt(&be_lun->options, "eui"); 4416 if (eui != NULL) { 4417 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4418 } 4419 naa = ctl_get_opt(&be_lun->options, "naa"); 4420 if (naa != NULL) { 4421 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4422 } 4423 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4424 M_CTL, M_WAITOK | M_ZERO); 4425 lun->lun_devid->len = len; 4426 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4427 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4428 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4429 desc->length = idlen1; 4430 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4431 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4432 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4433 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4434 } else { 4435 strncpy(t10id->vendor, vendor, 4436 min(sizeof(t10id->vendor), strlen(vendor))); 4437 } 4438 strncpy((char *)t10id->vendor_spec_id, 4439 (char *)be_lun->device_id, devidlen); 4440 if (scsiname != NULL) { 4441 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4442 desc->length); 4443 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4444 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4445 SVPD_ID_TYPE_SCSI_NAME; 4446 desc->length = idlen2; 4447 strlcpy(desc->identifier, scsiname, idlen2); 4448 } 4449 if (eui != NULL) { 4450 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4451 desc->length); 4452 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4453 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4454 SVPD_ID_TYPE_EUI64; 4455 desc->length = 8; 4456 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier); 4457 } 4458 if (naa != NULL) { 4459 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4460 desc->length); 4461 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4462 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4463 SVPD_ID_TYPE_NAA; 4464 desc->length = 8; 4465 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier); 4466 } 4467 4468 mtx_lock(&ctl_softc->ctl_lock); 4469 /* 4470 * See if the caller requested a particular LUN number. If so, see 4471 * if it is available. Otherwise, allocate the first available LUN. 4472 */ 4473 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4474 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4475 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4476 mtx_unlock(&ctl_softc->ctl_lock); 4477 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4478 printf("ctl: requested LUN ID %d is higher " 4479 "than CTL_MAX_LUNS - 1 (%d)\n", 4480 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4481 } else { 4482 /* 4483 * XXX KDM return an error, or just assign 4484 * another LUN ID in this case?? 4485 */ 4486 printf("ctl: requested LUN ID %d is already " 4487 "in use\n", be_lun->req_lun_id); 4488 } 4489 if (lun->flags & CTL_LUN_MALLOCED) 4490 free(lun, M_CTL); 4491 be_lun->lun_config_status(be_lun->be_lun, 4492 CTL_LUN_CONFIG_FAILURE); 4493 return (ENOSPC); 4494 } 4495 lun_number = be_lun->req_lun_id; 4496 } else { 4497 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4498 if (lun_number == -1) { 4499 mtx_unlock(&ctl_softc->ctl_lock); 4500 printf("ctl: can't allocate LUN on target %ju, out of " 4501 "LUNs\n", (uintmax_t)target_id.id); 4502 if (lun->flags & CTL_LUN_MALLOCED) 4503 free(lun, M_CTL); 4504 be_lun->lun_config_status(be_lun->be_lun, 4505 CTL_LUN_CONFIG_FAILURE); 4506 return (ENOSPC); 4507 } 4508 } 4509 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4510 4511 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4512 lun->target = target_id; 4513 lun->lun = lun_number; 4514 lun->be_lun = be_lun; 4515 /* 4516 * The processor LUN is always enabled. Disk LUNs come on line 4517 * disabled, and must be enabled by the backend. 4518 */ 4519 lun->flags |= CTL_LUN_DISABLED; 4520 lun->backend = be_lun->be; 4521 be_lun->ctl_lun = lun; 4522 be_lun->lun_id = lun_number; 4523 atomic_add_int(&be_lun->be->num_luns, 1); 4524 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4525 lun->flags |= CTL_LUN_OFFLINE; 4526 4527 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4528 lun->flags |= CTL_LUN_STOPPED; 4529 4530 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4531 lun->flags |= CTL_LUN_INOPERABLE; 4532 4533 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4534 lun->flags |= CTL_LUN_PRIMARY_SC; 4535 4536 value = ctl_get_opt(&be_lun->options, "readonly"); 4537 if (value != NULL && strcmp(value, "on") == 0) 4538 lun->flags |= CTL_LUN_READONLY; 4539 4540 lun->ctl_softc = ctl_softc; 4541 TAILQ_INIT(&lun->ooa_queue); 4542 TAILQ_INIT(&lun->blocked_queue); 4543 STAILQ_INIT(&lun->error_list); 4544 ctl_tpc_lun_init(lun); 4545 4546 /* 4547 * Initialize the mode and log page index. 4548 */ 4549 ctl_init_page_index(lun); 4550 ctl_init_log_page_index(lun); 4551 4552 /* 4553 * Set the poweron UA for all initiators on this LUN only. 4554 */ 4555 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4556 lun->pending_ua[i] = CTL_UA_POWERON; 4557 4558 /* 4559 * Now, before we insert this lun on the lun list, set the lun 4560 * inventory changed UA for all other luns. 4561 */ 4562 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4563 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4564 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4565 } 4566 } 4567 4568 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4569 4570 ctl_softc->ctl_luns[lun_number] = lun; 4571 4572 ctl_softc->num_luns++; 4573 4574 /* Setup statistics gathering */ 4575 lun->stats.device_type = be_lun->lun_type; 4576 lun->stats.lun_number = lun_number; 4577 if (lun->stats.device_type == T_DIRECT) 4578 lun->stats.blocksize = be_lun->blocksize; 4579 else 4580 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4581 for (i = 0;i < CTL_MAX_PORTS;i++) 4582 lun->stats.ports[i].targ_port = i; 4583 4584 mtx_unlock(&ctl_softc->ctl_lock); 4585 4586 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4587 4588 /* 4589 * Run through each registered FETD and bring it online if it isn't 4590 * already. Enable the target ID if it hasn't been enabled, and 4591 * enable this particular LUN. 4592 */ 4593 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4594 int retval; 4595 4596 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4597 if (retval != 0) { 4598 printf("ctl_alloc_lun: FETD %s port %d returned error " 4599 "%d for lun_enable on target %ju lun %d\n", 4600 port->port_name, port->targ_port, retval, 4601 (uintmax_t)target_id.id, lun_number); 4602 } else 4603 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4604 } 4605 return (0); 4606 } 4607 4608 /* 4609 * Delete a LUN. 4610 * Assumptions: 4611 * - LUN has already been marked invalid and any pending I/O has been taken 4612 * care of. 4613 */ 4614 static int 4615 ctl_free_lun(struct ctl_lun *lun) 4616 { 4617 struct ctl_softc *softc; 4618 #if 0 4619 struct ctl_port *port; 4620 #endif 4621 struct ctl_lun *nlun; 4622 int i; 4623 4624 softc = lun->ctl_softc; 4625 4626 mtx_assert(&softc->ctl_lock, MA_OWNED); 4627 4628 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4629 4630 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4631 4632 softc->ctl_luns[lun->lun] = NULL; 4633 4634 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4635 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4636 4637 softc->num_luns--; 4638 4639 /* 4640 * XXX KDM this scheme only works for a single target/multiple LUN 4641 * setup. It needs to be revamped for a multiple target scheme. 4642 * 4643 * XXX KDM this results in port->lun_disable() getting called twice, 4644 * once when ctl_disable_lun() is called, and a second time here. 4645 * We really need to re-think the LUN disable semantics. There 4646 * should probably be several steps/levels to LUN removal: 4647 * - disable 4648 * - invalidate 4649 * - free 4650 * 4651 * Right now we only have a disable method when communicating to 4652 * the front end ports, at least for individual LUNs. 4653 */ 4654 #if 0 4655 STAILQ_FOREACH(port, &softc->port_list, links) { 4656 int retval; 4657 4658 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4659 lun->lun); 4660 if (retval != 0) { 4661 printf("ctl_free_lun: FETD %s port %d returned error " 4662 "%d for lun_disable on target %ju lun %jd\n", 4663 port->port_name, port->targ_port, retval, 4664 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4665 } 4666 4667 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4668 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4669 4670 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4671 if (retval != 0) { 4672 printf("ctl_free_lun: FETD %s port %d " 4673 "returned error %d for targ_disable on " 4674 "target %ju\n", port->port_name, 4675 port->targ_port, retval, 4676 (uintmax_t)lun->target.id); 4677 } else 4678 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4679 4680 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4681 continue; 4682 4683 #if 0 4684 port->port_offline(port->onoff_arg); 4685 port->status &= ~CTL_PORT_STATUS_ONLINE; 4686 #endif 4687 } 4688 } 4689 #endif 4690 4691 /* 4692 * Tell the backend to free resources, if this LUN has a backend. 4693 */ 4694 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4695 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4696 4697 ctl_tpc_lun_shutdown(lun); 4698 mtx_destroy(&lun->lun_lock); 4699 free(lun->lun_devid, M_CTL); 4700 if (lun->flags & CTL_LUN_MALLOCED) 4701 free(lun, M_CTL); 4702 4703 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4704 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4705 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4706 } 4707 } 4708 4709 return (0); 4710 } 4711 4712 static void 4713 ctl_create_lun(struct ctl_be_lun *be_lun) 4714 { 4715 struct ctl_softc *ctl_softc; 4716 4717 ctl_softc = control_softc; 4718 4719 /* 4720 * ctl_alloc_lun() should handle all potential failure cases. 4721 */ 4722 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4723 } 4724 4725 int 4726 ctl_add_lun(struct ctl_be_lun *be_lun) 4727 { 4728 struct ctl_softc *ctl_softc = control_softc; 4729 4730 mtx_lock(&ctl_softc->ctl_lock); 4731 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4732 mtx_unlock(&ctl_softc->ctl_lock); 4733 wakeup(&ctl_softc->pending_lun_queue); 4734 4735 return (0); 4736 } 4737 4738 int 4739 ctl_enable_lun(struct ctl_be_lun *be_lun) 4740 { 4741 struct ctl_softc *ctl_softc; 4742 struct ctl_port *port, *nport; 4743 struct ctl_lun *lun; 4744 int retval; 4745 4746 ctl_softc = control_softc; 4747 4748 lun = (struct ctl_lun *)be_lun->ctl_lun; 4749 4750 mtx_lock(&ctl_softc->ctl_lock); 4751 mtx_lock(&lun->lun_lock); 4752 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4753 /* 4754 * eh? Why did we get called if the LUN is already 4755 * enabled? 4756 */ 4757 mtx_unlock(&lun->lun_lock); 4758 mtx_unlock(&ctl_softc->ctl_lock); 4759 return (0); 4760 } 4761 lun->flags &= ~CTL_LUN_DISABLED; 4762 mtx_unlock(&lun->lun_lock); 4763 4764 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4765 nport = STAILQ_NEXT(port, links); 4766 4767 /* 4768 * Drop the lock while we call the FETD's enable routine. 4769 * This can lead to a callback into CTL (at least in the 4770 * case of the internal initiator frontend. 4771 */ 4772 mtx_unlock(&ctl_softc->ctl_lock); 4773 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4774 mtx_lock(&ctl_softc->ctl_lock); 4775 if (retval != 0) { 4776 printf("%s: FETD %s port %d returned error " 4777 "%d for lun_enable on target %ju lun %jd\n", 4778 __func__, port->port_name, port->targ_port, retval, 4779 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4780 } 4781 #if 0 4782 else { 4783 /* NOTE: TODO: why does lun enable affect port status? */ 4784 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4785 } 4786 #endif 4787 } 4788 4789 mtx_unlock(&ctl_softc->ctl_lock); 4790 4791 return (0); 4792 } 4793 4794 int 4795 ctl_disable_lun(struct ctl_be_lun *be_lun) 4796 { 4797 struct ctl_softc *ctl_softc; 4798 struct ctl_port *port; 4799 struct ctl_lun *lun; 4800 int retval; 4801 4802 ctl_softc = control_softc; 4803 4804 lun = (struct ctl_lun *)be_lun->ctl_lun; 4805 4806 mtx_lock(&ctl_softc->ctl_lock); 4807 mtx_lock(&lun->lun_lock); 4808 if (lun->flags & CTL_LUN_DISABLED) { 4809 mtx_unlock(&lun->lun_lock); 4810 mtx_unlock(&ctl_softc->ctl_lock); 4811 return (0); 4812 } 4813 lun->flags |= CTL_LUN_DISABLED; 4814 mtx_unlock(&lun->lun_lock); 4815 4816 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4817 mtx_unlock(&ctl_softc->ctl_lock); 4818 /* 4819 * Drop the lock before we call the frontend's disable 4820 * routine, to avoid lock order reversals. 4821 * 4822 * XXX KDM what happens if the frontend list changes while 4823 * we're traversing it? It's unlikely, but should be handled. 4824 */ 4825 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4826 lun->lun); 4827 mtx_lock(&ctl_softc->ctl_lock); 4828 if (retval != 0) { 4829 printf("ctl_alloc_lun: FETD %s port %d returned error " 4830 "%d for lun_disable on target %ju lun %jd\n", 4831 port->port_name, port->targ_port, retval, 4832 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4833 } 4834 } 4835 4836 mtx_unlock(&ctl_softc->ctl_lock); 4837 4838 return (0); 4839 } 4840 4841 int 4842 ctl_start_lun(struct ctl_be_lun *be_lun) 4843 { 4844 struct ctl_softc *ctl_softc; 4845 struct ctl_lun *lun; 4846 4847 ctl_softc = control_softc; 4848 4849 lun = (struct ctl_lun *)be_lun->ctl_lun; 4850 4851 mtx_lock(&lun->lun_lock); 4852 lun->flags &= ~CTL_LUN_STOPPED; 4853 mtx_unlock(&lun->lun_lock); 4854 4855 return (0); 4856 } 4857 4858 int 4859 ctl_stop_lun(struct ctl_be_lun *be_lun) 4860 { 4861 struct ctl_softc *ctl_softc; 4862 struct ctl_lun *lun; 4863 4864 ctl_softc = control_softc; 4865 4866 lun = (struct ctl_lun *)be_lun->ctl_lun; 4867 4868 mtx_lock(&lun->lun_lock); 4869 lun->flags |= CTL_LUN_STOPPED; 4870 mtx_unlock(&lun->lun_lock); 4871 4872 return (0); 4873 } 4874 4875 int 4876 ctl_lun_offline(struct ctl_be_lun *be_lun) 4877 { 4878 struct ctl_softc *ctl_softc; 4879 struct ctl_lun *lun; 4880 4881 ctl_softc = control_softc; 4882 4883 lun = (struct ctl_lun *)be_lun->ctl_lun; 4884 4885 mtx_lock(&lun->lun_lock); 4886 lun->flags |= CTL_LUN_OFFLINE; 4887 mtx_unlock(&lun->lun_lock); 4888 4889 return (0); 4890 } 4891 4892 int 4893 ctl_lun_online(struct ctl_be_lun *be_lun) 4894 { 4895 struct ctl_softc *ctl_softc; 4896 struct ctl_lun *lun; 4897 4898 ctl_softc = control_softc; 4899 4900 lun = (struct ctl_lun *)be_lun->ctl_lun; 4901 4902 mtx_lock(&lun->lun_lock); 4903 lun->flags &= ~CTL_LUN_OFFLINE; 4904 mtx_unlock(&lun->lun_lock); 4905 4906 return (0); 4907 } 4908 4909 int 4910 ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4911 { 4912 struct ctl_softc *ctl_softc; 4913 struct ctl_lun *lun; 4914 4915 ctl_softc = control_softc; 4916 4917 lun = (struct ctl_lun *)be_lun->ctl_lun; 4918 4919 mtx_lock(&lun->lun_lock); 4920 4921 /* 4922 * The LUN needs to be disabled before it can be marked invalid. 4923 */ 4924 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4925 mtx_unlock(&lun->lun_lock); 4926 return (-1); 4927 } 4928 /* 4929 * Mark the LUN invalid. 4930 */ 4931 lun->flags |= CTL_LUN_INVALID; 4932 4933 /* 4934 * If there is nothing in the OOA queue, go ahead and free the LUN. 4935 * If we have something in the OOA queue, we'll free it when the 4936 * last I/O completes. 4937 */ 4938 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4939 mtx_unlock(&lun->lun_lock); 4940 mtx_lock(&ctl_softc->ctl_lock); 4941 ctl_free_lun(lun); 4942 mtx_unlock(&ctl_softc->ctl_lock); 4943 } else 4944 mtx_unlock(&lun->lun_lock); 4945 4946 return (0); 4947 } 4948 4949 int 4950 ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4951 { 4952 struct ctl_softc *ctl_softc; 4953 struct ctl_lun *lun; 4954 4955 ctl_softc = control_softc; 4956 lun = (struct ctl_lun *)be_lun->ctl_lun; 4957 4958 mtx_lock(&lun->lun_lock); 4959 lun->flags |= CTL_LUN_INOPERABLE; 4960 mtx_unlock(&lun->lun_lock); 4961 4962 return (0); 4963 } 4964 4965 int 4966 ctl_lun_operable(struct ctl_be_lun *be_lun) 4967 { 4968 struct ctl_softc *ctl_softc; 4969 struct ctl_lun *lun; 4970 4971 ctl_softc = control_softc; 4972 lun = (struct ctl_lun *)be_lun->ctl_lun; 4973 4974 mtx_lock(&lun->lun_lock); 4975 lun->flags &= ~CTL_LUN_INOPERABLE; 4976 mtx_unlock(&lun->lun_lock); 4977 4978 return (0); 4979 } 4980 4981 void 4982 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4983 { 4984 struct ctl_lun *lun; 4985 struct ctl_softc *softc; 4986 int i; 4987 4988 softc = control_softc; 4989 4990 lun = (struct ctl_lun *)be_lun->ctl_lun; 4991 4992 mtx_lock(&lun->lun_lock); 4993 4994 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4995 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 4996 4997 mtx_unlock(&lun->lun_lock); 4998 } 4999 5000 /* 5001 * Backend "memory move is complete" callback for requests that never 5002 * make it down to say RAIDCore's configuration code. 5003 */ 5004 int 5005 ctl_config_move_done(union ctl_io *io) 5006 { 5007 int retval; 5008 5009 retval = CTL_RETVAL_COMPLETE; 5010 5011 5012 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5013 /* 5014 * XXX KDM this shouldn't happen, but what if it does? 5015 */ 5016 if (io->io_hdr.io_type != CTL_IO_SCSI) 5017 panic("I/O type isn't CTL_IO_SCSI!"); 5018 5019 if ((io->io_hdr.port_status == 0) 5020 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5021 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5022 io->io_hdr.status = CTL_SUCCESS; 5023 else if ((io->io_hdr.port_status != 0) 5024 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5025 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5026 /* 5027 * For hardware error sense keys, the sense key 5028 * specific value is defined to be a retry count, 5029 * but we use it to pass back an internal FETD 5030 * error code. XXX KDM Hopefully the FETD is only 5031 * using 16 bits for an error code, since that's 5032 * all the space we have in the sks field. 5033 */ 5034 ctl_set_internal_failure(&io->scsiio, 5035 /*sks_valid*/ 1, 5036 /*retry_count*/ 5037 io->io_hdr.port_status); 5038 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5039 free(io->scsiio.kern_data_ptr, M_CTL); 5040 ctl_done(io); 5041 goto bailout; 5042 } 5043 5044 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5045 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5046 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5047 /* 5048 * XXX KDM just assuming a single pointer here, and not a 5049 * S/G list. If we start using S/G lists for config data, 5050 * we'll need to know how to clean them up here as well. 5051 */ 5052 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5053 free(io->scsiio.kern_data_ptr, M_CTL); 5054 /* Hopefully the user has already set the status... */ 5055 ctl_done(io); 5056 } else { 5057 /* 5058 * XXX KDM now we need to continue data movement. Some 5059 * options: 5060 * - call ctl_scsiio() again? We don't do this for data 5061 * writes, because for those at least we know ahead of 5062 * time where the write will go and how long it is. For 5063 * config writes, though, that information is largely 5064 * contained within the write itself, thus we need to 5065 * parse out the data again. 5066 * 5067 * - Call some other function once the data is in? 5068 */ 5069 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5070 ctl_data_print(io); 5071 5072 /* 5073 * XXX KDM call ctl_scsiio() again for now, and check flag 5074 * bits to see whether we're allocated or not. 5075 */ 5076 retval = ctl_scsiio(&io->scsiio); 5077 } 5078 bailout: 5079 return (retval); 5080 } 5081 5082 /* 5083 * This gets called by a backend driver when it is done with a 5084 * data_submit method. 5085 */ 5086 void 5087 ctl_data_submit_done(union ctl_io *io) 5088 { 5089 /* 5090 * If the IO_CONT flag is set, we need to call the supplied 5091 * function to continue processing the I/O, instead of completing 5092 * the I/O just yet. 5093 * 5094 * If there is an error, though, we don't want to keep processing. 5095 * Instead, just send status back to the initiator. 5096 */ 5097 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5098 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5099 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5100 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5101 io->scsiio.io_cont(io); 5102 return; 5103 } 5104 ctl_done(io); 5105 } 5106 5107 /* 5108 * This gets called by a backend driver when it is done with a 5109 * configuration write. 5110 */ 5111 void 5112 ctl_config_write_done(union ctl_io *io) 5113 { 5114 uint8_t *buf; 5115 5116 /* 5117 * If the IO_CONT flag is set, we need to call the supplied 5118 * function to continue processing the I/O, instead of completing 5119 * the I/O just yet. 5120 * 5121 * If there is an error, though, we don't want to keep processing. 5122 * Instead, just send status back to the initiator. 5123 */ 5124 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5125 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5126 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5127 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5128 io->scsiio.io_cont(io); 5129 return; 5130 } 5131 /* 5132 * Since a configuration write can be done for commands that actually 5133 * have data allocated, like write buffer, and commands that have 5134 * no data, like start/stop unit, we need to check here. 5135 */ 5136 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5137 buf = io->scsiio.kern_data_ptr; 5138 else 5139 buf = NULL; 5140 ctl_done(io); 5141 if (buf) 5142 free(buf, M_CTL); 5143 } 5144 5145 /* 5146 * SCSI release command. 5147 */ 5148 int 5149 ctl_scsi_release(struct ctl_scsiio *ctsio) 5150 { 5151 int length, longid, thirdparty_id, resv_id; 5152 struct ctl_softc *ctl_softc; 5153 struct ctl_lun *lun; 5154 uint32_t residx; 5155 5156 length = 0; 5157 resv_id = 0; 5158 5159 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5160 5161 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5162 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5163 ctl_softc = control_softc; 5164 5165 switch (ctsio->cdb[0]) { 5166 case RELEASE_10: { 5167 struct scsi_release_10 *cdb; 5168 5169 cdb = (struct scsi_release_10 *)ctsio->cdb; 5170 5171 if (cdb->byte2 & SR10_LONGID) 5172 longid = 1; 5173 else 5174 thirdparty_id = cdb->thirdparty_id; 5175 5176 resv_id = cdb->resv_id; 5177 length = scsi_2btoul(cdb->length); 5178 break; 5179 } 5180 } 5181 5182 5183 /* 5184 * XXX KDM right now, we only support LUN reservation. We don't 5185 * support 3rd party reservations, or extent reservations, which 5186 * might actually need the parameter list. If we've gotten this 5187 * far, we've got a LUN reservation. Anything else got kicked out 5188 * above. So, according to SPC, ignore the length. 5189 */ 5190 length = 0; 5191 5192 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5193 && (length > 0)) { 5194 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5195 ctsio->kern_data_len = length; 5196 ctsio->kern_total_len = length; 5197 ctsio->kern_data_resid = 0; 5198 ctsio->kern_rel_offset = 0; 5199 ctsio->kern_sg_entries = 0; 5200 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5201 ctsio->be_move_done = ctl_config_move_done; 5202 ctl_datamove((union ctl_io *)ctsio); 5203 5204 return (CTL_RETVAL_COMPLETE); 5205 } 5206 5207 if (length > 0) 5208 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5209 5210 mtx_lock(&lun->lun_lock); 5211 5212 /* 5213 * According to SPC, it is not an error for an intiator to attempt 5214 * to release a reservation on a LUN that isn't reserved, or that 5215 * is reserved by another initiator. The reservation can only be 5216 * released, though, by the initiator who made it or by one of 5217 * several reset type events. 5218 */ 5219 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5220 lun->flags &= ~CTL_LUN_RESERVED; 5221 5222 mtx_unlock(&lun->lun_lock); 5223 5224 ctsio->scsi_status = SCSI_STATUS_OK; 5225 ctsio->io_hdr.status = CTL_SUCCESS; 5226 5227 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5228 free(ctsio->kern_data_ptr, M_CTL); 5229 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5230 } 5231 5232 ctl_done((union ctl_io *)ctsio); 5233 return (CTL_RETVAL_COMPLETE); 5234 } 5235 5236 int 5237 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5238 { 5239 int extent, thirdparty, longid; 5240 int resv_id, length; 5241 uint64_t thirdparty_id; 5242 struct ctl_softc *ctl_softc; 5243 struct ctl_lun *lun; 5244 uint32_t residx; 5245 5246 extent = 0; 5247 thirdparty = 0; 5248 longid = 0; 5249 resv_id = 0; 5250 length = 0; 5251 thirdparty_id = 0; 5252 5253 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5254 5255 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5256 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5257 ctl_softc = control_softc; 5258 5259 switch (ctsio->cdb[0]) { 5260 case RESERVE_10: { 5261 struct scsi_reserve_10 *cdb; 5262 5263 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5264 5265 if (cdb->byte2 & SR10_LONGID) 5266 longid = 1; 5267 else 5268 thirdparty_id = cdb->thirdparty_id; 5269 5270 resv_id = cdb->resv_id; 5271 length = scsi_2btoul(cdb->length); 5272 break; 5273 } 5274 } 5275 5276 /* 5277 * XXX KDM right now, we only support LUN reservation. We don't 5278 * support 3rd party reservations, or extent reservations, which 5279 * might actually need the parameter list. If we've gotten this 5280 * far, we've got a LUN reservation. Anything else got kicked out 5281 * above. So, according to SPC, ignore the length. 5282 */ 5283 length = 0; 5284 5285 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5286 && (length > 0)) { 5287 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5288 ctsio->kern_data_len = length; 5289 ctsio->kern_total_len = length; 5290 ctsio->kern_data_resid = 0; 5291 ctsio->kern_rel_offset = 0; 5292 ctsio->kern_sg_entries = 0; 5293 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5294 ctsio->be_move_done = ctl_config_move_done; 5295 ctl_datamove((union ctl_io *)ctsio); 5296 5297 return (CTL_RETVAL_COMPLETE); 5298 } 5299 5300 if (length > 0) 5301 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5302 5303 mtx_lock(&lun->lun_lock); 5304 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5305 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5306 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5307 goto bailout; 5308 } 5309 5310 lun->flags |= CTL_LUN_RESERVED; 5311 lun->res_idx = residx; 5312 5313 ctsio->scsi_status = SCSI_STATUS_OK; 5314 ctsio->io_hdr.status = CTL_SUCCESS; 5315 5316 bailout: 5317 mtx_unlock(&lun->lun_lock); 5318 5319 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5320 free(ctsio->kern_data_ptr, M_CTL); 5321 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5322 } 5323 5324 ctl_done((union ctl_io *)ctsio); 5325 return (CTL_RETVAL_COMPLETE); 5326 } 5327 5328 int 5329 ctl_start_stop(struct ctl_scsiio *ctsio) 5330 { 5331 struct scsi_start_stop_unit *cdb; 5332 struct ctl_lun *lun; 5333 struct ctl_softc *ctl_softc; 5334 int retval; 5335 5336 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5337 5338 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5339 ctl_softc = control_softc; 5340 retval = 0; 5341 5342 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5343 5344 /* 5345 * XXX KDM 5346 * We don't support the immediate bit on a stop unit. In order to 5347 * do that, we would need to code up a way to know that a stop is 5348 * pending, and hold off any new commands until it completes, one 5349 * way or another. Then we could accept or reject those commands 5350 * depending on its status. We would almost need to do the reverse 5351 * of what we do below for an immediate start -- return the copy of 5352 * the ctl_io to the FETD with status to send to the host (and to 5353 * free the copy!) and then free the original I/O once the stop 5354 * actually completes. That way, the OOA queue mechanism can work 5355 * to block commands that shouldn't proceed. Another alternative 5356 * would be to put the copy in the queue in place of the original, 5357 * and return the original back to the caller. That could be 5358 * slightly safer.. 5359 */ 5360 if ((cdb->byte2 & SSS_IMMED) 5361 && ((cdb->how & SSS_START) == 0)) { 5362 ctl_set_invalid_field(ctsio, 5363 /*sks_valid*/ 1, 5364 /*command*/ 1, 5365 /*field*/ 1, 5366 /*bit_valid*/ 1, 5367 /*bit*/ 0); 5368 ctl_done((union ctl_io *)ctsio); 5369 return (CTL_RETVAL_COMPLETE); 5370 } 5371 5372 if ((lun->flags & CTL_LUN_PR_RESERVED) 5373 && ((cdb->how & SSS_START)==0)) { 5374 uint32_t residx; 5375 5376 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5377 if (lun->pr_keys[residx] == 0 5378 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5379 5380 ctl_set_reservation_conflict(ctsio); 5381 ctl_done((union ctl_io *)ctsio); 5382 return (CTL_RETVAL_COMPLETE); 5383 } 5384 } 5385 5386 /* 5387 * If there is no backend on this device, we can't start or stop 5388 * it. In theory we shouldn't get any start/stop commands in the 5389 * first place at this level if the LUN doesn't have a backend. 5390 * That should get stopped by the command decode code. 5391 */ 5392 if (lun->backend == NULL) { 5393 ctl_set_invalid_opcode(ctsio); 5394 ctl_done((union ctl_io *)ctsio); 5395 return (CTL_RETVAL_COMPLETE); 5396 } 5397 5398 /* 5399 * XXX KDM Copan-specific offline behavior. 5400 * Figure out a reasonable way to port this? 5401 */ 5402 #ifdef NEEDTOPORT 5403 mtx_lock(&lun->lun_lock); 5404 5405 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5406 && (lun->flags & CTL_LUN_OFFLINE)) { 5407 /* 5408 * If the LUN is offline, and the on/offline bit isn't set, 5409 * reject the start or stop. Otherwise, let it through. 5410 */ 5411 mtx_unlock(&lun->lun_lock); 5412 ctl_set_lun_not_ready(ctsio); 5413 ctl_done((union ctl_io *)ctsio); 5414 } else { 5415 mtx_unlock(&lun->lun_lock); 5416 #endif /* NEEDTOPORT */ 5417 /* 5418 * This could be a start or a stop when we're online, 5419 * or a stop/offline or start/online. A start or stop when 5420 * we're offline is covered in the case above. 5421 */ 5422 /* 5423 * In the non-immediate case, we send the request to 5424 * the backend and return status to the user when 5425 * it is done. 5426 * 5427 * In the immediate case, we allocate a new ctl_io 5428 * to hold a copy of the request, and send that to 5429 * the backend. We then set good status on the 5430 * user's request and return it immediately. 5431 */ 5432 if (cdb->byte2 & SSS_IMMED) { 5433 union ctl_io *new_io; 5434 5435 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5436 if (new_io == NULL) { 5437 ctl_set_busy(ctsio); 5438 ctl_done((union ctl_io *)ctsio); 5439 } else { 5440 ctl_copy_io((union ctl_io *)ctsio, 5441 new_io); 5442 retval = lun->backend->config_write(new_io); 5443 ctl_set_success(ctsio); 5444 ctl_done((union ctl_io *)ctsio); 5445 } 5446 } else { 5447 retval = lun->backend->config_write( 5448 (union ctl_io *)ctsio); 5449 } 5450 #ifdef NEEDTOPORT 5451 } 5452 #endif 5453 return (retval); 5454 } 5455 5456 /* 5457 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5458 * we don't really do anything with the LBA and length fields if the user 5459 * passes them in. Instead we'll just flush out the cache for the entire 5460 * LUN. 5461 */ 5462 int 5463 ctl_sync_cache(struct ctl_scsiio *ctsio) 5464 { 5465 struct ctl_lun *lun; 5466 struct ctl_softc *ctl_softc; 5467 uint64_t starting_lba; 5468 uint32_t block_count; 5469 int retval; 5470 5471 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5472 5473 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5474 ctl_softc = control_softc; 5475 retval = 0; 5476 5477 switch (ctsio->cdb[0]) { 5478 case SYNCHRONIZE_CACHE: { 5479 struct scsi_sync_cache *cdb; 5480 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5481 5482 starting_lba = scsi_4btoul(cdb->begin_lba); 5483 block_count = scsi_2btoul(cdb->lb_count); 5484 break; 5485 } 5486 case SYNCHRONIZE_CACHE_16: { 5487 struct scsi_sync_cache_16 *cdb; 5488 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5489 5490 starting_lba = scsi_8btou64(cdb->begin_lba); 5491 block_count = scsi_4btoul(cdb->lb_count); 5492 break; 5493 } 5494 default: 5495 ctl_set_invalid_opcode(ctsio); 5496 ctl_done((union ctl_io *)ctsio); 5497 goto bailout; 5498 break; /* NOTREACHED */ 5499 } 5500 5501 /* 5502 * We check the LBA and length, but don't do anything with them. 5503 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5504 * get flushed. This check will just help satisfy anyone who wants 5505 * to see an error for an out of range LBA. 5506 */ 5507 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5508 ctl_set_lba_out_of_range(ctsio); 5509 ctl_done((union ctl_io *)ctsio); 5510 goto bailout; 5511 } 5512 5513 /* 5514 * If this LUN has no backend, we can't flush the cache anyway. 5515 */ 5516 if (lun->backend == NULL) { 5517 ctl_set_invalid_opcode(ctsio); 5518 ctl_done((union ctl_io *)ctsio); 5519 goto bailout; 5520 } 5521 5522 /* 5523 * Check to see whether we're configured to send the SYNCHRONIZE 5524 * CACHE command directly to the back end. 5525 */ 5526 mtx_lock(&lun->lun_lock); 5527 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5528 && (++(lun->sync_count) >= lun->sync_interval)) { 5529 lun->sync_count = 0; 5530 mtx_unlock(&lun->lun_lock); 5531 retval = lun->backend->config_write((union ctl_io *)ctsio); 5532 } else { 5533 mtx_unlock(&lun->lun_lock); 5534 ctl_set_success(ctsio); 5535 ctl_done((union ctl_io *)ctsio); 5536 } 5537 5538 bailout: 5539 5540 return (retval); 5541 } 5542 5543 int 5544 ctl_format(struct ctl_scsiio *ctsio) 5545 { 5546 struct scsi_format *cdb; 5547 struct ctl_lun *lun; 5548 struct ctl_softc *ctl_softc; 5549 int length, defect_list_len; 5550 5551 CTL_DEBUG_PRINT(("ctl_format\n")); 5552 5553 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5554 ctl_softc = control_softc; 5555 5556 cdb = (struct scsi_format *)ctsio->cdb; 5557 5558 length = 0; 5559 if (cdb->byte2 & SF_FMTDATA) { 5560 if (cdb->byte2 & SF_LONGLIST) 5561 length = sizeof(struct scsi_format_header_long); 5562 else 5563 length = sizeof(struct scsi_format_header_short); 5564 } 5565 5566 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5567 && (length > 0)) { 5568 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5569 ctsio->kern_data_len = length; 5570 ctsio->kern_total_len = length; 5571 ctsio->kern_data_resid = 0; 5572 ctsio->kern_rel_offset = 0; 5573 ctsio->kern_sg_entries = 0; 5574 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5575 ctsio->be_move_done = ctl_config_move_done; 5576 ctl_datamove((union ctl_io *)ctsio); 5577 5578 return (CTL_RETVAL_COMPLETE); 5579 } 5580 5581 defect_list_len = 0; 5582 5583 if (cdb->byte2 & SF_FMTDATA) { 5584 if (cdb->byte2 & SF_LONGLIST) { 5585 struct scsi_format_header_long *header; 5586 5587 header = (struct scsi_format_header_long *) 5588 ctsio->kern_data_ptr; 5589 5590 defect_list_len = scsi_4btoul(header->defect_list_len); 5591 if (defect_list_len != 0) { 5592 ctl_set_invalid_field(ctsio, 5593 /*sks_valid*/ 1, 5594 /*command*/ 0, 5595 /*field*/ 2, 5596 /*bit_valid*/ 0, 5597 /*bit*/ 0); 5598 goto bailout; 5599 } 5600 } else { 5601 struct scsi_format_header_short *header; 5602 5603 header = (struct scsi_format_header_short *) 5604 ctsio->kern_data_ptr; 5605 5606 defect_list_len = scsi_2btoul(header->defect_list_len); 5607 if (defect_list_len != 0) { 5608 ctl_set_invalid_field(ctsio, 5609 /*sks_valid*/ 1, 5610 /*command*/ 0, 5611 /*field*/ 2, 5612 /*bit_valid*/ 0, 5613 /*bit*/ 0); 5614 goto bailout; 5615 } 5616 } 5617 } 5618 5619 /* 5620 * The format command will clear out the "Medium format corrupted" 5621 * status if set by the configuration code. That status is really 5622 * just a way to notify the host that we have lost the media, and 5623 * get them to issue a command that will basically make them think 5624 * they're blowing away the media. 5625 */ 5626 mtx_lock(&lun->lun_lock); 5627 lun->flags &= ~CTL_LUN_INOPERABLE; 5628 mtx_unlock(&lun->lun_lock); 5629 5630 ctsio->scsi_status = SCSI_STATUS_OK; 5631 ctsio->io_hdr.status = CTL_SUCCESS; 5632 bailout: 5633 5634 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5635 free(ctsio->kern_data_ptr, M_CTL); 5636 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5637 } 5638 5639 ctl_done((union ctl_io *)ctsio); 5640 return (CTL_RETVAL_COMPLETE); 5641 } 5642 5643 int 5644 ctl_read_buffer(struct ctl_scsiio *ctsio) 5645 { 5646 struct scsi_read_buffer *cdb; 5647 struct ctl_lun *lun; 5648 int buffer_offset, len; 5649 static uint8_t descr[4]; 5650 static uint8_t echo_descr[4] = { 0 }; 5651 5652 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5653 5654 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5655 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5656 5657 if (lun->flags & CTL_LUN_PR_RESERVED) { 5658 uint32_t residx; 5659 5660 /* 5661 * XXX KDM need a lock here. 5662 */ 5663 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5664 if ((lun->res_type == SPR_TYPE_EX_AC 5665 && residx != lun->pr_res_idx) 5666 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5667 || lun->res_type == SPR_TYPE_EX_AC_AR) 5668 && lun->pr_keys[residx] == 0)) { 5669 ctl_set_reservation_conflict(ctsio); 5670 ctl_done((union ctl_io *)ctsio); 5671 return (CTL_RETVAL_COMPLETE); 5672 } 5673 } 5674 5675 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5676 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5677 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5678 ctl_set_invalid_field(ctsio, 5679 /*sks_valid*/ 1, 5680 /*command*/ 1, 5681 /*field*/ 1, 5682 /*bit_valid*/ 1, 5683 /*bit*/ 4); 5684 ctl_done((union ctl_io *)ctsio); 5685 return (CTL_RETVAL_COMPLETE); 5686 } 5687 5688 len = scsi_3btoul(cdb->length); 5689 buffer_offset = scsi_3btoul(cdb->offset); 5690 5691 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5692 ctl_set_invalid_field(ctsio, 5693 /*sks_valid*/ 1, 5694 /*command*/ 1, 5695 /*field*/ 6, 5696 /*bit_valid*/ 0, 5697 /*bit*/ 0); 5698 ctl_done((union ctl_io *)ctsio); 5699 return (CTL_RETVAL_COMPLETE); 5700 } 5701 5702 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5703 descr[0] = 0; 5704 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5705 ctsio->kern_data_ptr = descr; 5706 len = min(len, sizeof(descr)); 5707 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5708 ctsio->kern_data_ptr = echo_descr; 5709 len = min(len, sizeof(echo_descr)); 5710 } else 5711 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5712 ctsio->kern_data_len = len; 5713 ctsio->kern_total_len = len; 5714 ctsio->kern_data_resid = 0; 5715 ctsio->kern_rel_offset = 0; 5716 ctsio->kern_sg_entries = 0; 5717 ctsio->be_move_done = ctl_config_move_done; 5718 ctl_datamove((union ctl_io *)ctsio); 5719 5720 return (CTL_RETVAL_COMPLETE); 5721 } 5722 5723 int 5724 ctl_write_buffer(struct ctl_scsiio *ctsio) 5725 { 5726 struct scsi_write_buffer *cdb; 5727 struct ctl_lun *lun; 5728 int buffer_offset, len; 5729 5730 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5731 5732 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5733 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5734 5735 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5736 ctl_set_invalid_field(ctsio, 5737 /*sks_valid*/ 1, 5738 /*command*/ 1, 5739 /*field*/ 1, 5740 /*bit_valid*/ 1, 5741 /*bit*/ 4); 5742 ctl_done((union ctl_io *)ctsio); 5743 return (CTL_RETVAL_COMPLETE); 5744 } 5745 5746 len = scsi_3btoul(cdb->length); 5747 buffer_offset = scsi_3btoul(cdb->offset); 5748 5749 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5750 ctl_set_invalid_field(ctsio, 5751 /*sks_valid*/ 1, 5752 /*command*/ 1, 5753 /*field*/ 6, 5754 /*bit_valid*/ 0, 5755 /*bit*/ 0); 5756 ctl_done((union ctl_io *)ctsio); 5757 return (CTL_RETVAL_COMPLETE); 5758 } 5759 5760 /* 5761 * If we've got a kernel request that hasn't been malloced yet, 5762 * malloc it and tell the caller the data buffer is here. 5763 */ 5764 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5765 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5766 ctsio->kern_data_len = len; 5767 ctsio->kern_total_len = len; 5768 ctsio->kern_data_resid = 0; 5769 ctsio->kern_rel_offset = 0; 5770 ctsio->kern_sg_entries = 0; 5771 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5772 ctsio->be_move_done = ctl_config_move_done; 5773 ctl_datamove((union ctl_io *)ctsio); 5774 5775 return (CTL_RETVAL_COMPLETE); 5776 } 5777 5778 ctl_done((union ctl_io *)ctsio); 5779 5780 return (CTL_RETVAL_COMPLETE); 5781 } 5782 5783 int 5784 ctl_write_same(struct ctl_scsiio *ctsio) 5785 { 5786 struct ctl_lun *lun; 5787 struct ctl_lba_len_flags *lbalen; 5788 uint64_t lba; 5789 uint32_t num_blocks; 5790 int len, retval; 5791 uint8_t byte2; 5792 5793 retval = CTL_RETVAL_COMPLETE; 5794 5795 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5796 5797 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5798 5799 switch (ctsio->cdb[0]) { 5800 case WRITE_SAME_10: { 5801 struct scsi_write_same_10 *cdb; 5802 5803 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5804 5805 lba = scsi_4btoul(cdb->addr); 5806 num_blocks = scsi_2btoul(cdb->length); 5807 byte2 = cdb->byte2; 5808 break; 5809 } 5810 case WRITE_SAME_16: { 5811 struct scsi_write_same_16 *cdb; 5812 5813 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5814 5815 lba = scsi_8btou64(cdb->addr); 5816 num_blocks = scsi_4btoul(cdb->length); 5817 byte2 = cdb->byte2; 5818 break; 5819 } 5820 default: 5821 /* 5822 * We got a command we don't support. This shouldn't 5823 * happen, commands should be filtered out above us. 5824 */ 5825 ctl_set_invalid_opcode(ctsio); 5826 ctl_done((union ctl_io *)ctsio); 5827 5828 return (CTL_RETVAL_COMPLETE); 5829 break; /* NOTREACHED */ 5830 } 5831 5832 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5833 if ((byte2 & SWS_UNMAP) == 0 && 5834 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5835 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5836 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5837 ctl_done((union ctl_io *)ctsio); 5838 return (CTL_RETVAL_COMPLETE); 5839 } 5840 5841 /* 5842 * The first check is to make sure we're in bounds, the second 5843 * check is to catch wrap-around problems. If the lba + num blocks 5844 * is less than the lba, then we've wrapped around and the block 5845 * range is invalid anyway. 5846 */ 5847 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5848 || ((lba + num_blocks) < lba)) { 5849 ctl_set_lba_out_of_range(ctsio); 5850 ctl_done((union ctl_io *)ctsio); 5851 return (CTL_RETVAL_COMPLETE); 5852 } 5853 5854 /* Zero number of blocks means "to the last logical block" */ 5855 if (num_blocks == 0) { 5856 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5857 ctl_set_invalid_field(ctsio, 5858 /*sks_valid*/ 0, 5859 /*command*/ 1, 5860 /*field*/ 0, 5861 /*bit_valid*/ 0, 5862 /*bit*/ 0); 5863 ctl_done((union ctl_io *)ctsio); 5864 return (CTL_RETVAL_COMPLETE); 5865 } 5866 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5867 } 5868 5869 len = lun->be_lun->blocksize; 5870 5871 /* 5872 * If we've got a kernel request that hasn't been malloced yet, 5873 * malloc it and tell the caller the data buffer is here. 5874 */ 5875 if ((byte2 & SWS_NDOB) == 0 && 5876 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5877 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5878 ctsio->kern_data_len = len; 5879 ctsio->kern_total_len = len; 5880 ctsio->kern_data_resid = 0; 5881 ctsio->kern_rel_offset = 0; 5882 ctsio->kern_sg_entries = 0; 5883 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5884 ctsio->be_move_done = ctl_config_move_done; 5885 ctl_datamove((union ctl_io *)ctsio); 5886 5887 return (CTL_RETVAL_COMPLETE); 5888 } 5889 5890 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5891 lbalen->lba = lba; 5892 lbalen->len = num_blocks; 5893 lbalen->flags = byte2; 5894 retval = lun->backend->config_write((union ctl_io *)ctsio); 5895 5896 return (retval); 5897 } 5898 5899 int 5900 ctl_unmap(struct ctl_scsiio *ctsio) 5901 { 5902 struct ctl_lun *lun; 5903 struct scsi_unmap *cdb; 5904 struct ctl_ptr_len_flags *ptrlen; 5905 struct scsi_unmap_header *hdr; 5906 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5907 uint64_t lba; 5908 uint32_t num_blocks; 5909 int len, retval; 5910 uint8_t byte2; 5911 5912 retval = CTL_RETVAL_COMPLETE; 5913 5914 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5915 5916 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5917 cdb = (struct scsi_unmap *)ctsio->cdb; 5918 5919 len = scsi_2btoul(cdb->length); 5920 byte2 = cdb->byte2; 5921 5922 /* 5923 * If we've got a kernel request that hasn't been malloced yet, 5924 * malloc it and tell the caller the data buffer is here. 5925 */ 5926 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5927 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5928 ctsio->kern_data_len = len; 5929 ctsio->kern_total_len = len; 5930 ctsio->kern_data_resid = 0; 5931 ctsio->kern_rel_offset = 0; 5932 ctsio->kern_sg_entries = 0; 5933 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5934 ctsio->be_move_done = ctl_config_move_done; 5935 ctl_datamove((union ctl_io *)ctsio); 5936 5937 return (CTL_RETVAL_COMPLETE); 5938 } 5939 5940 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5941 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5942 if (len < sizeof (*hdr) || 5943 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5944 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5945 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5946 ctl_set_invalid_field(ctsio, 5947 /*sks_valid*/ 0, 5948 /*command*/ 0, 5949 /*field*/ 0, 5950 /*bit_valid*/ 0, 5951 /*bit*/ 0); 5952 ctl_done((union ctl_io *)ctsio); 5953 return (CTL_RETVAL_COMPLETE); 5954 } 5955 len = scsi_2btoul(hdr->desc_length); 5956 buf = (struct scsi_unmap_desc *)(hdr + 1); 5957 end = buf + len / sizeof(*buf); 5958 5959 endnz = buf; 5960 for (range = buf; range < end; range++) { 5961 lba = scsi_8btou64(range->lba); 5962 num_blocks = scsi_4btoul(range->length); 5963 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5964 || ((lba + num_blocks) < lba)) { 5965 ctl_set_lba_out_of_range(ctsio); 5966 ctl_done((union ctl_io *)ctsio); 5967 return (CTL_RETVAL_COMPLETE); 5968 } 5969 if (num_blocks != 0) 5970 endnz = range + 1; 5971 } 5972 5973 /* 5974 * Block backend can not handle zero last range. 5975 * Filter it out and return if there is nothing left. 5976 */ 5977 len = (uint8_t *)endnz - (uint8_t *)buf; 5978 if (len == 0) { 5979 ctl_set_success(ctsio); 5980 ctl_done((union ctl_io *)ctsio); 5981 return (CTL_RETVAL_COMPLETE); 5982 } 5983 5984 mtx_lock(&lun->lun_lock); 5985 ptrlen = (struct ctl_ptr_len_flags *) 5986 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5987 ptrlen->ptr = (void *)buf; 5988 ptrlen->len = len; 5989 ptrlen->flags = byte2; 5990 ctl_check_blocked(lun); 5991 mtx_unlock(&lun->lun_lock); 5992 5993 retval = lun->backend->config_write((union ctl_io *)ctsio); 5994 return (retval); 5995 } 5996 5997 /* 5998 * Note that this function currently doesn't actually do anything inside 5999 * CTL to enforce things if the DQue bit is turned on. 6000 * 6001 * Also note that this function can't be used in the default case, because 6002 * the DQue bit isn't set in the changeable mask for the control mode page 6003 * anyway. This is just here as an example for how to implement a page 6004 * handler, and a placeholder in case we want to allow the user to turn 6005 * tagged queueing on and off. 6006 * 6007 * The D_SENSE bit handling is functional, however, and will turn 6008 * descriptor sense on and off for a given LUN. 6009 */ 6010 int 6011 ctl_control_page_handler(struct ctl_scsiio *ctsio, 6012 struct ctl_page_index *page_index, uint8_t *page_ptr) 6013 { 6014 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6015 struct ctl_lun *lun; 6016 struct ctl_softc *softc; 6017 int set_ua; 6018 uint32_t initidx; 6019 6020 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6021 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6022 set_ua = 0; 6023 6024 user_cp = (struct scsi_control_page *)page_ptr; 6025 current_cp = (struct scsi_control_page *) 6026 (page_index->page_data + (page_index->page_len * 6027 CTL_PAGE_CURRENT)); 6028 saved_cp = (struct scsi_control_page *) 6029 (page_index->page_data + (page_index->page_len * 6030 CTL_PAGE_SAVED)); 6031 6032 softc = control_softc; 6033 6034 mtx_lock(&lun->lun_lock); 6035 if (((current_cp->rlec & SCP_DSENSE) == 0) 6036 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6037 /* 6038 * Descriptor sense is currently turned off and the user 6039 * wants to turn it on. 6040 */ 6041 current_cp->rlec |= SCP_DSENSE; 6042 saved_cp->rlec |= SCP_DSENSE; 6043 lun->flags |= CTL_LUN_SENSE_DESC; 6044 set_ua = 1; 6045 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6046 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6047 /* 6048 * Descriptor sense is currently turned on, and the user 6049 * wants to turn it off. 6050 */ 6051 current_cp->rlec &= ~SCP_DSENSE; 6052 saved_cp->rlec &= ~SCP_DSENSE; 6053 lun->flags &= ~CTL_LUN_SENSE_DESC; 6054 set_ua = 1; 6055 } 6056 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6057 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6058 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6059 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6060 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6061 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6062 set_ua = 1; 6063 } 6064 if ((current_cp->eca_and_aen & SCP_SWP) != 6065 (user_cp->eca_and_aen & SCP_SWP)) { 6066 current_cp->eca_and_aen &= ~SCP_SWP; 6067 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6068 saved_cp->eca_and_aen &= ~SCP_SWP; 6069 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6070 set_ua = 1; 6071 } 6072 if (set_ua != 0) { 6073 int i; 6074 /* 6075 * Let other initiators know that the mode 6076 * parameters for this LUN have changed. 6077 */ 6078 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6079 if (i == initidx) 6080 continue; 6081 6082 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6083 } 6084 } 6085 mtx_unlock(&lun->lun_lock); 6086 6087 return (0); 6088 } 6089 6090 int 6091 ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6092 struct ctl_page_index *page_index, uint8_t *page_ptr) 6093 { 6094 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6095 struct ctl_lun *lun; 6096 int set_ua; 6097 uint32_t initidx; 6098 6099 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6100 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6101 set_ua = 0; 6102 6103 user_cp = (struct scsi_caching_page *)page_ptr; 6104 current_cp = (struct scsi_caching_page *) 6105 (page_index->page_data + (page_index->page_len * 6106 CTL_PAGE_CURRENT)); 6107 saved_cp = (struct scsi_caching_page *) 6108 (page_index->page_data + (page_index->page_len * 6109 CTL_PAGE_SAVED)); 6110 6111 mtx_lock(&lun->lun_lock); 6112 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6113 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6114 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6115 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6116 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6117 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6118 set_ua = 1; 6119 } 6120 if (set_ua != 0) { 6121 int i; 6122 /* 6123 * Let other initiators know that the mode 6124 * parameters for this LUN have changed. 6125 */ 6126 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6127 if (i == initidx) 6128 continue; 6129 6130 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6131 } 6132 } 6133 mtx_unlock(&lun->lun_lock); 6134 6135 return (0); 6136 } 6137 6138 int 6139 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6140 struct ctl_page_index *page_index, 6141 uint8_t *page_ptr) 6142 { 6143 uint8_t *c; 6144 int i; 6145 6146 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6147 ctl_time_io_secs = 6148 (c[0] << 8) | 6149 (c[1] << 0) | 6150 0; 6151 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6152 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6153 printf("page data:"); 6154 for (i=0; i<8; i++) 6155 printf(" %.2x",page_ptr[i]); 6156 printf("\n"); 6157 return (0); 6158 } 6159 6160 int 6161 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6162 struct ctl_page_index *page_index, 6163 int pc) 6164 { 6165 struct copan_debugconf_subpage *page; 6166 6167 page = (struct copan_debugconf_subpage *)page_index->page_data + 6168 (page_index->page_len * pc); 6169 6170 switch (pc) { 6171 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6172 case SMS_PAGE_CTRL_DEFAULT >> 6: 6173 case SMS_PAGE_CTRL_SAVED >> 6: 6174 /* 6175 * We don't update the changable or default bits for this page. 6176 */ 6177 break; 6178 case SMS_PAGE_CTRL_CURRENT >> 6: 6179 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6180 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6181 break; 6182 default: 6183 #ifdef NEEDTOPORT 6184 EPRINT(0, "Invalid PC %d!!", pc); 6185 #endif /* NEEDTOPORT */ 6186 break; 6187 } 6188 return (0); 6189 } 6190 6191 6192 static int 6193 ctl_do_mode_select(union ctl_io *io) 6194 { 6195 struct scsi_mode_page_header *page_header; 6196 struct ctl_page_index *page_index; 6197 struct ctl_scsiio *ctsio; 6198 int control_dev, page_len; 6199 int page_len_offset, page_len_size; 6200 union ctl_modepage_info *modepage_info; 6201 struct ctl_lun *lun; 6202 int *len_left, *len_used; 6203 int retval, i; 6204 6205 ctsio = &io->scsiio; 6206 page_index = NULL; 6207 page_len = 0; 6208 retval = CTL_RETVAL_COMPLETE; 6209 6210 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6211 6212 if (lun->be_lun->lun_type != T_DIRECT) 6213 control_dev = 1; 6214 else 6215 control_dev = 0; 6216 6217 modepage_info = (union ctl_modepage_info *) 6218 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6219 len_left = &modepage_info->header.len_left; 6220 len_used = &modepage_info->header.len_used; 6221 6222 do_next_page: 6223 6224 page_header = (struct scsi_mode_page_header *) 6225 (ctsio->kern_data_ptr + *len_used); 6226 6227 if (*len_left == 0) { 6228 free(ctsio->kern_data_ptr, M_CTL); 6229 ctl_set_success(ctsio); 6230 ctl_done((union ctl_io *)ctsio); 6231 return (CTL_RETVAL_COMPLETE); 6232 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6233 6234 free(ctsio->kern_data_ptr, M_CTL); 6235 ctl_set_param_len_error(ctsio); 6236 ctl_done((union ctl_io *)ctsio); 6237 return (CTL_RETVAL_COMPLETE); 6238 6239 } else if ((page_header->page_code & SMPH_SPF) 6240 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6241 6242 free(ctsio->kern_data_ptr, M_CTL); 6243 ctl_set_param_len_error(ctsio); 6244 ctl_done((union ctl_io *)ctsio); 6245 return (CTL_RETVAL_COMPLETE); 6246 } 6247 6248 6249 /* 6250 * XXX KDM should we do something with the block descriptor? 6251 */ 6252 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6253 6254 if ((control_dev != 0) 6255 && (lun->mode_pages.index[i].page_flags & 6256 CTL_PAGE_FLAG_DISK_ONLY)) 6257 continue; 6258 6259 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6260 (page_header->page_code & SMPH_PC_MASK)) 6261 continue; 6262 6263 /* 6264 * If neither page has a subpage code, then we've got a 6265 * match. 6266 */ 6267 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6268 && ((page_header->page_code & SMPH_SPF) == 0)) { 6269 page_index = &lun->mode_pages.index[i]; 6270 page_len = page_header->page_length; 6271 break; 6272 } 6273 6274 /* 6275 * If both pages have subpages, then the subpage numbers 6276 * have to match. 6277 */ 6278 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6279 && (page_header->page_code & SMPH_SPF)) { 6280 struct scsi_mode_page_header_sp *sph; 6281 6282 sph = (struct scsi_mode_page_header_sp *)page_header; 6283 6284 if (lun->mode_pages.index[i].subpage == 6285 sph->subpage) { 6286 page_index = &lun->mode_pages.index[i]; 6287 page_len = scsi_2btoul(sph->page_length); 6288 break; 6289 } 6290 } 6291 } 6292 6293 /* 6294 * If we couldn't find the page, or if we don't have a mode select 6295 * handler for it, send back an error to the user. 6296 */ 6297 if ((page_index == NULL) 6298 || (page_index->select_handler == NULL)) { 6299 ctl_set_invalid_field(ctsio, 6300 /*sks_valid*/ 1, 6301 /*command*/ 0, 6302 /*field*/ *len_used, 6303 /*bit_valid*/ 0, 6304 /*bit*/ 0); 6305 free(ctsio->kern_data_ptr, M_CTL); 6306 ctl_done((union ctl_io *)ctsio); 6307 return (CTL_RETVAL_COMPLETE); 6308 } 6309 6310 if (page_index->page_code & SMPH_SPF) { 6311 page_len_offset = 2; 6312 page_len_size = 2; 6313 } else { 6314 page_len_size = 1; 6315 page_len_offset = 1; 6316 } 6317 6318 /* 6319 * If the length the initiator gives us isn't the one we specify in 6320 * the mode page header, or if they didn't specify enough data in 6321 * the CDB to avoid truncating this page, kick out the request. 6322 */ 6323 if ((page_len != (page_index->page_len - page_len_offset - 6324 page_len_size)) 6325 || (*len_left < page_index->page_len)) { 6326 6327 6328 ctl_set_invalid_field(ctsio, 6329 /*sks_valid*/ 1, 6330 /*command*/ 0, 6331 /*field*/ *len_used + page_len_offset, 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 /* 6340 * Run through the mode page, checking to make sure that the bits 6341 * the user changed are actually legal for him to change. 6342 */ 6343 for (i = 0; i < page_index->page_len; i++) { 6344 uint8_t *user_byte, *change_mask, *current_byte; 6345 int bad_bit; 6346 int j; 6347 6348 user_byte = (uint8_t *)page_header + i; 6349 change_mask = page_index->page_data + 6350 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6351 current_byte = page_index->page_data + 6352 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6353 6354 /* 6355 * Check to see whether the user set any bits in this byte 6356 * that he is not allowed to set. 6357 */ 6358 if ((*user_byte & ~(*change_mask)) == 6359 (*current_byte & ~(*change_mask))) 6360 continue; 6361 6362 /* 6363 * Go through bit by bit to determine which one is illegal. 6364 */ 6365 bad_bit = 0; 6366 for (j = 7; j >= 0; j--) { 6367 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6368 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6369 bad_bit = i; 6370 break; 6371 } 6372 } 6373 ctl_set_invalid_field(ctsio, 6374 /*sks_valid*/ 1, 6375 /*command*/ 0, 6376 /*field*/ *len_used + i, 6377 /*bit_valid*/ 1, 6378 /*bit*/ bad_bit); 6379 free(ctsio->kern_data_ptr, M_CTL); 6380 ctl_done((union ctl_io *)ctsio); 6381 return (CTL_RETVAL_COMPLETE); 6382 } 6383 6384 /* 6385 * Decrement these before we call the page handler, since we may 6386 * end up getting called back one way or another before the handler 6387 * returns to this context. 6388 */ 6389 *len_left -= page_index->page_len; 6390 *len_used += page_index->page_len; 6391 6392 retval = page_index->select_handler(ctsio, page_index, 6393 (uint8_t *)page_header); 6394 6395 /* 6396 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6397 * wait until this queued command completes to finish processing 6398 * the mode page. If it returns anything other than 6399 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6400 * already set the sense information, freed the data pointer, and 6401 * completed the io for us. 6402 */ 6403 if (retval != CTL_RETVAL_COMPLETE) 6404 goto bailout_no_done; 6405 6406 /* 6407 * If the initiator sent us more than one page, parse the next one. 6408 */ 6409 if (*len_left > 0) 6410 goto do_next_page; 6411 6412 ctl_set_success(ctsio); 6413 free(ctsio->kern_data_ptr, M_CTL); 6414 ctl_done((union ctl_io *)ctsio); 6415 6416 bailout_no_done: 6417 6418 return (CTL_RETVAL_COMPLETE); 6419 6420 } 6421 6422 int 6423 ctl_mode_select(struct ctl_scsiio *ctsio) 6424 { 6425 int param_len, pf, sp; 6426 int header_size, bd_len; 6427 int len_left, len_used; 6428 struct ctl_page_index *page_index; 6429 struct ctl_lun *lun; 6430 int control_dev, page_len; 6431 union ctl_modepage_info *modepage_info; 6432 int retval; 6433 6434 pf = 0; 6435 sp = 0; 6436 page_len = 0; 6437 len_used = 0; 6438 len_left = 0; 6439 retval = 0; 6440 bd_len = 0; 6441 page_index = NULL; 6442 6443 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6444 6445 if (lun->be_lun->lun_type != T_DIRECT) 6446 control_dev = 1; 6447 else 6448 control_dev = 0; 6449 6450 switch (ctsio->cdb[0]) { 6451 case MODE_SELECT_6: { 6452 struct scsi_mode_select_6 *cdb; 6453 6454 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6455 6456 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6457 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6458 6459 param_len = cdb->length; 6460 header_size = sizeof(struct scsi_mode_header_6); 6461 break; 6462 } 6463 case MODE_SELECT_10: { 6464 struct scsi_mode_select_10 *cdb; 6465 6466 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6467 6468 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6469 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6470 6471 param_len = scsi_2btoul(cdb->length); 6472 header_size = sizeof(struct scsi_mode_header_10); 6473 break; 6474 } 6475 default: 6476 ctl_set_invalid_opcode(ctsio); 6477 ctl_done((union ctl_io *)ctsio); 6478 return (CTL_RETVAL_COMPLETE); 6479 break; /* NOTREACHED */ 6480 } 6481 6482 /* 6483 * From SPC-3: 6484 * "A parameter list length of zero indicates that the Data-Out Buffer 6485 * shall be empty. This condition shall not be considered as an error." 6486 */ 6487 if (param_len == 0) { 6488 ctl_set_success(ctsio); 6489 ctl_done((union ctl_io *)ctsio); 6490 return (CTL_RETVAL_COMPLETE); 6491 } 6492 6493 /* 6494 * Since we'll hit this the first time through, prior to 6495 * allocation, we don't need to free a data buffer here. 6496 */ 6497 if (param_len < header_size) { 6498 ctl_set_param_len_error(ctsio); 6499 ctl_done((union ctl_io *)ctsio); 6500 return (CTL_RETVAL_COMPLETE); 6501 } 6502 6503 /* 6504 * Allocate the data buffer and grab the user's data. In theory, 6505 * we shouldn't have to sanity check the parameter list length here 6506 * because the maximum size is 64K. We should be able to malloc 6507 * that much without too many problems. 6508 */ 6509 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6510 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6511 ctsio->kern_data_len = param_len; 6512 ctsio->kern_total_len = param_len; 6513 ctsio->kern_data_resid = 0; 6514 ctsio->kern_rel_offset = 0; 6515 ctsio->kern_sg_entries = 0; 6516 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6517 ctsio->be_move_done = ctl_config_move_done; 6518 ctl_datamove((union ctl_io *)ctsio); 6519 6520 return (CTL_RETVAL_COMPLETE); 6521 } 6522 6523 switch (ctsio->cdb[0]) { 6524 case MODE_SELECT_6: { 6525 struct scsi_mode_header_6 *mh6; 6526 6527 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6528 bd_len = mh6->blk_desc_len; 6529 break; 6530 } 6531 case MODE_SELECT_10: { 6532 struct scsi_mode_header_10 *mh10; 6533 6534 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6535 bd_len = scsi_2btoul(mh10->blk_desc_len); 6536 break; 6537 } 6538 default: 6539 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6540 break; 6541 } 6542 6543 if (param_len < (header_size + bd_len)) { 6544 free(ctsio->kern_data_ptr, M_CTL); 6545 ctl_set_param_len_error(ctsio); 6546 ctl_done((union ctl_io *)ctsio); 6547 return (CTL_RETVAL_COMPLETE); 6548 } 6549 6550 /* 6551 * Set the IO_CONT flag, so that if this I/O gets passed to 6552 * ctl_config_write_done(), it'll get passed back to 6553 * ctl_do_mode_select() for further processing, or completion if 6554 * we're all done. 6555 */ 6556 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6557 ctsio->io_cont = ctl_do_mode_select; 6558 6559 modepage_info = (union ctl_modepage_info *) 6560 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6561 6562 memset(modepage_info, 0, sizeof(*modepage_info)); 6563 6564 len_left = param_len - header_size - bd_len; 6565 len_used = header_size + bd_len; 6566 6567 modepage_info->header.len_left = len_left; 6568 modepage_info->header.len_used = len_used; 6569 6570 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6571 } 6572 6573 int 6574 ctl_mode_sense(struct ctl_scsiio *ctsio) 6575 { 6576 struct ctl_lun *lun; 6577 int pc, page_code, dbd, llba, subpage; 6578 int alloc_len, page_len, header_len, total_len; 6579 struct scsi_mode_block_descr *block_desc; 6580 struct ctl_page_index *page_index; 6581 int control_dev; 6582 6583 dbd = 0; 6584 llba = 0; 6585 block_desc = NULL; 6586 page_index = NULL; 6587 6588 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6589 6590 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6591 6592 if (lun->be_lun->lun_type != T_DIRECT) 6593 control_dev = 1; 6594 else 6595 control_dev = 0; 6596 6597 if (lun->flags & CTL_LUN_PR_RESERVED) { 6598 uint32_t residx; 6599 6600 /* 6601 * XXX KDM need a lock here. 6602 */ 6603 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6604 if ((lun->res_type == SPR_TYPE_EX_AC 6605 && residx != lun->pr_res_idx) 6606 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6607 || lun->res_type == SPR_TYPE_EX_AC_AR) 6608 && lun->pr_keys[residx] == 0)) { 6609 ctl_set_reservation_conflict(ctsio); 6610 ctl_done((union ctl_io *)ctsio); 6611 return (CTL_RETVAL_COMPLETE); 6612 } 6613 } 6614 6615 switch (ctsio->cdb[0]) { 6616 case MODE_SENSE_6: { 6617 struct scsi_mode_sense_6 *cdb; 6618 6619 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6620 6621 header_len = sizeof(struct scsi_mode_hdr_6); 6622 if (cdb->byte2 & SMS_DBD) 6623 dbd = 1; 6624 else 6625 header_len += sizeof(struct scsi_mode_block_descr); 6626 6627 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6628 page_code = cdb->page & SMS_PAGE_CODE; 6629 subpage = cdb->subpage; 6630 alloc_len = cdb->length; 6631 break; 6632 } 6633 case MODE_SENSE_10: { 6634 struct scsi_mode_sense_10 *cdb; 6635 6636 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6637 6638 header_len = sizeof(struct scsi_mode_hdr_10); 6639 6640 if (cdb->byte2 & SMS_DBD) 6641 dbd = 1; 6642 else 6643 header_len += sizeof(struct scsi_mode_block_descr); 6644 if (cdb->byte2 & SMS10_LLBAA) 6645 llba = 1; 6646 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6647 page_code = cdb->page & SMS_PAGE_CODE; 6648 subpage = cdb->subpage; 6649 alloc_len = scsi_2btoul(cdb->length); 6650 break; 6651 } 6652 default: 6653 ctl_set_invalid_opcode(ctsio); 6654 ctl_done((union ctl_io *)ctsio); 6655 return (CTL_RETVAL_COMPLETE); 6656 break; /* NOTREACHED */ 6657 } 6658 6659 /* 6660 * We have to make a first pass through to calculate the size of 6661 * the pages that match the user's query. Then we allocate enough 6662 * memory to hold it, and actually copy the data into the buffer. 6663 */ 6664 switch (page_code) { 6665 case SMS_ALL_PAGES_PAGE: { 6666 int i; 6667 6668 page_len = 0; 6669 6670 /* 6671 * At the moment, values other than 0 and 0xff here are 6672 * reserved according to SPC-3. 6673 */ 6674 if ((subpage != SMS_SUBPAGE_PAGE_0) 6675 && (subpage != SMS_SUBPAGE_ALL)) { 6676 ctl_set_invalid_field(ctsio, 6677 /*sks_valid*/ 1, 6678 /*command*/ 1, 6679 /*field*/ 3, 6680 /*bit_valid*/ 0, 6681 /*bit*/ 0); 6682 ctl_done((union ctl_io *)ctsio); 6683 return (CTL_RETVAL_COMPLETE); 6684 } 6685 6686 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6687 if ((control_dev != 0) 6688 && (lun->mode_pages.index[i].page_flags & 6689 CTL_PAGE_FLAG_DISK_ONLY)) 6690 continue; 6691 6692 /* 6693 * We don't use this subpage if the user didn't 6694 * request all subpages. 6695 */ 6696 if ((lun->mode_pages.index[i].subpage != 0) 6697 && (subpage == SMS_SUBPAGE_PAGE_0)) 6698 continue; 6699 6700 #if 0 6701 printf("found page %#x len %d\n", 6702 lun->mode_pages.index[i].page_code & 6703 SMPH_PC_MASK, 6704 lun->mode_pages.index[i].page_len); 6705 #endif 6706 page_len += lun->mode_pages.index[i].page_len; 6707 } 6708 break; 6709 } 6710 default: { 6711 int i; 6712 6713 page_len = 0; 6714 6715 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6716 /* Look for the right page code */ 6717 if ((lun->mode_pages.index[i].page_code & 6718 SMPH_PC_MASK) != page_code) 6719 continue; 6720 6721 /* Look for the right subpage or the subpage wildcard*/ 6722 if ((lun->mode_pages.index[i].subpage != subpage) 6723 && (subpage != SMS_SUBPAGE_ALL)) 6724 continue; 6725 6726 /* Make sure the page is supported for this dev type */ 6727 if ((control_dev != 0) 6728 && (lun->mode_pages.index[i].page_flags & 6729 CTL_PAGE_FLAG_DISK_ONLY)) 6730 continue; 6731 6732 #if 0 6733 printf("found page %#x len %d\n", 6734 lun->mode_pages.index[i].page_code & 6735 SMPH_PC_MASK, 6736 lun->mode_pages.index[i].page_len); 6737 #endif 6738 6739 page_len += lun->mode_pages.index[i].page_len; 6740 } 6741 6742 if (page_len == 0) { 6743 ctl_set_invalid_field(ctsio, 6744 /*sks_valid*/ 1, 6745 /*command*/ 1, 6746 /*field*/ 2, 6747 /*bit_valid*/ 1, 6748 /*bit*/ 5); 6749 ctl_done((union ctl_io *)ctsio); 6750 return (CTL_RETVAL_COMPLETE); 6751 } 6752 break; 6753 } 6754 } 6755 6756 total_len = header_len + page_len; 6757 #if 0 6758 printf("header_len = %d, page_len = %d, total_len = %d\n", 6759 header_len, page_len, total_len); 6760 #endif 6761 6762 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6763 ctsio->kern_sg_entries = 0; 6764 ctsio->kern_data_resid = 0; 6765 ctsio->kern_rel_offset = 0; 6766 if (total_len < alloc_len) { 6767 ctsio->residual = alloc_len - total_len; 6768 ctsio->kern_data_len = total_len; 6769 ctsio->kern_total_len = total_len; 6770 } else { 6771 ctsio->residual = 0; 6772 ctsio->kern_data_len = alloc_len; 6773 ctsio->kern_total_len = alloc_len; 6774 } 6775 6776 switch (ctsio->cdb[0]) { 6777 case MODE_SENSE_6: { 6778 struct scsi_mode_hdr_6 *header; 6779 6780 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6781 6782 header->datalen = ctl_min(total_len - 1, 254); 6783 if (control_dev == 0) { 6784 header->dev_specific = 0x10; /* DPOFUA */ 6785 if ((lun->flags & CTL_LUN_READONLY) || 6786 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6787 .eca_and_aen & SCP_SWP) != 0) 6788 header->dev_specific |= 0x80; /* WP */ 6789 } 6790 if (dbd) 6791 header->block_descr_len = 0; 6792 else 6793 header->block_descr_len = 6794 sizeof(struct scsi_mode_block_descr); 6795 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6796 break; 6797 } 6798 case MODE_SENSE_10: { 6799 struct scsi_mode_hdr_10 *header; 6800 int datalen; 6801 6802 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6803 6804 datalen = ctl_min(total_len - 2, 65533); 6805 scsi_ulto2b(datalen, header->datalen); 6806 if (control_dev == 0) { 6807 header->dev_specific = 0x10; /* DPOFUA */ 6808 if ((lun->flags & CTL_LUN_READONLY) || 6809 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6810 .eca_and_aen & SCP_SWP) != 0) 6811 header->dev_specific |= 0x80; /* WP */ 6812 } 6813 if (dbd) 6814 scsi_ulto2b(0, header->block_descr_len); 6815 else 6816 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6817 header->block_descr_len); 6818 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6819 break; 6820 } 6821 default: 6822 panic("invalid CDB type %#x", ctsio->cdb[0]); 6823 break; /* NOTREACHED */ 6824 } 6825 6826 /* 6827 * If we've got a disk, use its blocksize in the block 6828 * descriptor. Otherwise, just set it to 0. 6829 */ 6830 if (dbd == 0) { 6831 if (control_dev == 0) 6832 scsi_ulto3b(lun->be_lun->blocksize, 6833 block_desc->block_len); 6834 else 6835 scsi_ulto3b(0, block_desc->block_len); 6836 } 6837 6838 switch (page_code) { 6839 case SMS_ALL_PAGES_PAGE: { 6840 int i, data_used; 6841 6842 data_used = header_len; 6843 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6844 struct ctl_page_index *page_index; 6845 6846 page_index = &lun->mode_pages.index[i]; 6847 6848 if ((control_dev != 0) 6849 && (page_index->page_flags & 6850 CTL_PAGE_FLAG_DISK_ONLY)) 6851 continue; 6852 6853 /* 6854 * We don't use this subpage if the user didn't 6855 * request all subpages. We already checked (above) 6856 * to make sure the user only specified a subpage 6857 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6858 */ 6859 if ((page_index->subpage != 0) 6860 && (subpage == SMS_SUBPAGE_PAGE_0)) 6861 continue; 6862 6863 /* 6864 * Call the handler, if it exists, to update the 6865 * page to the latest values. 6866 */ 6867 if (page_index->sense_handler != NULL) 6868 page_index->sense_handler(ctsio, page_index,pc); 6869 6870 memcpy(ctsio->kern_data_ptr + data_used, 6871 page_index->page_data + 6872 (page_index->page_len * pc), 6873 page_index->page_len); 6874 data_used += page_index->page_len; 6875 } 6876 break; 6877 } 6878 default: { 6879 int i, data_used; 6880 6881 data_used = header_len; 6882 6883 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6884 struct ctl_page_index *page_index; 6885 6886 page_index = &lun->mode_pages.index[i]; 6887 6888 /* Look for the right page code */ 6889 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6890 continue; 6891 6892 /* Look for the right subpage or the subpage wildcard*/ 6893 if ((page_index->subpage != subpage) 6894 && (subpage != SMS_SUBPAGE_ALL)) 6895 continue; 6896 6897 /* Make sure the page is supported for this dev type */ 6898 if ((control_dev != 0) 6899 && (page_index->page_flags & 6900 CTL_PAGE_FLAG_DISK_ONLY)) 6901 continue; 6902 6903 /* 6904 * Call the handler, if it exists, to update the 6905 * page to the latest values. 6906 */ 6907 if (page_index->sense_handler != NULL) 6908 page_index->sense_handler(ctsio, page_index,pc); 6909 6910 memcpy(ctsio->kern_data_ptr + data_used, 6911 page_index->page_data + 6912 (page_index->page_len * pc), 6913 page_index->page_len); 6914 data_used += page_index->page_len; 6915 } 6916 break; 6917 } 6918 } 6919 6920 ctsio->scsi_status = SCSI_STATUS_OK; 6921 6922 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6923 ctsio->be_move_done = ctl_config_move_done; 6924 ctl_datamove((union ctl_io *)ctsio); 6925 6926 return (CTL_RETVAL_COMPLETE); 6927 } 6928 6929 int 6930 ctl_log_sense(struct ctl_scsiio *ctsio) 6931 { 6932 struct ctl_lun *lun; 6933 int i, pc, page_code, subpage; 6934 int alloc_len, total_len; 6935 struct ctl_page_index *page_index; 6936 struct scsi_log_sense *cdb; 6937 struct scsi_log_header *header; 6938 6939 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6940 6941 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6942 cdb = (struct scsi_log_sense *)ctsio->cdb; 6943 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6944 page_code = cdb->page & SLS_PAGE_CODE; 6945 subpage = cdb->subpage; 6946 alloc_len = scsi_2btoul(cdb->length); 6947 6948 page_index = NULL; 6949 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6950 page_index = &lun->log_pages.index[i]; 6951 6952 /* Look for the right page code */ 6953 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6954 continue; 6955 6956 /* Look for the right subpage or the subpage wildcard*/ 6957 if (page_index->subpage != subpage) 6958 continue; 6959 6960 break; 6961 } 6962 if (i >= CTL_NUM_LOG_PAGES) { 6963 ctl_set_invalid_field(ctsio, 6964 /*sks_valid*/ 1, 6965 /*command*/ 1, 6966 /*field*/ 2, 6967 /*bit_valid*/ 0, 6968 /*bit*/ 0); 6969 ctl_done((union ctl_io *)ctsio); 6970 return (CTL_RETVAL_COMPLETE); 6971 } 6972 6973 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6974 6975 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6976 ctsio->kern_sg_entries = 0; 6977 ctsio->kern_data_resid = 0; 6978 ctsio->kern_rel_offset = 0; 6979 if (total_len < alloc_len) { 6980 ctsio->residual = alloc_len - total_len; 6981 ctsio->kern_data_len = total_len; 6982 ctsio->kern_total_len = total_len; 6983 } else { 6984 ctsio->residual = 0; 6985 ctsio->kern_data_len = alloc_len; 6986 ctsio->kern_total_len = alloc_len; 6987 } 6988 6989 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6990 header->page = page_index->page_code; 6991 if (page_index->subpage) { 6992 header->page |= SL_SPF; 6993 header->subpage = page_index->subpage; 6994 } 6995 scsi_ulto2b(page_index->page_len, header->datalen); 6996 6997 /* 6998 * Call the handler, if it exists, to update the 6999 * page to the latest values. 7000 */ 7001 if (page_index->sense_handler != NULL) 7002 page_index->sense_handler(ctsio, page_index, pc); 7003 7004 memcpy(header + 1, page_index->page_data, page_index->page_len); 7005 7006 ctsio->scsi_status = SCSI_STATUS_OK; 7007 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7008 ctsio->be_move_done = ctl_config_move_done; 7009 ctl_datamove((union ctl_io *)ctsio); 7010 7011 return (CTL_RETVAL_COMPLETE); 7012 } 7013 7014 int 7015 ctl_read_capacity(struct ctl_scsiio *ctsio) 7016 { 7017 struct scsi_read_capacity *cdb; 7018 struct scsi_read_capacity_data *data; 7019 struct ctl_lun *lun; 7020 uint32_t lba; 7021 7022 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7023 7024 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7025 7026 lba = scsi_4btoul(cdb->addr); 7027 if (((cdb->pmi & SRC_PMI) == 0) 7028 && (lba != 0)) { 7029 ctl_set_invalid_field(/*ctsio*/ ctsio, 7030 /*sks_valid*/ 1, 7031 /*command*/ 1, 7032 /*field*/ 2, 7033 /*bit_valid*/ 0, 7034 /*bit*/ 0); 7035 ctl_done((union ctl_io *)ctsio); 7036 return (CTL_RETVAL_COMPLETE); 7037 } 7038 7039 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7040 7041 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7042 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7043 ctsio->residual = 0; 7044 ctsio->kern_data_len = sizeof(*data); 7045 ctsio->kern_total_len = sizeof(*data); 7046 ctsio->kern_data_resid = 0; 7047 ctsio->kern_rel_offset = 0; 7048 ctsio->kern_sg_entries = 0; 7049 7050 /* 7051 * If the maximum LBA is greater than 0xfffffffe, the user must 7052 * issue a SERVICE ACTION IN (16) command, with the read capacity 7053 * serivce action set. 7054 */ 7055 if (lun->be_lun->maxlba > 0xfffffffe) 7056 scsi_ulto4b(0xffffffff, data->addr); 7057 else 7058 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7059 7060 /* 7061 * XXX KDM this may not be 512 bytes... 7062 */ 7063 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7064 7065 ctsio->scsi_status = SCSI_STATUS_OK; 7066 7067 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7068 ctsio->be_move_done = ctl_config_move_done; 7069 ctl_datamove((union ctl_io *)ctsio); 7070 7071 return (CTL_RETVAL_COMPLETE); 7072 } 7073 7074 int 7075 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7076 { 7077 struct scsi_read_capacity_16 *cdb; 7078 struct scsi_read_capacity_data_long *data; 7079 struct ctl_lun *lun; 7080 uint64_t lba; 7081 uint32_t alloc_len; 7082 7083 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7084 7085 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7086 7087 alloc_len = scsi_4btoul(cdb->alloc_len); 7088 lba = scsi_8btou64(cdb->addr); 7089 7090 if ((cdb->reladr & SRC16_PMI) 7091 && (lba != 0)) { 7092 ctl_set_invalid_field(/*ctsio*/ ctsio, 7093 /*sks_valid*/ 1, 7094 /*command*/ 1, 7095 /*field*/ 2, 7096 /*bit_valid*/ 0, 7097 /*bit*/ 0); 7098 ctl_done((union ctl_io *)ctsio); 7099 return (CTL_RETVAL_COMPLETE); 7100 } 7101 7102 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7103 7104 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7105 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7106 7107 if (sizeof(*data) < alloc_len) { 7108 ctsio->residual = alloc_len - sizeof(*data); 7109 ctsio->kern_data_len = sizeof(*data); 7110 ctsio->kern_total_len = sizeof(*data); 7111 } else { 7112 ctsio->residual = 0; 7113 ctsio->kern_data_len = alloc_len; 7114 ctsio->kern_total_len = alloc_len; 7115 } 7116 ctsio->kern_data_resid = 0; 7117 ctsio->kern_rel_offset = 0; 7118 ctsio->kern_sg_entries = 0; 7119 7120 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7121 /* XXX KDM this may not be 512 bytes... */ 7122 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7123 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7124 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7125 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7126 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7127 7128 ctsio->scsi_status = SCSI_STATUS_OK; 7129 7130 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7131 ctsio->be_move_done = ctl_config_move_done; 7132 ctl_datamove((union ctl_io *)ctsio); 7133 7134 return (CTL_RETVAL_COMPLETE); 7135 } 7136 7137 int 7138 ctl_read_defect(struct ctl_scsiio *ctsio) 7139 { 7140 struct scsi_read_defect_data_10 *ccb10; 7141 struct scsi_read_defect_data_12 *ccb12; 7142 struct scsi_read_defect_data_hdr_10 *data10; 7143 struct scsi_read_defect_data_hdr_12 *data12; 7144 struct ctl_lun *lun; 7145 uint32_t alloc_len, data_len; 7146 uint8_t format; 7147 7148 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7149 7150 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7151 if (lun->flags & CTL_LUN_PR_RESERVED) { 7152 uint32_t residx; 7153 7154 /* 7155 * XXX KDM need a lock here. 7156 */ 7157 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 7158 if ((lun->res_type == SPR_TYPE_EX_AC 7159 && residx != lun->pr_res_idx) 7160 || ((lun->res_type == SPR_TYPE_EX_AC_RO 7161 || lun->res_type == SPR_TYPE_EX_AC_AR) 7162 && lun->pr_keys[residx] == 0)) { 7163 ctl_set_reservation_conflict(ctsio); 7164 ctl_done((union ctl_io *)ctsio); 7165 return (CTL_RETVAL_COMPLETE); 7166 } 7167 } 7168 7169 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7170 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7171 format = ccb10->format; 7172 alloc_len = scsi_2btoul(ccb10->alloc_length); 7173 data_len = sizeof(*data10); 7174 } else { 7175 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7176 format = ccb12->format; 7177 alloc_len = scsi_4btoul(ccb12->alloc_length); 7178 data_len = sizeof(*data12); 7179 } 7180 if (alloc_len == 0) { 7181 ctl_set_success(ctsio); 7182 ctl_done((union ctl_io *)ctsio); 7183 return (CTL_RETVAL_COMPLETE); 7184 } 7185 7186 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7187 if (data_len < alloc_len) { 7188 ctsio->residual = alloc_len - data_len; 7189 ctsio->kern_data_len = data_len; 7190 ctsio->kern_total_len = data_len; 7191 } else { 7192 ctsio->residual = 0; 7193 ctsio->kern_data_len = alloc_len; 7194 ctsio->kern_total_len = alloc_len; 7195 } 7196 ctsio->kern_data_resid = 0; 7197 ctsio->kern_rel_offset = 0; 7198 ctsio->kern_sg_entries = 0; 7199 7200 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7201 data10 = (struct scsi_read_defect_data_hdr_10 *) 7202 ctsio->kern_data_ptr; 7203 data10->format = format; 7204 scsi_ulto2b(0, data10->length); 7205 } else { 7206 data12 = (struct scsi_read_defect_data_hdr_12 *) 7207 ctsio->kern_data_ptr; 7208 data12->format = format; 7209 scsi_ulto2b(0, data12->generation); 7210 scsi_ulto4b(0, data12->length); 7211 } 7212 7213 ctsio->scsi_status = SCSI_STATUS_OK; 7214 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7215 ctsio->be_move_done = ctl_config_move_done; 7216 ctl_datamove((union ctl_io *)ctsio); 7217 return (CTL_RETVAL_COMPLETE); 7218 } 7219 7220 int 7221 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7222 { 7223 struct scsi_maintenance_in *cdb; 7224 int retval; 7225 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7226 int num_target_port_groups, num_target_ports, single; 7227 struct ctl_lun *lun; 7228 struct ctl_softc *softc; 7229 struct ctl_port *port; 7230 struct scsi_target_group_data *rtg_ptr; 7231 struct scsi_target_group_data_extended *rtg_ext_ptr; 7232 struct scsi_target_port_group_descriptor *tpg_desc; 7233 7234 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7235 7236 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7237 softc = control_softc; 7238 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7239 7240 retval = CTL_RETVAL_COMPLETE; 7241 7242 switch (cdb->byte2 & STG_PDF_MASK) { 7243 case STG_PDF_LENGTH: 7244 ext = 0; 7245 break; 7246 case STG_PDF_EXTENDED: 7247 ext = 1; 7248 break; 7249 default: 7250 ctl_set_invalid_field(/*ctsio*/ ctsio, 7251 /*sks_valid*/ 1, 7252 /*command*/ 1, 7253 /*field*/ 2, 7254 /*bit_valid*/ 1, 7255 /*bit*/ 5); 7256 ctl_done((union ctl_io *)ctsio); 7257 return(retval); 7258 } 7259 7260 single = ctl_is_single; 7261 if (single) 7262 num_target_port_groups = 1; 7263 else 7264 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7265 num_target_ports = 0; 7266 mtx_lock(&softc->ctl_lock); 7267 STAILQ_FOREACH(port, &softc->port_list, links) { 7268 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7269 continue; 7270 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7271 continue; 7272 num_target_ports++; 7273 } 7274 mtx_unlock(&softc->ctl_lock); 7275 7276 if (ext) 7277 total_len = sizeof(struct scsi_target_group_data_extended); 7278 else 7279 total_len = sizeof(struct scsi_target_group_data); 7280 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7281 num_target_port_groups + 7282 sizeof(struct scsi_target_port_descriptor) * 7283 num_target_ports * num_target_port_groups; 7284 7285 alloc_len = scsi_4btoul(cdb->length); 7286 7287 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7288 7289 ctsio->kern_sg_entries = 0; 7290 7291 if (total_len < alloc_len) { 7292 ctsio->residual = alloc_len - total_len; 7293 ctsio->kern_data_len = total_len; 7294 ctsio->kern_total_len = total_len; 7295 } else { 7296 ctsio->residual = 0; 7297 ctsio->kern_data_len = alloc_len; 7298 ctsio->kern_total_len = alloc_len; 7299 } 7300 ctsio->kern_data_resid = 0; 7301 ctsio->kern_rel_offset = 0; 7302 7303 if (ext) { 7304 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7305 ctsio->kern_data_ptr; 7306 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7307 rtg_ext_ptr->format_type = 0x10; 7308 rtg_ext_ptr->implicit_transition_time = 0; 7309 tpg_desc = &rtg_ext_ptr->groups[0]; 7310 } else { 7311 rtg_ptr = (struct scsi_target_group_data *) 7312 ctsio->kern_data_ptr; 7313 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7314 tpg_desc = &rtg_ptr->groups[0]; 7315 } 7316 7317 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7318 mtx_lock(&softc->ctl_lock); 7319 for (g = 0; g < num_target_port_groups; g++) { 7320 if (g == pg) 7321 tpg_desc->pref_state = TPG_PRIMARY | 7322 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7323 else 7324 tpg_desc->pref_state = 7325 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7326 tpg_desc->support = TPG_AO_SUP; 7327 if (!single) 7328 tpg_desc->support |= TPG_AN_SUP; 7329 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7330 tpg_desc->status = TPG_IMPLICIT; 7331 pc = 0; 7332 STAILQ_FOREACH(port, &softc->port_list, links) { 7333 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7334 continue; 7335 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7336 CTL_MAX_LUNS) 7337 continue; 7338 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7339 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7340 relative_target_port_identifier); 7341 pc++; 7342 } 7343 tpg_desc->target_port_count = pc; 7344 tpg_desc = (struct scsi_target_port_group_descriptor *) 7345 &tpg_desc->descriptors[pc]; 7346 } 7347 mtx_unlock(&softc->ctl_lock); 7348 7349 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7350 ctsio->be_move_done = ctl_config_move_done; 7351 7352 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7353 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7354 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7355 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7356 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7357 7358 ctl_datamove((union ctl_io *)ctsio); 7359 return(retval); 7360 } 7361 7362 int 7363 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7364 { 7365 struct ctl_lun *lun; 7366 struct scsi_report_supported_opcodes *cdb; 7367 const struct ctl_cmd_entry *entry, *sentry; 7368 struct scsi_report_supported_opcodes_all *all; 7369 struct scsi_report_supported_opcodes_descr *descr; 7370 struct scsi_report_supported_opcodes_one *one; 7371 int retval; 7372 int alloc_len, total_len; 7373 int opcode, service_action, i, j, num; 7374 7375 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7376 7377 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7378 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7379 7380 retval = CTL_RETVAL_COMPLETE; 7381 7382 opcode = cdb->requested_opcode; 7383 service_action = scsi_2btoul(cdb->requested_service_action); 7384 switch (cdb->options & RSO_OPTIONS_MASK) { 7385 case RSO_OPTIONS_ALL: 7386 num = 0; 7387 for (i = 0; i < 256; i++) { 7388 entry = &ctl_cmd_table[i]; 7389 if (entry->flags & CTL_CMD_FLAG_SA5) { 7390 for (j = 0; j < 32; j++) { 7391 sentry = &((const struct ctl_cmd_entry *) 7392 entry->execute)[j]; 7393 if (ctl_cmd_applicable( 7394 lun->be_lun->lun_type, sentry)) 7395 num++; 7396 } 7397 } else { 7398 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7399 entry)) 7400 num++; 7401 } 7402 } 7403 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7404 num * sizeof(struct scsi_report_supported_opcodes_descr); 7405 break; 7406 case RSO_OPTIONS_OC: 7407 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7408 ctl_set_invalid_field(/*ctsio*/ ctsio, 7409 /*sks_valid*/ 1, 7410 /*command*/ 1, 7411 /*field*/ 2, 7412 /*bit_valid*/ 1, 7413 /*bit*/ 2); 7414 ctl_done((union ctl_io *)ctsio); 7415 return (CTL_RETVAL_COMPLETE); 7416 } 7417 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7418 break; 7419 case RSO_OPTIONS_OC_SA: 7420 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7421 service_action >= 32) { 7422 ctl_set_invalid_field(/*ctsio*/ ctsio, 7423 /*sks_valid*/ 1, 7424 /*command*/ 1, 7425 /*field*/ 2, 7426 /*bit_valid*/ 1, 7427 /*bit*/ 2); 7428 ctl_done((union ctl_io *)ctsio); 7429 return (CTL_RETVAL_COMPLETE); 7430 } 7431 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7432 break; 7433 default: 7434 ctl_set_invalid_field(/*ctsio*/ ctsio, 7435 /*sks_valid*/ 1, 7436 /*command*/ 1, 7437 /*field*/ 2, 7438 /*bit_valid*/ 1, 7439 /*bit*/ 2); 7440 ctl_done((union ctl_io *)ctsio); 7441 return (CTL_RETVAL_COMPLETE); 7442 } 7443 7444 alloc_len = scsi_4btoul(cdb->length); 7445 7446 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7447 7448 ctsio->kern_sg_entries = 0; 7449 7450 if (total_len < alloc_len) { 7451 ctsio->residual = alloc_len - total_len; 7452 ctsio->kern_data_len = total_len; 7453 ctsio->kern_total_len = total_len; 7454 } else { 7455 ctsio->residual = 0; 7456 ctsio->kern_data_len = alloc_len; 7457 ctsio->kern_total_len = alloc_len; 7458 } 7459 ctsio->kern_data_resid = 0; 7460 ctsio->kern_rel_offset = 0; 7461 7462 switch (cdb->options & RSO_OPTIONS_MASK) { 7463 case RSO_OPTIONS_ALL: 7464 all = (struct scsi_report_supported_opcodes_all *) 7465 ctsio->kern_data_ptr; 7466 num = 0; 7467 for (i = 0; i < 256; i++) { 7468 entry = &ctl_cmd_table[i]; 7469 if (entry->flags & CTL_CMD_FLAG_SA5) { 7470 for (j = 0; j < 32; j++) { 7471 sentry = &((const struct ctl_cmd_entry *) 7472 entry->execute)[j]; 7473 if (!ctl_cmd_applicable( 7474 lun->be_lun->lun_type, sentry)) 7475 continue; 7476 descr = &all->descr[num++]; 7477 descr->opcode = i; 7478 scsi_ulto2b(j, descr->service_action); 7479 descr->flags = RSO_SERVACTV; 7480 scsi_ulto2b(sentry->length, 7481 descr->cdb_length); 7482 } 7483 } else { 7484 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7485 entry)) 7486 continue; 7487 descr = &all->descr[num++]; 7488 descr->opcode = i; 7489 scsi_ulto2b(0, descr->service_action); 7490 descr->flags = 0; 7491 scsi_ulto2b(entry->length, descr->cdb_length); 7492 } 7493 } 7494 scsi_ulto4b( 7495 num * sizeof(struct scsi_report_supported_opcodes_descr), 7496 all->length); 7497 break; 7498 case RSO_OPTIONS_OC: 7499 one = (struct scsi_report_supported_opcodes_one *) 7500 ctsio->kern_data_ptr; 7501 entry = &ctl_cmd_table[opcode]; 7502 goto fill_one; 7503 case RSO_OPTIONS_OC_SA: 7504 one = (struct scsi_report_supported_opcodes_one *) 7505 ctsio->kern_data_ptr; 7506 entry = &ctl_cmd_table[opcode]; 7507 entry = &((const struct ctl_cmd_entry *) 7508 entry->execute)[service_action]; 7509 fill_one: 7510 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7511 one->support = 3; 7512 scsi_ulto2b(entry->length, one->cdb_length); 7513 one->cdb_usage[0] = opcode; 7514 memcpy(&one->cdb_usage[1], entry->usage, 7515 entry->length - 1); 7516 } else 7517 one->support = 1; 7518 break; 7519 } 7520 7521 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7522 ctsio->be_move_done = ctl_config_move_done; 7523 7524 ctl_datamove((union ctl_io *)ctsio); 7525 return(retval); 7526 } 7527 7528 int 7529 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7530 { 7531 struct ctl_lun *lun; 7532 struct scsi_report_supported_tmf *cdb; 7533 struct scsi_report_supported_tmf_data *data; 7534 int retval; 7535 int alloc_len, total_len; 7536 7537 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7538 7539 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7540 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7541 7542 retval = CTL_RETVAL_COMPLETE; 7543 7544 total_len = sizeof(struct scsi_report_supported_tmf_data); 7545 alloc_len = scsi_4btoul(cdb->length); 7546 7547 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7548 7549 ctsio->kern_sg_entries = 0; 7550 7551 if (total_len < alloc_len) { 7552 ctsio->residual = alloc_len - total_len; 7553 ctsio->kern_data_len = total_len; 7554 ctsio->kern_total_len = total_len; 7555 } else { 7556 ctsio->residual = 0; 7557 ctsio->kern_data_len = alloc_len; 7558 ctsio->kern_total_len = alloc_len; 7559 } 7560 ctsio->kern_data_resid = 0; 7561 ctsio->kern_rel_offset = 0; 7562 7563 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7564 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7565 data->byte2 |= RST_ITNRS; 7566 7567 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7568 ctsio->be_move_done = ctl_config_move_done; 7569 7570 ctl_datamove((union ctl_io *)ctsio); 7571 return (retval); 7572 } 7573 7574 int 7575 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7576 { 7577 struct ctl_lun *lun; 7578 struct scsi_report_timestamp *cdb; 7579 struct scsi_report_timestamp_data *data; 7580 struct timeval tv; 7581 int64_t timestamp; 7582 int retval; 7583 int alloc_len, total_len; 7584 7585 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7586 7587 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7588 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7589 7590 retval = CTL_RETVAL_COMPLETE; 7591 7592 total_len = sizeof(struct scsi_report_timestamp_data); 7593 alloc_len = scsi_4btoul(cdb->length); 7594 7595 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7596 7597 ctsio->kern_sg_entries = 0; 7598 7599 if (total_len < alloc_len) { 7600 ctsio->residual = alloc_len - total_len; 7601 ctsio->kern_data_len = total_len; 7602 ctsio->kern_total_len = total_len; 7603 } else { 7604 ctsio->residual = 0; 7605 ctsio->kern_data_len = alloc_len; 7606 ctsio->kern_total_len = alloc_len; 7607 } 7608 ctsio->kern_data_resid = 0; 7609 ctsio->kern_rel_offset = 0; 7610 7611 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7612 scsi_ulto2b(sizeof(*data) - 2, data->length); 7613 data->origin = RTS_ORIG_OUTSIDE; 7614 getmicrotime(&tv); 7615 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7616 scsi_ulto4b(timestamp >> 16, data->timestamp); 7617 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7618 7619 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7620 ctsio->be_move_done = ctl_config_move_done; 7621 7622 ctl_datamove((union ctl_io *)ctsio); 7623 return (retval); 7624 } 7625 7626 int 7627 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7628 { 7629 struct scsi_per_res_in *cdb; 7630 int alloc_len, total_len = 0; 7631 /* struct scsi_per_res_in_rsrv in_data; */ 7632 struct ctl_lun *lun; 7633 struct ctl_softc *softc; 7634 7635 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7636 7637 softc = control_softc; 7638 7639 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7640 7641 alloc_len = scsi_2btoul(cdb->length); 7642 7643 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7644 7645 retry: 7646 mtx_lock(&lun->lun_lock); 7647 switch (cdb->action) { 7648 case SPRI_RK: /* read keys */ 7649 total_len = sizeof(struct scsi_per_res_in_keys) + 7650 lun->pr_key_count * 7651 sizeof(struct scsi_per_res_key); 7652 break; 7653 case SPRI_RR: /* read reservation */ 7654 if (lun->flags & CTL_LUN_PR_RESERVED) 7655 total_len = sizeof(struct scsi_per_res_in_rsrv); 7656 else 7657 total_len = sizeof(struct scsi_per_res_in_header); 7658 break; 7659 case SPRI_RC: /* report capabilities */ 7660 total_len = sizeof(struct scsi_per_res_cap); 7661 break; 7662 case SPRI_RS: /* read full status */ 7663 total_len = sizeof(struct scsi_per_res_in_header) + 7664 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7665 lun->pr_key_count; 7666 break; 7667 default: 7668 panic("Invalid PR type %x", cdb->action); 7669 } 7670 mtx_unlock(&lun->lun_lock); 7671 7672 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7673 7674 if (total_len < alloc_len) { 7675 ctsio->residual = alloc_len - total_len; 7676 ctsio->kern_data_len = total_len; 7677 ctsio->kern_total_len = total_len; 7678 } else { 7679 ctsio->residual = 0; 7680 ctsio->kern_data_len = alloc_len; 7681 ctsio->kern_total_len = alloc_len; 7682 } 7683 7684 ctsio->kern_data_resid = 0; 7685 ctsio->kern_rel_offset = 0; 7686 ctsio->kern_sg_entries = 0; 7687 7688 mtx_lock(&lun->lun_lock); 7689 switch (cdb->action) { 7690 case SPRI_RK: { // read keys 7691 struct scsi_per_res_in_keys *res_keys; 7692 int i, key_count; 7693 7694 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7695 7696 /* 7697 * We had to drop the lock to allocate our buffer, which 7698 * leaves time for someone to come in with another 7699 * persistent reservation. (That is unlikely, though, 7700 * since this should be the only persistent reservation 7701 * command active right now.) 7702 */ 7703 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7704 (lun->pr_key_count * 7705 sizeof(struct scsi_per_res_key)))){ 7706 mtx_unlock(&lun->lun_lock); 7707 free(ctsio->kern_data_ptr, M_CTL); 7708 printf("%s: reservation length changed, retrying\n", 7709 __func__); 7710 goto retry; 7711 } 7712 7713 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7714 7715 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7716 lun->pr_key_count, res_keys->header.length); 7717 7718 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7719 if (lun->pr_keys[i] == 0) 7720 continue; 7721 7722 /* 7723 * We used lun->pr_key_count to calculate the 7724 * size to allocate. If it turns out the number of 7725 * initiators with the registered flag set is 7726 * larger than that (i.e. they haven't been kept in 7727 * sync), we've got a problem. 7728 */ 7729 if (key_count >= lun->pr_key_count) { 7730 #ifdef NEEDTOPORT 7731 csevent_log(CSC_CTL | CSC_SHELF_SW | 7732 CTL_PR_ERROR, 7733 csevent_LogType_Fault, 7734 csevent_AlertLevel_Yellow, 7735 csevent_FRU_ShelfController, 7736 csevent_FRU_Firmware, 7737 csevent_FRU_Unknown, 7738 "registered keys %d >= key " 7739 "count %d", key_count, 7740 lun->pr_key_count); 7741 #endif 7742 key_count++; 7743 continue; 7744 } 7745 scsi_u64to8b(lun->pr_keys[i], 7746 res_keys->keys[key_count].key); 7747 key_count++; 7748 } 7749 break; 7750 } 7751 case SPRI_RR: { // read reservation 7752 struct scsi_per_res_in_rsrv *res; 7753 int tmp_len, header_only; 7754 7755 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7756 7757 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7758 7759 if (lun->flags & CTL_LUN_PR_RESERVED) 7760 { 7761 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7762 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7763 res->header.length); 7764 header_only = 0; 7765 } else { 7766 tmp_len = sizeof(struct scsi_per_res_in_header); 7767 scsi_ulto4b(0, res->header.length); 7768 header_only = 1; 7769 } 7770 7771 /* 7772 * We had to drop the lock to allocate our buffer, which 7773 * leaves time for someone to come in with another 7774 * persistent reservation. (That is unlikely, though, 7775 * since this should be the only persistent reservation 7776 * command active right now.) 7777 */ 7778 if (tmp_len != total_len) { 7779 mtx_unlock(&lun->lun_lock); 7780 free(ctsio->kern_data_ptr, M_CTL); 7781 printf("%s: reservation status changed, retrying\n", 7782 __func__); 7783 goto retry; 7784 } 7785 7786 /* 7787 * No reservation held, so we're done. 7788 */ 7789 if (header_only != 0) 7790 break; 7791 7792 /* 7793 * If the registration is an All Registrants type, the key 7794 * is 0, since it doesn't really matter. 7795 */ 7796 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7797 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx], 7798 res->data.reservation); 7799 } 7800 res->data.scopetype = lun->res_type; 7801 break; 7802 } 7803 case SPRI_RC: //report capabilities 7804 { 7805 struct scsi_per_res_cap *res_cap; 7806 uint16_t type_mask; 7807 7808 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7809 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7810 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7811 type_mask = SPRI_TM_WR_EX_AR | 7812 SPRI_TM_EX_AC_RO | 7813 SPRI_TM_WR_EX_RO | 7814 SPRI_TM_EX_AC | 7815 SPRI_TM_WR_EX | 7816 SPRI_TM_EX_AC_AR; 7817 scsi_ulto2b(type_mask, res_cap->type_mask); 7818 break; 7819 } 7820 case SPRI_RS: { // read full status 7821 struct scsi_per_res_in_full *res_status; 7822 struct scsi_per_res_in_full_desc *res_desc; 7823 struct ctl_port *port; 7824 int i, len; 7825 7826 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7827 7828 /* 7829 * We had to drop the lock to allocate our buffer, which 7830 * leaves time for someone to come in with another 7831 * persistent reservation. (That is unlikely, though, 7832 * since this should be the only persistent reservation 7833 * command active right now.) 7834 */ 7835 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7836 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7837 lun->pr_key_count)){ 7838 mtx_unlock(&lun->lun_lock); 7839 free(ctsio->kern_data_ptr, M_CTL); 7840 printf("%s: reservation length changed, retrying\n", 7841 __func__); 7842 goto retry; 7843 } 7844 7845 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7846 7847 res_desc = &res_status->desc[0]; 7848 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7849 if (lun->pr_keys[i] == 0) 7850 continue; 7851 7852 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key); 7853 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7854 (lun->pr_res_idx == i || 7855 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7856 res_desc->flags = SPRI_FULL_R_HOLDER; 7857 res_desc->scopetype = lun->res_type; 7858 } 7859 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7860 res_desc->rel_trgt_port_id); 7861 len = 0; 7862 port = softc->ctl_ports[ 7863 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7864 if (port != NULL) 7865 len = ctl_create_iid(port, 7866 i % CTL_MAX_INIT_PER_PORT, 7867 res_desc->transport_id); 7868 scsi_ulto4b(len, res_desc->additional_length); 7869 res_desc = (struct scsi_per_res_in_full_desc *) 7870 &res_desc->transport_id[len]; 7871 } 7872 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7873 res_status->header.length); 7874 break; 7875 } 7876 default: 7877 /* 7878 * This is a bug, because we just checked for this above, 7879 * and should have returned an error. 7880 */ 7881 panic("Invalid PR type %x", cdb->action); 7882 break; /* NOTREACHED */ 7883 } 7884 mtx_unlock(&lun->lun_lock); 7885 7886 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7887 ctsio->be_move_done = ctl_config_move_done; 7888 7889 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7890 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7891 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7892 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7893 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7894 7895 ctl_datamove((union ctl_io *)ctsio); 7896 7897 return (CTL_RETVAL_COMPLETE); 7898 } 7899 7900 /* 7901 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7902 * it should return. 7903 */ 7904 static int 7905 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7906 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7907 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7908 struct scsi_per_res_out_parms* param) 7909 { 7910 union ctl_ha_msg persis_io; 7911 int retval, i; 7912 int isc_retval; 7913 7914 retval = 0; 7915 7916 mtx_lock(&lun->lun_lock); 7917 if (sa_res_key == 0) { 7918 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7919 /* validate scope and type */ 7920 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7921 SPR_LU_SCOPE) { 7922 mtx_unlock(&lun->lun_lock); 7923 ctl_set_invalid_field(/*ctsio*/ ctsio, 7924 /*sks_valid*/ 1, 7925 /*command*/ 1, 7926 /*field*/ 2, 7927 /*bit_valid*/ 1, 7928 /*bit*/ 4); 7929 ctl_done((union ctl_io *)ctsio); 7930 return (1); 7931 } 7932 7933 if (type>8 || type==2 || type==4 || type==0) { 7934 mtx_unlock(&lun->lun_lock); 7935 ctl_set_invalid_field(/*ctsio*/ ctsio, 7936 /*sks_valid*/ 1, 7937 /*command*/ 1, 7938 /*field*/ 2, 7939 /*bit_valid*/ 1, 7940 /*bit*/ 0); 7941 ctl_done((union ctl_io *)ctsio); 7942 return (1); 7943 } 7944 7945 /* 7946 * Unregister everybody else and build UA for 7947 * them 7948 */ 7949 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7950 if (i == residx || lun->pr_keys[i] == 0) 7951 continue; 7952 7953 if (!persis_offset 7954 && i <CTL_MAX_INITIATORS) 7955 lun->pending_ua[i] |= 7956 CTL_UA_REG_PREEMPT; 7957 else if (persis_offset 7958 && i >= persis_offset) 7959 lun->pending_ua[i-persis_offset] |= 7960 CTL_UA_REG_PREEMPT; 7961 lun->pr_keys[i] = 0; 7962 } 7963 lun->pr_key_count = 1; 7964 lun->res_type = type; 7965 if (lun->res_type != SPR_TYPE_WR_EX_AR 7966 && lun->res_type != SPR_TYPE_EX_AC_AR) 7967 lun->pr_res_idx = residx; 7968 7969 /* send msg to other side */ 7970 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7971 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7972 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7973 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7974 persis_io.pr.pr_info.res_type = type; 7975 memcpy(persis_io.pr.pr_info.sa_res_key, 7976 param->serv_act_res_key, 7977 sizeof(param->serv_act_res_key)); 7978 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7979 &persis_io, sizeof(persis_io), 0)) > 7980 CTL_HA_STATUS_SUCCESS) { 7981 printf("CTL:Persis Out error returned " 7982 "from ctl_ha_msg_send %d\n", 7983 isc_retval); 7984 } 7985 } else { 7986 /* not all registrants */ 7987 mtx_unlock(&lun->lun_lock); 7988 free(ctsio->kern_data_ptr, M_CTL); 7989 ctl_set_invalid_field(ctsio, 7990 /*sks_valid*/ 1, 7991 /*command*/ 0, 7992 /*field*/ 8, 7993 /*bit_valid*/ 0, 7994 /*bit*/ 0); 7995 ctl_done((union ctl_io *)ctsio); 7996 return (1); 7997 } 7998 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7999 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8000 int found = 0; 8001 8002 if (res_key == sa_res_key) { 8003 /* special case */ 8004 /* 8005 * The spec implies this is not good but doesn't 8006 * say what to do. There are two choices either 8007 * generate a res conflict or check condition 8008 * with illegal field in parameter data. Since 8009 * that is what is done when the sa_res_key is 8010 * zero I'll take that approach since this has 8011 * to do with the sa_res_key. 8012 */ 8013 mtx_unlock(&lun->lun_lock); 8014 free(ctsio->kern_data_ptr, M_CTL); 8015 ctl_set_invalid_field(ctsio, 8016 /*sks_valid*/ 1, 8017 /*command*/ 0, 8018 /*field*/ 8, 8019 /*bit_valid*/ 0, 8020 /*bit*/ 0); 8021 ctl_done((union ctl_io *)ctsio); 8022 return (1); 8023 } 8024 8025 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8026 if (lun->pr_keys[i] != sa_res_key) 8027 continue; 8028 8029 found = 1; 8030 lun->pr_keys[i] = 0; 8031 lun->pr_key_count--; 8032 8033 if (!persis_offset && i < CTL_MAX_INITIATORS) 8034 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8035 else if (persis_offset && i >= persis_offset) 8036 lun->pending_ua[i-persis_offset] |= 8037 CTL_UA_REG_PREEMPT; 8038 } 8039 if (!found) { 8040 mtx_unlock(&lun->lun_lock); 8041 free(ctsio->kern_data_ptr, M_CTL); 8042 ctl_set_reservation_conflict(ctsio); 8043 ctl_done((union ctl_io *)ctsio); 8044 return (CTL_RETVAL_COMPLETE); 8045 } 8046 /* send msg to other side */ 8047 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8048 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8049 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8050 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8051 persis_io.pr.pr_info.res_type = type; 8052 memcpy(persis_io.pr.pr_info.sa_res_key, 8053 param->serv_act_res_key, 8054 sizeof(param->serv_act_res_key)); 8055 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8056 &persis_io, sizeof(persis_io), 0)) > 8057 CTL_HA_STATUS_SUCCESS) { 8058 printf("CTL:Persis Out error returned from " 8059 "ctl_ha_msg_send %d\n", isc_retval); 8060 } 8061 } else { 8062 /* Reserved but not all registrants */ 8063 /* sa_res_key is res holder */ 8064 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) { 8065 /* validate scope and type */ 8066 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8067 SPR_LU_SCOPE) { 8068 mtx_unlock(&lun->lun_lock); 8069 ctl_set_invalid_field(/*ctsio*/ ctsio, 8070 /*sks_valid*/ 1, 8071 /*command*/ 1, 8072 /*field*/ 2, 8073 /*bit_valid*/ 1, 8074 /*bit*/ 4); 8075 ctl_done((union ctl_io *)ctsio); 8076 return (1); 8077 } 8078 8079 if (type>8 || type==2 || type==4 || type==0) { 8080 mtx_unlock(&lun->lun_lock); 8081 ctl_set_invalid_field(/*ctsio*/ ctsio, 8082 /*sks_valid*/ 1, 8083 /*command*/ 1, 8084 /*field*/ 2, 8085 /*bit_valid*/ 1, 8086 /*bit*/ 0); 8087 ctl_done((union ctl_io *)ctsio); 8088 return (1); 8089 } 8090 8091 /* 8092 * Do the following: 8093 * if sa_res_key != res_key remove all 8094 * registrants w/sa_res_key and generate UA 8095 * for these registrants(Registrations 8096 * Preempted) if it wasn't an exclusive 8097 * reservation generate UA(Reservations 8098 * Preempted) for all other registered nexuses 8099 * if the type has changed. Establish the new 8100 * reservation and holder. If res_key and 8101 * sa_res_key are the same do the above 8102 * except don't unregister the res holder. 8103 */ 8104 8105 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8106 if (i == residx || lun->pr_keys[i] == 0) 8107 continue; 8108 8109 if (sa_res_key == lun->pr_keys[i]) { 8110 lun->pr_keys[i] = 0; 8111 lun->pr_key_count--; 8112 8113 if (!persis_offset 8114 && i < CTL_MAX_INITIATORS) 8115 lun->pending_ua[i] |= 8116 CTL_UA_REG_PREEMPT; 8117 else if (persis_offset 8118 && i >= persis_offset) 8119 lun->pending_ua[i-persis_offset] |= 8120 CTL_UA_REG_PREEMPT; 8121 } else if (type != lun->res_type 8122 && (lun->res_type == SPR_TYPE_WR_EX_RO 8123 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8124 if (!persis_offset 8125 && i < CTL_MAX_INITIATORS) 8126 lun->pending_ua[i] |= 8127 CTL_UA_RES_RELEASE; 8128 else if (persis_offset 8129 && i >= persis_offset) 8130 lun->pending_ua[ 8131 i-persis_offset] |= 8132 CTL_UA_RES_RELEASE; 8133 } 8134 } 8135 lun->res_type = type; 8136 if (lun->res_type != SPR_TYPE_WR_EX_AR 8137 && lun->res_type != SPR_TYPE_EX_AC_AR) 8138 lun->pr_res_idx = residx; 8139 else 8140 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8141 8142 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8143 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8144 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8145 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8146 persis_io.pr.pr_info.res_type = type; 8147 memcpy(persis_io.pr.pr_info.sa_res_key, 8148 param->serv_act_res_key, 8149 sizeof(param->serv_act_res_key)); 8150 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8151 &persis_io, sizeof(persis_io), 0)) > 8152 CTL_HA_STATUS_SUCCESS) { 8153 printf("CTL:Persis Out error returned " 8154 "from ctl_ha_msg_send %d\n", 8155 isc_retval); 8156 } 8157 } else { 8158 /* 8159 * sa_res_key is not the res holder just 8160 * remove registrants 8161 */ 8162 int found=0; 8163 8164 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8165 if (sa_res_key != lun->pr_keys[i]) 8166 continue; 8167 8168 found = 1; 8169 lun->pr_keys[i] = 0; 8170 lun->pr_key_count--; 8171 8172 if (!persis_offset 8173 && i < CTL_MAX_INITIATORS) 8174 lun->pending_ua[i] |= 8175 CTL_UA_REG_PREEMPT; 8176 else if (persis_offset 8177 && i >= persis_offset) 8178 lun->pending_ua[i-persis_offset] |= 8179 CTL_UA_REG_PREEMPT; 8180 } 8181 8182 if (!found) { 8183 mtx_unlock(&lun->lun_lock); 8184 free(ctsio->kern_data_ptr, M_CTL); 8185 ctl_set_reservation_conflict(ctsio); 8186 ctl_done((union ctl_io *)ctsio); 8187 return (1); 8188 } 8189 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8190 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8191 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8192 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8193 persis_io.pr.pr_info.res_type = type; 8194 memcpy(persis_io.pr.pr_info.sa_res_key, 8195 param->serv_act_res_key, 8196 sizeof(param->serv_act_res_key)); 8197 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8198 &persis_io, sizeof(persis_io), 0)) > 8199 CTL_HA_STATUS_SUCCESS) { 8200 printf("CTL:Persis Out error returned " 8201 "from ctl_ha_msg_send %d\n", 8202 isc_retval); 8203 } 8204 } 8205 } 8206 8207 lun->PRGeneration++; 8208 mtx_unlock(&lun->lun_lock); 8209 8210 return (retval); 8211 } 8212 8213 static void 8214 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8215 { 8216 uint64_t sa_res_key; 8217 int i; 8218 8219 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8220 8221 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8222 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8223 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) { 8224 if (sa_res_key == 0) { 8225 /* 8226 * Unregister everybody else and build UA for 8227 * them 8228 */ 8229 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8230 if (i == msg->pr.pr_info.residx || 8231 lun->pr_keys[i] == 0) 8232 continue; 8233 8234 if (!persis_offset 8235 && i < CTL_MAX_INITIATORS) 8236 lun->pending_ua[i] |= 8237 CTL_UA_REG_PREEMPT; 8238 else if (persis_offset && i >= persis_offset) 8239 lun->pending_ua[i - persis_offset] |= 8240 CTL_UA_REG_PREEMPT; 8241 lun->pr_keys[i] = 0; 8242 } 8243 8244 lun->pr_key_count = 1; 8245 lun->res_type = msg->pr.pr_info.res_type; 8246 if (lun->res_type != SPR_TYPE_WR_EX_AR 8247 && lun->res_type != SPR_TYPE_EX_AC_AR) 8248 lun->pr_res_idx = msg->pr.pr_info.residx; 8249 } else { 8250 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8251 if (sa_res_key == lun->pr_keys[i]) 8252 continue; 8253 8254 lun->pr_keys[i] = 0; 8255 lun->pr_key_count--; 8256 8257 if (!persis_offset 8258 && i < persis_offset) 8259 lun->pending_ua[i] |= 8260 CTL_UA_REG_PREEMPT; 8261 else if (persis_offset 8262 && i >= persis_offset) 8263 lun->pending_ua[i - persis_offset] |= 8264 CTL_UA_REG_PREEMPT; 8265 } 8266 } 8267 } else { 8268 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8269 if (i == msg->pr.pr_info.residx || 8270 lun->pr_keys[i] == 0) 8271 continue; 8272 8273 if (sa_res_key == lun->pr_keys[i]) { 8274 lun->pr_keys[i] = 0; 8275 lun->pr_key_count--; 8276 if (!persis_offset 8277 && i < CTL_MAX_INITIATORS) 8278 lun->pending_ua[i] |= 8279 CTL_UA_REG_PREEMPT; 8280 else if (persis_offset 8281 && i >= persis_offset) 8282 lun->pending_ua[i - persis_offset] |= 8283 CTL_UA_REG_PREEMPT; 8284 } else if (msg->pr.pr_info.res_type != lun->res_type 8285 && (lun->res_type == SPR_TYPE_WR_EX_RO 8286 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8287 if (!persis_offset 8288 && i < persis_offset) 8289 lun->pending_ua[i] |= 8290 CTL_UA_RES_RELEASE; 8291 else if (persis_offset 8292 && i >= persis_offset) 8293 lun->pending_ua[i - persis_offset] |= 8294 CTL_UA_RES_RELEASE; 8295 } 8296 } 8297 lun->res_type = msg->pr.pr_info.res_type; 8298 if (lun->res_type != SPR_TYPE_WR_EX_AR 8299 && lun->res_type != SPR_TYPE_EX_AC_AR) 8300 lun->pr_res_idx = msg->pr.pr_info.residx; 8301 else 8302 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8303 } 8304 lun->PRGeneration++; 8305 8306 } 8307 8308 8309 int 8310 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8311 { 8312 int retval; 8313 int isc_retval; 8314 u_int32_t param_len; 8315 struct scsi_per_res_out *cdb; 8316 struct ctl_lun *lun; 8317 struct scsi_per_res_out_parms* param; 8318 struct ctl_softc *softc; 8319 uint32_t residx; 8320 uint64_t res_key, sa_res_key; 8321 uint8_t type; 8322 union ctl_ha_msg persis_io; 8323 int i; 8324 8325 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8326 8327 retval = CTL_RETVAL_COMPLETE; 8328 8329 softc = control_softc; 8330 8331 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8332 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8333 8334 /* 8335 * We only support whole-LUN scope. The scope & type are ignored for 8336 * register, register and ignore existing key and clear. 8337 * We sometimes ignore scope and type on preempts too!! 8338 * Verify reservation type here as well. 8339 */ 8340 type = cdb->scope_type & SPR_TYPE_MASK; 8341 if ((cdb->action == SPRO_RESERVE) 8342 || (cdb->action == SPRO_RELEASE)) { 8343 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8344 ctl_set_invalid_field(/*ctsio*/ ctsio, 8345 /*sks_valid*/ 1, 8346 /*command*/ 1, 8347 /*field*/ 2, 8348 /*bit_valid*/ 1, 8349 /*bit*/ 4); 8350 ctl_done((union ctl_io *)ctsio); 8351 return (CTL_RETVAL_COMPLETE); 8352 } 8353 8354 if (type>8 || type==2 || type==4 || type==0) { 8355 ctl_set_invalid_field(/*ctsio*/ ctsio, 8356 /*sks_valid*/ 1, 8357 /*command*/ 1, 8358 /*field*/ 2, 8359 /*bit_valid*/ 1, 8360 /*bit*/ 0); 8361 ctl_done((union ctl_io *)ctsio); 8362 return (CTL_RETVAL_COMPLETE); 8363 } 8364 } 8365 8366 param_len = scsi_4btoul(cdb->length); 8367 8368 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8369 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8370 ctsio->kern_data_len = param_len; 8371 ctsio->kern_total_len = param_len; 8372 ctsio->kern_data_resid = 0; 8373 ctsio->kern_rel_offset = 0; 8374 ctsio->kern_sg_entries = 0; 8375 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8376 ctsio->be_move_done = ctl_config_move_done; 8377 ctl_datamove((union ctl_io *)ctsio); 8378 8379 return (CTL_RETVAL_COMPLETE); 8380 } 8381 8382 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8383 8384 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8385 res_key = scsi_8btou64(param->res_key.key); 8386 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8387 8388 /* 8389 * Validate the reservation key here except for SPRO_REG_IGNO 8390 * This must be done for all other service actions 8391 */ 8392 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8393 mtx_lock(&lun->lun_lock); 8394 if (lun->pr_keys[residx] != 0) { 8395 if (res_key != lun->pr_keys[residx]) { 8396 /* 8397 * The current key passed in doesn't match 8398 * the one the initiator previously 8399 * registered. 8400 */ 8401 mtx_unlock(&lun->lun_lock); 8402 free(ctsio->kern_data_ptr, M_CTL); 8403 ctl_set_reservation_conflict(ctsio); 8404 ctl_done((union ctl_io *)ctsio); 8405 return (CTL_RETVAL_COMPLETE); 8406 } 8407 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8408 /* 8409 * We are not registered 8410 */ 8411 mtx_unlock(&lun->lun_lock); 8412 free(ctsio->kern_data_ptr, M_CTL); 8413 ctl_set_reservation_conflict(ctsio); 8414 ctl_done((union ctl_io *)ctsio); 8415 return (CTL_RETVAL_COMPLETE); 8416 } else if (res_key != 0) { 8417 /* 8418 * We are not registered and trying to register but 8419 * the register key isn't zero. 8420 */ 8421 mtx_unlock(&lun->lun_lock); 8422 free(ctsio->kern_data_ptr, M_CTL); 8423 ctl_set_reservation_conflict(ctsio); 8424 ctl_done((union ctl_io *)ctsio); 8425 return (CTL_RETVAL_COMPLETE); 8426 } 8427 mtx_unlock(&lun->lun_lock); 8428 } 8429 8430 switch (cdb->action & SPRO_ACTION_MASK) { 8431 case SPRO_REGISTER: 8432 case SPRO_REG_IGNO: { 8433 8434 #if 0 8435 printf("Registration received\n"); 8436 #endif 8437 8438 /* 8439 * We don't support any of these options, as we report in 8440 * the read capabilities request (see 8441 * ctl_persistent_reserve_in(), above). 8442 */ 8443 if ((param->flags & SPR_SPEC_I_PT) 8444 || (param->flags & SPR_ALL_TG_PT) 8445 || (param->flags & SPR_APTPL)) { 8446 int bit_ptr; 8447 8448 if (param->flags & SPR_APTPL) 8449 bit_ptr = 0; 8450 else if (param->flags & SPR_ALL_TG_PT) 8451 bit_ptr = 2; 8452 else /* SPR_SPEC_I_PT */ 8453 bit_ptr = 3; 8454 8455 free(ctsio->kern_data_ptr, M_CTL); 8456 ctl_set_invalid_field(ctsio, 8457 /*sks_valid*/ 1, 8458 /*command*/ 0, 8459 /*field*/ 20, 8460 /*bit_valid*/ 1, 8461 /*bit*/ bit_ptr); 8462 ctl_done((union ctl_io *)ctsio); 8463 return (CTL_RETVAL_COMPLETE); 8464 } 8465 8466 mtx_lock(&lun->lun_lock); 8467 8468 /* 8469 * The initiator wants to clear the 8470 * key/unregister. 8471 */ 8472 if (sa_res_key == 0) { 8473 if ((res_key == 0 8474 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8475 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8476 && lun->pr_keys[residx] == 0)) { 8477 mtx_unlock(&lun->lun_lock); 8478 goto done; 8479 } 8480 8481 lun->pr_keys[residx] = 0; 8482 lun->pr_key_count--; 8483 8484 if (residx == lun->pr_res_idx) { 8485 lun->flags &= ~CTL_LUN_PR_RESERVED; 8486 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8487 8488 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8489 || lun->res_type == SPR_TYPE_EX_AC_RO) 8490 && lun->pr_key_count) { 8491 /* 8492 * If the reservation is a registrants 8493 * only type we need to generate a UA 8494 * for other registered inits. The 8495 * sense code should be RESERVATIONS 8496 * RELEASED 8497 */ 8498 8499 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8500 if (lun->pr_keys[ 8501 i + persis_offset] == 0) 8502 continue; 8503 lun->pending_ua[i] |= 8504 CTL_UA_RES_RELEASE; 8505 } 8506 } 8507 lun->res_type = 0; 8508 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8509 if (lun->pr_key_count==0) { 8510 lun->flags &= ~CTL_LUN_PR_RESERVED; 8511 lun->res_type = 0; 8512 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8513 } 8514 } 8515 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8516 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8517 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8518 persis_io.pr.pr_info.residx = residx; 8519 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8520 &persis_io, sizeof(persis_io), 0 )) > 8521 CTL_HA_STATUS_SUCCESS) { 8522 printf("CTL:Persis Out error returned from " 8523 "ctl_ha_msg_send %d\n", isc_retval); 8524 } 8525 } else /* sa_res_key != 0 */ { 8526 8527 /* 8528 * If we aren't registered currently then increment 8529 * the key count and set the registered flag. 8530 */ 8531 if (lun->pr_keys[residx] == 0) 8532 lun->pr_key_count++; 8533 lun->pr_keys[residx] = sa_res_key; 8534 8535 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8536 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8537 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8538 persis_io.pr.pr_info.residx = residx; 8539 memcpy(persis_io.pr.pr_info.sa_res_key, 8540 param->serv_act_res_key, 8541 sizeof(param->serv_act_res_key)); 8542 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8543 &persis_io, sizeof(persis_io), 0)) > 8544 CTL_HA_STATUS_SUCCESS) { 8545 printf("CTL:Persis Out error returned from " 8546 "ctl_ha_msg_send %d\n", isc_retval); 8547 } 8548 } 8549 lun->PRGeneration++; 8550 mtx_unlock(&lun->lun_lock); 8551 8552 break; 8553 } 8554 case SPRO_RESERVE: 8555 #if 0 8556 printf("Reserve executed type %d\n", type); 8557 #endif 8558 mtx_lock(&lun->lun_lock); 8559 if (lun->flags & CTL_LUN_PR_RESERVED) { 8560 /* 8561 * if this isn't the reservation holder and it's 8562 * not a "all registrants" type or if the type is 8563 * different then we have a conflict 8564 */ 8565 if ((lun->pr_res_idx != residx 8566 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8567 || lun->res_type != type) { 8568 mtx_unlock(&lun->lun_lock); 8569 free(ctsio->kern_data_ptr, M_CTL); 8570 ctl_set_reservation_conflict(ctsio); 8571 ctl_done((union ctl_io *)ctsio); 8572 return (CTL_RETVAL_COMPLETE); 8573 } 8574 mtx_unlock(&lun->lun_lock); 8575 } else /* create a reservation */ { 8576 /* 8577 * If it's not an "all registrants" type record 8578 * reservation holder 8579 */ 8580 if (type != SPR_TYPE_WR_EX_AR 8581 && type != SPR_TYPE_EX_AC_AR) 8582 lun->pr_res_idx = residx; /* Res holder */ 8583 else 8584 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8585 8586 lun->flags |= CTL_LUN_PR_RESERVED; 8587 lun->res_type = type; 8588 8589 mtx_unlock(&lun->lun_lock); 8590 8591 /* send msg to other side */ 8592 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8593 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8594 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8595 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8596 persis_io.pr.pr_info.res_type = type; 8597 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8598 &persis_io, sizeof(persis_io), 0)) > 8599 CTL_HA_STATUS_SUCCESS) { 8600 printf("CTL:Persis Out error returned from " 8601 "ctl_ha_msg_send %d\n", isc_retval); 8602 } 8603 } 8604 break; 8605 8606 case SPRO_RELEASE: 8607 mtx_lock(&lun->lun_lock); 8608 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8609 /* No reservation exists return good status */ 8610 mtx_unlock(&lun->lun_lock); 8611 goto done; 8612 } 8613 /* 8614 * Is this nexus a reservation holder? 8615 */ 8616 if (lun->pr_res_idx != residx 8617 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8618 /* 8619 * not a res holder return good status but 8620 * do nothing 8621 */ 8622 mtx_unlock(&lun->lun_lock); 8623 goto done; 8624 } 8625 8626 if (lun->res_type != type) { 8627 mtx_unlock(&lun->lun_lock); 8628 free(ctsio->kern_data_ptr, M_CTL); 8629 ctl_set_illegal_pr_release(ctsio); 8630 ctl_done((union ctl_io *)ctsio); 8631 return (CTL_RETVAL_COMPLETE); 8632 } 8633 8634 /* okay to release */ 8635 lun->flags &= ~CTL_LUN_PR_RESERVED; 8636 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8637 lun->res_type = 0; 8638 8639 /* 8640 * if this isn't an exclusive access 8641 * res generate UA for all other 8642 * registrants. 8643 */ 8644 if (type != SPR_TYPE_EX_AC 8645 && type != SPR_TYPE_WR_EX) { 8646 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8647 if (i == residx || 8648 lun->pr_keys[i + persis_offset] == 0) 8649 continue; 8650 lun->pending_ua[i] |= CTL_UA_RES_RELEASE; 8651 } 8652 } 8653 mtx_unlock(&lun->lun_lock); 8654 /* Send msg to other side */ 8655 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8656 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8657 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8658 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8659 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8660 printf("CTL:Persis Out error returned from " 8661 "ctl_ha_msg_send %d\n", isc_retval); 8662 } 8663 break; 8664 8665 case SPRO_CLEAR: 8666 /* send msg to other side */ 8667 8668 mtx_lock(&lun->lun_lock); 8669 lun->flags &= ~CTL_LUN_PR_RESERVED; 8670 lun->res_type = 0; 8671 lun->pr_key_count = 0; 8672 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8673 8674 lun->pr_keys[residx] = 0; 8675 8676 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8677 if (lun->pr_keys[i] != 0) { 8678 if (!persis_offset && i < CTL_MAX_INITIATORS) 8679 lun->pending_ua[i] |= 8680 CTL_UA_RES_PREEMPT; 8681 else if (persis_offset && i >= persis_offset) 8682 lun->pending_ua[i-persis_offset] |= 8683 CTL_UA_RES_PREEMPT; 8684 8685 lun->pr_keys[i] = 0; 8686 } 8687 lun->PRGeneration++; 8688 mtx_unlock(&lun->lun_lock); 8689 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8690 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8691 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8692 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8693 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8694 printf("CTL:Persis Out error returned from " 8695 "ctl_ha_msg_send %d\n", isc_retval); 8696 } 8697 break; 8698 8699 case SPRO_PREEMPT: { 8700 int nretval; 8701 8702 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8703 residx, ctsio, cdb, param); 8704 if (nretval != 0) 8705 return (CTL_RETVAL_COMPLETE); 8706 break; 8707 } 8708 default: 8709 panic("Invalid PR type %x", cdb->action); 8710 } 8711 8712 done: 8713 free(ctsio->kern_data_ptr, M_CTL); 8714 ctl_set_success(ctsio); 8715 ctl_done((union ctl_io *)ctsio); 8716 8717 return (retval); 8718 } 8719 8720 /* 8721 * This routine is for handling a message from the other SC pertaining to 8722 * persistent reserve out. All the error checking will have been done 8723 * so only perorming the action need be done here to keep the two 8724 * in sync. 8725 */ 8726 static void 8727 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8728 { 8729 struct ctl_lun *lun; 8730 struct ctl_softc *softc; 8731 int i; 8732 uint32_t targ_lun; 8733 8734 softc = control_softc; 8735 8736 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8737 lun = softc->ctl_luns[targ_lun]; 8738 mtx_lock(&lun->lun_lock); 8739 switch(msg->pr.pr_info.action) { 8740 case CTL_PR_REG_KEY: 8741 if (lun->pr_keys[msg->pr.pr_info.residx] == 0) 8742 lun->pr_key_count++; 8743 lun->pr_keys[msg->pr.pr_info.residx] = 8744 scsi_8btou64(msg->pr.pr_info.sa_res_key); 8745 lun->PRGeneration++; 8746 break; 8747 8748 case CTL_PR_UNREG_KEY: 8749 lun->pr_keys[msg->pr.pr_info.residx] = 0; 8750 lun->pr_key_count--; 8751 8752 /* XXX Need to see if the reservation has been released */ 8753 /* if so do we need to generate UA? */ 8754 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8755 lun->flags &= ~CTL_LUN_PR_RESERVED; 8756 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8757 8758 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8759 || lun->res_type == SPR_TYPE_EX_AC_RO) 8760 && lun->pr_key_count) { 8761 /* 8762 * If the reservation is a registrants 8763 * only type we need to generate a UA 8764 * for other registered inits. The 8765 * sense code should be RESERVATIONS 8766 * RELEASED 8767 */ 8768 8769 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8770 if (lun->pr_keys[i+ 8771 persis_offset] == 0) 8772 continue; 8773 8774 lun->pending_ua[i] |= 8775 CTL_UA_RES_RELEASE; 8776 } 8777 } 8778 lun->res_type = 0; 8779 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8780 if (lun->pr_key_count==0) { 8781 lun->flags &= ~CTL_LUN_PR_RESERVED; 8782 lun->res_type = 0; 8783 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8784 } 8785 } 8786 lun->PRGeneration++; 8787 break; 8788 8789 case CTL_PR_RESERVE: 8790 lun->flags |= CTL_LUN_PR_RESERVED; 8791 lun->res_type = msg->pr.pr_info.res_type; 8792 lun->pr_res_idx = msg->pr.pr_info.residx; 8793 8794 break; 8795 8796 case CTL_PR_RELEASE: 8797 /* 8798 * if this isn't an exclusive access res generate UA for all 8799 * other registrants. 8800 */ 8801 if (lun->res_type != SPR_TYPE_EX_AC 8802 && lun->res_type != SPR_TYPE_WR_EX) { 8803 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8804 if (lun->pr_keys[i+persis_offset] != 0) 8805 lun->pending_ua[i] |= 8806 CTL_UA_RES_RELEASE; 8807 } 8808 8809 lun->flags &= ~CTL_LUN_PR_RESERVED; 8810 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8811 lun->res_type = 0; 8812 break; 8813 8814 case CTL_PR_PREEMPT: 8815 ctl_pro_preempt_other(lun, msg); 8816 break; 8817 case CTL_PR_CLEAR: 8818 lun->flags &= ~CTL_LUN_PR_RESERVED; 8819 lun->res_type = 0; 8820 lun->pr_key_count = 0; 8821 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8822 8823 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8824 if (lun->pr_keys[i] == 0) 8825 continue; 8826 if (!persis_offset 8827 && i < CTL_MAX_INITIATORS) 8828 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 8829 else if (persis_offset 8830 && i >= persis_offset) 8831 lun->pending_ua[i-persis_offset] |= 8832 CTL_UA_RES_PREEMPT; 8833 lun->pr_keys[i] = 0; 8834 } 8835 lun->PRGeneration++; 8836 break; 8837 } 8838 8839 mtx_unlock(&lun->lun_lock); 8840 } 8841 8842 int 8843 ctl_read_write(struct ctl_scsiio *ctsio) 8844 { 8845 struct ctl_lun *lun; 8846 struct ctl_lba_len_flags *lbalen; 8847 uint64_t lba; 8848 uint32_t num_blocks; 8849 int flags, retval; 8850 int isread; 8851 8852 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8853 8854 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8855 8856 flags = 0; 8857 retval = CTL_RETVAL_COMPLETE; 8858 8859 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8860 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8861 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 8862 uint32_t residx; 8863 8864 /* 8865 * XXX KDM need a lock here. 8866 */ 8867 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8868 if ((lun->res_type == SPR_TYPE_EX_AC 8869 && residx != lun->pr_res_idx) 8870 || ((lun->res_type == SPR_TYPE_EX_AC_RO 8871 || lun->res_type == SPR_TYPE_EX_AC_AR) 8872 && lun->pr_keys[residx] == 0)) { 8873 ctl_set_reservation_conflict(ctsio); 8874 ctl_done((union ctl_io *)ctsio); 8875 return (CTL_RETVAL_COMPLETE); 8876 } 8877 } 8878 8879 switch (ctsio->cdb[0]) { 8880 case READ_6: 8881 case WRITE_6: { 8882 struct scsi_rw_6 *cdb; 8883 8884 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8885 8886 lba = scsi_3btoul(cdb->addr); 8887 /* only 5 bits are valid in the most significant address byte */ 8888 lba &= 0x1fffff; 8889 num_blocks = cdb->length; 8890 /* 8891 * This is correct according to SBC-2. 8892 */ 8893 if (num_blocks == 0) 8894 num_blocks = 256; 8895 break; 8896 } 8897 case READ_10: 8898 case WRITE_10: { 8899 struct scsi_rw_10 *cdb; 8900 8901 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8902 if (cdb->byte2 & SRW10_FUA) 8903 flags |= CTL_LLF_FUA; 8904 if (cdb->byte2 & SRW10_DPO) 8905 flags |= CTL_LLF_DPO; 8906 lba = scsi_4btoul(cdb->addr); 8907 num_blocks = scsi_2btoul(cdb->length); 8908 break; 8909 } 8910 case WRITE_VERIFY_10: { 8911 struct scsi_write_verify_10 *cdb; 8912 8913 cdb = (struct scsi_write_verify_10 *)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_2btoul(cdb->length); 8919 break; 8920 } 8921 case READ_12: 8922 case WRITE_12: { 8923 struct scsi_rw_12 *cdb; 8924 8925 cdb = (struct scsi_rw_12 *)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_4btoul(cdb->addr); 8931 num_blocks = scsi_4btoul(cdb->length); 8932 break; 8933 } 8934 case WRITE_VERIFY_12: { 8935 struct scsi_write_verify_12 *cdb; 8936 8937 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8938 flags |= CTL_LLF_FUA; 8939 if (cdb->byte2 & SWV_DPO) 8940 flags |= CTL_LLF_DPO; 8941 lba = scsi_4btoul(cdb->addr); 8942 num_blocks = scsi_4btoul(cdb->length); 8943 break; 8944 } 8945 case READ_16: 8946 case WRITE_16: { 8947 struct scsi_rw_16 *cdb; 8948 8949 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8950 if (cdb->byte2 & SRW12_FUA) 8951 flags |= CTL_LLF_FUA; 8952 if (cdb->byte2 & SRW12_DPO) 8953 flags |= CTL_LLF_DPO; 8954 lba = scsi_8btou64(cdb->addr); 8955 num_blocks = scsi_4btoul(cdb->length); 8956 break; 8957 } 8958 case WRITE_ATOMIC_16: { 8959 struct scsi_rw_16 *cdb; 8960 8961 if (lun->be_lun->atomicblock == 0) { 8962 ctl_set_invalid_opcode(ctsio); 8963 ctl_done((union ctl_io *)ctsio); 8964 return (CTL_RETVAL_COMPLETE); 8965 } 8966 8967 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8968 if (cdb->byte2 & SRW12_FUA) 8969 flags |= CTL_LLF_FUA; 8970 if (cdb->byte2 & SRW12_DPO) 8971 flags |= CTL_LLF_DPO; 8972 lba = scsi_8btou64(cdb->addr); 8973 num_blocks = scsi_4btoul(cdb->length); 8974 if (num_blocks > lun->be_lun->atomicblock) { 8975 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8976 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8977 /*bit*/ 0); 8978 ctl_done((union ctl_io *)ctsio); 8979 return (CTL_RETVAL_COMPLETE); 8980 } 8981 break; 8982 } 8983 case WRITE_VERIFY_16: { 8984 struct scsi_write_verify_16 *cdb; 8985 8986 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8987 flags |= CTL_LLF_FUA; 8988 if (cdb->byte2 & SWV_DPO) 8989 flags |= CTL_LLF_DPO; 8990 lba = scsi_8btou64(cdb->addr); 8991 num_blocks = scsi_4btoul(cdb->length); 8992 break; 8993 } 8994 default: 8995 /* 8996 * We got a command we don't support. This shouldn't 8997 * happen, commands should be filtered out above us. 8998 */ 8999 ctl_set_invalid_opcode(ctsio); 9000 ctl_done((union ctl_io *)ctsio); 9001 9002 return (CTL_RETVAL_COMPLETE); 9003 break; /* NOTREACHED */ 9004 } 9005 9006 /* 9007 * The first check is to make sure we're in bounds, the second 9008 * check is to catch wrap-around problems. If the lba + num blocks 9009 * is less than the lba, then we've wrapped around and the block 9010 * range is invalid anyway. 9011 */ 9012 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9013 || ((lba + num_blocks) < lba)) { 9014 ctl_set_lba_out_of_range(ctsio); 9015 ctl_done((union ctl_io *)ctsio); 9016 return (CTL_RETVAL_COMPLETE); 9017 } 9018 9019 /* 9020 * According to SBC-3, a transfer length of 0 is not an error. 9021 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9022 * translates to 256 blocks for those commands. 9023 */ 9024 if (num_blocks == 0) { 9025 ctl_set_success(ctsio); 9026 ctl_done((union ctl_io *)ctsio); 9027 return (CTL_RETVAL_COMPLETE); 9028 } 9029 9030 /* Set FUA and/or DPO if caches are disabled. */ 9031 if (isread) { 9032 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9033 SCP_RCD) != 0) 9034 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9035 } else { 9036 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9037 SCP_WCE) == 0) 9038 flags |= CTL_LLF_FUA; 9039 } 9040 9041 lbalen = (struct ctl_lba_len_flags *) 9042 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9043 lbalen->lba = lba; 9044 lbalen->len = num_blocks; 9045 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9046 9047 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9048 ctsio->kern_rel_offset = 0; 9049 9050 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9051 9052 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9053 9054 return (retval); 9055 } 9056 9057 static int 9058 ctl_cnw_cont(union ctl_io *io) 9059 { 9060 struct ctl_scsiio *ctsio; 9061 struct ctl_lun *lun; 9062 struct ctl_lba_len_flags *lbalen; 9063 int retval; 9064 9065 ctsio = &io->scsiio; 9066 ctsio->io_hdr.status = CTL_STATUS_NONE; 9067 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9068 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9069 lbalen = (struct ctl_lba_len_flags *) 9070 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9071 lbalen->flags &= ~CTL_LLF_COMPARE; 9072 lbalen->flags |= CTL_LLF_WRITE; 9073 9074 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9075 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9076 return (retval); 9077 } 9078 9079 int 9080 ctl_cnw(struct ctl_scsiio *ctsio) 9081 { 9082 struct ctl_lun *lun; 9083 struct ctl_lba_len_flags *lbalen; 9084 uint64_t lba; 9085 uint32_t num_blocks; 9086 int flags, retval; 9087 9088 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9089 9090 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9091 9092 flags = 0; 9093 retval = CTL_RETVAL_COMPLETE; 9094 9095 switch (ctsio->cdb[0]) { 9096 case COMPARE_AND_WRITE: { 9097 struct scsi_compare_and_write *cdb; 9098 9099 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9100 if (cdb->byte2 & SRW10_FUA) 9101 flags |= CTL_LLF_FUA; 9102 if (cdb->byte2 & SRW10_DPO) 9103 flags |= CTL_LLF_DPO; 9104 lba = scsi_8btou64(cdb->addr); 9105 num_blocks = cdb->length; 9106 break; 9107 } 9108 default: 9109 /* 9110 * We got a command we don't support. This shouldn't 9111 * happen, commands should be filtered out above us. 9112 */ 9113 ctl_set_invalid_opcode(ctsio); 9114 ctl_done((union ctl_io *)ctsio); 9115 9116 return (CTL_RETVAL_COMPLETE); 9117 break; /* NOTREACHED */ 9118 } 9119 9120 /* 9121 * The first check is to make sure we're in bounds, the second 9122 * check is to catch wrap-around problems. If the lba + num blocks 9123 * is less than the lba, then we've wrapped around and the block 9124 * range is invalid anyway. 9125 */ 9126 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9127 || ((lba + num_blocks) < lba)) { 9128 ctl_set_lba_out_of_range(ctsio); 9129 ctl_done((union ctl_io *)ctsio); 9130 return (CTL_RETVAL_COMPLETE); 9131 } 9132 9133 /* 9134 * According to SBC-3, a transfer length of 0 is not an error. 9135 */ 9136 if (num_blocks == 0) { 9137 ctl_set_success(ctsio); 9138 ctl_done((union ctl_io *)ctsio); 9139 return (CTL_RETVAL_COMPLETE); 9140 } 9141 9142 /* Set FUA if write cache is disabled. */ 9143 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9144 SCP_WCE) == 0) 9145 flags |= CTL_LLF_FUA; 9146 9147 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9148 ctsio->kern_rel_offset = 0; 9149 9150 /* 9151 * Set the IO_CONT flag, so that if this I/O gets passed to 9152 * ctl_data_submit_done(), it'll get passed back to 9153 * ctl_ctl_cnw_cont() for further processing. 9154 */ 9155 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9156 ctsio->io_cont = ctl_cnw_cont; 9157 9158 lbalen = (struct ctl_lba_len_flags *) 9159 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9160 lbalen->lba = lba; 9161 lbalen->len = num_blocks; 9162 lbalen->flags = CTL_LLF_COMPARE | flags; 9163 9164 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9165 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9166 return (retval); 9167 } 9168 9169 int 9170 ctl_verify(struct ctl_scsiio *ctsio) 9171 { 9172 struct ctl_lun *lun; 9173 struct ctl_lba_len_flags *lbalen; 9174 uint64_t lba; 9175 uint32_t num_blocks; 9176 int bytchk, flags; 9177 int retval; 9178 9179 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9180 9181 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9182 9183 bytchk = 0; 9184 flags = CTL_LLF_FUA; 9185 retval = CTL_RETVAL_COMPLETE; 9186 9187 switch (ctsio->cdb[0]) { 9188 case VERIFY_10: { 9189 struct scsi_verify_10 *cdb; 9190 9191 cdb = (struct scsi_verify_10 *)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_4btoul(cdb->addr); 9197 num_blocks = scsi_2btoul(cdb->length); 9198 break; 9199 } 9200 case VERIFY_12: { 9201 struct scsi_verify_12 *cdb; 9202 9203 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9204 if (cdb->byte2 & SVFY_BYTCHK) 9205 bytchk = 1; 9206 if (cdb->byte2 & SVFY_DPO) 9207 flags |= CTL_LLF_DPO; 9208 lba = scsi_4btoul(cdb->addr); 9209 num_blocks = scsi_4btoul(cdb->length); 9210 break; 9211 } 9212 case VERIFY_16: { 9213 struct scsi_rw_16 *cdb; 9214 9215 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9216 if (cdb->byte2 & SVFY_BYTCHK) 9217 bytchk = 1; 9218 if (cdb->byte2 & SVFY_DPO) 9219 flags |= CTL_LLF_DPO; 9220 lba = scsi_8btou64(cdb->addr); 9221 num_blocks = scsi_4btoul(cdb->length); 9222 break; 9223 } 9224 default: 9225 /* 9226 * We got a command we don't support. This shouldn't 9227 * happen, commands should be filtered out above us. 9228 */ 9229 ctl_set_invalid_opcode(ctsio); 9230 ctl_done((union ctl_io *)ctsio); 9231 return (CTL_RETVAL_COMPLETE); 9232 } 9233 9234 /* 9235 * The first check is to make sure we're in bounds, the second 9236 * check is to catch wrap-around problems. If the lba + num blocks 9237 * is less than the lba, then we've wrapped around and the block 9238 * range is invalid anyway. 9239 */ 9240 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9241 || ((lba + num_blocks) < lba)) { 9242 ctl_set_lba_out_of_range(ctsio); 9243 ctl_done((union ctl_io *)ctsio); 9244 return (CTL_RETVAL_COMPLETE); 9245 } 9246 9247 /* 9248 * According to SBC-3, a transfer length of 0 is not an error. 9249 */ 9250 if (num_blocks == 0) { 9251 ctl_set_success(ctsio); 9252 ctl_done((union ctl_io *)ctsio); 9253 return (CTL_RETVAL_COMPLETE); 9254 } 9255 9256 lbalen = (struct ctl_lba_len_flags *) 9257 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9258 lbalen->lba = lba; 9259 lbalen->len = num_blocks; 9260 if (bytchk) { 9261 lbalen->flags = CTL_LLF_COMPARE | flags; 9262 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9263 } else { 9264 lbalen->flags = CTL_LLF_VERIFY | flags; 9265 ctsio->kern_total_len = 0; 9266 } 9267 ctsio->kern_rel_offset = 0; 9268 9269 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9270 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9271 return (retval); 9272 } 9273 9274 int 9275 ctl_report_luns(struct ctl_scsiio *ctsio) 9276 { 9277 struct scsi_report_luns *cdb; 9278 struct scsi_report_luns_data *lun_data; 9279 struct ctl_lun *lun, *request_lun; 9280 int num_luns, retval; 9281 uint32_t alloc_len, lun_datalen; 9282 int num_filled, well_known; 9283 uint32_t initidx, targ_lun_id, lun_id; 9284 9285 retval = CTL_RETVAL_COMPLETE; 9286 well_known = 0; 9287 9288 cdb = (struct scsi_report_luns *)ctsio->cdb; 9289 9290 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9291 9292 mtx_lock(&control_softc->ctl_lock); 9293 num_luns = control_softc->num_luns; 9294 mtx_unlock(&control_softc->ctl_lock); 9295 9296 switch (cdb->select_report) { 9297 case RPL_REPORT_DEFAULT: 9298 case RPL_REPORT_ALL: 9299 break; 9300 case RPL_REPORT_WELLKNOWN: 9301 well_known = 1; 9302 num_luns = 0; 9303 break; 9304 default: 9305 ctl_set_invalid_field(ctsio, 9306 /*sks_valid*/ 1, 9307 /*command*/ 1, 9308 /*field*/ 2, 9309 /*bit_valid*/ 0, 9310 /*bit*/ 0); 9311 ctl_done((union ctl_io *)ctsio); 9312 return (retval); 9313 break; /* NOTREACHED */ 9314 } 9315 9316 alloc_len = scsi_4btoul(cdb->length); 9317 /* 9318 * The initiator has to allocate at least 16 bytes for this request, 9319 * so he can at least get the header and the first LUN. Otherwise 9320 * we reject the request (per SPC-3 rev 14, section 6.21). 9321 */ 9322 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9323 sizeof(struct scsi_report_luns_lundata))) { 9324 ctl_set_invalid_field(ctsio, 9325 /*sks_valid*/ 1, 9326 /*command*/ 1, 9327 /*field*/ 6, 9328 /*bit_valid*/ 0, 9329 /*bit*/ 0); 9330 ctl_done((union ctl_io *)ctsio); 9331 return (retval); 9332 } 9333 9334 request_lun = (struct ctl_lun *) 9335 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9336 9337 lun_datalen = sizeof(*lun_data) + 9338 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9339 9340 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9341 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9342 ctsio->kern_sg_entries = 0; 9343 9344 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9345 9346 mtx_lock(&control_softc->ctl_lock); 9347 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9348 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9349 if (lun_id >= CTL_MAX_LUNS) 9350 continue; 9351 lun = control_softc->ctl_luns[lun_id]; 9352 if (lun == NULL) 9353 continue; 9354 9355 if (targ_lun_id <= 0xff) { 9356 /* 9357 * Peripheral addressing method, bus number 0. 9358 */ 9359 lun_data->luns[num_filled].lundata[0] = 9360 RPL_LUNDATA_ATYP_PERIPH; 9361 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9362 num_filled++; 9363 } else if (targ_lun_id <= 0x3fff) { 9364 /* 9365 * Flat addressing method. 9366 */ 9367 lun_data->luns[num_filled].lundata[0] = 9368 RPL_LUNDATA_ATYP_FLAT | 9369 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9370 #ifdef OLDCTLHEADERS 9371 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9372 (targ_lun_id & SRLD_BUS_LUN_MASK); 9373 #endif 9374 lun_data->luns[num_filled].lundata[1] = 9375 #ifdef OLDCTLHEADERS 9376 targ_lun_id >> SRLD_BUS_LUN_BITS; 9377 #endif 9378 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9379 num_filled++; 9380 } else { 9381 printf("ctl_report_luns: bogus LUN number %jd, " 9382 "skipping\n", (intmax_t)targ_lun_id); 9383 } 9384 /* 9385 * According to SPC-3, rev 14 section 6.21: 9386 * 9387 * "The execution of a REPORT LUNS command to any valid and 9388 * installed logical unit shall clear the REPORTED LUNS DATA 9389 * HAS CHANGED unit attention condition for all logical 9390 * units of that target with respect to the requesting 9391 * initiator. A valid and installed logical unit is one 9392 * having a PERIPHERAL QUALIFIER of 000b in the standard 9393 * INQUIRY data (see 6.4.2)." 9394 * 9395 * If request_lun is NULL, the LUN this report luns command 9396 * was issued to is either disabled or doesn't exist. In that 9397 * case, we shouldn't clear any pending lun change unit 9398 * attention. 9399 */ 9400 if (request_lun != NULL) { 9401 mtx_lock(&lun->lun_lock); 9402 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9403 mtx_unlock(&lun->lun_lock); 9404 } 9405 } 9406 mtx_unlock(&control_softc->ctl_lock); 9407 9408 /* 9409 * It's quite possible that we've returned fewer LUNs than we allocated 9410 * space for. Trim it. 9411 */ 9412 lun_datalen = sizeof(*lun_data) + 9413 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9414 9415 if (lun_datalen < alloc_len) { 9416 ctsio->residual = alloc_len - lun_datalen; 9417 ctsio->kern_data_len = lun_datalen; 9418 ctsio->kern_total_len = lun_datalen; 9419 } else { 9420 ctsio->residual = 0; 9421 ctsio->kern_data_len = alloc_len; 9422 ctsio->kern_total_len = alloc_len; 9423 } 9424 ctsio->kern_data_resid = 0; 9425 ctsio->kern_rel_offset = 0; 9426 ctsio->kern_sg_entries = 0; 9427 9428 /* 9429 * We set this to the actual data length, regardless of how much 9430 * space we actually have to return results. If the user looks at 9431 * this value, he'll know whether or not he allocated enough space 9432 * and reissue the command if necessary. We don't support well 9433 * known logical units, so if the user asks for that, return none. 9434 */ 9435 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9436 9437 /* 9438 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9439 * this request. 9440 */ 9441 ctsio->scsi_status = SCSI_STATUS_OK; 9442 9443 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9444 ctsio->be_move_done = ctl_config_move_done; 9445 ctl_datamove((union ctl_io *)ctsio); 9446 9447 return (retval); 9448 } 9449 9450 int 9451 ctl_request_sense(struct ctl_scsiio *ctsio) 9452 { 9453 struct scsi_request_sense *cdb; 9454 struct scsi_sense_data *sense_ptr; 9455 struct ctl_lun *lun; 9456 uint32_t initidx; 9457 int have_error; 9458 scsi_sense_data_type sense_format; 9459 9460 cdb = (struct scsi_request_sense *)ctsio->cdb; 9461 9462 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9463 9464 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9465 9466 /* 9467 * Determine which sense format the user wants. 9468 */ 9469 if (cdb->byte2 & SRS_DESC) 9470 sense_format = SSD_TYPE_DESC; 9471 else 9472 sense_format = SSD_TYPE_FIXED; 9473 9474 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9475 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9476 ctsio->kern_sg_entries = 0; 9477 9478 /* 9479 * struct scsi_sense_data, which is currently set to 256 bytes, is 9480 * larger than the largest allowed value for the length field in the 9481 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9482 */ 9483 ctsio->residual = 0; 9484 ctsio->kern_data_len = cdb->length; 9485 ctsio->kern_total_len = cdb->length; 9486 9487 ctsio->kern_data_resid = 0; 9488 ctsio->kern_rel_offset = 0; 9489 ctsio->kern_sg_entries = 0; 9490 9491 /* 9492 * If we don't have a LUN, we don't have any pending sense. 9493 */ 9494 if (lun == NULL) 9495 goto no_sense; 9496 9497 have_error = 0; 9498 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9499 /* 9500 * Check for pending sense, and then for pending unit attentions. 9501 * Pending sense gets returned first, then pending unit attentions. 9502 */ 9503 mtx_lock(&lun->lun_lock); 9504 #ifdef CTL_WITH_CA 9505 if (ctl_is_set(lun->have_ca, initidx)) { 9506 scsi_sense_data_type stored_format; 9507 9508 /* 9509 * Check to see which sense format was used for the stored 9510 * sense data. 9511 */ 9512 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9513 9514 /* 9515 * If the user requested a different sense format than the 9516 * one we stored, then we need to convert it to the other 9517 * format. If we're going from descriptor to fixed format 9518 * sense data, we may lose things in translation, depending 9519 * on what options were used. 9520 * 9521 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9522 * for some reason we'll just copy it out as-is. 9523 */ 9524 if ((stored_format == SSD_TYPE_FIXED) 9525 && (sense_format == SSD_TYPE_DESC)) 9526 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9527 &lun->pending_sense[initidx], 9528 (struct scsi_sense_data_desc *)sense_ptr); 9529 else if ((stored_format == SSD_TYPE_DESC) 9530 && (sense_format == SSD_TYPE_FIXED)) 9531 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9532 &lun->pending_sense[initidx], 9533 (struct scsi_sense_data_fixed *)sense_ptr); 9534 else 9535 memcpy(sense_ptr, &lun->pending_sense[initidx], 9536 ctl_min(sizeof(*sense_ptr), 9537 sizeof(lun->pending_sense[initidx]))); 9538 9539 ctl_clear_mask(lun->have_ca, initidx); 9540 have_error = 1; 9541 } else 9542 #endif 9543 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9544 ctl_ua_type ua_type; 9545 9546 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9547 sense_ptr, sense_format); 9548 if (ua_type != CTL_UA_NONE) 9549 have_error = 1; 9550 } 9551 mtx_unlock(&lun->lun_lock); 9552 9553 /* 9554 * We already have a pending error, return it. 9555 */ 9556 if (have_error != 0) { 9557 /* 9558 * We report the SCSI status as OK, since the status of the 9559 * request sense command itself is OK. 9560 */ 9561 ctsio->scsi_status = SCSI_STATUS_OK; 9562 9563 /* 9564 * We report 0 for the sense length, because we aren't doing 9565 * autosense in this case. We're reporting sense as 9566 * parameter data. 9567 */ 9568 ctsio->sense_len = 0; 9569 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9570 ctsio->be_move_done = ctl_config_move_done; 9571 ctl_datamove((union ctl_io *)ctsio); 9572 9573 return (CTL_RETVAL_COMPLETE); 9574 } 9575 9576 no_sense: 9577 9578 /* 9579 * No sense information to report, so we report that everything is 9580 * okay. 9581 */ 9582 ctl_set_sense_data(sense_ptr, 9583 lun, 9584 sense_format, 9585 /*current_error*/ 1, 9586 /*sense_key*/ SSD_KEY_NO_SENSE, 9587 /*asc*/ 0x00, 9588 /*ascq*/ 0x00, 9589 SSD_ELEM_NONE); 9590 9591 ctsio->scsi_status = SCSI_STATUS_OK; 9592 9593 /* 9594 * We report 0 for the sense length, because we aren't doing 9595 * autosense in this case. We're reporting sense as parameter data. 9596 */ 9597 ctsio->sense_len = 0; 9598 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9599 ctsio->be_move_done = ctl_config_move_done; 9600 ctl_datamove((union ctl_io *)ctsio); 9601 9602 return (CTL_RETVAL_COMPLETE); 9603 } 9604 9605 int 9606 ctl_tur(struct ctl_scsiio *ctsio) 9607 { 9608 struct ctl_lun *lun; 9609 9610 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9611 9612 CTL_DEBUG_PRINT(("ctl_tur\n")); 9613 9614 if (lun == NULL) 9615 return (EINVAL); 9616 9617 ctsio->scsi_status = SCSI_STATUS_OK; 9618 ctsio->io_hdr.status = CTL_SUCCESS; 9619 9620 ctl_done((union ctl_io *)ctsio); 9621 9622 return (CTL_RETVAL_COMPLETE); 9623 } 9624 9625 #ifdef notyet 9626 static int 9627 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9628 { 9629 9630 } 9631 #endif 9632 9633 static int 9634 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9635 { 9636 struct scsi_vpd_supported_pages *pages; 9637 int sup_page_size; 9638 struct ctl_lun *lun; 9639 9640 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9641 9642 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9643 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9644 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9645 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9646 ctsio->kern_sg_entries = 0; 9647 9648 if (sup_page_size < alloc_len) { 9649 ctsio->residual = alloc_len - sup_page_size; 9650 ctsio->kern_data_len = sup_page_size; 9651 ctsio->kern_total_len = sup_page_size; 9652 } else { 9653 ctsio->residual = 0; 9654 ctsio->kern_data_len = alloc_len; 9655 ctsio->kern_total_len = alloc_len; 9656 } 9657 ctsio->kern_data_resid = 0; 9658 ctsio->kern_rel_offset = 0; 9659 ctsio->kern_sg_entries = 0; 9660 9661 /* 9662 * The control device is always connected. The disk device, on the 9663 * other hand, may not be online all the time. Need to change this 9664 * to figure out whether the disk device is actually online or not. 9665 */ 9666 if (lun != NULL) 9667 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9668 lun->be_lun->lun_type; 9669 else 9670 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9671 9672 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9673 /* Supported VPD pages */ 9674 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9675 /* Serial Number */ 9676 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9677 /* Device Identification */ 9678 pages->page_list[2] = SVPD_DEVICE_ID; 9679 /* Extended INQUIRY Data */ 9680 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9681 /* Mode Page Policy */ 9682 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9683 /* SCSI Ports */ 9684 pages->page_list[5] = SVPD_SCSI_PORTS; 9685 /* Third-party Copy */ 9686 pages->page_list[6] = SVPD_SCSI_TPC; 9687 /* Block limits */ 9688 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9689 /* Block Device Characteristics */ 9690 pages->page_list[8] = SVPD_BDC; 9691 /* Logical Block Provisioning */ 9692 pages->page_list[9] = SVPD_LBP; 9693 9694 ctsio->scsi_status = SCSI_STATUS_OK; 9695 9696 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9697 ctsio->be_move_done = ctl_config_move_done; 9698 ctl_datamove((union ctl_io *)ctsio); 9699 9700 return (CTL_RETVAL_COMPLETE); 9701 } 9702 9703 static int 9704 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9705 { 9706 struct scsi_vpd_unit_serial_number *sn_ptr; 9707 struct ctl_lun *lun; 9708 int data_len; 9709 9710 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9711 9712 data_len = 4 + CTL_SN_LEN; 9713 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9714 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9715 if (data_len < alloc_len) { 9716 ctsio->residual = alloc_len - data_len; 9717 ctsio->kern_data_len = data_len; 9718 ctsio->kern_total_len = data_len; 9719 } else { 9720 ctsio->residual = 0; 9721 ctsio->kern_data_len = alloc_len; 9722 ctsio->kern_total_len = alloc_len; 9723 } 9724 ctsio->kern_data_resid = 0; 9725 ctsio->kern_rel_offset = 0; 9726 ctsio->kern_sg_entries = 0; 9727 9728 /* 9729 * The control device is always connected. The disk device, on the 9730 * other hand, may not be online all the time. Need to change this 9731 * to figure out whether the disk device is actually online or not. 9732 */ 9733 if (lun != NULL) 9734 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9735 lun->be_lun->lun_type; 9736 else 9737 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9738 9739 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9740 sn_ptr->length = CTL_SN_LEN; 9741 /* 9742 * If we don't have a LUN, we just leave the serial number as 9743 * all spaces. 9744 */ 9745 if (lun != NULL) { 9746 strncpy((char *)sn_ptr->serial_num, 9747 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9748 } else 9749 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9750 ctsio->scsi_status = SCSI_STATUS_OK; 9751 9752 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9753 ctsio->be_move_done = ctl_config_move_done; 9754 ctl_datamove((union ctl_io *)ctsio); 9755 9756 return (CTL_RETVAL_COMPLETE); 9757 } 9758 9759 9760 static int 9761 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9762 { 9763 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9764 struct ctl_lun *lun; 9765 int data_len; 9766 9767 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9768 9769 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9770 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9771 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9772 ctsio->kern_sg_entries = 0; 9773 9774 if (data_len < alloc_len) { 9775 ctsio->residual = alloc_len - data_len; 9776 ctsio->kern_data_len = data_len; 9777 ctsio->kern_total_len = data_len; 9778 } else { 9779 ctsio->residual = 0; 9780 ctsio->kern_data_len = alloc_len; 9781 ctsio->kern_total_len = alloc_len; 9782 } 9783 ctsio->kern_data_resid = 0; 9784 ctsio->kern_rel_offset = 0; 9785 ctsio->kern_sg_entries = 0; 9786 9787 /* 9788 * The control device is always connected. The disk device, on the 9789 * other hand, may not be online all the time. 9790 */ 9791 if (lun != NULL) 9792 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9793 lun->be_lun->lun_type; 9794 else 9795 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9796 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9797 eid_ptr->page_length = data_len - 4; 9798 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9799 eid_ptr->flags3 = SVPD_EID_V_SUP; 9800 9801 ctsio->scsi_status = SCSI_STATUS_OK; 9802 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9803 ctsio->be_move_done = ctl_config_move_done; 9804 ctl_datamove((union ctl_io *)ctsio); 9805 9806 return (CTL_RETVAL_COMPLETE); 9807 } 9808 9809 static int 9810 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9811 { 9812 struct scsi_vpd_mode_page_policy *mpp_ptr; 9813 struct ctl_lun *lun; 9814 int data_len; 9815 9816 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9817 9818 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9819 sizeof(struct scsi_vpd_mode_page_policy_descr); 9820 9821 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9822 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9823 ctsio->kern_sg_entries = 0; 9824 9825 if (data_len < alloc_len) { 9826 ctsio->residual = alloc_len - data_len; 9827 ctsio->kern_data_len = data_len; 9828 ctsio->kern_total_len = data_len; 9829 } else { 9830 ctsio->residual = 0; 9831 ctsio->kern_data_len = alloc_len; 9832 ctsio->kern_total_len = alloc_len; 9833 } 9834 ctsio->kern_data_resid = 0; 9835 ctsio->kern_rel_offset = 0; 9836 ctsio->kern_sg_entries = 0; 9837 9838 /* 9839 * The control device is always connected. The disk device, on the 9840 * other hand, may not be online all the time. 9841 */ 9842 if (lun != NULL) 9843 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9844 lun->be_lun->lun_type; 9845 else 9846 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9847 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9848 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9849 mpp_ptr->descr[0].page_code = 0x3f; 9850 mpp_ptr->descr[0].subpage_code = 0xff; 9851 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9852 9853 ctsio->scsi_status = SCSI_STATUS_OK; 9854 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9855 ctsio->be_move_done = ctl_config_move_done; 9856 ctl_datamove((union ctl_io *)ctsio); 9857 9858 return (CTL_RETVAL_COMPLETE); 9859 } 9860 9861 static int 9862 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9863 { 9864 struct scsi_vpd_device_id *devid_ptr; 9865 struct scsi_vpd_id_descriptor *desc; 9866 struct ctl_softc *ctl_softc; 9867 struct ctl_lun *lun; 9868 struct ctl_port *port; 9869 int data_len; 9870 uint8_t proto; 9871 9872 ctl_softc = control_softc; 9873 9874 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9875 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9876 9877 data_len = sizeof(struct scsi_vpd_device_id) + 9878 sizeof(struct scsi_vpd_id_descriptor) + 9879 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9880 sizeof(struct scsi_vpd_id_descriptor) + 9881 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9882 if (lun && lun->lun_devid) 9883 data_len += lun->lun_devid->len; 9884 if (port->port_devid) 9885 data_len += port->port_devid->len; 9886 if (port->target_devid) 9887 data_len += port->target_devid->len; 9888 9889 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9890 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9891 ctsio->kern_sg_entries = 0; 9892 9893 if (data_len < alloc_len) { 9894 ctsio->residual = alloc_len - data_len; 9895 ctsio->kern_data_len = data_len; 9896 ctsio->kern_total_len = data_len; 9897 } else { 9898 ctsio->residual = 0; 9899 ctsio->kern_data_len = alloc_len; 9900 ctsio->kern_total_len = alloc_len; 9901 } 9902 ctsio->kern_data_resid = 0; 9903 ctsio->kern_rel_offset = 0; 9904 ctsio->kern_sg_entries = 0; 9905 9906 /* 9907 * The control device is always connected. The disk device, on the 9908 * other hand, may not be online all the time. 9909 */ 9910 if (lun != NULL) 9911 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9912 lun->be_lun->lun_type; 9913 else 9914 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9915 devid_ptr->page_code = SVPD_DEVICE_ID; 9916 scsi_ulto2b(data_len - 4, devid_ptr->length); 9917 9918 if (port->port_type == CTL_PORT_FC) 9919 proto = SCSI_PROTO_FC << 4; 9920 else if (port->port_type == CTL_PORT_ISCSI) 9921 proto = SCSI_PROTO_ISCSI << 4; 9922 else 9923 proto = SCSI_PROTO_SPI << 4; 9924 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9925 9926 /* 9927 * We're using a LUN association here. i.e., this device ID is a 9928 * per-LUN identifier. 9929 */ 9930 if (lun && lun->lun_devid) { 9931 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9932 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9933 lun->lun_devid->len); 9934 } 9935 9936 /* 9937 * This is for the WWPN which is a port association. 9938 */ 9939 if (port->port_devid) { 9940 memcpy(desc, port->port_devid->data, port->port_devid->len); 9941 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9942 port->port_devid->len); 9943 } 9944 9945 /* 9946 * This is for the Relative Target Port(type 4h) identifier 9947 */ 9948 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9949 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9950 SVPD_ID_TYPE_RELTARG; 9951 desc->length = 4; 9952 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9953 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9954 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9955 9956 /* 9957 * This is for the Target Port Group(type 5h) identifier 9958 */ 9959 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9960 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9961 SVPD_ID_TYPE_TPORTGRP; 9962 desc->length = 4; 9963 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9964 &desc->identifier[2]); 9965 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9966 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9967 9968 /* 9969 * This is for the Target identifier 9970 */ 9971 if (port->target_devid) { 9972 memcpy(desc, port->target_devid->data, port->target_devid->len); 9973 } 9974 9975 ctsio->scsi_status = SCSI_STATUS_OK; 9976 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9977 ctsio->be_move_done = ctl_config_move_done; 9978 ctl_datamove((union ctl_io *)ctsio); 9979 9980 return (CTL_RETVAL_COMPLETE); 9981 } 9982 9983 static int 9984 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9985 { 9986 struct ctl_softc *softc = control_softc; 9987 struct scsi_vpd_scsi_ports *sp; 9988 struct scsi_vpd_port_designation *pd; 9989 struct scsi_vpd_port_designation_cont *pdc; 9990 struct ctl_lun *lun; 9991 struct ctl_port *port; 9992 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 9993 int num_target_port_groups, single; 9994 9995 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9996 9997 single = ctl_is_single; 9998 if (single) 9999 num_target_port_groups = 1; 10000 else 10001 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10002 num_target_ports = 0; 10003 iid_len = 0; 10004 id_len = 0; 10005 mtx_lock(&softc->ctl_lock); 10006 STAILQ_FOREACH(port, &softc->port_list, links) { 10007 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10008 continue; 10009 if (lun != NULL && 10010 ctl_map_lun_back(port->targ_port, lun->lun) >= 10011 CTL_MAX_LUNS) 10012 continue; 10013 num_target_ports++; 10014 if (port->init_devid) 10015 iid_len += port->init_devid->len; 10016 if (port->port_devid) 10017 id_len += port->port_devid->len; 10018 } 10019 mtx_unlock(&softc->ctl_lock); 10020 10021 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10022 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10023 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10024 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10025 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10026 ctsio->kern_sg_entries = 0; 10027 10028 if (data_len < alloc_len) { 10029 ctsio->residual = alloc_len - data_len; 10030 ctsio->kern_data_len = data_len; 10031 ctsio->kern_total_len = data_len; 10032 } else { 10033 ctsio->residual = 0; 10034 ctsio->kern_data_len = alloc_len; 10035 ctsio->kern_total_len = alloc_len; 10036 } 10037 ctsio->kern_data_resid = 0; 10038 ctsio->kern_rel_offset = 0; 10039 ctsio->kern_sg_entries = 0; 10040 10041 /* 10042 * The control device is always connected. The disk device, on the 10043 * other hand, may not be online all the time. Need to change this 10044 * to figure out whether the disk device is actually online or not. 10045 */ 10046 if (lun != NULL) 10047 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10048 lun->be_lun->lun_type; 10049 else 10050 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10051 10052 sp->page_code = SVPD_SCSI_PORTS; 10053 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10054 sp->page_length); 10055 pd = &sp->design[0]; 10056 10057 mtx_lock(&softc->ctl_lock); 10058 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10059 pg = 0; 10060 else 10061 pg = 1; 10062 for (g = 0; g < num_target_port_groups; g++) { 10063 STAILQ_FOREACH(port, &softc->port_list, links) { 10064 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10065 continue; 10066 if (lun != NULL && 10067 ctl_map_lun_back(port->targ_port, lun->lun) >= 10068 CTL_MAX_LUNS) 10069 continue; 10070 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10071 scsi_ulto2b(p, pd->relative_port_id); 10072 if (port->init_devid && g == pg) { 10073 iid_len = port->init_devid->len; 10074 memcpy(pd->initiator_transportid, 10075 port->init_devid->data, port->init_devid->len); 10076 } else 10077 iid_len = 0; 10078 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10079 pdc = (struct scsi_vpd_port_designation_cont *) 10080 (&pd->initiator_transportid[iid_len]); 10081 if (port->port_devid && g == pg) { 10082 id_len = port->port_devid->len; 10083 memcpy(pdc->target_port_descriptors, 10084 port->port_devid->data, port->port_devid->len); 10085 } else 10086 id_len = 0; 10087 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10088 pd = (struct scsi_vpd_port_designation *) 10089 ((uint8_t *)pdc->target_port_descriptors + id_len); 10090 } 10091 } 10092 mtx_unlock(&softc->ctl_lock); 10093 10094 ctsio->scsi_status = SCSI_STATUS_OK; 10095 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10096 ctsio->be_move_done = ctl_config_move_done; 10097 ctl_datamove((union ctl_io *)ctsio); 10098 10099 return (CTL_RETVAL_COMPLETE); 10100 } 10101 10102 static int 10103 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10104 { 10105 struct scsi_vpd_block_limits *bl_ptr; 10106 struct ctl_lun *lun; 10107 int bs; 10108 10109 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10110 10111 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10112 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10113 ctsio->kern_sg_entries = 0; 10114 10115 if (sizeof(*bl_ptr) < alloc_len) { 10116 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10117 ctsio->kern_data_len = sizeof(*bl_ptr); 10118 ctsio->kern_total_len = sizeof(*bl_ptr); 10119 } else { 10120 ctsio->residual = 0; 10121 ctsio->kern_data_len = alloc_len; 10122 ctsio->kern_total_len = alloc_len; 10123 } 10124 ctsio->kern_data_resid = 0; 10125 ctsio->kern_rel_offset = 0; 10126 ctsio->kern_sg_entries = 0; 10127 10128 /* 10129 * The control device is always connected. The disk device, on the 10130 * other hand, may not be online all the time. Need to change this 10131 * to figure out whether the disk device is actually online or not. 10132 */ 10133 if (lun != NULL) 10134 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10135 lun->be_lun->lun_type; 10136 else 10137 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10138 10139 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10140 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10141 bl_ptr->max_cmp_write_len = 0xff; 10142 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10143 if (lun != NULL) { 10144 bs = lun->be_lun->blocksize; 10145 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10146 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10147 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10148 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10149 if (lun->be_lun->pblockexp != 0) { 10150 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10151 bl_ptr->opt_unmap_grain); 10152 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10153 bl_ptr->unmap_grain_align); 10154 } 10155 } 10156 scsi_ulto4b(lun->be_lun->atomicblock, 10157 bl_ptr->max_atomic_transfer_length); 10158 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10159 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10160 } 10161 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10162 10163 ctsio->scsi_status = SCSI_STATUS_OK; 10164 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10165 ctsio->be_move_done = ctl_config_move_done; 10166 ctl_datamove((union ctl_io *)ctsio); 10167 10168 return (CTL_RETVAL_COMPLETE); 10169 } 10170 10171 static int 10172 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10173 { 10174 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10175 struct ctl_lun *lun; 10176 10177 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10178 10179 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10180 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10181 ctsio->kern_sg_entries = 0; 10182 10183 if (sizeof(*bdc_ptr) < alloc_len) { 10184 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10185 ctsio->kern_data_len = sizeof(*bdc_ptr); 10186 ctsio->kern_total_len = sizeof(*bdc_ptr); 10187 } else { 10188 ctsio->residual = 0; 10189 ctsio->kern_data_len = alloc_len; 10190 ctsio->kern_total_len = alloc_len; 10191 } 10192 ctsio->kern_data_resid = 0; 10193 ctsio->kern_rel_offset = 0; 10194 ctsio->kern_sg_entries = 0; 10195 10196 /* 10197 * The control device is always connected. The disk device, on the 10198 * other hand, may not be online all the time. Need to change this 10199 * to figure out whether the disk device is actually online or not. 10200 */ 10201 if (lun != NULL) 10202 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10203 lun->be_lun->lun_type; 10204 else 10205 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10206 bdc_ptr->page_code = SVPD_BDC; 10207 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10208 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate); 10209 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10210 10211 ctsio->scsi_status = SCSI_STATUS_OK; 10212 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10213 ctsio->be_move_done = ctl_config_move_done; 10214 ctl_datamove((union ctl_io *)ctsio); 10215 10216 return (CTL_RETVAL_COMPLETE); 10217 } 10218 10219 static int 10220 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10221 { 10222 struct scsi_vpd_logical_block_prov *lbp_ptr; 10223 struct ctl_lun *lun; 10224 10225 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10226 10227 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10228 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10229 ctsio->kern_sg_entries = 0; 10230 10231 if (sizeof(*lbp_ptr) < alloc_len) { 10232 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10233 ctsio->kern_data_len = sizeof(*lbp_ptr); 10234 ctsio->kern_total_len = sizeof(*lbp_ptr); 10235 } else { 10236 ctsio->residual = 0; 10237 ctsio->kern_data_len = alloc_len; 10238 ctsio->kern_total_len = alloc_len; 10239 } 10240 ctsio->kern_data_resid = 0; 10241 ctsio->kern_rel_offset = 0; 10242 ctsio->kern_sg_entries = 0; 10243 10244 /* 10245 * The control device is always connected. The disk device, on the 10246 * other hand, may not be online all the time. Need to change this 10247 * to figure out whether the disk device is actually online or not. 10248 */ 10249 if (lun != NULL) 10250 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10251 lun->be_lun->lun_type; 10252 else 10253 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10254 10255 lbp_ptr->page_code = SVPD_LBP; 10256 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10257 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10258 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10259 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10260 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10261 } 10262 10263 ctsio->scsi_status = SCSI_STATUS_OK; 10264 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10265 ctsio->be_move_done = ctl_config_move_done; 10266 ctl_datamove((union ctl_io *)ctsio); 10267 10268 return (CTL_RETVAL_COMPLETE); 10269 } 10270 10271 static int 10272 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10273 { 10274 struct scsi_inquiry *cdb; 10275 struct ctl_lun *lun; 10276 int alloc_len, retval; 10277 10278 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10279 cdb = (struct scsi_inquiry *)ctsio->cdb; 10280 10281 retval = CTL_RETVAL_COMPLETE; 10282 10283 alloc_len = scsi_2btoul(cdb->length); 10284 10285 switch (cdb->page_code) { 10286 case SVPD_SUPPORTED_PAGES: 10287 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10288 break; 10289 case SVPD_UNIT_SERIAL_NUMBER: 10290 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10291 break; 10292 case SVPD_DEVICE_ID: 10293 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10294 break; 10295 case SVPD_EXTENDED_INQUIRY_DATA: 10296 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10297 break; 10298 case SVPD_MODE_PAGE_POLICY: 10299 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10300 break; 10301 case SVPD_SCSI_PORTS: 10302 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10303 break; 10304 case SVPD_SCSI_TPC: 10305 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10306 break; 10307 case SVPD_BLOCK_LIMITS: 10308 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10309 break; 10310 case SVPD_BDC: 10311 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10312 break; 10313 case SVPD_LBP: 10314 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10315 break; 10316 default: 10317 ctl_set_invalid_field(ctsio, 10318 /*sks_valid*/ 1, 10319 /*command*/ 1, 10320 /*field*/ 2, 10321 /*bit_valid*/ 0, 10322 /*bit*/ 0); 10323 ctl_done((union ctl_io *)ctsio); 10324 retval = CTL_RETVAL_COMPLETE; 10325 break; 10326 } 10327 10328 return (retval); 10329 } 10330 10331 static int 10332 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10333 { 10334 struct scsi_inquiry_data *inq_ptr; 10335 struct scsi_inquiry *cdb; 10336 struct ctl_softc *ctl_softc; 10337 struct ctl_lun *lun; 10338 char *val; 10339 uint32_t alloc_len, data_len; 10340 ctl_port_type port_type; 10341 10342 ctl_softc = control_softc; 10343 10344 /* 10345 * Figure out whether we're talking to a Fibre Channel port or not. 10346 * We treat the ioctl front end, and any SCSI adapters, as packetized 10347 * SCSI front ends. 10348 */ 10349 port_type = ctl_softc->ctl_ports[ 10350 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10351 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10352 port_type = CTL_PORT_SCSI; 10353 10354 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10355 cdb = (struct scsi_inquiry *)ctsio->cdb; 10356 alloc_len = scsi_2btoul(cdb->length); 10357 10358 /* 10359 * We malloc the full inquiry data size here and fill it 10360 * in. If the user only asks for less, we'll give him 10361 * that much. 10362 */ 10363 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10364 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10365 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10366 ctsio->kern_sg_entries = 0; 10367 ctsio->kern_data_resid = 0; 10368 ctsio->kern_rel_offset = 0; 10369 10370 if (data_len < alloc_len) { 10371 ctsio->residual = alloc_len - data_len; 10372 ctsio->kern_data_len = data_len; 10373 ctsio->kern_total_len = data_len; 10374 } else { 10375 ctsio->residual = 0; 10376 ctsio->kern_data_len = alloc_len; 10377 ctsio->kern_total_len = alloc_len; 10378 } 10379 10380 /* 10381 * If we have a LUN configured, report it as connected. Otherwise, 10382 * report that it is offline or no device is supported, depending 10383 * on the value of inquiry_pq_no_lun. 10384 * 10385 * According to the spec (SPC-4 r34), the peripheral qualifier 10386 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10387 * 10388 * "A peripheral device having the specified peripheral device type 10389 * is not connected to this logical unit. However, the device 10390 * server is capable of supporting the specified peripheral device 10391 * type on this logical unit." 10392 * 10393 * According to the same spec, the peripheral qualifier 10394 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10395 * 10396 * "The device server is not capable of supporting a peripheral 10397 * device on this logical unit. For this peripheral qualifier the 10398 * peripheral device type shall be set to 1Fh. All other peripheral 10399 * device type values are reserved for this peripheral qualifier." 10400 * 10401 * Given the text, it would seem that we probably want to report that 10402 * the LUN is offline here. There is no LUN connected, but we can 10403 * support a LUN at the given LUN number. 10404 * 10405 * In the real world, though, it sounds like things are a little 10406 * different: 10407 * 10408 * - Linux, when presented with a LUN with the offline peripheral 10409 * qualifier, will create an sg driver instance for it. So when 10410 * you attach it to CTL, you wind up with a ton of sg driver 10411 * instances. (One for every LUN that Linux bothered to probe.) 10412 * Linux does this despite the fact that it issues a REPORT LUNs 10413 * to LUN 0 to get the inventory of supported LUNs. 10414 * 10415 * - There is other anecdotal evidence (from Emulex folks) about 10416 * arrays that use the offline peripheral qualifier for LUNs that 10417 * are on the "passive" path in an active/passive array. 10418 * 10419 * So the solution is provide a hopefully reasonable default 10420 * (return bad/no LUN) and allow the user to change the behavior 10421 * with a tunable/sysctl variable. 10422 */ 10423 if (lun != NULL) 10424 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10425 lun->be_lun->lun_type; 10426 else if (ctl_softc->inquiry_pq_no_lun == 0) 10427 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10428 else 10429 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10430 10431 /* RMB in byte 2 is 0 */ 10432 inq_ptr->version = SCSI_REV_SPC4; 10433 10434 /* 10435 * According to SAM-3, even if a device only supports a single 10436 * level of LUN addressing, it should still set the HISUP bit: 10437 * 10438 * 4.9.1 Logical unit numbers overview 10439 * 10440 * All logical unit number formats described in this standard are 10441 * hierarchical in structure even when only a single level in that 10442 * hierarchy is used. The HISUP bit shall be set to one in the 10443 * standard INQUIRY data (see SPC-2) when any logical unit number 10444 * format described in this standard is used. Non-hierarchical 10445 * formats are outside the scope of this standard. 10446 * 10447 * Therefore we set the HiSup bit here. 10448 * 10449 * The reponse format is 2, per SPC-3. 10450 */ 10451 inq_ptr->response_format = SID_HiSup | 2; 10452 10453 inq_ptr->additional_length = data_len - 10454 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10455 CTL_DEBUG_PRINT(("additional_length = %d\n", 10456 inq_ptr->additional_length)); 10457 10458 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10459 /* 16 bit addressing */ 10460 if (port_type == CTL_PORT_SCSI) 10461 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10462 /* XXX set the SID_MultiP bit here if we're actually going to 10463 respond on multiple ports */ 10464 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10465 10466 /* 16 bit data bus, synchronous transfers */ 10467 if (port_type == CTL_PORT_SCSI) 10468 inq_ptr->flags = SID_WBus16 | SID_Sync; 10469 /* 10470 * XXX KDM do we want to support tagged queueing on the control 10471 * device at all? 10472 */ 10473 if ((lun == NULL) 10474 || (lun->be_lun->lun_type != T_PROCESSOR)) 10475 inq_ptr->flags |= SID_CmdQue; 10476 /* 10477 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10478 * We have 8 bytes for the vendor name, and 16 bytes for the device 10479 * name and 4 bytes for the revision. 10480 */ 10481 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10482 "vendor")) == NULL) { 10483 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10484 } else { 10485 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10486 strncpy(inq_ptr->vendor, val, 10487 min(sizeof(inq_ptr->vendor), strlen(val))); 10488 } 10489 if (lun == NULL) { 10490 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10491 sizeof(inq_ptr->product)); 10492 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10493 switch (lun->be_lun->lun_type) { 10494 case T_DIRECT: 10495 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10496 sizeof(inq_ptr->product)); 10497 break; 10498 case T_PROCESSOR: 10499 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10500 sizeof(inq_ptr->product)); 10501 break; 10502 default: 10503 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10504 sizeof(inq_ptr->product)); 10505 break; 10506 } 10507 } else { 10508 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10509 strncpy(inq_ptr->product, val, 10510 min(sizeof(inq_ptr->product), strlen(val))); 10511 } 10512 10513 /* 10514 * XXX make this a macro somewhere so it automatically gets 10515 * incremented when we make changes. 10516 */ 10517 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10518 "revision")) == NULL) { 10519 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10520 } else { 10521 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10522 strncpy(inq_ptr->revision, val, 10523 min(sizeof(inq_ptr->revision), strlen(val))); 10524 } 10525 10526 /* 10527 * For parallel SCSI, we support double transition and single 10528 * transition clocking. We also support QAS (Quick Arbitration 10529 * and Selection) and Information Unit transfers on both the 10530 * control and array devices. 10531 */ 10532 if (port_type == CTL_PORT_SCSI) 10533 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10534 SID_SPI_IUS; 10535 10536 /* SAM-5 (no version claimed) */ 10537 scsi_ulto2b(0x00A0, inq_ptr->version1); 10538 /* SPC-4 (no version claimed) */ 10539 scsi_ulto2b(0x0460, inq_ptr->version2); 10540 if (port_type == CTL_PORT_FC) { 10541 /* FCP-2 ANSI INCITS.350:2003 */ 10542 scsi_ulto2b(0x0917, inq_ptr->version3); 10543 } else if (port_type == CTL_PORT_SCSI) { 10544 /* SPI-4 ANSI INCITS.362:200x */ 10545 scsi_ulto2b(0x0B56, inq_ptr->version3); 10546 } else if (port_type == CTL_PORT_ISCSI) { 10547 /* iSCSI (no version claimed) */ 10548 scsi_ulto2b(0x0960, inq_ptr->version3); 10549 } else if (port_type == CTL_PORT_SAS) { 10550 /* SAS (no version claimed) */ 10551 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10552 } 10553 10554 if (lun == NULL) { 10555 /* SBC-4 (no version claimed) */ 10556 scsi_ulto2b(0x0600, inq_ptr->version4); 10557 } else { 10558 switch (lun->be_lun->lun_type) { 10559 case T_DIRECT: 10560 /* SBC-4 (no version claimed) */ 10561 scsi_ulto2b(0x0600, inq_ptr->version4); 10562 break; 10563 case T_PROCESSOR: 10564 default: 10565 break; 10566 } 10567 } 10568 10569 ctsio->scsi_status = SCSI_STATUS_OK; 10570 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10571 ctsio->be_move_done = ctl_config_move_done; 10572 ctl_datamove((union ctl_io *)ctsio); 10573 return (CTL_RETVAL_COMPLETE); 10574 } 10575 10576 int 10577 ctl_inquiry(struct ctl_scsiio *ctsio) 10578 { 10579 struct scsi_inquiry *cdb; 10580 int retval; 10581 10582 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10583 10584 cdb = (struct scsi_inquiry *)ctsio->cdb; 10585 if (cdb->byte2 & SI_EVPD) 10586 retval = ctl_inquiry_evpd(ctsio); 10587 else if (cdb->page_code == 0) 10588 retval = ctl_inquiry_std(ctsio); 10589 else { 10590 ctl_set_invalid_field(ctsio, 10591 /*sks_valid*/ 1, 10592 /*command*/ 1, 10593 /*field*/ 2, 10594 /*bit_valid*/ 0, 10595 /*bit*/ 0); 10596 ctl_done((union ctl_io *)ctsio); 10597 return (CTL_RETVAL_COMPLETE); 10598 } 10599 10600 return (retval); 10601 } 10602 10603 /* 10604 * For known CDB types, parse the LBA and length. 10605 */ 10606 static int 10607 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10608 { 10609 if (io->io_hdr.io_type != CTL_IO_SCSI) 10610 return (1); 10611 10612 switch (io->scsiio.cdb[0]) { 10613 case COMPARE_AND_WRITE: { 10614 struct scsi_compare_and_write *cdb; 10615 10616 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10617 10618 *lba = scsi_8btou64(cdb->addr); 10619 *len = cdb->length; 10620 break; 10621 } 10622 case READ_6: 10623 case WRITE_6: { 10624 struct scsi_rw_6 *cdb; 10625 10626 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10627 10628 *lba = scsi_3btoul(cdb->addr); 10629 /* only 5 bits are valid in the most significant address byte */ 10630 *lba &= 0x1fffff; 10631 *len = cdb->length; 10632 break; 10633 } 10634 case READ_10: 10635 case WRITE_10: { 10636 struct scsi_rw_10 *cdb; 10637 10638 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10639 10640 *lba = scsi_4btoul(cdb->addr); 10641 *len = scsi_2btoul(cdb->length); 10642 break; 10643 } 10644 case WRITE_VERIFY_10: { 10645 struct scsi_write_verify_10 *cdb; 10646 10647 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10648 10649 *lba = scsi_4btoul(cdb->addr); 10650 *len = scsi_2btoul(cdb->length); 10651 break; 10652 } 10653 case READ_12: 10654 case WRITE_12: { 10655 struct scsi_rw_12 *cdb; 10656 10657 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10658 10659 *lba = scsi_4btoul(cdb->addr); 10660 *len = scsi_4btoul(cdb->length); 10661 break; 10662 } 10663 case WRITE_VERIFY_12: { 10664 struct scsi_write_verify_12 *cdb; 10665 10666 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10667 10668 *lba = scsi_4btoul(cdb->addr); 10669 *len = scsi_4btoul(cdb->length); 10670 break; 10671 } 10672 case READ_16: 10673 case WRITE_16: 10674 case WRITE_ATOMIC_16: { 10675 struct scsi_rw_16 *cdb; 10676 10677 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10678 10679 *lba = scsi_8btou64(cdb->addr); 10680 *len = scsi_4btoul(cdb->length); 10681 break; 10682 } 10683 case WRITE_VERIFY_16: { 10684 struct scsi_write_verify_16 *cdb; 10685 10686 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10687 10688 *lba = scsi_8btou64(cdb->addr); 10689 *len = scsi_4btoul(cdb->length); 10690 break; 10691 } 10692 case WRITE_SAME_10: { 10693 struct scsi_write_same_10 *cdb; 10694 10695 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10696 10697 *lba = scsi_4btoul(cdb->addr); 10698 *len = scsi_2btoul(cdb->length); 10699 break; 10700 } 10701 case WRITE_SAME_16: { 10702 struct scsi_write_same_16 *cdb; 10703 10704 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10705 10706 *lba = scsi_8btou64(cdb->addr); 10707 *len = scsi_4btoul(cdb->length); 10708 break; 10709 } 10710 case VERIFY_10: { 10711 struct scsi_verify_10 *cdb; 10712 10713 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10714 10715 *lba = scsi_4btoul(cdb->addr); 10716 *len = scsi_2btoul(cdb->length); 10717 break; 10718 } 10719 case VERIFY_12: { 10720 struct scsi_verify_12 *cdb; 10721 10722 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10723 10724 *lba = scsi_4btoul(cdb->addr); 10725 *len = scsi_4btoul(cdb->length); 10726 break; 10727 } 10728 case VERIFY_16: { 10729 struct scsi_verify_16 *cdb; 10730 10731 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10732 10733 *lba = scsi_8btou64(cdb->addr); 10734 *len = scsi_4btoul(cdb->length); 10735 break; 10736 } 10737 case UNMAP: { 10738 *lba = 0; 10739 *len = UINT64_MAX; 10740 break; 10741 } 10742 default: 10743 return (1); 10744 break; /* NOTREACHED */ 10745 } 10746 10747 return (0); 10748 } 10749 10750 static ctl_action 10751 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10752 { 10753 uint64_t endlba1, endlba2; 10754 10755 endlba1 = lba1 + len1 - 1; 10756 endlba2 = lba2 + len2 - 1; 10757 10758 if ((endlba1 < lba2) 10759 || (endlba2 < lba1)) 10760 return (CTL_ACTION_PASS); 10761 else 10762 return (CTL_ACTION_BLOCK); 10763 } 10764 10765 static int 10766 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10767 { 10768 struct ctl_ptr_len_flags *ptrlen; 10769 struct scsi_unmap_desc *buf, *end, *range; 10770 uint64_t lba; 10771 uint32_t len; 10772 10773 /* If not UNMAP -- go other way. */ 10774 if (io->io_hdr.io_type != CTL_IO_SCSI || 10775 io->scsiio.cdb[0] != UNMAP) 10776 return (CTL_ACTION_ERROR); 10777 10778 /* If UNMAP without data -- block and wait for data. */ 10779 ptrlen = (struct ctl_ptr_len_flags *) 10780 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10781 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10782 ptrlen->ptr == NULL) 10783 return (CTL_ACTION_BLOCK); 10784 10785 /* UNMAP with data -- check for collision. */ 10786 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10787 end = buf + ptrlen->len / sizeof(*buf); 10788 for (range = buf; range < end; range++) { 10789 lba = scsi_8btou64(range->lba); 10790 len = scsi_4btoul(range->length); 10791 if ((lba < lba2 + len2) && (lba + len > lba2)) 10792 return (CTL_ACTION_BLOCK); 10793 } 10794 return (CTL_ACTION_PASS); 10795 } 10796 10797 static ctl_action 10798 ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10799 { 10800 uint64_t lba1, lba2; 10801 uint64_t len1, len2; 10802 int retval; 10803 10804 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10805 return (CTL_ACTION_ERROR); 10806 10807 retval = ctl_extent_check_unmap(io2, lba1, len1); 10808 if (retval != CTL_ACTION_ERROR) 10809 return (retval); 10810 10811 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10812 return (CTL_ACTION_ERROR); 10813 10814 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10815 } 10816 10817 static ctl_action 10818 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10819 union ctl_io *ooa_io) 10820 { 10821 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10822 ctl_serialize_action *serialize_row; 10823 10824 /* 10825 * The initiator attempted multiple untagged commands at the same 10826 * time. Can't do that. 10827 */ 10828 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10829 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10830 && ((pending_io->io_hdr.nexus.targ_port == 10831 ooa_io->io_hdr.nexus.targ_port) 10832 && (pending_io->io_hdr.nexus.initid.id == 10833 ooa_io->io_hdr.nexus.initid.id)) 10834 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10835 return (CTL_ACTION_OVERLAP); 10836 10837 /* 10838 * The initiator attempted to send multiple tagged commands with 10839 * the same ID. (It's fine if different initiators have the same 10840 * tag ID.) 10841 * 10842 * Even if all of those conditions are true, we don't kill the I/O 10843 * if the command ahead of us has been aborted. We won't end up 10844 * sending it to the FETD, and it's perfectly legal to resend a 10845 * command with the same tag number as long as the previous 10846 * instance of this tag number has been aborted somehow. 10847 */ 10848 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10849 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10850 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10851 && ((pending_io->io_hdr.nexus.targ_port == 10852 ooa_io->io_hdr.nexus.targ_port) 10853 && (pending_io->io_hdr.nexus.initid.id == 10854 ooa_io->io_hdr.nexus.initid.id)) 10855 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10856 return (CTL_ACTION_OVERLAP_TAG); 10857 10858 /* 10859 * If we get a head of queue tag, SAM-3 says that we should 10860 * immediately execute it. 10861 * 10862 * What happens if this command would normally block for some other 10863 * reason? e.g. a request sense with a head of queue tag 10864 * immediately after a write. Normally that would block, but this 10865 * will result in its getting executed immediately... 10866 * 10867 * We currently return "pass" instead of "skip", so we'll end up 10868 * going through the rest of the queue to check for overlapped tags. 10869 * 10870 * XXX KDM check for other types of blockage first?? 10871 */ 10872 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10873 return (CTL_ACTION_PASS); 10874 10875 /* 10876 * Ordered tags have to block until all items ahead of them 10877 * have completed. If we get called with an ordered tag, we always 10878 * block, if something else is ahead of us in the queue. 10879 */ 10880 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10881 return (CTL_ACTION_BLOCK); 10882 10883 /* 10884 * Simple tags get blocked until all head of queue and ordered tags 10885 * ahead of them have completed. I'm lumping untagged commands in 10886 * with simple tags here. XXX KDM is that the right thing to do? 10887 */ 10888 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10889 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10890 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10891 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10892 return (CTL_ACTION_BLOCK); 10893 10894 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10895 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10896 10897 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10898 10899 switch (serialize_row[pending_entry->seridx]) { 10900 case CTL_SER_BLOCK: 10901 return (CTL_ACTION_BLOCK); 10902 case CTL_SER_EXTENT: 10903 return (ctl_extent_check(pending_io, ooa_io)); 10904 case CTL_SER_EXTENTOPT: 10905 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10906 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10907 return (ctl_extent_check(pending_io, ooa_io)); 10908 /* FALLTHROUGH */ 10909 case CTL_SER_PASS: 10910 return (CTL_ACTION_PASS); 10911 case CTL_SER_BLOCKOPT: 10912 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10913 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10914 return (CTL_ACTION_BLOCK); 10915 return (CTL_ACTION_PASS); 10916 case CTL_SER_SKIP: 10917 return (CTL_ACTION_SKIP); 10918 default: 10919 panic("invalid serialization value %d", 10920 serialize_row[pending_entry->seridx]); 10921 } 10922 10923 return (CTL_ACTION_ERROR); 10924 } 10925 10926 /* 10927 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10928 * Assumptions: 10929 * - pending_io is generally either incoming, or on the blocked queue 10930 * - starting I/O is the I/O we want to start the check with. 10931 */ 10932 static ctl_action 10933 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10934 union ctl_io *starting_io) 10935 { 10936 union ctl_io *ooa_io; 10937 ctl_action action; 10938 10939 mtx_assert(&lun->lun_lock, MA_OWNED); 10940 10941 /* 10942 * Run back along the OOA queue, starting with the current 10943 * blocked I/O and going through every I/O before it on the 10944 * queue. If starting_io is NULL, we'll just end up returning 10945 * CTL_ACTION_PASS. 10946 */ 10947 for (ooa_io = starting_io; ooa_io != NULL; 10948 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10949 ooa_links)){ 10950 10951 /* 10952 * This routine just checks to see whether 10953 * cur_blocked is blocked by ooa_io, which is ahead 10954 * of it in the queue. It doesn't queue/dequeue 10955 * cur_blocked. 10956 */ 10957 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10958 switch (action) { 10959 case CTL_ACTION_BLOCK: 10960 case CTL_ACTION_OVERLAP: 10961 case CTL_ACTION_OVERLAP_TAG: 10962 case CTL_ACTION_SKIP: 10963 case CTL_ACTION_ERROR: 10964 return (action); 10965 break; /* NOTREACHED */ 10966 case CTL_ACTION_PASS: 10967 break; 10968 default: 10969 panic("invalid action %d", action); 10970 break; /* NOTREACHED */ 10971 } 10972 } 10973 10974 return (CTL_ACTION_PASS); 10975 } 10976 10977 /* 10978 * Assumptions: 10979 * - An I/O has just completed, and has been removed from the per-LUN OOA 10980 * queue, so some items on the blocked queue may now be unblocked. 10981 */ 10982 static int 10983 ctl_check_blocked(struct ctl_lun *lun) 10984 { 10985 union ctl_io *cur_blocked, *next_blocked; 10986 10987 mtx_assert(&lun->lun_lock, MA_OWNED); 10988 10989 /* 10990 * Run forward from the head of the blocked queue, checking each 10991 * entry against the I/Os prior to it on the OOA queue to see if 10992 * there is still any blockage. 10993 * 10994 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10995 * with our removing a variable on it while it is traversing the 10996 * list. 10997 */ 10998 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10999 cur_blocked != NULL; cur_blocked = next_blocked) { 11000 union ctl_io *prev_ooa; 11001 ctl_action action; 11002 11003 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11004 blocked_links); 11005 11006 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11007 ctl_ooaq, ooa_links); 11008 11009 /* 11010 * If cur_blocked happens to be the first item in the OOA 11011 * queue now, prev_ooa will be NULL, and the action 11012 * returned will just be CTL_ACTION_PASS. 11013 */ 11014 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11015 11016 switch (action) { 11017 case CTL_ACTION_BLOCK: 11018 /* Nothing to do here, still blocked */ 11019 break; 11020 case CTL_ACTION_OVERLAP: 11021 case CTL_ACTION_OVERLAP_TAG: 11022 /* 11023 * This shouldn't happen! In theory we've already 11024 * checked this command for overlap... 11025 */ 11026 break; 11027 case CTL_ACTION_PASS: 11028 case CTL_ACTION_SKIP: { 11029 struct ctl_softc *softc; 11030 const struct ctl_cmd_entry *entry; 11031 uint32_t initidx; 11032 int isc_retval; 11033 11034 /* 11035 * The skip case shouldn't happen, this transaction 11036 * should have never made it onto the blocked queue. 11037 */ 11038 /* 11039 * This I/O is no longer blocked, we can remove it 11040 * from the blocked queue. Since this is a TAILQ 11041 * (doubly linked list), we can do O(1) removals 11042 * from any place on the list. 11043 */ 11044 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11045 blocked_links); 11046 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11047 11048 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11049 /* 11050 * Need to send IO back to original side to 11051 * run 11052 */ 11053 union ctl_ha_msg msg_info; 11054 11055 msg_info.hdr.original_sc = 11056 cur_blocked->io_hdr.original_sc; 11057 msg_info.hdr.serializing_sc = cur_blocked; 11058 msg_info.hdr.msg_type = CTL_MSG_R2R; 11059 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11060 &msg_info, sizeof(msg_info), 0)) > 11061 CTL_HA_STATUS_SUCCESS) { 11062 printf("CTL:Check Blocked error from " 11063 "ctl_ha_msg_send %d\n", 11064 isc_retval); 11065 } 11066 break; 11067 } 11068 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11069 softc = control_softc; 11070 11071 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11072 11073 /* 11074 * Check this I/O for LUN state changes that may 11075 * have happened while this command was blocked. 11076 * The LUN state may have been changed by a command 11077 * ahead of us in the queue, so we need to re-check 11078 * for any states that can be caused by SCSI 11079 * commands. 11080 */ 11081 if (ctl_scsiio_lun_check(softc, lun, entry, 11082 &cur_blocked->scsiio) == 0) { 11083 cur_blocked->io_hdr.flags |= 11084 CTL_FLAG_IS_WAS_ON_RTR; 11085 ctl_enqueue_rtr(cur_blocked); 11086 } else 11087 ctl_done(cur_blocked); 11088 break; 11089 } 11090 default: 11091 /* 11092 * This probably shouldn't happen -- we shouldn't 11093 * get CTL_ACTION_ERROR, or anything else. 11094 */ 11095 break; 11096 } 11097 } 11098 11099 return (CTL_RETVAL_COMPLETE); 11100 } 11101 11102 /* 11103 * This routine (with one exception) checks LUN flags that can be set by 11104 * commands ahead of us in the OOA queue. These flags have to be checked 11105 * when a command initially comes in, and when we pull a command off the 11106 * blocked queue and are preparing to execute it. The reason we have to 11107 * check these flags for commands on the blocked queue is that the LUN 11108 * state may have been changed by a command ahead of us while we're on the 11109 * blocked queue. 11110 * 11111 * Ordering is somewhat important with these checks, so please pay 11112 * careful attention to the placement of any new checks. 11113 */ 11114 static int 11115 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11116 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11117 { 11118 int retval; 11119 uint32_t residx; 11120 11121 retval = 0; 11122 11123 mtx_assert(&lun->lun_lock, MA_OWNED); 11124 11125 /* 11126 * If this shelf is a secondary shelf controller, we have to reject 11127 * any media access commands. 11128 */ 11129 #if 0 11130 /* No longer needed for HA */ 11131 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11132 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11133 ctl_set_lun_standby(ctsio); 11134 retval = 1; 11135 goto bailout; 11136 } 11137 #endif 11138 11139 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11140 if (lun->flags & CTL_LUN_READONLY) { 11141 ctl_set_sense(ctsio, /*current_error*/ 1, 11142 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11143 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11144 retval = 1; 11145 goto bailout; 11146 } 11147 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11148 .eca_and_aen & SCP_SWP) != 0) { 11149 ctl_set_sense(ctsio, /*current_error*/ 1, 11150 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11151 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11152 retval = 1; 11153 goto bailout; 11154 } 11155 } 11156 11157 /* 11158 * Check for a reservation conflict. If this command isn't allowed 11159 * even on reserved LUNs, and if this initiator isn't the one who 11160 * reserved us, reject the command with a reservation conflict. 11161 */ 11162 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11163 if ((lun->flags & CTL_LUN_RESERVED) 11164 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11165 if (lun->res_idx != residx) { 11166 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11167 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11168 retval = 1; 11169 goto bailout; 11170 } 11171 } 11172 11173 if ((lun->flags & CTL_LUN_PR_RESERVED) 11174 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11175 /* 11176 * if we aren't registered or it's a res holder type 11177 * reservation and this isn't the res holder then set a 11178 * conflict. 11179 * NOTE: Commands which might be allowed on write exclusive 11180 * type reservations are checked in the particular command 11181 * for a conflict. Read and SSU are the only ones. 11182 */ 11183 if (lun->pr_keys[residx] == 0 11184 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11185 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11186 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11187 retval = 1; 11188 goto bailout; 11189 } 11190 11191 } 11192 11193 if ((lun->flags & CTL_LUN_OFFLINE) 11194 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11195 ctl_set_lun_not_ready(ctsio); 11196 retval = 1; 11197 goto bailout; 11198 } 11199 11200 /* 11201 * If the LUN is stopped, see if this particular command is allowed 11202 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11203 */ 11204 if ((lun->flags & CTL_LUN_STOPPED) 11205 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11206 /* "Logical unit not ready, initializing cmd. required" */ 11207 ctl_set_lun_stopped(ctsio); 11208 retval = 1; 11209 goto bailout; 11210 } 11211 11212 if ((lun->flags & CTL_LUN_INOPERABLE) 11213 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11214 /* "Medium format corrupted" */ 11215 ctl_set_medium_format_corrupted(ctsio); 11216 retval = 1; 11217 goto bailout; 11218 } 11219 11220 bailout: 11221 return (retval); 11222 11223 } 11224 11225 static void 11226 ctl_failover_io(union ctl_io *io, int have_lock) 11227 { 11228 ctl_set_busy(&io->scsiio); 11229 ctl_done(io); 11230 } 11231 11232 static void 11233 ctl_failover(void) 11234 { 11235 struct ctl_lun *lun; 11236 struct ctl_softc *ctl_softc; 11237 union ctl_io *next_io, *pending_io; 11238 union ctl_io *io; 11239 int lun_idx; 11240 int i; 11241 11242 ctl_softc = control_softc; 11243 11244 mtx_lock(&ctl_softc->ctl_lock); 11245 /* 11246 * Remove any cmds from the other SC from the rtr queue. These 11247 * will obviously only be for LUNs for which we're the primary. 11248 * We can't send status or get/send data for these commands. 11249 * Since they haven't been executed yet, we can just remove them. 11250 * We'll either abort them or delete them below, depending on 11251 * which HA mode we're in. 11252 */ 11253 #ifdef notyet 11254 mtx_lock(&ctl_softc->queue_lock); 11255 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11256 io != NULL; io = next_io) { 11257 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11258 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11259 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11260 ctl_io_hdr, links); 11261 } 11262 mtx_unlock(&ctl_softc->queue_lock); 11263 #endif 11264 11265 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11266 lun = ctl_softc->ctl_luns[lun_idx]; 11267 if (lun==NULL) 11268 continue; 11269 11270 /* 11271 * Processor LUNs are primary on both sides. 11272 * XXX will this always be true? 11273 */ 11274 if (lun->be_lun->lun_type == T_PROCESSOR) 11275 continue; 11276 11277 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11278 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11279 printf("FAILOVER: primary lun %d\n", lun_idx); 11280 /* 11281 * Remove all commands from the other SC. First from the 11282 * blocked queue then from the ooa queue. Once we have 11283 * removed them. Call ctl_check_blocked to see if there 11284 * is anything that can run. 11285 */ 11286 for (io = (union ctl_io *)TAILQ_FIRST( 11287 &lun->blocked_queue); io != NULL; io = next_io) { 11288 11289 next_io = (union ctl_io *)TAILQ_NEXT( 11290 &io->io_hdr, blocked_links); 11291 11292 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11293 TAILQ_REMOVE(&lun->blocked_queue, 11294 &io->io_hdr,blocked_links); 11295 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11296 TAILQ_REMOVE(&lun->ooa_queue, 11297 &io->io_hdr, ooa_links); 11298 11299 ctl_free_io(io); 11300 } 11301 } 11302 11303 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11304 io != NULL; io = next_io) { 11305 11306 next_io = (union ctl_io *)TAILQ_NEXT( 11307 &io->io_hdr, ooa_links); 11308 11309 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11310 11311 TAILQ_REMOVE(&lun->ooa_queue, 11312 &io->io_hdr, 11313 ooa_links); 11314 11315 ctl_free_io(io); 11316 } 11317 } 11318 ctl_check_blocked(lun); 11319 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11320 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11321 11322 printf("FAILOVER: primary lun %d\n", lun_idx); 11323 /* 11324 * Abort all commands from the other SC. We can't 11325 * send status back for them now. These should get 11326 * cleaned up when they are completed or come out 11327 * for a datamove operation. 11328 */ 11329 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11330 io != NULL; io = next_io) { 11331 next_io = (union ctl_io *)TAILQ_NEXT( 11332 &io->io_hdr, ooa_links); 11333 11334 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11335 io->io_hdr.flags |= CTL_FLAG_ABORT; 11336 } 11337 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11338 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11339 11340 printf("FAILOVER: secondary lun %d\n", lun_idx); 11341 11342 lun->flags |= CTL_LUN_PRIMARY_SC; 11343 11344 /* 11345 * We send all I/O that was sent to this controller 11346 * and redirected to the other side back with 11347 * busy status, and have the initiator retry it. 11348 * Figuring out how much data has been transferred, 11349 * etc. and picking up where we left off would be 11350 * very tricky. 11351 * 11352 * XXX KDM need to remove I/O from the blocked 11353 * queue as well! 11354 */ 11355 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11356 &lun->ooa_queue); pending_io != NULL; 11357 pending_io = next_io) { 11358 11359 next_io = (union ctl_io *)TAILQ_NEXT( 11360 &pending_io->io_hdr, ooa_links); 11361 11362 pending_io->io_hdr.flags &= 11363 ~CTL_FLAG_SENT_2OTHER_SC; 11364 11365 if (pending_io->io_hdr.flags & 11366 CTL_FLAG_IO_ACTIVE) { 11367 pending_io->io_hdr.flags |= 11368 CTL_FLAG_FAILOVER; 11369 } else { 11370 ctl_set_busy(&pending_io->scsiio); 11371 ctl_done(pending_io); 11372 } 11373 } 11374 11375 /* 11376 * Build Unit Attention 11377 */ 11378 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11379 lun->pending_ua[i] |= 11380 CTL_UA_ASYM_ACC_CHANGE; 11381 } 11382 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11383 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11384 printf("FAILOVER: secondary lun %d\n", lun_idx); 11385 /* 11386 * if the first io on the OOA is not on the RtR queue 11387 * add it. 11388 */ 11389 lun->flags |= CTL_LUN_PRIMARY_SC; 11390 11391 pending_io = (union ctl_io *)TAILQ_FIRST( 11392 &lun->ooa_queue); 11393 if (pending_io==NULL) { 11394 printf("Nothing on OOA queue\n"); 11395 continue; 11396 } 11397 11398 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11399 if ((pending_io->io_hdr.flags & 11400 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11401 pending_io->io_hdr.flags |= 11402 CTL_FLAG_IS_WAS_ON_RTR; 11403 ctl_enqueue_rtr(pending_io); 11404 } 11405 #if 0 11406 else 11407 { 11408 printf("Tag 0x%04x is running\n", 11409 pending_io->scsiio.tag_num); 11410 } 11411 #endif 11412 11413 next_io = (union ctl_io *)TAILQ_NEXT( 11414 &pending_io->io_hdr, ooa_links); 11415 for (pending_io=next_io; pending_io != NULL; 11416 pending_io = next_io) { 11417 pending_io->io_hdr.flags &= 11418 ~CTL_FLAG_SENT_2OTHER_SC; 11419 next_io = (union ctl_io *)TAILQ_NEXT( 11420 &pending_io->io_hdr, ooa_links); 11421 if (pending_io->io_hdr.flags & 11422 CTL_FLAG_IS_WAS_ON_RTR) { 11423 #if 0 11424 printf("Tag 0x%04x is running\n", 11425 pending_io->scsiio.tag_num); 11426 #endif 11427 continue; 11428 } 11429 11430 switch (ctl_check_ooa(lun, pending_io, 11431 (union ctl_io *)TAILQ_PREV( 11432 &pending_io->io_hdr, ctl_ooaq, 11433 ooa_links))) { 11434 11435 case CTL_ACTION_BLOCK: 11436 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11437 &pending_io->io_hdr, 11438 blocked_links); 11439 pending_io->io_hdr.flags |= 11440 CTL_FLAG_BLOCKED; 11441 break; 11442 case CTL_ACTION_PASS: 11443 case CTL_ACTION_SKIP: 11444 pending_io->io_hdr.flags |= 11445 CTL_FLAG_IS_WAS_ON_RTR; 11446 ctl_enqueue_rtr(pending_io); 11447 break; 11448 case CTL_ACTION_OVERLAP: 11449 ctl_set_overlapped_cmd( 11450 (struct ctl_scsiio *)pending_io); 11451 ctl_done(pending_io); 11452 break; 11453 case CTL_ACTION_OVERLAP_TAG: 11454 ctl_set_overlapped_tag( 11455 (struct ctl_scsiio *)pending_io, 11456 pending_io->scsiio.tag_num & 0xff); 11457 ctl_done(pending_io); 11458 break; 11459 case CTL_ACTION_ERROR: 11460 default: 11461 ctl_set_internal_failure( 11462 (struct ctl_scsiio *)pending_io, 11463 0, // sks_valid 11464 0); //retry count 11465 ctl_done(pending_io); 11466 break; 11467 } 11468 } 11469 11470 /* 11471 * Build Unit Attention 11472 */ 11473 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11474 lun->pending_ua[i] |= 11475 CTL_UA_ASYM_ACC_CHANGE; 11476 } 11477 } else { 11478 panic("Unhandled HA mode failover, LUN flags = %#x, " 11479 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11480 } 11481 } 11482 ctl_pause_rtr = 0; 11483 mtx_unlock(&ctl_softc->ctl_lock); 11484 } 11485 11486 static int 11487 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11488 { 11489 struct ctl_lun *lun; 11490 const struct ctl_cmd_entry *entry; 11491 uint32_t initidx, targ_lun; 11492 int retval; 11493 11494 retval = 0; 11495 11496 lun = NULL; 11497 11498 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11499 if ((targ_lun < CTL_MAX_LUNS) 11500 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11501 lun = ctl_softc->ctl_luns[targ_lun]; 11502 /* 11503 * If the LUN is invalid, pretend that it doesn't exist. 11504 * It will go away as soon as all pending I/O has been 11505 * completed. 11506 */ 11507 if (lun->flags & CTL_LUN_DISABLED) { 11508 lun = NULL; 11509 } else { 11510 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11511 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11512 lun->be_lun; 11513 if (lun->be_lun->lun_type == T_PROCESSOR) { 11514 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11515 } 11516 11517 /* 11518 * Every I/O goes into the OOA queue for a 11519 * particular LUN, and stays there until completion. 11520 */ 11521 mtx_lock(&lun->lun_lock); 11522 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11523 ooa_links); 11524 } 11525 } else { 11526 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11527 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11528 } 11529 11530 /* Get command entry and return error if it is unsuppotyed. */ 11531 entry = ctl_validate_command(ctsio); 11532 if (entry == NULL) { 11533 if (lun) 11534 mtx_unlock(&lun->lun_lock); 11535 return (retval); 11536 } 11537 11538 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11539 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11540 11541 /* 11542 * Check to see whether we can send this command to LUNs that don't 11543 * exist. This should pretty much only be the case for inquiry 11544 * and request sense. Further checks, below, really require having 11545 * a LUN, so we can't really check the command anymore. Just put 11546 * it on the rtr queue. 11547 */ 11548 if (lun == NULL) { 11549 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11550 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11551 ctl_enqueue_rtr((union ctl_io *)ctsio); 11552 return (retval); 11553 } 11554 11555 ctl_set_unsupported_lun(ctsio); 11556 ctl_done((union ctl_io *)ctsio); 11557 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11558 return (retval); 11559 } else { 11560 /* 11561 * Make sure we support this particular command on this LUN. 11562 * e.g., we don't support writes to the control LUN. 11563 */ 11564 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11565 mtx_unlock(&lun->lun_lock); 11566 ctl_set_invalid_opcode(ctsio); 11567 ctl_done((union ctl_io *)ctsio); 11568 return (retval); 11569 } 11570 } 11571 11572 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11573 11574 #ifdef CTL_WITH_CA 11575 /* 11576 * If we've got a request sense, it'll clear the contingent 11577 * allegiance condition. Otherwise, if we have a CA condition for 11578 * this initiator, clear it, because it sent down a command other 11579 * than request sense. 11580 */ 11581 if ((ctsio->cdb[0] != REQUEST_SENSE) 11582 && (ctl_is_set(lun->have_ca, initidx))) 11583 ctl_clear_mask(lun->have_ca, initidx); 11584 #endif 11585 11586 /* 11587 * If the command has this flag set, it handles its own unit 11588 * attention reporting, we shouldn't do anything. Otherwise we 11589 * check for any pending unit attentions, and send them back to the 11590 * initiator. We only do this when a command initially comes in, 11591 * not when we pull it off the blocked queue. 11592 * 11593 * According to SAM-3, section 5.3.2, the order that things get 11594 * presented back to the host is basically unit attentions caused 11595 * by some sort of reset event, busy status, reservation conflicts 11596 * or task set full, and finally any other status. 11597 * 11598 * One issue here is that some of the unit attentions we report 11599 * don't fall into the "reset" category (e.g. "reported luns data 11600 * has changed"). So reporting it here, before the reservation 11601 * check, may be technically wrong. I guess the only thing to do 11602 * would be to check for and report the reset events here, and then 11603 * check for the other unit attention types after we check for a 11604 * reservation conflict. 11605 * 11606 * XXX KDM need to fix this 11607 */ 11608 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11609 ctl_ua_type ua_type; 11610 11611 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11612 scsi_sense_data_type sense_format; 11613 11614 if (lun != NULL) 11615 sense_format = (lun->flags & 11616 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11617 SSD_TYPE_FIXED; 11618 else 11619 sense_format = SSD_TYPE_FIXED; 11620 11621 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11622 &ctsio->sense_data, sense_format); 11623 if (ua_type != CTL_UA_NONE) { 11624 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11625 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11626 CTL_AUTOSENSE; 11627 ctsio->sense_len = SSD_FULL_SIZE; 11628 mtx_unlock(&lun->lun_lock); 11629 ctl_done((union ctl_io *)ctsio); 11630 return (retval); 11631 } 11632 } 11633 } 11634 11635 11636 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11637 mtx_unlock(&lun->lun_lock); 11638 ctl_done((union ctl_io *)ctsio); 11639 return (retval); 11640 } 11641 11642 /* 11643 * XXX CHD this is where we want to send IO to other side if 11644 * this LUN is secondary on this SC. We will need to make a copy 11645 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11646 * the copy we send as FROM_OTHER. 11647 * We also need to stuff the address of the original IO so we can 11648 * find it easily. Something similar will need be done on the other 11649 * side so when we are done we can find the copy. 11650 */ 11651 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11652 union ctl_ha_msg msg_info; 11653 int isc_retval; 11654 11655 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11656 11657 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11658 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11659 #if 0 11660 printf("1. ctsio %p\n", ctsio); 11661 #endif 11662 msg_info.hdr.serializing_sc = NULL; 11663 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11664 msg_info.scsi.tag_num = ctsio->tag_num; 11665 msg_info.scsi.tag_type = ctsio->tag_type; 11666 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11667 11668 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11669 11670 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11671 (void *)&msg_info, sizeof(msg_info), 0)) > 11672 CTL_HA_STATUS_SUCCESS) { 11673 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11674 isc_retval); 11675 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11676 } else { 11677 #if 0 11678 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11679 #endif 11680 } 11681 11682 /* 11683 * XXX KDM this I/O is off the incoming queue, but hasn't 11684 * been inserted on any other queue. We may need to come 11685 * up with a holding queue while we wait for serialization 11686 * so that we have an idea of what we're waiting for from 11687 * the other side. 11688 */ 11689 mtx_unlock(&lun->lun_lock); 11690 return (retval); 11691 } 11692 11693 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11694 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11695 ctl_ooaq, ooa_links))) { 11696 case CTL_ACTION_BLOCK: 11697 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11698 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11699 blocked_links); 11700 mtx_unlock(&lun->lun_lock); 11701 return (retval); 11702 case CTL_ACTION_PASS: 11703 case CTL_ACTION_SKIP: 11704 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11705 mtx_unlock(&lun->lun_lock); 11706 ctl_enqueue_rtr((union ctl_io *)ctsio); 11707 break; 11708 case CTL_ACTION_OVERLAP: 11709 mtx_unlock(&lun->lun_lock); 11710 ctl_set_overlapped_cmd(ctsio); 11711 ctl_done((union ctl_io *)ctsio); 11712 break; 11713 case CTL_ACTION_OVERLAP_TAG: 11714 mtx_unlock(&lun->lun_lock); 11715 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11716 ctl_done((union ctl_io *)ctsio); 11717 break; 11718 case CTL_ACTION_ERROR: 11719 default: 11720 mtx_unlock(&lun->lun_lock); 11721 ctl_set_internal_failure(ctsio, 11722 /*sks_valid*/ 0, 11723 /*retry_count*/ 0); 11724 ctl_done((union ctl_io *)ctsio); 11725 break; 11726 } 11727 return (retval); 11728 } 11729 11730 const struct ctl_cmd_entry * 11731 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11732 { 11733 const struct ctl_cmd_entry *entry; 11734 int service_action; 11735 11736 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11737 if (sa) 11738 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11739 if (entry->flags & CTL_CMD_FLAG_SA5) { 11740 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11741 entry = &((const struct ctl_cmd_entry *) 11742 entry->execute)[service_action]; 11743 } 11744 return (entry); 11745 } 11746 11747 const struct ctl_cmd_entry * 11748 ctl_validate_command(struct ctl_scsiio *ctsio) 11749 { 11750 const struct ctl_cmd_entry *entry; 11751 int i, sa; 11752 uint8_t diff; 11753 11754 entry = ctl_get_cmd_entry(ctsio, &sa); 11755 if (entry->execute == NULL) { 11756 if (sa) 11757 ctl_set_invalid_field(ctsio, 11758 /*sks_valid*/ 1, 11759 /*command*/ 1, 11760 /*field*/ 1, 11761 /*bit_valid*/ 1, 11762 /*bit*/ 4); 11763 else 11764 ctl_set_invalid_opcode(ctsio); 11765 ctl_done((union ctl_io *)ctsio); 11766 return (NULL); 11767 } 11768 KASSERT(entry->length > 0, 11769 ("Not defined length for command 0x%02x/0x%02x", 11770 ctsio->cdb[0], ctsio->cdb[1])); 11771 for (i = 1; i < entry->length; i++) { 11772 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11773 if (diff == 0) 11774 continue; 11775 ctl_set_invalid_field(ctsio, 11776 /*sks_valid*/ 1, 11777 /*command*/ 1, 11778 /*field*/ i, 11779 /*bit_valid*/ 1, 11780 /*bit*/ fls(diff) - 1); 11781 ctl_done((union ctl_io *)ctsio); 11782 return (NULL); 11783 } 11784 return (entry); 11785 } 11786 11787 static int 11788 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11789 { 11790 11791 switch (lun_type) { 11792 case T_PROCESSOR: 11793 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11794 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11795 return (0); 11796 break; 11797 case T_DIRECT: 11798 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11799 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11800 return (0); 11801 break; 11802 default: 11803 return (0); 11804 } 11805 return (1); 11806 } 11807 11808 static int 11809 ctl_scsiio(struct ctl_scsiio *ctsio) 11810 { 11811 int retval; 11812 const struct ctl_cmd_entry *entry; 11813 11814 retval = CTL_RETVAL_COMPLETE; 11815 11816 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11817 11818 entry = ctl_get_cmd_entry(ctsio, NULL); 11819 11820 /* 11821 * If this I/O has been aborted, just send it straight to 11822 * ctl_done() without executing it. 11823 */ 11824 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11825 ctl_done((union ctl_io *)ctsio); 11826 goto bailout; 11827 } 11828 11829 /* 11830 * All the checks should have been handled by ctl_scsiio_precheck(). 11831 * We should be clear now to just execute the I/O. 11832 */ 11833 retval = entry->execute(ctsio); 11834 11835 bailout: 11836 return (retval); 11837 } 11838 11839 /* 11840 * Since we only implement one target right now, a bus reset simply resets 11841 * our single target. 11842 */ 11843 static int 11844 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11845 { 11846 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11847 } 11848 11849 static int 11850 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11851 ctl_ua_type ua_type) 11852 { 11853 struct ctl_lun *lun; 11854 int retval; 11855 11856 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11857 union ctl_ha_msg msg_info; 11858 11859 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11860 msg_info.hdr.nexus = io->io_hdr.nexus; 11861 if (ua_type==CTL_UA_TARG_RESET) 11862 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11863 else 11864 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11865 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11866 msg_info.hdr.original_sc = NULL; 11867 msg_info.hdr.serializing_sc = NULL; 11868 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11869 (void *)&msg_info, sizeof(msg_info), 0)) { 11870 } 11871 } 11872 retval = 0; 11873 11874 mtx_lock(&ctl_softc->ctl_lock); 11875 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11876 retval += ctl_lun_reset(lun, io, ua_type); 11877 mtx_unlock(&ctl_softc->ctl_lock); 11878 11879 return (retval); 11880 } 11881 11882 /* 11883 * The LUN should always be set. The I/O is optional, and is used to 11884 * distinguish between I/Os sent by this initiator, and by other 11885 * initiators. We set unit attention for initiators other than this one. 11886 * SAM-3 is vague on this point. It does say that a unit attention should 11887 * be established for other initiators when a LUN is reset (see section 11888 * 5.7.3), but it doesn't specifically say that the unit attention should 11889 * be established for this particular initiator when a LUN is reset. Here 11890 * is the relevant text, from SAM-3 rev 8: 11891 * 11892 * 5.7.2 When a SCSI initiator port aborts its own tasks 11893 * 11894 * When a SCSI initiator port causes its own task(s) to be aborted, no 11895 * notification that the task(s) have been aborted shall be returned to 11896 * the SCSI initiator port other than the completion response for the 11897 * command or task management function action that caused the task(s) to 11898 * be aborted and notification(s) associated with related effects of the 11899 * action (e.g., a reset unit attention condition). 11900 * 11901 * XXX KDM for now, we're setting unit attention for all initiators. 11902 */ 11903 static int 11904 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11905 { 11906 union ctl_io *xio; 11907 #if 0 11908 uint32_t initindex; 11909 #endif 11910 int i; 11911 11912 mtx_lock(&lun->lun_lock); 11913 /* 11914 * Run through the OOA queue and abort each I/O. 11915 */ 11916 #if 0 11917 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11918 #endif 11919 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11920 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11921 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11922 } 11923 11924 /* 11925 * This version sets unit attention for every 11926 */ 11927 #if 0 11928 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11929 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11930 if (initindex == i) 11931 continue; 11932 lun->pending_ua[i] |= ua_type; 11933 } 11934 #endif 11935 11936 /* 11937 * A reset (any kind, really) clears reservations established with 11938 * RESERVE/RELEASE. It does not clear reservations established 11939 * with PERSISTENT RESERVE OUT, but we don't support that at the 11940 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11941 * reservations made with the RESERVE/RELEASE commands, because 11942 * those commands are obsolete in SPC-3. 11943 */ 11944 lun->flags &= ~CTL_LUN_RESERVED; 11945 11946 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11947 #ifdef CTL_WITH_CA 11948 ctl_clear_mask(lun->have_ca, i); 11949 #endif 11950 lun->pending_ua[i] |= ua_type; 11951 } 11952 mtx_unlock(&lun->lun_lock); 11953 11954 return (0); 11955 } 11956 11957 static void 11958 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11959 int other_sc) 11960 { 11961 union ctl_io *xio; 11962 11963 mtx_assert(&lun->lun_lock, MA_OWNED); 11964 11965 /* 11966 * Run through the OOA queue and attempt to find the given I/O. 11967 * The target port, initiator ID, tag type and tag number have to 11968 * match the values that we got from the initiator. If we have an 11969 * untagged command to abort, simply abort the first untagged command 11970 * we come to. We only allow one untagged command at a time of course. 11971 */ 11972 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11973 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11974 11975 if ((targ_port == UINT32_MAX || 11976 targ_port == xio->io_hdr.nexus.targ_port) && 11977 (init_id == UINT32_MAX || 11978 init_id == xio->io_hdr.nexus.initid.id)) { 11979 if (targ_port != xio->io_hdr.nexus.targ_port || 11980 init_id != xio->io_hdr.nexus.initid.id) 11981 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11982 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11983 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11984 union ctl_ha_msg msg_info; 11985 11986 msg_info.hdr.nexus = xio->io_hdr.nexus; 11987 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11988 msg_info.task.tag_num = xio->scsiio.tag_num; 11989 msg_info.task.tag_type = xio->scsiio.tag_type; 11990 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11991 msg_info.hdr.original_sc = NULL; 11992 msg_info.hdr.serializing_sc = NULL; 11993 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11994 (void *)&msg_info, sizeof(msg_info), 0); 11995 } 11996 } 11997 } 11998 } 11999 12000 static int 12001 ctl_abort_task_set(union ctl_io *io) 12002 { 12003 struct ctl_softc *softc = control_softc; 12004 struct ctl_lun *lun; 12005 uint32_t targ_lun; 12006 12007 /* 12008 * Look up the LUN. 12009 */ 12010 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12011 mtx_lock(&softc->ctl_lock); 12012 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12013 lun = softc->ctl_luns[targ_lun]; 12014 else { 12015 mtx_unlock(&softc->ctl_lock); 12016 return (1); 12017 } 12018 12019 mtx_lock(&lun->lun_lock); 12020 mtx_unlock(&softc->ctl_lock); 12021 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12022 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12023 io->io_hdr.nexus.initid.id, 12024 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12025 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12026 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12027 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12028 } 12029 mtx_unlock(&lun->lun_lock); 12030 return (0); 12031 } 12032 12033 static int 12034 ctl_i_t_nexus_reset(union ctl_io *io) 12035 { 12036 struct ctl_softc *softc = control_softc; 12037 struct ctl_lun *lun; 12038 uint32_t initindex, residx; 12039 12040 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12041 residx = ctl_get_resindex(&io->io_hdr.nexus); 12042 mtx_lock(&softc->ctl_lock); 12043 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12044 mtx_lock(&lun->lun_lock); 12045 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12046 io->io_hdr.nexus.initid.id, 12047 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12048 #ifdef CTL_WITH_CA 12049 ctl_clear_mask(lun->have_ca, initindex); 12050 #endif 12051 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12052 lun->flags &= ~CTL_LUN_RESERVED; 12053 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12054 mtx_unlock(&lun->lun_lock); 12055 } 12056 mtx_unlock(&softc->ctl_lock); 12057 return (0); 12058 } 12059 12060 static int 12061 ctl_abort_task(union ctl_io *io) 12062 { 12063 union ctl_io *xio; 12064 struct ctl_lun *lun; 12065 struct ctl_softc *ctl_softc; 12066 #if 0 12067 struct sbuf sb; 12068 char printbuf[128]; 12069 #endif 12070 int found; 12071 uint32_t targ_lun; 12072 12073 ctl_softc = control_softc; 12074 found = 0; 12075 12076 /* 12077 * Look up the LUN. 12078 */ 12079 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12080 mtx_lock(&ctl_softc->ctl_lock); 12081 if ((targ_lun < CTL_MAX_LUNS) 12082 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12083 lun = ctl_softc->ctl_luns[targ_lun]; 12084 else { 12085 mtx_unlock(&ctl_softc->ctl_lock); 12086 return (1); 12087 } 12088 12089 #if 0 12090 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12091 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12092 #endif 12093 12094 mtx_lock(&lun->lun_lock); 12095 mtx_unlock(&ctl_softc->ctl_lock); 12096 /* 12097 * Run through the OOA queue and attempt to find the given I/O. 12098 * The target port, initiator ID, tag type and tag number have to 12099 * match the values that we got from the initiator. If we have an 12100 * untagged command to abort, simply abort the first untagged command 12101 * we come to. We only allow one untagged command at a time of course. 12102 */ 12103 #if 0 12104 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12105 #endif 12106 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12107 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12108 #if 0 12109 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12110 12111 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12112 lun->lun, xio->scsiio.tag_num, 12113 xio->scsiio.tag_type, 12114 (xio->io_hdr.blocked_links.tqe_prev 12115 == NULL) ? "" : " BLOCKED", 12116 (xio->io_hdr.flags & 12117 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12118 (xio->io_hdr.flags & 12119 CTL_FLAG_ABORT) ? " ABORT" : "", 12120 (xio->io_hdr.flags & 12121 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12122 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12123 sbuf_finish(&sb); 12124 printf("%s\n", sbuf_data(&sb)); 12125 #endif 12126 12127 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12128 && (xio->io_hdr.nexus.initid.id == 12129 io->io_hdr.nexus.initid.id)) { 12130 /* 12131 * If the abort says that the task is untagged, the 12132 * task in the queue must be untagged. Otherwise, 12133 * we just check to see whether the tag numbers 12134 * match. This is because the QLogic firmware 12135 * doesn't pass back the tag type in an abort 12136 * request. 12137 */ 12138 #if 0 12139 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12140 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12141 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12142 #endif 12143 /* 12144 * XXX KDM we've got problems with FC, because it 12145 * doesn't send down a tag type with aborts. So we 12146 * can only really go by the tag number... 12147 * This may cause problems with parallel SCSI. 12148 * Need to figure that out!! 12149 */ 12150 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12151 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12152 found = 1; 12153 if ((io->io_hdr.flags & 12154 CTL_FLAG_FROM_OTHER_SC) == 0 && 12155 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12156 union ctl_ha_msg msg_info; 12157 12158 io->io_hdr.flags |= 12159 CTL_FLAG_SENT_2OTHER_SC; 12160 msg_info.hdr.nexus = io->io_hdr.nexus; 12161 msg_info.task.task_action = 12162 CTL_TASK_ABORT_TASK; 12163 msg_info.task.tag_num = 12164 io->taskio.tag_num; 12165 msg_info.task.tag_type = 12166 io->taskio.tag_type; 12167 msg_info.hdr.msg_type = 12168 CTL_MSG_MANAGE_TASKS; 12169 msg_info.hdr.original_sc = NULL; 12170 msg_info.hdr.serializing_sc = NULL; 12171 #if 0 12172 printf("Sent Abort to other side\n"); 12173 #endif 12174 if (CTL_HA_STATUS_SUCCESS != 12175 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12176 (void *)&msg_info, 12177 sizeof(msg_info), 0)) { 12178 } 12179 } 12180 #if 0 12181 printf("ctl_abort_task: found I/O to abort\n"); 12182 #endif 12183 break; 12184 } 12185 } 12186 } 12187 mtx_unlock(&lun->lun_lock); 12188 12189 if (found == 0) { 12190 /* 12191 * This isn't really an error. It's entirely possible for 12192 * the abort and command completion to cross on the wire. 12193 * This is more of an informative/diagnostic error. 12194 */ 12195 #if 0 12196 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12197 "%d:%d:%d:%d tag %d type %d\n", 12198 io->io_hdr.nexus.initid.id, 12199 io->io_hdr.nexus.targ_port, 12200 io->io_hdr.nexus.targ_target.id, 12201 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12202 io->taskio.tag_type); 12203 #endif 12204 } 12205 return (0); 12206 } 12207 12208 static void 12209 ctl_run_task(union ctl_io *io) 12210 { 12211 struct ctl_softc *ctl_softc = control_softc; 12212 int retval = 1; 12213 const char *task_desc; 12214 12215 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12216 12217 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12218 ("ctl_run_task: Unextected io_type %d\n", 12219 io->io_hdr.io_type)); 12220 12221 task_desc = ctl_scsi_task_string(&io->taskio); 12222 if (task_desc != NULL) { 12223 #ifdef NEEDTOPORT 12224 csevent_log(CSC_CTL | CSC_SHELF_SW | 12225 CTL_TASK_REPORT, 12226 csevent_LogType_Trace, 12227 csevent_Severity_Information, 12228 csevent_AlertLevel_Green, 12229 csevent_FRU_Firmware, 12230 csevent_FRU_Unknown, 12231 "CTL: received task: %s",task_desc); 12232 #endif 12233 } else { 12234 #ifdef NEEDTOPORT 12235 csevent_log(CSC_CTL | CSC_SHELF_SW | 12236 CTL_TASK_REPORT, 12237 csevent_LogType_Trace, 12238 csevent_Severity_Information, 12239 csevent_AlertLevel_Green, 12240 csevent_FRU_Firmware, 12241 csevent_FRU_Unknown, 12242 "CTL: received unknown task " 12243 "type: %d (%#x)", 12244 io->taskio.task_action, 12245 io->taskio.task_action); 12246 #endif 12247 } 12248 switch (io->taskio.task_action) { 12249 case CTL_TASK_ABORT_TASK: 12250 retval = ctl_abort_task(io); 12251 break; 12252 case CTL_TASK_ABORT_TASK_SET: 12253 case CTL_TASK_CLEAR_TASK_SET: 12254 retval = ctl_abort_task_set(io); 12255 break; 12256 case CTL_TASK_CLEAR_ACA: 12257 break; 12258 case CTL_TASK_I_T_NEXUS_RESET: 12259 retval = ctl_i_t_nexus_reset(io); 12260 break; 12261 case CTL_TASK_LUN_RESET: { 12262 struct ctl_lun *lun; 12263 uint32_t targ_lun; 12264 12265 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12266 mtx_lock(&ctl_softc->ctl_lock); 12267 if ((targ_lun < CTL_MAX_LUNS) 12268 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12269 lun = ctl_softc->ctl_luns[targ_lun]; 12270 else { 12271 mtx_unlock(&ctl_softc->ctl_lock); 12272 retval = 1; 12273 break; 12274 } 12275 12276 if (!(io->io_hdr.flags & 12277 CTL_FLAG_FROM_OTHER_SC)) { 12278 union ctl_ha_msg msg_info; 12279 12280 io->io_hdr.flags |= 12281 CTL_FLAG_SENT_2OTHER_SC; 12282 msg_info.hdr.msg_type = 12283 CTL_MSG_MANAGE_TASKS; 12284 msg_info.hdr.nexus = io->io_hdr.nexus; 12285 msg_info.task.task_action = 12286 CTL_TASK_LUN_RESET; 12287 msg_info.hdr.original_sc = NULL; 12288 msg_info.hdr.serializing_sc = NULL; 12289 if (CTL_HA_STATUS_SUCCESS != 12290 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12291 (void *)&msg_info, 12292 sizeof(msg_info), 0)) { 12293 } 12294 } 12295 12296 retval = ctl_lun_reset(lun, io, 12297 CTL_UA_LUN_RESET); 12298 mtx_unlock(&ctl_softc->ctl_lock); 12299 break; 12300 } 12301 case CTL_TASK_TARGET_RESET: 12302 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12303 break; 12304 case CTL_TASK_BUS_RESET: 12305 retval = ctl_bus_reset(ctl_softc, io); 12306 break; 12307 case CTL_TASK_PORT_LOGIN: 12308 break; 12309 case CTL_TASK_PORT_LOGOUT: 12310 break; 12311 default: 12312 printf("ctl_run_task: got unknown task management event %d\n", 12313 io->taskio.task_action); 12314 break; 12315 } 12316 if (retval == 0) 12317 io->io_hdr.status = CTL_SUCCESS; 12318 else 12319 io->io_hdr.status = CTL_ERROR; 12320 ctl_done(io); 12321 } 12322 12323 /* 12324 * For HA operation. Handle commands that come in from the other 12325 * controller. 12326 */ 12327 static void 12328 ctl_handle_isc(union ctl_io *io) 12329 { 12330 int free_io; 12331 struct ctl_lun *lun; 12332 struct ctl_softc *ctl_softc; 12333 uint32_t targ_lun; 12334 12335 ctl_softc = control_softc; 12336 12337 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12338 lun = ctl_softc->ctl_luns[targ_lun]; 12339 12340 switch (io->io_hdr.msg_type) { 12341 case CTL_MSG_SERIALIZE: 12342 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12343 break; 12344 case CTL_MSG_R2R: { 12345 const struct ctl_cmd_entry *entry; 12346 12347 /* 12348 * This is only used in SER_ONLY mode. 12349 */ 12350 free_io = 0; 12351 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12352 mtx_lock(&lun->lun_lock); 12353 if (ctl_scsiio_lun_check(ctl_softc, lun, 12354 entry, (struct ctl_scsiio *)io) != 0) { 12355 mtx_unlock(&lun->lun_lock); 12356 ctl_done(io); 12357 break; 12358 } 12359 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12360 mtx_unlock(&lun->lun_lock); 12361 ctl_enqueue_rtr(io); 12362 break; 12363 } 12364 case CTL_MSG_FINISH_IO: 12365 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12366 free_io = 0; 12367 ctl_done(io); 12368 } else { 12369 free_io = 1; 12370 mtx_lock(&lun->lun_lock); 12371 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12372 ooa_links); 12373 ctl_check_blocked(lun); 12374 mtx_unlock(&lun->lun_lock); 12375 } 12376 break; 12377 case CTL_MSG_PERS_ACTION: 12378 ctl_hndl_per_res_out_on_other_sc( 12379 (union ctl_ha_msg *)&io->presio.pr_msg); 12380 free_io = 1; 12381 break; 12382 case CTL_MSG_BAD_JUJU: 12383 free_io = 0; 12384 ctl_done(io); 12385 break; 12386 case CTL_MSG_DATAMOVE: 12387 /* Only used in XFER mode */ 12388 free_io = 0; 12389 ctl_datamove_remote(io); 12390 break; 12391 case CTL_MSG_DATAMOVE_DONE: 12392 /* Only used in XFER mode */ 12393 free_io = 0; 12394 io->scsiio.be_move_done(io); 12395 break; 12396 default: 12397 free_io = 1; 12398 printf("%s: Invalid message type %d\n", 12399 __func__, io->io_hdr.msg_type); 12400 break; 12401 } 12402 if (free_io) 12403 ctl_free_io(io); 12404 12405 } 12406 12407 12408 /* 12409 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12410 * there is no match. 12411 */ 12412 static ctl_lun_error_pattern 12413 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12414 { 12415 const struct ctl_cmd_entry *entry; 12416 ctl_lun_error_pattern filtered_pattern, pattern; 12417 12418 pattern = desc->error_pattern; 12419 12420 /* 12421 * XXX KDM we need more data passed into this function to match a 12422 * custom pattern, and we actually need to implement custom pattern 12423 * matching. 12424 */ 12425 if (pattern & CTL_LUN_PAT_CMD) 12426 return (CTL_LUN_PAT_CMD); 12427 12428 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12429 return (CTL_LUN_PAT_ANY); 12430 12431 entry = ctl_get_cmd_entry(ctsio, NULL); 12432 12433 filtered_pattern = entry->pattern & pattern; 12434 12435 /* 12436 * If the user requested specific flags in the pattern (e.g. 12437 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12438 * flags. 12439 * 12440 * If the user did not specify any flags, it doesn't matter whether 12441 * or not the command supports the flags. 12442 */ 12443 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12444 (pattern & ~CTL_LUN_PAT_MASK)) 12445 return (CTL_LUN_PAT_NONE); 12446 12447 /* 12448 * If the user asked for a range check, see if the requested LBA 12449 * range overlaps with this command's LBA range. 12450 */ 12451 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12452 uint64_t lba1; 12453 uint64_t len1; 12454 ctl_action action; 12455 int retval; 12456 12457 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12458 if (retval != 0) 12459 return (CTL_LUN_PAT_NONE); 12460 12461 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12462 desc->lba_range.len); 12463 /* 12464 * A "pass" means that the LBA ranges don't overlap, so 12465 * this doesn't match the user's range criteria. 12466 */ 12467 if (action == CTL_ACTION_PASS) 12468 return (CTL_LUN_PAT_NONE); 12469 } 12470 12471 return (filtered_pattern); 12472 } 12473 12474 static void 12475 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12476 { 12477 struct ctl_error_desc *desc, *desc2; 12478 12479 mtx_assert(&lun->lun_lock, MA_OWNED); 12480 12481 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12482 ctl_lun_error_pattern pattern; 12483 /* 12484 * Check to see whether this particular command matches 12485 * the pattern in the descriptor. 12486 */ 12487 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12488 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12489 continue; 12490 12491 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12492 case CTL_LUN_INJ_ABORTED: 12493 ctl_set_aborted(&io->scsiio); 12494 break; 12495 case CTL_LUN_INJ_MEDIUM_ERR: 12496 ctl_set_medium_error(&io->scsiio); 12497 break; 12498 case CTL_LUN_INJ_UA: 12499 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12500 * OCCURRED */ 12501 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12502 break; 12503 case CTL_LUN_INJ_CUSTOM: 12504 /* 12505 * We're assuming the user knows what he is doing. 12506 * Just copy the sense information without doing 12507 * checks. 12508 */ 12509 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12510 ctl_min(sizeof(desc->custom_sense), 12511 sizeof(io->scsiio.sense_data))); 12512 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12513 io->scsiio.sense_len = SSD_FULL_SIZE; 12514 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12515 break; 12516 case CTL_LUN_INJ_NONE: 12517 default: 12518 /* 12519 * If this is an error injection type we don't know 12520 * about, clear the continuous flag (if it is set) 12521 * so it will get deleted below. 12522 */ 12523 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12524 break; 12525 } 12526 /* 12527 * By default, each error injection action is a one-shot 12528 */ 12529 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12530 continue; 12531 12532 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12533 12534 free(desc, M_CTL); 12535 } 12536 } 12537 12538 #ifdef CTL_IO_DELAY 12539 static void 12540 ctl_datamove_timer_wakeup(void *arg) 12541 { 12542 union ctl_io *io; 12543 12544 io = (union ctl_io *)arg; 12545 12546 ctl_datamove(io); 12547 } 12548 #endif /* CTL_IO_DELAY */ 12549 12550 void 12551 ctl_datamove(union ctl_io *io) 12552 { 12553 void (*fe_datamove)(union ctl_io *io); 12554 12555 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12556 12557 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12558 12559 #ifdef CTL_TIME_IO 12560 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12561 char str[256]; 12562 char path_str[64]; 12563 struct sbuf sb; 12564 12565 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12566 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12567 12568 sbuf_cat(&sb, path_str); 12569 switch (io->io_hdr.io_type) { 12570 case CTL_IO_SCSI: 12571 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12572 sbuf_printf(&sb, "\n"); 12573 sbuf_cat(&sb, path_str); 12574 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12575 io->scsiio.tag_num, io->scsiio.tag_type); 12576 break; 12577 case CTL_IO_TASK: 12578 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12579 "Tag Type: %d\n", io->taskio.task_action, 12580 io->taskio.tag_num, io->taskio.tag_type); 12581 break; 12582 default: 12583 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12584 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12585 break; 12586 } 12587 sbuf_cat(&sb, path_str); 12588 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12589 (intmax_t)time_uptime - io->io_hdr.start_time); 12590 sbuf_finish(&sb); 12591 printf("%s", sbuf_data(&sb)); 12592 } 12593 #endif /* CTL_TIME_IO */ 12594 12595 #ifdef CTL_IO_DELAY 12596 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12597 struct ctl_lun *lun; 12598 12599 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12600 12601 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12602 } else { 12603 struct ctl_lun *lun; 12604 12605 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12606 if ((lun != NULL) 12607 && (lun->delay_info.datamove_delay > 0)) { 12608 struct callout *callout; 12609 12610 callout = (struct callout *)&io->io_hdr.timer_bytes; 12611 callout_init(callout, /*mpsafe*/ 1); 12612 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12613 callout_reset(callout, 12614 lun->delay_info.datamove_delay * hz, 12615 ctl_datamove_timer_wakeup, io); 12616 if (lun->delay_info.datamove_type == 12617 CTL_DELAY_TYPE_ONESHOT) 12618 lun->delay_info.datamove_delay = 0; 12619 return; 12620 } 12621 } 12622 #endif 12623 12624 /* 12625 * This command has been aborted. Set the port status, so we fail 12626 * the data move. 12627 */ 12628 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12629 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12630 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12631 io->io_hdr.nexus.targ_port, 12632 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12633 io->io_hdr.nexus.targ_lun); 12634 io->io_hdr.port_status = 31337; 12635 /* 12636 * Note that the backend, in this case, will get the 12637 * callback in its context. In other cases it may get 12638 * called in the frontend's interrupt thread context. 12639 */ 12640 io->scsiio.be_move_done(io); 12641 return; 12642 } 12643 12644 /* Don't confuse frontend with zero length data move. */ 12645 if (io->scsiio.kern_data_len == 0) { 12646 io->scsiio.be_move_done(io); 12647 return; 12648 } 12649 12650 /* 12651 * If we're in XFER mode and this I/O is from the other shelf 12652 * controller, we need to send the DMA to the other side to 12653 * actually transfer the data to/from the host. In serialize only 12654 * mode the transfer happens below CTL and ctl_datamove() is only 12655 * called on the machine that originally received the I/O. 12656 */ 12657 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12658 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12659 union ctl_ha_msg msg; 12660 uint32_t sg_entries_sent; 12661 int do_sg_copy; 12662 int i; 12663 12664 memset(&msg, 0, sizeof(msg)); 12665 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12666 msg.hdr.original_sc = io->io_hdr.original_sc; 12667 msg.hdr.serializing_sc = io; 12668 msg.hdr.nexus = io->io_hdr.nexus; 12669 msg.dt.flags = io->io_hdr.flags; 12670 /* 12671 * We convert everything into a S/G list here. We can't 12672 * pass by reference, only by value between controllers. 12673 * So we can't pass a pointer to the S/G list, only as many 12674 * S/G entries as we can fit in here. If it's possible for 12675 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12676 * then we need to break this up into multiple transfers. 12677 */ 12678 if (io->scsiio.kern_sg_entries == 0) { 12679 msg.dt.kern_sg_entries = 1; 12680 /* 12681 * If this is in cached memory, flush the cache 12682 * before we send the DMA request to the other 12683 * controller. We want to do this in either the 12684 * read or the write case. The read case is 12685 * straightforward. In the write case, we want to 12686 * make sure nothing is in the local cache that 12687 * could overwrite the DMAed data. 12688 */ 12689 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12690 /* 12691 * XXX KDM use bus_dmamap_sync() here. 12692 */ 12693 } 12694 12695 /* 12696 * Convert to a physical address if this is a 12697 * virtual address. 12698 */ 12699 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12700 msg.dt.sg_list[0].addr = 12701 io->scsiio.kern_data_ptr; 12702 } else { 12703 /* 12704 * XXX KDM use busdma here! 12705 */ 12706 #if 0 12707 msg.dt.sg_list[0].addr = (void *) 12708 vtophys(io->scsiio.kern_data_ptr); 12709 #endif 12710 } 12711 12712 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12713 do_sg_copy = 0; 12714 } else { 12715 struct ctl_sg_entry *sgl; 12716 12717 do_sg_copy = 1; 12718 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12719 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12720 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12721 /* 12722 * XXX KDM use bus_dmamap_sync() here. 12723 */ 12724 } 12725 } 12726 12727 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12728 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12729 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12730 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12731 msg.dt.sg_sequence = 0; 12732 12733 /* 12734 * Loop until we've sent all of the S/G entries. On the 12735 * other end, we'll recompose these S/G entries into one 12736 * contiguous list before passing it to the 12737 */ 12738 for (sg_entries_sent = 0; sg_entries_sent < 12739 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12740 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12741 sizeof(msg.dt.sg_list[0])), 12742 msg.dt.kern_sg_entries - sg_entries_sent); 12743 12744 if (do_sg_copy != 0) { 12745 struct ctl_sg_entry *sgl; 12746 int j; 12747 12748 sgl = (struct ctl_sg_entry *) 12749 io->scsiio.kern_data_ptr; 12750 /* 12751 * If this is in cached memory, flush the cache 12752 * before we send the DMA request to the other 12753 * controller. We want to do this in either 12754 * the * read or the write case. The read 12755 * case is straightforward. In the write 12756 * case, we want to make sure nothing is 12757 * in the local cache that could overwrite 12758 * the DMAed data. 12759 */ 12760 12761 for (i = sg_entries_sent, j = 0; 12762 i < msg.dt.cur_sg_entries; i++, j++) { 12763 if ((io->io_hdr.flags & 12764 CTL_FLAG_NO_DATASYNC) == 0) { 12765 /* 12766 * XXX KDM use bus_dmamap_sync() 12767 */ 12768 } 12769 if ((io->io_hdr.flags & 12770 CTL_FLAG_BUS_ADDR) == 0) { 12771 /* 12772 * XXX KDM use busdma. 12773 */ 12774 #if 0 12775 msg.dt.sg_list[j].addr =(void *) 12776 vtophys(sgl[i].addr); 12777 #endif 12778 } else { 12779 msg.dt.sg_list[j].addr = 12780 sgl[i].addr; 12781 } 12782 msg.dt.sg_list[j].len = sgl[i].len; 12783 } 12784 } 12785 12786 sg_entries_sent += msg.dt.cur_sg_entries; 12787 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12788 msg.dt.sg_last = 1; 12789 else 12790 msg.dt.sg_last = 0; 12791 12792 /* 12793 * XXX KDM drop and reacquire the lock here? 12794 */ 12795 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12796 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12797 /* 12798 * XXX do something here. 12799 */ 12800 } 12801 12802 msg.dt.sent_sg_entries = sg_entries_sent; 12803 } 12804 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12805 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12806 ctl_failover_io(io, /*have_lock*/ 0); 12807 12808 } else { 12809 12810 /* 12811 * Lookup the fe_datamove() function for this particular 12812 * front end. 12813 */ 12814 fe_datamove = 12815 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12816 12817 fe_datamove(io); 12818 } 12819 } 12820 12821 static void 12822 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12823 { 12824 union ctl_ha_msg msg; 12825 int isc_status; 12826 12827 memset(&msg, 0, sizeof(msg)); 12828 12829 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12830 msg.hdr.original_sc = io; 12831 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12832 msg.hdr.nexus = io->io_hdr.nexus; 12833 msg.hdr.status = io->io_hdr.status; 12834 msg.scsi.tag_num = io->scsiio.tag_num; 12835 msg.scsi.tag_type = io->scsiio.tag_type; 12836 msg.scsi.scsi_status = io->scsiio.scsi_status; 12837 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12838 sizeof(io->scsiio.sense_data)); 12839 msg.scsi.sense_len = io->scsiio.sense_len; 12840 msg.scsi.sense_residual = io->scsiio.sense_residual; 12841 msg.scsi.fetd_status = io->io_hdr.port_status; 12842 msg.scsi.residual = io->scsiio.residual; 12843 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12844 12845 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12846 ctl_failover_io(io, /*have_lock*/ have_lock); 12847 return; 12848 } 12849 12850 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12851 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12852 /* XXX do something if this fails */ 12853 } 12854 12855 } 12856 12857 /* 12858 * The DMA to the remote side is done, now we need to tell the other side 12859 * we're done so it can continue with its data movement. 12860 */ 12861 static void 12862 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12863 { 12864 union ctl_io *io; 12865 12866 io = rq->context; 12867 12868 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12869 printf("%s: ISC DMA write failed with error %d", __func__, 12870 rq->ret); 12871 ctl_set_internal_failure(&io->scsiio, 12872 /*sks_valid*/ 1, 12873 /*retry_count*/ rq->ret); 12874 } 12875 12876 ctl_dt_req_free(rq); 12877 12878 /* 12879 * In this case, we had to malloc the memory locally. Free it. 12880 */ 12881 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12882 int i; 12883 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12884 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12885 } 12886 /* 12887 * The data is in local and remote memory, so now we need to send 12888 * status (good or back) back to the other side. 12889 */ 12890 ctl_send_datamove_done(io, /*have_lock*/ 0); 12891 } 12892 12893 /* 12894 * We've moved the data from the host/controller into local memory. Now we 12895 * need to push it over to the remote controller's memory. 12896 */ 12897 static int 12898 ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12899 { 12900 int retval; 12901 12902 retval = 0; 12903 12904 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12905 ctl_datamove_remote_write_cb); 12906 12907 return (retval); 12908 } 12909 12910 static void 12911 ctl_datamove_remote_write(union ctl_io *io) 12912 { 12913 int retval; 12914 void (*fe_datamove)(union ctl_io *io); 12915 12916 /* 12917 * - Get the data from the host/HBA into local memory. 12918 * - DMA memory from the local controller to the remote controller. 12919 * - Send status back to the remote controller. 12920 */ 12921 12922 retval = ctl_datamove_remote_sgl_setup(io); 12923 if (retval != 0) 12924 return; 12925 12926 /* Switch the pointer over so the FETD knows what to do */ 12927 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12928 12929 /* 12930 * Use a custom move done callback, since we need to send completion 12931 * back to the other controller, not to the backend on this side. 12932 */ 12933 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12934 12935 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12936 12937 fe_datamove(io); 12938 12939 return; 12940 12941 } 12942 12943 static int 12944 ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12945 { 12946 #if 0 12947 char str[256]; 12948 char path_str[64]; 12949 struct sbuf sb; 12950 #endif 12951 12952 /* 12953 * In this case, we had to malloc the memory locally. Free it. 12954 */ 12955 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12956 int i; 12957 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12958 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12959 } 12960 12961 #if 0 12962 scsi_path_string(io, path_str, sizeof(path_str)); 12963 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12964 sbuf_cat(&sb, path_str); 12965 scsi_command_string(&io->scsiio, NULL, &sb); 12966 sbuf_printf(&sb, "\n"); 12967 sbuf_cat(&sb, path_str); 12968 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12969 io->scsiio.tag_num, io->scsiio.tag_type); 12970 sbuf_cat(&sb, path_str); 12971 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12972 io->io_hdr.flags, io->io_hdr.status); 12973 sbuf_finish(&sb); 12974 printk("%s", sbuf_data(&sb)); 12975 #endif 12976 12977 12978 /* 12979 * The read is done, now we need to send status (good or bad) back 12980 * to the other side. 12981 */ 12982 ctl_send_datamove_done(io, /*have_lock*/ 0); 12983 12984 return (0); 12985 } 12986 12987 static void 12988 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12989 { 12990 union ctl_io *io; 12991 void (*fe_datamove)(union ctl_io *io); 12992 12993 io = rq->context; 12994 12995 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12996 printf("%s: ISC DMA read failed with error %d", __func__, 12997 rq->ret); 12998 ctl_set_internal_failure(&io->scsiio, 12999 /*sks_valid*/ 1, 13000 /*retry_count*/ rq->ret); 13001 } 13002 13003 ctl_dt_req_free(rq); 13004 13005 /* Switch the pointer over so the FETD knows what to do */ 13006 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13007 13008 /* 13009 * Use a custom move done callback, since we need to send completion 13010 * back to the other controller, not to the backend on this side. 13011 */ 13012 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13013 13014 /* XXX KDM add checks like the ones in ctl_datamove? */ 13015 13016 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13017 13018 fe_datamove(io); 13019 } 13020 13021 static int 13022 ctl_datamove_remote_sgl_setup(union ctl_io *io) 13023 { 13024 struct ctl_sg_entry *local_sglist, *remote_sglist; 13025 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13026 struct ctl_softc *softc; 13027 int retval; 13028 int i; 13029 13030 retval = 0; 13031 softc = control_softc; 13032 13033 local_sglist = io->io_hdr.local_sglist; 13034 local_dma_sglist = io->io_hdr.local_dma_sglist; 13035 remote_sglist = io->io_hdr.remote_sglist; 13036 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13037 13038 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13039 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13040 local_sglist[i].len = remote_sglist[i].len; 13041 13042 /* 13043 * XXX Detect the situation where the RS-level I/O 13044 * redirector on the other side has already read the 13045 * data off of the AOR RS on this side, and 13046 * transferred it to remote (mirror) memory on the 13047 * other side. Since we already have the data in 13048 * memory here, we just need to use it. 13049 * 13050 * XXX KDM this can probably be removed once we 13051 * get the cache device code in and take the 13052 * current AOR implementation out. 13053 */ 13054 #ifdef NEEDTOPORT 13055 if ((remote_sglist[i].addr >= 13056 (void *)vtophys(softc->mirr->addr)) 13057 && (remote_sglist[i].addr < 13058 ((void *)vtophys(softc->mirr->addr) + 13059 CacheMirrorOffset))) { 13060 local_sglist[i].addr = remote_sglist[i].addr - 13061 CacheMirrorOffset; 13062 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13063 CTL_FLAG_DATA_IN) 13064 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13065 } else { 13066 local_sglist[i].addr = remote_sglist[i].addr + 13067 CacheMirrorOffset; 13068 } 13069 #endif 13070 #if 0 13071 printf("%s: local %p, remote %p, len %d\n", 13072 __func__, local_sglist[i].addr, 13073 remote_sglist[i].addr, local_sglist[i].len); 13074 #endif 13075 } 13076 } else { 13077 uint32_t len_to_go; 13078 13079 /* 13080 * In this case, we don't have automatically allocated 13081 * memory for this I/O on this controller. This typically 13082 * happens with internal CTL I/O -- e.g. inquiry, mode 13083 * sense, etc. Anything coming from RAIDCore will have 13084 * a mirror area available. 13085 */ 13086 len_to_go = io->scsiio.kern_data_len; 13087 13088 /* 13089 * Clear the no datasync flag, we have to use malloced 13090 * buffers. 13091 */ 13092 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13093 13094 /* 13095 * The difficult thing here is that the size of the various 13096 * S/G segments may be different than the size from the 13097 * remote controller. That'll make it harder when DMAing 13098 * the data back to the other side. 13099 */ 13100 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13101 sizeof(io->io_hdr.remote_sglist[0])) && 13102 (len_to_go > 0); i++) { 13103 local_sglist[i].len = ctl_min(len_to_go, 131072); 13104 CTL_SIZE_8B(local_dma_sglist[i].len, 13105 local_sglist[i].len); 13106 local_sglist[i].addr = 13107 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13108 13109 local_dma_sglist[i].addr = local_sglist[i].addr; 13110 13111 if (local_sglist[i].addr == NULL) { 13112 int j; 13113 13114 printf("malloc failed for %zd bytes!", 13115 local_dma_sglist[i].len); 13116 for (j = 0; j < i; j++) { 13117 free(local_sglist[j].addr, M_CTL); 13118 } 13119 ctl_set_internal_failure(&io->scsiio, 13120 /*sks_valid*/ 1, 13121 /*retry_count*/ 4857); 13122 retval = 1; 13123 goto bailout_error; 13124 13125 } 13126 /* XXX KDM do we need a sync here? */ 13127 13128 len_to_go -= local_sglist[i].len; 13129 } 13130 /* 13131 * Reset the number of S/G entries accordingly. The 13132 * original number of S/G entries is available in 13133 * rem_sg_entries. 13134 */ 13135 io->scsiio.kern_sg_entries = i; 13136 13137 #if 0 13138 printf("%s: kern_sg_entries = %d\n", __func__, 13139 io->scsiio.kern_sg_entries); 13140 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13141 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13142 local_sglist[i].addr, local_sglist[i].len, 13143 local_dma_sglist[i].len); 13144 #endif 13145 } 13146 13147 13148 return (retval); 13149 13150 bailout_error: 13151 13152 ctl_send_datamove_done(io, /*have_lock*/ 0); 13153 13154 return (retval); 13155 } 13156 13157 static int 13158 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13159 ctl_ha_dt_cb callback) 13160 { 13161 struct ctl_ha_dt_req *rq; 13162 struct ctl_sg_entry *remote_sglist, *local_sglist; 13163 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13164 uint32_t local_used, remote_used, total_used; 13165 int retval; 13166 int i, j; 13167 13168 retval = 0; 13169 13170 rq = ctl_dt_req_alloc(); 13171 13172 /* 13173 * If we failed to allocate the request, and if the DMA didn't fail 13174 * anyway, set busy status. This is just a resource allocation 13175 * failure. 13176 */ 13177 if ((rq == NULL) 13178 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13179 ctl_set_busy(&io->scsiio); 13180 13181 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13182 13183 if (rq != NULL) 13184 ctl_dt_req_free(rq); 13185 13186 /* 13187 * The data move failed. We need to return status back 13188 * to the other controller. No point in trying to DMA 13189 * data to the remote controller. 13190 */ 13191 13192 ctl_send_datamove_done(io, /*have_lock*/ 0); 13193 13194 retval = 1; 13195 13196 goto bailout; 13197 } 13198 13199 local_sglist = io->io_hdr.local_sglist; 13200 local_dma_sglist = io->io_hdr.local_dma_sglist; 13201 remote_sglist = io->io_hdr.remote_sglist; 13202 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13203 local_used = 0; 13204 remote_used = 0; 13205 total_used = 0; 13206 13207 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13208 rq->ret = CTL_HA_STATUS_SUCCESS; 13209 rq->context = io; 13210 callback(rq); 13211 goto bailout; 13212 } 13213 13214 /* 13215 * Pull/push the data over the wire from/to the other controller. 13216 * This takes into account the possibility that the local and 13217 * remote sglists may not be identical in terms of the size of 13218 * the elements and the number of elements. 13219 * 13220 * One fundamental assumption here is that the length allocated for 13221 * both the local and remote sglists is identical. Otherwise, we've 13222 * essentially got a coding error of some sort. 13223 */ 13224 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13225 int isc_ret; 13226 uint32_t cur_len, dma_length; 13227 uint8_t *tmp_ptr; 13228 13229 rq->id = CTL_HA_DATA_CTL; 13230 rq->command = command; 13231 rq->context = io; 13232 13233 /* 13234 * Both pointers should be aligned. But it is possible 13235 * that the allocation length is not. They should both 13236 * also have enough slack left over at the end, though, 13237 * to round up to the next 8 byte boundary. 13238 */ 13239 cur_len = ctl_min(local_sglist[i].len - local_used, 13240 remote_sglist[j].len - remote_used); 13241 13242 /* 13243 * In this case, we have a size issue and need to decrease 13244 * the size, except in the case where we actually have less 13245 * than 8 bytes left. In that case, we need to increase 13246 * the DMA length to get the last bit. 13247 */ 13248 if ((cur_len & 0x7) != 0) { 13249 if (cur_len > 0x7) { 13250 cur_len = cur_len - (cur_len & 0x7); 13251 dma_length = cur_len; 13252 } else { 13253 CTL_SIZE_8B(dma_length, cur_len); 13254 } 13255 13256 } else 13257 dma_length = cur_len; 13258 13259 /* 13260 * If we had to allocate memory for this I/O, instead of using 13261 * the non-cached mirror memory, we'll need to flush the cache 13262 * before trying to DMA to the other controller. 13263 * 13264 * We could end up doing this multiple times for the same 13265 * segment if we have a larger local segment than remote 13266 * segment. That shouldn't be an issue. 13267 */ 13268 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13269 /* 13270 * XXX KDM use bus_dmamap_sync() here. 13271 */ 13272 } 13273 13274 rq->size = dma_length; 13275 13276 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13277 tmp_ptr += local_used; 13278 13279 /* Use physical addresses when talking to ISC hardware */ 13280 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13281 /* XXX KDM use busdma */ 13282 #if 0 13283 rq->local = vtophys(tmp_ptr); 13284 #endif 13285 } else 13286 rq->local = tmp_ptr; 13287 13288 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13289 tmp_ptr += remote_used; 13290 rq->remote = tmp_ptr; 13291 13292 rq->callback = NULL; 13293 13294 local_used += cur_len; 13295 if (local_used >= local_sglist[i].len) { 13296 i++; 13297 local_used = 0; 13298 } 13299 13300 remote_used += cur_len; 13301 if (remote_used >= remote_sglist[j].len) { 13302 j++; 13303 remote_used = 0; 13304 } 13305 total_used += cur_len; 13306 13307 if (total_used >= io->scsiio.kern_data_len) 13308 rq->callback = callback; 13309 13310 if ((rq->size & 0x7) != 0) { 13311 printf("%s: warning: size %d is not on 8b boundary\n", 13312 __func__, rq->size); 13313 } 13314 if (((uintptr_t)rq->local & 0x7) != 0) { 13315 printf("%s: warning: local %p not on 8b boundary\n", 13316 __func__, rq->local); 13317 } 13318 if (((uintptr_t)rq->remote & 0x7) != 0) { 13319 printf("%s: warning: remote %p not on 8b boundary\n", 13320 __func__, rq->local); 13321 } 13322 #if 0 13323 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13324 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13325 rq->local, rq->remote, rq->size); 13326 #endif 13327 13328 isc_ret = ctl_dt_single(rq); 13329 if (isc_ret == CTL_HA_STATUS_WAIT) 13330 continue; 13331 13332 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13333 rq->ret = CTL_HA_STATUS_SUCCESS; 13334 } else { 13335 rq->ret = isc_ret; 13336 } 13337 callback(rq); 13338 goto bailout; 13339 } 13340 13341 bailout: 13342 return (retval); 13343 13344 } 13345 13346 static void 13347 ctl_datamove_remote_read(union ctl_io *io) 13348 { 13349 int retval; 13350 int i; 13351 13352 /* 13353 * This will send an error to the other controller in the case of a 13354 * failure. 13355 */ 13356 retval = ctl_datamove_remote_sgl_setup(io); 13357 if (retval != 0) 13358 return; 13359 13360 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13361 ctl_datamove_remote_read_cb); 13362 if ((retval != 0) 13363 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13364 /* 13365 * Make sure we free memory if there was an error.. The 13366 * ctl_datamove_remote_xfer() function will send the 13367 * datamove done message, or call the callback with an 13368 * error if there is a problem. 13369 */ 13370 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13371 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13372 } 13373 13374 return; 13375 } 13376 13377 /* 13378 * Process a datamove request from the other controller. This is used for 13379 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13380 * first. Once that is complete, the data gets DMAed into the remote 13381 * controller's memory. For reads, we DMA from the remote controller's 13382 * memory into our memory first, and then move it out to the FETD. 13383 */ 13384 static void 13385 ctl_datamove_remote(union ctl_io *io) 13386 { 13387 struct ctl_softc *softc; 13388 13389 softc = control_softc; 13390 13391 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13392 13393 /* 13394 * Note that we look for an aborted I/O here, but don't do some of 13395 * the other checks that ctl_datamove() normally does. 13396 * We don't need to run the datamove delay code, since that should 13397 * have been done if need be on the other controller. 13398 */ 13399 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13400 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13401 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13402 io->io_hdr.nexus.targ_port, 13403 io->io_hdr.nexus.targ_target.id, 13404 io->io_hdr.nexus.targ_lun); 13405 io->io_hdr.port_status = 31338; 13406 ctl_send_datamove_done(io, /*have_lock*/ 0); 13407 return; 13408 } 13409 13410 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13411 ctl_datamove_remote_write(io); 13412 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13413 ctl_datamove_remote_read(io); 13414 } else { 13415 union ctl_ha_msg msg; 13416 struct scsi_sense_data *sense; 13417 uint8_t sks[3]; 13418 int retry_count; 13419 13420 memset(&msg, 0, sizeof(msg)); 13421 13422 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13423 msg.hdr.status = CTL_SCSI_ERROR; 13424 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13425 13426 retry_count = 4243; 13427 13428 sense = &msg.scsi.sense_data; 13429 sks[0] = SSD_SCS_VALID; 13430 sks[1] = (retry_count >> 8) & 0xff; 13431 sks[2] = retry_count & 0xff; 13432 13433 /* "Internal target failure" */ 13434 scsi_set_sense_data(sense, 13435 /*sense_format*/ SSD_TYPE_NONE, 13436 /*current_error*/ 1, 13437 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13438 /*asc*/ 0x44, 13439 /*ascq*/ 0x00, 13440 /*type*/ SSD_ELEM_SKS, 13441 /*size*/ sizeof(sks), 13442 /*data*/ sks, 13443 SSD_ELEM_NONE); 13444 13445 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13446 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13447 ctl_failover_io(io, /*have_lock*/ 1); 13448 return; 13449 } 13450 13451 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13452 CTL_HA_STATUS_SUCCESS) { 13453 /* XXX KDM what to do if this fails? */ 13454 } 13455 return; 13456 } 13457 13458 } 13459 13460 static int 13461 ctl_process_done(union ctl_io *io) 13462 { 13463 struct ctl_lun *lun; 13464 struct ctl_softc *ctl_softc; 13465 void (*fe_done)(union ctl_io *io); 13466 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13467 13468 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13469 13470 fe_done = 13471 control_softc->ctl_ports[targ_port]->fe_done; 13472 13473 #ifdef CTL_TIME_IO 13474 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13475 char str[256]; 13476 char path_str[64]; 13477 struct sbuf sb; 13478 13479 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13480 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13481 13482 sbuf_cat(&sb, path_str); 13483 switch (io->io_hdr.io_type) { 13484 case CTL_IO_SCSI: 13485 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13486 sbuf_printf(&sb, "\n"); 13487 sbuf_cat(&sb, path_str); 13488 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13489 io->scsiio.tag_num, io->scsiio.tag_type); 13490 break; 13491 case CTL_IO_TASK: 13492 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13493 "Tag Type: %d\n", io->taskio.task_action, 13494 io->taskio.tag_num, io->taskio.tag_type); 13495 break; 13496 default: 13497 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13498 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13499 break; 13500 } 13501 sbuf_cat(&sb, path_str); 13502 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13503 (intmax_t)time_uptime - io->io_hdr.start_time); 13504 sbuf_finish(&sb); 13505 printf("%s", sbuf_data(&sb)); 13506 } 13507 #endif /* CTL_TIME_IO */ 13508 13509 switch (io->io_hdr.io_type) { 13510 case CTL_IO_SCSI: 13511 break; 13512 case CTL_IO_TASK: 13513 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13514 ctl_io_error_print(io, NULL); 13515 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13516 ctl_free_io(io); 13517 else 13518 fe_done(io); 13519 return (CTL_RETVAL_COMPLETE); 13520 default: 13521 panic("ctl_process_done: invalid io type %d\n", 13522 io->io_hdr.io_type); 13523 break; /* NOTREACHED */ 13524 } 13525 13526 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13527 if (lun == NULL) { 13528 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13529 io->io_hdr.nexus.targ_mapped_lun)); 13530 fe_done(io); 13531 goto bailout; 13532 } 13533 ctl_softc = lun->ctl_softc; 13534 13535 mtx_lock(&lun->lun_lock); 13536 13537 /* 13538 * Check to see if we have any errors to inject here. We only 13539 * inject errors for commands that don't already have errors set. 13540 */ 13541 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13542 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13543 ctl_inject_error(lun, io); 13544 13545 /* 13546 * XXX KDM how do we treat commands that aren't completed 13547 * successfully? 13548 * 13549 * XXX KDM should we also track I/O latency? 13550 */ 13551 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13552 io->io_hdr.io_type == CTL_IO_SCSI) { 13553 #ifdef CTL_TIME_IO 13554 struct bintime cur_bt; 13555 #endif 13556 int type; 13557 13558 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13559 CTL_FLAG_DATA_IN) 13560 type = CTL_STATS_READ; 13561 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13562 CTL_FLAG_DATA_OUT) 13563 type = CTL_STATS_WRITE; 13564 else 13565 type = CTL_STATS_NO_IO; 13566 13567 lun->stats.ports[targ_port].bytes[type] += 13568 io->scsiio.kern_total_len; 13569 lun->stats.ports[targ_port].operations[type]++; 13570 #ifdef CTL_TIME_IO 13571 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13572 &io->io_hdr.dma_bt); 13573 lun->stats.ports[targ_port].num_dmas[type] += 13574 io->io_hdr.num_dmas; 13575 getbintime(&cur_bt); 13576 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13577 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13578 #endif 13579 } 13580 13581 /* 13582 * Remove this from the OOA queue. 13583 */ 13584 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13585 13586 /* 13587 * Run through the blocked queue on this LUN and see if anything 13588 * has become unblocked, now that this transaction is done. 13589 */ 13590 ctl_check_blocked(lun); 13591 13592 /* 13593 * If the LUN has been invalidated, free it if there is nothing 13594 * left on its OOA queue. 13595 */ 13596 if ((lun->flags & CTL_LUN_INVALID) 13597 && TAILQ_EMPTY(&lun->ooa_queue)) { 13598 mtx_unlock(&lun->lun_lock); 13599 mtx_lock(&ctl_softc->ctl_lock); 13600 ctl_free_lun(lun); 13601 mtx_unlock(&ctl_softc->ctl_lock); 13602 } else 13603 mtx_unlock(&lun->lun_lock); 13604 13605 /* 13606 * If this command has been aborted, make sure we set the status 13607 * properly. The FETD is responsible for freeing the I/O and doing 13608 * whatever it needs to do to clean up its state. 13609 */ 13610 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13611 ctl_set_task_aborted(&io->scsiio); 13612 13613 /* 13614 * If enabled, print command error status. 13615 * We don't print UAs unless debugging was enabled explicitly. 13616 */ 13617 do { 13618 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13619 break; 13620 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13621 break; 13622 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13623 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13624 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13625 int error_code, sense_key, asc, ascq; 13626 13627 scsi_extract_sense_len(&io->scsiio.sense_data, 13628 io->scsiio.sense_len, &error_code, &sense_key, 13629 &asc, &ascq, /*show_errors*/ 0); 13630 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13631 break; 13632 } 13633 13634 ctl_io_error_print(io, NULL); 13635 } while (0); 13636 13637 /* 13638 * Tell the FETD or the other shelf controller we're done with this 13639 * command. Note that only SCSI commands get to this point. Task 13640 * management commands are completed above. 13641 * 13642 * We only send status to the other controller if we're in XFER 13643 * mode. In SER_ONLY mode, the I/O is done on the controller that 13644 * received the I/O (from CTL's perspective), and so the status is 13645 * generated there. 13646 * 13647 * XXX KDM if we hold the lock here, we could cause a deadlock 13648 * if the frontend comes back in in this context to queue 13649 * something. 13650 */ 13651 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13652 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13653 union ctl_ha_msg msg; 13654 13655 memset(&msg, 0, sizeof(msg)); 13656 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13657 msg.hdr.original_sc = io->io_hdr.original_sc; 13658 msg.hdr.nexus = io->io_hdr.nexus; 13659 msg.hdr.status = io->io_hdr.status; 13660 msg.scsi.scsi_status = io->scsiio.scsi_status; 13661 msg.scsi.tag_num = io->scsiio.tag_num; 13662 msg.scsi.tag_type = io->scsiio.tag_type; 13663 msg.scsi.sense_len = io->scsiio.sense_len; 13664 msg.scsi.sense_residual = io->scsiio.sense_residual; 13665 msg.scsi.residual = io->scsiio.residual; 13666 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13667 sizeof(io->scsiio.sense_data)); 13668 /* 13669 * We copy this whether or not this is an I/O-related 13670 * command. Otherwise, we'd have to go and check to see 13671 * whether it's a read/write command, and it really isn't 13672 * worth it. 13673 */ 13674 memcpy(&msg.scsi.lbalen, 13675 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13676 sizeof(msg.scsi.lbalen)); 13677 13678 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13679 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13680 /* XXX do something here */ 13681 } 13682 13683 ctl_free_io(io); 13684 } else 13685 fe_done(io); 13686 13687 bailout: 13688 13689 return (CTL_RETVAL_COMPLETE); 13690 } 13691 13692 #ifdef CTL_WITH_CA 13693 /* 13694 * Front end should call this if it doesn't do autosense. When the request 13695 * sense comes back in from the initiator, we'll dequeue this and send it. 13696 */ 13697 int 13698 ctl_queue_sense(union ctl_io *io) 13699 { 13700 struct ctl_lun *lun; 13701 struct ctl_softc *ctl_softc; 13702 uint32_t initidx, targ_lun; 13703 13704 ctl_softc = control_softc; 13705 13706 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13707 13708 /* 13709 * LUN lookup will likely move to the ctl_work_thread() once we 13710 * have our new queueing infrastructure (that doesn't put things on 13711 * a per-LUN queue initially). That is so that we can handle 13712 * things like an INQUIRY to a LUN that we don't have enabled. We 13713 * can't deal with that right now. 13714 */ 13715 mtx_lock(&ctl_softc->ctl_lock); 13716 13717 /* 13718 * If we don't have a LUN for this, just toss the sense 13719 * information. 13720 */ 13721 targ_lun = io->io_hdr.nexus.targ_lun; 13722 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13723 if ((targ_lun < CTL_MAX_LUNS) 13724 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13725 lun = ctl_softc->ctl_luns[targ_lun]; 13726 else 13727 goto bailout; 13728 13729 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13730 13731 mtx_lock(&lun->lun_lock); 13732 /* 13733 * Already have CA set for this LUN...toss the sense information. 13734 */ 13735 if (ctl_is_set(lun->have_ca, initidx)) { 13736 mtx_unlock(&lun->lun_lock); 13737 goto bailout; 13738 } 13739 13740 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13741 ctl_min(sizeof(lun->pending_sense[initidx]), 13742 sizeof(io->scsiio.sense_data))); 13743 ctl_set_mask(lun->have_ca, initidx); 13744 mtx_unlock(&lun->lun_lock); 13745 13746 bailout: 13747 mtx_unlock(&ctl_softc->ctl_lock); 13748 13749 ctl_free_io(io); 13750 13751 return (CTL_RETVAL_COMPLETE); 13752 } 13753 #endif 13754 13755 /* 13756 * Primary command inlet from frontend ports. All SCSI and task I/O 13757 * requests must go through this function. 13758 */ 13759 int 13760 ctl_queue(union ctl_io *io) 13761 { 13762 struct ctl_softc *ctl_softc; 13763 13764 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13765 13766 ctl_softc = control_softc; 13767 13768 #ifdef CTL_TIME_IO 13769 io->io_hdr.start_time = time_uptime; 13770 getbintime(&io->io_hdr.start_bt); 13771 #endif /* CTL_TIME_IO */ 13772 13773 /* Map FE-specific LUN ID into global one. */ 13774 io->io_hdr.nexus.targ_mapped_lun = 13775 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13776 13777 switch (io->io_hdr.io_type) { 13778 case CTL_IO_SCSI: 13779 case CTL_IO_TASK: 13780 if (ctl_debug & CTL_DEBUG_CDB) 13781 ctl_io_print(io); 13782 ctl_enqueue_incoming(io); 13783 break; 13784 default: 13785 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13786 return (EINVAL); 13787 } 13788 13789 return (CTL_RETVAL_COMPLETE); 13790 } 13791 13792 #ifdef CTL_IO_DELAY 13793 static void 13794 ctl_done_timer_wakeup(void *arg) 13795 { 13796 union ctl_io *io; 13797 13798 io = (union ctl_io *)arg; 13799 ctl_done(io); 13800 } 13801 #endif /* CTL_IO_DELAY */ 13802 13803 void 13804 ctl_done(union ctl_io *io) 13805 { 13806 struct ctl_softc *ctl_softc; 13807 13808 ctl_softc = control_softc; 13809 13810 /* 13811 * Enable this to catch duplicate completion issues. 13812 */ 13813 #if 0 13814 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13815 printf("%s: type %d msg %d cdb %x iptl: " 13816 "%d:%d:%d:%d tag 0x%04x " 13817 "flag %#x status %x\n", 13818 __func__, 13819 io->io_hdr.io_type, 13820 io->io_hdr.msg_type, 13821 io->scsiio.cdb[0], 13822 io->io_hdr.nexus.initid.id, 13823 io->io_hdr.nexus.targ_port, 13824 io->io_hdr.nexus.targ_target.id, 13825 io->io_hdr.nexus.targ_lun, 13826 (io->io_hdr.io_type == 13827 CTL_IO_TASK) ? 13828 io->taskio.tag_num : 13829 io->scsiio.tag_num, 13830 io->io_hdr.flags, 13831 io->io_hdr.status); 13832 } else 13833 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13834 #endif 13835 13836 /* 13837 * This is an internal copy of an I/O, and should not go through 13838 * the normal done processing logic. 13839 */ 13840 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13841 return; 13842 13843 /* 13844 * We need to send a msg to the serializing shelf to finish the IO 13845 * as well. We don't send a finish message to the other shelf if 13846 * this is a task management command. Task management commands 13847 * aren't serialized in the OOA queue, but rather just executed on 13848 * both shelf controllers for commands that originated on that 13849 * controller. 13850 */ 13851 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13852 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13853 union ctl_ha_msg msg_io; 13854 13855 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13856 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13857 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13858 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13859 } 13860 /* continue on to finish IO */ 13861 } 13862 #ifdef CTL_IO_DELAY 13863 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13864 struct ctl_lun *lun; 13865 13866 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13867 13868 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13869 } else { 13870 struct ctl_lun *lun; 13871 13872 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13873 13874 if ((lun != NULL) 13875 && (lun->delay_info.done_delay > 0)) { 13876 struct callout *callout; 13877 13878 callout = (struct callout *)&io->io_hdr.timer_bytes; 13879 callout_init(callout, /*mpsafe*/ 1); 13880 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13881 callout_reset(callout, 13882 lun->delay_info.done_delay * hz, 13883 ctl_done_timer_wakeup, io); 13884 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13885 lun->delay_info.done_delay = 0; 13886 return; 13887 } 13888 } 13889 #endif /* CTL_IO_DELAY */ 13890 13891 ctl_enqueue_done(io); 13892 } 13893 13894 int 13895 ctl_isc(struct ctl_scsiio *ctsio) 13896 { 13897 struct ctl_lun *lun; 13898 int retval; 13899 13900 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13901 13902 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13903 13904 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13905 13906 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13907 13908 return (retval); 13909 } 13910 13911 13912 static void 13913 ctl_work_thread(void *arg) 13914 { 13915 struct ctl_thread *thr = (struct ctl_thread *)arg; 13916 struct ctl_softc *softc = thr->ctl_softc; 13917 union ctl_io *io; 13918 int retval; 13919 13920 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13921 13922 for (;;) { 13923 retval = 0; 13924 13925 /* 13926 * We handle the queues in this order: 13927 * - ISC 13928 * - done queue (to free up resources, unblock other commands) 13929 * - RtR queue 13930 * - incoming queue 13931 * 13932 * If those queues are empty, we break out of the loop and 13933 * go to sleep. 13934 */ 13935 mtx_lock(&thr->queue_lock); 13936 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13937 if (io != NULL) { 13938 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13939 mtx_unlock(&thr->queue_lock); 13940 ctl_handle_isc(io); 13941 continue; 13942 } 13943 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13944 if (io != NULL) { 13945 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13946 /* clear any blocked commands, call fe_done */ 13947 mtx_unlock(&thr->queue_lock); 13948 retval = ctl_process_done(io); 13949 continue; 13950 } 13951 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13952 if (io != NULL) { 13953 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13954 mtx_unlock(&thr->queue_lock); 13955 if (io->io_hdr.io_type == CTL_IO_TASK) 13956 ctl_run_task(io); 13957 else 13958 ctl_scsiio_precheck(softc, &io->scsiio); 13959 continue; 13960 } 13961 if (!ctl_pause_rtr) { 13962 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13963 if (io != NULL) { 13964 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13965 mtx_unlock(&thr->queue_lock); 13966 retval = ctl_scsiio(&io->scsiio); 13967 if (retval != CTL_RETVAL_COMPLETE) 13968 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13969 continue; 13970 } 13971 } 13972 13973 /* Sleep until we have something to do. */ 13974 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13975 } 13976 } 13977 13978 static void 13979 ctl_lun_thread(void *arg) 13980 { 13981 struct ctl_softc *softc = (struct ctl_softc *)arg; 13982 struct ctl_be_lun *be_lun; 13983 int retval; 13984 13985 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13986 13987 for (;;) { 13988 retval = 0; 13989 mtx_lock(&softc->ctl_lock); 13990 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13991 if (be_lun != NULL) { 13992 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13993 mtx_unlock(&softc->ctl_lock); 13994 ctl_create_lun(be_lun); 13995 continue; 13996 } 13997 13998 /* Sleep until we have something to do. */ 13999 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14000 PDROP | PRIBIO, "-", 0); 14001 } 14002 } 14003 14004 static void 14005 ctl_enqueue_incoming(union ctl_io *io) 14006 { 14007 struct ctl_softc *softc = control_softc; 14008 struct ctl_thread *thr; 14009 u_int idx; 14010 14011 idx = (io->io_hdr.nexus.targ_port * 127 + 14012 io->io_hdr.nexus.initid.id) % worker_threads; 14013 thr = &softc->threads[idx]; 14014 mtx_lock(&thr->queue_lock); 14015 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14016 mtx_unlock(&thr->queue_lock); 14017 wakeup(thr); 14018 } 14019 14020 static void 14021 ctl_enqueue_rtr(union ctl_io *io) 14022 { 14023 struct ctl_softc *softc = control_softc; 14024 struct ctl_thread *thr; 14025 14026 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14027 mtx_lock(&thr->queue_lock); 14028 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14029 mtx_unlock(&thr->queue_lock); 14030 wakeup(thr); 14031 } 14032 14033 static void 14034 ctl_enqueue_done(union ctl_io *io) 14035 { 14036 struct ctl_softc *softc = control_softc; 14037 struct ctl_thread *thr; 14038 14039 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14040 mtx_lock(&thr->queue_lock); 14041 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14042 mtx_unlock(&thr->queue_lock); 14043 wakeup(thr); 14044 } 14045 14046 static void 14047 ctl_enqueue_isc(union ctl_io *io) 14048 { 14049 struct ctl_softc *softc = control_softc; 14050 struct ctl_thread *thr; 14051 14052 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14053 mtx_lock(&thr->queue_lock); 14054 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14055 mtx_unlock(&thr->queue_lock); 14056 wakeup(thr); 14057 } 14058 14059 /* Initialization and failover */ 14060 14061 void 14062 ctl_init_isc_msg(void) 14063 { 14064 printf("CTL: Still calling this thing\n"); 14065 } 14066 14067 /* 14068 * Init component 14069 * Initializes component into configuration defined by bootMode 14070 * (see hasc-sv.c) 14071 * returns hasc_Status: 14072 * OK 14073 * ERROR - fatal error 14074 */ 14075 static ctl_ha_comp_status 14076 ctl_isc_init(struct ctl_ha_component *c) 14077 { 14078 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14079 14080 c->status = ret; 14081 return ret; 14082 } 14083 14084 /* Start component 14085 * Starts component in state requested. If component starts successfully, 14086 * it must set its own state to the requestrd state 14087 * When requested state is HASC_STATE_HA, the component may refine it 14088 * by adding _SLAVE or _MASTER flags. 14089 * Currently allowed state transitions are: 14090 * UNKNOWN->HA - initial startup 14091 * UNKNOWN->SINGLE - initial startup when no parter detected 14092 * HA->SINGLE - failover 14093 * returns ctl_ha_comp_status: 14094 * OK - component successfully started in requested state 14095 * FAILED - could not start the requested state, failover may 14096 * be possible 14097 * ERROR - fatal error detected, no future startup possible 14098 */ 14099 static ctl_ha_comp_status 14100 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14101 { 14102 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14103 14104 printf("%s: go\n", __func__); 14105 14106 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14107 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14108 ctl_is_single = 0; 14109 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14110 != CTL_HA_STATUS_SUCCESS) { 14111 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14112 ret = CTL_HA_COMP_STATUS_ERROR; 14113 } 14114 } else if (CTL_HA_STATE_IS_HA(c->state) 14115 && CTL_HA_STATE_IS_SINGLE(state)){ 14116 // HA->SINGLE transition 14117 ctl_failover(); 14118 ctl_is_single = 1; 14119 } else { 14120 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14121 c->state, state); 14122 ret = CTL_HA_COMP_STATUS_ERROR; 14123 } 14124 if (CTL_HA_STATE_IS_SINGLE(state)) 14125 ctl_is_single = 1; 14126 14127 c->state = state; 14128 c->status = ret; 14129 return ret; 14130 } 14131 14132 /* 14133 * Quiesce component 14134 * The component must clear any error conditions (set status to OK) and 14135 * prepare itself to another Start call 14136 * returns ctl_ha_comp_status: 14137 * OK 14138 * ERROR 14139 */ 14140 static ctl_ha_comp_status 14141 ctl_isc_quiesce(struct ctl_ha_component *c) 14142 { 14143 int ret = CTL_HA_COMP_STATUS_OK; 14144 14145 ctl_pause_rtr = 1; 14146 c->status = ret; 14147 return ret; 14148 } 14149 14150 struct ctl_ha_component ctl_ha_component_ctlisc = 14151 { 14152 .name = "CTL ISC", 14153 .state = CTL_HA_STATE_UNKNOWN, 14154 .init = ctl_isc_init, 14155 .start = ctl_isc_start, 14156 .quiesce = ctl_isc_quiesce 14157 }; 14158 14159 /* 14160 * vim: ts=8 14161 */ 14162