1 /*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36 /* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42 #define _CTL_C 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/ctype.h> 50 #include <sys/kernel.h> 51 #include <sys/types.h> 52 #include <sys/kthread.h> 53 #include <sys/bio.h> 54 #include <sys/fcntl.h> 55 #include <sys/lock.h> 56 #include <sys/module.h> 57 #include <sys/mutex.h> 58 #include <sys/condvar.h> 59 #include <sys/malloc.h> 60 #include <sys/conf.h> 61 #include <sys/ioccom.h> 62 #include <sys/queue.h> 63 #include <sys/sbuf.h> 64 #include <sys/smp.h> 65 #include <sys/endian.h> 66 #include <sys/sysctl.h> 67 #include <vm/uma.h> 68 69 #include <cam/cam.h> 70 #include <cam/scsi/scsi_all.h> 71 #include <cam/scsi/scsi_da.h> 72 #include <cam/ctl/ctl_io.h> 73 #include <cam/ctl/ctl.h> 74 #include <cam/ctl/ctl_frontend.h> 75 #include <cam/ctl/ctl_frontend_internal.h> 76 #include <cam/ctl/ctl_util.h> 77 #include <cam/ctl/ctl_backend.h> 78 #include <cam/ctl/ctl_ioctl.h> 79 #include <cam/ctl/ctl_ha.h> 80 #include <cam/ctl/ctl_private.h> 81 #include <cam/ctl/ctl_debug.h> 82 #include <cam/ctl/ctl_scsi_all.h> 83 #include <cam/ctl/ctl_error.h> 84 85 struct ctl_softc *control_softc = NULL; 86 87 /* 88 * Size and alignment macros needed for Copan-specific HA hardware. These 89 * can go away when the HA code is re-written, and uses busdma for any 90 * hardware. 91 */ 92 #define CTL_ALIGN_8B(target, source, type) \ 93 if (((uint32_t)source & 0x7) != 0) \ 94 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 95 else \ 96 target = (type)source; 97 98 #define CTL_SIZE_8B(target, size) \ 99 if ((size & 0x7) != 0) \ 100 target = size + (0x8 - (size & 0x7)); \ 101 else \ 102 target = size; 103 104 #define CTL_ALIGN_8B_MARGIN 16 105 106 /* 107 * Template mode pages. 108 */ 109 110 /* 111 * Note that these are default values only. The actual values will be 112 * filled in when the user does a mode sense. 113 */ 114 static struct copan_debugconf_subpage debugconf_page_default = { 115 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 116 DBGCNF_SUBPAGE_CODE, /* subpage */ 117 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 118 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 119 DBGCNF_VERSION, /* page_version */ 120 {CTL_TIME_IO_DEFAULT_SECS>>8, 121 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 122 }; 123 124 static struct copan_debugconf_subpage debugconf_page_changeable = { 125 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 126 DBGCNF_SUBPAGE_CODE, /* subpage */ 127 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 128 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 129 0, /* page_version */ 130 {0xff,0xff}, /* ctl_time_io_secs */ 131 }; 132 133 static struct scsi_da_rw_recovery_page rw_er_page_default = { 134 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 135 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 136 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 137 /*read_retry_count*/0, 138 /*correction_span*/0, 139 /*head_offset_count*/0, 140 /*data_strobe_offset_cnt*/0, 141 /*byte8*/SMS_RWER_LBPERE, 142 /*write_retry_count*/0, 143 /*reserved2*/0, 144 /*recovery_time_limit*/{0, 0}, 145 }; 146 147 static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 148 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 149 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 150 /*byte3*/0, 151 /*read_retry_count*/0, 152 /*correction_span*/0, 153 /*head_offset_count*/0, 154 /*data_strobe_offset_cnt*/0, 155 /*byte8*/0, 156 /*write_retry_count*/0, 157 /*reserved2*/0, 158 /*recovery_time_limit*/{0, 0}, 159 }; 160 161 static struct scsi_format_page format_page_default = { 162 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 163 /*page_length*/sizeof(struct scsi_format_page) - 2, 164 /*tracks_per_zone*/ {0, 0}, 165 /*alt_sectors_per_zone*/ {0, 0}, 166 /*alt_tracks_per_zone*/ {0, 0}, 167 /*alt_tracks_per_lun*/ {0, 0}, 168 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 169 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 170 /*bytes_per_sector*/ {0, 0}, 171 /*interleave*/ {0, 0}, 172 /*track_skew*/ {0, 0}, 173 /*cylinder_skew*/ {0, 0}, 174 /*flags*/ SFP_HSEC, 175 /*reserved*/ {0, 0, 0} 176 }; 177 178 static struct scsi_format_page format_page_changeable = { 179 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 180 /*page_length*/sizeof(struct scsi_format_page) - 2, 181 /*tracks_per_zone*/ {0, 0}, 182 /*alt_sectors_per_zone*/ {0, 0}, 183 /*alt_tracks_per_zone*/ {0, 0}, 184 /*alt_tracks_per_lun*/ {0, 0}, 185 /*sectors_per_track*/ {0, 0}, 186 /*bytes_per_sector*/ {0, 0}, 187 /*interleave*/ {0, 0}, 188 /*track_skew*/ {0, 0}, 189 /*cylinder_skew*/ {0, 0}, 190 /*flags*/ 0, 191 /*reserved*/ {0, 0, 0} 192 }; 193 194 static struct scsi_rigid_disk_page rigid_disk_page_default = { 195 /*page_code*/SMS_RIGID_DISK_PAGE, 196 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 197 /*cylinders*/ {0, 0, 0}, 198 /*heads*/ CTL_DEFAULT_HEADS, 199 /*start_write_precomp*/ {0, 0, 0}, 200 /*start_reduced_current*/ {0, 0, 0}, 201 /*step_rate*/ {0, 0}, 202 /*landing_zone_cylinder*/ {0, 0, 0}, 203 /*rpl*/ SRDP_RPL_DISABLED, 204 /*rotational_offset*/ 0, 205 /*reserved1*/ 0, 206 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 207 CTL_DEFAULT_ROTATION_RATE & 0xff}, 208 /*reserved2*/ {0, 0} 209 }; 210 211 static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 212 /*page_code*/SMS_RIGID_DISK_PAGE, 213 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 214 /*cylinders*/ {0, 0, 0}, 215 /*heads*/ 0, 216 /*start_write_precomp*/ {0, 0, 0}, 217 /*start_reduced_current*/ {0, 0, 0}, 218 /*step_rate*/ {0, 0}, 219 /*landing_zone_cylinder*/ {0, 0, 0}, 220 /*rpl*/ 0, 221 /*rotational_offset*/ 0, 222 /*reserved1*/ 0, 223 /*rotation_rate*/ {0, 0}, 224 /*reserved2*/ {0, 0} 225 }; 226 227 static struct scsi_caching_page caching_page_default = { 228 /*page_code*/SMS_CACHING_PAGE, 229 /*page_length*/sizeof(struct scsi_caching_page) - 2, 230 /*flags1*/ SCP_DISC | SCP_WCE, 231 /*ret_priority*/ 0, 232 /*disable_pf_transfer_len*/ {0xff, 0xff}, 233 /*min_prefetch*/ {0, 0}, 234 /*max_prefetch*/ {0xff, 0xff}, 235 /*max_pf_ceiling*/ {0xff, 0xff}, 236 /*flags2*/ 0, 237 /*cache_segments*/ 0, 238 /*cache_seg_size*/ {0, 0}, 239 /*reserved*/ 0, 240 /*non_cache_seg_size*/ {0, 0, 0} 241 }; 242 243 static struct scsi_caching_page caching_page_changeable = { 244 /*page_code*/SMS_CACHING_PAGE, 245 /*page_length*/sizeof(struct scsi_caching_page) - 2, 246 /*flags1*/ SCP_WCE | SCP_RCD, 247 /*ret_priority*/ 0, 248 /*disable_pf_transfer_len*/ {0, 0}, 249 /*min_prefetch*/ {0, 0}, 250 /*max_prefetch*/ {0, 0}, 251 /*max_pf_ceiling*/ {0, 0}, 252 /*flags2*/ 0, 253 /*cache_segments*/ 0, 254 /*cache_seg_size*/ {0, 0}, 255 /*reserved*/ 0, 256 /*non_cache_seg_size*/ {0, 0, 0} 257 }; 258 259 static struct scsi_control_page control_page_default = { 260 /*page_code*/SMS_CONTROL_MODE_PAGE, 261 /*page_length*/sizeof(struct scsi_control_page) - 2, 262 /*rlec*/0, 263 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 264 /*eca_and_aen*/0, 265 /*flags4*/SCP_TAS, 266 /*aen_holdoff_period*/{0, 0}, 267 /*busy_timeout_period*/{0, 0}, 268 /*extended_selftest_completion_time*/{0, 0} 269 }; 270 271 static struct scsi_control_page control_page_changeable = { 272 /*page_code*/SMS_CONTROL_MODE_PAGE, 273 /*page_length*/sizeof(struct scsi_control_page) - 2, 274 /*rlec*/SCP_DSENSE, 275 /*queue_flags*/SCP_QUEUE_ALG_MASK, 276 /*eca_and_aen*/SCP_SWP, 277 /*flags4*/0, 278 /*aen_holdoff_period*/{0, 0}, 279 /*busy_timeout_period*/{0, 0}, 280 /*extended_selftest_completion_time*/{0, 0} 281 }; 282 283 static struct scsi_info_exceptions_page ie_page_default = { 284 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 285 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 286 /*info_flags*/SIEP_FLAGS_DEXCPT, 287 /*mrie*/0, 288 /*interval_timer*/{0, 0, 0, 0}, 289 /*report_count*/{0, 0, 0, 0} 290 }; 291 292 static struct scsi_info_exceptions_page ie_page_changeable = { 293 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 294 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 295 /*info_flags*/0, 296 /*mrie*/0, 297 /*interval_timer*/{0, 0, 0, 0}, 298 /*report_count*/{0, 0, 0, 0} 299 }; 300 301 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 302 303 static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 304 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 305 /*subpage_code*/0x02, 306 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 307 /*flags*/0, 308 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 309 /*descr*/{}}, 310 {{/*flags*/0, 311 /*resource*/0x01, 312 /*reserved*/{0, 0}, 313 /*count*/{0, 0, 0, 0}}, 314 {/*flags*/0, 315 /*resource*/0x02, 316 /*reserved*/{0, 0}, 317 /*count*/{0, 0, 0, 0}}, 318 {/*flags*/0, 319 /*resource*/0xf1, 320 /*reserved*/{0, 0}, 321 /*count*/{0, 0, 0, 0}}, 322 {/*flags*/0, 323 /*resource*/0xf2, 324 /*reserved*/{0, 0}, 325 /*count*/{0, 0, 0, 0}} 326 } 327 }; 328 329 static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 330 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 331 /*subpage_code*/0x02, 332 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 333 /*flags*/0, 334 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 335 /*descr*/{}}, 336 {{/*flags*/0, 337 /*resource*/0, 338 /*reserved*/{0, 0}, 339 /*count*/{0, 0, 0, 0}}, 340 {/*flags*/0, 341 /*resource*/0, 342 /*reserved*/{0, 0}, 343 /*count*/{0, 0, 0, 0}}, 344 {/*flags*/0, 345 /*resource*/0, 346 /*reserved*/{0, 0}, 347 /*count*/{0, 0, 0, 0}}, 348 {/*flags*/0, 349 /*resource*/0, 350 /*reserved*/{0, 0}, 351 /*count*/{0, 0, 0, 0}} 352 } 353 }; 354 355 /* 356 * XXX KDM move these into the softc. 357 */ 358 static int rcv_sync_msg; 359 static int persis_offset; 360 static uint8_t ctl_pause_rtr; 361 362 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 363 static int worker_threads = -1; 364 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 365 &worker_threads, 1, "Number of worker threads"); 366 static int ctl_debug = CTL_DEBUG_NONE; 367 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 368 &ctl_debug, 0, "Enabled debug flags"); 369 370 /* 371 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 372 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 373 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 374 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 375 */ 376 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 377 378 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 379 int param); 380 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 381 static int ctl_init(void); 382 void ctl_shutdown(void); 383 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 384 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 385 static void ctl_ioctl_online(void *arg); 386 static void ctl_ioctl_offline(void *arg); 387 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 388 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 389 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 390 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 391 static int ctl_ioctl_submit_wait(union ctl_io *io); 392 static void ctl_ioctl_datamove(union ctl_io *io); 393 static void ctl_ioctl_done(union ctl_io *io); 394 static void ctl_ioctl_hard_startstop_callback(void *arg, 395 struct cfi_metatask *metatask); 396 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 397 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 398 struct ctl_ooa *ooa_hdr, 399 struct ctl_ooa_entry *kern_entries); 400 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 401 struct thread *td); 402 static uint32_t ctl_map_lun(int port_num, uint32_t lun); 403 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 404 #ifdef unused 405 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 406 uint32_t targ_target, uint32_t targ_lun, 407 int can_wait); 408 static void ctl_kfree_io(union ctl_io *io); 409 #endif /* unused */ 410 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 411 struct ctl_be_lun *be_lun, struct ctl_id target_id); 412 static int ctl_free_lun(struct ctl_lun *lun); 413 static void ctl_create_lun(struct ctl_be_lun *be_lun); 414 /** 415 static void ctl_failover_change_pages(struct ctl_softc *softc, 416 struct ctl_scsiio *ctsio, int master); 417 **/ 418 419 static int ctl_do_mode_select(union ctl_io *io); 420 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 421 uint64_t res_key, uint64_t sa_res_key, 422 uint8_t type, uint32_t residx, 423 struct ctl_scsiio *ctsio, 424 struct scsi_per_res_out *cdb, 425 struct scsi_per_res_out_parms* param); 426 static void ctl_pro_preempt_other(struct ctl_lun *lun, 427 union ctl_ha_msg *msg); 428 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 429 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 430 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 431 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 432 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 433 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 434 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 435 int alloc_len); 436 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 437 int alloc_len); 438 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 439 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 440 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 441 static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 442 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 443 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 444 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 445 union ctl_io *pending_io, union ctl_io *ooa_io); 446 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 447 union ctl_io *starting_io); 448 static int ctl_check_blocked(struct ctl_lun *lun); 449 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 450 struct ctl_lun *lun, 451 const struct ctl_cmd_entry *entry, 452 struct ctl_scsiio *ctsio); 453 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 454 static void ctl_failover(void); 455 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 456 struct ctl_scsiio *ctsio); 457 static int ctl_scsiio(struct ctl_scsiio *ctsio); 458 459 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 460 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 461 ctl_ua_type ua_type); 462 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 463 ctl_ua_type ua_type); 464 static int ctl_abort_task(union ctl_io *io); 465 static int ctl_abort_task_set(union ctl_io *io); 466 static int ctl_i_t_nexus_reset(union ctl_io *io); 467 static void ctl_run_task(union ctl_io *io); 468 #ifdef CTL_IO_DELAY 469 static void ctl_datamove_timer_wakeup(void *arg); 470 static void ctl_done_timer_wakeup(void *arg); 471 #endif /* CTL_IO_DELAY */ 472 473 static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 474 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 475 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 476 static void ctl_datamove_remote_write(union ctl_io *io); 477 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 478 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 479 static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 480 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 481 ctl_ha_dt_cb callback); 482 static void ctl_datamove_remote_read(union ctl_io *io); 483 static void ctl_datamove_remote(union ctl_io *io); 484 static int ctl_process_done(union ctl_io *io); 485 static void ctl_lun_thread(void *arg); 486 static void ctl_thresh_thread(void *arg); 487 static void ctl_work_thread(void *arg); 488 static void ctl_enqueue_incoming(union ctl_io *io); 489 static void ctl_enqueue_rtr(union ctl_io *io); 490 static void ctl_enqueue_done(union ctl_io *io); 491 static void ctl_enqueue_isc(union ctl_io *io); 492 static const struct ctl_cmd_entry * 493 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 494 static const struct ctl_cmd_entry * 495 ctl_validate_command(struct ctl_scsiio *ctsio); 496 static int ctl_cmd_applicable(uint8_t lun_type, 497 const struct ctl_cmd_entry *entry); 498 499 /* 500 * Load the serialization table. This isn't very pretty, but is probably 501 * the easiest way to do it. 502 */ 503 #include "ctl_ser_table.c" 504 505 /* 506 * We only need to define open, close and ioctl routines for this driver. 507 */ 508 static struct cdevsw ctl_cdevsw = { 509 .d_version = D_VERSION, 510 .d_flags = 0, 511 .d_open = ctl_open, 512 .d_close = ctl_close, 513 .d_ioctl = ctl_ioctl, 514 .d_name = "ctl", 515 }; 516 517 518 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 519 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 520 521 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 522 523 static moduledata_t ctl_moduledata = { 524 "ctl", 525 ctl_module_event_handler, 526 NULL 527 }; 528 529 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 530 MODULE_VERSION(ctl, 1); 531 532 static struct ctl_frontend ioctl_frontend = 533 { 534 .name = "ioctl", 535 }; 536 537 static void 538 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 539 union ctl_ha_msg *msg_info) 540 { 541 struct ctl_scsiio *ctsio; 542 543 if (msg_info->hdr.original_sc == NULL) { 544 printf("%s: original_sc == NULL!\n", __func__); 545 /* XXX KDM now what? */ 546 return; 547 } 548 549 ctsio = &msg_info->hdr.original_sc->scsiio; 550 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 551 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 552 ctsio->io_hdr.status = msg_info->hdr.status; 553 ctsio->scsi_status = msg_info->scsi.scsi_status; 554 ctsio->sense_len = msg_info->scsi.sense_len; 555 ctsio->sense_residual = msg_info->scsi.sense_residual; 556 ctsio->residual = msg_info->scsi.residual; 557 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 558 sizeof(ctsio->sense_data)); 559 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 560 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 561 ctl_enqueue_isc((union ctl_io *)ctsio); 562 } 563 564 static void 565 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 566 union ctl_ha_msg *msg_info) 567 { 568 struct ctl_scsiio *ctsio; 569 570 if (msg_info->hdr.serializing_sc == NULL) { 571 printf("%s: serializing_sc == NULL!\n", __func__); 572 /* XXX KDM now what? */ 573 return; 574 } 575 576 ctsio = &msg_info->hdr.serializing_sc->scsiio; 577 #if 0 578 /* 579 * Attempt to catch the situation where an I/O has 580 * been freed, and we're using it again. 581 */ 582 if (ctsio->io_hdr.io_type == 0xff) { 583 union ctl_io *tmp_io; 584 tmp_io = (union ctl_io *)ctsio; 585 printf("%s: %p use after free!\n", __func__, 586 ctsio); 587 printf("%s: type %d msg %d cdb %x iptl: " 588 "%d:%d:%d:%d tag 0x%04x " 589 "flag %#x status %x\n", 590 __func__, 591 tmp_io->io_hdr.io_type, 592 tmp_io->io_hdr.msg_type, 593 tmp_io->scsiio.cdb[0], 594 tmp_io->io_hdr.nexus.initid.id, 595 tmp_io->io_hdr.nexus.targ_port, 596 tmp_io->io_hdr.nexus.targ_target.id, 597 tmp_io->io_hdr.nexus.targ_lun, 598 (tmp_io->io_hdr.io_type == 599 CTL_IO_TASK) ? 600 tmp_io->taskio.tag_num : 601 tmp_io->scsiio.tag_num, 602 tmp_io->io_hdr.flags, 603 tmp_io->io_hdr.status); 604 } 605 #endif 606 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 607 ctl_enqueue_isc((union ctl_io *)ctsio); 608 } 609 610 /* 611 * ISC (Inter Shelf Communication) event handler. Events from the HA 612 * subsystem come in here. 613 */ 614 static void 615 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 616 { 617 struct ctl_softc *ctl_softc; 618 union ctl_io *io; 619 struct ctl_prio *presio; 620 ctl_ha_status isc_status; 621 622 ctl_softc = control_softc; 623 io = NULL; 624 625 626 #if 0 627 printf("CTL: Isc Msg event %d\n", event); 628 #endif 629 if (event == CTL_HA_EVT_MSG_RECV) { 630 union ctl_ha_msg msg_info; 631 632 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 633 sizeof(msg_info), /*wait*/ 0); 634 #if 0 635 printf("CTL: msg_type %d\n", msg_info.msg_type); 636 #endif 637 if (isc_status != 0) { 638 printf("Error receiving message, status = %d\n", 639 isc_status); 640 return; 641 } 642 643 switch (msg_info.hdr.msg_type) { 644 case CTL_MSG_SERIALIZE: 645 #if 0 646 printf("Serialize\n"); 647 #endif 648 io = ctl_alloc_io_nowait(ctl_softc->othersc_pool); 649 if (io == NULL) { 650 printf("ctl_isc_event_handler: can't allocate " 651 "ctl_io!\n"); 652 /* Bad Juju */ 653 /* Need to set busy and send msg back */ 654 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 655 msg_info.hdr.status = CTL_SCSI_ERROR; 656 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 657 msg_info.scsi.sense_len = 0; 658 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 659 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 660 } 661 goto bailout; 662 } 663 ctl_zero_io(io); 664 // populate ctsio from msg_info 665 io->io_hdr.io_type = CTL_IO_SCSI; 666 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 667 io->io_hdr.original_sc = msg_info.hdr.original_sc; 668 #if 0 669 printf("pOrig %x\n", (int)msg_info.original_sc); 670 #endif 671 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 672 CTL_FLAG_IO_ACTIVE; 673 /* 674 * If we're in serialization-only mode, we don't 675 * want to go through full done processing. Thus 676 * the COPY flag. 677 * 678 * XXX KDM add another flag that is more specific. 679 */ 680 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 681 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 682 io->io_hdr.nexus = msg_info.hdr.nexus; 683 #if 0 684 printf("targ %d, port %d, iid %d, lun %d\n", 685 io->io_hdr.nexus.targ_target.id, 686 io->io_hdr.nexus.targ_port, 687 io->io_hdr.nexus.initid.id, 688 io->io_hdr.nexus.targ_lun); 689 #endif 690 io->scsiio.tag_num = msg_info.scsi.tag_num; 691 io->scsiio.tag_type = msg_info.scsi.tag_type; 692 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 693 CTL_MAX_CDBLEN); 694 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 695 const struct ctl_cmd_entry *entry; 696 697 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 698 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 699 io->io_hdr.flags |= 700 entry->flags & CTL_FLAG_DATA_MASK; 701 } 702 ctl_enqueue_isc(io); 703 break; 704 705 /* Performed on the Originating SC, XFER mode only */ 706 case CTL_MSG_DATAMOVE: { 707 struct ctl_sg_entry *sgl; 708 int i, j; 709 710 io = msg_info.hdr.original_sc; 711 if (io == NULL) { 712 printf("%s: original_sc == NULL!\n", __func__); 713 /* XXX KDM do something here */ 714 break; 715 } 716 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 717 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 718 /* 719 * Keep track of this, we need to send it back over 720 * when the datamove is complete. 721 */ 722 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 723 724 if (msg_info.dt.sg_sequence == 0) { 725 /* 726 * XXX KDM we use the preallocated S/G list 727 * here, but we'll need to change this to 728 * dynamic allocation if we need larger S/G 729 * lists. 730 */ 731 if (msg_info.dt.kern_sg_entries > 732 sizeof(io->io_hdr.remote_sglist) / 733 sizeof(io->io_hdr.remote_sglist[0])) { 734 printf("%s: number of S/G entries " 735 "needed %u > allocated num %zd\n", 736 __func__, 737 msg_info.dt.kern_sg_entries, 738 sizeof(io->io_hdr.remote_sglist)/ 739 sizeof(io->io_hdr.remote_sglist[0])); 740 741 /* 742 * XXX KDM send a message back to 743 * the other side to shut down the 744 * DMA. The error will come back 745 * through via the normal channel. 746 */ 747 break; 748 } 749 sgl = io->io_hdr.remote_sglist; 750 memset(sgl, 0, 751 sizeof(io->io_hdr.remote_sglist)); 752 753 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 754 755 io->scsiio.kern_sg_entries = 756 msg_info.dt.kern_sg_entries; 757 io->scsiio.rem_sg_entries = 758 msg_info.dt.kern_sg_entries; 759 io->scsiio.kern_data_len = 760 msg_info.dt.kern_data_len; 761 io->scsiio.kern_total_len = 762 msg_info.dt.kern_total_len; 763 io->scsiio.kern_data_resid = 764 msg_info.dt.kern_data_resid; 765 io->scsiio.kern_rel_offset = 766 msg_info.dt.kern_rel_offset; 767 /* 768 * Clear out per-DMA flags. 769 */ 770 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 771 /* 772 * Add per-DMA flags that are set for this 773 * particular DMA request. 774 */ 775 io->io_hdr.flags |= msg_info.dt.flags & 776 CTL_FLAG_RDMA_MASK; 777 } else 778 sgl = (struct ctl_sg_entry *) 779 io->scsiio.kern_data_ptr; 780 781 for (i = msg_info.dt.sent_sg_entries, j = 0; 782 i < (msg_info.dt.sent_sg_entries + 783 msg_info.dt.cur_sg_entries); i++, j++) { 784 sgl[i].addr = msg_info.dt.sg_list[j].addr; 785 sgl[i].len = msg_info.dt.sg_list[j].len; 786 787 #if 0 788 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 789 __func__, 790 msg_info.dt.sg_list[j].addr, 791 msg_info.dt.sg_list[j].len, 792 sgl[i].addr, sgl[i].len, j, i); 793 #endif 794 } 795 #if 0 796 memcpy(&sgl[msg_info.dt.sent_sg_entries], 797 msg_info.dt.sg_list, 798 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 799 #endif 800 801 /* 802 * If this is the last piece of the I/O, we've got 803 * the full S/G list. Queue processing in the thread. 804 * Otherwise wait for the next piece. 805 */ 806 if (msg_info.dt.sg_last != 0) 807 ctl_enqueue_isc(io); 808 break; 809 } 810 /* Performed on the Serializing (primary) SC, XFER mode only */ 811 case CTL_MSG_DATAMOVE_DONE: { 812 if (msg_info.hdr.serializing_sc == NULL) { 813 printf("%s: serializing_sc == NULL!\n", 814 __func__); 815 /* XXX KDM now what? */ 816 break; 817 } 818 /* 819 * We grab the sense information here in case 820 * there was a failure, so we can return status 821 * back to the initiator. 822 */ 823 io = msg_info.hdr.serializing_sc; 824 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 825 io->io_hdr.status = msg_info.hdr.status; 826 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 827 io->scsiio.sense_len = msg_info.scsi.sense_len; 828 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 829 io->io_hdr.port_status = msg_info.scsi.fetd_status; 830 io->scsiio.residual = msg_info.scsi.residual; 831 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 832 sizeof(io->scsiio.sense_data)); 833 ctl_enqueue_isc(io); 834 break; 835 } 836 837 /* Preformed on Originating SC, SER_ONLY mode */ 838 case CTL_MSG_R2R: 839 io = msg_info.hdr.original_sc; 840 if (io == NULL) { 841 printf("%s: Major Bummer\n", __func__); 842 return; 843 } else { 844 #if 0 845 printf("pOrig %x\n",(int) ctsio); 846 #endif 847 } 848 io->io_hdr.msg_type = CTL_MSG_R2R; 849 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 850 ctl_enqueue_isc(io); 851 break; 852 853 /* 854 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 855 * mode. 856 * Performed on the Originating (i.e. secondary) SC in XFER 857 * mode 858 */ 859 case CTL_MSG_FINISH_IO: 860 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 861 ctl_isc_handler_finish_xfer(ctl_softc, 862 &msg_info); 863 else 864 ctl_isc_handler_finish_ser_only(ctl_softc, 865 &msg_info); 866 break; 867 868 /* Preformed on Originating SC */ 869 case CTL_MSG_BAD_JUJU: 870 io = msg_info.hdr.original_sc; 871 if (io == NULL) { 872 printf("%s: Bad JUJU!, original_sc is NULL!\n", 873 __func__); 874 break; 875 } 876 ctl_copy_sense_data(&msg_info, io); 877 /* 878 * IO should have already been cleaned up on other 879 * SC so clear this flag so we won't send a message 880 * back to finish the IO there. 881 */ 882 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 883 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 884 885 /* io = msg_info.hdr.serializing_sc; */ 886 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 887 ctl_enqueue_isc(io); 888 break; 889 890 /* Handle resets sent from the other side */ 891 case CTL_MSG_MANAGE_TASKS: { 892 struct ctl_taskio *taskio; 893 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait( 894 ctl_softc->othersc_pool); 895 if (taskio == NULL) { 896 printf("ctl_isc_event_handler: can't allocate " 897 "ctl_io!\n"); 898 /* Bad Juju */ 899 /* should I just call the proper reset func 900 here??? */ 901 goto bailout; 902 } 903 ctl_zero_io((union ctl_io *)taskio); 904 taskio->io_hdr.io_type = CTL_IO_TASK; 905 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 906 taskio->io_hdr.nexus = msg_info.hdr.nexus; 907 taskio->task_action = msg_info.task.task_action; 908 taskio->tag_num = msg_info.task.tag_num; 909 taskio->tag_type = msg_info.task.tag_type; 910 #ifdef CTL_TIME_IO 911 taskio->io_hdr.start_time = time_uptime; 912 getbintime(&taskio->io_hdr.start_bt); 913 #if 0 914 cs_prof_gettime(&taskio->io_hdr.start_ticks); 915 #endif 916 #endif /* CTL_TIME_IO */ 917 ctl_run_task((union ctl_io *)taskio); 918 break; 919 } 920 /* Persistent Reserve action which needs attention */ 921 case CTL_MSG_PERS_ACTION: 922 presio = (struct ctl_prio *)ctl_alloc_io_nowait( 923 ctl_softc->othersc_pool); 924 if (presio == NULL) { 925 printf("ctl_isc_event_handler: can't allocate " 926 "ctl_io!\n"); 927 /* Bad Juju */ 928 /* Need to set busy and send msg back */ 929 goto bailout; 930 } 931 ctl_zero_io((union ctl_io *)presio); 932 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 933 presio->pr_msg = msg_info.pr; 934 ctl_enqueue_isc((union ctl_io *)presio); 935 break; 936 case CTL_MSG_SYNC_FE: 937 rcv_sync_msg = 1; 938 break; 939 default: 940 printf("How did I get here?\n"); 941 } 942 } else if (event == CTL_HA_EVT_MSG_SENT) { 943 if (param != CTL_HA_STATUS_SUCCESS) { 944 printf("Bad status from ctl_ha_msg_send status %d\n", 945 param); 946 } 947 return; 948 } else if (event == CTL_HA_EVT_DISCONNECT) { 949 printf("CTL: Got a disconnect from Isc\n"); 950 return; 951 } else { 952 printf("ctl_isc_event_handler: Unknown event %d\n", event); 953 return; 954 } 955 956 bailout: 957 return; 958 } 959 960 static void 961 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 962 { 963 struct scsi_sense_data *sense; 964 965 sense = &dest->scsiio.sense_data; 966 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 967 dest->scsiio.scsi_status = src->scsi.scsi_status; 968 dest->scsiio.sense_len = src->scsi.sense_len; 969 dest->io_hdr.status = src->hdr.status; 970 } 971 972 static int 973 ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS) 974 { 975 struct ctl_softc *softc = (struct ctl_softc *)arg1; 976 struct ctl_lun *lun; 977 int error, value, i; 978 979 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) 980 value = 0; 981 else 982 value = 1; 983 984 error = sysctl_handle_int(oidp, &value, 0, req); 985 if ((error != 0) || (req->newptr == NULL)) 986 return (error); 987 988 mtx_lock(&softc->ctl_lock); 989 if (value == 0) 990 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 991 else 992 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 993 STAILQ_FOREACH(lun, &softc->lun_list, links) { 994 mtx_lock(&lun->lun_lock); 995 for (i = 0; i < CTL_MAX_INITIATORS; i++) 996 lun->pending_ua[i] |= CTL_UA_ASYM_ACC_CHANGE; 997 mtx_unlock(&lun->lun_lock); 998 } 999 mtx_unlock(&softc->ctl_lock); 1000 return (0); 1001 } 1002 1003 static int 1004 ctl_init(void) 1005 { 1006 struct ctl_softc *softc; 1007 void *other_pool; 1008 struct ctl_port *port; 1009 int i, error, retval; 1010 //int isc_retval; 1011 1012 retval = 0; 1013 ctl_pause_rtr = 0; 1014 rcv_sync_msg = 0; 1015 1016 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1017 M_WAITOK | M_ZERO); 1018 softc = control_softc; 1019 1020 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1021 "cam/ctl"); 1022 1023 softc->dev->si_drv1 = softc; 1024 1025 /* 1026 * By default, return a "bad LUN" peripheral qualifier for unknown 1027 * LUNs. The user can override this default using the tunable or 1028 * sysctl. See the comment in ctl_inquiry_std() for more details. 1029 */ 1030 softc->inquiry_pq_no_lun = 1; 1031 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 1032 &softc->inquiry_pq_no_lun); 1033 sysctl_ctx_init(&softc->sysctl_ctx); 1034 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1035 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1036 CTLFLAG_RD, 0, "CAM Target Layer"); 1037 1038 if (softc->sysctl_tree == NULL) { 1039 printf("%s: unable to allocate sysctl tree\n", __func__); 1040 destroy_dev(softc->dev); 1041 free(control_softc, M_DEVBUF); 1042 control_softc = NULL; 1043 return (ENOMEM); 1044 } 1045 1046 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1047 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1048 "inquiry_pq_no_lun", CTLFLAG_RW, 1049 &softc->inquiry_pq_no_lun, 0, 1050 "Report no lun possible for invalid LUNs"); 1051 1052 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1053 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1054 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1055 softc->open_count = 0; 1056 1057 /* 1058 * Default to actually sending a SYNCHRONIZE CACHE command down to 1059 * the drive. 1060 */ 1061 softc->flags = CTL_FLAG_REAL_SYNC; 1062 1063 /* 1064 * In Copan's HA scheme, the "master" and "slave" roles are 1065 * figured out through the slot the controller is in. Although it 1066 * is an active/active system, someone has to be in charge. 1067 */ 1068 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1069 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1070 "HA head ID (0 - no HA)"); 1071 if (softc->ha_id == 0) { 1072 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1073 softc->is_single = 1; 1074 softc->port_offset = 0; 1075 } else 1076 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS; 1077 persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT; 1078 1079 /* 1080 * XXX KDM need to figure out where we want to get our target ID 1081 * and WWID. Is it different on each port? 1082 */ 1083 softc->target.id = 0; 1084 softc->target.wwid[0] = 0x12345678; 1085 softc->target.wwid[1] = 0x87654321; 1086 STAILQ_INIT(&softc->lun_list); 1087 STAILQ_INIT(&softc->pending_lun_queue); 1088 STAILQ_INIT(&softc->fe_list); 1089 STAILQ_INIT(&softc->port_list); 1090 STAILQ_INIT(&softc->be_list); 1091 ctl_tpc_init(softc); 1092 1093 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1094 &other_pool) != 0) 1095 { 1096 printf("ctl: can't allocate %d entry other SC pool, " 1097 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1098 return (ENOMEM); 1099 } 1100 softc->othersc_pool = other_pool; 1101 1102 if (worker_threads <= 0) 1103 worker_threads = max(1, mp_ncpus / 4); 1104 if (worker_threads > CTL_MAX_THREADS) 1105 worker_threads = CTL_MAX_THREADS; 1106 1107 for (i = 0; i < worker_threads; i++) { 1108 struct ctl_thread *thr = &softc->threads[i]; 1109 1110 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1111 thr->ctl_softc = softc; 1112 STAILQ_INIT(&thr->incoming_queue); 1113 STAILQ_INIT(&thr->rtr_queue); 1114 STAILQ_INIT(&thr->done_queue); 1115 STAILQ_INIT(&thr->isc_queue); 1116 1117 error = kproc_kthread_add(ctl_work_thread, thr, 1118 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1119 if (error != 0) { 1120 printf("error creating CTL work thread!\n"); 1121 ctl_pool_free(other_pool); 1122 return (error); 1123 } 1124 } 1125 error = kproc_kthread_add(ctl_lun_thread, softc, 1126 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1127 if (error != 0) { 1128 printf("error creating CTL lun thread!\n"); 1129 ctl_pool_free(other_pool); 1130 return (error); 1131 } 1132 error = kproc_kthread_add(ctl_thresh_thread, softc, 1133 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1134 if (error != 0) { 1135 printf("error creating CTL threshold thread!\n"); 1136 ctl_pool_free(other_pool); 1137 return (error); 1138 } 1139 if (bootverbose) 1140 printf("ctl: CAM Target Layer loaded\n"); 1141 1142 /* 1143 * Initialize the ioctl front end. 1144 */ 1145 ctl_frontend_register(&ioctl_frontend); 1146 port = &softc->ioctl_info.port; 1147 port->frontend = &ioctl_frontend; 1148 sprintf(softc->ioctl_info.port_name, "ioctl"); 1149 port->port_type = CTL_PORT_IOCTL; 1150 port->num_requested_ctl_io = 100; 1151 port->port_name = softc->ioctl_info.port_name; 1152 port->port_online = ctl_ioctl_online; 1153 port->port_offline = ctl_ioctl_offline; 1154 port->onoff_arg = &softc->ioctl_info; 1155 port->lun_enable = ctl_ioctl_lun_enable; 1156 port->lun_disable = ctl_ioctl_lun_disable; 1157 port->targ_lun_arg = &softc->ioctl_info; 1158 port->fe_datamove = ctl_ioctl_datamove; 1159 port->fe_done = ctl_ioctl_done; 1160 port->max_targets = 15; 1161 port->max_target_id = 15; 1162 1163 if (ctl_port_register(&softc->ioctl_info.port) != 0) { 1164 printf("ctl: ioctl front end registration failed, will " 1165 "continue anyway\n"); 1166 } 1167 1168 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1169 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN, 1170 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head"); 1171 1172 #ifdef CTL_IO_DELAY 1173 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1174 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1175 sizeof(struct callout), CTL_TIMER_BYTES); 1176 return (EINVAL); 1177 } 1178 #endif /* CTL_IO_DELAY */ 1179 1180 return (0); 1181 } 1182 1183 void 1184 ctl_shutdown(void) 1185 { 1186 struct ctl_softc *softc; 1187 struct ctl_lun *lun, *next_lun; 1188 1189 softc = (struct ctl_softc *)control_softc; 1190 1191 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1192 printf("ctl: ioctl front end deregistration failed\n"); 1193 1194 mtx_lock(&softc->ctl_lock); 1195 1196 /* 1197 * Free up each LUN. 1198 */ 1199 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1200 next_lun = STAILQ_NEXT(lun, links); 1201 ctl_free_lun(lun); 1202 } 1203 1204 mtx_unlock(&softc->ctl_lock); 1205 1206 ctl_frontend_deregister(&ioctl_frontend); 1207 1208 #if 0 1209 ctl_shutdown_thread(softc->work_thread); 1210 mtx_destroy(&softc->queue_lock); 1211 #endif 1212 1213 ctl_tpc_shutdown(softc); 1214 uma_zdestroy(softc->io_zone); 1215 mtx_destroy(&softc->ctl_lock); 1216 1217 destroy_dev(softc->dev); 1218 1219 sysctl_ctx_free(&softc->sysctl_ctx); 1220 1221 free(control_softc, M_DEVBUF); 1222 control_softc = NULL; 1223 1224 if (bootverbose) 1225 printf("ctl: CAM Target Layer unloaded\n"); 1226 } 1227 1228 static int 1229 ctl_module_event_handler(module_t mod, int what, void *arg) 1230 { 1231 1232 switch (what) { 1233 case MOD_LOAD: 1234 return (ctl_init()); 1235 case MOD_UNLOAD: 1236 return (EBUSY); 1237 default: 1238 return (EOPNOTSUPP); 1239 } 1240 } 1241 1242 /* 1243 * XXX KDM should we do some access checks here? Bump a reference count to 1244 * prevent a CTL module from being unloaded while someone has it open? 1245 */ 1246 static int 1247 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1248 { 1249 return (0); 1250 } 1251 1252 static int 1253 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1254 { 1255 return (0); 1256 } 1257 1258 int 1259 ctl_port_enable(ctl_port_type port_type) 1260 { 1261 struct ctl_softc *softc = control_softc; 1262 struct ctl_port *port; 1263 1264 if (softc->is_single == 0) { 1265 union ctl_ha_msg msg_info; 1266 int isc_retval; 1267 1268 #if 0 1269 printf("%s: HA mode, synchronizing frontend enable\n", 1270 __func__); 1271 #endif 1272 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1273 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1274 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1275 printf("Sync msg send error retval %d\n", isc_retval); 1276 } 1277 if (!rcv_sync_msg) { 1278 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1279 sizeof(msg_info), 1); 1280 } 1281 #if 0 1282 printf("CTL:Frontend Enable\n"); 1283 } else { 1284 printf("%s: single mode, skipping frontend synchronization\n", 1285 __func__); 1286 #endif 1287 } 1288 1289 STAILQ_FOREACH(port, &softc->port_list, links) { 1290 if (port_type & port->port_type) 1291 { 1292 #if 0 1293 printf("port %d\n", port->targ_port); 1294 #endif 1295 ctl_port_online(port); 1296 } 1297 } 1298 1299 return (0); 1300 } 1301 1302 int 1303 ctl_port_disable(ctl_port_type port_type) 1304 { 1305 struct ctl_softc *softc; 1306 struct ctl_port *port; 1307 1308 softc = control_softc; 1309 1310 STAILQ_FOREACH(port, &softc->port_list, links) { 1311 if (port_type & port->port_type) 1312 ctl_port_offline(port); 1313 } 1314 1315 return (0); 1316 } 1317 1318 /* 1319 * Returns 0 for success, 1 for failure. 1320 * Currently the only failure mode is if there aren't enough entries 1321 * allocated. So, in case of a failure, look at num_entries_dropped, 1322 * reallocate and try again. 1323 */ 1324 int 1325 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1326 int *num_entries_filled, int *num_entries_dropped, 1327 ctl_port_type port_type, int no_virtual) 1328 { 1329 struct ctl_softc *softc; 1330 struct ctl_port *port; 1331 int entries_dropped, entries_filled; 1332 int retval; 1333 int i; 1334 1335 softc = control_softc; 1336 1337 retval = 0; 1338 entries_filled = 0; 1339 entries_dropped = 0; 1340 1341 i = 0; 1342 mtx_lock(&softc->ctl_lock); 1343 STAILQ_FOREACH(port, &softc->port_list, links) { 1344 struct ctl_port_entry *entry; 1345 1346 if ((port->port_type & port_type) == 0) 1347 continue; 1348 1349 if ((no_virtual != 0) 1350 && (port->virtual_port != 0)) 1351 continue; 1352 1353 if (entries_filled >= num_entries_alloced) { 1354 entries_dropped++; 1355 continue; 1356 } 1357 entry = &entries[i]; 1358 1359 entry->port_type = port->port_type; 1360 strlcpy(entry->port_name, port->port_name, 1361 sizeof(entry->port_name)); 1362 entry->physical_port = port->physical_port; 1363 entry->virtual_port = port->virtual_port; 1364 entry->wwnn = port->wwnn; 1365 entry->wwpn = port->wwpn; 1366 1367 i++; 1368 entries_filled++; 1369 } 1370 1371 mtx_unlock(&softc->ctl_lock); 1372 1373 if (entries_dropped > 0) 1374 retval = 1; 1375 1376 *num_entries_dropped = entries_dropped; 1377 *num_entries_filled = entries_filled; 1378 1379 return (retval); 1380 } 1381 1382 static void 1383 ctl_ioctl_online(void *arg) 1384 { 1385 struct ctl_ioctl_info *ioctl_info; 1386 1387 ioctl_info = (struct ctl_ioctl_info *)arg; 1388 1389 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1390 } 1391 1392 static void 1393 ctl_ioctl_offline(void *arg) 1394 { 1395 struct ctl_ioctl_info *ioctl_info; 1396 1397 ioctl_info = (struct ctl_ioctl_info *)arg; 1398 1399 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1400 } 1401 1402 /* 1403 * Remove an initiator by port number and initiator ID. 1404 * Returns 0 for success, -1 for failure. 1405 */ 1406 int 1407 ctl_remove_initiator(struct ctl_port *port, int iid) 1408 { 1409 struct ctl_softc *softc = control_softc; 1410 1411 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1412 1413 if (iid > CTL_MAX_INIT_PER_PORT) { 1414 printf("%s: initiator ID %u > maximun %u!\n", 1415 __func__, iid, CTL_MAX_INIT_PER_PORT); 1416 return (-1); 1417 } 1418 1419 mtx_lock(&softc->ctl_lock); 1420 port->wwpn_iid[iid].in_use--; 1421 port->wwpn_iid[iid].last_use = time_uptime; 1422 mtx_unlock(&softc->ctl_lock); 1423 1424 return (0); 1425 } 1426 1427 /* 1428 * Add an initiator to the initiator map. 1429 * Returns iid for success, < 0 for failure. 1430 */ 1431 int 1432 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1433 { 1434 struct ctl_softc *softc = control_softc; 1435 time_t best_time; 1436 int i, best; 1437 1438 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1439 1440 if (iid >= CTL_MAX_INIT_PER_PORT) { 1441 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1442 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1443 free(name, M_CTL); 1444 return (-1); 1445 } 1446 1447 mtx_lock(&softc->ctl_lock); 1448 1449 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1450 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1451 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1452 iid = i; 1453 break; 1454 } 1455 if (name != NULL && port->wwpn_iid[i].name != NULL && 1456 strcmp(name, port->wwpn_iid[i].name) == 0) { 1457 iid = i; 1458 break; 1459 } 1460 } 1461 } 1462 1463 if (iid < 0) { 1464 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1465 if (port->wwpn_iid[i].in_use == 0 && 1466 port->wwpn_iid[i].wwpn == 0 && 1467 port->wwpn_iid[i].name == NULL) { 1468 iid = i; 1469 break; 1470 } 1471 } 1472 } 1473 1474 if (iid < 0) { 1475 best = -1; 1476 best_time = INT32_MAX; 1477 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1478 if (port->wwpn_iid[i].in_use == 0) { 1479 if (port->wwpn_iid[i].last_use < best_time) { 1480 best = i; 1481 best_time = port->wwpn_iid[i].last_use; 1482 } 1483 } 1484 } 1485 iid = best; 1486 } 1487 1488 if (iid < 0) { 1489 mtx_unlock(&softc->ctl_lock); 1490 free(name, M_CTL); 1491 return (-2); 1492 } 1493 1494 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1495 /* 1496 * This is not an error yet. 1497 */ 1498 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1499 #if 0 1500 printf("%s: port %d iid %u WWPN %#jx arrived" 1501 " again\n", __func__, port->targ_port, 1502 iid, (uintmax_t)wwpn); 1503 #endif 1504 goto take; 1505 } 1506 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1507 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1508 #if 0 1509 printf("%s: port %d iid %u name '%s' arrived" 1510 " again\n", __func__, port->targ_port, 1511 iid, name); 1512 #endif 1513 goto take; 1514 } 1515 1516 /* 1517 * This is an error, but what do we do about it? The 1518 * driver is telling us we have a new WWPN for this 1519 * initiator ID, so we pretty much need to use it. 1520 */ 1521 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1522 " but WWPN %#jx '%s' is still at that address\n", 1523 __func__, port->targ_port, iid, wwpn, name, 1524 (uintmax_t)port->wwpn_iid[iid].wwpn, 1525 port->wwpn_iid[iid].name); 1526 1527 /* 1528 * XXX KDM clear have_ca and ua_pending on each LUN for 1529 * this initiator. 1530 */ 1531 } 1532 take: 1533 free(port->wwpn_iid[iid].name, M_CTL); 1534 port->wwpn_iid[iid].name = name; 1535 port->wwpn_iid[iid].wwpn = wwpn; 1536 port->wwpn_iid[iid].in_use++; 1537 mtx_unlock(&softc->ctl_lock); 1538 1539 return (iid); 1540 } 1541 1542 static int 1543 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1544 { 1545 int len; 1546 1547 switch (port->port_type) { 1548 case CTL_PORT_FC: 1549 { 1550 struct scsi_transportid_fcp *id = 1551 (struct scsi_transportid_fcp *)buf; 1552 if (port->wwpn_iid[iid].wwpn == 0) 1553 return (0); 1554 memset(id, 0, sizeof(*id)); 1555 id->format_protocol = SCSI_PROTO_FC; 1556 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1557 return (sizeof(*id)); 1558 } 1559 case CTL_PORT_ISCSI: 1560 { 1561 struct scsi_transportid_iscsi_port *id = 1562 (struct scsi_transportid_iscsi_port *)buf; 1563 if (port->wwpn_iid[iid].name == NULL) 1564 return (0); 1565 memset(id, 0, 256); 1566 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1567 SCSI_PROTO_ISCSI; 1568 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1569 len = roundup2(min(len, 252), 4); 1570 scsi_ulto2b(len, id->additional_length); 1571 return (sizeof(*id) + len); 1572 } 1573 case CTL_PORT_SAS: 1574 { 1575 struct scsi_transportid_sas *id = 1576 (struct scsi_transportid_sas *)buf; 1577 if (port->wwpn_iid[iid].wwpn == 0) 1578 return (0); 1579 memset(id, 0, sizeof(*id)); 1580 id->format_protocol = SCSI_PROTO_SAS; 1581 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1582 return (sizeof(*id)); 1583 } 1584 default: 1585 { 1586 struct scsi_transportid_spi *id = 1587 (struct scsi_transportid_spi *)buf; 1588 memset(id, 0, sizeof(*id)); 1589 id->format_protocol = SCSI_PROTO_SPI; 1590 scsi_ulto2b(iid, id->scsi_addr); 1591 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1592 return (sizeof(*id)); 1593 } 1594 } 1595 } 1596 1597 static int 1598 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1599 { 1600 return (0); 1601 } 1602 1603 static int 1604 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1605 { 1606 return (0); 1607 } 1608 1609 /* 1610 * Data movement routine for the CTL ioctl frontend port. 1611 */ 1612 static int 1613 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1614 { 1615 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1616 struct ctl_sg_entry ext_entry, kern_entry; 1617 int ext_sglen, ext_sg_entries, kern_sg_entries; 1618 int ext_sg_start, ext_offset; 1619 int len_to_copy, len_copied; 1620 int kern_watermark, ext_watermark; 1621 int ext_sglist_malloced; 1622 int i, j; 1623 1624 ext_sglist_malloced = 0; 1625 ext_sg_start = 0; 1626 ext_offset = 0; 1627 1628 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1629 1630 /* 1631 * If this flag is set, fake the data transfer. 1632 */ 1633 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1634 ctsio->ext_data_filled = ctsio->ext_data_len; 1635 goto bailout; 1636 } 1637 1638 /* 1639 * To simplify things here, if we have a single buffer, stick it in 1640 * a S/G entry and just make it a single entry S/G list. 1641 */ 1642 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1643 int len_seen; 1644 1645 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1646 1647 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1648 M_WAITOK); 1649 ext_sglist_malloced = 1; 1650 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1651 ext_sglen) != 0) { 1652 ctl_set_internal_failure(ctsio, 1653 /*sks_valid*/ 0, 1654 /*retry_count*/ 0); 1655 goto bailout; 1656 } 1657 ext_sg_entries = ctsio->ext_sg_entries; 1658 len_seen = 0; 1659 for (i = 0; i < ext_sg_entries; i++) { 1660 if ((len_seen + ext_sglist[i].len) >= 1661 ctsio->ext_data_filled) { 1662 ext_sg_start = i; 1663 ext_offset = ctsio->ext_data_filled - len_seen; 1664 break; 1665 } 1666 len_seen += ext_sglist[i].len; 1667 } 1668 } else { 1669 ext_sglist = &ext_entry; 1670 ext_sglist->addr = ctsio->ext_data_ptr; 1671 ext_sglist->len = ctsio->ext_data_len; 1672 ext_sg_entries = 1; 1673 ext_sg_start = 0; 1674 ext_offset = ctsio->ext_data_filled; 1675 } 1676 1677 if (ctsio->kern_sg_entries > 0) { 1678 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1679 kern_sg_entries = ctsio->kern_sg_entries; 1680 } else { 1681 kern_sglist = &kern_entry; 1682 kern_sglist->addr = ctsio->kern_data_ptr; 1683 kern_sglist->len = ctsio->kern_data_len; 1684 kern_sg_entries = 1; 1685 } 1686 1687 1688 kern_watermark = 0; 1689 ext_watermark = ext_offset; 1690 len_copied = 0; 1691 for (i = ext_sg_start, j = 0; 1692 i < ext_sg_entries && j < kern_sg_entries;) { 1693 uint8_t *ext_ptr, *kern_ptr; 1694 1695 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1696 kern_sglist[j].len - kern_watermark); 1697 1698 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1699 ext_ptr = ext_ptr + ext_watermark; 1700 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1701 /* 1702 * XXX KDM fix this! 1703 */ 1704 panic("need to implement bus address support"); 1705 #if 0 1706 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1707 #endif 1708 } else 1709 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1710 kern_ptr = kern_ptr + kern_watermark; 1711 1712 kern_watermark += len_to_copy; 1713 ext_watermark += len_to_copy; 1714 1715 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1716 CTL_FLAG_DATA_IN) { 1717 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1718 "bytes to user\n", len_to_copy)); 1719 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1720 "to %p\n", kern_ptr, ext_ptr)); 1721 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1722 ctl_set_internal_failure(ctsio, 1723 /*sks_valid*/ 0, 1724 /*retry_count*/ 0); 1725 goto bailout; 1726 } 1727 } else { 1728 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1729 "bytes from user\n", len_to_copy)); 1730 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1731 "to %p\n", ext_ptr, kern_ptr)); 1732 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1733 ctl_set_internal_failure(ctsio, 1734 /*sks_valid*/ 0, 1735 /*retry_count*/0); 1736 goto bailout; 1737 } 1738 } 1739 1740 len_copied += len_to_copy; 1741 1742 if (ext_sglist[i].len == ext_watermark) { 1743 i++; 1744 ext_watermark = 0; 1745 } 1746 1747 if (kern_sglist[j].len == kern_watermark) { 1748 j++; 1749 kern_watermark = 0; 1750 } 1751 } 1752 1753 ctsio->ext_data_filled += len_copied; 1754 1755 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1756 "kern_sg_entries: %d\n", ext_sg_entries, 1757 kern_sg_entries)); 1758 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1759 "kern_data_len = %d\n", ctsio->ext_data_len, 1760 ctsio->kern_data_len)); 1761 1762 1763 /* XXX KDM set residual?? */ 1764 bailout: 1765 1766 if (ext_sglist_malloced != 0) 1767 free(ext_sglist, M_CTL); 1768 1769 return (CTL_RETVAL_COMPLETE); 1770 } 1771 1772 /* 1773 * Serialize a command that went down the "wrong" side, and so was sent to 1774 * this controller for execution. The logic is a little different than the 1775 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1776 * sent back to the other side, but in the success case, we execute the 1777 * command on this side (XFER mode) or tell the other side to execute it 1778 * (SER_ONLY mode). 1779 */ 1780 static int 1781 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1782 { 1783 struct ctl_softc *ctl_softc; 1784 union ctl_ha_msg msg_info; 1785 struct ctl_lun *lun; 1786 int retval = 0; 1787 uint32_t targ_lun; 1788 1789 ctl_softc = control_softc; 1790 1791 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1792 lun = ctl_softc->ctl_luns[targ_lun]; 1793 if (lun==NULL) 1794 { 1795 /* 1796 * Why isn't LUN defined? The other side wouldn't 1797 * send a cmd if the LUN is undefined. 1798 */ 1799 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1800 1801 /* "Logical unit not supported" */ 1802 ctl_set_sense_data(&msg_info.scsi.sense_data, 1803 lun, 1804 /*sense_format*/SSD_TYPE_NONE, 1805 /*current_error*/ 1, 1806 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1807 /*asc*/ 0x25, 1808 /*ascq*/ 0x00, 1809 SSD_ELEM_NONE); 1810 1811 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1812 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1813 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1814 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1815 msg_info.hdr.serializing_sc = NULL; 1816 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1817 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1818 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1819 } 1820 return(1); 1821 1822 } 1823 1824 mtx_lock(&lun->lun_lock); 1825 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1826 1827 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1828 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1829 ooa_links))) { 1830 case CTL_ACTION_BLOCK: 1831 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1832 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1833 blocked_links); 1834 break; 1835 case CTL_ACTION_PASS: 1836 case CTL_ACTION_SKIP: 1837 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1838 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1839 ctl_enqueue_rtr((union ctl_io *)ctsio); 1840 } else { 1841 1842 /* send msg back to other side */ 1843 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1844 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1845 msg_info.hdr.msg_type = CTL_MSG_R2R; 1846 #if 0 1847 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1848 #endif 1849 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1850 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1851 } 1852 } 1853 break; 1854 case CTL_ACTION_OVERLAP: 1855 /* OVERLAPPED COMMANDS ATTEMPTED */ 1856 ctl_set_sense_data(&msg_info.scsi.sense_data, 1857 lun, 1858 /*sense_format*/SSD_TYPE_NONE, 1859 /*current_error*/ 1, 1860 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1861 /*asc*/ 0x4E, 1862 /*ascq*/ 0x00, 1863 SSD_ELEM_NONE); 1864 1865 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1866 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1867 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1868 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1869 msg_info.hdr.serializing_sc = NULL; 1870 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1871 #if 0 1872 printf("BAD JUJU:Major Bummer Overlap\n"); 1873 #endif 1874 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1875 retval = 1; 1876 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1877 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1878 } 1879 break; 1880 case CTL_ACTION_OVERLAP_TAG: 1881 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1882 ctl_set_sense_data(&msg_info.scsi.sense_data, 1883 lun, 1884 /*sense_format*/SSD_TYPE_NONE, 1885 /*current_error*/ 1, 1886 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1887 /*asc*/ 0x4D, 1888 /*ascq*/ ctsio->tag_num & 0xff, 1889 SSD_ELEM_NONE); 1890 1891 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1892 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1893 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1894 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1895 msg_info.hdr.serializing_sc = NULL; 1896 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1897 #if 0 1898 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1899 #endif 1900 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1901 retval = 1; 1902 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1903 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1904 } 1905 break; 1906 case CTL_ACTION_ERROR: 1907 default: 1908 /* "Internal target failure" */ 1909 ctl_set_sense_data(&msg_info.scsi.sense_data, 1910 lun, 1911 /*sense_format*/SSD_TYPE_NONE, 1912 /*current_error*/ 1, 1913 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1914 /*asc*/ 0x44, 1915 /*ascq*/ 0x00, 1916 SSD_ELEM_NONE); 1917 1918 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1919 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1920 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1921 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1922 msg_info.hdr.serializing_sc = NULL; 1923 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1924 #if 0 1925 printf("BAD JUJU:Major Bummer HW Error\n"); 1926 #endif 1927 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1928 retval = 1; 1929 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1930 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1931 } 1932 break; 1933 } 1934 mtx_unlock(&lun->lun_lock); 1935 return (retval); 1936 } 1937 1938 static int 1939 ctl_ioctl_submit_wait(union ctl_io *io) 1940 { 1941 struct ctl_fe_ioctl_params params; 1942 ctl_fe_ioctl_state last_state; 1943 int done, retval; 1944 1945 retval = 0; 1946 1947 bzero(¶ms, sizeof(params)); 1948 1949 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1950 cv_init(¶ms.sem, "ctlioccv"); 1951 params.state = CTL_IOCTL_INPROG; 1952 last_state = params.state; 1953 1954 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1955 1956 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1957 1958 /* This shouldn't happen */ 1959 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1960 return (retval); 1961 1962 done = 0; 1963 1964 do { 1965 mtx_lock(¶ms.ioctl_mtx); 1966 /* 1967 * Check the state here, and don't sleep if the state has 1968 * already changed (i.e. wakeup has already occured, but we 1969 * weren't waiting yet). 1970 */ 1971 if (params.state == last_state) { 1972 /* XXX KDM cv_wait_sig instead? */ 1973 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1974 } 1975 last_state = params.state; 1976 1977 switch (params.state) { 1978 case CTL_IOCTL_INPROG: 1979 /* Why did we wake up? */ 1980 /* XXX KDM error here? */ 1981 mtx_unlock(¶ms.ioctl_mtx); 1982 break; 1983 case CTL_IOCTL_DATAMOVE: 1984 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1985 1986 /* 1987 * change last_state back to INPROG to avoid 1988 * deadlock on subsequent data moves. 1989 */ 1990 params.state = last_state = CTL_IOCTL_INPROG; 1991 1992 mtx_unlock(¶ms.ioctl_mtx); 1993 ctl_ioctl_do_datamove(&io->scsiio); 1994 /* 1995 * Note that in some cases, most notably writes, 1996 * this will queue the I/O and call us back later. 1997 * In other cases, generally reads, this routine 1998 * will immediately call back and wake us up, 1999 * probably using our own context. 2000 */ 2001 io->scsiio.be_move_done(io); 2002 break; 2003 case CTL_IOCTL_DONE: 2004 mtx_unlock(¶ms.ioctl_mtx); 2005 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 2006 done = 1; 2007 break; 2008 default: 2009 mtx_unlock(¶ms.ioctl_mtx); 2010 /* XXX KDM error here? */ 2011 break; 2012 } 2013 } while (done == 0); 2014 2015 mtx_destroy(¶ms.ioctl_mtx); 2016 cv_destroy(¶ms.sem); 2017 2018 return (CTL_RETVAL_COMPLETE); 2019 } 2020 2021 static void 2022 ctl_ioctl_datamove(union ctl_io *io) 2023 { 2024 struct ctl_fe_ioctl_params *params; 2025 2026 params = (struct ctl_fe_ioctl_params *) 2027 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2028 2029 mtx_lock(¶ms->ioctl_mtx); 2030 params->state = CTL_IOCTL_DATAMOVE; 2031 cv_broadcast(¶ms->sem); 2032 mtx_unlock(¶ms->ioctl_mtx); 2033 } 2034 2035 static void 2036 ctl_ioctl_done(union ctl_io *io) 2037 { 2038 struct ctl_fe_ioctl_params *params; 2039 2040 params = (struct ctl_fe_ioctl_params *) 2041 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2042 2043 mtx_lock(¶ms->ioctl_mtx); 2044 params->state = CTL_IOCTL_DONE; 2045 cv_broadcast(¶ms->sem); 2046 mtx_unlock(¶ms->ioctl_mtx); 2047 } 2048 2049 static void 2050 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2051 { 2052 struct ctl_fe_ioctl_startstop_info *sd_info; 2053 2054 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2055 2056 sd_info->hs_info.status = metatask->status; 2057 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2058 sd_info->hs_info.luns_complete = 2059 metatask->taskinfo.startstop.luns_complete; 2060 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2061 2062 cv_broadcast(&sd_info->sem); 2063 } 2064 2065 static void 2066 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2067 { 2068 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2069 2070 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2071 2072 mtx_lock(fe_bbr_info->lock); 2073 fe_bbr_info->bbr_info->status = metatask->status; 2074 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2075 fe_bbr_info->wakeup_done = 1; 2076 mtx_unlock(fe_bbr_info->lock); 2077 2078 cv_broadcast(&fe_bbr_info->sem); 2079 } 2080 2081 /* 2082 * Returns 0 for success, errno for failure. 2083 */ 2084 static int 2085 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2086 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2087 { 2088 union ctl_io *io; 2089 int retval; 2090 2091 retval = 0; 2092 2093 mtx_lock(&lun->lun_lock); 2094 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2095 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2096 ooa_links)) { 2097 struct ctl_ooa_entry *entry; 2098 2099 /* 2100 * If we've got more than we can fit, just count the 2101 * remaining entries. 2102 */ 2103 if (*cur_fill_num >= ooa_hdr->alloc_num) 2104 continue; 2105 2106 entry = &kern_entries[*cur_fill_num]; 2107 2108 entry->tag_num = io->scsiio.tag_num; 2109 entry->lun_num = lun->lun; 2110 #ifdef CTL_TIME_IO 2111 entry->start_bt = io->io_hdr.start_bt; 2112 #endif 2113 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2114 entry->cdb_len = io->scsiio.cdb_len; 2115 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2116 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2117 2118 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2119 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2120 2121 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2122 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2123 2124 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2125 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2126 2127 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2128 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2129 } 2130 mtx_unlock(&lun->lun_lock); 2131 2132 return (retval); 2133 } 2134 2135 static void * 2136 ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2137 size_t error_str_len) 2138 { 2139 void *kptr; 2140 2141 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2142 2143 if (copyin(user_addr, kptr, len) != 0) { 2144 snprintf(error_str, error_str_len, "Error copying %d bytes " 2145 "from user address %p to kernel address %p", len, 2146 user_addr, kptr); 2147 free(kptr, M_CTL); 2148 return (NULL); 2149 } 2150 2151 return (kptr); 2152 } 2153 2154 static void 2155 ctl_free_args(int num_args, struct ctl_be_arg *args) 2156 { 2157 int i; 2158 2159 if (args == NULL) 2160 return; 2161 2162 for (i = 0; i < num_args; i++) { 2163 free(args[i].kname, M_CTL); 2164 free(args[i].kvalue, M_CTL); 2165 } 2166 2167 free(args, M_CTL); 2168 } 2169 2170 static struct ctl_be_arg * 2171 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2172 char *error_str, size_t error_str_len) 2173 { 2174 struct ctl_be_arg *args; 2175 int i; 2176 2177 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2178 error_str, error_str_len); 2179 2180 if (args == NULL) 2181 goto bailout; 2182 2183 for (i = 0; i < num_args; i++) { 2184 args[i].kname = NULL; 2185 args[i].kvalue = NULL; 2186 } 2187 2188 for (i = 0; i < num_args; i++) { 2189 uint8_t *tmpptr; 2190 2191 args[i].kname = ctl_copyin_alloc(args[i].name, 2192 args[i].namelen, error_str, error_str_len); 2193 if (args[i].kname == NULL) 2194 goto bailout; 2195 2196 if (args[i].kname[args[i].namelen - 1] != '\0') { 2197 snprintf(error_str, error_str_len, "Argument %d " 2198 "name is not NUL-terminated", i); 2199 goto bailout; 2200 } 2201 2202 if (args[i].flags & CTL_BEARG_RD) { 2203 tmpptr = ctl_copyin_alloc(args[i].value, 2204 args[i].vallen, error_str, error_str_len); 2205 if (tmpptr == NULL) 2206 goto bailout; 2207 if ((args[i].flags & CTL_BEARG_ASCII) 2208 && (tmpptr[args[i].vallen - 1] != '\0')) { 2209 snprintf(error_str, error_str_len, "Argument " 2210 "%d value is not NUL-terminated", i); 2211 goto bailout; 2212 } 2213 args[i].kvalue = tmpptr; 2214 } else { 2215 args[i].kvalue = malloc(args[i].vallen, 2216 M_CTL, M_WAITOK | M_ZERO); 2217 } 2218 } 2219 2220 return (args); 2221 bailout: 2222 2223 ctl_free_args(num_args, args); 2224 2225 return (NULL); 2226 } 2227 2228 static void 2229 ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2230 { 2231 int i; 2232 2233 for (i = 0; i < num_args; i++) { 2234 if (args[i].flags & CTL_BEARG_WR) 2235 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2236 } 2237 } 2238 2239 /* 2240 * Escape characters that are illegal or not recommended in XML. 2241 */ 2242 int 2243 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2244 { 2245 char *end = str + size; 2246 int retval; 2247 2248 retval = 0; 2249 2250 for (; *str && str < end; str++) { 2251 switch (*str) { 2252 case '&': 2253 retval = sbuf_printf(sb, "&"); 2254 break; 2255 case '>': 2256 retval = sbuf_printf(sb, ">"); 2257 break; 2258 case '<': 2259 retval = sbuf_printf(sb, "<"); 2260 break; 2261 default: 2262 retval = sbuf_putc(sb, *str); 2263 break; 2264 } 2265 2266 if (retval != 0) 2267 break; 2268 2269 } 2270 2271 return (retval); 2272 } 2273 2274 static void 2275 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2276 { 2277 struct scsi_vpd_id_descriptor *desc; 2278 int i; 2279 2280 if (id == NULL || id->len < 4) 2281 return; 2282 desc = (struct scsi_vpd_id_descriptor *)id->data; 2283 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2284 case SVPD_ID_TYPE_T10: 2285 sbuf_printf(sb, "t10."); 2286 break; 2287 case SVPD_ID_TYPE_EUI64: 2288 sbuf_printf(sb, "eui."); 2289 break; 2290 case SVPD_ID_TYPE_NAA: 2291 sbuf_printf(sb, "naa."); 2292 break; 2293 case SVPD_ID_TYPE_SCSI_NAME: 2294 break; 2295 } 2296 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2297 case SVPD_ID_CODESET_BINARY: 2298 for (i = 0; i < desc->length; i++) 2299 sbuf_printf(sb, "%02x", desc->identifier[i]); 2300 break; 2301 case SVPD_ID_CODESET_ASCII: 2302 sbuf_printf(sb, "%.*s", (int)desc->length, 2303 (char *)desc->identifier); 2304 break; 2305 case SVPD_ID_CODESET_UTF8: 2306 sbuf_printf(sb, "%s", (char *)desc->identifier); 2307 break; 2308 } 2309 } 2310 2311 static int 2312 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2313 struct thread *td) 2314 { 2315 struct ctl_softc *softc; 2316 int retval; 2317 2318 softc = control_softc; 2319 2320 retval = 0; 2321 2322 switch (cmd) { 2323 case CTL_IO: { 2324 union ctl_io *io; 2325 void *pool_tmp; 2326 2327 /* 2328 * If we haven't been "enabled", don't allow any SCSI I/O 2329 * to this FETD. 2330 */ 2331 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2332 retval = EPERM; 2333 break; 2334 } 2335 2336 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2337 2338 /* 2339 * Need to save the pool reference so it doesn't get 2340 * spammed by the user's ctl_io. 2341 */ 2342 pool_tmp = io->io_hdr.pool; 2343 memcpy(io, (void *)addr, sizeof(*io)); 2344 io->io_hdr.pool = pool_tmp; 2345 2346 /* 2347 * No status yet, so make sure the status is set properly. 2348 */ 2349 io->io_hdr.status = CTL_STATUS_NONE; 2350 2351 /* 2352 * The user sets the initiator ID, target and LUN IDs. 2353 */ 2354 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2355 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2356 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2357 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2358 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2359 2360 retval = ctl_ioctl_submit_wait(io); 2361 2362 if (retval != 0) { 2363 ctl_free_io(io); 2364 break; 2365 } 2366 2367 memcpy((void *)addr, io, sizeof(*io)); 2368 2369 /* return this to our pool */ 2370 ctl_free_io(io); 2371 2372 break; 2373 } 2374 case CTL_ENABLE_PORT: 2375 case CTL_DISABLE_PORT: 2376 case CTL_SET_PORT_WWNS: { 2377 struct ctl_port *port; 2378 struct ctl_port_entry *entry; 2379 2380 entry = (struct ctl_port_entry *)addr; 2381 2382 mtx_lock(&softc->ctl_lock); 2383 STAILQ_FOREACH(port, &softc->port_list, links) { 2384 int action, done; 2385 2386 action = 0; 2387 done = 0; 2388 2389 if ((entry->port_type == CTL_PORT_NONE) 2390 && (entry->targ_port == port->targ_port)) { 2391 /* 2392 * If the user only wants to enable or 2393 * disable or set WWNs on a specific port, 2394 * do the operation and we're done. 2395 */ 2396 action = 1; 2397 done = 1; 2398 } else if (entry->port_type & port->port_type) { 2399 /* 2400 * Compare the user's type mask with the 2401 * particular frontend type to see if we 2402 * have a match. 2403 */ 2404 action = 1; 2405 done = 0; 2406 2407 /* 2408 * Make sure the user isn't trying to set 2409 * WWNs on multiple ports at the same time. 2410 */ 2411 if (cmd == CTL_SET_PORT_WWNS) { 2412 printf("%s: Can't set WWNs on " 2413 "multiple ports\n", __func__); 2414 retval = EINVAL; 2415 break; 2416 } 2417 } 2418 if (action != 0) { 2419 /* 2420 * XXX KDM we have to drop the lock here, 2421 * because the online/offline operations 2422 * can potentially block. We need to 2423 * reference count the frontends so they 2424 * can't go away, 2425 */ 2426 mtx_unlock(&softc->ctl_lock); 2427 2428 if (cmd == CTL_ENABLE_PORT) { 2429 struct ctl_lun *lun; 2430 2431 STAILQ_FOREACH(lun, &softc->lun_list, 2432 links) { 2433 port->lun_enable(port->targ_lun_arg, 2434 lun->target, 2435 lun->lun); 2436 } 2437 2438 ctl_port_online(port); 2439 } else if (cmd == CTL_DISABLE_PORT) { 2440 struct ctl_lun *lun; 2441 2442 ctl_port_offline(port); 2443 2444 STAILQ_FOREACH(lun, &softc->lun_list, 2445 links) { 2446 port->lun_disable( 2447 port->targ_lun_arg, 2448 lun->target, 2449 lun->lun); 2450 } 2451 } 2452 2453 mtx_lock(&softc->ctl_lock); 2454 2455 if (cmd == CTL_SET_PORT_WWNS) 2456 ctl_port_set_wwns(port, 2457 (entry->flags & CTL_PORT_WWNN_VALID) ? 2458 1 : 0, entry->wwnn, 2459 (entry->flags & CTL_PORT_WWPN_VALID) ? 2460 1 : 0, entry->wwpn); 2461 } 2462 if (done != 0) 2463 break; 2464 } 2465 mtx_unlock(&softc->ctl_lock); 2466 break; 2467 } 2468 case CTL_GET_PORT_LIST: { 2469 struct ctl_port *port; 2470 struct ctl_port_list *list; 2471 int i; 2472 2473 list = (struct ctl_port_list *)addr; 2474 2475 if (list->alloc_len != (list->alloc_num * 2476 sizeof(struct ctl_port_entry))) { 2477 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2478 "alloc_num %u * sizeof(struct ctl_port_entry) " 2479 "%zu\n", __func__, list->alloc_len, 2480 list->alloc_num, sizeof(struct ctl_port_entry)); 2481 retval = EINVAL; 2482 break; 2483 } 2484 list->fill_len = 0; 2485 list->fill_num = 0; 2486 list->dropped_num = 0; 2487 i = 0; 2488 mtx_lock(&softc->ctl_lock); 2489 STAILQ_FOREACH(port, &softc->port_list, links) { 2490 struct ctl_port_entry entry, *list_entry; 2491 2492 if (list->fill_num >= list->alloc_num) { 2493 list->dropped_num++; 2494 continue; 2495 } 2496 2497 entry.port_type = port->port_type; 2498 strlcpy(entry.port_name, port->port_name, 2499 sizeof(entry.port_name)); 2500 entry.targ_port = port->targ_port; 2501 entry.physical_port = port->physical_port; 2502 entry.virtual_port = port->virtual_port; 2503 entry.wwnn = port->wwnn; 2504 entry.wwpn = port->wwpn; 2505 if (port->status & CTL_PORT_STATUS_ONLINE) 2506 entry.online = 1; 2507 else 2508 entry.online = 0; 2509 2510 list_entry = &list->entries[i]; 2511 2512 retval = copyout(&entry, list_entry, sizeof(entry)); 2513 if (retval != 0) { 2514 printf("%s: CTL_GET_PORT_LIST: copyout " 2515 "returned %d\n", __func__, retval); 2516 break; 2517 } 2518 i++; 2519 list->fill_num++; 2520 list->fill_len += sizeof(entry); 2521 } 2522 mtx_unlock(&softc->ctl_lock); 2523 2524 /* 2525 * If this is non-zero, we had a copyout fault, so there's 2526 * probably no point in attempting to set the status inside 2527 * the structure. 2528 */ 2529 if (retval != 0) 2530 break; 2531 2532 if (list->dropped_num > 0) 2533 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2534 else 2535 list->status = CTL_PORT_LIST_OK; 2536 break; 2537 } 2538 case CTL_DUMP_OOA: { 2539 struct ctl_lun *lun; 2540 union ctl_io *io; 2541 char printbuf[128]; 2542 struct sbuf sb; 2543 2544 mtx_lock(&softc->ctl_lock); 2545 printf("Dumping OOA queues:\n"); 2546 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2547 mtx_lock(&lun->lun_lock); 2548 for (io = (union ctl_io *)TAILQ_FIRST( 2549 &lun->ooa_queue); io != NULL; 2550 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2551 ooa_links)) { 2552 sbuf_new(&sb, printbuf, sizeof(printbuf), 2553 SBUF_FIXEDLEN); 2554 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2555 (intmax_t)lun->lun, 2556 io->scsiio.tag_num, 2557 (io->io_hdr.flags & 2558 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2559 (io->io_hdr.flags & 2560 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2561 (io->io_hdr.flags & 2562 CTL_FLAG_ABORT) ? " ABORT" : "", 2563 (io->io_hdr.flags & 2564 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2565 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2566 sbuf_finish(&sb); 2567 printf("%s\n", sbuf_data(&sb)); 2568 } 2569 mtx_unlock(&lun->lun_lock); 2570 } 2571 printf("OOA queues dump done\n"); 2572 mtx_unlock(&softc->ctl_lock); 2573 break; 2574 } 2575 case CTL_GET_OOA: { 2576 struct ctl_lun *lun; 2577 struct ctl_ooa *ooa_hdr; 2578 struct ctl_ooa_entry *entries; 2579 uint32_t cur_fill_num; 2580 2581 ooa_hdr = (struct ctl_ooa *)addr; 2582 2583 if ((ooa_hdr->alloc_len == 0) 2584 || (ooa_hdr->alloc_num == 0)) { 2585 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2586 "must be non-zero\n", __func__, 2587 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2588 retval = EINVAL; 2589 break; 2590 } 2591 2592 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2593 sizeof(struct ctl_ooa_entry))) { 2594 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2595 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2596 __func__, ooa_hdr->alloc_len, 2597 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2598 retval = EINVAL; 2599 break; 2600 } 2601 2602 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2603 if (entries == NULL) { 2604 printf("%s: could not allocate %d bytes for OOA " 2605 "dump\n", __func__, ooa_hdr->alloc_len); 2606 retval = ENOMEM; 2607 break; 2608 } 2609 2610 mtx_lock(&softc->ctl_lock); 2611 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2612 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2613 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2614 mtx_unlock(&softc->ctl_lock); 2615 free(entries, M_CTL); 2616 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2617 __func__, (uintmax_t)ooa_hdr->lun_num); 2618 retval = EINVAL; 2619 break; 2620 } 2621 2622 cur_fill_num = 0; 2623 2624 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2625 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2626 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2627 ooa_hdr, entries); 2628 if (retval != 0) 2629 break; 2630 } 2631 if (retval != 0) { 2632 mtx_unlock(&softc->ctl_lock); 2633 free(entries, M_CTL); 2634 break; 2635 } 2636 } else { 2637 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2638 2639 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2640 entries); 2641 } 2642 mtx_unlock(&softc->ctl_lock); 2643 2644 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2645 ooa_hdr->fill_len = ooa_hdr->fill_num * 2646 sizeof(struct ctl_ooa_entry); 2647 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2648 if (retval != 0) { 2649 printf("%s: error copying out %d bytes for OOA dump\n", 2650 __func__, ooa_hdr->fill_len); 2651 } 2652 2653 getbintime(&ooa_hdr->cur_bt); 2654 2655 if (cur_fill_num > ooa_hdr->alloc_num) { 2656 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2657 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2658 } else { 2659 ooa_hdr->dropped_num = 0; 2660 ooa_hdr->status = CTL_OOA_OK; 2661 } 2662 2663 free(entries, M_CTL); 2664 break; 2665 } 2666 case CTL_CHECK_OOA: { 2667 union ctl_io *io; 2668 struct ctl_lun *lun; 2669 struct ctl_ooa_info *ooa_info; 2670 2671 2672 ooa_info = (struct ctl_ooa_info *)addr; 2673 2674 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2675 ooa_info->status = CTL_OOA_INVALID_LUN; 2676 break; 2677 } 2678 mtx_lock(&softc->ctl_lock); 2679 lun = softc->ctl_luns[ooa_info->lun_id]; 2680 if (lun == NULL) { 2681 mtx_unlock(&softc->ctl_lock); 2682 ooa_info->status = CTL_OOA_INVALID_LUN; 2683 break; 2684 } 2685 mtx_lock(&lun->lun_lock); 2686 mtx_unlock(&softc->ctl_lock); 2687 ooa_info->num_entries = 0; 2688 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2689 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2690 &io->io_hdr, ooa_links)) { 2691 ooa_info->num_entries++; 2692 } 2693 mtx_unlock(&lun->lun_lock); 2694 2695 ooa_info->status = CTL_OOA_SUCCESS; 2696 2697 break; 2698 } 2699 case CTL_HARD_START: 2700 case CTL_HARD_STOP: { 2701 struct ctl_fe_ioctl_startstop_info ss_info; 2702 struct cfi_metatask *metatask; 2703 struct mtx hs_mtx; 2704 2705 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2706 2707 cv_init(&ss_info.sem, "hard start/stop cv" ); 2708 2709 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2710 if (metatask == NULL) { 2711 retval = ENOMEM; 2712 mtx_destroy(&hs_mtx); 2713 break; 2714 } 2715 2716 if (cmd == CTL_HARD_START) 2717 metatask->tasktype = CFI_TASK_STARTUP; 2718 else 2719 metatask->tasktype = CFI_TASK_SHUTDOWN; 2720 2721 metatask->callback = ctl_ioctl_hard_startstop_callback; 2722 metatask->callback_arg = &ss_info; 2723 2724 cfi_action(metatask); 2725 2726 /* Wait for the callback */ 2727 mtx_lock(&hs_mtx); 2728 cv_wait_sig(&ss_info.sem, &hs_mtx); 2729 mtx_unlock(&hs_mtx); 2730 2731 /* 2732 * All information has been copied from the metatask by the 2733 * time cv_broadcast() is called, so we free the metatask here. 2734 */ 2735 cfi_free_metatask(metatask); 2736 2737 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2738 2739 mtx_destroy(&hs_mtx); 2740 break; 2741 } 2742 case CTL_BBRREAD: { 2743 struct ctl_bbrread_info *bbr_info; 2744 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2745 struct mtx bbr_mtx; 2746 struct cfi_metatask *metatask; 2747 2748 bbr_info = (struct ctl_bbrread_info *)addr; 2749 2750 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2751 2752 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2753 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2754 2755 fe_bbr_info.bbr_info = bbr_info; 2756 fe_bbr_info.lock = &bbr_mtx; 2757 2758 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2759 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2760 2761 if (metatask == NULL) { 2762 mtx_destroy(&bbr_mtx); 2763 cv_destroy(&fe_bbr_info.sem); 2764 retval = ENOMEM; 2765 break; 2766 } 2767 metatask->tasktype = CFI_TASK_BBRREAD; 2768 metatask->callback = ctl_ioctl_bbrread_callback; 2769 metatask->callback_arg = &fe_bbr_info; 2770 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2771 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2772 metatask->taskinfo.bbrread.len = bbr_info->len; 2773 2774 cfi_action(metatask); 2775 2776 mtx_lock(&bbr_mtx); 2777 while (fe_bbr_info.wakeup_done == 0) 2778 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2779 mtx_unlock(&bbr_mtx); 2780 2781 bbr_info->status = metatask->status; 2782 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2783 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2784 memcpy(&bbr_info->sense_data, 2785 &metatask->taskinfo.bbrread.sense_data, 2786 ctl_min(sizeof(bbr_info->sense_data), 2787 sizeof(metatask->taskinfo.bbrread.sense_data))); 2788 2789 cfi_free_metatask(metatask); 2790 2791 mtx_destroy(&bbr_mtx); 2792 cv_destroy(&fe_bbr_info.sem); 2793 2794 break; 2795 } 2796 case CTL_DELAY_IO: { 2797 struct ctl_io_delay_info *delay_info; 2798 #ifdef CTL_IO_DELAY 2799 struct ctl_lun *lun; 2800 #endif /* CTL_IO_DELAY */ 2801 2802 delay_info = (struct ctl_io_delay_info *)addr; 2803 2804 #ifdef CTL_IO_DELAY 2805 mtx_lock(&softc->ctl_lock); 2806 2807 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2808 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2809 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2810 } else { 2811 lun = softc->ctl_luns[delay_info->lun_id]; 2812 mtx_lock(&lun->lun_lock); 2813 2814 delay_info->status = CTL_DELAY_STATUS_OK; 2815 2816 switch (delay_info->delay_type) { 2817 case CTL_DELAY_TYPE_CONT: 2818 break; 2819 case CTL_DELAY_TYPE_ONESHOT: 2820 break; 2821 default: 2822 delay_info->status = 2823 CTL_DELAY_STATUS_INVALID_TYPE; 2824 break; 2825 } 2826 2827 switch (delay_info->delay_loc) { 2828 case CTL_DELAY_LOC_DATAMOVE: 2829 lun->delay_info.datamove_type = 2830 delay_info->delay_type; 2831 lun->delay_info.datamove_delay = 2832 delay_info->delay_secs; 2833 break; 2834 case CTL_DELAY_LOC_DONE: 2835 lun->delay_info.done_type = 2836 delay_info->delay_type; 2837 lun->delay_info.done_delay = 2838 delay_info->delay_secs; 2839 break; 2840 default: 2841 delay_info->status = 2842 CTL_DELAY_STATUS_INVALID_LOC; 2843 break; 2844 } 2845 mtx_unlock(&lun->lun_lock); 2846 } 2847 2848 mtx_unlock(&softc->ctl_lock); 2849 #else 2850 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2851 #endif /* CTL_IO_DELAY */ 2852 break; 2853 } 2854 case CTL_REALSYNC_SET: { 2855 int *syncstate; 2856 2857 syncstate = (int *)addr; 2858 2859 mtx_lock(&softc->ctl_lock); 2860 switch (*syncstate) { 2861 case 0: 2862 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2863 break; 2864 case 1: 2865 softc->flags |= CTL_FLAG_REAL_SYNC; 2866 break; 2867 default: 2868 retval = EINVAL; 2869 break; 2870 } 2871 mtx_unlock(&softc->ctl_lock); 2872 break; 2873 } 2874 case CTL_REALSYNC_GET: { 2875 int *syncstate; 2876 2877 syncstate = (int*)addr; 2878 2879 mtx_lock(&softc->ctl_lock); 2880 if (softc->flags & CTL_FLAG_REAL_SYNC) 2881 *syncstate = 1; 2882 else 2883 *syncstate = 0; 2884 mtx_unlock(&softc->ctl_lock); 2885 2886 break; 2887 } 2888 case CTL_SETSYNC: 2889 case CTL_GETSYNC: { 2890 struct ctl_sync_info *sync_info; 2891 struct ctl_lun *lun; 2892 2893 sync_info = (struct ctl_sync_info *)addr; 2894 2895 mtx_lock(&softc->ctl_lock); 2896 lun = softc->ctl_luns[sync_info->lun_id]; 2897 if (lun == NULL) { 2898 mtx_unlock(&softc->ctl_lock); 2899 sync_info->status = CTL_GS_SYNC_NO_LUN; 2900 } 2901 /* 2902 * Get or set the sync interval. We're not bounds checking 2903 * in the set case, hopefully the user won't do something 2904 * silly. 2905 */ 2906 mtx_lock(&lun->lun_lock); 2907 mtx_unlock(&softc->ctl_lock); 2908 if (cmd == CTL_GETSYNC) 2909 sync_info->sync_interval = lun->sync_interval; 2910 else 2911 lun->sync_interval = sync_info->sync_interval; 2912 mtx_unlock(&lun->lun_lock); 2913 2914 sync_info->status = CTL_GS_SYNC_OK; 2915 2916 break; 2917 } 2918 case CTL_GETSTATS: { 2919 struct ctl_stats *stats; 2920 struct ctl_lun *lun; 2921 int i; 2922 2923 stats = (struct ctl_stats *)addr; 2924 2925 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2926 stats->alloc_len) { 2927 stats->status = CTL_SS_NEED_MORE_SPACE; 2928 stats->num_luns = softc->num_luns; 2929 break; 2930 } 2931 /* 2932 * XXX KDM no locking here. If the LUN list changes, 2933 * things can blow up. 2934 */ 2935 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2936 i++, lun = STAILQ_NEXT(lun, links)) { 2937 retval = copyout(&lun->stats, &stats->lun_stats[i], 2938 sizeof(lun->stats)); 2939 if (retval != 0) 2940 break; 2941 } 2942 stats->num_luns = softc->num_luns; 2943 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2944 softc->num_luns; 2945 stats->status = CTL_SS_OK; 2946 #ifdef CTL_TIME_IO 2947 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2948 #else 2949 stats->flags = CTL_STATS_FLAG_NONE; 2950 #endif 2951 getnanouptime(&stats->timestamp); 2952 break; 2953 } 2954 case CTL_ERROR_INJECT: { 2955 struct ctl_error_desc *err_desc, *new_err_desc; 2956 struct ctl_lun *lun; 2957 2958 err_desc = (struct ctl_error_desc *)addr; 2959 2960 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2961 M_WAITOK | M_ZERO); 2962 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2963 2964 mtx_lock(&softc->ctl_lock); 2965 lun = softc->ctl_luns[err_desc->lun_id]; 2966 if (lun == NULL) { 2967 mtx_unlock(&softc->ctl_lock); 2968 free(new_err_desc, M_CTL); 2969 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2970 __func__, (uintmax_t)err_desc->lun_id); 2971 retval = EINVAL; 2972 break; 2973 } 2974 mtx_lock(&lun->lun_lock); 2975 mtx_unlock(&softc->ctl_lock); 2976 2977 /* 2978 * We could do some checking here to verify the validity 2979 * of the request, but given the complexity of error 2980 * injection requests, the checking logic would be fairly 2981 * complex. 2982 * 2983 * For now, if the request is invalid, it just won't get 2984 * executed and might get deleted. 2985 */ 2986 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2987 2988 /* 2989 * XXX KDM check to make sure the serial number is unique, 2990 * in case we somehow manage to wrap. That shouldn't 2991 * happen for a very long time, but it's the right thing to 2992 * do. 2993 */ 2994 new_err_desc->serial = lun->error_serial; 2995 err_desc->serial = lun->error_serial; 2996 lun->error_serial++; 2997 2998 mtx_unlock(&lun->lun_lock); 2999 break; 3000 } 3001 case CTL_ERROR_INJECT_DELETE: { 3002 struct ctl_error_desc *delete_desc, *desc, *desc2; 3003 struct ctl_lun *lun; 3004 int delete_done; 3005 3006 delete_desc = (struct ctl_error_desc *)addr; 3007 delete_done = 0; 3008 3009 mtx_lock(&softc->ctl_lock); 3010 lun = softc->ctl_luns[delete_desc->lun_id]; 3011 if (lun == NULL) { 3012 mtx_unlock(&softc->ctl_lock); 3013 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 3014 __func__, (uintmax_t)delete_desc->lun_id); 3015 retval = EINVAL; 3016 break; 3017 } 3018 mtx_lock(&lun->lun_lock); 3019 mtx_unlock(&softc->ctl_lock); 3020 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 3021 if (desc->serial != delete_desc->serial) 3022 continue; 3023 3024 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 3025 links); 3026 free(desc, M_CTL); 3027 delete_done = 1; 3028 } 3029 mtx_unlock(&lun->lun_lock); 3030 if (delete_done == 0) { 3031 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 3032 "error serial %ju on LUN %u\n", __func__, 3033 delete_desc->serial, delete_desc->lun_id); 3034 retval = EINVAL; 3035 break; 3036 } 3037 break; 3038 } 3039 case CTL_DUMP_STRUCTS: { 3040 int i, j, k, idx; 3041 struct ctl_port *port; 3042 struct ctl_frontend *fe; 3043 3044 mtx_lock(&softc->ctl_lock); 3045 printf("CTL Persistent Reservation information start:\n"); 3046 for (i = 0; i < CTL_MAX_LUNS; i++) { 3047 struct ctl_lun *lun; 3048 3049 lun = softc->ctl_luns[i]; 3050 3051 if ((lun == NULL) 3052 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3053 continue; 3054 3055 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3056 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3057 idx = j * CTL_MAX_INIT_PER_PORT + k; 3058 if (lun->pr_keys[idx] == 0) 3059 continue; 3060 printf(" LUN %d port %d iid %d key " 3061 "%#jx\n", i, j, k, 3062 (uintmax_t)lun->pr_keys[idx]); 3063 } 3064 } 3065 } 3066 printf("CTL Persistent Reservation information end\n"); 3067 printf("CTL Ports:\n"); 3068 STAILQ_FOREACH(port, &softc->port_list, links) { 3069 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3070 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3071 port->frontend->name, port->port_type, 3072 port->physical_port, port->virtual_port, 3073 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3074 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3075 if (port->wwpn_iid[j].in_use == 0 && 3076 port->wwpn_iid[j].wwpn == 0 && 3077 port->wwpn_iid[j].name == NULL) 3078 continue; 3079 3080 printf(" iid %u use %d WWPN %#jx '%s'\n", 3081 j, port->wwpn_iid[j].in_use, 3082 (uintmax_t)port->wwpn_iid[j].wwpn, 3083 port->wwpn_iid[j].name); 3084 } 3085 } 3086 printf("CTL Port information end\n"); 3087 mtx_unlock(&softc->ctl_lock); 3088 /* 3089 * XXX KDM calling this without a lock. We'd likely want 3090 * to drop the lock before calling the frontend's dump 3091 * routine anyway. 3092 */ 3093 printf("CTL Frontends:\n"); 3094 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3095 printf(" Frontend '%s'\n", fe->name); 3096 if (fe->fe_dump != NULL) 3097 fe->fe_dump(); 3098 } 3099 printf("CTL Frontend information end\n"); 3100 break; 3101 } 3102 case CTL_LUN_REQ: { 3103 struct ctl_lun_req *lun_req; 3104 struct ctl_backend_driver *backend; 3105 3106 lun_req = (struct ctl_lun_req *)addr; 3107 3108 backend = ctl_backend_find(lun_req->backend); 3109 if (backend == NULL) { 3110 lun_req->status = CTL_LUN_ERROR; 3111 snprintf(lun_req->error_str, 3112 sizeof(lun_req->error_str), 3113 "Backend \"%s\" not found.", 3114 lun_req->backend); 3115 break; 3116 } 3117 if (lun_req->num_be_args > 0) { 3118 lun_req->kern_be_args = ctl_copyin_args( 3119 lun_req->num_be_args, 3120 lun_req->be_args, 3121 lun_req->error_str, 3122 sizeof(lun_req->error_str)); 3123 if (lun_req->kern_be_args == NULL) { 3124 lun_req->status = CTL_LUN_ERROR; 3125 break; 3126 } 3127 } 3128 3129 retval = backend->ioctl(dev, cmd, addr, flag, td); 3130 3131 if (lun_req->num_be_args > 0) { 3132 ctl_copyout_args(lun_req->num_be_args, 3133 lun_req->kern_be_args); 3134 ctl_free_args(lun_req->num_be_args, 3135 lun_req->kern_be_args); 3136 } 3137 break; 3138 } 3139 case CTL_LUN_LIST: { 3140 struct sbuf *sb; 3141 struct ctl_lun *lun; 3142 struct ctl_lun_list *list; 3143 struct ctl_option *opt; 3144 3145 list = (struct ctl_lun_list *)addr; 3146 3147 /* 3148 * Allocate a fixed length sbuf here, based on the length 3149 * of the user's buffer. We could allocate an auto-extending 3150 * buffer, and then tell the user how much larger our 3151 * amount of data is than his buffer, but that presents 3152 * some problems: 3153 * 3154 * 1. The sbuf(9) routines use a blocking malloc, and so 3155 * we can't hold a lock while calling them with an 3156 * auto-extending buffer. 3157 * 3158 * 2. There is not currently a LUN reference counting 3159 * mechanism, outside of outstanding transactions on 3160 * the LUN's OOA queue. So a LUN could go away on us 3161 * while we're getting the LUN number, backend-specific 3162 * information, etc. Thus, given the way things 3163 * currently work, we need to hold the CTL lock while 3164 * grabbing LUN information. 3165 * 3166 * So, from the user's standpoint, the best thing to do is 3167 * allocate what he thinks is a reasonable buffer length, 3168 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3169 * double the buffer length and try again. (And repeat 3170 * that until he succeeds.) 3171 */ 3172 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3173 if (sb == NULL) { 3174 list->status = CTL_LUN_LIST_ERROR; 3175 snprintf(list->error_str, sizeof(list->error_str), 3176 "Unable to allocate %d bytes for LUN list", 3177 list->alloc_len); 3178 break; 3179 } 3180 3181 sbuf_printf(sb, "<ctllunlist>\n"); 3182 3183 mtx_lock(&softc->ctl_lock); 3184 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3185 mtx_lock(&lun->lun_lock); 3186 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3187 (uintmax_t)lun->lun); 3188 3189 /* 3190 * Bail out as soon as we see that we've overfilled 3191 * the buffer. 3192 */ 3193 if (retval != 0) 3194 break; 3195 3196 retval = sbuf_printf(sb, "\t<backend_type>%s" 3197 "</backend_type>\n", 3198 (lun->backend == NULL) ? "none" : 3199 lun->backend->name); 3200 3201 if (retval != 0) 3202 break; 3203 3204 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3205 lun->be_lun->lun_type); 3206 3207 if (retval != 0) 3208 break; 3209 3210 if (lun->backend == NULL) { 3211 retval = sbuf_printf(sb, "</lun>\n"); 3212 if (retval != 0) 3213 break; 3214 continue; 3215 } 3216 3217 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3218 (lun->be_lun->maxlba > 0) ? 3219 lun->be_lun->maxlba + 1 : 0); 3220 3221 if (retval != 0) 3222 break; 3223 3224 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3225 lun->be_lun->blocksize); 3226 3227 if (retval != 0) 3228 break; 3229 3230 retval = sbuf_printf(sb, "\t<serial_number>"); 3231 3232 if (retval != 0) 3233 break; 3234 3235 retval = ctl_sbuf_printf_esc(sb, 3236 lun->be_lun->serial_num, 3237 sizeof(lun->be_lun->serial_num)); 3238 3239 if (retval != 0) 3240 break; 3241 3242 retval = sbuf_printf(sb, "</serial_number>\n"); 3243 3244 if (retval != 0) 3245 break; 3246 3247 retval = sbuf_printf(sb, "\t<device_id>"); 3248 3249 if (retval != 0) 3250 break; 3251 3252 retval = ctl_sbuf_printf_esc(sb, 3253 lun->be_lun->device_id, 3254 sizeof(lun->be_lun->device_id)); 3255 3256 if (retval != 0) 3257 break; 3258 3259 retval = sbuf_printf(sb, "</device_id>\n"); 3260 3261 if (retval != 0) 3262 break; 3263 3264 if (lun->backend->lun_info != NULL) { 3265 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3266 if (retval != 0) 3267 break; 3268 } 3269 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3270 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3271 opt->name, opt->value, opt->name); 3272 if (retval != 0) 3273 break; 3274 } 3275 3276 retval = sbuf_printf(sb, "</lun>\n"); 3277 3278 if (retval != 0) 3279 break; 3280 mtx_unlock(&lun->lun_lock); 3281 } 3282 if (lun != NULL) 3283 mtx_unlock(&lun->lun_lock); 3284 mtx_unlock(&softc->ctl_lock); 3285 3286 if ((retval != 0) 3287 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3288 retval = 0; 3289 sbuf_delete(sb); 3290 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3291 snprintf(list->error_str, sizeof(list->error_str), 3292 "Out of space, %d bytes is too small", 3293 list->alloc_len); 3294 break; 3295 } 3296 3297 sbuf_finish(sb); 3298 3299 retval = copyout(sbuf_data(sb), list->lun_xml, 3300 sbuf_len(sb) + 1); 3301 3302 list->fill_len = sbuf_len(sb) + 1; 3303 list->status = CTL_LUN_LIST_OK; 3304 sbuf_delete(sb); 3305 break; 3306 } 3307 case CTL_ISCSI: { 3308 struct ctl_iscsi *ci; 3309 struct ctl_frontend *fe; 3310 3311 ci = (struct ctl_iscsi *)addr; 3312 3313 fe = ctl_frontend_find("iscsi"); 3314 if (fe == NULL) { 3315 ci->status = CTL_ISCSI_ERROR; 3316 snprintf(ci->error_str, sizeof(ci->error_str), 3317 "Frontend \"iscsi\" not found."); 3318 break; 3319 } 3320 3321 retval = fe->ioctl(dev, cmd, addr, flag, td); 3322 break; 3323 } 3324 case CTL_PORT_REQ: { 3325 struct ctl_req *req; 3326 struct ctl_frontend *fe; 3327 3328 req = (struct ctl_req *)addr; 3329 3330 fe = ctl_frontend_find(req->driver); 3331 if (fe == NULL) { 3332 req->status = CTL_LUN_ERROR; 3333 snprintf(req->error_str, sizeof(req->error_str), 3334 "Frontend \"%s\" not found.", req->driver); 3335 break; 3336 } 3337 if (req->num_args > 0) { 3338 req->kern_args = ctl_copyin_args(req->num_args, 3339 req->args, req->error_str, sizeof(req->error_str)); 3340 if (req->kern_args == NULL) { 3341 req->status = CTL_LUN_ERROR; 3342 break; 3343 } 3344 } 3345 3346 retval = fe->ioctl(dev, cmd, addr, flag, td); 3347 3348 if (req->num_args > 0) { 3349 ctl_copyout_args(req->num_args, req->kern_args); 3350 ctl_free_args(req->num_args, req->kern_args); 3351 } 3352 break; 3353 } 3354 case CTL_PORT_LIST: { 3355 struct sbuf *sb; 3356 struct ctl_port *port; 3357 struct ctl_lun_list *list; 3358 struct ctl_option *opt; 3359 int j; 3360 3361 list = (struct ctl_lun_list *)addr; 3362 3363 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3364 if (sb == NULL) { 3365 list->status = CTL_LUN_LIST_ERROR; 3366 snprintf(list->error_str, sizeof(list->error_str), 3367 "Unable to allocate %d bytes for LUN list", 3368 list->alloc_len); 3369 break; 3370 } 3371 3372 sbuf_printf(sb, "<ctlportlist>\n"); 3373 3374 mtx_lock(&softc->ctl_lock); 3375 STAILQ_FOREACH(port, &softc->port_list, links) { 3376 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3377 (uintmax_t)port->targ_port); 3378 3379 /* 3380 * Bail out as soon as we see that we've overfilled 3381 * the buffer. 3382 */ 3383 if (retval != 0) 3384 break; 3385 3386 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3387 "</frontend_type>\n", port->frontend->name); 3388 if (retval != 0) 3389 break; 3390 3391 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3392 port->port_type); 3393 if (retval != 0) 3394 break; 3395 3396 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3397 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3398 if (retval != 0) 3399 break; 3400 3401 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3402 port->port_name); 3403 if (retval != 0) 3404 break; 3405 3406 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3407 port->physical_port); 3408 if (retval != 0) 3409 break; 3410 3411 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3412 port->virtual_port); 3413 if (retval != 0) 3414 break; 3415 3416 if (port->target_devid != NULL) { 3417 sbuf_printf(sb, "\t<target>"); 3418 ctl_id_sbuf(port->target_devid, sb); 3419 sbuf_printf(sb, "</target>\n"); 3420 } 3421 3422 if (port->port_devid != NULL) { 3423 sbuf_printf(sb, "\t<port>"); 3424 ctl_id_sbuf(port->port_devid, sb); 3425 sbuf_printf(sb, "</port>\n"); 3426 } 3427 3428 if (port->port_info != NULL) { 3429 retval = port->port_info(port->onoff_arg, sb); 3430 if (retval != 0) 3431 break; 3432 } 3433 STAILQ_FOREACH(opt, &port->options, links) { 3434 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3435 opt->name, opt->value, opt->name); 3436 if (retval != 0) 3437 break; 3438 } 3439 3440 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3441 if (port->wwpn_iid[j].in_use == 0 || 3442 (port->wwpn_iid[j].wwpn == 0 && 3443 port->wwpn_iid[j].name == NULL)) 3444 continue; 3445 3446 if (port->wwpn_iid[j].name != NULL) 3447 retval = sbuf_printf(sb, 3448 "\t<initiator>%u %s</initiator>\n", 3449 j, port->wwpn_iid[j].name); 3450 else 3451 retval = sbuf_printf(sb, 3452 "\t<initiator>%u naa.%08jx</initiator>\n", 3453 j, port->wwpn_iid[j].wwpn); 3454 if (retval != 0) 3455 break; 3456 } 3457 if (retval != 0) 3458 break; 3459 3460 retval = sbuf_printf(sb, "</targ_port>\n"); 3461 if (retval != 0) 3462 break; 3463 } 3464 mtx_unlock(&softc->ctl_lock); 3465 3466 if ((retval != 0) 3467 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3468 retval = 0; 3469 sbuf_delete(sb); 3470 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3471 snprintf(list->error_str, sizeof(list->error_str), 3472 "Out of space, %d bytes is too small", 3473 list->alloc_len); 3474 break; 3475 } 3476 3477 sbuf_finish(sb); 3478 3479 retval = copyout(sbuf_data(sb), list->lun_xml, 3480 sbuf_len(sb) + 1); 3481 3482 list->fill_len = sbuf_len(sb) + 1; 3483 list->status = CTL_LUN_LIST_OK; 3484 sbuf_delete(sb); 3485 break; 3486 } 3487 default: { 3488 /* XXX KDM should we fix this? */ 3489 #if 0 3490 struct ctl_backend_driver *backend; 3491 unsigned int type; 3492 int found; 3493 3494 found = 0; 3495 3496 /* 3497 * We encode the backend type as the ioctl type for backend 3498 * ioctls. So parse it out here, and then search for a 3499 * backend of this type. 3500 */ 3501 type = _IOC_TYPE(cmd); 3502 3503 STAILQ_FOREACH(backend, &softc->be_list, links) { 3504 if (backend->type == type) { 3505 found = 1; 3506 break; 3507 } 3508 } 3509 if (found == 0) { 3510 printf("ctl: unknown ioctl command %#lx or backend " 3511 "%d\n", cmd, type); 3512 retval = EINVAL; 3513 break; 3514 } 3515 retval = backend->ioctl(dev, cmd, addr, flag, td); 3516 #endif 3517 retval = ENOTTY; 3518 break; 3519 } 3520 } 3521 return (retval); 3522 } 3523 3524 uint32_t 3525 ctl_get_initindex(struct ctl_nexus *nexus) 3526 { 3527 if (nexus->targ_port < CTL_MAX_PORTS) 3528 return (nexus->initid.id + 3529 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3530 else 3531 return (nexus->initid.id + 3532 ((nexus->targ_port - CTL_MAX_PORTS) * 3533 CTL_MAX_INIT_PER_PORT)); 3534 } 3535 3536 uint32_t 3537 ctl_get_resindex(struct ctl_nexus *nexus) 3538 { 3539 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3540 } 3541 3542 uint32_t 3543 ctl_port_idx(int port_num) 3544 { 3545 if (port_num < CTL_MAX_PORTS) 3546 return(port_num); 3547 else 3548 return(port_num - CTL_MAX_PORTS); 3549 } 3550 3551 static uint32_t 3552 ctl_map_lun(int port_num, uint32_t lun_id) 3553 { 3554 struct ctl_port *port; 3555 3556 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3557 if (port == NULL) 3558 return (UINT32_MAX); 3559 if (port->lun_map == NULL) 3560 return (lun_id); 3561 return (port->lun_map(port->targ_lun_arg, lun_id)); 3562 } 3563 3564 static uint32_t 3565 ctl_map_lun_back(int port_num, uint32_t lun_id) 3566 { 3567 struct ctl_port *port; 3568 uint32_t i; 3569 3570 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3571 if (port->lun_map == NULL) 3572 return (lun_id); 3573 for (i = 0; i < CTL_MAX_LUNS; i++) { 3574 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3575 return (i); 3576 } 3577 return (UINT32_MAX); 3578 } 3579 3580 /* 3581 * Note: This only works for bitmask sizes that are at least 32 bits, and 3582 * that are a power of 2. 3583 */ 3584 int 3585 ctl_ffz(uint32_t *mask, uint32_t size) 3586 { 3587 uint32_t num_chunks, num_pieces; 3588 int i, j; 3589 3590 num_chunks = (size >> 5); 3591 if (num_chunks == 0) 3592 num_chunks++; 3593 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3594 3595 for (i = 0; i < num_chunks; i++) { 3596 for (j = 0; j < num_pieces; j++) { 3597 if ((mask[i] & (1 << j)) == 0) 3598 return ((i << 5) + j); 3599 } 3600 } 3601 3602 return (-1); 3603 } 3604 3605 int 3606 ctl_set_mask(uint32_t *mask, uint32_t bit) 3607 { 3608 uint32_t chunk, piece; 3609 3610 chunk = bit >> 5; 3611 piece = bit % (sizeof(uint32_t) * 8); 3612 3613 if ((mask[chunk] & (1 << piece)) != 0) 3614 return (-1); 3615 else 3616 mask[chunk] |= (1 << piece); 3617 3618 return (0); 3619 } 3620 3621 int 3622 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3623 { 3624 uint32_t chunk, piece; 3625 3626 chunk = bit >> 5; 3627 piece = bit % (sizeof(uint32_t) * 8); 3628 3629 if ((mask[chunk] & (1 << piece)) == 0) 3630 return (-1); 3631 else 3632 mask[chunk] &= ~(1 << piece); 3633 3634 return (0); 3635 } 3636 3637 int 3638 ctl_is_set(uint32_t *mask, uint32_t bit) 3639 { 3640 uint32_t chunk, piece; 3641 3642 chunk = bit >> 5; 3643 piece = bit % (sizeof(uint32_t) * 8); 3644 3645 if ((mask[chunk] & (1 << piece)) == 0) 3646 return (0); 3647 else 3648 return (1); 3649 } 3650 3651 #ifdef unused 3652 /* 3653 * The bus, target and lun are optional, they can be filled in later. 3654 * can_wait is used to determine whether we can wait on the malloc or not. 3655 */ 3656 union ctl_io* 3657 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3658 uint32_t targ_lun, int can_wait) 3659 { 3660 union ctl_io *io; 3661 3662 if (can_wait) 3663 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3664 else 3665 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3666 3667 if (io != NULL) { 3668 io->io_hdr.io_type = io_type; 3669 io->io_hdr.targ_port = targ_port; 3670 /* 3671 * XXX KDM this needs to change/go away. We need to move 3672 * to a preallocated pool of ctl_scsiio structures. 3673 */ 3674 io->io_hdr.nexus.targ_target.id = targ_target; 3675 io->io_hdr.nexus.targ_lun = targ_lun; 3676 } 3677 3678 return (io); 3679 } 3680 3681 void 3682 ctl_kfree_io(union ctl_io *io) 3683 { 3684 free(io, M_CTL); 3685 } 3686 #endif /* unused */ 3687 3688 /* 3689 * ctl_softc, pool_name, total_ctl_io are passed in. 3690 * npool is passed out. 3691 */ 3692 int 3693 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3694 uint32_t total_ctl_io, void **npool) 3695 { 3696 #ifdef IO_POOLS 3697 struct ctl_io_pool *pool; 3698 3699 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3700 M_NOWAIT | M_ZERO); 3701 if (pool == NULL) 3702 return (ENOMEM); 3703 3704 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3705 pool->ctl_softc = ctl_softc; 3706 pool->zone = uma_zsecond_create(pool->name, NULL, 3707 NULL, NULL, NULL, ctl_softc->io_zone); 3708 /* uma_prealloc(pool->zone, total_ctl_io); */ 3709 3710 *npool = pool; 3711 #else 3712 *npool = ctl_softc->io_zone; 3713 #endif 3714 return (0); 3715 } 3716 3717 void 3718 ctl_pool_free(struct ctl_io_pool *pool) 3719 { 3720 3721 if (pool == NULL) 3722 return; 3723 3724 #ifdef IO_POOLS 3725 uma_zdestroy(pool->zone); 3726 free(pool, M_CTL); 3727 #endif 3728 } 3729 3730 union ctl_io * 3731 ctl_alloc_io(void *pool_ref) 3732 { 3733 union ctl_io *io; 3734 #ifdef IO_POOLS 3735 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3736 3737 io = uma_zalloc(pool->zone, M_WAITOK); 3738 #else 3739 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3740 #endif 3741 if (io != NULL) 3742 io->io_hdr.pool = pool_ref; 3743 return (io); 3744 } 3745 3746 union ctl_io * 3747 ctl_alloc_io_nowait(void *pool_ref) 3748 { 3749 union ctl_io *io; 3750 #ifdef IO_POOLS 3751 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3752 3753 io = uma_zalloc(pool->zone, M_NOWAIT); 3754 #else 3755 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3756 #endif 3757 if (io != NULL) 3758 io->io_hdr.pool = pool_ref; 3759 return (io); 3760 } 3761 3762 void 3763 ctl_free_io(union ctl_io *io) 3764 { 3765 #ifdef IO_POOLS 3766 struct ctl_io_pool *pool; 3767 #endif 3768 3769 if (io == NULL) 3770 return; 3771 3772 #ifdef IO_POOLS 3773 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3774 uma_zfree(pool->zone, io); 3775 #else 3776 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3777 #endif 3778 } 3779 3780 void 3781 ctl_zero_io(union ctl_io *io) 3782 { 3783 void *pool_ref; 3784 3785 if (io == NULL) 3786 return; 3787 3788 /* 3789 * May need to preserve linked list pointers at some point too. 3790 */ 3791 pool_ref = io->io_hdr.pool; 3792 memset(io, 0, sizeof(*io)); 3793 io->io_hdr.pool = pool_ref; 3794 } 3795 3796 /* 3797 * This routine is currently used for internal copies of ctl_ios that need 3798 * to persist for some reason after we've already returned status to the 3799 * FETD. (Thus the flag set.) 3800 * 3801 * XXX XXX 3802 * Note that this makes a blind copy of all fields in the ctl_io, except 3803 * for the pool reference. This includes any memory that has been 3804 * allocated! That memory will no longer be valid after done has been 3805 * called, so this would be VERY DANGEROUS for command that actually does 3806 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3807 * start and stop commands, which don't transfer any data, so this is not a 3808 * problem. If it is used for anything else, the caller would also need to 3809 * allocate data buffer space and this routine would need to be modified to 3810 * copy the data buffer(s) as well. 3811 */ 3812 void 3813 ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3814 { 3815 void *pool_ref; 3816 3817 if ((src == NULL) 3818 || (dest == NULL)) 3819 return; 3820 3821 /* 3822 * May need to preserve linked list pointers at some point too. 3823 */ 3824 pool_ref = dest->io_hdr.pool; 3825 3826 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3827 3828 dest->io_hdr.pool = pool_ref; 3829 /* 3830 * We need to know that this is an internal copy, and doesn't need 3831 * to get passed back to the FETD that allocated it. 3832 */ 3833 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3834 } 3835 3836 static int 3837 ctl_expand_number(const char *buf, uint64_t *num) 3838 { 3839 char *endptr; 3840 uint64_t number; 3841 unsigned shift; 3842 3843 number = strtoq(buf, &endptr, 0); 3844 3845 switch (tolower((unsigned char)*endptr)) { 3846 case 'e': 3847 shift = 60; 3848 break; 3849 case 'p': 3850 shift = 50; 3851 break; 3852 case 't': 3853 shift = 40; 3854 break; 3855 case 'g': 3856 shift = 30; 3857 break; 3858 case 'm': 3859 shift = 20; 3860 break; 3861 case 'k': 3862 shift = 10; 3863 break; 3864 case 'b': 3865 case '\0': /* No unit. */ 3866 *num = number; 3867 return (0); 3868 default: 3869 /* Unrecognized unit. */ 3870 return (-1); 3871 } 3872 3873 if ((number << shift) >> shift != number) { 3874 /* Overflow */ 3875 return (-1); 3876 } 3877 *num = number << shift; 3878 return (0); 3879 } 3880 3881 3882 /* 3883 * This routine could be used in the future to load default and/or saved 3884 * mode page parameters for a particuar lun. 3885 */ 3886 static int 3887 ctl_init_page_index(struct ctl_lun *lun) 3888 { 3889 int i; 3890 struct ctl_page_index *page_index; 3891 const char *value; 3892 uint64_t ival; 3893 3894 memcpy(&lun->mode_pages.index, page_index_template, 3895 sizeof(page_index_template)); 3896 3897 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3898 3899 page_index = &lun->mode_pages.index[i]; 3900 /* 3901 * If this is a disk-only mode page, there's no point in 3902 * setting it up. For some pages, we have to have some 3903 * basic information about the disk in order to calculate the 3904 * mode page data. 3905 */ 3906 if ((lun->be_lun->lun_type != T_DIRECT) 3907 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3908 continue; 3909 3910 switch (page_index->page_code & SMPH_PC_MASK) { 3911 case SMS_RW_ERROR_RECOVERY_PAGE: { 3912 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3913 panic("subpage is incorrect!"); 3914 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3915 &rw_er_page_default, 3916 sizeof(rw_er_page_default)); 3917 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3918 &rw_er_page_changeable, 3919 sizeof(rw_er_page_changeable)); 3920 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3921 &rw_er_page_default, 3922 sizeof(rw_er_page_default)); 3923 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3924 &rw_er_page_default, 3925 sizeof(rw_er_page_default)); 3926 page_index->page_data = 3927 (uint8_t *)lun->mode_pages.rw_er_page; 3928 break; 3929 } 3930 case SMS_FORMAT_DEVICE_PAGE: { 3931 struct scsi_format_page *format_page; 3932 3933 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3934 panic("subpage is incorrect!"); 3935 3936 /* 3937 * Sectors per track are set above. Bytes per 3938 * sector need to be set here on a per-LUN basis. 3939 */ 3940 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3941 &format_page_default, 3942 sizeof(format_page_default)); 3943 memcpy(&lun->mode_pages.format_page[ 3944 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3945 sizeof(format_page_changeable)); 3946 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3947 &format_page_default, 3948 sizeof(format_page_default)); 3949 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3950 &format_page_default, 3951 sizeof(format_page_default)); 3952 3953 format_page = &lun->mode_pages.format_page[ 3954 CTL_PAGE_CURRENT]; 3955 scsi_ulto2b(lun->be_lun->blocksize, 3956 format_page->bytes_per_sector); 3957 3958 format_page = &lun->mode_pages.format_page[ 3959 CTL_PAGE_DEFAULT]; 3960 scsi_ulto2b(lun->be_lun->blocksize, 3961 format_page->bytes_per_sector); 3962 3963 format_page = &lun->mode_pages.format_page[ 3964 CTL_PAGE_SAVED]; 3965 scsi_ulto2b(lun->be_lun->blocksize, 3966 format_page->bytes_per_sector); 3967 3968 page_index->page_data = 3969 (uint8_t *)lun->mode_pages.format_page; 3970 break; 3971 } 3972 case SMS_RIGID_DISK_PAGE: { 3973 struct scsi_rigid_disk_page *rigid_disk_page; 3974 uint32_t sectors_per_cylinder; 3975 uint64_t cylinders; 3976 #ifndef __XSCALE__ 3977 int shift; 3978 #endif /* !__XSCALE__ */ 3979 3980 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3981 panic("invalid subpage value %d", 3982 page_index->subpage); 3983 3984 /* 3985 * Rotation rate and sectors per track are set 3986 * above. We calculate the cylinders here based on 3987 * capacity. Due to the number of heads and 3988 * sectors per track we're using, smaller arrays 3989 * may turn out to have 0 cylinders. Linux and 3990 * FreeBSD don't pay attention to these mode pages 3991 * to figure out capacity, but Solaris does. It 3992 * seems to deal with 0 cylinders just fine, and 3993 * works out a fake geometry based on the capacity. 3994 */ 3995 memcpy(&lun->mode_pages.rigid_disk_page[ 3996 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3997 sizeof(rigid_disk_page_default)); 3998 memcpy(&lun->mode_pages.rigid_disk_page[ 3999 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4000 sizeof(rigid_disk_page_changeable)); 4001 4002 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4003 CTL_DEFAULT_HEADS; 4004 4005 /* 4006 * The divide method here will be more accurate, 4007 * probably, but results in floating point being 4008 * used in the kernel on i386 (__udivdi3()). On the 4009 * XScale, though, __udivdi3() is implemented in 4010 * software. 4011 * 4012 * The shift method for cylinder calculation is 4013 * accurate if sectors_per_cylinder is a power of 4014 * 2. Otherwise it might be slightly off -- you 4015 * might have a bit of a truncation problem. 4016 */ 4017 #ifdef __XSCALE__ 4018 cylinders = (lun->be_lun->maxlba + 1) / 4019 sectors_per_cylinder; 4020 #else 4021 for (shift = 31; shift > 0; shift--) { 4022 if (sectors_per_cylinder & (1 << shift)) 4023 break; 4024 } 4025 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4026 #endif 4027 4028 /* 4029 * We've basically got 3 bytes, or 24 bits for the 4030 * cylinder size in the mode page. If we're over, 4031 * just round down to 2^24. 4032 */ 4033 if (cylinders > 0xffffff) 4034 cylinders = 0xffffff; 4035 4036 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4037 CTL_PAGE_DEFAULT]; 4038 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4039 4040 if ((value = ctl_get_opt(&lun->be_lun->options, 4041 "rpm")) != NULL) { 4042 scsi_ulto2b(strtol(value, NULL, 0), 4043 rigid_disk_page->rotation_rate); 4044 } 4045 4046 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4047 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4048 sizeof(rigid_disk_page_default)); 4049 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4050 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4051 sizeof(rigid_disk_page_default)); 4052 4053 page_index->page_data = 4054 (uint8_t *)lun->mode_pages.rigid_disk_page; 4055 break; 4056 } 4057 case SMS_CACHING_PAGE: { 4058 struct scsi_caching_page *caching_page; 4059 4060 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4061 panic("invalid subpage value %d", 4062 page_index->subpage); 4063 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4064 &caching_page_default, 4065 sizeof(caching_page_default)); 4066 memcpy(&lun->mode_pages.caching_page[ 4067 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4068 sizeof(caching_page_changeable)); 4069 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4070 &caching_page_default, 4071 sizeof(caching_page_default)); 4072 caching_page = &lun->mode_pages.caching_page[ 4073 CTL_PAGE_SAVED]; 4074 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4075 if (value != NULL && strcmp(value, "off") == 0) 4076 caching_page->flags1 &= ~SCP_WCE; 4077 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4078 if (value != NULL && strcmp(value, "off") == 0) 4079 caching_page->flags1 |= SCP_RCD; 4080 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4081 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4082 sizeof(caching_page_default)); 4083 page_index->page_data = 4084 (uint8_t *)lun->mode_pages.caching_page; 4085 break; 4086 } 4087 case SMS_CONTROL_MODE_PAGE: { 4088 struct scsi_control_page *control_page; 4089 4090 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4091 panic("invalid subpage value %d", 4092 page_index->subpage); 4093 4094 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4095 &control_page_default, 4096 sizeof(control_page_default)); 4097 memcpy(&lun->mode_pages.control_page[ 4098 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4099 sizeof(control_page_changeable)); 4100 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4101 &control_page_default, 4102 sizeof(control_page_default)); 4103 control_page = &lun->mode_pages.control_page[ 4104 CTL_PAGE_SAVED]; 4105 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4106 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4107 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4108 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4109 } 4110 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4111 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4112 sizeof(control_page_default)); 4113 page_index->page_data = 4114 (uint8_t *)lun->mode_pages.control_page; 4115 break; 4116 4117 } 4118 case SMS_INFO_EXCEPTIONS_PAGE: { 4119 switch (page_index->subpage) { 4120 case SMS_SUBPAGE_PAGE_0: 4121 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4122 &ie_page_default, 4123 sizeof(ie_page_default)); 4124 memcpy(&lun->mode_pages.ie_page[ 4125 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4126 sizeof(ie_page_changeable)); 4127 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4128 &ie_page_default, 4129 sizeof(ie_page_default)); 4130 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4131 &ie_page_default, 4132 sizeof(ie_page_default)); 4133 page_index->page_data = 4134 (uint8_t *)lun->mode_pages.ie_page; 4135 break; 4136 case 0x02: { 4137 struct ctl_logical_block_provisioning_page *page; 4138 4139 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4140 &lbp_page_default, 4141 sizeof(lbp_page_default)); 4142 memcpy(&lun->mode_pages.lbp_page[ 4143 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4144 sizeof(lbp_page_changeable)); 4145 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4146 &lbp_page_default, 4147 sizeof(lbp_page_default)); 4148 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4149 value = ctl_get_opt(&lun->be_lun->options, 4150 "avail-threshold"); 4151 if (value != NULL && 4152 ctl_expand_number(value, &ival) == 0) { 4153 page->descr[0].flags |= SLBPPD_ENABLED | 4154 SLBPPD_ARMING_DEC; 4155 if (lun->be_lun->blocksize) 4156 ival /= lun->be_lun->blocksize; 4157 else 4158 ival /= 512; 4159 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4160 page->descr[0].count); 4161 } 4162 value = ctl_get_opt(&lun->be_lun->options, 4163 "used-threshold"); 4164 if (value != NULL && 4165 ctl_expand_number(value, &ival) == 0) { 4166 page->descr[1].flags |= SLBPPD_ENABLED | 4167 SLBPPD_ARMING_INC; 4168 if (lun->be_lun->blocksize) 4169 ival /= lun->be_lun->blocksize; 4170 else 4171 ival /= 512; 4172 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4173 page->descr[1].count); 4174 } 4175 value = ctl_get_opt(&lun->be_lun->options, 4176 "pool-avail-threshold"); 4177 if (value != NULL && 4178 ctl_expand_number(value, &ival) == 0) { 4179 page->descr[2].flags |= SLBPPD_ENABLED | 4180 SLBPPD_ARMING_DEC; 4181 if (lun->be_lun->blocksize) 4182 ival /= lun->be_lun->blocksize; 4183 else 4184 ival /= 512; 4185 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4186 page->descr[2].count); 4187 } 4188 value = ctl_get_opt(&lun->be_lun->options, 4189 "pool-used-threshold"); 4190 if (value != NULL && 4191 ctl_expand_number(value, &ival) == 0) { 4192 page->descr[3].flags |= SLBPPD_ENABLED | 4193 SLBPPD_ARMING_INC; 4194 if (lun->be_lun->blocksize) 4195 ival /= lun->be_lun->blocksize; 4196 else 4197 ival /= 512; 4198 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4199 page->descr[3].count); 4200 } 4201 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4202 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4203 sizeof(lbp_page_default)); 4204 page_index->page_data = 4205 (uint8_t *)lun->mode_pages.lbp_page; 4206 }} 4207 break; 4208 } 4209 case SMS_VENDOR_SPECIFIC_PAGE:{ 4210 switch (page_index->subpage) { 4211 case DBGCNF_SUBPAGE_CODE: { 4212 struct copan_debugconf_subpage *current_page, 4213 *saved_page; 4214 4215 memcpy(&lun->mode_pages.debugconf_subpage[ 4216 CTL_PAGE_CURRENT], 4217 &debugconf_page_default, 4218 sizeof(debugconf_page_default)); 4219 memcpy(&lun->mode_pages.debugconf_subpage[ 4220 CTL_PAGE_CHANGEABLE], 4221 &debugconf_page_changeable, 4222 sizeof(debugconf_page_changeable)); 4223 memcpy(&lun->mode_pages.debugconf_subpage[ 4224 CTL_PAGE_DEFAULT], 4225 &debugconf_page_default, 4226 sizeof(debugconf_page_default)); 4227 memcpy(&lun->mode_pages.debugconf_subpage[ 4228 CTL_PAGE_SAVED], 4229 &debugconf_page_default, 4230 sizeof(debugconf_page_default)); 4231 page_index->page_data = 4232 (uint8_t *)lun->mode_pages.debugconf_subpage; 4233 4234 current_page = (struct copan_debugconf_subpage *) 4235 (page_index->page_data + 4236 (page_index->page_len * 4237 CTL_PAGE_CURRENT)); 4238 saved_page = (struct copan_debugconf_subpage *) 4239 (page_index->page_data + 4240 (page_index->page_len * 4241 CTL_PAGE_SAVED)); 4242 break; 4243 } 4244 default: 4245 panic("invalid subpage value %d", 4246 page_index->subpage); 4247 break; 4248 } 4249 break; 4250 } 4251 default: 4252 panic("invalid page value %d", 4253 page_index->page_code & SMPH_PC_MASK); 4254 break; 4255 } 4256 } 4257 4258 return (CTL_RETVAL_COMPLETE); 4259 } 4260 4261 static int 4262 ctl_init_log_page_index(struct ctl_lun *lun) 4263 { 4264 struct ctl_page_index *page_index; 4265 int i, j, k, prev; 4266 4267 memcpy(&lun->log_pages.index, log_page_index_template, 4268 sizeof(log_page_index_template)); 4269 4270 prev = -1; 4271 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4272 4273 page_index = &lun->log_pages.index[i]; 4274 /* 4275 * If this is a disk-only mode page, there's no point in 4276 * setting it up. For some pages, we have to have some 4277 * basic information about the disk in order to calculate the 4278 * mode page data. 4279 */ 4280 if ((lun->be_lun->lun_type != T_DIRECT) 4281 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4282 continue; 4283 4284 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4285 ((lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 4286 lun->backend->lun_attr == NULL)) 4287 continue; 4288 4289 if (page_index->page_code != prev) { 4290 lun->log_pages.pages_page[j] = page_index->page_code; 4291 prev = page_index->page_code; 4292 j++; 4293 } 4294 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4295 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4296 k++; 4297 } 4298 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4299 lun->log_pages.index[0].page_len = j; 4300 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4301 lun->log_pages.index[1].page_len = k * 2; 4302 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4303 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4304 4305 return (CTL_RETVAL_COMPLETE); 4306 } 4307 4308 static int 4309 hex2bin(const char *str, uint8_t *buf, int buf_size) 4310 { 4311 int i; 4312 u_char c; 4313 4314 memset(buf, 0, buf_size); 4315 while (isspace(str[0])) 4316 str++; 4317 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4318 str += 2; 4319 buf_size *= 2; 4320 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4321 c = str[i]; 4322 if (isdigit(c)) 4323 c -= '0'; 4324 else if (isalpha(c)) 4325 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4326 else 4327 break; 4328 if (c >= 16) 4329 break; 4330 if ((i & 1) == 0) 4331 buf[i / 2] |= (c << 4); 4332 else 4333 buf[i / 2] |= c; 4334 } 4335 return ((i + 1) / 2); 4336 } 4337 4338 /* 4339 * LUN allocation. 4340 * 4341 * Requirements: 4342 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4343 * wants us to allocate the LUN and he can block. 4344 * - ctl_softc is always set 4345 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4346 * 4347 * Returns 0 for success, non-zero (errno) for failure. 4348 */ 4349 static int 4350 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4351 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4352 { 4353 struct ctl_lun *nlun, *lun; 4354 struct ctl_port *port; 4355 struct scsi_vpd_id_descriptor *desc; 4356 struct scsi_vpd_id_t10 *t10id; 4357 const char *eui, *naa, *scsiname, *vendor, *value; 4358 int lun_number, i, lun_malloced; 4359 int devidlen, idlen1, idlen2 = 0, len; 4360 4361 if (be_lun == NULL) 4362 return (EINVAL); 4363 4364 /* 4365 * We currently only support Direct Access or Processor LUN types. 4366 */ 4367 switch (be_lun->lun_type) { 4368 case T_DIRECT: 4369 break; 4370 case T_PROCESSOR: 4371 break; 4372 case T_SEQUENTIAL: 4373 case T_CHANGER: 4374 default: 4375 be_lun->lun_config_status(be_lun->be_lun, 4376 CTL_LUN_CONFIG_FAILURE); 4377 break; 4378 } 4379 if (ctl_lun == NULL) { 4380 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4381 lun_malloced = 1; 4382 } else { 4383 lun_malloced = 0; 4384 lun = ctl_lun; 4385 } 4386 4387 memset(lun, 0, sizeof(*lun)); 4388 if (lun_malloced) 4389 lun->flags = CTL_LUN_MALLOCED; 4390 4391 /* Generate LUN ID. */ 4392 devidlen = max(CTL_DEVID_MIN_LEN, 4393 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4394 idlen1 = sizeof(*t10id) + devidlen; 4395 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4396 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4397 if (scsiname != NULL) { 4398 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4399 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4400 } 4401 eui = ctl_get_opt(&be_lun->options, "eui"); 4402 if (eui != NULL) { 4403 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4404 } 4405 naa = ctl_get_opt(&be_lun->options, "naa"); 4406 if (naa != NULL) { 4407 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4408 } 4409 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4410 M_CTL, M_WAITOK | M_ZERO); 4411 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4412 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4413 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4414 desc->length = idlen1; 4415 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4416 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4417 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4418 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4419 } else { 4420 strncpy(t10id->vendor, vendor, 4421 min(sizeof(t10id->vendor), strlen(vendor))); 4422 } 4423 strncpy((char *)t10id->vendor_spec_id, 4424 (char *)be_lun->device_id, devidlen); 4425 if (scsiname != NULL) { 4426 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4427 desc->length); 4428 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4429 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4430 SVPD_ID_TYPE_SCSI_NAME; 4431 desc->length = idlen2; 4432 strlcpy(desc->identifier, scsiname, idlen2); 4433 } 4434 if (eui != NULL) { 4435 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4436 desc->length); 4437 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4438 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4439 SVPD_ID_TYPE_EUI64; 4440 desc->length = hex2bin(eui, desc->identifier, 16); 4441 desc->length = desc->length > 12 ? 16 : 4442 (desc->length > 8 ? 12 : 8); 4443 len -= 16 - desc->length; 4444 } 4445 if (naa != NULL) { 4446 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4447 desc->length); 4448 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4449 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4450 SVPD_ID_TYPE_NAA; 4451 desc->length = hex2bin(naa, desc->identifier, 16); 4452 desc->length = desc->length > 8 ? 16 : 8; 4453 len -= 16 - desc->length; 4454 } 4455 lun->lun_devid->len = len; 4456 4457 mtx_lock(&ctl_softc->ctl_lock); 4458 /* 4459 * See if the caller requested a particular LUN number. If so, see 4460 * if it is available. Otherwise, allocate the first available LUN. 4461 */ 4462 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4463 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4464 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4465 mtx_unlock(&ctl_softc->ctl_lock); 4466 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4467 printf("ctl: requested LUN ID %d is higher " 4468 "than CTL_MAX_LUNS - 1 (%d)\n", 4469 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4470 } else { 4471 /* 4472 * XXX KDM return an error, or just assign 4473 * another LUN ID in this case?? 4474 */ 4475 printf("ctl: requested LUN ID %d is already " 4476 "in use\n", be_lun->req_lun_id); 4477 } 4478 if (lun->flags & CTL_LUN_MALLOCED) 4479 free(lun, M_CTL); 4480 be_lun->lun_config_status(be_lun->be_lun, 4481 CTL_LUN_CONFIG_FAILURE); 4482 return (ENOSPC); 4483 } 4484 lun_number = be_lun->req_lun_id; 4485 } else { 4486 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4487 if (lun_number == -1) { 4488 mtx_unlock(&ctl_softc->ctl_lock); 4489 printf("ctl: can't allocate LUN on target %ju, out of " 4490 "LUNs\n", (uintmax_t)target_id.id); 4491 if (lun->flags & CTL_LUN_MALLOCED) 4492 free(lun, M_CTL); 4493 be_lun->lun_config_status(be_lun->be_lun, 4494 CTL_LUN_CONFIG_FAILURE); 4495 return (ENOSPC); 4496 } 4497 } 4498 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4499 4500 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4501 lun->target = target_id; 4502 lun->lun = lun_number; 4503 lun->be_lun = be_lun; 4504 /* 4505 * The processor LUN is always enabled. Disk LUNs come on line 4506 * disabled, and must be enabled by the backend. 4507 */ 4508 lun->flags |= CTL_LUN_DISABLED; 4509 lun->backend = be_lun->be; 4510 be_lun->ctl_lun = lun; 4511 be_lun->lun_id = lun_number; 4512 atomic_add_int(&be_lun->be->num_luns, 1); 4513 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4514 lun->flags |= CTL_LUN_OFFLINE; 4515 4516 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4517 lun->flags |= CTL_LUN_STOPPED; 4518 4519 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4520 lun->flags |= CTL_LUN_INOPERABLE; 4521 4522 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4523 lun->flags |= CTL_LUN_PRIMARY_SC; 4524 4525 value = ctl_get_opt(&be_lun->options, "readonly"); 4526 if (value != NULL && strcmp(value, "on") == 0) 4527 lun->flags |= CTL_LUN_READONLY; 4528 4529 lun->ctl_softc = ctl_softc; 4530 TAILQ_INIT(&lun->ooa_queue); 4531 TAILQ_INIT(&lun->blocked_queue); 4532 STAILQ_INIT(&lun->error_list); 4533 ctl_tpc_lun_init(lun); 4534 4535 /* 4536 * Initialize the mode and log page index. 4537 */ 4538 ctl_init_page_index(lun); 4539 ctl_init_log_page_index(lun); 4540 4541 /* 4542 * Set the poweron UA for all initiators on this LUN only. 4543 */ 4544 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4545 lun->pending_ua[i] = CTL_UA_POWERON; 4546 4547 /* 4548 * Now, before we insert this lun on the lun list, set the lun 4549 * inventory changed UA for all other luns. 4550 */ 4551 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4552 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4553 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4554 } 4555 } 4556 4557 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4558 4559 ctl_softc->ctl_luns[lun_number] = lun; 4560 4561 ctl_softc->num_luns++; 4562 4563 /* Setup statistics gathering */ 4564 lun->stats.device_type = be_lun->lun_type; 4565 lun->stats.lun_number = lun_number; 4566 if (lun->stats.device_type == T_DIRECT) 4567 lun->stats.blocksize = be_lun->blocksize; 4568 else 4569 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4570 for (i = 0;i < CTL_MAX_PORTS;i++) 4571 lun->stats.ports[i].targ_port = i; 4572 4573 mtx_unlock(&ctl_softc->ctl_lock); 4574 4575 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4576 4577 /* 4578 * Run through each registered FETD and bring it online if it isn't 4579 * already. Enable the target ID if it hasn't been enabled, and 4580 * enable this particular LUN. 4581 */ 4582 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4583 int retval; 4584 4585 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4586 if (retval != 0) { 4587 printf("ctl_alloc_lun: FETD %s port %d returned error " 4588 "%d for lun_enable on target %ju lun %d\n", 4589 port->port_name, port->targ_port, retval, 4590 (uintmax_t)target_id.id, lun_number); 4591 } else 4592 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4593 } 4594 return (0); 4595 } 4596 4597 /* 4598 * Delete a LUN. 4599 * Assumptions: 4600 * - LUN has already been marked invalid and any pending I/O has been taken 4601 * care of. 4602 */ 4603 static int 4604 ctl_free_lun(struct ctl_lun *lun) 4605 { 4606 struct ctl_softc *softc; 4607 #if 0 4608 struct ctl_port *port; 4609 #endif 4610 struct ctl_lun *nlun; 4611 int i; 4612 4613 softc = lun->ctl_softc; 4614 4615 mtx_assert(&softc->ctl_lock, MA_OWNED); 4616 4617 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4618 4619 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4620 4621 softc->ctl_luns[lun->lun] = NULL; 4622 4623 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4624 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4625 4626 softc->num_luns--; 4627 4628 /* 4629 * XXX KDM this scheme only works for a single target/multiple LUN 4630 * setup. It needs to be revamped for a multiple target scheme. 4631 * 4632 * XXX KDM this results in port->lun_disable() getting called twice, 4633 * once when ctl_disable_lun() is called, and a second time here. 4634 * We really need to re-think the LUN disable semantics. There 4635 * should probably be several steps/levels to LUN removal: 4636 * - disable 4637 * - invalidate 4638 * - free 4639 * 4640 * Right now we only have a disable method when communicating to 4641 * the front end ports, at least for individual LUNs. 4642 */ 4643 #if 0 4644 STAILQ_FOREACH(port, &softc->port_list, links) { 4645 int retval; 4646 4647 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4648 lun->lun); 4649 if (retval != 0) { 4650 printf("ctl_free_lun: FETD %s port %d returned error " 4651 "%d for lun_disable on target %ju lun %jd\n", 4652 port->port_name, port->targ_port, retval, 4653 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4654 } 4655 4656 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4657 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4658 4659 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4660 if (retval != 0) { 4661 printf("ctl_free_lun: FETD %s port %d " 4662 "returned error %d for targ_disable on " 4663 "target %ju\n", port->port_name, 4664 port->targ_port, retval, 4665 (uintmax_t)lun->target.id); 4666 } else 4667 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4668 4669 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4670 continue; 4671 4672 #if 0 4673 port->port_offline(port->onoff_arg); 4674 port->status &= ~CTL_PORT_STATUS_ONLINE; 4675 #endif 4676 } 4677 } 4678 #endif 4679 4680 /* 4681 * Tell the backend to free resources, if this LUN has a backend. 4682 */ 4683 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4684 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4685 4686 ctl_tpc_lun_shutdown(lun); 4687 mtx_destroy(&lun->lun_lock); 4688 free(lun->lun_devid, M_CTL); 4689 free(lun->write_buffer, M_CTL); 4690 if (lun->flags & CTL_LUN_MALLOCED) 4691 free(lun, M_CTL); 4692 4693 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4694 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4695 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4696 } 4697 } 4698 4699 return (0); 4700 } 4701 4702 static void 4703 ctl_create_lun(struct ctl_be_lun *be_lun) 4704 { 4705 struct ctl_softc *ctl_softc; 4706 4707 ctl_softc = control_softc; 4708 4709 /* 4710 * ctl_alloc_lun() should handle all potential failure cases. 4711 */ 4712 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4713 } 4714 4715 int 4716 ctl_add_lun(struct ctl_be_lun *be_lun) 4717 { 4718 struct ctl_softc *ctl_softc = control_softc; 4719 4720 mtx_lock(&ctl_softc->ctl_lock); 4721 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4722 mtx_unlock(&ctl_softc->ctl_lock); 4723 wakeup(&ctl_softc->pending_lun_queue); 4724 4725 return (0); 4726 } 4727 4728 int 4729 ctl_enable_lun(struct ctl_be_lun *be_lun) 4730 { 4731 struct ctl_softc *ctl_softc; 4732 struct ctl_port *port, *nport; 4733 struct ctl_lun *lun; 4734 int retval; 4735 4736 ctl_softc = control_softc; 4737 4738 lun = (struct ctl_lun *)be_lun->ctl_lun; 4739 4740 mtx_lock(&ctl_softc->ctl_lock); 4741 mtx_lock(&lun->lun_lock); 4742 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4743 /* 4744 * eh? Why did we get called if the LUN is already 4745 * enabled? 4746 */ 4747 mtx_unlock(&lun->lun_lock); 4748 mtx_unlock(&ctl_softc->ctl_lock); 4749 return (0); 4750 } 4751 lun->flags &= ~CTL_LUN_DISABLED; 4752 mtx_unlock(&lun->lun_lock); 4753 4754 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4755 nport = STAILQ_NEXT(port, links); 4756 4757 /* 4758 * Drop the lock while we call the FETD's enable routine. 4759 * This can lead to a callback into CTL (at least in the 4760 * case of the internal initiator frontend. 4761 */ 4762 mtx_unlock(&ctl_softc->ctl_lock); 4763 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4764 mtx_lock(&ctl_softc->ctl_lock); 4765 if (retval != 0) { 4766 printf("%s: FETD %s port %d returned error " 4767 "%d for lun_enable on target %ju lun %jd\n", 4768 __func__, port->port_name, port->targ_port, retval, 4769 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4770 } 4771 #if 0 4772 else { 4773 /* NOTE: TODO: why does lun enable affect port status? */ 4774 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4775 } 4776 #endif 4777 } 4778 4779 mtx_unlock(&ctl_softc->ctl_lock); 4780 4781 return (0); 4782 } 4783 4784 int 4785 ctl_disable_lun(struct ctl_be_lun *be_lun) 4786 { 4787 struct ctl_softc *ctl_softc; 4788 struct ctl_port *port; 4789 struct ctl_lun *lun; 4790 int retval; 4791 4792 ctl_softc = control_softc; 4793 4794 lun = (struct ctl_lun *)be_lun->ctl_lun; 4795 4796 mtx_lock(&ctl_softc->ctl_lock); 4797 mtx_lock(&lun->lun_lock); 4798 if (lun->flags & CTL_LUN_DISABLED) { 4799 mtx_unlock(&lun->lun_lock); 4800 mtx_unlock(&ctl_softc->ctl_lock); 4801 return (0); 4802 } 4803 lun->flags |= CTL_LUN_DISABLED; 4804 mtx_unlock(&lun->lun_lock); 4805 4806 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4807 mtx_unlock(&ctl_softc->ctl_lock); 4808 /* 4809 * Drop the lock before we call the frontend's disable 4810 * routine, to avoid lock order reversals. 4811 * 4812 * XXX KDM what happens if the frontend list changes while 4813 * we're traversing it? It's unlikely, but should be handled. 4814 */ 4815 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4816 lun->lun); 4817 mtx_lock(&ctl_softc->ctl_lock); 4818 if (retval != 0) { 4819 printf("ctl_alloc_lun: FETD %s port %d returned error " 4820 "%d for lun_disable on target %ju lun %jd\n", 4821 port->port_name, port->targ_port, retval, 4822 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4823 } 4824 } 4825 4826 mtx_unlock(&ctl_softc->ctl_lock); 4827 4828 return (0); 4829 } 4830 4831 int 4832 ctl_start_lun(struct ctl_be_lun *be_lun) 4833 { 4834 struct ctl_softc *ctl_softc; 4835 struct ctl_lun *lun; 4836 4837 ctl_softc = control_softc; 4838 4839 lun = (struct ctl_lun *)be_lun->ctl_lun; 4840 4841 mtx_lock(&lun->lun_lock); 4842 lun->flags &= ~CTL_LUN_STOPPED; 4843 mtx_unlock(&lun->lun_lock); 4844 4845 return (0); 4846 } 4847 4848 int 4849 ctl_stop_lun(struct ctl_be_lun *be_lun) 4850 { 4851 struct ctl_softc *ctl_softc; 4852 struct ctl_lun *lun; 4853 4854 ctl_softc = control_softc; 4855 4856 lun = (struct ctl_lun *)be_lun->ctl_lun; 4857 4858 mtx_lock(&lun->lun_lock); 4859 lun->flags |= CTL_LUN_STOPPED; 4860 mtx_unlock(&lun->lun_lock); 4861 4862 return (0); 4863 } 4864 4865 int 4866 ctl_lun_offline(struct ctl_be_lun *be_lun) 4867 { 4868 struct ctl_softc *ctl_softc; 4869 struct ctl_lun *lun; 4870 4871 ctl_softc = control_softc; 4872 4873 lun = (struct ctl_lun *)be_lun->ctl_lun; 4874 4875 mtx_lock(&lun->lun_lock); 4876 lun->flags |= CTL_LUN_OFFLINE; 4877 mtx_unlock(&lun->lun_lock); 4878 4879 return (0); 4880 } 4881 4882 int 4883 ctl_lun_online(struct ctl_be_lun *be_lun) 4884 { 4885 struct ctl_softc *ctl_softc; 4886 struct ctl_lun *lun; 4887 4888 ctl_softc = control_softc; 4889 4890 lun = (struct ctl_lun *)be_lun->ctl_lun; 4891 4892 mtx_lock(&lun->lun_lock); 4893 lun->flags &= ~CTL_LUN_OFFLINE; 4894 mtx_unlock(&lun->lun_lock); 4895 4896 return (0); 4897 } 4898 4899 int 4900 ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4901 { 4902 struct ctl_softc *ctl_softc; 4903 struct ctl_lun *lun; 4904 4905 ctl_softc = control_softc; 4906 4907 lun = (struct ctl_lun *)be_lun->ctl_lun; 4908 4909 mtx_lock(&lun->lun_lock); 4910 4911 /* 4912 * The LUN needs to be disabled before it can be marked invalid. 4913 */ 4914 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4915 mtx_unlock(&lun->lun_lock); 4916 return (-1); 4917 } 4918 /* 4919 * Mark the LUN invalid. 4920 */ 4921 lun->flags |= CTL_LUN_INVALID; 4922 4923 /* 4924 * If there is nothing in the OOA queue, go ahead and free the LUN. 4925 * If we have something in the OOA queue, we'll free it when the 4926 * last I/O completes. 4927 */ 4928 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4929 mtx_unlock(&lun->lun_lock); 4930 mtx_lock(&ctl_softc->ctl_lock); 4931 ctl_free_lun(lun); 4932 mtx_unlock(&ctl_softc->ctl_lock); 4933 } else 4934 mtx_unlock(&lun->lun_lock); 4935 4936 return (0); 4937 } 4938 4939 int 4940 ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4941 { 4942 struct ctl_softc *ctl_softc; 4943 struct ctl_lun *lun; 4944 4945 ctl_softc = control_softc; 4946 lun = (struct ctl_lun *)be_lun->ctl_lun; 4947 4948 mtx_lock(&lun->lun_lock); 4949 lun->flags |= CTL_LUN_INOPERABLE; 4950 mtx_unlock(&lun->lun_lock); 4951 4952 return (0); 4953 } 4954 4955 int 4956 ctl_lun_operable(struct ctl_be_lun *be_lun) 4957 { 4958 struct ctl_softc *ctl_softc; 4959 struct ctl_lun *lun; 4960 4961 ctl_softc = control_softc; 4962 lun = (struct ctl_lun *)be_lun->ctl_lun; 4963 4964 mtx_lock(&lun->lun_lock); 4965 lun->flags &= ~CTL_LUN_INOPERABLE; 4966 mtx_unlock(&lun->lun_lock); 4967 4968 return (0); 4969 } 4970 4971 void 4972 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4973 { 4974 struct ctl_lun *lun; 4975 struct ctl_softc *softc; 4976 int i; 4977 4978 softc = control_softc; 4979 4980 lun = (struct ctl_lun *)be_lun->ctl_lun; 4981 4982 mtx_lock(&lun->lun_lock); 4983 4984 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4985 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 4986 4987 mtx_unlock(&lun->lun_lock); 4988 } 4989 4990 /* 4991 * Backend "memory move is complete" callback for requests that never 4992 * make it down to say RAIDCore's configuration code. 4993 */ 4994 int 4995 ctl_config_move_done(union ctl_io *io) 4996 { 4997 int retval; 4998 4999 retval = CTL_RETVAL_COMPLETE; 5000 5001 5002 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5003 /* 5004 * XXX KDM this shouldn't happen, but what if it does? 5005 */ 5006 if (io->io_hdr.io_type != CTL_IO_SCSI) 5007 panic("I/O type isn't CTL_IO_SCSI!"); 5008 5009 if ((io->io_hdr.port_status == 0) 5010 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5011 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5012 io->io_hdr.status = CTL_SUCCESS; 5013 else if ((io->io_hdr.port_status != 0) 5014 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5015 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5016 /* 5017 * For hardware error sense keys, the sense key 5018 * specific value is defined to be a retry count, 5019 * but we use it to pass back an internal FETD 5020 * error code. XXX KDM Hopefully the FETD is only 5021 * using 16 bits for an error code, since that's 5022 * all the space we have in the sks field. 5023 */ 5024 ctl_set_internal_failure(&io->scsiio, 5025 /*sks_valid*/ 1, 5026 /*retry_count*/ 5027 io->io_hdr.port_status); 5028 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5029 free(io->scsiio.kern_data_ptr, M_CTL); 5030 ctl_done(io); 5031 goto bailout; 5032 } 5033 5034 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5035 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5036 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5037 /* 5038 * XXX KDM just assuming a single pointer here, and not a 5039 * S/G list. If we start using S/G lists for config data, 5040 * we'll need to know how to clean them up here as well. 5041 */ 5042 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5043 free(io->scsiio.kern_data_ptr, M_CTL); 5044 /* Hopefully the user has already set the status... */ 5045 ctl_done(io); 5046 } else { 5047 /* 5048 * XXX KDM now we need to continue data movement. Some 5049 * options: 5050 * - call ctl_scsiio() again? We don't do this for data 5051 * writes, because for those at least we know ahead of 5052 * time where the write will go and how long it is. For 5053 * config writes, though, that information is largely 5054 * contained within the write itself, thus we need to 5055 * parse out the data again. 5056 * 5057 * - Call some other function once the data is in? 5058 */ 5059 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5060 ctl_data_print(io); 5061 5062 /* 5063 * XXX KDM call ctl_scsiio() again for now, and check flag 5064 * bits to see whether we're allocated or not. 5065 */ 5066 retval = ctl_scsiio(&io->scsiio); 5067 } 5068 bailout: 5069 return (retval); 5070 } 5071 5072 /* 5073 * This gets called by a backend driver when it is done with a 5074 * data_submit method. 5075 */ 5076 void 5077 ctl_data_submit_done(union ctl_io *io) 5078 { 5079 /* 5080 * If the IO_CONT flag is set, we need to call the supplied 5081 * function to continue processing the I/O, instead of completing 5082 * the I/O just yet. 5083 * 5084 * If there is an error, though, we don't want to keep processing. 5085 * Instead, just send status back to the initiator. 5086 */ 5087 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5088 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5089 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5090 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5091 io->scsiio.io_cont(io); 5092 return; 5093 } 5094 ctl_done(io); 5095 } 5096 5097 /* 5098 * This gets called by a backend driver when it is done with a 5099 * configuration write. 5100 */ 5101 void 5102 ctl_config_write_done(union ctl_io *io) 5103 { 5104 uint8_t *buf; 5105 5106 /* 5107 * If the IO_CONT flag is set, we need to call the supplied 5108 * function to continue processing the I/O, instead of completing 5109 * the I/O just yet. 5110 * 5111 * If there is an error, though, we don't want to keep processing. 5112 * Instead, just send status back to the initiator. 5113 */ 5114 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5115 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5116 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5117 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5118 io->scsiio.io_cont(io); 5119 return; 5120 } 5121 /* 5122 * Since a configuration write can be done for commands that actually 5123 * have data allocated, like write buffer, and commands that have 5124 * no data, like start/stop unit, we need to check here. 5125 */ 5126 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5127 buf = io->scsiio.kern_data_ptr; 5128 else 5129 buf = NULL; 5130 ctl_done(io); 5131 if (buf) 5132 free(buf, M_CTL); 5133 } 5134 5135 /* 5136 * SCSI release command. 5137 */ 5138 int 5139 ctl_scsi_release(struct ctl_scsiio *ctsio) 5140 { 5141 int length, longid, thirdparty_id, resv_id; 5142 struct ctl_softc *ctl_softc; 5143 struct ctl_lun *lun; 5144 uint32_t residx; 5145 5146 length = 0; 5147 resv_id = 0; 5148 5149 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5150 5151 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5152 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5153 ctl_softc = control_softc; 5154 5155 switch (ctsio->cdb[0]) { 5156 case RELEASE_10: { 5157 struct scsi_release_10 *cdb; 5158 5159 cdb = (struct scsi_release_10 *)ctsio->cdb; 5160 5161 if (cdb->byte2 & SR10_LONGID) 5162 longid = 1; 5163 else 5164 thirdparty_id = cdb->thirdparty_id; 5165 5166 resv_id = cdb->resv_id; 5167 length = scsi_2btoul(cdb->length); 5168 break; 5169 } 5170 } 5171 5172 5173 /* 5174 * XXX KDM right now, we only support LUN reservation. We don't 5175 * support 3rd party reservations, or extent reservations, which 5176 * might actually need the parameter list. If we've gotten this 5177 * far, we've got a LUN reservation. Anything else got kicked out 5178 * above. So, according to SPC, ignore the length. 5179 */ 5180 length = 0; 5181 5182 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5183 && (length > 0)) { 5184 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5185 ctsio->kern_data_len = length; 5186 ctsio->kern_total_len = length; 5187 ctsio->kern_data_resid = 0; 5188 ctsio->kern_rel_offset = 0; 5189 ctsio->kern_sg_entries = 0; 5190 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5191 ctsio->be_move_done = ctl_config_move_done; 5192 ctl_datamove((union ctl_io *)ctsio); 5193 5194 return (CTL_RETVAL_COMPLETE); 5195 } 5196 5197 if (length > 0) 5198 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5199 5200 mtx_lock(&lun->lun_lock); 5201 5202 /* 5203 * According to SPC, it is not an error for an intiator to attempt 5204 * to release a reservation on a LUN that isn't reserved, or that 5205 * is reserved by another initiator. The reservation can only be 5206 * released, though, by the initiator who made it or by one of 5207 * several reset type events. 5208 */ 5209 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5210 lun->flags &= ~CTL_LUN_RESERVED; 5211 5212 mtx_unlock(&lun->lun_lock); 5213 5214 ctsio->scsi_status = SCSI_STATUS_OK; 5215 ctsio->io_hdr.status = CTL_SUCCESS; 5216 5217 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5218 free(ctsio->kern_data_ptr, M_CTL); 5219 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5220 } 5221 5222 ctl_done((union ctl_io *)ctsio); 5223 return (CTL_RETVAL_COMPLETE); 5224 } 5225 5226 int 5227 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5228 { 5229 int extent, thirdparty, longid; 5230 int resv_id, length; 5231 uint64_t thirdparty_id; 5232 struct ctl_softc *ctl_softc; 5233 struct ctl_lun *lun; 5234 uint32_t residx; 5235 5236 extent = 0; 5237 thirdparty = 0; 5238 longid = 0; 5239 resv_id = 0; 5240 length = 0; 5241 thirdparty_id = 0; 5242 5243 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5244 5245 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5246 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5247 ctl_softc = control_softc; 5248 5249 switch (ctsio->cdb[0]) { 5250 case RESERVE_10: { 5251 struct scsi_reserve_10 *cdb; 5252 5253 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5254 5255 if (cdb->byte2 & SR10_LONGID) 5256 longid = 1; 5257 else 5258 thirdparty_id = cdb->thirdparty_id; 5259 5260 resv_id = cdb->resv_id; 5261 length = scsi_2btoul(cdb->length); 5262 break; 5263 } 5264 } 5265 5266 /* 5267 * XXX KDM right now, we only support LUN reservation. We don't 5268 * support 3rd party reservations, or extent reservations, which 5269 * might actually need the parameter list. If we've gotten this 5270 * far, we've got a LUN reservation. Anything else got kicked out 5271 * above. So, according to SPC, ignore the length. 5272 */ 5273 length = 0; 5274 5275 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5276 && (length > 0)) { 5277 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5278 ctsio->kern_data_len = length; 5279 ctsio->kern_total_len = length; 5280 ctsio->kern_data_resid = 0; 5281 ctsio->kern_rel_offset = 0; 5282 ctsio->kern_sg_entries = 0; 5283 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5284 ctsio->be_move_done = ctl_config_move_done; 5285 ctl_datamove((union ctl_io *)ctsio); 5286 5287 return (CTL_RETVAL_COMPLETE); 5288 } 5289 5290 if (length > 0) 5291 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5292 5293 mtx_lock(&lun->lun_lock); 5294 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5295 ctl_set_reservation_conflict(ctsio); 5296 goto bailout; 5297 } 5298 5299 lun->flags |= CTL_LUN_RESERVED; 5300 lun->res_idx = residx; 5301 5302 ctsio->scsi_status = SCSI_STATUS_OK; 5303 ctsio->io_hdr.status = CTL_SUCCESS; 5304 5305 bailout: 5306 mtx_unlock(&lun->lun_lock); 5307 5308 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5309 free(ctsio->kern_data_ptr, M_CTL); 5310 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5311 } 5312 5313 ctl_done((union ctl_io *)ctsio); 5314 return (CTL_RETVAL_COMPLETE); 5315 } 5316 5317 int 5318 ctl_start_stop(struct ctl_scsiio *ctsio) 5319 { 5320 struct scsi_start_stop_unit *cdb; 5321 struct ctl_lun *lun; 5322 struct ctl_softc *ctl_softc; 5323 int retval; 5324 5325 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5326 5327 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5328 ctl_softc = control_softc; 5329 retval = 0; 5330 5331 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5332 5333 /* 5334 * XXX KDM 5335 * We don't support the immediate bit on a stop unit. In order to 5336 * do that, we would need to code up a way to know that a stop is 5337 * pending, and hold off any new commands until it completes, one 5338 * way or another. Then we could accept or reject those commands 5339 * depending on its status. We would almost need to do the reverse 5340 * of what we do below for an immediate start -- return the copy of 5341 * the ctl_io to the FETD with status to send to the host (and to 5342 * free the copy!) and then free the original I/O once the stop 5343 * actually completes. That way, the OOA queue mechanism can work 5344 * to block commands that shouldn't proceed. Another alternative 5345 * would be to put the copy in the queue in place of the original, 5346 * and return the original back to the caller. That could be 5347 * slightly safer.. 5348 */ 5349 if ((cdb->byte2 & SSS_IMMED) 5350 && ((cdb->how & SSS_START) == 0)) { 5351 ctl_set_invalid_field(ctsio, 5352 /*sks_valid*/ 1, 5353 /*command*/ 1, 5354 /*field*/ 1, 5355 /*bit_valid*/ 1, 5356 /*bit*/ 0); 5357 ctl_done((union ctl_io *)ctsio); 5358 return (CTL_RETVAL_COMPLETE); 5359 } 5360 5361 if ((lun->flags & CTL_LUN_PR_RESERVED) 5362 && ((cdb->how & SSS_START)==0)) { 5363 uint32_t residx; 5364 5365 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5366 if (lun->pr_keys[residx] == 0 5367 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5368 5369 ctl_set_reservation_conflict(ctsio); 5370 ctl_done((union ctl_io *)ctsio); 5371 return (CTL_RETVAL_COMPLETE); 5372 } 5373 } 5374 5375 /* 5376 * If there is no backend on this device, we can't start or stop 5377 * it. In theory we shouldn't get any start/stop commands in the 5378 * first place at this level if the LUN doesn't have a backend. 5379 * That should get stopped by the command decode code. 5380 */ 5381 if (lun->backend == NULL) { 5382 ctl_set_invalid_opcode(ctsio); 5383 ctl_done((union ctl_io *)ctsio); 5384 return (CTL_RETVAL_COMPLETE); 5385 } 5386 5387 /* 5388 * XXX KDM Copan-specific offline behavior. 5389 * Figure out a reasonable way to port this? 5390 */ 5391 #ifdef NEEDTOPORT 5392 mtx_lock(&lun->lun_lock); 5393 5394 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5395 && (lun->flags & CTL_LUN_OFFLINE)) { 5396 /* 5397 * If the LUN is offline, and the on/offline bit isn't set, 5398 * reject the start or stop. Otherwise, let it through. 5399 */ 5400 mtx_unlock(&lun->lun_lock); 5401 ctl_set_lun_not_ready(ctsio); 5402 ctl_done((union ctl_io *)ctsio); 5403 } else { 5404 mtx_unlock(&lun->lun_lock); 5405 #endif /* NEEDTOPORT */ 5406 /* 5407 * This could be a start or a stop when we're online, 5408 * or a stop/offline or start/online. A start or stop when 5409 * we're offline is covered in the case above. 5410 */ 5411 /* 5412 * In the non-immediate case, we send the request to 5413 * the backend and return status to the user when 5414 * it is done. 5415 * 5416 * In the immediate case, we allocate a new ctl_io 5417 * to hold a copy of the request, and send that to 5418 * the backend. We then set good status on the 5419 * user's request and return it immediately. 5420 */ 5421 if (cdb->byte2 & SSS_IMMED) { 5422 union ctl_io *new_io; 5423 5424 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5425 ctl_copy_io((union ctl_io *)ctsio, new_io); 5426 retval = lun->backend->config_write(new_io); 5427 ctl_set_success(ctsio); 5428 ctl_done((union ctl_io *)ctsio); 5429 } else { 5430 retval = lun->backend->config_write( 5431 (union ctl_io *)ctsio); 5432 } 5433 #ifdef NEEDTOPORT 5434 } 5435 #endif 5436 return (retval); 5437 } 5438 5439 /* 5440 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5441 * we don't really do anything with the LBA and length fields if the user 5442 * passes them in. Instead we'll just flush out the cache for the entire 5443 * LUN. 5444 */ 5445 int 5446 ctl_sync_cache(struct ctl_scsiio *ctsio) 5447 { 5448 struct ctl_lun *lun; 5449 struct ctl_softc *ctl_softc; 5450 uint64_t starting_lba; 5451 uint32_t block_count; 5452 int retval; 5453 5454 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5455 5456 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5457 ctl_softc = control_softc; 5458 retval = 0; 5459 5460 switch (ctsio->cdb[0]) { 5461 case SYNCHRONIZE_CACHE: { 5462 struct scsi_sync_cache *cdb; 5463 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5464 5465 starting_lba = scsi_4btoul(cdb->begin_lba); 5466 block_count = scsi_2btoul(cdb->lb_count); 5467 break; 5468 } 5469 case SYNCHRONIZE_CACHE_16: { 5470 struct scsi_sync_cache_16 *cdb; 5471 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5472 5473 starting_lba = scsi_8btou64(cdb->begin_lba); 5474 block_count = scsi_4btoul(cdb->lb_count); 5475 break; 5476 } 5477 default: 5478 ctl_set_invalid_opcode(ctsio); 5479 ctl_done((union ctl_io *)ctsio); 5480 goto bailout; 5481 break; /* NOTREACHED */ 5482 } 5483 5484 /* 5485 * We check the LBA and length, but don't do anything with them. 5486 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5487 * get flushed. This check will just help satisfy anyone who wants 5488 * to see an error for an out of range LBA. 5489 */ 5490 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5491 ctl_set_lba_out_of_range(ctsio); 5492 ctl_done((union ctl_io *)ctsio); 5493 goto bailout; 5494 } 5495 5496 /* 5497 * If this LUN has no backend, we can't flush the cache anyway. 5498 */ 5499 if (lun->backend == NULL) { 5500 ctl_set_invalid_opcode(ctsio); 5501 ctl_done((union ctl_io *)ctsio); 5502 goto bailout; 5503 } 5504 5505 /* 5506 * Check to see whether we're configured to send the SYNCHRONIZE 5507 * CACHE command directly to the back end. 5508 */ 5509 mtx_lock(&lun->lun_lock); 5510 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5511 && (++(lun->sync_count) >= lun->sync_interval)) { 5512 lun->sync_count = 0; 5513 mtx_unlock(&lun->lun_lock); 5514 retval = lun->backend->config_write((union ctl_io *)ctsio); 5515 } else { 5516 mtx_unlock(&lun->lun_lock); 5517 ctl_set_success(ctsio); 5518 ctl_done((union ctl_io *)ctsio); 5519 } 5520 5521 bailout: 5522 5523 return (retval); 5524 } 5525 5526 int 5527 ctl_format(struct ctl_scsiio *ctsio) 5528 { 5529 struct scsi_format *cdb; 5530 struct ctl_lun *lun; 5531 struct ctl_softc *ctl_softc; 5532 int length, defect_list_len; 5533 5534 CTL_DEBUG_PRINT(("ctl_format\n")); 5535 5536 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5537 ctl_softc = control_softc; 5538 5539 cdb = (struct scsi_format *)ctsio->cdb; 5540 5541 length = 0; 5542 if (cdb->byte2 & SF_FMTDATA) { 5543 if (cdb->byte2 & SF_LONGLIST) 5544 length = sizeof(struct scsi_format_header_long); 5545 else 5546 length = sizeof(struct scsi_format_header_short); 5547 } 5548 5549 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5550 && (length > 0)) { 5551 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5552 ctsio->kern_data_len = length; 5553 ctsio->kern_total_len = length; 5554 ctsio->kern_data_resid = 0; 5555 ctsio->kern_rel_offset = 0; 5556 ctsio->kern_sg_entries = 0; 5557 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5558 ctsio->be_move_done = ctl_config_move_done; 5559 ctl_datamove((union ctl_io *)ctsio); 5560 5561 return (CTL_RETVAL_COMPLETE); 5562 } 5563 5564 defect_list_len = 0; 5565 5566 if (cdb->byte2 & SF_FMTDATA) { 5567 if (cdb->byte2 & SF_LONGLIST) { 5568 struct scsi_format_header_long *header; 5569 5570 header = (struct scsi_format_header_long *) 5571 ctsio->kern_data_ptr; 5572 5573 defect_list_len = scsi_4btoul(header->defect_list_len); 5574 if (defect_list_len != 0) { 5575 ctl_set_invalid_field(ctsio, 5576 /*sks_valid*/ 1, 5577 /*command*/ 0, 5578 /*field*/ 2, 5579 /*bit_valid*/ 0, 5580 /*bit*/ 0); 5581 goto bailout; 5582 } 5583 } else { 5584 struct scsi_format_header_short *header; 5585 5586 header = (struct scsi_format_header_short *) 5587 ctsio->kern_data_ptr; 5588 5589 defect_list_len = scsi_2btoul(header->defect_list_len); 5590 if (defect_list_len != 0) { 5591 ctl_set_invalid_field(ctsio, 5592 /*sks_valid*/ 1, 5593 /*command*/ 0, 5594 /*field*/ 2, 5595 /*bit_valid*/ 0, 5596 /*bit*/ 0); 5597 goto bailout; 5598 } 5599 } 5600 } 5601 5602 /* 5603 * The format command will clear out the "Medium format corrupted" 5604 * status if set by the configuration code. That status is really 5605 * just a way to notify the host that we have lost the media, and 5606 * get them to issue a command that will basically make them think 5607 * they're blowing away the media. 5608 */ 5609 mtx_lock(&lun->lun_lock); 5610 lun->flags &= ~CTL_LUN_INOPERABLE; 5611 mtx_unlock(&lun->lun_lock); 5612 5613 ctsio->scsi_status = SCSI_STATUS_OK; 5614 ctsio->io_hdr.status = CTL_SUCCESS; 5615 bailout: 5616 5617 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5618 free(ctsio->kern_data_ptr, M_CTL); 5619 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5620 } 5621 5622 ctl_done((union ctl_io *)ctsio); 5623 return (CTL_RETVAL_COMPLETE); 5624 } 5625 5626 int 5627 ctl_read_buffer(struct ctl_scsiio *ctsio) 5628 { 5629 struct scsi_read_buffer *cdb; 5630 struct ctl_lun *lun; 5631 int buffer_offset, len; 5632 static uint8_t descr[4]; 5633 static uint8_t echo_descr[4] = { 0 }; 5634 5635 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5636 5637 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5638 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5639 5640 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5641 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5642 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5643 ctl_set_invalid_field(ctsio, 5644 /*sks_valid*/ 1, 5645 /*command*/ 1, 5646 /*field*/ 1, 5647 /*bit_valid*/ 1, 5648 /*bit*/ 4); 5649 ctl_done((union ctl_io *)ctsio); 5650 return (CTL_RETVAL_COMPLETE); 5651 } 5652 5653 len = scsi_3btoul(cdb->length); 5654 buffer_offset = scsi_3btoul(cdb->offset); 5655 5656 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5657 ctl_set_invalid_field(ctsio, 5658 /*sks_valid*/ 1, 5659 /*command*/ 1, 5660 /*field*/ 6, 5661 /*bit_valid*/ 0, 5662 /*bit*/ 0); 5663 ctl_done((union ctl_io *)ctsio); 5664 return (CTL_RETVAL_COMPLETE); 5665 } 5666 5667 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5668 descr[0] = 0; 5669 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5670 ctsio->kern_data_ptr = descr; 5671 len = min(len, sizeof(descr)); 5672 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5673 ctsio->kern_data_ptr = echo_descr; 5674 len = min(len, sizeof(echo_descr)); 5675 } else { 5676 if (lun->write_buffer == NULL) { 5677 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5678 M_CTL, M_WAITOK); 5679 } 5680 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5681 } 5682 ctsio->kern_data_len = len; 5683 ctsio->kern_total_len = len; 5684 ctsio->kern_data_resid = 0; 5685 ctsio->kern_rel_offset = 0; 5686 ctsio->kern_sg_entries = 0; 5687 ctsio->be_move_done = ctl_config_move_done; 5688 ctl_datamove((union ctl_io *)ctsio); 5689 5690 return (CTL_RETVAL_COMPLETE); 5691 } 5692 5693 int 5694 ctl_write_buffer(struct ctl_scsiio *ctsio) 5695 { 5696 struct scsi_write_buffer *cdb; 5697 struct ctl_lun *lun; 5698 int buffer_offset, len; 5699 5700 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5701 5702 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5703 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5704 5705 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5706 ctl_set_invalid_field(ctsio, 5707 /*sks_valid*/ 1, 5708 /*command*/ 1, 5709 /*field*/ 1, 5710 /*bit_valid*/ 1, 5711 /*bit*/ 4); 5712 ctl_done((union ctl_io *)ctsio); 5713 return (CTL_RETVAL_COMPLETE); 5714 } 5715 5716 len = scsi_3btoul(cdb->length); 5717 buffer_offset = scsi_3btoul(cdb->offset); 5718 5719 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5720 ctl_set_invalid_field(ctsio, 5721 /*sks_valid*/ 1, 5722 /*command*/ 1, 5723 /*field*/ 6, 5724 /*bit_valid*/ 0, 5725 /*bit*/ 0); 5726 ctl_done((union ctl_io *)ctsio); 5727 return (CTL_RETVAL_COMPLETE); 5728 } 5729 5730 /* 5731 * If we've got a kernel request that hasn't been malloced yet, 5732 * malloc it and tell the caller the data buffer is here. 5733 */ 5734 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5735 if (lun->write_buffer == NULL) { 5736 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5737 M_CTL, M_WAITOK); 5738 } 5739 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5740 ctsio->kern_data_len = len; 5741 ctsio->kern_total_len = len; 5742 ctsio->kern_data_resid = 0; 5743 ctsio->kern_rel_offset = 0; 5744 ctsio->kern_sg_entries = 0; 5745 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5746 ctsio->be_move_done = ctl_config_move_done; 5747 ctl_datamove((union ctl_io *)ctsio); 5748 5749 return (CTL_RETVAL_COMPLETE); 5750 } 5751 5752 ctl_done((union ctl_io *)ctsio); 5753 5754 return (CTL_RETVAL_COMPLETE); 5755 } 5756 5757 int 5758 ctl_write_same(struct ctl_scsiio *ctsio) 5759 { 5760 struct ctl_lun *lun; 5761 struct ctl_lba_len_flags *lbalen; 5762 uint64_t lba; 5763 uint32_t num_blocks; 5764 int len, retval; 5765 uint8_t byte2; 5766 5767 retval = CTL_RETVAL_COMPLETE; 5768 5769 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5770 5771 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5772 5773 switch (ctsio->cdb[0]) { 5774 case WRITE_SAME_10: { 5775 struct scsi_write_same_10 *cdb; 5776 5777 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5778 5779 lba = scsi_4btoul(cdb->addr); 5780 num_blocks = scsi_2btoul(cdb->length); 5781 byte2 = cdb->byte2; 5782 break; 5783 } 5784 case WRITE_SAME_16: { 5785 struct scsi_write_same_16 *cdb; 5786 5787 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5788 5789 lba = scsi_8btou64(cdb->addr); 5790 num_blocks = scsi_4btoul(cdb->length); 5791 byte2 = cdb->byte2; 5792 break; 5793 } 5794 default: 5795 /* 5796 * We got a command we don't support. This shouldn't 5797 * happen, commands should be filtered out above us. 5798 */ 5799 ctl_set_invalid_opcode(ctsio); 5800 ctl_done((union ctl_io *)ctsio); 5801 5802 return (CTL_RETVAL_COMPLETE); 5803 break; /* NOTREACHED */ 5804 } 5805 5806 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5807 if ((byte2 & SWS_UNMAP) == 0 && 5808 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5809 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5810 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5811 ctl_done((union ctl_io *)ctsio); 5812 return (CTL_RETVAL_COMPLETE); 5813 } 5814 5815 /* 5816 * The first check is to make sure we're in bounds, the second 5817 * check is to catch wrap-around problems. If the lba + num blocks 5818 * is less than the lba, then we've wrapped around and the block 5819 * range is invalid anyway. 5820 */ 5821 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5822 || ((lba + num_blocks) < lba)) { 5823 ctl_set_lba_out_of_range(ctsio); 5824 ctl_done((union ctl_io *)ctsio); 5825 return (CTL_RETVAL_COMPLETE); 5826 } 5827 5828 /* Zero number of blocks means "to the last logical block" */ 5829 if (num_blocks == 0) { 5830 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5831 ctl_set_invalid_field(ctsio, 5832 /*sks_valid*/ 0, 5833 /*command*/ 1, 5834 /*field*/ 0, 5835 /*bit_valid*/ 0, 5836 /*bit*/ 0); 5837 ctl_done((union ctl_io *)ctsio); 5838 return (CTL_RETVAL_COMPLETE); 5839 } 5840 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5841 } 5842 5843 len = lun->be_lun->blocksize; 5844 5845 /* 5846 * If we've got a kernel request that hasn't been malloced yet, 5847 * malloc it and tell the caller the data buffer is here. 5848 */ 5849 if ((byte2 & SWS_NDOB) == 0 && 5850 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5851 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5852 ctsio->kern_data_len = len; 5853 ctsio->kern_total_len = len; 5854 ctsio->kern_data_resid = 0; 5855 ctsio->kern_rel_offset = 0; 5856 ctsio->kern_sg_entries = 0; 5857 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5858 ctsio->be_move_done = ctl_config_move_done; 5859 ctl_datamove((union ctl_io *)ctsio); 5860 5861 return (CTL_RETVAL_COMPLETE); 5862 } 5863 5864 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5865 lbalen->lba = lba; 5866 lbalen->len = num_blocks; 5867 lbalen->flags = byte2; 5868 retval = lun->backend->config_write((union ctl_io *)ctsio); 5869 5870 return (retval); 5871 } 5872 5873 int 5874 ctl_unmap(struct ctl_scsiio *ctsio) 5875 { 5876 struct ctl_lun *lun; 5877 struct scsi_unmap *cdb; 5878 struct ctl_ptr_len_flags *ptrlen; 5879 struct scsi_unmap_header *hdr; 5880 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5881 uint64_t lba; 5882 uint32_t num_blocks; 5883 int len, retval; 5884 uint8_t byte2; 5885 5886 retval = CTL_RETVAL_COMPLETE; 5887 5888 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5889 5890 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5891 cdb = (struct scsi_unmap *)ctsio->cdb; 5892 5893 len = scsi_2btoul(cdb->length); 5894 byte2 = cdb->byte2; 5895 5896 /* 5897 * If we've got a kernel request that hasn't been malloced yet, 5898 * malloc it and tell the caller the data buffer is here. 5899 */ 5900 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5901 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5902 ctsio->kern_data_len = len; 5903 ctsio->kern_total_len = len; 5904 ctsio->kern_data_resid = 0; 5905 ctsio->kern_rel_offset = 0; 5906 ctsio->kern_sg_entries = 0; 5907 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5908 ctsio->be_move_done = ctl_config_move_done; 5909 ctl_datamove((union ctl_io *)ctsio); 5910 5911 return (CTL_RETVAL_COMPLETE); 5912 } 5913 5914 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5915 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5916 if (len < sizeof (*hdr) || 5917 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5918 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5919 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5920 ctl_set_invalid_field(ctsio, 5921 /*sks_valid*/ 0, 5922 /*command*/ 0, 5923 /*field*/ 0, 5924 /*bit_valid*/ 0, 5925 /*bit*/ 0); 5926 ctl_done((union ctl_io *)ctsio); 5927 return (CTL_RETVAL_COMPLETE); 5928 } 5929 len = scsi_2btoul(hdr->desc_length); 5930 buf = (struct scsi_unmap_desc *)(hdr + 1); 5931 end = buf + len / sizeof(*buf); 5932 5933 endnz = buf; 5934 for (range = buf; range < end; range++) { 5935 lba = scsi_8btou64(range->lba); 5936 num_blocks = scsi_4btoul(range->length); 5937 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5938 || ((lba + num_blocks) < lba)) { 5939 ctl_set_lba_out_of_range(ctsio); 5940 ctl_done((union ctl_io *)ctsio); 5941 return (CTL_RETVAL_COMPLETE); 5942 } 5943 if (num_blocks != 0) 5944 endnz = range + 1; 5945 } 5946 5947 /* 5948 * Block backend can not handle zero last range. 5949 * Filter it out and return if there is nothing left. 5950 */ 5951 len = (uint8_t *)endnz - (uint8_t *)buf; 5952 if (len == 0) { 5953 ctl_set_success(ctsio); 5954 ctl_done((union ctl_io *)ctsio); 5955 return (CTL_RETVAL_COMPLETE); 5956 } 5957 5958 mtx_lock(&lun->lun_lock); 5959 ptrlen = (struct ctl_ptr_len_flags *) 5960 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5961 ptrlen->ptr = (void *)buf; 5962 ptrlen->len = len; 5963 ptrlen->flags = byte2; 5964 ctl_check_blocked(lun); 5965 mtx_unlock(&lun->lun_lock); 5966 5967 retval = lun->backend->config_write((union ctl_io *)ctsio); 5968 return (retval); 5969 } 5970 5971 /* 5972 * Note that this function currently doesn't actually do anything inside 5973 * CTL to enforce things if the DQue bit is turned on. 5974 * 5975 * Also note that this function can't be used in the default case, because 5976 * the DQue bit isn't set in the changeable mask for the control mode page 5977 * anyway. This is just here as an example for how to implement a page 5978 * handler, and a placeholder in case we want to allow the user to turn 5979 * tagged queueing on and off. 5980 * 5981 * The D_SENSE bit handling is functional, however, and will turn 5982 * descriptor sense on and off for a given LUN. 5983 */ 5984 int 5985 ctl_control_page_handler(struct ctl_scsiio *ctsio, 5986 struct ctl_page_index *page_index, uint8_t *page_ptr) 5987 { 5988 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5989 struct ctl_lun *lun; 5990 struct ctl_softc *softc; 5991 int set_ua; 5992 uint32_t initidx; 5993 5994 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5995 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5996 set_ua = 0; 5997 5998 user_cp = (struct scsi_control_page *)page_ptr; 5999 current_cp = (struct scsi_control_page *) 6000 (page_index->page_data + (page_index->page_len * 6001 CTL_PAGE_CURRENT)); 6002 saved_cp = (struct scsi_control_page *) 6003 (page_index->page_data + (page_index->page_len * 6004 CTL_PAGE_SAVED)); 6005 6006 softc = control_softc; 6007 6008 mtx_lock(&lun->lun_lock); 6009 if (((current_cp->rlec & SCP_DSENSE) == 0) 6010 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6011 /* 6012 * Descriptor sense is currently turned off and the user 6013 * wants to turn it on. 6014 */ 6015 current_cp->rlec |= SCP_DSENSE; 6016 saved_cp->rlec |= SCP_DSENSE; 6017 lun->flags |= CTL_LUN_SENSE_DESC; 6018 set_ua = 1; 6019 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6020 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6021 /* 6022 * Descriptor sense is currently turned on, and the user 6023 * wants to turn it off. 6024 */ 6025 current_cp->rlec &= ~SCP_DSENSE; 6026 saved_cp->rlec &= ~SCP_DSENSE; 6027 lun->flags &= ~CTL_LUN_SENSE_DESC; 6028 set_ua = 1; 6029 } 6030 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6031 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6032 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6033 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6034 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6035 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6036 set_ua = 1; 6037 } 6038 if ((current_cp->eca_and_aen & SCP_SWP) != 6039 (user_cp->eca_and_aen & SCP_SWP)) { 6040 current_cp->eca_and_aen &= ~SCP_SWP; 6041 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6042 saved_cp->eca_and_aen &= ~SCP_SWP; 6043 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6044 set_ua = 1; 6045 } 6046 if (set_ua != 0) { 6047 int i; 6048 /* 6049 * Let other initiators know that the mode 6050 * parameters for this LUN have changed. 6051 */ 6052 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6053 if (i == initidx) 6054 continue; 6055 6056 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6057 } 6058 } 6059 mtx_unlock(&lun->lun_lock); 6060 6061 return (0); 6062 } 6063 6064 int 6065 ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6066 struct ctl_page_index *page_index, uint8_t *page_ptr) 6067 { 6068 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6069 struct ctl_lun *lun; 6070 int set_ua; 6071 uint32_t initidx; 6072 6073 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6074 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6075 set_ua = 0; 6076 6077 user_cp = (struct scsi_caching_page *)page_ptr; 6078 current_cp = (struct scsi_caching_page *) 6079 (page_index->page_data + (page_index->page_len * 6080 CTL_PAGE_CURRENT)); 6081 saved_cp = (struct scsi_caching_page *) 6082 (page_index->page_data + (page_index->page_len * 6083 CTL_PAGE_SAVED)); 6084 6085 mtx_lock(&lun->lun_lock); 6086 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6087 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6088 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6089 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6090 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6091 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6092 set_ua = 1; 6093 } 6094 if (set_ua != 0) { 6095 int i; 6096 /* 6097 * Let other initiators know that the mode 6098 * parameters for this LUN have changed. 6099 */ 6100 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6101 if (i == initidx) 6102 continue; 6103 6104 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6105 } 6106 } 6107 mtx_unlock(&lun->lun_lock); 6108 6109 return (0); 6110 } 6111 6112 int 6113 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6114 struct ctl_page_index *page_index, 6115 uint8_t *page_ptr) 6116 { 6117 uint8_t *c; 6118 int i; 6119 6120 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6121 ctl_time_io_secs = 6122 (c[0] << 8) | 6123 (c[1] << 0) | 6124 0; 6125 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6126 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6127 printf("page data:"); 6128 for (i=0; i<8; i++) 6129 printf(" %.2x",page_ptr[i]); 6130 printf("\n"); 6131 return (0); 6132 } 6133 6134 int 6135 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6136 struct ctl_page_index *page_index, 6137 int pc) 6138 { 6139 struct copan_debugconf_subpage *page; 6140 6141 page = (struct copan_debugconf_subpage *)page_index->page_data + 6142 (page_index->page_len * pc); 6143 6144 switch (pc) { 6145 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6146 case SMS_PAGE_CTRL_DEFAULT >> 6: 6147 case SMS_PAGE_CTRL_SAVED >> 6: 6148 /* 6149 * We don't update the changable or default bits for this page. 6150 */ 6151 break; 6152 case SMS_PAGE_CTRL_CURRENT >> 6: 6153 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6154 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6155 break; 6156 default: 6157 #ifdef NEEDTOPORT 6158 EPRINT(0, "Invalid PC %d!!", pc); 6159 #endif /* NEEDTOPORT */ 6160 break; 6161 } 6162 return (0); 6163 } 6164 6165 6166 static int 6167 ctl_do_mode_select(union ctl_io *io) 6168 { 6169 struct scsi_mode_page_header *page_header; 6170 struct ctl_page_index *page_index; 6171 struct ctl_scsiio *ctsio; 6172 int control_dev, page_len; 6173 int page_len_offset, page_len_size; 6174 union ctl_modepage_info *modepage_info; 6175 struct ctl_lun *lun; 6176 int *len_left, *len_used; 6177 int retval, i; 6178 6179 ctsio = &io->scsiio; 6180 page_index = NULL; 6181 page_len = 0; 6182 retval = CTL_RETVAL_COMPLETE; 6183 6184 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6185 6186 if (lun->be_lun->lun_type != T_DIRECT) 6187 control_dev = 1; 6188 else 6189 control_dev = 0; 6190 6191 modepage_info = (union ctl_modepage_info *) 6192 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6193 len_left = &modepage_info->header.len_left; 6194 len_used = &modepage_info->header.len_used; 6195 6196 do_next_page: 6197 6198 page_header = (struct scsi_mode_page_header *) 6199 (ctsio->kern_data_ptr + *len_used); 6200 6201 if (*len_left == 0) { 6202 free(ctsio->kern_data_ptr, M_CTL); 6203 ctl_set_success(ctsio); 6204 ctl_done((union ctl_io *)ctsio); 6205 return (CTL_RETVAL_COMPLETE); 6206 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6207 6208 free(ctsio->kern_data_ptr, M_CTL); 6209 ctl_set_param_len_error(ctsio); 6210 ctl_done((union ctl_io *)ctsio); 6211 return (CTL_RETVAL_COMPLETE); 6212 6213 } else if ((page_header->page_code & SMPH_SPF) 6214 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6215 6216 free(ctsio->kern_data_ptr, M_CTL); 6217 ctl_set_param_len_error(ctsio); 6218 ctl_done((union ctl_io *)ctsio); 6219 return (CTL_RETVAL_COMPLETE); 6220 } 6221 6222 6223 /* 6224 * XXX KDM should we do something with the block descriptor? 6225 */ 6226 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6227 6228 if ((control_dev != 0) 6229 && (lun->mode_pages.index[i].page_flags & 6230 CTL_PAGE_FLAG_DISK_ONLY)) 6231 continue; 6232 6233 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6234 (page_header->page_code & SMPH_PC_MASK)) 6235 continue; 6236 6237 /* 6238 * If neither page has a subpage code, then we've got a 6239 * match. 6240 */ 6241 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6242 && ((page_header->page_code & SMPH_SPF) == 0)) { 6243 page_index = &lun->mode_pages.index[i]; 6244 page_len = page_header->page_length; 6245 break; 6246 } 6247 6248 /* 6249 * If both pages have subpages, then the subpage numbers 6250 * have to match. 6251 */ 6252 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6253 && (page_header->page_code & SMPH_SPF)) { 6254 struct scsi_mode_page_header_sp *sph; 6255 6256 sph = (struct scsi_mode_page_header_sp *)page_header; 6257 6258 if (lun->mode_pages.index[i].subpage == 6259 sph->subpage) { 6260 page_index = &lun->mode_pages.index[i]; 6261 page_len = scsi_2btoul(sph->page_length); 6262 break; 6263 } 6264 } 6265 } 6266 6267 /* 6268 * If we couldn't find the page, or if we don't have a mode select 6269 * handler for it, send back an error to the user. 6270 */ 6271 if ((page_index == NULL) 6272 || (page_index->select_handler == NULL)) { 6273 ctl_set_invalid_field(ctsio, 6274 /*sks_valid*/ 1, 6275 /*command*/ 0, 6276 /*field*/ *len_used, 6277 /*bit_valid*/ 0, 6278 /*bit*/ 0); 6279 free(ctsio->kern_data_ptr, M_CTL); 6280 ctl_done((union ctl_io *)ctsio); 6281 return (CTL_RETVAL_COMPLETE); 6282 } 6283 6284 if (page_index->page_code & SMPH_SPF) { 6285 page_len_offset = 2; 6286 page_len_size = 2; 6287 } else { 6288 page_len_size = 1; 6289 page_len_offset = 1; 6290 } 6291 6292 /* 6293 * If the length the initiator gives us isn't the one we specify in 6294 * the mode page header, or if they didn't specify enough data in 6295 * the CDB to avoid truncating this page, kick out the request. 6296 */ 6297 if ((page_len != (page_index->page_len - page_len_offset - 6298 page_len_size)) 6299 || (*len_left < page_index->page_len)) { 6300 6301 6302 ctl_set_invalid_field(ctsio, 6303 /*sks_valid*/ 1, 6304 /*command*/ 0, 6305 /*field*/ *len_used + page_len_offset, 6306 /*bit_valid*/ 0, 6307 /*bit*/ 0); 6308 free(ctsio->kern_data_ptr, M_CTL); 6309 ctl_done((union ctl_io *)ctsio); 6310 return (CTL_RETVAL_COMPLETE); 6311 } 6312 6313 /* 6314 * Run through the mode page, checking to make sure that the bits 6315 * the user changed are actually legal for him to change. 6316 */ 6317 for (i = 0; i < page_index->page_len; i++) { 6318 uint8_t *user_byte, *change_mask, *current_byte; 6319 int bad_bit; 6320 int j; 6321 6322 user_byte = (uint8_t *)page_header + i; 6323 change_mask = page_index->page_data + 6324 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6325 current_byte = page_index->page_data + 6326 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6327 6328 /* 6329 * Check to see whether the user set any bits in this byte 6330 * that he is not allowed to set. 6331 */ 6332 if ((*user_byte & ~(*change_mask)) == 6333 (*current_byte & ~(*change_mask))) 6334 continue; 6335 6336 /* 6337 * Go through bit by bit to determine which one is illegal. 6338 */ 6339 bad_bit = 0; 6340 for (j = 7; j >= 0; j--) { 6341 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6342 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6343 bad_bit = i; 6344 break; 6345 } 6346 } 6347 ctl_set_invalid_field(ctsio, 6348 /*sks_valid*/ 1, 6349 /*command*/ 0, 6350 /*field*/ *len_used + i, 6351 /*bit_valid*/ 1, 6352 /*bit*/ bad_bit); 6353 free(ctsio->kern_data_ptr, M_CTL); 6354 ctl_done((union ctl_io *)ctsio); 6355 return (CTL_RETVAL_COMPLETE); 6356 } 6357 6358 /* 6359 * Decrement these before we call the page handler, since we may 6360 * end up getting called back one way or another before the handler 6361 * returns to this context. 6362 */ 6363 *len_left -= page_index->page_len; 6364 *len_used += page_index->page_len; 6365 6366 retval = page_index->select_handler(ctsio, page_index, 6367 (uint8_t *)page_header); 6368 6369 /* 6370 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6371 * wait until this queued command completes to finish processing 6372 * the mode page. If it returns anything other than 6373 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6374 * already set the sense information, freed the data pointer, and 6375 * completed the io for us. 6376 */ 6377 if (retval != CTL_RETVAL_COMPLETE) 6378 goto bailout_no_done; 6379 6380 /* 6381 * If the initiator sent us more than one page, parse the next one. 6382 */ 6383 if (*len_left > 0) 6384 goto do_next_page; 6385 6386 ctl_set_success(ctsio); 6387 free(ctsio->kern_data_ptr, M_CTL); 6388 ctl_done((union ctl_io *)ctsio); 6389 6390 bailout_no_done: 6391 6392 return (CTL_RETVAL_COMPLETE); 6393 6394 } 6395 6396 int 6397 ctl_mode_select(struct ctl_scsiio *ctsio) 6398 { 6399 int param_len, pf, sp; 6400 int header_size, bd_len; 6401 int len_left, len_used; 6402 struct ctl_page_index *page_index; 6403 struct ctl_lun *lun; 6404 int control_dev, page_len; 6405 union ctl_modepage_info *modepage_info; 6406 int retval; 6407 6408 pf = 0; 6409 sp = 0; 6410 page_len = 0; 6411 len_used = 0; 6412 len_left = 0; 6413 retval = 0; 6414 bd_len = 0; 6415 page_index = NULL; 6416 6417 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6418 6419 if (lun->be_lun->lun_type != T_DIRECT) 6420 control_dev = 1; 6421 else 6422 control_dev = 0; 6423 6424 switch (ctsio->cdb[0]) { 6425 case MODE_SELECT_6: { 6426 struct scsi_mode_select_6 *cdb; 6427 6428 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6429 6430 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6431 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6432 6433 param_len = cdb->length; 6434 header_size = sizeof(struct scsi_mode_header_6); 6435 break; 6436 } 6437 case MODE_SELECT_10: { 6438 struct scsi_mode_select_10 *cdb; 6439 6440 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6441 6442 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6443 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6444 6445 param_len = scsi_2btoul(cdb->length); 6446 header_size = sizeof(struct scsi_mode_header_10); 6447 break; 6448 } 6449 default: 6450 ctl_set_invalid_opcode(ctsio); 6451 ctl_done((union ctl_io *)ctsio); 6452 return (CTL_RETVAL_COMPLETE); 6453 break; /* NOTREACHED */ 6454 } 6455 6456 /* 6457 * From SPC-3: 6458 * "A parameter list length of zero indicates that the Data-Out Buffer 6459 * shall be empty. This condition shall not be considered as an error." 6460 */ 6461 if (param_len == 0) { 6462 ctl_set_success(ctsio); 6463 ctl_done((union ctl_io *)ctsio); 6464 return (CTL_RETVAL_COMPLETE); 6465 } 6466 6467 /* 6468 * Since we'll hit this the first time through, prior to 6469 * allocation, we don't need to free a data buffer here. 6470 */ 6471 if (param_len < header_size) { 6472 ctl_set_param_len_error(ctsio); 6473 ctl_done((union ctl_io *)ctsio); 6474 return (CTL_RETVAL_COMPLETE); 6475 } 6476 6477 /* 6478 * Allocate the data buffer and grab the user's data. In theory, 6479 * we shouldn't have to sanity check the parameter list length here 6480 * because the maximum size is 64K. We should be able to malloc 6481 * that much without too many problems. 6482 */ 6483 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6484 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6485 ctsio->kern_data_len = param_len; 6486 ctsio->kern_total_len = param_len; 6487 ctsio->kern_data_resid = 0; 6488 ctsio->kern_rel_offset = 0; 6489 ctsio->kern_sg_entries = 0; 6490 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6491 ctsio->be_move_done = ctl_config_move_done; 6492 ctl_datamove((union ctl_io *)ctsio); 6493 6494 return (CTL_RETVAL_COMPLETE); 6495 } 6496 6497 switch (ctsio->cdb[0]) { 6498 case MODE_SELECT_6: { 6499 struct scsi_mode_header_6 *mh6; 6500 6501 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6502 bd_len = mh6->blk_desc_len; 6503 break; 6504 } 6505 case MODE_SELECT_10: { 6506 struct scsi_mode_header_10 *mh10; 6507 6508 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6509 bd_len = scsi_2btoul(mh10->blk_desc_len); 6510 break; 6511 } 6512 default: 6513 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6514 break; 6515 } 6516 6517 if (param_len < (header_size + bd_len)) { 6518 free(ctsio->kern_data_ptr, M_CTL); 6519 ctl_set_param_len_error(ctsio); 6520 ctl_done((union ctl_io *)ctsio); 6521 return (CTL_RETVAL_COMPLETE); 6522 } 6523 6524 /* 6525 * Set the IO_CONT flag, so that if this I/O gets passed to 6526 * ctl_config_write_done(), it'll get passed back to 6527 * ctl_do_mode_select() for further processing, or completion if 6528 * we're all done. 6529 */ 6530 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6531 ctsio->io_cont = ctl_do_mode_select; 6532 6533 modepage_info = (union ctl_modepage_info *) 6534 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6535 6536 memset(modepage_info, 0, sizeof(*modepage_info)); 6537 6538 len_left = param_len - header_size - bd_len; 6539 len_used = header_size + bd_len; 6540 6541 modepage_info->header.len_left = len_left; 6542 modepage_info->header.len_used = len_used; 6543 6544 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6545 } 6546 6547 int 6548 ctl_mode_sense(struct ctl_scsiio *ctsio) 6549 { 6550 struct ctl_lun *lun; 6551 int pc, page_code, dbd, llba, subpage; 6552 int alloc_len, page_len, header_len, total_len; 6553 struct scsi_mode_block_descr *block_desc; 6554 struct ctl_page_index *page_index; 6555 int control_dev; 6556 6557 dbd = 0; 6558 llba = 0; 6559 block_desc = NULL; 6560 page_index = NULL; 6561 6562 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6563 6564 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6565 6566 if (lun->be_lun->lun_type != T_DIRECT) 6567 control_dev = 1; 6568 else 6569 control_dev = 0; 6570 6571 switch (ctsio->cdb[0]) { 6572 case MODE_SENSE_6: { 6573 struct scsi_mode_sense_6 *cdb; 6574 6575 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6576 6577 header_len = sizeof(struct scsi_mode_hdr_6); 6578 if (cdb->byte2 & SMS_DBD) 6579 dbd = 1; 6580 else 6581 header_len += sizeof(struct scsi_mode_block_descr); 6582 6583 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6584 page_code = cdb->page & SMS_PAGE_CODE; 6585 subpage = cdb->subpage; 6586 alloc_len = cdb->length; 6587 break; 6588 } 6589 case MODE_SENSE_10: { 6590 struct scsi_mode_sense_10 *cdb; 6591 6592 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6593 6594 header_len = sizeof(struct scsi_mode_hdr_10); 6595 6596 if (cdb->byte2 & SMS_DBD) 6597 dbd = 1; 6598 else 6599 header_len += sizeof(struct scsi_mode_block_descr); 6600 if (cdb->byte2 & SMS10_LLBAA) 6601 llba = 1; 6602 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6603 page_code = cdb->page & SMS_PAGE_CODE; 6604 subpage = cdb->subpage; 6605 alloc_len = scsi_2btoul(cdb->length); 6606 break; 6607 } 6608 default: 6609 ctl_set_invalid_opcode(ctsio); 6610 ctl_done((union ctl_io *)ctsio); 6611 return (CTL_RETVAL_COMPLETE); 6612 break; /* NOTREACHED */ 6613 } 6614 6615 /* 6616 * We have to make a first pass through to calculate the size of 6617 * the pages that match the user's query. Then we allocate enough 6618 * memory to hold it, and actually copy the data into the buffer. 6619 */ 6620 switch (page_code) { 6621 case SMS_ALL_PAGES_PAGE: { 6622 int i; 6623 6624 page_len = 0; 6625 6626 /* 6627 * At the moment, values other than 0 and 0xff here are 6628 * reserved according to SPC-3. 6629 */ 6630 if ((subpage != SMS_SUBPAGE_PAGE_0) 6631 && (subpage != SMS_SUBPAGE_ALL)) { 6632 ctl_set_invalid_field(ctsio, 6633 /*sks_valid*/ 1, 6634 /*command*/ 1, 6635 /*field*/ 3, 6636 /*bit_valid*/ 0, 6637 /*bit*/ 0); 6638 ctl_done((union ctl_io *)ctsio); 6639 return (CTL_RETVAL_COMPLETE); 6640 } 6641 6642 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6643 if ((control_dev != 0) 6644 && (lun->mode_pages.index[i].page_flags & 6645 CTL_PAGE_FLAG_DISK_ONLY)) 6646 continue; 6647 6648 /* 6649 * We don't use this subpage if the user didn't 6650 * request all subpages. 6651 */ 6652 if ((lun->mode_pages.index[i].subpage != 0) 6653 && (subpage == SMS_SUBPAGE_PAGE_0)) 6654 continue; 6655 6656 #if 0 6657 printf("found page %#x len %d\n", 6658 lun->mode_pages.index[i].page_code & 6659 SMPH_PC_MASK, 6660 lun->mode_pages.index[i].page_len); 6661 #endif 6662 page_len += lun->mode_pages.index[i].page_len; 6663 } 6664 break; 6665 } 6666 default: { 6667 int i; 6668 6669 page_len = 0; 6670 6671 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6672 /* Look for the right page code */ 6673 if ((lun->mode_pages.index[i].page_code & 6674 SMPH_PC_MASK) != page_code) 6675 continue; 6676 6677 /* Look for the right subpage or the subpage wildcard*/ 6678 if ((lun->mode_pages.index[i].subpage != subpage) 6679 && (subpage != SMS_SUBPAGE_ALL)) 6680 continue; 6681 6682 /* Make sure the page is supported for this dev type */ 6683 if ((control_dev != 0) 6684 && (lun->mode_pages.index[i].page_flags & 6685 CTL_PAGE_FLAG_DISK_ONLY)) 6686 continue; 6687 6688 #if 0 6689 printf("found page %#x len %d\n", 6690 lun->mode_pages.index[i].page_code & 6691 SMPH_PC_MASK, 6692 lun->mode_pages.index[i].page_len); 6693 #endif 6694 6695 page_len += lun->mode_pages.index[i].page_len; 6696 } 6697 6698 if (page_len == 0) { 6699 ctl_set_invalid_field(ctsio, 6700 /*sks_valid*/ 1, 6701 /*command*/ 1, 6702 /*field*/ 2, 6703 /*bit_valid*/ 1, 6704 /*bit*/ 5); 6705 ctl_done((union ctl_io *)ctsio); 6706 return (CTL_RETVAL_COMPLETE); 6707 } 6708 break; 6709 } 6710 } 6711 6712 total_len = header_len + page_len; 6713 #if 0 6714 printf("header_len = %d, page_len = %d, total_len = %d\n", 6715 header_len, page_len, total_len); 6716 #endif 6717 6718 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6719 ctsio->kern_sg_entries = 0; 6720 ctsio->kern_data_resid = 0; 6721 ctsio->kern_rel_offset = 0; 6722 if (total_len < alloc_len) { 6723 ctsio->residual = alloc_len - total_len; 6724 ctsio->kern_data_len = total_len; 6725 ctsio->kern_total_len = total_len; 6726 } else { 6727 ctsio->residual = 0; 6728 ctsio->kern_data_len = alloc_len; 6729 ctsio->kern_total_len = alloc_len; 6730 } 6731 6732 switch (ctsio->cdb[0]) { 6733 case MODE_SENSE_6: { 6734 struct scsi_mode_hdr_6 *header; 6735 6736 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6737 6738 header->datalen = ctl_min(total_len - 1, 254); 6739 if (control_dev == 0) { 6740 header->dev_specific = 0x10; /* DPOFUA */ 6741 if ((lun->flags & CTL_LUN_READONLY) || 6742 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6743 .eca_and_aen & SCP_SWP) != 0) 6744 header->dev_specific |= 0x80; /* WP */ 6745 } 6746 if (dbd) 6747 header->block_descr_len = 0; 6748 else 6749 header->block_descr_len = 6750 sizeof(struct scsi_mode_block_descr); 6751 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6752 break; 6753 } 6754 case MODE_SENSE_10: { 6755 struct scsi_mode_hdr_10 *header; 6756 int datalen; 6757 6758 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6759 6760 datalen = ctl_min(total_len - 2, 65533); 6761 scsi_ulto2b(datalen, header->datalen); 6762 if (control_dev == 0) { 6763 header->dev_specific = 0x10; /* DPOFUA */ 6764 if ((lun->flags & CTL_LUN_READONLY) || 6765 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6766 .eca_and_aen & SCP_SWP) != 0) 6767 header->dev_specific |= 0x80; /* WP */ 6768 } 6769 if (dbd) 6770 scsi_ulto2b(0, header->block_descr_len); 6771 else 6772 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6773 header->block_descr_len); 6774 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6775 break; 6776 } 6777 default: 6778 panic("invalid CDB type %#x", ctsio->cdb[0]); 6779 break; /* NOTREACHED */ 6780 } 6781 6782 /* 6783 * If we've got a disk, use its blocksize in the block 6784 * descriptor. Otherwise, just set it to 0. 6785 */ 6786 if (dbd == 0) { 6787 if (control_dev == 0) 6788 scsi_ulto3b(lun->be_lun->blocksize, 6789 block_desc->block_len); 6790 else 6791 scsi_ulto3b(0, block_desc->block_len); 6792 } 6793 6794 switch (page_code) { 6795 case SMS_ALL_PAGES_PAGE: { 6796 int i, data_used; 6797 6798 data_used = header_len; 6799 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6800 struct ctl_page_index *page_index; 6801 6802 page_index = &lun->mode_pages.index[i]; 6803 6804 if ((control_dev != 0) 6805 && (page_index->page_flags & 6806 CTL_PAGE_FLAG_DISK_ONLY)) 6807 continue; 6808 6809 /* 6810 * We don't use this subpage if the user didn't 6811 * request all subpages. We already checked (above) 6812 * to make sure the user only specified a subpage 6813 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6814 */ 6815 if ((page_index->subpage != 0) 6816 && (subpage == SMS_SUBPAGE_PAGE_0)) 6817 continue; 6818 6819 /* 6820 * Call the handler, if it exists, to update the 6821 * page to the latest values. 6822 */ 6823 if (page_index->sense_handler != NULL) 6824 page_index->sense_handler(ctsio, page_index,pc); 6825 6826 memcpy(ctsio->kern_data_ptr + data_used, 6827 page_index->page_data + 6828 (page_index->page_len * pc), 6829 page_index->page_len); 6830 data_used += page_index->page_len; 6831 } 6832 break; 6833 } 6834 default: { 6835 int i, data_used; 6836 6837 data_used = header_len; 6838 6839 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6840 struct ctl_page_index *page_index; 6841 6842 page_index = &lun->mode_pages.index[i]; 6843 6844 /* Look for the right page code */ 6845 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6846 continue; 6847 6848 /* Look for the right subpage or the subpage wildcard*/ 6849 if ((page_index->subpage != subpage) 6850 && (subpage != SMS_SUBPAGE_ALL)) 6851 continue; 6852 6853 /* Make sure the page is supported for this dev type */ 6854 if ((control_dev != 0) 6855 && (page_index->page_flags & 6856 CTL_PAGE_FLAG_DISK_ONLY)) 6857 continue; 6858 6859 /* 6860 * Call the handler, if it exists, to update the 6861 * page to the latest values. 6862 */ 6863 if (page_index->sense_handler != NULL) 6864 page_index->sense_handler(ctsio, page_index,pc); 6865 6866 memcpy(ctsio->kern_data_ptr + data_used, 6867 page_index->page_data + 6868 (page_index->page_len * pc), 6869 page_index->page_len); 6870 data_used += page_index->page_len; 6871 } 6872 break; 6873 } 6874 } 6875 6876 ctsio->scsi_status = SCSI_STATUS_OK; 6877 6878 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6879 ctsio->be_move_done = ctl_config_move_done; 6880 ctl_datamove((union ctl_io *)ctsio); 6881 6882 return (CTL_RETVAL_COMPLETE); 6883 } 6884 6885 int 6886 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6887 struct ctl_page_index *page_index, 6888 int pc) 6889 { 6890 struct ctl_lun *lun; 6891 struct scsi_log_param_header *phdr; 6892 uint8_t *data; 6893 uint64_t val; 6894 6895 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6896 data = page_index->page_data; 6897 6898 if (lun->backend->lun_attr != NULL && 6899 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6900 != UINT64_MAX) { 6901 phdr = (struct scsi_log_param_header *)data; 6902 scsi_ulto2b(0x0001, phdr->param_code); 6903 phdr->param_control = SLP_LBIN | SLP_LP; 6904 phdr->param_len = 8; 6905 data = (uint8_t *)(phdr + 1); 6906 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6907 data[4] = 0x01; /* per-LUN */ 6908 data += phdr->param_len; 6909 } 6910 6911 if (lun->backend->lun_attr != NULL && 6912 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6913 != UINT64_MAX) { 6914 phdr = (struct scsi_log_param_header *)data; 6915 scsi_ulto2b(0x0002, phdr->param_code); 6916 phdr->param_control = SLP_LBIN | SLP_LP; 6917 phdr->param_len = 8; 6918 data = (uint8_t *)(phdr + 1); 6919 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6920 data[4] = 0x02; /* per-pool */ 6921 data += phdr->param_len; 6922 } 6923 6924 if (lun->backend->lun_attr != NULL && 6925 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6926 != UINT64_MAX) { 6927 phdr = (struct scsi_log_param_header *)data; 6928 scsi_ulto2b(0x00f1, phdr->param_code); 6929 phdr->param_control = SLP_LBIN | SLP_LP; 6930 phdr->param_len = 8; 6931 data = (uint8_t *)(phdr + 1); 6932 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6933 data[4] = 0x02; /* per-pool */ 6934 data += phdr->param_len; 6935 } 6936 6937 if (lun->backend->lun_attr != NULL && 6938 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6939 != UINT64_MAX) { 6940 phdr = (struct scsi_log_param_header *)data; 6941 scsi_ulto2b(0x00f2, phdr->param_code); 6942 phdr->param_control = SLP_LBIN | SLP_LP; 6943 phdr->param_len = 8; 6944 data = (uint8_t *)(phdr + 1); 6945 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6946 data[4] = 0x02; /* per-pool */ 6947 data += phdr->param_len; 6948 } 6949 6950 page_index->page_len = data - page_index->page_data; 6951 return (0); 6952 } 6953 6954 int 6955 ctl_log_sense(struct ctl_scsiio *ctsio) 6956 { 6957 struct ctl_lun *lun; 6958 int i, pc, page_code, subpage; 6959 int alloc_len, total_len; 6960 struct ctl_page_index *page_index; 6961 struct scsi_log_sense *cdb; 6962 struct scsi_log_header *header; 6963 6964 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6965 6966 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6967 cdb = (struct scsi_log_sense *)ctsio->cdb; 6968 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6969 page_code = cdb->page & SLS_PAGE_CODE; 6970 subpage = cdb->subpage; 6971 alloc_len = scsi_2btoul(cdb->length); 6972 6973 page_index = NULL; 6974 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6975 page_index = &lun->log_pages.index[i]; 6976 6977 /* Look for the right page code */ 6978 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6979 continue; 6980 6981 /* Look for the right subpage or the subpage wildcard*/ 6982 if (page_index->subpage != subpage) 6983 continue; 6984 6985 break; 6986 } 6987 if (i >= CTL_NUM_LOG_PAGES) { 6988 ctl_set_invalid_field(ctsio, 6989 /*sks_valid*/ 1, 6990 /*command*/ 1, 6991 /*field*/ 2, 6992 /*bit_valid*/ 0, 6993 /*bit*/ 0); 6994 ctl_done((union ctl_io *)ctsio); 6995 return (CTL_RETVAL_COMPLETE); 6996 } 6997 6998 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6999 7000 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7001 ctsio->kern_sg_entries = 0; 7002 ctsio->kern_data_resid = 0; 7003 ctsio->kern_rel_offset = 0; 7004 if (total_len < alloc_len) { 7005 ctsio->residual = alloc_len - total_len; 7006 ctsio->kern_data_len = total_len; 7007 ctsio->kern_total_len = total_len; 7008 } else { 7009 ctsio->residual = 0; 7010 ctsio->kern_data_len = alloc_len; 7011 ctsio->kern_total_len = alloc_len; 7012 } 7013 7014 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7015 header->page = page_index->page_code; 7016 if (page_index->subpage) { 7017 header->page |= SL_SPF; 7018 header->subpage = page_index->subpage; 7019 } 7020 scsi_ulto2b(page_index->page_len, header->datalen); 7021 7022 /* 7023 * Call the handler, if it exists, to update the 7024 * page to the latest values. 7025 */ 7026 if (page_index->sense_handler != NULL) 7027 page_index->sense_handler(ctsio, page_index, pc); 7028 7029 memcpy(header + 1, page_index->page_data, page_index->page_len); 7030 7031 ctsio->scsi_status = SCSI_STATUS_OK; 7032 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7033 ctsio->be_move_done = ctl_config_move_done; 7034 ctl_datamove((union ctl_io *)ctsio); 7035 7036 return (CTL_RETVAL_COMPLETE); 7037 } 7038 7039 int 7040 ctl_read_capacity(struct ctl_scsiio *ctsio) 7041 { 7042 struct scsi_read_capacity *cdb; 7043 struct scsi_read_capacity_data *data; 7044 struct ctl_lun *lun; 7045 uint32_t lba; 7046 7047 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7048 7049 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7050 7051 lba = scsi_4btoul(cdb->addr); 7052 if (((cdb->pmi & SRC_PMI) == 0) 7053 && (lba != 0)) { 7054 ctl_set_invalid_field(/*ctsio*/ ctsio, 7055 /*sks_valid*/ 1, 7056 /*command*/ 1, 7057 /*field*/ 2, 7058 /*bit_valid*/ 0, 7059 /*bit*/ 0); 7060 ctl_done((union ctl_io *)ctsio); 7061 return (CTL_RETVAL_COMPLETE); 7062 } 7063 7064 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7065 7066 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7067 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7068 ctsio->residual = 0; 7069 ctsio->kern_data_len = sizeof(*data); 7070 ctsio->kern_total_len = sizeof(*data); 7071 ctsio->kern_data_resid = 0; 7072 ctsio->kern_rel_offset = 0; 7073 ctsio->kern_sg_entries = 0; 7074 7075 /* 7076 * If the maximum LBA is greater than 0xfffffffe, the user must 7077 * issue a SERVICE ACTION IN (16) command, with the read capacity 7078 * serivce action set. 7079 */ 7080 if (lun->be_lun->maxlba > 0xfffffffe) 7081 scsi_ulto4b(0xffffffff, data->addr); 7082 else 7083 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7084 7085 /* 7086 * XXX KDM this may not be 512 bytes... 7087 */ 7088 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7089 7090 ctsio->scsi_status = SCSI_STATUS_OK; 7091 7092 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7093 ctsio->be_move_done = ctl_config_move_done; 7094 ctl_datamove((union ctl_io *)ctsio); 7095 7096 return (CTL_RETVAL_COMPLETE); 7097 } 7098 7099 int 7100 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7101 { 7102 struct scsi_read_capacity_16 *cdb; 7103 struct scsi_read_capacity_data_long *data; 7104 struct ctl_lun *lun; 7105 uint64_t lba; 7106 uint32_t alloc_len; 7107 7108 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7109 7110 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7111 7112 alloc_len = scsi_4btoul(cdb->alloc_len); 7113 lba = scsi_8btou64(cdb->addr); 7114 7115 if ((cdb->reladr & SRC16_PMI) 7116 && (lba != 0)) { 7117 ctl_set_invalid_field(/*ctsio*/ ctsio, 7118 /*sks_valid*/ 1, 7119 /*command*/ 1, 7120 /*field*/ 2, 7121 /*bit_valid*/ 0, 7122 /*bit*/ 0); 7123 ctl_done((union ctl_io *)ctsio); 7124 return (CTL_RETVAL_COMPLETE); 7125 } 7126 7127 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7128 7129 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7130 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7131 7132 if (sizeof(*data) < alloc_len) { 7133 ctsio->residual = alloc_len - sizeof(*data); 7134 ctsio->kern_data_len = sizeof(*data); 7135 ctsio->kern_total_len = sizeof(*data); 7136 } else { 7137 ctsio->residual = 0; 7138 ctsio->kern_data_len = alloc_len; 7139 ctsio->kern_total_len = alloc_len; 7140 } 7141 ctsio->kern_data_resid = 0; 7142 ctsio->kern_rel_offset = 0; 7143 ctsio->kern_sg_entries = 0; 7144 7145 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7146 /* XXX KDM this may not be 512 bytes... */ 7147 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7148 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7149 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7150 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7151 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7152 7153 ctsio->scsi_status = SCSI_STATUS_OK; 7154 7155 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7156 ctsio->be_move_done = ctl_config_move_done; 7157 ctl_datamove((union ctl_io *)ctsio); 7158 7159 return (CTL_RETVAL_COMPLETE); 7160 } 7161 7162 int 7163 ctl_read_defect(struct ctl_scsiio *ctsio) 7164 { 7165 struct scsi_read_defect_data_10 *ccb10; 7166 struct scsi_read_defect_data_12 *ccb12; 7167 struct scsi_read_defect_data_hdr_10 *data10; 7168 struct scsi_read_defect_data_hdr_12 *data12; 7169 uint32_t alloc_len, data_len; 7170 uint8_t format; 7171 7172 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7173 7174 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7175 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7176 format = ccb10->format; 7177 alloc_len = scsi_2btoul(ccb10->alloc_length); 7178 data_len = sizeof(*data10); 7179 } else { 7180 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7181 format = ccb12->format; 7182 alloc_len = scsi_4btoul(ccb12->alloc_length); 7183 data_len = sizeof(*data12); 7184 } 7185 if (alloc_len == 0) { 7186 ctl_set_success(ctsio); 7187 ctl_done((union ctl_io *)ctsio); 7188 return (CTL_RETVAL_COMPLETE); 7189 } 7190 7191 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7192 if (data_len < alloc_len) { 7193 ctsio->residual = alloc_len - data_len; 7194 ctsio->kern_data_len = data_len; 7195 ctsio->kern_total_len = data_len; 7196 } else { 7197 ctsio->residual = 0; 7198 ctsio->kern_data_len = alloc_len; 7199 ctsio->kern_total_len = alloc_len; 7200 } 7201 ctsio->kern_data_resid = 0; 7202 ctsio->kern_rel_offset = 0; 7203 ctsio->kern_sg_entries = 0; 7204 7205 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7206 data10 = (struct scsi_read_defect_data_hdr_10 *) 7207 ctsio->kern_data_ptr; 7208 data10->format = format; 7209 scsi_ulto2b(0, data10->length); 7210 } else { 7211 data12 = (struct scsi_read_defect_data_hdr_12 *) 7212 ctsio->kern_data_ptr; 7213 data12->format = format; 7214 scsi_ulto2b(0, data12->generation); 7215 scsi_ulto4b(0, data12->length); 7216 } 7217 7218 ctsio->scsi_status = SCSI_STATUS_OK; 7219 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7220 ctsio->be_move_done = ctl_config_move_done; 7221 ctl_datamove((union ctl_io *)ctsio); 7222 return (CTL_RETVAL_COMPLETE); 7223 } 7224 7225 int 7226 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7227 { 7228 struct scsi_maintenance_in *cdb; 7229 int retval; 7230 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 7231 int num_target_port_groups, num_target_ports; 7232 struct ctl_lun *lun; 7233 struct ctl_softc *softc; 7234 struct ctl_port *port; 7235 struct scsi_target_group_data *rtg_ptr; 7236 struct scsi_target_group_data_extended *rtg_ext_ptr; 7237 struct scsi_target_port_group_descriptor *tpg_desc; 7238 7239 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7240 7241 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7242 softc = control_softc; 7243 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7244 7245 retval = CTL_RETVAL_COMPLETE; 7246 7247 switch (cdb->byte2 & STG_PDF_MASK) { 7248 case STG_PDF_LENGTH: 7249 ext = 0; 7250 break; 7251 case STG_PDF_EXTENDED: 7252 ext = 1; 7253 break; 7254 default: 7255 ctl_set_invalid_field(/*ctsio*/ ctsio, 7256 /*sks_valid*/ 1, 7257 /*command*/ 1, 7258 /*field*/ 2, 7259 /*bit_valid*/ 1, 7260 /*bit*/ 5); 7261 ctl_done((union ctl_io *)ctsio); 7262 return(retval); 7263 } 7264 7265 if (softc->is_single) 7266 num_target_port_groups = 1; 7267 else 7268 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7269 num_target_ports = 0; 7270 mtx_lock(&softc->ctl_lock); 7271 STAILQ_FOREACH(port, &softc->port_list, links) { 7272 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7273 continue; 7274 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7275 continue; 7276 num_target_ports++; 7277 } 7278 mtx_unlock(&softc->ctl_lock); 7279 7280 if (ext) 7281 total_len = sizeof(struct scsi_target_group_data_extended); 7282 else 7283 total_len = sizeof(struct scsi_target_group_data); 7284 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7285 num_target_port_groups + 7286 sizeof(struct scsi_target_port_descriptor) * 7287 num_target_ports * num_target_port_groups; 7288 7289 alloc_len = scsi_4btoul(cdb->length); 7290 7291 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7292 7293 ctsio->kern_sg_entries = 0; 7294 7295 if (total_len < alloc_len) { 7296 ctsio->residual = alloc_len - total_len; 7297 ctsio->kern_data_len = total_len; 7298 ctsio->kern_total_len = total_len; 7299 } else { 7300 ctsio->residual = 0; 7301 ctsio->kern_data_len = alloc_len; 7302 ctsio->kern_total_len = alloc_len; 7303 } 7304 ctsio->kern_data_resid = 0; 7305 ctsio->kern_rel_offset = 0; 7306 7307 if (ext) { 7308 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7309 ctsio->kern_data_ptr; 7310 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7311 rtg_ext_ptr->format_type = 0x10; 7312 rtg_ext_ptr->implicit_transition_time = 0; 7313 tpg_desc = &rtg_ext_ptr->groups[0]; 7314 } else { 7315 rtg_ptr = (struct scsi_target_group_data *) 7316 ctsio->kern_data_ptr; 7317 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7318 tpg_desc = &rtg_ptr->groups[0]; 7319 } 7320 7321 mtx_lock(&softc->ctl_lock); 7322 pg = softc->port_offset / CTL_MAX_PORTS; 7323 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 7324 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 7325 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7326 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7327 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 7328 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7329 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7330 } else { 7331 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7332 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7333 } 7334 } else { 7335 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7336 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7337 } 7338 for (g = 0; g < num_target_port_groups; g++) { 7339 tpg_desc->pref_state = (g == pg) ? gs : os; 7340 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 7341 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7342 tpg_desc->status = TPG_IMPLICIT; 7343 pc = 0; 7344 STAILQ_FOREACH(port, &softc->port_list, links) { 7345 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7346 continue; 7347 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7348 CTL_MAX_LUNS) 7349 continue; 7350 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7351 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7352 relative_target_port_identifier); 7353 pc++; 7354 } 7355 tpg_desc->target_port_count = pc; 7356 tpg_desc = (struct scsi_target_port_group_descriptor *) 7357 &tpg_desc->descriptors[pc]; 7358 } 7359 mtx_unlock(&softc->ctl_lock); 7360 7361 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7362 ctsio->be_move_done = ctl_config_move_done; 7363 7364 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7365 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7366 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7367 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7368 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7369 7370 ctl_datamove((union ctl_io *)ctsio); 7371 return(retval); 7372 } 7373 7374 int 7375 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7376 { 7377 struct ctl_lun *lun; 7378 struct scsi_report_supported_opcodes *cdb; 7379 const struct ctl_cmd_entry *entry, *sentry; 7380 struct scsi_report_supported_opcodes_all *all; 7381 struct scsi_report_supported_opcodes_descr *descr; 7382 struct scsi_report_supported_opcodes_one *one; 7383 int retval; 7384 int alloc_len, total_len; 7385 int opcode, service_action, i, j, num; 7386 7387 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7388 7389 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7390 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7391 7392 retval = CTL_RETVAL_COMPLETE; 7393 7394 opcode = cdb->requested_opcode; 7395 service_action = scsi_2btoul(cdb->requested_service_action); 7396 switch (cdb->options & RSO_OPTIONS_MASK) { 7397 case RSO_OPTIONS_ALL: 7398 num = 0; 7399 for (i = 0; i < 256; i++) { 7400 entry = &ctl_cmd_table[i]; 7401 if (entry->flags & CTL_CMD_FLAG_SA5) { 7402 for (j = 0; j < 32; j++) { 7403 sentry = &((const struct ctl_cmd_entry *) 7404 entry->execute)[j]; 7405 if (ctl_cmd_applicable( 7406 lun->be_lun->lun_type, sentry)) 7407 num++; 7408 } 7409 } else { 7410 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7411 entry)) 7412 num++; 7413 } 7414 } 7415 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7416 num * sizeof(struct scsi_report_supported_opcodes_descr); 7417 break; 7418 case RSO_OPTIONS_OC: 7419 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7420 ctl_set_invalid_field(/*ctsio*/ ctsio, 7421 /*sks_valid*/ 1, 7422 /*command*/ 1, 7423 /*field*/ 2, 7424 /*bit_valid*/ 1, 7425 /*bit*/ 2); 7426 ctl_done((union ctl_io *)ctsio); 7427 return (CTL_RETVAL_COMPLETE); 7428 } 7429 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7430 break; 7431 case RSO_OPTIONS_OC_SA: 7432 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7433 service_action >= 32) { 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 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7444 break; 7445 default: 7446 ctl_set_invalid_field(/*ctsio*/ ctsio, 7447 /*sks_valid*/ 1, 7448 /*command*/ 1, 7449 /*field*/ 2, 7450 /*bit_valid*/ 1, 7451 /*bit*/ 2); 7452 ctl_done((union ctl_io *)ctsio); 7453 return (CTL_RETVAL_COMPLETE); 7454 } 7455 7456 alloc_len = scsi_4btoul(cdb->length); 7457 7458 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7459 7460 ctsio->kern_sg_entries = 0; 7461 7462 if (total_len < alloc_len) { 7463 ctsio->residual = alloc_len - total_len; 7464 ctsio->kern_data_len = total_len; 7465 ctsio->kern_total_len = total_len; 7466 } else { 7467 ctsio->residual = 0; 7468 ctsio->kern_data_len = alloc_len; 7469 ctsio->kern_total_len = alloc_len; 7470 } 7471 ctsio->kern_data_resid = 0; 7472 ctsio->kern_rel_offset = 0; 7473 7474 switch (cdb->options & RSO_OPTIONS_MASK) { 7475 case RSO_OPTIONS_ALL: 7476 all = (struct scsi_report_supported_opcodes_all *) 7477 ctsio->kern_data_ptr; 7478 num = 0; 7479 for (i = 0; i < 256; i++) { 7480 entry = &ctl_cmd_table[i]; 7481 if (entry->flags & CTL_CMD_FLAG_SA5) { 7482 for (j = 0; j < 32; j++) { 7483 sentry = &((const struct ctl_cmd_entry *) 7484 entry->execute)[j]; 7485 if (!ctl_cmd_applicable( 7486 lun->be_lun->lun_type, sentry)) 7487 continue; 7488 descr = &all->descr[num++]; 7489 descr->opcode = i; 7490 scsi_ulto2b(j, descr->service_action); 7491 descr->flags = RSO_SERVACTV; 7492 scsi_ulto2b(sentry->length, 7493 descr->cdb_length); 7494 } 7495 } else { 7496 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7497 entry)) 7498 continue; 7499 descr = &all->descr[num++]; 7500 descr->opcode = i; 7501 scsi_ulto2b(0, descr->service_action); 7502 descr->flags = 0; 7503 scsi_ulto2b(entry->length, descr->cdb_length); 7504 } 7505 } 7506 scsi_ulto4b( 7507 num * sizeof(struct scsi_report_supported_opcodes_descr), 7508 all->length); 7509 break; 7510 case RSO_OPTIONS_OC: 7511 one = (struct scsi_report_supported_opcodes_one *) 7512 ctsio->kern_data_ptr; 7513 entry = &ctl_cmd_table[opcode]; 7514 goto fill_one; 7515 case RSO_OPTIONS_OC_SA: 7516 one = (struct scsi_report_supported_opcodes_one *) 7517 ctsio->kern_data_ptr; 7518 entry = &ctl_cmd_table[opcode]; 7519 entry = &((const struct ctl_cmd_entry *) 7520 entry->execute)[service_action]; 7521 fill_one: 7522 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7523 one->support = 3; 7524 scsi_ulto2b(entry->length, one->cdb_length); 7525 one->cdb_usage[0] = opcode; 7526 memcpy(&one->cdb_usage[1], entry->usage, 7527 entry->length - 1); 7528 } else 7529 one->support = 1; 7530 break; 7531 } 7532 7533 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7534 ctsio->be_move_done = ctl_config_move_done; 7535 7536 ctl_datamove((union ctl_io *)ctsio); 7537 return(retval); 7538 } 7539 7540 int 7541 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7542 { 7543 struct scsi_report_supported_tmf *cdb; 7544 struct scsi_report_supported_tmf_data *data; 7545 int retval; 7546 int alloc_len, total_len; 7547 7548 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7549 7550 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7551 7552 retval = CTL_RETVAL_COMPLETE; 7553 7554 total_len = sizeof(struct scsi_report_supported_tmf_data); 7555 alloc_len = scsi_4btoul(cdb->length); 7556 7557 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7558 7559 ctsio->kern_sg_entries = 0; 7560 7561 if (total_len < alloc_len) { 7562 ctsio->residual = alloc_len - total_len; 7563 ctsio->kern_data_len = total_len; 7564 ctsio->kern_total_len = total_len; 7565 } else { 7566 ctsio->residual = 0; 7567 ctsio->kern_data_len = alloc_len; 7568 ctsio->kern_total_len = alloc_len; 7569 } 7570 ctsio->kern_data_resid = 0; 7571 ctsio->kern_rel_offset = 0; 7572 7573 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7574 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7575 data->byte2 |= RST_ITNRS; 7576 7577 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7578 ctsio->be_move_done = ctl_config_move_done; 7579 7580 ctl_datamove((union ctl_io *)ctsio); 7581 return (retval); 7582 } 7583 7584 int 7585 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7586 { 7587 struct scsi_report_timestamp *cdb; 7588 struct scsi_report_timestamp_data *data; 7589 struct timeval tv; 7590 int64_t timestamp; 7591 int retval; 7592 int alloc_len, total_len; 7593 7594 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7595 7596 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7597 7598 retval = CTL_RETVAL_COMPLETE; 7599 7600 total_len = sizeof(struct scsi_report_timestamp_data); 7601 alloc_len = scsi_4btoul(cdb->length); 7602 7603 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7604 7605 ctsio->kern_sg_entries = 0; 7606 7607 if (total_len < alloc_len) { 7608 ctsio->residual = alloc_len - total_len; 7609 ctsio->kern_data_len = total_len; 7610 ctsio->kern_total_len = total_len; 7611 } else { 7612 ctsio->residual = 0; 7613 ctsio->kern_data_len = alloc_len; 7614 ctsio->kern_total_len = alloc_len; 7615 } 7616 ctsio->kern_data_resid = 0; 7617 ctsio->kern_rel_offset = 0; 7618 7619 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7620 scsi_ulto2b(sizeof(*data) - 2, data->length); 7621 data->origin = RTS_ORIG_OUTSIDE; 7622 getmicrotime(&tv); 7623 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7624 scsi_ulto4b(timestamp >> 16, data->timestamp); 7625 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7626 7627 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7628 ctsio->be_move_done = ctl_config_move_done; 7629 7630 ctl_datamove((union ctl_io *)ctsio); 7631 return (retval); 7632 } 7633 7634 int 7635 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7636 { 7637 struct scsi_per_res_in *cdb; 7638 int alloc_len, total_len = 0; 7639 /* struct scsi_per_res_in_rsrv in_data; */ 7640 struct ctl_lun *lun; 7641 struct ctl_softc *softc; 7642 7643 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7644 7645 softc = control_softc; 7646 7647 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7648 7649 alloc_len = scsi_2btoul(cdb->length); 7650 7651 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7652 7653 retry: 7654 mtx_lock(&lun->lun_lock); 7655 switch (cdb->action) { 7656 case SPRI_RK: /* read keys */ 7657 total_len = sizeof(struct scsi_per_res_in_keys) + 7658 lun->pr_key_count * 7659 sizeof(struct scsi_per_res_key); 7660 break; 7661 case SPRI_RR: /* read reservation */ 7662 if (lun->flags & CTL_LUN_PR_RESERVED) 7663 total_len = sizeof(struct scsi_per_res_in_rsrv); 7664 else 7665 total_len = sizeof(struct scsi_per_res_in_header); 7666 break; 7667 case SPRI_RC: /* report capabilities */ 7668 total_len = sizeof(struct scsi_per_res_cap); 7669 break; 7670 case SPRI_RS: /* read full status */ 7671 total_len = sizeof(struct scsi_per_res_in_header) + 7672 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7673 lun->pr_key_count; 7674 break; 7675 default: 7676 panic("Invalid PR type %x", cdb->action); 7677 } 7678 mtx_unlock(&lun->lun_lock); 7679 7680 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7681 7682 if (total_len < alloc_len) { 7683 ctsio->residual = alloc_len - total_len; 7684 ctsio->kern_data_len = total_len; 7685 ctsio->kern_total_len = total_len; 7686 } else { 7687 ctsio->residual = 0; 7688 ctsio->kern_data_len = alloc_len; 7689 ctsio->kern_total_len = alloc_len; 7690 } 7691 7692 ctsio->kern_data_resid = 0; 7693 ctsio->kern_rel_offset = 0; 7694 ctsio->kern_sg_entries = 0; 7695 7696 mtx_lock(&lun->lun_lock); 7697 switch (cdb->action) { 7698 case SPRI_RK: { // read keys 7699 struct scsi_per_res_in_keys *res_keys; 7700 int i, key_count; 7701 7702 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7703 7704 /* 7705 * We had to drop the lock to allocate our buffer, which 7706 * leaves time for someone to come in with another 7707 * persistent reservation. (That is unlikely, though, 7708 * since this should be the only persistent reservation 7709 * command active right now.) 7710 */ 7711 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7712 (lun->pr_key_count * 7713 sizeof(struct scsi_per_res_key)))){ 7714 mtx_unlock(&lun->lun_lock); 7715 free(ctsio->kern_data_ptr, M_CTL); 7716 printf("%s: reservation length changed, retrying\n", 7717 __func__); 7718 goto retry; 7719 } 7720 7721 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7722 7723 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7724 lun->pr_key_count, res_keys->header.length); 7725 7726 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7727 if (lun->pr_keys[i] == 0) 7728 continue; 7729 7730 /* 7731 * We used lun->pr_key_count to calculate the 7732 * size to allocate. If it turns out the number of 7733 * initiators with the registered flag set is 7734 * larger than that (i.e. they haven't been kept in 7735 * sync), we've got a problem. 7736 */ 7737 if (key_count >= lun->pr_key_count) { 7738 #ifdef NEEDTOPORT 7739 csevent_log(CSC_CTL | CSC_SHELF_SW | 7740 CTL_PR_ERROR, 7741 csevent_LogType_Fault, 7742 csevent_AlertLevel_Yellow, 7743 csevent_FRU_ShelfController, 7744 csevent_FRU_Firmware, 7745 csevent_FRU_Unknown, 7746 "registered keys %d >= key " 7747 "count %d", key_count, 7748 lun->pr_key_count); 7749 #endif 7750 key_count++; 7751 continue; 7752 } 7753 scsi_u64to8b(lun->pr_keys[i], 7754 res_keys->keys[key_count].key); 7755 key_count++; 7756 } 7757 break; 7758 } 7759 case SPRI_RR: { // read reservation 7760 struct scsi_per_res_in_rsrv *res; 7761 int tmp_len, header_only; 7762 7763 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7764 7765 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7766 7767 if (lun->flags & CTL_LUN_PR_RESERVED) 7768 { 7769 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7770 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7771 res->header.length); 7772 header_only = 0; 7773 } else { 7774 tmp_len = sizeof(struct scsi_per_res_in_header); 7775 scsi_ulto4b(0, res->header.length); 7776 header_only = 1; 7777 } 7778 7779 /* 7780 * We had to drop the lock to allocate our buffer, which 7781 * leaves time for someone to come in with another 7782 * persistent reservation. (That is unlikely, though, 7783 * since this should be the only persistent reservation 7784 * command active right now.) 7785 */ 7786 if (tmp_len != total_len) { 7787 mtx_unlock(&lun->lun_lock); 7788 free(ctsio->kern_data_ptr, M_CTL); 7789 printf("%s: reservation status changed, retrying\n", 7790 __func__); 7791 goto retry; 7792 } 7793 7794 /* 7795 * No reservation held, so we're done. 7796 */ 7797 if (header_only != 0) 7798 break; 7799 7800 /* 7801 * If the registration is an All Registrants type, the key 7802 * is 0, since it doesn't really matter. 7803 */ 7804 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7805 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx], 7806 res->data.reservation); 7807 } 7808 res->data.scopetype = lun->res_type; 7809 break; 7810 } 7811 case SPRI_RC: //report capabilities 7812 { 7813 struct scsi_per_res_cap *res_cap; 7814 uint16_t type_mask; 7815 7816 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7817 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7818 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7819 type_mask = SPRI_TM_WR_EX_AR | 7820 SPRI_TM_EX_AC_RO | 7821 SPRI_TM_WR_EX_RO | 7822 SPRI_TM_EX_AC | 7823 SPRI_TM_WR_EX | 7824 SPRI_TM_EX_AC_AR; 7825 scsi_ulto2b(type_mask, res_cap->type_mask); 7826 break; 7827 } 7828 case SPRI_RS: { // read full status 7829 struct scsi_per_res_in_full *res_status; 7830 struct scsi_per_res_in_full_desc *res_desc; 7831 struct ctl_port *port; 7832 int i, len; 7833 7834 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7835 7836 /* 7837 * We had to drop the lock to allocate our buffer, which 7838 * leaves time for someone to come in with another 7839 * persistent reservation. (That is unlikely, though, 7840 * since this should be the only persistent reservation 7841 * command active right now.) 7842 */ 7843 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7844 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7845 lun->pr_key_count)){ 7846 mtx_unlock(&lun->lun_lock); 7847 free(ctsio->kern_data_ptr, M_CTL); 7848 printf("%s: reservation length changed, retrying\n", 7849 __func__); 7850 goto retry; 7851 } 7852 7853 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7854 7855 res_desc = &res_status->desc[0]; 7856 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7857 if (lun->pr_keys[i] == 0) 7858 continue; 7859 7860 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key); 7861 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7862 (lun->pr_res_idx == i || 7863 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7864 res_desc->flags = SPRI_FULL_R_HOLDER; 7865 res_desc->scopetype = lun->res_type; 7866 } 7867 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7868 res_desc->rel_trgt_port_id); 7869 len = 0; 7870 port = softc->ctl_ports[ 7871 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7872 if (port != NULL) 7873 len = ctl_create_iid(port, 7874 i % CTL_MAX_INIT_PER_PORT, 7875 res_desc->transport_id); 7876 scsi_ulto4b(len, res_desc->additional_length); 7877 res_desc = (struct scsi_per_res_in_full_desc *) 7878 &res_desc->transport_id[len]; 7879 } 7880 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7881 res_status->header.length); 7882 break; 7883 } 7884 default: 7885 /* 7886 * This is a bug, because we just checked for this above, 7887 * and should have returned an error. 7888 */ 7889 panic("Invalid PR type %x", cdb->action); 7890 break; /* NOTREACHED */ 7891 } 7892 mtx_unlock(&lun->lun_lock); 7893 7894 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7895 ctsio->be_move_done = ctl_config_move_done; 7896 7897 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7898 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7899 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7900 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7901 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7902 7903 ctl_datamove((union ctl_io *)ctsio); 7904 7905 return (CTL_RETVAL_COMPLETE); 7906 } 7907 7908 /* 7909 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7910 * it should return. 7911 */ 7912 static int 7913 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7914 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7915 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7916 struct scsi_per_res_out_parms* param) 7917 { 7918 union ctl_ha_msg persis_io; 7919 int retval, i; 7920 int isc_retval; 7921 7922 retval = 0; 7923 7924 mtx_lock(&lun->lun_lock); 7925 if (sa_res_key == 0) { 7926 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7927 /* validate scope and type */ 7928 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7929 SPR_LU_SCOPE) { 7930 mtx_unlock(&lun->lun_lock); 7931 ctl_set_invalid_field(/*ctsio*/ ctsio, 7932 /*sks_valid*/ 1, 7933 /*command*/ 1, 7934 /*field*/ 2, 7935 /*bit_valid*/ 1, 7936 /*bit*/ 4); 7937 ctl_done((union ctl_io *)ctsio); 7938 return (1); 7939 } 7940 7941 if (type>8 || type==2 || type==4 || type==0) { 7942 mtx_unlock(&lun->lun_lock); 7943 ctl_set_invalid_field(/*ctsio*/ ctsio, 7944 /*sks_valid*/ 1, 7945 /*command*/ 1, 7946 /*field*/ 2, 7947 /*bit_valid*/ 1, 7948 /*bit*/ 0); 7949 ctl_done((union ctl_io *)ctsio); 7950 return (1); 7951 } 7952 7953 /* 7954 * Unregister everybody else and build UA for 7955 * them 7956 */ 7957 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7958 if (i == residx || lun->pr_keys[i] == 0) 7959 continue; 7960 7961 if (!persis_offset 7962 && i <CTL_MAX_INITIATORS) 7963 lun->pending_ua[i] |= 7964 CTL_UA_REG_PREEMPT; 7965 else if (persis_offset 7966 && i >= persis_offset) 7967 lun->pending_ua[i-persis_offset] |= 7968 CTL_UA_REG_PREEMPT; 7969 lun->pr_keys[i] = 0; 7970 } 7971 lun->pr_key_count = 1; 7972 lun->res_type = type; 7973 if (lun->res_type != SPR_TYPE_WR_EX_AR 7974 && lun->res_type != SPR_TYPE_EX_AC_AR) 7975 lun->pr_res_idx = residx; 7976 7977 /* send msg to other side */ 7978 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7979 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7980 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7981 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7982 persis_io.pr.pr_info.res_type = type; 7983 memcpy(persis_io.pr.pr_info.sa_res_key, 7984 param->serv_act_res_key, 7985 sizeof(param->serv_act_res_key)); 7986 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7987 &persis_io, sizeof(persis_io), 0)) > 7988 CTL_HA_STATUS_SUCCESS) { 7989 printf("CTL:Persis Out error returned " 7990 "from ctl_ha_msg_send %d\n", 7991 isc_retval); 7992 } 7993 } else { 7994 /* not all registrants */ 7995 mtx_unlock(&lun->lun_lock); 7996 free(ctsio->kern_data_ptr, M_CTL); 7997 ctl_set_invalid_field(ctsio, 7998 /*sks_valid*/ 1, 7999 /*command*/ 0, 8000 /*field*/ 8, 8001 /*bit_valid*/ 0, 8002 /*bit*/ 0); 8003 ctl_done((union ctl_io *)ctsio); 8004 return (1); 8005 } 8006 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8007 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8008 int found = 0; 8009 8010 if (res_key == sa_res_key) { 8011 /* special case */ 8012 /* 8013 * The spec implies this is not good but doesn't 8014 * say what to do. There are two choices either 8015 * generate a res conflict or check condition 8016 * with illegal field in parameter data. Since 8017 * that is what is done when the sa_res_key is 8018 * zero I'll take that approach since this has 8019 * to do with the sa_res_key. 8020 */ 8021 mtx_unlock(&lun->lun_lock); 8022 free(ctsio->kern_data_ptr, M_CTL); 8023 ctl_set_invalid_field(ctsio, 8024 /*sks_valid*/ 1, 8025 /*command*/ 0, 8026 /*field*/ 8, 8027 /*bit_valid*/ 0, 8028 /*bit*/ 0); 8029 ctl_done((union ctl_io *)ctsio); 8030 return (1); 8031 } 8032 8033 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8034 if (lun->pr_keys[i] != sa_res_key) 8035 continue; 8036 8037 found = 1; 8038 lun->pr_keys[i] = 0; 8039 lun->pr_key_count--; 8040 8041 if (!persis_offset && i < CTL_MAX_INITIATORS) 8042 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8043 else if (persis_offset && i >= persis_offset) 8044 lun->pending_ua[i-persis_offset] |= 8045 CTL_UA_REG_PREEMPT; 8046 } 8047 if (!found) { 8048 mtx_unlock(&lun->lun_lock); 8049 free(ctsio->kern_data_ptr, M_CTL); 8050 ctl_set_reservation_conflict(ctsio); 8051 ctl_done((union ctl_io *)ctsio); 8052 return (CTL_RETVAL_COMPLETE); 8053 } 8054 /* send msg to other side */ 8055 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8056 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8057 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8058 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8059 persis_io.pr.pr_info.res_type = type; 8060 memcpy(persis_io.pr.pr_info.sa_res_key, 8061 param->serv_act_res_key, 8062 sizeof(param->serv_act_res_key)); 8063 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8064 &persis_io, sizeof(persis_io), 0)) > 8065 CTL_HA_STATUS_SUCCESS) { 8066 printf("CTL:Persis Out error returned from " 8067 "ctl_ha_msg_send %d\n", isc_retval); 8068 } 8069 } else { 8070 /* Reserved but not all registrants */ 8071 /* sa_res_key is res holder */ 8072 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) { 8073 /* validate scope and type */ 8074 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8075 SPR_LU_SCOPE) { 8076 mtx_unlock(&lun->lun_lock); 8077 ctl_set_invalid_field(/*ctsio*/ ctsio, 8078 /*sks_valid*/ 1, 8079 /*command*/ 1, 8080 /*field*/ 2, 8081 /*bit_valid*/ 1, 8082 /*bit*/ 4); 8083 ctl_done((union ctl_io *)ctsio); 8084 return (1); 8085 } 8086 8087 if (type>8 || type==2 || type==4 || type==0) { 8088 mtx_unlock(&lun->lun_lock); 8089 ctl_set_invalid_field(/*ctsio*/ ctsio, 8090 /*sks_valid*/ 1, 8091 /*command*/ 1, 8092 /*field*/ 2, 8093 /*bit_valid*/ 1, 8094 /*bit*/ 0); 8095 ctl_done((union ctl_io *)ctsio); 8096 return (1); 8097 } 8098 8099 /* 8100 * Do the following: 8101 * if sa_res_key != res_key remove all 8102 * registrants w/sa_res_key and generate UA 8103 * for these registrants(Registrations 8104 * Preempted) if it wasn't an exclusive 8105 * reservation generate UA(Reservations 8106 * Preempted) for all other registered nexuses 8107 * if the type has changed. Establish the new 8108 * reservation and holder. If res_key and 8109 * sa_res_key are the same do the above 8110 * except don't unregister the res holder. 8111 */ 8112 8113 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8114 if (i == residx || lun->pr_keys[i] == 0) 8115 continue; 8116 8117 if (sa_res_key == lun->pr_keys[i]) { 8118 lun->pr_keys[i] = 0; 8119 lun->pr_key_count--; 8120 8121 if (!persis_offset 8122 && i < CTL_MAX_INITIATORS) 8123 lun->pending_ua[i] |= 8124 CTL_UA_REG_PREEMPT; 8125 else if (persis_offset 8126 && i >= persis_offset) 8127 lun->pending_ua[i-persis_offset] |= 8128 CTL_UA_REG_PREEMPT; 8129 } else if (type != lun->res_type 8130 && (lun->res_type == SPR_TYPE_WR_EX_RO 8131 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8132 if (!persis_offset 8133 && i < CTL_MAX_INITIATORS) 8134 lun->pending_ua[i] |= 8135 CTL_UA_RES_RELEASE; 8136 else if (persis_offset 8137 && i >= persis_offset) 8138 lun->pending_ua[ 8139 i-persis_offset] |= 8140 CTL_UA_RES_RELEASE; 8141 } 8142 } 8143 lun->res_type = type; 8144 if (lun->res_type != SPR_TYPE_WR_EX_AR 8145 && lun->res_type != SPR_TYPE_EX_AC_AR) 8146 lun->pr_res_idx = residx; 8147 else 8148 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8149 8150 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8151 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8152 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8153 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8154 persis_io.pr.pr_info.res_type = type; 8155 memcpy(persis_io.pr.pr_info.sa_res_key, 8156 param->serv_act_res_key, 8157 sizeof(param->serv_act_res_key)); 8158 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8159 &persis_io, sizeof(persis_io), 0)) > 8160 CTL_HA_STATUS_SUCCESS) { 8161 printf("CTL:Persis Out error returned " 8162 "from ctl_ha_msg_send %d\n", 8163 isc_retval); 8164 } 8165 } else { 8166 /* 8167 * sa_res_key is not the res holder just 8168 * remove registrants 8169 */ 8170 int found=0; 8171 8172 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8173 if (sa_res_key != lun->pr_keys[i]) 8174 continue; 8175 8176 found = 1; 8177 lun->pr_keys[i] = 0; 8178 lun->pr_key_count--; 8179 8180 if (!persis_offset 8181 && i < CTL_MAX_INITIATORS) 8182 lun->pending_ua[i] |= 8183 CTL_UA_REG_PREEMPT; 8184 else if (persis_offset 8185 && i >= persis_offset) 8186 lun->pending_ua[i-persis_offset] |= 8187 CTL_UA_REG_PREEMPT; 8188 } 8189 8190 if (!found) { 8191 mtx_unlock(&lun->lun_lock); 8192 free(ctsio->kern_data_ptr, M_CTL); 8193 ctl_set_reservation_conflict(ctsio); 8194 ctl_done((union ctl_io *)ctsio); 8195 return (1); 8196 } 8197 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8198 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8199 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8200 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8201 persis_io.pr.pr_info.res_type = type; 8202 memcpy(persis_io.pr.pr_info.sa_res_key, 8203 param->serv_act_res_key, 8204 sizeof(param->serv_act_res_key)); 8205 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8206 &persis_io, sizeof(persis_io), 0)) > 8207 CTL_HA_STATUS_SUCCESS) { 8208 printf("CTL:Persis Out error returned " 8209 "from ctl_ha_msg_send %d\n", 8210 isc_retval); 8211 } 8212 } 8213 } 8214 8215 lun->PRGeneration++; 8216 mtx_unlock(&lun->lun_lock); 8217 8218 return (retval); 8219 } 8220 8221 static void 8222 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8223 { 8224 uint64_t sa_res_key; 8225 int i; 8226 8227 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8228 8229 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8230 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8231 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) { 8232 if (sa_res_key == 0) { 8233 /* 8234 * Unregister everybody else and build UA for 8235 * them 8236 */ 8237 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8238 if (i == msg->pr.pr_info.residx || 8239 lun->pr_keys[i] == 0) 8240 continue; 8241 8242 if (!persis_offset 8243 && i < CTL_MAX_INITIATORS) 8244 lun->pending_ua[i] |= 8245 CTL_UA_REG_PREEMPT; 8246 else if (persis_offset && i >= persis_offset) 8247 lun->pending_ua[i - persis_offset] |= 8248 CTL_UA_REG_PREEMPT; 8249 lun->pr_keys[i] = 0; 8250 } 8251 8252 lun->pr_key_count = 1; 8253 lun->res_type = msg->pr.pr_info.res_type; 8254 if (lun->res_type != SPR_TYPE_WR_EX_AR 8255 && lun->res_type != SPR_TYPE_EX_AC_AR) 8256 lun->pr_res_idx = msg->pr.pr_info.residx; 8257 } else { 8258 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8259 if (sa_res_key == lun->pr_keys[i]) 8260 continue; 8261 8262 lun->pr_keys[i] = 0; 8263 lun->pr_key_count--; 8264 8265 if (!persis_offset 8266 && i < persis_offset) 8267 lun->pending_ua[i] |= 8268 CTL_UA_REG_PREEMPT; 8269 else if (persis_offset 8270 && i >= persis_offset) 8271 lun->pending_ua[i - persis_offset] |= 8272 CTL_UA_REG_PREEMPT; 8273 } 8274 } 8275 } else { 8276 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8277 if (i == msg->pr.pr_info.residx || 8278 lun->pr_keys[i] == 0) 8279 continue; 8280 8281 if (sa_res_key == lun->pr_keys[i]) { 8282 lun->pr_keys[i] = 0; 8283 lun->pr_key_count--; 8284 if (!persis_offset 8285 && i < CTL_MAX_INITIATORS) 8286 lun->pending_ua[i] |= 8287 CTL_UA_REG_PREEMPT; 8288 else if (persis_offset 8289 && i >= persis_offset) 8290 lun->pending_ua[i - persis_offset] |= 8291 CTL_UA_REG_PREEMPT; 8292 } else if (msg->pr.pr_info.res_type != lun->res_type 8293 && (lun->res_type == SPR_TYPE_WR_EX_RO 8294 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8295 if (!persis_offset 8296 && i < persis_offset) 8297 lun->pending_ua[i] |= 8298 CTL_UA_RES_RELEASE; 8299 else if (persis_offset 8300 && i >= persis_offset) 8301 lun->pending_ua[i - persis_offset] |= 8302 CTL_UA_RES_RELEASE; 8303 } 8304 } 8305 lun->res_type = msg->pr.pr_info.res_type; 8306 if (lun->res_type != SPR_TYPE_WR_EX_AR 8307 && lun->res_type != SPR_TYPE_EX_AC_AR) 8308 lun->pr_res_idx = msg->pr.pr_info.residx; 8309 else 8310 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8311 } 8312 lun->PRGeneration++; 8313 8314 } 8315 8316 8317 int 8318 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8319 { 8320 int retval; 8321 int isc_retval; 8322 u_int32_t param_len; 8323 struct scsi_per_res_out *cdb; 8324 struct ctl_lun *lun; 8325 struct scsi_per_res_out_parms* param; 8326 struct ctl_softc *softc; 8327 uint32_t residx; 8328 uint64_t res_key, sa_res_key; 8329 uint8_t type; 8330 union ctl_ha_msg persis_io; 8331 int i; 8332 8333 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8334 8335 retval = CTL_RETVAL_COMPLETE; 8336 8337 softc = control_softc; 8338 8339 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8340 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8341 8342 /* 8343 * We only support whole-LUN scope. The scope & type are ignored for 8344 * register, register and ignore existing key and clear. 8345 * We sometimes ignore scope and type on preempts too!! 8346 * Verify reservation type here as well. 8347 */ 8348 type = cdb->scope_type & SPR_TYPE_MASK; 8349 if ((cdb->action == SPRO_RESERVE) 8350 || (cdb->action == SPRO_RELEASE)) { 8351 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8352 ctl_set_invalid_field(/*ctsio*/ ctsio, 8353 /*sks_valid*/ 1, 8354 /*command*/ 1, 8355 /*field*/ 2, 8356 /*bit_valid*/ 1, 8357 /*bit*/ 4); 8358 ctl_done((union ctl_io *)ctsio); 8359 return (CTL_RETVAL_COMPLETE); 8360 } 8361 8362 if (type>8 || type==2 || type==4 || type==0) { 8363 ctl_set_invalid_field(/*ctsio*/ ctsio, 8364 /*sks_valid*/ 1, 8365 /*command*/ 1, 8366 /*field*/ 2, 8367 /*bit_valid*/ 1, 8368 /*bit*/ 0); 8369 ctl_done((union ctl_io *)ctsio); 8370 return (CTL_RETVAL_COMPLETE); 8371 } 8372 } 8373 8374 param_len = scsi_4btoul(cdb->length); 8375 8376 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8377 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8378 ctsio->kern_data_len = param_len; 8379 ctsio->kern_total_len = param_len; 8380 ctsio->kern_data_resid = 0; 8381 ctsio->kern_rel_offset = 0; 8382 ctsio->kern_sg_entries = 0; 8383 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8384 ctsio->be_move_done = ctl_config_move_done; 8385 ctl_datamove((union ctl_io *)ctsio); 8386 8387 return (CTL_RETVAL_COMPLETE); 8388 } 8389 8390 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8391 8392 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8393 res_key = scsi_8btou64(param->res_key.key); 8394 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8395 8396 /* 8397 * Validate the reservation key here except for SPRO_REG_IGNO 8398 * This must be done for all other service actions 8399 */ 8400 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8401 mtx_lock(&lun->lun_lock); 8402 if (lun->pr_keys[residx] != 0) { 8403 if (res_key != lun->pr_keys[residx]) { 8404 /* 8405 * The current key passed in doesn't match 8406 * the one the initiator previously 8407 * registered. 8408 */ 8409 mtx_unlock(&lun->lun_lock); 8410 free(ctsio->kern_data_ptr, M_CTL); 8411 ctl_set_reservation_conflict(ctsio); 8412 ctl_done((union ctl_io *)ctsio); 8413 return (CTL_RETVAL_COMPLETE); 8414 } 8415 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8416 /* 8417 * We are not registered 8418 */ 8419 mtx_unlock(&lun->lun_lock); 8420 free(ctsio->kern_data_ptr, M_CTL); 8421 ctl_set_reservation_conflict(ctsio); 8422 ctl_done((union ctl_io *)ctsio); 8423 return (CTL_RETVAL_COMPLETE); 8424 } else if (res_key != 0) { 8425 /* 8426 * We are not registered and trying to register but 8427 * the register key isn't zero. 8428 */ 8429 mtx_unlock(&lun->lun_lock); 8430 free(ctsio->kern_data_ptr, M_CTL); 8431 ctl_set_reservation_conflict(ctsio); 8432 ctl_done((union ctl_io *)ctsio); 8433 return (CTL_RETVAL_COMPLETE); 8434 } 8435 mtx_unlock(&lun->lun_lock); 8436 } 8437 8438 switch (cdb->action & SPRO_ACTION_MASK) { 8439 case SPRO_REGISTER: 8440 case SPRO_REG_IGNO: { 8441 8442 #if 0 8443 printf("Registration received\n"); 8444 #endif 8445 8446 /* 8447 * We don't support any of these options, as we report in 8448 * the read capabilities request (see 8449 * ctl_persistent_reserve_in(), above). 8450 */ 8451 if ((param->flags & SPR_SPEC_I_PT) 8452 || (param->flags & SPR_ALL_TG_PT) 8453 || (param->flags & SPR_APTPL)) { 8454 int bit_ptr; 8455 8456 if (param->flags & SPR_APTPL) 8457 bit_ptr = 0; 8458 else if (param->flags & SPR_ALL_TG_PT) 8459 bit_ptr = 2; 8460 else /* SPR_SPEC_I_PT */ 8461 bit_ptr = 3; 8462 8463 free(ctsio->kern_data_ptr, M_CTL); 8464 ctl_set_invalid_field(ctsio, 8465 /*sks_valid*/ 1, 8466 /*command*/ 0, 8467 /*field*/ 20, 8468 /*bit_valid*/ 1, 8469 /*bit*/ bit_ptr); 8470 ctl_done((union ctl_io *)ctsio); 8471 return (CTL_RETVAL_COMPLETE); 8472 } 8473 8474 mtx_lock(&lun->lun_lock); 8475 8476 /* 8477 * The initiator wants to clear the 8478 * key/unregister. 8479 */ 8480 if (sa_res_key == 0) { 8481 if ((res_key == 0 8482 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8483 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8484 && lun->pr_keys[residx] == 0)) { 8485 mtx_unlock(&lun->lun_lock); 8486 goto done; 8487 } 8488 8489 lun->pr_keys[residx] = 0; 8490 lun->pr_key_count--; 8491 8492 if (residx == lun->pr_res_idx) { 8493 lun->flags &= ~CTL_LUN_PR_RESERVED; 8494 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8495 8496 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8497 || lun->res_type == SPR_TYPE_EX_AC_RO) 8498 && lun->pr_key_count) { 8499 /* 8500 * If the reservation is a registrants 8501 * only type we need to generate a UA 8502 * for other registered inits. The 8503 * sense code should be RESERVATIONS 8504 * RELEASED 8505 */ 8506 8507 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8508 if (lun->pr_keys[ 8509 i + persis_offset] == 0) 8510 continue; 8511 lun->pending_ua[i] |= 8512 CTL_UA_RES_RELEASE; 8513 } 8514 } 8515 lun->res_type = 0; 8516 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8517 if (lun->pr_key_count==0) { 8518 lun->flags &= ~CTL_LUN_PR_RESERVED; 8519 lun->res_type = 0; 8520 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8521 } 8522 } 8523 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8524 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8525 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8526 persis_io.pr.pr_info.residx = residx; 8527 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8528 &persis_io, sizeof(persis_io), 0 )) > 8529 CTL_HA_STATUS_SUCCESS) { 8530 printf("CTL:Persis Out error returned from " 8531 "ctl_ha_msg_send %d\n", isc_retval); 8532 } 8533 } else /* sa_res_key != 0 */ { 8534 8535 /* 8536 * If we aren't registered currently then increment 8537 * the key count and set the registered flag. 8538 */ 8539 if (lun->pr_keys[residx] == 0) 8540 lun->pr_key_count++; 8541 lun->pr_keys[residx] = sa_res_key; 8542 8543 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8544 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8545 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8546 persis_io.pr.pr_info.residx = residx; 8547 memcpy(persis_io.pr.pr_info.sa_res_key, 8548 param->serv_act_res_key, 8549 sizeof(param->serv_act_res_key)); 8550 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8551 &persis_io, sizeof(persis_io), 0)) > 8552 CTL_HA_STATUS_SUCCESS) { 8553 printf("CTL:Persis Out error returned from " 8554 "ctl_ha_msg_send %d\n", isc_retval); 8555 } 8556 } 8557 lun->PRGeneration++; 8558 mtx_unlock(&lun->lun_lock); 8559 8560 break; 8561 } 8562 case SPRO_RESERVE: 8563 #if 0 8564 printf("Reserve executed type %d\n", type); 8565 #endif 8566 mtx_lock(&lun->lun_lock); 8567 if (lun->flags & CTL_LUN_PR_RESERVED) { 8568 /* 8569 * if this isn't the reservation holder and it's 8570 * not a "all registrants" type or if the type is 8571 * different then we have a conflict 8572 */ 8573 if ((lun->pr_res_idx != residx 8574 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8575 || lun->res_type != type) { 8576 mtx_unlock(&lun->lun_lock); 8577 free(ctsio->kern_data_ptr, M_CTL); 8578 ctl_set_reservation_conflict(ctsio); 8579 ctl_done((union ctl_io *)ctsio); 8580 return (CTL_RETVAL_COMPLETE); 8581 } 8582 mtx_unlock(&lun->lun_lock); 8583 } else /* create a reservation */ { 8584 /* 8585 * If it's not an "all registrants" type record 8586 * reservation holder 8587 */ 8588 if (type != SPR_TYPE_WR_EX_AR 8589 && type != SPR_TYPE_EX_AC_AR) 8590 lun->pr_res_idx = residx; /* Res holder */ 8591 else 8592 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8593 8594 lun->flags |= CTL_LUN_PR_RESERVED; 8595 lun->res_type = type; 8596 8597 mtx_unlock(&lun->lun_lock); 8598 8599 /* send msg to other side */ 8600 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8601 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8602 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8603 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8604 persis_io.pr.pr_info.res_type = type; 8605 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8606 &persis_io, sizeof(persis_io), 0)) > 8607 CTL_HA_STATUS_SUCCESS) { 8608 printf("CTL:Persis Out error returned from " 8609 "ctl_ha_msg_send %d\n", isc_retval); 8610 } 8611 } 8612 break; 8613 8614 case SPRO_RELEASE: 8615 mtx_lock(&lun->lun_lock); 8616 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8617 /* No reservation exists return good status */ 8618 mtx_unlock(&lun->lun_lock); 8619 goto done; 8620 } 8621 /* 8622 * Is this nexus a reservation holder? 8623 */ 8624 if (lun->pr_res_idx != residx 8625 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8626 /* 8627 * not a res holder return good status but 8628 * do nothing 8629 */ 8630 mtx_unlock(&lun->lun_lock); 8631 goto done; 8632 } 8633 8634 if (lun->res_type != type) { 8635 mtx_unlock(&lun->lun_lock); 8636 free(ctsio->kern_data_ptr, M_CTL); 8637 ctl_set_illegal_pr_release(ctsio); 8638 ctl_done((union ctl_io *)ctsio); 8639 return (CTL_RETVAL_COMPLETE); 8640 } 8641 8642 /* okay to release */ 8643 lun->flags &= ~CTL_LUN_PR_RESERVED; 8644 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8645 lun->res_type = 0; 8646 8647 /* 8648 * if this isn't an exclusive access 8649 * res generate UA for all other 8650 * registrants. 8651 */ 8652 if (type != SPR_TYPE_EX_AC 8653 && type != SPR_TYPE_WR_EX) { 8654 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8655 if (i == residx || 8656 lun->pr_keys[i + persis_offset] == 0) 8657 continue; 8658 lun->pending_ua[i] |= CTL_UA_RES_RELEASE; 8659 } 8660 } 8661 mtx_unlock(&lun->lun_lock); 8662 /* Send msg to other side */ 8663 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8664 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8665 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8666 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8667 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8668 printf("CTL:Persis Out error returned from " 8669 "ctl_ha_msg_send %d\n", isc_retval); 8670 } 8671 break; 8672 8673 case SPRO_CLEAR: 8674 /* send msg to other side */ 8675 8676 mtx_lock(&lun->lun_lock); 8677 lun->flags &= ~CTL_LUN_PR_RESERVED; 8678 lun->res_type = 0; 8679 lun->pr_key_count = 0; 8680 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8681 8682 lun->pr_keys[residx] = 0; 8683 8684 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8685 if (lun->pr_keys[i] != 0) { 8686 if (!persis_offset && i < CTL_MAX_INITIATORS) 8687 lun->pending_ua[i] |= 8688 CTL_UA_RES_PREEMPT; 8689 else if (persis_offset && i >= persis_offset) 8690 lun->pending_ua[i-persis_offset] |= 8691 CTL_UA_RES_PREEMPT; 8692 8693 lun->pr_keys[i] = 0; 8694 } 8695 lun->PRGeneration++; 8696 mtx_unlock(&lun->lun_lock); 8697 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8698 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8699 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8700 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8701 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8702 printf("CTL:Persis Out error returned from " 8703 "ctl_ha_msg_send %d\n", isc_retval); 8704 } 8705 break; 8706 8707 case SPRO_PREEMPT: 8708 case SPRO_PRE_ABO: { 8709 int nretval; 8710 8711 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8712 residx, ctsio, cdb, param); 8713 if (nretval != 0) 8714 return (CTL_RETVAL_COMPLETE); 8715 break; 8716 } 8717 default: 8718 panic("Invalid PR type %x", cdb->action); 8719 } 8720 8721 done: 8722 free(ctsio->kern_data_ptr, M_CTL); 8723 ctl_set_success(ctsio); 8724 ctl_done((union ctl_io *)ctsio); 8725 8726 return (retval); 8727 } 8728 8729 /* 8730 * This routine is for handling a message from the other SC pertaining to 8731 * persistent reserve out. All the error checking will have been done 8732 * so only perorming the action need be done here to keep the two 8733 * in sync. 8734 */ 8735 static void 8736 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8737 { 8738 struct ctl_lun *lun; 8739 struct ctl_softc *softc; 8740 int i; 8741 uint32_t targ_lun; 8742 8743 softc = control_softc; 8744 8745 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8746 lun = softc->ctl_luns[targ_lun]; 8747 mtx_lock(&lun->lun_lock); 8748 switch(msg->pr.pr_info.action) { 8749 case CTL_PR_REG_KEY: 8750 if (lun->pr_keys[msg->pr.pr_info.residx] == 0) 8751 lun->pr_key_count++; 8752 lun->pr_keys[msg->pr.pr_info.residx] = 8753 scsi_8btou64(msg->pr.pr_info.sa_res_key); 8754 lun->PRGeneration++; 8755 break; 8756 8757 case CTL_PR_UNREG_KEY: 8758 lun->pr_keys[msg->pr.pr_info.residx] = 0; 8759 lun->pr_key_count--; 8760 8761 /* XXX Need to see if the reservation has been released */ 8762 /* if so do we need to generate UA? */ 8763 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8764 lun->flags &= ~CTL_LUN_PR_RESERVED; 8765 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8766 8767 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8768 || lun->res_type == SPR_TYPE_EX_AC_RO) 8769 && lun->pr_key_count) { 8770 /* 8771 * If the reservation is a registrants 8772 * only type we need to generate a UA 8773 * for other registered inits. The 8774 * sense code should be RESERVATIONS 8775 * RELEASED 8776 */ 8777 8778 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8779 if (lun->pr_keys[i+ 8780 persis_offset] == 0) 8781 continue; 8782 8783 lun->pending_ua[i] |= 8784 CTL_UA_RES_RELEASE; 8785 } 8786 } 8787 lun->res_type = 0; 8788 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8789 if (lun->pr_key_count==0) { 8790 lun->flags &= ~CTL_LUN_PR_RESERVED; 8791 lun->res_type = 0; 8792 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8793 } 8794 } 8795 lun->PRGeneration++; 8796 break; 8797 8798 case CTL_PR_RESERVE: 8799 lun->flags |= CTL_LUN_PR_RESERVED; 8800 lun->res_type = msg->pr.pr_info.res_type; 8801 lun->pr_res_idx = msg->pr.pr_info.residx; 8802 8803 break; 8804 8805 case CTL_PR_RELEASE: 8806 /* 8807 * if this isn't an exclusive access res generate UA for all 8808 * other registrants. 8809 */ 8810 if (lun->res_type != SPR_TYPE_EX_AC 8811 && lun->res_type != SPR_TYPE_WR_EX) { 8812 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8813 if (lun->pr_keys[i+persis_offset] != 0) 8814 lun->pending_ua[i] |= 8815 CTL_UA_RES_RELEASE; 8816 } 8817 8818 lun->flags &= ~CTL_LUN_PR_RESERVED; 8819 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8820 lun->res_type = 0; 8821 break; 8822 8823 case CTL_PR_PREEMPT: 8824 ctl_pro_preempt_other(lun, msg); 8825 break; 8826 case CTL_PR_CLEAR: 8827 lun->flags &= ~CTL_LUN_PR_RESERVED; 8828 lun->res_type = 0; 8829 lun->pr_key_count = 0; 8830 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8831 8832 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8833 if (lun->pr_keys[i] == 0) 8834 continue; 8835 if (!persis_offset 8836 && i < CTL_MAX_INITIATORS) 8837 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 8838 else if (persis_offset 8839 && i >= persis_offset) 8840 lun->pending_ua[i-persis_offset] |= 8841 CTL_UA_RES_PREEMPT; 8842 lun->pr_keys[i] = 0; 8843 } 8844 lun->PRGeneration++; 8845 break; 8846 } 8847 8848 mtx_unlock(&lun->lun_lock); 8849 } 8850 8851 int 8852 ctl_read_write(struct ctl_scsiio *ctsio) 8853 { 8854 struct ctl_lun *lun; 8855 struct ctl_lba_len_flags *lbalen; 8856 uint64_t lba; 8857 uint32_t num_blocks; 8858 int flags, retval; 8859 int isread; 8860 8861 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8862 8863 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8864 8865 flags = 0; 8866 retval = CTL_RETVAL_COMPLETE; 8867 8868 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8869 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8870 switch (ctsio->cdb[0]) { 8871 case READ_6: 8872 case WRITE_6: { 8873 struct scsi_rw_6 *cdb; 8874 8875 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8876 8877 lba = scsi_3btoul(cdb->addr); 8878 /* only 5 bits are valid in the most significant address byte */ 8879 lba &= 0x1fffff; 8880 num_blocks = cdb->length; 8881 /* 8882 * This is correct according to SBC-2. 8883 */ 8884 if (num_blocks == 0) 8885 num_blocks = 256; 8886 break; 8887 } 8888 case READ_10: 8889 case WRITE_10: { 8890 struct scsi_rw_10 *cdb; 8891 8892 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8893 if (cdb->byte2 & SRW10_FUA) 8894 flags |= CTL_LLF_FUA; 8895 if (cdb->byte2 & SRW10_DPO) 8896 flags |= CTL_LLF_DPO; 8897 lba = scsi_4btoul(cdb->addr); 8898 num_blocks = scsi_2btoul(cdb->length); 8899 break; 8900 } 8901 case WRITE_VERIFY_10: { 8902 struct scsi_write_verify_10 *cdb; 8903 8904 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8905 flags |= CTL_LLF_FUA; 8906 if (cdb->byte2 & SWV_DPO) 8907 flags |= CTL_LLF_DPO; 8908 lba = scsi_4btoul(cdb->addr); 8909 num_blocks = scsi_2btoul(cdb->length); 8910 break; 8911 } 8912 case READ_12: 8913 case WRITE_12: { 8914 struct scsi_rw_12 *cdb; 8915 8916 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8917 if (cdb->byte2 & SRW12_FUA) 8918 flags |= CTL_LLF_FUA; 8919 if (cdb->byte2 & SRW12_DPO) 8920 flags |= CTL_LLF_DPO; 8921 lba = scsi_4btoul(cdb->addr); 8922 num_blocks = scsi_4btoul(cdb->length); 8923 break; 8924 } 8925 case WRITE_VERIFY_12: { 8926 struct scsi_write_verify_12 *cdb; 8927 8928 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8929 flags |= CTL_LLF_FUA; 8930 if (cdb->byte2 & SWV_DPO) 8931 flags |= CTL_LLF_DPO; 8932 lba = scsi_4btoul(cdb->addr); 8933 num_blocks = scsi_4btoul(cdb->length); 8934 break; 8935 } 8936 case READ_16: 8937 case WRITE_16: { 8938 struct scsi_rw_16 *cdb; 8939 8940 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8941 if (cdb->byte2 & SRW12_FUA) 8942 flags |= CTL_LLF_FUA; 8943 if (cdb->byte2 & SRW12_DPO) 8944 flags |= CTL_LLF_DPO; 8945 lba = scsi_8btou64(cdb->addr); 8946 num_blocks = scsi_4btoul(cdb->length); 8947 break; 8948 } 8949 case WRITE_ATOMIC_16: { 8950 struct scsi_rw_16 *cdb; 8951 8952 if (lun->be_lun->atomicblock == 0) { 8953 ctl_set_invalid_opcode(ctsio); 8954 ctl_done((union ctl_io *)ctsio); 8955 return (CTL_RETVAL_COMPLETE); 8956 } 8957 8958 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8959 if (cdb->byte2 & SRW12_FUA) 8960 flags |= CTL_LLF_FUA; 8961 if (cdb->byte2 & SRW12_DPO) 8962 flags |= CTL_LLF_DPO; 8963 lba = scsi_8btou64(cdb->addr); 8964 num_blocks = scsi_4btoul(cdb->length); 8965 if (num_blocks > lun->be_lun->atomicblock) { 8966 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8967 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8968 /*bit*/ 0); 8969 ctl_done((union ctl_io *)ctsio); 8970 return (CTL_RETVAL_COMPLETE); 8971 } 8972 break; 8973 } 8974 case WRITE_VERIFY_16: { 8975 struct scsi_write_verify_16 *cdb; 8976 8977 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8978 flags |= CTL_LLF_FUA; 8979 if (cdb->byte2 & SWV_DPO) 8980 flags |= CTL_LLF_DPO; 8981 lba = scsi_8btou64(cdb->addr); 8982 num_blocks = scsi_4btoul(cdb->length); 8983 break; 8984 } 8985 default: 8986 /* 8987 * We got a command we don't support. This shouldn't 8988 * happen, commands should be filtered out above us. 8989 */ 8990 ctl_set_invalid_opcode(ctsio); 8991 ctl_done((union ctl_io *)ctsio); 8992 8993 return (CTL_RETVAL_COMPLETE); 8994 break; /* NOTREACHED */ 8995 } 8996 8997 /* 8998 * The first check is to make sure we're in bounds, the second 8999 * check is to catch wrap-around problems. If the lba + num blocks 9000 * is less than the lba, then we've wrapped around and the block 9001 * range is invalid anyway. 9002 */ 9003 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9004 || ((lba + num_blocks) < lba)) { 9005 ctl_set_lba_out_of_range(ctsio); 9006 ctl_done((union ctl_io *)ctsio); 9007 return (CTL_RETVAL_COMPLETE); 9008 } 9009 9010 /* 9011 * According to SBC-3, a transfer length of 0 is not an error. 9012 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9013 * translates to 256 blocks for those commands. 9014 */ 9015 if (num_blocks == 0) { 9016 ctl_set_success(ctsio); 9017 ctl_done((union ctl_io *)ctsio); 9018 return (CTL_RETVAL_COMPLETE); 9019 } 9020 9021 /* Set FUA and/or DPO if caches are disabled. */ 9022 if (isread) { 9023 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9024 SCP_RCD) != 0) 9025 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9026 } else { 9027 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9028 SCP_WCE) == 0) 9029 flags |= CTL_LLF_FUA; 9030 } 9031 9032 lbalen = (struct ctl_lba_len_flags *) 9033 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9034 lbalen->lba = lba; 9035 lbalen->len = num_blocks; 9036 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9037 9038 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9039 ctsio->kern_rel_offset = 0; 9040 9041 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9042 9043 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9044 9045 return (retval); 9046 } 9047 9048 static int 9049 ctl_cnw_cont(union ctl_io *io) 9050 { 9051 struct ctl_scsiio *ctsio; 9052 struct ctl_lun *lun; 9053 struct ctl_lba_len_flags *lbalen; 9054 int retval; 9055 9056 ctsio = &io->scsiio; 9057 ctsio->io_hdr.status = CTL_STATUS_NONE; 9058 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9059 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9060 lbalen = (struct ctl_lba_len_flags *) 9061 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9062 lbalen->flags &= ~CTL_LLF_COMPARE; 9063 lbalen->flags |= CTL_LLF_WRITE; 9064 9065 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9066 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9067 return (retval); 9068 } 9069 9070 int 9071 ctl_cnw(struct ctl_scsiio *ctsio) 9072 { 9073 struct ctl_lun *lun; 9074 struct ctl_lba_len_flags *lbalen; 9075 uint64_t lba; 9076 uint32_t num_blocks; 9077 int flags, retval; 9078 9079 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9080 9081 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9082 9083 flags = 0; 9084 retval = CTL_RETVAL_COMPLETE; 9085 9086 switch (ctsio->cdb[0]) { 9087 case COMPARE_AND_WRITE: { 9088 struct scsi_compare_and_write *cdb; 9089 9090 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9091 if (cdb->byte2 & SRW10_FUA) 9092 flags |= CTL_LLF_FUA; 9093 if (cdb->byte2 & SRW10_DPO) 9094 flags |= CTL_LLF_DPO; 9095 lba = scsi_8btou64(cdb->addr); 9096 num_blocks = cdb->length; 9097 break; 9098 } 9099 default: 9100 /* 9101 * We got a command we don't support. This shouldn't 9102 * happen, commands should be filtered out above us. 9103 */ 9104 ctl_set_invalid_opcode(ctsio); 9105 ctl_done((union ctl_io *)ctsio); 9106 9107 return (CTL_RETVAL_COMPLETE); 9108 break; /* NOTREACHED */ 9109 } 9110 9111 /* 9112 * The first check is to make sure we're in bounds, the second 9113 * check is to catch wrap-around problems. If the lba + num blocks 9114 * is less than the lba, then we've wrapped around and the block 9115 * range is invalid anyway. 9116 */ 9117 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9118 || ((lba + num_blocks) < lba)) { 9119 ctl_set_lba_out_of_range(ctsio); 9120 ctl_done((union ctl_io *)ctsio); 9121 return (CTL_RETVAL_COMPLETE); 9122 } 9123 9124 /* 9125 * According to SBC-3, a transfer length of 0 is not an error. 9126 */ 9127 if (num_blocks == 0) { 9128 ctl_set_success(ctsio); 9129 ctl_done((union ctl_io *)ctsio); 9130 return (CTL_RETVAL_COMPLETE); 9131 } 9132 9133 /* Set FUA if write cache is disabled. */ 9134 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9135 SCP_WCE) == 0) 9136 flags |= CTL_LLF_FUA; 9137 9138 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9139 ctsio->kern_rel_offset = 0; 9140 9141 /* 9142 * Set the IO_CONT flag, so that if this I/O gets passed to 9143 * ctl_data_submit_done(), it'll get passed back to 9144 * ctl_ctl_cnw_cont() for further processing. 9145 */ 9146 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9147 ctsio->io_cont = ctl_cnw_cont; 9148 9149 lbalen = (struct ctl_lba_len_flags *) 9150 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9151 lbalen->lba = lba; 9152 lbalen->len = num_blocks; 9153 lbalen->flags = CTL_LLF_COMPARE | flags; 9154 9155 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9156 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9157 return (retval); 9158 } 9159 9160 int 9161 ctl_verify(struct ctl_scsiio *ctsio) 9162 { 9163 struct ctl_lun *lun; 9164 struct ctl_lba_len_flags *lbalen; 9165 uint64_t lba; 9166 uint32_t num_blocks; 9167 int bytchk, flags; 9168 int retval; 9169 9170 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9171 9172 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9173 9174 bytchk = 0; 9175 flags = CTL_LLF_FUA; 9176 retval = CTL_RETVAL_COMPLETE; 9177 9178 switch (ctsio->cdb[0]) { 9179 case VERIFY_10: { 9180 struct scsi_verify_10 *cdb; 9181 9182 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9183 if (cdb->byte2 & SVFY_BYTCHK) 9184 bytchk = 1; 9185 if (cdb->byte2 & SVFY_DPO) 9186 flags |= CTL_LLF_DPO; 9187 lba = scsi_4btoul(cdb->addr); 9188 num_blocks = scsi_2btoul(cdb->length); 9189 break; 9190 } 9191 case VERIFY_12: { 9192 struct scsi_verify_12 *cdb; 9193 9194 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9195 if (cdb->byte2 & SVFY_BYTCHK) 9196 bytchk = 1; 9197 if (cdb->byte2 & SVFY_DPO) 9198 flags |= CTL_LLF_DPO; 9199 lba = scsi_4btoul(cdb->addr); 9200 num_blocks = scsi_4btoul(cdb->length); 9201 break; 9202 } 9203 case VERIFY_16: { 9204 struct scsi_rw_16 *cdb; 9205 9206 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9207 if (cdb->byte2 & SVFY_BYTCHK) 9208 bytchk = 1; 9209 if (cdb->byte2 & SVFY_DPO) 9210 flags |= CTL_LLF_DPO; 9211 lba = scsi_8btou64(cdb->addr); 9212 num_blocks = scsi_4btoul(cdb->length); 9213 break; 9214 } 9215 default: 9216 /* 9217 * We got a command we don't support. This shouldn't 9218 * happen, commands should be filtered out above us. 9219 */ 9220 ctl_set_invalid_opcode(ctsio); 9221 ctl_done((union ctl_io *)ctsio); 9222 return (CTL_RETVAL_COMPLETE); 9223 } 9224 9225 /* 9226 * The first check is to make sure we're in bounds, the second 9227 * check is to catch wrap-around problems. If the lba + num blocks 9228 * is less than the lba, then we've wrapped around and the block 9229 * range is invalid anyway. 9230 */ 9231 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9232 || ((lba + num_blocks) < lba)) { 9233 ctl_set_lba_out_of_range(ctsio); 9234 ctl_done((union ctl_io *)ctsio); 9235 return (CTL_RETVAL_COMPLETE); 9236 } 9237 9238 /* 9239 * According to SBC-3, a transfer length of 0 is not an error. 9240 */ 9241 if (num_blocks == 0) { 9242 ctl_set_success(ctsio); 9243 ctl_done((union ctl_io *)ctsio); 9244 return (CTL_RETVAL_COMPLETE); 9245 } 9246 9247 lbalen = (struct ctl_lba_len_flags *) 9248 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9249 lbalen->lba = lba; 9250 lbalen->len = num_blocks; 9251 if (bytchk) { 9252 lbalen->flags = CTL_LLF_COMPARE | flags; 9253 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9254 } else { 9255 lbalen->flags = CTL_LLF_VERIFY | flags; 9256 ctsio->kern_total_len = 0; 9257 } 9258 ctsio->kern_rel_offset = 0; 9259 9260 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9261 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9262 return (retval); 9263 } 9264 9265 int 9266 ctl_report_luns(struct ctl_scsiio *ctsio) 9267 { 9268 struct scsi_report_luns *cdb; 9269 struct scsi_report_luns_data *lun_data; 9270 struct ctl_lun *lun, *request_lun; 9271 int num_luns, retval; 9272 uint32_t alloc_len, lun_datalen; 9273 int num_filled, well_known; 9274 uint32_t initidx, targ_lun_id, lun_id; 9275 9276 retval = CTL_RETVAL_COMPLETE; 9277 well_known = 0; 9278 9279 cdb = (struct scsi_report_luns *)ctsio->cdb; 9280 9281 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9282 9283 mtx_lock(&control_softc->ctl_lock); 9284 num_luns = control_softc->num_luns; 9285 mtx_unlock(&control_softc->ctl_lock); 9286 9287 switch (cdb->select_report) { 9288 case RPL_REPORT_DEFAULT: 9289 case RPL_REPORT_ALL: 9290 break; 9291 case RPL_REPORT_WELLKNOWN: 9292 well_known = 1; 9293 num_luns = 0; 9294 break; 9295 default: 9296 ctl_set_invalid_field(ctsio, 9297 /*sks_valid*/ 1, 9298 /*command*/ 1, 9299 /*field*/ 2, 9300 /*bit_valid*/ 0, 9301 /*bit*/ 0); 9302 ctl_done((union ctl_io *)ctsio); 9303 return (retval); 9304 break; /* NOTREACHED */ 9305 } 9306 9307 alloc_len = scsi_4btoul(cdb->length); 9308 /* 9309 * The initiator has to allocate at least 16 bytes for this request, 9310 * so he can at least get the header and the first LUN. Otherwise 9311 * we reject the request (per SPC-3 rev 14, section 6.21). 9312 */ 9313 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9314 sizeof(struct scsi_report_luns_lundata))) { 9315 ctl_set_invalid_field(ctsio, 9316 /*sks_valid*/ 1, 9317 /*command*/ 1, 9318 /*field*/ 6, 9319 /*bit_valid*/ 0, 9320 /*bit*/ 0); 9321 ctl_done((union ctl_io *)ctsio); 9322 return (retval); 9323 } 9324 9325 request_lun = (struct ctl_lun *) 9326 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9327 9328 lun_datalen = sizeof(*lun_data) + 9329 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9330 9331 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9332 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9333 ctsio->kern_sg_entries = 0; 9334 9335 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9336 9337 mtx_lock(&control_softc->ctl_lock); 9338 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9339 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9340 if (lun_id >= CTL_MAX_LUNS) 9341 continue; 9342 lun = control_softc->ctl_luns[lun_id]; 9343 if (lun == NULL) 9344 continue; 9345 9346 if (targ_lun_id <= 0xff) { 9347 /* 9348 * Peripheral addressing method, bus number 0. 9349 */ 9350 lun_data->luns[num_filled].lundata[0] = 9351 RPL_LUNDATA_ATYP_PERIPH; 9352 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9353 num_filled++; 9354 } else if (targ_lun_id <= 0x3fff) { 9355 /* 9356 * Flat addressing method. 9357 */ 9358 lun_data->luns[num_filled].lundata[0] = 9359 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9360 lun_data->luns[num_filled].lundata[1] = 9361 (targ_lun_id & 0xff); 9362 num_filled++; 9363 } else if (targ_lun_id <= 0xffffff) { 9364 /* 9365 * Extended flat addressing method. 9366 */ 9367 lun_data->luns[num_filled].lundata[0] = 9368 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9369 scsi_ulto3b(targ_lun_id, 9370 &lun_data->luns[num_filled].lundata[1]); 9371 num_filled++; 9372 } else { 9373 printf("ctl_report_luns: bogus LUN number %jd, " 9374 "skipping\n", (intmax_t)targ_lun_id); 9375 } 9376 /* 9377 * According to SPC-3, rev 14 section 6.21: 9378 * 9379 * "The execution of a REPORT LUNS command to any valid and 9380 * installed logical unit shall clear the REPORTED LUNS DATA 9381 * HAS CHANGED unit attention condition for all logical 9382 * units of that target with respect to the requesting 9383 * initiator. A valid and installed logical unit is one 9384 * having a PERIPHERAL QUALIFIER of 000b in the standard 9385 * INQUIRY data (see 6.4.2)." 9386 * 9387 * If request_lun is NULL, the LUN this report luns command 9388 * was issued to is either disabled or doesn't exist. In that 9389 * case, we shouldn't clear any pending lun change unit 9390 * attention. 9391 */ 9392 if (request_lun != NULL) { 9393 mtx_lock(&lun->lun_lock); 9394 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9395 mtx_unlock(&lun->lun_lock); 9396 } 9397 } 9398 mtx_unlock(&control_softc->ctl_lock); 9399 9400 /* 9401 * It's quite possible that we've returned fewer LUNs than we allocated 9402 * space for. Trim it. 9403 */ 9404 lun_datalen = sizeof(*lun_data) + 9405 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9406 9407 if (lun_datalen < alloc_len) { 9408 ctsio->residual = alloc_len - lun_datalen; 9409 ctsio->kern_data_len = lun_datalen; 9410 ctsio->kern_total_len = lun_datalen; 9411 } else { 9412 ctsio->residual = 0; 9413 ctsio->kern_data_len = alloc_len; 9414 ctsio->kern_total_len = alloc_len; 9415 } 9416 ctsio->kern_data_resid = 0; 9417 ctsio->kern_rel_offset = 0; 9418 ctsio->kern_sg_entries = 0; 9419 9420 /* 9421 * We set this to the actual data length, regardless of how much 9422 * space we actually have to return results. If the user looks at 9423 * this value, he'll know whether or not he allocated enough space 9424 * and reissue the command if necessary. We don't support well 9425 * known logical units, so if the user asks for that, return none. 9426 */ 9427 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9428 9429 /* 9430 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9431 * this request. 9432 */ 9433 ctsio->scsi_status = SCSI_STATUS_OK; 9434 9435 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9436 ctsio->be_move_done = ctl_config_move_done; 9437 ctl_datamove((union ctl_io *)ctsio); 9438 9439 return (retval); 9440 } 9441 9442 int 9443 ctl_request_sense(struct ctl_scsiio *ctsio) 9444 { 9445 struct scsi_request_sense *cdb; 9446 struct scsi_sense_data *sense_ptr; 9447 struct ctl_lun *lun; 9448 uint32_t initidx; 9449 int have_error; 9450 scsi_sense_data_type sense_format; 9451 9452 cdb = (struct scsi_request_sense *)ctsio->cdb; 9453 9454 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9455 9456 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9457 9458 /* 9459 * Determine which sense format the user wants. 9460 */ 9461 if (cdb->byte2 & SRS_DESC) 9462 sense_format = SSD_TYPE_DESC; 9463 else 9464 sense_format = SSD_TYPE_FIXED; 9465 9466 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9467 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9468 ctsio->kern_sg_entries = 0; 9469 9470 /* 9471 * struct scsi_sense_data, which is currently set to 256 bytes, is 9472 * larger than the largest allowed value for the length field in the 9473 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9474 */ 9475 ctsio->residual = 0; 9476 ctsio->kern_data_len = cdb->length; 9477 ctsio->kern_total_len = cdb->length; 9478 9479 ctsio->kern_data_resid = 0; 9480 ctsio->kern_rel_offset = 0; 9481 ctsio->kern_sg_entries = 0; 9482 9483 /* 9484 * If we don't have a LUN, we don't have any pending sense. 9485 */ 9486 if (lun == NULL) 9487 goto no_sense; 9488 9489 have_error = 0; 9490 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9491 /* 9492 * Check for pending sense, and then for pending unit attentions. 9493 * Pending sense gets returned first, then pending unit attentions. 9494 */ 9495 mtx_lock(&lun->lun_lock); 9496 #ifdef CTL_WITH_CA 9497 if (ctl_is_set(lun->have_ca, initidx)) { 9498 scsi_sense_data_type stored_format; 9499 9500 /* 9501 * Check to see which sense format was used for the stored 9502 * sense data. 9503 */ 9504 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9505 9506 /* 9507 * If the user requested a different sense format than the 9508 * one we stored, then we need to convert it to the other 9509 * format. If we're going from descriptor to fixed format 9510 * sense data, we may lose things in translation, depending 9511 * on what options were used. 9512 * 9513 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9514 * for some reason we'll just copy it out as-is. 9515 */ 9516 if ((stored_format == SSD_TYPE_FIXED) 9517 && (sense_format == SSD_TYPE_DESC)) 9518 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9519 &lun->pending_sense[initidx], 9520 (struct scsi_sense_data_desc *)sense_ptr); 9521 else if ((stored_format == SSD_TYPE_DESC) 9522 && (sense_format == SSD_TYPE_FIXED)) 9523 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9524 &lun->pending_sense[initidx], 9525 (struct scsi_sense_data_fixed *)sense_ptr); 9526 else 9527 memcpy(sense_ptr, &lun->pending_sense[initidx], 9528 ctl_min(sizeof(*sense_ptr), 9529 sizeof(lun->pending_sense[initidx]))); 9530 9531 ctl_clear_mask(lun->have_ca, initidx); 9532 have_error = 1; 9533 } else 9534 #endif 9535 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9536 ctl_ua_type ua_type; 9537 9538 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9539 sense_ptr, sense_format); 9540 if (ua_type != CTL_UA_NONE) 9541 have_error = 1; 9542 } 9543 mtx_unlock(&lun->lun_lock); 9544 9545 /* 9546 * We already have a pending error, return it. 9547 */ 9548 if (have_error != 0) { 9549 /* 9550 * We report the SCSI status as OK, since the status of the 9551 * request sense command itself is OK. 9552 */ 9553 ctsio->scsi_status = SCSI_STATUS_OK; 9554 9555 /* 9556 * We report 0 for the sense length, because we aren't doing 9557 * autosense in this case. We're reporting sense as 9558 * parameter data. 9559 */ 9560 ctsio->sense_len = 0; 9561 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9562 ctsio->be_move_done = ctl_config_move_done; 9563 ctl_datamove((union ctl_io *)ctsio); 9564 9565 return (CTL_RETVAL_COMPLETE); 9566 } 9567 9568 no_sense: 9569 9570 /* 9571 * No sense information to report, so we report that everything is 9572 * okay. 9573 */ 9574 ctl_set_sense_data(sense_ptr, 9575 lun, 9576 sense_format, 9577 /*current_error*/ 1, 9578 /*sense_key*/ SSD_KEY_NO_SENSE, 9579 /*asc*/ 0x00, 9580 /*ascq*/ 0x00, 9581 SSD_ELEM_NONE); 9582 9583 ctsio->scsi_status = SCSI_STATUS_OK; 9584 9585 /* 9586 * We report 0 for the sense length, because we aren't doing 9587 * autosense in this case. We're reporting sense as parameter data. 9588 */ 9589 ctsio->sense_len = 0; 9590 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9591 ctsio->be_move_done = ctl_config_move_done; 9592 ctl_datamove((union ctl_io *)ctsio); 9593 9594 return (CTL_RETVAL_COMPLETE); 9595 } 9596 9597 int 9598 ctl_tur(struct ctl_scsiio *ctsio) 9599 { 9600 9601 CTL_DEBUG_PRINT(("ctl_tur\n")); 9602 9603 ctsio->scsi_status = SCSI_STATUS_OK; 9604 ctsio->io_hdr.status = CTL_SUCCESS; 9605 9606 ctl_done((union ctl_io *)ctsio); 9607 9608 return (CTL_RETVAL_COMPLETE); 9609 } 9610 9611 #ifdef notyet 9612 static int 9613 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9614 { 9615 9616 } 9617 #endif 9618 9619 static int 9620 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9621 { 9622 struct scsi_vpd_supported_pages *pages; 9623 int sup_page_size; 9624 struct ctl_lun *lun; 9625 9626 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9627 9628 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9629 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9630 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9631 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9632 ctsio->kern_sg_entries = 0; 9633 9634 if (sup_page_size < alloc_len) { 9635 ctsio->residual = alloc_len - sup_page_size; 9636 ctsio->kern_data_len = sup_page_size; 9637 ctsio->kern_total_len = sup_page_size; 9638 } else { 9639 ctsio->residual = 0; 9640 ctsio->kern_data_len = alloc_len; 9641 ctsio->kern_total_len = alloc_len; 9642 } 9643 ctsio->kern_data_resid = 0; 9644 ctsio->kern_rel_offset = 0; 9645 ctsio->kern_sg_entries = 0; 9646 9647 /* 9648 * The control device is always connected. The disk device, on the 9649 * other hand, may not be online all the time. Need to change this 9650 * to figure out whether the disk device is actually online or not. 9651 */ 9652 if (lun != NULL) 9653 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9654 lun->be_lun->lun_type; 9655 else 9656 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9657 9658 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9659 /* Supported VPD pages */ 9660 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9661 /* Serial Number */ 9662 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9663 /* Device Identification */ 9664 pages->page_list[2] = SVPD_DEVICE_ID; 9665 /* Extended INQUIRY Data */ 9666 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9667 /* Mode Page Policy */ 9668 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9669 /* SCSI Ports */ 9670 pages->page_list[5] = SVPD_SCSI_PORTS; 9671 /* Third-party Copy */ 9672 pages->page_list[6] = SVPD_SCSI_TPC; 9673 /* Block limits */ 9674 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9675 /* Block Device Characteristics */ 9676 pages->page_list[8] = SVPD_BDC; 9677 /* Logical Block Provisioning */ 9678 pages->page_list[9] = SVPD_LBP; 9679 9680 ctsio->scsi_status = SCSI_STATUS_OK; 9681 9682 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9683 ctsio->be_move_done = ctl_config_move_done; 9684 ctl_datamove((union ctl_io *)ctsio); 9685 9686 return (CTL_RETVAL_COMPLETE); 9687 } 9688 9689 static int 9690 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9691 { 9692 struct scsi_vpd_unit_serial_number *sn_ptr; 9693 struct ctl_lun *lun; 9694 int data_len; 9695 9696 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9697 9698 data_len = 4 + CTL_SN_LEN; 9699 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9700 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9701 if (data_len < alloc_len) { 9702 ctsio->residual = alloc_len - data_len; 9703 ctsio->kern_data_len = data_len; 9704 ctsio->kern_total_len = data_len; 9705 } else { 9706 ctsio->residual = 0; 9707 ctsio->kern_data_len = alloc_len; 9708 ctsio->kern_total_len = alloc_len; 9709 } 9710 ctsio->kern_data_resid = 0; 9711 ctsio->kern_rel_offset = 0; 9712 ctsio->kern_sg_entries = 0; 9713 9714 /* 9715 * The control device is always connected. The disk device, on the 9716 * other hand, may not be online all the time. Need to change this 9717 * to figure out whether the disk device is actually online or not. 9718 */ 9719 if (lun != NULL) 9720 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9721 lun->be_lun->lun_type; 9722 else 9723 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9724 9725 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9726 sn_ptr->length = CTL_SN_LEN; 9727 /* 9728 * If we don't have a LUN, we just leave the serial number as 9729 * all spaces. 9730 */ 9731 if (lun != NULL) { 9732 strncpy((char *)sn_ptr->serial_num, 9733 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9734 } else 9735 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9736 ctsio->scsi_status = SCSI_STATUS_OK; 9737 9738 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9739 ctsio->be_move_done = ctl_config_move_done; 9740 ctl_datamove((union ctl_io *)ctsio); 9741 9742 return (CTL_RETVAL_COMPLETE); 9743 } 9744 9745 9746 static int 9747 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9748 { 9749 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9750 struct ctl_lun *lun; 9751 int data_len; 9752 9753 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9754 9755 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9756 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9757 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9758 ctsio->kern_sg_entries = 0; 9759 9760 if (data_len < alloc_len) { 9761 ctsio->residual = alloc_len - data_len; 9762 ctsio->kern_data_len = data_len; 9763 ctsio->kern_total_len = data_len; 9764 } else { 9765 ctsio->residual = 0; 9766 ctsio->kern_data_len = alloc_len; 9767 ctsio->kern_total_len = alloc_len; 9768 } 9769 ctsio->kern_data_resid = 0; 9770 ctsio->kern_rel_offset = 0; 9771 ctsio->kern_sg_entries = 0; 9772 9773 /* 9774 * The control device is always connected. The disk device, on the 9775 * other hand, may not be online all the time. 9776 */ 9777 if (lun != NULL) 9778 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9779 lun->be_lun->lun_type; 9780 else 9781 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9782 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9783 eid_ptr->page_length = data_len - 4; 9784 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9785 eid_ptr->flags3 = SVPD_EID_V_SUP; 9786 9787 ctsio->scsi_status = SCSI_STATUS_OK; 9788 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9789 ctsio->be_move_done = ctl_config_move_done; 9790 ctl_datamove((union ctl_io *)ctsio); 9791 9792 return (CTL_RETVAL_COMPLETE); 9793 } 9794 9795 static int 9796 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9797 { 9798 struct scsi_vpd_mode_page_policy *mpp_ptr; 9799 struct ctl_lun *lun; 9800 int data_len; 9801 9802 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9803 9804 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9805 sizeof(struct scsi_vpd_mode_page_policy_descr); 9806 9807 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9808 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9809 ctsio->kern_sg_entries = 0; 9810 9811 if (data_len < alloc_len) { 9812 ctsio->residual = alloc_len - data_len; 9813 ctsio->kern_data_len = data_len; 9814 ctsio->kern_total_len = data_len; 9815 } else { 9816 ctsio->residual = 0; 9817 ctsio->kern_data_len = alloc_len; 9818 ctsio->kern_total_len = alloc_len; 9819 } 9820 ctsio->kern_data_resid = 0; 9821 ctsio->kern_rel_offset = 0; 9822 ctsio->kern_sg_entries = 0; 9823 9824 /* 9825 * The control device is always connected. The disk device, on the 9826 * other hand, may not be online all the time. 9827 */ 9828 if (lun != NULL) 9829 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9830 lun->be_lun->lun_type; 9831 else 9832 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9833 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9834 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9835 mpp_ptr->descr[0].page_code = 0x3f; 9836 mpp_ptr->descr[0].subpage_code = 0xff; 9837 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9838 9839 ctsio->scsi_status = SCSI_STATUS_OK; 9840 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9841 ctsio->be_move_done = ctl_config_move_done; 9842 ctl_datamove((union ctl_io *)ctsio); 9843 9844 return (CTL_RETVAL_COMPLETE); 9845 } 9846 9847 static int 9848 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9849 { 9850 struct scsi_vpd_device_id *devid_ptr; 9851 struct scsi_vpd_id_descriptor *desc; 9852 struct ctl_softc *ctl_softc; 9853 struct ctl_lun *lun; 9854 struct ctl_port *port; 9855 int data_len; 9856 uint8_t proto; 9857 9858 ctl_softc = control_softc; 9859 9860 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9861 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9862 9863 data_len = sizeof(struct scsi_vpd_device_id) + 9864 sizeof(struct scsi_vpd_id_descriptor) + 9865 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9866 sizeof(struct scsi_vpd_id_descriptor) + 9867 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9868 if (lun && lun->lun_devid) 9869 data_len += lun->lun_devid->len; 9870 if (port->port_devid) 9871 data_len += port->port_devid->len; 9872 if (port->target_devid) 9873 data_len += port->target_devid->len; 9874 9875 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9876 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9877 ctsio->kern_sg_entries = 0; 9878 9879 if (data_len < alloc_len) { 9880 ctsio->residual = alloc_len - data_len; 9881 ctsio->kern_data_len = data_len; 9882 ctsio->kern_total_len = data_len; 9883 } else { 9884 ctsio->residual = 0; 9885 ctsio->kern_data_len = alloc_len; 9886 ctsio->kern_total_len = alloc_len; 9887 } 9888 ctsio->kern_data_resid = 0; 9889 ctsio->kern_rel_offset = 0; 9890 ctsio->kern_sg_entries = 0; 9891 9892 /* 9893 * The control device is always connected. The disk device, on the 9894 * other hand, may not be online all the time. 9895 */ 9896 if (lun != NULL) 9897 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9898 lun->be_lun->lun_type; 9899 else 9900 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9901 devid_ptr->page_code = SVPD_DEVICE_ID; 9902 scsi_ulto2b(data_len - 4, devid_ptr->length); 9903 9904 if (port->port_type == CTL_PORT_FC) 9905 proto = SCSI_PROTO_FC << 4; 9906 else if (port->port_type == CTL_PORT_ISCSI) 9907 proto = SCSI_PROTO_ISCSI << 4; 9908 else 9909 proto = SCSI_PROTO_SPI << 4; 9910 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9911 9912 /* 9913 * We're using a LUN association here. i.e., this device ID is a 9914 * per-LUN identifier. 9915 */ 9916 if (lun && lun->lun_devid) { 9917 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9918 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9919 lun->lun_devid->len); 9920 } 9921 9922 /* 9923 * This is for the WWPN which is a port association. 9924 */ 9925 if (port->port_devid) { 9926 memcpy(desc, port->port_devid->data, port->port_devid->len); 9927 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9928 port->port_devid->len); 9929 } 9930 9931 /* 9932 * This is for the Relative Target Port(type 4h) identifier 9933 */ 9934 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9935 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9936 SVPD_ID_TYPE_RELTARG; 9937 desc->length = 4; 9938 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9939 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9940 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9941 9942 /* 9943 * This is for the Target Port Group(type 5h) identifier 9944 */ 9945 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9946 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9947 SVPD_ID_TYPE_TPORTGRP; 9948 desc->length = 4; 9949 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9950 &desc->identifier[2]); 9951 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9952 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9953 9954 /* 9955 * This is for the Target identifier 9956 */ 9957 if (port->target_devid) { 9958 memcpy(desc, port->target_devid->data, port->target_devid->len); 9959 } 9960 9961 ctsio->scsi_status = SCSI_STATUS_OK; 9962 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9963 ctsio->be_move_done = ctl_config_move_done; 9964 ctl_datamove((union ctl_io *)ctsio); 9965 9966 return (CTL_RETVAL_COMPLETE); 9967 } 9968 9969 static int 9970 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9971 { 9972 struct ctl_softc *softc = control_softc; 9973 struct scsi_vpd_scsi_ports *sp; 9974 struct scsi_vpd_port_designation *pd; 9975 struct scsi_vpd_port_designation_cont *pdc; 9976 struct ctl_lun *lun; 9977 struct ctl_port *port; 9978 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 9979 int num_target_port_groups; 9980 9981 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9982 9983 if (softc->is_single) 9984 num_target_port_groups = 1; 9985 else 9986 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 9987 num_target_ports = 0; 9988 iid_len = 0; 9989 id_len = 0; 9990 mtx_lock(&softc->ctl_lock); 9991 STAILQ_FOREACH(port, &softc->port_list, links) { 9992 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9993 continue; 9994 if (lun != NULL && 9995 ctl_map_lun_back(port->targ_port, lun->lun) >= 9996 CTL_MAX_LUNS) 9997 continue; 9998 num_target_ports++; 9999 if (port->init_devid) 10000 iid_len += port->init_devid->len; 10001 if (port->port_devid) 10002 id_len += port->port_devid->len; 10003 } 10004 mtx_unlock(&softc->ctl_lock); 10005 10006 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10007 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10008 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10009 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10010 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10011 ctsio->kern_sg_entries = 0; 10012 10013 if (data_len < alloc_len) { 10014 ctsio->residual = alloc_len - data_len; 10015 ctsio->kern_data_len = data_len; 10016 ctsio->kern_total_len = data_len; 10017 } else { 10018 ctsio->residual = 0; 10019 ctsio->kern_data_len = alloc_len; 10020 ctsio->kern_total_len = alloc_len; 10021 } 10022 ctsio->kern_data_resid = 0; 10023 ctsio->kern_rel_offset = 0; 10024 ctsio->kern_sg_entries = 0; 10025 10026 /* 10027 * The control device is always connected. The disk device, on the 10028 * other hand, may not be online all the time. Need to change this 10029 * to figure out whether the disk device is actually online or not. 10030 */ 10031 if (lun != NULL) 10032 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10033 lun->be_lun->lun_type; 10034 else 10035 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10036 10037 sp->page_code = SVPD_SCSI_PORTS; 10038 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10039 sp->page_length); 10040 pd = &sp->design[0]; 10041 10042 mtx_lock(&softc->ctl_lock); 10043 pg = softc->port_offset / CTL_MAX_PORTS; 10044 for (g = 0; g < num_target_port_groups; g++) { 10045 STAILQ_FOREACH(port, &softc->port_list, links) { 10046 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10047 continue; 10048 if (lun != NULL && 10049 ctl_map_lun_back(port->targ_port, lun->lun) >= 10050 CTL_MAX_LUNS) 10051 continue; 10052 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10053 scsi_ulto2b(p, pd->relative_port_id); 10054 if (port->init_devid && g == pg) { 10055 iid_len = port->init_devid->len; 10056 memcpy(pd->initiator_transportid, 10057 port->init_devid->data, port->init_devid->len); 10058 } else 10059 iid_len = 0; 10060 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10061 pdc = (struct scsi_vpd_port_designation_cont *) 10062 (&pd->initiator_transportid[iid_len]); 10063 if (port->port_devid && g == pg) { 10064 id_len = port->port_devid->len; 10065 memcpy(pdc->target_port_descriptors, 10066 port->port_devid->data, port->port_devid->len); 10067 } else 10068 id_len = 0; 10069 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10070 pd = (struct scsi_vpd_port_designation *) 10071 ((uint8_t *)pdc->target_port_descriptors + id_len); 10072 } 10073 } 10074 mtx_unlock(&softc->ctl_lock); 10075 10076 ctsio->scsi_status = SCSI_STATUS_OK; 10077 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10078 ctsio->be_move_done = ctl_config_move_done; 10079 ctl_datamove((union ctl_io *)ctsio); 10080 10081 return (CTL_RETVAL_COMPLETE); 10082 } 10083 10084 static int 10085 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10086 { 10087 struct scsi_vpd_block_limits *bl_ptr; 10088 struct ctl_lun *lun; 10089 int bs; 10090 10091 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10092 10093 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10094 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10095 ctsio->kern_sg_entries = 0; 10096 10097 if (sizeof(*bl_ptr) < alloc_len) { 10098 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10099 ctsio->kern_data_len = sizeof(*bl_ptr); 10100 ctsio->kern_total_len = sizeof(*bl_ptr); 10101 } else { 10102 ctsio->residual = 0; 10103 ctsio->kern_data_len = alloc_len; 10104 ctsio->kern_total_len = alloc_len; 10105 } 10106 ctsio->kern_data_resid = 0; 10107 ctsio->kern_rel_offset = 0; 10108 ctsio->kern_sg_entries = 0; 10109 10110 /* 10111 * The control device is always connected. The disk device, on the 10112 * other hand, may not be online all the time. Need to change this 10113 * to figure out whether the disk device is actually online or not. 10114 */ 10115 if (lun != NULL) 10116 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10117 lun->be_lun->lun_type; 10118 else 10119 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10120 10121 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10122 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10123 bl_ptr->max_cmp_write_len = 0xff; 10124 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10125 if (lun != NULL) { 10126 bs = lun->be_lun->blocksize; 10127 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10128 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10129 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10130 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10131 if (lun->be_lun->pblockexp != 0) { 10132 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10133 bl_ptr->opt_unmap_grain); 10134 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10135 bl_ptr->unmap_grain_align); 10136 } 10137 } 10138 scsi_ulto4b(lun->be_lun->atomicblock, 10139 bl_ptr->max_atomic_transfer_length); 10140 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10141 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10142 } 10143 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10144 10145 ctsio->scsi_status = SCSI_STATUS_OK; 10146 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10147 ctsio->be_move_done = ctl_config_move_done; 10148 ctl_datamove((union ctl_io *)ctsio); 10149 10150 return (CTL_RETVAL_COMPLETE); 10151 } 10152 10153 static int 10154 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10155 { 10156 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10157 struct ctl_lun *lun; 10158 const char *value; 10159 u_int i; 10160 10161 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10162 10163 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10164 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10165 ctsio->kern_sg_entries = 0; 10166 10167 if (sizeof(*bdc_ptr) < alloc_len) { 10168 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10169 ctsio->kern_data_len = sizeof(*bdc_ptr); 10170 ctsio->kern_total_len = sizeof(*bdc_ptr); 10171 } else { 10172 ctsio->residual = 0; 10173 ctsio->kern_data_len = alloc_len; 10174 ctsio->kern_total_len = alloc_len; 10175 } 10176 ctsio->kern_data_resid = 0; 10177 ctsio->kern_rel_offset = 0; 10178 ctsio->kern_sg_entries = 0; 10179 10180 /* 10181 * The control device is always connected. The disk device, on the 10182 * other hand, may not be online all the time. Need to change this 10183 * to figure out whether the disk device is actually online or not. 10184 */ 10185 if (lun != NULL) 10186 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10187 lun->be_lun->lun_type; 10188 else 10189 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10190 bdc_ptr->page_code = SVPD_BDC; 10191 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10192 if (lun != NULL && 10193 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10194 i = strtol(value, NULL, 0); 10195 else 10196 i = CTL_DEFAULT_ROTATION_RATE; 10197 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10198 if (lun != NULL && 10199 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10200 i = strtol(value, NULL, 0); 10201 else 10202 i = 0; 10203 bdc_ptr->wab_wac_ff = (i & 0x0f); 10204 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10205 10206 ctsio->scsi_status = SCSI_STATUS_OK; 10207 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10208 ctsio->be_move_done = ctl_config_move_done; 10209 ctl_datamove((union ctl_io *)ctsio); 10210 10211 return (CTL_RETVAL_COMPLETE); 10212 } 10213 10214 static int 10215 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10216 { 10217 struct scsi_vpd_logical_block_prov *lbp_ptr; 10218 struct ctl_lun *lun; 10219 10220 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10221 10222 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10223 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10224 ctsio->kern_sg_entries = 0; 10225 10226 if (sizeof(*lbp_ptr) < alloc_len) { 10227 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10228 ctsio->kern_data_len = sizeof(*lbp_ptr); 10229 ctsio->kern_total_len = sizeof(*lbp_ptr); 10230 } else { 10231 ctsio->residual = 0; 10232 ctsio->kern_data_len = alloc_len; 10233 ctsio->kern_total_len = alloc_len; 10234 } 10235 ctsio->kern_data_resid = 0; 10236 ctsio->kern_rel_offset = 0; 10237 ctsio->kern_sg_entries = 0; 10238 10239 /* 10240 * The control device is always connected. The disk device, on the 10241 * other hand, may not be online all the time. Need to change this 10242 * to figure out whether the disk device is actually online or not. 10243 */ 10244 if (lun != NULL) 10245 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10246 lun->be_lun->lun_type; 10247 else 10248 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10249 10250 lbp_ptr->page_code = SVPD_LBP; 10251 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10252 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10253 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10254 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10255 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10256 lbp_ptr->prov_type = SVPD_LBP_THIN; 10257 } 10258 10259 ctsio->scsi_status = SCSI_STATUS_OK; 10260 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10261 ctsio->be_move_done = ctl_config_move_done; 10262 ctl_datamove((union ctl_io *)ctsio); 10263 10264 return (CTL_RETVAL_COMPLETE); 10265 } 10266 10267 static int 10268 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10269 { 10270 struct scsi_inquiry *cdb; 10271 int alloc_len, retval; 10272 10273 cdb = (struct scsi_inquiry *)ctsio->cdb; 10274 10275 retval = CTL_RETVAL_COMPLETE; 10276 10277 alloc_len = scsi_2btoul(cdb->length); 10278 10279 switch (cdb->page_code) { 10280 case SVPD_SUPPORTED_PAGES: 10281 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10282 break; 10283 case SVPD_UNIT_SERIAL_NUMBER: 10284 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10285 break; 10286 case SVPD_DEVICE_ID: 10287 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10288 break; 10289 case SVPD_EXTENDED_INQUIRY_DATA: 10290 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10291 break; 10292 case SVPD_MODE_PAGE_POLICY: 10293 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10294 break; 10295 case SVPD_SCSI_PORTS: 10296 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10297 break; 10298 case SVPD_SCSI_TPC: 10299 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10300 break; 10301 case SVPD_BLOCK_LIMITS: 10302 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10303 break; 10304 case SVPD_BDC: 10305 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10306 break; 10307 case SVPD_LBP: 10308 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10309 break; 10310 default: 10311 ctl_set_invalid_field(ctsio, 10312 /*sks_valid*/ 1, 10313 /*command*/ 1, 10314 /*field*/ 2, 10315 /*bit_valid*/ 0, 10316 /*bit*/ 0); 10317 ctl_done((union ctl_io *)ctsio); 10318 retval = CTL_RETVAL_COMPLETE; 10319 break; 10320 } 10321 10322 return (retval); 10323 } 10324 10325 static int 10326 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10327 { 10328 struct scsi_inquiry_data *inq_ptr; 10329 struct scsi_inquiry *cdb; 10330 struct ctl_softc *ctl_softc; 10331 struct ctl_lun *lun; 10332 char *val; 10333 uint32_t alloc_len, data_len; 10334 ctl_port_type port_type; 10335 10336 ctl_softc = control_softc; 10337 10338 /* 10339 * Figure out whether we're talking to a Fibre Channel port or not. 10340 * We treat the ioctl front end, and any SCSI adapters, as packetized 10341 * SCSI front ends. 10342 */ 10343 port_type = ctl_softc->ctl_ports[ 10344 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10345 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10346 port_type = CTL_PORT_SCSI; 10347 10348 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10349 cdb = (struct scsi_inquiry *)ctsio->cdb; 10350 alloc_len = scsi_2btoul(cdb->length); 10351 10352 /* 10353 * We malloc the full inquiry data size here and fill it 10354 * in. If the user only asks for less, we'll give him 10355 * that much. 10356 */ 10357 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10358 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10359 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10360 ctsio->kern_sg_entries = 0; 10361 ctsio->kern_data_resid = 0; 10362 ctsio->kern_rel_offset = 0; 10363 10364 if (data_len < alloc_len) { 10365 ctsio->residual = alloc_len - data_len; 10366 ctsio->kern_data_len = data_len; 10367 ctsio->kern_total_len = data_len; 10368 } else { 10369 ctsio->residual = 0; 10370 ctsio->kern_data_len = alloc_len; 10371 ctsio->kern_total_len = alloc_len; 10372 } 10373 10374 /* 10375 * If we have a LUN configured, report it as connected. Otherwise, 10376 * report that it is offline or no device is supported, depending 10377 * on the value of inquiry_pq_no_lun. 10378 * 10379 * According to the spec (SPC-4 r34), the peripheral qualifier 10380 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10381 * 10382 * "A peripheral device having the specified peripheral device type 10383 * is not connected to this logical unit. However, the device 10384 * server is capable of supporting the specified peripheral device 10385 * type on this logical unit." 10386 * 10387 * According to the same spec, the peripheral qualifier 10388 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10389 * 10390 * "The device server is not capable of supporting a peripheral 10391 * device on this logical unit. For this peripheral qualifier the 10392 * peripheral device type shall be set to 1Fh. All other peripheral 10393 * device type values are reserved for this peripheral qualifier." 10394 * 10395 * Given the text, it would seem that we probably want to report that 10396 * the LUN is offline here. There is no LUN connected, but we can 10397 * support a LUN at the given LUN number. 10398 * 10399 * In the real world, though, it sounds like things are a little 10400 * different: 10401 * 10402 * - Linux, when presented with a LUN with the offline peripheral 10403 * qualifier, will create an sg driver instance for it. So when 10404 * you attach it to CTL, you wind up with a ton of sg driver 10405 * instances. (One for every LUN that Linux bothered to probe.) 10406 * Linux does this despite the fact that it issues a REPORT LUNs 10407 * to LUN 0 to get the inventory of supported LUNs. 10408 * 10409 * - There is other anecdotal evidence (from Emulex folks) about 10410 * arrays that use the offline peripheral qualifier for LUNs that 10411 * are on the "passive" path in an active/passive array. 10412 * 10413 * So the solution is provide a hopefully reasonable default 10414 * (return bad/no LUN) and allow the user to change the behavior 10415 * with a tunable/sysctl variable. 10416 */ 10417 if (lun != NULL) 10418 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10419 lun->be_lun->lun_type; 10420 else if (ctl_softc->inquiry_pq_no_lun == 0) 10421 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10422 else 10423 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10424 10425 /* RMB in byte 2 is 0 */ 10426 inq_ptr->version = SCSI_REV_SPC4; 10427 10428 /* 10429 * According to SAM-3, even if a device only supports a single 10430 * level of LUN addressing, it should still set the HISUP bit: 10431 * 10432 * 4.9.1 Logical unit numbers overview 10433 * 10434 * All logical unit number formats described in this standard are 10435 * hierarchical in structure even when only a single level in that 10436 * hierarchy is used. The HISUP bit shall be set to one in the 10437 * standard INQUIRY data (see SPC-2) when any logical unit number 10438 * format described in this standard is used. Non-hierarchical 10439 * formats are outside the scope of this standard. 10440 * 10441 * Therefore we set the HiSup bit here. 10442 * 10443 * The reponse format is 2, per SPC-3. 10444 */ 10445 inq_ptr->response_format = SID_HiSup | 2; 10446 10447 inq_ptr->additional_length = data_len - 10448 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10449 CTL_DEBUG_PRINT(("additional_length = %d\n", 10450 inq_ptr->additional_length)); 10451 10452 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10453 /* 16 bit addressing */ 10454 if (port_type == CTL_PORT_SCSI) 10455 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10456 /* XXX set the SID_MultiP bit here if we're actually going to 10457 respond on multiple ports */ 10458 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10459 10460 /* 16 bit data bus, synchronous transfers */ 10461 if (port_type == CTL_PORT_SCSI) 10462 inq_ptr->flags = SID_WBus16 | SID_Sync; 10463 /* 10464 * XXX KDM do we want to support tagged queueing on the control 10465 * device at all? 10466 */ 10467 if ((lun == NULL) 10468 || (lun->be_lun->lun_type != T_PROCESSOR)) 10469 inq_ptr->flags |= SID_CmdQue; 10470 /* 10471 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10472 * We have 8 bytes for the vendor name, and 16 bytes for the device 10473 * name and 4 bytes for the revision. 10474 */ 10475 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10476 "vendor")) == NULL) { 10477 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10478 } else { 10479 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10480 strncpy(inq_ptr->vendor, val, 10481 min(sizeof(inq_ptr->vendor), strlen(val))); 10482 } 10483 if (lun == NULL) { 10484 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10485 sizeof(inq_ptr->product)); 10486 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10487 switch (lun->be_lun->lun_type) { 10488 case T_DIRECT: 10489 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10490 sizeof(inq_ptr->product)); 10491 break; 10492 case T_PROCESSOR: 10493 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10494 sizeof(inq_ptr->product)); 10495 break; 10496 default: 10497 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10498 sizeof(inq_ptr->product)); 10499 break; 10500 } 10501 } else { 10502 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10503 strncpy(inq_ptr->product, val, 10504 min(sizeof(inq_ptr->product), strlen(val))); 10505 } 10506 10507 /* 10508 * XXX make this a macro somewhere so it automatically gets 10509 * incremented when we make changes. 10510 */ 10511 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10512 "revision")) == NULL) { 10513 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10514 } else { 10515 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10516 strncpy(inq_ptr->revision, val, 10517 min(sizeof(inq_ptr->revision), strlen(val))); 10518 } 10519 10520 /* 10521 * For parallel SCSI, we support double transition and single 10522 * transition clocking. We also support QAS (Quick Arbitration 10523 * and Selection) and Information Unit transfers on both the 10524 * control and array devices. 10525 */ 10526 if (port_type == CTL_PORT_SCSI) 10527 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10528 SID_SPI_IUS; 10529 10530 /* SAM-5 (no version claimed) */ 10531 scsi_ulto2b(0x00A0, inq_ptr->version1); 10532 /* SPC-4 (no version claimed) */ 10533 scsi_ulto2b(0x0460, inq_ptr->version2); 10534 if (port_type == CTL_PORT_FC) { 10535 /* FCP-2 ANSI INCITS.350:2003 */ 10536 scsi_ulto2b(0x0917, inq_ptr->version3); 10537 } else if (port_type == CTL_PORT_SCSI) { 10538 /* SPI-4 ANSI INCITS.362:200x */ 10539 scsi_ulto2b(0x0B56, inq_ptr->version3); 10540 } else if (port_type == CTL_PORT_ISCSI) { 10541 /* iSCSI (no version claimed) */ 10542 scsi_ulto2b(0x0960, inq_ptr->version3); 10543 } else if (port_type == CTL_PORT_SAS) { 10544 /* SAS (no version claimed) */ 10545 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10546 } 10547 10548 if (lun == NULL) { 10549 /* SBC-4 (no version claimed) */ 10550 scsi_ulto2b(0x0600, inq_ptr->version4); 10551 } else { 10552 switch (lun->be_lun->lun_type) { 10553 case T_DIRECT: 10554 /* SBC-4 (no version claimed) */ 10555 scsi_ulto2b(0x0600, inq_ptr->version4); 10556 break; 10557 case T_PROCESSOR: 10558 default: 10559 break; 10560 } 10561 } 10562 10563 ctsio->scsi_status = SCSI_STATUS_OK; 10564 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10565 ctsio->be_move_done = ctl_config_move_done; 10566 ctl_datamove((union ctl_io *)ctsio); 10567 return (CTL_RETVAL_COMPLETE); 10568 } 10569 10570 int 10571 ctl_inquiry(struct ctl_scsiio *ctsio) 10572 { 10573 struct scsi_inquiry *cdb; 10574 int retval; 10575 10576 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10577 10578 cdb = (struct scsi_inquiry *)ctsio->cdb; 10579 if (cdb->byte2 & SI_EVPD) 10580 retval = ctl_inquiry_evpd(ctsio); 10581 else if (cdb->page_code == 0) 10582 retval = ctl_inquiry_std(ctsio); 10583 else { 10584 ctl_set_invalid_field(ctsio, 10585 /*sks_valid*/ 1, 10586 /*command*/ 1, 10587 /*field*/ 2, 10588 /*bit_valid*/ 0, 10589 /*bit*/ 0); 10590 ctl_done((union ctl_io *)ctsio); 10591 return (CTL_RETVAL_COMPLETE); 10592 } 10593 10594 return (retval); 10595 } 10596 10597 /* 10598 * For known CDB types, parse the LBA and length. 10599 */ 10600 static int 10601 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10602 { 10603 if (io->io_hdr.io_type != CTL_IO_SCSI) 10604 return (1); 10605 10606 switch (io->scsiio.cdb[0]) { 10607 case COMPARE_AND_WRITE: { 10608 struct scsi_compare_and_write *cdb; 10609 10610 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10611 10612 *lba = scsi_8btou64(cdb->addr); 10613 *len = cdb->length; 10614 break; 10615 } 10616 case READ_6: 10617 case WRITE_6: { 10618 struct scsi_rw_6 *cdb; 10619 10620 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10621 10622 *lba = scsi_3btoul(cdb->addr); 10623 /* only 5 bits are valid in the most significant address byte */ 10624 *lba &= 0x1fffff; 10625 *len = cdb->length; 10626 break; 10627 } 10628 case READ_10: 10629 case WRITE_10: { 10630 struct scsi_rw_10 *cdb; 10631 10632 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10633 10634 *lba = scsi_4btoul(cdb->addr); 10635 *len = scsi_2btoul(cdb->length); 10636 break; 10637 } 10638 case WRITE_VERIFY_10: { 10639 struct scsi_write_verify_10 *cdb; 10640 10641 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10642 10643 *lba = scsi_4btoul(cdb->addr); 10644 *len = scsi_2btoul(cdb->length); 10645 break; 10646 } 10647 case READ_12: 10648 case WRITE_12: { 10649 struct scsi_rw_12 *cdb; 10650 10651 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10652 10653 *lba = scsi_4btoul(cdb->addr); 10654 *len = scsi_4btoul(cdb->length); 10655 break; 10656 } 10657 case WRITE_VERIFY_12: { 10658 struct scsi_write_verify_12 *cdb; 10659 10660 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10661 10662 *lba = scsi_4btoul(cdb->addr); 10663 *len = scsi_4btoul(cdb->length); 10664 break; 10665 } 10666 case READ_16: 10667 case WRITE_16: 10668 case WRITE_ATOMIC_16: { 10669 struct scsi_rw_16 *cdb; 10670 10671 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10672 10673 *lba = scsi_8btou64(cdb->addr); 10674 *len = scsi_4btoul(cdb->length); 10675 break; 10676 } 10677 case WRITE_VERIFY_16: { 10678 struct scsi_write_verify_16 *cdb; 10679 10680 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10681 10682 *lba = scsi_8btou64(cdb->addr); 10683 *len = scsi_4btoul(cdb->length); 10684 break; 10685 } 10686 case WRITE_SAME_10: { 10687 struct scsi_write_same_10 *cdb; 10688 10689 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10690 10691 *lba = scsi_4btoul(cdb->addr); 10692 *len = scsi_2btoul(cdb->length); 10693 break; 10694 } 10695 case WRITE_SAME_16: { 10696 struct scsi_write_same_16 *cdb; 10697 10698 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10699 10700 *lba = scsi_8btou64(cdb->addr); 10701 *len = scsi_4btoul(cdb->length); 10702 break; 10703 } 10704 case VERIFY_10: { 10705 struct scsi_verify_10 *cdb; 10706 10707 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10708 10709 *lba = scsi_4btoul(cdb->addr); 10710 *len = scsi_2btoul(cdb->length); 10711 break; 10712 } 10713 case VERIFY_12: { 10714 struct scsi_verify_12 *cdb; 10715 10716 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10717 10718 *lba = scsi_4btoul(cdb->addr); 10719 *len = scsi_4btoul(cdb->length); 10720 break; 10721 } 10722 case VERIFY_16: { 10723 struct scsi_verify_16 *cdb; 10724 10725 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10726 10727 *lba = scsi_8btou64(cdb->addr); 10728 *len = scsi_4btoul(cdb->length); 10729 break; 10730 } 10731 case UNMAP: { 10732 *lba = 0; 10733 *len = UINT64_MAX; 10734 break; 10735 } 10736 default: 10737 return (1); 10738 break; /* NOTREACHED */ 10739 } 10740 10741 return (0); 10742 } 10743 10744 static ctl_action 10745 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10746 { 10747 uint64_t endlba1, endlba2; 10748 10749 endlba1 = lba1 + len1 - 1; 10750 endlba2 = lba2 + len2 - 1; 10751 10752 if ((endlba1 < lba2) 10753 || (endlba2 < lba1)) 10754 return (CTL_ACTION_PASS); 10755 else 10756 return (CTL_ACTION_BLOCK); 10757 } 10758 10759 static int 10760 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10761 { 10762 struct ctl_ptr_len_flags *ptrlen; 10763 struct scsi_unmap_desc *buf, *end, *range; 10764 uint64_t lba; 10765 uint32_t len; 10766 10767 /* If not UNMAP -- go other way. */ 10768 if (io->io_hdr.io_type != CTL_IO_SCSI || 10769 io->scsiio.cdb[0] != UNMAP) 10770 return (CTL_ACTION_ERROR); 10771 10772 /* If UNMAP without data -- block and wait for data. */ 10773 ptrlen = (struct ctl_ptr_len_flags *) 10774 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10775 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10776 ptrlen->ptr == NULL) 10777 return (CTL_ACTION_BLOCK); 10778 10779 /* UNMAP with data -- check for collision. */ 10780 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10781 end = buf + ptrlen->len / sizeof(*buf); 10782 for (range = buf; range < end; range++) { 10783 lba = scsi_8btou64(range->lba); 10784 len = scsi_4btoul(range->length); 10785 if ((lba < lba2 + len2) && (lba + len > lba2)) 10786 return (CTL_ACTION_BLOCK); 10787 } 10788 return (CTL_ACTION_PASS); 10789 } 10790 10791 static ctl_action 10792 ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10793 { 10794 uint64_t lba1, lba2; 10795 uint64_t len1, len2; 10796 int retval; 10797 10798 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10799 return (CTL_ACTION_ERROR); 10800 10801 retval = ctl_extent_check_unmap(io2, lba1, len1); 10802 if (retval != CTL_ACTION_ERROR) 10803 return (retval); 10804 10805 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10806 return (CTL_ACTION_ERROR); 10807 10808 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10809 } 10810 10811 static ctl_action 10812 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10813 union ctl_io *ooa_io) 10814 { 10815 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10816 ctl_serialize_action *serialize_row; 10817 10818 /* 10819 * The initiator attempted multiple untagged commands at the same 10820 * time. Can't do that. 10821 */ 10822 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10823 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10824 && ((pending_io->io_hdr.nexus.targ_port == 10825 ooa_io->io_hdr.nexus.targ_port) 10826 && (pending_io->io_hdr.nexus.initid.id == 10827 ooa_io->io_hdr.nexus.initid.id)) 10828 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10829 return (CTL_ACTION_OVERLAP); 10830 10831 /* 10832 * The initiator attempted to send multiple tagged commands with 10833 * the same ID. (It's fine if different initiators have the same 10834 * tag ID.) 10835 * 10836 * Even if all of those conditions are true, we don't kill the I/O 10837 * if the command ahead of us has been aborted. We won't end up 10838 * sending it to the FETD, and it's perfectly legal to resend a 10839 * command with the same tag number as long as the previous 10840 * instance of this tag number has been aborted somehow. 10841 */ 10842 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10843 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10844 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10845 && ((pending_io->io_hdr.nexus.targ_port == 10846 ooa_io->io_hdr.nexus.targ_port) 10847 && (pending_io->io_hdr.nexus.initid.id == 10848 ooa_io->io_hdr.nexus.initid.id)) 10849 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10850 return (CTL_ACTION_OVERLAP_TAG); 10851 10852 /* 10853 * If we get a head of queue tag, SAM-3 says that we should 10854 * immediately execute it. 10855 * 10856 * What happens if this command would normally block for some other 10857 * reason? e.g. a request sense with a head of queue tag 10858 * immediately after a write. Normally that would block, but this 10859 * will result in its getting executed immediately... 10860 * 10861 * We currently return "pass" instead of "skip", so we'll end up 10862 * going through the rest of the queue to check for overlapped tags. 10863 * 10864 * XXX KDM check for other types of blockage first?? 10865 */ 10866 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10867 return (CTL_ACTION_PASS); 10868 10869 /* 10870 * Ordered tags have to block until all items ahead of them 10871 * have completed. If we get called with an ordered tag, we always 10872 * block, if something else is ahead of us in the queue. 10873 */ 10874 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10875 return (CTL_ACTION_BLOCK); 10876 10877 /* 10878 * Simple tags get blocked until all head of queue and ordered tags 10879 * ahead of them have completed. I'm lumping untagged commands in 10880 * with simple tags here. XXX KDM is that the right thing to do? 10881 */ 10882 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10883 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10884 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10885 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10886 return (CTL_ACTION_BLOCK); 10887 10888 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10889 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10890 10891 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10892 10893 switch (serialize_row[pending_entry->seridx]) { 10894 case CTL_SER_BLOCK: 10895 return (CTL_ACTION_BLOCK); 10896 case CTL_SER_EXTENT: 10897 return (ctl_extent_check(pending_io, ooa_io)); 10898 case CTL_SER_EXTENTOPT: 10899 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10900 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10901 return (ctl_extent_check(pending_io, ooa_io)); 10902 /* FALLTHROUGH */ 10903 case CTL_SER_PASS: 10904 return (CTL_ACTION_PASS); 10905 case CTL_SER_BLOCKOPT: 10906 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10907 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10908 return (CTL_ACTION_BLOCK); 10909 return (CTL_ACTION_PASS); 10910 case CTL_SER_SKIP: 10911 return (CTL_ACTION_SKIP); 10912 default: 10913 panic("invalid serialization value %d", 10914 serialize_row[pending_entry->seridx]); 10915 } 10916 10917 return (CTL_ACTION_ERROR); 10918 } 10919 10920 /* 10921 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10922 * Assumptions: 10923 * - pending_io is generally either incoming, or on the blocked queue 10924 * - starting I/O is the I/O we want to start the check with. 10925 */ 10926 static ctl_action 10927 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10928 union ctl_io *starting_io) 10929 { 10930 union ctl_io *ooa_io; 10931 ctl_action action; 10932 10933 mtx_assert(&lun->lun_lock, MA_OWNED); 10934 10935 /* 10936 * Run back along the OOA queue, starting with the current 10937 * blocked I/O and going through every I/O before it on the 10938 * queue. If starting_io is NULL, we'll just end up returning 10939 * CTL_ACTION_PASS. 10940 */ 10941 for (ooa_io = starting_io; ooa_io != NULL; 10942 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10943 ooa_links)){ 10944 10945 /* 10946 * This routine just checks to see whether 10947 * cur_blocked is blocked by ooa_io, which is ahead 10948 * of it in the queue. It doesn't queue/dequeue 10949 * cur_blocked. 10950 */ 10951 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10952 switch (action) { 10953 case CTL_ACTION_BLOCK: 10954 case CTL_ACTION_OVERLAP: 10955 case CTL_ACTION_OVERLAP_TAG: 10956 case CTL_ACTION_SKIP: 10957 case CTL_ACTION_ERROR: 10958 return (action); 10959 break; /* NOTREACHED */ 10960 case CTL_ACTION_PASS: 10961 break; 10962 default: 10963 panic("invalid action %d", action); 10964 break; /* NOTREACHED */ 10965 } 10966 } 10967 10968 return (CTL_ACTION_PASS); 10969 } 10970 10971 /* 10972 * Assumptions: 10973 * - An I/O has just completed, and has been removed from the per-LUN OOA 10974 * queue, so some items on the blocked queue may now be unblocked. 10975 */ 10976 static int 10977 ctl_check_blocked(struct ctl_lun *lun) 10978 { 10979 union ctl_io *cur_blocked, *next_blocked; 10980 10981 mtx_assert(&lun->lun_lock, MA_OWNED); 10982 10983 /* 10984 * Run forward from the head of the blocked queue, checking each 10985 * entry against the I/Os prior to it on the OOA queue to see if 10986 * there is still any blockage. 10987 * 10988 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10989 * with our removing a variable on it while it is traversing the 10990 * list. 10991 */ 10992 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10993 cur_blocked != NULL; cur_blocked = next_blocked) { 10994 union ctl_io *prev_ooa; 10995 ctl_action action; 10996 10997 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10998 blocked_links); 10999 11000 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11001 ctl_ooaq, ooa_links); 11002 11003 /* 11004 * If cur_blocked happens to be the first item in the OOA 11005 * queue now, prev_ooa will be NULL, and the action 11006 * returned will just be CTL_ACTION_PASS. 11007 */ 11008 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11009 11010 switch (action) { 11011 case CTL_ACTION_BLOCK: 11012 /* Nothing to do here, still blocked */ 11013 break; 11014 case CTL_ACTION_OVERLAP: 11015 case CTL_ACTION_OVERLAP_TAG: 11016 /* 11017 * This shouldn't happen! In theory we've already 11018 * checked this command for overlap... 11019 */ 11020 break; 11021 case CTL_ACTION_PASS: 11022 case CTL_ACTION_SKIP: { 11023 struct ctl_softc *softc; 11024 const struct ctl_cmd_entry *entry; 11025 uint32_t initidx; 11026 int isc_retval; 11027 11028 /* 11029 * The skip case shouldn't happen, this transaction 11030 * should have never made it onto the blocked queue. 11031 */ 11032 /* 11033 * This I/O is no longer blocked, we can remove it 11034 * from the blocked queue. Since this is a TAILQ 11035 * (doubly linked list), we can do O(1) removals 11036 * from any place on the list. 11037 */ 11038 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11039 blocked_links); 11040 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11041 11042 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11043 /* 11044 * Need to send IO back to original side to 11045 * run 11046 */ 11047 union ctl_ha_msg msg_info; 11048 11049 msg_info.hdr.original_sc = 11050 cur_blocked->io_hdr.original_sc; 11051 msg_info.hdr.serializing_sc = cur_blocked; 11052 msg_info.hdr.msg_type = CTL_MSG_R2R; 11053 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11054 &msg_info, sizeof(msg_info), 0)) > 11055 CTL_HA_STATUS_SUCCESS) { 11056 printf("CTL:Check Blocked error from " 11057 "ctl_ha_msg_send %d\n", 11058 isc_retval); 11059 } 11060 break; 11061 } 11062 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11063 softc = control_softc; 11064 11065 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11066 11067 /* 11068 * Check this I/O for LUN state changes that may 11069 * have happened while this command was blocked. 11070 * The LUN state may have been changed by a command 11071 * ahead of us in the queue, so we need to re-check 11072 * for any states that can be caused by SCSI 11073 * commands. 11074 */ 11075 if (ctl_scsiio_lun_check(softc, lun, entry, 11076 &cur_blocked->scsiio) == 0) { 11077 cur_blocked->io_hdr.flags |= 11078 CTL_FLAG_IS_WAS_ON_RTR; 11079 ctl_enqueue_rtr(cur_blocked); 11080 } else 11081 ctl_done(cur_blocked); 11082 break; 11083 } 11084 default: 11085 /* 11086 * This probably shouldn't happen -- we shouldn't 11087 * get CTL_ACTION_ERROR, or anything else. 11088 */ 11089 break; 11090 } 11091 } 11092 11093 return (CTL_RETVAL_COMPLETE); 11094 } 11095 11096 /* 11097 * This routine (with one exception) checks LUN flags that can be set by 11098 * commands ahead of us in the OOA queue. These flags have to be checked 11099 * when a command initially comes in, and when we pull a command off the 11100 * blocked queue and are preparing to execute it. The reason we have to 11101 * check these flags for commands on the blocked queue is that the LUN 11102 * state may have been changed by a command ahead of us while we're on the 11103 * blocked queue. 11104 * 11105 * Ordering is somewhat important with these checks, so please pay 11106 * careful attention to the placement of any new checks. 11107 */ 11108 static int 11109 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11110 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11111 { 11112 int retval; 11113 uint32_t residx; 11114 11115 retval = 0; 11116 11117 mtx_assert(&lun->lun_lock, MA_OWNED); 11118 11119 /* 11120 * If this shelf is a secondary shelf controller, we have to reject 11121 * any media access commands. 11122 */ 11123 if ((ctl_softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 11124 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 11125 ctl_set_lun_standby(ctsio); 11126 retval = 1; 11127 goto bailout; 11128 } 11129 11130 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11131 if (lun->flags & CTL_LUN_READONLY) { 11132 ctl_set_sense(ctsio, /*current_error*/ 1, 11133 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11134 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11135 retval = 1; 11136 goto bailout; 11137 } 11138 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11139 .eca_and_aen & SCP_SWP) != 0) { 11140 ctl_set_sense(ctsio, /*current_error*/ 1, 11141 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11142 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11143 retval = 1; 11144 goto bailout; 11145 } 11146 } 11147 11148 /* 11149 * Check for a reservation conflict. If this command isn't allowed 11150 * even on reserved LUNs, and if this initiator isn't the one who 11151 * reserved us, reject the command with a reservation conflict. 11152 */ 11153 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11154 if ((lun->flags & CTL_LUN_RESERVED) 11155 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11156 if (lun->res_idx != residx) { 11157 ctl_set_reservation_conflict(ctsio); 11158 retval = 1; 11159 goto bailout; 11160 } 11161 } 11162 11163 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11164 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11165 /* No reservation or command is allowed. */; 11166 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11167 (lun->res_type == SPR_TYPE_WR_EX || 11168 lun->res_type == SPR_TYPE_WR_EX_RO || 11169 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11170 /* The command is allowed for Write Exclusive resv. */; 11171 } else { 11172 /* 11173 * if we aren't registered or it's a res holder type 11174 * reservation and this isn't the res holder then set a 11175 * conflict. 11176 */ 11177 if (lun->pr_keys[residx] == 0 11178 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11179 ctl_set_reservation_conflict(ctsio); 11180 retval = 1; 11181 goto bailout; 11182 } 11183 11184 } 11185 11186 if ((lun->flags & CTL_LUN_OFFLINE) 11187 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11188 ctl_set_lun_not_ready(ctsio); 11189 retval = 1; 11190 goto bailout; 11191 } 11192 11193 /* 11194 * If the LUN is stopped, see if this particular command is allowed 11195 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11196 */ 11197 if ((lun->flags & CTL_LUN_STOPPED) 11198 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11199 /* "Logical unit not ready, initializing cmd. required" */ 11200 ctl_set_lun_stopped(ctsio); 11201 retval = 1; 11202 goto bailout; 11203 } 11204 11205 if ((lun->flags & CTL_LUN_INOPERABLE) 11206 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11207 /* "Medium format corrupted" */ 11208 ctl_set_medium_format_corrupted(ctsio); 11209 retval = 1; 11210 goto bailout; 11211 } 11212 11213 bailout: 11214 return (retval); 11215 11216 } 11217 11218 static void 11219 ctl_failover_io(union ctl_io *io, int have_lock) 11220 { 11221 ctl_set_busy(&io->scsiio); 11222 ctl_done(io); 11223 } 11224 11225 static void 11226 ctl_failover(void) 11227 { 11228 struct ctl_lun *lun; 11229 struct ctl_softc *ctl_softc; 11230 union ctl_io *next_io, *pending_io; 11231 union ctl_io *io; 11232 int lun_idx; 11233 int i; 11234 11235 ctl_softc = control_softc; 11236 11237 mtx_lock(&ctl_softc->ctl_lock); 11238 /* 11239 * Remove any cmds from the other SC from the rtr queue. These 11240 * will obviously only be for LUNs for which we're the primary. 11241 * We can't send status or get/send data for these commands. 11242 * Since they haven't been executed yet, we can just remove them. 11243 * We'll either abort them or delete them below, depending on 11244 * which HA mode we're in. 11245 */ 11246 #ifdef notyet 11247 mtx_lock(&ctl_softc->queue_lock); 11248 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11249 io != NULL; io = next_io) { 11250 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11251 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11252 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11253 ctl_io_hdr, links); 11254 } 11255 mtx_unlock(&ctl_softc->queue_lock); 11256 #endif 11257 11258 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11259 lun = ctl_softc->ctl_luns[lun_idx]; 11260 if (lun==NULL) 11261 continue; 11262 11263 /* 11264 * Processor LUNs are primary on both sides. 11265 * XXX will this always be true? 11266 */ 11267 if (lun->be_lun->lun_type == T_PROCESSOR) 11268 continue; 11269 11270 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11271 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11272 printf("FAILOVER: primary lun %d\n", lun_idx); 11273 /* 11274 * Remove all commands from the other SC. First from the 11275 * blocked queue then from the ooa queue. Once we have 11276 * removed them. Call ctl_check_blocked to see if there 11277 * is anything that can run. 11278 */ 11279 for (io = (union ctl_io *)TAILQ_FIRST( 11280 &lun->blocked_queue); io != NULL; io = next_io) { 11281 11282 next_io = (union ctl_io *)TAILQ_NEXT( 11283 &io->io_hdr, blocked_links); 11284 11285 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11286 TAILQ_REMOVE(&lun->blocked_queue, 11287 &io->io_hdr,blocked_links); 11288 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11289 TAILQ_REMOVE(&lun->ooa_queue, 11290 &io->io_hdr, ooa_links); 11291 11292 ctl_free_io(io); 11293 } 11294 } 11295 11296 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11297 io != NULL; io = next_io) { 11298 11299 next_io = (union ctl_io *)TAILQ_NEXT( 11300 &io->io_hdr, ooa_links); 11301 11302 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11303 11304 TAILQ_REMOVE(&lun->ooa_queue, 11305 &io->io_hdr, 11306 ooa_links); 11307 11308 ctl_free_io(io); 11309 } 11310 } 11311 ctl_check_blocked(lun); 11312 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11313 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11314 11315 printf("FAILOVER: primary lun %d\n", lun_idx); 11316 /* 11317 * Abort all commands from the other SC. We can't 11318 * send status back for them now. These should get 11319 * cleaned up when they are completed or come out 11320 * for a datamove operation. 11321 */ 11322 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11323 io != NULL; io = next_io) { 11324 next_io = (union ctl_io *)TAILQ_NEXT( 11325 &io->io_hdr, ooa_links); 11326 11327 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11328 io->io_hdr.flags |= CTL_FLAG_ABORT; 11329 } 11330 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11331 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11332 11333 printf("FAILOVER: secondary lun %d\n", lun_idx); 11334 11335 lun->flags |= CTL_LUN_PRIMARY_SC; 11336 11337 /* 11338 * We send all I/O that was sent to this controller 11339 * and redirected to the other side back with 11340 * busy status, and have the initiator retry it. 11341 * Figuring out how much data has been transferred, 11342 * etc. and picking up where we left off would be 11343 * very tricky. 11344 * 11345 * XXX KDM need to remove I/O from the blocked 11346 * queue as well! 11347 */ 11348 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11349 &lun->ooa_queue); pending_io != NULL; 11350 pending_io = next_io) { 11351 11352 next_io = (union ctl_io *)TAILQ_NEXT( 11353 &pending_io->io_hdr, ooa_links); 11354 11355 pending_io->io_hdr.flags &= 11356 ~CTL_FLAG_SENT_2OTHER_SC; 11357 11358 if (pending_io->io_hdr.flags & 11359 CTL_FLAG_IO_ACTIVE) { 11360 pending_io->io_hdr.flags |= 11361 CTL_FLAG_FAILOVER; 11362 } else { 11363 ctl_set_busy(&pending_io->scsiio); 11364 ctl_done(pending_io); 11365 } 11366 } 11367 11368 /* 11369 * Build Unit Attention 11370 */ 11371 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11372 lun->pending_ua[i] |= 11373 CTL_UA_ASYM_ACC_CHANGE; 11374 } 11375 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11376 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11377 printf("FAILOVER: secondary lun %d\n", lun_idx); 11378 /* 11379 * if the first io on the OOA is not on the RtR queue 11380 * add it. 11381 */ 11382 lun->flags |= CTL_LUN_PRIMARY_SC; 11383 11384 pending_io = (union ctl_io *)TAILQ_FIRST( 11385 &lun->ooa_queue); 11386 if (pending_io==NULL) { 11387 printf("Nothing on OOA queue\n"); 11388 continue; 11389 } 11390 11391 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11392 if ((pending_io->io_hdr.flags & 11393 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11394 pending_io->io_hdr.flags |= 11395 CTL_FLAG_IS_WAS_ON_RTR; 11396 ctl_enqueue_rtr(pending_io); 11397 } 11398 #if 0 11399 else 11400 { 11401 printf("Tag 0x%04x is running\n", 11402 pending_io->scsiio.tag_num); 11403 } 11404 #endif 11405 11406 next_io = (union ctl_io *)TAILQ_NEXT( 11407 &pending_io->io_hdr, ooa_links); 11408 for (pending_io=next_io; pending_io != NULL; 11409 pending_io = next_io) { 11410 pending_io->io_hdr.flags &= 11411 ~CTL_FLAG_SENT_2OTHER_SC; 11412 next_io = (union ctl_io *)TAILQ_NEXT( 11413 &pending_io->io_hdr, ooa_links); 11414 if (pending_io->io_hdr.flags & 11415 CTL_FLAG_IS_WAS_ON_RTR) { 11416 #if 0 11417 printf("Tag 0x%04x is running\n", 11418 pending_io->scsiio.tag_num); 11419 #endif 11420 continue; 11421 } 11422 11423 switch (ctl_check_ooa(lun, pending_io, 11424 (union ctl_io *)TAILQ_PREV( 11425 &pending_io->io_hdr, ctl_ooaq, 11426 ooa_links))) { 11427 11428 case CTL_ACTION_BLOCK: 11429 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11430 &pending_io->io_hdr, 11431 blocked_links); 11432 pending_io->io_hdr.flags |= 11433 CTL_FLAG_BLOCKED; 11434 break; 11435 case CTL_ACTION_PASS: 11436 case CTL_ACTION_SKIP: 11437 pending_io->io_hdr.flags |= 11438 CTL_FLAG_IS_WAS_ON_RTR; 11439 ctl_enqueue_rtr(pending_io); 11440 break; 11441 case CTL_ACTION_OVERLAP: 11442 ctl_set_overlapped_cmd( 11443 (struct ctl_scsiio *)pending_io); 11444 ctl_done(pending_io); 11445 break; 11446 case CTL_ACTION_OVERLAP_TAG: 11447 ctl_set_overlapped_tag( 11448 (struct ctl_scsiio *)pending_io, 11449 pending_io->scsiio.tag_num & 0xff); 11450 ctl_done(pending_io); 11451 break; 11452 case CTL_ACTION_ERROR: 11453 default: 11454 ctl_set_internal_failure( 11455 (struct ctl_scsiio *)pending_io, 11456 0, // sks_valid 11457 0); //retry count 11458 ctl_done(pending_io); 11459 break; 11460 } 11461 } 11462 11463 /* 11464 * Build Unit Attention 11465 */ 11466 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11467 lun->pending_ua[i] |= 11468 CTL_UA_ASYM_ACC_CHANGE; 11469 } 11470 } else { 11471 panic("Unhandled HA mode failover, LUN flags = %#x, " 11472 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11473 } 11474 } 11475 ctl_pause_rtr = 0; 11476 mtx_unlock(&ctl_softc->ctl_lock); 11477 } 11478 11479 static int 11480 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11481 { 11482 struct ctl_lun *lun; 11483 const struct ctl_cmd_entry *entry; 11484 uint32_t initidx, targ_lun; 11485 int retval; 11486 11487 retval = 0; 11488 11489 lun = NULL; 11490 11491 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11492 if ((targ_lun < CTL_MAX_LUNS) 11493 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) { 11494 /* 11495 * If the LUN is invalid, pretend that it doesn't exist. 11496 * It will go away as soon as all pending I/O has been 11497 * completed. 11498 */ 11499 mtx_lock(&lun->lun_lock); 11500 if (lun->flags & CTL_LUN_DISABLED) { 11501 mtx_unlock(&lun->lun_lock); 11502 lun = NULL; 11503 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11504 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11505 } else { 11506 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11507 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11508 lun->be_lun; 11509 if (lun->be_lun->lun_type == T_PROCESSOR) { 11510 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11511 } 11512 11513 /* 11514 * Every I/O goes into the OOA queue for a 11515 * particular LUN, and stays there until completion. 11516 */ 11517 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11518 ooa_links); 11519 } 11520 } else { 11521 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11522 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11523 } 11524 11525 /* Get command entry and return error if it is unsuppotyed. */ 11526 entry = ctl_validate_command(ctsio); 11527 if (entry == NULL) { 11528 if (lun) 11529 mtx_unlock(&lun->lun_lock); 11530 return (retval); 11531 } 11532 11533 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11534 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11535 11536 /* 11537 * Check to see whether we can send this command to LUNs that don't 11538 * exist. This should pretty much only be the case for inquiry 11539 * and request sense. Further checks, below, really require having 11540 * a LUN, so we can't really check the command anymore. Just put 11541 * it on the rtr queue. 11542 */ 11543 if (lun == NULL) { 11544 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11545 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11546 ctl_enqueue_rtr((union ctl_io *)ctsio); 11547 return (retval); 11548 } 11549 11550 ctl_set_unsupported_lun(ctsio); 11551 ctl_done((union ctl_io *)ctsio); 11552 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11553 return (retval); 11554 } else { 11555 /* 11556 * Make sure we support this particular command on this LUN. 11557 * e.g., we don't support writes to the control LUN. 11558 */ 11559 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11560 mtx_unlock(&lun->lun_lock); 11561 ctl_set_invalid_opcode(ctsio); 11562 ctl_done((union ctl_io *)ctsio); 11563 return (retval); 11564 } 11565 } 11566 11567 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11568 11569 #ifdef CTL_WITH_CA 11570 /* 11571 * If we've got a request sense, it'll clear the contingent 11572 * allegiance condition. Otherwise, if we have a CA condition for 11573 * this initiator, clear it, because it sent down a command other 11574 * than request sense. 11575 */ 11576 if ((ctsio->cdb[0] != REQUEST_SENSE) 11577 && (ctl_is_set(lun->have_ca, initidx))) 11578 ctl_clear_mask(lun->have_ca, initidx); 11579 #endif 11580 11581 /* 11582 * If the command has this flag set, it handles its own unit 11583 * attention reporting, we shouldn't do anything. Otherwise we 11584 * check for any pending unit attentions, and send them back to the 11585 * initiator. We only do this when a command initially comes in, 11586 * not when we pull it off the blocked queue. 11587 * 11588 * According to SAM-3, section 5.3.2, the order that things get 11589 * presented back to the host is basically unit attentions caused 11590 * by some sort of reset event, busy status, reservation conflicts 11591 * or task set full, and finally any other status. 11592 * 11593 * One issue here is that some of the unit attentions we report 11594 * don't fall into the "reset" category (e.g. "reported luns data 11595 * has changed"). So reporting it here, before the reservation 11596 * check, may be technically wrong. I guess the only thing to do 11597 * would be to check for and report the reset events here, and then 11598 * check for the other unit attention types after we check for a 11599 * reservation conflict. 11600 * 11601 * XXX KDM need to fix this 11602 */ 11603 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11604 ctl_ua_type ua_type; 11605 11606 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11607 scsi_sense_data_type sense_format; 11608 11609 if (lun != NULL) 11610 sense_format = (lun->flags & 11611 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11612 SSD_TYPE_FIXED; 11613 else 11614 sense_format = SSD_TYPE_FIXED; 11615 11616 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11617 &ctsio->sense_data, sense_format); 11618 if (ua_type != CTL_UA_NONE) { 11619 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11620 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11621 CTL_AUTOSENSE; 11622 ctsio->sense_len = SSD_FULL_SIZE; 11623 mtx_unlock(&lun->lun_lock); 11624 ctl_done((union ctl_io *)ctsio); 11625 return (retval); 11626 } 11627 } 11628 } 11629 11630 11631 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11632 mtx_unlock(&lun->lun_lock); 11633 ctl_done((union ctl_io *)ctsio); 11634 return (retval); 11635 } 11636 11637 /* 11638 * XXX CHD this is where we want to send IO to other side if 11639 * this LUN is secondary on this SC. We will need to make a copy 11640 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11641 * the copy we send as FROM_OTHER. 11642 * We also need to stuff the address of the original IO so we can 11643 * find it easily. Something similar will need be done on the other 11644 * side so when we are done we can find the copy. 11645 */ 11646 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11647 union ctl_ha_msg msg_info; 11648 int isc_retval; 11649 11650 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11651 11652 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11653 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11654 #if 0 11655 printf("1. ctsio %p\n", ctsio); 11656 #endif 11657 msg_info.hdr.serializing_sc = NULL; 11658 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11659 msg_info.scsi.tag_num = ctsio->tag_num; 11660 msg_info.scsi.tag_type = ctsio->tag_type; 11661 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11662 11663 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11664 11665 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11666 (void *)&msg_info, sizeof(msg_info), 0)) > 11667 CTL_HA_STATUS_SUCCESS) { 11668 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11669 isc_retval); 11670 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11671 } else { 11672 #if 0 11673 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11674 #endif 11675 } 11676 11677 /* 11678 * XXX KDM this I/O is off the incoming queue, but hasn't 11679 * been inserted on any other queue. We may need to come 11680 * up with a holding queue while we wait for serialization 11681 * so that we have an idea of what we're waiting for from 11682 * the other side. 11683 */ 11684 mtx_unlock(&lun->lun_lock); 11685 return (retval); 11686 } 11687 11688 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11689 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11690 ctl_ooaq, ooa_links))) { 11691 case CTL_ACTION_BLOCK: 11692 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11693 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11694 blocked_links); 11695 mtx_unlock(&lun->lun_lock); 11696 return (retval); 11697 case CTL_ACTION_PASS: 11698 case CTL_ACTION_SKIP: 11699 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11700 mtx_unlock(&lun->lun_lock); 11701 ctl_enqueue_rtr((union ctl_io *)ctsio); 11702 break; 11703 case CTL_ACTION_OVERLAP: 11704 mtx_unlock(&lun->lun_lock); 11705 ctl_set_overlapped_cmd(ctsio); 11706 ctl_done((union ctl_io *)ctsio); 11707 break; 11708 case CTL_ACTION_OVERLAP_TAG: 11709 mtx_unlock(&lun->lun_lock); 11710 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11711 ctl_done((union ctl_io *)ctsio); 11712 break; 11713 case CTL_ACTION_ERROR: 11714 default: 11715 mtx_unlock(&lun->lun_lock); 11716 ctl_set_internal_failure(ctsio, 11717 /*sks_valid*/ 0, 11718 /*retry_count*/ 0); 11719 ctl_done((union ctl_io *)ctsio); 11720 break; 11721 } 11722 return (retval); 11723 } 11724 11725 const struct ctl_cmd_entry * 11726 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11727 { 11728 const struct ctl_cmd_entry *entry; 11729 int service_action; 11730 11731 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11732 if (sa) 11733 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11734 if (entry->flags & CTL_CMD_FLAG_SA5) { 11735 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11736 entry = &((const struct ctl_cmd_entry *) 11737 entry->execute)[service_action]; 11738 } 11739 return (entry); 11740 } 11741 11742 const struct ctl_cmd_entry * 11743 ctl_validate_command(struct ctl_scsiio *ctsio) 11744 { 11745 const struct ctl_cmd_entry *entry; 11746 int i, sa; 11747 uint8_t diff; 11748 11749 entry = ctl_get_cmd_entry(ctsio, &sa); 11750 if (entry->execute == NULL) { 11751 if (sa) 11752 ctl_set_invalid_field(ctsio, 11753 /*sks_valid*/ 1, 11754 /*command*/ 1, 11755 /*field*/ 1, 11756 /*bit_valid*/ 1, 11757 /*bit*/ 4); 11758 else 11759 ctl_set_invalid_opcode(ctsio); 11760 ctl_done((union ctl_io *)ctsio); 11761 return (NULL); 11762 } 11763 KASSERT(entry->length > 0, 11764 ("Not defined length for command 0x%02x/0x%02x", 11765 ctsio->cdb[0], ctsio->cdb[1])); 11766 for (i = 1; i < entry->length; i++) { 11767 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11768 if (diff == 0) 11769 continue; 11770 ctl_set_invalid_field(ctsio, 11771 /*sks_valid*/ 1, 11772 /*command*/ 1, 11773 /*field*/ i, 11774 /*bit_valid*/ 1, 11775 /*bit*/ fls(diff) - 1); 11776 ctl_done((union ctl_io *)ctsio); 11777 return (NULL); 11778 } 11779 return (entry); 11780 } 11781 11782 static int 11783 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11784 { 11785 11786 switch (lun_type) { 11787 case T_PROCESSOR: 11788 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11789 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11790 return (0); 11791 break; 11792 case T_DIRECT: 11793 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11794 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11795 return (0); 11796 break; 11797 default: 11798 return (0); 11799 } 11800 return (1); 11801 } 11802 11803 static int 11804 ctl_scsiio(struct ctl_scsiio *ctsio) 11805 { 11806 int retval; 11807 const struct ctl_cmd_entry *entry; 11808 11809 retval = CTL_RETVAL_COMPLETE; 11810 11811 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11812 11813 entry = ctl_get_cmd_entry(ctsio, NULL); 11814 11815 /* 11816 * If this I/O has been aborted, just send it straight to 11817 * ctl_done() without executing it. 11818 */ 11819 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11820 ctl_done((union ctl_io *)ctsio); 11821 goto bailout; 11822 } 11823 11824 /* 11825 * All the checks should have been handled by ctl_scsiio_precheck(). 11826 * We should be clear now to just execute the I/O. 11827 */ 11828 retval = entry->execute(ctsio); 11829 11830 bailout: 11831 return (retval); 11832 } 11833 11834 /* 11835 * Since we only implement one target right now, a bus reset simply resets 11836 * our single target. 11837 */ 11838 static int 11839 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11840 { 11841 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11842 } 11843 11844 static int 11845 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11846 ctl_ua_type ua_type) 11847 { 11848 struct ctl_lun *lun; 11849 int retval; 11850 11851 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11852 union ctl_ha_msg msg_info; 11853 11854 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11855 msg_info.hdr.nexus = io->io_hdr.nexus; 11856 if (ua_type==CTL_UA_TARG_RESET) 11857 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11858 else 11859 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11860 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11861 msg_info.hdr.original_sc = NULL; 11862 msg_info.hdr.serializing_sc = NULL; 11863 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11864 (void *)&msg_info, sizeof(msg_info), 0)) { 11865 } 11866 } 11867 retval = 0; 11868 11869 mtx_lock(&ctl_softc->ctl_lock); 11870 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11871 retval += ctl_lun_reset(lun, io, ua_type); 11872 mtx_unlock(&ctl_softc->ctl_lock); 11873 11874 return (retval); 11875 } 11876 11877 /* 11878 * The LUN should always be set. The I/O is optional, and is used to 11879 * distinguish between I/Os sent by this initiator, and by other 11880 * initiators. We set unit attention for initiators other than this one. 11881 * SAM-3 is vague on this point. It does say that a unit attention should 11882 * be established for other initiators when a LUN is reset (see section 11883 * 5.7.3), but it doesn't specifically say that the unit attention should 11884 * be established for this particular initiator when a LUN is reset. Here 11885 * is the relevant text, from SAM-3 rev 8: 11886 * 11887 * 5.7.2 When a SCSI initiator port aborts its own tasks 11888 * 11889 * When a SCSI initiator port causes its own task(s) to be aborted, no 11890 * notification that the task(s) have been aborted shall be returned to 11891 * the SCSI initiator port other than the completion response for the 11892 * command or task management function action that caused the task(s) to 11893 * be aborted and notification(s) associated with related effects of the 11894 * action (e.g., a reset unit attention condition). 11895 * 11896 * XXX KDM for now, we're setting unit attention for all initiators. 11897 */ 11898 static int 11899 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11900 { 11901 union ctl_io *xio; 11902 #if 0 11903 uint32_t initindex; 11904 #endif 11905 int i; 11906 11907 mtx_lock(&lun->lun_lock); 11908 /* 11909 * Run through the OOA queue and abort each I/O. 11910 */ 11911 #if 0 11912 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11913 #endif 11914 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11915 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11916 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11917 } 11918 11919 /* 11920 * This version sets unit attention for every 11921 */ 11922 #if 0 11923 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11924 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11925 if (initindex == i) 11926 continue; 11927 lun->pending_ua[i] |= ua_type; 11928 } 11929 #endif 11930 11931 /* 11932 * A reset (any kind, really) clears reservations established with 11933 * RESERVE/RELEASE. It does not clear reservations established 11934 * with PERSISTENT RESERVE OUT, but we don't support that at the 11935 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11936 * reservations made with the RESERVE/RELEASE commands, because 11937 * those commands are obsolete in SPC-3. 11938 */ 11939 lun->flags &= ~CTL_LUN_RESERVED; 11940 11941 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11942 #ifdef CTL_WITH_CA 11943 ctl_clear_mask(lun->have_ca, i); 11944 #endif 11945 lun->pending_ua[i] |= ua_type; 11946 } 11947 mtx_unlock(&lun->lun_lock); 11948 11949 return (0); 11950 } 11951 11952 static void 11953 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11954 int other_sc) 11955 { 11956 union ctl_io *xio; 11957 11958 mtx_assert(&lun->lun_lock, MA_OWNED); 11959 11960 /* 11961 * Run through the OOA queue and attempt to find the given I/O. 11962 * The target port, initiator ID, tag type and tag number have to 11963 * match the values that we got from the initiator. If we have an 11964 * untagged command to abort, simply abort the first untagged command 11965 * we come to. We only allow one untagged command at a time of course. 11966 */ 11967 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11968 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11969 11970 if ((targ_port == UINT32_MAX || 11971 targ_port == xio->io_hdr.nexus.targ_port) && 11972 (init_id == UINT32_MAX || 11973 init_id == xio->io_hdr.nexus.initid.id)) { 11974 if (targ_port != xio->io_hdr.nexus.targ_port || 11975 init_id != xio->io_hdr.nexus.initid.id) 11976 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11977 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11978 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11979 union ctl_ha_msg msg_info; 11980 11981 msg_info.hdr.nexus = xio->io_hdr.nexus; 11982 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11983 msg_info.task.tag_num = xio->scsiio.tag_num; 11984 msg_info.task.tag_type = xio->scsiio.tag_type; 11985 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11986 msg_info.hdr.original_sc = NULL; 11987 msg_info.hdr.serializing_sc = NULL; 11988 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11989 (void *)&msg_info, sizeof(msg_info), 0); 11990 } 11991 } 11992 } 11993 } 11994 11995 static int 11996 ctl_abort_task_set(union ctl_io *io) 11997 { 11998 struct ctl_softc *softc = control_softc; 11999 struct ctl_lun *lun; 12000 uint32_t targ_lun; 12001 12002 /* 12003 * Look up the LUN. 12004 */ 12005 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12006 mtx_lock(&softc->ctl_lock); 12007 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12008 lun = softc->ctl_luns[targ_lun]; 12009 else { 12010 mtx_unlock(&softc->ctl_lock); 12011 return (1); 12012 } 12013 12014 mtx_lock(&lun->lun_lock); 12015 mtx_unlock(&softc->ctl_lock); 12016 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12017 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12018 io->io_hdr.nexus.initid.id, 12019 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12020 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12021 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12022 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12023 } 12024 mtx_unlock(&lun->lun_lock); 12025 return (0); 12026 } 12027 12028 static int 12029 ctl_i_t_nexus_reset(union ctl_io *io) 12030 { 12031 struct ctl_softc *softc = control_softc; 12032 struct ctl_lun *lun; 12033 uint32_t initindex, residx; 12034 12035 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12036 residx = ctl_get_resindex(&io->io_hdr.nexus); 12037 mtx_lock(&softc->ctl_lock); 12038 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12039 mtx_lock(&lun->lun_lock); 12040 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12041 io->io_hdr.nexus.initid.id, 12042 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12043 #ifdef CTL_WITH_CA 12044 ctl_clear_mask(lun->have_ca, initindex); 12045 #endif 12046 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12047 lun->flags &= ~CTL_LUN_RESERVED; 12048 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12049 mtx_unlock(&lun->lun_lock); 12050 } 12051 mtx_unlock(&softc->ctl_lock); 12052 return (0); 12053 } 12054 12055 static int 12056 ctl_abort_task(union ctl_io *io) 12057 { 12058 union ctl_io *xio; 12059 struct ctl_lun *lun; 12060 struct ctl_softc *ctl_softc; 12061 #if 0 12062 struct sbuf sb; 12063 char printbuf[128]; 12064 #endif 12065 int found; 12066 uint32_t targ_lun; 12067 12068 ctl_softc = control_softc; 12069 found = 0; 12070 12071 /* 12072 * Look up the LUN. 12073 */ 12074 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12075 mtx_lock(&ctl_softc->ctl_lock); 12076 if ((targ_lun < CTL_MAX_LUNS) 12077 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12078 lun = ctl_softc->ctl_luns[targ_lun]; 12079 else { 12080 mtx_unlock(&ctl_softc->ctl_lock); 12081 return (1); 12082 } 12083 12084 #if 0 12085 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12086 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12087 #endif 12088 12089 mtx_lock(&lun->lun_lock); 12090 mtx_unlock(&ctl_softc->ctl_lock); 12091 /* 12092 * Run through the OOA queue and attempt to find the given I/O. 12093 * The target port, initiator ID, tag type and tag number have to 12094 * match the values that we got from the initiator. If we have an 12095 * untagged command to abort, simply abort the first untagged command 12096 * we come to. We only allow one untagged command at a time of course. 12097 */ 12098 #if 0 12099 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12100 #endif 12101 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12102 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12103 #if 0 12104 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12105 12106 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12107 lun->lun, xio->scsiio.tag_num, 12108 xio->scsiio.tag_type, 12109 (xio->io_hdr.blocked_links.tqe_prev 12110 == NULL) ? "" : " BLOCKED", 12111 (xio->io_hdr.flags & 12112 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12113 (xio->io_hdr.flags & 12114 CTL_FLAG_ABORT) ? " ABORT" : "", 12115 (xio->io_hdr.flags & 12116 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12117 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12118 sbuf_finish(&sb); 12119 printf("%s\n", sbuf_data(&sb)); 12120 #endif 12121 12122 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12123 && (xio->io_hdr.nexus.initid.id == 12124 io->io_hdr.nexus.initid.id)) { 12125 /* 12126 * If the abort says that the task is untagged, the 12127 * task in the queue must be untagged. Otherwise, 12128 * we just check to see whether the tag numbers 12129 * match. This is because the QLogic firmware 12130 * doesn't pass back the tag type in an abort 12131 * request. 12132 */ 12133 #if 0 12134 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12135 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12136 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12137 #endif 12138 /* 12139 * XXX KDM we've got problems with FC, because it 12140 * doesn't send down a tag type with aborts. So we 12141 * can only really go by the tag number... 12142 * This may cause problems with parallel SCSI. 12143 * Need to figure that out!! 12144 */ 12145 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12146 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12147 found = 1; 12148 if ((io->io_hdr.flags & 12149 CTL_FLAG_FROM_OTHER_SC) == 0 && 12150 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12151 union ctl_ha_msg msg_info; 12152 12153 io->io_hdr.flags |= 12154 CTL_FLAG_SENT_2OTHER_SC; 12155 msg_info.hdr.nexus = io->io_hdr.nexus; 12156 msg_info.task.task_action = 12157 CTL_TASK_ABORT_TASK; 12158 msg_info.task.tag_num = 12159 io->taskio.tag_num; 12160 msg_info.task.tag_type = 12161 io->taskio.tag_type; 12162 msg_info.hdr.msg_type = 12163 CTL_MSG_MANAGE_TASKS; 12164 msg_info.hdr.original_sc = NULL; 12165 msg_info.hdr.serializing_sc = NULL; 12166 #if 0 12167 printf("Sent Abort to other side\n"); 12168 #endif 12169 if (CTL_HA_STATUS_SUCCESS != 12170 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12171 (void *)&msg_info, 12172 sizeof(msg_info), 0)) { 12173 } 12174 } 12175 #if 0 12176 printf("ctl_abort_task: found I/O to abort\n"); 12177 #endif 12178 break; 12179 } 12180 } 12181 } 12182 mtx_unlock(&lun->lun_lock); 12183 12184 if (found == 0) { 12185 /* 12186 * This isn't really an error. It's entirely possible for 12187 * the abort and command completion to cross on the wire. 12188 * This is more of an informative/diagnostic error. 12189 */ 12190 #if 0 12191 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12192 "%d:%d:%d:%d tag %d type %d\n", 12193 io->io_hdr.nexus.initid.id, 12194 io->io_hdr.nexus.targ_port, 12195 io->io_hdr.nexus.targ_target.id, 12196 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12197 io->taskio.tag_type); 12198 #endif 12199 } 12200 return (0); 12201 } 12202 12203 static void 12204 ctl_run_task(union ctl_io *io) 12205 { 12206 struct ctl_softc *ctl_softc = control_softc; 12207 int retval = 1; 12208 const char *task_desc; 12209 12210 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12211 12212 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12213 ("ctl_run_task: Unextected io_type %d\n", 12214 io->io_hdr.io_type)); 12215 12216 task_desc = ctl_scsi_task_string(&io->taskio); 12217 if (task_desc != NULL) { 12218 #ifdef NEEDTOPORT 12219 csevent_log(CSC_CTL | CSC_SHELF_SW | 12220 CTL_TASK_REPORT, 12221 csevent_LogType_Trace, 12222 csevent_Severity_Information, 12223 csevent_AlertLevel_Green, 12224 csevent_FRU_Firmware, 12225 csevent_FRU_Unknown, 12226 "CTL: received task: %s",task_desc); 12227 #endif 12228 } else { 12229 #ifdef NEEDTOPORT 12230 csevent_log(CSC_CTL | CSC_SHELF_SW | 12231 CTL_TASK_REPORT, 12232 csevent_LogType_Trace, 12233 csevent_Severity_Information, 12234 csevent_AlertLevel_Green, 12235 csevent_FRU_Firmware, 12236 csevent_FRU_Unknown, 12237 "CTL: received unknown task " 12238 "type: %d (%#x)", 12239 io->taskio.task_action, 12240 io->taskio.task_action); 12241 #endif 12242 } 12243 switch (io->taskio.task_action) { 12244 case CTL_TASK_ABORT_TASK: 12245 retval = ctl_abort_task(io); 12246 break; 12247 case CTL_TASK_ABORT_TASK_SET: 12248 case CTL_TASK_CLEAR_TASK_SET: 12249 retval = ctl_abort_task_set(io); 12250 break; 12251 case CTL_TASK_CLEAR_ACA: 12252 break; 12253 case CTL_TASK_I_T_NEXUS_RESET: 12254 retval = ctl_i_t_nexus_reset(io); 12255 break; 12256 case CTL_TASK_LUN_RESET: { 12257 struct ctl_lun *lun; 12258 uint32_t targ_lun; 12259 12260 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12261 mtx_lock(&ctl_softc->ctl_lock); 12262 if ((targ_lun < CTL_MAX_LUNS) 12263 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12264 lun = ctl_softc->ctl_luns[targ_lun]; 12265 else { 12266 mtx_unlock(&ctl_softc->ctl_lock); 12267 retval = 1; 12268 break; 12269 } 12270 12271 if (!(io->io_hdr.flags & 12272 CTL_FLAG_FROM_OTHER_SC)) { 12273 union ctl_ha_msg msg_info; 12274 12275 io->io_hdr.flags |= 12276 CTL_FLAG_SENT_2OTHER_SC; 12277 msg_info.hdr.msg_type = 12278 CTL_MSG_MANAGE_TASKS; 12279 msg_info.hdr.nexus = io->io_hdr.nexus; 12280 msg_info.task.task_action = 12281 CTL_TASK_LUN_RESET; 12282 msg_info.hdr.original_sc = NULL; 12283 msg_info.hdr.serializing_sc = NULL; 12284 if (CTL_HA_STATUS_SUCCESS != 12285 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12286 (void *)&msg_info, 12287 sizeof(msg_info), 0)) { 12288 } 12289 } 12290 12291 retval = ctl_lun_reset(lun, io, 12292 CTL_UA_LUN_RESET); 12293 mtx_unlock(&ctl_softc->ctl_lock); 12294 break; 12295 } 12296 case CTL_TASK_TARGET_RESET: 12297 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12298 break; 12299 case CTL_TASK_BUS_RESET: 12300 retval = ctl_bus_reset(ctl_softc, io); 12301 break; 12302 case CTL_TASK_PORT_LOGIN: 12303 break; 12304 case CTL_TASK_PORT_LOGOUT: 12305 break; 12306 default: 12307 printf("ctl_run_task: got unknown task management event %d\n", 12308 io->taskio.task_action); 12309 break; 12310 } 12311 if (retval == 0) 12312 io->io_hdr.status = CTL_SUCCESS; 12313 else 12314 io->io_hdr.status = CTL_ERROR; 12315 ctl_done(io); 12316 } 12317 12318 /* 12319 * For HA operation. Handle commands that come in from the other 12320 * controller. 12321 */ 12322 static void 12323 ctl_handle_isc(union ctl_io *io) 12324 { 12325 int free_io; 12326 struct ctl_lun *lun; 12327 struct ctl_softc *ctl_softc; 12328 uint32_t targ_lun; 12329 12330 ctl_softc = control_softc; 12331 12332 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12333 lun = ctl_softc->ctl_luns[targ_lun]; 12334 12335 switch (io->io_hdr.msg_type) { 12336 case CTL_MSG_SERIALIZE: 12337 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12338 break; 12339 case CTL_MSG_R2R: { 12340 const struct ctl_cmd_entry *entry; 12341 12342 /* 12343 * This is only used in SER_ONLY mode. 12344 */ 12345 free_io = 0; 12346 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12347 mtx_lock(&lun->lun_lock); 12348 if (ctl_scsiio_lun_check(ctl_softc, lun, 12349 entry, (struct ctl_scsiio *)io) != 0) { 12350 mtx_unlock(&lun->lun_lock); 12351 ctl_done(io); 12352 break; 12353 } 12354 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12355 mtx_unlock(&lun->lun_lock); 12356 ctl_enqueue_rtr(io); 12357 break; 12358 } 12359 case CTL_MSG_FINISH_IO: 12360 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12361 free_io = 0; 12362 ctl_done(io); 12363 } else { 12364 free_io = 1; 12365 mtx_lock(&lun->lun_lock); 12366 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12367 ooa_links); 12368 ctl_check_blocked(lun); 12369 mtx_unlock(&lun->lun_lock); 12370 } 12371 break; 12372 case CTL_MSG_PERS_ACTION: 12373 ctl_hndl_per_res_out_on_other_sc( 12374 (union ctl_ha_msg *)&io->presio.pr_msg); 12375 free_io = 1; 12376 break; 12377 case CTL_MSG_BAD_JUJU: 12378 free_io = 0; 12379 ctl_done(io); 12380 break; 12381 case CTL_MSG_DATAMOVE: 12382 /* Only used in XFER mode */ 12383 free_io = 0; 12384 ctl_datamove_remote(io); 12385 break; 12386 case CTL_MSG_DATAMOVE_DONE: 12387 /* Only used in XFER mode */ 12388 free_io = 0; 12389 io->scsiio.be_move_done(io); 12390 break; 12391 default: 12392 free_io = 1; 12393 printf("%s: Invalid message type %d\n", 12394 __func__, io->io_hdr.msg_type); 12395 break; 12396 } 12397 if (free_io) 12398 ctl_free_io(io); 12399 12400 } 12401 12402 12403 /* 12404 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12405 * there is no match. 12406 */ 12407 static ctl_lun_error_pattern 12408 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12409 { 12410 const struct ctl_cmd_entry *entry; 12411 ctl_lun_error_pattern filtered_pattern, pattern; 12412 12413 pattern = desc->error_pattern; 12414 12415 /* 12416 * XXX KDM we need more data passed into this function to match a 12417 * custom pattern, and we actually need to implement custom pattern 12418 * matching. 12419 */ 12420 if (pattern & CTL_LUN_PAT_CMD) 12421 return (CTL_LUN_PAT_CMD); 12422 12423 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12424 return (CTL_LUN_PAT_ANY); 12425 12426 entry = ctl_get_cmd_entry(ctsio, NULL); 12427 12428 filtered_pattern = entry->pattern & pattern; 12429 12430 /* 12431 * If the user requested specific flags in the pattern (e.g. 12432 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12433 * flags. 12434 * 12435 * If the user did not specify any flags, it doesn't matter whether 12436 * or not the command supports the flags. 12437 */ 12438 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12439 (pattern & ~CTL_LUN_PAT_MASK)) 12440 return (CTL_LUN_PAT_NONE); 12441 12442 /* 12443 * If the user asked for a range check, see if the requested LBA 12444 * range overlaps with this command's LBA range. 12445 */ 12446 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12447 uint64_t lba1; 12448 uint64_t len1; 12449 ctl_action action; 12450 int retval; 12451 12452 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12453 if (retval != 0) 12454 return (CTL_LUN_PAT_NONE); 12455 12456 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12457 desc->lba_range.len); 12458 /* 12459 * A "pass" means that the LBA ranges don't overlap, so 12460 * this doesn't match the user's range criteria. 12461 */ 12462 if (action == CTL_ACTION_PASS) 12463 return (CTL_LUN_PAT_NONE); 12464 } 12465 12466 return (filtered_pattern); 12467 } 12468 12469 static void 12470 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12471 { 12472 struct ctl_error_desc *desc, *desc2; 12473 12474 mtx_assert(&lun->lun_lock, MA_OWNED); 12475 12476 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12477 ctl_lun_error_pattern pattern; 12478 /* 12479 * Check to see whether this particular command matches 12480 * the pattern in the descriptor. 12481 */ 12482 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12483 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12484 continue; 12485 12486 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12487 case CTL_LUN_INJ_ABORTED: 12488 ctl_set_aborted(&io->scsiio); 12489 break; 12490 case CTL_LUN_INJ_MEDIUM_ERR: 12491 ctl_set_medium_error(&io->scsiio); 12492 break; 12493 case CTL_LUN_INJ_UA: 12494 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12495 * OCCURRED */ 12496 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12497 break; 12498 case CTL_LUN_INJ_CUSTOM: 12499 /* 12500 * We're assuming the user knows what he is doing. 12501 * Just copy the sense information without doing 12502 * checks. 12503 */ 12504 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12505 ctl_min(sizeof(desc->custom_sense), 12506 sizeof(io->scsiio.sense_data))); 12507 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12508 io->scsiio.sense_len = SSD_FULL_SIZE; 12509 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12510 break; 12511 case CTL_LUN_INJ_NONE: 12512 default: 12513 /* 12514 * If this is an error injection type we don't know 12515 * about, clear the continuous flag (if it is set) 12516 * so it will get deleted below. 12517 */ 12518 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12519 break; 12520 } 12521 /* 12522 * By default, each error injection action is a one-shot 12523 */ 12524 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12525 continue; 12526 12527 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12528 12529 free(desc, M_CTL); 12530 } 12531 } 12532 12533 #ifdef CTL_IO_DELAY 12534 static void 12535 ctl_datamove_timer_wakeup(void *arg) 12536 { 12537 union ctl_io *io; 12538 12539 io = (union ctl_io *)arg; 12540 12541 ctl_datamove(io); 12542 } 12543 #endif /* CTL_IO_DELAY */ 12544 12545 void 12546 ctl_datamove(union ctl_io *io) 12547 { 12548 void (*fe_datamove)(union ctl_io *io); 12549 12550 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12551 12552 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12553 12554 #ifdef CTL_TIME_IO 12555 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12556 char str[256]; 12557 char path_str[64]; 12558 struct sbuf sb; 12559 12560 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12561 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12562 12563 sbuf_cat(&sb, path_str); 12564 switch (io->io_hdr.io_type) { 12565 case CTL_IO_SCSI: 12566 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12567 sbuf_printf(&sb, "\n"); 12568 sbuf_cat(&sb, path_str); 12569 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12570 io->scsiio.tag_num, io->scsiio.tag_type); 12571 break; 12572 case CTL_IO_TASK: 12573 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12574 "Tag Type: %d\n", io->taskio.task_action, 12575 io->taskio.tag_num, io->taskio.tag_type); 12576 break; 12577 default: 12578 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12579 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12580 break; 12581 } 12582 sbuf_cat(&sb, path_str); 12583 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12584 (intmax_t)time_uptime - io->io_hdr.start_time); 12585 sbuf_finish(&sb); 12586 printf("%s", sbuf_data(&sb)); 12587 } 12588 #endif /* CTL_TIME_IO */ 12589 12590 #ifdef CTL_IO_DELAY 12591 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12592 struct ctl_lun *lun; 12593 12594 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12595 12596 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12597 } else { 12598 struct ctl_lun *lun; 12599 12600 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12601 if ((lun != NULL) 12602 && (lun->delay_info.datamove_delay > 0)) { 12603 struct callout *callout; 12604 12605 callout = (struct callout *)&io->io_hdr.timer_bytes; 12606 callout_init(callout, /*mpsafe*/ 1); 12607 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12608 callout_reset(callout, 12609 lun->delay_info.datamove_delay * hz, 12610 ctl_datamove_timer_wakeup, io); 12611 if (lun->delay_info.datamove_type == 12612 CTL_DELAY_TYPE_ONESHOT) 12613 lun->delay_info.datamove_delay = 0; 12614 return; 12615 } 12616 } 12617 #endif 12618 12619 /* 12620 * This command has been aborted. Set the port status, so we fail 12621 * the data move. 12622 */ 12623 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12624 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12625 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12626 io->io_hdr.nexus.targ_port, 12627 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12628 io->io_hdr.nexus.targ_lun); 12629 io->io_hdr.port_status = 31337; 12630 /* 12631 * Note that the backend, in this case, will get the 12632 * callback in its context. In other cases it may get 12633 * called in the frontend's interrupt thread context. 12634 */ 12635 io->scsiio.be_move_done(io); 12636 return; 12637 } 12638 12639 /* Don't confuse frontend with zero length data move. */ 12640 if (io->scsiio.kern_data_len == 0) { 12641 io->scsiio.be_move_done(io); 12642 return; 12643 } 12644 12645 /* 12646 * If we're in XFER mode and this I/O is from the other shelf 12647 * controller, we need to send the DMA to the other side to 12648 * actually transfer the data to/from the host. In serialize only 12649 * mode the transfer happens below CTL and ctl_datamove() is only 12650 * called on the machine that originally received the I/O. 12651 */ 12652 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12653 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12654 union ctl_ha_msg msg; 12655 uint32_t sg_entries_sent; 12656 int do_sg_copy; 12657 int i; 12658 12659 memset(&msg, 0, sizeof(msg)); 12660 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12661 msg.hdr.original_sc = io->io_hdr.original_sc; 12662 msg.hdr.serializing_sc = io; 12663 msg.hdr.nexus = io->io_hdr.nexus; 12664 msg.dt.flags = io->io_hdr.flags; 12665 /* 12666 * We convert everything into a S/G list here. We can't 12667 * pass by reference, only by value between controllers. 12668 * So we can't pass a pointer to the S/G list, only as many 12669 * S/G entries as we can fit in here. If it's possible for 12670 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12671 * then we need to break this up into multiple transfers. 12672 */ 12673 if (io->scsiio.kern_sg_entries == 0) { 12674 msg.dt.kern_sg_entries = 1; 12675 /* 12676 * If this is in cached memory, flush the cache 12677 * before we send the DMA request to the other 12678 * controller. We want to do this in either the 12679 * read or the write case. The read case is 12680 * straightforward. In the write case, we want to 12681 * make sure nothing is in the local cache that 12682 * could overwrite the DMAed data. 12683 */ 12684 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12685 /* 12686 * XXX KDM use bus_dmamap_sync() here. 12687 */ 12688 } 12689 12690 /* 12691 * Convert to a physical address if this is a 12692 * virtual address. 12693 */ 12694 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12695 msg.dt.sg_list[0].addr = 12696 io->scsiio.kern_data_ptr; 12697 } else { 12698 /* 12699 * XXX KDM use busdma here! 12700 */ 12701 #if 0 12702 msg.dt.sg_list[0].addr = (void *) 12703 vtophys(io->scsiio.kern_data_ptr); 12704 #endif 12705 } 12706 12707 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12708 do_sg_copy = 0; 12709 } else { 12710 struct ctl_sg_entry *sgl; 12711 12712 do_sg_copy = 1; 12713 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12714 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12715 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12716 /* 12717 * XXX KDM use bus_dmamap_sync() here. 12718 */ 12719 } 12720 } 12721 12722 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12723 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12724 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12725 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12726 msg.dt.sg_sequence = 0; 12727 12728 /* 12729 * Loop until we've sent all of the S/G entries. On the 12730 * other end, we'll recompose these S/G entries into one 12731 * contiguous list before passing it to the 12732 */ 12733 for (sg_entries_sent = 0; sg_entries_sent < 12734 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12735 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12736 sizeof(msg.dt.sg_list[0])), 12737 msg.dt.kern_sg_entries - sg_entries_sent); 12738 12739 if (do_sg_copy != 0) { 12740 struct ctl_sg_entry *sgl; 12741 int j; 12742 12743 sgl = (struct ctl_sg_entry *) 12744 io->scsiio.kern_data_ptr; 12745 /* 12746 * If this is in cached memory, flush the cache 12747 * before we send the DMA request to the other 12748 * controller. We want to do this in either 12749 * the * read or the write case. The read 12750 * case is straightforward. In the write 12751 * case, we want to make sure nothing is 12752 * in the local cache that could overwrite 12753 * the DMAed data. 12754 */ 12755 12756 for (i = sg_entries_sent, j = 0; 12757 i < msg.dt.cur_sg_entries; i++, j++) { 12758 if ((io->io_hdr.flags & 12759 CTL_FLAG_NO_DATASYNC) == 0) { 12760 /* 12761 * XXX KDM use bus_dmamap_sync() 12762 */ 12763 } 12764 if ((io->io_hdr.flags & 12765 CTL_FLAG_BUS_ADDR) == 0) { 12766 /* 12767 * XXX KDM use busdma. 12768 */ 12769 #if 0 12770 msg.dt.sg_list[j].addr =(void *) 12771 vtophys(sgl[i].addr); 12772 #endif 12773 } else { 12774 msg.dt.sg_list[j].addr = 12775 sgl[i].addr; 12776 } 12777 msg.dt.sg_list[j].len = sgl[i].len; 12778 } 12779 } 12780 12781 sg_entries_sent += msg.dt.cur_sg_entries; 12782 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12783 msg.dt.sg_last = 1; 12784 else 12785 msg.dt.sg_last = 0; 12786 12787 /* 12788 * XXX KDM drop and reacquire the lock here? 12789 */ 12790 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12791 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12792 /* 12793 * XXX do something here. 12794 */ 12795 } 12796 12797 msg.dt.sent_sg_entries = sg_entries_sent; 12798 } 12799 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12800 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12801 ctl_failover_io(io, /*have_lock*/ 0); 12802 12803 } else { 12804 12805 /* 12806 * Lookup the fe_datamove() function for this particular 12807 * front end. 12808 */ 12809 fe_datamove = 12810 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12811 12812 fe_datamove(io); 12813 } 12814 } 12815 12816 static void 12817 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12818 { 12819 union ctl_ha_msg msg; 12820 int isc_status; 12821 12822 memset(&msg, 0, sizeof(msg)); 12823 12824 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12825 msg.hdr.original_sc = io; 12826 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12827 msg.hdr.nexus = io->io_hdr.nexus; 12828 msg.hdr.status = io->io_hdr.status; 12829 msg.scsi.tag_num = io->scsiio.tag_num; 12830 msg.scsi.tag_type = io->scsiio.tag_type; 12831 msg.scsi.scsi_status = io->scsiio.scsi_status; 12832 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12833 sizeof(io->scsiio.sense_data)); 12834 msg.scsi.sense_len = io->scsiio.sense_len; 12835 msg.scsi.sense_residual = io->scsiio.sense_residual; 12836 msg.scsi.fetd_status = io->io_hdr.port_status; 12837 msg.scsi.residual = io->scsiio.residual; 12838 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12839 12840 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12841 ctl_failover_io(io, /*have_lock*/ have_lock); 12842 return; 12843 } 12844 12845 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12846 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12847 /* XXX do something if this fails */ 12848 } 12849 12850 } 12851 12852 /* 12853 * The DMA to the remote side is done, now we need to tell the other side 12854 * we're done so it can continue with its data movement. 12855 */ 12856 static void 12857 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12858 { 12859 union ctl_io *io; 12860 12861 io = rq->context; 12862 12863 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12864 printf("%s: ISC DMA write failed with error %d", __func__, 12865 rq->ret); 12866 ctl_set_internal_failure(&io->scsiio, 12867 /*sks_valid*/ 1, 12868 /*retry_count*/ rq->ret); 12869 } 12870 12871 ctl_dt_req_free(rq); 12872 12873 /* 12874 * In this case, we had to malloc the memory locally. Free it. 12875 */ 12876 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12877 int i; 12878 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12879 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12880 } 12881 /* 12882 * The data is in local and remote memory, so now we need to send 12883 * status (good or back) back to the other side. 12884 */ 12885 ctl_send_datamove_done(io, /*have_lock*/ 0); 12886 } 12887 12888 /* 12889 * We've moved the data from the host/controller into local memory. Now we 12890 * need to push it over to the remote controller's memory. 12891 */ 12892 static int 12893 ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12894 { 12895 int retval; 12896 12897 retval = 0; 12898 12899 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12900 ctl_datamove_remote_write_cb); 12901 12902 return (retval); 12903 } 12904 12905 static void 12906 ctl_datamove_remote_write(union ctl_io *io) 12907 { 12908 int retval; 12909 void (*fe_datamove)(union ctl_io *io); 12910 12911 /* 12912 * - Get the data from the host/HBA into local memory. 12913 * - DMA memory from the local controller to the remote controller. 12914 * - Send status back to the remote controller. 12915 */ 12916 12917 retval = ctl_datamove_remote_sgl_setup(io); 12918 if (retval != 0) 12919 return; 12920 12921 /* Switch the pointer over so the FETD knows what to do */ 12922 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12923 12924 /* 12925 * Use a custom move done callback, since we need to send completion 12926 * back to the other controller, not to the backend on this side. 12927 */ 12928 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12929 12930 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12931 12932 fe_datamove(io); 12933 12934 return; 12935 12936 } 12937 12938 static int 12939 ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12940 { 12941 #if 0 12942 char str[256]; 12943 char path_str[64]; 12944 struct sbuf sb; 12945 #endif 12946 12947 /* 12948 * In this case, we had to malloc the memory locally. Free it. 12949 */ 12950 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12951 int i; 12952 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12953 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12954 } 12955 12956 #if 0 12957 scsi_path_string(io, path_str, sizeof(path_str)); 12958 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12959 sbuf_cat(&sb, path_str); 12960 scsi_command_string(&io->scsiio, NULL, &sb); 12961 sbuf_printf(&sb, "\n"); 12962 sbuf_cat(&sb, path_str); 12963 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12964 io->scsiio.tag_num, io->scsiio.tag_type); 12965 sbuf_cat(&sb, path_str); 12966 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12967 io->io_hdr.flags, io->io_hdr.status); 12968 sbuf_finish(&sb); 12969 printk("%s", sbuf_data(&sb)); 12970 #endif 12971 12972 12973 /* 12974 * The read is done, now we need to send status (good or bad) back 12975 * to the other side. 12976 */ 12977 ctl_send_datamove_done(io, /*have_lock*/ 0); 12978 12979 return (0); 12980 } 12981 12982 static void 12983 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12984 { 12985 union ctl_io *io; 12986 void (*fe_datamove)(union ctl_io *io); 12987 12988 io = rq->context; 12989 12990 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12991 printf("%s: ISC DMA read failed with error %d", __func__, 12992 rq->ret); 12993 ctl_set_internal_failure(&io->scsiio, 12994 /*sks_valid*/ 1, 12995 /*retry_count*/ rq->ret); 12996 } 12997 12998 ctl_dt_req_free(rq); 12999 13000 /* Switch the pointer over so the FETD knows what to do */ 13001 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13002 13003 /* 13004 * Use a custom move done callback, since we need to send completion 13005 * back to the other controller, not to the backend on this side. 13006 */ 13007 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13008 13009 /* XXX KDM add checks like the ones in ctl_datamove? */ 13010 13011 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13012 13013 fe_datamove(io); 13014 } 13015 13016 static int 13017 ctl_datamove_remote_sgl_setup(union ctl_io *io) 13018 { 13019 struct ctl_sg_entry *local_sglist, *remote_sglist; 13020 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13021 struct ctl_softc *softc; 13022 int retval; 13023 int i; 13024 13025 retval = 0; 13026 softc = control_softc; 13027 13028 local_sglist = io->io_hdr.local_sglist; 13029 local_dma_sglist = io->io_hdr.local_dma_sglist; 13030 remote_sglist = io->io_hdr.remote_sglist; 13031 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13032 13033 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13034 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13035 local_sglist[i].len = remote_sglist[i].len; 13036 13037 /* 13038 * XXX Detect the situation where the RS-level I/O 13039 * redirector on the other side has already read the 13040 * data off of the AOR RS on this side, and 13041 * transferred it to remote (mirror) memory on the 13042 * other side. Since we already have the data in 13043 * memory here, we just need to use it. 13044 * 13045 * XXX KDM this can probably be removed once we 13046 * get the cache device code in and take the 13047 * current AOR implementation out. 13048 */ 13049 #ifdef NEEDTOPORT 13050 if ((remote_sglist[i].addr >= 13051 (void *)vtophys(softc->mirr->addr)) 13052 && (remote_sglist[i].addr < 13053 ((void *)vtophys(softc->mirr->addr) + 13054 CacheMirrorOffset))) { 13055 local_sglist[i].addr = remote_sglist[i].addr - 13056 CacheMirrorOffset; 13057 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13058 CTL_FLAG_DATA_IN) 13059 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13060 } else { 13061 local_sglist[i].addr = remote_sglist[i].addr + 13062 CacheMirrorOffset; 13063 } 13064 #endif 13065 #if 0 13066 printf("%s: local %p, remote %p, len %d\n", 13067 __func__, local_sglist[i].addr, 13068 remote_sglist[i].addr, local_sglist[i].len); 13069 #endif 13070 } 13071 } else { 13072 uint32_t len_to_go; 13073 13074 /* 13075 * In this case, we don't have automatically allocated 13076 * memory for this I/O on this controller. This typically 13077 * happens with internal CTL I/O -- e.g. inquiry, mode 13078 * sense, etc. Anything coming from RAIDCore will have 13079 * a mirror area available. 13080 */ 13081 len_to_go = io->scsiio.kern_data_len; 13082 13083 /* 13084 * Clear the no datasync flag, we have to use malloced 13085 * buffers. 13086 */ 13087 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13088 13089 /* 13090 * The difficult thing here is that the size of the various 13091 * S/G segments may be different than the size from the 13092 * remote controller. That'll make it harder when DMAing 13093 * the data back to the other side. 13094 */ 13095 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13096 sizeof(io->io_hdr.remote_sglist[0])) && 13097 (len_to_go > 0); i++) { 13098 local_sglist[i].len = ctl_min(len_to_go, 131072); 13099 CTL_SIZE_8B(local_dma_sglist[i].len, 13100 local_sglist[i].len); 13101 local_sglist[i].addr = 13102 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13103 13104 local_dma_sglist[i].addr = local_sglist[i].addr; 13105 13106 if (local_sglist[i].addr == NULL) { 13107 int j; 13108 13109 printf("malloc failed for %zd bytes!", 13110 local_dma_sglist[i].len); 13111 for (j = 0; j < i; j++) { 13112 free(local_sglist[j].addr, M_CTL); 13113 } 13114 ctl_set_internal_failure(&io->scsiio, 13115 /*sks_valid*/ 1, 13116 /*retry_count*/ 4857); 13117 retval = 1; 13118 goto bailout_error; 13119 13120 } 13121 /* XXX KDM do we need a sync here? */ 13122 13123 len_to_go -= local_sglist[i].len; 13124 } 13125 /* 13126 * Reset the number of S/G entries accordingly. The 13127 * original number of S/G entries is available in 13128 * rem_sg_entries. 13129 */ 13130 io->scsiio.kern_sg_entries = i; 13131 13132 #if 0 13133 printf("%s: kern_sg_entries = %d\n", __func__, 13134 io->scsiio.kern_sg_entries); 13135 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13136 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13137 local_sglist[i].addr, local_sglist[i].len, 13138 local_dma_sglist[i].len); 13139 #endif 13140 } 13141 13142 13143 return (retval); 13144 13145 bailout_error: 13146 13147 ctl_send_datamove_done(io, /*have_lock*/ 0); 13148 13149 return (retval); 13150 } 13151 13152 static int 13153 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13154 ctl_ha_dt_cb callback) 13155 { 13156 struct ctl_ha_dt_req *rq; 13157 struct ctl_sg_entry *remote_sglist, *local_sglist; 13158 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13159 uint32_t local_used, remote_used, total_used; 13160 int retval; 13161 int i, j; 13162 13163 retval = 0; 13164 13165 rq = ctl_dt_req_alloc(); 13166 13167 /* 13168 * If we failed to allocate the request, and if the DMA didn't fail 13169 * anyway, set busy status. This is just a resource allocation 13170 * failure. 13171 */ 13172 if ((rq == NULL) 13173 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13174 ctl_set_busy(&io->scsiio); 13175 13176 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13177 13178 if (rq != NULL) 13179 ctl_dt_req_free(rq); 13180 13181 /* 13182 * The data move failed. We need to return status back 13183 * to the other controller. No point in trying to DMA 13184 * data to the remote controller. 13185 */ 13186 13187 ctl_send_datamove_done(io, /*have_lock*/ 0); 13188 13189 retval = 1; 13190 13191 goto bailout; 13192 } 13193 13194 local_sglist = io->io_hdr.local_sglist; 13195 local_dma_sglist = io->io_hdr.local_dma_sglist; 13196 remote_sglist = io->io_hdr.remote_sglist; 13197 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13198 local_used = 0; 13199 remote_used = 0; 13200 total_used = 0; 13201 13202 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13203 rq->ret = CTL_HA_STATUS_SUCCESS; 13204 rq->context = io; 13205 callback(rq); 13206 goto bailout; 13207 } 13208 13209 /* 13210 * Pull/push the data over the wire from/to the other controller. 13211 * This takes into account the possibility that the local and 13212 * remote sglists may not be identical in terms of the size of 13213 * the elements and the number of elements. 13214 * 13215 * One fundamental assumption here is that the length allocated for 13216 * both the local and remote sglists is identical. Otherwise, we've 13217 * essentially got a coding error of some sort. 13218 */ 13219 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13220 int isc_ret; 13221 uint32_t cur_len, dma_length; 13222 uint8_t *tmp_ptr; 13223 13224 rq->id = CTL_HA_DATA_CTL; 13225 rq->command = command; 13226 rq->context = io; 13227 13228 /* 13229 * Both pointers should be aligned. But it is possible 13230 * that the allocation length is not. They should both 13231 * also have enough slack left over at the end, though, 13232 * to round up to the next 8 byte boundary. 13233 */ 13234 cur_len = ctl_min(local_sglist[i].len - local_used, 13235 remote_sglist[j].len - remote_used); 13236 13237 /* 13238 * In this case, we have a size issue and need to decrease 13239 * the size, except in the case where we actually have less 13240 * than 8 bytes left. In that case, we need to increase 13241 * the DMA length to get the last bit. 13242 */ 13243 if ((cur_len & 0x7) != 0) { 13244 if (cur_len > 0x7) { 13245 cur_len = cur_len - (cur_len & 0x7); 13246 dma_length = cur_len; 13247 } else { 13248 CTL_SIZE_8B(dma_length, cur_len); 13249 } 13250 13251 } else 13252 dma_length = cur_len; 13253 13254 /* 13255 * If we had to allocate memory for this I/O, instead of using 13256 * the non-cached mirror memory, we'll need to flush the cache 13257 * before trying to DMA to the other controller. 13258 * 13259 * We could end up doing this multiple times for the same 13260 * segment if we have a larger local segment than remote 13261 * segment. That shouldn't be an issue. 13262 */ 13263 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13264 /* 13265 * XXX KDM use bus_dmamap_sync() here. 13266 */ 13267 } 13268 13269 rq->size = dma_length; 13270 13271 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13272 tmp_ptr += local_used; 13273 13274 /* Use physical addresses when talking to ISC hardware */ 13275 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13276 /* XXX KDM use busdma */ 13277 #if 0 13278 rq->local = vtophys(tmp_ptr); 13279 #endif 13280 } else 13281 rq->local = tmp_ptr; 13282 13283 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13284 tmp_ptr += remote_used; 13285 rq->remote = tmp_ptr; 13286 13287 rq->callback = NULL; 13288 13289 local_used += cur_len; 13290 if (local_used >= local_sglist[i].len) { 13291 i++; 13292 local_used = 0; 13293 } 13294 13295 remote_used += cur_len; 13296 if (remote_used >= remote_sglist[j].len) { 13297 j++; 13298 remote_used = 0; 13299 } 13300 total_used += cur_len; 13301 13302 if (total_used >= io->scsiio.kern_data_len) 13303 rq->callback = callback; 13304 13305 if ((rq->size & 0x7) != 0) { 13306 printf("%s: warning: size %d is not on 8b boundary\n", 13307 __func__, rq->size); 13308 } 13309 if (((uintptr_t)rq->local & 0x7) != 0) { 13310 printf("%s: warning: local %p not on 8b boundary\n", 13311 __func__, rq->local); 13312 } 13313 if (((uintptr_t)rq->remote & 0x7) != 0) { 13314 printf("%s: warning: remote %p not on 8b boundary\n", 13315 __func__, rq->local); 13316 } 13317 #if 0 13318 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13319 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13320 rq->local, rq->remote, rq->size); 13321 #endif 13322 13323 isc_ret = ctl_dt_single(rq); 13324 if (isc_ret == CTL_HA_STATUS_WAIT) 13325 continue; 13326 13327 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13328 rq->ret = CTL_HA_STATUS_SUCCESS; 13329 } else { 13330 rq->ret = isc_ret; 13331 } 13332 callback(rq); 13333 goto bailout; 13334 } 13335 13336 bailout: 13337 return (retval); 13338 13339 } 13340 13341 static void 13342 ctl_datamove_remote_read(union ctl_io *io) 13343 { 13344 int retval; 13345 int i; 13346 13347 /* 13348 * This will send an error to the other controller in the case of a 13349 * failure. 13350 */ 13351 retval = ctl_datamove_remote_sgl_setup(io); 13352 if (retval != 0) 13353 return; 13354 13355 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13356 ctl_datamove_remote_read_cb); 13357 if ((retval != 0) 13358 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13359 /* 13360 * Make sure we free memory if there was an error.. The 13361 * ctl_datamove_remote_xfer() function will send the 13362 * datamove done message, or call the callback with an 13363 * error if there is a problem. 13364 */ 13365 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13366 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13367 } 13368 13369 return; 13370 } 13371 13372 /* 13373 * Process a datamove request from the other controller. This is used for 13374 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13375 * first. Once that is complete, the data gets DMAed into the remote 13376 * controller's memory. For reads, we DMA from the remote controller's 13377 * memory into our memory first, and then move it out to the FETD. 13378 */ 13379 static void 13380 ctl_datamove_remote(union ctl_io *io) 13381 { 13382 struct ctl_softc *softc; 13383 13384 softc = control_softc; 13385 13386 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13387 13388 /* 13389 * Note that we look for an aborted I/O here, but don't do some of 13390 * the other checks that ctl_datamove() normally does. 13391 * We don't need to run the datamove delay code, since that should 13392 * have been done if need be on the other controller. 13393 */ 13394 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13395 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13396 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13397 io->io_hdr.nexus.targ_port, 13398 io->io_hdr.nexus.targ_target.id, 13399 io->io_hdr.nexus.targ_lun); 13400 io->io_hdr.port_status = 31338; 13401 ctl_send_datamove_done(io, /*have_lock*/ 0); 13402 return; 13403 } 13404 13405 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13406 ctl_datamove_remote_write(io); 13407 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13408 ctl_datamove_remote_read(io); 13409 } else { 13410 union ctl_ha_msg msg; 13411 struct scsi_sense_data *sense; 13412 uint8_t sks[3]; 13413 int retry_count; 13414 13415 memset(&msg, 0, sizeof(msg)); 13416 13417 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13418 msg.hdr.status = CTL_SCSI_ERROR; 13419 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13420 13421 retry_count = 4243; 13422 13423 sense = &msg.scsi.sense_data; 13424 sks[0] = SSD_SCS_VALID; 13425 sks[1] = (retry_count >> 8) & 0xff; 13426 sks[2] = retry_count & 0xff; 13427 13428 /* "Internal target failure" */ 13429 scsi_set_sense_data(sense, 13430 /*sense_format*/ SSD_TYPE_NONE, 13431 /*current_error*/ 1, 13432 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13433 /*asc*/ 0x44, 13434 /*ascq*/ 0x00, 13435 /*type*/ SSD_ELEM_SKS, 13436 /*size*/ sizeof(sks), 13437 /*data*/ sks, 13438 SSD_ELEM_NONE); 13439 13440 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13441 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13442 ctl_failover_io(io, /*have_lock*/ 1); 13443 return; 13444 } 13445 13446 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13447 CTL_HA_STATUS_SUCCESS) { 13448 /* XXX KDM what to do if this fails? */ 13449 } 13450 return; 13451 } 13452 13453 } 13454 13455 static int 13456 ctl_process_done(union ctl_io *io) 13457 { 13458 struct ctl_lun *lun; 13459 struct ctl_softc *ctl_softc = control_softc; 13460 void (*fe_done)(union ctl_io *io); 13461 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13462 13463 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13464 13465 fe_done = 13466 control_softc->ctl_ports[targ_port]->fe_done; 13467 13468 #ifdef CTL_TIME_IO 13469 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13470 char str[256]; 13471 char path_str[64]; 13472 struct sbuf sb; 13473 13474 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13475 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13476 13477 sbuf_cat(&sb, path_str); 13478 switch (io->io_hdr.io_type) { 13479 case CTL_IO_SCSI: 13480 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13481 sbuf_printf(&sb, "\n"); 13482 sbuf_cat(&sb, path_str); 13483 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13484 io->scsiio.tag_num, io->scsiio.tag_type); 13485 break; 13486 case CTL_IO_TASK: 13487 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13488 "Tag Type: %d\n", io->taskio.task_action, 13489 io->taskio.tag_num, io->taskio.tag_type); 13490 break; 13491 default: 13492 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13493 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13494 break; 13495 } 13496 sbuf_cat(&sb, path_str); 13497 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13498 (intmax_t)time_uptime - io->io_hdr.start_time); 13499 sbuf_finish(&sb); 13500 printf("%s", sbuf_data(&sb)); 13501 } 13502 #endif /* CTL_TIME_IO */ 13503 13504 switch (io->io_hdr.io_type) { 13505 case CTL_IO_SCSI: 13506 break; 13507 case CTL_IO_TASK: 13508 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13509 ctl_io_error_print(io, NULL); 13510 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13511 ctl_free_io(io); 13512 else 13513 fe_done(io); 13514 return (CTL_RETVAL_COMPLETE); 13515 default: 13516 panic("ctl_process_done: invalid io type %d\n", 13517 io->io_hdr.io_type); 13518 break; /* NOTREACHED */ 13519 } 13520 13521 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13522 if (lun == NULL) { 13523 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13524 io->io_hdr.nexus.targ_mapped_lun)); 13525 goto bailout; 13526 } 13527 13528 mtx_lock(&lun->lun_lock); 13529 13530 /* 13531 * Check to see if we have any errors to inject here. We only 13532 * inject errors for commands that don't already have errors set. 13533 */ 13534 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13535 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13536 ctl_inject_error(lun, io); 13537 13538 /* 13539 * XXX KDM how do we treat commands that aren't completed 13540 * successfully? 13541 * 13542 * XXX KDM should we also track I/O latency? 13543 */ 13544 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13545 io->io_hdr.io_type == CTL_IO_SCSI) { 13546 #ifdef CTL_TIME_IO 13547 struct bintime cur_bt; 13548 #endif 13549 int type; 13550 13551 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13552 CTL_FLAG_DATA_IN) 13553 type = CTL_STATS_READ; 13554 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13555 CTL_FLAG_DATA_OUT) 13556 type = CTL_STATS_WRITE; 13557 else 13558 type = CTL_STATS_NO_IO; 13559 13560 lun->stats.ports[targ_port].bytes[type] += 13561 io->scsiio.kern_total_len; 13562 lun->stats.ports[targ_port].operations[type]++; 13563 #ifdef CTL_TIME_IO 13564 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13565 &io->io_hdr.dma_bt); 13566 lun->stats.ports[targ_port].num_dmas[type] += 13567 io->io_hdr.num_dmas; 13568 getbintime(&cur_bt); 13569 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13570 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13571 #endif 13572 } 13573 13574 /* 13575 * Remove this from the OOA queue. 13576 */ 13577 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13578 13579 /* 13580 * Run through the blocked queue on this LUN and see if anything 13581 * has become unblocked, now that this transaction is done. 13582 */ 13583 ctl_check_blocked(lun); 13584 13585 /* 13586 * If the LUN has been invalidated, free it if there is nothing 13587 * left on its OOA queue. 13588 */ 13589 if ((lun->flags & CTL_LUN_INVALID) 13590 && TAILQ_EMPTY(&lun->ooa_queue)) { 13591 mtx_unlock(&lun->lun_lock); 13592 mtx_lock(&ctl_softc->ctl_lock); 13593 ctl_free_lun(lun); 13594 mtx_unlock(&ctl_softc->ctl_lock); 13595 } else 13596 mtx_unlock(&lun->lun_lock); 13597 13598 bailout: 13599 13600 /* 13601 * If this command has been aborted, make sure we set the status 13602 * properly. The FETD is responsible for freeing the I/O and doing 13603 * whatever it needs to do to clean up its state. 13604 */ 13605 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13606 ctl_set_task_aborted(&io->scsiio); 13607 13608 /* 13609 * If enabled, print command error status. 13610 * We don't print UAs unless debugging was enabled explicitly. 13611 */ 13612 do { 13613 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13614 break; 13615 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13616 break; 13617 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13618 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13619 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13620 int error_code, sense_key, asc, ascq; 13621 13622 scsi_extract_sense_len(&io->scsiio.sense_data, 13623 io->scsiio.sense_len, &error_code, &sense_key, 13624 &asc, &ascq, /*show_errors*/ 0); 13625 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13626 break; 13627 } 13628 13629 ctl_io_error_print(io, NULL); 13630 } while (0); 13631 13632 /* 13633 * Tell the FETD or the other shelf controller we're done with this 13634 * command. Note that only SCSI commands get to this point. Task 13635 * management commands are completed above. 13636 * 13637 * We only send status to the other controller if we're in XFER 13638 * mode. In SER_ONLY mode, the I/O is done on the controller that 13639 * received the I/O (from CTL's perspective), and so the status is 13640 * generated there. 13641 * 13642 * XXX KDM if we hold the lock here, we could cause a deadlock 13643 * if the frontend comes back in in this context to queue 13644 * something. 13645 */ 13646 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13647 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13648 union ctl_ha_msg msg; 13649 13650 memset(&msg, 0, sizeof(msg)); 13651 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13652 msg.hdr.original_sc = io->io_hdr.original_sc; 13653 msg.hdr.nexus = io->io_hdr.nexus; 13654 msg.hdr.status = io->io_hdr.status; 13655 msg.scsi.scsi_status = io->scsiio.scsi_status; 13656 msg.scsi.tag_num = io->scsiio.tag_num; 13657 msg.scsi.tag_type = io->scsiio.tag_type; 13658 msg.scsi.sense_len = io->scsiio.sense_len; 13659 msg.scsi.sense_residual = io->scsiio.sense_residual; 13660 msg.scsi.residual = io->scsiio.residual; 13661 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13662 sizeof(io->scsiio.sense_data)); 13663 /* 13664 * We copy this whether or not this is an I/O-related 13665 * command. Otherwise, we'd have to go and check to see 13666 * whether it's a read/write command, and it really isn't 13667 * worth it. 13668 */ 13669 memcpy(&msg.scsi.lbalen, 13670 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13671 sizeof(msg.scsi.lbalen)); 13672 13673 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13674 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13675 /* XXX do something here */ 13676 } 13677 13678 ctl_free_io(io); 13679 } else 13680 fe_done(io); 13681 13682 return (CTL_RETVAL_COMPLETE); 13683 } 13684 13685 #ifdef CTL_WITH_CA 13686 /* 13687 * Front end should call this if it doesn't do autosense. When the request 13688 * sense comes back in from the initiator, we'll dequeue this and send it. 13689 */ 13690 int 13691 ctl_queue_sense(union ctl_io *io) 13692 { 13693 struct ctl_lun *lun; 13694 struct ctl_softc *ctl_softc; 13695 uint32_t initidx, targ_lun; 13696 13697 ctl_softc = control_softc; 13698 13699 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13700 13701 /* 13702 * LUN lookup will likely move to the ctl_work_thread() once we 13703 * have our new queueing infrastructure (that doesn't put things on 13704 * a per-LUN queue initially). That is so that we can handle 13705 * things like an INQUIRY to a LUN that we don't have enabled. We 13706 * can't deal with that right now. 13707 */ 13708 mtx_lock(&ctl_softc->ctl_lock); 13709 13710 /* 13711 * If we don't have a LUN for this, just toss the sense 13712 * information. 13713 */ 13714 targ_lun = io->io_hdr.nexus.targ_lun; 13715 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13716 if ((targ_lun < CTL_MAX_LUNS) 13717 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13718 lun = ctl_softc->ctl_luns[targ_lun]; 13719 else 13720 goto bailout; 13721 13722 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13723 13724 mtx_lock(&lun->lun_lock); 13725 /* 13726 * Already have CA set for this LUN...toss the sense information. 13727 */ 13728 if (ctl_is_set(lun->have_ca, initidx)) { 13729 mtx_unlock(&lun->lun_lock); 13730 goto bailout; 13731 } 13732 13733 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13734 ctl_min(sizeof(lun->pending_sense[initidx]), 13735 sizeof(io->scsiio.sense_data))); 13736 ctl_set_mask(lun->have_ca, initidx); 13737 mtx_unlock(&lun->lun_lock); 13738 13739 bailout: 13740 mtx_unlock(&ctl_softc->ctl_lock); 13741 13742 ctl_free_io(io); 13743 13744 return (CTL_RETVAL_COMPLETE); 13745 } 13746 #endif 13747 13748 /* 13749 * Primary command inlet from frontend ports. All SCSI and task I/O 13750 * requests must go through this function. 13751 */ 13752 int 13753 ctl_queue(union ctl_io *io) 13754 { 13755 struct ctl_softc *ctl_softc; 13756 13757 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13758 13759 ctl_softc = control_softc; 13760 13761 #ifdef CTL_TIME_IO 13762 io->io_hdr.start_time = time_uptime; 13763 getbintime(&io->io_hdr.start_bt); 13764 #endif /* CTL_TIME_IO */ 13765 13766 /* Map FE-specific LUN ID into global one. */ 13767 io->io_hdr.nexus.targ_mapped_lun = 13768 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13769 13770 switch (io->io_hdr.io_type) { 13771 case CTL_IO_SCSI: 13772 case CTL_IO_TASK: 13773 if (ctl_debug & CTL_DEBUG_CDB) 13774 ctl_io_print(io); 13775 ctl_enqueue_incoming(io); 13776 break; 13777 default: 13778 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13779 return (EINVAL); 13780 } 13781 13782 return (CTL_RETVAL_COMPLETE); 13783 } 13784 13785 #ifdef CTL_IO_DELAY 13786 static void 13787 ctl_done_timer_wakeup(void *arg) 13788 { 13789 union ctl_io *io; 13790 13791 io = (union ctl_io *)arg; 13792 ctl_done(io); 13793 } 13794 #endif /* CTL_IO_DELAY */ 13795 13796 void 13797 ctl_done(union ctl_io *io) 13798 { 13799 struct ctl_softc *ctl_softc; 13800 13801 ctl_softc = control_softc; 13802 13803 /* 13804 * Enable this to catch duplicate completion issues. 13805 */ 13806 #if 0 13807 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13808 printf("%s: type %d msg %d cdb %x iptl: " 13809 "%d:%d:%d:%d tag 0x%04x " 13810 "flag %#x status %x\n", 13811 __func__, 13812 io->io_hdr.io_type, 13813 io->io_hdr.msg_type, 13814 io->scsiio.cdb[0], 13815 io->io_hdr.nexus.initid.id, 13816 io->io_hdr.nexus.targ_port, 13817 io->io_hdr.nexus.targ_target.id, 13818 io->io_hdr.nexus.targ_lun, 13819 (io->io_hdr.io_type == 13820 CTL_IO_TASK) ? 13821 io->taskio.tag_num : 13822 io->scsiio.tag_num, 13823 io->io_hdr.flags, 13824 io->io_hdr.status); 13825 } else 13826 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13827 #endif 13828 13829 /* 13830 * This is an internal copy of an I/O, and should not go through 13831 * the normal done processing logic. 13832 */ 13833 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13834 return; 13835 13836 /* 13837 * We need to send a msg to the serializing shelf to finish the IO 13838 * as well. We don't send a finish message to the other shelf if 13839 * this is a task management command. Task management commands 13840 * aren't serialized in the OOA queue, but rather just executed on 13841 * both shelf controllers for commands that originated on that 13842 * controller. 13843 */ 13844 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13845 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13846 union ctl_ha_msg msg_io; 13847 13848 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13849 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13850 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13851 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13852 } 13853 /* continue on to finish IO */ 13854 } 13855 #ifdef CTL_IO_DELAY 13856 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13857 struct ctl_lun *lun; 13858 13859 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13860 13861 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13862 } else { 13863 struct ctl_lun *lun; 13864 13865 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13866 13867 if ((lun != NULL) 13868 && (lun->delay_info.done_delay > 0)) { 13869 struct callout *callout; 13870 13871 callout = (struct callout *)&io->io_hdr.timer_bytes; 13872 callout_init(callout, /*mpsafe*/ 1); 13873 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13874 callout_reset(callout, 13875 lun->delay_info.done_delay * hz, 13876 ctl_done_timer_wakeup, io); 13877 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13878 lun->delay_info.done_delay = 0; 13879 return; 13880 } 13881 } 13882 #endif /* CTL_IO_DELAY */ 13883 13884 ctl_enqueue_done(io); 13885 } 13886 13887 int 13888 ctl_isc(struct ctl_scsiio *ctsio) 13889 { 13890 struct ctl_lun *lun; 13891 int retval; 13892 13893 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13894 13895 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13896 13897 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13898 13899 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13900 13901 return (retval); 13902 } 13903 13904 13905 static void 13906 ctl_work_thread(void *arg) 13907 { 13908 struct ctl_thread *thr = (struct ctl_thread *)arg; 13909 struct ctl_softc *softc = thr->ctl_softc; 13910 union ctl_io *io; 13911 int retval; 13912 13913 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13914 13915 for (;;) { 13916 retval = 0; 13917 13918 /* 13919 * We handle the queues in this order: 13920 * - ISC 13921 * - done queue (to free up resources, unblock other commands) 13922 * - RtR queue 13923 * - incoming queue 13924 * 13925 * If those queues are empty, we break out of the loop and 13926 * go to sleep. 13927 */ 13928 mtx_lock(&thr->queue_lock); 13929 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13930 if (io != NULL) { 13931 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13932 mtx_unlock(&thr->queue_lock); 13933 ctl_handle_isc(io); 13934 continue; 13935 } 13936 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13937 if (io != NULL) { 13938 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13939 /* clear any blocked commands, call fe_done */ 13940 mtx_unlock(&thr->queue_lock); 13941 retval = ctl_process_done(io); 13942 continue; 13943 } 13944 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13945 if (io != NULL) { 13946 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13947 mtx_unlock(&thr->queue_lock); 13948 if (io->io_hdr.io_type == CTL_IO_TASK) 13949 ctl_run_task(io); 13950 else 13951 ctl_scsiio_precheck(softc, &io->scsiio); 13952 continue; 13953 } 13954 if (!ctl_pause_rtr) { 13955 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13956 if (io != NULL) { 13957 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13958 mtx_unlock(&thr->queue_lock); 13959 retval = ctl_scsiio(&io->scsiio); 13960 if (retval != CTL_RETVAL_COMPLETE) 13961 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13962 continue; 13963 } 13964 } 13965 13966 /* Sleep until we have something to do. */ 13967 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13968 } 13969 } 13970 13971 static void 13972 ctl_lun_thread(void *arg) 13973 { 13974 struct ctl_softc *softc = (struct ctl_softc *)arg; 13975 struct ctl_be_lun *be_lun; 13976 int retval; 13977 13978 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13979 13980 for (;;) { 13981 retval = 0; 13982 mtx_lock(&softc->ctl_lock); 13983 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13984 if (be_lun != NULL) { 13985 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13986 mtx_unlock(&softc->ctl_lock); 13987 ctl_create_lun(be_lun); 13988 continue; 13989 } 13990 13991 /* Sleep until we have something to do. */ 13992 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13993 PDROP | PRIBIO, "-", 0); 13994 } 13995 } 13996 13997 static void 13998 ctl_thresh_thread(void *arg) 13999 { 14000 struct ctl_softc *softc = (struct ctl_softc *)arg; 14001 struct ctl_lun *lun; 14002 struct ctl_be_lun *be_lun; 14003 struct scsi_da_rw_recovery_page *rwpage; 14004 struct ctl_logical_block_provisioning_page *page; 14005 const char *attr; 14006 uint64_t thres, val; 14007 int i, e; 14008 14009 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 14010 14011 for (;;) { 14012 mtx_lock(&softc->ctl_lock); 14013 STAILQ_FOREACH(lun, &softc->lun_list, links) { 14014 be_lun = lun->be_lun; 14015 if ((lun->flags & CTL_LUN_DISABLED) || 14016 (lun->flags & CTL_LUN_OFFLINE) || 14017 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 14018 lun->backend->lun_attr == NULL) 14019 continue; 14020 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 14021 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 14022 continue; 14023 e = 0; 14024 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 14025 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 14026 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 14027 continue; 14028 thres = scsi_4btoul(page->descr[i].count); 14029 thres <<= CTL_LBP_EXPONENT; 14030 switch (page->descr[i].resource) { 14031 case 0x01: 14032 attr = "blocksavail"; 14033 break; 14034 case 0x02: 14035 attr = "blocksused"; 14036 break; 14037 case 0xf1: 14038 attr = "poolblocksavail"; 14039 break; 14040 case 0xf2: 14041 attr = "poolblocksused"; 14042 break; 14043 default: 14044 continue; 14045 } 14046 mtx_unlock(&softc->ctl_lock); // XXX 14047 val = lun->backend->lun_attr( 14048 lun->be_lun->be_lun, attr); 14049 mtx_lock(&softc->ctl_lock); 14050 if (val == UINT64_MAX) 14051 continue; 14052 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 14053 == SLBPPD_ARMING_INC) 14054 e |= (val >= thres); 14055 else 14056 e |= (val <= thres); 14057 } 14058 mtx_lock(&lun->lun_lock); 14059 if (e) { 14060 if (lun->lasttpt == 0 || 14061 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 14062 lun->lasttpt = time_uptime; 14063 for (i = 0; i < CTL_MAX_INITIATORS; i++) 14064 lun->pending_ua[i] |= 14065 CTL_UA_THIN_PROV_THRES; 14066 } 14067 } else { 14068 lun->lasttpt = 0; 14069 for (i = 0; i < CTL_MAX_INITIATORS; i++) 14070 lun->pending_ua[i] &= ~CTL_UA_THIN_PROV_THRES; 14071 } 14072 mtx_unlock(&lun->lun_lock); 14073 } 14074 mtx_unlock(&softc->ctl_lock); 14075 pause("-", CTL_LBP_PERIOD * hz); 14076 } 14077 } 14078 14079 static void 14080 ctl_enqueue_incoming(union ctl_io *io) 14081 { 14082 struct ctl_softc *softc = control_softc; 14083 struct ctl_thread *thr; 14084 u_int idx; 14085 14086 idx = (io->io_hdr.nexus.targ_port * 127 + 14087 io->io_hdr.nexus.initid.id) % worker_threads; 14088 thr = &softc->threads[idx]; 14089 mtx_lock(&thr->queue_lock); 14090 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14091 mtx_unlock(&thr->queue_lock); 14092 wakeup(thr); 14093 } 14094 14095 static void 14096 ctl_enqueue_rtr(union ctl_io *io) 14097 { 14098 struct ctl_softc *softc = control_softc; 14099 struct ctl_thread *thr; 14100 14101 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14102 mtx_lock(&thr->queue_lock); 14103 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14104 mtx_unlock(&thr->queue_lock); 14105 wakeup(thr); 14106 } 14107 14108 static void 14109 ctl_enqueue_done(union ctl_io *io) 14110 { 14111 struct ctl_softc *softc = control_softc; 14112 struct ctl_thread *thr; 14113 14114 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14115 mtx_lock(&thr->queue_lock); 14116 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14117 mtx_unlock(&thr->queue_lock); 14118 wakeup(thr); 14119 } 14120 14121 static void 14122 ctl_enqueue_isc(union ctl_io *io) 14123 { 14124 struct ctl_softc *softc = control_softc; 14125 struct ctl_thread *thr; 14126 14127 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14128 mtx_lock(&thr->queue_lock); 14129 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14130 mtx_unlock(&thr->queue_lock); 14131 wakeup(thr); 14132 } 14133 14134 /* Initialization and failover */ 14135 14136 void 14137 ctl_init_isc_msg(void) 14138 { 14139 printf("CTL: Still calling this thing\n"); 14140 } 14141 14142 /* 14143 * Init component 14144 * Initializes component into configuration defined by bootMode 14145 * (see hasc-sv.c) 14146 * returns hasc_Status: 14147 * OK 14148 * ERROR - fatal error 14149 */ 14150 static ctl_ha_comp_status 14151 ctl_isc_init(struct ctl_ha_component *c) 14152 { 14153 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14154 14155 c->status = ret; 14156 return ret; 14157 } 14158 14159 /* Start component 14160 * Starts component in state requested. If component starts successfully, 14161 * it must set its own state to the requestrd state 14162 * When requested state is HASC_STATE_HA, the component may refine it 14163 * by adding _SLAVE or _MASTER flags. 14164 * Currently allowed state transitions are: 14165 * UNKNOWN->HA - initial startup 14166 * UNKNOWN->SINGLE - initial startup when no parter detected 14167 * HA->SINGLE - failover 14168 * returns ctl_ha_comp_status: 14169 * OK - component successfully started in requested state 14170 * FAILED - could not start the requested state, failover may 14171 * be possible 14172 * ERROR - fatal error detected, no future startup possible 14173 */ 14174 static ctl_ha_comp_status 14175 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14176 { 14177 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14178 14179 printf("%s: go\n", __func__); 14180 14181 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14182 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14183 control_softc->is_single = 0; 14184 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14185 != CTL_HA_STATUS_SUCCESS) { 14186 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14187 ret = CTL_HA_COMP_STATUS_ERROR; 14188 } 14189 } else if (CTL_HA_STATE_IS_HA(c->state) 14190 && CTL_HA_STATE_IS_SINGLE(state)){ 14191 // HA->SINGLE transition 14192 ctl_failover(); 14193 control_softc->is_single = 1; 14194 } else { 14195 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14196 c->state, state); 14197 ret = CTL_HA_COMP_STATUS_ERROR; 14198 } 14199 if (CTL_HA_STATE_IS_SINGLE(state)) 14200 control_softc->is_single = 1; 14201 14202 c->state = state; 14203 c->status = ret; 14204 return ret; 14205 } 14206 14207 /* 14208 * Quiesce component 14209 * The component must clear any error conditions (set status to OK) and 14210 * prepare itself to another Start call 14211 * returns ctl_ha_comp_status: 14212 * OK 14213 * ERROR 14214 */ 14215 static ctl_ha_comp_status 14216 ctl_isc_quiesce(struct ctl_ha_component *c) 14217 { 14218 int ret = CTL_HA_COMP_STATUS_OK; 14219 14220 ctl_pause_rtr = 1; 14221 c->status = ret; 14222 return ret; 14223 } 14224 14225 struct ctl_ha_component ctl_ha_component_ctlisc = 14226 { 14227 .name = "CTL ISC", 14228 .state = CTL_HA_STATE_UNKNOWN, 14229 .init = ctl_isc_init, 14230 .start = ctl_isc_start, 14231 .quiesce = ctl_isc_quiesce 14232 }; 14233 14234 /* 14235 * vim: ts=8 14236 */ 14237