1 /*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id$ 35 */ 36 /* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42 #define _CTL_C 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 uint8_t ctl_pause_rtr; 360 361 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 362 static int worker_threads = -1; 363 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 364 &worker_threads, 1, "Number of worker threads"); 365 static int ctl_debug = CTL_DEBUG_NONE; 366 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 367 &ctl_debug, 0, "Enabled debug flags"); 368 369 /* 370 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 371 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 372 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 373 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 374 */ 375 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 376 377 #ifdef notyet 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 #endif 382 static int ctl_init(void); 383 void ctl_shutdown(void); 384 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 385 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 386 static void ctl_ioctl_online(void *arg); 387 static void ctl_ioctl_offline(void *arg); 388 static int ctl_ioctl_lun_enable(void *arg, int lun_id); 389 static int ctl_ioctl_lun_disable(void *arg, int lun_id); 390 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 391 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 392 static int ctl_ioctl_submit_wait(union ctl_io *io); 393 static void ctl_ioctl_datamove(union ctl_io *io); 394 static void ctl_ioctl_done(union ctl_io *io); 395 static void ctl_ioctl_hard_startstop_callback(void *arg, 396 struct cfi_metatask *metatask); 397 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 398 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 399 struct ctl_ooa *ooa_hdr, 400 struct ctl_ooa_entry *kern_entries); 401 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 402 struct thread *td); 403 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 404 struct ctl_be_lun *be_lun); 405 static int ctl_free_lun(struct ctl_lun *lun); 406 static void ctl_create_lun(struct ctl_be_lun *be_lun); 407 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 408 /** 409 static void ctl_failover_change_pages(struct ctl_softc *softc, 410 struct ctl_scsiio *ctsio, int master); 411 **/ 412 413 static int ctl_do_mode_select(union ctl_io *io); 414 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 415 uint64_t res_key, uint64_t sa_res_key, 416 uint8_t type, uint32_t residx, 417 struct ctl_scsiio *ctsio, 418 struct scsi_per_res_out *cdb, 419 struct scsi_per_res_out_parms* param); 420 static void ctl_pro_preempt_other(struct ctl_lun *lun, 421 union ctl_ha_msg *msg); 422 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 423 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 424 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 425 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 426 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 427 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 428 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 429 int alloc_len); 430 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 431 int alloc_len); 432 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 433 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 434 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 435 static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 436 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 437 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 438 bool seq); 439 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 440 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 441 union ctl_io *pending_io, union ctl_io *ooa_io); 442 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 443 union ctl_io *starting_io); 444 static int ctl_check_blocked(struct ctl_lun *lun); 445 static int ctl_scsiio_lun_check(struct ctl_lun *lun, 446 const struct ctl_cmd_entry *entry, 447 struct ctl_scsiio *ctsio); 448 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 449 #ifdef notyet 450 static void ctl_failover(void); 451 #endif 452 static void ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx, 453 ctl_ua_type ua_type); 454 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 455 struct ctl_scsiio *ctsio); 456 static int ctl_scsiio(struct ctl_scsiio *ctsio); 457 458 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 459 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 460 ctl_ua_type ua_type); 461 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 462 ctl_ua_type ua_type); 463 static int ctl_abort_task(union ctl_io *io); 464 static int ctl_abort_task_set(union ctl_io *io); 465 static int ctl_i_t_nexus_reset(union ctl_io *io); 466 static void ctl_run_task(union ctl_io *io); 467 #ifdef CTL_IO_DELAY 468 static void ctl_datamove_timer_wakeup(void *arg); 469 static void ctl_done_timer_wakeup(void *arg); 470 #endif /* CTL_IO_DELAY */ 471 472 static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 473 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 474 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 475 static void ctl_datamove_remote_write(union ctl_io *io); 476 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 477 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 478 static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 479 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 480 ctl_ha_dt_cb callback); 481 static void ctl_datamove_remote_read(union ctl_io *io); 482 static void ctl_datamove_remote(union ctl_io *io); 483 static int ctl_process_done(union ctl_io *io); 484 static void ctl_lun_thread(void *arg); 485 static void ctl_thresh_thread(void *arg); 486 static void ctl_work_thread(void *arg); 487 static void ctl_enqueue_incoming(union ctl_io *io); 488 static void ctl_enqueue_rtr(union ctl_io *io); 489 static void ctl_enqueue_done(union ctl_io *io); 490 #ifdef notyet 491 static void ctl_enqueue_isc(union ctl_io *io); 492 #endif 493 static const struct ctl_cmd_entry * 494 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 495 static const struct ctl_cmd_entry * 496 ctl_validate_command(struct ctl_scsiio *ctsio); 497 static int ctl_cmd_applicable(uint8_t lun_type, 498 const struct ctl_cmd_entry *entry); 499 500 /* 501 * Load the serialization table. This isn't very pretty, but is probably 502 * the easiest way to do it. 503 */ 504 #include "ctl_ser_table.c" 505 506 /* 507 * We only need to define open, close and ioctl routines for this driver. 508 */ 509 static struct cdevsw ctl_cdevsw = { 510 .d_version = D_VERSION, 511 .d_flags = 0, 512 .d_open = ctl_open, 513 .d_close = ctl_close, 514 .d_ioctl = ctl_ioctl, 515 .d_name = "ctl", 516 }; 517 518 519 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 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 #ifdef notyet 538 static void 539 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 540 union ctl_ha_msg *msg_info) 541 { 542 struct ctl_scsiio *ctsio; 543 544 if (msg_info->hdr.original_sc == NULL) { 545 printf("%s: original_sc == NULL!\n", __func__); 546 /* XXX KDM now what? */ 547 return; 548 } 549 550 ctsio = &msg_info->hdr.original_sc->scsiio; 551 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 552 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 553 ctsio->io_hdr.status = msg_info->hdr.status; 554 ctsio->scsi_status = msg_info->scsi.scsi_status; 555 ctsio->sense_len = msg_info->scsi.sense_len; 556 ctsio->sense_residual = msg_info->scsi.sense_residual; 557 ctsio->residual = msg_info->scsi.residual; 558 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 559 sizeof(ctsio->sense_data)); 560 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 561 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 562 ctl_enqueue_isc((union ctl_io *)ctsio); 563 } 564 565 static void 566 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 567 union ctl_ha_msg *msg_info) 568 { 569 struct ctl_scsiio *ctsio; 570 571 if (msg_info->hdr.serializing_sc == NULL) { 572 printf("%s: serializing_sc == NULL!\n", __func__); 573 /* XXX KDM now what? */ 574 return; 575 } 576 577 ctsio = &msg_info->hdr.serializing_sc->scsiio; 578 #if 0 579 /* 580 * Attempt to catch the situation where an I/O has 581 * been freed, and we're using it again. 582 */ 583 if (ctsio->io_hdr.io_type == 0xff) { 584 union ctl_io *tmp_io; 585 tmp_io = (union ctl_io *)ctsio; 586 printf("%s: %p use after free!\n", __func__, 587 ctsio); 588 printf("%s: type %d msg %d cdb %x iptl: " 589 "%d:%d:%d:%d tag 0x%04x " 590 "flag %#x status %x\n", 591 __func__, 592 tmp_io->io_hdr.io_type, 593 tmp_io->io_hdr.msg_type, 594 tmp_io->scsiio.cdb[0], 595 tmp_io->io_hdr.nexus.initid.id, 596 tmp_io->io_hdr.nexus.targ_port, 597 tmp_io->io_hdr.nexus.targ_target.id, 598 tmp_io->io_hdr.nexus.targ_lun, 599 (tmp_io->io_hdr.io_type == 600 CTL_IO_TASK) ? 601 tmp_io->taskio.tag_num : 602 tmp_io->scsiio.tag_num, 603 tmp_io->io_hdr.flags, 604 tmp_io->io_hdr.status); 605 } 606 #endif 607 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 608 ctl_enqueue_isc((union ctl_io *)ctsio); 609 } 610 611 /* 612 * ISC (Inter Shelf Communication) event handler. Events from the HA 613 * subsystem come in here. 614 */ 615 static void 616 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 617 { 618 struct ctl_softc *softc; 619 union ctl_io *io; 620 struct ctl_prio *presio; 621 ctl_ha_status isc_status; 622 623 softc = control_softc; 624 io = NULL; 625 626 627 #if 0 628 printf("CTL: Isc Msg event %d\n", event); 629 #endif 630 if (event == CTL_HA_EVT_MSG_RECV) { 631 union ctl_ha_msg msg_info; 632 633 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 634 sizeof(msg_info), /*wait*/ 0); 635 #if 0 636 printf("CTL: msg_type %d\n", msg_info.msg_type); 637 #endif 638 if (isc_status != 0) { 639 printf("Error receiving message, status = %d\n", 640 isc_status); 641 return; 642 } 643 644 switch (msg_info.hdr.msg_type) { 645 case CTL_MSG_SERIALIZE: 646 #if 0 647 printf("Serialize\n"); 648 #endif 649 io = ctl_alloc_io_nowait(softc->othersc_pool); 650 if (io == NULL) { 651 printf("ctl_isc_event_handler: can't allocate " 652 "ctl_io!\n"); 653 /* Bad Juju */ 654 /* Need to set busy and send msg back */ 655 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 656 msg_info.hdr.status = CTL_SCSI_ERROR; 657 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 658 msg_info.scsi.sense_len = 0; 659 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 660 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 661 } 662 goto bailout; 663 } 664 ctl_zero_io(io); 665 // populate ctsio from msg_info 666 io->io_hdr.io_type = CTL_IO_SCSI; 667 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 668 io->io_hdr.original_sc = msg_info.hdr.original_sc; 669 #if 0 670 printf("pOrig %x\n", (int)msg_info.original_sc); 671 #endif 672 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 673 CTL_FLAG_IO_ACTIVE; 674 /* 675 * If we're in serialization-only mode, we don't 676 * want to go through full done processing. Thus 677 * the COPY flag. 678 * 679 * XXX KDM add another flag that is more specific. 680 */ 681 if (softc->ha_mode == CTL_HA_MODE_SER_ONLY) 682 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 683 io->io_hdr.nexus = msg_info.hdr.nexus; 684 #if 0 685 printf("targ %d, port %d, iid %d, lun %d\n", 686 io->io_hdr.nexus.targ_target.id, 687 io->io_hdr.nexus.targ_port, 688 io->io_hdr.nexus.initid.id, 689 io->io_hdr.nexus.targ_lun); 690 #endif 691 io->scsiio.tag_num = msg_info.scsi.tag_num; 692 io->scsiio.tag_type = msg_info.scsi.tag_type; 693 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 694 CTL_MAX_CDBLEN); 695 if (softc->ha_mode == CTL_HA_MODE_XFER) { 696 const struct ctl_cmd_entry *entry; 697 698 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 699 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 700 io->io_hdr.flags |= 701 entry->flags & CTL_FLAG_DATA_MASK; 702 } 703 ctl_enqueue_isc(io); 704 break; 705 706 /* Performed on the Originating SC, XFER mode only */ 707 case CTL_MSG_DATAMOVE: { 708 struct ctl_sg_entry *sgl; 709 int i, j; 710 711 io = msg_info.hdr.original_sc; 712 if (io == NULL) { 713 printf("%s: original_sc == NULL!\n", __func__); 714 /* XXX KDM do something here */ 715 break; 716 } 717 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 718 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 719 /* 720 * Keep track of this, we need to send it back over 721 * when the datamove is complete. 722 */ 723 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 724 725 if (msg_info.dt.sg_sequence == 0) { 726 /* 727 * XXX KDM we use the preallocated S/G list 728 * here, but we'll need to change this to 729 * dynamic allocation if we need larger S/G 730 * lists. 731 */ 732 if (msg_info.dt.kern_sg_entries > 733 sizeof(io->io_hdr.remote_sglist) / 734 sizeof(io->io_hdr.remote_sglist[0])) { 735 printf("%s: number of S/G entries " 736 "needed %u > allocated num %zd\n", 737 __func__, 738 msg_info.dt.kern_sg_entries, 739 sizeof(io->io_hdr.remote_sglist)/ 740 sizeof(io->io_hdr.remote_sglist[0])); 741 742 /* 743 * XXX KDM send a message back to 744 * the other side to shut down the 745 * DMA. The error will come back 746 * through via the normal channel. 747 */ 748 break; 749 } 750 sgl = io->io_hdr.remote_sglist; 751 memset(sgl, 0, 752 sizeof(io->io_hdr.remote_sglist)); 753 754 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 755 756 io->scsiio.kern_sg_entries = 757 msg_info.dt.kern_sg_entries; 758 io->scsiio.rem_sg_entries = 759 msg_info.dt.kern_sg_entries; 760 io->scsiio.kern_data_len = 761 msg_info.dt.kern_data_len; 762 io->scsiio.kern_total_len = 763 msg_info.dt.kern_total_len; 764 io->scsiio.kern_data_resid = 765 msg_info.dt.kern_data_resid; 766 io->scsiio.kern_rel_offset = 767 msg_info.dt.kern_rel_offset; 768 /* 769 * Clear out per-DMA flags. 770 */ 771 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 772 /* 773 * Add per-DMA flags that are set for this 774 * particular DMA request. 775 */ 776 io->io_hdr.flags |= msg_info.dt.flags & 777 CTL_FLAG_RDMA_MASK; 778 } else 779 sgl = (struct ctl_sg_entry *) 780 io->scsiio.kern_data_ptr; 781 782 for (i = msg_info.dt.sent_sg_entries, j = 0; 783 i < (msg_info.dt.sent_sg_entries + 784 msg_info.dt.cur_sg_entries); i++, j++) { 785 sgl[i].addr = msg_info.dt.sg_list[j].addr; 786 sgl[i].len = msg_info.dt.sg_list[j].len; 787 788 #if 0 789 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 790 __func__, 791 msg_info.dt.sg_list[j].addr, 792 msg_info.dt.sg_list[j].len, 793 sgl[i].addr, sgl[i].len, j, i); 794 #endif 795 } 796 #if 0 797 memcpy(&sgl[msg_info.dt.sent_sg_entries], 798 msg_info.dt.sg_list, 799 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 800 #endif 801 802 /* 803 * If this is the last piece of the I/O, we've got 804 * the full S/G list. Queue processing in the thread. 805 * Otherwise wait for the next piece. 806 */ 807 if (msg_info.dt.sg_last != 0) 808 ctl_enqueue_isc(io); 809 break; 810 } 811 /* Performed on the Serializing (primary) SC, XFER mode only */ 812 case CTL_MSG_DATAMOVE_DONE: { 813 if (msg_info.hdr.serializing_sc == NULL) { 814 printf("%s: serializing_sc == NULL!\n", 815 __func__); 816 /* XXX KDM now what? */ 817 break; 818 } 819 /* 820 * We grab the sense information here in case 821 * there was a failure, so we can return status 822 * back to the initiator. 823 */ 824 io = msg_info.hdr.serializing_sc; 825 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 826 io->io_hdr.status = msg_info.hdr.status; 827 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 828 io->scsiio.sense_len = msg_info.scsi.sense_len; 829 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 830 io->io_hdr.port_status = msg_info.scsi.fetd_status; 831 io->scsiio.residual = msg_info.scsi.residual; 832 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 833 sizeof(io->scsiio.sense_data)); 834 ctl_enqueue_isc(io); 835 break; 836 } 837 838 /* Preformed on Originating SC, SER_ONLY mode */ 839 case CTL_MSG_R2R: 840 io = msg_info.hdr.original_sc; 841 if (io == NULL) { 842 printf("%s: Major Bummer\n", __func__); 843 return; 844 } else { 845 #if 0 846 printf("pOrig %x\n",(int) ctsio); 847 #endif 848 } 849 io->io_hdr.msg_type = CTL_MSG_R2R; 850 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 851 ctl_enqueue_isc(io); 852 break; 853 854 /* 855 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 856 * mode. 857 * Performed on the Originating (i.e. secondary) SC in XFER 858 * mode 859 */ 860 case CTL_MSG_FINISH_IO: 861 if (softc->ha_mode == CTL_HA_MODE_XFER) 862 ctl_isc_handler_finish_xfer(softc, 863 &msg_info); 864 else 865 ctl_isc_handler_finish_ser_only(softc, 866 &msg_info); 867 break; 868 869 /* Preformed on Originating SC */ 870 case CTL_MSG_BAD_JUJU: 871 io = msg_info.hdr.original_sc; 872 if (io == NULL) { 873 printf("%s: Bad JUJU!, original_sc is NULL!\n", 874 __func__); 875 break; 876 } 877 ctl_copy_sense_data(&msg_info, io); 878 /* 879 * IO should have already been cleaned up on other 880 * SC so clear this flag so we won't send a message 881 * back to finish the IO there. 882 */ 883 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 884 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 885 886 /* io = msg_info.hdr.serializing_sc; */ 887 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 888 ctl_enqueue_isc(io); 889 break; 890 891 /* Handle resets sent from the other side */ 892 case CTL_MSG_MANAGE_TASKS: { 893 struct ctl_taskio *taskio; 894 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait( 895 softc->othersc_pool); 896 if (taskio == NULL) { 897 printf("ctl_isc_event_handler: can't allocate " 898 "ctl_io!\n"); 899 /* Bad Juju */ 900 /* should I just call the proper reset func 901 here??? */ 902 goto bailout; 903 } 904 ctl_zero_io((union ctl_io *)taskio); 905 taskio->io_hdr.io_type = CTL_IO_TASK; 906 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 907 taskio->io_hdr.nexus = msg_info.hdr.nexus; 908 taskio->task_action = msg_info.task.task_action; 909 taskio->tag_num = msg_info.task.tag_num; 910 taskio->tag_type = msg_info.task.tag_type; 911 #ifdef CTL_TIME_IO 912 taskio->io_hdr.start_time = time_uptime; 913 getbintime(&taskio->io_hdr.start_bt); 914 #if 0 915 cs_prof_gettime(&taskio->io_hdr.start_ticks); 916 #endif 917 #endif /* CTL_TIME_IO */ 918 ctl_run_task((union ctl_io *)taskio); 919 break; 920 } 921 /* Persistent Reserve action which needs attention */ 922 case CTL_MSG_PERS_ACTION: 923 presio = (struct ctl_prio *)ctl_alloc_io_nowait( 924 softc->othersc_pool); 925 if (presio == NULL) { 926 printf("ctl_isc_event_handler: can't allocate " 927 "ctl_io!\n"); 928 /* Bad Juju */ 929 /* Need to set busy and send msg back */ 930 goto bailout; 931 } 932 ctl_zero_io((union ctl_io *)presio); 933 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 934 presio->pr_msg = msg_info.pr; 935 ctl_enqueue_isc((union ctl_io *)presio); 936 break; 937 case CTL_MSG_SYNC_FE: 938 rcv_sync_msg = 1; 939 break; 940 default: 941 printf("How did I get here?\n"); 942 } 943 } else if (event == CTL_HA_EVT_MSG_SENT) { 944 if (param != CTL_HA_STATUS_SUCCESS) { 945 printf("Bad status from ctl_ha_msg_send status %d\n", 946 param); 947 } 948 return; 949 } else if (event == CTL_HA_EVT_DISCONNECT) { 950 printf("CTL: Got a disconnect from Isc\n"); 951 return; 952 } else { 953 printf("ctl_isc_event_handler: Unknown event %d\n", event); 954 return; 955 } 956 957 bailout: 958 return; 959 } 960 961 static void 962 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 963 { 964 struct scsi_sense_data *sense; 965 966 sense = &dest->scsiio.sense_data; 967 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 968 dest->scsiio.scsi_status = src->scsi.scsi_status; 969 dest->scsiio.sense_len = src->scsi.sense_len; 970 dest->io_hdr.status = src->hdr.status; 971 } 972 #endif 973 974 static void 975 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 976 { 977 ctl_ua_type *pu; 978 979 mtx_assert(&lun->lun_lock, MA_OWNED); 980 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 981 if (pu == NULL) 982 return; 983 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 984 } 985 986 static void 987 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 988 { 989 int i, j; 990 991 mtx_assert(&lun->lun_lock, MA_OWNED); 992 for (i = 0; i < CTL_MAX_PORTS; i++) { 993 if (lun->pending_ua[i] == NULL) 994 continue; 995 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 996 if (i * CTL_MAX_INIT_PER_PORT + j == except) 997 continue; 998 lun->pending_ua[i][j] |= ua; 999 } 1000 } 1001 } 1002 1003 static void 1004 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1005 { 1006 ctl_ua_type *pu; 1007 1008 mtx_assert(&lun->lun_lock, MA_OWNED); 1009 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1010 if (pu == NULL) 1011 return; 1012 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1013 } 1014 1015 static void 1016 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1017 { 1018 int i, j; 1019 1020 mtx_assert(&lun->lun_lock, MA_OWNED); 1021 for (i = 0; i < CTL_MAX_PORTS; i++) { 1022 if (lun->pending_ua[i] == NULL) 1023 continue; 1024 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1025 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1026 continue; 1027 lun->pending_ua[i][j] &= ~ua; 1028 } 1029 } 1030 } 1031 1032 static int 1033 ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS) 1034 { 1035 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1036 struct ctl_lun *lun; 1037 int error, value; 1038 1039 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) 1040 value = 0; 1041 else 1042 value = 1; 1043 1044 error = sysctl_handle_int(oidp, &value, 0, req); 1045 if ((error != 0) || (req->newptr == NULL)) 1046 return (error); 1047 1048 mtx_lock(&softc->ctl_lock); 1049 if (value == 0) 1050 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1051 else 1052 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1053 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1054 mtx_lock(&lun->lun_lock); 1055 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1056 mtx_unlock(&lun->lun_lock); 1057 } 1058 mtx_unlock(&softc->ctl_lock); 1059 return (0); 1060 } 1061 1062 static int 1063 ctl_init(void) 1064 { 1065 struct ctl_softc *softc; 1066 void *other_pool; 1067 struct ctl_port *port; 1068 int i, error, retval; 1069 //int isc_retval; 1070 1071 retval = 0; 1072 ctl_pause_rtr = 0; 1073 rcv_sync_msg = 0; 1074 1075 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1076 M_WAITOK | M_ZERO); 1077 softc = control_softc; 1078 1079 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1080 "cam/ctl"); 1081 1082 softc->dev->si_drv1 = softc; 1083 1084 /* 1085 * By default, return a "bad LUN" peripheral qualifier for unknown 1086 * LUNs. The user can override this default using the tunable or 1087 * sysctl. See the comment in ctl_inquiry_std() for more details. 1088 */ 1089 softc->inquiry_pq_no_lun = 1; 1090 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 1091 &softc->inquiry_pq_no_lun); 1092 sysctl_ctx_init(&softc->sysctl_ctx); 1093 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1094 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1095 CTLFLAG_RD, 0, "CAM Target Layer"); 1096 1097 if (softc->sysctl_tree == NULL) { 1098 printf("%s: unable to allocate sysctl tree\n", __func__); 1099 destroy_dev(softc->dev); 1100 free(control_softc, M_DEVBUF); 1101 control_softc = NULL; 1102 return (ENOMEM); 1103 } 1104 1105 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1106 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1107 "inquiry_pq_no_lun", CTLFLAG_RW, 1108 &softc->inquiry_pq_no_lun, 0, 1109 "Report no lun possible for invalid LUNs"); 1110 1111 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1112 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1113 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1114 softc->open_count = 0; 1115 1116 /* 1117 * Default to actually sending a SYNCHRONIZE CACHE command down to 1118 * the drive. 1119 */ 1120 softc->flags = CTL_FLAG_REAL_SYNC; 1121 1122 /* 1123 * In Copan's HA scheme, the "master" and "slave" roles are 1124 * figured out through the slot the controller is in. Although it 1125 * is an active/active system, someone has to be in charge. 1126 */ 1127 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1128 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1129 "HA head ID (0 - no HA)"); 1130 if (softc->ha_id == 0) { 1131 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1132 softc->is_single = 1; 1133 softc->port_offset = 0; 1134 } else 1135 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS; 1136 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT; 1137 1138 STAILQ_INIT(&softc->lun_list); 1139 STAILQ_INIT(&softc->pending_lun_queue); 1140 STAILQ_INIT(&softc->fe_list); 1141 STAILQ_INIT(&softc->port_list); 1142 STAILQ_INIT(&softc->be_list); 1143 ctl_tpc_init(softc); 1144 1145 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1146 &other_pool) != 0) 1147 { 1148 printf("ctl: can't allocate %d entry other SC pool, " 1149 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1150 return (ENOMEM); 1151 } 1152 softc->othersc_pool = other_pool; 1153 1154 if (worker_threads <= 0) 1155 worker_threads = max(1, mp_ncpus / 4); 1156 if (worker_threads > CTL_MAX_THREADS) 1157 worker_threads = CTL_MAX_THREADS; 1158 1159 for (i = 0; i < worker_threads; i++) { 1160 struct ctl_thread *thr = &softc->threads[i]; 1161 1162 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1163 thr->ctl_softc = softc; 1164 STAILQ_INIT(&thr->incoming_queue); 1165 STAILQ_INIT(&thr->rtr_queue); 1166 STAILQ_INIT(&thr->done_queue); 1167 STAILQ_INIT(&thr->isc_queue); 1168 1169 error = kproc_kthread_add(ctl_work_thread, thr, 1170 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1171 if (error != 0) { 1172 printf("error creating CTL work thread!\n"); 1173 ctl_pool_free(other_pool); 1174 return (error); 1175 } 1176 } 1177 error = kproc_kthread_add(ctl_lun_thread, softc, 1178 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1179 if (error != 0) { 1180 printf("error creating CTL lun thread!\n"); 1181 ctl_pool_free(other_pool); 1182 return (error); 1183 } 1184 error = kproc_kthread_add(ctl_thresh_thread, softc, 1185 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1186 if (error != 0) { 1187 printf("error creating CTL threshold thread!\n"); 1188 ctl_pool_free(other_pool); 1189 return (error); 1190 } 1191 1192 /* 1193 * Initialize the ioctl front end. 1194 */ 1195 ctl_frontend_register(&ioctl_frontend); 1196 port = &softc->ioctl_info.port; 1197 port->frontend = &ioctl_frontend; 1198 sprintf(softc->ioctl_info.port_name, "ioctl"); 1199 port->port_type = CTL_PORT_IOCTL; 1200 port->num_requested_ctl_io = 100; 1201 port->port_name = softc->ioctl_info.port_name; 1202 port->port_online = ctl_ioctl_online; 1203 port->port_offline = ctl_ioctl_offline; 1204 port->onoff_arg = &softc->ioctl_info; 1205 port->lun_enable = ctl_ioctl_lun_enable; 1206 port->lun_disable = ctl_ioctl_lun_disable; 1207 port->targ_lun_arg = &softc->ioctl_info; 1208 port->fe_datamove = ctl_ioctl_datamove; 1209 port->fe_done = ctl_ioctl_done; 1210 port->max_targets = 15; 1211 port->max_target_id = 15; 1212 1213 if (ctl_port_register(&softc->ioctl_info.port) != 0) { 1214 printf("ctl: ioctl front end registration failed, will " 1215 "continue anyway\n"); 1216 } 1217 1218 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1219 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN, 1220 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head"); 1221 1222 #ifdef CTL_IO_DELAY 1223 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1224 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1225 sizeof(struct callout), CTL_TIMER_BYTES); 1226 return (EINVAL); 1227 } 1228 #endif /* CTL_IO_DELAY */ 1229 1230 return (0); 1231 } 1232 1233 void 1234 ctl_shutdown(void) 1235 { 1236 struct ctl_softc *softc; 1237 struct ctl_lun *lun, *next_lun; 1238 1239 softc = (struct ctl_softc *)control_softc; 1240 1241 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1242 printf("ctl: ioctl front end deregistration failed\n"); 1243 1244 mtx_lock(&softc->ctl_lock); 1245 1246 /* 1247 * Free up each LUN. 1248 */ 1249 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1250 next_lun = STAILQ_NEXT(lun, links); 1251 ctl_free_lun(lun); 1252 } 1253 1254 mtx_unlock(&softc->ctl_lock); 1255 1256 ctl_frontend_deregister(&ioctl_frontend); 1257 1258 #if 0 1259 ctl_shutdown_thread(softc->work_thread); 1260 mtx_destroy(&softc->queue_lock); 1261 #endif 1262 1263 ctl_tpc_shutdown(softc); 1264 uma_zdestroy(softc->io_zone); 1265 mtx_destroy(&softc->ctl_lock); 1266 1267 destroy_dev(softc->dev); 1268 1269 sysctl_ctx_free(&softc->sysctl_ctx); 1270 1271 free(control_softc, M_DEVBUF); 1272 control_softc = NULL; 1273 } 1274 1275 static int 1276 ctl_module_event_handler(module_t mod, int what, void *arg) 1277 { 1278 1279 switch (what) { 1280 case MOD_LOAD: 1281 return (ctl_init()); 1282 case MOD_UNLOAD: 1283 return (EBUSY); 1284 default: 1285 return (EOPNOTSUPP); 1286 } 1287 } 1288 1289 /* 1290 * XXX KDM should we do some access checks here? Bump a reference count to 1291 * prevent a CTL module from being unloaded while someone has it open? 1292 */ 1293 static int 1294 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1295 { 1296 return (0); 1297 } 1298 1299 static int 1300 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1301 { 1302 return (0); 1303 } 1304 1305 int 1306 ctl_port_enable(ctl_port_type port_type) 1307 { 1308 struct ctl_softc *softc = control_softc; 1309 struct ctl_port *port; 1310 1311 if (softc->is_single == 0) { 1312 union ctl_ha_msg msg_info; 1313 int isc_retval; 1314 1315 #if 0 1316 printf("%s: HA mode, synchronizing frontend enable\n", 1317 __func__); 1318 #endif 1319 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1320 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1321 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1322 printf("Sync msg send error retval %d\n", isc_retval); 1323 } 1324 if (!rcv_sync_msg) { 1325 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1326 sizeof(msg_info), 1); 1327 } 1328 #if 0 1329 printf("CTL:Frontend Enable\n"); 1330 } else { 1331 printf("%s: single mode, skipping frontend synchronization\n", 1332 __func__); 1333 #endif 1334 } 1335 1336 STAILQ_FOREACH(port, &softc->port_list, links) { 1337 if (port_type & port->port_type) 1338 { 1339 #if 0 1340 printf("port %d\n", port->targ_port); 1341 #endif 1342 ctl_port_online(port); 1343 } 1344 } 1345 1346 return (0); 1347 } 1348 1349 int 1350 ctl_port_disable(ctl_port_type port_type) 1351 { 1352 struct ctl_softc *softc; 1353 struct ctl_port *port; 1354 1355 softc = control_softc; 1356 1357 STAILQ_FOREACH(port, &softc->port_list, links) { 1358 if (port_type & port->port_type) 1359 ctl_port_offline(port); 1360 } 1361 1362 return (0); 1363 } 1364 1365 /* 1366 * Returns 0 for success, 1 for failure. 1367 * Currently the only failure mode is if there aren't enough entries 1368 * allocated. So, in case of a failure, look at num_entries_dropped, 1369 * reallocate and try again. 1370 */ 1371 int 1372 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1373 int *num_entries_filled, int *num_entries_dropped, 1374 ctl_port_type port_type, int no_virtual) 1375 { 1376 struct ctl_softc *softc; 1377 struct ctl_port *port; 1378 int entries_dropped, entries_filled; 1379 int retval; 1380 int i; 1381 1382 softc = control_softc; 1383 1384 retval = 0; 1385 entries_filled = 0; 1386 entries_dropped = 0; 1387 1388 i = 0; 1389 mtx_lock(&softc->ctl_lock); 1390 STAILQ_FOREACH(port, &softc->port_list, links) { 1391 struct ctl_port_entry *entry; 1392 1393 if ((port->port_type & port_type) == 0) 1394 continue; 1395 1396 if ((no_virtual != 0) 1397 && (port->virtual_port != 0)) 1398 continue; 1399 1400 if (entries_filled >= num_entries_alloced) { 1401 entries_dropped++; 1402 continue; 1403 } 1404 entry = &entries[i]; 1405 1406 entry->port_type = port->port_type; 1407 strlcpy(entry->port_name, port->port_name, 1408 sizeof(entry->port_name)); 1409 entry->physical_port = port->physical_port; 1410 entry->virtual_port = port->virtual_port; 1411 entry->wwnn = port->wwnn; 1412 entry->wwpn = port->wwpn; 1413 1414 i++; 1415 entries_filled++; 1416 } 1417 1418 mtx_unlock(&softc->ctl_lock); 1419 1420 if (entries_dropped > 0) 1421 retval = 1; 1422 1423 *num_entries_dropped = entries_dropped; 1424 *num_entries_filled = entries_filled; 1425 1426 return (retval); 1427 } 1428 1429 static void 1430 ctl_ioctl_online(void *arg) 1431 { 1432 struct ctl_ioctl_info *ioctl_info; 1433 1434 ioctl_info = (struct ctl_ioctl_info *)arg; 1435 1436 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1437 } 1438 1439 static void 1440 ctl_ioctl_offline(void *arg) 1441 { 1442 struct ctl_ioctl_info *ioctl_info; 1443 1444 ioctl_info = (struct ctl_ioctl_info *)arg; 1445 1446 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1447 } 1448 1449 /* 1450 * Remove an initiator by port number and initiator ID. 1451 * Returns 0 for success, -1 for failure. 1452 */ 1453 int 1454 ctl_remove_initiator(struct ctl_port *port, int iid) 1455 { 1456 struct ctl_softc *softc = control_softc; 1457 1458 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1459 1460 if (iid > CTL_MAX_INIT_PER_PORT) { 1461 printf("%s: initiator ID %u > maximun %u!\n", 1462 __func__, iid, CTL_MAX_INIT_PER_PORT); 1463 return (-1); 1464 } 1465 1466 mtx_lock(&softc->ctl_lock); 1467 port->wwpn_iid[iid].in_use--; 1468 port->wwpn_iid[iid].last_use = time_uptime; 1469 mtx_unlock(&softc->ctl_lock); 1470 1471 return (0); 1472 } 1473 1474 /* 1475 * Add an initiator to the initiator map. 1476 * Returns iid for success, < 0 for failure. 1477 */ 1478 int 1479 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1480 { 1481 struct ctl_softc *softc = control_softc; 1482 time_t best_time; 1483 int i, best; 1484 1485 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1486 1487 if (iid >= CTL_MAX_INIT_PER_PORT) { 1488 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1489 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1490 free(name, M_CTL); 1491 return (-1); 1492 } 1493 1494 mtx_lock(&softc->ctl_lock); 1495 1496 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1497 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1498 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1499 iid = i; 1500 break; 1501 } 1502 if (name != NULL && port->wwpn_iid[i].name != NULL && 1503 strcmp(name, port->wwpn_iid[i].name) == 0) { 1504 iid = i; 1505 break; 1506 } 1507 } 1508 } 1509 1510 if (iid < 0) { 1511 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1512 if (port->wwpn_iid[i].in_use == 0 && 1513 port->wwpn_iid[i].wwpn == 0 && 1514 port->wwpn_iid[i].name == NULL) { 1515 iid = i; 1516 break; 1517 } 1518 } 1519 } 1520 1521 if (iid < 0) { 1522 best = -1; 1523 best_time = INT32_MAX; 1524 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1525 if (port->wwpn_iid[i].in_use == 0) { 1526 if (port->wwpn_iid[i].last_use < best_time) { 1527 best = i; 1528 best_time = port->wwpn_iid[i].last_use; 1529 } 1530 } 1531 } 1532 iid = best; 1533 } 1534 1535 if (iid < 0) { 1536 mtx_unlock(&softc->ctl_lock); 1537 free(name, M_CTL); 1538 return (-2); 1539 } 1540 1541 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1542 /* 1543 * This is not an error yet. 1544 */ 1545 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1546 #if 0 1547 printf("%s: port %d iid %u WWPN %#jx arrived" 1548 " again\n", __func__, port->targ_port, 1549 iid, (uintmax_t)wwpn); 1550 #endif 1551 goto take; 1552 } 1553 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1554 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1555 #if 0 1556 printf("%s: port %d iid %u name '%s' arrived" 1557 " again\n", __func__, port->targ_port, 1558 iid, name); 1559 #endif 1560 goto take; 1561 } 1562 1563 /* 1564 * This is an error, but what do we do about it? The 1565 * driver is telling us we have a new WWPN for this 1566 * initiator ID, so we pretty much need to use it. 1567 */ 1568 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1569 " but WWPN %#jx '%s' is still at that address\n", 1570 __func__, port->targ_port, iid, wwpn, name, 1571 (uintmax_t)port->wwpn_iid[iid].wwpn, 1572 port->wwpn_iid[iid].name); 1573 1574 /* 1575 * XXX KDM clear have_ca and ua_pending on each LUN for 1576 * this initiator. 1577 */ 1578 } 1579 take: 1580 free(port->wwpn_iid[iid].name, M_CTL); 1581 port->wwpn_iid[iid].name = name; 1582 port->wwpn_iid[iid].wwpn = wwpn; 1583 port->wwpn_iid[iid].in_use++; 1584 mtx_unlock(&softc->ctl_lock); 1585 1586 return (iid); 1587 } 1588 1589 static int 1590 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1591 { 1592 int len; 1593 1594 switch (port->port_type) { 1595 case CTL_PORT_FC: 1596 { 1597 struct scsi_transportid_fcp *id = 1598 (struct scsi_transportid_fcp *)buf; 1599 if (port->wwpn_iid[iid].wwpn == 0) 1600 return (0); 1601 memset(id, 0, sizeof(*id)); 1602 id->format_protocol = SCSI_PROTO_FC; 1603 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1604 return (sizeof(*id)); 1605 } 1606 case CTL_PORT_ISCSI: 1607 { 1608 struct scsi_transportid_iscsi_port *id = 1609 (struct scsi_transportid_iscsi_port *)buf; 1610 if (port->wwpn_iid[iid].name == NULL) 1611 return (0); 1612 memset(id, 0, 256); 1613 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1614 SCSI_PROTO_ISCSI; 1615 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1616 len = roundup2(min(len, 252), 4); 1617 scsi_ulto2b(len, id->additional_length); 1618 return (sizeof(*id) + len); 1619 } 1620 case CTL_PORT_SAS: 1621 { 1622 struct scsi_transportid_sas *id = 1623 (struct scsi_transportid_sas *)buf; 1624 if (port->wwpn_iid[iid].wwpn == 0) 1625 return (0); 1626 memset(id, 0, sizeof(*id)); 1627 id->format_protocol = SCSI_PROTO_SAS; 1628 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1629 return (sizeof(*id)); 1630 } 1631 default: 1632 { 1633 struct scsi_transportid_spi *id = 1634 (struct scsi_transportid_spi *)buf; 1635 memset(id, 0, sizeof(*id)); 1636 id->format_protocol = SCSI_PROTO_SPI; 1637 scsi_ulto2b(iid, id->scsi_addr); 1638 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1639 return (sizeof(*id)); 1640 } 1641 } 1642 } 1643 1644 static int 1645 ctl_ioctl_lun_enable(void *arg, int lun_id) 1646 { 1647 return (0); 1648 } 1649 1650 static int 1651 ctl_ioctl_lun_disable(void *arg, int lun_id) 1652 { 1653 return (0); 1654 } 1655 1656 /* 1657 * Data movement routine for the CTL ioctl frontend port. 1658 */ 1659 static int 1660 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1661 { 1662 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1663 struct ctl_sg_entry ext_entry, kern_entry; 1664 int ext_sglen, ext_sg_entries, kern_sg_entries; 1665 int ext_sg_start, ext_offset; 1666 int len_to_copy, len_copied; 1667 int kern_watermark, ext_watermark; 1668 int ext_sglist_malloced; 1669 int i, j; 1670 1671 ext_sglist_malloced = 0; 1672 ext_sg_start = 0; 1673 ext_offset = 0; 1674 1675 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1676 1677 /* 1678 * If this flag is set, fake the data transfer. 1679 */ 1680 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1681 ctsio->ext_data_filled = ctsio->ext_data_len; 1682 goto bailout; 1683 } 1684 1685 /* 1686 * To simplify things here, if we have a single buffer, stick it in 1687 * a S/G entry and just make it a single entry S/G list. 1688 */ 1689 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1690 int len_seen; 1691 1692 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1693 1694 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1695 M_WAITOK); 1696 ext_sglist_malloced = 1; 1697 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1698 ext_sglen) != 0) { 1699 ctl_set_internal_failure(ctsio, 1700 /*sks_valid*/ 0, 1701 /*retry_count*/ 0); 1702 goto bailout; 1703 } 1704 ext_sg_entries = ctsio->ext_sg_entries; 1705 len_seen = 0; 1706 for (i = 0; i < ext_sg_entries; i++) { 1707 if ((len_seen + ext_sglist[i].len) >= 1708 ctsio->ext_data_filled) { 1709 ext_sg_start = i; 1710 ext_offset = ctsio->ext_data_filled - len_seen; 1711 break; 1712 } 1713 len_seen += ext_sglist[i].len; 1714 } 1715 } else { 1716 ext_sglist = &ext_entry; 1717 ext_sglist->addr = ctsio->ext_data_ptr; 1718 ext_sglist->len = ctsio->ext_data_len; 1719 ext_sg_entries = 1; 1720 ext_sg_start = 0; 1721 ext_offset = ctsio->ext_data_filled; 1722 } 1723 1724 if (ctsio->kern_sg_entries > 0) { 1725 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1726 kern_sg_entries = ctsio->kern_sg_entries; 1727 } else { 1728 kern_sglist = &kern_entry; 1729 kern_sglist->addr = ctsio->kern_data_ptr; 1730 kern_sglist->len = ctsio->kern_data_len; 1731 kern_sg_entries = 1; 1732 } 1733 1734 1735 kern_watermark = 0; 1736 ext_watermark = ext_offset; 1737 len_copied = 0; 1738 for (i = ext_sg_start, j = 0; 1739 i < ext_sg_entries && j < kern_sg_entries;) { 1740 uint8_t *ext_ptr, *kern_ptr; 1741 1742 len_to_copy = MIN(ext_sglist[i].len - ext_watermark, 1743 kern_sglist[j].len - kern_watermark); 1744 1745 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1746 ext_ptr = ext_ptr + ext_watermark; 1747 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1748 /* 1749 * XXX KDM fix this! 1750 */ 1751 panic("need to implement bus address support"); 1752 #if 0 1753 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1754 #endif 1755 } else 1756 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1757 kern_ptr = kern_ptr + kern_watermark; 1758 1759 kern_watermark += len_to_copy; 1760 ext_watermark += len_to_copy; 1761 1762 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1763 CTL_FLAG_DATA_IN) { 1764 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1765 "bytes to user\n", len_to_copy)); 1766 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1767 "to %p\n", kern_ptr, ext_ptr)); 1768 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1769 ctl_set_internal_failure(ctsio, 1770 /*sks_valid*/ 0, 1771 /*retry_count*/ 0); 1772 goto bailout; 1773 } 1774 } else { 1775 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1776 "bytes from user\n", len_to_copy)); 1777 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1778 "to %p\n", ext_ptr, kern_ptr)); 1779 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1780 ctl_set_internal_failure(ctsio, 1781 /*sks_valid*/ 0, 1782 /*retry_count*/0); 1783 goto bailout; 1784 } 1785 } 1786 1787 len_copied += len_to_copy; 1788 1789 if (ext_sglist[i].len == ext_watermark) { 1790 i++; 1791 ext_watermark = 0; 1792 } 1793 1794 if (kern_sglist[j].len == kern_watermark) { 1795 j++; 1796 kern_watermark = 0; 1797 } 1798 } 1799 1800 ctsio->ext_data_filled += len_copied; 1801 1802 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1803 "kern_sg_entries: %d\n", ext_sg_entries, 1804 kern_sg_entries)); 1805 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1806 "kern_data_len = %d\n", ctsio->ext_data_len, 1807 ctsio->kern_data_len)); 1808 1809 1810 /* XXX KDM set residual?? */ 1811 bailout: 1812 1813 if (ext_sglist_malloced != 0) 1814 free(ext_sglist, M_CTL); 1815 1816 return (CTL_RETVAL_COMPLETE); 1817 } 1818 1819 /* 1820 * Serialize a command that went down the "wrong" side, and so was sent to 1821 * this controller for execution. The logic is a little different than the 1822 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1823 * sent back to the other side, but in the success case, we execute the 1824 * command on this side (XFER mode) or tell the other side to execute it 1825 * (SER_ONLY mode). 1826 */ 1827 static int 1828 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1829 { 1830 struct ctl_softc *softc; 1831 union ctl_ha_msg msg_info; 1832 struct ctl_lun *lun; 1833 int retval = 0; 1834 uint32_t targ_lun; 1835 1836 softc = control_softc; 1837 1838 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1839 lun = softc->ctl_luns[targ_lun]; 1840 if (lun==NULL) 1841 { 1842 /* 1843 * Why isn't LUN defined? The other side wouldn't 1844 * send a cmd if the LUN is undefined. 1845 */ 1846 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1847 1848 /* "Logical unit not supported" */ 1849 ctl_set_sense_data(&msg_info.scsi.sense_data, 1850 lun, 1851 /*sense_format*/SSD_TYPE_NONE, 1852 /*current_error*/ 1, 1853 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1854 /*asc*/ 0x25, 1855 /*ascq*/ 0x00, 1856 SSD_ELEM_NONE); 1857 1858 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1859 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1860 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1861 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1862 msg_info.hdr.serializing_sc = NULL; 1863 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1864 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1865 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1866 } 1867 return(1); 1868 1869 } 1870 1871 mtx_lock(&lun->lun_lock); 1872 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1873 1874 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1875 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1876 ooa_links))) { 1877 case CTL_ACTION_BLOCK: 1878 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1879 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1880 blocked_links); 1881 break; 1882 case CTL_ACTION_PASS: 1883 case CTL_ACTION_SKIP: 1884 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1885 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1886 ctl_enqueue_rtr((union ctl_io *)ctsio); 1887 } else { 1888 1889 /* send msg back to other side */ 1890 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1891 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1892 msg_info.hdr.msg_type = CTL_MSG_R2R; 1893 #if 0 1894 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1895 #endif 1896 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1897 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1898 } 1899 } 1900 break; 1901 case CTL_ACTION_OVERLAP: 1902 /* OVERLAPPED COMMANDS ATTEMPTED */ 1903 ctl_set_sense_data(&msg_info.scsi.sense_data, 1904 lun, 1905 /*sense_format*/SSD_TYPE_NONE, 1906 /*current_error*/ 1, 1907 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1908 /*asc*/ 0x4E, 1909 /*ascq*/ 0x00, 1910 SSD_ELEM_NONE); 1911 1912 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1913 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1914 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1915 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1916 msg_info.hdr.serializing_sc = NULL; 1917 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1918 #if 0 1919 printf("BAD JUJU:Major Bummer Overlap\n"); 1920 #endif 1921 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1922 retval = 1; 1923 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1924 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1925 } 1926 break; 1927 case CTL_ACTION_OVERLAP_TAG: 1928 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1929 ctl_set_sense_data(&msg_info.scsi.sense_data, 1930 lun, 1931 /*sense_format*/SSD_TYPE_NONE, 1932 /*current_error*/ 1, 1933 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1934 /*asc*/ 0x4D, 1935 /*ascq*/ ctsio->tag_num & 0xff, 1936 SSD_ELEM_NONE); 1937 1938 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1939 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1940 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1941 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1942 msg_info.hdr.serializing_sc = NULL; 1943 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1944 #if 0 1945 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1946 #endif 1947 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1948 retval = 1; 1949 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1950 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1951 } 1952 break; 1953 case CTL_ACTION_ERROR: 1954 default: 1955 /* "Internal target failure" */ 1956 ctl_set_sense_data(&msg_info.scsi.sense_data, 1957 lun, 1958 /*sense_format*/SSD_TYPE_NONE, 1959 /*current_error*/ 1, 1960 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1961 /*asc*/ 0x44, 1962 /*ascq*/ 0x00, 1963 SSD_ELEM_NONE); 1964 1965 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1966 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1967 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1968 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1969 msg_info.hdr.serializing_sc = NULL; 1970 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1971 #if 0 1972 printf("BAD JUJU:Major Bummer HW Error\n"); 1973 #endif 1974 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1975 retval = 1; 1976 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1977 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1978 } 1979 break; 1980 } 1981 mtx_unlock(&lun->lun_lock); 1982 return (retval); 1983 } 1984 1985 static int 1986 ctl_ioctl_submit_wait(union ctl_io *io) 1987 { 1988 struct ctl_fe_ioctl_params params; 1989 ctl_fe_ioctl_state last_state; 1990 int done, retval; 1991 1992 retval = 0; 1993 1994 bzero(¶ms, sizeof(params)); 1995 1996 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1997 cv_init(¶ms.sem, "ctlioccv"); 1998 params.state = CTL_IOCTL_INPROG; 1999 last_state = params.state; 2000 2001 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 2002 2003 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 2004 2005 /* This shouldn't happen */ 2006 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 2007 return (retval); 2008 2009 done = 0; 2010 2011 do { 2012 mtx_lock(¶ms.ioctl_mtx); 2013 /* 2014 * Check the state here, and don't sleep if the state has 2015 * already changed (i.e. wakeup has already occured, but we 2016 * weren't waiting yet). 2017 */ 2018 if (params.state == last_state) { 2019 /* XXX KDM cv_wait_sig instead? */ 2020 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 2021 } 2022 last_state = params.state; 2023 2024 switch (params.state) { 2025 case CTL_IOCTL_INPROG: 2026 /* Why did we wake up? */ 2027 /* XXX KDM error here? */ 2028 mtx_unlock(¶ms.ioctl_mtx); 2029 break; 2030 case CTL_IOCTL_DATAMOVE: 2031 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 2032 2033 /* 2034 * change last_state back to INPROG to avoid 2035 * deadlock on subsequent data moves. 2036 */ 2037 params.state = last_state = CTL_IOCTL_INPROG; 2038 2039 mtx_unlock(¶ms.ioctl_mtx); 2040 ctl_ioctl_do_datamove(&io->scsiio); 2041 /* 2042 * Note that in some cases, most notably writes, 2043 * this will queue the I/O and call us back later. 2044 * In other cases, generally reads, this routine 2045 * will immediately call back and wake us up, 2046 * probably using our own context. 2047 */ 2048 io->scsiio.be_move_done(io); 2049 break; 2050 case CTL_IOCTL_DONE: 2051 mtx_unlock(¶ms.ioctl_mtx); 2052 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 2053 done = 1; 2054 break; 2055 default: 2056 mtx_unlock(¶ms.ioctl_mtx); 2057 /* XXX KDM error here? */ 2058 break; 2059 } 2060 } while (done == 0); 2061 2062 mtx_destroy(¶ms.ioctl_mtx); 2063 cv_destroy(¶ms.sem); 2064 2065 return (CTL_RETVAL_COMPLETE); 2066 } 2067 2068 static void 2069 ctl_ioctl_datamove(union ctl_io *io) 2070 { 2071 struct ctl_fe_ioctl_params *params; 2072 2073 params = (struct ctl_fe_ioctl_params *) 2074 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2075 2076 mtx_lock(¶ms->ioctl_mtx); 2077 params->state = CTL_IOCTL_DATAMOVE; 2078 cv_broadcast(¶ms->sem); 2079 mtx_unlock(¶ms->ioctl_mtx); 2080 } 2081 2082 static void 2083 ctl_ioctl_done(union ctl_io *io) 2084 { 2085 struct ctl_fe_ioctl_params *params; 2086 2087 params = (struct ctl_fe_ioctl_params *) 2088 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2089 2090 mtx_lock(¶ms->ioctl_mtx); 2091 params->state = CTL_IOCTL_DONE; 2092 cv_broadcast(¶ms->sem); 2093 mtx_unlock(¶ms->ioctl_mtx); 2094 } 2095 2096 static void 2097 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2098 { 2099 struct ctl_fe_ioctl_startstop_info *sd_info; 2100 2101 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2102 2103 sd_info->hs_info.status = metatask->status; 2104 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2105 sd_info->hs_info.luns_complete = 2106 metatask->taskinfo.startstop.luns_complete; 2107 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2108 2109 cv_broadcast(&sd_info->sem); 2110 } 2111 2112 static void 2113 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2114 { 2115 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2116 2117 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2118 2119 mtx_lock(fe_bbr_info->lock); 2120 fe_bbr_info->bbr_info->status = metatask->status; 2121 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2122 fe_bbr_info->wakeup_done = 1; 2123 mtx_unlock(fe_bbr_info->lock); 2124 2125 cv_broadcast(&fe_bbr_info->sem); 2126 } 2127 2128 /* 2129 * Returns 0 for success, errno for failure. 2130 */ 2131 static int 2132 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2133 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2134 { 2135 union ctl_io *io; 2136 int retval; 2137 2138 retval = 0; 2139 2140 mtx_lock(&lun->lun_lock); 2141 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2142 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2143 ooa_links)) { 2144 struct ctl_ooa_entry *entry; 2145 2146 /* 2147 * If we've got more than we can fit, just count the 2148 * remaining entries. 2149 */ 2150 if (*cur_fill_num >= ooa_hdr->alloc_num) 2151 continue; 2152 2153 entry = &kern_entries[*cur_fill_num]; 2154 2155 entry->tag_num = io->scsiio.tag_num; 2156 entry->lun_num = lun->lun; 2157 #ifdef CTL_TIME_IO 2158 entry->start_bt = io->io_hdr.start_bt; 2159 #endif 2160 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2161 entry->cdb_len = io->scsiio.cdb_len; 2162 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2163 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2164 2165 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2166 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2167 2168 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2169 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2170 2171 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2172 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2173 2174 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2175 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2176 } 2177 mtx_unlock(&lun->lun_lock); 2178 2179 return (retval); 2180 } 2181 2182 static void * 2183 ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2184 size_t error_str_len) 2185 { 2186 void *kptr; 2187 2188 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2189 2190 if (copyin(user_addr, kptr, len) != 0) { 2191 snprintf(error_str, error_str_len, "Error copying %d bytes " 2192 "from user address %p to kernel address %p", len, 2193 user_addr, kptr); 2194 free(kptr, M_CTL); 2195 return (NULL); 2196 } 2197 2198 return (kptr); 2199 } 2200 2201 static void 2202 ctl_free_args(int num_args, struct ctl_be_arg *args) 2203 { 2204 int i; 2205 2206 if (args == NULL) 2207 return; 2208 2209 for (i = 0; i < num_args; i++) { 2210 free(args[i].kname, M_CTL); 2211 free(args[i].kvalue, M_CTL); 2212 } 2213 2214 free(args, M_CTL); 2215 } 2216 2217 static struct ctl_be_arg * 2218 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2219 char *error_str, size_t error_str_len) 2220 { 2221 struct ctl_be_arg *args; 2222 int i; 2223 2224 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2225 error_str, error_str_len); 2226 2227 if (args == NULL) 2228 goto bailout; 2229 2230 for (i = 0; i < num_args; i++) { 2231 args[i].kname = NULL; 2232 args[i].kvalue = NULL; 2233 } 2234 2235 for (i = 0; i < num_args; i++) { 2236 uint8_t *tmpptr; 2237 2238 args[i].kname = ctl_copyin_alloc(args[i].name, 2239 args[i].namelen, error_str, error_str_len); 2240 if (args[i].kname == NULL) 2241 goto bailout; 2242 2243 if (args[i].kname[args[i].namelen - 1] != '\0') { 2244 snprintf(error_str, error_str_len, "Argument %d " 2245 "name is not NUL-terminated", i); 2246 goto bailout; 2247 } 2248 2249 if (args[i].flags & CTL_BEARG_RD) { 2250 tmpptr = ctl_copyin_alloc(args[i].value, 2251 args[i].vallen, error_str, error_str_len); 2252 if (tmpptr == NULL) 2253 goto bailout; 2254 if ((args[i].flags & CTL_BEARG_ASCII) 2255 && (tmpptr[args[i].vallen - 1] != '\0')) { 2256 snprintf(error_str, error_str_len, "Argument " 2257 "%d value is not NUL-terminated", i); 2258 goto bailout; 2259 } 2260 args[i].kvalue = tmpptr; 2261 } else { 2262 args[i].kvalue = malloc(args[i].vallen, 2263 M_CTL, M_WAITOK | M_ZERO); 2264 } 2265 } 2266 2267 return (args); 2268 bailout: 2269 2270 ctl_free_args(num_args, args); 2271 2272 return (NULL); 2273 } 2274 2275 static void 2276 ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2277 { 2278 int i; 2279 2280 for (i = 0; i < num_args; i++) { 2281 if (args[i].flags & CTL_BEARG_WR) 2282 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2283 } 2284 } 2285 2286 /* 2287 * Escape characters that are illegal or not recommended in XML. 2288 */ 2289 int 2290 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2291 { 2292 char *end = str + size; 2293 int retval; 2294 2295 retval = 0; 2296 2297 for (; *str && str < end; str++) { 2298 switch (*str) { 2299 case '&': 2300 retval = sbuf_printf(sb, "&"); 2301 break; 2302 case '>': 2303 retval = sbuf_printf(sb, ">"); 2304 break; 2305 case '<': 2306 retval = sbuf_printf(sb, "<"); 2307 break; 2308 default: 2309 retval = sbuf_putc(sb, *str); 2310 break; 2311 } 2312 2313 if (retval != 0) 2314 break; 2315 2316 } 2317 2318 return (retval); 2319 } 2320 2321 static void 2322 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2323 { 2324 struct scsi_vpd_id_descriptor *desc; 2325 int i; 2326 2327 if (id == NULL || id->len < 4) 2328 return; 2329 desc = (struct scsi_vpd_id_descriptor *)id->data; 2330 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2331 case SVPD_ID_TYPE_T10: 2332 sbuf_printf(sb, "t10."); 2333 break; 2334 case SVPD_ID_TYPE_EUI64: 2335 sbuf_printf(sb, "eui."); 2336 break; 2337 case SVPD_ID_TYPE_NAA: 2338 sbuf_printf(sb, "naa."); 2339 break; 2340 case SVPD_ID_TYPE_SCSI_NAME: 2341 break; 2342 } 2343 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2344 case SVPD_ID_CODESET_BINARY: 2345 for (i = 0; i < desc->length; i++) 2346 sbuf_printf(sb, "%02x", desc->identifier[i]); 2347 break; 2348 case SVPD_ID_CODESET_ASCII: 2349 sbuf_printf(sb, "%.*s", (int)desc->length, 2350 (char *)desc->identifier); 2351 break; 2352 case SVPD_ID_CODESET_UTF8: 2353 sbuf_printf(sb, "%s", (char *)desc->identifier); 2354 break; 2355 } 2356 } 2357 2358 static int 2359 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2360 struct thread *td) 2361 { 2362 struct ctl_softc *softc; 2363 int retval; 2364 2365 softc = control_softc; 2366 2367 retval = 0; 2368 2369 switch (cmd) { 2370 case CTL_IO: { 2371 union ctl_io *io; 2372 void *pool_tmp; 2373 2374 /* 2375 * If we haven't been "enabled", don't allow any SCSI I/O 2376 * to this FETD. 2377 */ 2378 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2379 retval = EPERM; 2380 break; 2381 } 2382 2383 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2384 2385 /* 2386 * Need to save the pool reference so it doesn't get 2387 * spammed by the user's ctl_io. 2388 */ 2389 pool_tmp = io->io_hdr.pool; 2390 memcpy(io, (void *)addr, sizeof(*io)); 2391 io->io_hdr.pool = pool_tmp; 2392 2393 /* 2394 * No status yet, so make sure the status is set properly. 2395 */ 2396 io->io_hdr.status = CTL_STATUS_NONE; 2397 2398 /* 2399 * The user sets the initiator ID, target and LUN IDs. 2400 */ 2401 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2402 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2403 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2404 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2405 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2406 2407 retval = ctl_ioctl_submit_wait(io); 2408 2409 if (retval != 0) { 2410 ctl_free_io(io); 2411 break; 2412 } 2413 2414 memcpy((void *)addr, io, sizeof(*io)); 2415 2416 /* return this to our pool */ 2417 ctl_free_io(io); 2418 2419 break; 2420 } 2421 case CTL_ENABLE_PORT: 2422 case CTL_DISABLE_PORT: 2423 case CTL_SET_PORT_WWNS: { 2424 struct ctl_port *port; 2425 struct ctl_port_entry *entry; 2426 2427 entry = (struct ctl_port_entry *)addr; 2428 2429 mtx_lock(&softc->ctl_lock); 2430 STAILQ_FOREACH(port, &softc->port_list, links) { 2431 int action, done; 2432 2433 action = 0; 2434 done = 0; 2435 2436 if ((entry->port_type == CTL_PORT_NONE) 2437 && (entry->targ_port == port->targ_port)) { 2438 /* 2439 * If the user only wants to enable or 2440 * disable or set WWNs on a specific port, 2441 * do the operation and we're done. 2442 */ 2443 action = 1; 2444 done = 1; 2445 } else if (entry->port_type & port->port_type) { 2446 /* 2447 * Compare the user's type mask with the 2448 * particular frontend type to see if we 2449 * have a match. 2450 */ 2451 action = 1; 2452 done = 0; 2453 2454 /* 2455 * Make sure the user isn't trying to set 2456 * WWNs on multiple ports at the same time. 2457 */ 2458 if (cmd == CTL_SET_PORT_WWNS) { 2459 printf("%s: Can't set WWNs on " 2460 "multiple ports\n", __func__); 2461 retval = EINVAL; 2462 break; 2463 } 2464 } 2465 if (action != 0) { 2466 /* 2467 * XXX KDM we have to drop the lock here, 2468 * because the online/offline operations 2469 * can potentially block. We need to 2470 * reference count the frontends so they 2471 * can't go away, 2472 */ 2473 mtx_unlock(&softc->ctl_lock); 2474 2475 if (cmd == CTL_ENABLE_PORT) { 2476 ctl_port_online(port); 2477 } else if (cmd == CTL_DISABLE_PORT) { 2478 ctl_port_offline(port); 2479 } 2480 2481 mtx_lock(&softc->ctl_lock); 2482 2483 if (cmd == CTL_SET_PORT_WWNS) 2484 ctl_port_set_wwns(port, 2485 (entry->flags & CTL_PORT_WWNN_VALID) ? 2486 1 : 0, entry->wwnn, 2487 (entry->flags & CTL_PORT_WWPN_VALID) ? 2488 1 : 0, entry->wwpn); 2489 } 2490 if (done != 0) 2491 break; 2492 } 2493 mtx_unlock(&softc->ctl_lock); 2494 break; 2495 } 2496 case CTL_GET_PORT_LIST: { 2497 struct ctl_port *port; 2498 struct ctl_port_list *list; 2499 int i; 2500 2501 list = (struct ctl_port_list *)addr; 2502 2503 if (list->alloc_len != (list->alloc_num * 2504 sizeof(struct ctl_port_entry))) { 2505 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2506 "alloc_num %u * sizeof(struct ctl_port_entry) " 2507 "%zu\n", __func__, list->alloc_len, 2508 list->alloc_num, sizeof(struct ctl_port_entry)); 2509 retval = EINVAL; 2510 break; 2511 } 2512 list->fill_len = 0; 2513 list->fill_num = 0; 2514 list->dropped_num = 0; 2515 i = 0; 2516 mtx_lock(&softc->ctl_lock); 2517 STAILQ_FOREACH(port, &softc->port_list, links) { 2518 struct ctl_port_entry entry, *list_entry; 2519 2520 if (list->fill_num >= list->alloc_num) { 2521 list->dropped_num++; 2522 continue; 2523 } 2524 2525 entry.port_type = port->port_type; 2526 strlcpy(entry.port_name, port->port_name, 2527 sizeof(entry.port_name)); 2528 entry.targ_port = port->targ_port; 2529 entry.physical_port = port->physical_port; 2530 entry.virtual_port = port->virtual_port; 2531 entry.wwnn = port->wwnn; 2532 entry.wwpn = port->wwpn; 2533 if (port->status & CTL_PORT_STATUS_ONLINE) 2534 entry.online = 1; 2535 else 2536 entry.online = 0; 2537 2538 list_entry = &list->entries[i]; 2539 2540 retval = copyout(&entry, list_entry, sizeof(entry)); 2541 if (retval != 0) { 2542 printf("%s: CTL_GET_PORT_LIST: copyout " 2543 "returned %d\n", __func__, retval); 2544 break; 2545 } 2546 i++; 2547 list->fill_num++; 2548 list->fill_len += sizeof(entry); 2549 } 2550 mtx_unlock(&softc->ctl_lock); 2551 2552 /* 2553 * If this is non-zero, we had a copyout fault, so there's 2554 * probably no point in attempting to set the status inside 2555 * the structure. 2556 */ 2557 if (retval != 0) 2558 break; 2559 2560 if (list->dropped_num > 0) 2561 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2562 else 2563 list->status = CTL_PORT_LIST_OK; 2564 break; 2565 } 2566 case CTL_DUMP_OOA: { 2567 struct ctl_lun *lun; 2568 union ctl_io *io; 2569 char printbuf[128]; 2570 struct sbuf sb; 2571 2572 mtx_lock(&softc->ctl_lock); 2573 printf("Dumping OOA queues:\n"); 2574 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2575 mtx_lock(&lun->lun_lock); 2576 for (io = (union ctl_io *)TAILQ_FIRST( 2577 &lun->ooa_queue); io != NULL; 2578 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2579 ooa_links)) { 2580 sbuf_new(&sb, printbuf, sizeof(printbuf), 2581 SBUF_FIXEDLEN); 2582 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2583 (intmax_t)lun->lun, 2584 io->scsiio.tag_num, 2585 (io->io_hdr.flags & 2586 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2587 (io->io_hdr.flags & 2588 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2589 (io->io_hdr.flags & 2590 CTL_FLAG_ABORT) ? " ABORT" : "", 2591 (io->io_hdr.flags & 2592 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2593 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2594 sbuf_finish(&sb); 2595 printf("%s\n", sbuf_data(&sb)); 2596 } 2597 mtx_unlock(&lun->lun_lock); 2598 } 2599 printf("OOA queues dump done\n"); 2600 mtx_unlock(&softc->ctl_lock); 2601 break; 2602 } 2603 case CTL_GET_OOA: { 2604 struct ctl_lun *lun; 2605 struct ctl_ooa *ooa_hdr; 2606 struct ctl_ooa_entry *entries; 2607 uint32_t cur_fill_num; 2608 2609 ooa_hdr = (struct ctl_ooa *)addr; 2610 2611 if ((ooa_hdr->alloc_len == 0) 2612 || (ooa_hdr->alloc_num == 0)) { 2613 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2614 "must be non-zero\n", __func__, 2615 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2616 retval = EINVAL; 2617 break; 2618 } 2619 2620 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2621 sizeof(struct ctl_ooa_entry))) { 2622 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2623 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2624 __func__, ooa_hdr->alloc_len, 2625 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2626 retval = EINVAL; 2627 break; 2628 } 2629 2630 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2631 if (entries == NULL) { 2632 printf("%s: could not allocate %d bytes for OOA " 2633 "dump\n", __func__, ooa_hdr->alloc_len); 2634 retval = ENOMEM; 2635 break; 2636 } 2637 2638 mtx_lock(&softc->ctl_lock); 2639 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2640 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2641 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2642 mtx_unlock(&softc->ctl_lock); 2643 free(entries, M_CTL); 2644 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2645 __func__, (uintmax_t)ooa_hdr->lun_num); 2646 retval = EINVAL; 2647 break; 2648 } 2649 2650 cur_fill_num = 0; 2651 2652 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2653 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2654 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2655 ooa_hdr, entries); 2656 if (retval != 0) 2657 break; 2658 } 2659 if (retval != 0) { 2660 mtx_unlock(&softc->ctl_lock); 2661 free(entries, M_CTL); 2662 break; 2663 } 2664 } else { 2665 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2666 2667 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2668 entries); 2669 } 2670 mtx_unlock(&softc->ctl_lock); 2671 2672 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2673 ooa_hdr->fill_len = ooa_hdr->fill_num * 2674 sizeof(struct ctl_ooa_entry); 2675 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2676 if (retval != 0) { 2677 printf("%s: error copying out %d bytes for OOA dump\n", 2678 __func__, ooa_hdr->fill_len); 2679 } 2680 2681 getbintime(&ooa_hdr->cur_bt); 2682 2683 if (cur_fill_num > ooa_hdr->alloc_num) { 2684 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2685 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2686 } else { 2687 ooa_hdr->dropped_num = 0; 2688 ooa_hdr->status = CTL_OOA_OK; 2689 } 2690 2691 free(entries, M_CTL); 2692 break; 2693 } 2694 case CTL_CHECK_OOA: { 2695 union ctl_io *io; 2696 struct ctl_lun *lun; 2697 struct ctl_ooa_info *ooa_info; 2698 2699 2700 ooa_info = (struct ctl_ooa_info *)addr; 2701 2702 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2703 ooa_info->status = CTL_OOA_INVALID_LUN; 2704 break; 2705 } 2706 mtx_lock(&softc->ctl_lock); 2707 lun = softc->ctl_luns[ooa_info->lun_id]; 2708 if (lun == NULL) { 2709 mtx_unlock(&softc->ctl_lock); 2710 ooa_info->status = CTL_OOA_INVALID_LUN; 2711 break; 2712 } 2713 mtx_lock(&lun->lun_lock); 2714 mtx_unlock(&softc->ctl_lock); 2715 ooa_info->num_entries = 0; 2716 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2717 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2718 &io->io_hdr, ooa_links)) { 2719 ooa_info->num_entries++; 2720 } 2721 mtx_unlock(&lun->lun_lock); 2722 2723 ooa_info->status = CTL_OOA_SUCCESS; 2724 2725 break; 2726 } 2727 case CTL_HARD_START: 2728 case CTL_HARD_STOP: { 2729 struct ctl_fe_ioctl_startstop_info ss_info; 2730 struct cfi_metatask *metatask; 2731 struct mtx hs_mtx; 2732 2733 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2734 2735 cv_init(&ss_info.sem, "hard start/stop cv" ); 2736 2737 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2738 if (metatask == NULL) { 2739 retval = ENOMEM; 2740 mtx_destroy(&hs_mtx); 2741 break; 2742 } 2743 2744 if (cmd == CTL_HARD_START) 2745 metatask->tasktype = CFI_TASK_STARTUP; 2746 else 2747 metatask->tasktype = CFI_TASK_SHUTDOWN; 2748 2749 metatask->callback = ctl_ioctl_hard_startstop_callback; 2750 metatask->callback_arg = &ss_info; 2751 2752 cfi_action(metatask); 2753 2754 /* Wait for the callback */ 2755 mtx_lock(&hs_mtx); 2756 cv_wait_sig(&ss_info.sem, &hs_mtx); 2757 mtx_unlock(&hs_mtx); 2758 2759 /* 2760 * All information has been copied from the metatask by the 2761 * time cv_broadcast() is called, so we free the metatask here. 2762 */ 2763 cfi_free_metatask(metatask); 2764 2765 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2766 2767 mtx_destroy(&hs_mtx); 2768 break; 2769 } 2770 case CTL_BBRREAD: { 2771 struct ctl_bbrread_info *bbr_info; 2772 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2773 struct mtx bbr_mtx; 2774 struct cfi_metatask *metatask; 2775 2776 bbr_info = (struct ctl_bbrread_info *)addr; 2777 2778 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2779 2780 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2781 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2782 2783 fe_bbr_info.bbr_info = bbr_info; 2784 fe_bbr_info.lock = &bbr_mtx; 2785 2786 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2787 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2788 2789 if (metatask == NULL) { 2790 mtx_destroy(&bbr_mtx); 2791 cv_destroy(&fe_bbr_info.sem); 2792 retval = ENOMEM; 2793 break; 2794 } 2795 metatask->tasktype = CFI_TASK_BBRREAD; 2796 metatask->callback = ctl_ioctl_bbrread_callback; 2797 metatask->callback_arg = &fe_bbr_info; 2798 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2799 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2800 metatask->taskinfo.bbrread.len = bbr_info->len; 2801 2802 cfi_action(metatask); 2803 2804 mtx_lock(&bbr_mtx); 2805 while (fe_bbr_info.wakeup_done == 0) 2806 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2807 mtx_unlock(&bbr_mtx); 2808 2809 bbr_info->status = metatask->status; 2810 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2811 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2812 memcpy(&bbr_info->sense_data, 2813 &metatask->taskinfo.bbrread.sense_data, 2814 MIN(sizeof(bbr_info->sense_data), 2815 sizeof(metatask->taskinfo.bbrread.sense_data))); 2816 2817 cfi_free_metatask(metatask); 2818 2819 mtx_destroy(&bbr_mtx); 2820 cv_destroy(&fe_bbr_info.sem); 2821 2822 break; 2823 } 2824 case CTL_DELAY_IO: { 2825 struct ctl_io_delay_info *delay_info; 2826 #ifdef CTL_IO_DELAY 2827 struct ctl_lun *lun; 2828 #endif /* CTL_IO_DELAY */ 2829 2830 delay_info = (struct ctl_io_delay_info *)addr; 2831 2832 #ifdef CTL_IO_DELAY 2833 mtx_lock(&softc->ctl_lock); 2834 2835 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2836 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2837 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2838 } else { 2839 lun = softc->ctl_luns[delay_info->lun_id]; 2840 mtx_lock(&lun->lun_lock); 2841 2842 delay_info->status = CTL_DELAY_STATUS_OK; 2843 2844 switch (delay_info->delay_type) { 2845 case CTL_DELAY_TYPE_CONT: 2846 break; 2847 case CTL_DELAY_TYPE_ONESHOT: 2848 break; 2849 default: 2850 delay_info->status = 2851 CTL_DELAY_STATUS_INVALID_TYPE; 2852 break; 2853 } 2854 2855 switch (delay_info->delay_loc) { 2856 case CTL_DELAY_LOC_DATAMOVE: 2857 lun->delay_info.datamove_type = 2858 delay_info->delay_type; 2859 lun->delay_info.datamove_delay = 2860 delay_info->delay_secs; 2861 break; 2862 case CTL_DELAY_LOC_DONE: 2863 lun->delay_info.done_type = 2864 delay_info->delay_type; 2865 lun->delay_info.done_delay = 2866 delay_info->delay_secs; 2867 break; 2868 default: 2869 delay_info->status = 2870 CTL_DELAY_STATUS_INVALID_LOC; 2871 break; 2872 } 2873 mtx_unlock(&lun->lun_lock); 2874 } 2875 2876 mtx_unlock(&softc->ctl_lock); 2877 #else 2878 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2879 #endif /* CTL_IO_DELAY */ 2880 break; 2881 } 2882 case CTL_REALSYNC_SET: { 2883 int *syncstate; 2884 2885 syncstate = (int *)addr; 2886 2887 mtx_lock(&softc->ctl_lock); 2888 switch (*syncstate) { 2889 case 0: 2890 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2891 break; 2892 case 1: 2893 softc->flags |= CTL_FLAG_REAL_SYNC; 2894 break; 2895 default: 2896 retval = EINVAL; 2897 break; 2898 } 2899 mtx_unlock(&softc->ctl_lock); 2900 break; 2901 } 2902 case CTL_REALSYNC_GET: { 2903 int *syncstate; 2904 2905 syncstate = (int*)addr; 2906 2907 mtx_lock(&softc->ctl_lock); 2908 if (softc->flags & CTL_FLAG_REAL_SYNC) 2909 *syncstate = 1; 2910 else 2911 *syncstate = 0; 2912 mtx_unlock(&softc->ctl_lock); 2913 2914 break; 2915 } 2916 case CTL_SETSYNC: 2917 case CTL_GETSYNC: { 2918 struct ctl_sync_info *sync_info; 2919 struct ctl_lun *lun; 2920 2921 sync_info = (struct ctl_sync_info *)addr; 2922 2923 mtx_lock(&softc->ctl_lock); 2924 lun = softc->ctl_luns[sync_info->lun_id]; 2925 if (lun == NULL) { 2926 mtx_unlock(&softc->ctl_lock); 2927 sync_info->status = CTL_GS_SYNC_NO_LUN; 2928 } 2929 /* 2930 * Get or set the sync interval. We're not bounds checking 2931 * in the set case, hopefully the user won't do something 2932 * silly. 2933 */ 2934 mtx_lock(&lun->lun_lock); 2935 mtx_unlock(&softc->ctl_lock); 2936 if (cmd == CTL_GETSYNC) 2937 sync_info->sync_interval = lun->sync_interval; 2938 else 2939 lun->sync_interval = sync_info->sync_interval; 2940 mtx_unlock(&lun->lun_lock); 2941 2942 sync_info->status = CTL_GS_SYNC_OK; 2943 2944 break; 2945 } 2946 case CTL_GETSTATS: { 2947 struct ctl_stats *stats; 2948 struct ctl_lun *lun; 2949 int i; 2950 2951 stats = (struct ctl_stats *)addr; 2952 2953 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2954 stats->alloc_len) { 2955 stats->status = CTL_SS_NEED_MORE_SPACE; 2956 stats->num_luns = softc->num_luns; 2957 break; 2958 } 2959 /* 2960 * XXX KDM no locking here. If the LUN list changes, 2961 * things can blow up. 2962 */ 2963 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2964 i++, lun = STAILQ_NEXT(lun, links)) { 2965 retval = copyout(&lun->stats, &stats->lun_stats[i], 2966 sizeof(lun->stats)); 2967 if (retval != 0) 2968 break; 2969 } 2970 stats->num_luns = softc->num_luns; 2971 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2972 softc->num_luns; 2973 stats->status = CTL_SS_OK; 2974 #ifdef CTL_TIME_IO 2975 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2976 #else 2977 stats->flags = CTL_STATS_FLAG_NONE; 2978 #endif 2979 getnanouptime(&stats->timestamp); 2980 break; 2981 } 2982 case CTL_ERROR_INJECT: { 2983 struct ctl_error_desc *err_desc, *new_err_desc; 2984 struct ctl_lun *lun; 2985 2986 err_desc = (struct ctl_error_desc *)addr; 2987 2988 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2989 M_WAITOK | M_ZERO); 2990 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2991 2992 mtx_lock(&softc->ctl_lock); 2993 lun = softc->ctl_luns[err_desc->lun_id]; 2994 if (lun == NULL) { 2995 mtx_unlock(&softc->ctl_lock); 2996 free(new_err_desc, M_CTL); 2997 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2998 __func__, (uintmax_t)err_desc->lun_id); 2999 retval = EINVAL; 3000 break; 3001 } 3002 mtx_lock(&lun->lun_lock); 3003 mtx_unlock(&softc->ctl_lock); 3004 3005 /* 3006 * We could do some checking here to verify the validity 3007 * of the request, but given the complexity of error 3008 * injection requests, the checking logic would be fairly 3009 * complex. 3010 * 3011 * For now, if the request is invalid, it just won't get 3012 * executed and might get deleted. 3013 */ 3014 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 3015 3016 /* 3017 * XXX KDM check to make sure the serial number is unique, 3018 * in case we somehow manage to wrap. That shouldn't 3019 * happen for a very long time, but it's the right thing to 3020 * do. 3021 */ 3022 new_err_desc->serial = lun->error_serial; 3023 err_desc->serial = lun->error_serial; 3024 lun->error_serial++; 3025 3026 mtx_unlock(&lun->lun_lock); 3027 break; 3028 } 3029 case CTL_ERROR_INJECT_DELETE: { 3030 struct ctl_error_desc *delete_desc, *desc, *desc2; 3031 struct ctl_lun *lun; 3032 int delete_done; 3033 3034 delete_desc = (struct ctl_error_desc *)addr; 3035 delete_done = 0; 3036 3037 mtx_lock(&softc->ctl_lock); 3038 lun = softc->ctl_luns[delete_desc->lun_id]; 3039 if (lun == NULL) { 3040 mtx_unlock(&softc->ctl_lock); 3041 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 3042 __func__, (uintmax_t)delete_desc->lun_id); 3043 retval = EINVAL; 3044 break; 3045 } 3046 mtx_lock(&lun->lun_lock); 3047 mtx_unlock(&softc->ctl_lock); 3048 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 3049 if (desc->serial != delete_desc->serial) 3050 continue; 3051 3052 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 3053 links); 3054 free(desc, M_CTL); 3055 delete_done = 1; 3056 } 3057 mtx_unlock(&lun->lun_lock); 3058 if (delete_done == 0) { 3059 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 3060 "error serial %ju on LUN %u\n", __func__, 3061 delete_desc->serial, delete_desc->lun_id); 3062 retval = EINVAL; 3063 break; 3064 } 3065 break; 3066 } 3067 case CTL_DUMP_STRUCTS: { 3068 int i, j, k; 3069 struct ctl_port *port; 3070 struct ctl_frontend *fe; 3071 3072 mtx_lock(&softc->ctl_lock); 3073 printf("CTL Persistent Reservation information start:\n"); 3074 for (i = 0; i < CTL_MAX_LUNS; i++) { 3075 struct ctl_lun *lun; 3076 3077 lun = softc->ctl_luns[i]; 3078 3079 if ((lun == NULL) 3080 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3081 continue; 3082 3083 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3084 if (lun->pr_keys[j] == NULL) 3085 continue; 3086 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3087 if (lun->pr_keys[j][k] == 0) 3088 continue; 3089 printf(" LUN %d port %d iid %d key " 3090 "%#jx\n", i, j, k, 3091 (uintmax_t)lun->pr_keys[j][k]); 3092 } 3093 } 3094 } 3095 printf("CTL Persistent Reservation information end\n"); 3096 printf("CTL Ports:\n"); 3097 STAILQ_FOREACH(port, &softc->port_list, links) { 3098 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3099 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3100 port->frontend->name, port->port_type, 3101 port->physical_port, port->virtual_port, 3102 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3103 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3104 if (port->wwpn_iid[j].in_use == 0 && 3105 port->wwpn_iid[j].wwpn == 0 && 3106 port->wwpn_iid[j].name == NULL) 3107 continue; 3108 3109 printf(" iid %u use %d WWPN %#jx '%s'\n", 3110 j, port->wwpn_iid[j].in_use, 3111 (uintmax_t)port->wwpn_iid[j].wwpn, 3112 port->wwpn_iid[j].name); 3113 } 3114 } 3115 printf("CTL Port information end\n"); 3116 mtx_unlock(&softc->ctl_lock); 3117 /* 3118 * XXX KDM calling this without a lock. We'd likely want 3119 * to drop the lock before calling the frontend's dump 3120 * routine anyway. 3121 */ 3122 printf("CTL Frontends:\n"); 3123 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3124 printf(" Frontend '%s'\n", fe->name); 3125 if (fe->fe_dump != NULL) 3126 fe->fe_dump(); 3127 } 3128 printf("CTL Frontend information end\n"); 3129 break; 3130 } 3131 case CTL_LUN_REQ: { 3132 struct ctl_lun_req *lun_req; 3133 struct ctl_backend_driver *backend; 3134 3135 lun_req = (struct ctl_lun_req *)addr; 3136 3137 backend = ctl_backend_find(lun_req->backend); 3138 if (backend == NULL) { 3139 lun_req->status = CTL_LUN_ERROR; 3140 snprintf(lun_req->error_str, 3141 sizeof(lun_req->error_str), 3142 "Backend \"%s\" not found.", 3143 lun_req->backend); 3144 break; 3145 } 3146 if (lun_req->num_be_args > 0) { 3147 lun_req->kern_be_args = ctl_copyin_args( 3148 lun_req->num_be_args, 3149 lun_req->be_args, 3150 lun_req->error_str, 3151 sizeof(lun_req->error_str)); 3152 if (lun_req->kern_be_args == NULL) { 3153 lun_req->status = CTL_LUN_ERROR; 3154 break; 3155 } 3156 } 3157 3158 retval = backend->ioctl(dev, cmd, addr, flag, td); 3159 3160 if (lun_req->num_be_args > 0) { 3161 ctl_copyout_args(lun_req->num_be_args, 3162 lun_req->kern_be_args); 3163 ctl_free_args(lun_req->num_be_args, 3164 lun_req->kern_be_args); 3165 } 3166 break; 3167 } 3168 case CTL_LUN_LIST: { 3169 struct sbuf *sb; 3170 struct ctl_lun *lun; 3171 struct ctl_lun_list *list; 3172 struct ctl_option *opt; 3173 3174 list = (struct ctl_lun_list *)addr; 3175 3176 /* 3177 * Allocate a fixed length sbuf here, based on the length 3178 * of the user's buffer. We could allocate an auto-extending 3179 * buffer, and then tell the user how much larger our 3180 * amount of data is than his buffer, but that presents 3181 * some problems: 3182 * 3183 * 1. The sbuf(9) routines use a blocking malloc, and so 3184 * we can't hold a lock while calling them with an 3185 * auto-extending buffer. 3186 * 3187 * 2. There is not currently a LUN reference counting 3188 * mechanism, outside of outstanding transactions on 3189 * the LUN's OOA queue. So a LUN could go away on us 3190 * while we're getting the LUN number, backend-specific 3191 * information, etc. Thus, given the way things 3192 * currently work, we need to hold the CTL lock while 3193 * grabbing LUN information. 3194 * 3195 * So, from the user's standpoint, the best thing to do is 3196 * allocate what he thinks is a reasonable buffer length, 3197 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3198 * double the buffer length and try again. (And repeat 3199 * that until he succeeds.) 3200 */ 3201 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3202 if (sb == NULL) { 3203 list->status = CTL_LUN_LIST_ERROR; 3204 snprintf(list->error_str, sizeof(list->error_str), 3205 "Unable to allocate %d bytes for LUN list", 3206 list->alloc_len); 3207 break; 3208 } 3209 3210 sbuf_printf(sb, "<ctllunlist>\n"); 3211 3212 mtx_lock(&softc->ctl_lock); 3213 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3214 mtx_lock(&lun->lun_lock); 3215 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3216 (uintmax_t)lun->lun); 3217 3218 /* 3219 * Bail out as soon as we see that we've overfilled 3220 * the buffer. 3221 */ 3222 if (retval != 0) 3223 break; 3224 3225 retval = sbuf_printf(sb, "\t<backend_type>%s" 3226 "</backend_type>\n", 3227 (lun->backend == NULL) ? "none" : 3228 lun->backend->name); 3229 3230 if (retval != 0) 3231 break; 3232 3233 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3234 lun->be_lun->lun_type); 3235 3236 if (retval != 0) 3237 break; 3238 3239 if (lun->backend == NULL) { 3240 retval = sbuf_printf(sb, "</lun>\n"); 3241 if (retval != 0) 3242 break; 3243 continue; 3244 } 3245 3246 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3247 (lun->be_lun->maxlba > 0) ? 3248 lun->be_lun->maxlba + 1 : 0); 3249 3250 if (retval != 0) 3251 break; 3252 3253 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3254 lun->be_lun->blocksize); 3255 3256 if (retval != 0) 3257 break; 3258 3259 retval = sbuf_printf(sb, "\t<serial_number>"); 3260 3261 if (retval != 0) 3262 break; 3263 3264 retval = ctl_sbuf_printf_esc(sb, 3265 lun->be_lun->serial_num, 3266 sizeof(lun->be_lun->serial_num)); 3267 3268 if (retval != 0) 3269 break; 3270 3271 retval = sbuf_printf(sb, "</serial_number>\n"); 3272 3273 if (retval != 0) 3274 break; 3275 3276 retval = sbuf_printf(sb, "\t<device_id>"); 3277 3278 if (retval != 0) 3279 break; 3280 3281 retval = ctl_sbuf_printf_esc(sb, 3282 lun->be_lun->device_id, 3283 sizeof(lun->be_lun->device_id)); 3284 3285 if (retval != 0) 3286 break; 3287 3288 retval = sbuf_printf(sb, "</device_id>\n"); 3289 3290 if (retval != 0) 3291 break; 3292 3293 if (lun->backend->lun_info != NULL) { 3294 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3295 if (retval != 0) 3296 break; 3297 } 3298 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3299 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3300 opt->name, opt->value, opt->name); 3301 if (retval != 0) 3302 break; 3303 } 3304 3305 retval = sbuf_printf(sb, "</lun>\n"); 3306 3307 if (retval != 0) 3308 break; 3309 mtx_unlock(&lun->lun_lock); 3310 } 3311 if (lun != NULL) 3312 mtx_unlock(&lun->lun_lock); 3313 mtx_unlock(&softc->ctl_lock); 3314 3315 if ((retval != 0) 3316 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3317 retval = 0; 3318 sbuf_delete(sb); 3319 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3320 snprintf(list->error_str, sizeof(list->error_str), 3321 "Out of space, %d bytes is too small", 3322 list->alloc_len); 3323 break; 3324 } 3325 3326 sbuf_finish(sb); 3327 3328 retval = copyout(sbuf_data(sb), list->lun_xml, 3329 sbuf_len(sb) + 1); 3330 3331 list->fill_len = sbuf_len(sb) + 1; 3332 list->status = CTL_LUN_LIST_OK; 3333 sbuf_delete(sb); 3334 break; 3335 } 3336 case CTL_ISCSI: { 3337 struct ctl_iscsi *ci; 3338 struct ctl_frontend *fe; 3339 3340 ci = (struct ctl_iscsi *)addr; 3341 3342 fe = ctl_frontend_find("iscsi"); 3343 if (fe == NULL) { 3344 ci->status = CTL_ISCSI_ERROR; 3345 snprintf(ci->error_str, sizeof(ci->error_str), 3346 "Frontend \"iscsi\" not found."); 3347 break; 3348 } 3349 3350 retval = fe->ioctl(dev, cmd, addr, flag, td); 3351 break; 3352 } 3353 case CTL_PORT_REQ: { 3354 struct ctl_req *req; 3355 struct ctl_frontend *fe; 3356 3357 req = (struct ctl_req *)addr; 3358 3359 fe = ctl_frontend_find(req->driver); 3360 if (fe == NULL) { 3361 req->status = CTL_LUN_ERROR; 3362 snprintf(req->error_str, sizeof(req->error_str), 3363 "Frontend \"%s\" not found.", req->driver); 3364 break; 3365 } 3366 if (req->num_args > 0) { 3367 req->kern_args = ctl_copyin_args(req->num_args, 3368 req->args, req->error_str, sizeof(req->error_str)); 3369 if (req->kern_args == NULL) { 3370 req->status = CTL_LUN_ERROR; 3371 break; 3372 } 3373 } 3374 3375 retval = fe->ioctl(dev, cmd, addr, flag, td); 3376 3377 if (req->num_args > 0) { 3378 ctl_copyout_args(req->num_args, req->kern_args); 3379 ctl_free_args(req->num_args, req->kern_args); 3380 } 3381 break; 3382 } 3383 case CTL_PORT_LIST: { 3384 struct sbuf *sb; 3385 struct ctl_port *port; 3386 struct ctl_lun_list *list; 3387 struct ctl_option *opt; 3388 int j; 3389 uint32_t plun; 3390 3391 list = (struct ctl_lun_list *)addr; 3392 3393 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3394 if (sb == NULL) { 3395 list->status = CTL_LUN_LIST_ERROR; 3396 snprintf(list->error_str, sizeof(list->error_str), 3397 "Unable to allocate %d bytes for LUN list", 3398 list->alloc_len); 3399 break; 3400 } 3401 3402 sbuf_printf(sb, "<ctlportlist>\n"); 3403 3404 mtx_lock(&softc->ctl_lock); 3405 STAILQ_FOREACH(port, &softc->port_list, links) { 3406 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3407 (uintmax_t)port->targ_port); 3408 3409 /* 3410 * Bail out as soon as we see that we've overfilled 3411 * the buffer. 3412 */ 3413 if (retval != 0) 3414 break; 3415 3416 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3417 "</frontend_type>\n", port->frontend->name); 3418 if (retval != 0) 3419 break; 3420 3421 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3422 port->port_type); 3423 if (retval != 0) 3424 break; 3425 3426 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3427 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3428 if (retval != 0) 3429 break; 3430 3431 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3432 port->port_name); 3433 if (retval != 0) 3434 break; 3435 3436 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3437 port->physical_port); 3438 if (retval != 0) 3439 break; 3440 3441 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3442 port->virtual_port); 3443 if (retval != 0) 3444 break; 3445 3446 if (port->target_devid != NULL) { 3447 sbuf_printf(sb, "\t<target>"); 3448 ctl_id_sbuf(port->target_devid, sb); 3449 sbuf_printf(sb, "</target>\n"); 3450 } 3451 3452 if (port->port_devid != NULL) { 3453 sbuf_printf(sb, "\t<port>"); 3454 ctl_id_sbuf(port->port_devid, sb); 3455 sbuf_printf(sb, "</port>\n"); 3456 } 3457 3458 if (port->port_info != NULL) { 3459 retval = port->port_info(port->onoff_arg, sb); 3460 if (retval != 0) 3461 break; 3462 } 3463 STAILQ_FOREACH(opt, &port->options, links) { 3464 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3465 opt->name, opt->value, opt->name); 3466 if (retval != 0) 3467 break; 3468 } 3469 3470 if (port->lun_map != NULL) { 3471 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3472 for (j = 0; j < CTL_MAX_LUNS; j++) { 3473 plun = ctl_lun_map_from_port(port, j); 3474 if (plun >= CTL_MAX_LUNS) 3475 continue; 3476 sbuf_printf(sb, 3477 "\t<lun id=\"%u\">%u</lun>\n", 3478 j, plun); 3479 } 3480 } 3481 3482 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3483 if (port->wwpn_iid[j].in_use == 0 || 3484 (port->wwpn_iid[j].wwpn == 0 && 3485 port->wwpn_iid[j].name == NULL)) 3486 continue; 3487 3488 if (port->wwpn_iid[j].name != NULL) 3489 retval = sbuf_printf(sb, 3490 "\t<initiator id=\"%u\">%s</initiator>\n", 3491 j, port->wwpn_iid[j].name); 3492 else 3493 retval = sbuf_printf(sb, 3494 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3495 j, port->wwpn_iid[j].wwpn); 3496 if (retval != 0) 3497 break; 3498 } 3499 if (retval != 0) 3500 break; 3501 3502 retval = sbuf_printf(sb, "</targ_port>\n"); 3503 if (retval != 0) 3504 break; 3505 } 3506 mtx_unlock(&softc->ctl_lock); 3507 3508 if ((retval != 0) 3509 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3510 retval = 0; 3511 sbuf_delete(sb); 3512 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3513 snprintf(list->error_str, sizeof(list->error_str), 3514 "Out of space, %d bytes is too small", 3515 list->alloc_len); 3516 break; 3517 } 3518 3519 sbuf_finish(sb); 3520 3521 retval = copyout(sbuf_data(sb), list->lun_xml, 3522 sbuf_len(sb) + 1); 3523 3524 list->fill_len = sbuf_len(sb) + 1; 3525 list->status = CTL_LUN_LIST_OK; 3526 sbuf_delete(sb); 3527 break; 3528 } 3529 case CTL_LUN_MAP: { 3530 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3531 struct ctl_port *port; 3532 3533 mtx_lock(&softc->ctl_lock); 3534 if (lm->port >= CTL_MAX_PORTS || 3535 (port = softc->ctl_ports[lm->port]) == NULL) { 3536 mtx_unlock(&softc->ctl_lock); 3537 return (ENXIO); 3538 } 3539 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3540 if (lm->plun < CTL_MAX_LUNS) { 3541 if (lm->lun == UINT32_MAX) 3542 retval = ctl_lun_map_unset(port, lm->plun); 3543 else if (lm->lun < CTL_MAX_LUNS && 3544 softc->ctl_luns[lm->lun] != NULL) 3545 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3546 else 3547 return (ENXIO); 3548 } else if (lm->plun == UINT32_MAX) { 3549 if (lm->lun == UINT32_MAX) 3550 retval = ctl_lun_map_deinit(port); 3551 else 3552 retval = ctl_lun_map_init(port); 3553 } else 3554 return (ENXIO); 3555 break; 3556 } 3557 default: { 3558 /* XXX KDM should we fix this? */ 3559 #if 0 3560 struct ctl_backend_driver *backend; 3561 unsigned int type; 3562 int found; 3563 3564 found = 0; 3565 3566 /* 3567 * We encode the backend type as the ioctl type for backend 3568 * ioctls. So parse it out here, and then search for a 3569 * backend of this type. 3570 */ 3571 type = _IOC_TYPE(cmd); 3572 3573 STAILQ_FOREACH(backend, &softc->be_list, links) { 3574 if (backend->type == type) { 3575 found = 1; 3576 break; 3577 } 3578 } 3579 if (found == 0) { 3580 printf("ctl: unknown ioctl command %#lx or backend " 3581 "%d\n", cmd, type); 3582 retval = EINVAL; 3583 break; 3584 } 3585 retval = backend->ioctl(dev, cmd, addr, flag, td); 3586 #endif 3587 retval = ENOTTY; 3588 break; 3589 } 3590 } 3591 return (retval); 3592 } 3593 3594 uint32_t 3595 ctl_get_initindex(struct ctl_nexus *nexus) 3596 { 3597 if (nexus->targ_port < CTL_MAX_PORTS) 3598 return (nexus->initid.id + 3599 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3600 else 3601 return (nexus->initid.id + 3602 ((nexus->targ_port - CTL_MAX_PORTS) * 3603 CTL_MAX_INIT_PER_PORT)); 3604 } 3605 3606 uint32_t 3607 ctl_get_resindex(struct ctl_nexus *nexus) 3608 { 3609 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3610 } 3611 3612 uint32_t 3613 ctl_port_idx(int port_num) 3614 { 3615 if (port_num < CTL_MAX_PORTS) 3616 return(port_num); 3617 else 3618 return(port_num - CTL_MAX_PORTS); 3619 } 3620 3621 int 3622 ctl_lun_map_init(struct ctl_port *port) 3623 { 3624 struct ctl_softc *softc = control_softc; 3625 struct ctl_lun *lun; 3626 uint32_t i; 3627 3628 if (port->lun_map == NULL) 3629 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3630 M_CTL, M_NOWAIT); 3631 if (port->lun_map == NULL) 3632 return (ENOMEM); 3633 for (i = 0; i < CTL_MAX_LUNS; i++) 3634 port->lun_map[i] = UINT32_MAX; 3635 if (port->status & CTL_PORT_STATUS_ONLINE) { 3636 STAILQ_FOREACH(lun, &softc->lun_list, links) 3637 port->lun_disable(port->targ_lun_arg, lun->lun); 3638 } 3639 return (0); 3640 } 3641 3642 int 3643 ctl_lun_map_deinit(struct ctl_port *port) 3644 { 3645 struct ctl_softc *softc = control_softc; 3646 struct ctl_lun *lun; 3647 3648 if (port->lun_map == NULL) 3649 return (0); 3650 free(port->lun_map, M_CTL); 3651 port->lun_map = NULL; 3652 if (port->status & CTL_PORT_STATUS_ONLINE) { 3653 STAILQ_FOREACH(lun, &softc->lun_list, links) 3654 port->lun_enable(port->targ_lun_arg, lun->lun); 3655 } 3656 return (0); 3657 } 3658 3659 int 3660 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3661 { 3662 int status; 3663 uint32_t old; 3664 3665 if (port->lun_map == NULL) { 3666 status = ctl_lun_map_init(port); 3667 if (status != 0) 3668 return (status); 3669 } 3670 old = port->lun_map[plun]; 3671 port->lun_map[plun] = glun; 3672 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) 3673 port->lun_enable(port->targ_lun_arg, plun); 3674 return (0); 3675 } 3676 3677 int 3678 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3679 { 3680 uint32_t old; 3681 3682 if (port->lun_map == NULL) 3683 return (0); 3684 old = port->lun_map[plun]; 3685 port->lun_map[plun] = UINT32_MAX; 3686 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) 3687 port->lun_disable(port->targ_lun_arg, plun); 3688 return (0); 3689 } 3690 3691 uint32_t 3692 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3693 { 3694 3695 if (port == NULL) 3696 return (UINT32_MAX); 3697 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3698 return (lun_id); 3699 return (port->lun_map[lun_id]); 3700 } 3701 3702 uint32_t 3703 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3704 { 3705 uint32_t i; 3706 3707 if (port == NULL) 3708 return (UINT32_MAX); 3709 if (port->lun_map == NULL) 3710 return (lun_id); 3711 for (i = 0; i < CTL_MAX_LUNS; i++) { 3712 if (port->lun_map[i] == lun_id) 3713 return (i); 3714 } 3715 return (UINT32_MAX); 3716 } 3717 3718 static struct ctl_port * 3719 ctl_io_port(struct ctl_io_hdr *io_hdr) 3720 { 3721 int port_num; 3722 3723 port_num = io_hdr->nexus.targ_port; 3724 return (control_softc->ctl_ports[ctl_port_idx(port_num)]); 3725 } 3726 3727 /* 3728 * Note: This only works for bitmask sizes that are at least 32 bits, and 3729 * that are a power of 2. 3730 */ 3731 int 3732 ctl_ffz(uint32_t *mask, uint32_t size) 3733 { 3734 uint32_t num_chunks, num_pieces; 3735 int i, j; 3736 3737 num_chunks = (size >> 5); 3738 if (num_chunks == 0) 3739 num_chunks++; 3740 num_pieces = MIN((sizeof(uint32_t) * 8), size); 3741 3742 for (i = 0; i < num_chunks; i++) { 3743 for (j = 0; j < num_pieces; j++) { 3744 if ((mask[i] & (1 << j)) == 0) 3745 return ((i << 5) + j); 3746 } 3747 } 3748 3749 return (-1); 3750 } 3751 3752 int 3753 ctl_set_mask(uint32_t *mask, uint32_t bit) 3754 { 3755 uint32_t chunk, piece; 3756 3757 chunk = bit >> 5; 3758 piece = bit % (sizeof(uint32_t) * 8); 3759 3760 if ((mask[chunk] & (1 << piece)) != 0) 3761 return (-1); 3762 else 3763 mask[chunk] |= (1 << piece); 3764 3765 return (0); 3766 } 3767 3768 int 3769 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3770 { 3771 uint32_t chunk, piece; 3772 3773 chunk = bit >> 5; 3774 piece = bit % (sizeof(uint32_t) * 8); 3775 3776 if ((mask[chunk] & (1 << piece)) == 0) 3777 return (-1); 3778 else 3779 mask[chunk] &= ~(1 << piece); 3780 3781 return (0); 3782 } 3783 3784 int 3785 ctl_is_set(uint32_t *mask, uint32_t bit) 3786 { 3787 uint32_t chunk, piece; 3788 3789 chunk = bit >> 5; 3790 piece = bit % (sizeof(uint32_t) * 8); 3791 3792 if ((mask[chunk] & (1 << piece)) == 0) 3793 return (0); 3794 else 3795 return (1); 3796 } 3797 3798 static uint64_t 3799 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3800 { 3801 uint64_t *t; 3802 3803 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3804 if (t == NULL) 3805 return (0); 3806 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3807 } 3808 3809 static void 3810 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3811 { 3812 uint64_t *t; 3813 3814 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3815 if (t == NULL) 3816 return; 3817 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3818 } 3819 3820 static void 3821 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3822 { 3823 uint64_t *p; 3824 u_int i; 3825 3826 i = residx/CTL_MAX_INIT_PER_PORT; 3827 if (lun->pr_keys[i] != NULL) 3828 return; 3829 mtx_unlock(&lun->lun_lock); 3830 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3831 M_WAITOK | M_ZERO); 3832 mtx_lock(&lun->lun_lock); 3833 if (lun->pr_keys[i] == NULL) 3834 lun->pr_keys[i] = p; 3835 else 3836 free(p, M_CTL); 3837 } 3838 3839 static void 3840 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3841 { 3842 uint64_t *t; 3843 3844 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3845 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3846 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3847 } 3848 3849 /* 3850 * ctl_softc, pool_name, total_ctl_io are passed in. 3851 * npool is passed out. 3852 */ 3853 int 3854 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3855 uint32_t total_ctl_io, void **npool) 3856 { 3857 #ifdef IO_POOLS 3858 struct ctl_io_pool *pool; 3859 3860 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3861 M_NOWAIT | M_ZERO); 3862 if (pool == NULL) 3863 return (ENOMEM); 3864 3865 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3866 pool->ctl_softc = ctl_softc; 3867 pool->zone = uma_zsecond_create(pool->name, NULL, 3868 NULL, NULL, NULL, ctl_softc->io_zone); 3869 /* uma_prealloc(pool->zone, total_ctl_io); */ 3870 3871 *npool = pool; 3872 #else 3873 *npool = ctl_softc->io_zone; 3874 #endif 3875 return (0); 3876 } 3877 3878 void 3879 ctl_pool_free(struct ctl_io_pool *pool) 3880 { 3881 3882 if (pool == NULL) 3883 return; 3884 3885 #ifdef IO_POOLS 3886 uma_zdestroy(pool->zone); 3887 free(pool, M_CTL); 3888 #endif 3889 } 3890 3891 union ctl_io * 3892 ctl_alloc_io(void *pool_ref) 3893 { 3894 union ctl_io *io; 3895 #ifdef IO_POOLS 3896 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3897 3898 io = uma_zalloc(pool->zone, M_WAITOK); 3899 #else 3900 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3901 #endif 3902 if (io != NULL) 3903 io->io_hdr.pool = pool_ref; 3904 return (io); 3905 } 3906 3907 union ctl_io * 3908 ctl_alloc_io_nowait(void *pool_ref) 3909 { 3910 union ctl_io *io; 3911 #ifdef IO_POOLS 3912 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3913 3914 io = uma_zalloc(pool->zone, M_NOWAIT); 3915 #else 3916 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3917 #endif 3918 if (io != NULL) 3919 io->io_hdr.pool = pool_ref; 3920 return (io); 3921 } 3922 3923 void 3924 ctl_free_io(union ctl_io *io) 3925 { 3926 #ifdef IO_POOLS 3927 struct ctl_io_pool *pool; 3928 #endif 3929 3930 if (io == NULL) 3931 return; 3932 3933 #ifdef IO_POOLS 3934 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3935 uma_zfree(pool->zone, io); 3936 #else 3937 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3938 #endif 3939 } 3940 3941 void 3942 ctl_zero_io(union ctl_io *io) 3943 { 3944 void *pool_ref; 3945 3946 if (io == NULL) 3947 return; 3948 3949 /* 3950 * May need to preserve linked list pointers at some point too. 3951 */ 3952 pool_ref = io->io_hdr.pool; 3953 memset(io, 0, sizeof(*io)); 3954 io->io_hdr.pool = pool_ref; 3955 } 3956 3957 /* 3958 * This routine is currently used for internal copies of ctl_ios that need 3959 * to persist for some reason after we've already returned status to the 3960 * FETD. (Thus the flag set.) 3961 * 3962 * XXX XXX 3963 * Note that this makes a blind copy of all fields in the ctl_io, except 3964 * for the pool reference. This includes any memory that has been 3965 * allocated! That memory will no longer be valid after done has been 3966 * called, so this would be VERY DANGEROUS for command that actually does 3967 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3968 * start and stop commands, which don't transfer any data, so this is not a 3969 * problem. If it is used for anything else, the caller would also need to 3970 * allocate data buffer space and this routine would need to be modified to 3971 * copy the data buffer(s) as well. 3972 */ 3973 void 3974 ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3975 { 3976 void *pool_ref; 3977 3978 if ((src == NULL) 3979 || (dest == NULL)) 3980 return; 3981 3982 /* 3983 * May need to preserve linked list pointers at some point too. 3984 */ 3985 pool_ref = dest->io_hdr.pool; 3986 3987 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest))); 3988 3989 dest->io_hdr.pool = pool_ref; 3990 /* 3991 * We need to know that this is an internal copy, and doesn't need 3992 * to get passed back to the FETD that allocated it. 3993 */ 3994 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3995 } 3996 3997 int 3998 ctl_expand_number(const char *buf, uint64_t *num) 3999 { 4000 char *endptr; 4001 uint64_t number; 4002 unsigned shift; 4003 4004 number = strtoq(buf, &endptr, 0); 4005 4006 switch (tolower((unsigned char)*endptr)) { 4007 case 'e': 4008 shift = 60; 4009 break; 4010 case 'p': 4011 shift = 50; 4012 break; 4013 case 't': 4014 shift = 40; 4015 break; 4016 case 'g': 4017 shift = 30; 4018 break; 4019 case 'm': 4020 shift = 20; 4021 break; 4022 case 'k': 4023 shift = 10; 4024 break; 4025 case 'b': 4026 case '\0': /* No unit. */ 4027 *num = number; 4028 return (0); 4029 default: 4030 /* Unrecognized unit. */ 4031 return (-1); 4032 } 4033 4034 if ((number << shift) >> shift != number) { 4035 /* Overflow */ 4036 return (-1); 4037 } 4038 *num = number << shift; 4039 return (0); 4040 } 4041 4042 4043 /* 4044 * This routine could be used in the future to load default and/or saved 4045 * mode page parameters for a particuar lun. 4046 */ 4047 static int 4048 ctl_init_page_index(struct ctl_lun *lun) 4049 { 4050 int i; 4051 struct ctl_page_index *page_index; 4052 const char *value; 4053 uint64_t ival; 4054 4055 memcpy(&lun->mode_pages.index, page_index_template, 4056 sizeof(page_index_template)); 4057 4058 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4059 4060 page_index = &lun->mode_pages.index[i]; 4061 /* 4062 * If this is a disk-only mode page, there's no point in 4063 * setting it up. For some pages, we have to have some 4064 * basic information about the disk in order to calculate the 4065 * mode page data. 4066 */ 4067 if ((lun->be_lun->lun_type != T_DIRECT) 4068 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4069 continue; 4070 4071 switch (page_index->page_code & SMPH_PC_MASK) { 4072 case SMS_RW_ERROR_RECOVERY_PAGE: { 4073 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4074 panic("subpage is incorrect!"); 4075 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4076 &rw_er_page_default, 4077 sizeof(rw_er_page_default)); 4078 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4079 &rw_er_page_changeable, 4080 sizeof(rw_er_page_changeable)); 4081 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4082 &rw_er_page_default, 4083 sizeof(rw_er_page_default)); 4084 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4085 &rw_er_page_default, 4086 sizeof(rw_er_page_default)); 4087 page_index->page_data = 4088 (uint8_t *)lun->mode_pages.rw_er_page; 4089 break; 4090 } 4091 case SMS_FORMAT_DEVICE_PAGE: { 4092 struct scsi_format_page *format_page; 4093 4094 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4095 panic("subpage is incorrect!"); 4096 4097 /* 4098 * Sectors per track are set above. Bytes per 4099 * sector need to be set here on a per-LUN basis. 4100 */ 4101 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4102 &format_page_default, 4103 sizeof(format_page_default)); 4104 memcpy(&lun->mode_pages.format_page[ 4105 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4106 sizeof(format_page_changeable)); 4107 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4108 &format_page_default, 4109 sizeof(format_page_default)); 4110 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4111 &format_page_default, 4112 sizeof(format_page_default)); 4113 4114 format_page = &lun->mode_pages.format_page[ 4115 CTL_PAGE_CURRENT]; 4116 scsi_ulto2b(lun->be_lun->blocksize, 4117 format_page->bytes_per_sector); 4118 4119 format_page = &lun->mode_pages.format_page[ 4120 CTL_PAGE_DEFAULT]; 4121 scsi_ulto2b(lun->be_lun->blocksize, 4122 format_page->bytes_per_sector); 4123 4124 format_page = &lun->mode_pages.format_page[ 4125 CTL_PAGE_SAVED]; 4126 scsi_ulto2b(lun->be_lun->blocksize, 4127 format_page->bytes_per_sector); 4128 4129 page_index->page_data = 4130 (uint8_t *)lun->mode_pages.format_page; 4131 break; 4132 } 4133 case SMS_RIGID_DISK_PAGE: { 4134 struct scsi_rigid_disk_page *rigid_disk_page; 4135 uint32_t sectors_per_cylinder; 4136 uint64_t cylinders; 4137 #ifndef __XSCALE__ 4138 int shift; 4139 #endif /* !__XSCALE__ */ 4140 4141 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4142 panic("invalid subpage value %d", 4143 page_index->subpage); 4144 4145 /* 4146 * Rotation rate and sectors per track are set 4147 * above. We calculate the cylinders here based on 4148 * capacity. Due to the number of heads and 4149 * sectors per track we're using, smaller arrays 4150 * may turn out to have 0 cylinders. Linux and 4151 * FreeBSD don't pay attention to these mode pages 4152 * to figure out capacity, but Solaris does. It 4153 * seems to deal with 0 cylinders just fine, and 4154 * works out a fake geometry based on the capacity. 4155 */ 4156 memcpy(&lun->mode_pages.rigid_disk_page[ 4157 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4158 sizeof(rigid_disk_page_default)); 4159 memcpy(&lun->mode_pages.rigid_disk_page[ 4160 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4161 sizeof(rigid_disk_page_changeable)); 4162 4163 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4164 CTL_DEFAULT_HEADS; 4165 4166 /* 4167 * The divide method here will be more accurate, 4168 * probably, but results in floating point being 4169 * used in the kernel on i386 (__udivdi3()). On the 4170 * XScale, though, __udivdi3() is implemented in 4171 * software. 4172 * 4173 * The shift method for cylinder calculation is 4174 * accurate if sectors_per_cylinder is a power of 4175 * 2. Otherwise it might be slightly off -- you 4176 * might have a bit of a truncation problem. 4177 */ 4178 #ifdef __XSCALE__ 4179 cylinders = (lun->be_lun->maxlba + 1) / 4180 sectors_per_cylinder; 4181 #else 4182 for (shift = 31; shift > 0; shift--) { 4183 if (sectors_per_cylinder & (1 << shift)) 4184 break; 4185 } 4186 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4187 #endif 4188 4189 /* 4190 * We've basically got 3 bytes, or 24 bits for the 4191 * cylinder size in the mode page. If we're over, 4192 * just round down to 2^24. 4193 */ 4194 if (cylinders > 0xffffff) 4195 cylinders = 0xffffff; 4196 4197 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4198 CTL_PAGE_DEFAULT]; 4199 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4200 4201 if ((value = ctl_get_opt(&lun->be_lun->options, 4202 "rpm")) != NULL) { 4203 scsi_ulto2b(strtol(value, NULL, 0), 4204 rigid_disk_page->rotation_rate); 4205 } 4206 4207 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4208 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4209 sizeof(rigid_disk_page_default)); 4210 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4211 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4212 sizeof(rigid_disk_page_default)); 4213 4214 page_index->page_data = 4215 (uint8_t *)lun->mode_pages.rigid_disk_page; 4216 break; 4217 } 4218 case SMS_CACHING_PAGE: { 4219 struct scsi_caching_page *caching_page; 4220 4221 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4222 panic("invalid subpage value %d", 4223 page_index->subpage); 4224 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4225 &caching_page_default, 4226 sizeof(caching_page_default)); 4227 memcpy(&lun->mode_pages.caching_page[ 4228 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4229 sizeof(caching_page_changeable)); 4230 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4231 &caching_page_default, 4232 sizeof(caching_page_default)); 4233 caching_page = &lun->mode_pages.caching_page[ 4234 CTL_PAGE_SAVED]; 4235 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4236 if (value != NULL && strcmp(value, "off") == 0) 4237 caching_page->flags1 &= ~SCP_WCE; 4238 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4239 if (value != NULL && strcmp(value, "off") == 0) 4240 caching_page->flags1 |= SCP_RCD; 4241 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4242 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4243 sizeof(caching_page_default)); 4244 page_index->page_data = 4245 (uint8_t *)lun->mode_pages.caching_page; 4246 break; 4247 } 4248 case SMS_CONTROL_MODE_PAGE: { 4249 struct scsi_control_page *control_page; 4250 4251 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4252 panic("invalid subpage value %d", 4253 page_index->subpage); 4254 4255 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4256 &control_page_default, 4257 sizeof(control_page_default)); 4258 memcpy(&lun->mode_pages.control_page[ 4259 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4260 sizeof(control_page_changeable)); 4261 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4262 &control_page_default, 4263 sizeof(control_page_default)); 4264 control_page = &lun->mode_pages.control_page[ 4265 CTL_PAGE_SAVED]; 4266 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4267 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4268 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4269 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4270 } 4271 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4272 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4273 sizeof(control_page_default)); 4274 page_index->page_data = 4275 (uint8_t *)lun->mode_pages.control_page; 4276 break; 4277 4278 } 4279 case SMS_INFO_EXCEPTIONS_PAGE: { 4280 switch (page_index->subpage) { 4281 case SMS_SUBPAGE_PAGE_0: 4282 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4283 &ie_page_default, 4284 sizeof(ie_page_default)); 4285 memcpy(&lun->mode_pages.ie_page[ 4286 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4287 sizeof(ie_page_changeable)); 4288 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4289 &ie_page_default, 4290 sizeof(ie_page_default)); 4291 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4292 &ie_page_default, 4293 sizeof(ie_page_default)); 4294 page_index->page_data = 4295 (uint8_t *)lun->mode_pages.ie_page; 4296 break; 4297 case 0x02: { 4298 struct ctl_logical_block_provisioning_page *page; 4299 4300 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4301 &lbp_page_default, 4302 sizeof(lbp_page_default)); 4303 memcpy(&lun->mode_pages.lbp_page[ 4304 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4305 sizeof(lbp_page_changeable)); 4306 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4307 &lbp_page_default, 4308 sizeof(lbp_page_default)); 4309 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4310 value = ctl_get_opt(&lun->be_lun->options, 4311 "avail-threshold"); 4312 if (value != NULL && 4313 ctl_expand_number(value, &ival) == 0) { 4314 page->descr[0].flags |= SLBPPD_ENABLED | 4315 SLBPPD_ARMING_DEC; 4316 if (lun->be_lun->blocksize) 4317 ival /= lun->be_lun->blocksize; 4318 else 4319 ival /= 512; 4320 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4321 page->descr[0].count); 4322 } 4323 value = ctl_get_opt(&lun->be_lun->options, 4324 "used-threshold"); 4325 if (value != NULL && 4326 ctl_expand_number(value, &ival) == 0) { 4327 page->descr[1].flags |= SLBPPD_ENABLED | 4328 SLBPPD_ARMING_INC; 4329 if (lun->be_lun->blocksize) 4330 ival /= lun->be_lun->blocksize; 4331 else 4332 ival /= 512; 4333 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4334 page->descr[1].count); 4335 } 4336 value = ctl_get_opt(&lun->be_lun->options, 4337 "pool-avail-threshold"); 4338 if (value != NULL && 4339 ctl_expand_number(value, &ival) == 0) { 4340 page->descr[2].flags |= SLBPPD_ENABLED | 4341 SLBPPD_ARMING_DEC; 4342 if (lun->be_lun->blocksize) 4343 ival /= lun->be_lun->blocksize; 4344 else 4345 ival /= 512; 4346 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4347 page->descr[2].count); 4348 } 4349 value = ctl_get_opt(&lun->be_lun->options, 4350 "pool-used-threshold"); 4351 if (value != NULL && 4352 ctl_expand_number(value, &ival) == 0) { 4353 page->descr[3].flags |= SLBPPD_ENABLED | 4354 SLBPPD_ARMING_INC; 4355 if (lun->be_lun->blocksize) 4356 ival /= lun->be_lun->blocksize; 4357 else 4358 ival /= 512; 4359 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4360 page->descr[3].count); 4361 } 4362 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4363 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4364 sizeof(lbp_page_default)); 4365 page_index->page_data = 4366 (uint8_t *)lun->mode_pages.lbp_page; 4367 }} 4368 break; 4369 } 4370 case SMS_VENDOR_SPECIFIC_PAGE:{ 4371 switch (page_index->subpage) { 4372 case DBGCNF_SUBPAGE_CODE: { 4373 struct copan_debugconf_subpage *current_page, 4374 *saved_page; 4375 4376 memcpy(&lun->mode_pages.debugconf_subpage[ 4377 CTL_PAGE_CURRENT], 4378 &debugconf_page_default, 4379 sizeof(debugconf_page_default)); 4380 memcpy(&lun->mode_pages.debugconf_subpage[ 4381 CTL_PAGE_CHANGEABLE], 4382 &debugconf_page_changeable, 4383 sizeof(debugconf_page_changeable)); 4384 memcpy(&lun->mode_pages.debugconf_subpage[ 4385 CTL_PAGE_DEFAULT], 4386 &debugconf_page_default, 4387 sizeof(debugconf_page_default)); 4388 memcpy(&lun->mode_pages.debugconf_subpage[ 4389 CTL_PAGE_SAVED], 4390 &debugconf_page_default, 4391 sizeof(debugconf_page_default)); 4392 page_index->page_data = 4393 (uint8_t *)lun->mode_pages.debugconf_subpage; 4394 4395 current_page = (struct copan_debugconf_subpage *) 4396 (page_index->page_data + 4397 (page_index->page_len * 4398 CTL_PAGE_CURRENT)); 4399 saved_page = (struct copan_debugconf_subpage *) 4400 (page_index->page_data + 4401 (page_index->page_len * 4402 CTL_PAGE_SAVED)); 4403 break; 4404 } 4405 default: 4406 panic("invalid subpage value %d", 4407 page_index->subpage); 4408 break; 4409 } 4410 break; 4411 } 4412 default: 4413 panic("invalid page value %d", 4414 page_index->page_code & SMPH_PC_MASK); 4415 break; 4416 } 4417 } 4418 4419 return (CTL_RETVAL_COMPLETE); 4420 } 4421 4422 static int 4423 ctl_init_log_page_index(struct ctl_lun *lun) 4424 { 4425 struct ctl_page_index *page_index; 4426 int i, j, k, prev; 4427 4428 memcpy(&lun->log_pages.index, log_page_index_template, 4429 sizeof(log_page_index_template)); 4430 4431 prev = -1; 4432 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4433 4434 page_index = &lun->log_pages.index[i]; 4435 /* 4436 * If this is a disk-only mode page, there's no point in 4437 * setting it up. For some pages, we have to have some 4438 * basic information about the disk in order to calculate the 4439 * mode page data. 4440 */ 4441 if ((lun->be_lun->lun_type != T_DIRECT) 4442 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4443 continue; 4444 4445 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4446 lun->backend->lun_attr == NULL) 4447 continue; 4448 4449 if (page_index->page_code != prev) { 4450 lun->log_pages.pages_page[j] = page_index->page_code; 4451 prev = page_index->page_code; 4452 j++; 4453 } 4454 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4455 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4456 k++; 4457 } 4458 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4459 lun->log_pages.index[0].page_len = j; 4460 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4461 lun->log_pages.index[1].page_len = k * 2; 4462 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4463 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4464 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4465 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4466 4467 return (CTL_RETVAL_COMPLETE); 4468 } 4469 4470 static int 4471 hex2bin(const char *str, uint8_t *buf, int buf_size) 4472 { 4473 int i; 4474 u_char c; 4475 4476 memset(buf, 0, buf_size); 4477 while (isspace(str[0])) 4478 str++; 4479 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4480 str += 2; 4481 buf_size *= 2; 4482 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4483 c = str[i]; 4484 if (isdigit(c)) 4485 c -= '0'; 4486 else if (isalpha(c)) 4487 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4488 else 4489 break; 4490 if (c >= 16) 4491 break; 4492 if ((i & 1) == 0) 4493 buf[i / 2] |= (c << 4); 4494 else 4495 buf[i / 2] |= c; 4496 } 4497 return ((i + 1) / 2); 4498 } 4499 4500 /* 4501 * LUN allocation. 4502 * 4503 * Requirements: 4504 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4505 * wants us to allocate the LUN and he can block. 4506 * - ctl_softc is always set 4507 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4508 * 4509 * Returns 0 for success, non-zero (errno) for failure. 4510 */ 4511 static int 4512 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4513 struct ctl_be_lun *const be_lun) 4514 { 4515 struct ctl_lun *nlun, *lun; 4516 struct scsi_vpd_id_descriptor *desc; 4517 struct scsi_vpd_id_t10 *t10id; 4518 const char *eui, *naa, *scsiname, *vendor, *value; 4519 int lun_number, i, lun_malloced; 4520 int devidlen, idlen1, idlen2 = 0, len; 4521 4522 if (be_lun == NULL) 4523 return (EINVAL); 4524 4525 /* 4526 * We currently only support Direct Access or Processor LUN types. 4527 */ 4528 switch (be_lun->lun_type) { 4529 case T_DIRECT: 4530 break; 4531 case T_PROCESSOR: 4532 break; 4533 case T_SEQUENTIAL: 4534 case T_CHANGER: 4535 default: 4536 be_lun->lun_config_status(be_lun->be_lun, 4537 CTL_LUN_CONFIG_FAILURE); 4538 break; 4539 } 4540 if (ctl_lun == NULL) { 4541 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4542 lun_malloced = 1; 4543 } else { 4544 lun_malloced = 0; 4545 lun = ctl_lun; 4546 } 4547 4548 memset(lun, 0, sizeof(*lun)); 4549 if (lun_malloced) 4550 lun->flags = CTL_LUN_MALLOCED; 4551 4552 /* Generate LUN ID. */ 4553 devidlen = max(CTL_DEVID_MIN_LEN, 4554 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4555 idlen1 = sizeof(*t10id) + devidlen; 4556 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4557 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4558 if (scsiname != NULL) { 4559 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4560 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4561 } 4562 eui = ctl_get_opt(&be_lun->options, "eui"); 4563 if (eui != NULL) { 4564 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4565 } 4566 naa = ctl_get_opt(&be_lun->options, "naa"); 4567 if (naa != NULL) { 4568 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4569 } 4570 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4571 M_CTL, M_WAITOK | M_ZERO); 4572 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4573 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4574 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4575 desc->length = idlen1; 4576 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4577 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4578 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4579 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4580 } else { 4581 strncpy(t10id->vendor, vendor, 4582 min(sizeof(t10id->vendor), strlen(vendor))); 4583 } 4584 strncpy((char *)t10id->vendor_spec_id, 4585 (char *)be_lun->device_id, devidlen); 4586 if (scsiname != NULL) { 4587 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4588 desc->length); 4589 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4590 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4591 SVPD_ID_TYPE_SCSI_NAME; 4592 desc->length = idlen2; 4593 strlcpy(desc->identifier, scsiname, idlen2); 4594 } 4595 if (eui != NULL) { 4596 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4597 desc->length); 4598 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4599 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4600 SVPD_ID_TYPE_EUI64; 4601 desc->length = hex2bin(eui, desc->identifier, 16); 4602 desc->length = desc->length > 12 ? 16 : 4603 (desc->length > 8 ? 12 : 8); 4604 len -= 16 - desc->length; 4605 } 4606 if (naa != NULL) { 4607 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4608 desc->length); 4609 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4610 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4611 SVPD_ID_TYPE_NAA; 4612 desc->length = hex2bin(naa, desc->identifier, 16); 4613 desc->length = desc->length > 8 ? 16 : 8; 4614 len -= 16 - desc->length; 4615 } 4616 lun->lun_devid->len = len; 4617 4618 mtx_lock(&ctl_softc->ctl_lock); 4619 /* 4620 * See if the caller requested a particular LUN number. If so, see 4621 * if it is available. Otherwise, allocate the first available LUN. 4622 */ 4623 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4624 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4625 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4626 mtx_unlock(&ctl_softc->ctl_lock); 4627 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4628 printf("ctl: requested LUN ID %d is higher " 4629 "than CTL_MAX_LUNS - 1 (%d)\n", 4630 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4631 } else { 4632 /* 4633 * XXX KDM return an error, or just assign 4634 * another LUN ID in this case?? 4635 */ 4636 printf("ctl: requested LUN ID %d is already " 4637 "in use\n", be_lun->req_lun_id); 4638 } 4639 if (lun->flags & CTL_LUN_MALLOCED) 4640 free(lun, M_CTL); 4641 be_lun->lun_config_status(be_lun->be_lun, 4642 CTL_LUN_CONFIG_FAILURE); 4643 return (ENOSPC); 4644 } 4645 lun_number = be_lun->req_lun_id; 4646 } else { 4647 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4648 if (lun_number == -1) { 4649 mtx_unlock(&ctl_softc->ctl_lock); 4650 printf("ctl: can't allocate LUN, out of LUNs\n"); 4651 if (lun->flags & CTL_LUN_MALLOCED) 4652 free(lun, M_CTL); 4653 be_lun->lun_config_status(be_lun->be_lun, 4654 CTL_LUN_CONFIG_FAILURE); 4655 return (ENOSPC); 4656 } 4657 } 4658 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4659 4660 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4661 lun->lun = lun_number; 4662 lun->be_lun = be_lun; 4663 /* 4664 * The processor LUN is always enabled. Disk LUNs come on line 4665 * disabled, and must be enabled by the backend. 4666 */ 4667 lun->flags |= CTL_LUN_DISABLED; 4668 lun->backend = be_lun->be; 4669 be_lun->ctl_lun = lun; 4670 be_lun->lun_id = lun_number; 4671 atomic_add_int(&be_lun->be->num_luns, 1); 4672 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4673 lun->flags |= CTL_LUN_OFFLINE; 4674 4675 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4676 lun->flags |= CTL_LUN_STOPPED; 4677 4678 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4679 lun->flags |= CTL_LUN_INOPERABLE; 4680 4681 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4682 lun->flags |= CTL_LUN_PRIMARY_SC; 4683 4684 value = ctl_get_opt(&be_lun->options, "readonly"); 4685 if (value != NULL && strcmp(value, "on") == 0) 4686 lun->flags |= CTL_LUN_READONLY; 4687 4688 lun->serseq = CTL_LUN_SERSEQ_OFF; 4689 if (be_lun->flags & CTL_LUN_FLAG_SERSEQ_READ) 4690 lun->serseq = CTL_LUN_SERSEQ_READ; 4691 value = ctl_get_opt(&be_lun->options, "serseq"); 4692 if (value != NULL && strcmp(value, "on") == 0) 4693 lun->serseq = CTL_LUN_SERSEQ_ON; 4694 else if (value != NULL && strcmp(value, "read") == 0) 4695 lun->serseq = CTL_LUN_SERSEQ_READ; 4696 else if (value != NULL && strcmp(value, "off") == 0) 4697 lun->serseq = CTL_LUN_SERSEQ_OFF; 4698 4699 lun->ctl_softc = ctl_softc; 4700 #ifdef CTL_TIME_IO 4701 lun->last_busy = getsbinuptime(); 4702 #endif 4703 TAILQ_INIT(&lun->ooa_queue); 4704 TAILQ_INIT(&lun->blocked_queue); 4705 STAILQ_INIT(&lun->error_list); 4706 ctl_tpc_lun_init(lun); 4707 4708 /* 4709 * Initialize the mode and log page index. 4710 */ 4711 ctl_init_page_index(lun); 4712 ctl_init_log_page_index(lun); 4713 4714 /* 4715 * Now, before we insert this lun on the lun list, set the lun 4716 * inventory changed UA for all other luns. 4717 */ 4718 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4719 mtx_lock(&nlun->lun_lock); 4720 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4721 mtx_unlock(&nlun->lun_lock); 4722 } 4723 4724 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4725 4726 ctl_softc->ctl_luns[lun_number] = lun; 4727 4728 ctl_softc->num_luns++; 4729 4730 /* Setup statistics gathering */ 4731 lun->stats.device_type = be_lun->lun_type; 4732 lun->stats.lun_number = lun_number; 4733 if (lun->stats.device_type == T_DIRECT) 4734 lun->stats.blocksize = be_lun->blocksize; 4735 else 4736 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4737 for (i = 0;i < CTL_MAX_PORTS;i++) 4738 lun->stats.ports[i].targ_port = i; 4739 4740 mtx_unlock(&ctl_softc->ctl_lock); 4741 4742 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4743 return (0); 4744 } 4745 4746 /* 4747 * Delete a LUN. 4748 * Assumptions: 4749 * - LUN has already been marked invalid and any pending I/O has been taken 4750 * care of. 4751 */ 4752 static int 4753 ctl_free_lun(struct ctl_lun *lun) 4754 { 4755 struct ctl_softc *softc; 4756 struct ctl_lun *nlun; 4757 int i; 4758 4759 softc = lun->ctl_softc; 4760 4761 mtx_assert(&softc->ctl_lock, MA_OWNED); 4762 4763 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4764 4765 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4766 4767 softc->ctl_luns[lun->lun] = NULL; 4768 4769 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4770 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4771 4772 softc->num_luns--; 4773 4774 /* 4775 * Tell the backend to free resources, if this LUN has a backend. 4776 */ 4777 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4778 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4779 4780 ctl_tpc_lun_shutdown(lun); 4781 mtx_destroy(&lun->lun_lock); 4782 free(lun->lun_devid, M_CTL); 4783 for (i = 0; i < CTL_MAX_PORTS; i++) 4784 free(lun->pending_ua[i], M_CTL); 4785 for (i = 0; i < 2 * CTL_MAX_PORTS; i++) 4786 free(lun->pr_keys[i], M_CTL); 4787 free(lun->write_buffer, M_CTL); 4788 if (lun->flags & CTL_LUN_MALLOCED) 4789 free(lun, M_CTL); 4790 4791 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4792 mtx_lock(&nlun->lun_lock); 4793 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4794 mtx_unlock(&nlun->lun_lock); 4795 } 4796 4797 return (0); 4798 } 4799 4800 static void 4801 ctl_create_lun(struct ctl_be_lun *be_lun) 4802 { 4803 struct ctl_softc *softc; 4804 4805 softc = control_softc; 4806 4807 /* 4808 * ctl_alloc_lun() should handle all potential failure cases. 4809 */ 4810 ctl_alloc_lun(softc, NULL, be_lun); 4811 } 4812 4813 int 4814 ctl_add_lun(struct ctl_be_lun *be_lun) 4815 { 4816 struct ctl_softc *softc = control_softc; 4817 4818 mtx_lock(&softc->ctl_lock); 4819 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4820 mtx_unlock(&softc->ctl_lock); 4821 wakeup(&softc->pending_lun_queue); 4822 4823 return (0); 4824 } 4825 4826 int 4827 ctl_enable_lun(struct ctl_be_lun *be_lun) 4828 { 4829 struct ctl_softc *softc; 4830 struct ctl_port *port, *nport; 4831 struct ctl_lun *lun; 4832 int retval; 4833 4834 lun = (struct ctl_lun *)be_lun->ctl_lun; 4835 softc = lun->ctl_softc; 4836 4837 mtx_lock(&softc->ctl_lock); 4838 mtx_lock(&lun->lun_lock); 4839 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4840 /* 4841 * eh? Why did we get called if the LUN is already 4842 * enabled? 4843 */ 4844 mtx_unlock(&lun->lun_lock); 4845 mtx_unlock(&softc->ctl_lock); 4846 return (0); 4847 } 4848 lun->flags &= ~CTL_LUN_DISABLED; 4849 mtx_unlock(&lun->lun_lock); 4850 4851 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) { 4852 nport = STAILQ_NEXT(port, links); 4853 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4854 port->lun_map != NULL) 4855 continue; 4856 4857 /* 4858 * Drop the lock while we call the FETD's enable routine. 4859 * This can lead to a callback into CTL (at least in the 4860 * case of the internal initiator frontend. 4861 */ 4862 mtx_unlock(&softc->ctl_lock); 4863 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4864 mtx_lock(&softc->ctl_lock); 4865 if (retval != 0) { 4866 printf("%s: FETD %s port %d returned error " 4867 "%d for lun_enable on lun %jd\n", 4868 __func__, port->port_name, port->targ_port, 4869 retval, (intmax_t)lun->lun); 4870 } 4871 } 4872 4873 mtx_unlock(&softc->ctl_lock); 4874 4875 return (0); 4876 } 4877 4878 int 4879 ctl_disable_lun(struct ctl_be_lun *be_lun) 4880 { 4881 struct ctl_softc *softc; 4882 struct ctl_port *port; 4883 struct ctl_lun *lun; 4884 int retval; 4885 4886 lun = (struct ctl_lun *)be_lun->ctl_lun; 4887 softc = lun->ctl_softc; 4888 4889 mtx_lock(&softc->ctl_lock); 4890 mtx_lock(&lun->lun_lock); 4891 if (lun->flags & CTL_LUN_DISABLED) { 4892 mtx_unlock(&lun->lun_lock); 4893 mtx_unlock(&softc->ctl_lock); 4894 return (0); 4895 } 4896 lun->flags |= CTL_LUN_DISABLED; 4897 mtx_unlock(&lun->lun_lock); 4898 4899 STAILQ_FOREACH(port, &softc->port_list, links) { 4900 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4901 port->lun_map != NULL) 4902 continue; 4903 mtx_unlock(&softc->ctl_lock); 4904 /* 4905 * Drop the lock before we call the frontend's disable 4906 * routine, to avoid lock order reversals. 4907 * 4908 * XXX KDM what happens if the frontend list changes while 4909 * we're traversing it? It's unlikely, but should be handled. 4910 */ 4911 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4912 mtx_lock(&softc->ctl_lock); 4913 if (retval != 0) { 4914 printf("%s: FETD %s port %d returned error " 4915 "%d for lun_disable on lun %jd\n", 4916 __func__, port->port_name, port->targ_port, 4917 retval, (intmax_t)lun->lun); 4918 } 4919 } 4920 4921 mtx_unlock(&softc->ctl_lock); 4922 4923 return (0); 4924 } 4925 4926 int 4927 ctl_start_lun(struct ctl_be_lun *be_lun) 4928 { 4929 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4930 4931 mtx_lock(&lun->lun_lock); 4932 lun->flags &= ~CTL_LUN_STOPPED; 4933 mtx_unlock(&lun->lun_lock); 4934 return (0); 4935 } 4936 4937 int 4938 ctl_stop_lun(struct ctl_be_lun *be_lun) 4939 { 4940 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4941 4942 mtx_lock(&lun->lun_lock); 4943 lun->flags |= CTL_LUN_STOPPED; 4944 mtx_unlock(&lun->lun_lock); 4945 return (0); 4946 } 4947 4948 int 4949 ctl_lun_offline(struct ctl_be_lun *be_lun) 4950 { 4951 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4952 4953 mtx_lock(&lun->lun_lock); 4954 lun->flags |= CTL_LUN_OFFLINE; 4955 mtx_unlock(&lun->lun_lock); 4956 return (0); 4957 } 4958 4959 int 4960 ctl_lun_online(struct ctl_be_lun *be_lun) 4961 { 4962 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4963 4964 mtx_lock(&lun->lun_lock); 4965 lun->flags &= ~CTL_LUN_OFFLINE; 4966 mtx_unlock(&lun->lun_lock); 4967 return (0); 4968 } 4969 4970 int 4971 ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4972 { 4973 struct ctl_softc *softc; 4974 struct ctl_lun *lun; 4975 4976 lun = (struct ctl_lun *)be_lun->ctl_lun; 4977 softc = lun->ctl_softc; 4978 4979 mtx_lock(&lun->lun_lock); 4980 4981 /* 4982 * The LUN needs to be disabled before it can be marked invalid. 4983 */ 4984 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4985 mtx_unlock(&lun->lun_lock); 4986 return (-1); 4987 } 4988 /* 4989 * Mark the LUN invalid. 4990 */ 4991 lun->flags |= CTL_LUN_INVALID; 4992 4993 /* 4994 * If there is nothing in the OOA queue, go ahead and free the LUN. 4995 * If we have something in the OOA queue, we'll free it when the 4996 * last I/O completes. 4997 */ 4998 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4999 mtx_unlock(&lun->lun_lock); 5000 mtx_lock(&softc->ctl_lock); 5001 ctl_free_lun(lun); 5002 mtx_unlock(&softc->ctl_lock); 5003 } else 5004 mtx_unlock(&lun->lun_lock); 5005 5006 return (0); 5007 } 5008 5009 int 5010 ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5011 { 5012 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5013 5014 mtx_lock(&lun->lun_lock); 5015 lun->flags |= CTL_LUN_INOPERABLE; 5016 mtx_unlock(&lun->lun_lock); 5017 return (0); 5018 } 5019 5020 int 5021 ctl_lun_operable(struct ctl_be_lun *be_lun) 5022 { 5023 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5024 5025 mtx_lock(&lun->lun_lock); 5026 lun->flags &= ~CTL_LUN_INOPERABLE; 5027 mtx_unlock(&lun->lun_lock); 5028 return (0); 5029 } 5030 5031 void 5032 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5033 { 5034 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5035 5036 mtx_lock(&lun->lun_lock); 5037 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED); 5038 mtx_unlock(&lun->lun_lock); 5039 } 5040 5041 /* 5042 * Backend "memory move is complete" callback for requests that never 5043 * make it down to say RAIDCore's configuration code. 5044 */ 5045 int 5046 ctl_config_move_done(union ctl_io *io) 5047 { 5048 int retval; 5049 5050 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5051 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5052 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5053 5054 if ((io->io_hdr.port_status != 0) && 5055 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5056 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5057 /* 5058 * For hardware error sense keys, the sense key 5059 * specific value is defined to be a retry count, 5060 * but we use it to pass back an internal FETD 5061 * error code. XXX KDM Hopefully the FETD is only 5062 * using 16 bits for an error code, since that's 5063 * all the space we have in the sks field. 5064 */ 5065 ctl_set_internal_failure(&io->scsiio, 5066 /*sks_valid*/ 1, 5067 /*retry_count*/ 5068 io->io_hdr.port_status); 5069 } 5070 5071 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5072 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5073 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5074 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5075 /* 5076 * XXX KDM just assuming a single pointer here, and not a 5077 * S/G list. If we start using S/G lists for config data, 5078 * we'll need to know how to clean them up here as well. 5079 */ 5080 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5081 free(io->scsiio.kern_data_ptr, M_CTL); 5082 ctl_done(io); 5083 retval = CTL_RETVAL_COMPLETE; 5084 } else { 5085 /* 5086 * XXX KDM now we need to continue data movement. Some 5087 * options: 5088 * - call ctl_scsiio() again? We don't do this for data 5089 * writes, because for those at least we know ahead of 5090 * time where the write will go and how long it is. For 5091 * config writes, though, that information is largely 5092 * contained within the write itself, thus we need to 5093 * parse out the data again. 5094 * 5095 * - Call some other function once the data is in? 5096 */ 5097 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5098 ctl_data_print(io); 5099 5100 /* 5101 * XXX KDM call ctl_scsiio() again for now, and check flag 5102 * bits to see whether we're allocated or not. 5103 */ 5104 retval = ctl_scsiio(&io->scsiio); 5105 } 5106 return (retval); 5107 } 5108 5109 /* 5110 * This gets called by a backend driver when it is done with a 5111 * data_submit method. 5112 */ 5113 void 5114 ctl_data_submit_done(union ctl_io *io) 5115 { 5116 /* 5117 * If the IO_CONT flag is set, we need to call the supplied 5118 * function to continue processing the I/O, instead of completing 5119 * the I/O just yet. 5120 * 5121 * If there is an error, though, we don't want to keep processing. 5122 * Instead, just send status back to the initiator. 5123 */ 5124 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5125 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5126 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5127 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5128 io->scsiio.io_cont(io); 5129 return; 5130 } 5131 ctl_done(io); 5132 } 5133 5134 /* 5135 * This gets called by a backend driver when it is done with a 5136 * configuration write. 5137 */ 5138 void 5139 ctl_config_write_done(union ctl_io *io) 5140 { 5141 uint8_t *buf; 5142 5143 /* 5144 * If the IO_CONT flag is set, we need to call the supplied 5145 * function to continue processing the I/O, instead of completing 5146 * the I/O just yet. 5147 * 5148 * If there is an error, though, we don't want to keep processing. 5149 * Instead, just send status back to the initiator. 5150 */ 5151 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5152 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5153 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5154 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5155 io->scsiio.io_cont(io); 5156 return; 5157 } 5158 /* 5159 * Since a configuration write can be done for commands that actually 5160 * have data allocated, like write buffer, and commands that have 5161 * no data, like start/stop unit, we need to check here. 5162 */ 5163 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5164 buf = io->scsiio.kern_data_ptr; 5165 else 5166 buf = NULL; 5167 ctl_done(io); 5168 if (buf) 5169 free(buf, M_CTL); 5170 } 5171 5172 void 5173 ctl_config_read_done(union ctl_io *io) 5174 { 5175 uint8_t *buf; 5176 5177 /* 5178 * If there is some error -- we are done, skip data transfer. 5179 */ 5180 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5181 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5182 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5183 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5184 buf = io->scsiio.kern_data_ptr; 5185 else 5186 buf = NULL; 5187 ctl_done(io); 5188 if (buf) 5189 free(buf, M_CTL); 5190 return; 5191 } 5192 5193 /* 5194 * If the IO_CONT flag is set, we need to call the supplied 5195 * function to continue processing the I/O, instead of completing 5196 * the I/O just yet. 5197 */ 5198 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5199 io->scsiio.io_cont(io); 5200 return; 5201 } 5202 5203 ctl_datamove(io); 5204 } 5205 5206 /* 5207 * SCSI release command. 5208 */ 5209 int 5210 ctl_scsi_release(struct ctl_scsiio *ctsio) 5211 { 5212 int length, longid, thirdparty_id, resv_id; 5213 struct ctl_lun *lun; 5214 uint32_t residx; 5215 5216 length = 0; 5217 resv_id = 0; 5218 5219 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5220 5221 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5222 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5223 5224 switch (ctsio->cdb[0]) { 5225 case RELEASE_10: { 5226 struct scsi_release_10 *cdb; 5227 5228 cdb = (struct scsi_release_10 *)ctsio->cdb; 5229 5230 if (cdb->byte2 & SR10_LONGID) 5231 longid = 1; 5232 else 5233 thirdparty_id = cdb->thirdparty_id; 5234 5235 resv_id = cdb->resv_id; 5236 length = scsi_2btoul(cdb->length); 5237 break; 5238 } 5239 } 5240 5241 5242 /* 5243 * XXX KDM right now, we only support LUN reservation. We don't 5244 * support 3rd party reservations, or extent reservations, which 5245 * might actually need the parameter list. If we've gotten this 5246 * far, we've got a LUN reservation. Anything else got kicked out 5247 * above. So, according to SPC, ignore the length. 5248 */ 5249 length = 0; 5250 5251 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5252 && (length > 0)) { 5253 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5254 ctsio->kern_data_len = length; 5255 ctsio->kern_total_len = length; 5256 ctsio->kern_data_resid = 0; 5257 ctsio->kern_rel_offset = 0; 5258 ctsio->kern_sg_entries = 0; 5259 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5260 ctsio->be_move_done = ctl_config_move_done; 5261 ctl_datamove((union ctl_io *)ctsio); 5262 5263 return (CTL_RETVAL_COMPLETE); 5264 } 5265 5266 if (length > 0) 5267 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5268 5269 mtx_lock(&lun->lun_lock); 5270 5271 /* 5272 * According to SPC, it is not an error for an intiator to attempt 5273 * to release a reservation on a LUN that isn't reserved, or that 5274 * is reserved by another initiator. The reservation can only be 5275 * released, though, by the initiator who made it or by one of 5276 * several reset type events. 5277 */ 5278 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5279 lun->flags &= ~CTL_LUN_RESERVED; 5280 5281 mtx_unlock(&lun->lun_lock); 5282 5283 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5284 free(ctsio->kern_data_ptr, M_CTL); 5285 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5286 } 5287 5288 ctl_set_success(ctsio); 5289 ctl_done((union ctl_io *)ctsio); 5290 return (CTL_RETVAL_COMPLETE); 5291 } 5292 5293 int 5294 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5295 { 5296 int extent, thirdparty, longid; 5297 int resv_id, length; 5298 uint64_t thirdparty_id; 5299 struct ctl_lun *lun; 5300 uint32_t residx; 5301 5302 extent = 0; 5303 thirdparty = 0; 5304 longid = 0; 5305 resv_id = 0; 5306 length = 0; 5307 thirdparty_id = 0; 5308 5309 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5310 5311 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5312 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5313 5314 switch (ctsio->cdb[0]) { 5315 case RESERVE_10: { 5316 struct scsi_reserve_10 *cdb; 5317 5318 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5319 5320 if (cdb->byte2 & SR10_LONGID) 5321 longid = 1; 5322 else 5323 thirdparty_id = cdb->thirdparty_id; 5324 5325 resv_id = cdb->resv_id; 5326 length = scsi_2btoul(cdb->length); 5327 break; 5328 } 5329 } 5330 5331 /* 5332 * XXX KDM right now, we only support LUN reservation. We don't 5333 * support 3rd party reservations, or extent reservations, which 5334 * might actually need the parameter list. If we've gotten this 5335 * far, we've got a LUN reservation. Anything else got kicked out 5336 * above. So, according to SPC, ignore the length. 5337 */ 5338 length = 0; 5339 5340 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5341 && (length > 0)) { 5342 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5343 ctsio->kern_data_len = length; 5344 ctsio->kern_total_len = length; 5345 ctsio->kern_data_resid = 0; 5346 ctsio->kern_rel_offset = 0; 5347 ctsio->kern_sg_entries = 0; 5348 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5349 ctsio->be_move_done = ctl_config_move_done; 5350 ctl_datamove((union ctl_io *)ctsio); 5351 5352 return (CTL_RETVAL_COMPLETE); 5353 } 5354 5355 if (length > 0) 5356 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5357 5358 mtx_lock(&lun->lun_lock); 5359 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5360 ctl_set_reservation_conflict(ctsio); 5361 goto bailout; 5362 } 5363 5364 lun->flags |= CTL_LUN_RESERVED; 5365 lun->res_idx = residx; 5366 5367 ctl_set_success(ctsio); 5368 5369 bailout: 5370 mtx_unlock(&lun->lun_lock); 5371 5372 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5373 free(ctsio->kern_data_ptr, M_CTL); 5374 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5375 } 5376 5377 ctl_done((union ctl_io *)ctsio); 5378 return (CTL_RETVAL_COMPLETE); 5379 } 5380 5381 int 5382 ctl_start_stop(struct ctl_scsiio *ctsio) 5383 { 5384 struct scsi_start_stop_unit *cdb; 5385 struct ctl_lun *lun; 5386 int retval; 5387 5388 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5389 5390 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5391 retval = 0; 5392 5393 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5394 5395 /* 5396 * XXX KDM 5397 * We don't support the immediate bit on a stop unit. In order to 5398 * do that, we would need to code up a way to know that a stop is 5399 * pending, and hold off any new commands until it completes, one 5400 * way or another. Then we could accept or reject those commands 5401 * depending on its status. We would almost need to do the reverse 5402 * of what we do below for an immediate start -- return the copy of 5403 * the ctl_io to the FETD with status to send to the host (and to 5404 * free the copy!) and then free the original I/O once the stop 5405 * actually completes. That way, the OOA queue mechanism can work 5406 * to block commands that shouldn't proceed. Another alternative 5407 * would be to put the copy in the queue in place of the original, 5408 * and return the original back to the caller. That could be 5409 * slightly safer.. 5410 */ 5411 if ((cdb->byte2 & SSS_IMMED) 5412 && ((cdb->how & SSS_START) == 0)) { 5413 ctl_set_invalid_field(ctsio, 5414 /*sks_valid*/ 1, 5415 /*command*/ 1, 5416 /*field*/ 1, 5417 /*bit_valid*/ 1, 5418 /*bit*/ 0); 5419 ctl_done((union ctl_io *)ctsio); 5420 return (CTL_RETVAL_COMPLETE); 5421 } 5422 5423 if ((lun->flags & CTL_LUN_PR_RESERVED) 5424 && ((cdb->how & SSS_START)==0)) { 5425 uint32_t residx; 5426 5427 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5428 if (ctl_get_prkey(lun, residx) == 0 5429 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5430 5431 ctl_set_reservation_conflict(ctsio); 5432 ctl_done((union ctl_io *)ctsio); 5433 return (CTL_RETVAL_COMPLETE); 5434 } 5435 } 5436 5437 /* 5438 * If there is no backend on this device, we can't start or stop 5439 * it. In theory we shouldn't get any start/stop commands in the 5440 * first place at this level if the LUN doesn't have a backend. 5441 * That should get stopped by the command decode code. 5442 */ 5443 if (lun->backend == NULL) { 5444 ctl_set_invalid_opcode(ctsio); 5445 ctl_done((union ctl_io *)ctsio); 5446 return (CTL_RETVAL_COMPLETE); 5447 } 5448 5449 /* 5450 * XXX KDM Copan-specific offline behavior. 5451 * Figure out a reasonable way to port this? 5452 */ 5453 #ifdef NEEDTOPORT 5454 mtx_lock(&lun->lun_lock); 5455 5456 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5457 && (lun->flags & CTL_LUN_OFFLINE)) { 5458 /* 5459 * If the LUN is offline, and the on/offline bit isn't set, 5460 * reject the start or stop. Otherwise, let it through. 5461 */ 5462 mtx_unlock(&lun->lun_lock); 5463 ctl_set_lun_not_ready(ctsio); 5464 ctl_done((union ctl_io *)ctsio); 5465 } else { 5466 mtx_unlock(&lun->lun_lock); 5467 #endif /* NEEDTOPORT */ 5468 /* 5469 * This could be a start or a stop when we're online, 5470 * or a stop/offline or start/online. A start or stop when 5471 * we're offline is covered in the case above. 5472 */ 5473 /* 5474 * In the non-immediate case, we send the request to 5475 * the backend and return status to the user when 5476 * it is done. 5477 * 5478 * In the immediate case, we allocate a new ctl_io 5479 * to hold a copy of the request, and send that to 5480 * the backend. We then set good status on the 5481 * user's request and return it immediately. 5482 */ 5483 if (cdb->byte2 & SSS_IMMED) { 5484 union ctl_io *new_io; 5485 5486 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5487 ctl_copy_io((union ctl_io *)ctsio, new_io); 5488 retval = lun->backend->config_write(new_io); 5489 ctl_set_success(ctsio); 5490 ctl_done((union ctl_io *)ctsio); 5491 } else { 5492 retval = lun->backend->config_write( 5493 (union ctl_io *)ctsio); 5494 } 5495 #ifdef NEEDTOPORT 5496 } 5497 #endif 5498 return (retval); 5499 } 5500 5501 /* 5502 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5503 * we don't really do anything with the LBA and length fields if the user 5504 * passes them in. Instead we'll just flush out the cache for the entire 5505 * LUN. 5506 */ 5507 int 5508 ctl_sync_cache(struct ctl_scsiio *ctsio) 5509 { 5510 struct ctl_lun *lun; 5511 struct ctl_softc *softc; 5512 struct ctl_lba_len_flags *lbalen; 5513 uint64_t starting_lba; 5514 uint32_t block_count; 5515 int retval; 5516 uint8_t byte2; 5517 5518 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5519 5520 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5521 softc = lun->ctl_softc; 5522 retval = 0; 5523 5524 switch (ctsio->cdb[0]) { 5525 case SYNCHRONIZE_CACHE: { 5526 struct scsi_sync_cache *cdb; 5527 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5528 5529 starting_lba = scsi_4btoul(cdb->begin_lba); 5530 block_count = scsi_2btoul(cdb->lb_count); 5531 byte2 = cdb->byte2; 5532 break; 5533 } 5534 case SYNCHRONIZE_CACHE_16: { 5535 struct scsi_sync_cache_16 *cdb; 5536 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5537 5538 starting_lba = scsi_8btou64(cdb->begin_lba); 5539 block_count = scsi_4btoul(cdb->lb_count); 5540 byte2 = cdb->byte2; 5541 break; 5542 } 5543 default: 5544 ctl_set_invalid_opcode(ctsio); 5545 ctl_done((union ctl_io *)ctsio); 5546 goto bailout; 5547 break; /* NOTREACHED */ 5548 } 5549 5550 /* 5551 * We check the LBA and length, but don't do anything with them. 5552 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5553 * get flushed. This check will just help satisfy anyone who wants 5554 * to see an error for an out of range LBA. 5555 */ 5556 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5557 ctl_set_lba_out_of_range(ctsio); 5558 ctl_done((union ctl_io *)ctsio); 5559 goto bailout; 5560 } 5561 5562 /* 5563 * If this LUN has no backend, we can't flush the cache anyway. 5564 */ 5565 if (lun->backend == NULL) { 5566 ctl_set_invalid_opcode(ctsio); 5567 ctl_done((union ctl_io *)ctsio); 5568 goto bailout; 5569 } 5570 5571 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5572 lbalen->lba = starting_lba; 5573 lbalen->len = block_count; 5574 lbalen->flags = byte2; 5575 5576 /* 5577 * Check to see whether we're configured to send the SYNCHRONIZE 5578 * CACHE command directly to the back end. 5579 */ 5580 mtx_lock(&lun->lun_lock); 5581 if ((softc->flags & CTL_FLAG_REAL_SYNC) 5582 && (++(lun->sync_count) >= lun->sync_interval)) { 5583 lun->sync_count = 0; 5584 mtx_unlock(&lun->lun_lock); 5585 retval = lun->backend->config_write((union ctl_io *)ctsio); 5586 } else { 5587 mtx_unlock(&lun->lun_lock); 5588 ctl_set_success(ctsio); 5589 ctl_done((union ctl_io *)ctsio); 5590 } 5591 5592 bailout: 5593 5594 return (retval); 5595 } 5596 5597 int 5598 ctl_format(struct ctl_scsiio *ctsio) 5599 { 5600 struct scsi_format *cdb; 5601 struct ctl_lun *lun; 5602 int length, defect_list_len; 5603 5604 CTL_DEBUG_PRINT(("ctl_format\n")); 5605 5606 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5607 5608 cdb = (struct scsi_format *)ctsio->cdb; 5609 5610 length = 0; 5611 if (cdb->byte2 & SF_FMTDATA) { 5612 if (cdb->byte2 & SF_LONGLIST) 5613 length = sizeof(struct scsi_format_header_long); 5614 else 5615 length = sizeof(struct scsi_format_header_short); 5616 } 5617 5618 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5619 && (length > 0)) { 5620 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5621 ctsio->kern_data_len = length; 5622 ctsio->kern_total_len = length; 5623 ctsio->kern_data_resid = 0; 5624 ctsio->kern_rel_offset = 0; 5625 ctsio->kern_sg_entries = 0; 5626 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5627 ctsio->be_move_done = ctl_config_move_done; 5628 ctl_datamove((union ctl_io *)ctsio); 5629 5630 return (CTL_RETVAL_COMPLETE); 5631 } 5632 5633 defect_list_len = 0; 5634 5635 if (cdb->byte2 & SF_FMTDATA) { 5636 if (cdb->byte2 & SF_LONGLIST) { 5637 struct scsi_format_header_long *header; 5638 5639 header = (struct scsi_format_header_long *) 5640 ctsio->kern_data_ptr; 5641 5642 defect_list_len = scsi_4btoul(header->defect_list_len); 5643 if (defect_list_len != 0) { 5644 ctl_set_invalid_field(ctsio, 5645 /*sks_valid*/ 1, 5646 /*command*/ 0, 5647 /*field*/ 2, 5648 /*bit_valid*/ 0, 5649 /*bit*/ 0); 5650 goto bailout; 5651 } 5652 } else { 5653 struct scsi_format_header_short *header; 5654 5655 header = (struct scsi_format_header_short *) 5656 ctsio->kern_data_ptr; 5657 5658 defect_list_len = scsi_2btoul(header->defect_list_len); 5659 if (defect_list_len != 0) { 5660 ctl_set_invalid_field(ctsio, 5661 /*sks_valid*/ 1, 5662 /*command*/ 0, 5663 /*field*/ 2, 5664 /*bit_valid*/ 0, 5665 /*bit*/ 0); 5666 goto bailout; 5667 } 5668 } 5669 } 5670 5671 /* 5672 * The format command will clear out the "Medium format corrupted" 5673 * status if set by the configuration code. That status is really 5674 * just a way to notify the host that we have lost the media, and 5675 * get them to issue a command that will basically make them think 5676 * they're blowing away the media. 5677 */ 5678 mtx_lock(&lun->lun_lock); 5679 lun->flags &= ~CTL_LUN_INOPERABLE; 5680 mtx_unlock(&lun->lun_lock); 5681 5682 ctl_set_success(ctsio); 5683 bailout: 5684 5685 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5686 free(ctsio->kern_data_ptr, M_CTL); 5687 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5688 } 5689 5690 ctl_done((union ctl_io *)ctsio); 5691 return (CTL_RETVAL_COMPLETE); 5692 } 5693 5694 int 5695 ctl_read_buffer(struct ctl_scsiio *ctsio) 5696 { 5697 struct scsi_read_buffer *cdb; 5698 struct ctl_lun *lun; 5699 int buffer_offset, len; 5700 static uint8_t descr[4]; 5701 static uint8_t echo_descr[4] = { 0 }; 5702 5703 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5704 5705 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5706 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5707 5708 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5709 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5710 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5711 ctl_set_invalid_field(ctsio, 5712 /*sks_valid*/ 1, 5713 /*command*/ 1, 5714 /*field*/ 1, 5715 /*bit_valid*/ 1, 5716 /*bit*/ 4); 5717 ctl_done((union ctl_io *)ctsio); 5718 return (CTL_RETVAL_COMPLETE); 5719 } 5720 5721 len = scsi_3btoul(cdb->length); 5722 buffer_offset = scsi_3btoul(cdb->offset); 5723 5724 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5725 ctl_set_invalid_field(ctsio, 5726 /*sks_valid*/ 1, 5727 /*command*/ 1, 5728 /*field*/ 6, 5729 /*bit_valid*/ 0, 5730 /*bit*/ 0); 5731 ctl_done((union ctl_io *)ctsio); 5732 return (CTL_RETVAL_COMPLETE); 5733 } 5734 5735 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5736 descr[0] = 0; 5737 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5738 ctsio->kern_data_ptr = descr; 5739 len = min(len, sizeof(descr)); 5740 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5741 ctsio->kern_data_ptr = echo_descr; 5742 len = min(len, sizeof(echo_descr)); 5743 } else { 5744 if (lun->write_buffer == NULL) { 5745 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5746 M_CTL, M_WAITOK); 5747 } 5748 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5749 } 5750 ctsio->kern_data_len = len; 5751 ctsio->kern_total_len = len; 5752 ctsio->kern_data_resid = 0; 5753 ctsio->kern_rel_offset = 0; 5754 ctsio->kern_sg_entries = 0; 5755 ctl_set_success(ctsio); 5756 ctsio->be_move_done = ctl_config_move_done; 5757 ctl_datamove((union ctl_io *)ctsio); 5758 return (CTL_RETVAL_COMPLETE); 5759 } 5760 5761 int 5762 ctl_write_buffer(struct ctl_scsiio *ctsio) 5763 { 5764 struct scsi_write_buffer *cdb; 5765 struct ctl_lun *lun; 5766 int buffer_offset, len; 5767 5768 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5769 5770 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5771 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5772 5773 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5774 ctl_set_invalid_field(ctsio, 5775 /*sks_valid*/ 1, 5776 /*command*/ 1, 5777 /*field*/ 1, 5778 /*bit_valid*/ 1, 5779 /*bit*/ 4); 5780 ctl_done((union ctl_io *)ctsio); 5781 return (CTL_RETVAL_COMPLETE); 5782 } 5783 5784 len = scsi_3btoul(cdb->length); 5785 buffer_offset = scsi_3btoul(cdb->offset); 5786 5787 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5788 ctl_set_invalid_field(ctsio, 5789 /*sks_valid*/ 1, 5790 /*command*/ 1, 5791 /*field*/ 6, 5792 /*bit_valid*/ 0, 5793 /*bit*/ 0); 5794 ctl_done((union ctl_io *)ctsio); 5795 return (CTL_RETVAL_COMPLETE); 5796 } 5797 5798 /* 5799 * If we've got a kernel request that hasn't been malloced yet, 5800 * malloc it and tell the caller the data buffer is here. 5801 */ 5802 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5803 if (lun->write_buffer == NULL) { 5804 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5805 M_CTL, M_WAITOK); 5806 } 5807 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5808 ctsio->kern_data_len = len; 5809 ctsio->kern_total_len = len; 5810 ctsio->kern_data_resid = 0; 5811 ctsio->kern_rel_offset = 0; 5812 ctsio->kern_sg_entries = 0; 5813 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5814 ctsio->be_move_done = ctl_config_move_done; 5815 ctl_datamove((union ctl_io *)ctsio); 5816 5817 return (CTL_RETVAL_COMPLETE); 5818 } 5819 5820 ctl_set_success(ctsio); 5821 ctl_done((union ctl_io *)ctsio); 5822 return (CTL_RETVAL_COMPLETE); 5823 } 5824 5825 int 5826 ctl_write_same(struct ctl_scsiio *ctsio) 5827 { 5828 struct ctl_lun *lun; 5829 struct ctl_lba_len_flags *lbalen; 5830 uint64_t lba; 5831 uint32_t num_blocks; 5832 int len, retval; 5833 uint8_t byte2; 5834 5835 retval = CTL_RETVAL_COMPLETE; 5836 5837 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5838 5839 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5840 5841 switch (ctsio->cdb[0]) { 5842 case WRITE_SAME_10: { 5843 struct scsi_write_same_10 *cdb; 5844 5845 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5846 5847 lba = scsi_4btoul(cdb->addr); 5848 num_blocks = scsi_2btoul(cdb->length); 5849 byte2 = cdb->byte2; 5850 break; 5851 } 5852 case WRITE_SAME_16: { 5853 struct scsi_write_same_16 *cdb; 5854 5855 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5856 5857 lba = scsi_8btou64(cdb->addr); 5858 num_blocks = scsi_4btoul(cdb->length); 5859 byte2 = cdb->byte2; 5860 break; 5861 } 5862 default: 5863 /* 5864 * We got a command we don't support. This shouldn't 5865 * happen, commands should be filtered out above us. 5866 */ 5867 ctl_set_invalid_opcode(ctsio); 5868 ctl_done((union ctl_io *)ctsio); 5869 5870 return (CTL_RETVAL_COMPLETE); 5871 break; /* NOTREACHED */ 5872 } 5873 5874 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5875 if ((byte2 & SWS_UNMAP) == 0 && 5876 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5877 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5878 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5879 ctl_done((union ctl_io *)ctsio); 5880 return (CTL_RETVAL_COMPLETE); 5881 } 5882 5883 /* 5884 * The first check is to make sure we're in bounds, the second 5885 * check is to catch wrap-around problems. If the lba + num blocks 5886 * is less than the lba, then we've wrapped around and the block 5887 * range is invalid anyway. 5888 */ 5889 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5890 || ((lba + num_blocks) < lba)) { 5891 ctl_set_lba_out_of_range(ctsio); 5892 ctl_done((union ctl_io *)ctsio); 5893 return (CTL_RETVAL_COMPLETE); 5894 } 5895 5896 /* Zero number of blocks means "to the last logical block" */ 5897 if (num_blocks == 0) { 5898 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5899 ctl_set_invalid_field(ctsio, 5900 /*sks_valid*/ 0, 5901 /*command*/ 1, 5902 /*field*/ 0, 5903 /*bit_valid*/ 0, 5904 /*bit*/ 0); 5905 ctl_done((union ctl_io *)ctsio); 5906 return (CTL_RETVAL_COMPLETE); 5907 } 5908 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5909 } 5910 5911 len = lun->be_lun->blocksize; 5912 5913 /* 5914 * If we've got a kernel request that hasn't been malloced yet, 5915 * malloc it and tell the caller the data buffer is here. 5916 */ 5917 if ((byte2 & SWS_NDOB) == 0 && 5918 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5919 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5920 ctsio->kern_data_len = len; 5921 ctsio->kern_total_len = len; 5922 ctsio->kern_data_resid = 0; 5923 ctsio->kern_rel_offset = 0; 5924 ctsio->kern_sg_entries = 0; 5925 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5926 ctsio->be_move_done = ctl_config_move_done; 5927 ctl_datamove((union ctl_io *)ctsio); 5928 5929 return (CTL_RETVAL_COMPLETE); 5930 } 5931 5932 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5933 lbalen->lba = lba; 5934 lbalen->len = num_blocks; 5935 lbalen->flags = byte2; 5936 retval = lun->backend->config_write((union ctl_io *)ctsio); 5937 5938 return (retval); 5939 } 5940 5941 int 5942 ctl_unmap(struct ctl_scsiio *ctsio) 5943 { 5944 struct ctl_lun *lun; 5945 struct scsi_unmap *cdb; 5946 struct ctl_ptr_len_flags *ptrlen; 5947 struct scsi_unmap_header *hdr; 5948 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5949 uint64_t lba; 5950 uint32_t num_blocks; 5951 int len, retval; 5952 uint8_t byte2; 5953 5954 retval = CTL_RETVAL_COMPLETE; 5955 5956 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5957 5958 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5959 cdb = (struct scsi_unmap *)ctsio->cdb; 5960 5961 len = scsi_2btoul(cdb->length); 5962 byte2 = cdb->byte2; 5963 5964 /* 5965 * If we've got a kernel request that hasn't been malloced yet, 5966 * malloc it and tell the caller the data buffer is here. 5967 */ 5968 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5969 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5970 ctsio->kern_data_len = len; 5971 ctsio->kern_total_len = len; 5972 ctsio->kern_data_resid = 0; 5973 ctsio->kern_rel_offset = 0; 5974 ctsio->kern_sg_entries = 0; 5975 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5976 ctsio->be_move_done = ctl_config_move_done; 5977 ctl_datamove((union ctl_io *)ctsio); 5978 5979 return (CTL_RETVAL_COMPLETE); 5980 } 5981 5982 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5983 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5984 if (len < sizeof (*hdr) || 5985 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5986 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5987 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5988 ctl_set_invalid_field(ctsio, 5989 /*sks_valid*/ 0, 5990 /*command*/ 0, 5991 /*field*/ 0, 5992 /*bit_valid*/ 0, 5993 /*bit*/ 0); 5994 goto done; 5995 } 5996 len = scsi_2btoul(hdr->desc_length); 5997 buf = (struct scsi_unmap_desc *)(hdr + 1); 5998 end = buf + len / sizeof(*buf); 5999 6000 endnz = buf; 6001 for (range = buf; range < end; range++) { 6002 lba = scsi_8btou64(range->lba); 6003 num_blocks = scsi_4btoul(range->length); 6004 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6005 || ((lba + num_blocks) < lba)) { 6006 ctl_set_lba_out_of_range(ctsio); 6007 ctl_done((union ctl_io *)ctsio); 6008 return (CTL_RETVAL_COMPLETE); 6009 } 6010 if (num_blocks != 0) 6011 endnz = range + 1; 6012 } 6013 6014 /* 6015 * Block backend can not handle zero last range. 6016 * Filter it out and return if there is nothing left. 6017 */ 6018 len = (uint8_t *)endnz - (uint8_t *)buf; 6019 if (len == 0) { 6020 ctl_set_success(ctsio); 6021 goto done; 6022 } 6023 6024 mtx_lock(&lun->lun_lock); 6025 ptrlen = (struct ctl_ptr_len_flags *) 6026 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6027 ptrlen->ptr = (void *)buf; 6028 ptrlen->len = len; 6029 ptrlen->flags = byte2; 6030 ctl_check_blocked(lun); 6031 mtx_unlock(&lun->lun_lock); 6032 6033 retval = lun->backend->config_write((union ctl_io *)ctsio); 6034 return (retval); 6035 6036 done: 6037 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 6038 free(ctsio->kern_data_ptr, M_CTL); 6039 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 6040 } 6041 ctl_done((union ctl_io *)ctsio); 6042 return (CTL_RETVAL_COMPLETE); 6043 } 6044 6045 /* 6046 * Note that this function currently doesn't actually do anything inside 6047 * CTL to enforce things if the DQue bit is turned on. 6048 * 6049 * Also note that this function can't be used in the default case, because 6050 * the DQue bit isn't set in the changeable mask for the control mode page 6051 * anyway. This is just here as an example for how to implement a page 6052 * handler, and a placeholder in case we want to allow the user to turn 6053 * tagged queueing on and off. 6054 * 6055 * The D_SENSE bit handling is functional, however, and will turn 6056 * descriptor sense on and off for a given LUN. 6057 */ 6058 int 6059 ctl_control_page_handler(struct ctl_scsiio *ctsio, 6060 struct ctl_page_index *page_index, uint8_t *page_ptr) 6061 { 6062 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6063 struct ctl_lun *lun; 6064 int set_ua; 6065 uint32_t initidx; 6066 6067 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6068 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6069 set_ua = 0; 6070 6071 user_cp = (struct scsi_control_page *)page_ptr; 6072 current_cp = (struct scsi_control_page *) 6073 (page_index->page_data + (page_index->page_len * 6074 CTL_PAGE_CURRENT)); 6075 saved_cp = (struct scsi_control_page *) 6076 (page_index->page_data + (page_index->page_len * 6077 CTL_PAGE_SAVED)); 6078 6079 mtx_lock(&lun->lun_lock); 6080 if (((current_cp->rlec & SCP_DSENSE) == 0) 6081 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6082 /* 6083 * Descriptor sense is currently turned off and the user 6084 * wants to turn it on. 6085 */ 6086 current_cp->rlec |= SCP_DSENSE; 6087 saved_cp->rlec |= SCP_DSENSE; 6088 lun->flags |= CTL_LUN_SENSE_DESC; 6089 set_ua = 1; 6090 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6091 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6092 /* 6093 * Descriptor sense is currently turned on, and the user 6094 * wants to turn it off. 6095 */ 6096 current_cp->rlec &= ~SCP_DSENSE; 6097 saved_cp->rlec &= ~SCP_DSENSE; 6098 lun->flags &= ~CTL_LUN_SENSE_DESC; 6099 set_ua = 1; 6100 } 6101 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6102 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6103 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6104 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6105 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6106 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6107 set_ua = 1; 6108 } 6109 if ((current_cp->eca_and_aen & SCP_SWP) != 6110 (user_cp->eca_and_aen & SCP_SWP)) { 6111 current_cp->eca_and_aen &= ~SCP_SWP; 6112 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6113 saved_cp->eca_and_aen &= ~SCP_SWP; 6114 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6115 set_ua = 1; 6116 } 6117 if (set_ua != 0) 6118 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6119 mtx_unlock(&lun->lun_lock); 6120 6121 return (0); 6122 } 6123 6124 int 6125 ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6126 struct ctl_page_index *page_index, uint8_t *page_ptr) 6127 { 6128 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6129 struct ctl_lun *lun; 6130 int set_ua; 6131 uint32_t initidx; 6132 6133 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6134 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6135 set_ua = 0; 6136 6137 user_cp = (struct scsi_caching_page *)page_ptr; 6138 current_cp = (struct scsi_caching_page *) 6139 (page_index->page_data + (page_index->page_len * 6140 CTL_PAGE_CURRENT)); 6141 saved_cp = (struct scsi_caching_page *) 6142 (page_index->page_data + (page_index->page_len * 6143 CTL_PAGE_SAVED)); 6144 6145 mtx_lock(&lun->lun_lock); 6146 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6147 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6148 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6149 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6150 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6151 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6152 set_ua = 1; 6153 } 6154 if (set_ua != 0) 6155 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6156 mtx_unlock(&lun->lun_lock); 6157 6158 return (0); 6159 } 6160 6161 int 6162 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6163 struct ctl_page_index *page_index, 6164 uint8_t *page_ptr) 6165 { 6166 uint8_t *c; 6167 int i; 6168 6169 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6170 ctl_time_io_secs = 6171 (c[0] << 8) | 6172 (c[1] << 0) | 6173 0; 6174 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6175 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6176 printf("page data:"); 6177 for (i=0; i<8; i++) 6178 printf(" %.2x",page_ptr[i]); 6179 printf("\n"); 6180 return (0); 6181 } 6182 6183 int 6184 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6185 struct ctl_page_index *page_index, 6186 int pc) 6187 { 6188 struct copan_debugconf_subpage *page; 6189 6190 page = (struct copan_debugconf_subpage *)page_index->page_data + 6191 (page_index->page_len * pc); 6192 6193 switch (pc) { 6194 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6195 case SMS_PAGE_CTRL_DEFAULT >> 6: 6196 case SMS_PAGE_CTRL_SAVED >> 6: 6197 /* 6198 * We don't update the changable or default bits for this page. 6199 */ 6200 break; 6201 case SMS_PAGE_CTRL_CURRENT >> 6: 6202 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6203 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6204 break; 6205 default: 6206 #ifdef NEEDTOPORT 6207 EPRINT(0, "Invalid PC %d!!", pc); 6208 #endif /* NEEDTOPORT */ 6209 break; 6210 } 6211 return (0); 6212 } 6213 6214 6215 static int 6216 ctl_do_mode_select(union ctl_io *io) 6217 { 6218 struct scsi_mode_page_header *page_header; 6219 struct ctl_page_index *page_index; 6220 struct ctl_scsiio *ctsio; 6221 int control_dev, page_len; 6222 int page_len_offset, page_len_size; 6223 union ctl_modepage_info *modepage_info; 6224 struct ctl_lun *lun; 6225 int *len_left, *len_used; 6226 int retval, i; 6227 6228 ctsio = &io->scsiio; 6229 page_index = NULL; 6230 page_len = 0; 6231 retval = CTL_RETVAL_COMPLETE; 6232 6233 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6234 6235 if (lun->be_lun->lun_type != T_DIRECT) 6236 control_dev = 1; 6237 else 6238 control_dev = 0; 6239 6240 modepage_info = (union ctl_modepage_info *) 6241 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6242 len_left = &modepage_info->header.len_left; 6243 len_used = &modepage_info->header.len_used; 6244 6245 do_next_page: 6246 6247 page_header = (struct scsi_mode_page_header *) 6248 (ctsio->kern_data_ptr + *len_used); 6249 6250 if (*len_left == 0) { 6251 free(ctsio->kern_data_ptr, M_CTL); 6252 ctl_set_success(ctsio); 6253 ctl_done((union ctl_io *)ctsio); 6254 return (CTL_RETVAL_COMPLETE); 6255 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6256 6257 free(ctsio->kern_data_ptr, M_CTL); 6258 ctl_set_param_len_error(ctsio); 6259 ctl_done((union ctl_io *)ctsio); 6260 return (CTL_RETVAL_COMPLETE); 6261 6262 } else if ((page_header->page_code & SMPH_SPF) 6263 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6264 6265 free(ctsio->kern_data_ptr, M_CTL); 6266 ctl_set_param_len_error(ctsio); 6267 ctl_done((union ctl_io *)ctsio); 6268 return (CTL_RETVAL_COMPLETE); 6269 } 6270 6271 6272 /* 6273 * XXX KDM should we do something with the block descriptor? 6274 */ 6275 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6276 6277 if ((control_dev != 0) 6278 && (lun->mode_pages.index[i].page_flags & 6279 CTL_PAGE_FLAG_DISK_ONLY)) 6280 continue; 6281 6282 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6283 (page_header->page_code & SMPH_PC_MASK)) 6284 continue; 6285 6286 /* 6287 * If neither page has a subpage code, then we've got a 6288 * match. 6289 */ 6290 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6291 && ((page_header->page_code & SMPH_SPF) == 0)) { 6292 page_index = &lun->mode_pages.index[i]; 6293 page_len = page_header->page_length; 6294 break; 6295 } 6296 6297 /* 6298 * If both pages have subpages, then the subpage numbers 6299 * have to match. 6300 */ 6301 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6302 && (page_header->page_code & SMPH_SPF)) { 6303 struct scsi_mode_page_header_sp *sph; 6304 6305 sph = (struct scsi_mode_page_header_sp *)page_header; 6306 6307 if (lun->mode_pages.index[i].subpage == 6308 sph->subpage) { 6309 page_index = &lun->mode_pages.index[i]; 6310 page_len = scsi_2btoul(sph->page_length); 6311 break; 6312 } 6313 } 6314 } 6315 6316 /* 6317 * If we couldn't find the page, or if we don't have a mode select 6318 * handler for it, send back an error to the user. 6319 */ 6320 if ((page_index == NULL) 6321 || (page_index->select_handler == NULL)) { 6322 ctl_set_invalid_field(ctsio, 6323 /*sks_valid*/ 1, 6324 /*command*/ 0, 6325 /*field*/ *len_used, 6326 /*bit_valid*/ 0, 6327 /*bit*/ 0); 6328 free(ctsio->kern_data_ptr, M_CTL); 6329 ctl_done((union ctl_io *)ctsio); 6330 return (CTL_RETVAL_COMPLETE); 6331 } 6332 6333 if (page_index->page_code & SMPH_SPF) { 6334 page_len_offset = 2; 6335 page_len_size = 2; 6336 } else { 6337 page_len_size = 1; 6338 page_len_offset = 1; 6339 } 6340 6341 /* 6342 * If the length the initiator gives us isn't the one we specify in 6343 * the mode page header, or if they didn't specify enough data in 6344 * the CDB to avoid truncating this page, kick out the request. 6345 */ 6346 if ((page_len != (page_index->page_len - page_len_offset - 6347 page_len_size)) 6348 || (*len_left < page_index->page_len)) { 6349 6350 6351 ctl_set_invalid_field(ctsio, 6352 /*sks_valid*/ 1, 6353 /*command*/ 0, 6354 /*field*/ *len_used + page_len_offset, 6355 /*bit_valid*/ 0, 6356 /*bit*/ 0); 6357 free(ctsio->kern_data_ptr, M_CTL); 6358 ctl_done((union ctl_io *)ctsio); 6359 return (CTL_RETVAL_COMPLETE); 6360 } 6361 6362 /* 6363 * Run through the mode page, checking to make sure that the bits 6364 * the user changed are actually legal for him to change. 6365 */ 6366 for (i = 0; i < page_index->page_len; i++) { 6367 uint8_t *user_byte, *change_mask, *current_byte; 6368 int bad_bit; 6369 int j; 6370 6371 user_byte = (uint8_t *)page_header + i; 6372 change_mask = page_index->page_data + 6373 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6374 current_byte = page_index->page_data + 6375 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6376 6377 /* 6378 * Check to see whether the user set any bits in this byte 6379 * that he is not allowed to set. 6380 */ 6381 if ((*user_byte & ~(*change_mask)) == 6382 (*current_byte & ~(*change_mask))) 6383 continue; 6384 6385 /* 6386 * Go through bit by bit to determine which one is illegal. 6387 */ 6388 bad_bit = 0; 6389 for (j = 7; j >= 0; j--) { 6390 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6391 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6392 bad_bit = i; 6393 break; 6394 } 6395 } 6396 ctl_set_invalid_field(ctsio, 6397 /*sks_valid*/ 1, 6398 /*command*/ 0, 6399 /*field*/ *len_used + i, 6400 /*bit_valid*/ 1, 6401 /*bit*/ bad_bit); 6402 free(ctsio->kern_data_ptr, M_CTL); 6403 ctl_done((union ctl_io *)ctsio); 6404 return (CTL_RETVAL_COMPLETE); 6405 } 6406 6407 /* 6408 * Decrement these before we call the page handler, since we may 6409 * end up getting called back one way or another before the handler 6410 * returns to this context. 6411 */ 6412 *len_left -= page_index->page_len; 6413 *len_used += page_index->page_len; 6414 6415 retval = page_index->select_handler(ctsio, page_index, 6416 (uint8_t *)page_header); 6417 6418 /* 6419 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6420 * wait until this queued command completes to finish processing 6421 * the mode page. If it returns anything other than 6422 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6423 * already set the sense information, freed the data pointer, and 6424 * completed the io for us. 6425 */ 6426 if (retval != CTL_RETVAL_COMPLETE) 6427 goto bailout_no_done; 6428 6429 /* 6430 * If the initiator sent us more than one page, parse the next one. 6431 */ 6432 if (*len_left > 0) 6433 goto do_next_page; 6434 6435 ctl_set_success(ctsio); 6436 free(ctsio->kern_data_ptr, M_CTL); 6437 ctl_done((union ctl_io *)ctsio); 6438 6439 bailout_no_done: 6440 6441 return (CTL_RETVAL_COMPLETE); 6442 6443 } 6444 6445 int 6446 ctl_mode_select(struct ctl_scsiio *ctsio) 6447 { 6448 int param_len, pf, sp; 6449 int header_size, bd_len; 6450 int len_left, len_used; 6451 struct ctl_page_index *page_index; 6452 struct ctl_lun *lun; 6453 int control_dev, page_len; 6454 union ctl_modepage_info *modepage_info; 6455 int retval; 6456 6457 pf = 0; 6458 sp = 0; 6459 page_len = 0; 6460 len_used = 0; 6461 len_left = 0; 6462 retval = 0; 6463 bd_len = 0; 6464 page_index = NULL; 6465 6466 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6467 6468 if (lun->be_lun->lun_type != T_DIRECT) 6469 control_dev = 1; 6470 else 6471 control_dev = 0; 6472 6473 switch (ctsio->cdb[0]) { 6474 case MODE_SELECT_6: { 6475 struct scsi_mode_select_6 *cdb; 6476 6477 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6478 6479 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6480 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6481 6482 param_len = cdb->length; 6483 header_size = sizeof(struct scsi_mode_header_6); 6484 break; 6485 } 6486 case MODE_SELECT_10: { 6487 struct scsi_mode_select_10 *cdb; 6488 6489 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6490 6491 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6492 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6493 6494 param_len = scsi_2btoul(cdb->length); 6495 header_size = sizeof(struct scsi_mode_header_10); 6496 break; 6497 } 6498 default: 6499 ctl_set_invalid_opcode(ctsio); 6500 ctl_done((union ctl_io *)ctsio); 6501 return (CTL_RETVAL_COMPLETE); 6502 break; /* NOTREACHED */ 6503 } 6504 6505 /* 6506 * From SPC-3: 6507 * "A parameter list length of zero indicates that the Data-Out Buffer 6508 * shall be empty. This condition shall not be considered as an error." 6509 */ 6510 if (param_len == 0) { 6511 ctl_set_success(ctsio); 6512 ctl_done((union ctl_io *)ctsio); 6513 return (CTL_RETVAL_COMPLETE); 6514 } 6515 6516 /* 6517 * Since we'll hit this the first time through, prior to 6518 * allocation, we don't need to free a data buffer here. 6519 */ 6520 if (param_len < header_size) { 6521 ctl_set_param_len_error(ctsio); 6522 ctl_done((union ctl_io *)ctsio); 6523 return (CTL_RETVAL_COMPLETE); 6524 } 6525 6526 /* 6527 * Allocate the data buffer and grab the user's data. In theory, 6528 * we shouldn't have to sanity check the parameter list length here 6529 * because the maximum size is 64K. We should be able to malloc 6530 * that much without too many problems. 6531 */ 6532 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6533 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6534 ctsio->kern_data_len = param_len; 6535 ctsio->kern_total_len = param_len; 6536 ctsio->kern_data_resid = 0; 6537 ctsio->kern_rel_offset = 0; 6538 ctsio->kern_sg_entries = 0; 6539 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6540 ctsio->be_move_done = ctl_config_move_done; 6541 ctl_datamove((union ctl_io *)ctsio); 6542 6543 return (CTL_RETVAL_COMPLETE); 6544 } 6545 6546 switch (ctsio->cdb[0]) { 6547 case MODE_SELECT_6: { 6548 struct scsi_mode_header_6 *mh6; 6549 6550 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6551 bd_len = mh6->blk_desc_len; 6552 break; 6553 } 6554 case MODE_SELECT_10: { 6555 struct scsi_mode_header_10 *mh10; 6556 6557 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6558 bd_len = scsi_2btoul(mh10->blk_desc_len); 6559 break; 6560 } 6561 default: 6562 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6563 break; 6564 } 6565 6566 if (param_len < (header_size + bd_len)) { 6567 free(ctsio->kern_data_ptr, M_CTL); 6568 ctl_set_param_len_error(ctsio); 6569 ctl_done((union ctl_io *)ctsio); 6570 return (CTL_RETVAL_COMPLETE); 6571 } 6572 6573 /* 6574 * Set the IO_CONT flag, so that if this I/O gets passed to 6575 * ctl_config_write_done(), it'll get passed back to 6576 * ctl_do_mode_select() for further processing, or completion if 6577 * we're all done. 6578 */ 6579 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6580 ctsio->io_cont = ctl_do_mode_select; 6581 6582 modepage_info = (union ctl_modepage_info *) 6583 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6584 6585 memset(modepage_info, 0, sizeof(*modepage_info)); 6586 6587 len_left = param_len - header_size - bd_len; 6588 len_used = header_size + bd_len; 6589 6590 modepage_info->header.len_left = len_left; 6591 modepage_info->header.len_used = len_used; 6592 6593 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6594 } 6595 6596 int 6597 ctl_mode_sense(struct ctl_scsiio *ctsio) 6598 { 6599 struct ctl_lun *lun; 6600 int pc, page_code, dbd, llba, subpage; 6601 int alloc_len, page_len, header_len, total_len; 6602 struct scsi_mode_block_descr *block_desc; 6603 struct ctl_page_index *page_index; 6604 int control_dev; 6605 6606 dbd = 0; 6607 llba = 0; 6608 block_desc = NULL; 6609 page_index = NULL; 6610 6611 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6612 6613 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6614 6615 if (lun->be_lun->lun_type != T_DIRECT) 6616 control_dev = 1; 6617 else 6618 control_dev = 0; 6619 6620 switch (ctsio->cdb[0]) { 6621 case MODE_SENSE_6: { 6622 struct scsi_mode_sense_6 *cdb; 6623 6624 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6625 6626 header_len = sizeof(struct scsi_mode_hdr_6); 6627 if (cdb->byte2 & SMS_DBD) 6628 dbd = 1; 6629 else 6630 header_len += sizeof(struct scsi_mode_block_descr); 6631 6632 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6633 page_code = cdb->page & SMS_PAGE_CODE; 6634 subpage = cdb->subpage; 6635 alloc_len = cdb->length; 6636 break; 6637 } 6638 case MODE_SENSE_10: { 6639 struct scsi_mode_sense_10 *cdb; 6640 6641 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6642 6643 header_len = sizeof(struct scsi_mode_hdr_10); 6644 6645 if (cdb->byte2 & SMS_DBD) 6646 dbd = 1; 6647 else 6648 header_len += sizeof(struct scsi_mode_block_descr); 6649 if (cdb->byte2 & SMS10_LLBAA) 6650 llba = 1; 6651 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6652 page_code = cdb->page & SMS_PAGE_CODE; 6653 subpage = cdb->subpage; 6654 alloc_len = scsi_2btoul(cdb->length); 6655 break; 6656 } 6657 default: 6658 ctl_set_invalid_opcode(ctsio); 6659 ctl_done((union ctl_io *)ctsio); 6660 return (CTL_RETVAL_COMPLETE); 6661 break; /* NOTREACHED */ 6662 } 6663 6664 /* 6665 * We have to make a first pass through to calculate the size of 6666 * the pages that match the user's query. Then we allocate enough 6667 * memory to hold it, and actually copy the data into the buffer. 6668 */ 6669 switch (page_code) { 6670 case SMS_ALL_PAGES_PAGE: { 6671 int i; 6672 6673 page_len = 0; 6674 6675 /* 6676 * At the moment, values other than 0 and 0xff here are 6677 * reserved according to SPC-3. 6678 */ 6679 if ((subpage != SMS_SUBPAGE_PAGE_0) 6680 && (subpage != SMS_SUBPAGE_ALL)) { 6681 ctl_set_invalid_field(ctsio, 6682 /*sks_valid*/ 1, 6683 /*command*/ 1, 6684 /*field*/ 3, 6685 /*bit_valid*/ 0, 6686 /*bit*/ 0); 6687 ctl_done((union ctl_io *)ctsio); 6688 return (CTL_RETVAL_COMPLETE); 6689 } 6690 6691 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6692 if ((control_dev != 0) 6693 && (lun->mode_pages.index[i].page_flags & 6694 CTL_PAGE_FLAG_DISK_ONLY)) 6695 continue; 6696 6697 /* 6698 * We don't use this subpage if the user didn't 6699 * request all subpages. 6700 */ 6701 if ((lun->mode_pages.index[i].subpage != 0) 6702 && (subpage == SMS_SUBPAGE_PAGE_0)) 6703 continue; 6704 6705 #if 0 6706 printf("found page %#x len %d\n", 6707 lun->mode_pages.index[i].page_code & 6708 SMPH_PC_MASK, 6709 lun->mode_pages.index[i].page_len); 6710 #endif 6711 page_len += lun->mode_pages.index[i].page_len; 6712 } 6713 break; 6714 } 6715 default: { 6716 int i; 6717 6718 page_len = 0; 6719 6720 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6721 /* Look for the right page code */ 6722 if ((lun->mode_pages.index[i].page_code & 6723 SMPH_PC_MASK) != page_code) 6724 continue; 6725 6726 /* Look for the right subpage or the subpage wildcard*/ 6727 if ((lun->mode_pages.index[i].subpage != subpage) 6728 && (subpage != SMS_SUBPAGE_ALL)) 6729 continue; 6730 6731 /* Make sure the page is supported for this dev type */ 6732 if ((control_dev != 0) 6733 && (lun->mode_pages.index[i].page_flags & 6734 CTL_PAGE_FLAG_DISK_ONLY)) 6735 continue; 6736 6737 #if 0 6738 printf("found page %#x len %d\n", 6739 lun->mode_pages.index[i].page_code & 6740 SMPH_PC_MASK, 6741 lun->mode_pages.index[i].page_len); 6742 #endif 6743 6744 page_len += lun->mode_pages.index[i].page_len; 6745 } 6746 6747 if (page_len == 0) { 6748 ctl_set_invalid_field(ctsio, 6749 /*sks_valid*/ 1, 6750 /*command*/ 1, 6751 /*field*/ 2, 6752 /*bit_valid*/ 1, 6753 /*bit*/ 5); 6754 ctl_done((union ctl_io *)ctsio); 6755 return (CTL_RETVAL_COMPLETE); 6756 } 6757 break; 6758 } 6759 } 6760 6761 total_len = header_len + page_len; 6762 #if 0 6763 printf("header_len = %d, page_len = %d, total_len = %d\n", 6764 header_len, page_len, total_len); 6765 #endif 6766 6767 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6768 ctsio->kern_sg_entries = 0; 6769 ctsio->kern_data_resid = 0; 6770 ctsio->kern_rel_offset = 0; 6771 if (total_len < alloc_len) { 6772 ctsio->residual = alloc_len - total_len; 6773 ctsio->kern_data_len = total_len; 6774 ctsio->kern_total_len = total_len; 6775 } else { 6776 ctsio->residual = 0; 6777 ctsio->kern_data_len = alloc_len; 6778 ctsio->kern_total_len = alloc_len; 6779 } 6780 6781 switch (ctsio->cdb[0]) { 6782 case MODE_SENSE_6: { 6783 struct scsi_mode_hdr_6 *header; 6784 6785 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6786 6787 header->datalen = MIN(total_len - 1, 254); 6788 if (control_dev == 0) { 6789 header->dev_specific = 0x10; /* DPOFUA */ 6790 if ((lun->flags & CTL_LUN_READONLY) || 6791 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6792 .eca_and_aen & SCP_SWP) != 0) 6793 header->dev_specific |= 0x80; /* WP */ 6794 } 6795 if (dbd) 6796 header->block_descr_len = 0; 6797 else 6798 header->block_descr_len = 6799 sizeof(struct scsi_mode_block_descr); 6800 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6801 break; 6802 } 6803 case MODE_SENSE_10: { 6804 struct scsi_mode_hdr_10 *header; 6805 int datalen; 6806 6807 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6808 6809 datalen = MIN(total_len - 2, 65533); 6810 scsi_ulto2b(datalen, header->datalen); 6811 if (control_dev == 0) { 6812 header->dev_specific = 0x10; /* DPOFUA */ 6813 if ((lun->flags & CTL_LUN_READONLY) || 6814 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6815 .eca_and_aen & SCP_SWP) != 0) 6816 header->dev_specific |= 0x80; /* WP */ 6817 } 6818 if (dbd) 6819 scsi_ulto2b(0, header->block_descr_len); 6820 else 6821 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6822 header->block_descr_len); 6823 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6824 break; 6825 } 6826 default: 6827 panic("invalid CDB type %#x", ctsio->cdb[0]); 6828 break; /* NOTREACHED */ 6829 } 6830 6831 /* 6832 * If we've got a disk, use its blocksize in the block 6833 * descriptor. Otherwise, just set it to 0. 6834 */ 6835 if (dbd == 0) { 6836 if (control_dev == 0) 6837 scsi_ulto3b(lun->be_lun->blocksize, 6838 block_desc->block_len); 6839 else 6840 scsi_ulto3b(0, block_desc->block_len); 6841 } 6842 6843 switch (page_code) { 6844 case SMS_ALL_PAGES_PAGE: { 6845 int i, data_used; 6846 6847 data_used = header_len; 6848 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6849 struct ctl_page_index *page_index; 6850 6851 page_index = &lun->mode_pages.index[i]; 6852 6853 if ((control_dev != 0) 6854 && (page_index->page_flags & 6855 CTL_PAGE_FLAG_DISK_ONLY)) 6856 continue; 6857 6858 /* 6859 * We don't use this subpage if the user didn't 6860 * request all subpages. We already checked (above) 6861 * to make sure the user only specified a subpage 6862 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6863 */ 6864 if ((page_index->subpage != 0) 6865 && (subpage == SMS_SUBPAGE_PAGE_0)) 6866 continue; 6867 6868 /* 6869 * Call the handler, if it exists, to update the 6870 * page to the latest values. 6871 */ 6872 if (page_index->sense_handler != NULL) 6873 page_index->sense_handler(ctsio, page_index,pc); 6874 6875 memcpy(ctsio->kern_data_ptr + data_used, 6876 page_index->page_data + 6877 (page_index->page_len * pc), 6878 page_index->page_len); 6879 data_used += page_index->page_len; 6880 } 6881 break; 6882 } 6883 default: { 6884 int i, data_used; 6885 6886 data_used = header_len; 6887 6888 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6889 struct ctl_page_index *page_index; 6890 6891 page_index = &lun->mode_pages.index[i]; 6892 6893 /* Look for the right page code */ 6894 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6895 continue; 6896 6897 /* Look for the right subpage or the subpage wildcard*/ 6898 if ((page_index->subpage != subpage) 6899 && (subpage != SMS_SUBPAGE_ALL)) 6900 continue; 6901 6902 /* Make sure the page is supported for this dev type */ 6903 if ((control_dev != 0) 6904 && (page_index->page_flags & 6905 CTL_PAGE_FLAG_DISK_ONLY)) 6906 continue; 6907 6908 /* 6909 * Call the handler, if it exists, to update the 6910 * page to the latest values. 6911 */ 6912 if (page_index->sense_handler != NULL) 6913 page_index->sense_handler(ctsio, page_index,pc); 6914 6915 memcpy(ctsio->kern_data_ptr + data_used, 6916 page_index->page_data + 6917 (page_index->page_len * pc), 6918 page_index->page_len); 6919 data_used += page_index->page_len; 6920 } 6921 break; 6922 } 6923 } 6924 6925 ctl_set_success(ctsio); 6926 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6927 ctsio->be_move_done = ctl_config_move_done; 6928 ctl_datamove((union ctl_io *)ctsio); 6929 return (CTL_RETVAL_COMPLETE); 6930 } 6931 6932 int 6933 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6934 struct ctl_page_index *page_index, 6935 int pc) 6936 { 6937 struct ctl_lun *lun; 6938 struct scsi_log_param_header *phdr; 6939 uint8_t *data; 6940 uint64_t val; 6941 6942 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6943 data = page_index->page_data; 6944 6945 if (lun->backend->lun_attr != NULL && 6946 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6947 != UINT64_MAX) { 6948 phdr = (struct scsi_log_param_header *)data; 6949 scsi_ulto2b(0x0001, phdr->param_code); 6950 phdr->param_control = SLP_LBIN | SLP_LP; 6951 phdr->param_len = 8; 6952 data = (uint8_t *)(phdr + 1); 6953 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6954 data[4] = 0x02; /* per-pool */ 6955 data += phdr->param_len; 6956 } 6957 6958 if (lun->backend->lun_attr != NULL && 6959 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6960 != UINT64_MAX) { 6961 phdr = (struct scsi_log_param_header *)data; 6962 scsi_ulto2b(0x0002, phdr->param_code); 6963 phdr->param_control = SLP_LBIN | SLP_LP; 6964 phdr->param_len = 8; 6965 data = (uint8_t *)(phdr + 1); 6966 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6967 data[4] = 0x01; /* per-LUN */ 6968 data += phdr->param_len; 6969 } 6970 6971 if (lun->backend->lun_attr != NULL && 6972 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6973 != UINT64_MAX) { 6974 phdr = (struct scsi_log_param_header *)data; 6975 scsi_ulto2b(0x00f1, phdr->param_code); 6976 phdr->param_control = SLP_LBIN | SLP_LP; 6977 phdr->param_len = 8; 6978 data = (uint8_t *)(phdr + 1); 6979 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6980 data[4] = 0x02; /* per-pool */ 6981 data += phdr->param_len; 6982 } 6983 6984 if (lun->backend->lun_attr != NULL && 6985 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6986 != UINT64_MAX) { 6987 phdr = (struct scsi_log_param_header *)data; 6988 scsi_ulto2b(0x00f2, phdr->param_code); 6989 phdr->param_control = SLP_LBIN | SLP_LP; 6990 phdr->param_len = 8; 6991 data = (uint8_t *)(phdr + 1); 6992 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6993 data[4] = 0x02; /* per-pool */ 6994 data += phdr->param_len; 6995 } 6996 6997 page_index->page_len = data - page_index->page_data; 6998 return (0); 6999 } 7000 7001 int 7002 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 7003 struct ctl_page_index *page_index, 7004 int pc) 7005 { 7006 struct ctl_lun *lun; 7007 struct stat_page *data; 7008 uint64_t rn, wn, rb, wb; 7009 struct bintime rt, wt; 7010 int i; 7011 7012 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7013 data = (struct stat_page *)page_index->page_data; 7014 7015 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 7016 data->sap.hdr.param_control = SLP_LBIN; 7017 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 7018 sizeof(struct scsi_log_param_header); 7019 rn = wn = rb = wb = 0; 7020 bintime_clear(&rt); 7021 bintime_clear(&wt); 7022 for (i = 0; i < CTL_MAX_PORTS; i++) { 7023 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 7024 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 7025 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 7026 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 7027 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 7028 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 7029 } 7030 scsi_u64to8b(rn, data->sap.read_num); 7031 scsi_u64to8b(wn, data->sap.write_num); 7032 if (lun->stats.blocksize > 0) { 7033 scsi_u64to8b(wb / lun->stats.blocksize, 7034 data->sap.recvieved_lba); 7035 scsi_u64to8b(rb / lun->stats.blocksize, 7036 data->sap.transmitted_lba); 7037 } 7038 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 7039 data->sap.read_int); 7040 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 7041 data->sap.write_int); 7042 scsi_u64to8b(0, data->sap.weighted_num); 7043 scsi_u64to8b(0, data->sap.weighted_int); 7044 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 7045 data->it.hdr.param_control = SLP_LBIN; 7046 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 7047 sizeof(struct scsi_log_param_header); 7048 #ifdef CTL_TIME_IO 7049 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 7050 #endif 7051 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 7052 data->it.hdr.param_control = SLP_LBIN; 7053 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 7054 sizeof(struct scsi_log_param_header); 7055 scsi_ulto4b(3, data->ti.exponent); 7056 scsi_ulto4b(1, data->ti.integer); 7057 7058 page_index->page_len = sizeof(*data); 7059 return (0); 7060 } 7061 7062 int 7063 ctl_log_sense(struct ctl_scsiio *ctsio) 7064 { 7065 struct ctl_lun *lun; 7066 int i, pc, page_code, subpage; 7067 int alloc_len, total_len; 7068 struct ctl_page_index *page_index; 7069 struct scsi_log_sense *cdb; 7070 struct scsi_log_header *header; 7071 7072 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 7073 7074 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7075 cdb = (struct scsi_log_sense *)ctsio->cdb; 7076 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 7077 page_code = cdb->page & SLS_PAGE_CODE; 7078 subpage = cdb->subpage; 7079 alloc_len = scsi_2btoul(cdb->length); 7080 7081 page_index = NULL; 7082 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 7083 page_index = &lun->log_pages.index[i]; 7084 7085 /* Look for the right page code */ 7086 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 7087 continue; 7088 7089 /* Look for the right subpage or the subpage wildcard*/ 7090 if (page_index->subpage != subpage) 7091 continue; 7092 7093 break; 7094 } 7095 if (i >= CTL_NUM_LOG_PAGES) { 7096 ctl_set_invalid_field(ctsio, 7097 /*sks_valid*/ 1, 7098 /*command*/ 1, 7099 /*field*/ 2, 7100 /*bit_valid*/ 0, 7101 /*bit*/ 0); 7102 ctl_done((union ctl_io *)ctsio); 7103 return (CTL_RETVAL_COMPLETE); 7104 } 7105 7106 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 7107 7108 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7109 ctsio->kern_sg_entries = 0; 7110 ctsio->kern_data_resid = 0; 7111 ctsio->kern_rel_offset = 0; 7112 if (total_len < alloc_len) { 7113 ctsio->residual = alloc_len - total_len; 7114 ctsio->kern_data_len = total_len; 7115 ctsio->kern_total_len = total_len; 7116 } else { 7117 ctsio->residual = 0; 7118 ctsio->kern_data_len = alloc_len; 7119 ctsio->kern_total_len = alloc_len; 7120 } 7121 7122 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7123 header->page = page_index->page_code; 7124 if (page_index->subpage) { 7125 header->page |= SL_SPF; 7126 header->subpage = page_index->subpage; 7127 } 7128 scsi_ulto2b(page_index->page_len, header->datalen); 7129 7130 /* 7131 * Call the handler, if it exists, to update the 7132 * page to the latest values. 7133 */ 7134 if (page_index->sense_handler != NULL) 7135 page_index->sense_handler(ctsio, page_index, pc); 7136 7137 memcpy(header + 1, page_index->page_data, page_index->page_len); 7138 7139 ctl_set_success(ctsio); 7140 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7141 ctsio->be_move_done = ctl_config_move_done; 7142 ctl_datamove((union ctl_io *)ctsio); 7143 return (CTL_RETVAL_COMPLETE); 7144 } 7145 7146 int 7147 ctl_read_capacity(struct ctl_scsiio *ctsio) 7148 { 7149 struct scsi_read_capacity *cdb; 7150 struct scsi_read_capacity_data *data; 7151 struct ctl_lun *lun; 7152 uint32_t lba; 7153 7154 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7155 7156 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7157 7158 lba = scsi_4btoul(cdb->addr); 7159 if (((cdb->pmi & SRC_PMI) == 0) 7160 && (lba != 0)) { 7161 ctl_set_invalid_field(/*ctsio*/ ctsio, 7162 /*sks_valid*/ 1, 7163 /*command*/ 1, 7164 /*field*/ 2, 7165 /*bit_valid*/ 0, 7166 /*bit*/ 0); 7167 ctl_done((union ctl_io *)ctsio); 7168 return (CTL_RETVAL_COMPLETE); 7169 } 7170 7171 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7172 7173 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7174 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7175 ctsio->residual = 0; 7176 ctsio->kern_data_len = sizeof(*data); 7177 ctsio->kern_total_len = sizeof(*data); 7178 ctsio->kern_data_resid = 0; 7179 ctsio->kern_rel_offset = 0; 7180 ctsio->kern_sg_entries = 0; 7181 7182 /* 7183 * If the maximum LBA is greater than 0xfffffffe, the user must 7184 * issue a SERVICE ACTION IN (16) command, with the read capacity 7185 * serivce action set. 7186 */ 7187 if (lun->be_lun->maxlba > 0xfffffffe) 7188 scsi_ulto4b(0xffffffff, data->addr); 7189 else 7190 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7191 7192 /* 7193 * XXX KDM this may not be 512 bytes... 7194 */ 7195 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7196 7197 ctl_set_success(ctsio); 7198 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7199 ctsio->be_move_done = ctl_config_move_done; 7200 ctl_datamove((union ctl_io *)ctsio); 7201 return (CTL_RETVAL_COMPLETE); 7202 } 7203 7204 int 7205 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7206 { 7207 struct scsi_read_capacity_16 *cdb; 7208 struct scsi_read_capacity_data_long *data; 7209 struct ctl_lun *lun; 7210 uint64_t lba; 7211 uint32_t alloc_len; 7212 7213 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7214 7215 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7216 7217 alloc_len = scsi_4btoul(cdb->alloc_len); 7218 lba = scsi_8btou64(cdb->addr); 7219 7220 if ((cdb->reladr & SRC16_PMI) 7221 && (lba != 0)) { 7222 ctl_set_invalid_field(/*ctsio*/ ctsio, 7223 /*sks_valid*/ 1, 7224 /*command*/ 1, 7225 /*field*/ 2, 7226 /*bit_valid*/ 0, 7227 /*bit*/ 0); 7228 ctl_done((union ctl_io *)ctsio); 7229 return (CTL_RETVAL_COMPLETE); 7230 } 7231 7232 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7233 7234 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7235 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7236 7237 if (sizeof(*data) < alloc_len) { 7238 ctsio->residual = alloc_len - sizeof(*data); 7239 ctsio->kern_data_len = sizeof(*data); 7240 ctsio->kern_total_len = sizeof(*data); 7241 } else { 7242 ctsio->residual = 0; 7243 ctsio->kern_data_len = alloc_len; 7244 ctsio->kern_total_len = alloc_len; 7245 } 7246 ctsio->kern_data_resid = 0; 7247 ctsio->kern_rel_offset = 0; 7248 ctsio->kern_sg_entries = 0; 7249 7250 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7251 /* XXX KDM this may not be 512 bytes... */ 7252 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7253 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7254 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7255 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7256 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7257 7258 ctl_set_success(ctsio); 7259 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7260 ctsio->be_move_done = ctl_config_move_done; 7261 ctl_datamove((union ctl_io *)ctsio); 7262 return (CTL_RETVAL_COMPLETE); 7263 } 7264 7265 int 7266 ctl_get_lba_status(struct ctl_scsiio *ctsio) 7267 { 7268 struct scsi_get_lba_status *cdb; 7269 struct scsi_get_lba_status_data *data; 7270 struct ctl_lun *lun; 7271 struct ctl_lba_len_flags *lbalen; 7272 uint64_t lba; 7273 uint32_t alloc_len, total_len; 7274 int retval; 7275 7276 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7277 7278 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7279 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7280 lba = scsi_8btou64(cdb->addr); 7281 alloc_len = scsi_4btoul(cdb->alloc_len); 7282 7283 if (lba > lun->be_lun->maxlba) { 7284 ctl_set_lba_out_of_range(ctsio); 7285 ctl_done((union ctl_io *)ctsio); 7286 return (CTL_RETVAL_COMPLETE); 7287 } 7288 7289 total_len = sizeof(*data) + sizeof(data->descr[0]); 7290 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7291 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7292 7293 if (total_len < alloc_len) { 7294 ctsio->residual = alloc_len - total_len; 7295 ctsio->kern_data_len = total_len; 7296 ctsio->kern_total_len = total_len; 7297 } else { 7298 ctsio->residual = 0; 7299 ctsio->kern_data_len = alloc_len; 7300 ctsio->kern_total_len = alloc_len; 7301 } 7302 ctsio->kern_data_resid = 0; 7303 ctsio->kern_rel_offset = 0; 7304 ctsio->kern_sg_entries = 0; 7305 7306 /* Fill dummy data in case backend can't tell anything. */ 7307 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7308 scsi_u64to8b(lba, data->descr[0].addr); 7309 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7310 data->descr[0].length); 7311 data->descr[0].status = 0; /* Mapped or unknown. */ 7312 7313 ctl_set_success(ctsio); 7314 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7315 ctsio->be_move_done = ctl_config_move_done; 7316 7317 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7318 lbalen->lba = lba; 7319 lbalen->len = total_len; 7320 lbalen->flags = 0; 7321 retval = lun->backend->config_read((union ctl_io *)ctsio); 7322 return (CTL_RETVAL_COMPLETE); 7323 } 7324 7325 int 7326 ctl_read_defect(struct ctl_scsiio *ctsio) 7327 { 7328 struct scsi_read_defect_data_10 *ccb10; 7329 struct scsi_read_defect_data_12 *ccb12; 7330 struct scsi_read_defect_data_hdr_10 *data10; 7331 struct scsi_read_defect_data_hdr_12 *data12; 7332 uint32_t alloc_len, data_len; 7333 uint8_t format; 7334 7335 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7336 7337 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7338 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7339 format = ccb10->format; 7340 alloc_len = scsi_2btoul(ccb10->alloc_length); 7341 data_len = sizeof(*data10); 7342 } else { 7343 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7344 format = ccb12->format; 7345 alloc_len = scsi_4btoul(ccb12->alloc_length); 7346 data_len = sizeof(*data12); 7347 } 7348 if (alloc_len == 0) { 7349 ctl_set_success(ctsio); 7350 ctl_done((union ctl_io *)ctsio); 7351 return (CTL_RETVAL_COMPLETE); 7352 } 7353 7354 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7355 if (data_len < alloc_len) { 7356 ctsio->residual = alloc_len - data_len; 7357 ctsio->kern_data_len = data_len; 7358 ctsio->kern_total_len = data_len; 7359 } else { 7360 ctsio->residual = 0; 7361 ctsio->kern_data_len = alloc_len; 7362 ctsio->kern_total_len = alloc_len; 7363 } 7364 ctsio->kern_data_resid = 0; 7365 ctsio->kern_rel_offset = 0; 7366 ctsio->kern_sg_entries = 0; 7367 7368 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7369 data10 = (struct scsi_read_defect_data_hdr_10 *) 7370 ctsio->kern_data_ptr; 7371 data10->format = format; 7372 scsi_ulto2b(0, data10->length); 7373 } else { 7374 data12 = (struct scsi_read_defect_data_hdr_12 *) 7375 ctsio->kern_data_ptr; 7376 data12->format = format; 7377 scsi_ulto2b(0, data12->generation); 7378 scsi_ulto4b(0, data12->length); 7379 } 7380 7381 ctl_set_success(ctsio); 7382 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7383 ctsio->be_move_done = ctl_config_move_done; 7384 ctl_datamove((union ctl_io *)ctsio); 7385 return (CTL_RETVAL_COMPLETE); 7386 } 7387 7388 int 7389 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7390 { 7391 struct scsi_maintenance_in *cdb; 7392 int retval; 7393 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 7394 int num_target_port_groups, num_target_ports; 7395 struct ctl_lun *lun; 7396 struct ctl_softc *softc; 7397 struct ctl_port *port; 7398 struct scsi_target_group_data *rtg_ptr; 7399 struct scsi_target_group_data_extended *rtg_ext_ptr; 7400 struct scsi_target_port_group_descriptor *tpg_desc; 7401 7402 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7403 7404 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7405 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7406 softc = lun->ctl_softc; 7407 7408 retval = CTL_RETVAL_COMPLETE; 7409 7410 switch (cdb->byte2 & STG_PDF_MASK) { 7411 case STG_PDF_LENGTH: 7412 ext = 0; 7413 break; 7414 case STG_PDF_EXTENDED: 7415 ext = 1; 7416 break; 7417 default: 7418 ctl_set_invalid_field(/*ctsio*/ ctsio, 7419 /*sks_valid*/ 1, 7420 /*command*/ 1, 7421 /*field*/ 2, 7422 /*bit_valid*/ 1, 7423 /*bit*/ 5); 7424 ctl_done((union ctl_io *)ctsio); 7425 return(retval); 7426 } 7427 7428 if (softc->is_single) 7429 num_target_port_groups = 1; 7430 else 7431 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7432 num_target_ports = 0; 7433 mtx_lock(&softc->ctl_lock); 7434 STAILQ_FOREACH(port, &softc->port_list, links) { 7435 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7436 continue; 7437 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7438 continue; 7439 num_target_ports++; 7440 } 7441 mtx_unlock(&softc->ctl_lock); 7442 7443 if (ext) 7444 total_len = sizeof(struct scsi_target_group_data_extended); 7445 else 7446 total_len = sizeof(struct scsi_target_group_data); 7447 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7448 num_target_port_groups + 7449 sizeof(struct scsi_target_port_descriptor) * 7450 num_target_ports * num_target_port_groups; 7451 7452 alloc_len = scsi_4btoul(cdb->length); 7453 7454 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7455 7456 ctsio->kern_sg_entries = 0; 7457 7458 if (total_len < alloc_len) { 7459 ctsio->residual = alloc_len - total_len; 7460 ctsio->kern_data_len = total_len; 7461 ctsio->kern_total_len = total_len; 7462 } else { 7463 ctsio->residual = 0; 7464 ctsio->kern_data_len = alloc_len; 7465 ctsio->kern_total_len = alloc_len; 7466 } 7467 ctsio->kern_data_resid = 0; 7468 ctsio->kern_rel_offset = 0; 7469 7470 if (ext) { 7471 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7472 ctsio->kern_data_ptr; 7473 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7474 rtg_ext_ptr->format_type = 0x10; 7475 rtg_ext_ptr->implicit_transition_time = 0; 7476 tpg_desc = &rtg_ext_ptr->groups[0]; 7477 } else { 7478 rtg_ptr = (struct scsi_target_group_data *) 7479 ctsio->kern_data_ptr; 7480 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7481 tpg_desc = &rtg_ptr->groups[0]; 7482 } 7483 7484 mtx_lock(&softc->ctl_lock); 7485 pg = softc->port_offset / CTL_MAX_PORTS; 7486 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 7487 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 7488 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7489 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7490 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 7491 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7492 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7493 } else { 7494 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7495 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7496 } 7497 } else { 7498 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7499 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7500 } 7501 for (g = 0; g < num_target_port_groups; g++) { 7502 tpg_desc->pref_state = (g == pg) ? gs : os; 7503 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 7504 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7505 tpg_desc->status = TPG_IMPLICIT; 7506 pc = 0; 7507 STAILQ_FOREACH(port, &softc->port_list, links) { 7508 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7509 continue; 7510 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7511 continue; 7512 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7513 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7514 relative_target_port_identifier); 7515 pc++; 7516 } 7517 tpg_desc->target_port_count = pc; 7518 tpg_desc = (struct scsi_target_port_group_descriptor *) 7519 &tpg_desc->descriptors[pc]; 7520 } 7521 mtx_unlock(&softc->ctl_lock); 7522 7523 ctl_set_success(ctsio); 7524 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7525 ctsio->be_move_done = ctl_config_move_done; 7526 ctl_datamove((union ctl_io *)ctsio); 7527 return(retval); 7528 } 7529 7530 int 7531 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7532 { 7533 struct ctl_lun *lun; 7534 struct scsi_report_supported_opcodes *cdb; 7535 const struct ctl_cmd_entry *entry, *sentry; 7536 struct scsi_report_supported_opcodes_all *all; 7537 struct scsi_report_supported_opcodes_descr *descr; 7538 struct scsi_report_supported_opcodes_one *one; 7539 int retval; 7540 int alloc_len, total_len; 7541 int opcode, service_action, i, j, num; 7542 7543 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7544 7545 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7546 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7547 7548 retval = CTL_RETVAL_COMPLETE; 7549 7550 opcode = cdb->requested_opcode; 7551 service_action = scsi_2btoul(cdb->requested_service_action); 7552 switch (cdb->options & RSO_OPTIONS_MASK) { 7553 case RSO_OPTIONS_ALL: 7554 num = 0; 7555 for (i = 0; i < 256; i++) { 7556 entry = &ctl_cmd_table[i]; 7557 if (entry->flags & CTL_CMD_FLAG_SA5) { 7558 for (j = 0; j < 32; j++) { 7559 sentry = &((const struct ctl_cmd_entry *) 7560 entry->execute)[j]; 7561 if (ctl_cmd_applicable( 7562 lun->be_lun->lun_type, sentry)) 7563 num++; 7564 } 7565 } else { 7566 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7567 entry)) 7568 num++; 7569 } 7570 } 7571 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7572 num * sizeof(struct scsi_report_supported_opcodes_descr); 7573 break; 7574 case RSO_OPTIONS_OC: 7575 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7576 ctl_set_invalid_field(/*ctsio*/ ctsio, 7577 /*sks_valid*/ 1, 7578 /*command*/ 1, 7579 /*field*/ 2, 7580 /*bit_valid*/ 1, 7581 /*bit*/ 2); 7582 ctl_done((union ctl_io *)ctsio); 7583 return (CTL_RETVAL_COMPLETE); 7584 } 7585 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7586 break; 7587 case RSO_OPTIONS_OC_SA: 7588 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7589 service_action >= 32) { 7590 ctl_set_invalid_field(/*ctsio*/ ctsio, 7591 /*sks_valid*/ 1, 7592 /*command*/ 1, 7593 /*field*/ 2, 7594 /*bit_valid*/ 1, 7595 /*bit*/ 2); 7596 ctl_done((union ctl_io *)ctsio); 7597 return (CTL_RETVAL_COMPLETE); 7598 } 7599 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7600 break; 7601 default: 7602 ctl_set_invalid_field(/*ctsio*/ ctsio, 7603 /*sks_valid*/ 1, 7604 /*command*/ 1, 7605 /*field*/ 2, 7606 /*bit_valid*/ 1, 7607 /*bit*/ 2); 7608 ctl_done((union ctl_io *)ctsio); 7609 return (CTL_RETVAL_COMPLETE); 7610 } 7611 7612 alloc_len = scsi_4btoul(cdb->length); 7613 7614 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7615 7616 ctsio->kern_sg_entries = 0; 7617 7618 if (total_len < alloc_len) { 7619 ctsio->residual = alloc_len - total_len; 7620 ctsio->kern_data_len = total_len; 7621 ctsio->kern_total_len = total_len; 7622 } else { 7623 ctsio->residual = 0; 7624 ctsio->kern_data_len = alloc_len; 7625 ctsio->kern_total_len = alloc_len; 7626 } 7627 ctsio->kern_data_resid = 0; 7628 ctsio->kern_rel_offset = 0; 7629 7630 switch (cdb->options & RSO_OPTIONS_MASK) { 7631 case RSO_OPTIONS_ALL: 7632 all = (struct scsi_report_supported_opcodes_all *) 7633 ctsio->kern_data_ptr; 7634 num = 0; 7635 for (i = 0; i < 256; i++) { 7636 entry = &ctl_cmd_table[i]; 7637 if (entry->flags & CTL_CMD_FLAG_SA5) { 7638 for (j = 0; j < 32; j++) { 7639 sentry = &((const struct ctl_cmd_entry *) 7640 entry->execute)[j]; 7641 if (!ctl_cmd_applicable( 7642 lun->be_lun->lun_type, sentry)) 7643 continue; 7644 descr = &all->descr[num++]; 7645 descr->opcode = i; 7646 scsi_ulto2b(j, descr->service_action); 7647 descr->flags = RSO_SERVACTV; 7648 scsi_ulto2b(sentry->length, 7649 descr->cdb_length); 7650 } 7651 } else { 7652 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7653 entry)) 7654 continue; 7655 descr = &all->descr[num++]; 7656 descr->opcode = i; 7657 scsi_ulto2b(0, descr->service_action); 7658 descr->flags = 0; 7659 scsi_ulto2b(entry->length, descr->cdb_length); 7660 } 7661 } 7662 scsi_ulto4b( 7663 num * sizeof(struct scsi_report_supported_opcodes_descr), 7664 all->length); 7665 break; 7666 case RSO_OPTIONS_OC: 7667 one = (struct scsi_report_supported_opcodes_one *) 7668 ctsio->kern_data_ptr; 7669 entry = &ctl_cmd_table[opcode]; 7670 goto fill_one; 7671 case RSO_OPTIONS_OC_SA: 7672 one = (struct scsi_report_supported_opcodes_one *) 7673 ctsio->kern_data_ptr; 7674 entry = &ctl_cmd_table[opcode]; 7675 entry = &((const struct ctl_cmd_entry *) 7676 entry->execute)[service_action]; 7677 fill_one: 7678 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7679 one->support = 3; 7680 scsi_ulto2b(entry->length, one->cdb_length); 7681 one->cdb_usage[0] = opcode; 7682 memcpy(&one->cdb_usage[1], entry->usage, 7683 entry->length - 1); 7684 } else 7685 one->support = 1; 7686 break; 7687 } 7688 7689 ctl_set_success(ctsio); 7690 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7691 ctsio->be_move_done = ctl_config_move_done; 7692 ctl_datamove((union ctl_io *)ctsio); 7693 return(retval); 7694 } 7695 7696 int 7697 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7698 { 7699 struct scsi_report_supported_tmf *cdb; 7700 struct scsi_report_supported_tmf_data *data; 7701 int retval; 7702 int alloc_len, total_len; 7703 7704 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7705 7706 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7707 7708 retval = CTL_RETVAL_COMPLETE; 7709 7710 total_len = sizeof(struct scsi_report_supported_tmf_data); 7711 alloc_len = scsi_4btoul(cdb->length); 7712 7713 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7714 7715 ctsio->kern_sg_entries = 0; 7716 7717 if (total_len < alloc_len) { 7718 ctsio->residual = alloc_len - total_len; 7719 ctsio->kern_data_len = total_len; 7720 ctsio->kern_total_len = total_len; 7721 } else { 7722 ctsio->residual = 0; 7723 ctsio->kern_data_len = alloc_len; 7724 ctsio->kern_total_len = alloc_len; 7725 } 7726 ctsio->kern_data_resid = 0; 7727 ctsio->kern_rel_offset = 0; 7728 7729 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7730 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7731 data->byte2 |= RST_ITNRS; 7732 7733 ctl_set_success(ctsio); 7734 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7735 ctsio->be_move_done = ctl_config_move_done; 7736 ctl_datamove((union ctl_io *)ctsio); 7737 return (retval); 7738 } 7739 7740 int 7741 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7742 { 7743 struct scsi_report_timestamp *cdb; 7744 struct scsi_report_timestamp_data *data; 7745 struct timeval tv; 7746 int64_t timestamp; 7747 int retval; 7748 int alloc_len, total_len; 7749 7750 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7751 7752 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7753 7754 retval = CTL_RETVAL_COMPLETE; 7755 7756 total_len = sizeof(struct scsi_report_timestamp_data); 7757 alloc_len = scsi_4btoul(cdb->length); 7758 7759 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7760 7761 ctsio->kern_sg_entries = 0; 7762 7763 if (total_len < alloc_len) { 7764 ctsio->residual = alloc_len - total_len; 7765 ctsio->kern_data_len = total_len; 7766 ctsio->kern_total_len = total_len; 7767 } else { 7768 ctsio->residual = 0; 7769 ctsio->kern_data_len = alloc_len; 7770 ctsio->kern_total_len = alloc_len; 7771 } 7772 ctsio->kern_data_resid = 0; 7773 ctsio->kern_rel_offset = 0; 7774 7775 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7776 scsi_ulto2b(sizeof(*data) - 2, data->length); 7777 data->origin = RTS_ORIG_OUTSIDE; 7778 getmicrotime(&tv); 7779 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7780 scsi_ulto4b(timestamp >> 16, data->timestamp); 7781 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7782 7783 ctl_set_success(ctsio); 7784 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7785 ctsio->be_move_done = ctl_config_move_done; 7786 ctl_datamove((union ctl_io *)ctsio); 7787 return (retval); 7788 } 7789 7790 int 7791 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7792 { 7793 struct scsi_per_res_in *cdb; 7794 int alloc_len, total_len = 0; 7795 /* struct scsi_per_res_in_rsrv in_data; */ 7796 struct ctl_lun *lun; 7797 struct ctl_softc *softc; 7798 uint64_t key; 7799 7800 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7801 7802 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7803 7804 alloc_len = scsi_2btoul(cdb->length); 7805 7806 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7807 softc = lun->ctl_softc; 7808 7809 retry: 7810 mtx_lock(&lun->lun_lock); 7811 switch (cdb->action) { 7812 case SPRI_RK: /* read keys */ 7813 total_len = sizeof(struct scsi_per_res_in_keys) + 7814 lun->pr_key_count * 7815 sizeof(struct scsi_per_res_key); 7816 break; 7817 case SPRI_RR: /* read reservation */ 7818 if (lun->flags & CTL_LUN_PR_RESERVED) 7819 total_len = sizeof(struct scsi_per_res_in_rsrv); 7820 else 7821 total_len = sizeof(struct scsi_per_res_in_header); 7822 break; 7823 case SPRI_RC: /* report capabilities */ 7824 total_len = sizeof(struct scsi_per_res_cap); 7825 break; 7826 case SPRI_RS: /* read full status */ 7827 total_len = sizeof(struct scsi_per_res_in_header) + 7828 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7829 lun->pr_key_count; 7830 break; 7831 default: 7832 panic("Invalid PR type %x", cdb->action); 7833 } 7834 mtx_unlock(&lun->lun_lock); 7835 7836 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7837 7838 if (total_len < alloc_len) { 7839 ctsio->residual = alloc_len - total_len; 7840 ctsio->kern_data_len = total_len; 7841 ctsio->kern_total_len = total_len; 7842 } else { 7843 ctsio->residual = 0; 7844 ctsio->kern_data_len = alloc_len; 7845 ctsio->kern_total_len = alloc_len; 7846 } 7847 7848 ctsio->kern_data_resid = 0; 7849 ctsio->kern_rel_offset = 0; 7850 ctsio->kern_sg_entries = 0; 7851 7852 mtx_lock(&lun->lun_lock); 7853 switch (cdb->action) { 7854 case SPRI_RK: { // read keys 7855 struct scsi_per_res_in_keys *res_keys; 7856 int i, key_count; 7857 7858 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7859 7860 /* 7861 * We had to drop the lock to allocate our buffer, which 7862 * leaves time for someone to come in with another 7863 * persistent reservation. (That is unlikely, though, 7864 * since this should be the only persistent reservation 7865 * command active right now.) 7866 */ 7867 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7868 (lun->pr_key_count * 7869 sizeof(struct scsi_per_res_key)))){ 7870 mtx_unlock(&lun->lun_lock); 7871 free(ctsio->kern_data_ptr, M_CTL); 7872 printf("%s: reservation length changed, retrying\n", 7873 __func__); 7874 goto retry; 7875 } 7876 7877 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7878 7879 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7880 lun->pr_key_count, res_keys->header.length); 7881 7882 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7883 if ((key = ctl_get_prkey(lun, i)) == 0) 7884 continue; 7885 7886 /* 7887 * We used lun->pr_key_count to calculate the 7888 * size to allocate. If it turns out the number of 7889 * initiators with the registered flag set is 7890 * larger than that (i.e. they haven't been kept in 7891 * sync), we've got a problem. 7892 */ 7893 if (key_count >= lun->pr_key_count) { 7894 #ifdef NEEDTOPORT 7895 csevent_log(CSC_CTL | CSC_SHELF_SW | 7896 CTL_PR_ERROR, 7897 csevent_LogType_Fault, 7898 csevent_AlertLevel_Yellow, 7899 csevent_FRU_ShelfController, 7900 csevent_FRU_Firmware, 7901 csevent_FRU_Unknown, 7902 "registered keys %d >= key " 7903 "count %d", key_count, 7904 lun->pr_key_count); 7905 #endif 7906 key_count++; 7907 continue; 7908 } 7909 scsi_u64to8b(key, res_keys->keys[key_count].key); 7910 key_count++; 7911 } 7912 break; 7913 } 7914 case SPRI_RR: { // read reservation 7915 struct scsi_per_res_in_rsrv *res; 7916 int tmp_len, header_only; 7917 7918 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7919 7920 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7921 7922 if (lun->flags & CTL_LUN_PR_RESERVED) 7923 { 7924 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7925 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7926 res->header.length); 7927 header_only = 0; 7928 } else { 7929 tmp_len = sizeof(struct scsi_per_res_in_header); 7930 scsi_ulto4b(0, res->header.length); 7931 header_only = 1; 7932 } 7933 7934 /* 7935 * We had to drop the lock to allocate our buffer, which 7936 * leaves time for someone to come in with another 7937 * persistent reservation. (That is unlikely, though, 7938 * since this should be the only persistent reservation 7939 * command active right now.) 7940 */ 7941 if (tmp_len != total_len) { 7942 mtx_unlock(&lun->lun_lock); 7943 free(ctsio->kern_data_ptr, M_CTL); 7944 printf("%s: reservation status changed, retrying\n", 7945 __func__); 7946 goto retry; 7947 } 7948 7949 /* 7950 * No reservation held, so we're done. 7951 */ 7952 if (header_only != 0) 7953 break; 7954 7955 /* 7956 * If the registration is an All Registrants type, the key 7957 * is 0, since it doesn't really matter. 7958 */ 7959 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7960 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7961 res->data.reservation); 7962 } 7963 res->data.scopetype = lun->res_type; 7964 break; 7965 } 7966 case SPRI_RC: //report capabilities 7967 { 7968 struct scsi_per_res_cap *res_cap; 7969 uint16_t type_mask; 7970 7971 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7972 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7973 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7974 type_mask = SPRI_TM_WR_EX_AR | 7975 SPRI_TM_EX_AC_RO | 7976 SPRI_TM_WR_EX_RO | 7977 SPRI_TM_EX_AC | 7978 SPRI_TM_WR_EX | 7979 SPRI_TM_EX_AC_AR; 7980 scsi_ulto2b(type_mask, res_cap->type_mask); 7981 break; 7982 } 7983 case SPRI_RS: { // read full status 7984 struct scsi_per_res_in_full *res_status; 7985 struct scsi_per_res_in_full_desc *res_desc; 7986 struct ctl_port *port; 7987 int i, len; 7988 7989 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7990 7991 /* 7992 * We had to drop the lock to allocate our buffer, which 7993 * leaves time for someone to come in with another 7994 * persistent reservation. (That is unlikely, though, 7995 * since this should be the only persistent reservation 7996 * command active right now.) 7997 */ 7998 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7999 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 8000 lun->pr_key_count)){ 8001 mtx_unlock(&lun->lun_lock); 8002 free(ctsio->kern_data_ptr, M_CTL); 8003 printf("%s: reservation length changed, retrying\n", 8004 __func__); 8005 goto retry; 8006 } 8007 8008 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 8009 8010 res_desc = &res_status->desc[0]; 8011 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 8012 if ((key = ctl_get_prkey(lun, i)) == 0) 8013 continue; 8014 8015 scsi_u64to8b(key, res_desc->res_key.key); 8016 if ((lun->flags & CTL_LUN_PR_RESERVED) && 8017 (lun->pr_res_idx == i || 8018 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 8019 res_desc->flags = SPRI_FULL_R_HOLDER; 8020 res_desc->scopetype = lun->res_type; 8021 } 8022 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 8023 res_desc->rel_trgt_port_id); 8024 len = 0; 8025 port = softc->ctl_ports[ 8026 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 8027 if (port != NULL) 8028 len = ctl_create_iid(port, 8029 i % CTL_MAX_INIT_PER_PORT, 8030 res_desc->transport_id); 8031 scsi_ulto4b(len, res_desc->additional_length); 8032 res_desc = (struct scsi_per_res_in_full_desc *) 8033 &res_desc->transport_id[len]; 8034 } 8035 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 8036 res_status->header.length); 8037 break; 8038 } 8039 default: 8040 /* 8041 * This is a bug, because we just checked for this above, 8042 * and should have returned an error. 8043 */ 8044 panic("Invalid PR type %x", cdb->action); 8045 break; /* NOTREACHED */ 8046 } 8047 mtx_unlock(&lun->lun_lock); 8048 8049 ctl_set_success(ctsio); 8050 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8051 ctsio->be_move_done = ctl_config_move_done; 8052 ctl_datamove((union ctl_io *)ctsio); 8053 return (CTL_RETVAL_COMPLETE); 8054 } 8055 8056 static void 8057 ctl_est_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua) 8058 { 8059 int off = lun->ctl_softc->persis_offset; 8060 8061 if (residx >= off && residx < off + CTL_MAX_INITIATORS) 8062 ctl_est_ua(lun, residx - off, ua); 8063 } 8064 8065 /* 8066 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 8067 * it should return. 8068 */ 8069 static int 8070 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 8071 uint64_t sa_res_key, uint8_t type, uint32_t residx, 8072 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 8073 struct scsi_per_res_out_parms* param) 8074 { 8075 union ctl_ha_msg persis_io; 8076 int retval, i; 8077 int isc_retval; 8078 8079 retval = 0; 8080 8081 mtx_lock(&lun->lun_lock); 8082 if (sa_res_key == 0) { 8083 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8084 /* validate scope and type */ 8085 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8086 SPR_LU_SCOPE) { 8087 mtx_unlock(&lun->lun_lock); 8088 ctl_set_invalid_field(/*ctsio*/ ctsio, 8089 /*sks_valid*/ 1, 8090 /*command*/ 1, 8091 /*field*/ 2, 8092 /*bit_valid*/ 1, 8093 /*bit*/ 4); 8094 ctl_done((union ctl_io *)ctsio); 8095 return (1); 8096 } 8097 8098 if (type>8 || type==2 || type==4 || type==0) { 8099 mtx_unlock(&lun->lun_lock); 8100 ctl_set_invalid_field(/*ctsio*/ ctsio, 8101 /*sks_valid*/ 1, 8102 /*command*/ 1, 8103 /*field*/ 2, 8104 /*bit_valid*/ 1, 8105 /*bit*/ 0); 8106 ctl_done((union ctl_io *)ctsio); 8107 return (1); 8108 } 8109 8110 /* 8111 * Unregister everybody else and build UA for 8112 * them 8113 */ 8114 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8115 if (i == residx || ctl_get_prkey(lun, i) == 0) 8116 continue; 8117 8118 ctl_clr_prkey(lun, i); 8119 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8120 } 8121 lun->pr_key_count = 1; 8122 lun->res_type = type; 8123 if (lun->res_type != SPR_TYPE_WR_EX_AR 8124 && lun->res_type != SPR_TYPE_EX_AC_AR) 8125 lun->pr_res_idx = residx; 8126 8127 /* send msg to other side */ 8128 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8129 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8130 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8131 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8132 persis_io.pr.pr_info.res_type = type; 8133 memcpy(persis_io.pr.pr_info.sa_res_key, 8134 param->serv_act_res_key, 8135 sizeof(param->serv_act_res_key)); 8136 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8137 &persis_io, sizeof(persis_io), 0)) > 8138 CTL_HA_STATUS_SUCCESS) { 8139 printf("CTL:Persis Out error returned " 8140 "from ctl_ha_msg_send %d\n", 8141 isc_retval); 8142 } 8143 } else { 8144 /* not all registrants */ 8145 mtx_unlock(&lun->lun_lock); 8146 free(ctsio->kern_data_ptr, M_CTL); 8147 ctl_set_invalid_field(ctsio, 8148 /*sks_valid*/ 1, 8149 /*command*/ 0, 8150 /*field*/ 8, 8151 /*bit_valid*/ 0, 8152 /*bit*/ 0); 8153 ctl_done((union ctl_io *)ctsio); 8154 return (1); 8155 } 8156 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8157 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8158 int found = 0; 8159 8160 if (res_key == sa_res_key) { 8161 /* special case */ 8162 /* 8163 * The spec implies this is not good but doesn't 8164 * say what to do. There are two choices either 8165 * generate a res conflict or check condition 8166 * with illegal field in parameter data. Since 8167 * that is what is done when the sa_res_key is 8168 * zero I'll take that approach since this has 8169 * to do with the sa_res_key. 8170 */ 8171 mtx_unlock(&lun->lun_lock); 8172 free(ctsio->kern_data_ptr, M_CTL); 8173 ctl_set_invalid_field(ctsio, 8174 /*sks_valid*/ 1, 8175 /*command*/ 0, 8176 /*field*/ 8, 8177 /*bit_valid*/ 0, 8178 /*bit*/ 0); 8179 ctl_done((union ctl_io *)ctsio); 8180 return (1); 8181 } 8182 8183 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8184 if (ctl_get_prkey(lun, i) != sa_res_key) 8185 continue; 8186 8187 found = 1; 8188 ctl_clr_prkey(lun, i); 8189 lun->pr_key_count--; 8190 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8191 } 8192 if (!found) { 8193 mtx_unlock(&lun->lun_lock); 8194 free(ctsio->kern_data_ptr, M_CTL); 8195 ctl_set_reservation_conflict(ctsio); 8196 ctl_done((union ctl_io *)ctsio); 8197 return (CTL_RETVAL_COMPLETE); 8198 } 8199 /* send msg to other side */ 8200 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8201 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8202 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8203 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8204 persis_io.pr.pr_info.res_type = type; 8205 memcpy(persis_io.pr.pr_info.sa_res_key, 8206 param->serv_act_res_key, 8207 sizeof(param->serv_act_res_key)); 8208 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8209 &persis_io, sizeof(persis_io), 0)) > 8210 CTL_HA_STATUS_SUCCESS) { 8211 printf("CTL:Persis Out error returned from " 8212 "ctl_ha_msg_send %d\n", isc_retval); 8213 } 8214 } else { 8215 /* Reserved but not all registrants */ 8216 /* sa_res_key is res holder */ 8217 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 8218 /* validate scope and type */ 8219 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8220 SPR_LU_SCOPE) { 8221 mtx_unlock(&lun->lun_lock); 8222 ctl_set_invalid_field(/*ctsio*/ ctsio, 8223 /*sks_valid*/ 1, 8224 /*command*/ 1, 8225 /*field*/ 2, 8226 /*bit_valid*/ 1, 8227 /*bit*/ 4); 8228 ctl_done((union ctl_io *)ctsio); 8229 return (1); 8230 } 8231 8232 if (type>8 || type==2 || type==4 || type==0) { 8233 mtx_unlock(&lun->lun_lock); 8234 ctl_set_invalid_field(/*ctsio*/ ctsio, 8235 /*sks_valid*/ 1, 8236 /*command*/ 1, 8237 /*field*/ 2, 8238 /*bit_valid*/ 1, 8239 /*bit*/ 0); 8240 ctl_done((union ctl_io *)ctsio); 8241 return (1); 8242 } 8243 8244 /* 8245 * Do the following: 8246 * if sa_res_key != res_key remove all 8247 * registrants w/sa_res_key and generate UA 8248 * for these registrants(Registrations 8249 * Preempted) if it wasn't an exclusive 8250 * reservation generate UA(Reservations 8251 * Preempted) for all other registered nexuses 8252 * if the type has changed. Establish the new 8253 * reservation and holder. If res_key and 8254 * sa_res_key are the same do the above 8255 * except don't unregister the res holder. 8256 */ 8257 8258 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8259 if (i == residx || ctl_get_prkey(lun, i) == 0) 8260 continue; 8261 8262 if (sa_res_key == ctl_get_prkey(lun, i)) { 8263 ctl_clr_prkey(lun, i); 8264 lun->pr_key_count--; 8265 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8266 } else if (type != lun->res_type 8267 && (lun->res_type == SPR_TYPE_WR_EX_RO 8268 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8269 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 8270 } 8271 } 8272 lun->res_type = type; 8273 if (lun->res_type != SPR_TYPE_WR_EX_AR 8274 && lun->res_type != SPR_TYPE_EX_AC_AR) 8275 lun->pr_res_idx = residx; 8276 else 8277 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8278 8279 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8280 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8281 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8282 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8283 persis_io.pr.pr_info.res_type = type; 8284 memcpy(persis_io.pr.pr_info.sa_res_key, 8285 param->serv_act_res_key, 8286 sizeof(param->serv_act_res_key)); 8287 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8288 &persis_io, sizeof(persis_io), 0)) > 8289 CTL_HA_STATUS_SUCCESS) { 8290 printf("CTL:Persis Out error returned " 8291 "from ctl_ha_msg_send %d\n", 8292 isc_retval); 8293 } 8294 } else { 8295 /* 8296 * sa_res_key is not the res holder just 8297 * remove registrants 8298 */ 8299 int found=0; 8300 8301 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8302 if (sa_res_key != ctl_get_prkey(lun, i)) 8303 continue; 8304 8305 found = 1; 8306 ctl_clr_prkey(lun, i); 8307 lun->pr_key_count--; 8308 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8309 } 8310 8311 if (!found) { 8312 mtx_unlock(&lun->lun_lock); 8313 free(ctsio->kern_data_ptr, M_CTL); 8314 ctl_set_reservation_conflict(ctsio); 8315 ctl_done((union ctl_io *)ctsio); 8316 return (1); 8317 } 8318 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8319 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8320 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8321 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8322 persis_io.pr.pr_info.res_type = type; 8323 memcpy(persis_io.pr.pr_info.sa_res_key, 8324 param->serv_act_res_key, 8325 sizeof(param->serv_act_res_key)); 8326 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8327 &persis_io, sizeof(persis_io), 0)) > 8328 CTL_HA_STATUS_SUCCESS) { 8329 printf("CTL:Persis Out error returned " 8330 "from ctl_ha_msg_send %d\n", 8331 isc_retval); 8332 } 8333 } 8334 } 8335 8336 lun->PRGeneration++; 8337 mtx_unlock(&lun->lun_lock); 8338 8339 return (retval); 8340 } 8341 8342 static void 8343 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8344 { 8345 uint64_t sa_res_key; 8346 int i; 8347 8348 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8349 8350 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8351 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8352 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8353 if (sa_res_key == 0) { 8354 /* 8355 * Unregister everybody else and build UA for 8356 * them 8357 */ 8358 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8359 if (i == msg->pr.pr_info.residx || 8360 ctl_get_prkey(lun, i) == 0) 8361 continue; 8362 8363 ctl_clr_prkey(lun, i); 8364 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8365 } 8366 8367 lun->pr_key_count = 1; 8368 lun->res_type = msg->pr.pr_info.res_type; 8369 if (lun->res_type != SPR_TYPE_WR_EX_AR 8370 && lun->res_type != SPR_TYPE_EX_AC_AR) 8371 lun->pr_res_idx = msg->pr.pr_info.residx; 8372 } else { 8373 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8374 if (sa_res_key == ctl_get_prkey(lun, i)) 8375 continue; 8376 8377 ctl_clr_prkey(lun, i); 8378 lun->pr_key_count--; 8379 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8380 } 8381 } 8382 } else { 8383 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8384 if (i == msg->pr.pr_info.residx || 8385 ctl_get_prkey(lun, i) == 0) 8386 continue; 8387 8388 if (sa_res_key == ctl_get_prkey(lun, i)) { 8389 ctl_clr_prkey(lun, i); 8390 lun->pr_key_count--; 8391 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8392 } else if (msg->pr.pr_info.res_type != lun->res_type 8393 && (lun->res_type == SPR_TYPE_WR_EX_RO 8394 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8395 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 8396 } 8397 } 8398 lun->res_type = msg->pr.pr_info.res_type; 8399 if (lun->res_type != SPR_TYPE_WR_EX_AR 8400 && lun->res_type != SPR_TYPE_EX_AC_AR) 8401 lun->pr_res_idx = msg->pr.pr_info.residx; 8402 else 8403 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8404 } 8405 lun->PRGeneration++; 8406 8407 } 8408 8409 8410 int 8411 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8412 { 8413 int retval; 8414 int isc_retval; 8415 u_int32_t param_len; 8416 struct scsi_per_res_out *cdb; 8417 struct ctl_lun *lun; 8418 struct scsi_per_res_out_parms* param; 8419 struct ctl_softc *softc; 8420 uint32_t residx; 8421 uint64_t res_key, sa_res_key, key; 8422 uint8_t type; 8423 union ctl_ha_msg persis_io; 8424 int i; 8425 8426 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8427 8428 retval = CTL_RETVAL_COMPLETE; 8429 8430 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8431 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8432 softc = lun->ctl_softc; 8433 8434 /* 8435 * We only support whole-LUN scope. The scope & type are ignored for 8436 * register, register and ignore existing key and clear. 8437 * We sometimes ignore scope and type on preempts too!! 8438 * Verify reservation type here as well. 8439 */ 8440 type = cdb->scope_type & SPR_TYPE_MASK; 8441 if ((cdb->action == SPRO_RESERVE) 8442 || (cdb->action == SPRO_RELEASE)) { 8443 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8444 ctl_set_invalid_field(/*ctsio*/ ctsio, 8445 /*sks_valid*/ 1, 8446 /*command*/ 1, 8447 /*field*/ 2, 8448 /*bit_valid*/ 1, 8449 /*bit*/ 4); 8450 ctl_done((union ctl_io *)ctsio); 8451 return (CTL_RETVAL_COMPLETE); 8452 } 8453 8454 if (type>8 || type==2 || type==4 || type==0) { 8455 ctl_set_invalid_field(/*ctsio*/ ctsio, 8456 /*sks_valid*/ 1, 8457 /*command*/ 1, 8458 /*field*/ 2, 8459 /*bit_valid*/ 1, 8460 /*bit*/ 0); 8461 ctl_done((union ctl_io *)ctsio); 8462 return (CTL_RETVAL_COMPLETE); 8463 } 8464 } 8465 8466 param_len = scsi_4btoul(cdb->length); 8467 8468 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8469 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8470 ctsio->kern_data_len = param_len; 8471 ctsio->kern_total_len = param_len; 8472 ctsio->kern_data_resid = 0; 8473 ctsio->kern_rel_offset = 0; 8474 ctsio->kern_sg_entries = 0; 8475 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8476 ctsio->be_move_done = ctl_config_move_done; 8477 ctl_datamove((union ctl_io *)ctsio); 8478 8479 return (CTL_RETVAL_COMPLETE); 8480 } 8481 8482 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8483 8484 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8485 res_key = scsi_8btou64(param->res_key.key); 8486 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8487 8488 /* 8489 * Validate the reservation key here except for SPRO_REG_IGNO 8490 * This must be done for all other service actions 8491 */ 8492 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8493 mtx_lock(&lun->lun_lock); 8494 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8495 if (res_key != key) { 8496 /* 8497 * The current key passed in doesn't match 8498 * the one the initiator previously 8499 * registered. 8500 */ 8501 mtx_unlock(&lun->lun_lock); 8502 free(ctsio->kern_data_ptr, M_CTL); 8503 ctl_set_reservation_conflict(ctsio); 8504 ctl_done((union ctl_io *)ctsio); 8505 return (CTL_RETVAL_COMPLETE); 8506 } 8507 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8508 /* 8509 * We are not registered 8510 */ 8511 mtx_unlock(&lun->lun_lock); 8512 free(ctsio->kern_data_ptr, M_CTL); 8513 ctl_set_reservation_conflict(ctsio); 8514 ctl_done((union ctl_io *)ctsio); 8515 return (CTL_RETVAL_COMPLETE); 8516 } else if (res_key != 0) { 8517 /* 8518 * We are not registered and trying to register but 8519 * the register key isn't zero. 8520 */ 8521 mtx_unlock(&lun->lun_lock); 8522 free(ctsio->kern_data_ptr, M_CTL); 8523 ctl_set_reservation_conflict(ctsio); 8524 ctl_done((union ctl_io *)ctsio); 8525 return (CTL_RETVAL_COMPLETE); 8526 } 8527 mtx_unlock(&lun->lun_lock); 8528 } 8529 8530 switch (cdb->action & SPRO_ACTION_MASK) { 8531 case SPRO_REGISTER: 8532 case SPRO_REG_IGNO: { 8533 8534 #if 0 8535 printf("Registration received\n"); 8536 #endif 8537 8538 /* 8539 * We don't support any of these options, as we report in 8540 * the read capabilities request (see 8541 * ctl_persistent_reserve_in(), above). 8542 */ 8543 if ((param->flags & SPR_SPEC_I_PT) 8544 || (param->flags & SPR_ALL_TG_PT) 8545 || (param->flags & SPR_APTPL)) { 8546 int bit_ptr; 8547 8548 if (param->flags & SPR_APTPL) 8549 bit_ptr = 0; 8550 else if (param->flags & SPR_ALL_TG_PT) 8551 bit_ptr = 2; 8552 else /* SPR_SPEC_I_PT */ 8553 bit_ptr = 3; 8554 8555 free(ctsio->kern_data_ptr, M_CTL); 8556 ctl_set_invalid_field(ctsio, 8557 /*sks_valid*/ 1, 8558 /*command*/ 0, 8559 /*field*/ 20, 8560 /*bit_valid*/ 1, 8561 /*bit*/ bit_ptr); 8562 ctl_done((union ctl_io *)ctsio); 8563 return (CTL_RETVAL_COMPLETE); 8564 } 8565 8566 mtx_lock(&lun->lun_lock); 8567 8568 /* 8569 * The initiator wants to clear the 8570 * key/unregister. 8571 */ 8572 if (sa_res_key == 0) { 8573 if ((res_key == 0 8574 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8575 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8576 && ctl_get_prkey(lun, residx) == 0)) { 8577 mtx_unlock(&lun->lun_lock); 8578 goto done; 8579 } 8580 8581 ctl_clr_prkey(lun, residx); 8582 lun->pr_key_count--; 8583 8584 if (residx == lun->pr_res_idx) { 8585 lun->flags &= ~CTL_LUN_PR_RESERVED; 8586 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8587 8588 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8589 || lun->res_type == SPR_TYPE_EX_AC_RO) 8590 && lun->pr_key_count) { 8591 /* 8592 * If the reservation is a registrants 8593 * only type we need to generate a UA 8594 * for other registered inits. The 8595 * sense code should be RESERVATIONS 8596 * RELEASED 8597 */ 8598 8599 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8600 if (ctl_get_prkey(lun, i + 8601 softc->persis_offset) == 0) 8602 continue; 8603 ctl_est_ua(lun, i, 8604 CTL_UA_RES_RELEASE); 8605 } 8606 } 8607 lun->res_type = 0; 8608 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8609 if (lun->pr_key_count==0) { 8610 lun->flags &= ~CTL_LUN_PR_RESERVED; 8611 lun->res_type = 0; 8612 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8613 } 8614 } 8615 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8616 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8617 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8618 persis_io.pr.pr_info.residx = residx; 8619 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8620 &persis_io, sizeof(persis_io), 0 )) > 8621 CTL_HA_STATUS_SUCCESS) { 8622 printf("CTL:Persis Out error returned from " 8623 "ctl_ha_msg_send %d\n", isc_retval); 8624 } 8625 } else /* sa_res_key != 0 */ { 8626 8627 /* 8628 * If we aren't registered currently then increment 8629 * the key count and set the registered flag. 8630 */ 8631 ctl_alloc_prkey(lun, residx); 8632 if (ctl_get_prkey(lun, residx) == 0) 8633 lun->pr_key_count++; 8634 ctl_set_prkey(lun, residx, sa_res_key); 8635 8636 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8637 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8638 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8639 persis_io.pr.pr_info.residx = residx; 8640 memcpy(persis_io.pr.pr_info.sa_res_key, 8641 param->serv_act_res_key, 8642 sizeof(param->serv_act_res_key)); 8643 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8644 &persis_io, sizeof(persis_io), 0)) > 8645 CTL_HA_STATUS_SUCCESS) { 8646 printf("CTL:Persis Out error returned from " 8647 "ctl_ha_msg_send %d\n", isc_retval); 8648 } 8649 } 8650 lun->PRGeneration++; 8651 mtx_unlock(&lun->lun_lock); 8652 8653 break; 8654 } 8655 case SPRO_RESERVE: 8656 #if 0 8657 printf("Reserve executed type %d\n", type); 8658 #endif 8659 mtx_lock(&lun->lun_lock); 8660 if (lun->flags & CTL_LUN_PR_RESERVED) { 8661 /* 8662 * if this isn't the reservation holder and it's 8663 * not a "all registrants" type or if the type is 8664 * different then we have a conflict 8665 */ 8666 if ((lun->pr_res_idx != residx 8667 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8668 || lun->res_type != type) { 8669 mtx_unlock(&lun->lun_lock); 8670 free(ctsio->kern_data_ptr, M_CTL); 8671 ctl_set_reservation_conflict(ctsio); 8672 ctl_done((union ctl_io *)ctsio); 8673 return (CTL_RETVAL_COMPLETE); 8674 } 8675 mtx_unlock(&lun->lun_lock); 8676 } else /* create a reservation */ { 8677 /* 8678 * If it's not an "all registrants" type record 8679 * reservation holder 8680 */ 8681 if (type != SPR_TYPE_WR_EX_AR 8682 && type != SPR_TYPE_EX_AC_AR) 8683 lun->pr_res_idx = residx; /* Res holder */ 8684 else 8685 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8686 8687 lun->flags |= CTL_LUN_PR_RESERVED; 8688 lun->res_type = type; 8689 8690 mtx_unlock(&lun->lun_lock); 8691 8692 /* send msg to other side */ 8693 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8694 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8695 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8696 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8697 persis_io.pr.pr_info.res_type = type; 8698 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8699 &persis_io, sizeof(persis_io), 0)) > 8700 CTL_HA_STATUS_SUCCESS) { 8701 printf("CTL:Persis Out error returned from " 8702 "ctl_ha_msg_send %d\n", isc_retval); 8703 } 8704 } 8705 break; 8706 8707 case SPRO_RELEASE: 8708 mtx_lock(&lun->lun_lock); 8709 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8710 /* No reservation exists return good status */ 8711 mtx_unlock(&lun->lun_lock); 8712 goto done; 8713 } 8714 /* 8715 * Is this nexus a reservation holder? 8716 */ 8717 if (lun->pr_res_idx != residx 8718 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8719 /* 8720 * not a res holder return good status but 8721 * do nothing 8722 */ 8723 mtx_unlock(&lun->lun_lock); 8724 goto done; 8725 } 8726 8727 if (lun->res_type != type) { 8728 mtx_unlock(&lun->lun_lock); 8729 free(ctsio->kern_data_ptr, M_CTL); 8730 ctl_set_illegal_pr_release(ctsio); 8731 ctl_done((union ctl_io *)ctsio); 8732 return (CTL_RETVAL_COMPLETE); 8733 } 8734 8735 /* okay to release */ 8736 lun->flags &= ~CTL_LUN_PR_RESERVED; 8737 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8738 lun->res_type = 0; 8739 8740 /* 8741 * if this isn't an exclusive access 8742 * res generate UA for all other 8743 * registrants. 8744 */ 8745 if (type != SPR_TYPE_EX_AC 8746 && type != SPR_TYPE_WR_EX) { 8747 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8748 if (i == residx || 8749 ctl_get_prkey(lun, 8750 i + softc->persis_offset) == 0) 8751 continue; 8752 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8753 } 8754 } 8755 mtx_unlock(&lun->lun_lock); 8756 /* Send msg to other side */ 8757 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8758 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8759 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8760 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8761 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8762 printf("CTL:Persis Out error returned from " 8763 "ctl_ha_msg_send %d\n", isc_retval); 8764 } 8765 break; 8766 8767 case SPRO_CLEAR: 8768 /* send msg to other side */ 8769 8770 mtx_lock(&lun->lun_lock); 8771 lun->flags &= ~CTL_LUN_PR_RESERVED; 8772 lun->res_type = 0; 8773 lun->pr_key_count = 0; 8774 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8775 8776 ctl_clr_prkey(lun, residx); 8777 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8778 if (ctl_get_prkey(lun, i) != 0) { 8779 ctl_clr_prkey(lun, i); 8780 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8781 } 8782 lun->PRGeneration++; 8783 mtx_unlock(&lun->lun_lock); 8784 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8785 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8786 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8787 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8788 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8789 printf("CTL:Persis Out error returned from " 8790 "ctl_ha_msg_send %d\n", isc_retval); 8791 } 8792 break; 8793 8794 case SPRO_PREEMPT: 8795 case SPRO_PRE_ABO: { 8796 int nretval; 8797 8798 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8799 residx, ctsio, cdb, param); 8800 if (nretval != 0) 8801 return (CTL_RETVAL_COMPLETE); 8802 break; 8803 } 8804 default: 8805 panic("Invalid PR type %x", cdb->action); 8806 } 8807 8808 done: 8809 free(ctsio->kern_data_ptr, M_CTL); 8810 ctl_set_success(ctsio); 8811 ctl_done((union ctl_io *)ctsio); 8812 8813 return (retval); 8814 } 8815 8816 /* 8817 * This routine is for handling a message from the other SC pertaining to 8818 * persistent reserve out. All the error checking will have been done 8819 * so only perorming the action need be done here to keep the two 8820 * in sync. 8821 */ 8822 static void 8823 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8824 { 8825 struct ctl_lun *lun; 8826 struct ctl_softc *softc; 8827 int i; 8828 uint32_t targ_lun; 8829 8830 softc = control_softc; 8831 8832 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8833 lun = softc->ctl_luns[targ_lun]; 8834 mtx_lock(&lun->lun_lock); 8835 switch(msg->pr.pr_info.action) { 8836 case CTL_PR_REG_KEY: 8837 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8838 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8839 lun->pr_key_count++; 8840 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8841 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8842 lun->PRGeneration++; 8843 break; 8844 8845 case CTL_PR_UNREG_KEY: 8846 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8847 lun->pr_key_count--; 8848 8849 /* XXX Need to see if the reservation has been released */ 8850 /* if so do we need to generate UA? */ 8851 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8852 lun->flags &= ~CTL_LUN_PR_RESERVED; 8853 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8854 8855 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8856 || lun->res_type == SPR_TYPE_EX_AC_RO) 8857 && lun->pr_key_count) { 8858 /* 8859 * If the reservation is a registrants 8860 * only type we need to generate a UA 8861 * for other registered inits. The 8862 * sense code should be RESERVATIONS 8863 * RELEASED 8864 */ 8865 8866 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8867 if (ctl_get_prkey(lun, i + 8868 softc->persis_offset) == 0) 8869 continue; 8870 8871 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8872 } 8873 } 8874 lun->res_type = 0; 8875 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8876 if (lun->pr_key_count==0) { 8877 lun->flags &= ~CTL_LUN_PR_RESERVED; 8878 lun->res_type = 0; 8879 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8880 } 8881 } 8882 lun->PRGeneration++; 8883 break; 8884 8885 case CTL_PR_RESERVE: 8886 lun->flags |= CTL_LUN_PR_RESERVED; 8887 lun->res_type = msg->pr.pr_info.res_type; 8888 lun->pr_res_idx = msg->pr.pr_info.residx; 8889 8890 break; 8891 8892 case CTL_PR_RELEASE: 8893 /* 8894 * if this isn't an exclusive access res generate UA for all 8895 * other registrants. 8896 */ 8897 if (lun->res_type != SPR_TYPE_EX_AC 8898 && lun->res_type != SPR_TYPE_WR_EX) { 8899 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8900 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0) 8901 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8902 } 8903 8904 lun->flags &= ~CTL_LUN_PR_RESERVED; 8905 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8906 lun->res_type = 0; 8907 break; 8908 8909 case CTL_PR_PREEMPT: 8910 ctl_pro_preempt_other(lun, msg); 8911 break; 8912 case CTL_PR_CLEAR: 8913 lun->flags &= ~CTL_LUN_PR_RESERVED; 8914 lun->res_type = 0; 8915 lun->pr_key_count = 0; 8916 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8917 8918 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8919 if (ctl_get_prkey(lun, i) == 0) 8920 continue; 8921 ctl_clr_prkey(lun, i); 8922 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8923 } 8924 lun->PRGeneration++; 8925 break; 8926 } 8927 8928 mtx_unlock(&lun->lun_lock); 8929 } 8930 8931 int 8932 ctl_read_write(struct ctl_scsiio *ctsio) 8933 { 8934 struct ctl_lun *lun; 8935 struct ctl_lba_len_flags *lbalen; 8936 uint64_t lba; 8937 uint32_t num_blocks; 8938 int flags, retval; 8939 int isread; 8940 8941 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8942 8943 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8944 8945 flags = 0; 8946 retval = CTL_RETVAL_COMPLETE; 8947 8948 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8949 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8950 switch (ctsio->cdb[0]) { 8951 case READ_6: 8952 case WRITE_6: { 8953 struct scsi_rw_6 *cdb; 8954 8955 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8956 8957 lba = scsi_3btoul(cdb->addr); 8958 /* only 5 bits are valid in the most significant address byte */ 8959 lba &= 0x1fffff; 8960 num_blocks = cdb->length; 8961 /* 8962 * This is correct according to SBC-2. 8963 */ 8964 if (num_blocks == 0) 8965 num_blocks = 256; 8966 break; 8967 } 8968 case READ_10: 8969 case WRITE_10: { 8970 struct scsi_rw_10 *cdb; 8971 8972 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8973 if (cdb->byte2 & SRW10_FUA) 8974 flags |= CTL_LLF_FUA; 8975 if (cdb->byte2 & SRW10_DPO) 8976 flags |= CTL_LLF_DPO; 8977 lba = scsi_4btoul(cdb->addr); 8978 num_blocks = scsi_2btoul(cdb->length); 8979 break; 8980 } 8981 case WRITE_VERIFY_10: { 8982 struct scsi_write_verify_10 *cdb; 8983 8984 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8985 flags |= CTL_LLF_FUA; 8986 if (cdb->byte2 & SWV_DPO) 8987 flags |= CTL_LLF_DPO; 8988 lba = scsi_4btoul(cdb->addr); 8989 num_blocks = scsi_2btoul(cdb->length); 8990 break; 8991 } 8992 case READ_12: 8993 case WRITE_12: { 8994 struct scsi_rw_12 *cdb; 8995 8996 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8997 if (cdb->byte2 & SRW12_FUA) 8998 flags |= CTL_LLF_FUA; 8999 if (cdb->byte2 & SRW12_DPO) 9000 flags |= CTL_LLF_DPO; 9001 lba = scsi_4btoul(cdb->addr); 9002 num_blocks = scsi_4btoul(cdb->length); 9003 break; 9004 } 9005 case WRITE_VERIFY_12: { 9006 struct scsi_write_verify_12 *cdb; 9007 9008 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9009 flags |= CTL_LLF_FUA; 9010 if (cdb->byte2 & SWV_DPO) 9011 flags |= CTL_LLF_DPO; 9012 lba = scsi_4btoul(cdb->addr); 9013 num_blocks = scsi_4btoul(cdb->length); 9014 break; 9015 } 9016 case READ_16: 9017 case WRITE_16: { 9018 struct scsi_rw_16 *cdb; 9019 9020 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9021 if (cdb->byte2 & SRW12_FUA) 9022 flags |= CTL_LLF_FUA; 9023 if (cdb->byte2 & SRW12_DPO) 9024 flags |= CTL_LLF_DPO; 9025 lba = scsi_8btou64(cdb->addr); 9026 num_blocks = scsi_4btoul(cdb->length); 9027 break; 9028 } 9029 case WRITE_ATOMIC_16: { 9030 struct scsi_rw_16 *cdb; 9031 9032 if (lun->be_lun->atomicblock == 0) { 9033 ctl_set_invalid_opcode(ctsio); 9034 ctl_done((union ctl_io *)ctsio); 9035 return (CTL_RETVAL_COMPLETE); 9036 } 9037 9038 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9039 if (cdb->byte2 & SRW12_FUA) 9040 flags |= CTL_LLF_FUA; 9041 if (cdb->byte2 & SRW12_DPO) 9042 flags |= CTL_LLF_DPO; 9043 lba = scsi_8btou64(cdb->addr); 9044 num_blocks = scsi_4btoul(cdb->length); 9045 if (num_blocks > lun->be_lun->atomicblock) { 9046 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 9047 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 9048 /*bit*/ 0); 9049 ctl_done((union ctl_io *)ctsio); 9050 return (CTL_RETVAL_COMPLETE); 9051 } 9052 break; 9053 } 9054 case WRITE_VERIFY_16: { 9055 struct scsi_write_verify_16 *cdb; 9056 9057 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9058 flags |= CTL_LLF_FUA; 9059 if (cdb->byte2 & SWV_DPO) 9060 flags |= CTL_LLF_DPO; 9061 lba = scsi_8btou64(cdb->addr); 9062 num_blocks = scsi_4btoul(cdb->length); 9063 break; 9064 } 9065 default: 9066 /* 9067 * We got a command we don't support. This shouldn't 9068 * happen, commands should be filtered out above us. 9069 */ 9070 ctl_set_invalid_opcode(ctsio); 9071 ctl_done((union ctl_io *)ctsio); 9072 9073 return (CTL_RETVAL_COMPLETE); 9074 break; /* NOTREACHED */ 9075 } 9076 9077 /* 9078 * The first check is to make sure we're in bounds, the second 9079 * check is to catch wrap-around problems. If the lba + num blocks 9080 * is less than the lba, then we've wrapped around and the block 9081 * range is invalid anyway. 9082 */ 9083 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9084 || ((lba + num_blocks) < lba)) { 9085 ctl_set_lba_out_of_range(ctsio); 9086 ctl_done((union ctl_io *)ctsio); 9087 return (CTL_RETVAL_COMPLETE); 9088 } 9089 9090 /* 9091 * According to SBC-3, a transfer length of 0 is not an error. 9092 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9093 * translates to 256 blocks for those commands. 9094 */ 9095 if (num_blocks == 0) { 9096 ctl_set_success(ctsio); 9097 ctl_done((union ctl_io *)ctsio); 9098 return (CTL_RETVAL_COMPLETE); 9099 } 9100 9101 /* Set FUA and/or DPO if caches are disabled. */ 9102 if (isread) { 9103 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9104 SCP_RCD) != 0) 9105 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9106 } else { 9107 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9108 SCP_WCE) == 0) 9109 flags |= CTL_LLF_FUA; 9110 } 9111 9112 lbalen = (struct ctl_lba_len_flags *) 9113 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9114 lbalen->lba = lba; 9115 lbalen->len = num_blocks; 9116 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9117 9118 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9119 ctsio->kern_rel_offset = 0; 9120 9121 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9122 9123 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9124 9125 return (retval); 9126 } 9127 9128 static int 9129 ctl_cnw_cont(union ctl_io *io) 9130 { 9131 struct ctl_scsiio *ctsio; 9132 struct ctl_lun *lun; 9133 struct ctl_lba_len_flags *lbalen; 9134 int retval; 9135 9136 ctsio = &io->scsiio; 9137 ctsio->io_hdr.status = CTL_STATUS_NONE; 9138 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9139 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9140 lbalen = (struct ctl_lba_len_flags *) 9141 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9142 lbalen->flags &= ~CTL_LLF_COMPARE; 9143 lbalen->flags |= CTL_LLF_WRITE; 9144 9145 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9146 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9147 return (retval); 9148 } 9149 9150 int 9151 ctl_cnw(struct ctl_scsiio *ctsio) 9152 { 9153 struct ctl_lun *lun; 9154 struct ctl_lba_len_flags *lbalen; 9155 uint64_t lba; 9156 uint32_t num_blocks; 9157 int flags, retval; 9158 9159 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9160 9161 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9162 9163 flags = 0; 9164 retval = CTL_RETVAL_COMPLETE; 9165 9166 switch (ctsio->cdb[0]) { 9167 case COMPARE_AND_WRITE: { 9168 struct scsi_compare_and_write *cdb; 9169 9170 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9171 if (cdb->byte2 & SRW10_FUA) 9172 flags |= CTL_LLF_FUA; 9173 if (cdb->byte2 & SRW10_DPO) 9174 flags |= CTL_LLF_DPO; 9175 lba = scsi_8btou64(cdb->addr); 9176 num_blocks = cdb->length; 9177 break; 9178 } 9179 default: 9180 /* 9181 * We got a command we don't support. This shouldn't 9182 * happen, commands should be filtered out above us. 9183 */ 9184 ctl_set_invalid_opcode(ctsio); 9185 ctl_done((union ctl_io *)ctsio); 9186 9187 return (CTL_RETVAL_COMPLETE); 9188 break; /* NOTREACHED */ 9189 } 9190 9191 /* 9192 * The first check is to make sure we're in bounds, the second 9193 * check is to catch wrap-around problems. If the lba + num blocks 9194 * is less than the lba, then we've wrapped around and the block 9195 * range is invalid anyway. 9196 */ 9197 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9198 || ((lba + num_blocks) < lba)) { 9199 ctl_set_lba_out_of_range(ctsio); 9200 ctl_done((union ctl_io *)ctsio); 9201 return (CTL_RETVAL_COMPLETE); 9202 } 9203 9204 /* 9205 * According to SBC-3, a transfer length of 0 is not an error. 9206 */ 9207 if (num_blocks == 0) { 9208 ctl_set_success(ctsio); 9209 ctl_done((union ctl_io *)ctsio); 9210 return (CTL_RETVAL_COMPLETE); 9211 } 9212 9213 /* Set FUA if write cache is disabled. */ 9214 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9215 SCP_WCE) == 0) 9216 flags |= CTL_LLF_FUA; 9217 9218 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9219 ctsio->kern_rel_offset = 0; 9220 9221 /* 9222 * Set the IO_CONT flag, so that if this I/O gets passed to 9223 * ctl_data_submit_done(), it'll get passed back to 9224 * ctl_ctl_cnw_cont() for further processing. 9225 */ 9226 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9227 ctsio->io_cont = ctl_cnw_cont; 9228 9229 lbalen = (struct ctl_lba_len_flags *) 9230 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9231 lbalen->lba = lba; 9232 lbalen->len = num_blocks; 9233 lbalen->flags = CTL_LLF_COMPARE | flags; 9234 9235 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9236 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9237 return (retval); 9238 } 9239 9240 int 9241 ctl_verify(struct ctl_scsiio *ctsio) 9242 { 9243 struct ctl_lun *lun; 9244 struct ctl_lba_len_flags *lbalen; 9245 uint64_t lba; 9246 uint32_t num_blocks; 9247 int bytchk, flags; 9248 int retval; 9249 9250 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9251 9252 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9253 9254 bytchk = 0; 9255 flags = CTL_LLF_FUA; 9256 retval = CTL_RETVAL_COMPLETE; 9257 9258 switch (ctsio->cdb[0]) { 9259 case VERIFY_10: { 9260 struct scsi_verify_10 *cdb; 9261 9262 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9263 if (cdb->byte2 & SVFY_BYTCHK) 9264 bytchk = 1; 9265 if (cdb->byte2 & SVFY_DPO) 9266 flags |= CTL_LLF_DPO; 9267 lba = scsi_4btoul(cdb->addr); 9268 num_blocks = scsi_2btoul(cdb->length); 9269 break; 9270 } 9271 case VERIFY_12: { 9272 struct scsi_verify_12 *cdb; 9273 9274 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9275 if (cdb->byte2 & SVFY_BYTCHK) 9276 bytchk = 1; 9277 if (cdb->byte2 & SVFY_DPO) 9278 flags |= CTL_LLF_DPO; 9279 lba = scsi_4btoul(cdb->addr); 9280 num_blocks = scsi_4btoul(cdb->length); 9281 break; 9282 } 9283 case VERIFY_16: { 9284 struct scsi_rw_16 *cdb; 9285 9286 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9287 if (cdb->byte2 & SVFY_BYTCHK) 9288 bytchk = 1; 9289 if (cdb->byte2 & SVFY_DPO) 9290 flags |= CTL_LLF_DPO; 9291 lba = scsi_8btou64(cdb->addr); 9292 num_blocks = scsi_4btoul(cdb->length); 9293 break; 9294 } 9295 default: 9296 /* 9297 * We got a command we don't support. This shouldn't 9298 * happen, commands should be filtered out above us. 9299 */ 9300 ctl_set_invalid_opcode(ctsio); 9301 ctl_done((union ctl_io *)ctsio); 9302 return (CTL_RETVAL_COMPLETE); 9303 } 9304 9305 /* 9306 * The first check is to make sure we're in bounds, the second 9307 * check is to catch wrap-around problems. If the lba + num blocks 9308 * is less than the lba, then we've wrapped around and the block 9309 * range is invalid anyway. 9310 */ 9311 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9312 || ((lba + num_blocks) < lba)) { 9313 ctl_set_lba_out_of_range(ctsio); 9314 ctl_done((union ctl_io *)ctsio); 9315 return (CTL_RETVAL_COMPLETE); 9316 } 9317 9318 /* 9319 * According to SBC-3, a transfer length of 0 is not an error. 9320 */ 9321 if (num_blocks == 0) { 9322 ctl_set_success(ctsio); 9323 ctl_done((union ctl_io *)ctsio); 9324 return (CTL_RETVAL_COMPLETE); 9325 } 9326 9327 lbalen = (struct ctl_lba_len_flags *) 9328 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9329 lbalen->lba = lba; 9330 lbalen->len = num_blocks; 9331 if (bytchk) { 9332 lbalen->flags = CTL_LLF_COMPARE | flags; 9333 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9334 } else { 9335 lbalen->flags = CTL_LLF_VERIFY | flags; 9336 ctsio->kern_total_len = 0; 9337 } 9338 ctsio->kern_rel_offset = 0; 9339 9340 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9341 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9342 return (retval); 9343 } 9344 9345 int 9346 ctl_report_luns(struct ctl_scsiio *ctsio) 9347 { 9348 struct ctl_softc *softc = control_softc; 9349 struct scsi_report_luns *cdb; 9350 struct scsi_report_luns_data *lun_data; 9351 struct ctl_lun *lun, *request_lun; 9352 struct ctl_port *port; 9353 int num_luns, retval; 9354 uint32_t alloc_len, lun_datalen; 9355 int num_filled, well_known; 9356 uint32_t initidx, targ_lun_id, lun_id; 9357 9358 retval = CTL_RETVAL_COMPLETE; 9359 well_known = 0; 9360 9361 cdb = (struct scsi_report_luns *)ctsio->cdb; 9362 port = ctl_io_port(&ctsio->io_hdr); 9363 9364 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9365 9366 mtx_lock(&softc->ctl_lock); 9367 num_luns = 0; 9368 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 9369 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 9370 num_luns++; 9371 } 9372 mtx_unlock(&softc->ctl_lock); 9373 9374 switch (cdb->select_report) { 9375 case RPL_REPORT_DEFAULT: 9376 case RPL_REPORT_ALL: 9377 break; 9378 case RPL_REPORT_WELLKNOWN: 9379 well_known = 1; 9380 num_luns = 0; 9381 break; 9382 default: 9383 ctl_set_invalid_field(ctsio, 9384 /*sks_valid*/ 1, 9385 /*command*/ 1, 9386 /*field*/ 2, 9387 /*bit_valid*/ 0, 9388 /*bit*/ 0); 9389 ctl_done((union ctl_io *)ctsio); 9390 return (retval); 9391 break; /* NOTREACHED */ 9392 } 9393 9394 alloc_len = scsi_4btoul(cdb->length); 9395 /* 9396 * The initiator has to allocate at least 16 bytes for this request, 9397 * so he can at least get the header and the first LUN. Otherwise 9398 * we reject the request (per SPC-3 rev 14, section 6.21). 9399 */ 9400 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9401 sizeof(struct scsi_report_luns_lundata))) { 9402 ctl_set_invalid_field(ctsio, 9403 /*sks_valid*/ 1, 9404 /*command*/ 1, 9405 /*field*/ 6, 9406 /*bit_valid*/ 0, 9407 /*bit*/ 0); 9408 ctl_done((union ctl_io *)ctsio); 9409 return (retval); 9410 } 9411 9412 request_lun = (struct ctl_lun *) 9413 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9414 9415 lun_datalen = sizeof(*lun_data) + 9416 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9417 9418 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9419 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9420 ctsio->kern_sg_entries = 0; 9421 9422 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9423 9424 mtx_lock(&softc->ctl_lock); 9425 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9426 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9427 if (lun_id >= CTL_MAX_LUNS) 9428 continue; 9429 lun = softc->ctl_luns[lun_id]; 9430 if (lun == NULL) 9431 continue; 9432 9433 if (targ_lun_id <= 0xff) { 9434 /* 9435 * Peripheral addressing method, bus number 0. 9436 */ 9437 lun_data->luns[num_filled].lundata[0] = 9438 RPL_LUNDATA_ATYP_PERIPH; 9439 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9440 num_filled++; 9441 } else if (targ_lun_id <= 0x3fff) { 9442 /* 9443 * Flat addressing method. 9444 */ 9445 lun_data->luns[num_filled].lundata[0] = 9446 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9447 lun_data->luns[num_filled].lundata[1] = 9448 (targ_lun_id & 0xff); 9449 num_filled++; 9450 } else if (targ_lun_id <= 0xffffff) { 9451 /* 9452 * Extended flat addressing method. 9453 */ 9454 lun_data->luns[num_filled].lundata[0] = 9455 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9456 scsi_ulto3b(targ_lun_id, 9457 &lun_data->luns[num_filled].lundata[1]); 9458 num_filled++; 9459 } else { 9460 printf("ctl_report_luns: bogus LUN number %jd, " 9461 "skipping\n", (intmax_t)targ_lun_id); 9462 } 9463 /* 9464 * According to SPC-3, rev 14 section 6.21: 9465 * 9466 * "The execution of a REPORT LUNS command to any valid and 9467 * installed logical unit shall clear the REPORTED LUNS DATA 9468 * HAS CHANGED unit attention condition for all logical 9469 * units of that target with respect to the requesting 9470 * initiator. A valid and installed logical unit is one 9471 * having a PERIPHERAL QUALIFIER of 000b in the standard 9472 * INQUIRY data (see 6.4.2)." 9473 * 9474 * If request_lun is NULL, the LUN this report luns command 9475 * was issued to is either disabled or doesn't exist. In that 9476 * case, we shouldn't clear any pending lun change unit 9477 * attention. 9478 */ 9479 if (request_lun != NULL) { 9480 mtx_lock(&lun->lun_lock); 9481 ctl_clr_ua(lun, initidx, CTL_UA_RES_RELEASE); 9482 mtx_unlock(&lun->lun_lock); 9483 } 9484 } 9485 mtx_unlock(&softc->ctl_lock); 9486 9487 /* 9488 * It's quite possible that we've returned fewer LUNs than we allocated 9489 * space for. Trim it. 9490 */ 9491 lun_datalen = sizeof(*lun_data) + 9492 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9493 9494 if (lun_datalen < alloc_len) { 9495 ctsio->residual = alloc_len - lun_datalen; 9496 ctsio->kern_data_len = lun_datalen; 9497 ctsio->kern_total_len = lun_datalen; 9498 } else { 9499 ctsio->residual = 0; 9500 ctsio->kern_data_len = alloc_len; 9501 ctsio->kern_total_len = alloc_len; 9502 } 9503 ctsio->kern_data_resid = 0; 9504 ctsio->kern_rel_offset = 0; 9505 ctsio->kern_sg_entries = 0; 9506 9507 /* 9508 * We set this to the actual data length, regardless of how much 9509 * space we actually have to return results. If the user looks at 9510 * this value, he'll know whether or not he allocated enough space 9511 * and reissue the command if necessary. We don't support well 9512 * known logical units, so if the user asks for that, return none. 9513 */ 9514 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9515 9516 /* 9517 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9518 * this request. 9519 */ 9520 ctl_set_success(ctsio); 9521 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9522 ctsio->be_move_done = ctl_config_move_done; 9523 ctl_datamove((union ctl_io *)ctsio); 9524 return (retval); 9525 } 9526 9527 int 9528 ctl_request_sense(struct ctl_scsiio *ctsio) 9529 { 9530 struct scsi_request_sense *cdb; 9531 struct scsi_sense_data *sense_ptr; 9532 struct ctl_softc *ctl_softc; 9533 struct ctl_lun *lun; 9534 uint32_t initidx; 9535 int have_error; 9536 scsi_sense_data_type sense_format; 9537 ctl_ua_type ua_type; 9538 9539 cdb = (struct scsi_request_sense *)ctsio->cdb; 9540 9541 ctl_softc = control_softc; 9542 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9543 9544 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9545 9546 /* 9547 * Determine which sense format the user wants. 9548 */ 9549 if (cdb->byte2 & SRS_DESC) 9550 sense_format = SSD_TYPE_DESC; 9551 else 9552 sense_format = SSD_TYPE_FIXED; 9553 9554 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9555 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9556 ctsio->kern_sg_entries = 0; 9557 9558 /* 9559 * struct scsi_sense_data, which is currently set to 256 bytes, is 9560 * larger than the largest allowed value for the length field in the 9561 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9562 */ 9563 ctsio->residual = 0; 9564 ctsio->kern_data_len = cdb->length; 9565 ctsio->kern_total_len = cdb->length; 9566 9567 ctsio->kern_data_resid = 0; 9568 ctsio->kern_rel_offset = 0; 9569 ctsio->kern_sg_entries = 0; 9570 9571 /* 9572 * If we don't have a LUN, we don't have any pending sense. 9573 */ 9574 if (lun == NULL) 9575 goto no_sense; 9576 9577 have_error = 0; 9578 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9579 /* 9580 * Check for pending sense, and then for pending unit attentions. 9581 * Pending sense gets returned first, then pending unit attentions. 9582 */ 9583 mtx_lock(&lun->lun_lock); 9584 #ifdef CTL_WITH_CA 9585 if (ctl_is_set(lun->have_ca, initidx)) { 9586 scsi_sense_data_type stored_format; 9587 9588 /* 9589 * Check to see which sense format was used for the stored 9590 * sense data. 9591 */ 9592 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9593 9594 /* 9595 * If the user requested a different sense format than the 9596 * one we stored, then we need to convert it to the other 9597 * format. If we're going from descriptor to fixed format 9598 * sense data, we may lose things in translation, depending 9599 * on what options were used. 9600 * 9601 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9602 * for some reason we'll just copy it out as-is. 9603 */ 9604 if ((stored_format == SSD_TYPE_FIXED) 9605 && (sense_format == SSD_TYPE_DESC)) 9606 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9607 &lun->pending_sense[initidx], 9608 (struct scsi_sense_data_desc *)sense_ptr); 9609 else if ((stored_format == SSD_TYPE_DESC) 9610 && (sense_format == SSD_TYPE_FIXED)) 9611 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9612 &lun->pending_sense[initidx], 9613 (struct scsi_sense_data_fixed *)sense_ptr); 9614 else 9615 memcpy(sense_ptr, &lun->pending_sense[initidx], 9616 MIN(sizeof(*sense_ptr), 9617 sizeof(lun->pending_sense[initidx]))); 9618 9619 ctl_clear_mask(lun->have_ca, initidx); 9620 have_error = 1; 9621 } else 9622 #endif 9623 { 9624 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9625 if (ua_type != CTL_UA_NONE) 9626 have_error = 1; 9627 if (ua_type == CTL_UA_LUN_CHANGE) { 9628 mtx_unlock(&lun->lun_lock); 9629 mtx_lock(&ctl_softc->ctl_lock); 9630 ctl_clear_ua(ctl_softc, initidx, ua_type); 9631 mtx_unlock(&ctl_softc->ctl_lock); 9632 mtx_lock(&lun->lun_lock); 9633 } 9634 9635 } 9636 mtx_unlock(&lun->lun_lock); 9637 9638 /* 9639 * We already have a pending error, return it. 9640 */ 9641 if (have_error != 0) { 9642 /* 9643 * We report the SCSI status as OK, since the status of the 9644 * request sense command itself is OK. 9645 * We report 0 for the sense length, because we aren't doing 9646 * autosense in this case. We're reporting sense as 9647 * parameter data. 9648 */ 9649 ctl_set_success(ctsio); 9650 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9651 ctsio->be_move_done = ctl_config_move_done; 9652 ctl_datamove((union ctl_io *)ctsio); 9653 return (CTL_RETVAL_COMPLETE); 9654 } 9655 9656 no_sense: 9657 9658 /* 9659 * No sense information to report, so we report that everything is 9660 * okay. 9661 */ 9662 ctl_set_sense_data(sense_ptr, 9663 lun, 9664 sense_format, 9665 /*current_error*/ 1, 9666 /*sense_key*/ SSD_KEY_NO_SENSE, 9667 /*asc*/ 0x00, 9668 /*ascq*/ 0x00, 9669 SSD_ELEM_NONE); 9670 9671 /* 9672 * We report 0 for the sense length, because we aren't doing 9673 * autosense in this case. We're reporting sense as parameter data. 9674 */ 9675 ctl_set_success(ctsio); 9676 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9677 ctsio->be_move_done = ctl_config_move_done; 9678 ctl_datamove((union ctl_io *)ctsio); 9679 return (CTL_RETVAL_COMPLETE); 9680 } 9681 9682 int 9683 ctl_tur(struct ctl_scsiio *ctsio) 9684 { 9685 9686 CTL_DEBUG_PRINT(("ctl_tur\n")); 9687 9688 ctl_set_success(ctsio); 9689 ctl_done((union ctl_io *)ctsio); 9690 9691 return (CTL_RETVAL_COMPLETE); 9692 } 9693 9694 #ifdef notyet 9695 static int 9696 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9697 { 9698 9699 } 9700 #endif 9701 9702 /* 9703 * SCSI VPD page 0x00, the Supported VPD Pages page. 9704 */ 9705 static int 9706 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9707 { 9708 struct scsi_vpd_supported_pages *pages; 9709 int sup_page_size; 9710 struct ctl_lun *lun; 9711 int p; 9712 9713 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9714 9715 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9716 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9717 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9718 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9719 ctsio->kern_sg_entries = 0; 9720 9721 if (sup_page_size < alloc_len) { 9722 ctsio->residual = alloc_len - sup_page_size; 9723 ctsio->kern_data_len = sup_page_size; 9724 ctsio->kern_total_len = sup_page_size; 9725 } else { 9726 ctsio->residual = 0; 9727 ctsio->kern_data_len = alloc_len; 9728 ctsio->kern_total_len = alloc_len; 9729 } 9730 ctsio->kern_data_resid = 0; 9731 ctsio->kern_rel_offset = 0; 9732 ctsio->kern_sg_entries = 0; 9733 9734 /* 9735 * The control device is always connected. The disk device, on the 9736 * other hand, may not be online all the time. Need to change this 9737 * to figure out whether the disk device is actually online or not. 9738 */ 9739 if (lun != NULL) 9740 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9741 lun->be_lun->lun_type; 9742 else 9743 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9744 9745 p = 0; 9746 /* Supported VPD pages */ 9747 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9748 /* Serial Number */ 9749 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9750 /* Device Identification */ 9751 pages->page_list[p++] = SVPD_DEVICE_ID; 9752 /* Extended INQUIRY Data */ 9753 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9754 /* Mode Page Policy */ 9755 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9756 /* SCSI Ports */ 9757 pages->page_list[p++] = SVPD_SCSI_PORTS; 9758 /* Third-party Copy */ 9759 pages->page_list[p++] = SVPD_SCSI_TPC; 9760 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9761 /* Block limits */ 9762 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9763 /* Block Device Characteristics */ 9764 pages->page_list[p++] = SVPD_BDC; 9765 /* Logical Block Provisioning */ 9766 pages->page_list[p++] = SVPD_LBP; 9767 } 9768 pages->length = p; 9769 9770 ctl_set_success(ctsio); 9771 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9772 ctsio->be_move_done = ctl_config_move_done; 9773 ctl_datamove((union ctl_io *)ctsio); 9774 return (CTL_RETVAL_COMPLETE); 9775 } 9776 9777 /* 9778 * SCSI VPD page 0x80, the Unit Serial Number page. 9779 */ 9780 static int 9781 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9782 { 9783 struct scsi_vpd_unit_serial_number *sn_ptr; 9784 struct ctl_lun *lun; 9785 int data_len; 9786 9787 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9788 9789 data_len = 4 + CTL_SN_LEN; 9790 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9791 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9792 if (data_len < alloc_len) { 9793 ctsio->residual = alloc_len - data_len; 9794 ctsio->kern_data_len = data_len; 9795 ctsio->kern_total_len = data_len; 9796 } else { 9797 ctsio->residual = 0; 9798 ctsio->kern_data_len = alloc_len; 9799 ctsio->kern_total_len = alloc_len; 9800 } 9801 ctsio->kern_data_resid = 0; 9802 ctsio->kern_rel_offset = 0; 9803 ctsio->kern_sg_entries = 0; 9804 9805 /* 9806 * The control device is always connected. The disk device, on the 9807 * other hand, may not be online all the time. Need to change this 9808 * to figure out whether the disk device is actually online or not. 9809 */ 9810 if (lun != NULL) 9811 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9812 lun->be_lun->lun_type; 9813 else 9814 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9815 9816 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9817 sn_ptr->length = CTL_SN_LEN; 9818 /* 9819 * If we don't have a LUN, we just leave the serial number as 9820 * all spaces. 9821 */ 9822 if (lun != NULL) { 9823 strncpy((char *)sn_ptr->serial_num, 9824 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9825 } else 9826 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9827 9828 ctl_set_success(ctsio); 9829 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9830 ctsio->be_move_done = ctl_config_move_done; 9831 ctl_datamove((union ctl_io *)ctsio); 9832 return (CTL_RETVAL_COMPLETE); 9833 } 9834 9835 9836 /* 9837 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9838 */ 9839 static int 9840 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9841 { 9842 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9843 struct ctl_lun *lun; 9844 int data_len; 9845 9846 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9847 9848 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9849 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9850 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9851 ctsio->kern_sg_entries = 0; 9852 9853 if (data_len < alloc_len) { 9854 ctsio->residual = alloc_len - data_len; 9855 ctsio->kern_data_len = data_len; 9856 ctsio->kern_total_len = data_len; 9857 } else { 9858 ctsio->residual = 0; 9859 ctsio->kern_data_len = alloc_len; 9860 ctsio->kern_total_len = alloc_len; 9861 } 9862 ctsio->kern_data_resid = 0; 9863 ctsio->kern_rel_offset = 0; 9864 ctsio->kern_sg_entries = 0; 9865 9866 /* 9867 * The control device is always connected. The disk device, on the 9868 * other hand, may not be online all the time. 9869 */ 9870 if (lun != NULL) 9871 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9872 lun->be_lun->lun_type; 9873 else 9874 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9875 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9876 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9877 /* 9878 * We support head of queue, ordered and simple tags. 9879 */ 9880 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9881 /* 9882 * Volatile cache supported. 9883 */ 9884 eid_ptr->flags3 = SVPD_EID_V_SUP; 9885 9886 /* 9887 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9888 * attention for a particular IT nexus on all LUNs once we report 9889 * it to that nexus once. This bit is required as of SPC-4. 9890 */ 9891 eid_ptr->flags4 = SVPD_EID_LUICLT; 9892 9893 /* 9894 * XXX KDM in order to correctly answer this, we would need 9895 * information from the SIM to determine how much sense data it 9896 * can send. So this would really be a path inquiry field, most 9897 * likely. This can be set to a maximum of 252 according to SPC-4, 9898 * but the hardware may or may not be able to support that much. 9899 * 0 just means that the maximum sense data length is not reported. 9900 */ 9901 eid_ptr->max_sense_length = 0; 9902 9903 ctl_set_success(ctsio); 9904 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9905 ctsio->be_move_done = ctl_config_move_done; 9906 ctl_datamove((union ctl_io *)ctsio); 9907 return (CTL_RETVAL_COMPLETE); 9908 } 9909 9910 static int 9911 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9912 { 9913 struct scsi_vpd_mode_page_policy *mpp_ptr; 9914 struct ctl_lun *lun; 9915 int data_len; 9916 9917 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9918 9919 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9920 sizeof(struct scsi_vpd_mode_page_policy_descr); 9921 9922 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9923 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9924 ctsio->kern_sg_entries = 0; 9925 9926 if (data_len < alloc_len) { 9927 ctsio->residual = alloc_len - data_len; 9928 ctsio->kern_data_len = data_len; 9929 ctsio->kern_total_len = data_len; 9930 } else { 9931 ctsio->residual = 0; 9932 ctsio->kern_data_len = alloc_len; 9933 ctsio->kern_total_len = alloc_len; 9934 } 9935 ctsio->kern_data_resid = 0; 9936 ctsio->kern_rel_offset = 0; 9937 ctsio->kern_sg_entries = 0; 9938 9939 /* 9940 * The control device is always connected. The disk device, on the 9941 * other hand, may not be online all the time. 9942 */ 9943 if (lun != NULL) 9944 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9945 lun->be_lun->lun_type; 9946 else 9947 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9948 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9949 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9950 mpp_ptr->descr[0].page_code = 0x3f; 9951 mpp_ptr->descr[0].subpage_code = 0xff; 9952 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9953 9954 ctl_set_success(ctsio); 9955 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9956 ctsio->be_move_done = ctl_config_move_done; 9957 ctl_datamove((union ctl_io *)ctsio); 9958 return (CTL_RETVAL_COMPLETE); 9959 } 9960 9961 /* 9962 * SCSI VPD page 0x83, the Device Identification page. 9963 */ 9964 static int 9965 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9966 { 9967 struct scsi_vpd_device_id *devid_ptr; 9968 struct scsi_vpd_id_descriptor *desc; 9969 struct ctl_softc *softc; 9970 struct ctl_lun *lun; 9971 struct ctl_port *port; 9972 int data_len; 9973 uint8_t proto; 9974 9975 softc = control_softc; 9976 9977 port = softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9978 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9979 9980 data_len = sizeof(struct scsi_vpd_device_id) + 9981 sizeof(struct scsi_vpd_id_descriptor) + 9982 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9983 sizeof(struct scsi_vpd_id_descriptor) + 9984 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9985 if (lun && lun->lun_devid) 9986 data_len += lun->lun_devid->len; 9987 if (port->port_devid) 9988 data_len += port->port_devid->len; 9989 if (port->target_devid) 9990 data_len += port->target_devid->len; 9991 9992 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9993 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9994 ctsio->kern_sg_entries = 0; 9995 9996 if (data_len < alloc_len) { 9997 ctsio->residual = alloc_len - data_len; 9998 ctsio->kern_data_len = data_len; 9999 ctsio->kern_total_len = data_len; 10000 } else { 10001 ctsio->residual = 0; 10002 ctsio->kern_data_len = alloc_len; 10003 ctsio->kern_total_len = alloc_len; 10004 } 10005 ctsio->kern_data_resid = 0; 10006 ctsio->kern_rel_offset = 0; 10007 ctsio->kern_sg_entries = 0; 10008 10009 /* 10010 * The control device is always connected. The disk device, on the 10011 * other hand, may not be online all the time. 10012 */ 10013 if (lun != NULL) 10014 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10015 lun->be_lun->lun_type; 10016 else 10017 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10018 devid_ptr->page_code = SVPD_DEVICE_ID; 10019 scsi_ulto2b(data_len - 4, devid_ptr->length); 10020 10021 if (port->port_type == CTL_PORT_FC) 10022 proto = SCSI_PROTO_FC << 4; 10023 else if (port->port_type == CTL_PORT_ISCSI) 10024 proto = SCSI_PROTO_ISCSI << 4; 10025 else 10026 proto = SCSI_PROTO_SPI << 4; 10027 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 10028 10029 /* 10030 * We're using a LUN association here. i.e., this device ID is a 10031 * per-LUN identifier. 10032 */ 10033 if (lun && lun->lun_devid) { 10034 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 10035 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10036 lun->lun_devid->len); 10037 } 10038 10039 /* 10040 * This is for the WWPN which is a port association. 10041 */ 10042 if (port->port_devid) { 10043 memcpy(desc, port->port_devid->data, port->port_devid->len); 10044 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10045 port->port_devid->len); 10046 } 10047 10048 /* 10049 * This is for the Relative Target Port(type 4h) identifier 10050 */ 10051 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10052 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10053 SVPD_ID_TYPE_RELTARG; 10054 desc->length = 4; 10055 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 10056 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10057 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 10058 10059 /* 10060 * This is for the Target Port Group(type 5h) identifier 10061 */ 10062 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10063 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10064 SVPD_ID_TYPE_TPORTGRP; 10065 desc->length = 4; 10066 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10067 &desc->identifier[2]); 10068 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10069 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10070 10071 /* 10072 * This is for the Target identifier 10073 */ 10074 if (port->target_devid) { 10075 memcpy(desc, port->target_devid->data, port->target_devid->len); 10076 } 10077 10078 ctl_set_success(ctsio); 10079 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10080 ctsio->be_move_done = ctl_config_move_done; 10081 ctl_datamove((union ctl_io *)ctsio); 10082 return (CTL_RETVAL_COMPLETE); 10083 } 10084 10085 static int 10086 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10087 { 10088 struct ctl_softc *softc = control_softc; 10089 struct scsi_vpd_scsi_ports *sp; 10090 struct scsi_vpd_port_designation *pd; 10091 struct scsi_vpd_port_designation_cont *pdc; 10092 struct ctl_lun *lun; 10093 struct ctl_port *port; 10094 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10095 int num_target_port_groups; 10096 10097 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10098 10099 if (softc->is_single) 10100 num_target_port_groups = 1; 10101 else 10102 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10103 num_target_ports = 0; 10104 iid_len = 0; 10105 id_len = 0; 10106 mtx_lock(&softc->ctl_lock); 10107 STAILQ_FOREACH(port, &softc->port_list, links) { 10108 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10109 continue; 10110 if (lun != NULL && 10111 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 10112 continue; 10113 num_target_ports++; 10114 if (port->init_devid) 10115 iid_len += port->init_devid->len; 10116 if (port->port_devid) 10117 id_len += port->port_devid->len; 10118 } 10119 mtx_unlock(&softc->ctl_lock); 10120 10121 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10122 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10123 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10124 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10125 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10126 ctsio->kern_sg_entries = 0; 10127 10128 if (data_len < alloc_len) { 10129 ctsio->residual = alloc_len - data_len; 10130 ctsio->kern_data_len = data_len; 10131 ctsio->kern_total_len = data_len; 10132 } else { 10133 ctsio->residual = 0; 10134 ctsio->kern_data_len = alloc_len; 10135 ctsio->kern_total_len = alloc_len; 10136 } 10137 ctsio->kern_data_resid = 0; 10138 ctsio->kern_rel_offset = 0; 10139 ctsio->kern_sg_entries = 0; 10140 10141 /* 10142 * The control device is always connected. The disk device, on the 10143 * other hand, may not be online all the time. Need to change this 10144 * to figure out whether the disk device is actually online or not. 10145 */ 10146 if (lun != NULL) 10147 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10148 lun->be_lun->lun_type; 10149 else 10150 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10151 10152 sp->page_code = SVPD_SCSI_PORTS; 10153 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10154 sp->page_length); 10155 pd = &sp->design[0]; 10156 10157 mtx_lock(&softc->ctl_lock); 10158 pg = softc->port_offset / CTL_MAX_PORTS; 10159 for (g = 0; g < num_target_port_groups; g++) { 10160 STAILQ_FOREACH(port, &softc->port_list, links) { 10161 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10162 continue; 10163 if (lun != NULL && 10164 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 10165 continue; 10166 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10167 scsi_ulto2b(p, pd->relative_port_id); 10168 if (port->init_devid && g == pg) { 10169 iid_len = port->init_devid->len; 10170 memcpy(pd->initiator_transportid, 10171 port->init_devid->data, port->init_devid->len); 10172 } else 10173 iid_len = 0; 10174 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10175 pdc = (struct scsi_vpd_port_designation_cont *) 10176 (&pd->initiator_transportid[iid_len]); 10177 if (port->port_devid && g == pg) { 10178 id_len = port->port_devid->len; 10179 memcpy(pdc->target_port_descriptors, 10180 port->port_devid->data, port->port_devid->len); 10181 } else 10182 id_len = 0; 10183 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10184 pd = (struct scsi_vpd_port_designation *) 10185 ((uint8_t *)pdc->target_port_descriptors + id_len); 10186 } 10187 } 10188 mtx_unlock(&softc->ctl_lock); 10189 10190 ctl_set_success(ctsio); 10191 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10192 ctsio->be_move_done = ctl_config_move_done; 10193 ctl_datamove((union ctl_io *)ctsio); 10194 return (CTL_RETVAL_COMPLETE); 10195 } 10196 10197 static int 10198 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10199 { 10200 struct scsi_vpd_block_limits *bl_ptr; 10201 struct ctl_lun *lun; 10202 int bs; 10203 10204 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10205 10206 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10207 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10208 ctsio->kern_sg_entries = 0; 10209 10210 if (sizeof(*bl_ptr) < alloc_len) { 10211 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10212 ctsio->kern_data_len = sizeof(*bl_ptr); 10213 ctsio->kern_total_len = sizeof(*bl_ptr); 10214 } else { 10215 ctsio->residual = 0; 10216 ctsio->kern_data_len = alloc_len; 10217 ctsio->kern_total_len = alloc_len; 10218 } 10219 ctsio->kern_data_resid = 0; 10220 ctsio->kern_rel_offset = 0; 10221 ctsio->kern_sg_entries = 0; 10222 10223 /* 10224 * The control device is always connected. The disk device, on the 10225 * other hand, may not be online all the time. Need to change this 10226 * to figure out whether the disk device is actually online or not. 10227 */ 10228 if (lun != NULL) 10229 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10230 lun->be_lun->lun_type; 10231 else 10232 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10233 10234 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10235 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10236 bl_ptr->max_cmp_write_len = 0xff; 10237 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10238 if (lun != NULL) { 10239 bs = lun->be_lun->blocksize; 10240 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 10241 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10242 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10243 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10244 if (lun->be_lun->ublockexp != 0) { 10245 scsi_ulto4b((1 << lun->be_lun->ublockexp), 10246 bl_ptr->opt_unmap_grain); 10247 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 10248 bl_ptr->unmap_grain_align); 10249 } 10250 } 10251 scsi_ulto4b(lun->be_lun->atomicblock, 10252 bl_ptr->max_atomic_transfer_length); 10253 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10254 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10255 } 10256 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10257 10258 ctl_set_success(ctsio); 10259 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10260 ctsio->be_move_done = ctl_config_move_done; 10261 ctl_datamove((union ctl_io *)ctsio); 10262 return (CTL_RETVAL_COMPLETE); 10263 } 10264 10265 static int 10266 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10267 { 10268 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10269 struct ctl_lun *lun; 10270 const char *value; 10271 u_int i; 10272 10273 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10274 10275 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10276 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10277 ctsio->kern_sg_entries = 0; 10278 10279 if (sizeof(*bdc_ptr) < alloc_len) { 10280 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10281 ctsio->kern_data_len = sizeof(*bdc_ptr); 10282 ctsio->kern_total_len = sizeof(*bdc_ptr); 10283 } else { 10284 ctsio->residual = 0; 10285 ctsio->kern_data_len = alloc_len; 10286 ctsio->kern_total_len = alloc_len; 10287 } 10288 ctsio->kern_data_resid = 0; 10289 ctsio->kern_rel_offset = 0; 10290 ctsio->kern_sg_entries = 0; 10291 10292 /* 10293 * The control device is always connected. The disk device, on the 10294 * other hand, may not be online all the time. Need to change this 10295 * to figure out whether the disk device is actually online or not. 10296 */ 10297 if (lun != NULL) 10298 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10299 lun->be_lun->lun_type; 10300 else 10301 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10302 bdc_ptr->page_code = SVPD_BDC; 10303 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10304 if (lun != NULL && 10305 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10306 i = strtol(value, NULL, 0); 10307 else 10308 i = CTL_DEFAULT_ROTATION_RATE; 10309 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10310 if (lun != NULL && 10311 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10312 i = strtol(value, NULL, 0); 10313 else 10314 i = 0; 10315 bdc_ptr->wab_wac_ff = (i & 0x0f); 10316 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10317 10318 ctl_set_success(ctsio); 10319 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10320 ctsio->be_move_done = ctl_config_move_done; 10321 ctl_datamove((union ctl_io *)ctsio); 10322 return (CTL_RETVAL_COMPLETE); 10323 } 10324 10325 static int 10326 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10327 { 10328 struct scsi_vpd_logical_block_prov *lbp_ptr; 10329 struct ctl_lun *lun; 10330 10331 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10332 10333 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10334 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10335 ctsio->kern_sg_entries = 0; 10336 10337 if (sizeof(*lbp_ptr) < alloc_len) { 10338 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10339 ctsio->kern_data_len = sizeof(*lbp_ptr); 10340 ctsio->kern_total_len = sizeof(*lbp_ptr); 10341 } else { 10342 ctsio->residual = 0; 10343 ctsio->kern_data_len = alloc_len; 10344 ctsio->kern_total_len = alloc_len; 10345 } 10346 ctsio->kern_data_resid = 0; 10347 ctsio->kern_rel_offset = 0; 10348 ctsio->kern_sg_entries = 0; 10349 10350 /* 10351 * The control device is always connected. The disk device, on the 10352 * other hand, may not be online all the time. Need to change this 10353 * to figure out whether the disk device is actually online or not. 10354 */ 10355 if (lun != NULL) 10356 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10357 lun->be_lun->lun_type; 10358 else 10359 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10360 10361 lbp_ptr->page_code = SVPD_LBP; 10362 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10363 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10364 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10365 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10366 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10367 lbp_ptr->prov_type = SVPD_LBP_THIN; 10368 } 10369 10370 ctl_set_success(ctsio); 10371 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10372 ctsio->be_move_done = ctl_config_move_done; 10373 ctl_datamove((union ctl_io *)ctsio); 10374 return (CTL_RETVAL_COMPLETE); 10375 } 10376 10377 /* 10378 * INQUIRY with the EVPD bit set. 10379 */ 10380 static int 10381 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10382 { 10383 struct ctl_lun *lun; 10384 struct scsi_inquiry *cdb; 10385 int alloc_len, retval; 10386 10387 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10388 cdb = (struct scsi_inquiry *)ctsio->cdb; 10389 alloc_len = scsi_2btoul(cdb->length); 10390 10391 switch (cdb->page_code) { 10392 case SVPD_SUPPORTED_PAGES: 10393 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10394 break; 10395 case SVPD_UNIT_SERIAL_NUMBER: 10396 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10397 break; 10398 case SVPD_DEVICE_ID: 10399 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10400 break; 10401 case SVPD_EXTENDED_INQUIRY_DATA: 10402 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10403 break; 10404 case SVPD_MODE_PAGE_POLICY: 10405 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10406 break; 10407 case SVPD_SCSI_PORTS: 10408 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10409 break; 10410 case SVPD_SCSI_TPC: 10411 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10412 break; 10413 case SVPD_BLOCK_LIMITS: 10414 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10415 goto err; 10416 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10417 break; 10418 case SVPD_BDC: 10419 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10420 goto err; 10421 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10422 break; 10423 case SVPD_LBP: 10424 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10425 goto err; 10426 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10427 break; 10428 default: 10429 err: 10430 ctl_set_invalid_field(ctsio, 10431 /*sks_valid*/ 1, 10432 /*command*/ 1, 10433 /*field*/ 2, 10434 /*bit_valid*/ 0, 10435 /*bit*/ 0); 10436 ctl_done((union ctl_io *)ctsio); 10437 retval = CTL_RETVAL_COMPLETE; 10438 break; 10439 } 10440 10441 return (retval); 10442 } 10443 10444 /* 10445 * Standard INQUIRY data. 10446 */ 10447 static int 10448 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10449 { 10450 struct scsi_inquiry_data *inq_ptr; 10451 struct scsi_inquiry *cdb; 10452 struct ctl_softc *softc; 10453 struct ctl_lun *lun; 10454 char *val; 10455 uint32_t alloc_len, data_len; 10456 ctl_port_type port_type; 10457 10458 softc = control_softc; 10459 10460 /* 10461 * Figure out whether we're talking to a Fibre Channel port or not. 10462 * We treat the ioctl front end, and any SCSI adapters, as packetized 10463 * SCSI front ends. 10464 */ 10465 port_type = softc->ctl_ports[ 10466 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10467 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10468 port_type = CTL_PORT_SCSI; 10469 10470 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10471 cdb = (struct scsi_inquiry *)ctsio->cdb; 10472 alloc_len = scsi_2btoul(cdb->length); 10473 10474 /* 10475 * We malloc the full inquiry data size here and fill it 10476 * in. If the user only asks for less, we'll give him 10477 * that much. 10478 */ 10479 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10480 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10481 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10482 ctsio->kern_sg_entries = 0; 10483 ctsio->kern_data_resid = 0; 10484 ctsio->kern_rel_offset = 0; 10485 10486 if (data_len < alloc_len) { 10487 ctsio->residual = alloc_len - data_len; 10488 ctsio->kern_data_len = data_len; 10489 ctsio->kern_total_len = data_len; 10490 } else { 10491 ctsio->residual = 0; 10492 ctsio->kern_data_len = alloc_len; 10493 ctsio->kern_total_len = alloc_len; 10494 } 10495 10496 /* 10497 * If we have a LUN configured, report it as connected. Otherwise, 10498 * report that it is offline or no device is supported, depending 10499 * on the value of inquiry_pq_no_lun. 10500 * 10501 * According to the spec (SPC-4 r34), the peripheral qualifier 10502 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10503 * 10504 * "A peripheral device having the specified peripheral device type 10505 * is not connected to this logical unit. However, the device 10506 * server is capable of supporting the specified peripheral device 10507 * type on this logical unit." 10508 * 10509 * According to the same spec, the peripheral qualifier 10510 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10511 * 10512 * "The device server is not capable of supporting a peripheral 10513 * device on this logical unit. For this peripheral qualifier the 10514 * peripheral device type shall be set to 1Fh. All other peripheral 10515 * device type values are reserved for this peripheral qualifier." 10516 * 10517 * Given the text, it would seem that we probably want to report that 10518 * the LUN is offline here. There is no LUN connected, but we can 10519 * support a LUN at the given LUN number. 10520 * 10521 * In the real world, though, it sounds like things are a little 10522 * different: 10523 * 10524 * - Linux, when presented with a LUN with the offline peripheral 10525 * qualifier, will create an sg driver instance for it. So when 10526 * you attach it to CTL, you wind up with a ton of sg driver 10527 * instances. (One for every LUN that Linux bothered to probe.) 10528 * Linux does this despite the fact that it issues a REPORT LUNs 10529 * to LUN 0 to get the inventory of supported LUNs. 10530 * 10531 * - There is other anecdotal evidence (from Emulex folks) about 10532 * arrays that use the offline peripheral qualifier for LUNs that 10533 * are on the "passive" path in an active/passive array. 10534 * 10535 * So the solution is provide a hopefully reasonable default 10536 * (return bad/no LUN) and allow the user to change the behavior 10537 * with a tunable/sysctl variable. 10538 */ 10539 if (lun != NULL) 10540 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10541 lun->be_lun->lun_type; 10542 else if (softc->inquiry_pq_no_lun == 0) 10543 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10544 else 10545 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10546 10547 /* RMB in byte 2 is 0 */ 10548 inq_ptr->version = SCSI_REV_SPC4; 10549 10550 /* 10551 * According to SAM-3, even if a device only supports a single 10552 * level of LUN addressing, it should still set the HISUP bit: 10553 * 10554 * 4.9.1 Logical unit numbers overview 10555 * 10556 * All logical unit number formats described in this standard are 10557 * hierarchical in structure even when only a single level in that 10558 * hierarchy is used. The HISUP bit shall be set to one in the 10559 * standard INQUIRY data (see SPC-2) when any logical unit number 10560 * format described in this standard is used. Non-hierarchical 10561 * formats are outside the scope of this standard. 10562 * 10563 * Therefore we set the HiSup bit here. 10564 * 10565 * The reponse format is 2, per SPC-3. 10566 */ 10567 inq_ptr->response_format = SID_HiSup | 2; 10568 10569 inq_ptr->additional_length = data_len - 10570 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10571 CTL_DEBUG_PRINT(("additional_length = %d\n", 10572 inq_ptr->additional_length)); 10573 10574 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10575 /* 16 bit addressing */ 10576 if (port_type == CTL_PORT_SCSI) 10577 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10578 /* XXX set the SID_MultiP bit here if we're actually going to 10579 respond on multiple ports */ 10580 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10581 10582 /* 16 bit data bus, synchronous transfers */ 10583 if (port_type == CTL_PORT_SCSI) 10584 inq_ptr->flags = SID_WBus16 | SID_Sync; 10585 /* 10586 * XXX KDM do we want to support tagged queueing on the control 10587 * device at all? 10588 */ 10589 if ((lun == NULL) 10590 || (lun->be_lun->lun_type != T_PROCESSOR)) 10591 inq_ptr->flags |= SID_CmdQue; 10592 /* 10593 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10594 * We have 8 bytes for the vendor name, and 16 bytes for the device 10595 * name and 4 bytes for the revision. 10596 */ 10597 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10598 "vendor")) == NULL) { 10599 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10600 } else { 10601 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10602 strncpy(inq_ptr->vendor, val, 10603 min(sizeof(inq_ptr->vendor), strlen(val))); 10604 } 10605 if (lun == NULL) { 10606 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10607 sizeof(inq_ptr->product)); 10608 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10609 switch (lun->be_lun->lun_type) { 10610 case T_DIRECT: 10611 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10612 sizeof(inq_ptr->product)); 10613 break; 10614 case T_PROCESSOR: 10615 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10616 sizeof(inq_ptr->product)); 10617 break; 10618 default: 10619 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10620 sizeof(inq_ptr->product)); 10621 break; 10622 } 10623 } else { 10624 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10625 strncpy(inq_ptr->product, val, 10626 min(sizeof(inq_ptr->product), strlen(val))); 10627 } 10628 10629 /* 10630 * XXX make this a macro somewhere so it automatically gets 10631 * incremented when we make changes. 10632 */ 10633 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10634 "revision")) == NULL) { 10635 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10636 } else { 10637 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10638 strncpy(inq_ptr->revision, val, 10639 min(sizeof(inq_ptr->revision), strlen(val))); 10640 } 10641 10642 /* 10643 * For parallel SCSI, we support double transition and single 10644 * transition clocking. We also support QAS (Quick Arbitration 10645 * and Selection) and Information Unit transfers on both the 10646 * control and array devices. 10647 */ 10648 if (port_type == CTL_PORT_SCSI) 10649 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10650 SID_SPI_IUS; 10651 10652 /* SAM-5 (no version claimed) */ 10653 scsi_ulto2b(0x00A0, inq_ptr->version1); 10654 /* SPC-4 (no version claimed) */ 10655 scsi_ulto2b(0x0460, inq_ptr->version2); 10656 if (port_type == CTL_PORT_FC) { 10657 /* FCP-2 ANSI INCITS.350:2003 */ 10658 scsi_ulto2b(0x0917, inq_ptr->version3); 10659 } else if (port_type == CTL_PORT_SCSI) { 10660 /* SPI-4 ANSI INCITS.362:200x */ 10661 scsi_ulto2b(0x0B56, inq_ptr->version3); 10662 } else if (port_type == CTL_PORT_ISCSI) { 10663 /* iSCSI (no version claimed) */ 10664 scsi_ulto2b(0x0960, inq_ptr->version3); 10665 } else if (port_type == CTL_PORT_SAS) { 10666 /* SAS (no version claimed) */ 10667 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10668 } 10669 10670 if (lun == NULL) { 10671 /* SBC-4 (no version claimed) */ 10672 scsi_ulto2b(0x0600, inq_ptr->version4); 10673 } else { 10674 switch (lun->be_lun->lun_type) { 10675 case T_DIRECT: 10676 /* SBC-4 (no version claimed) */ 10677 scsi_ulto2b(0x0600, inq_ptr->version4); 10678 break; 10679 case T_PROCESSOR: 10680 default: 10681 break; 10682 } 10683 } 10684 10685 ctl_set_success(ctsio); 10686 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10687 ctsio->be_move_done = ctl_config_move_done; 10688 ctl_datamove((union ctl_io *)ctsio); 10689 return (CTL_RETVAL_COMPLETE); 10690 } 10691 10692 int 10693 ctl_inquiry(struct ctl_scsiio *ctsio) 10694 { 10695 struct scsi_inquiry *cdb; 10696 int retval; 10697 10698 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10699 10700 cdb = (struct scsi_inquiry *)ctsio->cdb; 10701 if (cdb->byte2 & SI_EVPD) 10702 retval = ctl_inquiry_evpd(ctsio); 10703 else if (cdb->page_code == 0) 10704 retval = ctl_inquiry_std(ctsio); 10705 else { 10706 ctl_set_invalid_field(ctsio, 10707 /*sks_valid*/ 1, 10708 /*command*/ 1, 10709 /*field*/ 2, 10710 /*bit_valid*/ 0, 10711 /*bit*/ 0); 10712 ctl_done((union ctl_io *)ctsio); 10713 return (CTL_RETVAL_COMPLETE); 10714 } 10715 10716 return (retval); 10717 } 10718 10719 /* 10720 * For known CDB types, parse the LBA and length. 10721 */ 10722 static int 10723 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10724 { 10725 if (io->io_hdr.io_type != CTL_IO_SCSI) 10726 return (1); 10727 10728 switch (io->scsiio.cdb[0]) { 10729 case COMPARE_AND_WRITE: { 10730 struct scsi_compare_and_write *cdb; 10731 10732 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10733 10734 *lba = scsi_8btou64(cdb->addr); 10735 *len = cdb->length; 10736 break; 10737 } 10738 case READ_6: 10739 case WRITE_6: { 10740 struct scsi_rw_6 *cdb; 10741 10742 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10743 10744 *lba = scsi_3btoul(cdb->addr); 10745 /* only 5 bits are valid in the most significant address byte */ 10746 *lba &= 0x1fffff; 10747 *len = cdb->length; 10748 break; 10749 } 10750 case READ_10: 10751 case WRITE_10: { 10752 struct scsi_rw_10 *cdb; 10753 10754 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10755 10756 *lba = scsi_4btoul(cdb->addr); 10757 *len = scsi_2btoul(cdb->length); 10758 break; 10759 } 10760 case WRITE_VERIFY_10: { 10761 struct scsi_write_verify_10 *cdb; 10762 10763 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10764 10765 *lba = scsi_4btoul(cdb->addr); 10766 *len = scsi_2btoul(cdb->length); 10767 break; 10768 } 10769 case READ_12: 10770 case WRITE_12: { 10771 struct scsi_rw_12 *cdb; 10772 10773 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10774 10775 *lba = scsi_4btoul(cdb->addr); 10776 *len = scsi_4btoul(cdb->length); 10777 break; 10778 } 10779 case WRITE_VERIFY_12: { 10780 struct scsi_write_verify_12 *cdb; 10781 10782 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10783 10784 *lba = scsi_4btoul(cdb->addr); 10785 *len = scsi_4btoul(cdb->length); 10786 break; 10787 } 10788 case READ_16: 10789 case WRITE_16: 10790 case WRITE_ATOMIC_16: { 10791 struct scsi_rw_16 *cdb; 10792 10793 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10794 10795 *lba = scsi_8btou64(cdb->addr); 10796 *len = scsi_4btoul(cdb->length); 10797 break; 10798 } 10799 case WRITE_VERIFY_16: { 10800 struct scsi_write_verify_16 *cdb; 10801 10802 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10803 10804 *lba = scsi_8btou64(cdb->addr); 10805 *len = scsi_4btoul(cdb->length); 10806 break; 10807 } 10808 case WRITE_SAME_10: { 10809 struct scsi_write_same_10 *cdb; 10810 10811 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10812 10813 *lba = scsi_4btoul(cdb->addr); 10814 *len = scsi_2btoul(cdb->length); 10815 break; 10816 } 10817 case WRITE_SAME_16: { 10818 struct scsi_write_same_16 *cdb; 10819 10820 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10821 10822 *lba = scsi_8btou64(cdb->addr); 10823 *len = scsi_4btoul(cdb->length); 10824 break; 10825 } 10826 case VERIFY_10: { 10827 struct scsi_verify_10 *cdb; 10828 10829 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10830 10831 *lba = scsi_4btoul(cdb->addr); 10832 *len = scsi_2btoul(cdb->length); 10833 break; 10834 } 10835 case VERIFY_12: { 10836 struct scsi_verify_12 *cdb; 10837 10838 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10839 10840 *lba = scsi_4btoul(cdb->addr); 10841 *len = scsi_4btoul(cdb->length); 10842 break; 10843 } 10844 case VERIFY_16: { 10845 struct scsi_verify_16 *cdb; 10846 10847 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10848 10849 *lba = scsi_8btou64(cdb->addr); 10850 *len = scsi_4btoul(cdb->length); 10851 break; 10852 } 10853 case UNMAP: { 10854 *lba = 0; 10855 *len = UINT64_MAX; 10856 break; 10857 } 10858 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10859 struct scsi_get_lba_status *cdb; 10860 10861 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10862 *lba = scsi_8btou64(cdb->addr); 10863 *len = UINT32_MAX; 10864 break; 10865 } 10866 default: 10867 return (1); 10868 break; /* NOTREACHED */ 10869 } 10870 10871 return (0); 10872 } 10873 10874 static ctl_action 10875 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10876 bool seq) 10877 { 10878 uint64_t endlba1, endlba2; 10879 10880 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10881 endlba2 = lba2 + len2 - 1; 10882 10883 if ((endlba1 < lba2) || (endlba2 < lba1)) 10884 return (CTL_ACTION_PASS); 10885 else 10886 return (CTL_ACTION_BLOCK); 10887 } 10888 10889 static int 10890 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10891 { 10892 struct ctl_ptr_len_flags *ptrlen; 10893 struct scsi_unmap_desc *buf, *end, *range; 10894 uint64_t lba; 10895 uint32_t len; 10896 10897 /* If not UNMAP -- go other way. */ 10898 if (io->io_hdr.io_type != CTL_IO_SCSI || 10899 io->scsiio.cdb[0] != UNMAP) 10900 return (CTL_ACTION_ERROR); 10901 10902 /* If UNMAP without data -- block and wait for data. */ 10903 ptrlen = (struct ctl_ptr_len_flags *) 10904 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10905 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10906 ptrlen->ptr == NULL) 10907 return (CTL_ACTION_BLOCK); 10908 10909 /* UNMAP with data -- check for collision. */ 10910 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10911 end = buf + ptrlen->len / sizeof(*buf); 10912 for (range = buf; range < end; range++) { 10913 lba = scsi_8btou64(range->lba); 10914 len = scsi_4btoul(range->length); 10915 if ((lba < lba2 + len2) && (lba + len > lba2)) 10916 return (CTL_ACTION_BLOCK); 10917 } 10918 return (CTL_ACTION_PASS); 10919 } 10920 10921 static ctl_action 10922 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10923 { 10924 uint64_t lba1, lba2; 10925 uint64_t len1, len2; 10926 int retval; 10927 10928 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10929 return (CTL_ACTION_ERROR); 10930 10931 retval = ctl_extent_check_unmap(io1, lba2, len2); 10932 if (retval != CTL_ACTION_ERROR) 10933 return (retval); 10934 10935 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10936 return (CTL_ACTION_ERROR); 10937 10938 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10939 } 10940 10941 static ctl_action 10942 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10943 { 10944 uint64_t lba1, lba2; 10945 uint64_t len1, len2; 10946 10947 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10948 return (CTL_ACTION_ERROR); 10949 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10950 return (CTL_ACTION_ERROR); 10951 10952 if (lba1 + len1 == lba2) 10953 return (CTL_ACTION_BLOCK); 10954 return (CTL_ACTION_PASS); 10955 } 10956 10957 static ctl_action 10958 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10959 union ctl_io *ooa_io) 10960 { 10961 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10962 ctl_serialize_action *serialize_row; 10963 10964 /* 10965 * The initiator attempted multiple untagged commands at the same 10966 * time. Can't do that. 10967 */ 10968 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10969 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10970 && ((pending_io->io_hdr.nexus.targ_port == 10971 ooa_io->io_hdr.nexus.targ_port) 10972 && (pending_io->io_hdr.nexus.initid.id == 10973 ooa_io->io_hdr.nexus.initid.id)) 10974 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10975 CTL_FLAG_STATUS_SENT)) == 0)) 10976 return (CTL_ACTION_OVERLAP); 10977 10978 /* 10979 * The initiator attempted to send multiple tagged commands with 10980 * the same ID. (It's fine if different initiators have the same 10981 * tag ID.) 10982 * 10983 * Even if all of those conditions are true, we don't kill the I/O 10984 * if the command ahead of us has been aborted. We won't end up 10985 * sending it to the FETD, and it's perfectly legal to resend a 10986 * command with the same tag number as long as the previous 10987 * instance of this tag number has been aborted somehow. 10988 */ 10989 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10990 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10991 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10992 && ((pending_io->io_hdr.nexus.targ_port == 10993 ooa_io->io_hdr.nexus.targ_port) 10994 && (pending_io->io_hdr.nexus.initid.id == 10995 ooa_io->io_hdr.nexus.initid.id)) 10996 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10997 CTL_FLAG_STATUS_SENT)) == 0)) 10998 return (CTL_ACTION_OVERLAP_TAG); 10999 11000 /* 11001 * If we get a head of queue tag, SAM-3 says that we should 11002 * immediately execute it. 11003 * 11004 * What happens if this command would normally block for some other 11005 * reason? e.g. a request sense with a head of queue tag 11006 * immediately after a write. Normally that would block, but this 11007 * will result in its getting executed immediately... 11008 * 11009 * We currently return "pass" instead of "skip", so we'll end up 11010 * going through the rest of the queue to check for overlapped tags. 11011 * 11012 * XXX KDM check for other types of blockage first?? 11013 */ 11014 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11015 return (CTL_ACTION_PASS); 11016 11017 /* 11018 * Ordered tags have to block until all items ahead of them 11019 * have completed. If we get called with an ordered tag, we always 11020 * block, if something else is ahead of us in the queue. 11021 */ 11022 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11023 return (CTL_ACTION_BLOCK); 11024 11025 /* 11026 * Simple tags get blocked until all head of queue and ordered tags 11027 * ahead of them have completed. I'm lumping untagged commands in 11028 * with simple tags here. XXX KDM is that the right thing to do? 11029 */ 11030 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11031 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11032 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11033 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11034 return (CTL_ACTION_BLOCK); 11035 11036 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11037 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11038 11039 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11040 11041 switch (serialize_row[pending_entry->seridx]) { 11042 case CTL_SER_BLOCK: 11043 return (CTL_ACTION_BLOCK); 11044 case CTL_SER_EXTENT: 11045 return (ctl_extent_check(ooa_io, pending_io, 11046 (lun->serseq == CTL_LUN_SERSEQ_ON))); 11047 case CTL_SER_EXTENTOPT: 11048 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11049 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11050 return (ctl_extent_check(ooa_io, pending_io, 11051 (lun->serseq == CTL_LUN_SERSEQ_ON))); 11052 return (CTL_ACTION_PASS); 11053 case CTL_SER_EXTENTSEQ: 11054 if (lun->serseq != CTL_LUN_SERSEQ_OFF) 11055 return (ctl_extent_check_seq(ooa_io, pending_io)); 11056 return (CTL_ACTION_PASS); 11057 case CTL_SER_PASS: 11058 return (CTL_ACTION_PASS); 11059 case CTL_SER_BLOCKOPT: 11060 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11061 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11062 return (CTL_ACTION_BLOCK); 11063 return (CTL_ACTION_PASS); 11064 case CTL_SER_SKIP: 11065 return (CTL_ACTION_SKIP); 11066 default: 11067 panic("invalid serialization value %d", 11068 serialize_row[pending_entry->seridx]); 11069 } 11070 11071 return (CTL_ACTION_ERROR); 11072 } 11073 11074 /* 11075 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11076 * Assumptions: 11077 * - pending_io is generally either incoming, or on the blocked queue 11078 * - starting I/O is the I/O we want to start the check with. 11079 */ 11080 static ctl_action 11081 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11082 union ctl_io *starting_io) 11083 { 11084 union ctl_io *ooa_io; 11085 ctl_action action; 11086 11087 mtx_assert(&lun->lun_lock, MA_OWNED); 11088 11089 /* 11090 * Run back along the OOA queue, starting with the current 11091 * blocked I/O and going through every I/O before it on the 11092 * queue. If starting_io is NULL, we'll just end up returning 11093 * CTL_ACTION_PASS. 11094 */ 11095 for (ooa_io = starting_io; ooa_io != NULL; 11096 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11097 ooa_links)){ 11098 11099 /* 11100 * This routine just checks to see whether 11101 * cur_blocked is blocked by ooa_io, which is ahead 11102 * of it in the queue. It doesn't queue/dequeue 11103 * cur_blocked. 11104 */ 11105 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11106 switch (action) { 11107 case CTL_ACTION_BLOCK: 11108 case CTL_ACTION_OVERLAP: 11109 case CTL_ACTION_OVERLAP_TAG: 11110 case CTL_ACTION_SKIP: 11111 case CTL_ACTION_ERROR: 11112 return (action); 11113 break; /* NOTREACHED */ 11114 case CTL_ACTION_PASS: 11115 break; 11116 default: 11117 panic("invalid action %d", action); 11118 break; /* NOTREACHED */ 11119 } 11120 } 11121 11122 return (CTL_ACTION_PASS); 11123 } 11124 11125 /* 11126 * Assumptions: 11127 * - An I/O has just completed, and has been removed from the per-LUN OOA 11128 * queue, so some items on the blocked queue may now be unblocked. 11129 */ 11130 static int 11131 ctl_check_blocked(struct ctl_lun *lun) 11132 { 11133 union ctl_io *cur_blocked, *next_blocked; 11134 11135 mtx_assert(&lun->lun_lock, MA_OWNED); 11136 11137 /* 11138 * Run forward from the head of the blocked queue, checking each 11139 * entry against the I/Os prior to it on the OOA queue to see if 11140 * there is still any blockage. 11141 * 11142 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11143 * with our removing a variable on it while it is traversing the 11144 * list. 11145 */ 11146 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11147 cur_blocked != NULL; cur_blocked = next_blocked) { 11148 union ctl_io *prev_ooa; 11149 ctl_action action; 11150 11151 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11152 blocked_links); 11153 11154 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11155 ctl_ooaq, ooa_links); 11156 11157 /* 11158 * If cur_blocked happens to be the first item in the OOA 11159 * queue now, prev_ooa will be NULL, and the action 11160 * returned will just be CTL_ACTION_PASS. 11161 */ 11162 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11163 11164 switch (action) { 11165 case CTL_ACTION_BLOCK: 11166 /* Nothing to do here, still blocked */ 11167 break; 11168 case CTL_ACTION_OVERLAP: 11169 case CTL_ACTION_OVERLAP_TAG: 11170 /* 11171 * This shouldn't happen! In theory we've already 11172 * checked this command for overlap... 11173 */ 11174 break; 11175 case CTL_ACTION_PASS: 11176 case CTL_ACTION_SKIP: { 11177 const struct ctl_cmd_entry *entry; 11178 int isc_retval; 11179 11180 /* 11181 * The skip case shouldn't happen, this transaction 11182 * should have never made it onto the blocked queue. 11183 */ 11184 /* 11185 * This I/O is no longer blocked, we can remove it 11186 * from the blocked queue. Since this is a TAILQ 11187 * (doubly linked list), we can do O(1) removals 11188 * from any place on the list. 11189 */ 11190 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11191 blocked_links); 11192 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11193 11194 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11195 /* 11196 * Need to send IO back to original side to 11197 * run 11198 */ 11199 union ctl_ha_msg msg_info; 11200 11201 msg_info.hdr.original_sc = 11202 cur_blocked->io_hdr.original_sc; 11203 msg_info.hdr.serializing_sc = cur_blocked; 11204 msg_info.hdr.msg_type = CTL_MSG_R2R; 11205 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11206 &msg_info, sizeof(msg_info), 0)) > 11207 CTL_HA_STATUS_SUCCESS) { 11208 printf("CTL:Check Blocked error from " 11209 "ctl_ha_msg_send %d\n", 11210 isc_retval); 11211 } 11212 break; 11213 } 11214 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11215 11216 /* 11217 * Check this I/O for LUN state changes that may 11218 * have happened while this command was blocked. 11219 * The LUN state may have been changed by a command 11220 * ahead of us in the queue, so we need to re-check 11221 * for any states that can be caused by SCSI 11222 * commands. 11223 */ 11224 if (ctl_scsiio_lun_check(lun, entry, 11225 &cur_blocked->scsiio) == 0) { 11226 cur_blocked->io_hdr.flags |= 11227 CTL_FLAG_IS_WAS_ON_RTR; 11228 ctl_enqueue_rtr(cur_blocked); 11229 } else 11230 ctl_done(cur_blocked); 11231 break; 11232 } 11233 default: 11234 /* 11235 * This probably shouldn't happen -- we shouldn't 11236 * get CTL_ACTION_ERROR, or anything else. 11237 */ 11238 break; 11239 } 11240 } 11241 11242 return (CTL_RETVAL_COMPLETE); 11243 } 11244 11245 /* 11246 * This routine (with one exception) checks LUN flags that can be set by 11247 * commands ahead of us in the OOA queue. These flags have to be checked 11248 * when a command initially comes in, and when we pull a command off the 11249 * blocked queue and are preparing to execute it. The reason we have to 11250 * check these flags for commands on the blocked queue is that the LUN 11251 * state may have been changed by a command ahead of us while we're on the 11252 * blocked queue. 11253 * 11254 * Ordering is somewhat important with these checks, so please pay 11255 * careful attention to the placement of any new checks. 11256 */ 11257 static int 11258 ctl_scsiio_lun_check(struct ctl_lun *lun, 11259 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11260 { 11261 struct ctl_softc *softc = lun->ctl_softc; 11262 int retval; 11263 uint32_t residx; 11264 11265 retval = 0; 11266 11267 mtx_assert(&lun->lun_lock, MA_OWNED); 11268 11269 /* 11270 * If this shelf is a secondary shelf controller, we have to reject 11271 * any media access commands. 11272 */ 11273 if ((softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 11274 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 11275 ctl_set_lun_standby(ctsio); 11276 retval = 1; 11277 goto bailout; 11278 } 11279 11280 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11281 if (lun->flags & CTL_LUN_READONLY) { 11282 ctl_set_sense(ctsio, /*current_error*/ 1, 11283 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11284 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11285 retval = 1; 11286 goto bailout; 11287 } 11288 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11289 .eca_and_aen & SCP_SWP) != 0) { 11290 ctl_set_sense(ctsio, /*current_error*/ 1, 11291 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11292 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11293 retval = 1; 11294 goto bailout; 11295 } 11296 } 11297 11298 /* 11299 * Check for a reservation conflict. If this command isn't allowed 11300 * even on reserved LUNs, and if this initiator isn't the one who 11301 * reserved us, reject the command with a reservation conflict. 11302 */ 11303 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11304 if ((lun->flags & CTL_LUN_RESERVED) 11305 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11306 if (lun->res_idx != residx) { 11307 ctl_set_reservation_conflict(ctsio); 11308 retval = 1; 11309 goto bailout; 11310 } 11311 } 11312 11313 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11314 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11315 /* No reservation or command is allowed. */; 11316 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11317 (lun->res_type == SPR_TYPE_WR_EX || 11318 lun->res_type == SPR_TYPE_WR_EX_RO || 11319 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11320 /* The command is allowed for Write Exclusive resv. */; 11321 } else { 11322 /* 11323 * if we aren't registered or it's a res holder type 11324 * reservation and this isn't the res holder then set a 11325 * conflict. 11326 */ 11327 if (ctl_get_prkey(lun, residx) == 0 11328 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11329 ctl_set_reservation_conflict(ctsio); 11330 retval = 1; 11331 goto bailout; 11332 } 11333 11334 } 11335 11336 if ((lun->flags & CTL_LUN_OFFLINE) 11337 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11338 ctl_set_lun_not_ready(ctsio); 11339 retval = 1; 11340 goto bailout; 11341 } 11342 11343 /* 11344 * If the LUN is stopped, see if this particular command is allowed 11345 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11346 */ 11347 if ((lun->flags & CTL_LUN_STOPPED) 11348 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11349 /* "Logical unit not ready, initializing cmd. required" */ 11350 ctl_set_lun_stopped(ctsio); 11351 retval = 1; 11352 goto bailout; 11353 } 11354 11355 if ((lun->flags & CTL_LUN_INOPERABLE) 11356 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11357 /* "Medium format corrupted" */ 11358 ctl_set_medium_format_corrupted(ctsio); 11359 retval = 1; 11360 goto bailout; 11361 } 11362 11363 bailout: 11364 return (retval); 11365 11366 } 11367 11368 static void 11369 ctl_failover_io(union ctl_io *io, int have_lock) 11370 { 11371 ctl_set_busy(&io->scsiio); 11372 ctl_done(io); 11373 } 11374 11375 #ifdef notyet 11376 static void 11377 ctl_failover(void) 11378 { 11379 struct ctl_lun *lun; 11380 struct ctl_softc *softc; 11381 union ctl_io *next_io, *pending_io; 11382 union ctl_io *io; 11383 int lun_idx; 11384 11385 softc = control_softc; 11386 11387 mtx_lock(&softc->ctl_lock); 11388 /* 11389 * Remove any cmds from the other SC from the rtr queue. These 11390 * will obviously only be for LUNs for which we're the primary. 11391 * We can't send status or get/send data for these commands. 11392 * Since they haven't been executed yet, we can just remove them. 11393 * We'll either abort them or delete them below, depending on 11394 * which HA mode we're in. 11395 */ 11396 #ifdef notyet 11397 mtx_lock(&softc->queue_lock); 11398 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); 11399 io != NULL; io = next_io) { 11400 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11401 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11402 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr, 11403 ctl_io_hdr, links); 11404 } 11405 mtx_unlock(&softc->queue_lock); 11406 #endif 11407 11408 for (lun_idx=0; lun_idx < softc->num_luns; lun_idx++) { 11409 lun = softc->ctl_luns[lun_idx]; 11410 if (lun==NULL) 11411 continue; 11412 11413 /* 11414 * Processor LUNs are primary on both sides. 11415 * XXX will this always be true? 11416 */ 11417 if (lun->be_lun->lun_type == T_PROCESSOR) 11418 continue; 11419 11420 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11421 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11422 printf("FAILOVER: primary lun %d\n", lun_idx); 11423 /* 11424 * Remove all commands from the other SC. First from the 11425 * blocked queue then from the ooa queue. Once we have 11426 * removed them. Call ctl_check_blocked to see if there 11427 * is anything that can run. 11428 */ 11429 for (io = (union ctl_io *)TAILQ_FIRST( 11430 &lun->blocked_queue); io != NULL; io = next_io) { 11431 11432 next_io = (union ctl_io *)TAILQ_NEXT( 11433 &io->io_hdr, blocked_links); 11434 11435 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11436 TAILQ_REMOVE(&lun->blocked_queue, 11437 &io->io_hdr,blocked_links); 11438 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11439 TAILQ_REMOVE(&lun->ooa_queue, 11440 &io->io_hdr, ooa_links); 11441 11442 ctl_free_io(io); 11443 } 11444 } 11445 11446 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11447 io != NULL; io = next_io) { 11448 11449 next_io = (union ctl_io *)TAILQ_NEXT( 11450 &io->io_hdr, ooa_links); 11451 11452 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11453 11454 TAILQ_REMOVE(&lun->ooa_queue, 11455 &io->io_hdr, 11456 ooa_links); 11457 11458 ctl_free_io(io); 11459 } 11460 } 11461 ctl_check_blocked(lun); 11462 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11463 && (softc->ha_mode == CTL_HA_MODE_XFER)) { 11464 11465 printf("FAILOVER: primary lun %d\n", lun_idx); 11466 /* 11467 * Abort all commands from the other SC. We can't 11468 * send status back for them now. These should get 11469 * cleaned up when they are completed or come out 11470 * for a datamove operation. 11471 */ 11472 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11473 io != NULL; io = next_io) { 11474 next_io = (union ctl_io *)TAILQ_NEXT( 11475 &io->io_hdr, ooa_links); 11476 11477 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11478 io->io_hdr.flags |= CTL_FLAG_ABORT; 11479 } 11480 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11481 && (softc->ha_mode == CTL_HA_MODE_XFER)) { 11482 11483 printf("FAILOVER: secondary lun %d\n", lun_idx); 11484 11485 lun->flags |= CTL_LUN_PRIMARY_SC; 11486 11487 /* 11488 * We send all I/O that was sent to this controller 11489 * and redirected to the other side back with 11490 * busy status, and have the initiator retry it. 11491 * Figuring out how much data has been transferred, 11492 * etc. and picking up where we left off would be 11493 * very tricky. 11494 * 11495 * XXX KDM need to remove I/O from the blocked 11496 * queue as well! 11497 */ 11498 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11499 &lun->ooa_queue); pending_io != NULL; 11500 pending_io = next_io) { 11501 11502 next_io = (union ctl_io *)TAILQ_NEXT( 11503 &pending_io->io_hdr, ooa_links); 11504 11505 pending_io->io_hdr.flags &= 11506 ~CTL_FLAG_SENT_2OTHER_SC; 11507 11508 if (pending_io->io_hdr.flags & 11509 CTL_FLAG_IO_ACTIVE) { 11510 pending_io->io_hdr.flags |= 11511 CTL_FLAG_FAILOVER; 11512 } else { 11513 ctl_set_busy(&pending_io->scsiio); 11514 ctl_done(pending_io); 11515 } 11516 } 11517 11518 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11519 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11520 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11521 printf("FAILOVER: secondary lun %d\n", lun_idx); 11522 /* 11523 * if the first io on the OOA is not on the RtR queue 11524 * add it. 11525 */ 11526 lun->flags |= CTL_LUN_PRIMARY_SC; 11527 11528 pending_io = (union ctl_io *)TAILQ_FIRST( 11529 &lun->ooa_queue); 11530 if (pending_io==NULL) { 11531 printf("Nothing on OOA queue\n"); 11532 continue; 11533 } 11534 11535 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11536 if ((pending_io->io_hdr.flags & 11537 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11538 pending_io->io_hdr.flags |= 11539 CTL_FLAG_IS_WAS_ON_RTR; 11540 ctl_enqueue_rtr(pending_io); 11541 } 11542 #if 0 11543 else 11544 { 11545 printf("Tag 0x%04x is running\n", 11546 pending_io->scsiio.tag_num); 11547 } 11548 #endif 11549 11550 next_io = (union ctl_io *)TAILQ_NEXT( 11551 &pending_io->io_hdr, ooa_links); 11552 for (pending_io=next_io; pending_io != NULL; 11553 pending_io = next_io) { 11554 pending_io->io_hdr.flags &= 11555 ~CTL_FLAG_SENT_2OTHER_SC; 11556 next_io = (union ctl_io *)TAILQ_NEXT( 11557 &pending_io->io_hdr, ooa_links); 11558 if (pending_io->io_hdr.flags & 11559 CTL_FLAG_IS_WAS_ON_RTR) { 11560 #if 0 11561 printf("Tag 0x%04x is running\n", 11562 pending_io->scsiio.tag_num); 11563 #endif 11564 continue; 11565 } 11566 11567 switch (ctl_check_ooa(lun, pending_io, 11568 (union ctl_io *)TAILQ_PREV( 11569 &pending_io->io_hdr, ctl_ooaq, 11570 ooa_links))) { 11571 11572 case CTL_ACTION_BLOCK: 11573 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11574 &pending_io->io_hdr, 11575 blocked_links); 11576 pending_io->io_hdr.flags |= 11577 CTL_FLAG_BLOCKED; 11578 break; 11579 case CTL_ACTION_PASS: 11580 case CTL_ACTION_SKIP: 11581 pending_io->io_hdr.flags |= 11582 CTL_FLAG_IS_WAS_ON_RTR; 11583 ctl_enqueue_rtr(pending_io); 11584 break; 11585 case CTL_ACTION_OVERLAP: 11586 ctl_set_overlapped_cmd( 11587 (struct ctl_scsiio *)pending_io); 11588 ctl_done(pending_io); 11589 break; 11590 case CTL_ACTION_OVERLAP_TAG: 11591 ctl_set_overlapped_tag( 11592 (struct ctl_scsiio *)pending_io, 11593 pending_io->scsiio.tag_num & 0xff); 11594 ctl_done(pending_io); 11595 break; 11596 case CTL_ACTION_ERROR: 11597 default: 11598 ctl_set_internal_failure( 11599 (struct ctl_scsiio *)pending_io, 11600 0, // sks_valid 11601 0); //retry count 11602 ctl_done(pending_io); 11603 break; 11604 } 11605 } 11606 11607 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11608 } else { 11609 panic("Unhandled HA mode failover, LUN flags = %#x, " 11610 "ha_mode = #%x", lun->flags, softc->ha_mode); 11611 } 11612 } 11613 ctl_pause_rtr = 0; 11614 mtx_unlock(&softc->ctl_lock); 11615 } 11616 #endif 11617 11618 static void 11619 ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx, 11620 ctl_ua_type ua_type) 11621 { 11622 struct ctl_lun *lun; 11623 ctl_ua_type *pu; 11624 11625 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 11626 11627 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 11628 mtx_lock(&lun->lun_lock); 11629 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 11630 if (pu != NULL) 11631 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua_type; 11632 mtx_unlock(&lun->lun_lock); 11633 } 11634 } 11635 11636 static int 11637 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11638 { 11639 struct ctl_lun *lun; 11640 const struct ctl_cmd_entry *entry; 11641 uint32_t initidx, targ_lun; 11642 int retval; 11643 11644 retval = 0; 11645 11646 lun = NULL; 11647 11648 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11649 if ((targ_lun < CTL_MAX_LUNS) 11650 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11651 /* 11652 * If the LUN is invalid, pretend that it doesn't exist. 11653 * It will go away as soon as all pending I/O has been 11654 * completed. 11655 */ 11656 mtx_lock(&lun->lun_lock); 11657 if (lun->flags & CTL_LUN_DISABLED) { 11658 mtx_unlock(&lun->lun_lock); 11659 lun = NULL; 11660 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11661 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11662 } else { 11663 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11664 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11665 lun->be_lun; 11666 if (lun->be_lun->lun_type == T_PROCESSOR) { 11667 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11668 } 11669 11670 /* 11671 * Every I/O goes into the OOA queue for a 11672 * particular LUN, and stays there until completion. 11673 */ 11674 #ifdef CTL_TIME_IO 11675 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11676 lun->idle_time += getsbinuptime() - 11677 lun->last_busy; 11678 } 11679 #endif 11680 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11681 ooa_links); 11682 } 11683 } else { 11684 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11685 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11686 } 11687 11688 /* Get command entry and return error if it is unsuppotyed. */ 11689 entry = ctl_validate_command(ctsio); 11690 if (entry == NULL) { 11691 if (lun) 11692 mtx_unlock(&lun->lun_lock); 11693 return (retval); 11694 } 11695 11696 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11697 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11698 11699 /* 11700 * Check to see whether we can send this command to LUNs that don't 11701 * exist. This should pretty much only be the case for inquiry 11702 * and request sense. Further checks, below, really require having 11703 * a LUN, so we can't really check the command anymore. Just put 11704 * it on the rtr queue. 11705 */ 11706 if (lun == NULL) { 11707 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11708 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11709 ctl_enqueue_rtr((union ctl_io *)ctsio); 11710 return (retval); 11711 } 11712 11713 ctl_set_unsupported_lun(ctsio); 11714 ctl_done((union ctl_io *)ctsio); 11715 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11716 return (retval); 11717 } else { 11718 /* 11719 * Make sure we support this particular command on this LUN. 11720 * e.g., we don't support writes to the control LUN. 11721 */ 11722 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11723 mtx_unlock(&lun->lun_lock); 11724 ctl_set_invalid_opcode(ctsio); 11725 ctl_done((union ctl_io *)ctsio); 11726 return (retval); 11727 } 11728 } 11729 11730 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11731 11732 #ifdef CTL_WITH_CA 11733 /* 11734 * If we've got a request sense, it'll clear the contingent 11735 * allegiance condition. Otherwise, if we have a CA condition for 11736 * this initiator, clear it, because it sent down a command other 11737 * than request sense. 11738 */ 11739 if ((ctsio->cdb[0] != REQUEST_SENSE) 11740 && (ctl_is_set(lun->have_ca, initidx))) 11741 ctl_clear_mask(lun->have_ca, initidx); 11742 #endif 11743 11744 /* 11745 * If the command has this flag set, it handles its own unit 11746 * attention reporting, we shouldn't do anything. Otherwise we 11747 * check for any pending unit attentions, and send them back to the 11748 * initiator. We only do this when a command initially comes in, 11749 * not when we pull it off the blocked queue. 11750 * 11751 * According to SAM-3, section 5.3.2, the order that things get 11752 * presented back to the host is basically unit attentions caused 11753 * by some sort of reset event, busy status, reservation conflicts 11754 * or task set full, and finally any other status. 11755 * 11756 * One issue here is that some of the unit attentions we report 11757 * don't fall into the "reset" category (e.g. "reported luns data 11758 * has changed"). So reporting it here, before the reservation 11759 * check, may be technically wrong. I guess the only thing to do 11760 * would be to check for and report the reset events here, and then 11761 * check for the other unit attention types after we check for a 11762 * reservation conflict. 11763 * 11764 * XXX KDM need to fix this 11765 */ 11766 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11767 ctl_ua_type ua_type; 11768 scsi_sense_data_type sense_format; 11769 11770 if (lun->flags & CTL_LUN_SENSE_DESC) 11771 sense_format = SSD_TYPE_DESC; 11772 else 11773 sense_format = SSD_TYPE_FIXED; 11774 11775 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11776 sense_format); 11777 if (ua_type != CTL_UA_NONE) { 11778 mtx_unlock(&lun->lun_lock); 11779 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11780 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11781 ctsio->sense_len = SSD_FULL_SIZE; 11782 ctl_done((union ctl_io *)ctsio); 11783 return (retval); 11784 } 11785 } 11786 11787 11788 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11789 mtx_unlock(&lun->lun_lock); 11790 ctl_done((union ctl_io *)ctsio); 11791 return (retval); 11792 } 11793 11794 /* 11795 * XXX CHD this is where we want to send IO to other side if 11796 * this LUN is secondary on this SC. We will need to make a copy 11797 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11798 * the copy we send as FROM_OTHER. 11799 * We also need to stuff the address of the original IO so we can 11800 * find it easily. Something similar will need be done on the other 11801 * side so when we are done we can find the copy. 11802 */ 11803 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11804 union ctl_ha_msg msg_info; 11805 int isc_retval; 11806 11807 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11808 11809 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11810 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11811 #if 0 11812 printf("1. ctsio %p\n", ctsio); 11813 #endif 11814 msg_info.hdr.serializing_sc = NULL; 11815 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11816 msg_info.scsi.tag_num = ctsio->tag_num; 11817 msg_info.scsi.tag_type = ctsio->tag_type; 11818 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11819 11820 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11821 11822 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11823 (void *)&msg_info, sizeof(msg_info), 0)) > 11824 CTL_HA_STATUS_SUCCESS) { 11825 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11826 isc_retval); 11827 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11828 } else { 11829 #if 0 11830 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11831 #endif 11832 } 11833 11834 /* 11835 * XXX KDM this I/O is off the incoming queue, but hasn't 11836 * been inserted on any other queue. We may need to come 11837 * up with a holding queue while we wait for serialization 11838 * so that we have an idea of what we're waiting for from 11839 * the other side. 11840 */ 11841 mtx_unlock(&lun->lun_lock); 11842 return (retval); 11843 } 11844 11845 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11846 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11847 ctl_ooaq, ooa_links))) { 11848 case CTL_ACTION_BLOCK: 11849 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11850 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11851 blocked_links); 11852 mtx_unlock(&lun->lun_lock); 11853 return (retval); 11854 case CTL_ACTION_PASS: 11855 case CTL_ACTION_SKIP: 11856 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11857 mtx_unlock(&lun->lun_lock); 11858 ctl_enqueue_rtr((union ctl_io *)ctsio); 11859 break; 11860 case CTL_ACTION_OVERLAP: 11861 mtx_unlock(&lun->lun_lock); 11862 ctl_set_overlapped_cmd(ctsio); 11863 ctl_done((union ctl_io *)ctsio); 11864 break; 11865 case CTL_ACTION_OVERLAP_TAG: 11866 mtx_unlock(&lun->lun_lock); 11867 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11868 ctl_done((union ctl_io *)ctsio); 11869 break; 11870 case CTL_ACTION_ERROR: 11871 default: 11872 mtx_unlock(&lun->lun_lock); 11873 ctl_set_internal_failure(ctsio, 11874 /*sks_valid*/ 0, 11875 /*retry_count*/ 0); 11876 ctl_done((union ctl_io *)ctsio); 11877 break; 11878 } 11879 return (retval); 11880 } 11881 11882 const struct ctl_cmd_entry * 11883 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11884 { 11885 const struct ctl_cmd_entry *entry; 11886 int service_action; 11887 11888 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11889 if (sa) 11890 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11891 if (entry->flags & CTL_CMD_FLAG_SA5) { 11892 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11893 entry = &((const struct ctl_cmd_entry *) 11894 entry->execute)[service_action]; 11895 } 11896 return (entry); 11897 } 11898 11899 const struct ctl_cmd_entry * 11900 ctl_validate_command(struct ctl_scsiio *ctsio) 11901 { 11902 const struct ctl_cmd_entry *entry; 11903 int i, sa; 11904 uint8_t diff; 11905 11906 entry = ctl_get_cmd_entry(ctsio, &sa); 11907 if (entry->execute == NULL) { 11908 if (sa) 11909 ctl_set_invalid_field(ctsio, 11910 /*sks_valid*/ 1, 11911 /*command*/ 1, 11912 /*field*/ 1, 11913 /*bit_valid*/ 1, 11914 /*bit*/ 4); 11915 else 11916 ctl_set_invalid_opcode(ctsio); 11917 ctl_done((union ctl_io *)ctsio); 11918 return (NULL); 11919 } 11920 KASSERT(entry->length > 0, 11921 ("Not defined length for command 0x%02x/0x%02x", 11922 ctsio->cdb[0], ctsio->cdb[1])); 11923 for (i = 1; i < entry->length; i++) { 11924 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11925 if (diff == 0) 11926 continue; 11927 ctl_set_invalid_field(ctsio, 11928 /*sks_valid*/ 1, 11929 /*command*/ 1, 11930 /*field*/ i, 11931 /*bit_valid*/ 1, 11932 /*bit*/ fls(diff) - 1); 11933 ctl_done((union ctl_io *)ctsio); 11934 return (NULL); 11935 } 11936 return (entry); 11937 } 11938 11939 static int 11940 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11941 { 11942 11943 switch (lun_type) { 11944 case T_PROCESSOR: 11945 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11946 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11947 return (0); 11948 break; 11949 case T_DIRECT: 11950 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11951 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11952 return (0); 11953 break; 11954 default: 11955 return (0); 11956 } 11957 return (1); 11958 } 11959 11960 static int 11961 ctl_scsiio(struct ctl_scsiio *ctsio) 11962 { 11963 int retval; 11964 const struct ctl_cmd_entry *entry; 11965 11966 retval = CTL_RETVAL_COMPLETE; 11967 11968 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11969 11970 entry = ctl_get_cmd_entry(ctsio, NULL); 11971 11972 /* 11973 * If this I/O has been aborted, just send it straight to 11974 * ctl_done() without executing it. 11975 */ 11976 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11977 ctl_done((union ctl_io *)ctsio); 11978 goto bailout; 11979 } 11980 11981 /* 11982 * All the checks should have been handled by ctl_scsiio_precheck(). 11983 * We should be clear now to just execute the I/O. 11984 */ 11985 retval = entry->execute(ctsio); 11986 11987 bailout: 11988 return (retval); 11989 } 11990 11991 /* 11992 * Since we only implement one target right now, a bus reset simply resets 11993 * our single target. 11994 */ 11995 static int 11996 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11997 { 11998 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11999 } 12000 12001 static int 12002 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 12003 ctl_ua_type ua_type) 12004 { 12005 struct ctl_lun *lun; 12006 int retval; 12007 12008 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12009 union ctl_ha_msg msg_info; 12010 12011 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12012 msg_info.hdr.nexus = io->io_hdr.nexus; 12013 if (ua_type==CTL_UA_TARG_RESET) 12014 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 12015 else 12016 msg_info.task.task_action = CTL_TASK_BUS_RESET; 12017 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12018 msg_info.hdr.original_sc = NULL; 12019 msg_info.hdr.serializing_sc = NULL; 12020 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12021 (void *)&msg_info, sizeof(msg_info), 0)) { 12022 } 12023 } 12024 retval = 0; 12025 12026 mtx_lock(&softc->ctl_lock); 12027 STAILQ_FOREACH(lun, &softc->lun_list, links) 12028 retval += ctl_lun_reset(lun, io, ua_type); 12029 mtx_unlock(&softc->ctl_lock); 12030 12031 return (retval); 12032 } 12033 12034 /* 12035 * The LUN should always be set. The I/O is optional, and is used to 12036 * distinguish between I/Os sent by this initiator, and by other 12037 * initiators. We set unit attention for initiators other than this one. 12038 * SAM-3 is vague on this point. It does say that a unit attention should 12039 * be established for other initiators when a LUN is reset (see section 12040 * 5.7.3), but it doesn't specifically say that the unit attention should 12041 * be established for this particular initiator when a LUN is reset. Here 12042 * is the relevant text, from SAM-3 rev 8: 12043 * 12044 * 5.7.2 When a SCSI initiator port aborts its own tasks 12045 * 12046 * When a SCSI initiator port causes its own task(s) to be aborted, no 12047 * notification that the task(s) have been aborted shall be returned to 12048 * the SCSI initiator port other than the completion response for the 12049 * command or task management function action that caused the task(s) to 12050 * be aborted and notification(s) associated with related effects of the 12051 * action (e.g., a reset unit attention condition). 12052 * 12053 * XXX KDM for now, we're setting unit attention for all initiators. 12054 */ 12055 static int 12056 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 12057 { 12058 union ctl_io *xio; 12059 #if 0 12060 uint32_t initidx; 12061 #endif 12062 #ifdef CTL_WITH_CA 12063 int i; 12064 #endif 12065 12066 mtx_lock(&lun->lun_lock); 12067 /* 12068 * Run through the OOA queue and abort each I/O. 12069 */ 12070 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12071 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12072 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 12073 } 12074 12075 /* 12076 * This version sets unit attention for every 12077 */ 12078 #if 0 12079 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12080 ctl_est_ua_all(lun, initidx, ua_type); 12081 #else 12082 ctl_est_ua_all(lun, -1, ua_type); 12083 #endif 12084 12085 /* 12086 * A reset (any kind, really) clears reservations established with 12087 * RESERVE/RELEASE. It does not clear reservations established 12088 * with PERSISTENT RESERVE OUT, but we don't support that at the 12089 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 12090 * reservations made with the RESERVE/RELEASE commands, because 12091 * those commands are obsolete in SPC-3. 12092 */ 12093 lun->flags &= ~CTL_LUN_RESERVED; 12094 12095 #ifdef CTL_WITH_CA 12096 for (i = 0; i < CTL_MAX_INITIATORS; i++) 12097 ctl_clear_mask(lun->have_ca, i); 12098 #endif 12099 mtx_unlock(&lun->lun_lock); 12100 12101 return (0); 12102 } 12103 12104 static void 12105 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12106 int other_sc) 12107 { 12108 union ctl_io *xio; 12109 12110 mtx_assert(&lun->lun_lock, MA_OWNED); 12111 12112 /* 12113 * Run through the OOA queue and attempt to find the given I/O. 12114 * The target port, initiator ID, tag type and tag number have to 12115 * match the values that we got from the initiator. If we have an 12116 * untagged command to abort, simply abort the first untagged command 12117 * we come to. We only allow one untagged command at a time of course. 12118 */ 12119 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12120 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12121 12122 if ((targ_port == UINT32_MAX || 12123 targ_port == xio->io_hdr.nexus.targ_port) && 12124 (init_id == UINT32_MAX || 12125 init_id == xio->io_hdr.nexus.initid.id)) { 12126 if (targ_port != xio->io_hdr.nexus.targ_port || 12127 init_id != xio->io_hdr.nexus.initid.id) 12128 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12129 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12130 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12131 union ctl_ha_msg msg_info; 12132 12133 msg_info.hdr.nexus = xio->io_hdr.nexus; 12134 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12135 msg_info.task.tag_num = xio->scsiio.tag_num; 12136 msg_info.task.tag_type = xio->scsiio.tag_type; 12137 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12138 msg_info.hdr.original_sc = NULL; 12139 msg_info.hdr.serializing_sc = NULL; 12140 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12141 (void *)&msg_info, sizeof(msg_info), 0); 12142 } 12143 } 12144 } 12145 } 12146 12147 static int 12148 ctl_abort_task_set(union ctl_io *io) 12149 { 12150 struct ctl_softc *softc = control_softc; 12151 struct ctl_lun *lun; 12152 uint32_t targ_lun; 12153 12154 /* 12155 * Look up the LUN. 12156 */ 12157 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12158 mtx_lock(&softc->ctl_lock); 12159 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12160 lun = softc->ctl_luns[targ_lun]; 12161 else { 12162 mtx_unlock(&softc->ctl_lock); 12163 return (1); 12164 } 12165 12166 mtx_lock(&lun->lun_lock); 12167 mtx_unlock(&softc->ctl_lock); 12168 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12169 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12170 io->io_hdr.nexus.initid.id, 12171 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12172 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12173 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12174 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12175 } 12176 mtx_unlock(&lun->lun_lock); 12177 return (0); 12178 } 12179 12180 static int 12181 ctl_i_t_nexus_reset(union ctl_io *io) 12182 { 12183 struct ctl_softc *softc = control_softc; 12184 struct ctl_lun *lun; 12185 uint32_t initidx, residx; 12186 12187 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12188 residx = ctl_get_resindex(&io->io_hdr.nexus); 12189 mtx_lock(&softc->ctl_lock); 12190 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12191 mtx_lock(&lun->lun_lock); 12192 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12193 io->io_hdr.nexus.initid.id, 12194 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12195 #ifdef CTL_WITH_CA 12196 ctl_clear_mask(lun->have_ca, initidx); 12197 #endif 12198 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12199 lun->flags &= ~CTL_LUN_RESERVED; 12200 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12201 mtx_unlock(&lun->lun_lock); 12202 } 12203 mtx_unlock(&softc->ctl_lock); 12204 return (0); 12205 } 12206 12207 static int 12208 ctl_abort_task(union ctl_io *io) 12209 { 12210 union ctl_io *xio; 12211 struct ctl_lun *lun; 12212 struct ctl_softc *softc; 12213 #if 0 12214 struct sbuf sb; 12215 char printbuf[128]; 12216 #endif 12217 int found; 12218 uint32_t targ_lun; 12219 12220 softc = control_softc; 12221 found = 0; 12222 12223 /* 12224 * Look up the LUN. 12225 */ 12226 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12227 mtx_lock(&softc->ctl_lock); 12228 if ((targ_lun < CTL_MAX_LUNS) 12229 && (softc->ctl_luns[targ_lun] != NULL)) 12230 lun = softc->ctl_luns[targ_lun]; 12231 else { 12232 mtx_unlock(&softc->ctl_lock); 12233 return (1); 12234 } 12235 12236 #if 0 12237 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12238 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12239 #endif 12240 12241 mtx_lock(&lun->lun_lock); 12242 mtx_unlock(&softc->ctl_lock); 12243 /* 12244 * Run through the OOA queue and attempt to find the given I/O. 12245 * The target port, initiator ID, tag type and tag number have to 12246 * match the values that we got from the initiator. If we have an 12247 * untagged command to abort, simply abort the first untagged command 12248 * we come to. We only allow one untagged command at a time of course. 12249 */ 12250 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12251 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12252 #if 0 12253 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12254 12255 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12256 lun->lun, xio->scsiio.tag_num, 12257 xio->scsiio.tag_type, 12258 (xio->io_hdr.blocked_links.tqe_prev 12259 == NULL) ? "" : " BLOCKED", 12260 (xio->io_hdr.flags & 12261 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12262 (xio->io_hdr.flags & 12263 CTL_FLAG_ABORT) ? " ABORT" : "", 12264 (xio->io_hdr.flags & 12265 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12266 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12267 sbuf_finish(&sb); 12268 printf("%s\n", sbuf_data(&sb)); 12269 #endif 12270 12271 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12272 || (xio->io_hdr.nexus.initid.id != io->io_hdr.nexus.initid.id) 12273 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12274 continue; 12275 12276 /* 12277 * If the abort says that the task is untagged, the 12278 * task in the queue must be untagged. Otherwise, 12279 * we just check to see whether the tag numbers 12280 * match. This is because the QLogic firmware 12281 * doesn't pass back the tag type in an abort 12282 * request. 12283 */ 12284 #if 0 12285 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12286 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12287 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12288 #endif 12289 /* 12290 * XXX KDM we've got problems with FC, because it 12291 * doesn't send down a tag type with aborts. So we 12292 * can only really go by the tag number... 12293 * This may cause problems with parallel SCSI. 12294 * Need to figure that out!! 12295 */ 12296 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12297 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12298 found = 1; 12299 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12300 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12301 union ctl_ha_msg msg_info; 12302 12303 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12304 msg_info.hdr.nexus = io->io_hdr.nexus; 12305 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12306 msg_info.task.tag_num = io->taskio.tag_num; 12307 msg_info.task.tag_type = io->taskio.tag_type; 12308 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12309 msg_info.hdr.original_sc = NULL; 12310 msg_info.hdr.serializing_sc = NULL; 12311 #if 0 12312 printf("Sent Abort to other side\n"); 12313 #endif 12314 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12315 (void *)&msg_info, sizeof(msg_info), 0) != 12316 CTL_HA_STATUS_SUCCESS) { 12317 } 12318 } 12319 #if 0 12320 printf("ctl_abort_task: found I/O to abort\n"); 12321 #endif 12322 } 12323 } 12324 mtx_unlock(&lun->lun_lock); 12325 12326 if (found == 0) { 12327 /* 12328 * This isn't really an error. It's entirely possible for 12329 * the abort and command completion to cross on the wire. 12330 * This is more of an informative/diagnostic error. 12331 */ 12332 #if 0 12333 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12334 "%d:%d:%d:%d tag %d type %d\n", 12335 io->io_hdr.nexus.initid.id, 12336 io->io_hdr.nexus.targ_port, 12337 io->io_hdr.nexus.targ_target.id, 12338 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12339 io->taskio.tag_type); 12340 #endif 12341 } 12342 return (0); 12343 } 12344 12345 static void 12346 ctl_run_task(union ctl_io *io) 12347 { 12348 struct ctl_softc *softc = control_softc; 12349 int retval = 1; 12350 const char *task_desc; 12351 12352 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12353 12354 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12355 ("ctl_run_task: Unextected io_type %d\n", 12356 io->io_hdr.io_type)); 12357 12358 task_desc = ctl_scsi_task_string(&io->taskio); 12359 if (task_desc != NULL) { 12360 #ifdef NEEDTOPORT 12361 csevent_log(CSC_CTL | CSC_SHELF_SW | 12362 CTL_TASK_REPORT, 12363 csevent_LogType_Trace, 12364 csevent_Severity_Information, 12365 csevent_AlertLevel_Green, 12366 csevent_FRU_Firmware, 12367 csevent_FRU_Unknown, 12368 "CTL: received task: %s",task_desc); 12369 #endif 12370 } else { 12371 #ifdef NEEDTOPORT 12372 csevent_log(CSC_CTL | CSC_SHELF_SW | 12373 CTL_TASK_REPORT, 12374 csevent_LogType_Trace, 12375 csevent_Severity_Information, 12376 csevent_AlertLevel_Green, 12377 csevent_FRU_Firmware, 12378 csevent_FRU_Unknown, 12379 "CTL: received unknown task " 12380 "type: %d (%#x)", 12381 io->taskio.task_action, 12382 io->taskio.task_action); 12383 #endif 12384 } 12385 switch (io->taskio.task_action) { 12386 case CTL_TASK_ABORT_TASK: 12387 retval = ctl_abort_task(io); 12388 break; 12389 case CTL_TASK_ABORT_TASK_SET: 12390 case CTL_TASK_CLEAR_TASK_SET: 12391 retval = ctl_abort_task_set(io); 12392 break; 12393 case CTL_TASK_CLEAR_ACA: 12394 break; 12395 case CTL_TASK_I_T_NEXUS_RESET: 12396 retval = ctl_i_t_nexus_reset(io); 12397 break; 12398 case CTL_TASK_LUN_RESET: { 12399 struct ctl_lun *lun; 12400 uint32_t targ_lun; 12401 12402 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12403 mtx_lock(&softc->ctl_lock); 12404 if ((targ_lun < CTL_MAX_LUNS) 12405 && (softc->ctl_luns[targ_lun] != NULL)) 12406 lun = softc->ctl_luns[targ_lun]; 12407 else { 12408 mtx_unlock(&softc->ctl_lock); 12409 retval = 1; 12410 break; 12411 } 12412 12413 if (!(io->io_hdr.flags & 12414 CTL_FLAG_FROM_OTHER_SC)) { 12415 union ctl_ha_msg msg_info; 12416 12417 io->io_hdr.flags |= 12418 CTL_FLAG_SENT_2OTHER_SC; 12419 msg_info.hdr.msg_type = 12420 CTL_MSG_MANAGE_TASKS; 12421 msg_info.hdr.nexus = io->io_hdr.nexus; 12422 msg_info.task.task_action = 12423 CTL_TASK_LUN_RESET; 12424 msg_info.hdr.original_sc = NULL; 12425 msg_info.hdr.serializing_sc = NULL; 12426 if (CTL_HA_STATUS_SUCCESS != 12427 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12428 (void *)&msg_info, 12429 sizeof(msg_info), 0)) { 12430 } 12431 } 12432 12433 retval = ctl_lun_reset(lun, io, 12434 CTL_UA_LUN_RESET); 12435 mtx_unlock(&softc->ctl_lock); 12436 break; 12437 } 12438 case CTL_TASK_TARGET_RESET: 12439 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12440 break; 12441 case CTL_TASK_BUS_RESET: 12442 retval = ctl_bus_reset(softc, io); 12443 break; 12444 case CTL_TASK_PORT_LOGIN: 12445 break; 12446 case CTL_TASK_PORT_LOGOUT: 12447 break; 12448 default: 12449 printf("ctl_run_task: got unknown task management event %d\n", 12450 io->taskio.task_action); 12451 break; 12452 } 12453 if (retval == 0) 12454 io->io_hdr.status = CTL_SUCCESS; 12455 else 12456 io->io_hdr.status = CTL_ERROR; 12457 ctl_done(io); 12458 } 12459 12460 /* 12461 * For HA operation. Handle commands that come in from the other 12462 * controller. 12463 */ 12464 static void 12465 ctl_handle_isc(union ctl_io *io) 12466 { 12467 int free_io; 12468 struct ctl_lun *lun; 12469 struct ctl_softc *softc; 12470 uint32_t targ_lun; 12471 12472 softc = control_softc; 12473 12474 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12475 lun = softc->ctl_luns[targ_lun]; 12476 12477 switch (io->io_hdr.msg_type) { 12478 case CTL_MSG_SERIALIZE: 12479 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12480 break; 12481 case CTL_MSG_R2R: { 12482 const struct ctl_cmd_entry *entry; 12483 12484 /* 12485 * This is only used in SER_ONLY mode. 12486 */ 12487 free_io = 0; 12488 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12489 mtx_lock(&lun->lun_lock); 12490 if (ctl_scsiio_lun_check(lun, 12491 entry, (struct ctl_scsiio *)io) != 0) { 12492 mtx_unlock(&lun->lun_lock); 12493 ctl_done(io); 12494 break; 12495 } 12496 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12497 mtx_unlock(&lun->lun_lock); 12498 ctl_enqueue_rtr(io); 12499 break; 12500 } 12501 case CTL_MSG_FINISH_IO: 12502 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12503 free_io = 0; 12504 ctl_done(io); 12505 } else { 12506 free_io = 1; 12507 mtx_lock(&lun->lun_lock); 12508 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12509 ooa_links); 12510 ctl_check_blocked(lun); 12511 mtx_unlock(&lun->lun_lock); 12512 } 12513 break; 12514 case CTL_MSG_PERS_ACTION: 12515 ctl_hndl_per_res_out_on_other_sc( 12516 (union ctl_ha_msg *)&io->presio.pr_msg); 12517 free_io = 1; 12518 break; 12519 case CTL_MSG_BAD_JUJU: 12520 free_io = 0; 12521 ctl_done(io); 12522 break; 12523 case CTL_MSG_DATAMOVE: 12524 /* Only used in XFER mode */ 12525 free_io = 0; 12526 ctl_datamove_remote(io); 12527 break; 12528 case CTL_MSG_DATAMOVE_DONE: 12529 /* Only used in XFER mode */ 12530 free_io = 0; 12531 io->scsiio.be_move_done(io); 12532 break; 12533 default: 12534 free_io = 1; 12535 printf("%s: Invalid message type %d\n", 12536 __func__, io->io_hdr.msg_type); 12537 break; 12538 } 12539 if (free_io) 12540 ctl_free_io(io); 12541 12542 } 12543 12544 12545 /* 12546 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12547 * there is no match. 12548 */ 12549 static ctl_lun_error_pattern 12550 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12551 { 12552 const struct ctl_cmd_entry *entry; 12553 ctl_lun_error_pattern filtered_pattern, pattern; 12554 12555 pattern = desc->error_pattern; 12556 12557 /* 12558 * XXX KDM we need more data passed into this function to match a 12559 * custom pattern, and we actually need to implement custom pattern 12560 * matching. 12561 */ 12562 if (pattern & CTL_LUN_PAT_CMD) 12563 return (CTL_LUN_PAT_CMD); 12564 12565 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12566 return (CTL_LUN_PAT_ANY); 12567 12568 entry = ctl_get_cmd_entry(ctsio, NULL); 12569 12570 filtered_pattern = entry->pattern & pattern; 12571 12572 /* 12573 * If the user requested specific flags in the pattern (e.g. 12574 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12575 * flags. 12576 * 12577 * If the user did not specify any flags, it doesn't matter whether 12578 * or not the command supports the flags. 12579 */ 12580 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12581 (pattern & ~CTL_LUN_PAT_MASK)) 12582 return (CTL_LUN_PAT_NONE); 12583 12584 /* 12585 * If the user asked for a range check, see if the requested LBA 12586 * range overlaps with this command's LBA range. 12587 */ 12588 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12589 uint64_t lba1; 12590 uint64_t len1; 12591 ctl_action action; 12592 int retval; 12593 12594 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12595 if (retval != 0) 12596 return (CTL_LUN_PAT_NONE); 12597 12598 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12599 desc->lba_range.len, FALSE); 12600 /* 12601 * A "pass" means that the LBA ranges don't overlap, so 12602 * this doesn't match the user's range criteria. 12603 */ 12604 if (action == CTL_ACTION_PASS) 12605 return (CTL_LUN_PAT_NONE); 12606 } 12607 12608 return (filtered_pattern); 12609 } 12610 12611 static void 12612 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12613 { 12614 struct ctl_error_desc *desc, *desc2; 12615 12616 mtx_assert(&lun->lun_lock, MA_OWNED); 12617 12618 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12619 ctl_lun_error_pattern pattern; 12620 /* 12621 * Check to see whether this particular command matches 12622 * the pattern in the descriptor. 12623 */ 12624 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12625 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12626 continue; 12627 12628 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12629 case CTL_LUN_INJ_ABORTED: 12630 ctl_set_aborted(&io->scsiio); 12631 break; 12632 case CTL_LUN_INJ_MEDIUM_ERR: 12633 ctl_set_medium_error(&io->scsiio); 12634 break; 12635 case CTL_LUN_INJ_UA: 12636 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12637 * OCCURRED */ 12638 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12639 break; 12640 case CTL_LUN_INJ_CUSTOM: 12641 /* 12642 * We're assuming the user knows what he is doing. 12643 * Just copy the sense information without doing 12644 * checks. 12645 */ 12646 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12647 MIN(sizeof(desc->custom_sense), 12648 sizeof(io->scsiio.sense_data))); 12649 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12650 io->scsiio.sense_len = SSD_FULL_SIZE; 12651 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12652 break; 12653 case CTL_LUN_INJ_NONE: 12654 default: 12655 /* 12656 * If this is an error injection type we don't know 12657 * about, clear the continuous flag (if it is set) 12658 * so it will get deleted below. 12659 */ 12660 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12661 break; 12662 } 12663 /* 12664 * By default, each error injection action is a one-shot 12665 */ 12666 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12667 continue; 12668 12669 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12670 12671 free(desc, M_CTL); 12672 } 12673 } 12674 12675 #ifdef CTL_IO_DELAY 12676 static void 12677 ctl_datamove_timer_wakeup(void *arg) 12678 { 12679 union ctl_io *io; 12680 12681 io = (union ctl_io *)arg; 12682 12683 ctl_datamove(io); 12684 } 12685 #endif /* CTL_IO_DELAY */ 12686 12687 void 12688 ctl_datamove(union ctl_io *io) 12689 { 12690 void (*fe_datamove)(union ctl_io *io); 12691 12692 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12693 12694 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12695 12696 #ifdef CTL_TIME_IO 12697 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12698 char str[256]; 12699 char path_str[64]; 12700 struct sbuf sb; 12701 12702 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12703 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12704 12705 sbuf_cat(&sb, path_str); 12706 switch (io->io_hdr.io_type) { 12707 case CTL_IO_SCSI: 12708 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12709 sbuf_printf(&sb, "\n"); 12710 sbuf_cat(&sb, path_str); 12711 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12712 io->scsiio.tag_num, io->scsiio.tag_type); 12713 break; 12714 case CTL_IO_TASK: 12715 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12716 "Tag Type: %d\n", io->taskio.task_action, 12717 io->taskio.tag_num, io->taskio.tag_type); 12718 break; 12719 default: 12720 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12721 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12722 break; 12723 } 12724 sbuf_cat(&sb, path_str); 12725 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12726 (intmax_t)time_uptime - io->io_hdr.start_time); 12727 sbuf_finish(&sb); 12728 printf("%s", sbuf_data(&sb)); 12729 } 12730 #endif /* CTL_TIME_IO */ 12731 12732 #ifdef CTL_IO_DELAY 12733 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12734 struct ctl_lun *lun; 12735 12736 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12737 12738 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12739 } else { 12740 struct ctl_lun *lun; 12741 12742 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12743 if ((lun != NULL) 12744 && (lun->delay_info.datamove_delay > 0)) { 12745 struct callout *callout; 12746 12747 callout = (struct callout *)&io->io_hdr.timer_bytes; 12748 callout_init(callout, /*mpsafe*/ 1); 12749 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12750 callout_reset(callout, 12751 lun->delay_info.datamove_delay * hz, 12752 ctl_datamove_timer_wakeup, io); 12753 if (lun->delay_info.datamove_type == 12754 CTL_DELAY_TYPE_ONESHOT) 12755 lun->delay_info.datamove_delay = 0; 12756 return; 12757 } 12758 } 12759 #endif 12760 12761 /* 12762 * This command has been aborted. Set the port status, so we fail 12763 * the data move. 12764 */ 12765 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12766 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12767 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12768 io->io_hdr.nexus.targ_port, 12769 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12770 io->io_hdr.nexus.targ_lun); 12771 io->io_hdr.port_status = 31337; 12772 /* 12773 * Note that the backend, in this case, will get the 12774 * callback in its context. In other cases it may get 12775 * called in the frontend's interrupt thread context. 12776 */ 12777 io->scsiio.be_move_done(io); 12778 return; 12779 } 12780 12781 /* Don't confuse frontend with zero length data move. */ 12782 if (io->scsiio.kern_data_len == 0) { 12783 io->scsiio.be_move_done(io); 12784 return; 12785 } 12786 12787 /* 12788 * If we're in XFER mode and this I/O is from the other shelf 12789 * controller, we need to send the DMA to the other side to 12790 * actually transfer the data to/from the host. In serialize only 12791 * mode the transfer happens below CTL and ctl_datamove() is only 12792 * called on the machine that originally received the I/O. 12793 */ 12794 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12795 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12796 union ctl_ha_msg msg; 12797 uint32_t sg_entries_sent; 12798 int do_sg_copy; 12799 int i; 12800 12801 memset(&msg, 0, sizeof(msg)); 12802 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12803 msg.hdr.original_sc = io->io_hdr.original_sc; 12804 msg.hdr.serializing_sc = io; 12805 msg.hdr.nexus = io->io_hdr.nexus; 12806 msg.dt.flags = io->io_hdr.flags; 12807 /* 12808 * We convert everything into a S/G list here. We can't 12809 * pass by reference, only by value between controllers. 12810 * So we can't pass a pointer to the S/G list, only as many 12811 * S/G entries as we can fit in here. If it's possible for 12812 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12813 * then we need to break this up into multiple transfers. 12814 */ 12815 if (io->scsiio.kern_sg_entries == 0) { 12816 msg.dt.kern_sg_entries = 1; 12817 /* 12818 * If this is in cached memory, flush the cache 12819 * before we send the DMA request to the other 12820 * controller. We want to do this in either the 12821 * read or the write case. The read case is 12822 * straightforward. In the write case, we want to 12823 * make sure nothing is in the local cache that 12824 * could overwrite the DMAed data. 12825 */ 12826 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12827 /* 12828 * XXX KDM use bus_dmamap_sync() here. 12829 */ 12830 } 12831 12832 /* 12833 * Convert to a physical address if this is a 12834 * virtual address. 12835 */ 12836 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12837 msg.dt.sg_list[0].addr = 12838 io->scsiio.kern_data_ptr; 12839 } else { 12840 /* 12841 * XXX KDM use busdma here! 12842 */ 12843 #if 0 12844 msg.dt.sg_list[0].addr = (void *) 12845 vtophys(io->scsiio.kern_data_ptr); 12846 #endif 12847 } 12848 12849 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12850 do_sg_copy = 0; 12851 } else { 12852 struct ctl_sg_entry *sgl; 12853 12854 do_sg_copy = 1; 12855 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12856 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12857 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12858 /* 12859 * XXX KDM use bus_dmamap_sync() here. 12860 */ 12861 } 12862 } 12863 12864 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12865 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12866 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12867 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12868 msg.dt.sg_sequence = 0; 12869 12870 /* 12871 * Loop until we've sent all of the S/G entries. On the 12872 * other end, we'll recompose these S/G entries into one 12873 * contiguous list before passing it to the 12874 */ 12875 for (sg_entries_sent = 0; sg_entries_sent < 12876 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12877 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/ 12878 sizeof(msg.dt.sg_list[0])), 12879 msg.dt.kern_sg_entries - sg_entries_sent); 12880 12881 if (do_sg_copy != 0) { 12882 struct ctl_sg_entry *sgl; 12883 int j; 12884 12885 sgl = (struct ctl_sg_entry *) 12886 io->scsiio.kern_data_ptr; 12887 /* 12888 * If this is in cached memory, flush the cache 12889 * before we send the DMA request to the other 12890 * controller. We want to do this in either 12891 * the * read or the write case. The read 12892 * case is straightforward. In the write 12893 * case, we want to make sure nothing is 12894 * in the local cache that could overwrite 12895 * the DMAed data. 12896 */ 12897 12898 for (i = sg_entries_sent, j = 0; 12899 i < msg.dt.cur_sg_entries; i++, j++) { 12900 if ((io->io_hdr.flags & 12901 CTL_FLAG_NO_DATASYNC) == 0) { 12902 /* 12903 * XXX KDM use bus_dmamap_sync() 12904 */ 12905 } 12906 if ((io->io_hdr.flags & 12907 CTL_FLAG_BUS_ADDR) == 0) { 12908 /* 12909 * XXX KDM use busdma. 12910 */ 12911 #if 0 12912 msg.dt.sg_list[j].addr =(void *) 12913 vtophys(sgl[i].addr); 12914 #endif 12915 } else { 12916 msg.dt.sg_list[j].addr = 12917 sgl[i].addr; 12918 } 12919 msg.dt.sg_list[j].len = sgl[i].len; 12920 } 12921 } 12922 12923 sg_entries_sent += msg.dt.cur_sg_entries; 12924 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12925 msg.dt.sg_last = 1; 12926 else 12927 msg.dt.sg_last = 0; 12928 12929 /* 12930 * XXX KDM drop and reacquire the lock here? 12931 */ 12932 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12933 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12934 /* 12935 * XXX do something here. 12936 */ 12937 } 12938 12939 msg.dt.sent_sg_entries = sg_entries_sent; 12940 } 12941 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12942 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12943 ctl_failover_io(io, /*have_lock*/ 0); 12944 12945 } else { 12946 12947 /* 12948 * Lookup the fe_datamove() function for this particular 12949 * front end. 12950 */ 12951 fe_datamove = 12952 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12953 12954 fe_datamove(io); 12955 } 12956 } 12957 12958 static void 12959 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12960 { 12961 union ctl_ha_msg msg; 12962 int isc_status; 12963 12964 memset(&msg, 0, sizeof(msg)); 12965 12966 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12967 msg.hdr.original_sc = io; 12968 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12969 msg.hdr.nexus = io->io_hdr.nexus; 12970 msg.hdr.status = io->io_hdr.status; 12971 msg.scsi.tag_num = io->scsiio.tag_num; 12972 msg.scsi.tag_type = io->scsiio.tag_type; 12973 msg.scsi.scsi_status = io->scsiio.scsi_status; 12974 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12975 sizeof(io->scsiio.sense_data)); 12976 msg.scsi.sense_len = io->scsiio.sense_len; 12977 msg.scsi.sense_residual = io->scsiio.sense_residual; 12978 msg.scsi.fetd_status = io->io_hdr.port_status; 12979 msg.scsi.residual = io->scsiio.residual; 12980 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12981 12982 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12983 ctl_failover_io(io, /*have_lock*/ have_lock); 12984 return; 12985 } 12986 12987 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12988 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12989 /* XXX do something if this fails */ 12990 } 12991 12992 } 12993 12994 /* 12995 * The DMA to the remote side is done, now we need to tell the other side 12996 * we're done so it can continue with its data movement. 12997 */ 12998 static void 12999 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 13000 { 13001 union ctl_io *io; 13002 13003 io = rq->context; 13004 13005 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13006 printf("%s: ISC DMA write failed with error %d", __func__, 13007 rq->ret); 13008 ctl_set_internal_failure(&io->scsiio, 13009 /*sks_valid*/ 1, 13010 /*retry_count*/ rq->ret); 13011 } 13012 13013 ctl_dt_req_free(rq); 13014 13015 /* 13016 * In this case, we had to malloc the memory locally. Free it. 13017 */ 13018 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13019 int i; 13020 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13021 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13022 } 13023 /* 13024 * The data is in local and remote memory, so now we need to send 13025 * status (good or back) back to the other side. 13026 */ 13027 ctl_send_datamove_done(io, /*have_lock*/ 0); 13028 } 13029 13030 /* 13031 * We've moved the data from the host/controller into local memory. Now we 13032 * need to push it over to the remote controller's memory. 13033 */ 13034 static int 13035 ctl_datamove_remote_dm_write_cb(union ctl_io *io) 13036 { 13037 int retval; 13038 13039 retval = 0; 13040 13041 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 13042 ctl_datamove_remote_write_cb); 13043 13044 return (retval); 13045 } 13046 13047 static void 13048 ctl_datamove_remote_write(union ctl_io *io) 13049 { 13050 int retval; 13051 void (*fe_datamove)(union ctl_io *io); 13052 13053 /* 13054 * - Get the data from the host/HBA into local memory. 13055 * - DMA memory from the local controller to the remote controller. 13056 * - Send status back to the remote controller. 13057 */ 13058 13059 retval = ctl_datamove_remote_sgl_setup(io); 13060 if (retval != 0) 13061 return; 13062 13063 /* Switch the pointer over so the FETD knows what to do */ 13064 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13065 13066 /* 13067 * Use a custom move done callback, since we need to send completion 13068 * back to the other controller, not to the backend on this side. 13069 */ 13070 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 13071 13072 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13073 13074 fe_datamove(io); 13075 13076 return; 13077 13078 } 13079 13080 static int 13081 ctl_datamove_remote_dm_read_cb(union ctl_io *io) 13082 { 13083 #if 0 13084 char str[256]; 13085 char path_str[64]; 13086 struct sbuf sb; 13087 #endif 13088 13089 /* 13090 * In this case, we had to malloc the memory locally. Free it. 13091 */ 13092 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13093 int i; 13094 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13095 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13096 } 13097 13098 #if 0 13099 scsi_path_string(io, path_str, sizeof(path_str)); 13100 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13101 sbuf_cat(&sb, path_str); 13102 scsi_command_string(&io->scsiio, NULL, &sb); 13103 sbuf_printf(&sb, "\n"); 13104 sbuf_cat(&sb, path_str); 13105 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13106 io->scsiio.tag_num, io->scsiio.tag_type); 13107 sbuf_cat(&sb, path_str); 13108 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 13109 io->io_hdr.flags, io->io_hdr.status); 13110 sbuf_finish(&sb); 13111 printk("%s", sbuf_data(&sb)); 13112 #endif 13113 13114 13115 /* 13116 * The read is done, now we need to send status (good or bad) back 13117 * to the other side. 13118 */ 13119 ctl_send_datamove_done(io, /*have_lock*/ 0); 13120 13121 return (0); 13122 } 13123 13124 static void 13125 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13126 { 13127 union ctl_io *io; 13128 void (*fe_datamove)(union ctl_io *io); 13129 13130 io = rq->context; 13131 13132 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13133 printf("%s: ISC DMA read failed with error %d", __func__, 13134 rq->ret); 13135 ctl_set_internal_failure(&io->scsiio, 13136 /*sks_valid*/ 1, 13137 /*retry_count*/ rq->ret); 13138 } 13139 13140 ctl_dt_req_free(rq); 13141 13142 /* Switch the pointer over so the FETD knows what to do */ 13143 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13144 13145 /* 13146 * Use a custom move done callback, since we need to send completion 13147 * back to the other controller, not to the backend on this side. 13148 */ 13149 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13150 13151 /* XXX KDM add checks like the ones in ctl_datamove? */ 13152 13153 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13154 13155 fe_datamove(io); 13156 } 13157 13158 static int 13159 ctl_datamove_remote_sgl_setup(union ctl_io *io) 13160 { 13161 struct ctl_sg_entry *local_sglist, *remote_sglist; 13162 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13163 struct ctl_softc *softc; 13164 int retval; 13165 int i; 13166 13167 retval = 0; 13168 softc = control_softc; 13169 13170 local_sglist = io->io_hdr.local_sglist; 13171 local_dma_sglist = io->io_hdr.local_dma_sglist; 13172 remote_sglist = io->io_hdr.remote_sglist; 13173 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13174 13175 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13176 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13177 local_sglist[i].len = remote_sglist[i].len; 13178 13179 /* 13180 * XXX Detect the situation where the RS-level I/O 13181 * redirector on the other side has already read the 13182 * data off of the AOR RS on this side, and 13183 * transferred it to remote (mirror) memory on the 13184 * other side. Since we already have the data in 13185 * memory here, we just need to use it. 13186 * 13187 * XXX KDM this can probably be removed once we 13188 * get the cache device code in and take the 13189 * current AOR implementation out. 13190 */ 13191 #ifdef NEEDTOPORT 13192 if ((remote_sglist[i].addr >= 13193 (void *)vtophys(softc->mirr->addr)) 13194 && (remote_sglist[i].addr < 13195 ((void *)vtophys(softc->mirr->addr) + 13196 CacheMirrorOffset))) { 13197 local_sglist[i].addr = remote_sglist[i].addr - 13198 CacheMirrorOffset; 13199 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13200 CTL_FLAG_DATA_IN) 13201 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13202 } else { 13203 local_sglist[i].addr = remote_sglist[i].addr + 13204 CacheMirrorOffset; 13205 } 13206 #endif 13207 #if 0 13208 printf("%s: local %p, remote %p, len %d\n", 13209 __func__, local_sglist[i].addr, 13210 remote_sglist[i].addr, local_sglist[i].len); 13211 #endif 13212 } 13213 } else { 13214 uint32_t len_to_go; 13215 13216 /* 13217 * In this case, we don't have automatically allocated 13218 * memory for this I/O on this controller. This typically 13219 * happens with internal CTL I/O -- e.g. inquiry, mode 13220 * sense, etc. Anything coming from RAIDCore will have 13221 * a mirror area available. 13222 */ 13223 len_to_go = io->scsiio.kern_data_len; 13224 13225 /* 13226 * Clear the no datasync flag, we have to use malloced 13227 * buffers. 13228 */ 13229 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13230 13231 /* 13232 * The difficult thing here is that the size of the various 13233 * S/G segments may be different than the size from the 13234 * remote controller. That'll make it harder when DMAing 13235 * the data back to the other side. 13236 */ 13237 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13238 sizeof(io->io_hdr.remote_sglist[0])) && 13239 (len_to_go > 0); i++) { 13240 local_sglist[i].len = MIN(len_to_go, 131072); 13241 CTL_SIZE_8B(local_dma_sglist[i].len, 13242 local_sglist[i].len); 13243 local_sglist[i].addr = 13244 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13245 13246 local_dma_sglist[i].addr = local_sglist[i].addr; 13247 13248 if (local_sglist[i].addr == NULL) { 13249 int j; 13250 13251 printf("malloc failed for %zd bytes!", 13252 local_dma_sglist[i].len); 13253 for (j = 0; j < i; j++) { 13254 free(local_sglist[j].addr, M_CTL); 13255 } 13256 ctl_set_internal_failure(&io->scsiio, 13257 /*sks_valid*/ 1, 13258 /*retry_count*/ 4857); 13259 retval = 1; 13260 goto bailout_error; 13261 13262 } 13263 /* XXX KDM do we need a sync here? */ 13264 13265 len_to_go -= local_sglist[i].len; 13266 } 13267 /* 13268 * Reset the number of S/G entries accordingly. The 13269 * original number of S/G entries is available in 13270 * rem_sg_entries. 13271 */ 13272 io->scsiio.kern_sg_entries = i; 13273 13274 #if 0 13275 printf("%s: kern_sg_entries = %d\n", __func__, 13276 io->scsiio.kern_sg_entries); 13277 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13278 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13279 local_sglist[i].addr, local_sglist[i].len, 13280 local_dma_sglist[i].len); 13281 #endif 13282 } 13283 13284 13285 return (retval); 13286 13287 bailout_error: 13288 13289 ctl_send_datamove_done(io, /*have_lock*/ 0); 13290 13291 return (retval); 13292 } 13293 13294 static int 13295 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13296 ctl_ha_dt_cb callback) 13297 { 13298 struct ctl_ha_dt_req *rq; 13299 struct ctl_sg_entry *remote_sglist, *local_sglist; 13300 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13301 uint32_t local_used, remote_used, total_used; 13302 int retval; 13303 int i, j; 13304 13305 retval = 0; 13306 13307 rq = ctl_dt_req_alloc(); 13308 13309 /* 13310 * If we failed to allocate the request, and if the DMA didn't fail 13311 * anyway, set busy status. This is just a resource allocation 13312 * failure. 13313 */ 13314 if ((rq == NULL) 13315 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13316 ctl_set_busy(&io->scsiio); 13317 13318 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13319 13320 if (rq != NULL) 13321 ctl_dt_req_free(rq); 13322 13323 /* 13324 * The data move failed. We need to return status back 13325 * to the other controller. No point in trying to DMA 13326 * data to the remote controller. 13327 */ 13328 13329 ctl_send_datamove_done(io, /*have_lock*/ 0); 13330 13331 retval = 1; 13332 13333 goto bailout; 13334 } 13335 13336 local_sglist = io->io_hdr.local_sglist; 13337 local_dma_sglist = io->io_hdr.local_dma_sglist; 13338 remote_sglist = io->io_hdr.remote_sglist; 13339 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13340 local_used = 0; 13341 remote_used = 0; 13342 total_used = 0; 13343 13344 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13345 rq->ret = CTL_HA_STATUS_SUCCESS; 13346 rq->context = io; 13347 callback(rq); 13348 goto bailout; 13349 } 13350 13351 /* 13352 * Pull/push the data over the wire from/to the other controller. 13353 * This takes into account the possibility that the local and 13354 * remote sglists may not be identical in terms of the size of 13355 * the elements and the number of elements. 13356 * 13357 * One fundamental assumption here is that the length allocated for 13358 * both the local and remote sglists is identical. Otherwise, we've 13359 * essentially got a coding error of some sort. 13360 */ 13361 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13362 int isc_ret; 13363 uint32_t cur_len, dma_length; 13364 uint8_t *tmp_ptr; 13365 13366 rq->id = CTL_HA_DATA_CTL; 13367 rq->command = command; 13368 rq->context = io; 13369 13370 /* 13371 * Both pointers should be aligned. But it is possible 13372 * that the allocation length is not. They should both 13373 * also have enough slack left over at the end, though, 13374 * to round up to the next 8 byte boundary. 13375 */ 13376 cur_len = MIN(local_sglist[i].len - local_used, 13377 remote_sglist[j].len - remote_used); 13378 13379 /* 13380 * In this case, we have a size issue and need to decrease 13381 * the size, except in the case where we actually have less 13382 * than 8 bytes left. In that case, we need to increase 13383 * the DMA length to get the last bit. 13384 */ 13385 if ((cur_len & 0x7) != 0) { 13386 if (cur_len > 0x7) { 13387 cur_len = cur_len - (cur_len & 0x7); 13388 dma_length = cur_len; 13389 } else { 13390 CTL_SIZE_8B(dma_length, cur_len); 13391 } 13392 13393 } else 13394 dma_length = cur_len; 13395 13396 /* 13397 * If we had to allocate memory for this I/O, instead of using 13398 * the non-cached mirror memory, we'll need to flush the cache 13399 * before trying to DMA to the other controller. 13400 * 13401 * We could end up doing this multiple times for the same 13402 * segment if we have a larger local segment than remote 13403 * segment. That shouldn't be an issue. 13404 */ 13405 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13406 /* 13407 * XXX KDM use bus_dmamap_sync() here. 13408 */ 13409 } 13410 13411 rq->size = dma_length; 13412 13413 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13414 tmp_ptr += local_used; 13415 13416 /* Use physical addresses when talking to ISC hardware */ 13417 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13418 /* XXX KDM use busdma */ 13419 #if 0 13420 rq->local = vtophys(tmp_ptr); 13421 #endif 13422 } else 13423 rq->local = tmp_ptr; 13424 13425 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13426 tmp_ptr += remote_used; 13427 rq->remote = tmp_ptr; 13428 13429 rq->callback = NULL; 13430 13431 local_used += cur_len; 13432 if (local_used >= local_sglist[i].len) { 13433 i++; 13434 local_used = 0; 13435 } 13436 13437 remote_used += cur_len; 13438 if (remote_used >= remote_sglist[j].len) { 13439 j++; 13440 remote_used = 0; 13441 } 13442 total_used += cur_len; 13443 13444 if (total_used >= io->scsiio.kern_data_len) 13445 rq->callback = callback; 13446 13447 if ((rq->size & 0x7) != 0) { 13448 printf("%s: warning: size %d is not on 8b boundary\n", 13449 __func__, rq->size); 13450 } 13451 if (((uintptr_t)rq->local & 0x7) != 0) { 13452 printf("%s: warning: local %p not on 8b boundary\n", 13453 __func__, rq->local); 13454 } 13455 if (((uintptr_t)rq->remote & 0x7) != 0) { 13456 printf("%s: warning: remote %p not on 8b boundary\n", 13457 __func__, rq->local); 13458 } 13459 #if 0 13460 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13461 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13462 rq->local, rq->remote, rq->size); 13463 #endif 13464 13465 isc_ret = ctl_dt_single(rq); 13466 if (isc_ret == CTL_HA_STATUS_WAIT) 13467 continue; 13468 13469 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13470 rq->ret = CTL_HA_STATUS_SUCCESS; 13471 } else { 13472 rq->ret = isc_ret; 13473 } 13474 callback(rq); 13475 goto bailout; 13476 } 13477 13478 bailout: 13479 return (retval); 13480 13481 } 13482 13483 static void 13484 ctl_datamove_remote_read(union ctl_io *io) 13485 { 13486 int retval; 13487 int i; 13488 13489 /* 13490 * This will send an error to the other controller in the case of a 13491 * failure. 13492 */ 13493 retval = ctl_datamove_remote_sgl_setup(io); 13494 if (retval != 0) 13495 return; 13496 13497 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13498 ctl_datamove_remote_read_cb); 13499 if ((retval != 0) 13500 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13501 /* 13502 * Make sure we free memory if there was an error.. The 13503 * ctl_datamove_remote_xfer() function will send the 13504 * datamove done message, or call the callback with an 13505 * error if there is a problem. 13506 */ 13507 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13508 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13509 } 13510 13511 return; 13512 } 13513 13514 /* 13515 * Process a datamove request from the other controller. This is used for 13516 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13517 * first. Once that is complete, the data gets DMAed into the remote 13518 * controller's memory. For reads, we DMA from the remote controller's 13519 * memory into our memory first, and then move it out to the FETD. 13520 */ 13521 static void 13522 ctl_datamove_remote(union ctl_io *io) 13523 { 13524 struct ctl_softc *softc; 13525 13526 softc = control_softc; 13527 13528 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13529 13530 /* 13531 * Note that we look for an aborted I/O here, but don't do some of 13532 * the other checks that ctl_datamove() normally does. 13533 * We don't need to run the datamove delay code, since that should 13534 * have been done if need be on the other controller. 13535 */ 13536 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13537 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13538 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13539 io->io_hdr.nexus.targ_port, 13540 io->io_hdr.nexus.targ_target.id, 13541 io->io_hdr.nexus.targ_lun); 13542 io->io_hdr.port_status = 31338; 13543 ctl_send_datamove_done(io, /*have_lock*/ 0); 13544 return; 13545 } 13546 13547 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13548 ctl_datamove_remote_write(io); 13549 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13550 ctl_datamove_remote_read(io); 13551 } else { 13552 union ctl_ha_msg msg; 13553 struct scsi_sense_data *sense; 13554 uint8_t sks[3]; 13555 int retry_count; 13556 13557 memset(&msg, 0, sizeof(msg)); 13558 13559 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13560 msg.hdr.status = CTL_SCSI_ERROR; 13561 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13562 13563 retry_count = 4243; 13564 13565 sense = &msg.scsi.sense_data; 13566 sks[0] = SSD_SCS_VALID; 13567 sks[1] = (retry_count >> 8) & 0xff; 13568 sks[2] = retry_count & 0xff; 13569 13570 /* "Internal target failure" */ 13571 scsi_set_sense_data(sense, 13572 /*sense_format*/ SSD_TYPE_NONE, 13573 /*current_error*/ 1, 13574 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13575 /*asc*/ 0x44, 13576 /*ascq*/ 0x00, 13577 /*type*/ SSD_ELEM_SKS, 13578 /*size*/ sizeof(sks), 13579 /*data*/ sks, 13580 SSD_ELEM_NONE); 13581 13582 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13583 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13584 ctl_failover_io(io, /*have_lock*/ 1); 13585 return; 13586 } 13587 13588 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13589 CTL_HA_STATUS_SUCCESS) { 13590 /* XXX KDM what to do if this fails? */ 13591 } 13592 return; 13593 } 13594 13595 } 13596 13597 static int 13598 ctl_process_done(union ctl_io *io) 13599 { 13600 struct ctl_lun *lun; 13601 struct ctl_softc *softc = control_softc; 13602 void (*fe_done)(union ctl_io *io); 13603 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13604 13605 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13606 13607 fe_done = softc->ctl_ports[targ_port]->fe_done; 13608 13609 #ifdef CTL_TIME_IO 13610 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13611 char str[256]; 13612 char path_str[64]; 13613 struct sbuf sb; 13614 13615 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13616 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13617 13618 sbuf_cat(&sb, path_str); 13619 switch (io->io_hdr.io_type) { 13620 case CTL_IO_SCSI: 13621 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13622 sbuf_printf(&sb, "\n"); 13623 sbuf_cat(&sb, path_str); 13624 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13625 io->scsiio.tag_num, io->scsiio.tag_type); 13626 break; 13627 case CTL_IO_TASK: 13628 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13629 "Tag Type: %d\n", io->taskio.task_action, 13630 io->taskio.tag_num, io->taskio.tag_type); 13631 break; 13632 default: 13633 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13634 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13635 break; 13636 } 13637 sbuf_cat(&sb, path_str); 13638 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13639 (intmax_t)time_uptime - io->io_hdr.start_time); 13640 sbuf_finish(&sb); 13641 printf("%s", sbuf_data(&sb)); 13642 } 13643 #endif /* CTL_TIME_IO */ 13644 13645 switch (io->io_hdr.io_type) { 13646 case CTL_IO_SCSI: 13647 break; 13648 case CTL_IO_TASK: 13649 if (ctl_debug & CTL_DEBUG_INFO) 13650 ctl_io_error_print(io, NULL); 13651 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13652 ctl_free_io(io); 13653 else 13654 fe_done(io); 13655 return (CTL_RETVAL_COMPLETE); 13656 default: 13657 panic("ctl_process_done: invalid io type %d\n", 13658 io->io_hdr.io_type); 13659 break; /* NOTREACHED */ 13660 } 13661 13662 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13663 if (lun == NULL) { 13664 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13665 io->io_hdr.nexus.targ_mapped_lun)); 13666 goto bailout; 13667 } 13668 13669 mtx_lock(&lun->lun_lock); 13670 13671 /* 13672 * Check to see if we have any errors to inject here. We only 13673 * inject errors for commands that don't already have errors set. 13674 */ 13675 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13676 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13677 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13678 ctl_inject_error(lun, io); 13679 13680 /* 13681 * XXX KDM how do we treat commands that aren't completed 13682 * successfully? 13683 * 13684 * XXX KDM should we also track I/O latency? 13685 */ 13686 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13687 io->io_hdr.io_type == CTL_IO_SCSI) { 13688 #ifdef CTL_TIME_IO 13689 struct bintime cur_bt; 13690 #endif 13691 int type; 13692 13693 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13694 CTL_FLAG_DATA_IN) 13695 type = CTL_STATS_READ; 13696 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13697 CTL_FLAG_DATA_OUT) 13698 type = CTL_STATS_WRITE; 13699 else 13700 type = CTL_STATS_NO_IO; 13701 13702 lun->stats.ports[targ_port].bytes[type] += 13703 io->scsiio.kern_total_len; 13704 lun->stats.ports[targ_port].operations[type]++; 13705 #ifdef CTL_TIME_IO 13706 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13707 &io->io_hdr.dma_bt); 13708 lun->stats.ports[targ_port].num_dmas[type] += 13709 io->io_hdr.num_dmas; 13710 getbintime(&cur_bt); 13711 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13712 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13713 #endif 13714 } 13715 13716 /* 13717 * Remove this from the OOA queue. 13718 */ 13719 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13720 #ifdef CTL_TIME_IO 13721 if (TAILQ_EMPTY(&lun->ooa_queue)) 13722 lun->last_busy = getsbinuptime(); 13723 #endif 13724 13725 /* 13726 * Run through the blocked queue on this LUN and see if anything 13727 * has become unblocked, now that this transaction is done. 13728 */ 13729 ctl_check_blocked(lun); 13730 13731 /* 13732 * If the LUN has been invalidated, free it if there is nothing 13733 * left on its OOA queue. 13734 */ 13735 if ((lun->flags & CTL_LUN_INVALID) 13736 && TAILQ_EMPTY(&lun->ooa_queue)) { 13737 mtx_unlock(&lun->lun_lock); 13738 mtx_lock(&softc->ctl_lock); 13739 ctl_free_lun(lun); 13740 mtx_unlock(&softc->ctl_lock); 13741 } else 13742 mtx_unlock(&lun->lun_lock); 13743 13744 bailout: 13745 13746 /* 13747 * If this command has been aborted, make sure we set the status 13748 * properly. The FETD is responsible for freeing the I/O and doing 13749 * whatever it needs to do to clean up its state. 13750 */ 13751 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13752 ctl_set_task_aborted(&io->scsiio); 13753 13754 /* 13755 * If enabled, print command error status. 13756 */ 13757 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13758 (ctl_debug & CTL_DEBUG_INFO) != 0) 13759 ctl_io_error_print(io, NULL); 13760 13761 /* 13762 * Tell the FETD or the other shelf controller we're done with this 13763 * command. Note that only SCSI commands get to this point. Task 13764 * management commands are completed above. 13765 * 13766 * We only send status to the other controller if we're in XFER 13767 * mode. In SER_ONLY mode, the I/O is done on the controller that 13768 * received the I/O (from CTL's perspective), and so the status is 13769 * generated there. 13770 * 13771 * XXX KDM if we hold the lock here, we could cause a deadlock 13772 * if the frontend comes back in in this context to queue 13773 * something. 13774 */ 13775 if ((softc->ha_mode == CTL_HA_MODE_XFER) 13776 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13777 union ctl_ha_msg msg; 13778 13779 memset(&msg, 0, sizeof(msg)); 13780 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13781 msg.hdr.original_sc = io->io_hdr.original_sc; 13782 msg.hdr.nexus = io->io_hdr.nexus; 13783 msg.hdr.status = io->io_hdr.status; 13784 msg.scsi.scsi_status = io->scsiio.scsi_status; 13785 msg.scsi.tag_num = io->scsiio.tag_num; 13786 msg.scsi.tag_type = io->scsiio.tag_type; 13787 msg.scsi.sense_len = io->scsiio.sense_len; 13788 msg.scsi.sense_residual = io->scsiio.sense_residual; 13789 msg.scsi.residual = io->scsiio.residual; 13790 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13791 sizeof(io->scsiio.sense_data)); 13792 /* 13793 * We copy this whether or not this is an I/O-related 13794 * command. Otherwise, we'd have to go and check to see 13795 * whether it's a read/write command, and it really isn't 13796 * worth it. 13797 */ 13798 memcpy(&msg.scsi.lbalen, 13799 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13800 sizeof(msg.scsi.lbalen)); 13801 13802 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13803 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13804 /* XXX do something here */ 13805 } 13806 13807 ctl_free_io(io); 13808 } else 13809 fe_done(io); 13810 13811 return (CTL_RETVAL_COMPLETE); 13812 } 13813 13814 #ifdef CTL_WITH_CA 13815 /* 13816 * Front end should call this if it doesn't do autosense. When the request 13817 * sense comes back in from the initiator, we'll dequeue this and send it. 13818 */ 13819 int 13820 ctl_queue_sense(union ctl_io *io) 13821 { 13822 struct ctl_lun *lun; 13823 struct ctl_port *port; 13824 struct ctl_softc *softc; 13825 uint32_t initidx, targ_lun; 13826 13827 softc = control_softc; 13828 13829 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13830 13831 /* 13832 * LUN lookup will likely move to the ctl_work_thread() once we 13833 * have our new queueing infrastructure (that doesn't put things on 13834 * a per-LUN queue initially). That is so that we can handle 13835 * things like an INQUIRY to a LUN that we don't have enabled. We 13836 * can't deal with that right now. 13837 */ 13838 mtx_lock(&softc->ctl_lock); 13839 13840 /* 13841 * If we don't have a LUN for this, just toss the sense 13842 * information. 13843 */ 13844 port = ctl_io_port(&ctsio->io_hdr); 13845 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13846 if ((targ_lun < CTL_MAX_LUNS) 13847 && (softc->ctl_luns[targ_lun] != NULL)) 13848 lun = softc->ctl_luns[targ_lun]; 13849 else 13850 goto bailout; 13851 13852 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13853 13854 mtx_lock(&lun->lun_lock); 13855 /* 13856 * Already have CA set for this LUN...toss the sense information. 13857 */ 13858 if (ctl_is_set(lun->have_ca, initidx)) { 13859 mtx_unlock(&lun->lun_lock); 13860 goto bailout; 13861 } 13862 13863 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13864 MIN(sizeof(lun->pending_sense[initidx]), 13865 sizeof(io->scsiio.sense_data))); 13866 ctl_set_mask(lun->have_ca, initidx); 13867 mtx_unlock(&lun->lun_lock); 13868 13869 bailout: 13870 mtx_unlock(&softc->ctl_lock); 13871 13872 ctl_free_io(io); 13873 13874 return (CTL_RETVAL_COMPLETE); 13875 } 13876 #endif 13877 13878 /* 13879 * Primary command inlet from frontend ports. All SCSI and task I/O 13880 * requests must go through this function. 13881 */ 13882 int 13883 ctl_queue(union ctl_io *io) 13884 { 13885 struct ctl_port *port; 13886 13887 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13888 13889 #ifdef CTL_TIME_IO 13890 io->io_hdr.start_time = time_uptime; 13891 getbintime(&io->io_hdr.start_bt); 13892 #endif /* CTL_TIME_IO */ 13893 13894 /* Map FE-specific LUN ID into global one. */ 13895 port = ctl_io_port(&io->io_hdr); 13896 io->io_hdr.nexus.targ_mapped_lun = 13897 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13898 13899 switch (io->io_hdr.io_type) { 13900 case CTL_IO_SCSI: 13901 case CTL_IO_TASK: 13902 if (ctl_debug & CTL_DEBUG_CDB) 13903 ctl_io_print(io); 13904 ctl_enqueue_incoming(io); 13905 break; 13906 default: 13907 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13908 return (EINVAL); 13909 } 13910 13911 return (CTL_RETVAL_COMPLETE); 13912 } 13913 13914 #ifdef CTL_IO_DELAY 13915 static void 13916 ctl_done_timer_wakeup(void *arg) 13917 { 13918 union ctl_io *io; 13919 13920 io = (union ctl_io *)arg; 13921 ctl_done(io); 13922 } 13923 #endif /* CTL_IO_DELAY */ 13924 13925 void 13926 ctl_done(union ctl_io *io) 13927 { 13928 13929 /* 13930 * Enable this to catch duplicate completion issues. 13931 */ 13932 #if 0 13933 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13934 printf("%s: type %d msg %d cdb %x iptl: " 13935 "%d:%d:%d:%d tag 0x%04x " 13936 "flag %#x status %x\n", 13937 __func__, 13938 io->io_hdr.io_type, 13939 io->io_hdr.msg_type, 13940 io->scsiio.cdb[0], 13941 io->io_hdr.nexus.initid.id, 13942 io->io_hdr.nexus.targ_port, 13943 io->io_hdr.nexus.targ_target.id, 13944 io->io_hdr.nexus.targ_lun, 13945 (io->io_hdr.io_type == 13946 CTL_IO_TASK) ? 13947 io->taskio.tag_num : 13948 io->scsiio.tag_num, 13949 io->io_hdr.flags, 13950 io->io_hdr.status); 13951 } else 13952 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13953 #endif 13954 13955 /* 13956 * This is an internal copy of an I/O, and should not go through 13957 * the normal done processing logic. 13958 */ 13959 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13960 return; 13961 13962 /* 13963 * We need to send a msg to the serializing shelf to finish the IO 13964 * as well. We don't send a finish message to the other shelf if 13965 * this is a task management command. Task management commands 13966 * aren't serialized in the OOA queue, but rather just executed on 13967 * both shelf controllers for commands that originated on that 13968 * controller. 13969 */ 13970 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13971 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13972 union ctl_ha_msg msg_io; 13973 13974 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13975 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13976 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13977 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13978 } 13979 /* continue on to finish IO */ 13980 } 13981 #ifdef CTL_IO_DELAY 13982 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13983 struct ctl_lun *lun; 13984 13985 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13986 13987 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13988 } else { 13989 struct ctl_lun *lun; 13990 13991 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13992 13993 if ((lun != NULL) 13994 && (lun->delay_info.done_delay > 0)) { 13995 struct callout *callout; 13996 13997 callout = (struct callout *)&io->io_hdr.timer_bytes; 13998 callout_init(callout, /*mpsafe*/ 1); 13999 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 14000 callout_reset(callout, 14001 lun->delay_info.done_delay * hz, 14002 ctl_done_timer_wakeup, io); 14003 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 14004 lun->delay_info.done_delay = 0; 14005 return; 14006 } 14007 } 14008 #endif /* CTL_IO_DELAY */ 14009 14010 ctl_enqueue_done(io); 14011 } 14012 14013 int 14014 ctl_isc(struct ctl_scsiio *ctsio) 14015 { 14016 struct ctl_lun *lun; 14017 int retval; 14018 14019 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14020 14021 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 14022 14023 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 14024 14025 retval = lun->backend->data_submit((union ctl_io *)ctsio); 14026 14027 return (retval); 14028 } 14029 14030 14031 static void 14032 ctl_work_thread(void *arg) 14033 { 14034 struct ctl_thread *thr = (struct ctl_thread *)arg; 14035 struct ctl_softc *softc = thr->ctl_softc; 14036 union ctl_io *io; 14037 int retval; 14038 14039 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 14040 14041 for (;;) { 14042 retval = 0; 14043 14044 /* 14045 * We handle the queues in this order: 14046 * - ISC 14047 * - done queue (to free up resources, unblock other commands) 14048 * - RtR queue 14049 * - incoming queue 14050 * 14051 * If those queues are empty, we break out of the loop and 14052 * go to sleep. 14053 */ 14054 mtx_lock(&thr->queue_lock); 14055 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 14056 if (io != NULL) { 14057 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 14058 mtx_unlock(&thr->queue_lock); 14059 ctl_handle_isc(io); 14060 continue; 14061 } 14062 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 14063 if (io != NULL) { 14064 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 14065 /* clear any blocked commands, call fe_done */ 14066 mtx_unlock(&thr->queue_lock); 14067 retval = ctl_process_done(io); 14068 continue; 14069 } 14070 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 14071 if (io != NULL) { 14072 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 14073 mtx_unlock(&thr->queue_lock); 14074 if (io->io_hdr.io_type == CTL_IO_TASK) 14075 ctl_run_task(io); 14076 else 14077 ctl_scsiio_precheck(softc, &io->scsiio); 14078 continue; 14079 } 14080 if (!ctl_pause_rtr) { 14081 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 14082 if (io != NULL) { 14083 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14084 mtx_unlock(&thr->queue_lock); 14085 retval = ctl_scsiio(&io->scsiio); 14086 if (retval != CTL_RETVAL_COMPLETE) 14087 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14088 continue; 14089 } 14090 } 14091 14092 /* Sleep until we have something to do. */ 14093 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 14094 } 14095 } 14096 14097 static void 14098 ctl_lun_thread(void *arg) 14099 { 14100 struct ctl_softc *softc = (struct ctl_softc *)arg; 14101 struct ctl_be_lun *be_lun; 14102 int retval; 14103 14104 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14105 14106 for (;;) { 14107 retval = 0; 14108 mtx_lock(&softc->ctl_lock); 14109 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14110 if (be_lun != NULL) { 14111 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14112 mtx_unlock(&softc->ctl_lock); 14113 ctl_create_lun(be_lun); 14114 continue; 14115 } 14116 14117 /* Sleep until we have something to do. */ 14118 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14119 PDROP | PRIBIO, "-", 0); 14120 } 14121 } 14122 14123 static void 14124 ctl_thresh_thread(void *arg) 14125 { 14126 struct ctl_softc *softc = (struct ctl_softc *)arg; 14127 struct ctl_lun *lun; 14128 struct ctl_be_lun *be_lun; 14129 struct scsi_da_rw_recovery_page *rwpage; 14130 struct ctl_logical_block_provisioning_page *page; 14131 const char *attr; 14132 uint64_t thres, val; 14133 int i, e; 14134 14135 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 14136 14137 for (;;) { 14138 mtx_lock(&softc->ctl_lock); 14139 STAILQ_FOREACH(lun, &softc->lun_list, links) { 14140 be_lun = lun->be_lun; 14141 if ((lun->flags & CTL_LUN_DISABLED) || 14142 (lun->flags & CTL_LUN_OFFLINE) || 14143 lun->backend->lun_attr == NULL) 14144 continue; 14145 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 14146 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 14147 continue; 14148 e = 0; 14149 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 14150 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 14151 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 14152 continue; 14153 thres = scsi_4btoul(page->descr[i].count); 14154 thres <<= CTL_LBP_EXPONENT; 14155 switch (page->descr[i].resource) { 14156 case 0x01: 14157 attr = "blocksavail"; 14158 break; 14159 case 0x02: 14160 attr = "blocksused"; 14161 break; 14162 case 0xf1: 14163 attr = "poolblocksavail"; 14164 break; 14165 case 0xf2: 14166 attr = "poolblocksused"; 14167 break; 14168 default: 14169 continue; 14170 } 14171 mtx_unlock(&softc->ctl_lock); // XXX 14172 val = lun->backend->lun_attr( 14173 lun->be_lun->be_lun, attr); 14174 mtx_lock(&softc->ctl_lock); 14175 if (val == UINT64_MAX) 14176 continue; 14177 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 14178 == SLBPPD_ARMING_INC) 14179 e |= (val >= thres); 14180 else 14181 e |= (val <= thres); 14182 } 14183 mtx_lock(&lun->lun_lock); 14184 if (e) { 14185 if (lun->lasttpt == 0 || 14186 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 14187 lun->lasttpt = time_uptime; 14188 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14189 } 14190 } else { 14191 lun->lasttpt = 0; 14192 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14193 } 14194 mtx_unlock(&lun->lun_lock); 14195 } 14196 mtx_unlock(&softc->ctl_lock); 14197 pause("-", CTL_LBP_PERIOD * hz); 14198 } 14199 } 14200 14201 static void 14202 ctl_enqueue_incoming(union ctl_io *io) 14203 { 14204 struct ctl_softc *softc = control_softc; 14205 struct ctl_thread *thr; 14206 u_int idx; 14207 14208 idx = (io->io_hdr.nexus.targ_port * 127 + 14209 io->io_hdr.nexus.initid.id) % worker_threads; 14210 thr = &softc->threads[idx]; 14211 mtx_lock(&thr->queue_lock); 14212 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14213 mtx_unlock(&thr->queue_lock); 14214 wakeup(thr); 14215 } 14216 14217 static void 14218 ctl_enqueue_rtr(union ctl_io *io) 14219 { 14220 struct ctl_softc *softc = control_softc; 14221 struct ctl_thread *thr; 14222 14223 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14224 mtx_lock(&thr->queue_lock); 14225 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14226 mtx_unlock(&thr->queue_lock); 14227 wakeup(thr); 14228 } 14229 14230 static void 14231 ctl_enqueue_done(union ctl_io *io) 14232 { 14233 struct ctl_softc *softc = control_softc; 14234 struct ctl_thread *thr; 14235 14236 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14237 mtx_lock(&thr->queue_lock); 14238 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14239 mtx_unlock(&thr->queue_lock); 14240 wakeup(thr); 14241 } 14242 14243 #ifdef notyet 14244 static void 14245 ctl_enqueue_isc(union ctl_io *io) 14246 { 14247 struct ctl_softc *softc = control_softc; 14248 struct ctl_thread *thr; 14249 14250 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14251 mtx_lock(&thr->queue_lock); 14252 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14253 mtx_unlock(&thr->queue_lock); 14254 wakeup(thr); 14255 } 14256 14257 /* Initialization and failover */ 14258 14259 void 14260 ctl_init_isc_msg(void) 14261 { 14262 printf("CTL: Still calling this thing\n"); 14263 } 14264 14265 /* 14266 * Init component 14267 * Initializes component into configuration defined by bootMode 14268 * (see hasc-sv.c) 14269 * returns hasc_Status: 14270 * OK 14271 * ERROR - fatal error 14272 */ 14273 static ctl_ha_comp_status 14274 ctl_isc_init(struct ctl_ha_component *c) 14275 { 14276 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14277 14278 c->status = ret; 14279 return ret; 14280 } 14281 14282 /* Start component 14283 * Starts component in state requested. If component starts successfully, 14284 * it must set its own state to the requestrd state 14285 * When requested state is HASC_STATE_HA, the component may refine it 14286 * by adding _SLAVE or _MASTER flags. 14287 * Currently allowed state transitions are: 14288 * UNKNOWN->HA - initial startup 14289 * UNKNOWN->SINGLE - initial startup when no parter detected 14290 * HA->SINGLE - failover 14291 * returns ctl_ha_comp_status: 14292 * OK - component successfully started in requested state 14293 * FAILED - could not start the requested state, failover may 14294 * be possible 14295 * ERROR - fatal error detected, no future startup possible 14296 */ 14297 static ctl_ha_comp_status 14298 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14299 { 14300 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14301 14302 printf("%s: go\n", __func__); 14303 14304 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14305 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14306 control_softc->is_single = 0; 14307 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14308 != CTL_HA_STATUS_SUCCESS) { 14309 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14310 ret = CTL_HA_COMP_STATUS_ERROR; 14311 } 14312 } else if (CTL_HA_STATE_IS_HA(c->state) 14313 && CTL_HA_STATE_IS_SINGLE(state)){ 14314 // HA->SINGLE transition 14315 ctl_failover(); 14316 control_softc->is_single = 1; 14317 } else { 14318 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14319 c->state, state); 14320 ret = CTL_HA_COMP_STATUS_ERROR; 14321 } 14322 if (CTL_HA_STATE_IS_SINGLE(state)) 14323 control_softc->is_single = 1; 14324 14325 c->state = state; 14326 c->status = ret; 14327 return ret; 14328 } 14329 14330 /* 14331 * Quiesce component 14332 * The component must clear any error conditions (set status to OK) and 14333 * prepare itself to another Start call 14334 * returns ctl_ha_comp_status: 14335 * OK 14336 * ERROR 14337 */ 14338 static ctl_ha_comp_status 14339 ctl_isc_quiesce(struct ctl_ha_component *c) 14340 { 14341 int ret = CTL_HA_COMP_STATUS_OK; 14342 14343 ctl_pause_rtr = 1; 14344 c->status = ret; 14345 return ret; 14346 } 14347 14348 struct ctl_ha_component ctl_ha_component_ctlisc = 14349 { 14350 .name = "CTL ISC", 14351 .state = CTL_HA_STATE_UNKNOWN, 14352 .init = ctl_isc_init, 14353 .start = ctl_isc_start, 14354 .quiesce = ctl_isc_quiesce 14355 }; 14356 #endif 14357 14358 /* 14359 * vim: ts=8 14360 */ 14361