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 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5000 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5001 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5002 5003 if ((io->io_hdr.port_status != 0) && 5004 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5005 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5006 /* 5007 * For hardware error sense keys, the sense key 5008 * specific value is defined to be a retry count, 5009 * but we use it to pass back an internal FETD 5010 * error code. XXX KDM Hopefully the FETD is only 5011 * using 16 bits for an error code, since that's 5012 * all the space we have in the sks field. 5013 */ 5014 ctl_set_internal_failure(&io->scsiio, 5015 /*sks_valid*/ 1, 5016 /*retry_count*/ 5017 io->io_hdr.port_status); 5018 } 5019 5020 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5021 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5022 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5023 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5024 /* 5025 * XXX KDM just assuming a single pointer here, and not a 5026 * S/G list. If we start using S/G lists for config data, 5027 * we'll need to know how to clean them up here as well. 5028 */ 5029 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5030 free(io->scsiio.kern_data_ptr, M_CTL); 5031 ctl_done(io); 5032 retval = CTL_RETVAL_COMPLETE; 5033 } else { 5034 /* 5035 * XXX KDM now we need to continue data movement. Some 5036 * options: 5037 * - call ctl_scsiio() again? We don't do this for data 5038 * writes, because for those at least we know ahead of 5039 * time where the write will go and how long it is. For 5040 * config writes, though, that information is largely 5041 * contained within the write itself, thus we need to 5042 * parse out the data again. 5043 * 5044 * - Call some other function once the data is in? 5045 */ 5046 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5047 ctl_data_print(io); 5048 5049 /* 5050 * XXX KDM call ctl_scsiio() again for now, and check flag 5051 * bits to see whether we're allocated or not. 5052 */ 5053 retval = ctl_scsiio(&io->scsiio); 5054 } 5055 return (retval); 5056 } 5057 5058 /* 5059 * This gets called by a backend driver when it is done with a 5060 * data_submit method. 5061 */ 5062 void 5063 ctl_data_submit_done(union ctl_io *io) 5064 { 5065 /* 5066 * If the IO_CONT flag is set, we need to call the supplied 5067 * function to continue processing the I/O, instead of completing 5068 * the I/O just yet. 5069 * 5070 * If there is an error, though, we don't want to keep processing. 5071 * Instead, just send status back to the initiator. 5072 */ 5073 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5074 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5075 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5076 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5077 io->scsiio.io_cont(io); 5078 return; 5079 } 5080 ctl_done(io); 5081 } 5082 5083 /* 5084 * This gets called by a backend driver when it is done with a 5085 * configuration write. 5086 */ 5087 void 5088 ctl_config_write_done(union ctl_io *io) 5089 { 5090 uint8_t *buf; 5091 5092 /* 5093 * If the IO_CONT flag is set, we need to call the supplied 5094 * function to continue processing the I/O, instead of completing 5095 * the I/O just yet. 5096 * 5097 * If there is an error, though, we don't want to keep processing. 5098 * Instead, just send status back to the initiator. 5099 */ 5100 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5101 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5102 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5103 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5104 io->scsiio.io_cont(io); 5105 return; 5106 } 5107 /* 5108 * Since a configuration write can be done for commands that actually 5109 * have data allocated, like write buffer, and commands that have 5110 * no data, like start/stop unit, we need to check here. 5111 */ 5112 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5113 buf = io->scsiio.kern_data_ptr; 5114 else 5115 buf = NULL; 5116 ctl_done(io); 5117 if (buf) 5118 free(buf, M_CTL); 5119 } 5120 5121 /* 5122 * SCSI release command. 5123 */ 5124 int 5125 ctl_scsi_release(struct ctl_scsiio *ctsio) 5126 { 5127 int length, longid, thirdparty_id, resv_id; 5128 struct ctl_softc *ctl_softc; 5129 struct ctl_lun *lun; 5130 uint32_t residx; 5131 5132 length = 0; 5133 resv_id = 0; 5134 5135 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5136 5137 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5138 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5139 ctl_softc = control_softc; 5140 5141 switch (ctsio->cdb[0]) { 5142 case RELEASE_10: { 5143 struct scsi_release_10 *cdb; 5144 5145 cdb = (struct scsi_release_10 *)ctsio->cdb; 5146 5147 if (cdb->byte2 & SR10_LONGID) 5148 longid = 1; 5149 else 5150 thirdparty_id = cdb->thirdparty_id; 5151 5152 resv_id = cdb->resv_id; 5153 length = scsi_2btoul(cdb->length); 5154 break; 5155 } 5156 } 5157 5158 5159 /* 5160 * XXX KDM right now, we only support LUN reservation. We don't 5161 * support 3rd party reservations, or extent reservations, which 5162 * might actually need the parameter list. If we've gotten this 5163 * far, we've got a LUN reservation. Anything else got kicked out 5164 * above. So, according to SPC, ignore the length. 5165 */ 5166 length = 0; 5167 5168 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5169 && (length > 0)) { 5170 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5171 ctsio->kern_data_len = length; 5172 ctsio->kern_total_len = length; 5173 ctsio->kern_data_resid = 0; 5174 ctsio->kern_rel_offset = 0; 5175 ctsio->kern_sg_entries = 0; 5176 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5177 ctsio->be_move_done = ctl_config_move_done; 5178 ctl_datamove((union ctl_io *)ctsio); 5179 5180 return (CTL_RETVAL_COMPLETE); 5181 } 5182 5183 if (length > 0) 5184 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5185 5186 mtx_lock(&lun->lun_lock); 5187 5188 /* 5189 * According to SPC, it is not an error for an intiator to attempt 5190 * to release a reservation on a LUN that isn't reserved, or that 5191 * is reserved by another initiator. The reservation can only be 5192 * released, though, by the initiator who made it or by one of 5193 * several reset type events. 5194 */ 5195 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5196 lun->flags &= ~CTL_LUN_RESERVED; 5197 5198 mtx_unlock(&lun->lun_lock); 5199 5200 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5201 free(ctsio->kern_data_ptr, M_CTL); 5202 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5203 } 5204 5205 ctl_set_success(ctsio); 5206 ctl_done((union ctl_io *)ctsio); 5207 return (CTL_RETVAL_COMPLETE); 5208 } 5209 5210 int 5211 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5212 { 5213 int extent, thirdparty, longid; 5214 int resv_id, length; 5215 uint64_t thirdparty_id; 5216 struct ctl_softc *ctl_softc; 5217 struct ctl_lun *lun; 5218 uint32_t residx; 5219 5220 extent = 0; 5221 thirdparty = 0; 5222 longid = 0; 5223 resv_id = 0; 5224 length = 0; 5225 thirdparty_id = 0; 5226 5227 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5228 5229 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5230 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5231 ctl_softc = control_softc; 5232 5233 switch (ctsio->cdb[0]) { 5234 case RESERVE_10: { 5235 struct scsi_reserve_10 *cdb; 5236 5237 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5238 5239 if (cdb->byte2 & SR10_LONGID) 5240 longid = 1; 5241 else 5242 thirdparty_id = cdb->thirdparty_id; 5243 5244 resv_id = cdb->resv_id; 5245 length = scsi_2btoul(cdb->length); 5246 break; 5247 } 5248 } 5249 5250 /* 5251 * XXX KDM right now, we only support LUN reservation. We don't 5252 * support 3rd party reservations, or extent reservations, which 5253 * might actually need the parameter list. If we've gotten this 5254 * far, we've got a LUN reservation. Anything else got kicked out 5255 * above. So, according to SPC, ignore the length. 5256 */ 5257 length = 0; 5258 5259 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5260 && (length > 0)) { 5261 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5262 ctsio->kern_data_len = length; 5263 ctsio->kern_total_len = length; 5264 ctsio->kern_data_resid = 0; 5265 ctsio->kern_rel_offset = 0; 5266 ctsio->kern_sg_entries = 0; 5267 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5268 ctsio->be_move_done = ctl_config_move_done; 5269 ctl_datamove((union ctl_io *)ctsio); 5270 5271 return (CTL_RETVAL_COMPLETE); 5272 } 5273 5274 if (length > 0) 5275 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5276 5277 mtx_lock(&lun->lun_lock); 5278 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5279 ctl_set_reservation_conflict(ctsio); 5280 goto bailout; 5281 } 5282 5283 lun->flags |= CTL_LUN_RESERVED; 5284 lun->res_idx = residx; 5285 5286 ctl_set_success(ctsio); 5287 5288 bailout: 5289 mtx_unlock(&lun->lun_lock); 5290 5291 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5292 free(ctsio->kern_data_ptr, M_CTL); 5293 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5294 } 5295 5296 ctl_done((union ctl_io *)ctsio); 5297 return (CTL_RETVAL_COMPLETE); 5298 } 5299 5300 int 5301 ctl_start_stop(struct ctl_scsiio *ctsio) 5302 { 5303 struct scsi_start_stop_unit *cdb; 5304 struct ctl_lun *lun; 5305 struct ctl_softc *ctl_softc; 5306 int retval; 5307 5308 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5309 5310 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5311 ctl_softc = control_softc; 5312 retval = 0; 5313 5314 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5315 5316 /* 5317 * XXX KDM 5318 * We don't support the immediate bit on a stop unit. In order to 5319 * do that, we would need to code up a way to know that a stop is 5320 * pending, and hold off any new commands until it completes, one 5321 * way or another. Then we could accept or reject those commands 5322 * depending on its status. We would almost need to do the reverse 5323 * of what we do below for an immediate start -- return the copy of 5324 * the ctl_io to the FETD with status to send to the host (and to 5325 * free the copy!) and then free the original I/O once the stop 5326 * actually completes. That way, the OOA queue mechanism can work 5327 * to block commands that shouldn't proceed. Another alternative 5328 * would be to put the copy in the queue in place of the original, 5329 * and return the original back to the caller. That could be 5330 * slightly safer.. 5331 */ 5332 if ((cdb->byte2 & SSS_IMMED) 5333 && ((cdb->how & SSS_START) == 0)) { 5334 ctl_set_invalid_field(ctsio, 5335 /*sks_valid*/ 1, 5336 /*command*/ 1, 5337 /*field*/ 1, 5338 /*bit_valid*/ 1, 5339 /*bit*/ 0); 5340 ctl_done((union ctl_io *)ctsio); 5341 return (CTL_RETVAL_COMPLETE); 5342 } 5343 5344 if ((lun->flags & CTL_LUN_PR_RESERVED) 5345 && ((cdb->how & SSS_START)==0)) { 5346 uint32_t residx; 5347 5348 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5349 if (lun->pr_keys[residx] == 0 5350 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5351 5352 ctl_set_reservation_conflict(ctsio); 5353 ctl_done((union ctl_io *)ctsio); 5354 return (CTL_RETVAL_COMPLETE); 5355 } 5356 } 5357 5358 /* 5359 * If there is no backend on this device, we can't start or stop 5360 * it. In theory we shouldn't get any start/stop commands in the 5361 * first place at this level if the LUN doesn't have a backend. 5362 * That should get stopped by the command decode code. 5363 */ 5364 if (lun->backend == NULL) { 5365 ctl_set_invalid_opcode(ctsio); 5366 ctl_done((union ctl_io *)ctsio); 5367 return (CTL_RETVAL_COMPLETE); 5368 } 5369 5370 /* 5371 * XXX KDM Copan-specific offline behavior. 5372 * Figure out a reasonable way to port this? 5373 */ 5374 #ifdef NEEDTOPORT 5375 mtx_lock(&lun->lun_lock); 5376 5377 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5378 && (lun->flags & CTL_LUN_OFFLINE)) { 5379 /* 5380 * If the LUN is offline, and the on/offline bit isn't set, 5381 * reject the start or stop. Otherwise, let it through. 5382 */ 5383 mtx_unlock(&lun->lun_lock); 5384 ctl_set_lun_not_ready(ctsio); 5385 ctl_done((union ctl_io *)ctsio); 5386 } else { 5387 mtx_unlock(&lun->lun_lock); 5388 #endif /* NEEDTOPORT */ 5389 /* 5390 * This could be a start or a stop when we're online, 5391 * or a stop/offline or start/online. A start or stop when 5392 * we're offline is covered in the case above. 5393 */ 5394 /* 5395 * In the non-immediate case, we send the request to 5396 * the backend and return status to the user when 5397 * it is done. 5398 * 5399 * In the immediate case, we allocate a new ctl_io 5400 * to hold a copy of the request, and send that to 5401 * the backend. We then set good status on the 5402 * user's request and return it immediately. 5403 */ 5404 if (cdb->byte2 & SSS_IMMED) { 5405 union ctl_io *new_io; 5406 5407 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5408 ctl_copy_io((union ctl_io *)ctsio, new_io); 5409 retval = lun->backend->config_write(new_io); 5410 ctl_set_success(ctsio); 5411 ctl_done((union ctl_io *)ctsio); 5412 } else { 5413 retval = lun->backend->config_write( 5414 (union ctl_io *)ctsio); 5415 } 5416 #ifdef NEEDTOPORT 5417 } 5418 #endif 5419 return (retval); 5420 } 5421 5422 /* 5423 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5424 * we don't really do anything with the LBA and length fields if the user 5425 * passes them in. Instead we'll just flush out the cache for the entire 5426 * LUN. 5427 */ 5428 int 5429 ctl_sync_cache(struct ctl_scsiio *ctsio) 5430 { 5431 struct ctl_lun *lun; 5432 struct ctl_softc *ctl_softc; 5433 uint64_t starting_lba; 5434 uint32_t block_count; 5435 int retval; 5436 5437 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5438 5439 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5440 ctl_softc = control_softc; 5441 retval = 0; 5442 5443 switch (ctsio->cdb[0]) { 5444 case SYNCHRONIZE_CACHE: { 5445 struct scsi_sync_cache *cdb; 5446 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5447 5448 starting_lba = scsi_4btoul(cdb->begin_lba); 5449 block_count = scsi_2btoul(cdb->lb_count); 5450 break; 5451 } 5452 case SYNCHRONIZE_CACHE_16: { 5453 struct scsi_sync_cache_16 *cdb; 5454 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5455 5456 starting_lba = scsi_8btou64(cdb->begin_lba); 5457 block_count = scsi_4btoul(cdb->lb_count); 5458 break; 5459 } 5460 default: 5461 ctl_set_invalid_opcode(ctsio); 5462 ctl_done((union ctl_io *)ctsio); 5463 goto bailout; 5464 break; /* NOTREACHED */ 5465 } 5466 5467 /* 5468 * We check the LBA and length, but don't do anything with them. 5469 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5470 * get flushed. This check will just help satisfy anyone who wants 5471 * to see an error for an out of range LBA. 5472 */ 5473 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5474 ctl_set_lba_out_of_range(ctsio); 5475 ctl_done((union ctl_io *)ctsio); 5476 goto bailout; 5477 } 5478 5479 /* 5480 * If this LUN has no backend, we can't flush the cache anyway. 5481 */ 5482 if (lun->backend == NULL) { 5483 ctl_set_invalid_opcode(ctsio); 5484 ctl_done((union ctl_io *)ctsio); 5485 goto bailout; 5486 } 5487 5488 /* 5489 * Check to see whether we're configured to send the SYNCHRONIZE 5490 * CACHE command directly to the back end. 5491 */ 5492 mtx_lock(&lun->lun_lock); 5493 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5494 && (++(lun->sync_count) >= lun->sync_interval)) { 5495 lun->sync_count = 0; 5496 mtx_unlock(&lun->lun_lock); 5497 retval = lun->backend->config_write((union ctl_io *)ctsio); 5498 } else { 5499 mtx_unlock(&lun->lun_lock); 5500 ctl_set_success(ctsio); 5501 ctl_done((union ctl_io *)ctsio); 5502 } 5503 5504 bailout: 5505 5506 return (retval); 5507 } 5508 5509 int 5510 ctl_format(struct ctl_scsiio *ctsio) 5511 { 5512 struct scsi_format *cdb; 5513 struct ctl_lun *lun; 5514 struct ctl_softc *ctl_softc; 5515 int length, defect_list_len; 5516 5517 CTL_DEBUG_PRINT(("ctl_format\n")); 5518 5519 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5520 ctl_softc = control_softc; 5521 5522 cdb = (struct scsi_format *)ctsio->cdb; 5523 5524 length = 0; 5525 if (cdb->byte2 & SF_FMTDATA) { 5526 if (cdb->byte2 & SF_LONGLIST) 5527 length = sizeof(struct scsi_format_header_long); 5528 else 5529 length = sizeof(struct scsi_format_header_short); 5530 } 5531 5532 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5533 && (length > 0)) { 5534 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5535 ctsio->kern_data_len = length; 5536 ctsio->kern_total_len = length; 5537 ctsio->kern_data_resid = 0; 5538 ctsio->kern_rel_offset = 0; 5539 ctsio->kern_sg_entries = 0; 5540 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5541 ctsio->be_move_done = ctl_config_move_done; 5542 ctl_datamove((union ctl_io *)ctsio); 5543 5544 return (CTL_RETVAL_COMPLETE); 5545 } 5546 5547 defect_list_len = 0; 5548 5549 if (cdb->byte2 & SF_FMTDATA) { 5550 if (cdb->byte2 & SF_LONGLIST) { 5551 struct scsi_format_header_long *header; 5552 5553 header = (struct scsi_format_header_long *) 5554 ctsio->kern_data_ptr; 5555 5556 defect_list_len = scsi_4btoul(header->defect_list_len); 5557 if (defect_list_len != 0) { 5558 ctl_set_invalid_field(ctsio, 5559 /*sks_valid*/ 1, 5560 /*command*/ 0, 5561 /*field*/ 2, 5562 /*bit_valid*/ 0, 5563 /*bit*/ 0); 5564 goto bailout; 5565 } 5566 } else { 5567 struct scsi_format_header_short *header; 5568 5569 header = (struct scsi_format_header_short *) 5570 ctsio->kern_data_ptr; 5571 5572 defect_list_len = scsi_2btoul(header->defect_list_len); 5573 if (defect_list_len != 0) { 5574 ctl_set_invalid_field(ctsio, 5575 /*sks_valid*/ 1, 5576 /*command*/ 0, 5577 /*field*/ 2, 5578 /*bit_valid*/ 0, 5579 /*bit*/ 0); 5580 goto bailout; 5581 } 5582 } 5583 } 5584 5585 /* 5586 * The format command will clear out the "Medium format corrupted" 5587 * status if set by the configuration code. That status is really 5588 * just a way to notify the host that we have lost the media, and 5589 * get them to issue a command that will basically make them think 5590 * they're blowing away the media. 5591 */ 5592 mtx_lock(&lun->lun_lock); 5593 lun->flags &= ~CTL_LUN_INOPERABLE; 5594 mtx_unlock(&lun->lun_lock); 5595 5596 ctl_set_success(ctsio); 5597 bailout: 5598 5599 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5600 free(ctsio->kern_data_ptr, M_CTL); 5601 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5602 } 5603 5604 ctl_done((union ctl_io *)ctsio); 5605 return (CTL_RETVAL_COMPLETE); 5606 } 5607 5608 int 5609 ctl_read_buffer(struct ctl_scsiio *ctsio) 5610 { 5611 struct scsi_read_buffer *cdb; 5612 struct ctl_lun *lun; 5613 int buffer_offset, len; 5614 static uint8_t descr[4]; 5615 static uint8_t echo_descr[4] = { 0 }; 5616 5617 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5618 5619 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5620 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5621 5622 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5623 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5624 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5625 ctl_set_invalid_field(ctsio, 5626 /*sks_valid*/ 1, 5627 /*command*/ 1, 5628 /*field*/ 1, 5629 /*bit_valid*/ 1, 5630 /*bit*/ 4); 5631 ctl_done((union ctl_io *)ctsio); 5632 return (CTL_RETVAL_COMPLETE); 5633 } 5634 5635 len = scsi_3btoul(cdb->length); 5636 buffer_offset = scsi_3btoul(cdb->offset); 5637 5638 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5639 ctl_set_invalid_field(ctsio, 5640 /*sks_valid*/ 1, 5641 /*command*/ 1, 5642 /*field*/ 6, 5643 /*bit_valid*/ 0, 5644 /*bit*/ 0); 5645 ctl_done((union ctl_io *)ctsio); 5646 return (CTL_RETVAL_COMPLETE); 5647 } 5648 5649 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5650 descr[0] = 0; 5651 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5652 ctsio->kern_data_ptr = descr; 5653 len = min(len, sizeof(descr)); 5654 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5655 ctsio->kern_data_ptr = echo_descr; 5656 len = min(len, sizeof(echo_descr)); 5657 } else { 5658 if (lun->write_buffer == NULL) { 5659 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5660 M_CTL, M_WAITOK); 5661 } 5662 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5663 } 5664 ctsio->kern_data_len = len; 5665 ctsio->kern_total_len = len; 5666 ctsio->kern_data_resid = 0; 5667 ctsio->kern_rel_offset = 0; 5668 ctsio->kern_sg_entries = 0; 5669 ctl_set_success(ctsio); 5670 ctsio->be_move_done = ctl_config_move_done; 5671 ctl_datamove((union ctl_io *)ctsio); 5672 return (CTL_RETVAL_COMPLETE); 5673 } 5674 5675 int 5676 ctl_write_buffer(struct ctl_scsiio *ctsio) 5677 { 5678 struct scsi_write_buffer *cdb; 5679 struct ctl_lun *lun; 5680 int buffer_offset, len; 5681 5682 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5683 5684 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5685 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5686 5687 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5688 ctl_set_invalid_field(ctsio, 5689 /*sks_valid*/ 1, 5690 /*command*/ 1, 5691 /*field*/ 1, 5692 /*bit_valid*/ 1, 5693 /*bit*/ 4); 5694 ctl_done((union ctl_io *)ctsio); 5695 return (CTL_RETVAL_COMPLETE); 5696 } 5697 5698 len = scsi_3btoul(cdb->length); 5699 buffer_offset = scsi_3btoul(cdb->offset); 5700 5701 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5702 ctl_set_invalid_field(ctsio, 5703 /*sks_valid*/ 1, 5704 /*command*/ 1, 5705 /*field*/ 6, 5706 /*bit_valid*/ 0, 5707 /*bit*/ 0); 5708 ctl_done((union ctl_io *)ctsio); 5709 return (CTL_RETVAL_COMPLETE); 5710 } 5711 5712 /* 5713 * If we've got a kernel request that hasn't been malloced yet, 5714 * malloc it and tell the caller the data buffer is here. 5715 */ 5716 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5717 if (lun->write_buffer == NULL) { 5718 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5719 M_CTL, M_WAITOK); 5720 } 5721 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5722 ctsio->kern_data_len = len; 5723 ctsio->kern_total_len = len; 5724 ctsio->kern_data_resid = 0; 5725 ctsio->kern_rel_offset = 0; 5726 ctsio->kern_sg_entries = 0; 5727 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5728 ctsio->be_move_done = ctl_config_move_done; 5729 ctl_datamove((union ctl_io *)ctsio); 5730 5731 return (CTL_RETVAL_COMPLETE); 5732 } 5733 5734 ctl_set_success(ctsio); 5735 ctl_done((union ctl_io *)ctsio); 5736 return (CTL_RETVAL_COMPLETE); 5737 } 5738 5739 int 5740 ctl_write_same(struct ctl_scsiio *ctsio) 5741 { 5742 struct ctl_lun *lun; 5743 struct ctl_lba_len_flags *lbalen; 5744 uint64_t lba; 5745 uint32_t num_blocks; 5746 int len, retval; 5747 uint8_t byte2; 5748 5749 retval = CTL_RETVAL_COMPLETE; 5750 5751 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5752 5753 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5754 5755 switch (ctsio->cdb[0]) { 5756 case WRITE_SAME_10: { 5757 struct scsi_write_same_10 *cdb; 5758 5759 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5760 5761 lba = scsi_4btoul(cdb->addr); 5762 num_blocks = scsi_2btoul(cdb->length); 5763 byte2 = cdb->byte2; 5764 break; 5765 } 5766 case WRITE_SAME_16: { 5767 struct scsi_write_same_16 *cdb; 5768 5769 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5770 5771 lba = scsi_8btou64(cdb->addr); 5772 num_blocks = scsi_4btoul(cdb->length); 5773 byte2 = cdb->byte2; 5774 break; 5775 } 5776 default: 5777 /* 5778 * We got a command we don't support. This shouldn't 5779 * happen, commands should be filtered out above us. 5780 */ 5781 ctl_set_invalid_opcode(ctsio); 5782 ctl_done((union ctl_io *)ctsio); 5783 5784 return (CTL_RETVAL_COMPLETE); 5785 break; /* NOTREACHED */ 5786 } 5787 5788 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5789 if ((byte2 & SWS_UNMAP) == 0 && 5790 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5791 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5792 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5793 ctl_done((union ctl_io *)ctsio); 5794 return (CTL_RETVAL_COMPLETE); 5795 } 5796 5797 /* 5798 * The first check is to make sure we're in bounds, the second 5799 * check is to catch wrap-around problems. If the lba + num blocks 5800 * is less than the lba, then we've wrapped around and the block 5801 * range is invalid anyway. 5802 */ 5803 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5804 || ((lba + num_blocks) < lba)) { 5805 ctl_set_lba_out_of_range(ctsio); 5806 ctl_done((union ctl_io *)ctsio); 5807 return (CTL_RETVAL_COMPLETE); 5808 } 5809 5810 /* Zero number of blocks means "to the last logical block" */ 5811 if (num_blocks == 0) { 5812 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5813 ctl_set_invalid_field(ctsio, 5814 /*sks_valid*/ 0, 5815 /*command*/ 1, 5816 /*field*/ 0, 5817 /*bit_valid*/ 0, 5818 /*bit*/ 0); 5819 ctl_done((union ctl_io *)ctsio); 5820 return (CTL_RETVAL_COMPLETE); 5821 } 5822 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5823 } 5824 5825 len = lun->be_lun->blocksize; 5826 5827 /* 5828 * If we've got a kernel request that hasn't been malloced yet, 5829 * malloc it and tell the caller the data buffer is here. 5830 */ 5831 if ((byte2 & SWS_NDOB) == 0 && 5832 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5833 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5834 ctsio->kern_data_len = len; 5835 ctsio->kern_total_len = len; 5836 ctsio->kern_data_resid = 0; 5837 ctsio->kern_rel_offset = 0; 5838 ctsio->kern_sg_entries = 0; 5839 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5840 ctsio->be_move_done = ctl_config_move_done; 5841 ctl_datamove((union ctl_io *)ctsio); 5842 5843 return (CTL_RETVAL_COMPLETE); 5844 } 5845 5846 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5847 lbalen->lba = lba; 5848 lbalen->len = num_blocks; 5849 lbalen->flags = byte2; 5850 retval = lun->backend->config_write((union ctl_io *)ctsio); 5851 5852 return (retval); 5853 } 5854 5855 int 5856 ctl_unmap(struct ctl_scsiio *ctsio) 5857 { 5858 struct ctl_lun *lun; 5859 struct scsi_unmap *cdb; 5860 struct ctl_ptr_len_flags *ptrlen; 5861 struct scsi_unmap_header *hdr; 5862 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5863 uint64_t lba; 5864 uint32_t num_blocks; 5865 int len, retval; 5866 uint8_t byte2; 5867 5868 retval = CTL_RETVAL_COMPLETE; 5869 5870 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5871 5872 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5873 cdb = (struct scsi_unmap *)ctsio->cdb; 5874 5875 len = scsi_2btoul(cdb->length); 5876 byte2 = cdb->byte2; 5877 5878 /* 5879 * If we've got a kernel request that hasn't been malloced yet, 5880 * malloc it and tell the caller the data buffer is here. 5881 */ 5882 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5883 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5884 ctsio->kern_data_len = len; 5885 ctsio->kern_total_len = len; 5886 ctsio->kern_data_resid = 0; 5887 ctsio->kern_rel_offset = 0; 5888 ctsio->kern_sg_entries = 0; 5889 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5890 ctsio->be_move_done = ctl_config_move_done; 5891 ctl_datamove((union ctl_io *)ctsio); 5892 5893 return (CTL_RETVAL_COMPLETE); 5894 } 5895 5896 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5897 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5898 if (len < sizeof (*hdr) || 5899 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5900 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5901 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5902 ctl_set_invalid_field(ctsio, 5903 /*sks_valid*/ 0, 5904 /*command*/ 0, 5905 /*field*/ 0, 5906 /*bit_valid*/ 0, 5907 /*bit*/ 0); 5908 ctl_done((union ctl_io *)ctsio); 5909 return (CTL_RETVAL_COMPLETE); 5910 } 5911 len = scsi_2btoul(hdr->desc_length); 5912 buf = (struct scsi_unmap_desc *)(hdr + 1); 5913 end = buf + len / sizeof(*buf); 5914 5915 endnz = buf; 5916 for (range = buf; range < end; range++) { 5917 lba = scsi_8btou64(range->lba); 5918 num_blocks = scsi_4btoul(range->length); 5919 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5920 || ((lba + num_blocks) < lba)) { 5921 ctl_set_lba_out_of_range(ctsio); 5922 ctl_done((union ctl_io *)ctsio); 5923 return (CTL_RETVAL_COMPLETE); 5924 } 5925 if (num_blocks != 0) 5926 endnz = range + 1; 5927 } 5928 5929 /* 5930 * Block backend can not handle zero last range. 5931 * Filter it out and return if there is nothing left. 5932 */ 5933 len = (uint8_t *)endnz - (uint8_t *)buf; 5934 if (len == 0) { 5935 ctl_set_success(ctsio); 5936 ctl_done((union ctl_io *)ctsio); 5937 return (CTL_RETVAL_COMPLETE); 5938 } 5939 5940 mtx_lock(&lun->lun_lock); 5941 ptrlen = (struct ctl_ptr_len_flags *) 5942 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5943 ptrlen->ptr = (void *)buf; 5944 ptrlen->len = len; 5945 ptrlen->flags = byte2; 5946 ctl_check_blocked(lun); 5947 mtx_unlock(&lun->lun_lock); 5948 5949 retval = lun->backend->config_write((union ctl_io *)ctsio); 5950 return (retval); 5951 } 5952 5953 /* 5954 * Note that this function currently doesn't actually do anything inside 5955 * CTL to enforce things if the DQue bit is turned on. 5956 * 5957 * Also note that this function can't be used in the default case, because 5958 * the DQue bit isn't set in the changeable mask for the control mode page 5959 * anyway. This is just here as an example for how to implement a page 5960 * handler, and a placeholder in case we want to allow the user to turn 5961 * tagged queueing on and off. 5962 * 5963 * The D_SENSE bit handling is functional, however, and will turn 5964 * descriptor sense on and off for a given LUN. 5965 */ 5966 int 5967 ctl_control_page_handler(struct ctl_scsiio *ctsio, 5968 struct ctl_page_index *page_index, uint8_t *page_ptr) 5969 { 5970 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5971 struct ctl_lun *lun; 5972 struct ctl_softc *softc; 5973 int set_ua; 5974 uint32_t initidx; 5975 5976 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5977 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5978 set_ua = 0; 5979 5980 user_cp = (struct scsi_control_page *)page_ptr; 5981 current_cp = (struct scsi_control_page *) 5982 (page_index->page_data + (page_index->page_len * 5983 CTL_PAGE_CURRENT)); 5984 saved_cp = (struct scsi_control_page *) 5985 (page_index->page_data + (page_index->page_len * 5986 CTL_PAGE_SAVED)); 5987 5988 softc = control_softc; 5989 5990 mtx_lock(&lun->lun_lock); 5991 if (((current_cp->rlec & SCP_DSENSE) == 0) 5992 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5993 /* 5994 * Descriptor sense is currently turned off and the user 5995 * wants to turn it on. 5996 */ 5997 current_cp->rlec |= SCP_DSENSE; 5998 saved_cp->rlec |= SCP_DSENSE; 5999 lun->flags |= CTL_LUN_SENSE_DESC; 6000 set_ua = 1; 6001 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6002 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6003 /* 6004 * Descriptor sense is currently turned on, and the user 6005 * wants to turn it off. 6006 */ 6007 current_cp->rlec &= ~SCP_DSENSE; 6008 saved_cp->rlec &= ~SCP_DSENSE; 6009 lun->flags &= ~CTL_LUN_SENSE_DESC; 6010 set_ua = 1; 6011 } 6012 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6013 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6014 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6015 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6016 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6017 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6018 set_ua = 1; 6019 } 6020 if ((current_cp->eca_and_aen & SCP_SWP) != 6021 (user_cp->eca_and_aen & SCP_SWP)) { 6022 current_cp->eca_and_aen &= ~SCP_SWP; 6023 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6024 saved_cp->eca_and_aen &= ~SCP_SWP; 6025 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6026 set_ua = 1; 6027 } 6028 if (set_ua != 0) { 6029 int i; 6030 /* 6031 * Let other initiators know that the mode 6032 * parameters for this LUN have changed. 6033 */ 6034 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6035 if (i == initidx) 6036 continue; 6037 6038 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6039 } 6040 } 6041 mtx_unlock(&lun->lun_lock); 6042 6043 return (0); 6044 } 6045 6046 int 6047 ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6048 struct ctl_page_index *page_index, uint8_t *page_ptr) 6049 { 6050 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6051 struct ctl_lun *lun; 6052 int set_ua; 6053 uint32_t initidx; 6054 6055 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6056 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6057 set_ua = 0; 6058 6059 user_cp = (struct scsi_caching_page *)page_ptr; 6060 current_cp = (struct scsi_caching_page *) 6061 (page_index->page_data + (page_index->page_len * 6062 CTL_PAGE_CURRENT)); 6063 saved_cp = (struct scsi_caching_page *) 6064 (page_index->page_data + (page_index->page_len * 6065 CTL_PAGE_SAVED)); 6066 6067 mtx_lock(&lun->lun_lock); 6068 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6069 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6070 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6071 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6072 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6073 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6074 set_ua = 1; 6075 } 6076 if (set_ua != 0) { 6077 int i; 6078 /* 6079 * Let other initiators know that the mode 6080 * parameters for this LUN have changed. 6081 */ 6082 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6083 if (i == initidx) 6084 continue; 6085 6086 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6087 } 6088 } 6089 mtx_unlock(&lun->lun_lock); 6090 6091 return (0); 6092 } 6093 6094 int 6095 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6096 struct ctl_page_index *page_index, 6097 uint8_t *page_ptr) 6098 { 6099 uint8_t *c; 6100 int i; 6101 6102 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6103 ctl_time_io_secs = 6104 (c[0] << 8) | 6105 (c[1] << 0) | 6106 0; 6107 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6108 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6109 printf("page data:"); 6110 for (i=0; i<8; i++) 6111 printf(" %.2x",page_ptr[i]); 6112 printf("\n"); 6113 return (0); 6114 } 6115 6116 int 6117 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6118 struct ctl_page_index *page_index, 6119 int pc) 6120 { 6121 struct copan_debugconf_subpage *page; 6122 6123 page = (struct copan_debugconf_subpage *)page_index->page_data + 6124 (page_index->page_len * pc); 6125 6126 switch (pc) { 6127 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6128 case SMS_PAGE_CTRL_DEFAULT >> 6: 6129 case SMS_PAGE_CTRL_SAVED >> 6: 6130 /* 6131 * We don't update the changable or default bits for this page. 6132 */ 6133 break; 6134 case SMS_PAGE_CTRL_CURRENT >> 6: 6135 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6136 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6137 break; 6138 default: 6139 #ifdef NEEDTOPORT 6140 EPRINT(0, "Invalid PC %d!!", pc); 6141 #endif /* NEEDTOPORT */ 6142 break; 6143 } 6144 return (0); 6145 } 6146 6147 6148 static int 6149 ctl_do_mode_select(union ctl_io *io) 6150 { 6151 struct scsi_mode_page_header *page_header; 6152 struct ctl_page_index *page_index; 6153 struct ctl_scsiio *ctsio; 6154 int control_dev, page_len; 6155 int page_len_offset, page_len_size; 6156 union ctl_modepage_info *modepage_info; 6157 struct ctl_lun *lun; 6158 int *len_left, *len_used; 6159 int retval, i; 6160 6161 ctsio = &io->scsiio; 6162 page_index = NULL; 6163 page_len = 0; 6164 retval = CTL_RETVAL_COMPLETE; 6165 6166 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6167 6168 if (lun->be_lun->lun_type != T_DIRECT) 6169 control_dev = 1; 6170 else 6171 control_dev = 0; 6172 6173 modepage_info = (union ctl_modepage_info *) 6174 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6175 len_left = &modepage_info->header.len_left; 6176 len_used = &modepage_info->header.len_used; 6177 6178 do_next_page: 6179 6180 page_header = (struct scsi_mode_page_header *) 6181 (ctsio->kern_data_ptr + *len_used); 6182 6183 if (*len_left == 0) { 6184 free(ctsio->kern_data_ptr, M_CTL); 6185 ctl_set_success(ctsio); 6186 ctl_done((union ctl_io *)ctsio); 6187 return (CTL_RETVAL_COMPLETE); 6188 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6189 6190 free(ctsio->kern_data_ptr, M_CTL); 6191 ctl_set_param_len_error(ctsio); 6192 ctl_done((union ctl_io *)ctsio); 6193 return (CTL_RETVAL_COMPLETE); 6194 6195 } else if ((page_header->page_code & SMPH_SPF) 6196 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6197 6198 free(ctsio->kern_data_ptr, M_CTL); 6199 ctl_set_param_len_error(ctsio); 6200 ctl_done((union ctl_io *)ctsio); 6201 return (CTL_RETVAL_COMPLETE); 6202 } 6203 6204 6205 /* 6206 * XXX KDM should we do something with the block descriptor? 6207 */ 6208 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6209 6210 if ((control_dev != 0) 6211 && (lun->mode_pages.index[i].page_flags & 6212 CTL_PAGE_FLAG_DISK_ONLY)) 6213 continue; 6214 6215 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6216 (page_header->page_code & SMPH_PC_MASK)) 6217 continue; 6218 6219 /* 6220 * If neither page has a subpage code, then we've got a 6221 * match. 6222 */ 6223 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6224 && ((page_header->page_code & SMPH_SPF) == 0)) { 6225 page_index = &lun->mode_pages.index[i]; 6226 page_len = page_header->page_length; 6227 break; 6228 } 6229 6230 /* 6231 * If both pages have subpages, then the subpage numbers 6232 * have to match. 6233 */ 6234 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6235 && (page_header->page_code & SMPH_SPF)) { 6236 struct scsi_mode_page_header_sp *sph; 6237 6238 sph = (struct scsi_mode_page_header_sp *)page_header; 6239 6240 if (lun->mode_pages.index[i].subpage == 6241 sph->subpage) { 6242 page_index = &lun->mode_pages.index[i]; 6243 page_len = scsi_2btoul(sph->page_length); 6244 break; 6245 } 6246 } 6247 } 6248 6249 /* 6250 * If we couldn't find the page, or if we don't have a mode select 6251 * handler for it, send back an error to the user. 6252 */ 6253 if ((page_index == NULL) 6254 || (page_index->select_handler == NULL)) { 6255 ctl_set_invalid_field(ctsio, 6256 /*sks_valid*/ 1, 6257 /*command*/ 0, 6258 /*field*/ *len_used, 6259 /*bit_valid*/ 0, 6260 /*bit*/ 0); 6261 free(ctsio->kern_data_ptr, M_CTL); 6262 ctl_done((union ctl_io *)ctsio); 6263 return (CTL_RETVAL_COMPLETE); 6264 } 6265 6266 if (page_index->page_code & SMPH_SPF) { 6267 page_len_offset = 2; 6268 page_len_size = 2; 6269 } else { 6270 page_len_size = 1; 6271 page_len_offset = 1; 6272 } 6273 6274 /* 6275 * If the length the initiator gives us isn't the one we specify in 6276 * the mode page header, or if they didn't specify enough data in 6277 * the CDB to avoid truncating this page, kick out the request. 6278 */ 6279 if ((page_len != (page_index->page_len - page_len_offset - 6280 page_len_size)) 6281 || (*len_left < page_index->page_len)) { 6282 6283 6284 ctl_set_invalid_field(ctsio, 6285 /*sks_valid*/ 1, 6286 /*command*/ 0, 6287 /*field*/ *len_used + page_len_offset, 6288 /*bit_valid*/ 0, 6289 /*bit*/ 0); 6290 free(ctsio->kern_data_ptr, M_CTL); 6291 ctl_done((union ctl_io *)ctsio); 6292 return (CTL_RETVAL_COMPLETE); 6293 } 6294 6295 /* 6296 * Run through the mode page, checking to make sure that the bits 6297 * the user changed are actually legal for him to change. 6298 */ 6299 for (i = 0; i < page_index->page_len; i++) { 6300 uint8_t *user_byte, *change_mask, *current_byte; 6301 int bad_bit; 6302 int j; 6303 6304 user_byte = (uint8_t *)page_header + i; 6305 change_mask = page_index->page_data + 6306 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6307 current_byte = page_index->page_data + 6308 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6309 6310 /* 6311 * Check to see whether the user set any bits in this byte 6312 * that he is not allowed to set. 6313 */ 6314 if ((*user_byte & ~(*change_mask)) == 6315 (*current_byte & ~(*change_mask))) 6316 continue; 6317 6318 /* 6319 * Go through bit by bit to determine which one is illegal. 6320 */ 6321 bad_bit = 0; 6322 for (j = 7; j >= 0; j--) { 6323 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6324 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6325 bad_bit = i; 6326 break; 6327 } 6328 } 6329 ctl_set_invalid_field(ctsio, 6330 /*sks_valid*/ 1, 6331 /*command*/ 0, 6332 /*field*/ *len_used + i, 6333 /*bit_valid*/ 1, 6334 /*bit*/ bad_bit); 6335 free(ctsio->kern_data_ptr, M_CTL); 6336 ctl_done((union ctl_io *)ctsio); 6337 return (CTL_RETVAL_COMPLETE); 6338 } 6339 6340 /* 6341 * Decrement these before we call the page handler, since we may 6342 * end up getting called back one way or another before the handler 6343 * returns to this context. 6344 */ 6345 *len_left -= page_index->page_len; 6346 *len_used += page_index->page_len; 6347 6348 retval = page_index->select_handler(ctsio, page_index, 6349 (uint8_t *)page_header); 6350 6351 /* 6352 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6353 * wait until this queued command completes to finish processing 6354 * the mode page. If it returns anything other than 6355 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6356 * already set the sense information, freed the data pointer, and 6357 * completed the io for us. 6358 */ 6359 if (retval != CTL_RETVAL_COMPLETE) 6360 goto bailout_no_done; 6361 6362 /* 6363 * If the initiator sent us more than one page, parse the next one. 6364 */ 6365 if (*len_left > 0) 6366 goto do_next_page; 6367 6368 ctl_set_success(ctsio); 6369 free(ctsio->kern_data_ptr, M_CTL); 6370 ctl_done((union ctl_io *)ctsio); 6371 6372 bailout_no_done: 6373 6374 return (CTL_RETVAL_COMPLETE); 6375 6376 } 6377 6378 int 6379 ctl_mode_select(struct ctl_scsiio *ctsio) 6380 { 6381 int param_len, pf, sp; 6382 int header_size, bd_len; 6383 int len_left, len_used; 6384 struct ctl_page_index *page_index; 6385 struct ctl_lun *lun; 6386 int control_dev, page_len; 6387 union ctl_modepage_info *modepage_info; 6388 int retval; 6389 6390 pf = 0; 6391 sp = 0; 6392 page_len = 0; 6393 len_used = 0; 6394 len_left = 0; 6395 retval = 0; 6396 bd_len = 0; 6397 page_index = NULL; 6398 6399 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6400 6401 if (lun->be_lun->lun_type != T_DIRECT) 6402 control_dev = 1; 6403 else 6404 control_dev = 0; 6405 6406 switch (ctsio->cdb[0]) { 6407 case MODE_SELECT_6: { 6408 struct scsi_mode_select_6 *cdb; 6409 6410 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6411 6412 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6413 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6414 6415 param_len = cdb->length; 6416 header_size = sizeof(struct scsi_mode_header_6); 6417 break; 6418 } 6419 case MODE_SELECT_10: { 6420 struct scsi_mode_select_10 *cdb; 6421 6422 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6423 6424 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6425 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6426 6427 param_len = scsi_2btoul(cdb->length); 6428 header_size = sizeof(struct scsi_mode_header_10); 6429 break; 6430 } 6431 default: 6432 ctl_set_invalid_opcode(ctsio); 6433 ctl_done((union ctl_io *)ctsio); 6434 return (CTL_RETVAL_COMPLETE); 6435 break; /* NOTREACHED */ 6436 } 6437 6438 /* 6439 * From SPC-3: 6440 * "A parameter list length of zero indicates that the Data-Out Buffer 6441 * shall be empty. This condition shall not be considered as an error." 6442 */ 6443 if (param_len == 0) { 6444 ctl_set_success(ctsio); 6445 ctl_done((union ctl_io *)ctsio); 6446 return (CTL_RETVAL_COMPLETE); 6447 } 6448 6449 /* 6450 * Since we'll hit this the first time through, prior to 6451 * allocation, we don't need to free a data buffer here. 6452 */ 6453 if (param_len < header_size) { 6454 ctl_set_param_len_error(ctsio); 6455 ctl_done((union ctl_io *)ctsio); 6456 return (CTL_RETVAL_COMPLETE); 6457 } 6458 6459 /* 6460 * Allocate the data buffer and grab the user's data. In theory, 6461 * we shouldn't have to sanity check the parameter list length here 6462 * because the maximum size is 64K. We should be able to malloc 6463 * that much without too many problems. 6464 */ 6465 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6466 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6467 ctsio->kern_data_len = param_len; 6468 ctsio->kern_total_len = param_len; 6469 ctsio->kern_data_resid = 0; 6470 ctsio->kern_rel_offset = 0; 6471 ctsio->kern_sg_entries = 0; 6472 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6473 ctsio->be_move_done = ctl_config_move_done; 6474 ctl_datamove((union ctl_io *)ctsio); 6475 6476 return (CTL_RETVAL_COMPLETE); 6477 } 6478 6479 switch (ctsio->cdb[0]) { 6480 case MODE_SELECT_6: { 6481 struct scsi_mode_header_6 *mh6; 6482 6483 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6484 bd_len = mh6->blk_desc_len; 6485 break; 6486 } 6487 case MODE_SELECT_10: { 6488 struct scsi_mode_header_10 *mh10; 6489 6490 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6491 bd_len = scsi_2btoul(mh10->blk_desc_len); 6492 break; 6493 } 6494 default: 6495 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6496 break; 6497 } 6498 6499 if (param_len < (header_size + bd_len)) { 6500 free(ctsio->kern_data_ptr, M_CTL); 6501 ctl_set_param_len_error(ctsio); 6502 ctl_done((union ctl_io *)ctsio); 6503 return (CTL_RETVAL_COMPLETE); 6504 } 6505 6506 /* 6507 * Set the IO_CONT flag, so that if this I/O gets passed to 6508 * ctl_config_write_done(), it'll get passed back to 6509 * ctl_do_mode_select() for further processing, or completion if 6510 * we're all done. 6511 */ 6512 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6513 ctsio->io_cont = ctl_do_mode_select; 6514 6515 modepage_info = (union ctl_modepage_info *) 6516 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6517 6518 memset(modepage_info, 0, sizeof(*modepage_info)); 6519 6520 len_left = param_len - header_size - bd_len; 6521 len_used = header_size + bd_len; 6522 6523 modepage_info->header.len_left = len_left; 6524 modepage_info->header.len_used = len_used; 6525 6526 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6527 } 6528 6529 int 6530 ctl_mode_sense(struct ctl_scsiio *ctsio) 6531 { 6532 struct ctl_lun *lun; 6533 int pc, page_code, dbd, llba, subpage; 6534 int alloc_len, page_len, header_len, total_len; 6535 struct scsi_mode_block_descr *block_desc; 6536 struct ctl_page_index *page_index; 6537 int control_dev; 6538 6539 dbd = 0; 6540 llba = 0; 6541 block_desc = NULL; 6542 page_index = NULL; 6543 6544 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6545 6546 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6547 6548 if (lun->be_lun->lun_type != T_DIRECT) 6549 control_dev = 1; 6550 else 6551 control_dev = 0; 6552 6553 switch (ctsio->cdb[0]) { 6554 case MODE_SENSE_6: { 6555 struct scsi_mode_sense_6 *cdb; 6556 6557 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6558 6559 header_len = sizeof(struct scsi_mode_hdr_6); 6560 if (cdb->byte2 & SMS_DBD) 6561 dbd = 1; 6562 else 6563 header_len += sizeof(struct scsi_mode_block_descr); 6564 6565 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6566 page_code = cdb->page & SMS_PAGE_CODE; 6567 subpage = cdb->subpage; 6568 alloc_len = cdb->length; 6569 break; 6570 } 6571 case MODE_SENSE_10: { 6572 struct scsi_mode_sense_10 *cdb; 6573 6574 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6575 6576 header_len = sizeof(struct scsi_mode_hdr_10); 6577 6578 if (cdb->byte2 & SMS_DBD) 6579 dbd = 1; 6580 else 6581 header_len += sizeof(struct scsi_mode_block_descr); 6582 if (cdb->byte2 & SMS10_LLBAA) 6583 llba = 1; 6584 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6585 page_code = cdb->page & SMS_PAGE_CODE; 6586 subpage = cdb->subpage; 6587 alloc_len = scsi_2btoul(cdb->length); 6588 break; 6589 } 6590 default: 6591 ctl_set_invalid_opcode(ctsio); 6592 ctl_done((union ctl_io *)ctsio); 6593 return (CTL_RETVAL_COMPLETE); 6594 break; /* NOTREACHED */ 6595 } 6596 6597 /* 6598 * We have to make a first pass through to calculate the size of 6599 * the pages that match the user's query. Then we allocate enough 6600 * memory to hold it, and actually copy the data into the buffer. 6601 */ 6602 switch (page_code) { 6603 case SMS_ALL_PAGES_PAGE: { 6604 int i; 6605 6606 page_len = 0; 6607 6608 /* 6609 * At the moment, values other than 0 and 0xff here are 6610 * reserved according to SPC-3. 6611 */ 6612 if ((subpage != SMS_SUBPAGE_PAGE_0) 6613 && (subpage != SMS_SUBPAGE_ALL)) { 6614 ctl_set_invalid_field(ctsio, 6615 /*sks_valid*/ 1, 6616 /*command*/ 1, 6617 /*field*/ 3, 6618 /*bit_valid*/ 0, 6619 /*bit*/ 0); 6620 ctl_done((union ctl_io *)ctsio); 6621 return (CTL_RETVAL_COMPLETE); 6622 } 6623 6624 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6625 if ((control_dev != 0) 6626 && (lun->mode_pages.index[i].page_flags & 6627 CTL_PAGE_FLAG_DISK_ONLY)) 6628 continue; 6629 6630 /* 6631 * We don't use this subpage if the user didn't 6632 * request all subpages. 6633 */ 6634 if ((lun->mode_pages.index[i].subpage != 0) 6635 && (subpage == SMS_SUBPAGE_PAGE_0)) 6636 continue; 6637 6638 #if 0 6639 printf("found page %#x len %d\n", 6640 lun->mode_pages.index[i].page_code & 6641 SMPH_PC_MASK, 6642 lun->mode_pages.index[i].page_len); 6643 #endif 6644 page_len += lun->mode_pages.index[i].page_len; 6645 } 6646 break; 6647 } 6648 default: { 6649 int i; 6650 6651 page_len = 0; 6652 6653 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6654 /* Look for the right page code */ 6655 if ((lun->mode_pages.index[i].page_code & 6656 SMPH_PC_MASK) != page_code) 6657 continue; 6658 6659 /* Look for the right subpage or the subpage wildcard*/ 6660 if ((lun->mode_pages.index[i].subpage != subpage) 6661 && (subpage != SMS_SUBPAGE_ALL)) 6662 continue; 6663 6664 /* Make sure the page is supported for this dev type */ 6665 if ((control_dev != 0) 6666 && (lun->mode_pages.index[i].page_flags & 6667 CTL_PAGE_FLAG_DISK_ONLY)) 6668 continue; 6669 6670 #if 0 6671 printf("found page %#x len %d\n", 6672 lun->mode_pages.index[i].page_code & 6673 SMPH_PC_MASK, 6674 lun->mode_pages.index[i].page_len); 6675 #endif 6676 6677 page_len += lun->mode_pages.index[i].page_len; 6678 } 6679 6680 if (page_len == 0) { 6681 ctl_set_invalid_field(ctsio, 6682 /*sks_valid*/ 1, 6683 /*command*/ 1, 6684 /*field*/ 2, 6685 /*bit_valid*/ 1, 6686 /*bit*/ 5); 6687 ctl_done((union ctl_io *)ctsio); 6688 return (CTL_RETVAL_COMPLETE); 6689 } 6690 break; 6691 } 6692 } 6693 6694 total_len = header_len + page_len; 6695 #if 0 6696 printf("header_len = %d, page_len = %d, total_len = %d\n", 6697 header_len, page_len, total_len); 6698 #endif 6699 6700 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6701 ctsio->kern_sg_entries = 0; 6702 ctsio->kern_data_resid = 0; 6703 ctsio->kern_rel_offset = 0; 6704 if (total_len < alloc_len) { 6705 ctsio->residual = alloc_len - total_len; 6706 ctsio->kern_data_len = total_len; 6707 ctsio->kern_total_len = total_len; 6708 } else { 6709 ctsio->residual = 0; 6710 ctsio->kern_data_len = alloc_len; 6711 ctsio->kern_total_len = alloc_len; 6712 } 6713 6714 switch (ctsio->cdb[0]) { 6715 case MODE_SENSE_6: { 6716 struct scsi_mode_hdr_6 *header; 6717 6718 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6719 6720 header->datalen = ctl_min(total_len - 1, 254); 6721 if (control_dev == 0) { 6722 header->dev_specific = 0x10; /* DPOFUA */ 6723 if ((lun->flags & CTL_LUN_READONLY) || 6724 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6725 .eca_and_aen & SCP_SWP) != 0) 6726 header->dev_specific |= 0x80; /* WP */ 6727 } 6728 if (dbd) 6729 header->block_descr_len = 0; 6730 else 6731 header->block_descr_len = 6732 sizeof(struct scsi_mode_block_descr); 6733 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6734 break; 6735 } 6736 case MODE_SENSE_10: { 6737 struct scsi_mode_hdr_10 *header; 6738 int datalen; 6739 6740 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6741 6742 datalen = ctl_min(total_len - 2, 65533); 6743 scsi_ulto2b(datalen, header->datalen); 6744 if (control_dev == 0) { 6745 header->dev_specific = 0x10; /* DPOFUA */ 6746 if ((lun->flags & CTL_LUN_READONLY) || 6747 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6748 .eca_and_aen & SCP_SWP) != 0) 6749 header->dev_specific |= 0x80; /* WP */ 6750 } 6751 if (dbd) 6752 scsi_ulto2b(0, header->block_descr_len); 6753 else 6754 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6755 header->block_descr_len); 6756 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6757 break; 6758 } 6759 default: 6760 panic("invalid CDB type %#x", ctsio->cdb[0]); 6761 break; /* NOTREACHED */ 6762 } 6763 6764 /* 6765 * If we've got a disk, use its blocksize in the block 6766 * descriptor. Otherwise, just set it to 0. 6767 */ 6768 if (dbd == 0) { 6769 if (control_dev == 0) 6770 scsi_ulto3b(lun->be_lun->blocksize, 6771 block_desc->block_len); 6772 else 6773 scsi_ulto3b(0, block_desc->block_len); 6774 } 6775 6776 switch (page_code) { 6777 case SMS_ALL_PAGES_PAGE: { 6778 int i, data_used; 6779 6780 data_used = header_len; 6781 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6782 struct ctl_page_index *page_index; 6783 6784 page_index = &lun->mode_pages.index[i]; 6785 6786 if ((control_dev != 0) 6787 && (page_index->page_flags & 6788 CTL_PAGE_FLAG_DISK_ONLY)) 6789 continue; 6790 6791 /* 6792 * We don't use this subpage if the user didn't 6793 * request all subpages. We already checked (above) 6794 * to make sure the user only specified a subpage 6795 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6796 */ 6797 if ((page_index->subpage != 0) 6798 && (subpage == SMS_SUBPAGE_PAGE_0)) 6799 continue; 6800 6801 /* 6802 * Call the handler, if it exists, to update the 6803 * page to the latest values. 6804 */ 6805 if (page_index->sense_handler != NULL) 6806 page_index->sense_handler(ctsio, page_index,pc); 6807 6808 memcpy(ctsio->kern_data_ptr + data_used, 6809 page_index->page_data + 6810 (page_index->page_len * pc), 6811 page_index->page_len); 6812 data_used += page_index->page_len; 6813 } 6814 break; 6815 } 6816 default: { 6817 int i, data_used; 6818 6819 data_used = header_len; 6820 6821 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6822 struct ctl_page_index *page_index; 6823 6824 page_index = &lun->mode_pages.index[i]; 6825 6826 /* Look for the right page code */ 6827 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6828 continue; 6829 6830 /* Look for the right subpage or the subpage wildcard*/ 6831 if ((page_index->subpage != subpage) 6832 && (subpage != SMS_SUBPAGE_ALL)) 6833 continue; 6834 6835 /* Make sure the page is supported for this dev type */ 6836 if ((control_dev != 0) 6837 && (page_index->page_flags & 6838 CTL_PAGE_FLAG_DISK_ONLY)) 6839 continue; 6840 6841 /* 6842 * Call the handler, if it exists, to update the 6843 * page to the latest values. 6844 */ 6845 if (page_index->sense_handler != NULL) 6846 page_index->sense_handler(ctsio, page_index,pc); 6847 6848 memcpy(ctsio->kern_data_ptr + data_used, 6849 page_index->page_data + 6850 (page_index->page_len * pc), 6851 page_index->page_len); 6852 data_used += page_index->page_len; 6853 } 6854 break; 6855 } 6856 } 6857 6858 ctl_set_success(ctsio); 6859 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6860 ctsio->be_move_done = ctl_config_move_done; 6861 ctl_datamove((union ctl_io *)ctsio); 6862 return (CTL_RETVAL_COMPLETE); 6863 } 6864 6865 int 6866 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6867 struct ctl_page_index *page_index, 6868 int pc) 6869 { 6870 struct ctl_lun *lun; 6871 struct scsi_log_param_header *phdr; 6872 uint8_t *data; 6873 uint64_t val; 6874 6875 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6876 data = page_index->page_data; 6877 6878 if (lun->backend->lun_attr != NULL && 6879 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6880 != UINT64_MAX) { 6881 phdr = (struct scsi_log_param_header *)data; 6882 scsi_ulto2b(0x0001, phdr->param_code); 6883 phdr->param_control = SLP_LBIN | SLP_LP; 6884 phdr->param_len = 8; 6885 data = (uint8_t *)(phdr + 1); 6886 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6887 data[4] = 0x01; /* per-LUN */ 6888 data += phdr->param_len; 6889 } 6890 6891 if (lun->backend->lun_attr != NULL && 6892 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6893 != UINT64_MAX) { 6894 phdr = (struct scsi_log_param_header *)data; 6895 scsi_ulto2b(0x0002, phdr->param_code); 6896 phdr->param_control = SLP_LBIN | SLP_LP; 6897 phdr->param_len = 8; 6898 data = (uint8_t *)(phdr + 1); 6899 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6900 data[4] = 0x02; /* per-pool */ 6901 data += phdr->param_len; 6902 } 6903 6904 if (lun->backend->lun_attr != NULL && 6905 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6906 != UINT64_MAX) { 6907 phdr = (struct scsi_log_param_header *)data; 6908 scsi_ulto2b(0x00f1, phdr->param_code); 6909 phdr->param_control = SLP_LBIN | SLP_LP; 6910 phdr->param_len = 8; 6911 data = (uint8_t *)(phdr + 1); 6912 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6913 data[4] = 0x02; /* per-pool */ 6914 data += phdr->param_len; 6915 } 6916 6917 if (lun->backend->lun_attr != NULL && 6918 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6919 != UINT64_MAX) { 6920 phdr = (struct scsi_log_param_header *)data; 6921 scsi_ulto2b(0x00f2, phdr->param_code); 6922 phdr->param_control = SLP_LBIN | SLP_LP; 6923 phdr->param_len = 8; 6924 data = (uint8_t *)(phdr + 1); 6925 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6926 data[4] = 0x02; /* per-pool */ 6927 data += phdr->param_len; 6928 } 6929 6930 page_index->page_len = data - page_index->page_data; 6931 return (0); 6932 } 6933 6934 int 6935 ctl_log_sense(struct ctl_scsiio *ctsio) 6936 { 6937 struct ctl_lun *lun; 6938 int i, pc, page_code, subpage; 6939 int alloc_len, total_len; 6940 struct ctl_page_index *page_index; 6941 struct scsi_log_sense *cdb; 6942 struct scsi_log_header *header; 6943 6944 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6945 6946 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6947 cdb = (struct scsi_log_sense *)ctsio->cdb; 6948 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6949 page_code = cdb->page & SLS_PAGE_CODE; 6950 subpage = cdb->subpage; 6951 alloc_len = scsi_2btoul(cdb->length); 6952 6953 page_index = NULL; 6954 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6955 page_index = &lun->log_pages.index[i]; 6956 6957 /* Look for the right page code */ 6958 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6959 continue; 6960 6961 /* Look for the right subpage or the subpage wildcard*/ 6962 if (page_index->subpage != subpage) 6963 continue; 6964 6965 break; 6966 } 6967 if (i >= CTL_NUM_LOG_PAGES) { 6968 ctl_set_invalid_field(ctsio, 6969 /*sks_valid*/ 1, 6970 /*command*/ 1, 6971 /*field*/ 2, 6972 /*bit_valid*/ 0, 6973 /*bit*/ 0); 6974 ctl_done((union ctl_io *)ctsio); 6975 return (CTL_RETVAL_COMPLETE); 6976 } 6977 6978 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6979 6980 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6981 ctsio->kern_sg_entries = 0; 6982 ctsio->kern_data_resid = 0; 6983 ctsio->kern_rel_offset = 0; 6984 if (total_len < alloc_len) { 6985 ctsio->residual = alloc_len - total_len; 6986 ctsio->kern_data_len = total_len; 6987 ctsio->kern_total_len = total_len; 6988 } else { 6989 ctsio->residual = 0; 6990 ctsio->kern_data_len = alloc_len; 6991 ctsio->kern_total_len = alloc_len; 6992 } 6993 6994 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6995 header->page = page_index->page_code; 6996 if (page_index->subpage) { 6997 header->page |= SL_SPF; 6998 header->subpage = page_index->subpage; 6999 } 7000 scsi_ulto2b(page_index->page_len, header->datalen); 7001 7002 /* 7003 * Call the handler, if it exists, to update the 7004 * page to the latest values. 7005 */ 7006 if (page_index->sense_handler != NULL) 7007 page_index->sense_handler(ctsio, page_index, pc); 7008 7009 memcpy(header + 1, page_index->page_data, page_index->page_len); 7010 7011 ctl_set_success(ctsio); 7012 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7013 ctsio->be_move_done = ctl_config_move_done; 7014 ctl_datamove((union ctl_io *)ctsio); 7015 return (CTL_RETVAL_COMPLETE); 7016 } 7017 7018 int 7019 ctl_read_capacity(struct ctl_scsiio *ctsio) 7020 { 7021 struct scsi_read_capacity *cdb; 7022 struct scsi_read_capacity_data *data; 7023 struct ctl_lun *lun; 7024 uint32_t lba; 7025 7026 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7027 7028 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7029 7030 lba = scsi_4btoul(cdb->addr); 7031 if (((cdb->pmi & SRC_PMI) == 0) 7032 && (lba != 0)) { 7033 ctl_set_invalid_field(/*ctsio*/ ctsio, 7034 /*sks_valid*/ 1, 7035 /*command*/ 1, 7036 /*field*/ 2, 7037 /*bit_valid*/ 0, 7038 /*bit*/ 0); 7039 ctl_done((union ctl_io *)ctsio); 7040 return (CTL_RETVAL_COMPLETE); 7041 } 7042 7043 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7044 7045 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7046 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7047 ctsio->residual = 0; 7048 ctsio->kern_data_len = sizeof(*data); 7049 ctsio->kern_total_len = sizeof(*data); 7050 ctsio->kern_data_resid = 0; 7051 ctsio->kern_rel_offset = 0; 7052 ctsio->kern_sg_entries = 0; 7053 7054 /* 7055 * If the maximum LBA is greater than 0xfffffffe, the user must 7056 * issue a SERVICE ACTION IN (16) command, with the read capacity 7057 * serivce action set. 7058 */ 7059 if (lun->be_lun->maxlba > 0xfffffffe) 7060 scsi_ulto4b(0xffffffff, data->addr); 7061 else 7062 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7063 7064 /* 7065 * XXX KDM this may not be 512 bytes... 7066 */ 7067 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7068 7069 ctl_set_success(ctsio); 7070 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7071 ctsio->be_move_done = ctl_config_move_done; 7072 ctl_datamove((union ctl_io *)ctsio); 7073 return (CTL_RETVAL_COMPLETE); 7074 } 7075 7076 int 7077 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7078 { 7079 struct scsi_read_capacity_16 *cdb; 7080 struct scsi_read_capacity_data_long *data; 7081 struct ctl_lun *lun; 7082 uint64_t lba; 7083 uint32_t alloc_len; 7084 7085 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7086 7087 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7088 7089 alloc_len = scsi_4btoul(cdb->alloc_len); 7090 lba = scsi_8btou64(cdb->addr); 7091 7092 if ((cdb->reladr & SRC16_PMI) 7093 && (lba != 0)) { 7094 ctl_set_invalid_field(/*ctsio*/ ctsio, 7095 /*sks_valid*/ 1, 7096 /*command*/ 1, 7097 /*field*/ 2, 7098 /*bit_valid*/ 0, 7099 /*bit*/ 0); 7100 ctl_done((union ctl_io *)ctsio); 7101 return (CTL_RETVAL_COMPLETE); 7102 } 7103 7104 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7105 7106 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7107 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7108 7109 if (sizeof(*data) < alloc_len) { 7110 ctsio->residual = alloc_len - sizeof(*data); 7111 ctsio->kern_data_len = sizeof(*data); 7112 ctsio->kern_total_len = sizeof(*data); 7113 } else { 7114 ctsio->residual = 0; 7115 ctsio->kern_data_len = alloc_len; 7116 ctsio->kern_total_len = alloc_len; 7117 } 7118 ctsio->kern_data_resid = 0; 7119 ctsio->kern_rel_offset = 0; 7120 ctsio->kern_sg_entries = 0; 7121 7122 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7123 /* XXX KDM this may not be 512 bytes... */ 7124 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7125 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7126 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7127 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7128 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7129 7130 ctl_set_success(ctsio); 7131 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7132 ctsio->be_move_done = ctl_config_move_done; 7133 ctl_datamove((union ctl_io *)ctsio); 7134 return (CTL_RETVAL_COMPLETE); 7135 } 7136 7137 int 7138 ctl_read_defect(struct ctl_scsiio *ctsio) 7139 { 7140 struct scsi_read_defect_data_10 *ccb10; 7141 struct scsi_read_defect_data_12 *ccb12; 7142 struct scsi_read_defect_data_hdr_10 *data10; 7143 struct scsi_read_defect_data_hdr_12 *data12; 7144 uint32_t alloc_len, data_len; 7145 uint8_t format; 7146 7147 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7148 7149 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7150 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7151 format = ccb10->format; 7152 alloc_len = scsi_2btoul(ccb10->alloc_length); 7153 data_len = sizeof(*data10); 7154 } else { 7155 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7156 format = ccb12->format; 7157 alloc_len = scsi_4btoul(ccb12->alloc_length); 7158 data_len = sizeof(*data12); 7159 } 7160 if (alloc_len == 0) { 7161 ctl_set_success(ctsio); 7162 ctl_done((union ctl_io *)ctsio); 7163 return (CTL_RETVAL_COMPLETE); 7164 } 7165 7166 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7167 if (data_len < alloc_len) { 7168 ctsio->residual = alloc_len - data_len; 7169 ctsio->kern_data_len = data_len; 7170 ctsio->kern_total_len = data_len; 7171 } else { 7172 ctsio->residual = 0; 7173 ctsio->kern_data_len = alloc_len; 7174 ctsio->kern_total_len = alloc_len; 7175 } 7176 ctsio->kern_data_resid = 0; 7177 ctsio->kern_rel_offset = 0; 7178 ctsio->kern_sg_entries = 0; 7179 7180 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7181 data10 = (struct scsi_read_defect_data_hdr_10 *) 7182 ctsio->kern_data_ptr; 7183 data10->format = format; 7184 scsi_ulto2b(0, data10->length); 7185 } else { 7186 data12 = (struct scsi_read_defect_data_hdr_12 *) 7187 ctsio->kern_data_ptr; 7188 data12->format = format; 7189 scsi_ulto2b(0, data12->generation); 7190 scsi_ulto4b(0, data12->length); 7191 } 7192 7193 ctl_set_success(ctsio); 7194 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7195 ctsio->be_move_done = ctl_config_move_done; 7196 ctl_datamove((union ctl_io *)ctsio); 7197 return (CTL_RETVAL_COMPLETE); 7198 } 7199 7200 int 7201 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7202 { 7203 struct scsi_maintenance_in *cdb; 7204 int retval; 7205 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 7206 int num_target_port_groups, num_target_ports; 7207 struct ctl_lun *lun; 7208 struct ctl_softc *softc; 7209 struct ctl_port *port; 7210 struct scsi_target_group_data *rtg_ptr; 7211 struct scsi_target_group_data_extended *rtg_ext_ptr; 7212 struct scsi_target_port_group_descriptor *tpg_desc; 7213 7214 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7215 7216 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7217 softc = control_softc; 7218 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7219 7220 retval = CTL_RETVAL_COMPLETE; 7221 7222 switch (cdb->byte2 & STG_PDF_MASK) { 7223 case STG_PDF_LENGTH: 7224 ext = 0; 7225 break; 7226 case STG_PDF_EXTENDED: 7227 ext = 1; 7228 break; 7229 default: 7230 ctl_set_invalid_field(/*ctsio*/ ctsio, 7231 /*sks_valid*/ 1, 7232 /*command*/ 1, 7233 /*field*/ 2, 7234 /*bit_valid*/ 1, 7235 /*bit*/ 5); 7236 ctl_done((union ctl_io *)ctsio); 7237 return(retval); 7238 } 7239 7240 if (softc->is_single) 7241 num_target_port_groups = 1; 7242 else 7243 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7244 num_target_ports = 0; 7245 mtx_lock(&softc->ctl_lock); 7246 STAILQ_FOREACH(port, &softc->port_list, links) { 7247 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7248 continue; 7249 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7250 continue; 7251 num_target_ports++; 7252 } 7253 mtx_unlock(&softc->ctl_lock); 7254 7255 if (ext) 7256 total_len = sizeof(struct scsi_target_group_data_extended); 7257 else 7258 total_len = sizeof(struct scsi_target_group_data); 7259 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7260 num_target_port_groups + 7261 sizeof(struct scsi_target_port_descriptor) * 7262 num_target_ports * num_target_port_groups; 7263 7264 alloc_len = scsi_4btoul(cdb->length); 7265 7266 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7267 7268 ctsio->kern_sg_entries = 0; 7269 7270 if (total_len < alloc_len) { 7271 ctsio->residual = alloc_len - total_len; 7272 ctsio->kern_data_len = total_len; 7273 ctsio->kern_total_len = total_len; 7274 } else { 7275 ctsio->residual = 0; 7276 ctsio->kern_data_len = alloc_len; 7277 ctsio->kern_total_len = alloc_len; 7278 } 7279 ctsio->kern_data_resid = 0; 7280 ctsio->kern_rel_offset = 0; 7281 7282 if (ext) { 7283 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7284 ctsio->kern_data_ptr; 7285 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7286 rtg_ext_ptr->format_type = 0x10; 7287 rtg_ext_ptr->implicit_transition_time = 0; 7288 tpg_desc = &rtg_ext_ptr->groups[0]; 7289 } else { 7290 rtg_ptr = (struct scsi_target_group_data *) 7291 ctsio->kern_data_ptr; 7292 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7293 tpg_desc = &rtg_ptr->groups[0]; 7294 } 7295 7296 mtx_lock(&softc->ctl_lock); 7297 pg = softc->port_offset / CTL_MAX_PORTS; 7298 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 7299 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 7300 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7301 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7302 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 7303 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7304 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7305 } else { 7306 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7307 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7308 } 7309 } else { 7310 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7311 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7312 } 7313 for (g = 0; g < num_target_port_groups; g++) { 7314 tpg_desc->pref_state = (g == pg) ? gs : os; 7315 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 7316 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7317 tpg_desc->status = TPG_IMPLICIT; 7318 pc = 0; 7319 STAILQ_FOREACH(port, &softc->port_list, links) { 7320 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7321 continue; 7322 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7323 CTL_MAX_LUNS) 7324 continue; 7325 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7326 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7327 relative_target_port_identifier); 7328 pc++; 7329 } 7330 tpg_desc->target_port_count = pc; 7331 tpg_desc = (struct scsi_target_port_group_descriptor *) 7332 &tpg_desc->descriptors[pc]; 7333 } 7334 mtx_unlock(&softc->ctl_lock); 7335 7336 ctl_set_success(ctsio); 7337 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7338 ctsio->be_move_done = ctl_config_move_done; 7339 ctl_datamove((union ctl_io *)ctsio); 7340 return(retval); 7341 } 7342 7343 int 7344 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7345 { 7346 struct ctl_lun *lun; 7347 struct scsi_report_supported_opcodes *cdb; 7348 const struct ctl_cmd_entry *entry, *sentry; 7349 struct scsi_report_supported_opcodes_all *all; 7350 struct scsi_report_supported_opcodes_descr *descr; 7351 struct scsi_report_supported_opcodes_one *one; 7352 int retval; 7353 int alloc_len, total_len; 7354 int opcode, service_action, i, j, num; 7355 7356 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7357 7358 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7359 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7360 7361 retval = CTL_RETVAL_COMPLETE; 7362 7363 opcode = cdb->requested_opcode; 7364 service_action = scsi_2btoul(cdb->requested_service_action); 7365 switch (cdb->options & RSO_OPTIONS_MASK) { 7366 case RSO_OPTIONS_ALL: 7367 num = 0; 7368 for (i = 0; i < 256; i++) { 7369 entry = &ctl_cmd_table[i]; 7370 if (entry->flags & CTL_CMD_FLAG_SA5) { 7371 for (j = 0; j < 32; j++) { 7372 sentry = &((const struct ctl_cmd_entry *) 7373 entry->execute)[j]; 7374 if (ctl_cmd_applicable( 7375 lun->be_lun->lun_type, sentry)) 7376 num++; 7377 } 7378 } else { 7379 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7380 entry)) 7381 num++; 7382 } 7383 } 7384 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7385 num * sizeof(struct scsi_report_supported_opcodes_descr); 7386 break; 7387 case RSO_OPTIONS_OC: 7388 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7389 ctl_set_invalid_field(/*ctsio*/ ctsio, 7390 /*sks_valid*/ 1, 7391 /*command*/ 1, 7392 /*field*/ 2, 7393 /*bit_valid*/ 1, 7394 /*bit*/ 2); 7395 ctl_done((union ctl_io *)ctsio); 7396 return (CTL_RETVAL_COMPLETE); 7397 } 7398 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7399 break; 7400 case RSO_OPTIONS_OC_SA: 7401 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7402 service_action >= 32) { 7403 ctl_set_invalid_field(/*ctsio*/ ctsio, 7404 /*sks_valid*/ 1, 7405 /*command*/ 1, 7406 /*field*/ 2, 7407 /*bit_valid*/ 1, 7408 /*bit*/ 2); 7409 ctl_done((union ctl_io *)ctsio); 7410 return (CTL_RETVAL_COMPLETE); 7411 } 7412 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7413 break; 7414 default: 7415 ctl_set_invalid_field(/*ctsio*/ ctsio, 7416 /*sks_valid*/ 1, 7417 /*command*/ 1, 7418 /*field*/ 2, 7419 /*bit_valid*/ 1, 7420 /*bit*/ 2); 7421 ctl_done((union ctl_io *)ctsio); 7422 return (CTL_RETVAL_COMPLETE); 7423 } 7424 7425 alloc_len = scsi_4btoul(cdb->length); 7426 7427 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7428 7429 ctsio->kern_sg_entries = 0; 7430 7431 if (total_len < alloc_len) { 7432 ctsio->residual = alloc_len - total_len; 7433 ctsio->kern_data_len = total_len; 7434 ctsio->kern_total_len = total_len; 7435 } else { 7436 ctsio->residual = 0; 7437 ctsio->kern_data_len = alloc_len; 7438 ctsio->kern_total_len = alloc_len; 7439 } 7440 ctsio->kern_data_resid = 0; 7441 ctsio->kern_rel_offset = 0; 7442 7443 switch (cdb->options & RSO_OPTIONS_MASK) { 7444 case RSO_OPTIONS_ALL: 7445 all = (struct scsi_report_supported_opcodes_all *) 7446 ctsio->kern_data_ptr; 7447 num = 0; 7448 for (i = 0; i < 256; i++) { 7449 entry = &ctl_cmd_table[i]; 7450 if (entry->flags & CTL_CMD_FLAG_SA5) { 7451 for (j = 0; j < 32; j++) { 7452 sentry = &((const struct ctl_cmd_entry *) 7453 entry->execute)[j]; 7454 if (!ctl_cmd_applicable( 7455 lun->be_lun->lun_type, sentry)) 7456 continue; 7457 descr = &all->descr[num++]; 7458 descr->opcode = i; 7459 scsi_ulto2b(j, descr->service_action); 7460 descr->flags = RSO_SERVACTV; 7461 scsi_ulto2b(sentry->length, 7462 descr->cdb_length); 7463 } 7464 } else { 7465 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7466 entry)) 7467 continue; 7468 descr = &all->descr[num++]; 7469 descr->opcode = i; 7470 scsi_ulto2b(0, descr->service_action); 7471 descr->flags = 0; 7472 scsi_ulto2b(entry->length, descr->cdb_length); 7473 } 7474 } 7475 scsi_ulto4b( 7476 num * sizeof(struct scsi_report_supported_opcodes_descr), 7477 all->length); 7478 break; 7479 case RSO_OPTIONS_OC: 7480 one = (struct scsi_report_supported_opcodes_one *) 7481 ctsio->kern_data_ptr; 7482 entry = &ctl_cmd_table[opcode]; 7483 goto fill_one; 7484 case RSO_OPTIONS_OC_SA: 7485 one = (struct scsi_report_supported_opcodes_one *) 7486 ctsio->kern_data_ptr; 7487 entry = &ctl_cmd_table[opcode]; 7488 entry = &((const struct ctl_cmd_entry *) 7489 entry->execute)[service_action]; 7490 fill_one: 7491 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7492 one->support = 3; 7493 scsi_ulto2b(entry->length, one->cdb_length); 7494 one->cdb_usage[0] = opcode; 7495 memcpy(&one->cdb_usage[1], entry->usage, 7496 entry->length - 1); 7497 } else 7498 one->support = 1; 7499 break; 7500 } 7501 7502 ctl_set_success(ctsio); 7503 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7504 ctsio->be_move_done = ctl_config_move_done; 7505 ctl_datamove((union ctl_io *)ctsio); 7506 return(retval); 7507 } 7508 7509 int 7510 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7511 { 7512 struct scsi_report_supported_tmf *cdb; 7513 struct scsi_report_supported_tmf_data *data; 7514 int retval; 7515 int alloc_len, total_len; 7516 7517 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7518 7519 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7520 7521 retval = CTL_RETVAL_COMPLETE; 7522 7523 total_len = sizeof(struct scsi_report_supported_tmf_data); 7524 alloc_len = scsi_4btoul(cdb->length); 7525 7526 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7527 7528 ctsio->kern_sg_entries = 0; 7529 7530 if (total_len < alloc_len) { 7531 ctsio->residual = alloc_len - total_len; 7532 ctsio->kern_data_len = total_len; 7533 ctsio->kern_total_len = total_len; 7534 } else { 7535 ctsio->residual = 0; 7536 ctsio->kern_data_len = alloc_len; 7537 ctsio->kern_total_len = alloc_len; 7538 } 7539 ctsio->kern_data_resid = 0; 7540 ctsio->kern_rel_offset = 0; 7541 7542 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7543 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7544 data->byte2 |= RST_ITNRS; 7545 7546 ctl_set_success(ctsio); 7547 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7548 ctsio->be_move_done = ctl_config_move_done; 7549 ctl_datamove((union ctl_io *)ctsio); 7550 return (retval); 7551 } 7552 7553 int 7554 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7555 { 7556 struct scsi_report_timestamp *cdb; 7557 struct scsi_report_timestamp_data *data; 7558 struct timeval tv; 7559 int64_t timestamp; 7560 int retval; 7561 int alloc_len, total_len; 7562 7563 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7564 7565 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7566 7567 retval = CTL_RETVAL_COMPLETE; 7568 7569 total_len = sizeof(struct scsi_report_timestamp_data); 7570 alloc_len = scsi_4btoul(cdb->length); 7571 7572 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7573 7574 ctsio->kern_sg_entries = 0; 7575 7576 if (total_len < alloc_len) { 7577 ctsio->residual = alloc_len - total_len; 7578 ctsio->kern_data_len = total_len; 7579 ctsio->kern_total_len = total_len; 7580 } else { 7581 ctsio->residual = 0; 7582 ctsio->kern_data_len = alloc_len; 7583 ctsio->kern_total_len = alloc_len; 7584 } 7585 ctsio->kern_data_resid = 0; 7586 ctsio->kern_rel_offset = 0; 7587 7588 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7589 scsi_ulto2b(sizeof(*data) - 2, data->length); 7590 data->origin = RTS_ORIG_OUTSIDE; 7591 getmicrotime(&tv); 7592 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7593 scsi_ulto4b(timestamp >> 16, data->timestamp); 7594 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7595 7596 ctl_set_success(ctsio); 7597 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7598 ctsio->be_move_done = ctl_config_move_done; 7599 ctl_datamove((union ctl_io *)ctsio); 7600 return (retval); 7601 } 7602 7603 int 7604 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7605 { 7606 struct scsi_per_res_in *cdb; 7607 int alloc_len, total_len = 0; 7608 /* struct scsi_per_res_in_rsrv in_data; */ 7609 struct ctl_lun *lun; 7610 struct ctl_softc *softc; 7611 7612 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7613 7614 softc = control_softc; 7615 7616 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7617 7618 alloc_len = scsi_2btoul(cdb->length); 7619 7620 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7621 7622 retry: 7623 mtx_lock(&lun->lun_lock); 7624 switch (cdb->action) { 7625 case SPRI_RK: /* read keys */ 7626 total_len = sizeof(struct scsi_per_res_in_keys) + 7627 lun->pr_key_count * 7628 sizeof(struct scsi_per_res_key); 7629 break; 7630 case SPRI_RR: /* read reservation */ 7631 if (lun->flags & CTL_LUN_PR_RESERVED) 7632 total_len = sizeof(struct scsi_per_res_in_rsrv); 7633 else 7634 total_len = sizeof(struct scsi_per_res_in_header); 7635 break; 7636 case SPRI_RC: /* report capabilities */ 7637 total_len = sizeof(struct scsi_per_res_cap); 7638 break; 7639 case SPRI_RS: /* read full status */ 7640 total_len = sizeof(struct scsi_per_res_in_header) + 7641 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7642 lun->pr_key_count; 7643 break; 7644 default: 7645 panic("Invalid PR type %x", cdb->action); 7646 } 7647 mtx_unlock(&lun->lun_lock); 7648 7649 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7650 7651 if (total_len < alloc_len) { 7652 ctsio->residual = alloc_len - total_len; 7653 ctsio->kern_data_len = total_len; 7654 ctsio->kern_total_len = total_len; 7655 } else { 7656 ctsio->residual = 0; 7657 ctsio->kern_data_len = alloc_len; 7658 ctsio->kern_total_len = alloc_len; 7659 } 7660 7661 ctsio->kern_data_resid = 0; 7662 ctsio->kern_rel_offset = 0; 7663 ctsio->kern_sg_entries = 0; 7664 7665 mtx_lock(&lun->lun_lock); 7666 switch (cdb->action) { 7667 case SPRI_RK: { // read keys 7668 struct scsi_per_res_in_keys *res_keys; 7669 int i, key_count; 7670 7671 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7672 7673 /* 7674 * We had to drop the lock to allocate our buffer, which 7675 * leaves time for someone to come in with another 7676 * persistent reservation. (That is unlikely, though, 7677 * since this should be the only persistent reservation 7678 * command active right now.) 7679 */ 7680 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7681 (lun->pr_key_count * 7682 sizeof(struct scsi_per_res_key)))){ 7683 mtx_unlock(&lun->lun_lock); 7684 free(ctsio->kern_data_ptr, M_CTL); 7685 printf("%s: reservation length changed, retrying\n", 7686 __func__); 7687 goto retry; 7688 } 7689 7690 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7691 7692 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7693 lun->pr_key_count, res_keys->header.length); 7694 7695 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7696 if (lun->pr_keys[i] == 0) 7697 continue; 7698 7699 /* 7700 * We used lun->pr_key_count to calculate the 7701 * size to allocate. If it turns out the number of 7702 * initiators with the registered flag set is 7703 * larger than that (i.e. they haven't been kept in 7704 * sync), we've got a problem. 7705 */ 7706 if (key_count >= lun->pr_key_count) { 7707 #ifdef NEEDTOPORT 7708 csevent_log(CSC_CTL | CSC_SHELF_SW | 7709 CTL_PR_ERROR, 7710 csevent_LogType_Fault, 7711 csevent_AlertLevel_Yellow, 7712 csevent_FRU_ShelfController, 7713 csevent_FRU_Firmware, 7714 csevent_FRU_Unknown, 7715 "registered keys %d >= key " 7716 "count %d", key_count, 7717 lun->pr_key_count); 7718 #endif 7719 key_count++; 7720 continue; 7721 } 7722 scsi_u64to8b(lun->pr_keys[i], 7723 res_keys->keys[key_count].key); 7724 key_count++; 7725 } 7726 break; 7727 } 7728 case SPRI_RR: { // read reservation 7729 struct scsi_per_res_in_rsrv *res; 7730 int tmp_len, header_only; 7731 7732 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7733 7734 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7735 7736 if (lun->flags & CTL_LUN_PR_RESERVED) 7737 { 7738 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7739 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7740 res->header.length); 7741 header_only = 0; 7742 } else { 7743 tmp_len = sizeof(struct scsi_per_res_in_header); 7744 scsi_ulto4b(0, res->header.length); 7745 header_only = 1; 7746 } 7747 7748 /* 7749 * We had to drop the lock to allocate our buffer, which 7750 * leaves time for someone to come in with another 7751 * persistent reservation. (That is unlikely, though, 7752 * since this should be the only persistent reservation 7753 * command active right now.) 7754 */ 7755 if (tmp_len != total_len) { 7756 mtx_unlock(&lun->lun_lock); 7757 free(ctsio->kern_data_ptr, M_CTL); 7758 printf("%s: reservation status changed, retrying\n", 7759 __func__); 7760 goto retry; 7761 } 7762 7763 /* 7764 * No reservation held, so we're done. 7765 */ 7766 if (header_only != 0) 7767 break; 7768 7769 /* 7770 * If the registration is an All Registrants type, the key 7771 * is 0, since it doesn't really matter. 7772 */ 7773 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7774 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx], 7775 res->data.reservation); 7776 } 7777 res->data.scopetype = lun->res_type; 7778 break; 7779 } 7780 case SPRI_RC: //report capabilities 7781 { 7782 struct scsi_per_res_cap *res_cap; 7783 uint16_t type_mask; 7784 7785 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7786 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7787 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7788 type_mask = SPRI_TM_WR_EX_AR | 7789 SPRI_TM_EX_AC_RO | 7790 SPRI_TM_WR_EX_RO | 7791 SPRI_TM_EX_AC | 7792 SPRI_TM_WR_EX | 7793 SPRI_TM_EX_AC_AR; 7794 scsi_ulto2b(type_mask, res_cap->type_mask); 7795 break; 7796 } 7797 case SPRI_RS: { // read full status 7798 struct scsi_per_res_in_full *res_status; 7799 struct scsi_per_res_in_full_desc *res_desc; 7800 struct ctl_port *port; 7801 int i, len; 7802 7803 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7804 7805 /* 7806 * We had to drop the lock to allocate our buffer, which 7807 * leaves time for someone to come in with another 7808 * persistent reservation. (That is unlikely, though, 7809 * since this should be the only persistent reservation 7810 * command active right now.) 7811 */ 7812 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7813 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7814 lun->pr_key_count)){ 7815 mtx_unlock(&lun->lun_lock); 7816 free(ctsio->kern_data_ptr, M_CTL); 7817 printf("%s: reservation length changed, retrying\n", 7818 __func__); 7819 goto retry; 7820 } 7821 7822 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7823 7824 res_desc = &res_status->desc[0]; 7825 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7826 if (lun->pr_keys[i] == 0) 7827 continue; 7828 7829 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key); 7830 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7831 (lun->pr_res_idx == i || 7832 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7833 res_desc->flags = SPRI_FULL_R_HOLDER; 7834 res_desc->scopetype = lun->res_type; 7835 } 7836 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7837 res_desc->rel_trgt_port_id); 7838 len = 0; 7839 port = softc->ctl_ports[ 7840 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7841 if (port != NULL) 7842 len = ctl_create_iid(port, 7843 i % CTL_MAX_INIT_PER_PORT, 7844 res_desc->transport_id); 7845 scsi_ulto4b(len, res_desc->additional_length); 7846 res_desc = (struct scsi_per_res_in_full_desc *) 7847 &res_desc->transport_id[len]; 7848 } 7849 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7850 res_status->header.length); 7851 break; 7852 } 7853 default: 7854 /* 7855 * This is a bug, because we just checked for this above, 7856 * and should have returned an error. 7857 */ 7858 panic("Invalid PR type %x", cdb->action); 7859 break; /* NOTREACHED */ 7860 } 7861 mtx_unlock(&lun->lun_lock); 7862 7863 ctl_set_success(ctsio); 7864 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7865 ctsio->be_move_done = ctl_config_move_done; 7866 ctl_datamove((union ctl_io *)ctsio); 7867 return (CTL_RETVAL_COMPLETE); 7868 } 7869 7870 /* 7871 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7872 * it should return. 7873 */ 7874 static int 7875 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7876 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7877 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7878 struct scsi_per_res_out_parms* param) 7879 { 7880 union ctl_ha_msg persis_io; 7881 int retval, i; 7882 int isc_retval; 7883 7884 retval = 0; 7885 7886 mtx_lock(&lun->lun_lock); 7887 if (sa_res_key == 0) { 7888 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7889 /* validate scope and type */ 7890 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7891 SPR_LU_SCOPE) { 7892 mtx_unlock(&lun->lun_lock); 7893 ctl_set_invalid_field(/*ctsio*/ ctsio, 7894 /*sks_valid*/ 1, 7895 /*command*/ 1, 7896 /*field*/ 2, 7897 /*bit_valid*/ 1, 7898 /*bit*/ 4); 7899 ctl_done((union ctl_io *)ctsio); 7900 return (1); 7901 } 7902 7903 if (type>8 || type==2 || type==4 || type==0) { 7904 mtx_unlock(&lun->lun_lock); 7905 ctl_set_invalid_field(/*ctsio*/ ctsio, 7906 /*sks_valid*/ 1, 7907 /*command*/ 1, 7908 /*field*/ 2, 7909 /*bit_valid*/ 1, 7910 /*bit*/ 0); 7911 ctl_done((union ctl_io *)ctsio); 7912 return (1); 7913 } 7914 7915 /* 7916 * Unregister everybody else and build UA for 7917 * them 7918 */ 7919 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7920 if (i == residx || lun->pr_keys[i] == 0) 7921 continue; 7922 7923 if (!persis_offset 7924 && i <CTL_MAX_INITIATORS) 7925 lun->pending_ua[i] |= 7926 CTL_UA_REG_PREEMPT; 7927 else if (persis_offset 7928 && i >= persis_offset) 7929 lun->pending_ua[i-persis_offset] |= 7930 CTL_UA_REG_PREEMPT; 7931 lun->pr_keys[i] = 0; 7932 } 7933 lun->pr_key_count = 1; 7934 lun->res_type = type; 7935 if (lun->res_type != SPR_TYPE_WR_EX_AR 7936 && lun->res_type != SPR_TYPE_EX_AC_AR) 7937 lun->pr_res_idx = residx; 7938 7939 /* send msg to other side */ 7940 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7941 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7942 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7943 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7944 persis_io.pr.pr_info.res_type = type; 7945 memcpy(persis_io.pr.pr_info.sa_res_key, 7946 param->serv_act_res_key, 7947 sizeof(param->serv_act_res_key)); 7948 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7949 &persis_io, sizeof(persis_io), 0)) > 7950 CTL_HA_STATUS_SUCCESS) { 7951 printf("CTL:Persis Out error returned " 7952 "from ctl_ha_msg_send %d\n", 7953 isc_retval); 7954 } 7955 } else { 7956 /* not all registrants */ 7957 mtx_unlock(&lun->lun_lock); 7958 free(ctsio->kern_data_ptr, M_CTL); 7959 ctl_set_invalid_field(ctsio, 7960 /*sks_valid*/ 1, 7961 /*command*/ 0, 7962 /*field*/ 8, 7963 /*bit_valid*/ 0, 7964 /*bit*/ 0); 7965 ctl_done((union ctl_io *)ctsio); 7966 return (1); 7967 } 7968 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7969 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7970 int found = 0; 7971 7972 if (res_key == sa_res_key) { 7973 /* special case */ 7974 /* 7975 * The spec implies this is not good but doesn't 7976 * say what to do. There are two choices either 7977 * generate a res conflict or check condition 7978 * with illegal field in parameter data. Since 7979 * that is what is done when the sa_res_key is 7980 * zero I'll take that approach since this has 7981 * to do with the sa_res_key. 7982 */ 7983 mtx_unlock(&lun->lun_lock); 7984 free(ctsio->kern_data_ptr, M_CTL); 7985 ctl_set_invalid_field(ctsio, 7986 /*sks_valid*/ 1, 7987 /*command*/ 0, 7988 /*field*/ 8, 7989 /*bit_valid*/ 0, 7990 /*bit*/ 0); 7991 ctl_done((union ctl_io *)ctsio); 7992 return (1); 7993 } 7994 7995 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7996 if (lun->pr_keys[i] != sa_res_key) 7997 continue; 7998 7999 found = 1; 8000 lun->pr_keys[i] = 0; 8001 lun->pr_key_count--; 8002 8003 if (!persis_offset && i < CTL_MAX_INITIATORS) 8004 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8005 else if (persis_offset && i >= persis_offset) 8006 lun->pending_ua[i-persis_offset] |= 8007 CTL_UA_REG_PREEMPT; 8008 } 8009 if (!found) { 8010 mtx_unlock(&lun->lun_lock); 8011 free(ctsio->kern_data_ptr, M_CTL); 8012 ctl_set_reservation_conflict(ctsio); 8013 ctl_done((union ctl_io *)ctsio); 8014 return (CTL_RETVAL_COMPLETE); 8015 } 8016 /* send msg to other side */ 8017 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8018 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8019 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8020 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8021 persis_io.pr.pr_info.res_type = type; 8022 memcpy(persis_io.pr.pr_info.sa_res_key, 8023 param->serv_act_res_key, 8024 sizeof(param->serv_act_res_key)); 8025 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8026 &persis_io, sizeof(persis_io), 0)) > 8027 CTL_HA_STATUS_SUCCESS) { 8028 printf("CTL:Persis Out error returned from " 8029 "ctl_ha_msg_send %d\n", isc_retval); 8030 } 8031 } else { 8032 /* Reserved but not all registrants */ 8033 /* sa_res_key is res holder */ 8034 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) { 8035 /* validate scope and type */ 8036 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8037 SPR_LU_SCOPE) { 8038 mtx_unlock(&lun->lun_lock); 8039 ctl_set_invalid_field(/*ctsio*/ ctsio, 8040 /*sks_valid*/ 1, 8041 /*command*/ 1, 8042 /*field*/ 2, 8043 /*bit_valid*/ 1, 8044 /*bit*/ 4); 8045 ctl_done((union ctl_io *)ctsio); 8046 return (1); 8047 } 8048 8049 if (type>8 || type==2 || type==4 || type==0) { 8050 mtx_unlock(&lun->lun_lock); 8051 ctl_set_invalid_field(/*ctsio*/ ctsio, 8052 /*sks_valid*/ 1, 8053 /*command*/ 1, 8054 /*field*/ 2, 8055 /*bit_valid*/ 1, 8056 /*bit*/ 0); 8057 ctl_done((union ctl_io *)ctsio); 8058 return (1); 8059 } 8060 8061 /* 8062 * Do the following: 8063 * if sa_res_key != res_key remove all 8064 * registrants w/sa_res_key and generate UA 8065 * for these registrants(Registrations 8066 * Preempted) if it wasn't an exclusive 8067 * reservation generate UA(Reservations 8068 * Preempted) for all other registered nexuses 8069 * if the type has changed. Establish the new 8070 * reservation and holder. If res_key and 8071 * sa_res_key are the same do the above 8072 * except don't unregister the res holder. 8073 */ 8074 8075 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8076 if (i == residx || lun->pr_keys[i] == 0) 8077 continue; 8078 8079 if (sa_res_key == lun->pr_keys[i]) { 8080 lun->pr_keys[i] = 0; 8081 lun->pr_key_count--; 8082 8083 if (!persis_offset 8084 && i < CTL_MAX_INITIATORS) 8085 lun->pending_ua[i] |= 8086 CTL_UA_REG_PREEMPT; 8087 else if (persis_offset 8088 && i >= persis_offset) 8089 lun->pending_ua[i-persis_offset] |= 8090 CTL_UA_REG_PREEMPT; 8091 } else if (type != lun->res_type 8092 && (lun->res_type == SPR_TYPE_WR_EX_RO 8093 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8094 if (!persis_offset 8095 && i < CTL_MAX_INITIATORS) 8096 lun->pending_ua[i] |= 8097 CTL_UA_RES_RELEASE; 8098 else if (persis_offset 8099 && i >= persis_offset) 8100 lun->pending_ua[ 8101 i-persis_offset] |= 8102 CTL_UA_RES_RELEASE; 8103 } 8104 } 8105 lun->res_type = type; 8106 if (lun->res_type != SPR_TYPE_WR_EX_AR 8107 && lun->res_type != SPR_TYPE_EX_AC_AR) 8108 lun->pr_res_idx = residx; 8109 else 8110 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8111 8112 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8113 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8114 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8115 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8116 persis_io.pr.pr_info.res_type = type; 8117 memcpy(persis_io.pr.pr_info.sa_res_key, 8118 param->serv_act_res_key, 8119 sizeof(param->serv_act_res_key)); 8120 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8121 &persis_io, sizeof(persis_io), 0)) > 8122 CTL_HA_STATUS_SUCCESS) { 8123 printf("CTL:Persis Out error returned " 8124 "from ctl_ha_msg_send %d\n", 8125 isc_retval); 8126 } 8127 } else { 8128 /* 8129 * sa_res_key is not the res holder just 8130 * remove registrants 8131 */ 8132 int found=0; 8133 8134 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8135 if (sa_res_key != lun->pr_keys[i]) 8136 continue; 8137 8138 found = 1; 8139 lun->pr_keys[i] = 0; 8140 lun->pr_key_count--; 8141 8142 if (!persis_offset 8143 && i < CTL_MAX_INITIATORS) 8144 lun->pending_ua[i] |= 8145 CTL_UA_REG_PREEMPT; 8146 else if (persis_offset 8147 && i >= persis_offset) 8148 lun->pending_ua[i-persis_offset] |= 8149 CTL_UA_REG_PREEMPT; 8150 } 8151 8152 if (!found) { 8153 mtx_unlock(&lun->lun_lock); 8154 free(ctsio->kern_data_ptr, M_CTL); 8155 ctl_set_reservation_conflict(ctsio); 8156 ctl_done((union ctl_io *)ctsio); 8157 return (1); 8158 } 8159 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8160 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8161 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8162 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8163 persis_io.pr.pr_info.res_type = type; 8164 memcpy(persis_io.pr.pr_info.sa_res_key, 8165 param->serv_act_res_key, 8166 sizeof(param->serv_act_res_key)); 8167 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8168 &persis_io, sizeof(persis_io), 0)) > 8169 CTL_HA_STATUS_SUCCESS) { 8170 printf("CTL:Persis Out error returned " 8171 "from ctl_ha_msg_send %d\n", 8172 isc_retval); 8173 } 8174 } 8175 } 8176 8177 lun->PRGeneration++; 8178 mtx_unlock(&lun->lun_lock); 8179 8180 return (retval); 8181 } 8182 8183 static void 8184 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8185 { 8186 uint64_t sa_res_key; 8187 int i; 8188 8189 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8190 8191 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8192 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8193 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) { 8194 if (sa_res_key == 0) { 8195 /* 8196 * Unregister everybody else and build UA for 8197 * them 8198 */ 8199 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8200 if (i == msg->pr.pr_info.residx || 8201 lun->pr_keys[i] == 0) 8202 continue; 8203 8204 if (!persis_offset 8205 && i < CTL_MAX_INITIATORS) 8206 lun->pending_ua[i] |= 8207 CTL_UA_REG_PREEMPT; 8208 else if (persis_offset && i >= persis_offset) 8209 lun->pending_ua[i - persis_offset] |= 8210 CTL_UA_REG_PREEMPT; 8211 lun->pr_keys[i] = 0; 8212 } 8213 8214 lun->pr_key_count = 1; 8215 lun->res_type = msg->pr.pr_info.res_type; 8216 if (lun->res_type != SPR_TYPE_WR_EX_AR 8217 && lun->res_type != SPR_TYPE_EX_AC_AR) 8218 lun->pr_res_idx = msg->pr.pr_info.residx; 8219 } else { 8220 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8221 if (sa_res_key == lun->pr_keys[i]) 8222 continue; 8223 8224 lun->pr_keys[i] = 0; 8225 lun->pr_key_count--; 8226 8227 if (!persis_offset 8228 && i < persis_offset) 8229 lun->pending_ua[i] |= 8230 CTL_UA_REG_PREEMPT; 8231 else if (persis_offset 8232 && i >= persis_offset) 8233 lun->pending_ua[i - persis_offset] |= 8234 CTL_UA_REG_PREEMPT; 8235 } 8236 } 8237 } else { 8238 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8239 if (i == msg->pr.pr_info.residx || 8240 lun->pr_keys[i] == 0) 8241 continue; 8242 8243 if (sa_res_key == lun->pr_keys[i]) { 8244 lun->pr_keys[i] = 0; 8245 lun->pr_key_count--; 8246 if (!persis_offset 8247 && i < CTL_MAX_INITIATORS) 8248 lun->pending_ua[i] |= 8249 CTL_UA_REG_PREEMPT; 8250 else if (persis_offset 8251 && i >= persis_offset) 8252 lun->pending_ua[i - persis_offset] |= 8253 CTL_UA_REG_PREEMPT; 8254 } else if (msg->pr.pr_info.res_type != lun->res_type 8255 && (lun->res_type == SPR_TYPE_WR_EX_RO 8256 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8257 if (!persis_offset 8258 && i < persis_offset) 8259 lun->pending_ua[i] |= 8260 CTL_UA_RES_RELEASE; 8261 else if (persis_offset 8262 && i >= persis_offset) 8263 lun->pending_ua[i - persis_offset] |= 8264 CTL_UA_RES_RELEASE; 8265 } 8266 } 8267 lun->res_type = msg->pr.pr_info.res_type; 8268 if (lun->res_type != SPR_TYPE_WR_EX_AR 8269 && lun->res_type != SPR_TYPE_EX_AC_AR) 8270 lun->pr_res_idx = msg->pr.pr_info.residx; 8271 else 8272 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8273 } 8274 lun->PRGeneration++; 8275 8276 } 8277 8278 8279 int 8280 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8281 { 8282 int retval; 8283 int isc_retval; 8284 u_int32_t param_len; 8285 struct scsi_per_res_out *cdb; 8286 struct ctl_lun *lun; 8287 struct scsi_per_res_out_parms* param; 8288 struct ctl_softc *softc; 8289 uint32_t residx; 8290 uint64_t res_key, sa_res_key; 8291 uint8_t type; 8292 union ctl_ha_msg persis_io; 8293 int i; 8294 8295 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8296 8297 retval = CTL_RETVAL_COMPLETE; 8298 8299 softc = control_softc; 8300 8301 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8302 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8303 8304 /* 8305 * We only support whole-LUN scope. The scope & type are ignored for 8306 * register, register and ignore existing key and clear. 8307 * We sometimes ignore scope and type on preempts too!! 8308 * Verify reservation type here as well. 8309 */ 8310 type = cdb->scope_type & SPR_TYPE_MASK; 8311 if ((cdb->action == SPRO_RESERVE) 8312 || (cdb->action == SPRO_RELEASE)) { 8313 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8314 ctl_set_invalid_field(/*ctsio*/ ctsio, 8315 /*sks_valid*/ 1, 8316 /*command*/ 1, 8317 /*field*/ 2, 8318 /*bit_valid*/ 1, 8319 /*bit*/ 4); 8320 ctl_done((union ctl_io *)ctsio); 8321 return (CTL_RETVAL_COMPLETE); 8322 } 8323 8324 if (type>8 || type==2 || type==4 || type==0) { 8325 ctl_set_invalid_field(/*ctsio*/ ctsio, 8326 /*sks_valid*/ 1, 8327 /*command*/ 1, 8328 /*field*/ 2, 8329 /*bit_valid*/ 1, 8330 /*bit*/ 0); 8331 ctl_done((union ctl_io *)ctsio); 8332 return (CTL_RETVAL_COMPLETE); 8333 } 8334 } 8335 8336 param_len = scsi_4btoul(cdb->length); 8337 8338 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8339 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8340 ctsio->kern_data_len = param_len; 8341 ctsio->kern_total_len = param_len; 8342 ctsio->kern_data_resid = 0; 8343 ctsio->kern_rel_offset = 0; 8344 ctsio->kern_sg_entries = 0; 8345 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8346 ctsio->be_move_done = ctl_config_move_done; 8347 ctl_datamove((union ctl_io *)ctsio); 8348 8349 return (CTL_RETVAL_COMPLETE); 8350 } 8351 8352 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8353 8354 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8355 res_key = scsi_8btou64(param->res_key.key); 8356 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8357 8358 /* 8359 * Validate the reservation key here except for SPRO_REG_IGNO 8360 * This must be done for all other service actions 8361 */ 8362 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8363 mtx_lock(&lun->lun_lock); 8364 if (lun->pr_keys[residx] != 0) { 8365 if (res_key != lun->pr_keys[residx]) { 8366 /* 8367 * The current key passed in doesn't match 8368 * the one the initiator previously 8369 * registered. 8370 */ 8371 mtx_unlock(&lun->lun_lock); 8372 free(ctsio->kern_data_ptr, M_CTL); 8373 ctl_set_reservation_conflict(ctsio); 8374 ctl_done((union ctl_io *)ctsio); 8375 return (CTL_RETVAL_COMPLETE); 8376 } 8377 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8378 /* 8379 * We are not registered 8380 */ 8381 mtx_unlock(&lun->lun_lock); 8382 free(ctsio->kern_data_ptr, M_CTL); 8383 ctl_set_reservation_conflict(ctsio); 8384 ctl_done((union ctl_io *)ctsio); 8385 return (CTL_RETVAL_COMPLETE); 8386 } else if (res_key != 0) { 8387 /* 8388 * We are not registered and trying to register but 8389 * the register key isn't zero. 8390 */ 8391 mtx_unlock(&lun->lun_lock); 8392 free(ctsio->kern_data_ptr, M_CTL); 8393 ctl_set_reservation_conflict(ctsio); 8394 ctl_done((union ctl_io *)ctsio); 8395 return (CTL_RETVAL_COMPLETE); 8396 } 8397 mtx_unlock(&lun->lun_lock); 8398 } 8399 8400 switch (cdb->action & SPRO_ACTION_MASK) { 8401 case SPRO_REGISTER: 8402 case SPRO_REG_IGNO: { 8403 8404 #if 0 8405 printf("Registration received\n"); 8406 #endif 8407 8408 /* 8409 * We don't support any of these options, as we report in 8410 * the read capabilities request (see 8411 * ctl_persistent_reserve_in(), above). 8412 */ 8413 if ((param->flags & SPR_SPEC_I_PT) 8414 || (param->flags & SPR_ALL_TG_PT) 8415 || (param->flags & SPR_APTPL)) { 8416 int bit_ptr; 8417 8418 if (param->flags & SPR_APTPL) 8419 bit_ptr = 0; 8420 else if (param->flags & SPR_ALL_TG_PT) 8421 bit_ptr = 2; 8422 else /* SPR_SPEC_I_PT */ 8423 bit_ptr = 3; 8424 8425 free(ctsio->kern_data_ptr, M_CTL); 8426 ctl_set_invalid_field(ctsio, 8427 /*sks_valid*/ 1, 8428 /*command*/ 0, 8429 /*field*/ 20, 8430 /*bit_valid*/ 1, 8431 /*bit*/ bit_ptr); 8432 ctl_done((union ctl_io *)ctsio); 8433 return (CTL_RETVAL_COMPLETE); 8434 } 8435 8436 mtx_lock(&lun->lun_lock); 8437 8438 /* 8439 * The initiator wants to clear the 8440 * key/unregister. 8441 */ 8442 if (sa_res_key == 0) { 8443 if ((res_key == 0 8444 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8445 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8446 && lun->pr_keys[residx] == 0)) { 8447 mtx_unlock(&lun->lun_lock); 8448 goto done; 8449 } 8450 8451 lun->pr_keys[residx] = 0; 8452 lun->pr_key_count--; 8453 8454 if (residx == lun->pr_res_idx) { 8455 lun->flags &= ~CTL_LUN_PR_RESERVED; 8456 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8457 8458 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8459 || lun->res_type == SPR_TYPE_EX_AC_RO) 8460 && lun->pr_key_count) { 8461 /* 8462 * If the reservation is a registrants 8463 * only type we need to generate a UA 8464 * for other registered inits. The 8465 * sense code should be RESERVATIONS 8466 * RELEASED 8467 */ 8468 8469 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8470 if (lun->pr_keys[ 8471 i + persis_offset] == 0) 8472 continue; 8473 lun->pending_ua[i] |= 8474 CTL_UA_RES_RELEASE; 8475 } 8476 } 8477 lun->res_type = 0; 8478 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8479 if (lun->pr_key_count==0) { 8480 lun->flags &= ~CTL_LUN_PR_RESERVED; 8481 lun->res_type = 0; 8482 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8483 } 8484 } 8485 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8486 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8487 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8488 persis_io.pr.pr_info.residx = residx; 8489 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8490 &persis_io, sizeof(persis_io), 0 )) > 8491 CTL_HA_STATUS_SUCCESS) { 8492 printf("CTL:Persis Out error returned from " 8493 "ctl_ha_msg_send %d\n", isc_retval); 8494 } 8495 } else /* sa_res_key != 0 */ { 8496 8497 /* 8498 * If we aren't registered currently then increment 8499 * the key count and set the registered flag. 8500 */ 8501 if (lun->pr_keys[residx] == 0) 8502 lun->pr_key_count++; 8503 lun->pr_keys[residx] = sa_res_key; 8504 8505 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8506 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8507 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8508 persis_io.pr.pr_info.residx = residx; 8509 memcpy(persis_io.pr.pr_info.sa_res_key, 8510 param->serv_act_res_key, 8511 sizeof(param->serv_act_res_key)); 8512 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8513 &persis_io, sizeof(persis_io), 0)) > 8514 CTL_HA_STATUS_SUCCESS) { 8515 printf("CTL:Persis Out error returned from " 8516 "ctl_ha_msg_send %d\n", isc_retval); 8517 } 8518 } 8519 lun->PRGeneration++; 8520 mtx_unlock(&lun->lun_lock); 8521 8522 break; 8523 } 8524 case SPRO_RESERVE: 8525 #if 0 8526 printf("Reserve executed type %d\n", type); 8527 #endif 8528 mtx_lock(&lun->lun_lock); 8529 if (lun->flags & CTL_LUN_PR_RESERVED) { 8530 /* 8531 * if this isn't the reservation holder and it's 8532 * not a "all registrants" type or if the type is 8533 * different then we have a conflict 8534 */ 8535 if ((lun->pr_res_idx != residx 8536 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8537 || lun->res_type != type) { 8538 mtx_unlock(&lun->lun_lock); 8539 free(ctsio->kern_data_ptr, M_CTL); 8540 ctl_set_reservation_conflict(ctsio); 8541 ctl_done((union ctl_io *)ctsio); 8542 return (CTL_RETVAL_COMPLETE); 8543 } 8544 mtx_unlock(&lun->lun_lock); 8545 } else /* create a reservation */ { 8546 /* 8547 * If it's not an "all registrants" type record 8548 * reservation holder 8549 */ 8550 if (type != SPR_TYPE_WR_EX_AR 8551 && type != SPR_TYPE_EX_AC_AR) 8552 lun->pr_res_idx = residx; /* Res holder */ 8553 else 8554 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8555 8556 lun->flags |= CTL_LUN_PR_RESERVED; 8557 lun->res_type = type; 8558 8559 mtx_unlock(&lun->lun_lock); 8560 8561 /* send msg to other side */ 8562 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8563 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8564 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8565 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8566 persis_io.pr.pr_info.res_type = type; 8567 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8568 &persis_io, sizeof(persis_io), 0)) > 8569 CTL_HA_STATUS_SUCCESS) { 8570 printf("CTL:Persis Out error returned from " 8571 "ctl_ha_msg_send %d\n", isc_retval); 8572 } 8573 } 8574 break; 8575 8576 case SPRO_RELEASE: 8577 mtx_lock(&lun->lun_lock); 8578 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8579 /* No reservation exists return good status */ 8580 mtx_unlock(&lun->lun_lock); 8581 goto done; 8582 } 8583 /* 8584 * Is this nexus a reservation holder? 8585 */ 8586 if (lun->pr_res_idx != residx 8587 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8588 /* 8589 * not a res holder return good status but 8590 * do nothing 8591 */ 8592 mtx_unlock(&lun->lun_lock); 8593 goto done; 8594 } 8595 8596 if (lun->res_type != type) { 8597 mtx_unlock(&lun->lun_lock); 8598 free(ctsio->kern_data_ptr, M_CTL); 8599 ctl_set_illegal_pr_release(ctsio); 8600 ctl_done((union ctl_io *)ctsio); 8601 return (CTL_RETVAL_COMPLETE); 8602 } 8603 8604 /* okay to release */ 8605 lun->flags &= ~CTL_LUN_PR_RESERVED; 8606 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8607 lun->res_type = 0; 8608 8609 /* 8610 * if this isn't an exclusive access 8611 * res generate UA for all other 8612 * registrants. 8613 */ 8614 if (type != SPR_TYPE_EX_AC 8615 && type != SPR_TYPE_WR_EX) { 8616 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8617 if (i == residx || 8618 lun->pr_keys[i + persis_offset] == 0) 8619 continue; 8620 lun->pending_ua[i] |= CTL_UA_RES_RELEASE; 8621 } 8622 } 8623 mtx_unlock(&lun->lun_lock); 8624 /* Send msg to other side */ 8625 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8626 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8627 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8628 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8629 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8630 printf("CTL:Persis Out error returned from " 8631 "ctl_ha_msg_send %d\n", isc_retval); 8632 } 8633 break; 8634 8635 case SPRO_CLEAR: 8636 /* send msg to other side */ 8637 8638 mtx_lock(&lun->lun_lock); 8639 lun->flags &= ~CTL_LUN_PR_RESERVED; 8640 lun->res_type = 0; 8641 lun->pr_key_count = 0; 8642 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8643 8644 lun->pr_keys[residx] = 0; 8645 8646 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8647 if (lun->pr_keys[i] != 0) { 8648 if (!persis_offset && i < CTL_MAX_INITIATORS) 8649 lun->pending_ua[i] |= 8650 CTL_UA_RES_PREEMPT; 8651 else if (persis_offset && i >= persis_offset) 8652 lun->pending_ua[i-persis_offset] |= 8653 CTL_UA_RES_PREEMPT; 8654 8655 lun->pr_keys[i] = 0; 8656 } 8657 lun->PRGeneration++; 8658 mtx_unlock(&lun->lun_lock); 8659 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8660 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8661 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8662 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8663 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8664 printf("CTL:Persis Out error returned from " 8665 "ctl_ha_msg_send %d\n", isc_retval); 8666 } 8667 break; 8668 8669 case SPRO_PREEMPT: 8670 case SPRO_PRE_ABO: { 8671 int nretval; 8672 8673 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8674 residx, ctsio, cdb, param); 8675 if (nretval != 0) 8676 return (CTL_RETVAL_COMPLETE); 8677 break; 8678 } 8679 default: 8680 panic("Invalid PR type %x", cdb->action); 8681 } 8682 8683 done: 8684 free(ctsio->kern_data_ptr, M_CTL); 8685 ctl_set_success(ctsio); 8686 ctl_done((union ctl_io *)ctsio); 8687 8688 return (retval); 8689 } 8690 8691 /* 8692 * This routine is for handling a message from the other SC pertaining to 8693 * persistent reserve out. All the error checking will have been done 8694 * so only perorming the action need be done here to keep the two 8695 * in sync. 8696 */ 8697 static void 8698 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8699 { 8700 struct ctl_lun *lun; 8701 struct ctl_softc *softc; 8702 int i; 8703 uint32_t targ_lun; 8704 8705 softc = control_softc; 8706 8707 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8708 lun = softc->ctl_luns[targ_lun]; 8709 mtx_lock(&lun->lun_lock); 8710 switch(msg->pr.pr_info.action) { 8711 case CTL_PR_REG_KEY: 8712 if (lun->pr_keys[msg->pr.pr_info.residx] == 0) 8713 lun->pr_key_count++; 8714 lun->pr_keys[msg->pr.pr_info.residx] = 8715 scsi_8btou64(msg->pr.pr_info.sa_res_key); 8716 lun->PRGeneration++; 8717 break; 8718 8719 case CTL_PR_UNREG_KEY: 8720 lun->pr_keys[msg->pr.pr_info.residx] = 0; 8721 lun->pr_key_count--; 8722 8723 /* XXX Need to see if the reservation has been released */ 8724 /* if so do we need to generate UA? */ 8725 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8726 lun->flags &= ~CTL_LUN_PR_RESERVED; 8727 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8728 8729 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8730 || lun->res_type == SPR_TYPE_EX_AC_RO) 8731 && lun->pr_key_count) { 8732 /* 8733 * If the reservation is a registrants 8734 * only type we need to generate a UA 8735 * for other registered inits. The 8736 * sense code should be RESERVATIONS 8737 * RELEASED 8738 */ 8739 8740 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8741 if (lun->pr_keys[i+ 8742 persis_offset] == 0) 8743 continue; 8744 8745 lun->pending_ua[i] |= 8746 CTL_UA_RES_RELEASE; 8747 } 8748 } 8749 lun->res_type = 0; 8750 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8751 if (lun->pr_key_count==0) { 8752 lun->flags &= ~CTL_LUN_PR_RESERVED; 8753 lun->res_type = 0; 8754 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8755 } 8756 } 8757 lun->PRGeneration++; 8758 break; 8759 8760 case CTL_PR_RESERVE: 8761 lun->flags |= CTL_LUN_PR_RESERVED; 8762 lun->res_type = msg->pr.pr_info.res_type; 8763 lun->pr_res_idx = msg->pr.pr_info.residx; 8764 8765 break; 8766 8767 case CTL_PR_RELEASE: 8768 /* 8769 * if this isn't an exclusive access res generate UA for all 8770 * other registrants. 8771 */ 8772 if (lun->res_type != SPR_TYPE_EX_AC 8773 && lun->res_type != SPR_TYPE_WR_EX) { 8774 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8775 if (lun->pr_keys[i+persis_offset] != 0) 8776 lun->pending_ua[i] |= 8777 CTL_UA_RES_RELEASE; 8778 } 8779 8780 lun->flags &= ~CTL_LUN_PR_RESERVED; 8781 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8782 lun->res_type = 0; 8783 break; 8784 8785 case CTL_PR_PREEMPT: 8786 ctl_pro_preempt_other(lun, msg); 8787 break; 8788 case CTL_PR_CLEAR: 8789 lun->flags &= ~CTL_LUN_PR_RESERVED; 8790 lun->res_type = 0; 8791 lun->pr_key_count = 0; 8792 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8793 8794 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8795 if (lun->pr_keys[i] == 0) 8796 continue; 8797 if (!persis_offset 8798 && i < CTL_MAX_INITIATORS) 8799 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 8800 else if (persis_offset 8801 && i >= persis_offset) 8802 lun->pending_ua[i-persis_offset] |= 8803 CTL_UA_RES_PREEMPT; 8804 lun->pr_keys[i] = 0; 8805 } 8806 lun->PRGeneration++; 8807 break; 8808 } 8809 8810 mtx_unlock(&lun->lun_lock); 8811 } 8812 8813 int 8814 ctl_read_write(struct ctl_scsiio *ctsio) 8815 { 8816 struct ctl_lun *lun; 8817 struct ctl_lba_len_flags *lbalen; 8818 uint64_t lba; 8819 uint32_t num_blocks; 8820 int flags, retval; 8821 int isread; 8822 8823 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8824 8825 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8826 8827 flags = 0; 8828 retval = CTL_RETVAL_COMPLETE; 8829 8830 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8831 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8832 switch (ctsio->cdb[0]) { 8833 case READ_6: 8834 case WRITE_6: { 8835 struct scsi_rw_6 *cdb; 8836 8837 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8838 8839 lba = scsi_3btoul(cdb->addr); 8840 /* only 5 bits are valid in the most significant address byte */ 8841 lba &= 0x1fffff; 8842 num_blocks = cdb->length; 8843 /* 8844 * This is correct according to SBC-2. 8845 */ 8846 if (num_blocks == 0) 8847 num_blocks = 256; 8848 break; 8849 } 8850 case READ_10: 8851 case WRITE_10: { 8852 struct scsi_rw_10 *cdb; 8853 8854 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8855 if (cdb->byte2 & SRW10_FUA) 8856 flags |= CTL_LLF_FUA; 8857 if (cdb->byte2 & SRW10_DPO) 8858 flags |= CTL_LLF_DPO; 8859 lba = scsi_4btoul(cdb->addr); 8860 num_blocks = scsi_2btoul(cdb->length); 8861 break; 8862 } 8863 case WRITE_VERIFY_10: { 8864 struct scsi_write_verify_10 *cdb; 8865 8866 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8867 flags |= CTL_LLF_FUA; 8868 if (cdb->byte2 & SWV_DPO) 8869 flags |= CTL_LLF_DPO; 8870 lba = scsi_4btoul(cdb->addr); 8871 num_blocks = scsi_2btoul(cdb->length); 8872 break; 8873 } 8874 case READ_12: 8875 case WRITE_12: { 8876 struct scsi_rw_12 *cdb; 8877 8878 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8879 if (cdb->byte2 & SRW12_FUA) 8880 flags |= CTL_LLF_FUA; 8881 if (cdb->byte2 & SRW12_DPO) 8882 flags |= CTL_LLF_DPO; 8883 lba = scsi_4btoul(cdb->addr); 8884 num_blocks = scsi_4btoul(cdb->length); 8885 break; 8886 } 8887 case WRITE_VERIFY_12: { 8888 struct scsi_write_verify_12 *cdb; 8889 8890 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8891 flags |= CTL_LLF_FUA; 8892 if (cdb->byte2 & SWV_DPO) 8893 flags |= CTL_LLF_DPO; 8894 lba = scsi_4btoul(cdb->addr); 8895 num_blocks = scsi_4btoul(cdb->length); 8896 break; 8897 } 8898 case READ_16: 8899 case WRITE_16: { 8900 struct scsi_rw_16 *cdb; 8901 8902 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8903 if (cdb->byte2 & SRW12_FUA) 8904 flags |= CTL_LLF_FUA; 8905 if (cdb->byte2 & SRW12_DPO) 8906 flags |= CTL_LLF_DPO; 8907 lba = scsi_8btou64(cdb->addr); 8908 num_blocks = scsi_4btoul(cdb->length); 8909 break; 8910 } 8911 case WRITE_ATOMIC_16: { 8912 struct scsi_rw_16 *cdb; 8913 8914 if (lun->be_lun->atomicblock == 0) { 8915 ctl_set_invalid_opcode(ctsio); 8916 ctl_done((union ctl_io *)ctsio); 8917 return (CTL_RETVAL_COMPLETE); 8918 } 8919 8920 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8921 if (cdb->byte2 & SRW12_FUA) 8922 flags |= CTL_LLF_FUA; 8923 if (cdb->byte2 & SRW12_DPO) 8924 flags |= CTL_LLF_DPO; 8925 lba = scsi_8btou64(cdb->addr); 8926 num_blocks = scsi_4btoul(cdb->length); 8927 if (num_blocks > lun->be_lun->atomicblock) { 8928 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8929 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8930 /*bit*/ 0); 8931 ctl_done((union ctl_io *)ctsio); 8932 return (CTL_RETVAL_COMPLETE); 8933 } 8934 break; 8935 } 8936 case WRITE_VERIFY_16: { 8937 struct scsi_write_verify_16 *cdb; 8938 8939 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8940 flags |= CTL_LLF_FUA; 8941 if (cdb->byte2 & SWV_DPO) 8942 flags |= CTL_LLF_DPO; 8943 lba = scsi_8btou64(cdb->addr); 8944 num_blocks = scsi_4btoul(cdb->length); 8945 break; 8946 } 8947 default: 8948 /* 8949 * We got a command we don't support. This shouldn't 8950 * happen, commands should be filtered out above us. 8951 */ 8952 ctl_set_invalid_opcode(ctsio); 8953 ctl_done((union ctl_io *)ctsio); 8954 8955 return (CTL_RETVAL_COMPLETE); 8956 break; /* NOTREACHED */ 8957 } 8958 8959 /* 8960 * The first check is to make sure we're in bounds, the second 8961 * check is to catch wrap-around problems. If the lba + num blocks 8962 * is less than the lba, then we've wrapped around and the block 8963 * range is invalid anyway. 8964 */ 8965 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8966 || ((lba + num_blocks) < lba)) { 8967 ctl_set_lba_out_of_range(ctsio); 8968 ctl_done((union ctl_io *)ctsio); 8969 return (CTL_RETVAL_COMPLETE); 8970 } 8971 8972 /* 8973 * According to SBC-3, a transfer length of 0 is not an error. 8974 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8975 * translates to 256 blocks for those commands. 8976 */ 8977 if (num_blocks == 0) { 8978 ctl_set_success(ctsio); 8979 ctl_done((union ctl_io *)ctsio); 8980 return (CTL_RETVAL_COMPLETE); 8981 } 8982 8983 /* Set FUA and/or DPO if caches are disabled. */ 8984 if (isread) { 8985 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8986 SCP_RCD) != 0) 8987 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8988 } else { 8989 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8990 SCP_WCE) == 0) 8991 flags |= CTL_LLF_FUA; 8992 } 8993 8994 lbalen = (struct ctl_lba_len_flags *) 8995 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8996 lbalen->lba = lba; 8997 lbalen->len = num_blocks; 8998 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8999 9000 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9001 ctsio->kern_rel_offset = 0; 9002 9003 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9004 9005 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9006 9007 return (retval); 9008 } 9009 9010 static int 9011 ctl_cnw_cont(union ctl_io *io) 9012 { 9013 struct ctl_scsiio *ctsio; 9014 struct ctl_lun *lun; 9015 struct ctl_lba_len_flags *lbalen; 9016 int retval; 9017 9018 ctsio = &io->scsiio; 9019 ctsio->io_hdr.status = CTL_STATUS_NONE; 9020 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9021 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9022 lbalen = (struct ctl_lba_len_flags *) 9023 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9024 lbalen->flags &= ~CTL_LLF_COMPARE; 9025 lbalen->flags |= CTL_LLF_WRITE; 9026 9027 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9028 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9029 return (retval); 9030 } 9031 9032 int 9033 ctl_cnw(struct ctl_scsiio *ctsio) 9034 { 9035 struct ctl_lun *lun; 9036 struct ctl_lba_len_flags *lbalen; 9037 uint64_t lba; 9038 uint32_t num_blocks; 9039 int flags, retval; 9040 9041 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9042 9043 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9044 9045 flags = 0; 9046 retval = CTL_RETVAL_COMPLETE; 9047 9048 switch (ctsio->cdb[0]) { 9049 case COMPARE_AND_WRITE: { 9050 struct scsi_compare_and_write *cdb; 9051 9052 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9053 if (cdb->byte2 & SRW10_FUA) 9054 flags |= CTL_LLF_FUA; 9055 if (cdb->byte2 & SRW10_DPO) 9056 flags |= CTL_LLF_DPO; 9057 lba = scsi_8btou64(cdb->addr); 9058 num_blocks = cdb->length; 9059 break; 9060 } 9061 default: 9062 /* 9063 * We got a command we don't support. This shouldn't 9064 * happen, commands should be filtered out above us. 9065 */ 9066 ctl_set_invalid_opcode(ctsio); 9067 ctl_done((union ctl_io *)ctsio); 9068 9069 return (CTL_RETVAL_COMPLETE); 9070 break; /* NOTREACHED */ 9071 } 9072 9073 /* 9074 * The first check is to make sure we're in bounds, the second 9075 * check is to catch wrap-around problems. If the lba + num blocks 9076 * is less than the lba, then we've wrapped around and the block 9077 * range is invalid anyway. 9078 */ 9079 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9080 || ((lba + num_blocks) < lba)) { 9081 ctl_set_lba_out_of_range(ctsio); 9082 ctl_done((union ctl_io *)ctsio); 9083 return (CTL_RETVAL_COMPLETE); 9084 } 9085 9086 /* 9087 * According to SBC-3, a transfer length of 0 is not an error. 9088 */ 9089 if (num_blocks == 0) { 9090 ctl_set_success(ctsio); 9091 ctl_done((union ctl_io *)ctsio); 9092 return (CTL_RETVAL_COMPLETE); 9093 } 9094 9095 /* Set FUA if write cache is disabled. */ 9096 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9097 SCP_WCE) == 0) 9098 flags |= CTL_LLF_FUA; 9099 9100 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9101 ctsio->kern_rel_offset = 0; 9102 9103 /* 9104 * Set the IO_CONT flag, so that if this I/O gets passed to 9105 * ctl_data_submit_done(), it'll get passed back to 9106 * ctl_ctl_cnw_cont() for further processing. 9107 */ 9108 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9109 ctsio->io_cont = ctl_cnw_cont; 9110 9111 lbalen = (struct ctl_lba_len_flags *) 9112 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9113 lbalen->lba = lba; 9114 lbalen->len = num_blocks; 9115 lbalen->flags = CTL_LLF_COMPARE | flags; 9116 9117 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9118 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9119 return (retval); 9120 } 9121 9122 int 9123 ctl_verify(struct ctl_scsiio *ctsio) 9124 { 9125 struct ctl_lun *lun; 9126 struct ctl_lba_len_flags *lbalen; 9127 uint64_t lba; 9128 uint32_t num_blocks; 9129 int bytchk, flags; 9130 int retval; 9131 9132 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9133 9134 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9135 9136 bytchk = 0; 9137 flags = CTL_LLF_FUA; 9138 retval = CTL_RETVAL_COMPLETE; 9139 9140 switch (ctsio->cdb[0]) { 9141 case VERIFY_10: { 9142 struct scsi_verify_10 *cdb; 9143 9144 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9145 if (cdb->byte2 & SVFY_BYTCHK) 9146 bytchk = 1; 9147 if (cdb->byte2 & SVFY_DPO) 9148 flags |= CTL_LLF_DPO; 9149 lba = scsi_4btoul(cdb->addr); 9150 num_blocks = scsi_2btoul(cdb->length); 9151 break; 9152 } 9153 case VERIFY_12: { 9154 struct scsi_verify_12 *cdb; 9155 9156 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9157 if (cdb->byte2 & SVFY_BYTCHK) 9158 bytchk = 1; 9159 if (cdb->byte2 & SVFY_DPO) 9160 flags |= CTL_LLF_DPO; 9161 lba = scsi_4btoul(cdb->addr); 9162 num_blocks = scsi_4btoul(cdb->length); 9163 break; 9164 } 9165 case VERIFY_16: { 9166 struct scsi_rw_16 *cdb; 9167 9168 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9169 if (cdb->byte2 & SVFY_BYTCHK) 9170 bytchk = 1; 9171 if (cdb->byte2 & SVFY_DPO) 9172 flags |= CTL_LLF_DPO; 9173 lba = scsi_8btou64(cdb->addr); 9174 num_blocks = scsi_4btoul(cdb->length); 9175 break; 9176 } 9177 default: 9178 /* 9179 * We got a command we don't support. This shouldn't 9180 * happen, commands should be filtered out above us. 9181 */ 9182 ctl_set_invalid_opcode(ctsio); 9183 ctl_done((union ctl_io *)ctsio); 9184 return (CTL_RETVAL_COMPLETE); 9185 } 9186 9187 /* 9188 * The first check is to make sure we're in bounds, the second 9189 * check is to catch wrap-around problems. If the lba + num blocks 9190 * is less than the lba, then we've wrapped around and the block 9191 * range is invalid anyway. 9192 */ 9193 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9194 || ((lba + num_blocks) < lba)) { 9195 ctl_set_lba_out_of_range(ctsio); 9196 ctl_done((union ctl_io *)ctsio); 9197 return (CTL_RETVAL_COMPLETE); 9198 } 9199 9200 /* 9201 * According to SBC-3, a transfer length of 0 is not an error. 9202 */ 9203 if (num_blocks == 0) { 9204 ctl_set_success(ctsio); 9205 ctl_done((union ctl_io *)ctsio); 9206 return (CTL_RETVAL_COMPLETE); 9207 } 9208 9209 lbalen = (struct ctl_lba_len_flags *) 9210 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9211 lbalen->lba = lba; 9212 lbalen->len = num_blocks; 9213 if (bytchk) { 9214 lbalen->flags = CTL_LLF_COMPARE | flags; 9215 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9216 } else { 9217 lbalen->flags = CTL_LLF_VERIFY | flags; 9218 ctsio->kern_total_len = 0; 9219 } 9220 ctsio->kern_rel_offset = 0; 9221 9222 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9223 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9224 return (retval); 9225 } 9226 9227 int 9228 ctl_report_luns(struct ctl_scsiio *ctsio) 9229 { 9230 struct scsi_report_luns *cdb; 9231 struct scsi_report_luns_data *lun_data; 9232 struct ctl_lun *lun, *request_lun; 9233 int num_luns, retval; 9234 uint32_t alloc_len, lun_datalen; 9235 int num_filled, well_known; 9236 uint32_t initidx, targ_lun_id, lun_id; 9237 9238 retval = CTL_RETVAL_COMPLETE; 9239 well_known = 0; 9240 9241 cdb = (struct scsi_report_luns *)ctsio->cdb; 9242 9243 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9244 9245 mtx_lock(&control_softc->ctl_lock); 9246 num_luns = control_softc->num_luns; 9247 mtx_unlock(&control_softc->ctl_lock); 9248 9249 switch (cdb->select_report) { 9250 case RPL_REPORT_DEFAULT: 9251 case RPL_REPORT_ALL: 9252 break; 9253 case RPL_REPORT_WELLKNOWN: 9254 well_known = 1; 9255 num_luns = 0; 9256 break; 9257 default: 9258 ctl_set_invalid_field(ctsio, 9259 /*sks_valid*/ 1, 9260 /*command*/ 1, 9261 /*field*/ 2, 9262 /*bit_valid*/ 0, 9263 /*bit*/ 0); 9264 ctl_done((union ctl_io *)ctsio); 9265 return (retval); 9266 break; /* NOTREACHED */ 9267 } 9268 9269 alloc_len = scsi_4btoul(cdb->length); 9270 /* 9271 * The initiator has to allocate at least 16 bytes for this request, 9272 * so he can at least get the header and the first LUN. Otherwise 9273 * we reject the request (per SPC-3 rev 14, section 6.21). 9274 */ 9275 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9276 sizeof(struct scsi_report_luns_lundata))) { 9277 ctl_set_invalid_field(ctsio, 9278 /*sks_valid*/ 1, 9279 /*command*/ 1, 9280 /*field*/ 6, 9281 /*bit_valid*/ 0, 9282 /*bit*/ 0); 9283 ctl_done((union ctl_io *)ctsio); 9284 return (retval); 9285 } 9286 9287 request_lun = (struct ctl_lun *) 9288 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9289 9290 lun_datalen = sizeof(*lun_data) + 9291 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9292 9293 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9294 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9295 ctsio->kern_sg_entries = 0; 9296 9297 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9298 9299 mtx_lock(&control_softc->ctl_lock); 9300 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9301 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9302 if (lun_id >= CTL_MAX_LUNS) 9303 continue; 9304 lun = control_softc->ctl_luns[lun_id]; 9305 if (lun == NULL) 9306 continue; 9307 9308 if (targ_lun_id <= 0xff) { 9309 /* 9310 * Peripheral addressing method, bus number 0. 9311 */ 9312 lun_data->luns[num_filled].lundata[0] = 9313 RPL_LUNDATA_ATYP_PERIPH; 9314 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9315 num_filled++; 9316 } else if (targ_lun_id <= 0x3fff) { 9317 /* 9318 * Flat addressing method. 9319 */ 9320 lun_data->luns[num_filled].lundata[0] = 9321 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9322 lun_data->luns[num_filled].lundata[1] = 9323 (targ_lun_id & 0xff); 9324 num_filled++; 9325 } else if (targ_lun_id <= 0xffffff) { 9326 /* 9327 * Extended flat addressing method. 9328 */ 9329 lun_data->luns[num_filled].lundata[0] = 9330 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9331 scsi_ulto3b(targ_lun_id, 9332 &lun_data->luns[num_filled].lundata[1]); 9333 num_filled++; 9334 } else { 9335 printf("ctl_report_luns: bogus LUN number %jd, " 9336 "skipping\n", (intmax_t)targ_lun_id); 9337 } 9338 /* 9339 * According to SPC-3, rev 14 section 6.21: 9340 * 9341 * "The execution of a REPORT LUNS command to any valid and 9342 * installed logical unit shall clear the REPORTED LUNS DATA 9343 * HAS CHANGED unit attention condition for all logical 9344 * units of that target with respect to the requesting 9345 * initiator. A valid and installed logical unit is one 9346 * having a PERIPHERAL QUALIFIER of 000b in the standard 9347 * INQUIRY data (see 6.4.2)." 9348 * 9349 * If request_lun is NULL, the LUN this report luns command 9350 * was issued to is either disabled or doesn't exist. In that 9351 * case, we shouldn't clear any pending lun change unit 9352 * attention. 9353 */ 9354 if (request_lun != NULL) { 9355 mtx_lock(&lun->lun_lock); 9356 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9357 mtx_unlock(&lun->lun_lock); 9358 } 9359 } 9360 mtx_unlock(&control_softc->ctl_lock); 9361 9362 /* 9363 * It's quite possible that we've returned fewer LUNs than we allocated 9364 * space for. Trim it. 9365 */ 9366 lun_datalen = sizeof(*lun_data) + 9367 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9368 9369 if (lun_datalen < alloc_len) { 9370 ctsio->residual = alloc_len - lun_datalen; 9371 ctsio->kern_data_len = lun_datalen; 9372 ctsio->kern_total_len = lun_datalen; 9373 } else { 9374 ctsio->residual = 0; 9375 ctsio->kern_data_len = alloc_len; 9376 ctsio->kern_total_len = alloc_len; 9377 } 9378 ctsio->kern_data_resid = 0; 9379 ctsio->kern_rel_offset = 0; 9380 ctsio->kern_sg_entries = 0; 9381 9382 /* 9383 * We set this to the actual data length, regardless of how much 9384 * space we actually have to return results. If the user looks at 9385 * this value, he'll know whether or not he allocated enough space 9386 * and reissue the command if necessary. We don't support well 9387 * known logical units, so if the user asks for that, return none. 9388 */ 9389 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9390 9391 /* 9392 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9393 * this request. 9394 */ 9395 ctl_set_success(ctsio); 9396 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9397 ctsio->be_move_done = ctl_config_move_done; 9398 ctl_datamove((union ctl_io *)ctsio); 9399 return (retval); 9400 } 9401 9402 int 9403 ctl_request_sense(struct ctl_scsiio *ctsio) 9404 { 9405 struct scsi_request_sense *cdb; 9406 struct scsi_sense_data *sense_ptr; 9407 struct ctl_lun *lun; 9408 uint32_t initidx; 9409 int have_error; 9410 scsi_sense_data_type sense_format; 9411 9412 cdb = (struct scsi_request_sense *)ctsio->cdb; 9413 9414 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9415 9416 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9417 9418 /* 9419 * Determine which sense format the user wants. 9420 */ 9421 if (cdb->byte2 & SRS_DESC) 9422 sense_format = SSD_TYPE_DESC; 9423 else 9424 sense_format = SSD_TYPE_FIXED; 9425 9426 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9427 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9428 ctsio->kern_sg_entries = 0; 9429 9430 /* 9431 * struct scsi_sense_data, which is currently set to 256 bytes, is 9432 * larger than the largest allowed value for the length field in the 9433 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9434 */ 9435 ctsio->residual = 0; 9436 ctsio->kern_data_len = cdb->length; 9437 ctsio->kern_total_len = cdb->length; 9438 9439 ctsio->kern_data_resid = 0; 9440 ctsio->kern_rel_offset = 0; 9441 ctsio->kern_sg_entries = 0; 9442 9443 /* 9444 * If we don't have a LUN, we don't have any pending sense. 9445 */ 9446 if (lun == NULL) 9447 goto no_sense; 9448 9449 have_error = 0; 9450 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9451 /* 9452 * Check for pending sense, and then for pending unit attentions. 9453 * Pending sense gets returned first, then pending unit attentions. 9454 */ 9455 mtx_lock(&lun->lun_lock); 9456 #ifdef CTL_WITH_CA 9457 if (ctl_is_set(lun->have_ca, initidx)) { 9458 scsi_sense_data_type stored_format; 9459 9460 /* 9461 * Check to see which sense format was used for the stored 9462 * sense data. 9463 */ 9464 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9465 9466 /* 9467 * If the user requested a different sense format than the 9468 * one we stored, then we need to convert it to the other 9469 * format. If we're going from descriptor to fixed format 9470 * sense data, we may lose things in translation, depending 9471 * on what options were used. 9472 * 9473 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9474 * for some reason we'll just copy it out as-is. 9475 */ 9476 if ((stored_format == SSD_TYPE_FIXED) 9477 && (sense_format == SSD_TYPE_DESC)) 9478 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9479 &lun->pending_sense[initidx], 9480 (struct scsi_sense_data_desc *)sense_ptr); 9481 else if ((stored_format == SSD_TYPE_DESC) 9482 && (sense_format == SSD_TYPE_FIXED)) 9483 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9484 &lun->pending_sense[initidx], 9485 (struct scsi_sense_data_fixed *)sense_ptr); 9486 else 9487 memcpy(sense_ptr, &lun->pending_sense[initidx], 9488 ctl_min(sizeof(*sense_ptr), 9489 sizeof(lun->pending_sense[initidx]))); 9490 9491 ctl_clear_mask(lun->have_ca, initidx); 9492 have_error = 1; 9493 } else 9494 #endif 9495 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9496 ctl_ua_type ua_type; 9497 9498 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9499 sense_ptr, sense_format); 9500 if (ua_type != CTL_UA_NONE) 9501 have_error = 1; 9502 } 9503 mtx_unlock(&lun->lun_lock); 9504 9505 /* 9506 * We already have a pending error, return it. 9507 */ 9508 if (have_error != 0) { 9509 /* 9510 * We report the SCSI status as OK, since the status of the 9511 * request sense command itself is OK. 9512 * We report 0 for the sense length, because we aren't doing 9513 * autosense in this case. We're reporting sense as 9514 * parameter data. 9515 */ 9516 ctl_set_success(ctsio); 9517 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9518 ctsio->be_move_done = ctl_config_move_done; 9519 ctl_datamove((union ctl_io *)ctsio); 9520 return (CTL_RETVAL_COMPLETE); 9521 } 9522 9523 no_sense: 9524 9525 /* 9526 * No sense information to report, so we report that everything is 9527 * okay. 9528 */ 9529 ctl_set_sense_data(sense_ptr, 9530 lun, 9531 sense_format, 9532 /*current_error*/ 1, 9533 /*sense_key*/ SSD_KEY_NO_SENSE, 9534 /*asc*/ 0x00, 9535 /*ascq*/ 0x00, 9536 SSD_ELEM_NONE); 9537 9538 /* 9539 * We report 0 for the sense length, because we aren't doing 9540 * autosense in this case. We're reporting sense as parameter data. 9541 */ 9542 ctl_set_success(ctsio); 9543 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9544 ctsio->be_move_done = ctl_config_move_done; 9545 ctl_datamove((union ctl_io *)ctsio); 9546 return (CTL_RETVAL_COMPLETE); 9547 } 9548 9549 int 9550 ctl_tur(struct ctl_scsiio *ctsio) 9551 { 9552 9553 CTL_DEBUG_PRINT(("ctl_tur\n")); 9554 9555 ctl_set_success(ctsio); 9556 ctl_done((union ctl_io *)ctsio); 9557 9558 return (CTL_RETVAL_COMPLETE); 9559 } 9560 9561 #ifdef notyet 9562 static int 9563 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9564 { 9565 9566 } 9567 #endif 9568 9569 static int 9570 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9571 { 9572 struct scsi_vpd_supported_pages *pages; 9573 int sup_page_size; 9574 struct ctl_lun *lun; 9575 9576 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9577 9578 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9579 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9580 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9581 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9582 ctsio->kern_sg_entries = 0; 9583 9584 if (sup_page_size < alloc_len) { 9585 ctsio->residual = alloc_len - sup_page_size; 9586 ctsio->kern_data_len = sup_page_size; 9587 ctsio->kern_total_len = sup_page_size; 9588 } else { 9589 ctsio->residual = 0; 9590 ctsio->kern_data_len = alloc_len; 9591 ctsio->kern_total_len = alloc_len; 9592 } 9593 ctsio->kern_data_resid = 0; 9594 ctsio->kern_rel_offset = 0; 9595 ctsio->kern_sg_entries = 0; 9596 9597 /* 9598 * The control device is always connected. The disk device, on the 9599 * other hand, may not be online all the time. Need to change this 9600 * to figure out whether the disk device is actually online or not. 9601 */ 9602 if (lun != NULL) 9603 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9604 lun->be_lun->lun_type; 9605 else 9606 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9607 9608 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9609 /* Supported VPD pages */ 9610 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9611 /* Serial Number */ 9612 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9613 /* Device Identification */ 9614 pages->page_list[2] = SVPD_DEVICE_ID; 9615 /* Extended INQUIRY Data */ 9616 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9617 /* Mode Page Policy */ 9618 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9619 /* SCSI Ports */ 9620 pages->page_list[5] = SVPD_SCSI_PORTS; 9621 /* Third-party Copy */ 9622 pages->page_list[6] = SVPD_SCSI_TPC; 9623 /* Block limits */ 9624 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9625 /* Block Device Characteristics */ 9626 pages->page_list[8] = SVPD_BDC; 9627 /* Logical Block Provisioning */ 9628 pages->page_list[9] = SVPD_LBP; 9629 9630 ctl_set_success(ctsio); 9631 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9632 ctsio->be_move_done = ctl_config_move_done; 9633 ctl_datamove((union ctl_io *)ctsio); 9634 return (CTL_RETVAL_COMPLETE); 9635 } 9636 9637 static int 9638 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9639 { 9640 struct scsi_vpd_unit_serial_number *sn_ptr; 9641 struct ctl_lun *lun; 9642 int data_len; 9643 9644 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9645 9646 data_len = 4 + CTL_SN_LEN; 9647 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9648 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9649 if (data_len < alloc_len) { 9650 ctsio->residual = alloc_len - data_len; 9651 ctsio->kern_data_len = data_len; 9652 ctsio->kern_total_len = data_len; 9653 } else { 9654 ctsio->residual = 0; 9655 ctsio->kern_data_len = alloc_len; 9656 ctsio->kern_total_len = alloc_len; 9657 } 9658 ctsio->kern_data_resid = 0; 9659 ctsio->kern_rel_offset = 0; 9660 ctsio->kern_sg_entries = 0; 9661 9662 /* 9663 * The control device is always connected. The disk device, on the 9664 * other hand, may not be online all the time. Need to change this 9665 * to figure out whether the disk device is actually online or not. 9666 */ 9667 if (lun != NULL) 9668 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9669 lun->be_lun->lun_type; 9670 else 9671 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9672 9673 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9674 sn_ptr->length = CTL_SN_LEN; 9675 /* 9676 * If we don't have a LUN, we just leave the serial number as 9677 * all spaces. 9678 */ 9679 if (lun != NULL) { 9680 strncpy((char *)sn_ptr->serial_num, 9681 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9682 } else 9683 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9684 9685 ctl_set_success(ctsio); 9686 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9687 ctsio->be_move_done = ctl_config_move_done; 9688 ctl_datamove((union ctl_io *)ctsio); 9689 return (CTL_RETVAL_COMPLETE); 9690 } 9691 9692 9693 static int 9694 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9695 { 9696 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9697 struct ctl_lun *lun; 9698 int data_len; 9699 9700 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9701 9702 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9703 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9704 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9705 ctsio->kern_sg_entries = 0; 9706 9707 if (data_len < alloc_len) { 9708 ctsio->residual = alloc_len - data_len; 9709 ctsio->kern_data_len = data_len; 9710 ctsio->kern_total_len = data_len; 9711 } else { 9712 ctsio->residual = 0; 9713 ctsio->kern_data_len = alloc_len; 9714 ctsio->kern_total_len = alloc_len; 9715 } 9716 ctsio->kern_data_resid = 0; 9717 ctsio->kern_rel_offset = 0; 9718 ctsio->kern_sg_entries = 0; 9719 9720 /* 9721 * The control device is always connected. The disk device, on the 9722 * other hand, may not be online all the time. 9723 */ 9724 if (lun != NULL) 9725 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9726 lun->be_lun->lun_type; 9727 else 9728 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9729 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9730 eid_ptr->page_length = data_len - 4; 9731 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9732 eid_ptr->flags3 = SVPD_EID_V_SUP; 9733 9734 ctl_set_success(ctsio); 9735 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9736 ctsio->be_move_done = ctl_config_move_done; 9737 ctl_datamove((union ctl_io *)ctsio); 9738 return (CTL_RETVAL_COMPLETE); 9739 } 9740 9741 static int 9742 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9743 { 9744 struct scsi_vpd_mode_page_policy *mpp_ptr; 9745 struct ctl_lun *lun; 9746 int data_len; 9747 9748 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9749 9750 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9751 sizeof(struct scsi_vpd_mode_page_policy_descr); 9752 9753 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9754 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9755 ctsio->kern_sg_entries = 0; 9756 9757 if (data_len < alloc_len) { 9758 ctsio->residual = alloc_len - data_len; 9759 ctsio->kern_data_len = data_len; 9760 ctsio->kern_total_len = data_len; 9761 } else { 9762 ctsio->residual = 0; 9763 ctsio->kern_data_len = alloc_len; 9764 ctsio->kern_total_len = alloc_len; 9765 } 9766 ctsio->kern_data_resid = 0; 9767 ctsio->kern_rel_offset = 0; 9768 ctsio->kern_sg_entries = 0; 9769 9770 /* 9771 * The control device is always connected. The disk device, on the 9772 * other hand, may not be online all the time. 9773 */ 9774 if (lun != NULL) 9775 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9776 lun->be_lun->lun_type; 9777 else 9778 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9779 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9780 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9781 mpp_ptr->descr[0].page_code = 0x3f; 9782 mpp_ptr->descr[0].subpage_code = 0xff; 9783 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9784 9785 ctl_set_success(ctsio); 9786 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9787 ctsio->be_move_done = ctl_config_move_done; 9788 ctl_datamove((union ctl_io *)ctsio); 9789 return (CTL_RETVAL_COMPLETE); 9790 } 9791 9792 static int 9793 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9794 { 9795 struct scsi_vpd_device_id *devid_ptr; 9796 struct scsi_vpd_id_descriptor *desc; 9797 struct ctl_softc *ctl_softc; 9798 struct ctl_lun *lun; 9799 struct ctl_port *port; 9800 int data_len; 9801 uint8_t proto; 9802 9803 ctl_softc = control_softc; 9804 9805 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9806 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9807 9808 data_len = sizeof(struct scsi_vpd_device_id) + 9809 sizeof(struct scsi_vpd_id_descriptor) + 9810 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9811 sizeof(struct scsi_vpd_id_descriptor) + 9812 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9813 if (lun && lun->lun_devid) 9814 data_len += lun->lun_devid->len; 9815 if (port->port_devid) 9816 data_len += port->port_devid->len; 9817 if (port->target_devid) 9818 data_len += port->target_devid->len; 9819 9820 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9821 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9822 ctsio->kern_sg_entries = 0; 9823 9824 if (data_len < alloc_len) { 9825 ctsio->residual = alloc_len - data_len; 9826 ctsio->kern_data_len = data_len; 9827 ctsio->kern_total_len = data_len; 9828 } else { 9829 ctsio->residual = 0; 9830 ctsio->kern_data_len = alloc_len; 9831 ctsio->kern_total_len = alloc_len; 9832 } 9833 ctsio->kern_data_resid = 0; 9834 ctsio->kern_rel_offset = 0; 9835 ctsio->kern_sg_entries = 0; 9836 9837 /* 9838 * The control device is always connected. The disk device, on the 9839 * other hand, may not be online all the time. 9840 */ 9841 if (lun != NULL) 9842 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9843 lun->be_lun->lun_type; 9844 else 9845 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9846 devid_ptr->page_code = SVPD_DEVICE_ID; 9847 scsi_ulto2b(data_len - 4, devid_ptr->length); 9848 9849 if (port->port_type == CTL_PORT_FC) 9850 proto = SCSI_PROTO_FC << 4; 9851 else if (port->port_type == CTL_PORT_ISCSI) 9852 proto = SCSI_PROTO_ISCSI << 4; 9853 else 9854 proto = SCSI_PROTO_SPI << 4; 9855 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9856 9857 /* 9858 * We're using a LUN association here. i.e., this device ID is a 9859 * per-LUN identifier. 9860 */ 9861 if (lun && lun->lun_devid) { 9862 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9863 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9864 lun->lun_devid->len); 9865 } 9866 9867 /* 9868 * This is for the WWPN which is a port association. 9869 */ 9870 if (port->port_devid) { 9871 memcpy(desc, port->port_devid->data, port->port_devid->len); 9872 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9873 port->port_devid->len); 9874 } 9875 9876 /* 9877 * This is for the Relative Target Port(type 4h) identifier 9878 */ 9879 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9880 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9881 SVPD_ID_TYPE_RELTARG; 9882 desc->length = 4; 9883 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9884 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9885 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9886 9887 /* 9888 * This is for the Target Port Group(type 5h) identifier 9889 */ 9890 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9891 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9892 SVPD_ID_TYPE_TPORTGRP; 9893 desc->length = 4; 9894 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9895 &desc->identifier[2]); 9896 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9897 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9898 9899 /* 9900 * This is for the Target identifier 9901 */ 9902 if (port->target_devid) { 9903 memcpy(desc, port->target_devid->data, port->target_devid->len); 9904 } 9905 9906 ctl_set_success(ctsio); 9907 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9908 ctsio->be_move_done = ctl_config_move_done; 9909 ctl_datamove((union ctl_io *)ctsio); 9910 return (CTL_RETVAL_COMPLETE); 9911 } 9912 9913 static int 9914 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9915 { 9916 struct ctl_softc *softc = control_softc; 9917 struct scsi_vpd_scsi_ports *sp; 9918 struct scsi_vpd_port_designation *pd; 9919 struct scsi_vpd_port_designation_cont *pdc; 9920 struct ctl_lun *lun; 9921 struct ctl_port *port; 9922 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 9923 int num_target_port_groups; 9924 9925 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9926 9927 if (softc->is_single) 9928 num_target_port_groups = 1; 9929 else 9930 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 9931 num_target_ports = 0; 9932 iid_len = 0; 9933 id_len = 0; 9934 mtx_lock(&softc->ctl_lock); 9935 STAILQ_FOREACH(port, &softc->port_list, links) { 9936 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9937 continue; 9938 if (lun != NULL && 9939 ctl_map_lun_back(port->targ_port, lun->lun) >= 9940 CTL_MAX_LUNS) 9941 continue; 9942 num_target_ports++; 9943 if (port->init_devid) 9944 iid_len += port->init_devid->len; 9945 if (port->port_devid) 9946 id_len += port->port_devid->len; 9947 } 9948 mtx_unlock(&softc->ctl_lock); 9949 9950 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 9951 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9952 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9953 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9954 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9955 ctsio->kern_sg_entries = 0; 9956 9957 if (data_len < alloc_len) { 9958 ctsio->residual = alloc_len - data_len; 9959 ctsio->kern_data_len = data_len; 9960 ctsio->kern_total_len = data_len; 9961 } else { 9962 ctsio->residual = 0; 9963 ctsio->kern_data_len = alloc_len; 9964 ctsio->kern_total_len = alloc_len; 9965 } 9966 ctsio->kern_data_resid = 0; 9967 ctsio->kern_rel_offset = 0; 9968 ctsio->kern_sg_entries = 0; 9969 9970 /* 9971 * The control device is always connected. The disk device, on the 9972 * other hand, may not be online all the time. Need to change this 9973 * to figure out whether the disk device is actually online or not. 9974 */ 9975 if (lun != NULL) 9976 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9977 lun->be_lun->lun_type; 9978 else 9979 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9980 9981 sp->page_code = SVPD_SCSI_PORTS; 9982 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9983 sp->page_length); 9984 pd = &sp->design[0]; 9985 9986 mtx_lock(&softc->ctl_lock); 9987 pg = softc->port_offset / CTL_MAX_PORTS; 9988 for (g = 0; g < num_target_port_groups; g++) { 9989 STAILQ_FOREACH(port, &softc->port_list, links) { 9990 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9991 continue; 9992 if (lun != NULL && 9993 ctl_map_lun_back(port->targ_port, lun->lun) >= 9994 CTL_MAX_LUNS) 9995 continue; 9996 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 9997 scsi_ulto2b(p, pd->relative_port_id); 9998 if (port->init_devid && g == pg) { 9999 iid_len = port->init_devid->len; 10000 memcpy(pd->initiator_transportid, 10001 port->init_devid->data, port->init_devid->len); 10002 } else 10003 iid_len = 0; 10004 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10005 pdc = (struct scsi_vpd_port_designation_cont *) 10006 (&pd->initiator_transportid[iid_len]); 10007 if (port->port_devid && g == pg) { 10008 id_len = port->port_devid->len; 10009 memcpy(pdc->target_port_descriptors, 10010 port->port_devid->data, port->port_devid->len); 10011 } else 10012 id_len = 0; 10013 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10014 pd = (struct scsi_vpd_port_designation *) 10015 ((uint8_t *)pdc->target_port_descriptors + id_len); 10016 } 10017 } 10018 mtx_unlock(&softc->ctl_lock); 10019 10020 ctl_set_success(ctsio); 10021 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10022 ctsio->be_move_done = ctl_config_move_done; 10023 ctl_datamove((union ctl_io *)ctsio); 10024 return (CTL_RETVAL_COMPLETE); 10025 } 10026 10027 static int 10028 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10029 { 10030 struct scsi_vpd_block_limits *bl_ptr; 10031 struct ctl_lun *lun; 10032 int bs; 10033 10034 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10035 10036 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10037 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10038 ctsio->kern_sg_entries = 0; 10039 10040 if (sizeof(*bl_ptr) < alloc_len) { 10041 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10042 ctsio->kern_data_len = sizeof(*bl_ptr); 10043 ctsio->kern_total_len = sizeof(*bl_ptr); 10044 } else { 10045 ctsio->residual = 0; 10046 ctsio->kern_data_len = alloc_len; 10047 ctsio->kern_total_len = alloc_len; 10048 } 10049 ctsio->kern_data_resid = 0; 10050 ctsio->kern_rel_offset = 0; 10051 ctsio->kern_sg_entries = 0; 10052 10053 /* 10054 * The control device is always connected. The disk device, on the 10055 * other hand, may not be online all the time. Need to change this 10056 * to figure out whether the disk device is actually online or not. 10057 */ 10058 if (lun != NULL) 10059 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10060 lun->be_lun->lun_type; 10061 else 10062 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10063 10064 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10065 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10066 bl_ptr->max_cmp_write_len = 0xff; 10067 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10068 if (lun != NULL) { 10069 bs = lun->be_lun->blocksize; 10070 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10071 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10072 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10073 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10074 if (lun->be_lun->pblockexp != 0) { 10075 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10076 bl_ptr->opt_unmap_grain); 10077 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10078 bl_ptr->unmap_grain_align); 10079 } 10080 } 10081 scsi_ulto4b(lun->be_lun->atomicblock, 10082 bl_ptr->max_atomic_transfer_length); 10083 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10084 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10085 } 10086 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10087 10088 ctl_set_success(ctsio); 10089 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10090 ctsio->be_move_done = ctl_config_move_done; 10091 ctl_datamove((union ctl_io *)ctsio); 10092 return (CTL_RETVAL_COMPLETE); 10093 } 10094 10095 static int 10096 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10097 { 10098 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10099 struct ctl_lun *lun; 10100 const char *value; 10101 u_int i; 10102 10103 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10104 10105 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10106 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10107 ctsio->kern_sg_entries = 0; 10108 10109 if (sizeof(*bdc_ptr) < alloc_len) { 10110 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10111 ctsio->kern_data_len = sizeof(*bdc_ptr); 10112 ctsio->kern_total_len = sizeof(*bdc_ptr); 10113 } else { 10114 ctsio->residual = 0; 10115 ctsio->kern_data_len = alloc_len; 10116 ctsio->kern_total_len = alloc_len; 10117 } 10118 ctsio->kern_data_resid = 0; 10119 ctsio->kern_rel_offset = 0; 10120 ctsio->kern_sg_entries = 0; 10121 10122 /* 10123 * The control device is always connected. The disk device, on the 10124 * other hand, may not be online all the time. Need to change this 10125 * to figure out whether the disk device is actually online or not. 10126 */ 10127 if (lun != NULL) 10128 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10129 lun->be_lun->lun_type; 10130 else 10131 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10132 bdc_ptr->page_code = SVPD_BDC; 10133 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10134 if (lun != NULL && 10135 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10136 i = strtol(value, NULL, 0); 10137 else 10138 i = CTL_DEFAULT_ROTATION_RATE; 10139 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10140 if (lun != NULL && 10141 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10142 i = strtol(value, NULL, 0); 10143 else 10144 i = 0; 10145 bdc_ptr->wab_wac_ff = (i & 0x0f); 10146 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10147 10148 ctl_set_success(ctsio); 10149 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10150 ctsio->be_move_done = ctl_config_move_done; 10151 ctl_datamove((union ctl_io *)ctsio); 10152 return (CTL_RETVAL_COMPLETE); 10153 } 10154 10155 static int 10156 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10157 { 10158 struct scsi_vpd_logical_block_prov *lbp_ptr; 10159 struct ctl_lun *lun; 10160 10161 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10162 10163 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10164 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10165 ctsio->kern_sg_entries = 0; 10166 10167 if (sizeof(*lbp_ptr) < alloc_len) { 10168 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10169 ctsio->kern_data_len = sizeof(*lbp_ptr); 10170 ctsio->kern_total_len = sizeof(*lbp_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 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10187 lun->be_lun->lun_type; 10188 else 10189 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10190 10191 lbp_ptr->page_code = SVPD_LBP; 10192 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10193 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10194 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10195 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10196 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10197 lbp_ptr->prov_type = SVPD_LBP_THIN; 10198 } 10199 10200 ctl_set_success(ctsio); 10201 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10202 ctsio->be_move_done = ctl_config_move_done; 10203 ctl_datamove((union ctl_io *)ctsio); 10204 return (CTL_RETVAL_COMPLETE); 10205 } 10206 10207 static int 10208 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10209 { 10210 struct scsi_inquiry *cdb; 10211 int alloc_len, retval; 10212 10213 cdb = (struct scsi_inquiry *)ctsio->cdb; 10214 10215 retval = CTL_RETVAL_COMPLETE; 10216 10217 alloc_len = scsi_2btoul(cdb->length); 10218 10219 switch (cdb->page_code) { 10220 case SVPD_SUPPORTED_PAGES: 10221 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10222 break; 10223 case SVPD_UNIT_SERIAL_NUMBER: 10224 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10225 break; 10226 case SVPD_DEVICE_ID: 10227 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10228 break; 10229 case SVPD_EXTENDED_INQUIRY_DATA: 10230 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10231 break; 10232 case SVPD_MODE_PAGE_POLICY: 10233 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10234 break; 10235 case SVPD_SCSI_PORTS: 10236 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10237 break; 10238 case SVPD_SCSI_TPC: 10239 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10240 break; 10241 case SVPD_BLOCK_LIMITS: 10242 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10243 break; 10244 case SVPD_BDC: 10245 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10246 break; 10247 case SVPD_LBP: 10248 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10249 break; 10250 default: 10251 ctl_set_invalid_field(ctsio, 10252 /*sks_valid*/ 1, 10253 /*command*/ 1, 10254 /*field*/ 2, 10255 /*bit_valid*/ 0, 10256 /*bit*/ 0); 10257 ctl_done((union ctl_io *)ctsio); 10258 retval = CTL_RETVAL_COMPLETE; 10259 break; 10260 } 10261 10262 return (retval); 10263 } 10264 10265 static int 10266 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10267 { 10268 struct scsi_inquiry_data *inq_ptr; 10269 struct scsi_inquiry *cdb; 10270 struct ctl_softc *ctl_softc; 10271 struct ctl_lun *lun; 10272 char *val; 10273 uint32_t alloc_len, data_len; 10274 ctl_port_type port_type; 10275 10276 ctl_softc = control_softc; 10277 10278 /* 10279 * Figure out whether we're talking to a Fibre Channel port or not. 10280 * We treat the ioctl front end, and any SCSI adapters, as packetized 10281 * SCSI front ends. 10282 */ 10283 port_type = ctl_softc->ctl_ports[ 10284 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10285 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10286 port_type = CTL_PORT_SCSI; 10287 10288 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10289 cdb = (struct scsi_inquiry *)ctsio->cdb; 10290 alloc_len = scsi_2btoul(cdb->length); 10291 10292 /* 10293 * We malloc the full inquiry data size here and fill it 10294 * in. If the user only asks for less, we'll give him 10295 * that much. 10296 */ 10297 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10298 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10299 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10300 ctsio->kern_sg_entries = 0; 10301 ctsio->kern_data_resid = 0; 10302 ctsio->kern_rel_offset = 0; 10303 10304 if (data_len < alloc_len) { 10305 ctsio->residual = alloc_len - data_len; 10306 ctsio->kern_data_len = data_len; 10307 ctsio->kern_total_len = data_len; 10308 } else { 10309 ctsio->residual = 0; 10310 ctsio->kern_data_len = alloc_len; 10311 ctsio->kern_total_len = alloc_len; 10312 } 10313 10314 /* 10315 * If we have a LUN configured, report it as connected. Otherwise, 10316 * report that it is offline or no device is supported, depending 10317 * on the value of inquiry_pq_no_lun. 10318 * 10319 * According to the spec (SPC-4 r34), the peripheral qualifier 10320 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10321 * 10322 * "A peripheral device having the specified peripheral device type 10323 * is not connected to this logical unit. However, the device 10324 * server is capable of supporting the specified peripheral device 10325 * type on this logical unit." 10326 * 10327 * According to the same spec, the peripheral qualifier 10328 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10329 * 10330 * "The device server is not capable of supporting a peripheral 10331 * device on this logical unit. For this peripheral qualifier the 10332 * peripheral device type shall be set to 1Fh. All other peripheral 10333 * device type values are reserved for this peripheral qualifier." 10334 * 10335 * Given the text, it would seem that we probably want to report that 10336 * the LUN is offline here. There is no LUN connected, but we can 10337 * support a LUN at the given LUN number. 10338 * 10339 * In the real world, though, it sounds like things are a little 10340 * different: 10341 * 10342 * - Linux, when presented with a LUN with the offline peripheral 10343 * qualifier, will create an sg driver instance for it. So when 10344 * you attach it to CTL, you wind up with a ton of sg driver 10345 * instances. (One for every LUN that Linux bothered to probe.) 10346 * Linux does this despite the fact that it issues a REPORT LUNs 10347 * to LUN 0 to get the inventory of supported LUNs. 10348 * 10349 * - There is other anecdotal evidence (from Emulex folks) about 10350 * arrays that use the offline peripheral qualifier for LUNs that 10351 * are on the "passive" path in an active/passive array. 10352 * 10353 * So the solution is provide a hopefully reasonable default 10354 * (return bad/no LUN) and allow the user to change the behavior 10355 * with a tunable/sysctl variable. 10356 */ 10357 if (lun != NULL) 10358 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10359 lun->be_lun->lun_type; 10360 else if (ctl_softc->inquiry_pq_no_lun == 0) 10361 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10362 else 10363 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10364 10365 /* RMB in byte 2 is 0 */ 10366 inq_ptr->version = SCSI_REV_SPC4; 10367 10368 /* 10369 * According to SAM-3, even if a device only supports a single 10370 * level of LUN addressing, it should still set the HISUP bit: 10371 * 10372 * 4.9.1 Logical unit numbers overview 10373 * 10374 * All logical unit number formats described in this standard are 10375 * hierarchical in structure even when only a single level in that 10376 * hierarchy is used. The HISUP bit shall be set to one in the 10377 * standard INQUIRY data (see SPC-2) when any logical unit number 10378 * format described in this standard is used. Non-hierarchical 10379 * formats are outside the scope of this standard. 10380 * 10381 * Therefore we set the HiSup bit here. 10382 * 10383 * The reponse format is 2, per SPC-3. 10384 */ 10385 inq_ptr->response_format = SID_HiSup | 2; 10386 10387 inq_ptr->additional_length = data_len - 10388 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10389 CTL_DEBUG_PRINT(("additional_length = %d\n", 10390 inq_ptr->additional_length)); 10391 10392 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10393 /* 16 bit addressing */ 10394 if (port_type == CTL_PORT_SCSI) 10395 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10396 /* XXX set the SID_MultiP bit here if we're actually going to 10397 respond on multiple ports */ 10398 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10399 10400 /* 16 bit data bus, synchronous transfers */ 10401 if (port_type == CTL_PORT_SCSI) 10402 inq_ptr->flags = SID_WBus16 | SID_Sync; 10403 /* 10404 * XXX KDM do we want to support tagged queueing on the control 10405 * device at all? 10406 */ 10407 if ((lun == NULL) 10408 || (lun->be_lun->lun_type != T_PROCESSOR)) 10409 inq_ptr->flags |= SID_CmdQue; 10410 /* 10411 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10412 * We have 8 bytes for the vendor name, and 16 bytes for the device 10413 * name and 4 bytes for the revision. 10414 */ 10415 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10416 "vendor")) == NULL) { 10417 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10418 } else { 10419 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10420 strncpy(inq_ptr->vendor, val, 10421 min(sizeof(inq_ptr->vendor), strlen(val))); 10422 } 10423 if (lun == NULL) { 10424 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10425 sizeof(inq_ptr->product)); 10426 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10427 switch (lun->be_lun->lun_type) { 10428 case T_DIRECT: 10429 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10430 sizeof(inq_ptr->product)); 10431 break; 10432 case T_PROCESSOR: 10433 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10434 sizeof(inq_ptr->product)); 10435 break; 10436 default: 10437 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10438 sizeof(inq_ptr->product)); 10439 break; 10440 } 10441 } else { 10442 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10443 strncpy(inq_ptr->product, val, 10444 min(sizeof(inq_ptr->product), strlen(val))); 10445 } 10446 10447 /* 10448 * XXX make this a macro somewhere so it automatically gets 10449 * incremented when we make changes. 10450 */ 10451 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10452 "revision")) == NULL) { 10453 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10454 } else { 10455 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10456 strncpy(inq_ptr->revision, val, 10457 min(sizeof(inq_ptr->revision), strlen(val))); 10458 } 10459 10460 /* 10461 * For parallel SCSI, we support double transition and single 10462 * transition clocking. We also support QAS (Quick Arbitration 10463 * and Selection) and Information Unit transfers on both the 10464 * control and array devices. 10465 */ 10466 if (port_type == CTL_PORT_SCSI) 10467 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10468 SID_SPI_IUS; 10469 10470 /* SAM-5 (no version claimed) */ 10471 scsi_ulto2b(0x00A0, inq_ptr->version1); 10472 /* SPC-4 (no version claimed) */ 10473 scsi_ulto2b(0x0460, inq_ptr->version2); 10474 if (port_type == CTL_PORT_FC) { 10475 /* FCP-2 ANSI INCITS.350:2003 */ 10476 scsi_ulto2b(0x0917, inq_ptr->version3); 10477 } else if (port_type == CTL_PORT_SCSI) { 10478 /* SPI-4 ANSI INCITS.362:200x */ 10479 scsi_ulto2b(0x0B56, inq_ptr->version3); 10480 } else if (port_type == CTL_PORT_ISCSI) { 10481 /* iSCSI (no version claimed) */ 10482 scsi_ulto2b(0x0960, inq_ptr->version3); 10483 } else if (port_type == CTL_PORT_SAS) { 10484 /* SAS (no version claimed) */ 10485 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10486 } 10487 10488 if (lun == NULL) { 10489 /* SBC-4 (no version claimed) */ 10490 scsi_ulto2b(0x0600, inq_ptr->version4); 10491 } else { 10492 switch (lun->be_lun->lun_type) { 10493 case T_DIRECT: 10494 /* SBC-4 (no version claimed) */ 10495 scsi_ulto2b(0x0600, inq_ptr->version4); 10496 break; 10497 case T_PROCESSOR: 10498 default: 10499 break; 10500 } 10501 } 10502 10503 ctl_set_success(ctsio); 10504 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10505 ctsio->be_move_done = ctl_config_move_done; 10506 ctl_datamove((union ctl_io *)ctsio); 10507 return (CTL_RETVAL_COMPLETE); 10508 } 10509 10510 int 10511 ctl_inquiry(struct ctl_scsiio *ctsio) 10512 { 10513 struct scsi_inquiry *cdb; 10514 int retval; 10515 10516 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10517 10518 cdb = (struct scsi_inquiry *)ctsio->cdb; 10519 if (cdb->byte2 & SI_EVPD) 10520 retval = ctl_inquiry_evpd(ctsio); 10521 else if (cdb->page_code == 0) 10522 retval = ctl_inquiry_std(ctsio); 10523 else { 10524 ctl_set_invalid_field(ctsio, 10525 /*sks_valid*/ 1, 10526 /*command*/ 1, 10527 /*field*/ 2, 10528 /*bit_valid*/ 0, 10529 /*bit*/ 0); 10530 ctl_done((union ctl_io *)ctsio); 10531 return (CTL_RETVAL_COMPLETE); 10532 } 10533 10534 return (retval); 10535 } 10536 10537 /* 10538 * For known CDB types, parse the LBA and length. 10539 */ 10540 static int 10541 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10542 { 10543 if (io->io_hdr.io_type != CTL_IO_SCSI) 10544 return (1); 10545 10546 switch (io->scsiio.cdb[0]) { 10547 case COMPARE_AND_WRITE: { 10548 struct scsi_compare_and_write *cdb; 10549 10550 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10551 10552 *lba = scsi_8btou64(cdb->addr); 10553 *len = cdb->length; 10554 break; 10555 } 10556 case READ_6: 10557 case WRITE_6: { 10558 struct scsi_rw_6 *cdb; 10559 10560 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10561 10562 *lba = scsi_3btoul(cdb->addr); 10563 /* only 5 bits are valid in the most significant address byte */ 10564 *lba &= 0x1fffff; 10565 *len = cdb->length; 10566 break; 10567 } 10568 case READ_10: 10569 case WRITE_10: { 10570 struct scsi_rw_10 *cdb; 10571 10572 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10573 10574 *lba = scsi_4btoul(cdb->addr); 10575 *len = scsi_2btoul(cdb->length); 10576 break; 10577 } 10578 case WRITE_VERIFY_10: { 10579 struct scsi_write_verify_10 *cdb; 10580 10581 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10582 10583 *lba = scsi_4btoul(cdb->addr); 10584 *len = scsi_2btoul(cdb->length); 10585 break; 10586 } 10587 case READ_12: 10588 case WRITE_12: { 10589 struct scsi_rw_12 *cdb; 10590 10591 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10592 10593 *lba = scsi_4btoul(cdb->addr); 10594 *len = scsi_4btoul(cdb->length); 10595 break; 10596 } 10597 case WRITE_VERIFY_12: { 10598 struct scsi_write_verify_12 *cdb; 10599 10600 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10601 10602 *lba = scsi_4btoul(cdb->addr); 10603 *len = scsi_4btoul(cdb->length); 10604 break; 10605 } 10606 case READ_16: 10607 case WRITE_16: 10608 case WRITE_ATOMIC_16: { 10609 struct scsi_rw_16 *cdb; 10610 10611 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10612 10613 *lba = scsi_8btou64(cdb->addr); 10614 *len = scsi_4btoul(cdb->length); 10615 break; 10616 } 10617 case WRITE_VERIFY_16: { 10618 struct scsi_write_verify_16 *cdb; 10619 10620 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10621 10622 *lba = scsi_8btou64(cdb->addr); 10623 *len = scsi_4btoul(cdb->length); 10624 break; 10625 } 10626 case WRITE_SAME_10: { 10627 struct scsi_write_same_10 *cdb; 10628 10629 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10630 10631 *lba = scsi_4btoul(cdb->addr); 10632 *len = scsi_2btoul(cdb->length); 10633 break; 10634 } 10635 case WRITE_SAME_16: { 10636 struct scsi_write_same_16 *cdb; 10637 10638 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10639 10640 *lba = scsi_8btou64(cdb->addr); 10641 *len = scsi_4btoul(cdb->length); 10642 break; 10643 } 10644 case VERIFY_10: { 10645 struct scsi_verify_10 *cdb; 10646 10647 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10648 10649 *lba = scsi_4btoul(cdb->addr); 10650 *len = scsi_2btoul(cdb->length); 10651 break; 10652 } 10653 case VERIFY_12: { 10654 struct scsi_verify_12 *cdb; 10655 10656 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10657 10658 *lba = scsi_4btoul(cdb->addr); 10659 *len = scsi_4btoul(cdb->length); 10660 break; 10661 } 10662 case VERIFY_16: { 10663 struct scsi_verify_16 *cdb; 10664 10665 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10666 10667 *lba = scsi_8btou64(cdb->addr); 10668 *len = scsi_4btoul(cdb->length); 10669 break; 10670 } 10671 case UNMAP: { 10672 *lba = 0; 10673 *len = UINT64_MAX; 10674 break; 10675 } 10676 default: 10677 return (1); 10678 break; /* NOTREACHED */ 10679 } 10680 10681 return (0); 10682 } 10683 10684 static ctl_action 10685 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10686 { 10687 uint64_t endlba1, endlba2; 10688 10689 endlba1 = lba1 + len1 - 1; 10690 endlba2 = lba2 + len2 - 1; 10691 10692 if ((endlba1 < lba2) 10693 || (endlba2 < lba1)) 10694 return (CTL_ACTION_PASS); 10695 else 10696 return (CTL_ACTION_BLOCK); 10697 } 10698 10699 static int 10700 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10701 { 10702 struct ctl_ptr_len_flags *ptrlen; 10703 struct scsi_unmap_desc *buf, *end, *range; 10704 uint64_t lba; 10705 uint32_t len; 10706 10707 /* If not UNMAP -- go other way. */ 10708 if (io->io_hdr.io_type != CTL_IO_SCSI || 10709 io->scsiio.cdb[0] != UNMAP) 10710 return (CTL_ACTION_ERROR); 10711 10712 /* If UNMAP without data -- block and wait for data. */ 10713 ptrlen = (struct ctl_ptr_len_flags *) 10714 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10715 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10716 ptrlen->ptr == NULL) 10717 return (CTL_ACTION_BLOCK); 10718 10719 /* UNMAP with data -- check for collision. */ 10720 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10721 end = buf + ptrlen->len / sizeof(*buf); 10722 for (range = buf; range < end; range++) { 10723 lba = scsi_8btou64(range->lba); 10724 len = scsi_4btoul(range->length); 10725 if ((lba < lba2 + len2) && (lba + len > lba2)) 10726 return (CTL_ACTION_BLOCK); 10727 } 10728 return (CTL_ACTION_PASS); 10729 } 10730 10731 static ctl_action 10732 ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10733 { 10734 uint64_t lba1, lba2; 10735 uint64_t len1, len2; 10736 int retval; 10737 10738 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10739 return (CTL_ACTION_ERROR); 10740 10741 retval = ctl_extent_check_unmap(io2, lba1, len1); 10742 if (retval != CTL_ACTION_ERROR) 10743 return (retval); 10744 10745 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10746 return (CTL_ACTION_ERROR); 10747 10748 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10749 } 10750 10751 static ctl_action 10752 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10753 union ctl_io *ooa_io) 10754 { 10755 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10756 ctl_serialize_action *serialize_row; 10757 10758 /* 10759 * The initiator attempted multiple untagged commands at the same 10760 * time. Can't do that. 10761 */ 10762 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10763 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10764 && ((pending_io->io_hdr.nexus.targ_port == 10765 ooa_io->io_hdr.nexus.targ_port) 10766 && (pending_io->io_hdr.nexus.initid.id == 10767 ooa_io->io_hdr.nexus.initid.id)) 10768 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10769 return (CTL_ACTION_OVERLAP); 10770 10771 /* 10772 * The initiator attempted to send multiple tagged commands with 10773 * the same ID. (It's fine if different initiators have the same 10774 * tag ID.) 10775 * 10776 * Even if all of those conditions are true, we don't kill the I/O 10777 * if the command ahead of us has been aborted. We won't end up 10778 * sending it to the FETD, and it's perfectly legal to resend a 10779 * command with the same tag number as long as the previous 10780 * instance of this tag number has been aborted somehow. 10781 */ 10782 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10783 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10784 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10785 && ((pending_io->io_hdr.nexus.targ_port == 10786 ooa_io->io_hdr.nexus.targ_port) 10787 && (pending_io->io_hdr.nexus.initid.id == 10788 ooa_io->io_hdr.nexus.initid.id)) 10789 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10790 return (CTL_ACTION_OVERLAP_TAG); 10791 10792 /* 10793 * If we get a head of queue tag, SAM-3 says that we should 10794 * immediately execute it. 10795 * 10796 * What happens if this command would normally block for some other 10797 * reason? e.g. a request sense with a head of queue tag 10798 * immediately after a write. Normally that would block, but this 10799 * will result in its getting executed immediately... 10800 * 10801 * We currently return "pass" instead of "skip", so we'll end up 10802 * going through the rest of the queue to check for overlapped tags. 10803 * 10804 * XXX KDM check for other types of blockage first?? 10805 */ 10806 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10807 return (CTL_ACTION_PASS); 10808 10809 /* 10810 * Ordered tags have to block until all items ahead of them 10811 * have completed. If we get called with an ordered tag, we always 10812 * block, if something else is ahead of us in the queue. 10813 */ 10814 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10815 return (CTL_ACTION_BLOCK); 10816 10817 /* 10818 * Simple tags get blocked until all head of queue and ordered tags 10819 * ahead of them have completed. I'm lumping untagged commands in 10820 * with simple tags here. XXX KDM is that the right thing to do? 10821 */ 10822 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10823 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10824 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10825 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10826 return (CTL_ACTION_BLOCK); 10827 10828 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10829 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10830 10831 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10832 10833 switch (serialize_row[pending_entry->seridx]) { 10834 case CTL_SER_BLOCK: 10835 return (CTL_ACTION_BLOCK); 10836 case CTL_SER_EXTENT: 10837 return (ctl_extent_check(pending_io, ooa_io)); 10838 case CTL_SER_EXTENTOPT: 10839 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10840 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10841 return (ctl_extent_check(pending_io, ooa_io)); 10842 /* FALLTHROUGH */ 10843 case CTL_SER_PASS: 10844 return (CTL_ACTION_PASS); 10845 case CTL_SER_BLOCKOPT: 10846 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10847 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10848 return (CTL_ACTION_BLOCK); 10849 return (CTL_ACTION_PASS); 10850 case CTL_SER_SKIP: 10851 return (CTL_ACTION_SKIP); 10852 default: 10853 panic("invalid serialization value %d", 10854 serialize_row[pending_entry->seridx]); 10855 } 10856 10857 return (CTL_ACTION_ERROR); 10858 } 10859 10860 /* 10861 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10862 * Assumptions: 10863 * - pending_io is generally either incoming, or on the blocked queue 10864 * - starting I/O is the I/O we want to start the check with. 10865 */ 10866 static ctl_action 10867 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10868 union ctl_io *starting_io) 10869 { 10870 union ctl_io *ooa_io; 10871 ctl_action action; 10872 10873 mtx_assert(&lun->lun_lock, MA_OWNED); 10874 10875 /* 10876 * Run back along the OOA queue, starting with the current 10877 * blocked I/O and going through every I/O before it on the 10878 * queue. If starting_io is NULL, we'll just end up returning 10879 * CTL_ACTION_PASS. 10880 */ 10881 for (ooa_io = starting_io; ooa_io != NULL; 10882 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10883 ooa_links)){ 10884 10885 /* 10886 * This routine just checks to see whether 10887 * cur_blocked is blocked by ooa_io, which is ahead 10888 * of it in the queue. It doesn't queue/dequeue 10889 * cur_blocked. 10890 */ 10891 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10892 switch (action) { 10893 case CTL_ACTION_BLOCK: 10894 case CTL_ACTION_OVERLAP: 10895 case CTL_ACTION_OVERLAP_TAG: 10896 case CTL_ACTION_SKIP: 10897 case CTL_ACTION_ERROR: 10898 return (action); 10899 break; /* NOTREACHED */ 10900 case CTL_ACTION_PASS: 10901 break; 10902 default: 10903 panic("invalid action %d", action); 10904 break; /* NOTREACHED */ 10905 } 10906 } 10907 10908 return (CTL_ACTION_PASS); 10909 } 10910 10911 /* 10912 * Assumptions: 10913 * - An I/O has just completed, and has been removed from the per-LUN OOA 10914 * queue, so some items on the blocked queue may now be unblocked. 10915 */ 10916 static int 10917 ctl_check_blocked(struct ctl_lun *lun) 10918 { 10919 union ctl_io *cur_blocked, *next_blocked; 10920 10921 mtx_assert(&lun->lun_lock, MA_OWNED); 10922 10923 /* 10924 * Run forward from the head of the blocked queue, checking each 10925 * entry against the I/Os prior to it on the OOA queue to see if 10926 * there is still any blockage. 10927 * 10928 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10929 * with our removing a variable on it while it is traversing the 10930 * list. 10931 */ 10932 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10933 cur_blocked != NULL; cur_blocked = next_blocked) { 10934 union ctl_io *prev_ooa; 10935 ctl_action action; 10936 10937 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10938 blocked_links); 10939 10940 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10941 ctl_ooaq, ooa_links); 10942 10943 /* 10944 * If cur_blocked happens to be the first item in the OOA 10945 * queue now, prev_ooa will be NULL, and the action 10946 * returned will just be CTL_ACTION_PASS. 10947 */ 10948 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10949 10950 switch (action) { 10951 case CTL_ACTION_BLOCK: 10952 /* Nothing to do here, still blocked */ 10953 break; 10954 case CTL_ACTION_OVERLAP: 10955 case CTL_ACTION_OVERLAP_TAG: 10956 /* 10957 * This shouldn't happen! In theory we've already 10958 * checked this command for overlap... 10959 */ 10960 break; 10961 case CTL_ACTION_PASS: 10962 case CTL_ACTION_SKIP: { 10963 struct ctl_softc *softc; 10964 const struct ctl_cmd_entry *entry; 10965 uint32_t initidx; 10966 int isc_retval; 10967 10968 /* 10969 * The skip case shouldn't happen, this transaction 10970 * should have never made it onto the blocked queue. 10971 */ 10972 /* 10973 * This I/O is no longer blocked, we can remove it 10974 * from the blocked queue. Since this is a TAILQ 10975 * (doubly linked list), we can do O(1) removals 10976 * from any place on the list. 10977 */ 10978 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10979 blocked_links); 10980 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10981 10982 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10983 /* 10984 * Need to send IO back to original side to 10985 * run 10986 */ 10987 union ctl_ha_msg msg_info; 10988 10989 msg_info.hdr.original_sc = 10990 cur_blocked->io_hdr.original_sc; 10991 msg_info.hdr.serializing_sc = cur_blocked; 10992 msg_info.hdr.msg_type = CTL_MSG_R2R; 10993 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10994 &msg_info, sizeof(msg_info), 0)) > 10995 CTL_HA_STATUS_SUCCESS) { 10996 printf("CTL:Check Blocked error from " 10997 "ctl_ha_msg_send %d\n", 10998 isc_retval); 10999 } 11000 break; 11001 } 11002 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11003 softc = control_softc; 11004 11005 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11006 11007 /* 11008 * Check this I/O for LUN state changes that may 11009 * have happened while this command was blocked. 11010 * The LUN state may have been changed by a command 11011 * ahead of us in the queue, so we need to re-check 11012 * for any states that can be caused by SCSI 11013 * commands. 11014 */ 11015 if (ctl_scsiio_lun_check(softc, lun, entry, 11016 &cur_blocked->scsiio) == 0) { 11017 cur_blocked->io_hdr.flags |= 11018 CTL_FLAG_IS_WAS_ON_RTR; 11019 ctl_enqueue_rtr(cur_blocked); 11020 } else 11021 ctl_done(cur_blocked); 11022 break; 11023 } 11024 default: 11025 /* 11026 * This probably shouldn't happen -- we shouldn't 11027 * get CTL_ACTION_ERROR, or anything else. 11028 */ 11029 break; 11030 } 11031 } 11032 11033 return (CTL_RETVAL_COMPLETE); 11034 } 11035 11036 /* 11037 * This routine (with one exception) checks LUN flags that can be set by 11038 * commands ahead of us in the OOA queue. These flags have to be checked 11039 * when a command initially comes in, and when we pull a command off the 11040 * blocked queue and are preparing to execute it. The reason we have to 11041 * check these flags for commands on the blocked queue is that the LUN 11042 * state may have been changed by a command ahead of us while we're on the 11043 * blocked queue. 11044 * 11045 * Ordering is somewhat important with these checks, so please pay 11046 * careful attention to the placement of any new checks. 11047 */ 11048 static int 11049 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11050 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11051 { 11052 int retval; 11053 uint32_t residx; 11054 11055 retval = 0; 11056 11057 mtx_assert(&lun->lun_lock, MA_OWNED); 11058 11059 /* 11060 * If this shelf is a secondary shelf controller, we have to reject 11061 * any media access commands. 11062 */ 11063 if ((ctl_softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 11064 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 11065 ctl_set_lun_standby(ctsio); 11066 retval = 1; 11067 goto bailout; 11068 } 11069 11070 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11071 if (lun->flags & CTL_LUN_READONLY) { 11072 ctl_set_sense(ctsio, /*current_error*/ 1, 11073 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11074 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11075 retval = 1; 11076 goto bailout; 11077 } 11078 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11079 .eca_and_aen & SCP_SWP) != 0) { 11080 ctl_set_sense(ctsio, /*current_error*/ 1, 11081 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11082 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11083 retval = 1; 11084 goto bailout; 11085 } 11086 } 11087 11088 /* 11089 * Check for a reservation conflict. If this command isn't allowed 11090 * even on reserved LUNs, and if this initiator isn't the one who 11091 * reserved us, reject the command with a reservation conflict. 11092 */ 11093 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11094 if ((lun->flags & CTL_LUN_RESERVED) 11095 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11096 if (lun->res_idx != residx) { 11097 ctl_set_reservation_conflict(ctsio); 11098 retval = 1; 11099 goto bailout; 11100 } 11101 } 11102 11103 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11104 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11105 /* No reservation or command is allowed. */; 11106 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11107 (lun->res_type == SPR_TYPE_WR_EX || 11108 lun->res_type == SPR_TYPE_WR_EX_RO || 11109 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11110 /* The command is allowed for Write Exclusive resv. */; 11111 } else { 11112 /* 11113 * if we aren't registered or it's a res holder type 11114 * reservation and this isn't the res holder then set a 11115 * conflict. 11116 */ 11117 if (lun->pr_keys[residx] == 0 11118 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11119 ctl_set_reservation_conflict(ctsio); 11120 retval = 1; 11121 goto bailout; 11122 } 11123 11124 } 11125 11126 if ((lun->flags & CTL_LUN_OFFLINE) 11127 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11128 ctl_set_lun_not_ready(ctsio); 11129 retval = 1; 11130 goto bailout; 11131 } 11132 11133 /* 11134 * If the LUN is stopped, see if this particular command is allowed 11135 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11136 */ 11137 if ((lun->flags & CTL_LUN_STOPPED) 11138 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11139 /* "Logical unit not ready, initializing cmd. required" */ 11140 ctl_set_lun_stopped(ctsio); 11141 retval = 1; 11142 goto bailout; 11143 } 11144 11145 if ((lun->flags & CTL_LUN_INOPERABLE) 11146 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11147 /* "Medium format corrupted" */ 11148 ctl_set_medium_format_corrupted(ctsio); 11149 retval = 1; 11150 goto bailout; 11151 } 11152 11153 bailout: 11154 return (retval); 11155 11156 } 11157 11158 static void 11159 ctl_failover_io(union ctl_io *io, int have_lock) 11160 { 11161 ctl_set_busy(&io->scsiio); 11162 ctl_done(io); 11163 } 11164 11165 static void 11166 ctl_failover(void) 11167 { 11168 struct ctl_lun *lun; 11169 struct ctl_softc *ctl_softc; 11170 union ctl_io *next_io, *pending_io; 11171 union ctl_io *io; 11172 int lun_idx; 11173 int i; 11174 11175 ctl_softc = control_softc; 11176 11177 mtx_lock(&ctl_softc->ctl_lock); 11178 /* 11179 * Remove any cmds from the other SC from the rtr queue. These 11180 * will obviously only be for LUNs for which we're the primary. 11181 * We can't send status or get/send data for these commands. 11182 * Since they haven't been executed yet, we can just remove them. 11183 * We'll either abort them or delete them below, depending on 11184 * which HA mode we're in. 11185 */ 11186 #ifdef notyet 11187 mtx_lock(&ctl_softc->queue_lock); 11188 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11189 io != NULL; io = next_io) { 11190 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11191 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11192 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11193 ctl_io_hdr, links); 11194 } 11195 mtx_unlock(&ctl_softc->queue_lock); 11196 #endif 11197 11198 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11199 lun = ctl_softc->ctl_luns[lun_idx]; 11200 if (lun==NULL) 11201 continue; 11202 11203 /* 11204 * Processor LUNs are primary on both sides. 11205 * XXX will this always be true? 11206 */ 11207 if (lun->be_lun->lun_type == T_PROCESSOR) 11208 continue; 11209 11210 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11211 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11212 printf("FAILOVER: primary lun %d\n", lun_idx); 11213 /* 11214 * Remove all commands from the other SC. First from the 11215 * blocked queue then from the ooa queue. Once we have 11216 * removed them. Call ctl_check_blocked to see if there 11217 * is anything that can run. 11218 */ 11219 for (io = (union ctl_io *)TAILQ_FIRST( 11220 &lun->blocked_queue); io != NULL; io = next_io) { 11221 11222 next_io = (union ctl_io *)TAILQ_NEXT( 11223 &io->io_hdr, blocked_links); 11224 11225 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11226 TAILQ_REMOVE(&lun->blocked_queue, 11227 &io->io_hdr,blocked_links); 11228 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11229 TAILQ_REMOVE(&lun->ooa_queue, 11230 &io->io_hdr, ooa_links); 11231 11232 ctl_free_io(io); 11233 } 11234 } 11235 11236 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11237 io != NULL; io = next_io) { 11238 11239 next_io = (union ctl_io *)TAILQ_NEXT( 11240 &io->io_hdr, ooa_links); 11241 11242 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11243 11244 TAILQ_REMOVE(&lun->ooa_queue, 11245 &io->io_hdr, 11246 ooa_links); 11247 11248 ctl_free_io(io); 11249 } 11250 } 11251 ctl_check_blocked(lun); 11252 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11253 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11254 11255 printf("FAILOVER: primary lun %d\n", lun_idx); 11256 /* 11257 * Abort all commands from the other SC. We can't 11258 * send status back for them now. These should get 11259 * cleaned up when they are completed or come out 11260 * for a datamove operation. 11261 */ 11262 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11263 io != NULL; io = next_io) { 11264 next_io = (union ctl_io *)TAILQ_NEXT( 11265 &io->io_hdr, ooa_links); 11266 11267 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11268 io->io_hdr.flags |= CTL_FLAG_ABORT; 11269 } 11270 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11271 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11272 11273 printf("FAILOVER: secondary lun %d\n", lun_idx); 11274 11275 lun->flags |= CTL_LUN_PRIMARY_SC; 11276 11277 /* 11278 * We send all I/O that was sent to this controller 11279 * and redirected to the other side back with 11280 * busy status, and have the initiator retry it. 11281 * Figuring out how much data has been transferred, 11282 * etc. and picking up where we left off would be 11283 * very tricky. 11284 * 11285 * XXX KDM need to remove I/O from the blocked 11286 * queue as well! 11287 */ 11288 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11289 &lun->ooa_queue); pending_io != NULL; 11290 pending_io = next_io) { 11291 11292 next_io = (union ctl_io *)TAILQ_NEXT( 11293 &pending_io->io_hdr, ooa_links); 11294 11295 pending_io->io_hdr.flags &= 11296 ~CTL_FLAG_SENT_2OTHER_SC; 11297 11298 if (pending_io->io_hdr.flags & 11299 CTL_FLAG_IO_ACTIVE) { 11300 pending_io->io_hdr.flags |= 11301 CTL_FLAG_FAILOVER; 11302 } else { 11303 ctl_set_busy(&pending_io->scsiio); 11304 ctl_done(pending_io); 11305 } 11306 } 11307 11308 /* 11309 * Build Unit Attention 11310 */ 11311 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11312 lun->pending_ua[i] |= 11313 CTL_UA_ASYM_ACC_CHANGE; 11314 } 11315 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11316 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11317 printf("FAILOVER: secondary lun %d\n", lun_idx); 11318 /* 11319 * if the first io on the OOA is not on the RtR queue 11320 * add it. 11321 */ 11322 lun->flags |= CTL_LUN_PRIMARY_SC; 11323 11324 pending_io = (union ctl_io *)TAILQ_FIRST( 11325 &lun->ooa_queue); 11326 if (pending_io==NULL) { 11327 printf("Nothing on OOA queue\n"); 11328 continue; 11329 } 11330 11331 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11332 if ((pending_io->io_hdr.flags & 11333 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11334 pending_io->io_hdr.flags |= 11335 CTL_FLAG_IS_WAS_ON_RTR; 11336 ctl_enqueue_rtr(pending_io); 11337 } 11338 #if 0 11339 else 11340 { 11341 printf("Tag 0x%04x is running\n", 11342 pending_io->scsiio.tag_num); 11343 } 11344 #endif 11345 11346 next_io = (union ctl_io *)TAILQ_NEXT( 11347 &pending_io->io_hdr, ooa_links); 11348 for (pending_io=next_io; pending_io != NULL; 11349 pending_io = next_io) { 11350 pending_io->io_hdr.flags &= 11351 ~CTL_FLAG_SENT_2OTHER_SC; 11352 next_io = (union ctl_io *)TAILQ_NEXT( 11353 &pending_io->io_hdr, ooa_links); 11354 if (pending_io->io_hdr.flags & 11355 CTL_FLAG_IS_WAS_ON_RTR) { 11356 #if 0 11357 printf("Tag 0x%04x is running\n", 11358 pending_io->scsiio.tag_num); 11359 #endif 11360 continue; 11361 } 11362 11363 switch (ctl_check_ooa(lun, pending_io, 11364 (union ctl_io *)TAILQ_PREV( 11365 &pending_io->io_hdr, ctl_ooaq, 11366 ooa_links))) { 11367 11368 case CTL_ACTION_BLOCK: 11369 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11370 &pending_io->io_hdr, 11371 blocked_links); 11372 pending_io->io_hdr.flags |= 11373 CTL_FLAG_BLOCKED; 11374 break; 11375 case CTL_ACTION_PASS: 11376 case CTL_ACTION_SKIP: 11377 pending_io->io_hdr.flags |= 11378 CTL_FLAG_IS_WAS_ON_RTR; 11379 ctl_enqueue_rtr(pending_io); 11380 break; 11381 case CTL_ACTION_OVERLAP: 11382 ctl_set_overlapped_cmd( 11383 (struct ctl_scsiio *)pending_io); 11384 ctl_done(pending_io); 11385 break; 11386 case CTL_ACTION_OVERLAP_TAG: 11387 ctl_set_overlapped_tag( 11388 (struct ctl_scsiio *)pending_io, 11389 pending_io->scsiio.tag_num & 0xff); 11390 ctl_done(pending_io); 11391 break; 11392 case CTL_ACTION_ERROR: 11393 default: 11394 ctl_set_internal_failure( 11395 (struct ctl_scsiio *)pending_io, 11396 0, // sks_valid 11397 0); //retry count 11398 ctl_done(pending_io); 11399 break; 11400 } 11401 } 11402 11403 /* 11404 * Build Unit Attention 11405 */ 11406 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11407 lun->pending_ua[i] |= 11408 CTL_UA_ASYM_ACC_CHANGE; 11409 } 11410 } else { 11411 panic("Unhandled HA mode failover, LUN flags = %#x, " 11412 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11413 } 11414 } 11415 ctl_pause_rtr = 0; 11416 mtx_unlock(&ctl_softc->ctl_lock); 11417 } 11418 11419 static int 11420 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11421 { 11422 struct ctl_lun *lun; 11423 const struct ctl_cmd_entry *entry; 11424 uint32_t initidx, targ_lun; 11425 int retval; 11426 11427 retval = 0; 11428 11429 lun = NULL; 11430 11431 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11432 if ((targ_lun < CTL_MAX_LUNS) 11433 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) { 11434 /* 11435 * If the LUN is invalid, pretend that it doesn't exist. 11436 * It will go away as soon as all pending I/O has been 11437 * completed. 11438 */ 11439 mtx_lock(&lun->lun_lock); 11440 if (lun->flags & CTL_LUN_DISABLED) { 11441 mtx_unlock(&lun->lun_lock); 11442 lun = NULL; 11443 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11444 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11445 } else { 11446 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11447 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11448 lun->be_lun; 11449 if (lun->be_lun->lun_type == T_PROCESSOR) { 11450 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11451 } 11452 11453 /* 11454 * Every I/O goes into the OOA queue for a 11455 * particular LUN, and stays there until completion. 11456 */ 11457 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11458 ooa_links); 11459 } 11460 } else { 11461 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11462 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11463 } 11464 11465 /* Get command entry and return error if it is unsuppotyed. */ 11466 entry = ctl_validate_command(ctsio); 11467 if (entry == NULL) { 11468 if (lun) 11469 mtx_unlock(&lun->lun_lock); 11470 return (retval); 11471 } 11472 11473 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11474 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11475 11476 /* 11477 * Check to see whether we can send this command to LUNs that don't 11478 * exist. This should pretty much only be the case for inquiry 11479 * and request sense. Further checks, below, really require having 11480 * a LUN, so we can't really check the command anymore. Just put 11481 * it on the rtr queue. 11482 */ 11483 if (lun == NULL) { 11484 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11485 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11486 ctl_enqueue_rtr((union ctl_io *)ctsio); 11487 return (retval); 11488 } 11489 11490 ctl_set_unsupported_lun(ctsio); 11491 ctl_done((union ctl_io *)ctsio); 11492 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11493 return (retval); 11494 } else { 11495 /* 11496 * Make sure we support this particular command on this LUN. 11497 * e.g., we don't support writes to the control LUN. 11498 */ 11499 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11500 mtx_unlock(&lun->lun_lock); 11501 ctl_set_invalid_opcode(ctsio); 11502 ctl_done((union ctl_io *)ctsio); 11503 return (retval); 11504 } 11505 } 11506 11507 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11508 11509 #ifdef CTL_WITH_CA 11510 /* 11511 * If we've got a request sense, it'll clear the contingent 11512 * allegiance condition. Otherwise, if we have a CA condition for 11513 * this initiator, clear it, because it sent down a command other 11514 * than request sense. 11515 */ 11516 if ((ctsio->cdb[0] != REQUEST_SENSE) 11517 && (ctl_is_set(lun->have_ca, initidx))) 11518 ctl_clear_mask(lun->have_ca, initidx); 11519 #endif 11520 11521 /* 11522 * If the command has this flag set, it handles its own unit 11523 * attention reporting, we shouldn't do anything. Otherwise we 11524 * check for any pending unit attentions, and send them back to the 11525 * initiator. We only do this when a command initially comes in, 11526 * not when we pull it off the blocked queue. 11527 * 11528 * According to SAM-3, section 5.3.2, the order that things get 11529 * presented back to the host is basically unit attentions caused 11530 * by some sort of reset event, busy status, reservation conflicts 11531 * or task set full, and finally any other status. 11532 * 11533 * One issue here is that some of the unit attentions we report 11534 * don't fall into the "reset" category (e.g. "reported luns data 11535 * has changed"). So reporting it here, before the reservation 11536 * check, may be technically wrong. I guess the only thing to do 11537 * would be to check for and report the reset events here, and then 11538 * check for the other unit attention types after we check for a 11539 * reservation conflict. 11540 * 11541 * XXX KDM need to fix this 11542 */ 11543 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11544 ctl_ua_type ua_type; 11545 11546 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11547 scsi_sense_data_type sense_format; 11548 11549 if (lun != NULL) 11550 sense_format = (lun->flags & 11551 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11552 SSD_TYPE_FIXED; 11553 else 11554 sense_format = SSD_TYPE_FIXED; 11555 11556 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11557 &ctsio->sense_data, sense_format); 11558 if (ua_type != CTL_UA_NONE) { 11559 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11560 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11561 CTL_AUTOSENSE; 11562 ctsio->sense_len = SSD_FULL_SIZE; 11563 mtx_unlock(&lun->lun_lock); 11564 ctl_done((union ctl_io *)ctsio); 11565 return (retval); 11566 } 11567 } 11568 } 11569 11570 11571 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11572 mtx_unlock(&lun->lun_lock); 11573 ctl_done((union ctl_io *)ctsio); 11574 return (retval); 11575 } 11576 11577 /* 11578 * XXX CHD this is where we want to send IO to other side if 11579 * this LUN is secondary on this SC. We will need to make a copy 11580 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11581 * the copy we send as FROM_OTHER. 11582 * We also need to stuff the address of the original IO so we can 11583 * find it easily. Something similar will need be done on the other 11584 * side so when we are done we can find the copy. 11585 */ 11586 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11587 union ctl_ha_msg msg_info; 11588 int isc_retval; 11589 11590 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11591 11592 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11593 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11594 #if 0 11595 printf("1. ctsio %p\n", ctsio); 11596 #endif 11597 msg_info.hdr.serializing_sc = NULL; 11598 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11599 msg_info.scsi.tag_num = ctsio->tag_num; 11600 msg_info.scsi.tag_type = ctsio->tag_type; 11601 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11602 11603 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11604 11605 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11606 (void *)&msg_info, sizeof(msg_info), 0)) > 11607 CTL_HA_STATUS_SUCCESS) { 11608 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11609 isc_retval); 11610 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11611 } else { 11612 #if 0 11613 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11614 #endif 11615 } 11616 11617 /* 11618 * XXX KDM this I/O is off the incoming queue, but hasn't 11619 * been inserted on any other queue. We may need to come 11620 * up with a holding queue while we wait for serialization 11621 * so that we have an idea of what we're waiting for from 11622 * the other side. 11623 */ 11624 mtx_unlock(&lun->lun_lock); 11625 return (retval); 11626 } 11627 11628 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11629 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11630 ctl_ooaq, ooa_links))) { 11631 case CTL_ACTION_BLOCK: 11632 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11633 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11634 blocked_links); 11635 mtx_unlock(&lun->lun_lock); 11636 return (retval); 11637 case CTL_ACTION_PASS: 11638 case CTL_ACTION_SKIP: 11639 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11640 mtx_unlock(&lun->lun_lock); 11641 ctl_enqueue_rtr((union ctl_io *)ctsio); 11642 break; 11643 case CTL_ACTION_OVERLAP: 11644 mtx_unlock(&lun->lun_lock); 11645 ctl_set_overlapped_cmd(ctsio); 11646 ctl_done((union ctl_io *)ctsio); 11647 break; 11648 case CTL_ACTION_OVERLAP_TAG: 11649 mtx_unlock(&lun->lun_lock); 11650 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11651 ctl_done((union ctl_io *)ctsio); 11652 break; 11653 case CTL_ACTION_ERROR: 11654 default: 11655 mtx_unlock(&lun->lun_lock); 11656 ctl_set_internal_failure(ctsio, 11657 /*sks_valid*/ 0, 11658 /*retry_count*/ 0); 11659 ctl_done((union ctl_io *)ctsio); 11660 break; 11661 } 11662 return (retval); 11663 } 11664 11665 const struct ctl_cmd_entry * 11666 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11667 { 11668 const struct ctl_cmd_entry *entry; 11669 int service_action; 11670 11671 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11672 if (sa) 11673 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11674 if (entry->flags & CTL_CMD_FLAG_SA5) { 11675 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11676 entry = &((const struct ctl_cmd_entry *) 11677 entry->execute)[service_action]; 11678 } 11679 return (entry); 11680 } 11681 11682 const struct ctl_cmd_entry * 11683 ctl_validate_command(struct ctl_scsiio *ctsio) 11684 { 11685 const struct ctl_cmd_entry *entry; 11686 int i, sa; 11687 uint8_t diff; 11688 11689 entry = ctl_get_cmd_entry(ctsio, &sa); 11690 if (entry->execute == NULL) { 11691 if (sa) 11692 ctl_set_invalid_field(ctsio, 11693 /*sks_valid*/ 1, 11694 /*command*/ 1, 11695 /*field*/ 1, 11696 /*bit_valid*/ 1, 11697 /*bit*/ 4); 11698 else 11699 ctl_set_invalid_opcode(ctsio); 11700 ctl_done((union ctl_io *)ctsio); 11701 return (NULL); 11702 } 11703 KASSERT(entry->length > 0, 11704 ("Not defined length for command 0x%02x/0x%02x", 11705 ctsio->cdb[0], ctsio->cdb[1])); 11706 for (i = 1; i < entry->length; i++) { 11707 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11708 if (diff == 0) 11709 continue; 11710 ctl_set_invalid_field(ctsio, 11711 /*sks_valid*/ 1, 11712 /*command*/ 1, 11713 /*field*/ i, 11714 /*bit_valid*/ 1, 11715 /*bit*/ fls(diff) - 1); 11716 ctl_done((union ctl_io *)ctsio); 11717 return (NULL); 11718 } 11719 return (entry); 11720 } 11721 11722 static int 11723 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11724 { 11725 11726 switch (lun_type) { 11727 case T_PROCESSOR: 11728 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11729 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11730 return (0); 11731 break; 11732 case T_DIRECT: 11733 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11734 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11735 return (0); 11736 break; 11737 default: 11738 return (0); 11739 } 11740 return (1); 11741 } 11742 11743 static int 11744 ctl_scsiio(struct ctl_scsiio *ctsio) 11745 { 11746 int retval; 11747 const struct ctl_cmd_entry *entry; 11748 11749 retval = CTL_RETVAL_COMPLETE; 11750 11751 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11752 11753 entry = ctl_get_cmd_entry(ctsio, NULL); 11754 11755 /* 11756 * If this I/O has been aborted, just send it straight to 11757 * ctl_done() without executing it. 11758 */ 11759 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11760 ctl_done((union ctl_io *)ctsio); 11761 goto bailout; 11762 } 11763 11764 /* 11765 * All the checks should have been handled by ctl_scsiio_precheck(). 11766 * We should be clear now to just execute the I/O. 11767 */ 11768 retval = entry->execute(ctsio); 11769 11770 bailout: 11771 return (retval); 11772 } 11773 11774 /* 11775 * Since we only implement one target right now, a bus reset simply resets 11776 * our single target. 11777 */ 11778 static int 11779 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11780 { 11781 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11782 } 11783 11784 static int 11785 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11786 ctl_ua_type ua_type) 11787 { 11788 struct ctl_lun *lun; 11789 int retval; 11790 11791 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11792 union ctl_ha_msg msg_info; 11793 11794 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11795 msg_info.hdr.nexus = io->io_hdr.nexus; 11796 if (ua_type==CTL_UA_TARG_RESET) 11797 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11798 else 11799 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11800 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11801 msg_info.hdr.original_sc = NULL; 11802 msg_info.hdr.serializing_sc = NULL; 11803 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11804 (void *)&msg_info, sizeof(msg_info), 0)) { 11805 } 11806 } 11807 retval = 0; 11808 11809 mtx_lock(&ctl_softc->ctl_lock); 11810 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11811 retval += ctl_lun_reset(lun, io, ua_type); 11812 mtx_unlock(&ctl_softc->ctl_lock); 11813 11814 return (retval); 11815 } 11816 11817 /* 11818 * The LUN should always be set. The I/O is optional, and is used to 11819 * distinguish between I/Os sent by this initiator, and by other 11820 * initiators. We set unit attention for initiators other than this one. 11821 * SAM-3 is vague on this point. It does say that a unit attention should 11822 * be established for other initiators when a LUN is reset (see section 11823 * 5.7.3), but it doesn't specifically say that the unit attention should 11824 * be established for this particular initiator when a LUN is reset. Here 11825 * is the relevant text, from SAM-3 rev 8: 11826 * 11827 * 5.7.2 When a SCSI initiator port aborts its own tasks 11828 * 11829 * When a SCSI initiator port causes its own task(s) to be aborted, no 11830 * notification that the task(s) have been aborted shall be returned to 11831 * the SCSI initiator port other than the completion response for the 11832 * command or task management function action that caused the task(s) to 11833 * be aborted and notification(s) associated with related effects of the 11834 * action (e.g., a reset unit attention condition). 11835 * 11836 * XXX KDM for now, we're setting unit attention for all initiators. 11837 */ 11838 static int 11839 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11840 { 11841 union ctl_io *xio; 11842 #if 0 11843 uint32_t initindex; 11844 #endif 11845 int i; 11846 11847 mtx_lock(&lun->lun_lock); 11848 /* 11849 * Run through the OOA queue and abort each I/O. 11850 */ 11851 #if 0 11852 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11853 #endif 11854 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11855 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11856 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11857 } 11858 11859 /* 11860 * This version sets unit attention for every 11861 */ 11862 #if 0 11863 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11864 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11865 if (initindex == i) 11866 continue; 11867 lun->pending_ua[i] |= ua_type; 11868 } 11869 #endif 11870 11871 /* 11872 * A reset (any kind, really) clears reservations established with 11873 * RESERVE/RELEASE. It does not clear reservations established 11874 * with PERSISTENT RESERVE OUT, but we don't support that at the 11875 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11876 * reservations made with the RESERVE/RELEASE commands, because 11877 * those commands are obsolete in SPC-3. 11878 */ 11879 lun->flags &= ~CTL_LUN_RESERVED; 11880 11881 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11882 #ifdef CTL_WITH_CA 11883 ctl_clear_mask(lun->have_ca, i); 11884 #endif 11885 lun->pending_ua[i] |= ua_type; 11886 } 11887 mtx_unlock(&lun->lun_lock); 11888 11889 return (0); 11890 } 11891 11892 static void 11893 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11894 int other_sc) 11895 { 11896 union ctl_io *xio; 11897 11898 mtx_assert(&lun->lun_lock, MA_OWNED); 11899 11900 /* 11901 * Run through the OOA queue and attempt to find the given I/O. 11902 * The target port, initiator ID, tag type and tag number have to 11903 * match the values that we got from the initiator. If we have an 11904 * untagged command to abort, simply abort the first untagged command 11905 * we come to. We only allow one untagged command at a time of course. 11906 */ 11907 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11908 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11909 11910 if ((targ_port == UINT32_MAX || 11911 targ_port == xio->io_hdr.nexus.targ_port) && 11912 (init_id == UINT32_MAX || 11913 init_id == xio->io_hdr.nexus.initid.id)) { 11914 if (targ_port != xio->io_hdr.nexus.targ_port || 11915 init_id != xio->io_hdr.nexus.initid.id) 11916 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11917 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11918 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11919 union ctl_ha_msg msg_info; 11920 11921 msg_info.hdr.nexus = xio->io_hdr.nexus; 11922 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11923 msg_info.task.tag_num = xio->scsiio.tag_num; 11924 msg_info.task.tag_type = xio->scsiio.tag_type; 11925 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11926 msg_info.hdr.original_sc = NULL; 11927 msg_info.hdr.serializing_sc = NULL; 11928 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11929 (void *)&msg_info, sizeof(msg_info), 0); 11930 } 11931 } 11932 } 11933 } 11934 11935 static int 11936 ctl_abort_task_set(union ctl_io *io) 11937 { 11938 struct ctl_softc *softc = control_softc; 11939 struct ctl_lun *lun; 11940 uint32_t targ_lun; 11941 11942 /* 11943 * Look up the LUN. 11944 */ 11945 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11946 mtx_lock(&softc->ctl_lock); 11947 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11948 lun = softc->ctl_luns[targ_lun]; 11949 else { 11950 mtx_unlock(&softc->ctl_lock); 11951 return (1); 11952 } 11953 11954 mtx_lock(&lun->lun_lock); 11955 mtx_unlock(&softc->ctl_lock); 11956 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11957 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11958 io->io_hdr.nexus.initid.id, 11959 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11960 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11961 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11962 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11963 } 11964 mtx_unlock(&lun->lun_lock); 11965 return (0); 11966 } 11967 11968 static int 11969 ctl_i_t_nexus_reset(union ctl_io *io) 11970 { 11971 struct ctl_softc *softc = control_softc; 11972 struct ctl_lun *lun; 11973 uint32_t initindex, residx; 11974 11975 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11976 residx = ctl_get_resindex(&io->io_hdr.nexus); 11977 mtx_lock(&softc->ctl_lock); 11978 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11979 mtx_lock(&lun->lun_lock); 11980 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11981 io->io_hdr.nexus.initid.id, 11982 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11983 #ifdef CTL_WITH_CA 11984 ctl_clear_mask(lun->have_ca, initindex); 11985 #endif 11986 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 11987 lun->flags &= ~CTL_LUN_RESERVED; 11988 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 11989 mtx_unlock(&lun->lun_lock); 11990 } 11991 mtx_unlock(&softc->ctl_lock); 11992 return (0); 11993 } 11994 11995 static int 11996 ctl_abort_task(union ctl_io *io) 11997 { 11998 union ctl_io *xio; 11999 struct ctl_lun *lun; 12000 struct ctl_softc *ctl_softc; 12001 #if 0 12002 struct sbuf sb; 12003 char printbuf[128]; 12004 #endif 12005 int found; 12006 uint32_t targ_lun; 12007 12008 ctl_softc = control_softc; 12009 found = 0; 12010 12011 /* 12012 * Look up the LUN. 12013 */ 12014 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12015 mtx_lock(&ctl_softc->ctl_lock); 12016 if ((targ_lun < CTL_MAX_LUNS) 12017 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12018 lun = ctl_softc->ctl_luns[targ_lun]; 12019 else { 12020 mtx_unlock(&ctl_softc->ctl_lock); 12021 return (1); 12022 } 12023 12024 #if 0 12025 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12026 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12027 #endif 12028 12029 mtx_lock(&lun->lun_lock); 12030 mtx_unlock(&ctl_softc->ctl_lock); 12031 /* 12032 * Run through the OOA queue and attempt to find the given I/O. 12033 * The target port, initiator ID, tag type and tag number have to 12034 * match the values that we got from the initiator. If we have an 12035 * untagged command to abort, simply abort the first untagged command 12036 * we come to. We only allow one untagged command at a time of course. 12037 */ 12038 #if 0 12039 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12040 #endif 12041 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12042 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12043 #if 0 12044 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12045 12046 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12047 lun->lun, xio->scsiio.tag_num, 12048 xio->scsiio.tag_type, 12049 (xio->io_hdr.blocked_links.tqe_prev 12050 == NULL) ? "" : " BLOCKED", 12051 (xio->io_hdr.flags & 12052 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12053 (xio->io_hdr.flags & 12054 CTL_FLAG_ABORT) ? " ABORT" : "", 12055 (xio->io_hdr.flags & 12056 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12057 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12058 sbuf_finish(&sb); 12059 printf("%s\n", sbuf_data(&sb)); 12060 #endif 12061 12062 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12063 && (xio->io_hdr.nexus.initid.id == 12064 io->io_hdr.nexus.initid.id)) { 12065 /* 12066 * If the abort says that the task is untagged, the 12067 * task in the queue must be untagged. Otherwise, 12068 * we just check to see whether the tag numbers 12069 * match. This is because the QLogic firmware 12070 * doesn't pass back the tag type in an abort 12071 * request. 12072 */ 12073 #if 0 12074 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12075 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12076 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12077 #endif 12078 /* 12079 * XXX KDM we've got problems with FC, because it 12080 * doesn't send down a tag type with aborts. So we 12081 * can only really go by the tag number... 12082 * This may cause problems with parallel SCSI. 12083 * Need to figure that out!! 12084 */ 12085 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12086 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12087 found = 1; 12088 if ((io->io_hdr.flags & 12089 CTL_FLAG_FROM_OTHER_SC) == 0 && 12090 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12091 union ctl_ha_msg msg_info; 12092 12093 io->io_hdr.flags |= 12094 CTL_FLAG_SENT_2OTHER_SC; 12095 msg_info.hdr.nexus = io->io_hdr.nexus; 12096 msg_info.task.task_action = 12097 CTL_TASK_ABORT_TASK; 12098 msg_info.task.tag_num = 12099 io->taskio.tag_num; 12100 msg_info.task.tag_type = 12101 io->taskio.tag_type; 12102 msg_info.hdr.msg_type = 12103 CTL_MSG_MANAGE_TASKS; 12104 msg_info.hdr.original_sc = NULL; 12105 msg_info.hdr.serializing_sc = NULL; 12106 #if 0 12107 printf("Sent Abort to other side\n"); 12108 #endif 12109 if (CTL_HA_STATUS_SUCCESS != 12110 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12111 (void *)&msg_info, 12112 sizeof(msg_info), 0)) { 12113 } 12114 } 12115 #if 0 12116 printf("ctl_abort_task: found I/O to abort\n"); 12117 #endif 12118 break; 12119 } 12120 } 12121 } 12122 mtx_unlock(&lun->lun_lock); 12123 12124 if (found == 0) { 12125 /* 12126 * This isn't really an error. It's entirely possible for 12127 * the abort and command completion to cross on the wire. 12128 * This is more of an informative/diagnostic error. 12129 */ 12130 #if 0 12131 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12132 "%d:%d:%d:%d tag %d type %d\n", 12133 io->io_hdr.nexus.initid.id, 12134 io->io_hdr.nexus.targ_port, 12135 io->io_hdr.nexus.targ_target.id, 12136 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12137 io->taskio.tag_type); 12138 #endif 12139 } 12140 return (0); 12141 } 12142 12143 static void 12144 ctl_run_task(union ctl_io *io) 12145 { 12146 struct ctl_softc *ctl_softc = control_softc; 12147 int retval = 1; 12148 const char *task_desc; 12149 12150 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12151 12152 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12153 ("ctl_run_task: Unextected io_type %d\n", 12154 io->io_hdr.io_type)); 12155 12156 task_desc = ctl_scsi_task_string(&io->taskio); 12157 if (task_desc != NULL) { 12158 #ifdef NEEDTOPORT 12159 csevent_log(CSC_CTL | CSC_SHELF_SW | 12160 CTL_TASK_REPORT, 12161 csevent_LogType_Trace, 12162 csevent_Severity_Information, 12163 csevent_AlertLevel_Green, 12164 csevent_FRU_Firmware, 12165 csevent_FRU_Unknown, 12166 "CTL: received task: %s",task_desc); 12167 #endif 12168 } else { 12169 #ifdef NEEDTOPORT 12170 csevent_log(CSC_CTL | CSC_SHELF_SW | 12171 CTL_TASK_REPORT, 12172 csevent_LogType_Trace, 12173 csevent_Severity_Information, 12174 csevent_AlertLevel_Green, 12175 csevent_FRU_Firmware, 12176 csevent_FRU_Unknown, 12177 "CTL: received unknown task " 12178 "type: %d (%#x)", 12179 io->taskio.task_action, 12180 io->taskio.task_action); 12181 #endif 12182 } 12183 switch (io->taskio.task_action) { 12184 case CTL_TASK_ABORT_TASK: 12185 retval = ctl_abort_task(io); 12186 break; 12187 case CTL_TASK_ABORT_TASK_SET: 12188 case CTL_TASK_CLEAR_TASK_SET: 12189 retval = ctl_abort_task_set(io); 12190 break; 12191 case CTL_TASK_CLEAR_ACA: 12192 break; 12193 case CTL_TASK_I_T_NEXUS_RESET: 12194 retval = ctl_i_t_nexus_reset(io); 12195 break; 12196 case CTL_TASK_LUN_RESET: { 12197 struct ctl_lun *lun; 12198 uint32_t targ_lun; 12199 12200 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12201 mtx_lock(&ctl_softc->ctl_lock); 12202 if ((targ_lun < CTL_MAX_LUNS) 12203 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12204 lun = ctl_softc->ctl_luns[targ_lun]; 12205 else { 12206 mtx_unlock(&ctl_softc->ctl_lock); 12207 retval = 1; 12208 break; 12209 } 12210 12211 if (!(io->io_hdr.flags & 12212 CTL_FLAG_FROM_OTHER_SC)) { 12213 union ctl_ha_msg msg_info; 12214 12215 io->io_hdr.flags |= 12216 CTL_FLAG_SENT_2OTHER_SC; 12217 msg_info.hdr.msg_type = 12218 CTL_MSG_MANAGE_TASKS; 12219 msg_info.hdr.nexus = io->io_hdr.nexus; 12220 msg_info.task.task_action = 12221 CTL_TASK_LUN_RESET; 12222 msg_info.hdr.original_sc = NULL; 12223 msg_info.hdr.serializing_sc = NULL; 12224 if (CTL_HA_STATUS_SUCCESS != 12225 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12226 (void *)&msg_info, 12227 sizeof(msg_info), 0)) { 12228 } 12229 } 12230 12231 retval = ctl_lun_reset(lun, io, 12232 CTL_UA_LUN_RESET); 12233 mtx_unlock(&ctl_softc->ctl_lock); 12234 break; 12235 } 12236 case CTL_TASK_TARGET_RESET: 12237 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12238 break; 12239 case CTL_TASK_BUS_RESET: 12240 retval = ctl_bus_reset(ctl_softc, io); 12241 break; 12242 case CTL_TASK_PORT_LOGIN: 12243 break; 12244 case CTL_TASK_PORT_LOGOUT: 12245 break; 12246 default: 12247 printf("ctl_run_task: got unknown task management event %d\n", 12248 io->taskio.task_action); 12249 break; 12250 } 12251 if (retval == 0) 12252 io->io_hdr.status = CTL_SUCCESS; 12253 else 12254 io->io_hdr.status = CTL_ERROR; 12255 ctl_done(io); 12256 } 12257 12258 /* 12259 * For HA operation. Handle commands that come in from the other 12260 * controller. 12261 */ 12262 static void 12263 ctl_handle_isc(union ctl_io *io) 12264 { 12265 int free_io; 12266 struct ctl_lun *lun; 12267 struct ctl_softc *ctl_softc; 12268 uint32_t targ_lun; 12269 12270 ctl_softc = control_softc; 12271 12272 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12273 lun = ctl_softc->ctl_luns[targ_lun]; 12274 12275 switch (io->io_hdr.msg_type) { 12276 case CTL_MSG_SERIALIZE: 12277 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12278 break; 12279 case CTL_MSG_R2R: { 12280 const struct ctl_cmd_entry *entry; 12281 12282 /* 12283 * This is only used in SER_ONLY mode. 12284 */ 12285 free_io = 0; 12286 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12287 mtx_lock(&lun->lun_lock); 12288 if (ctl_scsiio_lun_check(ctl_softc, lun, 12289 entry, (struct ctl_scsiio *)io) != 0) { 12290 mtx_unlock(&lun->lun_lock); 12291 ctl_done(io); 12292 break; 12293 } 12294 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12295 mtx_unlock(&lun->lun_lock); 12296 ctl_enqueue_rtr(io); 12297 break; 12298 } 12299 case CTL_MSG_FINISH_IO: 12300 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12301 free_io = 0; 12302 ctl_done(io); 12303 } else { 12304 free_io = 1; 12305 mtx_lock(&lun->lun_lock); 12306 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12307 ooa_links); 12308 ctl_check_blocked(lun); 12309 mtx_unlock(&lun->lun_lock); 12310 } 12311 break; 12312 case CTL_MSG_PERS_ACTION: 12313 ctl_hndl_per_res_out_on_other_sc( 12314 (union ctl_ha_msg *)&io->presio.pr_msg); 12315 free_io = 1; 12316 break; 12317 case CTL_MSG_BAD_JUJU: 12318 free_io = 0; 12319 ctl_done(io); 12320 break; 12321 case CTL_MSG_DATAMOVE: 12322 /* Only used in XFER mode */ 12323 free_io = 0; 12324 ctl_datamove_remote(io); 12325 break; 12326 case CTL_MSG_DATAMOVE_DONE: 12327 /* Only used in XFER mode */ 12328 free_io = 0; 12329 io->scsiio.be_move_done(io); 12330 break; 12331 default: 12332 free_io = 1; 12333 printf("%s: Invalid message type %d\n", 12334 __func__, io->io_hdr.msg_type); 12335 break; 12336 } 12337 if (free_io) 12338 ctl_free_io(io); 12339 12340 } 12341 12342 12343 /* 12344 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12345 * there is no match. 12346 */ 12347 static ctl_lun_error_pattern 12348 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12349 { 12350 const struct ctl_cmd_entry *entry; 12351 ctl_lun_error_pattern filtered_pattern, pattern; 12352 12353 pattern = desc->error_pattern; 12354 12355 /* 12356 * XXX KDM we need more data passed into this function to match a 12357 * custom pattern, and we actually need to implement custom pattern 12358 * matching. 12359 */ 12360 if (pattern & CTL_LUN_PAT_CMD) 12361 return (CTL_LUN_PAT_CMD); 12362 12363 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12364 return (CTL_LUN_PAT_ANY); 12365 12366 entry = ctl_get_cmd_entry(ctsio, NULL); 12367 12368 filtered_pattern = entry->pattern & pattern; 12369 12370 /* 12371 * If the user requested specific flags in the pattern (e.g. 12372 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12373 * flags. 12374 * 12375 * If the user did not specify any flags, it doesn't matter whether 12376 * or not the command supports the flags. 12377 */ 12378 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12379 (pattern & ~CTL_LUN_PAT_MASK)) 12380 return (CTL_LUN_PAT_NONE); 12381 12382 /* 12383 * If the user asked for a range check, see if the requested LBA 12384 * range overlaps with this command's LBA range. 12385 */ 12386 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12387 uint64_t lba1; 12388 uint64_t len1; 12389 ctl_action action; 12390 int retval; 12391 12392 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12393 if (retval != 0) 12394 return (CTL_LUN_PAT_NONE); 12395 12396 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12397 desc->lba_range.len); 12398 /* 12399 * A "pass" means that the LBA ranges don't overlap, so 12400 * this doesn't match the user's range criteria. 12401 */ 12402 if (action == CTL_ACTION_PASS) 12403 return (CTL_LUN_PAT_NONE); 12404 } 12405 12406 return (filtered_pattern); 12407 } 12408 12409 static void 12410 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12411 { 12412 struct ctl_error_desc *desc, *desc2; 12413 12414 mtx_assert(&lun->lun_lock, MA_OWNED); 12415 12416 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12417 ctl_lun_error_pattern pattern; 12418 /* 12419 * Check to see whether this particular command matches 12420 * the pattern in the descriptor. 12421 */ 12422 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12423 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12424 continue; 12425 12426 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12427 case CTL_LUN_INJ_ABORTED: 12428 ctl_set_aborted(&io->scsiio); 12429 break; 12430 case CTL_LUN_INJ_MEDIUM_ERR: 12431 ctl_set_medium_error(&io->scsiio); 12432 break; 12433 case CTL_LUN_INJ_UA: 12434 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12435 * OCCURRED */ 12436 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12437 break; 12438 case CTL_LUN_INJ_CUSTOM: 12439 /* 12440 * We're assuming the user knows what he is doing. 12441 * Just copy the sense information without doing 12442 * checks. 12443 */ 12444 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12445 ctl_min(sizeof(desc->custom_sense), 12446 sizeof(io->scsiio.sense_data))); 12447 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12448 io->scsiio.sense_len = SSD_FULL_SIZE; 12449 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12450 break; 12451 case CTL_LUN_INJ_NONE: 12452 default: 12453 /* 12454 * If this is an error injection type we don't know 12455 * about, clear the continuous flag (if it is set) 12456 * so it will get deleted below. 12457 */ 12458 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12459 break; 12460 } 12461 /* 12462 * By default, each error injection action is a one-shot 12463 */ 12464 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12465 continue; 12466 12467 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12468 12469 free(desc, M_CTL); 12470 } 12471 } 12472 12473 #ifdef CTL_IO_DELAY 12474 static void 12475 ctl_datamove_timer_wakeup(void *arg) 12476 { 12477 union ctl_io *io; 12478 12479 io = (union ctl_io *)arg; 12480 12481 ctl_datamove(io); 12482 } 12483 #endif /* CTL_IO_DELAY */ 12484 12485 void 12486 ctl_datamove(union ctl_io *io) 12487 { 12488 void (*fe_datamove)(union ctl_io *io); 12489 12490 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12491 12492 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12493 12494 #ifdef CTL_TIME_IO 12495 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12496 char str[256]; 12497 char path_str[64]; 12498 struct sbuf sb; 12499 12500 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12501 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12502 12503 sbuf_cat(&sb, path_str); 12504 switch (io->io_hdr.io_type) { 12505 case CTL_IO_SCSI: 12506 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12507 sbuf_printf(&sb, "\n"); 12508 sbuf_cat(&sb, path_str); 12509 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12510 io->scsiio.tag_num, io->scsiio.tag_type); 12511 break; 12512 case CTL_IO_TASK: 12513 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12514 "Tag Type: %d\n", io->taskio.task_action, 12515 io->taskio.tag_num, io->taskio.tag_type); 12516 break; 12517 default: 12518 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12519 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12520 break; 12521 } 12522 sbuf_cat(&sb, path_str); 12523 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12524 (intmax_t)time_uptime - io->io_hdr.start_time); 12525 sbuf_finish(&sb); 12526 printf("%s", sbuf_data(&sb)); 12527 } 12528 #endif /* CTL_TIME_IO */ 12529 12530 #ifdef CTL_IO_DELAY 12531 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12532 struct ctl_lun *lun; 12533 12534 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12535 12536 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12537 } else { 12538 struct ctl_lun *lun; 12539 12540 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12541 if ((lun != NULL) 12542 && (lun->delay_info.datamove_delay > 0)) { 12543 struct callout *callout; 12544 12545 callout = (struct callout *)&io->io_hdr.timer_bytes; 12546 callout_init(callout, /*mpsafe*/ 1); 12547 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12548 callout_reset(callout, 12549 lun->delay_info.datamove_delay * hz, 12550 ctl_datamove_timer_wakeup, io); 12551 if (lun->delay_info.datamove_type == 12552 CTL_DELAY_TYPE_ONESHOT) 12553 lun->delay_info.datamove_delay = 0; 12554 return; 12555 } 12556 } 12557 #endif 12558 12559 /* 12560 * This command has been aborted. Set the port status, so we fail 12561 * the data move. 12562 */ 12563 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12564 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12565 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12566 io->io_hdr.nexus.targ_port, 12567 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12568 io->io_hdr.nexus.targ_lun); 12569 io->io_hdr.port_status = 31337; 12570 /* 12571 * Note that the backend, in this case, will get the 12572 * callback in its context. In other cases it may get 12573 * called in the frontend's interrupt thread context. 12574 */ 12575 io->scsiio.be_move_done(io); 12576 return; 12577 } 12578 12579 /* Don't confuse frontend with zero length data move. */ 12580 if (io->scsiio.kern_data_len == 0) { 12581 io->scsiio.be_move_done(io); 12582 return; 12583 } 12584 12585 /* 12586 * If we're in XFER mode and this I/O is from the other shelf 12587 * controller, we need to send the DMA to the other side to 12588 * actually transfer the data to/from the host. In serialize only 12589 * mode the transfer happens below CTL and ctl_datamove() is only 12590 * called on the machine that originally received the I/O. 12591 */ 12592 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12593 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12594 union ctl_ha_msg msg; 12595 uint32_t sg_entries_sent; 12596 int do_sg_copy; 12597 int i; 12598 12599 memset(&msg, 0, sizeof(msg)); 12600 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12601 msg.hdr.original_sc = io->io_hdr.original_sc; 12602 msg.hdr.serializing_sc = io; 12603 msg.hdr.nexus = io->io_hdr.nexus; 12604 msg.dt.flags = io->io_hdr.flags; 12605 /* 12606 * We convert everything into a S/G list here. We can't 12607 * pass by reference, only by value between controllers. 12608 * So we can't pass a pointer to the S/G list, only as many 12609 * S/G entries as we can fit in here. If it's possible for 12610 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12611 * then we need to break this up into multiple transfers. 12612 */ 12613 if (io->scsiio.kern_sg_entries == 0) { 12614 msg.dt.kern_sg_entries = 1; 12615 /* 12616 * If this is in cached memory, flush the cache 12617 * before we send the DMA request to the other 12618 * controller. We want to do this in either the 12619 * read or the write case. The read case is 12620 * straightforward. In the write case, we want to 12621 * make sure nothing is in the local cache that 12622 * could overwrite the DMAed data. 12623 */ 12624 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12625 /* 12626 * XXX KDM use bus_dmamap_sync() here. 12627 */ 12628 } 12629 12630 /* 12631 * Convert to a physical address if this is a 12632 * virtual address. 12633 */ 12634 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12635 msg.dt.sg_list[0].addr = 12636 io->scsiio.kern_data_ptr; 12637 } else { 12638 /* 12639 * XXX KDM use busdma here! 12640 */ 12641 #if 0 12642 msg.dt.sg_list[0].addr = (void *) 12643 vtophys(io->scsiio.kern_data_ptr); 12644 #endif 12645 } 12646 12647 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12648 do_sg_copy = 0; 12649 } else { 12650 struct ctl_sg_entry *sgl; 12651 12652 do_sg_copy = 1; 12653 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12654 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12655 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12656 /* 12657 * XXX KDM use bus_dmamap_sync() here. 12658 */ 12659 } 12660 } 12661 12662 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12663 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12664 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12665 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12666 msg.dt.sg_sequence = 0; 12667 12668 /* 12669 * Loop until we've sent all of the S/G entries. On the 12670 * other end, we'll recompose these S/G entries into one 12671 * contiguous list before passing it to the 12672 */ 12673 for (sg_entries_sent = 0; sg_entries_sent < 12674 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12675 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12676 sizeof(msg.dt.sg_list[0])), 12677 msg.dt.kern_sg_entries - sg_entries_sent); 12678 12679 if (do_sg_copy != 0) { 12680 struct ctl_sg_entry *sgl; 12681 int j; 12682 12683 sgl = (struct ctl_sg_entry *) 12684 io->scsiio.kern_data_ptr; 12685 /* 12686 * If this is in cached memory, flush the cache 12687 * before we send the DMA request to the other 12688 * controller. We want to do this in either 12689 * the * read or the write case. The read 12690 * case is straightforward. In the write 12691 * case, we want to make sure nothing is 12692 * in the local cache that could overwrite 12693 * the DMAed data. 12694 */ 12695 12696 for (i = sg_entries_sent, j = 0; 12697 i < msg.dt.cur_sg_entries; i++, j++) { 12698 if ((io->io_hdr.flags & 12699 CTL_FLAG_NO_DATASYNC) == 0) { 12700 /* 12701 * XXX KDM use bus_dmamap_sync() 12702 */ 12703 } 12704 if ((io->io_hdr.flags & 12705 CTL_FLAG_BUS_ADDR) == 0) { 12706 /* 12707 * XXX KDM use busdma. 12708 */ 12709 #if 0 12710 msg.dt.sg_list[j].addr =(void *) 12711 vtophys(sgl[i].addr); 12712 #endif 12713 } else { 12714 msg.dt.sg_list[j].addr = 12715 sgl[i].addr; 12716 } 12717 msg.dt.sg_list[j].len = sgl[i].len; 12718 } 12719 } 12720 12721 sg_entries_sent += msg.dt.cur_sg_entries; 12722 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12723 msg.dt.sg_last = 1; 12724 else 12725 msg.dt.sg_last = 0; 12726 12727 /* 12728 * XXX KDM drop and reacquire the lock here? 12729 */ 12730 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12731 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12732 /* 12733 * XXX do something here. 12734 */ 12735 } 12736 12737 msg.dt.sent_sg_entries = sg_entries_sent; 12738 } 12739 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12740 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12741 ctl_failover_io(io, /*have_lock*/ 0); 12742 12743 } else { 12744 12745 /* 12746 * Lookup the fe_datamove() function for this particular 12747 * front end. 12748 */ 12749 fe_datamove = 12750 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12751 12752 fe_datamove(io); 12753 } 12754 } 12755 12756 static void 12757 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12758 { 12759 union ctl_ha_msg msg; 12760 int isc_status; 12761 12762 memset(&msg, 0, sizeof(msg)); 12763 12764 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12765 msg.hdr.original_sc = io; 12766 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12767 msg.hdr.nexus = io->io_hdr.nexus; 12768 msg.hdr.status = io->io_hdr.status; 12769 msg.scsi.tag_num = io->scsiio.tag_num; 12770 msg.scsi.tag_type = io->scsiio.tag_type; 12771 msg.scsi.scsi_status = io->scsiio.scsi_status; 12772 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12773 sizeof(io->scsiio.sense_data)); 12774 msg.scsi.sense_len = io->scsiio.sense_len; 12775 msg.scsi.sense_residual = io->scsiio.sense_residual; 12776 msg.scsi.fetd_status = io->io_hdr.port_status; 12777 msg.scsi.residual = io->scsiio.residual; 12778 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12779 12780 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12781 ctl_failover_io(io, /*have_lock*/ have_lock); 12782 return; 12783 } 12784 12785 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12786 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12787 /* XXX do something if this fails */ 12788 } 12789 12790 } 12791 12792 /* 12793 * The DMA to the remote side is done, now we need to tell the other side 12794 * we're done so it can continue with its data movement. 12795 */ 12796 static void 12797 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12798 { 12799 union ctl_io *io; 12800 12801 io = rq->context; 12802 12803 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12804 printf("%s: ISC DMA write failed with error %d", __func__, 12805 rq->ret); 12806 ctl_set_internal_failure(&io->scsiio, 12807 /*sks_valid*/ 1, 12808 /*retry_count*/ rq->ret); 12809 } 12810 12811 ctl_dt_req_free(rq); 12812 12813 /* 12814 * In this case, we had to malloc the memory locally. Free it. 12815 */ 12816 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12817 int i; 12818 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12819 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12820 } 12821 /* 12822 * The data is in local and remote memory, so now we need to send 12823 * status (good or back) back to the other side. 12824 */ 12825 ctl_send_datamove_done(io, /*have_lock*/ 0); 12826 } 12827 12828 /* 12829 * We've moved the data from the host/controller into local memory. Now we 12830 * need to push it over to the remote controller's memory. 12831 */ 12832 static int 12833 ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12834 { 12835 int retval; 12836 12837 retval = 0; 12838 12839 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12840 ctl_datamove_remote_write_cb); 12841 12842 return (retval); 12843 } 12844 12845 static void 12846 ctl_datamove_remote_write(union ctl_io *io) 12847 { 12848 int retval; 12849 void (*fe_datamove)(union ctl_io *io); 12850 12851 /* 12852 * - Get the data from the host/HBA into local memory. 12853 * - DMA memory from the local controller to the remote controller. 12854 * - Send status back to the remote controller. 12855 */ 12856 12857 retval = ctl_datamove_remote_sgl_setup(io); 12858 if (retval != 0) 12859 return; 12860 12861 /* Switch the pointer over so the FETD knows what to do */ 12862 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12863 12864 /* 12865 * Use a custom move done callback, since we need to send completion 12866 * back to the other controller, not to the backend on this side. 12867 */ 12868 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12869 12870 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12871 12872 fe_datamove(io); 12873 12874 return; 12875 12876 } 12877 12878 static int 12879 ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12880 { 12881 #if 0 12882 char str[256]; 12883 char path_str[64]; 12884 struct sbuf sb; 12885 #endif 12886 12887 /* 12888 * In this case, we had to malloc the memory locally. Free it. 12889 */ 12890 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12891 int i; 12892 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12893 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12894 } 12895 12896 #if 0 12897 scsi_path_string(io, path_str, sizeof(path_str)); 12898 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12899 sbuf_cat(&sb, path_str); 12900 scsi_command_string(&io->scsiio, NULL, &sb); 12901 sbuf_printf(&sb, "\n"); 12902 sbuf_cat(&sb, path_str); 12903 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12904 io->scsiio.tag_num, io->scsiio.tag_type); 12905 sbuf_cat(&sb, path_str); 12906 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12907 io->io_hdr.flags, io->io_hdr.status); 12908 sbuf_finish(&sb); 12909 printk("%s", sbuf_data(&sb)); 12910 #endif 12911 12912 12913 /* 12914 * The read is done, now we need to send status (good or bad) back 12915 * to the other side. 12916 */ 12917 ctl_send_datamove_done(io, /*have_lock*/ 0); 12918 12919 return (0); 12920 } 12921 12922 static void 12923 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12924 { 12925 union ctl_io *io; 12926 void (*fe_datamove)(union ctl_io *io); 12927 12928 io = rq->context; 12929 12930 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12931 printf("%s: ISC DMA read failed with error %d", __func__, 12932 rq->ret); 12933 ctl_set_internal_failure(&io->scsiio, 12934 /*sks_valid*/ 1, 12935 /*retry_count*/ rq->ret); 12936 } 12937 12938 ctl_dt_req_free(rq); 12939 12940 /* Switch the pointer over so the FETD knows what to do */ 12941 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12942 12943 /* 12944 * Use a custom move done callback, since we need to send completion 12945 * back to the other controller, not to the backend on this side. 12946 */ 12947 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12948 12949 /* XXX KDM add checks like the ones in ctl_datamove? */ 12950 12951 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12952 12953 fe_datamove(io); 12954 } 12955 12956 static int 12957 ctl_datamove_remote_sgl_setup(union ctl_io *io) 12958 { 12959 struct ctl_sg_entry *local_sglist, *remote_sglist; 12960 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12961 struct ctl_softc *softc; 12962 int retval; 12963 int i; 12964 12965 retval = 0; 12966 softc = control_softc; 12967 12968 local_sglist = io->io_hdr.local_sglist; 12969 local_dma_sglist = io->io_hdr.local_dma_sglist; 12970 remote_sglist = io->io_hdr.remote_sglist; 12971 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12972 12973 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12974 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12975 local_sglist[i].len = remote_sglist[i].len; 12976 12977 /* 12978 * XXX Detect the situation where the RS-level I/O 12979 * redirector on the other side has already read the 12980 * data off of the AOR RS on this side, and 12981 * transferred it to remote (mirror) memory on the 12982 * other side. Since we already have the data in 12983 * memory here, we just need to use it. 12984 * 12985 * XXX KDM this can probably be removed once we 12986 * get the cache device code in and take the 12987 * current AOR implementation out. 12988 */ 12989 #ifdef NEEDTOPORT 12990 if ((remote_sglist[i].addr >= 12991 (void *)vtophys(softc->mirr->addr)) 12992 && (remote_sglist[i].addr < 12993 ((void *)vtophys(softc->mirr->addr) + 12994 CacheMirrorOffset))) { 12995 local_sglist[i].addr = remote_sglist[i].addr - 12996 CacheMirrorOffset; 12997 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12998 CTL_FLAG_DATA_IN) 12999 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13000 } else { 13001 local_sglist[i].addr = remote_sglist[i].addr + 13002 CacheMirrorOffset; 13003 } 13004 #endif 13005 #if 0 13006 printf("%s: local %p, remote %p, len %d\n", 13007 __func__, local_sglist[i].addr, 13008 remote_sglist[i].addr, local_sglist[i].len); 13009 #endif 13010 } 13011 } else { 13012 uint32_t len_to_go; 13013 13014 /* 13015 * In this case, we don't have automatically allocated 13016 * memory for this I/O on this controller. This typically 13017 * happens with internal CTL I/O -- e.g. inquiry, mode 13018 * sense, etc. Anything coming from RAIDCore will have 13019 * a mirror area available. 13020 */ 13021 len_to_go = io->scsiio.kern_data_len; 13022 13023 /* 13024 * Clear the no datasync flag, we have to use malloced 13025 * buffers. 13026 */ 13027 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13028 13029 /* 13030 * The difficult thing here is that the size of the various 13031 * S/G segments may be different than the size from the 13032 * remote controller. That'll make it harder when DMAing 13033 * the data back to the other side. 13034 */ 13035 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13036 sizeof(io->io_hdr.remote_sglist[0])) && 13037 (len_to_go > 0); i++) { 13038 local_sglist[i].len = ctl_min(len_to_go, 131072); 13039 CTL_SIZE_8B(local_dma_sglist[i].len, 13040 local_sglist[i].len); 13041 local_sglist[i].addr = 13042 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13043 13044 local_dma_sglist[i].addr = local_sglist[i].addr; 13045 13046 if (local_sglist[i].addr == NULL) { 13047 int j; 13048 13049 printf("malloc failed for %zd bytes!", 13050 local_dma_sglist[i].len); 13051 for (j = 0; j < i; j++) { 13052 free(local_sglist[j].addr, M_CTL); 13053 } 13054 ctl_set_internal_failure(&io->scsiio, 13055 /*sks_valid*/ 1, 13056 /*retry_count*/ 4857); 13057 retval = 1; 13058 goto bailout_error; 13059 13060 } 13061 /* XXX KDM do we need a sync here? */ 13062 13063 len_to_go -= local_sglist[i].len; 13064 } 13065 /* 13066 * Reset the number of S/G entries accordingly. The 13067 * original number of S/G entries is available in 13068 * rem_sg_entries. 13069 */ 13070 io->scsiio.kern_sg_entries = i; 13071 13072 #if 0 13073 printf("%s: kern_sg_entries = %d\n", __func__, 13074 io->scsiio.kern_sg_entries); 13075 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13076 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13077 local_sglist[i].addr, local_sglist[i].len, 13078 local_dma_sglist[i].len); 13079 #endif 13080 } 13081 13082 13083 return (retval); 13084 13085 bailout_error: 13086 13087 ctl_send_datamove_done(io, /*have_lock*/ 0); 13088 13089 return (retval); 13090 } 13091 13092 static int 13093 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13094 ctl_ha_dt_cb callback) 13095 { 13096 struct ctl_ha_dt_req *rq; 13097 struct ctl_sg_entry *remote_sglist, *local_sglist; 13098 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13099 uint32_t local_used, remote_used, total_used; 13100 int retval; 13101 int i, j; 13102 13103 retval = 0; 13104 13105 rq = ctl_dt_req_alloc(); 13106 13107 /* 13108 * If we failed to allocate the request, and if the DMA didn't fail 13109 * anyway, set busy status. This is just a resource allocation 13110 * failure. 13111 */ 13112 if ((rq == NULL) 13113 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13114 ctl_set_busy(&io->scsiio); 13115 13116 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13117 13118 if (rq != NULL) 13119 ctl_dt_req_free(rq); 13120 13121 /* 13122 * The data move failed. We need to return status back 13123 * to the other controller. No point in trying to DMA 13124 * data to the remote controller. 13125 */ 13126 13127 ctl_send_datamove_done(io, /*have_lock*/ 0); 13128 13129 retval = 1; 13130 13131 goto bailout; 13132 } 13133 13134 local_sglist = io->io_hdr.local_sglist; 13135 local_dma_sglist = io->io_hdr.local_dma_sglist; 13136 remote_sglist = io->io_hdr.remote_sglist; 13137 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13138 local_used = 0; 13139 remote_used = 0; 13140 total_used = 0; 13141 13142 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13143 rq->ret = CTL_HA_STATUS_SUCCESS; 13144 rq->context = io; 13145 callback(rq); 13146 goto bailout; 13147 } 13148 13149 /* 13150 * Pull/push the data over the wire from/to the other controller. 13151 * This takes into account the possibility that the local and 13152 * remote sglists may not be identical in terms of the size of 13153 * the elements and the number of elements. 13154 * 13155 * One fundamental assumption here is that the length allocated for 13156 * both the local and remote sglists is identical. Otherwise, we've 13157 * essentially got a coding error of some sort. 13158 */ 13159 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13160 int isc_ret; 13161 uint32_t cur_len, dma_length; 13162 uint8_t *tmp_ptr; 13163 13164 rq->id = CTL_HA_DATA_CTL; 13165 rq->command = command; 13166 rq->context = io; 13167 13168 /* 13169 * Both pointers should be aligned. But it is possible 13170 * that the allocation length is not. They should both 13171 * also have enough slack left over at the end, though, 13172 * to round up to the next 8 byte boundary. 13173 */ 13174 cur_len = ctl_min(local_sglist[i].len - local_used, 13175 remote_sglist[j].len - remote_used); 13176 13177 /* 13178 * In this case, we have a size issue and need to decrease 13179 * the size, except in the case where we actually have less 13180 * than 8 bytes left. In that case, we need to increase 13181 * the DMA length to get the last bit. 13182 */ 13183 if ((cur_len & 0x7) != 0) { 13184 if (cur_len > 0x7) { 13185 cur_len = cur_len - (cur_len & 0x7); 13186 dma_length = cur_len; 13187 } else { 13188 CTL_SIZE_8B(dma_length, cur_len); 13189 } 13190 13191 } else 13192 dma_length = cur_len; 13193 13194 /* 13195 * If we had to allocate memory for this I/O, instead of using 13196 * the non-cached mirror memory, we'll need to flush the cache 13197 * before trying to DMA to the other controller. 13198 * 13199 * We could end up doing this multiple times for the same 13200 * segment if we have a larger local segment than remote 13201 * segment. That shouldn't be an issue. 13202 */ 13203 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13204 /* 13205 * XXX KDM use bus_dmamap_sync() here. 13206 */ 13207 } 13208 13209 rq->size = dma_length; 13210 13211 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13212 tmp_ptr += local_used; 13213 13214 /* Use physical addresses when talking to ISC hardware */ 13215 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13216 /* XXX KDM use busdma */ 13217 #if 0 13218 rq->local = vtophys(tmp_ptr); 13219 #endif 13220 } else 13221 rq->local = tmp_ptr; 13222 13223 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13224 tmp_ptr += remote_used; 13225 rq->remote = tmp_ptr; 13226 13227 rq->callback = NULL; 13228 13229 local_used += cur_len; 13230 if (local_used >= local_sglist[i].len) { 13231 i++; 13232 local_used = 0; 13233 } 13234 13235 remote_used += cur_len; 13236 if (remote_used >= remote_sglist[j].len) { 13237 j++; 13238 remote_used = 0; 13239 } 13240 total_used += cur_len; 13241 13242 if (total_used >= io->scsiio.kern_data_len) 13243 rq->callback = callback; 13244 13245 if ((rq->size & 0x7) != 0) { 13246 printf("%s: warning: size %d is not on 8b boundary\n", 13247 __func__, rq->size); 13248 } 13249 if (((uintptr_t)rq->local & 0x7) != 0) { 13250 printf("%s: warning: local %p not on 8b boundary\n", 13251 __func__, rq->local); 13252 } 13253 if (((uintptr_t)rq->remote & 0x7) != 0) { 13254 printf("%s: warning: remote %p not on 8b boundary\n", 13255 __func__, rq->local); 13256 } 13257 #if 0 13258 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13259 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13260 rq->local, rq->remote, rq->size); 13261 #endif 13262 13263 isc_ret = ctl_dt_single(rq); 13264 if (isc_ret == CTL_HA_STATUS_WAIT) 13265 continue; 13266 13267 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13268 rq->ret = CTL_HA_STATUS_SUCCESS; 13269 } else { 13270 rq->ret = isc_ret; 13271 } 13272 callback(rq); 13273 goto bailout; 13274 } 13275 13276 bailout: 13277 return (retval); 13278 13279 } 13280 13281 static void 13282 ctl_datamove_remote_read(union ctl_io *io) 13283 { 13284 int retval; 13285 int i; 13286 13287 /* 13288 * This will send an error to the other controller in the case of a 13289 * failure. 13290 */ 13291 retval = ctl_datamove_remote_sgl_setup(io); 13292 if (retval != 0) 13293 return; 13294 13295 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13296 ctl_datamove_remote_read_cb); 13297 if ((retval != 0) 13298 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13299 /* 13300 * Make sure we free memory if there was an error.. The 13301 * ctl_datamove_remote_xfer() function will send the 13302 * datamove done message, or call the callback with an 13303 * error if there is a problem. 13304 */ 13305 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13306 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13307 } 13308 13309 return; 13310 } 13311 13312 /* 13313 * Process a datamove request from the other controller. This is used for 13314 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13315 * first. Once that is complete, the data gets DMAed into the remote 13316 * controller's memory. For reads, we DMA from the remote controller's 13317 * memory into our memory first, and then move it out to the FETD. 13318 */ 13319 static void 13320 ctl_datamove_remote(union ctl_io *io) 13321 { 13322 struct ctl_softc *softc; 13323 13324 softc = control_softc; 13325 13326 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13327 13328 /* 13329 * Note that we look for an aborted I/O here, but don't do some of 13330 * the other checks that ctl_datamove() normally does. 13331 * We don't need to run the datamove delay code, since that should 13332 * have been done if need be on the other controller. 13333 */ 13334 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13335 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13336 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13337 io->io_hdr.nexus.targ_port, 13338 io->io_hdr.nexus.targ_target.id, 13339 io->io_hdr.nexus.targ_lun); 13340 io->io_hdr.port_status = 31338; 13341 ctl_send_datamove_done(io, /*have_lock*/ 0); 13342 return; 13343 } 13344 13345 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13346 ctl_datamove_remote_write(io); 13347 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13348 ctl_datamove_remote_read(io); 13349 } else { 13350 union ctl_ha_msg msg; 13351 struct scsi_sense_data *sense; 13352 uint8_t sks[3]; 13353 int retry_count; 13354 13355 memset(&msg, 0, sizeof(msg)); 13356 13357 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13358 msg.hdr.status = CTL_SCSI_ERROR; 13359 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13360 13361 retry_count = 4243; 13362 13363 sense = &msg.scsi.sense_data; 13364 sks[0] = SSD_SCS_VALID; 13365 sks[1] = (retry_count >> 8) & 0xff; 13366 sks[2] = retry_count & 0xff; 13367 13368 /* "Internal target failure" */ 13369 scsi_set_sense_data(sense, 13370 /*sense_format*/ SSD_TYPE_NONE, 13371 /*current_error*/ 1, 13372 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13373 /*asc*/ 0x44, 13374 /*ascq*/ 0x00, 13375 /*type*/ SSD_ELEM_SKS, 13376 /*size*/ sizeof(sks), 13377 /*data*/ sks, 13378 SSD_ELEM_NONE); 13379 13380 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13381 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13382 ctl_failover_io(io, /*have_lock*/ 1); 13383 return; 13384 } 13385 13386 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13387 CTL_HA_STATUS_SUCCESS) { 13388 /* XXX KDM what to do if this fails? */ 13389 } 13390 return; 13391 } 13392 13393 } 13394 13395 static int 13396 ctl_process_done(union ctl_io *io) 13397 { 13398 struct ctl_lun *lun; 13399 struct ctl_softc *ctl_softc = control_softc; 13400 void (*fe_done)(union ctl_io *io); 13401 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13402 13403 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13404 13405 fe_done = 13406 control_softc->ctl_ports[targ_port]->fe_done; 13407 13408 #ifdef CTL_TIME_IO 13409 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13410 char str[256]; 13411 char path_str[64]; 13412 struct sbuf sb; 13413 13414 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13415 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13416 13417 sbuf_cat(&sb, path_str); 13418 switch (io->io_hdr.io_type) { 13419 case CTL_IO_SCSI: 13420 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13421 sbuf_printf(&sb, "\n"); 13422 sbuf_cat(&sb, path_str); 13423 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13424 io->scsiio.tag_num, io->scsiio.tag_type); 13425 break; 13426 case CTL_IO_TASK: 13427 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13428 "Tag Type: %d\n", io->taskio.task_action, 13429 io->taskio.tag_num, io->taskio.tag_type); 13430 break; 13431 default: 13432 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13433 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13434 break; 13435 } 13436 sbuf_cat(&sb, path_str); 13437 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13438 (intmax_t)time_uptime - io->io_hdr.start_time); 13439 sbuf_finish(&sb); 13440 printf("%s", sbuf_data(&sb)); 13441 } 13442 #endif /* CTL_TIME_IO */ 13443 13444 switch (io->io_hdr.io_type) { 13445 case CTL_IO_SCSI: 13446 break; 13447 case CTL_IO_TASK: 13448 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13449 ctl_io_error_print(io, NULL); 13450 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13451 ctl_free_io(io); 13452 else 13453 fe_done(io); 13454 return (CTL_RETVAL_COMPLETE); 13455 default: 13456 panic("ctl_process_done: invalid io type %d\n", 13457 io->io_hdr.io_type); 13458 break; /* NOTREACHED */ 13459 } 13460 13461 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13462 if (lun == NULL) { 13463 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13464 io->io_hdr.nexus.targ_mapped_lun)); 13465 goto bailout; 13466 } 13467 13468 mtx_lock(&lun->lun_lock); 13469 13470 /* 13471 * Check to see if we have any errors to inject here. We only 13472 * inject errors for commands that don't already have errors set. 13473 */ 13474 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13475 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13476 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13477 ctl_inject_error(lun, io); 13478 13479 /* 13480 * XXX KDM how do we treat commands that aren't completed 13481 * successfully? 13482 * 13483 * XXX KDM should we also track I/O latency? 13484 */ 13485 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13486 io->io_hdr.io_type == CTL_IO_SCSI) { 13487 #ifdef CTL_TIME_IO 13488 struct bintime cur_bt; 13489 #endif 13490 int type; 13491 13492 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13493 CTL_FLAG_DATA_IN) 13494 type = CTL_STATS_READ; 13495 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13496 CTL_FLAG_DATA_OUT) 13497 type = CTL_STATS_WRITE; 13498 else 13499 type = CTL_STATS_NO_IO; 13500 13501 lun->stats.ports[targ_port].bytes[type] += 13502 io->scsiio.kern_total_len; 13503 lun->stats.ports[targ_port].operations[type]++; 13504 #ifdef CTL_TIME_IO 13505 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13506 &io->io_hdr.dma_bt); 13507 lun->stats.ports[targ_port].num_dmas[type] += 13508 io->io_hdr.num_dmas; 13509 getbintime(&cur_bt); 13510 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13511 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13512 #endif 13513 } 13514 13515 /* 13516 * Remove this from the OOA queue. 13517 */ 13518 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13519 13520 /* 13521 * Run through the blocked queue on this LUN and see if anything 13522 * has become unblocked, now that this transaction is done. 13523 */ 13524 ctl_check_blocked(lun); 13525 13526 /* 13527 * If the LUN has been invalidated, free it if there is nothing 13528 * left on its OOA queue. 13529 */ 13530 if ((lun->flags & CTL_LUN_INVALID) 13531 && TAILQ_EMPTY(&lun->ooa_queue)) { 13532 mtx_unlock(&lun->lun_lock); 13533 mtx_lock(&ctl_softc->ctl_lock); 13534 ctl_free_lun(lun); 13535 mtx_unlock(&ctl_softc->ctl_lock); 13536 } else 13537 mtx_unlock(&lun->lun_lock); 13538 13539 bailout: 13540 13541 /* 13542 * If this command has been aborted, make sure we set the status 13543 * properly. The FETD is responsible for freeing the I/O and doing 13544 * whatever it needs to do to clean up its state. 13545 */ 13546 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13547 ctl_set_task_aborted(&io->scsiio); 13548 13549 /* 13550 * If enabled, print command error status. 13551 * We don't print UAs unless debugging was enabled explicitly. 13552 */ 13553 do { 13554 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13555 break; 13556 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13557 break; 13558 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13559 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13560 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13561 int error_code, sense_key, asc, ascq; 13562 13563 scsi_extract_sense_len(&io->scsiio.sense_data, 13564 io->scsiio.sense_len, &error_code, &sense_key, 13565 &asc, &ascq, /*show_errors*/ 0); 13566 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13567 break; 13568 } 13569 13570 ctl_io_error_print(io, NULL); 13571 } while (0); 13572 13573 /* 13574 * Tell the FETD or the other shelf controller we're done with this 13575 * command. Note that only SCSI commands get to this point. Task 13576 * management commands are completed above. 13577 * 13578 * We only send status to the other controller if we're in XFER 13579 * mode. In SER_ONLY mode, the I/O is done on the controller that 13580 * received the I/O (from CTL's perspective), and so the status is 13581 * generated there. 13582 * 13583 * XXX KDM if we hold the lock here, we could cause a deadlock 13584 * if the frontend comes back in in this context to queue 13585 * something. 13586 */ 13587 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13588 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13589 union ctl_ha_msg msg; 13590 13591 memset(&msg, 0, sizeof(msg)); 13592 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13593 msg.hdr.original_sc = io->io_hdr.original_sc; 13594 msg.hdr.nexus = io->io_hdr.nexus; 13595 msg.hdr.status = io->io_hdr.status; 13596 msg.scsi.scsi_status = io->scsiio.scsi_status; 13597 msg.scsi.tag_num = io->scsiio.tag_num; 13598 msg.scsi.tag_type = io->scsiio.tag_type; 13599 msg.scsi.sense_len = io->scsiio.sense_len; 13600 msg.scsi.sense_residual = io->scsiio.sense_residual; 13601 msg.scsi.residual = io->scsiio.residual; 13602 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13603 sizeof(io->scsiio.sense_data)); 13604 /* 13605 * We copy this whether or not this is an I/O-related 13606 * command. Otherwise, we'd have to go and check to see 13607 * whether it's a read/write command, and it really isn't 13608 * worth it. 13609 */ 13610 memcpy(&msg.scsi.lbalen, 13611 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13612 sizeof(msg.scsi.lbalen)); 13613 13614 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13615 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13616 /* XXX do something here */ 13617 } 13618 13619 ctl_free_io(io); 13620 } else 13621 fe_done(io); 13622 13623 return (CTL_RETVAL_COMPLETE); 13624 } 13625 13626 #ifdef CTL_WITH_CA 13627 /* 13628 * Front end should call this if it doesn't do autosense. When the request 13629 * sense comes back in from the initiator, we'll dequeue this and send it. 13630 */ 13631 int 13632 ctl_queue_sense(union ctl_io *io) 13633 { 13634 struct ctl_lun *lun; 13635 struct ctl_softc *ctl_softc; 13636 uint32_t initidx, targ_lun; 13637 13638 ctl_softc = control_softc; 13639 13640 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13641 13642 /* 13643 * LUN lookup will likely move to the ctl_work_thread() once we 13644 * have our new queueing infrastructure (that doesn't put things on 13645 * a per-LUN queue initially). That is so that we can handle 13646 * things like an INQUIRY to a LUN that we don't have enabled. We 13647 * can't deal with that right now. 13648 */ 13649 mtx_lock(&ctl_softc->ctl_lock); 13650 13651 /* 13652 * If we don't have a LUN for this, just toss the sense 13653 * information. 13654 */ 13655 targ_lun = io->io_hdr.nexus.targ_lun; 13656 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13657 if ((targ_lun < CTL_MAX_LUNS) 13658 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13659 lun = ctl_softc->ctl_luns[targ_lun]; 13660 else 13661 goto bailout; 13662 13663 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13664 13665 mtx_lock(&lun->lun_lock); 13666 /* 13667 * Already have CA set for this LUN...toss the sense information. 13668 */ 13669 if (ctl_is_set(lun->have_ca, initidx)) { 13670 mtx_unlock(&lun->lun_lock); 13671 goto bailout; 13672 } 13673 13674 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13675 ctl_min(sizeof(lun->pending_sense[initidx]), 13676 sizeof(io->scsiio.sense_data))); 13677 ctl_set_mask(lun->have_ca, initidx); 13678 mtx_unlock(&lun->lun_lock); 13679 13680 bailout: 13681 mtx_unlock(&ctl_softc->ctl_lock); 13682 13683 ctl_free_io(io); 13684 13685 return (CTL_RETVAL_COMPLETE); 13686 } 13687 #endif 13688 13689 /* 13690 * Primary command inlet from frontend ports. All SCSI and task I/O 13691 * requests must go through this function. 13692 */ 13693 int 13694 ctl_queue(union ctl_io *io) 13695 { 13696 struct ctl_softc *ctl_softc; 13697 13698 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13699 13700 ctl_softc = control_softc; 13701 13702 #ifdef CTL_TIME_IO 13703 io->io_hdr.start_time = time_uptime; 13704 getbintime(&io->io_hdr.start_bt); 13705 #endif /* CTL_TIME_IO */ 13706 13707 /* Map FE-specific LUN ID into global one. */ 13708 io->io_hdr.nexus.targ_mapped_lun = 13709 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13710 13711 switch (io->io_hdr.io_type) { 13712 case CTL_IO_SCSI: 13713 case CTL_IO_TASK: 13714 if (ctl_debug & CTL_DEBUG_CDB) 13715 ctl_io_print(io); 13716 ctl_enqueue_incoming(io); 13717 break; 13718 default: 13719 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13720 return (EINVAL); 13721 } 13722 13723 return (CTL_RETVAL_COMPLETE); 13724 } 13725 13726 #ifdef CTL_IO_DELAY 13727 static void 13728 ctl_done_timer_wakeup(void *arg) 13729 { 13730 union ctl_io *io; 13731 13732 io = (union ctl_io *)arg; 13733 ctl_done(io); 13734 } 13735 #endif /* CTL_IO_DELAY */ 13736 13737 void 13738 ctl_done(union ctl_io *io) 13739 { 13740 struct ctl_softc *ctl_softc; 13741 13742 ctl_softc = control_softc; 13743 13744 /* 13745 * Enable this to catch duplicate completion issues. 13746 */ 13747 #if 0 13748 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13749 printf("%s: type %d msg %d cdb %x iptl: " 13750 "%d:%d:%d:%d tag 0x%04x " 13751 "flag %#x status %x\n", 13752 __func__, 13753 io->io_hdr.io_type, 13754 io->io_hdr.msg_type, 13755 io->scsiio.cdb[0], 13756 io->io_hdr.nexus.initid.id, 13757 io->io_hdr.nexus.targ_port, 13758 io->io_hdr.nexus.targ_target.id, 13759 io->io_hdr.nexus.targ_lun, 13760 (io->io_hdr.io_type == 13761 CTL_IO_TASK) ? 13762 io->taskio.tag_num : 13763 io->scsiio.tag_num, 13764 io->io_hdr.flags, 13765 io->io_hdr.status); 13766 } else 13767 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13768 #endif 13769 13770 /* 13771 * This is an internal copy of an I/O, and should not go through 13772 * the normal done processing logic. 13773 */ 13774 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13775 return; 13776 13777 /* 13778 * We need to send a msg to the serializing shelf to finish the IO 13779 * as well. We don't send a finish message to the other shelf if 13780 * this is a task management command. Task management commands 13781 * aren't serialized in the OOA queue, but rather just executed on 13782 * both shelf controllers for commands that originated on that 13783 * controller. 13784 */ 13785 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13786 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13787 union ctl_ha_msg msg_io; 13788 13789 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13790 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13791 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13792 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13793 } 13794 /* continue on to finish IO */ 13795 } 13796 #ifdef CTL_IO_DELAY 13797 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13798 struct ctl_lun *lun; 13799 13800 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13801 13802 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13803 } else { 13804 struct ctl_lun *lun; 13805 13806 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13807 13808 if ((lun != NULL) 13809 && (lun->delay_info.done_delay > 0)) { 13810 struct callout *callout; 13811 13812 callout = (struct callout *)&io->io_hdr.timer_bytes; 13813 callout_init(callout, /*mpsafe*/ 1); 13814 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13815 callout_reset(callout, 13816 lun->delay_info.done_delay * hz, 13817 ctl_done_timer_wakeup, io); 13818 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13819 lun->delay_info.done_delay = 0; 13820 return; 13821 } 13822 } 13823 #endif /* CTL_IO_DELAY */ 13824 13825 ctl_enqueue_done(io); 13826 } 13827 13828 int 13829 ctl_isc(struct ctl_scsiio *ctsio) 13830 { 13831 struct ctl_lun *lun; 13832 int retval; 13833 13834 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13835 13836 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13837 13838 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13839 13840 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13841 13842 return (retval); 13843 } 13844 13845 13846 static void 13847 ctl_work_thread(void *arg) 13848 { 13849 struct ctl_thread *thr = (struct ctl_thread *)arg; 13850 struct ctl_softc *softc = thr->ctl_softc; 13851 union ctl_io *io; 13852 int retval; 13853 13854 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13855 13856 for (;;) { 13857 retval = 0; 13858 13859 /* 13860 * We handle the queues in this order: 13861 * - ISC 13862 * - done queue (to free up resources, unblock other commands) 13863 * - RtR queue 13864 * - incoming queue 13865 * 13866 * If those queues are empty, we break out of the loop and 13867 * go to sleep. 13868 */ 13869 mtx_lock(&thr->queue_lock); 13870 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13871 if (io != NULL) { 13872 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13873 mtx_unlock(&thr->queue_lock); 13874 ctl_handle_isc(io); 13875 continue; 13876 } 13877 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13878 if (io != NULL) { 13879 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13880 /* clear any blocked commands, call fe_done */ 13881 mtx_unlock(&thr->queue_lock); 13882 retval = ctl_process_done(io); 13883 continue; 13884 } 13885 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13886 if (io != NULL) { 13887 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13888 mtx_unlock(&thr->queue_lock); 13889 if (io->io_hdr.io_type == CTL_IO_TASK) 13890 ctl_run_task(io); 13891 else 13892 ctl_scsiio_precheck(softc, &io->scsiio); 13893 continue; 13894 } 13895 if (!ctl_pause_rtr) { 13896 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13897 if (io != NULL) { 13898 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13899 mtx_unlock(&thr->queue_lock); 13900 retval = ctl_scsiio(&io->scsiio); 13901 if (retval != CTL_RETVAL_COMPLETE) 13902 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13903 continue; 13904 } 13905 } 13906 13907 /* Sleep until we have something to do. */ 13908 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13909 } 13910 } 13911 13912 static void 13913 ctl_lun_thread(void *arg) 13914 { 13915 struct ctl_softc *softc = (struct ctl_softc *)arg; 13916 struct ctl_be_lun *be_lun; 13917 int retval; 13918 13919 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13920 13921 for (;;) { 13922 retval = 0; 13923 mtx_lock(&softc->ctl_lock); 13924 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13925 if (be_lun != NULL) { 13926 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13927 mtx_unlock(&softc->ctl_lock); 13928 ctl_create_lun(be_lun); 13929 continue; 13930 } 13931 13932 /* Sleep until we have something to do. */ 13933 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13934 PDROP | PRIBIO, "-", 0); 13935 } 13936 } 13937 13938 static void 13939 ctl_thresh_thread(void *arg) 13940 { 13941 struct ctl_softc *softc = (struct ctl_softc *)arg; 13942 struct ctl_lun *lun; 13943 struct ctl_be_lun *be_lun; 13944 struct scsi_da_rw_recovery_page *rwpage; 13945 struct ctl_logical_block_provisioning_page *page; 13946 const char *attr; 13947 uint64_t thres, val; 13948 int i, e; 13949 13950 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13951 13952 for (;;) { 13953 mtx_lock(&softc->ctl_lock); 13954 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13955 be_lun = lun->be_lun; 13956 if ((lun->flags & CTL_LUN_DISABLED) || 13957 (lun->flags & CTL_LUN_OFFLINE) || 13958 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 13959 lun->backend->lun_attr == NULL) 13960 continue; 13961 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13962 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13963 continue; 13964 e = 0; 13965 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13966 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13967 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13968 continue; 13969 thres = scsi_4btoul(page->descr[i].count); 13970 thres <<= CTL_LBP_EXPONENT; 13971 switch (page->descr[i].resource) { 13972 case 0x01: 13973 attr = "blocksavail"; 13974 break; 13975 case 0x02: 13976 attr = "blocksused"; 13977 break; 13978 case 0xf1: 13979 attr = "poolblocksavail"; 13980 break; 13981 case 0xf2: 13982 attr = "poolblocksused"; 13983 break; 13984 default: 13985 continue; 13986 } 13987 mtx_unlock(&softc->ctl_lock); // XXX 13988 val = lun->backend->lun_attr( 13989 lun->be_lun->be_lun, attr); 13990 mtx_lock(&softc->ctl_lock); 13991 if (val == UINT64_MAX) 13992 continue; 13993 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13994 == SLBPPD_ARMING_INC) 13995 e |= (val >= thres); 13996 else 13997 e |= (val <= thres); 13998 } 13999 mtx_lock(&lun->lun_lock); 14000 if (e) { 14001 if (lun->lasttpt == 0 || 14002 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 14003 lun->lasttpt = time_uptime; 14004 for (i = 0; i < CTL_MAX_INITIATORS; i++) 14005 lun->pending_ua[i] |= 14006 CTL_UA_THIN_PROV_THRES; 14007 } 14008 } else { 14009 lun->lasttpt = 0; 14010 for (i = 0; i < CTL_MAX_INITIATORS; i++) 14011 lun->pending_ua[i] &= ~CTL_UA_THIN_PROV_THRES; 14012 } 14013 mtx_unlock(&lun->lun_lock); 14014 } 14015 mtx_unlock(&softc->ctl_lock); 14016 pause("-", CTL_LBP_PERIOD * hz); 14017 } 14018 } 14019 14020 static void 14021 ctl_enqueue_incoming(union ctl_io *io) 14022 { 14023 struct ctl_softc *softc = control_softc; 14024 struct ctl_thread *thr; 14025 u_int idx; 14026 14027 idx = (io->io_hdr.nexus.targ_port * 127 + 14028 io->io_hdr.nexus.initid.id) % worker_threads; 14029 thr = &softc->threads[idx]; 14030 mtx_lock(&thr->queue_lock); 14031 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14032 mtx_unlock(&thr->queue_lock); 14033 wakeup(thr); 14034 } 14035 14036 static void 14037 ctl_enqueue_rtr(union ctl_io *io) 14038 { 14039 struct ctl_softc *softc = control_softc; 14040 struct ctl_thread *thr; 14041 14042 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14043 mtx_lock(&thr->queue_lock); 14044 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14045 mtx_unlock(&thr->queue_lock); 14046 wakeup(thr); 14047 } 14048 14049 static void 14050 ctl_enqueue_done(union ctl_io *io) 14051 { 14052 struct ctl_softc *softc = control_softc; 14053 struct ctl_thread *thr; 14054 14055 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14056 mtx_lock(&thr->queue_lock); 14057 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14058 mtx_unlock(&thr->queue_lock); 14059 wakeup(thr); 14060 } 14061 14062 static void 14063 ctl_enqueue_isc(union ctl_io *io) 14064 { 14065 struct ctl_softc *softc = control_softc; 14066 struct ctl_thread *thr; 14067 14068 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14069 mtx_lock(&thr->queue_lock); 14070 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14071 mtx_unlock(&thr->queue_lock); 14072 wakeup(thr); 14073 } 14074 14075 /* Initialization and failover */ 14076 14077 void 14078 ctl_init_isc_msg(void) 14079 { 14080 printf("CTL: Still calling this thing\n"); 14081 } 14082 14083 /* 14084 * Init component 14085 * Initializes component into configuration defined by bootMode 14086 * (see hasc-sv.c) 14087 * returns hasc_Status: 14088 * OK 14089 * ERROR - fatal error 14090 */ 14091 static ctl_ha_comp_status 14092 ctl_isc_init(struct ctl_ha_component *c) 14093 { 14094 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14095 14096 c->status = ret; 14097 return ret; 14098 } 14099 14100 /* Start component 14101 * Starts component in state requested. If component starts successfully, 14102 * it must set its own state to the requestrd state 14103 * When requested state is HASC_STATE_HA, the component may refine it 14104 * by adding _SLAVE or _MASTER flags. 14105 * Currently allowed state transitions are: 14106 * UNKNOWN->HA - initial startup 14107 * UNKNOWN->SINGLE - initial startup when no parter detected 14108 * HA->SINGLE - failover 14109 * returns ctl_ha_comp_status: 14110 * OK - component successfully started in requested state 14111 * FAILED - could not start the requested state, failover may 14112 * be possible 14113 * ERROR - fatal error detected, no future startup possible 14114 */ 14115 static ctl_ha_comp_status 14116 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14117 { 14118 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14119 14120 printf("%s: go\n", __func__); 14121 14122 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14123 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14124 control_softc->is_single = 0; 14125 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14126 != CTL_HA_STATUS_SUCCESS) { 14127 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14128 ret = CTL_HA_COMP_STATUS_ERROR; 14129 } 14130 } else if (CTL_HA_STATE_IS_HA(c->state) 14131 && CTL_HA_STATE_IS_SINGLE(state)){ 14132 // HA->SINGLE transition 14133 ctl_failover(); 14134 control_softc->is_single = 1; 14135 } else { 14136 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14137 c->state, state); 14138 ret = CTL_HA_COMP_STATUS_ERROR; 14139 } 14140 if (CTL_HA_STATE_IS_SINGLE(state)) 14141 control_softc->is_single = 1; 14142 14143 c->state = state; 14144 c->status = ret; 14145 return ret; 14146 } 14147 14148 /* 14149 * Quiesce component 14150 * The component must clear any error conditions (set status to OK) and 14151 * prepare itself to another Start call 14152 * returns ctl_ha_comp_status: 14153 * OK 14154 * ERROR 14155 */ 14156 static ctl_ha_comp_status 14157 ctl_isc_quiesce(struct ctl_ha_component *c) 14158 { 14159 int ret = CTL_HA_COMP_STATUS_OK; 14160 14161 ctl_pause_rtr = 1; 14162 c->status = ret; 14163 return ret; 14164 } 14165 14166 struct ctl_ha_component ctl_ha_component_ctlisc = 14167 { 14168 .name = "CTL ISC", 14169 .state = CTL_HA_STATE_UNKNOWN, 14170 .init = ctl_isc_init, 14171 .start = ctl_isc_start, 14172 .quiesce = ctl_isc_quiesce 14173 }; 14174 14175 /* 14176 * vim: ts=8 14177 */ 14178