1 /*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Edward Tomasz Napierala 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions, and the following disclaimer, 15 * without modification. 16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17 * substantially similar to the "NO WARRANTY" disclaimer below 18 * ("Disclaimer") and any redistribution must be conditioned upon 19 * including a substantially similar Disclaimer requirement for further 20 * binary redistribution. 21 * 22 * NO WARRANTY 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGES. 34 * 35 * $Id$ 36 */ 37 /* 38 * CAM Target Layer, a SCSI device emulation subsystem. 39 * 40 * Author: Ken Merry <ken@FreeBSD.org> 41 */ 42 43 #define _CTL_C 44 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/ctype.h> 51 #include <sys/kernel.h> 52 #include <sys/types.h> 53 #include <sys/kthread.h> 54 #include <sys/bio.h> 55 #include <sys/fcntl.h> 56 #include <sys/lock.h> 57 #include <sys/module.h> 58 #include <sys/mutex.h> 59 #include <sys/condvar.h> 60 #include <sys/malloc.h> 61 #include <sys/conf.h> 62 #include <sys/ioccom.h> 63 #include <sys/queue.h> 64 #include <sys/sbuf.h> 65 #include <sys/smp.h> 66 #include <sys/endian.h> 67 #include <sys/sysctl.h> 68 #include <vm/uma.h> 69 70 #include <cam/cam.h> 71 #include <cam/scsi/scsi_all.h> 72 #include <cam/scsi/scsi_cd.h> 73 #include <cam/scsi/scsi_da.h> 74 #include <cam/ctl/ctl_io.h> 75 #include <cam/ctl/ctl.h> 76 #include <cam/ctl/ctl_frontend.h> 77 #include <cam/ctl/ctl_util.h> 78 #include <cam/ctl/ctl_backend.h> 79 #include <cam/ctl/ctl_ioctl.h> 80 #include <cam/ctl/ctl_ha.h> 81 #include <cam/ctl/ctl_private.h> 82 #include <cam/ctl/ctl_debug.h> 83 #include <cam/ctl/ctl_scsi_all.h> 84 #include <cam/ctl/ctl_error.h> 85 86 struct ctl_softc *control_softc = NULL; 87 88 /* 89 * Template mode pages. 90 */ 91 92 /* 93 * Note that these are default values only. The actual values will be 94 * filled in when the user does a mode sense. 95 */ 96 const static struct copan_debugconf_subpage debugconf_page_default = { 97 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 98 DBGCNF_SUBPAGE_CODE, /* subpage */ 99 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 100 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 101 DBGCNF_VERSION, /* page_version */ 102 {CTL_TIME_IO_DEFAULT_SECS>>8, 103 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 104 }; 105 106 const static struct copan_debugconf_subpage debugconf_page_changeable = { 107 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 108 DBGCNF_SUBPAGE_CODE, /* subpage */ 109 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 110 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 111 0, /* page_version */ 112 {0xff,0xff}, /* ctl_time_io_secs */ 113 }; 114 115 const static struct scsi_da_rw_recovery_page rw_er_page_default = { 116 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 117 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 118 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 119 /*read_retry_count*/0, 120 /*correction_span*/0, 121 /*head_offset_count*/0, 122 /*data_strobe_offset_cnt*/0, 123 /*byte8*/SMS_RWER_LBPERE, 124 /*write_retry_count*/0, 125 /*reserved2*/0, 126 /*recovery_time_limit*/{0, 0}, 127 }; 128 129 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 130 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 131 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 132 /*byte3*/0, 133 /*read_retry_count*/0, 134 /*correction_span*/0, 135 /*head_offset_count*/0, 136 /*data_strobe_offset_cnt*/0, 137 /*byte8*/0, 138 /*write_retry_count*/0, 139 /*reserved2*/0, 140 /*recovery_time_limit*/{0, 0}, 141 }; 142 143 const static struct scsi_format_page format_page_default = { 144 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 145 /*page_length*/sizeof(struct scsi_format_page) - 2, 146 /*tracks_per_zone*/ {0, 0}, 147 /*alt_sectors_per_zone*/ {0, 0}, 148 /*alt_tracks_per_zone*/ {0, 0}, 149 /*alt_tracks_per_lun*/ {0, 0}, 150 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 151 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 152 /*bytes_per_sector*/ {0, 0}, 153 /*interleave*/ {0, 0}, 154 /*track_skew*/ {0, 0}, 155 /*cylinder_skew*/ {0, 0}, 156 /*flags*/ SFP_HSEC, 157 /*reserved*/ {0, 0, 0} 158 }; 159 160 const static struct scsi_format_page format_page_changeable = { 161 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 162 /*page_length*/sizeof(struct scsi_format_page) - 2, 163 /*tracks_per_zone*/ {0, 0}, 164 /*alt_sectors_per_zone*/ {0, 0}, 165 /*alt_tracks_per_zone*/ {0, 0}, 166 /*alt_tracks_per_lun*/ {0, 0}, 167 /*sectors_per_track*/ {0, 0}, 168 /*bytes_per_sector*/ {0, 0}, 169 /*interleave*/ {0, 0}, 170 /*track_skew*/ {0, 0}, 171 /*cylinder_skew*/ {0, 0}, 172 /*flags*/ 0, 173 /*reserved*/ {0, 0, 0} 174 }; 175 176 const static struct scsi_rigid_disk_page rigid_disk_page_default = { 177 /*page_code*/SMS_RIGID_DISK_PAGE, 178 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 179 /*cylinders*/ {0, 0, 0}, 180 /*heads*/ CTL_DEFAULT_HEADS, 181 /*start_write_precomp*/ {0, 0, 0}, 182 /*start_reduced_current*/ {0, 0, 0}, 183 /*step_rate*/ {0, 0}, 184 /*landing_zone_cylinder*/ {0, 0, 0}, 185 /*rpl*/ SRDP_RPL_DISABLED, 186 /*rotational_offset*/ 0, 187 /*reserved1*/ 0, 188 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 189 CTL_DEFAULT_ROTATION_RATE & 0xff}, 190 /*reserved2*/ {0, 0} 191 }; 192 193 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 194 /*page_code*/SMS_RIGID_DISK_PAGE, 195 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 196 /*cylinders*/ {0, 0, 0}, 197 /*heads*/ 0, 198 /*start_write_precomp*/ {0, 0, 0}, 199 /*start_reduced_current*/ {0, 0, 0}, 200 /*step_rate*/ {0, 0}, 201 /*landing_zone_cylinder*/ {0, 0, 0}, 202 /*rpl*/ 0, 203 /*rotational_offset*/ 0, 204 /*reserved1*/ 0, 205 /*rotation_rate*/ {0, 0}, 206 /*reserved2*/ {0, 0} 207 }; 208 209 const static struct scsi_caching_page caching_page_default = { 210 /*page_code*/SMS_CACHING_PAGE, 211 /*page_length*/sizeof(struct scsi_caching_page) - 2, 212 /*flags1*/ SCP_DISC | SCP_WCE, 213 /*ret_priority*/ 0, 214 /*disable_pf_transfer_len*/ {0xff, 0xff}, 215 /*min_prefetch*/ {0, 0}, 216 /*max_prefetch*/ {0xff, 0xff}, 217 /*max_pf_ceiling*/ {0xff, 0xff}, 218 /*flags2*/ 0, 219 /*cache_segments*/ 0, 220 /*cache_seg_size*/ {0, 0}, 221 /*reserved*/ 0, 222 /*non_cache_seg_size*/ {0, 0, 0} 223 }; 224 225 const static struct scsi_caching_page caching_page_changeable = { 226 /*page_code*/SMS_CACHING_PAGE, 227 /*page_length*/sizeof(struct scsi_caching_page) - 2, 228 /*flags1*/ SCP_WCE | SCP_RCD, 229 /*ret_priority*/ 0, 230 /*disable_pf_transfer_len*/ {0, 0}, 231 /*min_prefetch*/ {0, 0}, 232 /*max_prefetch*/ {0, 0}, 233 /*max_pf_ceiling*/ {0, 0}, 234 /*flags2*/ 0, 235 /*cache_segments*/ 0, 236 /*cache_seg_size*/ {0, 0}, 237 /*reserved*/ 0, 238 /*non_cache_seg_size*/ {0, 0, 0} 239 }; 240 241 const static struct scsi_control_page control_page_default = { 242 /*page_code*/SMS_CONTROL_MODE_PAGE, 243 /*page_length*/sizeof(struct scsi_control_page) - 2, 244 /*rlec*/0, 245 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 246 /*eca_and_aen*/0, 247 /*flags4*/SCP_TAS, 248 /*aen_holdoff_period*/{0, 0}, 249 /*busy_timeout_period*/{0, 0}, 250 /*extended_selftest_completion_time*/{0, 0} 251 }; 252 253 const static struct scsi_control_page control_page_changeable = { 254 /*page_code*/SMS_CONTROL_MODE_PAGE, 255 /*page_length*/sizeof(struct scsi_control_page) - 2, 256 /*rlec*/SCP_DSENSE, 257 /*queue_flags*/SCP_QUEUE_ALG_MASK, 258 /*eca_and_aen*/SCP_SWP, 259 /*flags4*/0, 260 /*aen_holdoff_period*/{0, 0}, 261 /*busy_timeout_period*/{0, 0}, 262 /*extended_selftest_completion_time*/{0, 0} 263 }; 264 265 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4) 266 267 const static struct scsi_control_ext_page control_ext_page_default = { 268 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 269 /*subpage_code*/0x01, 270 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 271 /*flags*/0, 272 /*prio*/0, 273 /*max_sense*/0 274 }; 275 276 const static struct scsi_control_ext_page control_ext_page_changeable = { 277 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 278 /*subpage_code*/0x01, 279 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 280 /*flags*/0, 281 /*prio*/0, 282 /*max_sense*/0 283 }; 284 285 const static struct scsi_info_exceptions_page ie_page_default = { 286 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 287 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 288 /*info_flags*/SIEP_FLAGS_DEXCPT, 289 /*mrie*/0, 290 /*interval_timer*/{0, 0, 0, 0}, 291 /*report_count*/{0, 0, 0, 0} 292 }; 293 294 const static struct scsi_info_exceptions_page ie_page_changeable = { 295 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 296 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 297 /*info_flags*/0, 298 /*mrie*/0, 299 /*interval_timer*/{0, 0, 0, 0}, 300 /*report_count*/{0, 0, 0, 0} 301 }; 302 303 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 304 305 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 307 /*subpage_code*/0x02, 308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 309 /*flags*/0, 310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 311 /*descr*/{}}, 312 {{/*flags*/0, 313 /*resource*/0x01, 314 /*reserved*/{0, 0}, 315 /*count*/{0, 0, 0, 0}}, 316 {/*flags*/0, 317 /*resource*/0x02, 318 /*reserved*/{0, 0}, 319 /*count*/{0, 0, 0, 0}}, 320 {/*flags*/0, 321 /*resource*/0xf1, 322 /*reserved*/{0, 0}, 323 /*count*/{0, 0, 0, 0}}, 324 {/*flags*/0, 325 /*resource*/0xf2, 326 /*reserved*/{0, 0}, 327 /*count*/{0, 0, 0, 0}} 328 } 329 }; 330 331 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 333 /*subpage_code*/0x02, 334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 335 /*flags*/0, 336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 337 /*descr*/{}}, 338 {{/*flags*/0, 339 /*resource*/0, 340 /*reserved*/{0, 0}, 341 /*count*/{0, 0, 0, 0}}, 342 {/*flags*/0, 343 /*resource*/0, 344 /*reserved*/{0, 0}, 345 /*count*/{0, 0, 0, 0}}, 346 {/*flags*/0, 347 /*resource*/0, 348 /*reserved*/{0, 0}, 349 /*count*/{0, 0, 0, 0}}, 350 {/*flags*/0, 351 /*resource*/0, 352 /*reserved*/{0, 0}, 353 /*count*/{0, 0, 0, 0}} 354 } 355 }; 356 357 const static struct scsi_cddvd_capabilities_page cddvd_page_default = { 358 /*page_code*/SMS_CDDVD_CAPS_PAGE, 359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 360 /*caps1*/0x3f, 361 /*caps2*/0x00, 362 /*caps3*/0xf0, 363 /*caps4*/0x00, 364 /*caps5*/0x29, 365 /*caps6*/0x00, 366 /*obsolete*/{0, 0}, 367 /*nvol_levels*/{0, 0}, 368 /*buffer_size*/{8, 0}, 369 /*obsolete2*/{0, 0}, 370 /*reserved*/0, 371 /*digital*/0, 372 /*obsolete3*/0, 373 /*copy_management*/0, 374 /*reserved2*/0, 375 /*rotation_control*/0, 376 /*cur_write_speed*/0, 377 /*num_speed_descr*/0, 378 }; 379 380 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = { 381 /*page_code*/SMS_CDDVD_CAPS_PAGE, 382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 383 /*caps1*/0, 384 /*caps2*/0, 385 /*caps3*/0, 386 /*caps4*/0, 387 /*caps5*/0, 388 /*caps6*/0, 389 /*obsolete*/{0, 0}, 390 /*nvol_levels*/{0, 0}, 391 /*buffer_size*/{0, 0}, 392 /*obsolete2*/{0, 0}, 393 /*reserved*/0, 394 /*digital*/0, 395 /*obsolete3*/0, 396 /*copy_management*/0, 397 /*reserved2*/0, 398 /*rotation_control*/0, 399 /*cur_write_speed*/0, 400 /*num_speed_descr*/0, 401 }; 402 403 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 404 static int worker_threads = -1; 405 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 406 &worker_threads, 1, "Number of worker threads"); 407 static int ctl_debug = CTL_DEBUG_NONE; 408 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 409 &ctl_debug, 0, "Enabled debug flags"); 410 411 /* 412 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 413 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 414 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 415 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 416 */ 417 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 418 419 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 420 int param); 421 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 422 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 423 static int ctl_init(void); 424 void ctl_shutdown(void); 425 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 426 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 427 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 428 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 429 struct ctl_ooa *ooa_hdr, 430 struct ctl_ooa_entry *kern_entries); 431 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 432 struct thread *td); 433 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 434 struct ctl_be_lun *be_lun); 435 static int ctl_free_lun(struct ctl_lun *lun); 436 static void ctl_create_lun(struct ctl_be_lun *be_lun); 437 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 438 439 static int ctl_do_mode_select(union ctl_io *io); 440 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 441 uint64_t res_key, uint64_t sa_res_key, 442 uint8_t type, uint32_t residx, 443 struct ctl_scsiio *ctsio, 444 struct scsi_per_res_out *cdb, 445 struct scsi_per_res_out_parms* param); 446 static void ctl_pro_preempt_other(struct ctl_lun *lun, 447 union ctl_ha_msg *msg); 448 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 449 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 450 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 451 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 452 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 453 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 454 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 455 int alloc_len); 456 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 457 int alloc_len); 458 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 459 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 460 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 461 static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 462 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 463 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 464 bool seq); 465 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 466 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 467 union ctl_io *pending_io, union ctl_io *ooa_io); 468 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 469 union ctl_io *starting_io); 470 static int ctl_check_blocked(struct ctl_lun *lun); 471 static int ctl_scsiio_lun_check(struct ctl_lun *lun, 472 const struct ctl_cmd_entry *entry, 473 struct ctl_scsiio *ctsio); 474 static void ctl_failover_lun(union ctl_io *io); 475 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 476 struct ctl_scsiio *ctsio); 477 static int ctl_scsiio(struct ctl_scsiio *ctsio); 478 479 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 480 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 481 ctl_ua_type ua_type); 482 static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, 483 ctl_ua_type ua_type); 484 static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 485 static int ctl_abort_task(union ctl_io *io); 486 static int ctl_abort_task_set(union ctl_io *io); 487 static int ctl_query_task(union ctl_io *io, int task_set); 488 static int ctl_i_t_nexus_reset(union ctl_io *io); 489 static int ctl_query_async_event(union ctl_io *io); 490 static void ctl_run_task(union ctl_io *io); 491 #ifdef CTL_IO_DELAY 492 static void ctl_datamove_timer_wakeup(void *arg); 493 static void ctl_done_timer_wakeup(void *arg); 494 #endif /* CTL_IO_DELAY */ 495 496 static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 497 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 498 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 499 static void ctl_datamove_remote_write(union ctl_io *io); 500 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 501 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 502 static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 503 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 504 ctl_ha_dt_cb callback); 505 static void ctl_datamove_remote_read(union ctl_io *io); 506 static void ctl_datamove_remote(union ctl_io *io); 507 static void ctl_process_done(union ctl_io *io); 508 static void ctl_lun_thread(void *arg); 509 static void ctl_thresh_thread(void *arg); 510 static void ctl_work_thread(void *arg); 511 static void ctl_enqueue_incoming(union ctl_io *io); 512 static void ctl_enqueue_rtr(union ctl_io *io); 513 static void ctl_enqueue_done(union ctl_io *io); 514 static void ctl_enqueue_isc(union ctl_io *io); 515 static const struct ctl_cmd_entry * 516 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 517 static const struct ctl_cmd_entry * 518 ctl_validate_command(struct ctl_scsiio *ctsio); 519 static int ctl_cmd_applicable(uint8_t lun_type, 520 const struct ctl_cmd_entry *entry); 521 522 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 523 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 524 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 525 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 526 527 /* 528 * Load the serialization table. This isn't very pretty, but is probably 529 * the easiest way to do it. 530 */ 531 #include "ctl_ser_table.c" 532 533 /* 534 * We only need to define open, close and ioctl routines for this driver. 535 */ 536 static struct cdevsw ctl_cdevsw = { 537 .d_version = D_VERSION, 538 .d_flags = 0, 539 .d_open = ctl_open, 540 .d_close = ctl_close, 541 .d_ioctl = ctl_ioctl, 542 .d_name = "ctl", 543 }; 544 545 546 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 547 548 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 549 550 static moduledata_t ctl_moduledata = { 551 "ctl", 552 ctl_module_event_handler, 553 NULL 554 }; 555 556 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 557 MODULE_VERSION(ctl, 1); 558 559 static struct ctl_frontend ha_frontend = 560 { 561 .name = "ha", 562 }; 563 564 static void 565 ctl_ha_datamove(union ctl_io *io) 566 { 567 struct ctl_lun *lun; 568 struct ctl_sg_entry *sgl; 569 union ctl_ha_msg msg; 570 uint32_t sg_entries_sent; 571 int do_sg_copy, i, j; 572 573 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 574 memset(&msg.dt, 0, sizeof(msg.dt)); 575 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 576 msg.hdr.original_sc = io->io_hdr.original_sc; 577 msg.hdr.serializing_sc = io; 578 msg.hdr.nexus = io->io_hdr.nexus; 579 msg.hdr.status = io->io_hdr.status; 580 msg.dt.flags = io->io_hdr.flags; 581 582 /* 583 * We convert everything into a S/G list here. We can't 584 * pass by reference, only by value between controllers. 585 * So we can't pass a pointer to the S/G list, only as many 586 * S/G entries as we can fit in here. If it's possible for 587 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 588 * then we need to break this up into multiple transfers. 589 */ 590 if (io->scsiio.kern_sg_entries == 0) { 591 msg.dt.kern_sg_entries = 1; 592 #if 0 593 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 594 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 595 } else { 596 /* XXX KDM use busdma here! */ 597 msg.dt.sg_list[0].addr = 598 (void *)vtophys(io->scsiio.kern_data_ptr); 599 } 600 #else 601 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 602 ("HA does not support BUS_ADDR")); 603 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 604 #endif 605 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 606 do_sg_copy = 0; 607 } else { 608 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 609 do_sg_copy = 1; 610 } 611 612 msg.dt.kern_data_len = io->scsiio.kern_data_len; 613 msg.dt.kern_total_len = io->scsiio.kern_total_len; 614 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 615 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 616 msg.dt.sg_sequence = 0; 617 618 /* 619 * Loop until we've sent all of the S/G entries. On the 620 * other end, we'll recompose these S/G entries into one 621 * contiguous list before processing. 622 */ 623 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 624 msg.dt.sg_sequence++) { 625 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 626 sizeof(msg.dt.sg_list[0])), 627 msg.dt.kern_sg_entries - sg_entries_sent); 628 if (do_sg_copy != 0) { 629 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 630 for (i = sg_entries_sent, j = 0; 631 i < msg.dt.cur_sg_entries; i++, j++) { 632 #if 0 633 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 634 msg.dt.sg_list[j].addr = sgl[i].addr; 635 } else { 636 /* XXX KDM use busdma here! */ 637 msg.dt.sg_list[j].addr = 638 (void *)vtophys(sgl[i].addr); 639 } 640 #else 641 KASSERT((io->io_hdr.flags & 642 CTL_FLAG_BUS_ADDR) == 0, 643 ("HA does not support BUS_ADDR")); 644 msg.dt.sg_list[j].addr = sgl[i].addr; 645 #endif 646 msg.dt.sg_list[j].len = sgl[i].len; 647 } 648 } 649 650 sg_entries_sent += msg.dt.cur_sg_entries; 651 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 652 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 653 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 654 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 655 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 656 io->io_hdr.port_status = 31341; 657 io->scsiio.be_move_done(io); 658 return; 659 } 660 msg.dt.sent_sg_entries = sg_entries_sent; 661 } 662 663 /* 664 * Officially handover the request from us to peer. 665 * If failover has just happened, then we must return error. 666 * If failover happen just after, then it is not our problem. 667 */ 668 if (lun) 669 mtx_lock(&lun->lun_lock); 670 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 671 if (lun) 672 mtx_unlock(&lun->lun_lock); 673 io->io_hdr.port_status = 31342; 674 io->scsiio.be_move_done(io); 675 return; 676 } 677 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 678 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 679 if (lun) 680 mtx_unlock(&lun->lun_lock); 681 } 682 683 static void 684 ctl_ha_done(union ctl_io *io) 685 { 686 union ctl_ha_msg msg; 687 688 if (io->io_hdr.io_type == CTL_IO_SCSI) { 689 memset(&msg, 0, sizeof(msg)); 690 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 691 msg.hdr.original_sc = io->io_hdr.original_sc; 692 msg.hdr.nexus = io->io_hdr.nexus; 693 msg.hdr.status = io->io_hdr.status; 694 msg.scsi.scsi_status = io->scsiio.scsi_status; 695 msg.scsi.tag_num = io->scsiio.tag_num; 696 msg.scsi.tag_type = io->scsiio.tag_type; 697 msg.scsi.sense_len = io->scsiio.sense_len; 698 msg.scsi.sense_residual = io->scsiio.sense_residual; 699 msg.scsi.residual = io->scsiio.residual; 700 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 701 io->scsiio.sense_len); 702 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 703 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 704 msg.scsi.sense_len, M_WAITOK); 705 } 706 ctl_free_io(io); 707 } 708 709 static void 710 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 711 union ctl_ha_msg *msg_info) 712 { 713 struct ctl_scsiio *ctsio; 714 715 if (msg_info->hdr.original_sc == NULL) { 716 printf("%s: original_sc == NULL!\n", __func__); 717 /* XXX KDM now what? */ 718 return; 719 } 720 721 ctsio = &msg_info->hdr.original_sc->scsiio; 722 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 723 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 724 ctsio->io_hdr.status = msg_info->hdr.status; 725 ctsio->scsi_status = msg_info->scsi.scsi_status; 726 ctsio->sense_len = msg_info->scsi.sense_len; 727 ctsio->sense_residual = msg_info->scsi.sense_residual; 728 ctsio->residual = msg_info->scsi.residual; 729 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 730 msg_info->scsi.sense_len); 731 ctl_enqueue_isc((union ctl_io *)ctsio); 732 } 733 734 static void 735 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 736 union ctl_ha_msg *msg_info) 737 { 738 struct ctl_scsiio *ctsio; 739 740 if (msg_info->hdr.serializing_sc == NULL) { 741 printf("%s: serializing_sc == NULL!\n", __func__); 742 /* XXX KDM now what? */ 743 return; 744 } 745 746 ctsio = &msg_info->hdr.serializing_sc->scsiio; 747 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 748 ctl_enqueue_isc((union ctl_io *)ctsio); 749 } 750 751 void 752 ctl_isc_announce_lun(struct ctl_lun *lun) 753 { 754 struct ctl_softc *softc = lun->ctl_softc; 755 union ctl_ha_msg *msg; 756 struct ctl_ha_msg_lun_pr_key pr_key; 757 int i, k; 758 759 if (softc->ha_link != CTL_HA_LINK_ONLINE) 760 return; 761 mtx_lock(&lun->lun_lock); 762 i = sizeof(msg->lun); 763 if (lun->lun_devid) 764 i += lun->lun_devid->len; 765 i += sizeof(pr_key) * lun->pr_key_count; 766 alloc: 767 mtx_unlock(&lun->lun_lock); 768 msg = malloc(i, M_CTL, M_WAITOK); 769 mtx_lock(&lun->lun_lock); 770 k = sizeof(msg->lun); 771 if (lun->lun_devid) 772 k += lun->lun_devid->len; 773 k += sizeof(pr_key) * lun->pr_key_count; 774 if (i < k) { 775 free(msg, M_CTL); 776 i = k; 777 goto alloc; 778 } 779 bzero(&msg->lun, sizeof(msg->lun)); 780 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 781 msg->hdr.nexus.targ_lun = lun->lun; 782 msg->hdr.nexus.targ_mapped_lun = lun->lun; 783 msg->lun.flags = lun->flags; 784 msg->lun.pr_generation = lun->PRGeneration; 785 msg->lun.pr_res_idx = lun->pr_res_idx; 786 msg->lun.pr_res_type = lun->res_type; 787 msg->lun.pr_key_count = lun->pr_key_count; 788 i = 0; 789 if (lun->lun_devid) { 790 msg->lun.lun_devid_len = lun->lun_devid->len; 791 memcpy(&msg->lun.data[i], lun->lun_devid->data, 792 msg->lun.lun_devid_len); 793 i += msg->lun.lun_devid_len; 794 } 795 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 796 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 797 continue; 798 pr_key.pr_iid = k; 799 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 800 i += sizeof(pr_key); 801 } 802 mtx_unlock(&lun->lun_lock); 803 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 804 M_WAITOK); 805 free(msg, M_CTL); 806 807 if (lun->flags & CTL_LUN_PRIMARY_SC) { 808 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 809 ctl_isc_announce_mode(lun, -1, 810 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 811 lun->mode_pages.index[i].subpage); 812 } 813 } 814 } 815 816 void 817 ctl_isc_announce_port(struct ctl_port *port) 818 { 819 struct ctl_softc *softc = port->ctl_softc; 820 union ctl_ha_msg *msg; 821 int i; 822 823 if (port->targ_port < softc->port_min || 824 port->targ_port >= softc->port_max || 825 softc->ha_link != CTL_HA_LINK_ONLINE) 826 return; 827 i = sizeof(msg->port) + strlen(port->port_name) + 1; 828 if (port->lun_map) 829 i += sizeof(uint32_t) * CTL_MAX_LUNS; 830 if (port->port_devid) 831 i += port->port_devid->len; 832 if (port->target_devid) 833 i += port->target_devid->len; 834 if (port->init_devid) 835 i += port->init_devid->len; 836 msg = malloc(i, M_CTL, M_WAITOK); 837 bzero(&msg->port, sizeof(msg->port)); 838 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 839 msg->hdr.nexus.targ_port = port->targ_port; 840 msg->port.port_type = port->port_type; 841 msg->port.physical_port = port->physical_port; 842 msg->port.virtual_port = port->virtual_port; 843 msg->port.status = port->status; 844 i = 0; 845 msg->port.name_len = sprintf(&msg->port.data[i], 846 "%d:%s", softc->ha_id, port->port_name) + 1; 847 i += msg->port.name_len; 848 if (port->lun_map) { 849 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS; 850 memcpy(&msg->port.data[i], port->lun_map, 851 msg->port.lun_map_len); 852 i += msg->port.lun_map_len; 853 } 854 if (port->port_devid) { 855 msg->port.port_devid_len = port->port_devid->len; 856 memcpy(&msg->port.data[i], port->port_devid->data, 857 msg->port.port_devid_len); 858 i += msg->port.port_devid_len; 859 } 860 if (port->target_devid) { 861 msg->port.target_devid_len = port->target_devid->len; 862 memcpy(&msg->port.data[i], port->target_devid->data, 863 msg->port.target_devid_len); 864 i += msg->port.target_devid_len; 865 } 866 if (port->init_devid) { 867 msg->port.init_devid_len = port->init_devid->len; 868 memcpy(&msg->port.data[i], port->init_devid->data, 869 msg->port.init_devid_len); 870 i += msg->port.init_devid_len; 871 } 872 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 873 M_WAITOK); 874 free(msg, M_CTL); 875 } 876 877 void 878 ctl_isc_announce_iid(struct ctl_port *port, int iid) 879 { 880 struct ctl_softc *softc = port->ctl_softc; 881 union ctl_ha_msg *msg; 882 int i, l; 883 884 if (port->targ_port < softc->port_min || 885 port->targ_port >= softc->port_max || 886 softc->ha_link != CTL_HA_LINK_ONLINE) 887 return; 888 mtx_lock(&softc->ctl_lock); 889 i = sizeof(msg->iid); 890 l = 0; 891 if (port->wwpn_iid[iid].name) 892 l = strlen(port->wwpn_iid[iid].name) + 1; 893 i += l; 894 msg = malloc(i, M_CTL, M_NOWAIT); 895 if (msg == NULL) { 896 mtx_unlock(&softc->ctl_lock); 897 return; 898 } 899 bzero(&msg->iid, sizeof(msg->iid)); 900 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 901 msg->hdr.nexus.targ_port = port->targ_port; 902 msg->hdr.nexus.initid = iid; 903 msg->iid.in_use = port->wwpn_iid[iid].in_use; 904 msg->iid.name_len = l; 905 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 906 if (port->wwpn_iid[iid].name) 907 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 908 mtx_unlock(&softc->ctl_lock); 909 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 910 free(msg, M_CTL); 911 } 912 913 void 914 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 915 uint8_t page, uint8_t subpage) 916 { 917 struct ctl_softc *softc = lun->ctl_softc; 918 union ctl_ha_msg msg; 919 int i; 920 921 if (softc->ha_link != CTL_HA_LINK_ONLINE) 922 return; 923 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 924 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 925 page && lun->mode_pages.index[i].subpage == subpage) 926 break; 927 } 928 if (i == CTL_NUM_MODE_PAGES) 929 return; 930 bzero(&msg.mode, sizeof(msg.mode)); 931 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 932 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 933 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 934 msg.hdr.nexus.targ_lun = lun->lun; 935 msg.hdr.nexus.targ_mapped_lun = lun->lun; 936 msg.mode.page_code = page; 937 msg.mode.subpage = subpage; 938 msg.mode.page_len = lun->mode_pages.index[i].page_len; 939 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 940 msg.mode.page_len); 941 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 942 M_WAITOK); 943 } 944 945 static void 946 ctl_isc_ha_link_up(struct ctl_softc *softc) 947 { 948 struct ctl_port *port; 949 struct ctl_lun *lun; 950 union ctl_ha_msg msg; 951 int i; 952 953 /* Announce this node parameters to peer for validation. */ 954 msg.login.msg_type = CTL_MSG_LOGIN; 955 msg.login.version = CTL_HA_VERSION; 956 msg.login.ha_mode = softc->ha_mode; 957 msg.login.ha_id = softc->ha_id; 958 msg.login.max_luns = CTL_MAX_LUNS; 959 msg.login.max_ports = CTL_MAX_PORTS; 960 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 961 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 962 M_WAITOK); 963 964 STAILQ_FOREACH(port, &softc->port_list, links) { 965 ctl_isc_announce_port(port); 966 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 967 if (port->wwpn_iid[i].in_use) 968 ctl_isc_announce_iid(port, i); 969 } 970 } 971 STAILQ_FOREACH(lun, &softc->lun_list, links) 972 ctl_isc_announce_lun(lun); 973 } 974 975 static void 976 ctl_isc_ha_link_down(struct ctl_softc *softc) 977 { 978 struct ctl_port *port; 979 struct ctl_lun *lun; 980 union ctl_io *io; 981 int i; 982 983 mtx_lock(&softc->ctl_lock); 984 STAILQ_FOREACH(lun, &softc->lun_list, links) { 985 mtx_lock(&lun->lun_lock); 986 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 987 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 988 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 989 } 990 mtx_unlock(&lun->lun_lock); 991 992 mtx_unlock(&softc->ctl_lock); 993 io = ctl_alloc_io(softc->othersc_pool); 994 mtx_lock(&softc->ctl_lock); 995 ctl_zero_io(io); 996 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 997 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 998 ctl_enqueue_isc(io); 999 } 1000 1001 STAILQ_FOREACH(port, &softc->port_list, links) { 1002 if (port->targ_port >= softc->port_min && 1003 port->targ_port < softc->port_max) 1004 continue; 1005 port->status &= ~CTL_PORT_STATUS_ONLINE; 1006 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1007 port->wwpn_iid[i].in_use = 0; 1008 free(port->wwpn_iid[i].name, M_CTL); 1009 port->wwpn_iid[i].name = NULL; 1010 } 1011 } 1012 mtx_unlock(&softc->ctl_lock); 1013 } 1014 1015 static void 1016 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1017 { 1018 struct ctl_lun *lun; 1019 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1020 1021 mtx_lock(&softc->ctl_lock); 1022 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS && 1023 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) { 1024 mtx_lock(&lun->lun_lock); 1025 mtx_unlock(&softc->ctl_lock); 1026 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && 1027 msg->ua.ua_set) 1028 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1029 if (msg->ua.ua_all) { 1030 if (msg->ua.ua_set) 1031 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1032 else 1033 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1034 } else { 1035 if (msg->ua.ua_set) 1036 ctl_est_ua(lun, iid, msg->ua.ua_type); 1037 else 1038 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1039 } 1040 mtx_unlock(&lun->lun_lock); 1041 } else 1042 mtx_unlock(&softc->ctl_lock); 1043 } 1044 1045 static void 1046 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1047 { 1048 struct ctl_lun *lun; 1049 struct ctl_ha_msg_lun_pr_key pr_key; 1050 int i, k; 1051 ctl_lun_flags oflags; 1052 uint32_t targ_lun; 1053 1054 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1055 mtx_lock(&softc->ctl_lock); 1056 if ((targ_lun >= CTL_MAX_LUNS) || 1057 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 1058 mtx_unlock(&softc->ctl_lock); 1059 return; 1060 } 1061 mtx_lock(&lun->lun_lock); 1062 mtx_unlock(&softc->ctl_lock); 1063 if (lun->flags & CTL_LUN_DISABLED) { 1064 mtx_unlock(&lun->lun_lock); 1065 return; 1066 } 1067 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1068 if (msg->lun.lun_devid_len != i || (i > 0 && 1069 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1070 mtx_unlock(&lun->lun_lock); 1071 printf("%s: Received conflicting HA LUN %d\n", 1072 __func__, msg->hdr.nexus.targ_lun); 1073 return; 1074 } else { 1075 /* Record whether peer is primary. */ 1076 oflags = lun->flags; 1077 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1078 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1079 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1080 else 1081 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1082 if (oflags != lun->flags) 1083 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1084 1085 /* If peer is primary and we are not -- use data */ 1086 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1087 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1088 lun->PRGeneration = msg->lun.pr_generation; 1089 lun->pr_res_idx = msg->lun.pr_res_idx; 1090 lun->res_type = msg->lun.pr_res_type; 1091 lun->pr_key_count = msg->lun.pr_key_count; 1092 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1093 ctl_clr_prkey(lun, k); 1094 for (k = 0; k < msg->lun.pr_key_count; k++) { 1095 memcpy(&pr_key, &msg->lun.data[i], 1096 sizeof(pr_key)); 1097 ctl_alloc_prkey(lun, pr_key.pr_iid); 1098 ctl_set_prkey(lun, pr_key.pr_iid, 1099 pr_key.pr_key); 1100 i += sizeof(pr_key); 1101 } 1102 } 1103 1104 mtx_unlock(&lun->lun_lock); 1105 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1106 __func__, msg->hdr.nexus.targ_lun, 1107 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1108 "primary" : "secondary")); 1109 1110 /* If we are primary but peer doesn't know -- notify */ 1111 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1112 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1113 ctl_isc_announce_lun(lun); 1114 } 1115 } 1116 1117 static void 1118 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1119 { 1120 struct ctl_port *port; 1121 struct ctl_lun *lun; 1122 int i, new; 1123 1124 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1125 if (port == NULL) { 1126 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1127 msg->hdr.nexus.targ_port)); 1128 new = 1; 1129 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1130 port->frontend = &ha_frontend; 1131 port->targ_port = msg->hdr.nexus.targ_port; 1132 port->fe_datamove = ctl_ha_datamove; 1133 port->fe_done = ctl_ha_done; 1134 } else if (port->frontend == &ha_frontend) { 1135 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1136 msg->hdr.nexus.targ_port)); 1137 new = 0; 1138 } else { 1139 printf("%s: Received conflicting HA port %d\n", 1140 __func__, msg->hdr.nexus.targ_port); 1141 return; 1142 } 1143 port->port_type = msg->port.port_type; 1144 port->physical_port = msg->port.physical_port; 1145 port->virtual_port = msg->port.virtual_port; 1146 port->status = msg->port.status; 1147 i = 0; 1148 free(port->port_name, M_CTL); 1149 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1150 M_CTL); 1151 i += msg->port.name_len; 1152 if (msg->port.lun_map_len != 0) { 1153 if (port->lun_map == NULL) 1154 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 1155 M_CTL, M_WAITOK); 1156 memcpy(port->lun_map, &msg->port.data[i], 1157 sizeof(uint32_t) * CTL_MAX_LUNS); 1158 i += msg->port.lun_map_len; 1159 } else { 1160 free(port->lun_map, M_CTL); 1161 port->lun_map = NULL; 1162 } 1163 if (msg->port.port_devid_len != 0) { 1164 if (port->port_devid == NULL || 1165 port->port_devid->len != msg->port.port_devid_len) { 1166 free(port->port_devid, M_CTL); 1167 port->port_devid = malloc(sizeof(struct ctl_devid) + 1168 msg->port.port_devid_len, M_CTL, M_WAITOK); 1169 } 1170 memcpy(port->port_devid->data, &msg->port.data[i], 1171 msg->port.port_devid_len); 1172 port->port_devid->len = msg->port.port_devid_len; 1173 i += msg->port.port_devid_len; 1174 } else { 1175 free(port->port_devid, M_CTL); 1176 port->port_devid = NULL; 1177 } 1178 if (msg->port.target_devid_len != 0) { 1179 if (port->target_devid == NULL || 1180 port->target_devid->len != msg->port.target_devid_len) { 1181 free(port->target_devid, M_CTL); 1182 port->target_devid = malloc(sizeof(struct ctl_devid) + 1183 msg->port.target_devid_len, M_CTL, M_WAITOK); 1184 } 1185 memcpy(port->target_devid->data, &msg->port.data[i], 1186 msg->port.target_devid_len); 1187 port->target_devid->len = msg->port.target_devid_len; 1188 i += msg->port.target_devid_len; 1189 } else { 1190 free(port->target_devid, M_CTL); 1191 port->target_devid = NULL; 1192 } 1193 if (msg->port.init_devid_len != 0) { 1194 if (port->init_devid == NULL || 1195 port->init_devid->len != msg->port.init_devid_len) { 1196 free(port->init_devid, M_CTL); 1197 port->init_devid = malloc(sizeof(struct ctl_devid) + 1198 msg->port.init_devid_len, M_CTL, M_WAITOK); 1199 } 1200 memcpy(port->init_devid->data, &msg->port.data[i], 1201 msg->port.init_devid_len); 1202 port->init_devid->len = msg->port.init_devid_len; 1203 i += msg->port.init_devid_len; 1204 } else { 1205 free(port->init_devid, M_CTL); 1206 port->init_devid = NULL; 1207 } 1208 if (new) { 1209 if (ctl_port_register(port) != 0) { 1210 printf("%s: ctl_port_register() failed with error\n", 1211 __func__); 1212 } 1213 } 1214 mtx_lock(&softc->ctl_lock); 1215 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1216 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 1217 continue; 1218 mtx_lock(&lun->lun_lock); 1219 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1220 mtx_unlock(&lun->lun_lock); 1221 } 1222 mtx_unlock(&softc->ctl_lock); 1223 } 1224 1225 static void 1226 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1227 { 1228 struct ctl_port *port; 1229 int iid; 1230 1231 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1232 if (port == NULL) { 1233 printf("%s: Received IID for unknown port %d\n", 1234 __func__, msg->hdr.nexus.targ_port); 1235 return; 1236 } 1237 iid = msg->hdr.nexus.initid; 1238 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1239 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1240 free(port->wwpn_iid[iid].name, M_CTL); 1241 if (msg->iid.name_len) { 1242 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1243 msg->iid.name_len, M_CTL); 1244 } else 1245 port->wwpn_iid[iid].name = NULL; 1246 } 1247 1248 static void 1249 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1250 { 1251 1252 if (msg->login.version != CTL_HA_VERSION) { 1253 printf("CTL HA peers have different versions %d != %d\n", 1254 msg->login.version, CTL_HA_VERSION); 1255 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1256 return; 1257 } 1258 if (msg->login.ha_mode != softc->ha_mode) { 1259 printf("CTL HA peers have different ha_mode %d != %d\n", 1260 msg->login.ha_mode, softc->ha_mode); 1261 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1262 return; 1263 } 1264 if (msg->login.ha_id == softc->ha_id) { 1265 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1266 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1267 return; 1268 } 1269 if (msg->login.max_luns != CTL_MAX_LUNS || 1270 msg->login.max_ports != CTL_MAX_PORTS || 1271 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1272 printf("CTL HA peers have different limits\n"); 1273 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1274 return; 1275 } 1276 } 1277 1278 static void 1279 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1280 { 1281 struct ctl_lun *lun; 1282 int i; 1283 uint32_t initidx, targ_lun; 1284 1285 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1286 mtx_lock(&softc->ctl_lock); 1287 if ((targ_lun >= CTL_MAX_LUNS) || 1288 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 1289 mtx_unlock(&softc->ctl_lock); 1290 return; 1291 } 1292 mtx_lock(&lun->lun_lock); 1293 mtx_unlock(&softc->ctl_lock); 1294 if (lun->flags & CTL_LUN_DISABLED) { 1295 mtx_unlock(&lun->lun_lock); 1296 return; 1297 } 1298 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1299 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1300 msg->mode.page_code && 1301 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1302 break; 1303 } 1304 if (i == CTL_NUM_MODE_PAGES) { 1305 mtx_unlock(&lun->lun_lock); 1306 return; 1307 } 1308 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1309 lun->mode_pages.index[i].page_len); 1310 initidx = ctl_get_initindex(&msg->hdr.nexus); 1311 if (initidx != -1) 1312 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1313 mtx_unlock(&lun->lun_lock); 1314 } 1315 1316 /* 1317 * ISC (Inter Shelf Communication) event handler. Events from the HA 1318 * subsystem come in here. 1319 */ 1320 static void 1321 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1322 { 1323 struct ctl_softc *softc = control_softc; 1324 union ctl_io *io; 1325 struct ctl_prio *presio; 1326 ctl_ha_status isc_status; 1327 1328 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1329 if (event == CTL_HA_EVT_MSG_RECV) { 1330 union ctl_ha_msg *msg, msgbuf; 1331 1332 if (param > sizeof(msgbuf)) 1333 msg = malloc(param, M_CTL, M_WAITOK); 1334 else 1335 msg = &msgbuf; 1336 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1337 M_WAITOK); 1338 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1339 printf("%s: Error receiving message: %d\n", 1340 __func__, isc_status); 1341 if (msg != &msgbuf) 1342 free(msg, M_CTL); 1343 return; 1344 } 1345 1346 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1347 switch (msg->hdr.msg_type) { 1348 case CTL_MSG_SERIALIZE: 1349 io = ctl_alloc_io(softc->othersc_pool); 1350 ctl_zero_io(io); 1351 // populate ctsio from msg 1352 io->io_hdr.io_type = CTL_IO_SCSI; 1353 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1354 io->io_hdr.original_sc = msg->hdr.original_sc; 1355 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1356 CTL_FLAG_IO_ACTIVE; 1357 /* 1358 * If we're in serialization-only mode, we don't 1359 * want to go through full done processing. Thus 1360 * the COPY flag. 1361 * 1362 * XXX KDM add another flag that is more specific. 1363 */ 1364 if (softc->ha_mode != CTL_HA_MODE_XFER) 1365 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1366 io->io_hdr.nexus = msg->hdr.nexus; 1367 #if 0 1368 printf("port %u, iid %u, lun %u\n", 1369 io->io_hdr.nexus.targ_port, 1370 io->io_hdr.nexus.initid, 1371 io->io_hdr.nexus.targ_lun); 1372 #endif 1373 io->scsiio.tag_num = msg->scsi.tag_num; 1374 io->scsiio.tag_type = msg->scsi.tag_type; 1375 #ifdef CTL_TIME_IO 1376 io->io_hdr.start_time = time_uptime; 1377 getbinuptime(&io->io_hdr.start_bt); 1378 #endif /* CTL_TIME_IO */ 1379 io->scsiio.cdb_len = msg->scsi.cdb_len; 1380 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1381 CTL_MAX_CDBLEN); 1382 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1383 const struct ctl_cmd_entry *entry; 1384 1385 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1386 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1387 io->io_hdr.flags |= 1388 entry->flags & CTL_FLAG_DATA_MASK; 1389 } 1390 ctl_enqueue_isc(io); 1391 break; 1392 1393 /* Performed on the Originating SC, XFER mode only */ 1394 case CTL_MSG_DATAMOVE: { 1395 struct ctl_sg_entry *sgl; 1396 int i, j; 1397 1398 io = msg->hdr.original_sc; 1399 if (io == NULL) { 1400 printf("%s: original_sc == NULL!\n", __func__); 1401 /* XXX KDM do something here */ 1402 break; 1403 } 1404 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1405 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1406 /* 1407 * Keep track of this, we need to send it back over 1408 * when the datamove is complete. 1409 */ 1410 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1411 if (msg->hdr.status == CTL_SUCCESS) 1412 io->io_hdr.status = msg->hdr.status; 1413 1414 if (msg->dt.sg_sequence == 0) { 1415 #ifdef CTL_TIME_IO 1416 getbinuptime(&io->io_hdr.dma_start_bt); 1417 #endif 1418 i = msg->dt.kern_sg_entries + 1419 msg->dt.kern_data_len / 1420 CTL_HA_DATAMOVE_SEGMENT + 1; 1421 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1422 M_WAITOK | M_ZERO); 1423 io->io_hdr.remote_sglist = sgl; 1424 io->io_hdr.local_sglist = 1425 &sgl[msg->dt.kern_sg_entries]; 1426 1427 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1428 1429 io->scsiio.kern_sg_entries = 1430 msg->dt.kern_sg_entries; 1431 io->scsiio.rem_sg_entries = 1432 msg->dt.kern_sg_entries; 1433 io->scsiio.kern_data_len = 1434 msg->dt.kern_data_len; 1435 io->scsiio.kern_total_len = 1436 msg->dt.kern_total_len; 1437 io->scsiio.kern_data_resid = 1438 msg->dt.kern_data_resid; 1439 io->scsiio.kern_rel_offset = 1440 msg->dt.kern_rel_offset; 1441 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1442 io->io_hdr.flags |= msg->dt.flags & 1443 CTL_FLAG_BUS_ADDR; 1444 } else 1445 sgl = (struct ctl_sg_entry *) 1446 io->scsiio.kern_data_ptr; 1447 1448 for (i = msg->dt.sent_sg_entries, j = 0; 1449 i < (msg->dt.sent_sg_entries + 1450 msg->dt.cur_sg_entries); i++, j++) { 1451 sgl[i].addr = msg->dt.sg_list[j].addr; 1452 sgl[i].len = msg->dt.sg_list[j].len; 1453 1454 #if 0 1455 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1456 __func__, sgl[i].addr, sgl[i].len, j, i); 1457 #endif 1458 } 1459 1460 /* 1461 * If this is the last piece of the I/O, we've got 1462 * the full S/G list. Queue processing in the thread. 1463 * Otherwise wait for the next piece. 1464 */ 1465 if (msg->dt.sg_last != 0) 1466 ctl_enqueue_isc(io); 1467 break; 1468 } 1469 /* Performed on the Serializing (primary) SC, XFER mode only */ 1470 case CTL_MSG_DATAMOVE_DONE: { 1471 if (msg->hdr.serializing_sc == NULL) { 1472 printf("%s: serializing_sc == NULL!\n", 1473 __func__); 1474 /* XXX KDM now what? */ 1475 break; 1476 } 1477 /* 1478 * We grab the sense information here in case 1479 * there was a failure, so we can return status 1480 * back to the initiator. 1481 */ 1482 io = msg->hdr.serializing_sc; 1483 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1484 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1485 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1486 io->io_hdr.port_status = msg->scsi.fetd_status; 1487 io->scsiio.residual = msg->scsi.residual; 1488 if (msg->hdr.status != CTL_STATUS_NONE) { 1489 io->io_hdr.status = msg->hdr.status; 1490 io->scsiio.scsi_status = msg->scsi.scsi_status; 1491 io->scsiio.sense_len = msg->scsi.sense_len; 1492 io->scsiio.sense_residual =msg->scsi.sense_residual; 1493 memcpy(&io->scsiio.sense_data, 1494 &msg->scsi.sense_data, 1495 msg->scsi.sense_len); 1496 if (msg->hdr.status == CTL_SUCCESS) 1497 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1498 } 1499 ctl_enqueue_isc(io); 1500 break; 1501 } 1502 1503 /* Preformed on Originating SC, SER_ONLY mode */ 1504 case CTL_MSG_R2R: 1505 io = msg->hdr.original_sc; 1506 if (io == NULL) { 1507 printf("%s: original_sc == NULL!\n", 1508 __func__); 1509 break; 1510 } 1511 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1512 io->io_hdr.msg_type = CTL_MSG_R2R; 1513 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1514 ctl_enqueue_isc(io); 1515 break; 1516 1517 /* 1518 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1519 * mode. 1520 * Performed on the Originating (i.e. secondary) SC in XFER 1521 * mode 1522 */ 1523 case CTL_MSG_FINISH_IO: 1524 if (softc->ha_mode == CTL_HA_MODE_XFER) 1525 ctl_isc_handler_finish_xfer(softc, msg); 1526 else 1527 ctl_isc_handler_finish_ser_only(softc, msg); 1528 break; 1529 1530 /* Preformed on Originating SC */ 1531 case CTL_MSG_BAD_JUJU: 1532 io = msg->hdr.original_sc; 1533 if (io == NULL) { 1534 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1535 __func__); 1536 break; 1537 } 1538 ctl_copy_sense_data(msg, io); 1539 /* 1540 * IO should have already been cleaned up on other 1541 * SC so clear this flag so we won't send a message 1542 * back to finish the IO there. 1543 */ 1544 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1545 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1546 1547 /* io = msg->hdr.serializing_sc; */ 1548 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1549 ctl_enqueue_isc(io); 1550 break; 1551 1552 /* Handle resets sent from the other side */ 1553 case CTL_MSG_MANAGE_TASKS: { 1554 struct ctl_taskio *taskio; 1555 taskio = (struct ctl_taskio *)ctl_alloc_io( 1556 softc->othersc_pool); 1557 ctl_zero_io((union ctl_io *)taskio); 1558 taskio->io_hdr.io_type = CTL_IO_TASK; 1559 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1560 taskio->io_hdr.nexus = msg->hdr.nexus; 1561 taskio->task_action = msg->task.task_action; 1562 taskio->tag_num = msg->task.tag_num; 1563 taskio->tag_type = msg->task.tag_type; 1564 #ifdef CTL_TIME_IO 1565 taskio->io_hdr.start_time = time_uptime; 1566 getbinuptime(&taskio->io_hdr.start_bt); 1567 #endif /* CTL_TIME_IO */ 1568 ctl_run_task((union ctl_io *)taskio); 1569 break; 1570 } 1571 /* Persistent Reserve action which needs attention */ 1572 case CTL_MSG_PERS_ACTION: 1573 presio = (struct ctl_prio *)ctl_alloc_io( 1574 softc->othersc_pool); 1575 ctl_zero_io((union ctl_io *)presio); 1576 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1577 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1578 presio->io_hdr.nexus = msg->hdr.nexus; 1579 presio->pr_msg = msg->pr; 1580 ctl_enqueue_isc((union ctl_io *)presio); 1581 break; 1582 case CTL_MSG_UA: 1583 ctl_isc_ua(softc, msg, param); 1584 break; 1585 case CTL_MSG_PORT_SYNC: 1586 ctl_isc_port_sync(softc, msg, param); 1587 break; 1588 case CTL_MSG_LUN_SYNC: 1589 ctl_isc_lun_sync(softc, msg, param); 1590 break; 1591 case CTL_MSG_IID_SYNC: 1592 ctl_isc_iid_sync(softc, msg, param); 1593 break; 1594 case CTL_MSG_LOGIN: 1595 ctl_isc_login(softc, msg, param); 1596 break; 1597 case CTL_MSG_MODE_SYNC: 1598 ctl_isc_mode_sync(softc, msg, param); 1599 break; 1600 default: 1601 printf("Received HA message of unknown type %d\n", 1602 msg->hdr.msg_type); 1603 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1604 break; 1605 } 1606 if (msg != &msgbuf) 1607 free(msg, M_CTL); 1608 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1609 printf("CTL: HA link status changed from %d to %d\n", 1610 softc->ha_link, param); 1611 if (param == softc->ha_link) 1612 return; 1613 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1614 softc->ha_link = param; 1615 ctl_isc_ha_link_down(softc); 1616 } else { 1617 softc->ha_link = param; 1618 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1619 ctl_isc_ha_link_up(softc); 1620 } 1621 return; 1622 } else { 1623 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1624 return; 1625 } 1626 } 1627 1628 static void 1629 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1630 { 1631 1632 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1633 src->scsi.sense_len); 1634 dest->scsiio.scsi_status = src->scsi.scsi_status; 1635 dest->scsiio.sense_len = src->scsi.sense_len; 1636 dest->io_hdr.status = src->hdr.status; 1637 } 1638 1639 static void 1640 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1641 { 1642 1643 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1644 src->scsiio.sense_len); 1645 dest->scsi.scsi_status = src->scsiio.scsi_status; 1646 dest->scsi.sense_len = src->scsiio.sense_len; 1647 dest->hdr.status = src->io_hdr.status; 1648 } 1649 1650 void 1651 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1652 { 1653 struct ctl_softc *softc = lun->ctl_softc; 1654 ctl_ua_type *pu; 1655 1656 if (initidx < softc->init_min || initidx >= softc->init_max) 1657 return; 1658 mtx_assert(&lun->lun_lock, MA_OWNED); 1659 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1660 if (pu == NULL) 1661 return; 1662 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1663 } 1664 1665 void 1666 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1667 { 1668 int i; 1669 1670 mtx_assert(&lun->lun_lock, MA_OWNED); 1671 if (lun->pending_ua[port] == NULL) 1672 return; 1673 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1674 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1675 continue; 1676 lun->pending_ua[port][i] |= ua; 1677 } 1678 } 1679 1680 void 1681 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1682 { 1683 struct ctl_softc *softc = lun->ctl_softc; 1684 int i; 1685 1686 mtx_assert(&lun->lun_lock, MA_OWNED); 1687 for (i = softc->port_min; i < softc->port_max; i++) 1688 ctl_est_ua_port(lun, i, except, ua); 1689 } 1690 1691 void 1692 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1693 { 1694 struct ctl_softc *softc = lun->ctl_softc; 1695 ctl_ua_type *pu; 1696 1697 if (initidx < softc->init_min || initidx >= softc->init_max) 1698 return; 1699 mtx_assert(&lun->lun_lock, MA_OWNED); 1700 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1701 if (pu == NULL) 1702 return; 1703 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1704 } 1705 1706 void 1707 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1708 { 1709 struct ctl_softc *softc = lun->ctl_softc; 1710 int i, j; 1711 1712 mtx_assert(&lun->lun_lock, MA_OWNED); 1713 for (i = softc->port_min; i < softc->port_max; i++) { 1714 if (lun->pending_ua[i] == NULL) 1715 continue; 1716 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1717 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1718 continue; 1719 lun->pending_ua[i][j] &= ~ua; 1720 } 1721 } 1722 } 1723 1724 void 1725 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1726 ctl_ua_type ua_type) 1727 { 1728 struct ctl_lun *lun; 1729 1730 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1731 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1732 mtx_lock(&lun->lun_lock); 1733 ctl_clr_ua(lun, initidx, ua_type); 1734 mtx_unlock(&lun->lun_lock); 1735 } 1736 } 1737 1738 static int 1739 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1740 { 1741 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1742 struct ctl_lun *lun; 1743 struct ctl_lun_req ireq; 1744 int error, value; 1745 1746 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1747 error = sysctl_handle_int(oidp, &value, 0, req); 1748 if ((error != 0) || (req->newptr == NULL)) 1749 return (error); 1750 1751 mtx_lock(&softc->ctl_lock); 1752 if (value == 0) 1753 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1754 else 1755 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1756 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1757 mtx_unlock(&softc->ctl_lock); 1758 bzero(&ireq, sizeof(ireq)); 1759 ireq.reqtype = CTL_LUNREQ_MODIFY; 1760 ireq.reqdata.modify.lun_id = lun->lun; 1761 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1762 curthread); 1763 if (ireq.status != CTL_LUN_OK) { 1764 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1765 __func__, ireq.status, ireq.error_str); 1766 } 1767 mtx_lock(&softc->ctl_lock); 1768 } 1769 mtx_unlock(&softc->ctl_lock); 1770 return (0); 1771 } 1772 1773 static int 1774 ctl_init(void) 1775 { 1776 struct ctl_softc *softc; 1777 void *other_pool; 1778 int i, error; 1779 1780 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1781 M_WAITOK | M_ZERO); 1782 1783 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1784 "cam/ctl"); 1785 softc->dev->si_drv1 = softc; 1786 1787 sysctl_ctx_init(&softc->sysctl_ctx); 1788 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1789 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1790 CTLFLAG_RD, 0, "CAM Target Layer"); 1791 1792 if (softc->sysctl_tree == NULL) { 1793 printf("%s: unable to allocate sysctl tree\n", __func__); 1794 destroy_dev(softc->dev); 1795 free(control_softc, M_DEVBUF); 1796 control_softc = NULL; 1797 return (ENOMEM); 1798 } 1799 1800 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1801 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1802 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1803 softc->flags = 0; 1804 1805 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1806 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1807 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1808 1809 /* 1810 * In Copan's HA scheme, the "master" and "slave" roles are 1811 * figured out through the slot the controller is in. Although it 1812 * is an active/active system, someone has to be in charge. 1813 */ 1814 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1815 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1816 "HA head ID (0 - no HA)"); 1817 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) { 1818 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1819 softc->is_single = 1; 1820 softc->port_cnt = CTL_MAX_PORTS; 1821 softc->port_min = 0; 1822 } else { 1823 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS; 1824 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1825 } 1826 softc->port_max = softc->port_min + softc->port_cnt; 1827 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1828 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1829 1830 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1831 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1832 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1833 1834 STAILQ_INIT(&softc->lun_list); 1835 STAILQ_INIT(&softc->pending_lun_queue); 1836 STAILQ_INIT(&softc->fe_list); 1837 STAILQ_INIT(&softc->port_list); 1838 STAILQ_INIT(&softc->be_list); 1839 ctl_tpc_init(softc); 1840 1841 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1842 &other_pool) != 0) 1843 { 1844 printf("ctl: can't allocate %d entry other SC pool, " 1845 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1846 return (ENOMEM); 1847 } 1848 softc->othersc_pool = other_pool; 1849 1850 if (worker_threads <= 0) 1851 worker_threads = max(1, mp_ncpus / 4); 1852 if (worker_threads > CTL_MAX_THREADS) 1853 worker_threads = CTL_MAX_THREADS; 1854 1855 for (i = 0; i < worker_threads; i++) { 1856 struct ctl_thread *thr = &softc->threads[i]; 1857 1858 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1859 thr->ctl_softc = softc; 1860 STAILQ_INIT(&thr->incoming_queue); 1861 STAILQ_INIT(&thr->rtr_queue); 1862 STAILQ_INIT(&thr->done_queue); 1863 STAILQ_INIT(&thr->isc_queue); 1864 1865 error = kproc_kthread_add(ctl_work_thread, thr, 1866 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1867 if (error != 0) { 1868 printf("error creating CTL work thread!\n"); 1869 ctl_pool_free(other_pool); 1870 return (error); 1871 } 1872 } 1873 error = kproc_kthread_add(ctl_lun_thread, softc, 1874 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1875 if (error != 0) { 1876 printf("error creating CTL lun thread!\n"); 1877 ctl_pool_free(other_pool); 1878 return (error); 1879 } 1880 error = kproc_kthread_add(ctl_thresh_thread, softc, 1881 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1882 if (error != 0) { 1883 printf("error creating CTL threshold thread!\n"); 1884 ctl_pool_free(other_pool); 1885 return (error); 1886 } 1887 1888 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1889 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1890 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1891 1892 if (softc->is_single == 0) { 1893 ctl_frontend_register(&ha_frontend); 1894 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1895 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1896 softc->is_single = 1; 1897 } else 1898 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1899 != CTL_HA_STATUS_SUCCESS) { 1900 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1901 softc->is_single = 1; 1902 } 1903 } 1904 return (0); 1905 } 1906 1907 void 1908 ctl_shutdown(void) 1909 { 1910 struct ctl_softc *softc = control_softc; 1911 struct ctl_lun *lun, *next_lun; 1912 1913 if (softc->is_single == 0) { 1914 ctl_ha_msg_shutdown(softc); 1915 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1916 != CTL_HA_STATUS_SUCCESS) 1917 printf("%s: ctl_ha_msg_deregister failed.\n", __func__); 1918 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 1919 printf("%s: ctl_ha_msg_destroy failed.\n", __func__); 1920 ctl_frontend_deregister(&ha_frontend); 1921 } 1922 1923 mtx_lock(&softc->ctl_lock); 1924 1925 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) 1926 ctl_free_lun(lun); 1927 1928 mtx_unlock(&softc->ctl_lock); 1929 1930 #if 0 1931 ctl_shutdown_thread(softc->work_thread); 1932 mtx_destroy(&softc->queue_lock); 1933 #endif 1934 1935 ctl_tpc_shutdown(softc); 1936 uma_zdestroy(softc->io_zone); 1937 mtx_destroy(&softc->ctl_lock); 1938 1939 destroy_dev(softc->dev); 1940 1941 sysctl_ctx_free(&softc->sysctl_ctx); 1942 1943 free(control_softc, M_DEVBUF); 1944 control_softc = NULL; 1945 } 1946 1947 static int 1948 ctl_module_event_handler(module_t mod, int what, void *arg) 1949 { 1950 1951 switch (what) { 1952 case MOD_LOAD: 1953 return (ctl_init()); 1954 case MOD_UNLOAD: 1955 return (EBUSY); 1956 default: 1957 return (EOPNOTSUPP); 1958 } 1959 } 1960 1961 /* 1962 * XXX KDM should we do some access checks here? Bump a reference count to 1963 * prevent a CTL module from being unloaded while someone has it open? 1964 */ 1965 static int 1966 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1967 { 1968 return (0); 1969 } 1970 1971 static int 1972 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1973 { 1974 return (0); 1975 } 1976 1977 /* 1978 * Remove an initiator by port number and initiator ID. 1979 * Returns 0 for success, -1 for failure. 1980 */ 1981 int 1982 ctl_remove_initiator(struct ctl_port *port, int iid) 1983 { 1984 struct ctl_softc *softc = port->ctl_softc; 1985 1986 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1987 1988 if (iid > CTL_MAX_INIT_PER_PORT) { 1989 printf("%s: initiator ID %u > maximun %u!\n", 1990 __func__, iid, CTL_MAX_INIT_PER_PORT); 1991 return (-1); 1992 } 1993 1994 mtx_lock(&softc->ctl_lock); 1995 port->wwpn_iid[iid].in_use--; 1996 port->wwpn_iid[iid].last_use = time_uptime; 1997 mtx_unlock(&softc->ctl_lock); 1998 ctl_isc_announce_iid(port, iid); 1999 2000 return (0); 2001 } 2002 2003 /* 2004 * Add an initiator to the initiator map. 2005 * Returns iid for success, < 0 for failure. 2006 */ 2007 int 2008 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2009 { 2010 struct ctl_softc *softc = port->ctl_softc; 2011 time_t best_time; 2012 int i, best; 2013 2014 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2015 2016 if (iid >= CTL_MAX_INIT_PER_PORT) { 2017 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2018 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2019 free(name, M_CTL); 2020 return (-1); 2021 } 2022 2023 mtx_lock(&softc->ctl_lock); 2024 2025 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2026 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2027 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2028 iid = i; 2029 break; 2030 } 2031 if (name != NULL && port->wwpn_iid[i].name != NULL && 2032 strcmp(name, port->wwpn_iid[i].name) == 0) { 2033 iid = i; 2034 break; 2035 } 2036 } 2037 } 2038 2039 if (iid < 0) { 2040 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2041 if (port->wwpn_iid[i].in_use == 0 && 2042 port->wwpn_iid[i].wwpn == 0 && 2043 port->wwpn_iid[i].name == NULL) { 2044 iid = i; 2045 break; 2046 } 2047 } 2048 } 2049 2050 if (iid < 0) { 2051 best = -1; 2052 best_time = INT32_MAX; 2053 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2054 if (port->wwpn_iid[i].in_use == 0) { 2055 if (port->wwpn_iid[i].last_use < best_time) { 2056 best = i; 2057 best_time = port->wwpn_iid[i].last_use; 2058 } 2059 } 2060 } 2061 iid = best; 2062 } 2063 2064 if (iid < 0) { 2065 mtx_unlock(&softc->ctl_lock); 2066 free(name, M_CTL); 2067 return (-2); 2068 } 2069 2070 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2071 /* 2072 * This is not an error yet. 2073 */ 2074 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2075 #if 0 2076 printf("%s: port %d iid %u WWPN %#jx arrived" 2077 " again\n", __func__, port->targ_port, 2078 iid, (uintmax_t)wwpn); 2079 #endif 2080 goto take; 2081 } 2082 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2083 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2084 #if 0 2085 printf("%s: port %d iid %u name '%s' arrived" 2086 " again\n", __func__, port->targ_port, 2087 iid, name); 2088 #endif 2089 goto take; 2090 } 2091 2092 /* 2093 * This is an error, but what do we do about it? The 2094 * driver is telling us we have a new WWPN for this 2095 * initiator ID, so we pretty much need to use it. 2096 */ 2097 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2098 " but WWPN %#jx '%s' is still at that address\n", 2099 __func__, port->targ_port, iid, wwpn, name, 2100 (uintmax_t)port->wwpn_iid[iid].wwpn, 2101 port->wwpn_iid[iid].name); 2102 2103 /* 2104 * XXX KDM clear have_ca and ua_pending on each LUN for 2105 * this initiator. 2106 */ 2107 } 2108 take: 2109 free(port->wwpn_iid[iid].name, M_CTL); 2110 port->wwpn_iid[iid].name = name; 2111 port->wwpn_iid[iid].wwpn = wwpn; 2112 port->wwpn_iid[iid].in_use++; 2113 mtx_unlock(&softc->ctl_lock); 2114 ctl_isc_announce_iid(port, iid); 2115 2116 return (iid); 2117 } 2118 2119 static int 2120 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2121 { 2122 int len; 2123 2124 switch (port->port_type) { 2125 case CTL_PORT_FC: 2126 { 2127 struct scsi_transportid_fcp *id = 2128 (struct scsi_transportid_fcp *)buf; 2129 if (port->wwpn_iid[iid].wwpn == 0) 2130 return (0); 2131 memset(id, 0, sizeof(*id)); 2132 id->format_protocol = SCSI_PROTO_FC; 2133 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2134 return (sizeof(*id)); 2135 } 2136 case CTL_PORT_ISCSI: 2137 { 2138 struct scsi_transportid_iscsi_port *id = 2139 (struct scsi_transportid_iscsi_port *)buf; 2140 if (port->wwpn_iid[iid].name == NULL) 2141 return (0); 2142 memset(id, 0, 256); 2143 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2144 SCSI_PROTO_ISCSI; 2145 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2146 len = roundup2(min(len, 252), 4); 2147 scsi_ulto2b(len, id->additional_length); 2148 return (sizeof(*id) + len); 2149 } 2150 case CTL_PORT_SAS: 2151 { 2152 struct scsi_transportid_sas *id = 2153 (struct scsi_transportid_sas *)buf; 2154 if (port->wwpn_iid[iid].wwpn == 0) 2155 return (0); 2156 memset(id, 0, sizeof(*id)); 2157 id->format_protocol = SCSI_PROTO_SAS; 2158 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2159 return (sizeof(*id)); 2160 } 2161 default: 2162 { 2163 struct scsi_transportid_spi *id = 2164 (struct scsi_transportid_spi *)buf; 2165 memset(id, 0, sizeof(*id)); 2166 id->format_protocol = SCSI_PROTO_SPI; 2167 scsi_ulto2b(iid, id->scsi_addr); 2168 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2169 return (sizeof(*id)); 2170 } 2171 } 2172 } 2173 2174 /* 2175 * Serialize a command that went down the "wrong" side, and so was sent to 2176 * this controller for execution. The logic is a little different than the 2177 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2178 * sent back to the other side, but in the success case, we execute the 2179 * command on this side (XFER mode) or tell the other side to execute it 2180 * (SER_ONLY mode). 2181 */ 2182 static int 2183 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2184 { 2185 struct ctl_softc *softc = control_softc; 2186 union ctl_ha_msg msg_info; 2187 struct ctl_port *port; 2188 struct ctl_lun *lun; 2189 const struct ctl_cmd_entry *entry; 2190 int retval = 0; 2191 uint32_t targ_lun; 2192 2193 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2194 mtx_lock(&softc->ctl_lock); 2195 2196 /* Make sure that we know about this port. */ 2197 port = ctl_io_port(&ctsio->io_hdr); 2198 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2199 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2200 /*retry_count*/ 1); 2201 goto badjuju; 2202 } 2203 2204 /* Make sure that we know about this LUN. */ 2205 if ((targ_lun < CTL_MAX_LUNS) && 2206 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 2207 mtx_lock(&lun->lun_lock); 2208 mtx_unlock(&softc->ctl_lock); 2209 /* 2210 * If the LUN is invalid, pretend that it doesn't exist. 2211 * It will go away as soon as all pending I/O has been 2212 * completed. 2213 */ 2214 if (lun->flags & CTL_LUN_DISABLED) { 2215 mtx_unlock(&lun->lun_lock); 2216 lun = NULL; 2217 } 2218 } else { 2219 mtx_unlock(&softc->ctl_lock); 2220 lun = NULL; 2221 } 2222 if (lun == NULL) { 2223 /* 2224 * The other node would not send this request to us unless 2225 * received announce that we are primary node for this LUN. 2226 * If this LUN does not exist now, it is probably result of 2227 * a race, so respond to initiator in the most opaque way. 2228 */ 2229 ctl_set_busy(ctsio); 2230 goto badjuju; 2231 } 2232 2233 entry = ctl_get_cmd_entry(ctsio, NULL); 2234 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2235 mtx_unlock(&lun->lun_lock); 2236 goto badjuju; 2237 } 2238 2239 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 2240 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun; 2241 2242 /* 2243 * Every I/O goes into the OOA queue for a 2244 * particular LUN, and stays there until completion. 2245 */ 2246 #ifdef CTL_TIME_IO 2247 if (TAILQ_EMPTY(&lun->ooa_queue)) 2248 lun->idle_time += getsbinuptime() - lun->last_busy; 2249 #endif 2250 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2251 2252 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2253 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2254 ooa_links))) { 2255 case CTL_ACTION_BLOCK: 2256 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2257 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2258 blocked_links); 2259 mtx_unlock(&lun->lun_lock); 2260 break; 2261 case CTL_ACTION_PASS: 2262 case CTL_ACTION_SKIP: 2263 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2264 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2265 ctl_enqueue_rtr((union ctl_io *)ctsio); 2266 mtx_unlock(&lun->lun_lock); 2267 } else { 2268 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2269 mtx_unlock(&lun->lun_lock); 2270 2271 /* send msg back to other side */ 2272 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2273 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2274 msg_info.hdr.msg_type = CTL_MSG_R2R; 2275 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2276 sizeof(msg_info.hdr), M_WAITOK); 2277 } 2278 break; 2279 case CTL_ACTION_OVERLAP: 2280 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2281 mtx_unlock(&lun->lun_lock); 2282 ctl_set_overlapped_cmd(ctsio); 2283 goto badjuju; 2284 case CTL_ACTION_OVERLAP_TAG: 2285 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2286 mtx_unlock(&lun->lun_lock); 2287 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2288 goto badjuju; 2289 case CTL_ACTION_ERROR: 2290 default: 2291 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2292 mtx_unlock(&lun->lun_lock); 2293 2294 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2295 /*retry_count*/ 0); 2296 badjuju: 2297 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2298 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2299 msg_info.hdr.serializing_sc = NULL; 2300 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2301 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2302 sizeof(msg_info.scsi), M_WAITOK); 2303 retval = 1; 2304 break; 2305 } 2306 return (retval); 2307 } 2308 2309 /* 2310 * Returns 0 for success, errno for failure. 2311 */ 2312 static void 2313 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2314 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2315 { 2316 union ctl_io *io; 2317 2318 mtx_lock(&lun->lun_lock); 2319 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2320 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2321 ooa_links)) { 2322 struct ctl_ooa_entry *entry; 2323 2324 /* 2325 * If we've got more than we can fit, just count the 2326 * remaining entries. 2327 */ 2328 if (*cur_fill_num >= ooa_hdr->alloc_num) 2329 continue; 2330 2331 entry = &kern_entries[*cur_fill_num]; 2332 2333 entry->tag_num = io->scsiio.tag_num; 2334 entry->lun_num = lun->lun; 2335 #ifdef CTL_TIME_IO 2336 entry->start_bt = io->io_hdr.start_bt; 2337 #endif 2338 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2339 entry->cdb_len = io->scsiio.cdb_len; 2340 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2341 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2342 2343 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2344 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2345 2346 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2347 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2348 2349 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2350 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2351 2352 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2353 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2354 } 2355 mtx_unlock(&lun->lun_lock); 2356 } 2357 2358 static void * 2359 ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2360 size_t error_str_len) 2361 { 2362 void *kptr; 2363 2364 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2365 2366 if (copyin(user_addr, kptr, len) != 0) { 2367 snprintf(error_str, error_str_len, "Error copying %d bytes " 2368 "from user address %p to kernel address %p", len, 2369 user_addr, kptr); 2370 free(kptr, M_CTL); 2371 return (NULL); 2372 } 2373 2374 return (kptr); 2375 } 2376 2377 static void 2378 ctl_free_args(int num_args, struct ctl_be_arg *args) 2379 { 2380 int i; 2381 2382 if (args == NULL) 2383 return; 2384 2385 for (i = 0; i < num_args; i++) { 2386 free(args[i].kname, M_CTL); 2387 free(args[i].kvalue, M_CTL); 2388 } 2389 2390 free(args, M_CTL); 2391 } 2392 2393 static struct ctl_be_arg * 2394 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2395 char *error_str, size_t error_str_len) 2396 { 2397 struct ctl_be_arg *args; 2398 int i; 2399 2400 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2401 error_str, error_str_len); 2402 2403 if (args == NULL) 2404 goto bailout; 2405 2406 for (i = 0; i < num_args; i++) { 2407 args[i].kname = NULL; 2408 args[i].kvalue = NULL; 2409 } 2410 2411 for (i = 0; i < num_args; i++) { 2412 uint8_t *tmpptr; 2413 2414 args[i].kname = ctl_copyin_alloc(args[i].name, 2415 args[i].namelen, error_str, error_str_len); 2416 if (args[i].kname == NULL) 2417 goto bailout; 2418 2419 if (args[i].kname[args[i].namelen - 1] != '\0') { 2420 snprintf(error_str, error_str_len, "Argument %d " 2421 "name is not NUL-terminated", i); 2422 goto bailout; 2423 } 2424 2425 if (args[i].flags & CTL_BEARG_RD) { 2426 tmpptr = ctl_copyin_alloc(args[i].value, 2427 args[i].vallen, error_str, error_str_len); 2428 if (tmpptr == NULL) 2429 goto bailout; 2430 if ((args[i].flags & CTL_BEARG_ASCII) 2431 && (tmpptr[args[i].vallen - 1] != '\0')) { 2432 snprintf(error_str, error_str_len, "Argument " 2433 "%d value is not NUL-terminated", i); 2434 goto bailout; 2435 } 2436 args[i].kvalue = tmpptr; 2437 } else { 2438 args[i].kvalue = malloc(args[i].vallen, 2439 M_CTL, M_WAITOK | M_ZERO); 2440 } 2441 } 2442 2443 return (args); 2444 bailout: 2445 2446 ctl_free_args(num_args, args); 2447 2448 return (NULL); 2449 } 2450 2451 static void 2452 ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2453 { 2454 int i; 2455 2456 for (i = 0; i < num_args; i++) { 2457 if (args[i].flags & CTL_BEARG_WR) 2458 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2459 } 2460 } 2461 2462 /* 2463 * Escape characters that are illegal or not recommended in XML. 2464 */ 2465 int 2466 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2467 { 2468 char *end = str + size; 2469 int retval; 2470 2471 retval = 0; 2472 2473 for (; *str && str < end; str++) { 2474 switch (*str) { 2475 case '&': 2476 retval = sbuf_printf(sb, "&"); 2477 break; 2478 case '>': 2479 retval = sbuf_printf(sb, ">"); 2480 break; 2481 case '<': 2482 retval = sbuf_printf(sb, "<"); 2483 break; 2484 default: 2485 retval = sbuf_putc(sb, *str); 2486 break; 2487 } 2488 2489 if (retval != 0) 2490 break; 2491 2492 } 2493 2494 return (retval); 2495 } 2496 2497 static void 2498 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2499 { 2500 struct scsi_vpd_id_descriptor *desc; 2501 int i; 2502 2503 if (id == NULL || id->len < 4) 2504 return; 2505 desc = (struct scsi_vpd_id_descriptor *)id->data; 2506 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2507 case SVPD_ID_TYPE_T10: 2508 sbuf_printf(sb, "t10."); 2509 break; 2510 case SVPD_ID_TYPE_EUI64: 2511 sbuf_printf(sb, "eui."); 2512 break; 2513 case SVPD_ID_TYPE_NAA: 2514 sbuf_printf(sb, "naa."); 2515 break; 2516 case SVPD_ID_TYPE_SCSI_NAME: 2517 break; 2518 } 2519 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2520 case SVPD_ID_CODESET_BINARY: 2521 for (i = 0; i < desc->length; i++) 2522 sbuf_printf(sb, "%02x", desc->identifier[i]); 2523 break; 2524 case SVPD_ID_CODESET_ASCII: 2525 sbuf_printf(sb, "%.*s", (int)desc->length, 2526 (char *)desc->identifier); 2527 break; 2528 case SVPD_ID_CODESET_UTF8: 2529 sbuf_printf(sb, "%s", (char *)desc->identifier); 2530 break; 2531 } 2532 } 2533 2534 static int 2535 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2536 struct thread *td) 2537 { 2538 struct ctl_softc *softc = dev->si_drv1; 2539 struct ctl_lun *lun; 2540 int retval; 2541 2542 retval = 0; 2543 2544 switch (cmd) { 2545 case CTL_IO: 2546 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2547 break; 2548 case CTL_ENABLE_PORT: 2549 case CTL_DISABLE_PORT: 2550 case CTL_SET_PORT_WWNS: { 2551 struct ctl_port *port; 2552 struct ctl_port_entry *entry; 2553 2554 entry = (struct ctl_port_entry *)addr; 2555 2556 mtx_lock(&softc->ctl_lock); 2557 STAILQ_FOREACH(port, &softc->port_list, links) { 2558 int action, done; 2559 2560 if (port->targ_port < softc->port_min || 2561 port->targ_port >= softc->port_max) 2562 continue; 2563 2564 action = 0; 2565 done = 0; 2566 if ((entry->port_type == CTL_PORT_NONE) 2567 && (entry->targ_port == port->targ_port)) { 2568 /* 2569 * If the user only wants to enable or 2570 * disable or set WWNs on a specific port, 2571 * do the operation and we're done. 2572 */ 2573 action = 1; 2574 done = 1; 2575 } else if (entry->port_type & port->port_type) { 2576 /* 2577 * Compare the user's type mask with the 2578 * particular frontend type to see if we 2579 * have a match. 2580 */ 2581 action = 1; 2582 done = 0; 2583 2584 /* 2585 * Make sure the user isn't trying to set 2586 * WWNs on multiple ports at the same time. 2587 */ 2588 if (cmd == CTL_SET_PORT_WWNS) { 2589 printf("%s: Can't set WWNs on " 2590 "multiple ports\n", __func__); 2591 retval = EINVAL; 2592 break; 2593 } 2594 } 2595 if (action == 0) 2596 continue; 2597 2598 /* 2599 * XXX KDM we have to drop the lock here, because 2600 * the online/offline operations can potentially 2601 * block. We need to reference count the frontends 2602 * so they can't go away, 2603 */ 2604 if (cmd == CTL_ENABLE_PORT) { 2605 mtx_unlock(&softc->ctl_lock); 2606 ctl_port_online(port); 2607 mtx_lock(&softc->ctl_lock); 2608 } else if (cmd == CTL_DISABLE_PORT) { 2609 mtx_unlock(&softc->ctl_lock); 2610 ctl_port_offline(port); 2611 mtx_lock(&softc->ctl_lock); 2612 } else if (cmd == CTL_SET_PORT_WWNS) { 2613 ctl_port_set_wwns(port, 2614 (entry->flags & CTL_PORT_WWNN_VALID) ? 2615 1 : 0, entry->wwnn, 2616 (entry->flags & CTL_PORT_WWPN_VALID) ? 2617 1 : 0, entry->wwpn); 2618 } 2619 if (done != 0) 2620 break; 2621 } 2622 mtx_unlock(&softc->ctl_lock); 2623 break; 2624 } 2625 case CTL_GET_OOA: { 2626 struct ctl_ooa *ooa_hdr; 2627 struct ctl_ooa_entry *entries; 2628 uint32_t cur_fill_num; 2629 2630 ooa_hdr = (struct ctl_ooa *)addr; 2631 2632 if ((ooa_hdr->alloc_len == 0) 2633 || (ooa_hdr->alloc_num == 0)) { 2634 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2635 "must be non-zero\n", __func__, 2636 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2637 retval = EINVAL; 2638 break; 2639 } 2640 2641 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2642 sizeof(struct ctl_ooa_entry))) { 2643 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2644 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2645 __func__, ooa_hdr->alloc_len, 2646 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2647 retval = EINVAL; 2648 break; 2649 } 2650 2651 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2652 if (entries == NULL) { 2653 printf("%s: could not allocate %d bytes for OOA " 2654 "dump\n", __func__, ooa_hdr->alloc_len); 2655 retval = ENOMEM; 2656 break; 2657 } 2658 2659 mtx_lock(&softc->ctl_lock); 2660 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2661 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2662 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2663 mtx_unlock(&softc->ctl_lock); 2664 free(entries, M_CTL); 2665 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2666 __func__, (uintmax_t)ooa_hdr->lun_num); 2667 retval = EINVAL; 2668 break; 2669 } 2670 2671 cur_fill_num = 0; 2672 2673 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2674 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2675 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2676 ooa_hdr, entries); 2677 } 2678 } else { 2679 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2680 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2681 entries); 2682 } 2683 mtx_unlock(&softc->ctl_lock); 2684 2685 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2686 ooa_hdr->fill_len = ooa_hdr->fill_num * 2687 sizeof(struct ctl_ooa_entry); 2688 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2689 if (retval != 0) { 2690 printf("%s: error copying out %d bytes for OOA dump\n", 2691 __func__, ooa_hdr->fill_len); 2692 } 2693 2694 getbinuptime(&ooa_hdr->cur_bt); 2695 2696 if (cur_fill_num > ooa_hdr->alloc_num) { 2697 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2698 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2699 } else { 2700 ooa_hdr->dropped_num = 0; 2701 ooa_hdr->status = CTL_OOA_OK; 2702 } 2703 2704 free(entries, M_CTL); 2705 break; 2706 } 2707 case CTL_DELAY_IO: { 2708 struct ctl_io_delay_info *delay_info; 2709 2710 delay_info = (struct ctl_io_delay_info *)addr; 2711 2712 #ifdef CTL_IO_DELAY 2713 mtx_lock(&softc->ctl_lock); 2714 2715 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2716 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2717 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2718 } else { 2719 lun = softc->ctl_luns[delay_info->lun_id]; 2720 mtx_lock(&lun->lun_lock); 2721 2722 delay_info->status = CTL_DELAY_STATUS_OK; 2723 2724 switch (delay_info->delay_type) { 2725 case CTL_DELAY_TYPE_CONT: 2726 break; 2727 case CTL_DELAY_TYPE_ONESHOT: 2728 break; 2729 default: 2730 delay_info->status = 2731 CTL_DELAY_STATUS_INVALID_TYPE; 2732 break; 2733 } 2734 2735 switch (delay_info->delay_loc) { 2736 case CTL_DELAY_LOC_DATAMOVE: 2737 lun->delay_info.datamove_type = 2738 delay_info->delay_type; 2739 lun->delay_info.datamove_delay = 2740 delay_info->delay_secs; 2741 break; 2742 case CTL_DELAY_LOC_DONE: 2743 lun->delay_info.done_type = 2744 delay_info->delay_type; 2745 lun->delay_info.done_delay = 2746 delay_info->delay_secs; 2747 break; 2748 default: 2749 delay_info->status = 2750 CTL_DELAY_STATUS_INVALID_LOC; 2751 break; 2752 } 2753 mtx_unlock(&lun->lun_lock); 2754 } 2755 2756 mtx_unlock(&softc->ctl_lock); 2757 #else 2758 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2759 #endif /* CTL_IO_DELAY */ 2760 break; 2761 } 2762 case CTL_GETSTATS: { 2763 struct ctl_stats *stats; 2764 int i; 2765 2766 stats = (struct ctl_stats *)addr; 2767 2768 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2769 stats->alloc_len) { 2770 stats->status = CTL_SS_NEED_MORE_SPACE; 2771 stats->num_luns = softc->num_luns; 2772 break; 2773 } 2774 /* 2775 * XXX KDM no locking here. If the LUN list changes, 2776 * things can blow up. 2777 */ 2778 i = 0; 2779 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2780 retval = copyout(&lun->stats, &stats->lun_stats[i++], 2781 sizeof(lun->stats)); 2782 if (retval != 0) 2783 break; 2784 } 2785 stats->num_luns = softc->num_luns; 2786 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2787 softc->num_luns; 2788 stats->status = CTL_SS_OK; 2789 #ifdef CTL_TIME_IO 2790 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2791 #else 2792 stats->flags = CTL_STATS_FLAG_NONE; 2793 #endif 2794 getnanouptime(&stats->timestamp); 2795 break; 2796 } 2797 case CTL_ERROR_INJECT: { 2798 struct ctl_error_desc *err_desc, *new_err_desc; 2799 2800 err_desc = (struct ctl_error_desc *)addr; 2801 2802 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2803 M_WAITOK | M_ZERO); 2804 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2805 2806 mtx_lock(&softc->ctl_lock); 2807 lun = softc->ctl_luns[err_desc->lun_id]; 2808 if (lun == NULL) { 2809 mtx_unlock(&softc->ctl_lock); 2810 free(new_err_desc, M_CTL); 2811 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2812 __func__, (uintmax_t)err_desc->lun_id); 2813 retval = EINVAL; 2814 break; 2815 } 2816 mtx_lock(&lun->lun_lock); 2817 mtx_unlock(&softc->ctl_lock); 2818 2819 /* 2820 * We could do some checking here to verify the validity 2821 * of the request, but given the complexity of error 2822 * injection requests, the checking logic would be fairly 2823 * complex. 2824 * 2825 * For now, if the request is invalid, it just won't get 2826 * executed and might get deleted. 2827 */ 2828 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2829 2830 /* 2831 * XXX KDM check to make sure the serial number is unique, 2832 * in case we somehow manage to wrap. That shouldn't 2833 * happen for a very long time, but it's the right thing to 2834 * do. 2835 */ 2836 new_err_desc->serial = lun->error_serial; 2837 err_desc->serial = lun->error_serial; 2838 lun->error_serial++; 2839 2840 mtx_unlock(&lun->lun_lock); 2841 break; 2842 } 2843 case CTL_ERROR_INJECT_DELETE: { 2844 struct ctl_error_desc *delete_desc, *desc, *desc2; 2845 int delete_done; 2846 2847 delete_desc = (struct ctl_error_desc *)addr; 2848 delete_done = 0; 2849 2850 mtx_lock(&softc->ctl_lock); 2851 lun = softc->ctl_luns[delete_desc->lun_id]; 2852 if (lun == NULL) { 2853 mtx_unlock(&softc->ctl_lock); 2854 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2855 __func__, (uintmax_t)delete_desc->lun_id); 2856 retval = EINVAL; 2857 break; 2858 } 2859 mtx_lock(&lun->lun_lock); 2860 mtx_unlock(&softc->ctl_lock); 2861 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2862 if (desc->serial != delete_desc->serial) 2863 continue; 2864 2865 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2866 links); 2867 free(desc, M_CTL); 2868 delete_done = 1; 2869 } 2870 mtx_unlock(&lun->lun_lock); 2871 if (delete_done == 0) { 2872 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2873 "error serial %ju on LUN %u\n", __func__, 2874 delete_desc->serial, delete_desc->lun_id); 2875 retval = EINVAL; 2876 break; 2877 } 2878 break; 2879 } 2880 case CTL_DUMP_STRUCTS: { 2881 int i, j, k; 2882 struct ctl_port *port; 2883 struct ctl_frontend *fe; 2884 2885 mtx_lock(&softc->ctl_lock); 2886 printf("CTL Persistent Reservation information start:\n"); 2887 for (i = 0; i < CTL_MAX_LUNS; i++) { 2888 lun = softc->ctl_luns[i]; 2889 2890 if ((lun == NULL) 2891 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2892 continue; 2893 2894 for (j = 0; j < CTL_MAX_PORTS; j++) { 2895 if (lun->pr_keys[j] == NULL) 2896 continue; 2897 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2898 if (lun->pr_keys[j][k] == 0) 2899 continue; 2900 printf(" LUN %d port %d iid %d key " 2901 "%#jx\n", i, j, k, 2902 (uintmax_t)lun->pr_keys[j][k]); 2903 } 2904 } 2905 } 2906 printf("CTL Persistent Reservation information end\n"); 2907 printf("CTL Ports:\n"); 2908 STAILQ_FOREACH(port, &softc->port_list, links) { 2909 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2910 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2911 port->frontend->name, port->port_type, 2912 port->physical_port, port->virtual_port, 2913 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2914 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2915 if (port->wwpn_iid[j].in_use == 0 && 2916 port->wwpn_iid[j].wwpn == 0 && 2917 port->wwpn_iid[j].name == NULL) 2918 continue; 2919 2920 printf(" iid %u use %d WWPN %#jx '%s'\n", 2921 j, port->wwpn_iid[j].in_use, 2922 (uintmax_t)port->wwpn_iid[j].wwpn, 2923 port->wwpn_iid[j].name); 2924 } 2925 } 2926 printf("CTL Port information end\n"); 2927 mtx_unlock(&softc->ctl_lock); 2928 /* 2929 * XXX KDM calling this without a lock. We'd likely want 2930 * to drop the lock before calling the frontend's dump 2931 * routine anyway. 2932 */ 2933 printf("CTL Frontends:\n"); 2934 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2935 printf(" Frontend '%s'\n", fe->name); 2936 if (fe->fe_dump != NULL) 2937 fe->fe_dump(); 2938 } 2939 printf("CTL Frontend information end\n"); 2940 break; 2941 } 2942 case CTL_LUN_REQ: { 2943 struct ctl_lun_req *lun_req; 2944 struct ctl_backend_driver *backend; 2945 2946 lun_req = (struct ctl_lun_req *)addr; 2947 2948 backend = ctl_backend_find(lun_req->backend); 2949 if (backend == NULL) { 2950 lun_req->status = CTL_LUN_ERROR; 2951 snprintf(lun_req->error_str, 2952 sizeof(lun_req->error_str), 2953 "Backend \"%s\" not found.", 2954 lun_req->backend); 2955 break; 2956 } 2957 if (lun_req->num_be_args > 0) { 2958 lun_req->kern_be_args = ctl_copyin_args( 2959 lun_req->num_be_args, 2960 lun_req->be_args, 2961 lun_req->error_str, 2962 sizeof(lun_req->error_str)); 2963 if (lun_req->kern_be_args == NULL) { 2964 lun_req->status = CTL_LUN_ERROR; 2965 break; 2966 } 2967 } 2968 2969 retval = backend->ioctl(dev, cmd, addr, flag, td); 2970 2971 if (lun_req->num_be_args > 0) { 2972 ctl_copyout_args(lun_req->num_be_args, 2973 lun_req->kern_be_args); 2974 ctl_free_args(lun_req->num_be_args, 2975 lun_req->kern_be_args); 2976 } 2977 break; 2978 } 2979 case CTL_LUN_LIST: { 2980 struct sbuf *sb; 2981 struct ctl_lun_list *list; 2982 struct ctl_option *opt; 2983 2984 list = (struct ctl_lun_list *)addr; 2985 2986 /* 2987 * Allocate a fixed length sbuf here, based on the length 2988 * of the user's buffer. We could allocate an auto-extending 2989 * buffer, and then tell the user how much larger our 2990 * amount of data is than his buffer, but that presents 2991 * some problems: 2992 * 2993 * 1. The sbuf(9) routines use a blocking malloc, and so 2994 * we can't hold a lock while calling them with an 2995 * auto-extending buffer. 2996 * 2997 * 2. There is not currently a LUN reference counting 2998 * mechanism, outside of outstanding transactions on 2999 * the LUN's OOA queue. So a LUN could go away on us 3000 * while we're getting the LUN number, backend-specific 3001 * information, etc. Thus, given the way things 3002 * currently work, we need to hold the CTL lock while 3003 * grabbing LUN information. 3004 * 3005 * So, from the user's standpoint, the best thing to do is 3006 * allocate what he thinks is a reasonable buffer length, 3007 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3008 * double the buffer length and try again. (And repeat 3009 * that until he succeeds.) 3010 */ 3011 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3012 if (sb == NULL) { 3013 list->status = CTL_LUN_LIST_ERROR; 3014 snprintf(list->error_str, sizeof(list->error_str), 3015 "Unable to allocate %d bytes for LUN list", 3016 list->alloc_len); 3017 break; 3018 } 3019 3020 sbuf_printf(sb, "<ctllunlist>\n"); 3021 3022 mtx_lock(&softc->ctl_lock); 3023 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3024 mtx_lock(&lun->lun_lock); 3025 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3026 (uintmax_t)lun->lun); 3027 3028 /* 3029 * Bail out as soon as we see that we've overfilled 3030 * the buffer. 3031 */ 3032 if (retval != 0) 3033 break; 3034 3035 retval = sbuf_printf(sb, "\t<backend_type>%s" 3036 "</backend_type>\n", 3037 (lun->backend == NULL) ? "none" : 3038 lun->backend->name); 3039 3040 if (retval != 0) 3041 break; 3042 3043 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3044 lun->be_lun->lun_type); 3045 3046 if (retval != 0) 3047 break; 3048 3049 if (lun->backend == NULL) { 3050 retval = sbuf_printf(sb, "</lun>\n"); 3051 if (retval != 0) 3052 break; 3053 continue; 3054 } 3055 3056 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3057 (lun->be_lun->maxlba > 0) ? 3058 lun->be_lun->maxlba + 1 : 0); 3059 3060 if (retval != 0) 3061 break; 3062 3063 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3064 lun->be_lun->blocksize); 3065 3066 if (retval != 0) 3067 break; 3068 3069 retval = sbuf_printf(sb, "\t<serial_number>"); 3070 3071 if (retval != 0) 3072 break; 3073 3074 retval = ctl_sbuf_printf_esc(sb, 3075 lun->be_lun->serial_num, 3076 sizeof(lun->be_lun->serial_num)); 3077 3078 if (retval != 0) 3079 break; 3080 3081 retval = sbuf_printf(sb, "</serial_number>\n"); 3082 3083 if (retval != 0) 3084 break; 3085 3086 retval = sbuf_printf(sb, "\t<device_id>"); 3087 3088 if (retval != 0) 3089 break; 3090 3091 retval = ctl_sbuf_printf_esc(sb, 3092 lun->be_lun->device_id, 3093 sizeof(lun->be_lun->device_id)); 3094 3095 if (retval != 0) 3096 break; 3097 3098 retval = sbuf_printf(sb, "</device_id>\n"); 3099 3100 if (retval != 0) 3101 break; 3102 3103 if (lun->backend->lun_info != NULL) { 3104 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3105 if (retval != 0) 3106 break; 3107 } 3108 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3109 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3110 opt->name, opt->value, opt->name); 3111 if (retval != 0) 3112 break; 3113 } 3114 3115 retval = sbuf_printf(sb, "</lun>\n"); 3116 3117 if (retval != 0) 3118 break; 3119 mtx_unlock(&lun->lun_lock); 3120 } 3121 if (lun != NULL) 3122 mtx_unlock(&lun->lun_lock); 3123 mtx_unlock(&softc->ctl_lock); 3124 3125 if ((retval != 0) 3126 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3127 retval = 0; 3128 sbuf_delete(sb); 3129 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3130 snprintf(list->error_str, sizeof(list->error_str), 3131 "Out of space, %d bytes is too small", 3132 list->alloc_len); 3133 break; 3134 } 3135 3136 sbuf_finish(sb); 3137 3138 retval = copyout(sbuf_data(sb), list->lun_xml, 3139 sbuf_len(sb) + 1); 3140 3141 list->fill_len = sbuf_len(sb) + 1; 3142 list->status = CTL_LUN_LIST_OK; 3143 sbuf_delete(sb); 3144 break; 3145 } 3146 case CTL_ISCSI: { 3147 struct ctl_iscsi *ci; 3148 struct ctl_frontend *fe; 3149 3150 ci = (struct ctl_iscsi *)addr; 3151 3152 fe = ctl_frontend_find("iscsi"); 3153 if (fe == NULL) { 3154 ci->status = CTL_ISCSI_ERROR; 3155 snprintf(ci->error_str, sizeof(ci->error_str), 3156 "Frontend \"iscsi\" not found."); 3157 break; 3158 } 3159 3160 retval = fe->ioctl(dev, cmd, addr, flag, td); 3161 break; 3162 } 3163 case CTL_PORT_REQ: { 3164 struct ctl_req *req; 3165 struct ctl_frontend *fe; 3166 3167 req = (struct ctl_req *)addr; 3168 3169 fe = ctl_frontend_find(req->driver); 3170 if (fe == NULL) { 3171 req->status = CTL_LUN_ERROR; 3172 snprintf(req->error_str, sizeof(req->error_str), 3173 "Frontend \"%s\" not found.", req->driver); 3174 break; 3175 } 3176 if (req->num_args > 0) { 3177 req->kern_args = ctl_copyin_args(req->num_args, 3178 req->args, req->error_str, sizeof(req->error_str)); 3179 if (req->kern_args == NULL) { 3180 req->status = CTL_LUN_ERROR; 3181 break; 3182 } 3183 } 3184 3185 if (fe->ioctl) 3186 retval = fe->ioctl(dev, cmd, addr, flag, td); 3187 else 3188 retval = ENODEV; 3189 3190 if (req->num_args > 0) { 3191 ctl_copyout_args(req->num_args, req->kern_args); 3192 ctl_free_args(req->num_args, req->kern_args); 3193 } 3194 break; 3195 } 3196 case CTL_PORT_LIST: { 3197 struct sbuf *sb; 3198 struct ctl_port *port; 3199 struct ctl_lun_list *list; 3200 struct ctl_option *opt; 3201 int j; 3202 uint32_t plun; 3203 3204 list = (struct ctl_lun_list *)addr; 3205 3206 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3207 if (sb == NULL) { 3208 list->status = CTL_LUN_LIST_ERROR; 3209 snprintf(list->error_str, sizeof(list->error_str), 3210 "Unable to allocate %d bytes for LUN list", 3211 list->alloc_len); 3212 break; 3213 } 3214 3215 sbuf_printf(sb, "<ctlportlist>\n"); 3216 3217 mtx_lock(&softc->ctl_lock); 3218 STAILQ_FOREACH(port, &softc->port_list, links) { 3219 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3220 (uintmax_t)port->targ_port); 3221 3222 /* 3223 * Bail out as soon as we see that we've overfilled 3224 * the buffer. 3225 */ 3226 if (retval != 0) 3227 break; 3228 3229 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3230 "</frontend_type>\n", port->frontend->name); 3231 if (retval != 0) 3232 break; 3233 3234 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3235 port->port_type); 3236 if (retval != 0) 3237 break; 3238 3239 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3240 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3241 if (retval != 0) 3242 break; 3243 3244 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3245 port->port_name); 3246 if (retval != 0) 3247 break; 3248 3249 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3250 port->physical_port); 3251 if (retval != 0) 3252 break; 3253 3254 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3255 port->virtual_port); 3256 if (retval != 0) 3257 break; 3258 3259 if (port->target_devid != NULL) { 3260 sbuf_printf(sb, "\t<target>"); 3261 ctl_id_sbuf(port->target_devid, sb); 3262 sbuf_printf(sb, "</target>\n"); 3263 } 3264 3265 if (port->port_devid != NULL) { 3266 sbuf_printf(sb, "\t<port>"); 3267 ctl_id_sbuf(port->port_devid, sb); 3268 sbuf_printf(sb, "</port>\n"); 3269 } 3270 3271 if (port->port_info != NULL) { 3272 retval = port->port_info(port->onoff_arg, sb); 3273 if (retval != 0) 3274 break; 3275 } 3276 STAILQ_FOREACH(opt, &port->options, links) { 3277 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3278 opt->name, opt->value, opt->name); 3279 if (retval != 0) 3280 break; 3281 } 3282 3283 if (port->lun_map != NULL) { 3284 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3285 for (j = 0; j < CTL_MAX_LUNS; j++) { 3286 plun = ctl_lun_map_from_port(port, j); 3287 if (plun >= CTL_MAX_LUNS) 3288 continue; 3289 sbuf_printf(sb, 3290 "\t<lun id=\"%u\">%u</lun>\n", 3291 j, plun); 3292 } 3293 } 3294 3295 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3296 if (port->wwpn_iid[j].in_use == 0 || 3297 (port->wwpn_iid[j].wwpn == 0 && 3298 port->wwpn_iid[j].name == NULL)) 3299 continue; 3300 3301 if (port->wwpn_iid[j].name != NULL) 3302 retval = sbuf_printf(sb, 3303 "\t<initiator id=\"%u\">%s</initiator>\n", 3304 j, port->wwpn_iid[j].name); 3305 else 3306 retval = sbuf_printf(sb, 3307 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3308 j, port->wwpn_iid[j].wwpn); 3309 if (retval != 0) 3310 break; 3311 } 3312 if (retval != 0) 3313 break; 3314 3315 retval = sbuf_printf(sb, "</targ_port>\n"); 3316 if (retval != 0) 3317 break; 3318 } 3319 mtx_unlock(&softc->ctl_lock); 3320 3321 if ((retval != 0) 3322 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3323 retval = 0; 3324 sbuf_delete(sb); 3325 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3326 snprintf(list->error_str, sizeof(list->error_str), 3327 "Out of space, %d bytes is too small", 3328 list->alloc_len); 3329 break; 3330 } 3331 3332 sbuf_finish(sb); 3333 3334 retval = copyout(sbuf_data(sb), list->lun_xml, 3335 sbuf_len(sb) + 1); 3336 3337 list->fill_len = sbuf_len(sb) + 1; 3338 list->status = CTL_LUN_LIST_OK; 3339 sbuf_delete(sb); 3340 break; 3341 } 3342 case CTL_LUN_MAP: { 3343 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3344 struct ctl_port *port; 3345 3346 mtx_lock(&softc->ctl_lock); 3347 if (lm->port < softc->port_min || 3348 lm->port >= softc->port_max || 3349 (port = softc->ctl_ports[lm->port]) == NULL) { 3350 mtx_unlock(&softc->ctl_lock); 3351 return (ENXIO); 3352 } 3353 if (port->status & CTL_PORT_STATUS_ONLINE) { 3354 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3355 if (ctl_lun_map_to_port(port, lun->lun) >= 3356 CTL_MAX_LUNS) 3357 continue; 3358 mtx_lock(&lun->lun_lock); 3359 ctl_est_ua_port(lun, lm->port, -1, 3360 CTL_UA_LUN_CHANGE); 3361 mtx_unlock(&lun->lun_lock); 3362 } 3363 } 3364 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3365 if (lm->plun < CTL_MAX_LUNS) { 3366 if (lm->lun == UINT32_MAX) 3367 retval = ctl_lun_map_unset(port, lm->plun); 3368 else if (lm->lun < CTL_MAX_LUNS && 3369 softc->ctl_luns[lm->lun] != NULL) 3370 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3371 else 3372 return (ENXIO); 3373 } else if (lm->plun == UINT32_MAX) { 3374 if (lm->lun == UINT32_MAX) 3375 retval = ctl_lun_map_deinit(port); 3376 else 3377 retval = ctl_lun_map_init(port); 3378 } else 3379 return (ENXIO); 3380 if (port->status & CTL_PORT_STATUS_ONLINE) 3381 ctl_isc_announce_port(port); 3382 break; 3383 } 3384 default: { 3385 /* XXX KDM should we fix this? */ 3386 #if 0 3387 struct ctl_backend_driver *backend; 3388 unsigned int type; 3389 int found; 3390 3391 found = 0; 3392 3393 /* 3394 * We encode the backend type as the ioctl type for backend 3395 * ioctls. So parse it out here, and then search for a 3396 * backend of this type. 3397 */ 3398 type = _IOC_TYPE(cmd); 3399 3400 STAILQ_FOREACH(backend, &softc->be_list, links) { 3401 if (backend->type == type) { 3402 found = 1; 3403 break; 3404 } 3405 } 3406 if (found == 0) { 3407 printf("ctl: unknown ioctl command %#lx or backend " 3408 "%d\n", cmd, type); 3409 retval = EINVAL; 3410 break; 3411 } 3412 retval = backend->ioctl(dev, cmd, addr, flag, td); 3413 #endif 3414 retval = ENOTTY; 3415 break; 3416 } 3417 } 3418 return (retval); 3419 } 3420 3421 uint32_t 3422 ctl_get_initindex(struct ctl_nexus *nexus) 3423 { 3424 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3425 } 3426 3427 int 3428 ctl_lun_map_init(struct ctl_port *port) 3429 { 3430 struct ctl_softc *softc = port->ctl_softc; 3431 struct ctl_lun *lun; 3432 uint32_t i; 3433 3434 if (port->lun_map == NULL) 3435 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3436 M_CTL, M_NOWAIT); 3437 if (port->lun_map == NULL) 3438 return (ENOMEM); 3439 for (i = 0; i < CTL_MAX_LUNS; i++) 3440 port->lun_map[i] = UINT32_MAX; 3441 if (port->status & CTL_PORT_STATUS_ONLINE) { 3442 if (port->lun_disable != NULL) { 3443 STAILQ_FOREACH(lun, &softc->lun_list, links) 3444 port->lun_disable(port->targ_lun_arg, lun->lun); 3445 } 3446 ctl_isc_announce_port(port); 3447 } 3448 return (0); 3449 } 3450 3451 int 3452 ctl_lun_map_deinit(struct ctl_port *port) 3453 { 3454 struct ctl_softc *softc = port->ctl_softc; 3455 struct ctl_lun *lun; 3456 3457 if (port->lun_map == NULL) 3458 return (0); 3459 free(port->lun_map, M_CTL); 3460 port->lun_map = NULL; 3461 if (port->status & CTL_PORT_STATUS_ONLINE) { 3462 if (port->lun_enable != NULL) { 3463 STAILQ_FOREACH(lun, &softc->lun_list, links) 3464 port->lun_enable(port->targ_lun_arg, lun->lun); 3465 } 3466 ctl_isc_announce_port(port); 3467 } 3468 return (0); 3469 } 3470 3471 int 3472 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3473 { 3474 int status; 3475 uint32_t old; 3476 3477 if (port->lun_map == NULL) { 3478 status = ctl_lun_map_init(port); 3479 if (status != 0) 3480 return (status); 3481 } 3482 old = port->lun_map[plun]; 3483 port->lun_map[plun] = glun; 3484 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) { 3485 if (port->lun_enable != NULL) 3486 port->lun_enable(port->targ_lun_arg, plun); 3487 ctl_isc_announce_port(port); 3488 } 3489 return (0); 3490 } 3491 3492 int 3493 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3494 { 3495 uint32_t old; 3496 3497 if (port->lun_map == NULL) 3498 return (0); 3499 old = port->lun_map[plun]; 3500 port->lun_map[plun] = UINT32_MAX; 3501 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) { 3502 if (port->lun_disable != NULL) 3503 port->lun_disable(port->targ_lun_arg, plun); 3504 ctl_isc_announce_port(port); 3505 } 3506 return (0); 3507 } 3508 3509 uint32_t 3510 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3511 { 3512 3513 if (port == NULL) 3514 return (UINT32_MAX); 3515 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3516 return (lun_id); 3517 return (port->lun_map[lun_id]); 3518 } 3519 3520 uint32_t 3521 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3522 { 3523 uint32_t i; 3524 3525 if (port == NULL) 3526 return (UINT32_MAX); 3527 if (port->lun_map == NULL) 3528 return (lun_id); 3529 for (i = 0; i < CTL_MAX_LUNS; i++) { 3530 if (port->lun_map[i] == lun_id) 3531 return (i); 3532 } 3533 return (UINT32_MAX); 3534 } 3535 3536 static struct ctl_port * 3537 ctl_io_port(struct ctl_io_hdr *io_hdr) 3538 { 3539 3540 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]); 3541 } 3542 3543 int 3544 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3545 { 3546 int i; 3547 3548 for (i = first; i < last; i++) { 3549 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3550 return (i); 3551 } 3552 return (-1); 3553 } 3554 3555 int 3556 ctl_set_mask(uint32_t *mask, uint32_t bit) 3557 { 3558 uint32_t chunk, piece; 3559 3560 chunk = bit >> 5; 3561 piece = bit % (sizeof(uint32_t) * 8); 3562 3563 if ((mask[chunk] & (1 << piece)) != 0) 3564 return (-1); 3565 else 3566 mask[chunk] |= (1 << piece); 3567 3568 return (0); 3569 } 3570 3571 int 3572 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3573 { 3574 uint32_t chunk, piece; 3575 3576 chunk = bit >> 5; 3577 piece = bit % (sizeof(uint32_t) * 8); 3578 3579 if ((mask[chunk] & (1 << piece)) == 0) 3580 return (-1); 3581 else 3582 mask[chunk] &= ~(1 << piece); 3583 3584 return (0); 3585 } 3586 3587 int 3588 ctl_is_set(uint32_t *mask, uint32_t bit) 3589 { 3590 uint32_t chunk, piece; 3591 3592 chunk = bit >> 5; 3593 piece = bit % (sizeof(uint32_t) * 8); 3594 3595 if ((mask[chunk] & (1 << piece)) == 0) 3596 return (0); 3597 else 3598 return (1); 3599 } 3600 3601 static uint64_t 3602 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3603 { 3604 uint64_t *t; 3605 3606 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3607 if (t == NULL) 3608 return (0); 3609 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3610 } 3611 3612 static void 3613 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3614 { 3615 uint64_t *t; 3616 3617 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3618 if (t == NULL) 3619 return; 3620 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3621 } 3622 3623 static void 3624 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3625 { 3626 uint64_t *p; 3627 u_int i; 3628 3629 i = residx/CTL_MAX_INIT_PER_PORT; 3630 if (lun->pr_keys[i] != NULL) 3631 return; 3632 mtx_unlock(&lun->lun_lock); 3633 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3634 M_WAITOK | M_ZERO); 3635 mtx_lock(&lun->lun_lock); 3636 if (lun->pr_keys[i] == NULL) 3637 lun->pr_keys[i] = p; 3638 else 3639 free(p, M_CTL); 3640 } 3641 3642 static void 3643 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3644 { 3645 uint64_t *t; 3646 3647 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3648 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3649 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3650 } 3651 3652 /* 3653 * ctl_softc, pool_name, total_ctl_io are passed in. 3654 * npool is passed out. 3655 */ 3656 int 3657 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3658 uint32_t total_ctl_io, void **npool) 3659 { 3660 #ifdef IO_POOLS 3661 struct ctl_io_pool *pool; 3662 3663 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3664 M_NOWAIT | M_ZERO); 3665 if (pool == NULL) 3666 return (ENOMEM); 3667 3668 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3669 pool->ctl_softc = ctl_softc; 3670 pool->zone = uma_zsecond_create(pool->name, NULL, 3671 NULL, NULL, NULL, ctl_softc->io_zone); 3672 /* uma_prealloc(pool->zone, total_ctl_io); */ 3673 3674 *npool = pool; 3675 #else 3676 *npool = ctl_softc->io_zone; 3677 #endif 3678 return (0); 3679 } 3680 3681 void 3682 ctl_pool_free(struct ctl_io_pool *pool) 3683 { 3684 3685 if (pool == NULL) 3686 return; 3687 3688 #ifdef IO_POOLS 3689 uma_zdestroy(pool->zone); 3690 free(pool, M_CTL); 3691 #endif 3692 } 3693 3694 union ctl_io * 3695 ctl_alloc_io(void *pool_ref) 3696 { 3697 union ctl_io *io; 3698 #ifdef IO_POOLS 3699 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3700 3701 io = uma_zalloc(pool->zone, M_WAITOK); 3702 #else 3703 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3704 #endif 3705 if (io != NULL) 3706 io->io_hdr.pool = pool_ref; 3707 return (io); 3708 } 3709 3710 union ctl_io * 3711 ctl_alloc_io_nowait(void *pool_ref) 3712 { 3713 union ctl_io *io; 3714 #ifdef IO_POOLS 3715 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3716 3717 io = uma_zalloc(pool->zone, M_NOWAIT); 3718 #else 3719 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3720 #endif 3721 if (io != NULL) 3722 io->io_hdr.pool = pool_ref; 3723 return (io); 3724 } 3725 3726 void 3727 ctl_free_io(union ctl_io *io) 3728 { 3729 #ifdef IO_POOLS 3730 struct ctl_io_pool *pool; 3731 #endif 3732 3733 if (io == NULL) 3734 return; 3735 3736 #ifdef IO_POOLS 3737 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3738 uma_zfree(pool->zone, io); 3739 #else 3740 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3741 #endif 3742 } 3743 3744 void 3745 ctl_zero_io(union ctl_io *io) 3746 { 3747 void *pool_ref; 3748 3749 if (io == NULL) 3750 return; 3751 3752 /* 3753 * May need to preserve linked list pointers at some point too. 3754 */ 3755 pool_ref = io->io_hdr.pool; 3756 memset(io, 0, sizeof(*io)); 3757 io->io_hdr.pool = pool_ref; 3758 } 3759 3760 int 3761 ctl_expand_number(const char *buf, uint64_t *num) 3762 { 3763 char *endptr; 3764 uint64_t number; 3765 unsigned shift; 3766 3767 number = strtoq(buf, &endptr, 0); 3768 3769 switch (tolower((unsigned char)*endptr)) { 3770 case 'e': 3771 shift = 60; 3772 break; 3773 case 'p': 3774 shift = 50; 3775 break; 3776 case 't': 3777 shift = 40; 3778 break; 3779 case 'g': 3780 shift = 30; 3781 break; 3782 case 'm': 3783 shift = 20; 3784 break; 3785 case 'k': 3786 shift = 10; 3787 break; 3788 case 'b': 3789 case '\0': /* No unit. */ 3790 *num = number; 3791 return (0); 3792 default: 3793 /* Unrecognized unit. */ 3794 return (-1); 3795 } 3796 3797 if ((number << shift) >> shift != number) { 3798 /* Overflow */ 3799 return (-1); 3800 } 3801 *num = number << shift; 3802 return (0); 3803 } 3804 3805 3806 /* 3807 * This routine could be used in the future to load default and/or saved 3808 * mode page parameters for a particuar lun. 3809 */ 3810 static int 3811 ctl_init_page_index(struct ctl_lun *lun) 3812 { 3813 int i; 3814 struct ctl_page_index *page_index; 3815 const char *value; 3816 uint64_t ival; 3817 3818 memcpy(&lun->mode_pages.index, page_index_template, 3819 sizeof(page_index_template)); 3820 3821 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3822 3823 page_index = &lun->mode_pages.index[i]; 3824 if (lun->be_lun->lun_type == T_DIRECT && 3825 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 3826 continue; 3827 if (lun->be_lun->lun_type == T_PROCESSOR && 3828 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 3829 continue; 3830 if (lun->be_lun->lun_type == T_CDROM && 3831 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 3832 continue; 3833 3834 switch (page_index->page_code & SMPH_PC_MASK) { 3835 case SMS_RW_ERROR_RECOVERY_PAGE: { 3836 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3837 panic("subpage is incorrect!"); 3838 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3839 &rw_er_page_default, 3840 sizeof(rw_er_page_default)); 3841 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3842 &rw_er_page_changeable, 3843 sizeof(rw_er_page_changeable)); 3844 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3845 &rw_er_page_default, 3846 sizeof(rw_er_page_default)); 3847 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3848 &rw_er_page_default, 3849 sizeof(rw_er_page_default)); 3850 page_index->page_data = 3851 (uint8_t *)lun->mode_pages.rw_er_page; 3852 break; 3853 } 3854 case SMS_FORMAT_DEVICE_PAGE: { 3855 struct scsi_format_page *format_page; 3856 3857 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3858 panic("subpage is incorrect!"); 3859 3860 /* 3861 * Sectors per track are set above. Bytes per 3862 * sector need to be set here on a per-LUN basis. 3863 */ 3864 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3865 &format_page_default, 3866 sizeof(format_page_default)); 3867 memcpy(&lun->mode_pages.format_page[ 3868 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3869 sizeof(format_page_changeable)); 3870 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3871 &format_page_default, 3872 sizeof(format_page_default)); 3873 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3874 &format_page_default, 3875 sizeof(format_page_default)); 3876 3877 format_page = &lun->mode_pages.format_page[ 3878 CTL_PAGE_CURRENT]; 3879 scsi_ulto2b(lun->be_lun->blocksize, 3880 format_page->bytes_per_sector); 3881 3882 format_page = &lun->mode_pages.format_page[ 3883 CTL_PAGE_DEFAULT]; 3884 scsi_ulto2b(lun->be_lun->blocksize, 3885 format_page->bytes_per_sector); 3886 3887 format_page = &lun->mode_pages.format_page[ 3888 CTL_PAGE_SAVED]; 3889 scsi_ulto2b(lun->be_lun->blocksize, 3890 format_page->bytes_per_sector); 3891 3892 page_index->page_data = 3893 (uint8_t *)lun->mode_pages.format_page; 3894 break; 3895 } 3896 case SMS_RIGID_DISK_PAGE: { 3897 struct scsi_rigid_disk_page *rigid_disk_page; 3898 uint32_t sectors_per_cylinder; 3899 uint64_t cylinders; 3900 #ifndef __XSCALE__ 3901 int shift; 3902 #endif /* !__XSCALE__ */ 3903 3904 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3905 panic("invalid subpage value %d", 3906 page_index->subpage); 3907 3908 /* 3909 * Rotation rate and sectors per track are set 3910 * above. We calculate the cylinders here based on 3911 * capacity. Due to the number of heads and 3912 * sectors per track we're using, smaller arrays 3913 * may turn out to have 0 cylinders. Linux and 3914 * FreeBSD don't pay attention to these mode pages 3915 * to figure out capacity, but Solaris does. It 3916 * seems to deal with 0 cylinders just fine, and 3917 * works out a fake geometry based on the capacity. 3918 */ 3919 memcpy(&lun->mode_pages.rigid_disk_page[ 3920 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3921 sizeof(rigid_disk_page_default)); 3922 memcpy(&lun->mode_pages.rigid_disk_page[ 3923 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3924 sizeof(rigid_disk_page_changeable)); 3925 3926 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 3927 CTL_DEFAULT_HEADS; 3928 3929 /* 3930 * The divide method here will be more accurate, 3931 * probably, but results in floating point being 3932 * used in the kernel on i386 (__udivdi3()). On the 3933 * XScale, though, __udivdi3() is implemented in 3934 * software. 3935 * 3936 * The shift method for cylinder calculation is 3937 * accurate if sectors_per_cylinder is a power of 3938 * 2. Otherwise it might be slightly off -- you 3939 * might have a bit of a truncation problem. 3940 */ 3941 #ifdef __XSCALE__ 3942 cylinders = (lun->be_lun->maxlba + 1) / 3943 sectors_per_cylinder; 3944 #else 3945 for (shift = 31; shift > 0; shift--) { 3946 if (sectors_per_cylinder & (1 << shift)) 3947 break; 3948 } 3949 cylinders = (lun->be_lun->maxlba + 1) >> shift; 3950 #endif 3951 3952 /* 3953 * We've basically got 3 bytes, or 24 bits for the 3954 * cylinder size in the mode page. If we're over, 3955 * just round down to 2^24. 3956 */ 3957 if (cylinders > 0xffffff) 3958 cylinders = 0xffffff; 3959 3960 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 3961 CTL_PAGE_DEFAULT]; 3962 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 3963 3964 if ((value = ctl_get_opt(&lun->be_lun->options, 3965 "rpm")) != NULL) { 3966 scsi_ulto2b(strtol(value, NULL, 0), 3967 rigid_disk_page->rotation_rate); 3968 } 3969 3970 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 3971 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3972 sizeof(rigid_disk_page_default)); 3973 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 3974 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3975 sizeof(rigid_disk_page_default)); 3976 3977 page_index->page_data = 3978 (uint8_t *)lun->mode_pages.rigid_disk_page; 3979 break; 3980 } 3981 case SMS_CACHING_PAGE: { 3982 struct scsi_caching_page *caching_page; 3983 3984 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3985 panic("invalid subpage value %d", 3986 page_index->subpage); 3987 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 3988 &caching_page_default, 3989 sizeof(caching_page_default)); 3990 memcpy(&lun->mode_pages.caching_page[ 3991 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 3992 sizeof(caching_page_changeable)); 3993 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 3994 &caching_page_default, 3995 sizeof(caching_page_default)); 3996 caching_page = &lun->mode_pages.caching_page[ 3997 CTL_PAGE_SAVED]; 3998 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 3999 if (value != NULL && strcmp(value, "off") == 0) 4000 caching_page->flags1 &= ~SCP_WCE; 4001 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4002 if (value != NULL && strcmp(value, "off") == 0) 4003 caching_page->flags1 |= SCP_RCD; 4004 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4005 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4006 sizeof(caching_page_default)); 4007 page_index->page_data = 4008 (uint8_t *)lun->mode_pages.caching_page; 4009 break; 4010 } 4011 case SMS_CONTROL_MODE_PAGE: { 4012 switch (page_index->subpage) { 4013 case SMS_SUBPAGE_PAGE_0: { 4014 struct scsi_control_page *control_page; 4015 4016 memcpy(&lun->mode_pages.control_page[ 4017 CTL_PAGE_DEFAULT], 4018 &control_page_default, 4019 sizeof(control_page_default)); 4020 memcpy(&lun->mode_pages.control_page[ 4021 CTL_PAGE_CHANGEABLE], 4022 &control_page_changeable, 4023 sizeof(control_page_changeable)); 4024 memcpy(&lun->mode_pages.control_page[ 4025 CTL_PAGE_SAVED], 4026 &control_page_default, 4027 sizeof(control_page_default)); 4028 control_page = &lun->mode_pages.control_page[ 4029 CTL_PAGE_SAVED]; 4030 value = ctl_get_opt(&lun->be_lun->options, 4031 "reordering"); 4032 if (value != NULL && 4033 strcmp(value, "unrestricted") == 0) { 4034 control_page->queue_flags &= 4035 ~SCP_QUEUE_ALG_MASK; 4036 control_page->queue_flags |= 4037 SCP_QUEUE_ALG_UNRESTRICTED; 4038 } 4039 memcpy(&lun->mode_pages.control_page[ 4040 CTL_PAGE_CURRENT], 4041 &lun->mode_pages.control_page[ 4042 CTL_PAGE_SAVED], 4043 sizeof(control_page_default)); 4044 page_index->page_data = 4045 (uint8_t *)lun->mode_pages.control_page; 4046 break; 4047 } 4048 case 0x01: 4049 memcpy(&lun->mode_pages.control_ext_page[ 4050 CTL_PAGE_DEFAULT], 4051 &control_ext_page_default, 4052 sizeof(control_ext_page_default)); 4053 memcpy(&lun->mode_pages.control_ext_page[ 4054 CTL_PAGE_CHANGEABLE], 4055 &control_ext_page_changeable, 4056 sizeof(control_ext_page_changeable)); 4057 memcpy(&lun->mode_pages.control_ext_page[ 4058 CTL_PAGE_SAVED], 4059 &control_ext_page_default, 4060 sizeof(control_ext_page_default)); 4061 memcpy(&lun->mode_pages.control_ext_page[ 4062 CTL_PAGE_CURRENT], 4063 &lun->mode_pages.control_ext_page[ 4064 CTL_PAGE_SAVED], 4065 sizeof(control_ext_page_default)); 4066 page_index->page_data = 4067 (uint8_t *)lun->mode_pages.control_ext_page; 4068 break; 4069 } 4070 break; 4071 } 4072 case SMS_INFO_EXCEPTIONS_PAGE: { 4073 switch (page_index->subpage) { 4074 case SMS_SUBPAGE_PAGE_0: 4075 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4076 &ie_page_default, 4077 sizeof(ie_page_default)); 4078 memcpy(&lun->mode_pages.ie_page[ 4079 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4080 sizeof(ie_page_changeable)); 4081 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4082 &ie_page_default, 4083 sizeof(ie_page_default)); 4084 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4085 &ie_page_default, 4086 sizeof(ie_page_default)); 4087 page_index->page_data = 4088 (uint8_t *)lun->mode_pages.ie_page; 4089 break; 4090 case 0x02: { 4091 struct ctl_logical_block_provisioning_page *page; 4092 4093 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4094 &lbp_page_default, 4095 sizeof(lbp_page_default)); 4096 memcpy(&lun->mode_pages.lbp_page[ 4097 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4098 sizeof(lbp_page_changeable)); 4099 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4100 &lbp_page_default, 4101 sizeof(lbp_page_default)); 4102 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4103 value = ctl_get_opt(&lun->be_lun->options, 4104 "avail-threshold"); 4105 if (value != NULL && 4106 ctl_expand_number(value, &ival) == 0) { 4107 page->descr[0].flags |= SLBPPD_ENABLED | 4108 SLBPPD_ARMING_DEC; 4109 if (lun->be_lun->blocksize) 4110 ival /= lun->be_lun->blocksize; 4111 else 4112 ival /= 512; 4113 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4114 page->descr[0].count); 4115 } 4116 value = ctl_get_opt(&lun->be_lun->options, 4117 "used-threshold"); 4118 if (value != NULL && 4119 ctl_expand_number(value, &ival) == 0) { 4120 page->descr[1].flags |= SLBPPD_ENABLED | 4121 SLBPPD_ARMING_INC; 4122 if (lun->be_lun->blocksize) 4123 ival /= lun->be_lun->blocksize; 4124 else 4125 ival /= 512; 4126 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4127 page->descr[1].count); 4128 } 4129 value = ctl_get_opt(&lun->be_lun->options, 4130 "pool-avail-threshold"); 4131 if (value != NULL && 4132 ctl_expand_number(value, &ival) == 0) { 4133 page->descr[2].flags |= SLBPPD_ENABLED | 4134 SLBPPD_ARMING_DEC; 4135 if (lun->be_lun->blocksize) 4136 ival /= lun->be_lun->blocksize; 4137 else 4138 ival /= 512; 4139 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4140 page->descr[2].count); 4141 } 4142 value = ctl_get_opt(&lun->be_lun->options, 4143 "pool-used-threshold"); 4144 if (value != NULL && 4145 ctl_expand_number(value, &ival) == 0) { 4146 page->descr[3].flags |= SLBPPD_ENABLED | 4147 SLBPPD_ARMING_INC; 4148 if (lun->be_lun->blocksize) 4149 ival /= lun->be_lun->blocksize; 4150 else 4151 ival /= 512; 4152 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4153 page->descr[3].count); 4154 } 4155 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4156 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4157 sizeof(lbp_page_default)); 4158 page_index->page_data = 4159 (uint8_t *)lun->mode_pages.lbp_page; 4160 }} 4161 break; 4162 } 4163 case SMS_CDDVD_CAPS_PAGE:{ 4164 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4165 &cddvd_page_default, 4166 sizeof(cddvd_page_default)); 4167 memcpy(&lun->mode_pages.cddvd_page[ 4168 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4169 sizeof(cddvd_page_changeable)); 4170 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4171 &cddvd_page_default, 4172 sizeof(cddvd_page_default)); 4173 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4174 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4175 sizeof(cddvd_page_default)); 4176 page_index->page_data = 4177 (uint8_t *)lun->mode_pages.cddvd_page; 4178 break; 4179 } 4180 case SMS_VENDOR_SPECIFIC_PAGE:{ 4181 switch (page_index->subpage) { 4182 case DBGCNF_SUBPAGE_CODE: { 4183 memcpy(&lun->mode_pages.debugconf_subpage[ 4184 CTL_PAGE_CURRENT], 4185 &debugconf_page_default, 4186 sizeof(debugconf_page_default)); 4187 memcpy(&lun->mode_pages.debugconf_subpage[ 4188 CTL_PAGE_CHANGEABLE], 4189 &debugconf_page_changeable, 4190 sizeof(debugconf_page_changeable)); 4191 memcpy(&lun->mode_pages.debugconf_subpage[ 4192 CTL_PAGE_DEFAULT], 4193 &debugconf_page_default, 4194 sizeof(debugconf_page_default)); 4195 memcpy(&lun->mode_pages.debugconf_subpage[ 4196 CTL_PAGE_SAVED], 4197 &debugconf_page_default, 4198 sizeof(debugconf_page_default)); 4199 page_index->page_data = 4200 (uint8_t *)lun->mode_pages.debugconf_subpage; 4201 break; 4202 } 4203 default: 4204 panic("invalid subpage value %d", 4205 page_index->subpage); 4206 break; 4207 } 4208 break; 4209 } 4210 default: 4211 panic("invalid page value %d", 4212 page_index->page_code & SMPH_PC_MASK); 4213 break; 4214 } 4215 } 4216 4217 return (CTL_RETVAL_COMPLETE); 4218 } 4219 4220 static int 4221 ctl_init_log_page_index(struct ctl_lun *lun) 4222 { 4223 struct ctl_page_index *page_index; 4224 int i, j, k, prev; 4225 4226 memcpy(&lun->log_pages.index, log_page_index_template, 4227 sizeof(log_page_index_template)); 4228 4229 prev = -1; 4230 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4231 4232 page_index = &lun->log_pages.index[i]; 4233 if (lun->be_lun->lun_type == T_DIRECT && 4234 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4235 continue; 4236 if (lun->be_lun->lun_type == T_PROCESSOR && 4237 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4238 continue; 4239 if (lun->be_lun->lun_type == T_CDROM && 4240 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4241 continue; 4242 4243 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4244 lun->backend->lun_attr == NULL) 4245 continue; 4246 4247 if (page_index->page_code != prev) { 4248 lun->log_pages.pages_page[j] = page_index->page_code; 4249 prev = page_index->page_code; 4250 j++; 4251 } 4252 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4253 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4254 k++; 4255 } 4256 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4257 lun->log_pages.index[0].page_len = j; 4258 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4259 lun->log_pages.index[1].page_len = k * 2; 4260 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4261 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4262 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4263 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4264 4265 return (CTL_RETVAL_COMPLETE); 4266 } 4267 4268 static int 4269 hex2bin(const char *str, uint8_t *buf, int buf_size) 4270 { 4271 int i; 4272 u_char c; 4273 4274 memset(buf, 0, buf_size); 4275 while (isspace(str[0])) 4276 str++; 4277 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4278 str += 2; 4279 buf_size *= 2; 4280 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4281 c = str[i]; 4282 if (isdigit(c)) 4283 c -= '0'; 4284 else if (isalpha(c)) 4285 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4286 else 4287 break; 4288 if (c >= 16) 4289 break; 4290 if ((i & 1) == 0) 4291 buf[i / 2] |= (c << 4); 4292 else 4293 buf[i / 2] |= c; 4294 } 4295 return ((i + 1) / 2); 4296 } 4297 4298 /* 4299 * LUN allocation. 4300 * 4301 * Requirements: 4302 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4303 * wants us to allocate the LUN and he can block. 4304 * - ctl_softc is always set 4305 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4306 * 4307 * Returns 0 for success, non-zero (errno) for failure. 4308 */ 4309 static int 4310 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4311 struct ctl_be_lun *const be_lun) 4312 { 4313 struct ctl_lun *nlun, *lun; 4314 struct scsi_vpd_id_descriptor *desc; 4315 struct scsi_vpd_id_t10 *t10id; 4316 const char *eui, *naa, *scsiname, *vendor, *value; 4317 int lun_number, i, lun_malloced; 4318 int devidlen, idlen1, idlen2 = 0, len; 4319 4320 if (be_lun == NULL) 4321 return (EINVAL); 4322 4323 /* 4324 * We currently only support Direct Access or Processor LUN types. 4325 */ 4326 switch (be_lun->lun_type) { 4327 case T_DIRECT: 4328 case T_PROCESSOR: 4329 case T_CDROM: 4330 break; 4331 case T_SEQUENTIAL: 4332 case T_CHANGER: 4333 default: 4334 be_lun->lun_config_status(be_lun->be_lun, 4335 CTL_LUN_CONFIG_FAILURE); 4336 break; 4337 } 4338 if (ctl_lun == NULL) { 4339 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4340 lun_malloced = 1; 4341 } else { 4342 lun_malloced = 0; 4343 lun = ctl_lun; 4344 } 4345 4346 memset(lun, 0, sizeof(*lun)); 4347 if (lun_malloced) 4348 lun->flags = CTL_LUN_MALLOCED; 4349 4350 /* Generate LUN ID. */ 4351 devidlen = max(CTL_DEVID_MIN_LEN, 4352 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4353 idlen1 = sizeof(*t10id) + devidlen; 4354 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4355 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4356 if (scsiname != NULL) { 4357 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4358 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4359 } 4360 eui = ctl_get_opt(&be_lun->options, "eui"); 4361 if (eui != NULL) { 4362 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4363 } 4364 naa = ctl_get_opt(&be_lun->options, "naa"); 4365 if (naa != NULL) { 4366 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4367 } 4368 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4369 M_CTL, M_WAITOK | M_ZERO); 4370 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4371 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4372 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4373 desc->length = idlen1; 4374 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4375 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4376 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4377 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4378 } else { 4379 strncpy(t10id->vendor, vendor, 4380 min(sizeof(t10id->vendor), strlen(vendor))); 4381 } 4382 strncpy((char *)t10id->vendor_spec_id, 4383 (char *)be_lun->device_id, devidlen); 4384 if (scsiname != NULL) { 4385 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4386 desc->length); 4387 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4388 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4389 SVPD_ID_TYPE_SCSI_NAME; 4390 desc->length = idlen2; 4391 strlcpy(desc->identifier, scsiname, idlen2); 4392 } 4393 if (eui != NULL) { 4394 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4395 desc->length); 4396 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4397 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4398 SVPD_ID_TYPE_EUI64; 4399 desc->length = hex2bin(eui, desc->identifier, 16); 4400 desc->length = desc->length > 12 ? 16 : 4401 (desc->length > 8 ? 12 : 8); 4402 len -= 16 - desc->length; 4403 } 4404 if (naa != NULL) { 4405 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4406 desc->length); 4407 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4408 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4409 SVPD_ID_TYPE_NAA; 4410 desc->length = hex2bin(naa, desc->identifier, 16); 4411 desc->length = desc->length > 8 ? 16 : 8; 4412 len -= 16 - desc->length; 4413 } 4414 lun->lun_devid->len = len; 4415 4416 mtx_lock(&ctl_softc->ctl_lock); 4417 /* 4418 * See if the caller requested a particular LUN number. If so, see 4419 * if it is available. Otherwise, allocate the first available LUN. 4420 */ 4421 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4422 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4423 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4424 mtx_unlock(&ctl_softc->ctl_lock); 4425 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4426 printf("ctl: requested LUN ID %d is higher " 4427 "than CTL_MAX_LUNS - 1 (%d)\n", 4428 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4429 } else { 4430 /* 4431 * XXX KDM return an error, or just assign 4432 * another LUN ID in this case?? 4433 */ 4434 printf("ctl: requested LUN ID %d is already " 4435 "in use\n", be_lun->req_lun_id); 4436 } 4437 if (lun->flags & CTL_LUN_MALLOCED) 4438 free(lun, M_CTL); 4439 be_lun->lun_config_status(be_lun->be_lun, 4440 CTL_LUN_CONFIG_FAILURE); 4441 return (ENOSPC); 4442 } 4443 lun_number = be_lun->req_lun_id; 4444 } else { 4445 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4446 if (lun_number == -1) { 4447 mtx_unlock(&ctl_softc->ctl_lock); 4448 printf("ctl: can't allocate LUN, out of LUNs\n"); 4449 if (lun->flags & CTL_LUN_MALLOCED) 4450 free(lun, M_CTL); 4451 be_lun->lun_config_status(be_lun->be_lun, 4452 CTL_LUN_CONFIG_FAILURE); 4453 return (ENOSPC); 4454 } 4455 } 4456 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4457 4458 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4459 lun->lun = lun_number; 4460 lun->be_lun = be_lun; 4461 /* 4462 * The processor LUN is always enabled. Disk LUNs come on line 4463 * disabled, and must be enabled by the backend. 4464 */ 4465 lun->flags |= CTL_LUN_DISABLED; 4466 lun->backend = be_lun->be; 4467 be_lun->ctl_lun = lun; 4468 be_lun->lun_id = lun_number; 4469 atomic_add_int(&be_lun->be->num_luns, 1); 4470 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4471 lun->flags |= CTL_LUN_EJECTED; 4472 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4473 lun->flags |= CTL_LUN_NO_MEDIA; 4474 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4475 lun->flags |= CTL_LUN_STOPPED; 4476 4477 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4478 lun->flags |= CTL_LUN_PRIMARY_SC; 4479 4480 value = ctl_get_opt(&be_lun->options, "removable"); 4481 if (value != NULL) { 4482 if (strcmp(value, "on") == 0) 4483 lun->flags |= CTL_LUN_REMOVABLE; 4484 } else if (be_lun->lun_type == T_CDROM) 4485 lun->flags |= CTL_LUN_REMOVABLE; 4486 4487 lun->ctl_softc = ctl_softc; 4488 #ifdef CTL_TIME_IO 4489 lun->last_busy = getsbinuptime(); 4490 #endif 4491 TAILQ_INIT(&lun->ooa_queue); 4492 TAILQ_INIT(&lun->blocked_queue); 4493 STAILQ_INIT(&lun->error_list); 4494 ctl_tpc_lun_init(lun); 4495 4496 /* 4497 * Initialize the mode and log page index. 4498 */ 4499 ctl_init_page_index(lun); 4500 ctl_init_log_page_index(lun); 4501 4502 /* 4503 * Now, before we insert this lun on the lun list, set the lun 4504 * inventory changed UA for all other luns. 4505 */ 4506 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4507 mtx_lock(&nlun->lun_lock); 4508 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4509 mtx_unlock(&nlun->lun_lock); 4510 } 4511 4512 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4513 4514 ctl_softc->ctl_luns[lun_number] = lun; 4515 4516 ctl_softc->num_luns++; 4517 4518 /* Setup statistics gathering */ 4519 lun->stats.device_type = be_lun->lun_type; 4520 lun->stats.lun_number = lun_number; 4521 lun->stats.blocksize = be_lun->blocksize; 4522 if (be_lun->blocksize == 0) 4523 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4524 for (i = 0;i < CTL_MAX_PORTS;i++) 4525 lun->stats.ports[i].targ_port = i; 4526 4527 mtx_unlock(&ctl_softc->ctl_lock); 4528 4529 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4530 return (0); 4531 } 4532 4533 /* 4534 * Delete a LUN. 4535 * Assumptions: 4536 * - LUN has already been marked invalid and any pending I/O has been taken 4537 * care of. 4538 */ 4539 static int 4540 ctl_free_lun(struct ctl_lun *lun) 4541 { 4542 struct ctl_softc *softc; 4543 struct ctl_lun *nlun; 4544 int i; 4545 4546 softc = lun->ctl_softc; 4547 4548 mtx_assert(&softc->ctl_lock, MA_OWNED); 4549 4550 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4551 4552 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4553 4554 softc->ctl_luns[lun->lun] = NULL; 4555 4556 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4557 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4558 4559 softc->num_luns--; 4560 4561 /* 4562 * Tell the backend to free resources, if this LUN has a backend. 4563 */ 4564 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4565 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4566 4567 ctl_tpc_lun_shutdown(lun); 4568 mtx_destroy(&lun->lun_lock); 4569 free(lun->lun_devid, M_CTL); 4570 for (i = 0; i < CTL_MAX_PORTS; i++) 4571 free(lun->pending_ua[i], M_CTL); 4572 for (i = 0; i < CTL_MAX_PORTS; i++) 4573 free(lun->pr_keys[i], M_CTL); 4574 free(lun->write_buffer, M_CTL); 4575 if (lun->flags & CTL_LUN_MALLOCED) 4576 free(lun, M_CTL); 4577 4578 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4579 mtx_lock(&nlun->lun_lock); 4580 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4581 mtx_unlock(&nlun->lun_lock); 4582 } 4583 4584 return (0); 4585 } 4586 4587 static void 4588 ctl_create_lun(struct ctl_be_lun *be_lun) 4589 { 4590 4591 /* 4592 * ctl_alloc_lun() should handle all potential failure cases. 4593 */ 4594 ctl_alloc_lun(control_softc, NULL, be_lun); 4595 } 4596 4597 int 4598 ctl_add_lun(struct ctl_be_lun *be_lun) 4599 { 4600 struct ctl_softc *softc = control_softc; 4601 4602 mtx_lock(&softc->ctl_lock); 4603 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4604 mtx_unlock(&softc->ctl_lock); 4605 wakeup(&softc->pending_lun_queue); 4606 4607 return (0); 4608 } 4609 4610 int 4611 ctl_enable_lun(struct ctl_be_lun *be_lun) 4612 { 4613 struct ctl_softc *softc; 4614 struct ctl_port *port, *nport; 4615 struct ctl_lun *lun; 4616 int retval; 4617 4618 lun = (struct ctl_lun *)be_lun->ctl_lun; 4619 softc = lun->ctl_softc; 4620 4621 mtx_lock(&softc->ctl_lock); 4622 mtx_lock(&lun->lun_lock); 4623 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4624 /* 4625 * eh? Why did we get called if the LUN is already 4626 * enabled? 4627 */ 4628 mtx_unlock(&lun->lun_lock); 4629 mtx_unlock(&softc->ctl_lock); 4630 return (0); 4631 } 4632 lun->flags &= ~CTL_LUN_DISABLED; 4633 mtx_unlock(&lun->lun_lock); 4634 4635 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4636 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4637 port->lun_map != NULL || port->lun_enable == NULL) 4638 continue; 4639 4640 /* 4641 * Drop the lock while we call the FETD's enable routine. 4642 * This can lead to a callback into CTL (at least in the 4643 * case of the internal initiator frontend. 4644 */ 4645 mtx_unlock(&softc->ctl_lock); 4646 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4647 mtx_lock(&softc->ctl_lock); 4648 if (retval != 0) { 4649 printf("%s: FETD %s port %d returned error " 4650 "%d for lun_enable on lun %jd\n", 4651 __func__, port->port_name, port->targ_port, 4652 retval, (intmax_t)lun->lun); 4653 } 4654 } 4655 4656 mtx_unlock(&softc->ctl_lock); 4657 ctl_isc_announce_lun(lun); 4658 4659 return (0); 4660 } 4661 4662 int 4663 ctl_disable_lun(struct ctl_be_lun *be_lun) 4664 { 4665 struct ctl_softc *softc; 4666 struct ctl_port *port; 4667 struct ctl_lun *lun; 4668 int retval; 4669 4670 lun = (struct ctl_lun *)be_lun->ctl_lun; 4671 softc = lun->ctl_softc; 4672 4673 mtx_lock(&softc->ctl_lock); 4674 mtx_lock(&lun->lun_lock); 4675 if (lun->flags & CTL_LUN_DISABLED) { 4676 mtx_unlock(&lun->lun_lock); 4677 mtx_unlock(&softc->ctl_lock); 4678 return (0); 4679 } 4680 lun->flags |= CTL_LUN_DISABLED; 4681 mtx_unlock(&lun->lun_lock); 4682 4683 STAILQ_FOREACH(port, &softc->port_list, links) { 4684 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4685 port->lun_map != NULL || port->lun_disable == NULL) 4686 continue; 4687 4688 /* 4689 * Drop the lock before we call the frontend's disable 4690 * routine, to avoid lock order reversals. 4691 * 4692 * XXX KDM what happens if the frontend list changes while 4693 * we're traversing it? It's unlikely, but should be handled. 4694 */ 4695 mtx_unlock(&softc->ctl_lock); 4696 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4697 mtx_lock(&softc->ctl_lock); 4698 if (retval != 0) { 4699 printf("%s: FETD %s port %d returned error " 4700 "%d for lun_disable on lun %jd\n", 4701 __func__, port->port_name, port->targ_port, 4702 retval, (intmax_t)lun->lun); 4703 } 4704 } 4705 4706 mtx_unlock(&softc->ctl_lock); 4707 ctl_isc_announce_lun(lun); 4708 4709 return (0); 4710 } 4711 4712 int 4713 ctl_start_lun(struct ctl_be_lun *be_lun) 4714 { 4715 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4716 4717 mtx_lock(&lun->lun_lock); 4718 lun->flags &= ~CTL_LUN_STOPPED; 4719 mtx_unlock(&lun->lun_lock); 4720 return (0); 4721 } 4722 4723 int 4724 ctl_stop_lun(struct ctl_be_lun *be_lun) 4725 { 4726 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4727 4728 mtx_lock(&lun->lun_lock); 4729 lun->flags |= CTL_LUN_STOPPED; 4730 mtx_unlock(&lun->lun_lock); 4731 return (0); 4732 } 4733 4734 int 4735 ctl_lun_no_media(struct ctl_be_lun *be_lun) 4736 { 4737 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4738 4739 mtx_lock(&lun->lun_lock); 4740 lun->flags |= CTL_LUN_NO_MEDIA; 4741 mtx_unlock(&lun->lun_lock); 4742 return (0); 4743 } 4744 4745 int 4746 ctl_lun_has_media(struct ctl_be_lun *be_lun) 4747 { 4748 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4749 union ctl_ha_msg msg; 4750 4751 mtx_lock(&lun->lun_lock); 4752 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4753 if (lun->flags & CTL_LUN_REMOVABLE) 4754 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4755 mtx_unlock(&lun->lun_lock); 4756 if ((lun->flags & CTL_LUN_REMOVABLE) && 4757 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4758 bzero(&msg.ua, sizeof(msg.ua)); 4759 msg.hdr.msg_type = CTL_MSG_UA; 4760 msg.hdr.nexus.initid = -1; 4761 msg.hdr.nexus.targ_port = -1; 4762 msg.hdr.nexus.targ_lun = lun->lun; 4763 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4764 msg.ua.ua_all = 1; 4765 msg.ua.ua_set = 1; 4766 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4767 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4768 M_WAITOK); 4769 } 4770 return (0); 4771 } 4772 4773 int 4774 ctl_lun_ejected(struct ctl_be_lun *be_lun) 4775 { 4776 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4777 4778 mtx_lock(&lun->lun_lock); 4779 lun->flags |= CTL_LUN_EJECTED; 4780 mtx_unlock(&lun->lun_lock); 4781 return (0); 4782 } 4783 4784 int 4785 ctl_lun_primary(struct ctl_be_lun *be_lun) 4786 { 4787 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4788 4789 mtx_lock(&lun->lun_lock); 4790 lun->flags |= CTL_LUN_PRIMARY_SC; 4791 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4792 mtx_unlock(&lun->lun_lock); 4793 ctl_isc_announce_lun(lun); 4794 return (0); 4795 } 4796 4797 int 4798 ctl_lun_secondary(struct ctl_be_lun *be_lun) 4799 { 4800 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4801 4802 mtx_lock(&lun->lun_lock); 4803 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4804 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4805 mtx_unlock(&lun->lun_lock); 4806 ctl_isc_announce_lun(lun); 4807 return (0); 4808 } 4809 4810 int 4811 ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4812 { 4813 struct ctl_softc *softc; 4814 struct ctl_lun *lun; 4815 4816 lun = (struct ctl_lun *)be_lun->ctl_lun; 4817 softc = lun->ctl_softc; 4818 4819 mtx_lock(&lun->lun_lock); 4820 4821 /* 4822 * The LUN needs to be disabled before it can be marked invalid. 4823 */ 4824 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4825 mtx_unlock(&lun->lun_lock); 4826 return (-1); 4827 } 4828 /* 4829 * Mark the LUN invalid. 4830 */ 4831 lun->flags |= CTL_LUN_INVALID; 4832 4833 /* 4834 * If there is nothing in the OOA queue, go ahead and free the LUN. 4835 * If we have something in the OOA queue, we'll free it when the 4836 * last I/O completes. 4837 */ 4838 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4839 mtx_unlock(&lun->lun_lock); 4840 mtx_lock(&softc->ctl_lock); 4841 ctl_free_lun(lun); 4842 mtx_unlock(&softc->ctl_lock); 4843 } else 4844 mtx_unlock(&lun->lun_lock); 4845 4846 return (0); 4847 } 4848 4849 void 4850 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4851 { 4852 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4853 union ctl_ha_msg msg; 4854 4855 mtx_lock(&lun->lun_lock); 4856 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 4857 mtx_unlock(&lun->lun_lock); 4858 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4859 /* Send msg to other side. */ 4860 bzero(&msg.ua, sizeof(msg.ua)); 4861 msg.hdr.msg_type = CTL_MSG_UA; 4862 msg.hdr.nexus.initid = -1; 4863 msg.hdr.nexus.targ_port = -1; 4864 msg.hdr.nexus.targ_lun = lun->lun; 4865 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4866 msg.ua.ua_all = 1; 4867 msg.ua.ua_set = 1; 4868 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 4869 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4870 M_WAITOK); 4871 } 4872 } 4873 4874 /* 4875 * Backend "memory move is complete" callback for requests that never 4876 * make it down to say RAIDCore's configuration code. 4877 */ 4878 int 4879 ctl_config_move_done(union ctl_io *io) 4880 { 4881 int retval; 4882 4883 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4884 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4885 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 4886 4887 if ((io->io_hdr.port_status != 0) && 4888 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4889 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4890 /* 4891 * For hardware error sense keys, the sense key 4892 * specific value is defined to be a retry count, 4893 * but we use it to pass back an internal FETD 4894 * error code. XXX KDM Hopefully the FETD is only 4895 * using 16 bits for an error code, since that's 4896 * all the space we have in the sks field. 4897 */ 4898 ctl_set_internal_failure(&io->scsiio, 4899 /*sks_valid*/ 1, 4900 /*retry_count*/ 4901 io->io_hdr.port_status); 4902 } 4903 4904 if (ctl_debug & CTL_DEBUG_CDB_DATA) 4905 ctl_data_print(io); 4906 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 4907 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4908 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 4909 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4910 /* 4911 * XXX KDM just assuming a single pointer here, and not a 4912 * S/G list. If we start using S/G lists for config data, 4913 * we'll need to know how to clean them up here as well. 4914 */ 4915 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4916 free(io->scsiio.kern_data_ptr, M_CTL); 4917 ctl_done(io); 4918 retval = CTL_RETVAL_COMPLETE; 4919 } else { 4920 /* 4921 * XXX KDM now we need to continue data movement. Some 4922 * options: 4923 * - call ctl_scsiio() again? We don't do this for data 4924 * writes, because for those at least we know ahead of 4925 * time where the write will go and how long it is. For 4926 * config writes, though, that information is largely 4927 * contained within the write itself, thus we need to 4928 * parse out the data again. 4929 * 4930 * - Call some other function once the data is in? 4931 */ 4932 4933 /* 4934 * XXX KDM call ctl_scsiio() again for now, and check flag 4935 * bits to see whether we're allocated or not. 4936 */ 4937 retval = ctl_scsiio(&io->scsiio); 4938 } 4939 return (retval); 4940 } 4941 4942 /* 4943 * This gets called by a backend driver when it is done with a 4944 * data_submit method. 4945 */ 4946 void 4947 ctl_data_submit_done(union ctl_io *io) 4948 { 4949 /* 4950 * If the IO_CONT flag is set, we need to call the supplied 4951 * function to continue processing the I/O, instead of completing 4952 * the I/O just yet. 4953 * 4954 * If there is an error, though, we don't want to keep processing. 4955 * Instead, just send status back to the initiator. 4956 */ 4957 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4958 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4959 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4960 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4961 io->scsiio.io_cont(io); 4962 return; 4963 } 4964 ctl_done(io); 4965 } 4966 4967 /* 4968 * This gets called by a backend driver when it is done with a 4969 * configuration write. 4970 */ 4971 void 4972 ctl_config_write_done(union ctl_io *io) 4973 { 4974 uint8_t *buf; 4975 4976 /* 4977 * If the IO_CONT flag is set, we need to call the supplied 4978 * function to continue processing the I/O, instead of completing 4979 * the I/O just yet. 4980 * 4981 * If there is an error, though, we don't want to keep processing. 4982 * Instead, just send status back to the initiator. 4983 */ 4984 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4985 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4986 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4987 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4988 io->scsiio.io_cont(io); 4989 return; 4990 } 4991 /* 4992 * Since a configuration write can be done for commands that actually 4993 * have data allocated, like write buffer, and commands that have 4994 * no data, like start/stop unit, we need to check here. 4995 */ 4996 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4997 buf = io->scsiio.kern_data_ptr; 4998 else 4999 buf = NULL; 5000 ctl_done(io); 5001 if (buf) 5002 free(buf, M_CTL); 5003 } 5004 5005 void 5006 ctl_config_read_done(union ctl_io *io) 5007 { 5008 uint8_t *buf; 5009 5010 /* 5011 * If there is some error -- we are done, skip data transfer. 5012 */ 5013 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5014 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5015 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5016 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5017 buf = io->scsiio.kern_data_ptr; 5018 else 5019 buf = NULL; 5020 ctl_done(io); 5021 if (buf) 5022 free(buf, M_CTL); 5023 return; 5024 } 5025 5026 /* 5027 * If the IO_CONT flag is set, we need to call the supplied 5028 * function to continue processing the I/O, instead of completing 5029 * the I/O just yet. 5030 */ 5031 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5032 io->scsiio.io_cont(io); 5033 return; 5034 } 5035 5036 ctl_datamove(io); 5037 } 5038 5039 /* 5040 * SCSI release command. 5041 */ 5042 int 5043 ctl_scsi_release(struct ctl_scsiio *ctsio) 5044 { 5045 struct ctl_lun *lun; 5046 uint32_t residx; 5047 5048 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5049 5050 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5051 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5052 5053 /* 5054 * XXX KDM right now, we only support LUN reservation. We don't 5055 * support 3rd party reservations, or extent reservations, which 5056 * might actually need the parameter list. If we've gotten this 5057 * far, we've got a LUN reservation. Anything else got kicked out 5058 * above. So, according to SPC, ignore the length. 5059 */ 5060 5061 mtx_lock(&lun->lun_lock); 5062 5063 /* 5064 * According to SPC, it is not an error for an intiator to attempt 5065 * to release a reservation on a LUN that isn't reserved, or that 5066 * is reserved by another initiator. The reservation can only be 5067 * released, though, by the initiator who made it or by one of 5068 * several reset type events. 5069 */ 5070 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5071 lun->flags &= ~CTL_LUN_RESERVED; 5072 5073 mtx_unlock(&lun->lun_lock); 5074 5075 ctl_set_success(ctsio); 5076 ctl_done((union ctl_io *)ctsio); 5077 return (CTL_RETVAL_COMPLETE); 5078 } 5079 5080 int 5081 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5082 { 5083 struct ctl_lun *lun; 5084 uint32_t residx; 5085 5086 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5087 5088 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5089 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5090 5091 /* 5092 * XXX KDM right now, we only support LUN reservation. We don't 5093 * support 3rd party reservations, or extent reservations, which 5094 * might actually need the parameter list. If we've gotten this 5095 * far, we've got a LUN reservation. Anything else got kicked out 5096 * above. So, according to SPC, ignore the length. 5097 */ 5098 5099 mtx_lock(&lun->lun_lock); 5100 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5101 ctl_set_reservation_conflict(ctsio); 5102 goto bailout; 5103 } 5104 lun->flags |= CTL_LUN_RESERVED; 5105 lun->res_idx = residx; 5106 ctl_set_success(ctsio); 5107 5108 bailout: 5109 mtx_unlock(&lun->lun_lock); 5110 ctl_done((union ctl_io *)ctsio); 5111 return (CTL_RETVAL_COMPLETE); 5112 } 5113 5114 int 5115 ctl_start_stop(struct ctl_scsiio *ctsio) 5116 { 5117 struct scsi_start_stop_unit *cdb; 5118 struct ctl_lun *lun; 5119 int retval; 5120 5121 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5122 5123 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5124 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5125 5126 if ((cdb->how & SSS_PC_MASK) == 0) { 5127 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5128 (cdb->how & SSS_START) == 0) { 5129 uint32_t residx; 5130 5131 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5132 if (ctl_get_prkey(lun, residx) == 0 || 5133 (lun->pr_res_idx != residx && lun->res_type < 4)) { 5134 5135 ctl_set_reservation_conflict(ctsio); 5136 ctl_done((union ctl_io *)ctsio); 5137 return (CTL_RETVAL_COMPLETE); 5138 } 5139 } 5140 5141 if ((cdb->how & SSS_LOEJ) && 5142 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5143 ctl_set_invalid_field(ctsio, 5144 /*sks_valid*/ 1, 5145 /*command*/ 1, 5146 /*field*/ 4, 5147 /*bit_valid*/ 1, 5148 /*bit*/ 1); 5149 ctl_done((union ctl_io *)ctsio); 5150 return (CTL_RETVAL_COMPLETE); 5151 } 5152 5153 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5154 lun->prevent_count > 0) { 5155 /* "Medium removal prevented" */ 5156 ctl_set_sense(ctsio, /*current_error*/ 1, 5157 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5158 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5159 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5160 ctl_done((union ctl_io *)ctsio); 5161 return (CTL_RETVAL_COMPLETE); 5162 } 5163 } 5164 5165 retval = lun->backend->config_write((union ctl_io *)ctsio); 5166 return (retval); 5167 } 5168 5169 int 5170 ctl_prevent_allow(struct ctl_scsiio *ctsio) 5171 { 5172 struct ctl_lun *lun; 5173 struct scsi_prevent *cdb; 5174 int retval; 5175 uint32_t initidx; 5176 5177 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5178 5179 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5180 cdb = (struct scsi_prevent *)ctsio->cdb; 5181 5182 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) { 5183 ctl_set_invalid_opcode(ctsio); 5184 ctl_done((union ctl_io *)ctsio); 5185 return (CTL_RETVAL_COMPLETE); 5186 } 5187 5188 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5189 mtx_lock(&lun->lun_lock); 5190 if ((cdb->how & PR_PREVENT) && 5191 ctl_is_set(lun->prevent, initidx) == 0) { 5192 ctl_set_mask(lun->prevent, initidx); 5193 lun->prevent_count++; 5194 } else if ((cdb->how & PR_PREVENT) == 0 && 5195 ctl_is_set(lun->prevent, initidx)) { 5196 ctl_clear_mask(lun->prevent, initidx); 5197 lun->prevent_count--; 5198 } 5199 mtx_unlock(&lun->lun_lock); 5200 retval = lun->backend->config_write((union ctl_io *)ctsio); 5201 return (retval); 5202 } 5203 5204 /* 5205 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5206 * we don't really do anything with the LBA and length fields if the user 5207 * passes them in. Instead we'll just flush out the cache for the entire 5208 * LUN. 5209 */ 5210 int 5211 ctl_sync_cache(struct ctl_scsiio *ctsio) 5212 { 5213 struct ctl_lun *lun; 5214 struct ctl_softc *softc; 5215 struct ctl_lba_len_flags *lbalen; 5216 uint64_t starting_lba; 5217 uint32_t block_count; 5218 int retval; 5219 uint8_t byte2; 5220 5221 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5222 5223 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5224 softc = lun->ctl_softc; 5225 retval = 0; 5226 5227 switch (ctsio->cdb[0]) { 5228 case SYNCHRONIZE_CACHE: { 5229 struct scsi_sync_cache *cdb; 5230 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5231 5232 starting_lba = scsi_4btoul(cdb->begin_lba); 5233 block_count = scsi_2btoul(cdb->lb_count); 5234 byte2 = cdb->byte2; 5235 break; 5236 } 5237 case SYNCHRONIZE_CACHE_16: { 5238 struct scsi_sync_cache_16 *cdb; 5239 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5240 5241 starting_lba = scsi_8btou64(cdb->begin_lba); 5242 block_count = scsi_4btoul(cdb->lb_count); 5243 byte2 = cdb->byte2; 5244 break; 5245 } 5246 default: 5247 ctl_set_invalid_opcode(ctsio); 5248 ctl_done((union ctl_io *)ctsio); 5249 goto bailout; 5250 break; /* NOTREACHED */ 5251 } 5252 5253 /* 5254 * We check the LBA and length, but don't do anything with them. 5255 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5256 * get flushed. This check will just help satisfy anyone who wants 5257 * to see an error for an out of range LBA. 5258 */ 5259 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5260 ctl_set_lba_out_of_range(ctsio); 5261 ctl_done((union ctl_io *)ctsio); 5262 goto bailout; 5263 } 5264 5265 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5266 lbalen->lba = starting_lba; 5267 lbalen->len = block_count; 5268 lbalen->flags = byte2; 5269 retval = lun->backend->config_write((union ctl_io *)ctsio); 5270 5271 bailout: 5272 return (retval); 5273 } 5274 5275 int 5276 ctl_format(struct ctl_scsiio *ctsio) 5277 { 5278 struct scsi_format *cdb; 5279 struct ctl_lun *lun; 5280 int length, defect_list_len; 5281 5282 CTL_DEBUG_PRINT(("ctl_format\n")); 5283 5284 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5285 5286 cdb = (struct scsi_format *)ctsio->cdb; 5287 5288 length = 0; 5289 if (cdb->byte2 & SF_FMTDATA) { 5290 if (cdb->byte2 & SF_LONGLIST) 5291 length = sizeof(struct scsi_format_header_long); 5292 else 5293 length = sizeof(struct scsi_format_header_short); 5294 } 5295 5296 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5297 && (length > 0)) { 5298 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5299 ctsio->kern_data_len = length; 5300 ctsio->kern_total_len = length; 5301 ctsio->kern_data_resid = 0; 5302 ctsio->kern_rel_offset = 0; 5303 ctsio->kern_sg_entries = 0; 5304 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5305 ctsio->be_move_done = ctl_config_move_done; 5306 ctl_datamove((union ctl_io *)ctsio); 5307 5308 return (CTL_RETVAL_COMPLETE); 5309 } 5310 5311 defect_list_len = 0; 5312 5313 if (cdb->byte2 & SF_FMTDATA) { 5314 if (cdb->byte2 & SF_LONGLIST) { 5315 struct scsi_format_header_long *header; 5316 5317 header = (struct scsi_format_header_long *) 5318 ctsio->kern_data_ptr; 5319 5320 defect_list_len = scsi_4btoul(header->defect_list_len); 5321 if (defect_list_len != 0) { 5322 ctl_set_invalid_field(ctsio, 5323 /*sks_valid*/ 1, 5324 /*command*/ 0, 5325 /*field*/ 2, 5326 /*bit_valid*/ 0, 5327 /*bit*/ 0); 5328 goto bailout; 5329 } 5330 } else { 5331 struct scsi_format_header_short *header; 5332 5333 header = (struct scsi_format_header_short *) 5334 ctsio->kern_data_ptr; 5335 5336 defect_list_len = scsi_2btoul(header->defect_list_len); 5337 if (defect_list_len != 0) { 5338 ctl_set_invalid_field(ctsio, 5339 /*sks_valid*/ 1, 5340 /*command*/ 0, 5341 /*field*/ 2, 5342 /*bit_valid*/ 0, 5343 /*bit*/ 0); 5344 goto bailout; 5345 } 5346 } 5347 } 5348 5349 ctl_set_success(ctsio); 5350 bailout: 5351 5352 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5353 free(ctsio->kern_data_ptr, M_CTL); 5354 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5355 } 5356 5357 ctl_done((union ctl_io *)ctsio); 5358 return (CTL_RETVAL_COMPLETE); 5359 } 5360 5361 int 5362 ctl_read_buffer(struct ctl_scsiio *ctsio) 5363 { 5364 struct ctl_lun *lun; 5365 uint64_t buffer_offset; 5366 uint32_t len; 5367 uint8_t byte2; 5368 static uint8_t descr[4]; 5369 static uint8_t echo_descr[4] = { 0 }; 5370 5371 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5372 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5373 switch (ctsio->cdb[0]) { 5374 case READ_BUFFER: { 5375 struct scsi_read_buffer *cdb; 5376 5377 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5378 buffer_offset = scsi_3btoul(cdb->offset); 5379 len = scsi_3btoul(cdb->length); 5380 byte2 = cdb->byte2; 5381 break; 5382 } 5383 case READ_BUFFER_16: { 5384 struct scsi_read_buffer_16 *cdb; 5385 5386 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5387 buffer_offset = scsi_8btou64(cdb->offset); 5388 len = scsi_4btoul(cdb->length); 5389 byte2 = cdb->byte2; 5390 break; 5391 } 5392 default: /* This shouldn't happen. */ 5393 ctl_set_invalid_opcode(ctsio); 5394 ctl_done((union ctl_io *)ctsio); 5395 return (CTL_RETVAL_COMPLETE); 5396 } 5397 5398 if ((byte2 & RWB_MODE) != RWB_MODE_DATA && 5399 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5400 (byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5401 ctl_set_invalid_field(ctsio, 5402 /*sks_valid*/ 1, 5403 /*command*/ 1, 5404 /*field*/ 1, 5405 /*bit_valid*/ 1, 5406 /*bit*/ 4); 5407 ctl_done((union ctl_io *)ctsio); 5408 return (CTL_RETVAL_COMPLETE); 5409 } 5410 5411 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5412 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5413 ctl_set_invalid_field(ctsio, 5414 /*sks_valid*/ 1, 5415 /*command*/ 1, 5416 /*field*/ 6, 5417 /*bit_valid*/ 0, 5418 /*bit*/ 0); 5419 ctl_done((union ctl_io *)ctsio); 5420 return (CTL_RETVAL_COMPLETE); 5421 } 5422 5423 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5424 descr[0] = 0; 5425 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5426 ctsio->kern_data_ptr = descr; 5427 len = min(len, sizeof(descr)); 5428 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5429 ctsio->kern_data_ptr = echo_descr; 5430 len = min(len, sizeof(echo_descr)); 5431 } else { 5432 if (lun->write_buffer == NULL) { 5433 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5434 M_CTL, M_WAITOK); 5435 } 5436 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5437 } 5438 ctsio->kern_data_len = len; 5439 ctsio->kern_total_len = len; 5440 ctsio->kern_data_resid = 0; 5441 ctsio->kern_rel_offset = 0; 5442 ctsio->kern_sg_entries = 0; 5443 ctl_set_success(ctsio); 5444 ctsio->be_move_done = ctl_config_move_done; 5445 ctl_datamove((union ctl_io *)ctsio); 5446 return (CTL_RETVAL_COMPLETE); 5447 } 5448 5449 int 5450 ctl_write_buffer(struct ctl_scsiio *ctsio) 5451 { 5452 struct scsi_write_buffer *cdb; 5453 struct ctl_lun *lun; 5454 int buffer_offset, len; 5455 5456 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5457 5458 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5459 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5460 5461 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5462 ctl_set_invalid_field(ctsio, 5463 /*sks_valid*/ 1, 5464 /*command*/ 1, 5465 /*field*/ 1, 5466 /*bit_valid*/ 1, 5467 /*bit*/ 4); 5468 ctl_done((union ctl_io *)ctsio); 5469 return (CTL_RETVAL_COMPLETE); 5470 } 5471 5472 len = scsi_3btoul(cdb->length); 5473 buffer_offset = scsi_3btoul(cdb->offset); 5474 5475 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5476 ctl_set_invalid_field(ctsio, 5477 /*sks_valid*/ 1, 5478 /*command*/ 1, 5479 /*field*/ 6, 5480 /*bit_valid*/ 0, 5481 /*bit*/ 0); 5482 ctl_done((union ctl_io *)ctsio); 5483 return (CTL_RETVAL_COMPLETE); 5484 } 5485 5486 /* 5487 * If we've got a kernel request that hasn't been malloced yet, 5488 * malloc it and tell the caller the data buffer is here. 5489 */ 5490 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5491 if (lun->write_buffer == NULL) { 5492 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5493 M_CTL, M_WAITOK); 5494 } 5495 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5496 ctsio->kern_data_len = len; 5497 ctsio->kern_total_len = len; 5498 ctsio->kern_data_resid = 0; 5499 ctsio->kern_rel_offset = 0; 5500 ctsio->kern_sg_entries = 0; 5501 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5502 ctsio->be_move_done = ctl_config_move_done; 5503 ctl_datamove((union ctl_io *)ctsio); 5504 5505 return (CTL_RETVAL_COMPLETE); 5506 } 5507 5508 ctl_set_success(ctsio); 5509 ctl_done((union ctl_io *)ctsio); 5510 return (CTL_RETVAL_COMPLETE); 5511 } 5512 5513 int 5514 ctl_write_same(struct ctl_scsiio *ctsio) 5515 { 5516 struct ctl_lun *lun; 5517 struct ctl_lba_len_flags *lbalen; 5518 uint64_t lba; 5519 uint32_t num_blocks; 5520 int len, retval; 5521 uint8_t byte2; 5522 5523 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5524 5525 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5526 5527 switch (ctsio->cdb[0]) { 5528 case WRITE_SAME_10: { 5529 struct scsi_write_same_10 *cdb; 5530 5531 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5532 5533 lba = scsi_4btoul(cdb->addr); 5534 num_blocks = scsi_2btoul(cdb->length); 5535 byte2 = cdb->byte2; 5536 break; 5537 } 5538 case WRITE_SAME_16: { 5539 struct scsi_write_same_16 *cdb; 5540 5541 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5542 5543 lba = scsi_8btou64(cdb->addr); 5544 num_blocks = scsi_4btoul(cdb->length); 5545 byte2 = cdb->byte2; 5546 break; 5547 } 5548 default: 5549 /* 5550 * We got a command we don't support. This shouldn't 5551 * happen, commands should be filtered out above us. 5552 */ 5553 ctl_set_invalid_opcode(ctsio); 5554 ctl_done((union ctl_io *)ctsio); 5555 5556 return (CTL_RETVAL_COMPLETE); 5557 break; /* NOTREACHED */ 5558 } 5559 5560 /* ANCHOR flag can be used only together with UNMAP */ 5561 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5562 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5563 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5564 ctl_done((union ctl_io *)ctsio); 5565 return (CTL_RETVAL_COMPLETE); 5566 } 5567 5568 /* 5569 * The first check is to make sure we're in bounds, the second 5570 * check is to catch wrap-around problems. If the lba + num blocks 5571 * is less than the lba, then we've wrapped around and the block 5572 * range is invalid anyway. 5573 */ 5574 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5575 || ((lba + num_blocks) < lba)) { 5576 ctl_set_lba_out_of_range(ctsio); 5577 ctl_done((union ctl_io *)ctsio); 5578 return (CTL_RETVAL_COMPLETE); 5579 } 5580 5581 /* Zero number of blocks means "to the last logical block" */ 5582 if (num_blocks == 0) { 5583 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5584 ctl_set_invalid_field(ctsio, 5585 /*sks_valid*/ 0, 5586 /*command*/ 1, 5587 /*field*/ 0, 5588 /*bit_valid*/ 0, 5589 /*bit*/ 0); 5590 ctl_done((union ctl_io *)ctsio); 5591 return (CTL_RETVAL_COMPLETE); 5592 } 5593 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5594 } 5595 5596 len = lun->be_lun->blocksize; 5597 5598 /* 5599 * If we've got a kernel request that hasn't been malloced yet, 5600 * malloc it and tell the caller the data buffer is here. 5601 */ 5602 if ((byte2 & SWS_NDOB) == 0 && 5603 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5604 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5605 ctsio->kern_data_len = len; 5606 ctsio->kern_total_len = len; 5607 ctsio->kern_data_resid = 0; 5608 ctsio->kern_rel_offset = 0; 5609 ctsio->kern_sg_entries = 0; 5610 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5611 ctsio->be_move_done = ctl_config_move_done; 5612 ctl_datamove((union ctl_io *)ctsio); 5613 5614 return (CTL_RETVAL_COMPLETE); 5615 } 5616 5617 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5618 lbalen->lba = lba; 5619 lbalen->len = num_blocks; 5620 lbalen->flags = byte2; 5621 retval = lun->backend->config_write((union ctl_io *)ctsio); 5622 5623 return (retval); 5624 } 5625 5626 int 5627 ctl_unmap(struct ctl_scsiio *ctsio) 5628 { 5629 struct ctl_lun *lun; 5630 struct scsi_unmap *cdb; 5631 struct ctl_ptr_len_flags *ptrlen; 5632 struct scsi_unmap_header *hdr; 5633 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5634 uint64_t lba; 5635 uint32_t num_blocks; 5636 int len, retval; 5637 uint8_t byte2; 5638 5639 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5640 5641 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5642 cdb = (struct scsi_unmap *)ctsio->cdb; 5643 5644 len = scsi_2btoul(cdb->length); 5645 byte2 = cdb->byte2; 5646 5647 /* 5648 * If we've got a kernel request that hasn't been malloced yet, 5649 * malloc it and tell the caller the data buffer is here. 5650 */ 5651 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5652 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5653 ctsio->kern_data_len = len; 5654 ctsio->kern_total_len = len; 5655 ctsio->kern_data_resid = 0; 5656 ctsio->kern_rel_offset = 0; 5657 ctsio->kern_sg_entries = 0; 5658 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5659 ctsio->be_move_done = ctl_config_move_done; 5660 ctl_datamove((union ctl_io *)ctsio); 5661 5662 return (CTL_RETVAL_COMPLETE); 5663 } 5664 5665 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5666 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5667 if (len < sizeof (*hdr) || 5668 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5669 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5670 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5671 ctl_set_invalid_field(ctsio, 5672 /*sks_valid*/ 0, 5673 /*command*/ 0, 5674 /*field*/ 0, 5675 /*bit_valid*/ 0, 5676 /*bit*/ 0); 5677 goto done; 5678 } 5679 len = scsi_2btoul(hdr->desc_length); 5680 buf = (struct scsi_unmap_desc *)(hdr + 1); 5681 end = buf + len / sizeof(*buf); 5682 5683 endnz = buf; 5684 for (range = buf; range < end; range++) { 5685 lba = scsi_8btou64(range->lba); 5686 num_blocks = scsi_4btoul(range->length); 5687 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5688 || ((lba + num_blocks) < lba)) { 5689 ctl_set_lba_out_of_range(ctsio); 5690 ctl_done((union ctl_io *)ctsio); 5691 return (CTL_RETVAL_COMPLETE); 5692 } 5693 if (num_blocks != 0) 5694 endnz = range + 1; 5695 } 5696 5697 /* 5698 * Block backend can not handle zero last range. 5699 * Filter it out and return if there is nothing left. 5700 */ 5701 len = (uint8_t *)endnz - (uint8_t *)buf; 5702 if (len == 0) { 5703 ctl_set_success(ctsio); 5704 goto done; 5705 } 5706 5707 mtx_lock(&lun->lun_lock); 5708 ptrlen = (struct ctl_ptr_len_flags *) 5709 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5710 ptrlen->ptr = (void *)buf; 5711 ptrlen->len = len; 5712 ptrlen->flags = byte2; 5713 ctl_check_blocked(lun); 5714 mtx_unlock(&lun->lun_lock); 5715 5716 retval = lun->backend->config_write((union ctl_io *)ctsio); 5717 return (retval); 5718 5719 done: 5720 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5721 free(ctsio->kern_data_ptr, M_CTL); 5722 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5723 } 5724 ctl_done((union ctl_io *)ctsio); 5725 return (CTL_RETVAL_COMPLETE); 5726 } 5727 5728 /* 5729 * Note that this function currently doesn't actually do anything inside 5730 * CTL to enforce things if the DQue bit is turned on. 5731 * 5732 * Also note that this function can't be used in the default case, because 5733 * the DQue bit isn't set in the changeable mask for the control mode page 5734 * anyway. This is just here as an example for how to implement a page 5735 * handler, and a placeholder in case we want to allow the user to turn 5736 * tagged queueing on and off. 5737 * 5738 * The D_SENSE bit handling is functional, however, and will turn 5739 * descriptor sense on and off for a given LUN. 5740 */ 5741 int 5742 ctl_control_page_handler(struct ctl_scsiio *ctsio, 5743 struct ctl_page_index *page_index, uint8_t *page_ptr) 5744 { 5745 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5746 struct ctl_lun *lun; 5747 int set_ua; 5748 uint32_t initidx; 5749 5750 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5751 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5752 set_ua = 0; 5753 5754 user_cp = (struct scsi_control_page *)page_ptr; 5755 current_cp = (struct scsi_control_page *) 5756 (page_index->page_data + (page_index->page_len * 5757 CTL_PAGE_CURRENT)); 5758 saved_cp = (struct scsi_control_page *) 5759 (page_index->page_data + (page_index->page_len * 5760 CTL_PAGE_SAVED)); 5761 5762 mtx_lock(&lun->lun_lock); 5763 if (((current_cp->rlec & SCP_DSENSE) == 0) 5764 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5765 /* 5766 * Descriptor sense is currently turned off and the user 5767 * wants to turn it on. 5768 */ 5769 current_cp->rlec |= SCP_DSENSE; 5770 saved_cp->rlec |= SCP_DSENSE; 5771 lun->flags |= CTL_LUN_SENSE_DESC; 5772 set_ua = 1; 5773 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 5774 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 5775 /* 5776 * Descriptor sense is currently turned on, and the user 5777 * wants to turn it off. 5778 */ 5779 current_cp->rlec &= ~SCP_DSENSE; 5780 saved_cp->rlec &= ~SCP_DSENSE; 5781 lun->flags &= ~CTL_LUN_SENSE_DESC; 5782 set_ua = 1; 5783 } 5784 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 5785 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 5786 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5787 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5788 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5789 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5790 set_ua = 1; 5791 } 5792 if ((current_cp->eca_and_aen & SCP_SWP) != 5793 (user_cp->eca_and_aen & SCP_SWP)) { 5794 current_cp->eca_and_aen &= ~SCP_SWP; 5795 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5796 saved_cp->eca_and_aen &= ~SCP_SWP; 5797 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5798 set_ua = 1; 5799 } 5800 if (set_ua != 0) 5801 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5802 mtx_unlock(&lun->lun_lock); 5803 if (set_ua) { 5804 ctl_isc_announce_mode(lun, 5805 ctl_get_initindex(&ctsio->io_hdr.nexus), 5806 page_index->page_code, page_index->subpage); 5807 } 5808 return (0); 5809 } 5810 5811 int 5812 ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 5813 struct ctl_page_index *page_index, uint8_t *page_ptr) 5814 { 5815 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 5816 struct ctl_lun *lun; 5817 int set_ua; 5818 uint32_t initidx; 5819 5820 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5821 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5822 set_ua = 0; 5823 5824 user_cp = (struct scsi_caching_page *)page_ptr; 5825 current_cp = (struct scsi_caching_page *) 5826 (page_index->page_data + (page_index->page_len * 5827 CTL_PAGE_CURRENT)); 5828 saved_cp = (struct scsi_caching_page *) 5829 (page_index->page_data + (page_index->page_len * 5830 CTL_PAGE_SAVED)); 5831 5832 mtx_lock(&lun->lun_lock); 5833 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 5834 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 5835 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5836 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5837 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5838 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5839 set_ua = 1; 5840 } 5841 if (set_ua != 0) 5842 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5843 mtx_unlock(&lun->lun_lock); 5844 if (set_ua) { 5845 ctl_isc_announce_mode(lun, 5846 ctl_get_initindex(&ctsio->io_hdr.nexus), 5847 page_index->page_code, page_index->subpage); 5848 } 5849 return (0); 5850 } 5851 5852 int 5853 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 5854 struct ctl_page_index *page_index, 5855 uint8_t *page_ptr) 5856 { 5857 uint8_t *c; 5858 int i; 5859 5860 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 5861 ctl_time_io_secs = 5862 (c[0] << 8) | 5863 (c[1] << 0) | 5864 0; 5865 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 5866 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 5867 printf("page data:"); 5868 for (i=0; i<8; i++) 5869 printf(" %.2x",page_ptr[i]); 5870 printf("\n"); 5871 return (0); 5872 } 5873 5874 int 5875 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 5876 struct ctl_page_index *page_index, 5877 int pc) 5878 { 5879 struct copan_debugconf_subpage *page; 5880 5881 page = (struct copan_debugconf_subpage *)page_index->page_data + 5882 (page_index->page_len * pc); 5883 5884 switch (pc) { 5885 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 5886 case SMS_PAGE_CTRL_DEFAULT >> 6: 5887 case SMS_PAGE_CTRL_SAVED >> 6: 5888 /* 5889 * We don't update the changable or default bits for this page. 5890 */ 5891 break; 5892 case SMS_PAGE_CTRL_CURRENT >> 6: 5893 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 5894 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 5895 break; 5896 default: 5897 break; 5898 } 5899 return (0); 5900 } 5901 5902 5903 static int 5904 ctl_do_mode_select(union ctl_io *io) 5905 { 5906 struct scsi_mode_page_header *page_header; 5907 struct ctl_page_index *page_index; 5908 struct ctl_scsiio *ctsio; 5909 int page_len, page_len_offset, page_len_size; 5910 union ctl_modepage_info *modepage_info; 5911 struct ctl_lun *lun; 5912 int *len_left, *len_used; 5913 int retval, i; 5914 5915 ctsio = &io->scsiio; 5916 page_index = NULL; 5917 page_len = 0; 5918 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5919 5920 modepage_info = (union ctl_modepage_info *) 5921 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5922 len_left = &modepage_info->header.len_left; 5923 len_used = &modepage_info->header.len_used; 5924 5925 do_next_page: 5926 5927 page_header = (struct scsi_mode_page_header *) 5928 (ctsio->kern_data_ptr + *len_used); 5929 5930 if (*len_left == 0) { 5931 free(ctsio->kern_data_ptr, M_CTL); 5932 ctl_set_success(ctsio); 5933 ctl_done((union ctl_io *)ctsio); 5934 return (CTL_RETVAL_COMPLETE); 5935 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5936 5937 free(ctsio->kern_data_ptr, M_CTL); 5938 ctl_set_param_len_error(ctsio); 5939 ctl_done((union ctl_io *)ctsio); 5940 return (CTL_RETVAL_COMPLETE); 5941 5942 } else if ((page_header->page_code & SMPH_SPF) 5943 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5944 5945 free(ctsio->kern_data_ptr, M_CTL); 5946 ctl_set_param_len_error(ctsio); 5947 ctl_done((union ctl_io *)ctsio); 5948 return (CTL_RETVAL_COMPLETE); 5949 } 5950 5951 5952 /* 5953 * XXX KDM should we do something with the block descriptor? 5954 */ 5955 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5956 page_index = &lun->mode_pages.index[i]; 5957 if (lun->be_lun->lun_type == T_DIRECT && 5958 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 5959 continue; 5960 if (lun->be_lun->lun_type == T_PROCESSOR && 5961 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 5962 continue; 5963 if (lun->be_lun->lun_type == T_CDROM && 5964 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 5965 continue; 5966 5967 if ((page_index->page_code & SMPH_PC_MASK) != 5968 (page_header->page_code & SMPH_PC_MASK)) 5969 continue; 5970 5971 /* 5972 * If neither page has a subpage code, then we've got a 5973 * match. 5974 */ 5975 if (((page_index->page_code & SMPH_SPF) == 0) 5976 && ((page_header->page_code & SMPH_SPF) == 0)) { 5977 page_len = page_header->page_length; 5978 break; 5979 } 5980 5981 /* 5982 * If both pages have subpages, then the subpage numbers 5983 * have to match. 5984 */ 5985 if ((page_index->page_code & SMPH_SPF) 5986 && (page_header->page_code & SMPH_SPF)) { 5987 struct scsi_mode_page_header_sp *sph; 5988 5989 sph = (struct scsi_mode_page_header_sp *)page_header; 5990 if (page_index->subpage == sph->subpage) { 5991 page_len = scsi_2btoul(sph->page_length); 5992 break; 5993 } 5994 } 5995 } 5996 5997 /* 5998 * If we couldn't find the page, or if we don't have a mode select 5999 * handler for it, send back an error to the user. 6000 */ 6001 if ((i >= CTL_NUM_MODE_PAGES) 6002 || (page_index->select_handler == NULL)) { 6003 ctl_set_invalid_field(ctsio, 6004 /*sks_valid*/ 1, 6005 /*command*/ 0, 6006 /*field*/ *len_used, 6007 /*bit_valid*/ 0, 6008 /*bit*/ 0); 6009 free(ctsio->kern_data_ptr, M_CTL); 6010 ctl_done((union ctl_io *)ctsio); 6011 return (CTL_RETVAL_COMPLETE); 6012 } 6013 6014 if (page_index->page_code & SMPH_SPF) { 6015 page_len_offset = 2; 6016 page_len_size = 2; 6017 } else { 6018 page_len_size = 1; 6019 page_len_offset = 1; 6020 } 6021 6022 /* 6023 * If the length the initiator gives us isn't the one we specify in 6024 * the mode page header, or if they didn't specify enough data in 6025 * the CDB to avoid truncating this page, kick out the request. 6026 */ 6027 if ((page_len != (page_index->page_len - page_len_offset - 6028 page_len_size)) 6029 || (*len_left < page_index->page_len)) { 6030 6031 6032 ctl_set_invalid_field(ctsio, 6033 /*sks_valid*/ 1, 6034 /*command*/ 0, 6035 /*field*/ *len_used + page_len_offset, 6036 /*bit_valid*/ 0, 6037 /*bit*/ 0); 6038 free(ctsio->kern_data_ptr, M_CTL); 6039 ctl_done((union ctl_io *)ctsio); 6040 return (CTL_RETVAL_COMPLETE); 6041 } 6042 6043 /* 6044 * Run through the mode page, checking to make sure that the bits 6045 * the user changed are actually legal for him to change. 6046 */ 6047 for (i = 0; i < page_index->page_len; i++) { 6048 uint8_t *user_byte, *change_mask, *current_byte; 6049 int bad_bit; 6050 int j; 6051 6052 user_byte = (uint8_t *)page_header + i; 6053 change_mask = page_index->page_data + 6054 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6055 current_byte = page_index->page_data + 6056 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6057 6058 /* 6059 * Check to see whether the user set any bits in this byte 6060 * that he is not allowed to set. 6061 */ 6062 if ((*user_byte & ~(*change_mask)) == 6063 (*current_byte & ~(*change_mask))) 6064 continue; 6065 6066 /* 6067 * Go through bit by bit to determine which one is illegal. 6068 */ 6069 bad_bit = 0; 6070 for (j = 7; j >= 0; j--) { 6071 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6072 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6073 bad_bit = i; 6074 break; 6075 } 6076 } 6077 ctl_set_invalid_field(ctsio, 6078 /*sks_valid*/ 1, 6079 /*command*/ 0, 6080 /*field*/ *len_used + i, 6081 /*bit_valid*/ 1, 6082 /*bit*/ bad_bit); 6083 free(ctsio->kern_data_ptr, M_CTL); 6084 ctl_done((union ctl_io *)ctsio); 6085 return (CTL_RETVAL_COMPLETE); 6086 } 6087 6088 /* 6089 * Decrement these before we call the page handler, since we may 6090 * end up getting called back one way or another before the handler 6091 * returns to this context. 6092 */ 6093 *len_left -= page_index->page_len; 6094 *len_used += page_index->page_len; 6095 6096 retval = page_index->select_handler(ctsio, page_index, 6097 (uint8_t *)page_header); 6098 6099 /* 6100 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6101 * wait until this queued command completes to finish processing 6102 * the mode page. If it returns anything other than 6103 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6104 * already set the sense information, freed the data pointer, and 6105 * completed the io for us. 6106 */ 6107 if (retval != CTL_RETVAL_COMPLETE) 6108 goto bailout_no_done; 6109 6110 /* 6111 * If the initiator sent us more than one page, parse the next one. 6112 */ 6113 if (*len_left > 0) 6114 goto do_next_page; 6115 6116 ctl_set_success(ctsio); 6117 free(ctsio->kern_data_ptr, M_CTL); 6118 ctl_done((union ctl_io *)ctsio); 6119 6120 bailout_no_done: 6121 6122 return (CTL_RETVAL_COMPLETE); 6123 6124 } 6125 6126 int 6127 ctl_mode_select(struct ctl_scsiio *ctsio) 6128 { 6129 int param_len, pf, sp; 6130 int header_size, bd_len; 6131 union ctl_modepage_info *modepage_info; 6132 6133 switch (ctsio->cdb[0]) { 6134 case MODE_SELECT_6: { 6135 struct scsi_mode_select_6 *cdb; 6136 6137 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6138 6139 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6140 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6141 param_len = cdb->length; 6142 header_size = sizeof(struct scsi_mode_header_6); 6143 break; 6144 } 6145 case MODE_SELECT_10: { 6146 struct scsi_mode_select_10 *cdb; 6147 6148 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6149 6150 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6151 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6152 param_len = scsi_2btoul(cdb->length); 6153 header_size = sizeof(struct scsi_mode_header_10); 6154 break; 6155 } 6156 default: 6157 ctl_set_invalid_opcode(ctsio); 6158 ctl_done((union ctl_io *)ctsio); 6159 return (CTL_RETVAL_COMPLETE); 6160 } 6161 6162 /* 6163 * From SPC-3: 6164 * "A parameter list length of zero indicates that the Data-Out Buffer 6165 * shall be empty. This condition shall not be considered as an error." 6166 */ 6167 if (param_len == 0) { 6168 ctl_set_success(ctsio); 6169 ctl_done((union ctl_io *)ctsio); 6170 return (CTL_RETVAL_COMPLETE); 6171 } 6172 6173 /* 6174 * Since we'll hit this the first time through, prior to 6175 * allocation, we don't need to free a data buffer here. 6176 */ 6177 if (param_len < header_size) { 6178 ctl_set_param_len_error(ctsio); 6179 ctl_done((union ctl_io *)ctsio); 6180 return (CTL_RETVAL_COMPLETE); 6181 } 6182 6183 /* 6184 * Allocate the data buffer and grab the user's data. In theory, 6185 * we shouldn't have to sanity check the parameter list length here 6186 * because the maximum size is 64K. We should be able to malloc 6187 * that much without too many problems. 6188 */ 6189 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6190 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6191 ctsio->kern_data_len = param_len; 6192 ctsio->kern_total_len = param_len; 6193 ctsio->kern_data_resid = 0; 6194 ctsio->kern_rel_offset = 0; 6195 ctsio->kern_sg_entries = 0; 6196 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6197 ctsio->be_move_done = ctl_config_move_done; 6198 ctl_datamove((union ctl_io *)ctsio); 6199 6200 return (CTL_RETVAL_COMPLETE); 6201 } 6202 6203 switch (ctsio->cdb[0]) { 6204 case MODE_SELECT_6: { 6205 struct scsi_mode_header_6 *mh6; 6206 6207 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6208 bd_len = mh6->blk_desc_len; 6209 break; 6210 } 6211 case MODE_SELECT_10: { 6212 struct scsi_mode_header_10 *mh10; 6213 6214 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6215 bd_len = scsi_2btoul(mh10->blk_desc_len); 6216 break; 6217 } 6218 default: 6219 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6220 break; 6221 } 6222 6223 if (param_len < (header_size + bd_len)) { 6224 free(ctsio->kern_data_ptr, M_CTL); 6225 ctl_set_param_len_error(ctsio); 6226 ctl_done((union ctl_io *)ctsio); 6227 return (CTL_RETVAL_COMPLETE); 6228 } 6229 6230 /* 6231 * Set the IO_CONT flag, so that if this I/O gets passed to 6232 * ctl_config_write_done(), it'll get passed back to 6233 * ctl_do_mode_select() for further processing, or completion if 6234 * we're all done. 6235 */ 6236 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6237 ctsio->io_cont = ctl_do_mode_select; 6238 6239 modepage_info = (union ctl_modepage_info *) 6240 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6241 memset(modepage_info, 0, sizeof(*modepage_info)); 6242 modepage_info->header.len_left = param_len - header_size - bd_len; 6243 modepage_info->header.len_used = header_size + bd_len; 6244 6245 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6246 } 6247 6248 int 6249 ctl_mode_sense(struct ctl_scsiio *ctsio) 6250 { 6251 struct ctl_lun *lun; 6252 int pc, page_code, dbd, llba, subpage; 6253 int alloc_len, page_len, header_len, total_len; 6254 struct scsi_mode_block_descr *block_desc; 6255 struct ctl_page_index *page_index; 6256 6257 dbd = 0; 6258 llba = 0; 6259 block_desc = NULL; 6260 6261 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6262 6263 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6264 switch (ctsio->cdb[0]) { 6265 case MODE_SENSE_6: { 6266 struct scsi_mode_sense_6 *cdb; 6267 6268 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6269 6270 header_len = sizeof(struct scsi_mode_hdr_6); 6271 if (cdb->byte2 & SMS_DBD) 6272 dbd = 1; 6273 else 6274 header_len += sizeof(struct scsi_mode_block_descr); 6275 6276 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6277 page_code = cdb->page & SMS_PAGE_CODE; 6278 subpage = cdb->subpage; 6279 alloc_len = cdb->length; 6280 break; 6281 } 6282 case MODE_SENSE_10: { 6283 struct scsi_mode_sense_10 *cdb; 6284 6285 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6286 6287 header_len = sizeof(struct scsi_mode_hdr_10); 6288 6289 if (cdb->byte2 & SMS_DBD) 6290 dbd = 1; 6291 else 6292 header_len += sizeof(struct scsi_mode_block_descr); 6293 if (cdb->byte2 & SMS10_LLBAA) 6294 llba = 1; 6295 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6296 page_code = cdb->page & SMS_PAGE_CODE; 6297 subpage = cdb->subpage; 6298 alloc_len = scsi_2btoul(cdb->length); 6299 break; 6300 } 6301 default: 6302 ctl_set_invalid_opcode(ctsio); 6303 ctl_done((union ctl_io *)ctsio); 6304 return (CTL_RETVAL_COMPLETE); 6305 break; /* NOTREACHED */ 6306 } 6307 6308 /* 6309 * We have to make a first pass through to calculate the size of 6310 * the pages that match the user's query. Then we allocate enough 6311 * memory to hold it, and actually copy the data into the buffer. 6312 */ 6313 switch (page_code) { 6314 case SMS_ALL_PAGES_PAGE: { 6315 int i; 6316 6317 page_len = 0; 6318 6319 /* 6320 * At the moment, values other than 0 and 0xff here are 6321 * reserved according to SPC-3. 6322 */ 6323 if ((subpage != SMS_SUBPAGE_PAGE_0) 6324 && (subpage != SMS_SUBPAGE_ALL)) { 6325 ctl_set_invalid_field(ctsio, 6326 /*sks_valid*/ 1, 6327 /*command*/ 1, 6328 /*field*/ 3, 6329 /*bit_valid*/ 0, 6330 /*bit*/ 0); 6331 ctl_done((union ctl_io *)ctsio); 6332 return (CTL_RETVAL_COMPLETE); 6333 } 6334 6335 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6336 page_index = &lun->mode_pages.index[i]; 6337 6338 /* Make sure the page is supported for this dev type */ 6339 if (lun->be_lun->lun_type == T_DIRECT && 6340 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6341 continue; 6342 if (lun->be_lun->lun_type == T_PROCESSOR && 6343 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6344 continue; 6345 if (lun->be_lun->lun_type == T_CDROM && 6346 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6347 continue; 6348 6349 /* 6350 * We don't use this subpage if the user didn't 6351 * request all subpages. 6352 */ 6353 if ((page_index->subpage != 0) 6354 && (subpage == SMS_SUBPAGE_PAGE_0)) 6355 continue; 6356 6357 #if 0 6358 printf("found page %#x len %d\n", 6359 page_index->page_code & SMPH_PC_MASK, 6360 page_index->page_len); 6361 #endif 6362 page_len += page_index->page_len; 6363 } 6364 break; 6365 } 6366 default: { 6367 int i; 6368 6369 page_len = 0; 6370 6371 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6372 page_index = &lun->mode_pages.index[i]; 6373 6374 /* Make sure the page is supported for this dev type */ 6375 if (lun->be_lun->lun_type == T_DIRECT && 6376 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6377 continue; 6378 if (lun->be_lun->lun_type == T_PROCESSOR && 6379 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6380 continue; 6381 if (lun->be_lun->lun_type == T_CDROM && 6382 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6383 continue; 6384 6385 /* Look for the right page code */ 6386 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6387 continue; 6388 6389 /* Look for the right subpage or the subpage wildcard*/ 6390 if ((page_index->subpage != subpage) 6391 && (subpage != SMS_SUBPAGE_ALL)) 6392 continue; 6393 6394 #if 0 6395 printf("found page %#x len %d\n", 6396 page_index->page_code & SMPH_PC_MASK, 6397 page_index->page_len); 6398 #endif 6399 6400 page_len += page_index->page_len; 6401 } 6402 6403 if (page_len == 0) { 6404 ctl_set_invalid_field(ctsio, 6405 /*sks_valid*/ 1, 6406 /*command*/ 1, 6407 /*field*/ 2, 6408 /*bit_valid*/ 1, 6409 /*bit*/ 5); 6410 ctl_done((union ctl_io *)ctsio); 6411 return (CTL_RETVAL_COMPLETE); 6412 } 6413 break; 6414 } 6415 } 6416 6417 total_len = header_len + page_len; 6418 #if 0 6419 printf("header_len = %d, page_len = %d, total_len = %d\n", 6420 header_len, page_len, total_len); 6421 #endif 6422 6423 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6424 ctsio->kern_sg_entries = 0; 6425 ctsio->kern_data_resid = 0; 6426 ctsio->kern_rel_offset = 0; 6427 if (total_len < alloc_len) { 6428 ctsio->residual = alloc_len - total_len; 6429 ctsio->kern_data_len = total_len; 6430 ctsio->kern_total_len = total_len; 6431 } else { 6432 ctsio->residual = 0; 6433 ctsio->kern_data_len = alloc_len; 6434 ctsio->kern_total_len = alloc_len; 6435 } 6436 6437 switch (ctsio->cdb[0]) { 6438 case MODE_SENSE_6: { 6439 struct scsi_mode_hdr_6 *header; 6440 6441 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6442 6443 header->datalen = MIN(total_len - 1, 254); 6444 if (lun->be_lun->lun_type == T_DIRECT) { 6445 header->dev_specific = 0x10; /* DPOFUA */ 6446 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6447 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6448 .eca_and_aen & SCP_SWP) != 0) 6449 header->dev_specific |= 0x80; /* WP */ 6450 } 6451 if (dbd) 6452 header->block_descr_len = 0; 6453 else 6454 header->block_descr_len = 6455 sizeof(struct scsi_mode_block_descr); 6456 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6457 break; 6458 } 6459 case MODE_SENSE_10: { 6460 struct scsi_mode_hdr_10 *header; 6461 int datalen; 6462 6463 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6464 6465 datalen = MIN(total_len - 2, 65533); 6466 scsi_ulto2b(datalen, header->datalen); 6467 if (lun->be_lun->lun_type == T_DIRECT) { 6468 header->dev_specific = 0x10; /* DPOFUA */ 6469 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6470 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6471 .eca_and_aen & SCP_SWP) != 0) 6472 header->dev_specific |= 0x80; /* WP */ 6473 } 6474 if (dbd) 6475 scsi_ulto2b(0, header->block_descr_len); 6476 else 6477 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6478 header->block_descr_len); 6479 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6480 break; 6481 } 6482 default: 6483 panic("invalid CDB type %#x", ctsio->cdb[0]); 6484 break; /* NOTREACHED */ 6485 } 6486 6487 /* 6488 * If we've got a disk, use its blocksize in the block 6489 * descriptor. Otherwise, just set it to 0. 6490 */ 6491 if (dbd == 0) { 6492 if (lun->be_lun->lun_type == T_DIRECT) 6493 scsi_ulto3b(lun->be_lun->blocksize, 6494 block_desc->block_len); 6495 else 6496 scsi_ulto3b(0, block_desc->block_len); 6497 } 6498 6499 switch (page_code) { 6500 case SMS_ALL_PAGES_PAGE: { 6501 int i, data_used; 6502 6503 data_used = header_len; 6504 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6505 struct ctl_page_index *page_index; 6506 6507 page_index = &lun->mode_pages.index[i]; 6508 if (lun->be_lun->lun_type == T_DIRECT && 6509 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6510 continue; 6511 if (lun->be_lun->lun_type == T_PROCESSOR && 6512 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6513 continue; 6514 if (lun->be_lun->lun_type == T_CDROM && 6515 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6516 continue; 6517 6518 /* 6519 * We don't use this subpage if the user didn't 6520 * request all subpages. We already checked (above) 6521 * to make sure the user only specified a subpage 6522 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6523 */ 6524 if ((page_index->subpage != 0) 6525 && (subpage == SMS_SUBPAGE_PAGE_0)) 6526 continue; 6527 6528 /* 6529 * Call the handler, if it exists, to update the 6530 * page to the latest values. 6531 */ 6532 if (page_index->sense_handler != NULL) 6533 page_index->sense_handler(ctsio, page_index,pc); 6534 6535 memcpy(ctsio->kern_data_ptr + data_used, 6536 page_index->page_data + 6537 (page_index->page_len * pc), 6538 page_index->page_len); 6539 data_used += page_index->page_len; 6540 } 6541 break; 6542 } 6543 default: { 6544 int i, data_used; 6545 6546 data_used = header_len; 6547 6548 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6549 struct ctl_page_index *page_index; 6550 6551 page_index = &lun->mode_pages.index[i]; 6552 6553 /* Look for the right page code */ 6554 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6555 continue; 6556 6557 /* Look for the right subpage or the subpage wildcard*/ 6558 if ((page_index->subpage != subpage) 6559 && (subpage != SMS_SUBPAGE_ALL)) 6560 continue; 6561 6562 /* Make sure the page is supported for this dev type */ 6563 if (lun->be_lun->lun_type == T_DIRECT && 6564 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6565 continue; 6566 if (lun->be_lun->lun_type == T_PROCESSOR && 6567 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6568 continue; 6569 if (lun->be_lun->lun_type == T_CDROM && 6570 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6571 continue; 6572 6573 /* 6574 * Call the handler, if it exists, to update the 6575 * page to the latest values. 6576 */ 6577 if (page_index->sense_handler != NULL) 6578 page_index->sense_handler(ctsio, page_index,pc); 6579 6580 memcpy(ctsio->kern_data_ptr + data_used, 6581 page_index->page_data + 6582 (page_index->page_len * pc), 6583 page_index->page_len); 6584 data_used += page_index->page_len; 6585 } 6586 break; 6587 } 6588 } 6589 6590 ctl_set_success(ctsio); 6591 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6592 ctsio->be_move_done = ctl_config_move_done; 6593 ctl_datamove((union ctl_io *)ctsio); 6594 return (CTL_RETVAL_COMPLETE); 6595 } 6596 6597 int 6598 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6599 struct ctl_page_index *page_index, 6600 int pc) 6601 { 6602 struct ctl_lun *lun; 6603 struct scsi_log_param_header *phdr; 6604 uint8_t *data; 6605 uint64_t val; 6606 6607 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6608 data = page_index->page_data; 6609 6610 if (lun->backend->lun_attr != NULL && 6611 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6612 != UINT64_MAX) { 6613 phdr = (struct scsi_log_param_header *)data; 6614 scsi_ulto2b(0x0001, phdr->param_code); 6615 phdr->param_control = SLP_LBIN | SLP_LP; 6616 phdr->param_len = 8; 6617 data = (uint8_t *)(phdr + 1); 6618 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6619 data[4] = 0x02; /* per-pool */ 6620 data += phdr->param_len; 6621 } 6622 6623 if (lun->backend->lun_attr != NULL && 6624 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6625 != UINT64_MAX) { 6626 phdr = (struct scsi_log_param_header *)data; 6627 scsi_ulto2b(0x0002, phdr->param_code); 6628 phdr->param_control = SLP_LBIN | SLP_LP; 6629 phdr->param_len = 8; 6630 data = (uint8_t *)(phdr + 1); 6631 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6632 data[4] = 0x01; /* per-LUN */ 6633 data += phdr->param_len; 6634 } 6635 6636 if (lun->backend->lun_attr != NULL && 6637 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6638 != UINT64_MAX) { 6639 phdr = (struct scsi_log_param_header *)data; 6640 scsi_ulto2b(0x00f1, phdr->param_code); 6641 phdr->param_control = SLP_LBIN | SLP_LP; 6642 phdr->param_len = 8; 6643 data = (uint8_t *)(phdr + 1); 6644 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6645 data[4] = 0x02; /* per-pool */ 6646 data += phdr->param_len; 6647 } 6648 6649 if (lun->backend->lun_attr != NULL && 6650 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6651 != UINT64_MAX) { 6652 phdr = (struct scsi_log_param_header *)data; 6653 scsi_ulto2b(0x00f2, phdr->param_code); 6654 phdr->param_control = SLP_LBIN | SLP_LP; 6655 phdr->param_len = 8; 6656 data = (uint8_t *)(phdr + 1); 6657 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6658 data[4] = 0x02; /* per-pool */ 6659 data += phdr->param_len; 6660 } 6661 6662 page_index->page_len = data - page_index->page_data; 6663 return (0); 6664 } 6665 6666 int 6667 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6668 struct ctl_page_index *page_index, 6669 int pc) 6670 { 6671 struct ctl_lun *lun; 6672 struct stat_page *data; 6673 uint64_t rn, wn, rb, wb; 6674 struct bintime rt, wt; 6675 int i; 6676 6677 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6678 data = (struct stat_page *)page_index->page_data; 6679 6680 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6681 data->sap.hdr.param_control = SLP_LBIN; 6682 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6683 sizeof(struct scsi_log_param_header); 6684 rn = wn = rb = wb = 0; 6685 bintime_clear(&rt); 6686 bintime_clear(&wt); 6687 for (i = 0; i < CTL_MAX_PORTS; i++) { 6688 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6689 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6690 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6691 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6692 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6693 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6694 } 6695 scsi_u64to8b(rn, data->sap.read_num); 6696 scsi_u64to8b(wn, data->sap.write_num); 6697 if (lun->stats.blocksize > 0) { 6698 scsi_u64to8b(wb / lun->stats.blocksize, 6699 data->sap.recvieved_lba); 6700 scsi_u64to8b(rb / lun->stats.blocksize, 6701 data->sap.transmitted_lba); 6702 } 6703 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6704 data->sap.read_int); 6705 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6706 data->sap.write_int); 6707 scsi_u64to8b(0, data->sap.weighted_num); 6708 scsi_u64to8b(0, data->sap.weighted_int); 6709 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6710 data->it.hdr.param_control = SLP_LBIN; 6711 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6712 sizeof(struct scsi_log_param_header); 6713 #ifdef CTL_TIME_IO 6714 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6715 #endif 6716 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6717 data->it.hdr.param_control = SLP_LBIN; 6718 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6719 sizeof(struct scsi_log_param_header); 6720 scsi_ulto4b(3, data->ti.exponent); 6721 scsi_ulto4b(1, data->ti.integer); 6722 6723 page_index->page_len = sizeof(*data); 6724 return (0); 6725 } 6726 6727 int 6728 ctl_log_sense(struct ctl_scsiio *ctsio) 6729 { 6730 struct ctl_lun *lun; 6731 int i, pc, page_code, subpage; 6732 int alloc_len, total_len; 6733 struct ctl_page_index *page_index; 6734 struct scsi_log_sense *cdb; 6735 struct scsi_log_header *header; 6736 6737 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6738 6739 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6740 cdb = (struct scsi_log_sense *)ctsio->cdb; 6741 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6742 page_code = cdb->page & SLS_PAGE_CODE; 6743 subpage = cdb->subpage; 6744 alloc_len = scsi_2btoul(cdb->length); 6745 6746 page_index = NULL; 6747 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6748 page_index = &lun->log_pages.index[i]; 6749 6750 /* Look for the right page code */ 6751 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6752 continue; 6753 6754 /* Look for the right subpage or the subpage wildcard*/ 6755 if (page_index->subpage != subpage) 6756 continue; 6757 6758 break; 6759 } 6760 if (i >= CTL_NUM_LOG_PAGES) { 6761 ctl_set_invalid_field(ctsio, 6762 /*sks_valid*/ 1, 6763 /*command*/ 1, 6764 /*field*/ 2, 6765 /*bit_valid*/ 0, 6766 /*bit*/ 0); 6767 ctl_done((union ctl_io *)ctsio); 6768 return (CTL_RETVAL_COMPLETE); 6769 } 6770 6771 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6772 6773 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6774 ctsio->kern_sg_entries = 0; 6775 ctsio->kern_data_resid = 0; 6776 ctsio->kern_rel_offset = 0; 6777 if (total_len < alloc_len) { 6778 ctsio->residual = alloc_len - total_len; 6779 ctsio->kern_data_len = total_len; 6780 ctsio->kern_total_len = total_len; 6781 } else { 6782 ctsio->residual = 0; 6783 ctsio->kern_data_len = alloc_len; 6784 ctsio->kern_total_len = alloc_len; 6785 } 6786 6787 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6788 header->page = page_index->page_code; 6789 if (page_index->subpage) { 6790 header->page |= SL_SPF; 6791 header->subpage = page_index->subpage; 6792 } 6793 scsi_ulto2b(page_index->page_len, header->datalen); 6794 6795 /* 6796 * Call the handler, if it exists, to update the 6797 * page to the latest values. 6798 */ 6799 if (page_index->sense_handler != NULL) 6800 page_index->sense_handler(ctsio, page_index, pc); 6801 6802 memcpy(header + 1, page_index->page_data, page_index->page_len); 6803 6804 ctl_set_success(ctsio); 6805 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6806 ctsio->be_move_done = ctl_config_move_done; 6807 ctl_datamove((union ctl_io *)ctsio); 6808 return (CTL_RETVAL_COMPLETE); 6809 } 6810 6811 int 6812 ctl_read_capacity(struct ctl_scsiio *ctsio) 6813 { 6814 struct scsi_read_capacity *cdb; 6815 struct scsi_read_capacity_data *data; 6816 struct ctl_lun *lun; 6817 uint32_t lba; 6818 6819 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6820 6821 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6822 6823 lba = scsi_4btoul(cdb->addr); 6824 if (((cdb->pmi & SRC_PMI) == 0) 6825 && (lba != 0)) { 6826 ctl_set_invalid_field(/*ctsio*/ ctsio, 6827 /*sks_valid*/ 1, 6828 /*command*/ 1, 6829 /*field*/ 2, 6830 /*bit_valid*/ 0, 6831 /*bit*/ 0); 6832 ctl_done((union ctl_io *)ctsio); 6833 return (CTL_RETVAL_COMPLETE); 6834 } 6835 6836 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6837 6838 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6839 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6840 ctsio->residual = 0; 6841 ctsio->kern_data_len = sizeof(*data); 6842 ctsio->kern_total_len = sizeof(*data); 6843 ctsio->kern_data_resid = 0; 6844 ctsio->kern_rel_offset = 0; 6845 ctsio->kern_sg_entries = 0; 6846 6847 /* 6848 * If the maximum LBA is greater than 0xfffffffe, the user must 6849 * issue a SERVICE ACTION IN (16) command, with the read capacity 6850 * serivce action set. 6851 */ 6852 if (lun->be_lun->maxlba > 0xfffffffe) 6853 scsi_ulto4b(0xffffffff, data->addr); 6854 else 6855 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6856 6857 /* 6858 * XXX KDM this may not be 512 bytes... 6859 */ 6860 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6861 6862 ctl_set_success(ctsio); 6863 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6864 ctsio->be_move_done = ctl_config_move_done; 6865 ctl_datamove((union ctl_io *)ctsio); 6866 return (CTL_RETVAL_COMPLETE); 6867 } 6868 6869 int 6870 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6871 { 6872 struct scsi_read_capacity_16 *cdb; 6873 struct scsi_read_capacity_data_long *data; 6874 struct ctl_lun *lun; 6875 uint64_t lba; 6876 uint32_t alloc_len; 6877 6878 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6879 6880 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6881 6882 alloc_len = scsi_4btoul(cdb->alloc_len); 6883 lba = scsi_8btou64(cdb->addr); 6884 6885 if ((cdb->reladr & SRC16_PMI) 6886 && (lba != 0)) { 6887 ctl_set_invalid_field(/*ctsio*/ ctsio, 6888 /*sks_valid*/ 1, 6889 /*command*/ 1, 6890 /*field*/ 2, 6891 /*bit_valid*/ 0, 6892 /*bit*/ 0); 6893 ctl_done((union ctl_io *)ctsio); 6894 return (CTL_RETVAL_COMPLETE); 6895 } 6896 6897 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6898 6899 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6900 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6901 6902 if (sizeof(*data) < alloc_len) { 6903 ctsio->residual = alloc_len - sizeof(*data); 6904 ctsio->kern_data_len = sizeof(*data); 6905 ctsio->kern_total_len = sizeof(*data); 6906 } else { 6907 ctsio->residual = 0; 6908 ctsio->kern_data_len = alloc_len; 6909 ctsio->kern_total_len = alloc_len; 6910 } 6911 ctsio->kern_data_resid = 0; 6912 ctsio->kern_rel_offset = 0; 6913 ctsio->kern_sg_entries = 0; 6914 6915 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6916 /* XXX KDM this may not be 512 bytes... */ 6917 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6918 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6919 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6920 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6921 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6922 6923 ctl_set_success(ctsio); 6924 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6925 ctsio->be_move_done = ctl_config_move_done; 6926 ctl_datamove((union ctl_io *)ctsio); 6927 return (CTL_RETVAL_COMPLETE); 6928 } 6929 6930 int 6931 ctl_get_lba_status(struct ctl_scsiio *ctsio) 6932 { 6933 struct scsi_get_lba_status *cdb; 6934 struct scsi_get_lba_status_data *data; 6935 struct ctl_lun *lun; 6936 struct ctl_lba_len_flags *lbalen; 6937 uint64_t lba; 6938 uint32_t alloc_len, total_len; 6939 int retval; 6940 6941 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 6942 6943 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6944 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 6945 lba = scsi_8btou64(cdb->addr); 6946 alloc_len = scsi_4btoul(cdb->alloc_len); 6947 6948 if (lba > lun->be_lun->maxlba) { 6949 ctl_set_lba_out_of_range(ctsio); 6950 ctl_done((union ctl_io *)ctsio); 6951 return (CTL_RETVAL_COMPLETE); 6952 } 6953 6954 total_len = sizeof(*data) + sizeof(data->descr[0]); 6955 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6956 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 6957 6958 if (total_len < alloc_len) { 6959 ctsio->residual = alloc_len - total_len; 6960 ctsio->kern_data_len = total_len; 6961 ctsio->kern_total_len = total_len; 6962 } else { 6963 ctsio->residual = 0; 6964 ctsio->kern_data_len = alloc_len; 6965 ctsio->kern_total_len = alloc_len; 6966 } 6967 ctsio->kern_data_resid = 0; 6968 ctsio->kern_rel_offset = 0; 6969 ctsio->kern_sg_entries = 0; 6970 6971 /* Fill dummy data in case backend can't tell anything. */ 6972 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 6973 scsi_u64to8b(lba, data->descr[0].addr); 6974 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 6975 data->descr[0].length); 6976 data->descr[0].status = 0; /* Mapped or unknown. */ 6977 6978 ctl_set_success(ctsio); 6979 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6980 ctsio->be_move_done = ctl_config_move_done; 6981 6982 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6983 lbalen->lba = lba; 6984 lbalen->len = total_len; 6985 lbalen->flags = 0; 6986 retval = lun->backend->config_read((union ctl_io *)ctsio); 6987 return (CTL_RETVAL_COMPLETE); 6988 } 6989 6990 int 6991 ctl_read_defect(struct ctl_scsiio *ctsio) 6992 { 6993 struct scsi_read_defect_data_10 *ccb10; 6994 struct scsi_read_defect_data_12 *ccb12; 6995 struct scsi_read_defect_data_hdr_10 *data10; 6996 struct scsi_read_defect_data_hdr_12 *data12; 6997 uint32_t alloc_len, data_len; 6998 uint8_t format; 6999 7000 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7001 7002 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7003 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7004 format = ccb10->format; 7005 alloc_len = scsi_2btoul(ccb10->alloc_length); 7006 data_len = sizeof(*data10); 7007 } else { 7008 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7009 format = ccb12->format; 7010 alloc_len = scsi_4btoul(ccb12->alloc_length); 7011 data_len = sizeof(*data12); 7012 } 7013 if (alloc_len == 0) { 7014 ctl_set_success(ctsio); 7015 ctl_done((union ctl_io *)ctsio); 7016 return (CTL_RETVAL_COMPLETE); 7017 } 7018 7019 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7020 if (data_len < alloc_len) { 7021 ctsio->residual = alloc_len - data_len; 7022 ctsio->kern_data_len = data_len; 7023 ctsio->kern_total_len = data_len; 7024 } else { 7025 ctsio->residual = 0; 7026 ctsio->kern_data_len = alloc_len; 7027 ctsio->kern_total_len = alloc_len; 7028 } 7029 ctsio->kern_data_resid = 0; 7030 ctsio->kern_rel_offset = 0; 7031 ctsio->kern_sg_entries = 0; 7032 7033 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7034 data10 = (struct scsi_read_defect_data_hdr_10 *) 7035 ctsio->kern_data_ptr; 7036 data10->format = format; 7037 scsi_ulto2b(0, data10->length); 7038 } else { 7039 data12 = (struct scsi_read_defect_data_hdr_12 *) 7040 ctsio->kern_data_ptr; 7041 data12->format = format; 7042 scsi_ulto2b(0, data12->generation); 7043 scsi_ulto4b(0, data12->length); 7044 } 7045 7046 ctl_set_success(ctsio); 7047 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7048 ctsio->be_move_done = ctl_config_move_done; 7049 ctl_datamove((union ctl_io *)ctsio); 7050 return (CTL_RETVAL_COMPLETE); 7051 } 7052 7053 int 7054 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7055 { 7056 struct scsi_maintenance_in *cdb; 7057 int retval; 7058 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os; 7059 int num_target_port_groups, num_target_ports; 7060 struct ctl_lun *lun; 7061 struct ctl_softc *softc; 7062 struct ctl_port *port; 7063 struct scsi_target_group_data *rtg_ptr; 7064 struct scsi_target_group_data_extended *rtg_ext_ptr; 7065 struct scsi_target_port_group_descriptor *tpg_desc; 7066 7067 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7068 7069 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7070 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7071 softc = lun->ctl_softc; 7072 7073 retval = CTL_RETVAL_COMPLETE; 7074 7075 switch (cdb->byte2 & STG_PDF_MASK) { 7076 case STG_PDF_LENGTH: 7077 ext = 0; 7078 break; 7079 case STG_PDF_EXTENDED: 7080 ext = 1; 7081 break; 7082 default: 7083 ctl_set_invalid_field(/*ctsio*/ ctsio, 7084 /*sks_valid*/ 1, 7085 /*command*/ 1, 7086 /*field*/ 2, 7087 /*bit_valid*/ 1, 7088 /*bit*/ 5); 7089 ctl_done((union ctl_io *)ctsio); 7090 return(retval); 7091 } 7092 7093 if (softc->is_single) 7094 num_target_port_groups = 1; 7095 else 7096 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7097 num_target_ports = 0; 7098 mtx_lock(&softc->ctl_lock); 7099 STAILQ_FOREACH(port, &softc->port_list, links) { 7100 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7101 continue; 7102 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7103 continue; 7104 num_target_ports++; 7105 } 7106 mtx_unlock(&softc->ctl_lock); 7107 7108 if (ext) 7109 total_len = sizeof(struct scsi_target_group_data_extended); 7110 else 7111 total_len = sizeof(struct scsi_target_group_data); 7112 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7113 num_target_port_groups + 7114 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7115 7116 alloc_len = scsi_4btoul(cdb->length); 7117 7118 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7119 7120 ctsio->kern_sg_entries = 0; 7121 7122 if (total_len < alloc_len) { 7123 ctsio->residual = alloc_len - total_len; 7124 ctsio->kern_data_len = total_len; 7125 ctsio->kern_total_len = total_len; 7126 } else { 7127 ctsio->residual = 0; 7128 ctsio->kern_data_len = alloc_len; 7129 ctsio->kern_total_len = alloc_len; 7130 } 7131 ctsio->kern_data_resid = 0; 7132 ctsio->kern_rel_offset = 0; 7133 7134 if (ext) { 7135 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7136 ctsio->kern_data_ptr; 7137 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7138 rtg_ext_ptr->format_type = 0x10; 7139 rtg_ext_ptr->implicit_transition_time = 0; 7140 tpg_desc = &rtg_ext_ptr->groups[0]; 7141 } else { 7142 rtg_ptr = (struct scsi_target_group_data *) 7143 ctsio->kern_data_ptr; 7144 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7145 tpg_desc = &rtg_ptr->groups[0]; 7146 } 7147 7148 mtx_lock(&softc->ctl_lock); 7149 pg = softc->port_min / softc->port_cnt; 7150 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7151 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7152 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7153 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7154 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7155 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7156 else 7157 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7158 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7159 os = gs; 7160 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7161 } else 7162 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7163 for (g = 0; g < num_target_port_groups; g++) { 7164 tpg_desc->pref_state = (g == pg) ? gs : os; 7165 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7166 TPG_U_SUP | TPG_T_SUP; 7167 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7168 tpg_desc->status = TPG_IMPLICIT; 7169 pc = 0; 7170 STAILQ_FOREACH(port, &softc->port_list, links) { 7171 if (port->targ_port < g * softc->port_cnt || 7172 port->targ_port >= (g + 1) * softc->port_cnt) 7173 continue; 7174 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7175 continue; 7176 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7177 continue; 7178 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7179 relative_target_port_identifier); 7180 pc++; 7181 } 7182 tpg_desc->target_port_count = pc; 7183 tpg_desc = (struct scsi_target_port_group_descriptor *) 7184 &tpg_desc->descriptors[pc]; 7185 } 7186 mtx_unlock(&softc->ctl_lock); 7187 7188 ctl_set_success(ctsio); 7189 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7190 ctsio->be_move_done = ctl_config_move_done; 7191 ctl_datamove((union ctl_io *)ctsio); 7192 return(retval); 7193 } 7194 7195 int 7196 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7197 { 7198 struct ctl_lun *lun; 7199 struct scsi_report_supported_opcodes *cdb; 7200 const struct ctl_cmd_entry *entry, *sentry; 7201 struct scsi_report_supported_opcodes_all *all; 7202 struct scsi_report_supported_opcodes_descr *descr; 7203 struct scsi_report_supported_opcodes_one *one; 7204 int retval; 7205 int alloc_len, total_len; 7206 int opcode, service_action, i, j, num; 7207 7208 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7209 7210 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7211 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7212 7213 retval = CTL_RETVAL_COMPLETE; 7214 7215 opcode = cdb->requested_opcode; 7216 service_action = scsi_2btoul(cdb->requested_service_action); 7217 switch (cdb->options & RSO_OPTIONS_MASK) { 7218 case RSO_OPTIONS_ALL: 7219 num = 0; 7220 for (i = 0; i < 256; i++) { 7221 entry = &ctl_cmd_table[i]; 7222 if (entry->flags & CTL_CMD_FLAG_SA5) { 7223 for (j = 0; j < 32; j++) { 7224 sentry = &((const struct ctl_cmd_entry *) 7225 entry->execute)[j]; 7226 if (ctl_cmd_applicable( 7227 lun->be_lun->lun_type, sentry)) 7228 num++; 7229 } 7230 } else { 7231 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7232 entry)) 7233 num++; 7234 } 7235 } 7236 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7237 num * sizeof(struct scsi_report_supported_opcodes_descr); 7238 break; 7239 case RSO_OPTIONS_OC: 7240 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7241 ctl_set_invalid_field(/*ctsio*/ ctsio, 7242 /*sks_valid*/ 1, 7243 /*command*/ 1, 7244 /*field*/ 2, 7245 /*bit_valid*/ 1, 7246 /*bit*/ 2); 7247 ctl_done((union ctl_io *)ctsio); 7248 return (CTL_RETVAL_COMPLETE); 7249 } 7250 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7251 break; 7252 case RSO_OPTIONS_OC_SA: 7253 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7254 service_action >= 32) { 7255 ctl_set_invalid_field(/*ctsio*/ ctsio, 7256 /*sks_valid*/ 1, 7257 /*command*/ 1, 7258 /*field*/ 2, 7259 /*bit_valid*/ 1, 7260 /*bit*/ 2); 7261 ctl_done((union ctl_io *)ctsio); 7262 return (CTL_RETVAL_COMPLETE); 7263 } 7264 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7265 break; 7266 default: 7267 ctl_set_invalid_field(/*ctsio*/ ctsio, 7268 /*sks_valid*/ 1, 7269 /*command*/ 1, 7270 /*field*/ 2, 7271 /*bit_valid*/ 1, 7272 /*bit*/ 2); 7273 ctl_done((union ctl_io *)ctsio); 7274 return (CTL_RETVAL_COMPLETE); 7275 } 7276 7277 alloc_len = scsi_4btoul(cdb->length); 7278 7279 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7280 7281 ctsio->kern_sg_entries = 0; 7282 7283 if (total_len < alloc_len) { 7284 ctsio->residual = alloc_len - total_len; 7285 ctsio->kern_data_len = total_len; 7286 ctsio->kern_total_len = total_len; 7287 } else { 7288 ctsio->residual = 0; 7289 ctsio->kern_data_len = alloc_len; 7290 ctsio->kern_total_len = alloc_len; 7291 } 7292 ctsio->kern_data_resid = 0; 7293 ctsio->kern_rel_offset = 0; 7294 7295 switch (cdb->options & RSO_OPTIONS_MASK) { 7296 case RSO_OPTIONS_ALL: 7297 all = (struct scsi_report_supported_opcodes_all *) 7298 ctsio->kern_data_ptr; 7299 num = 0; 7300 for (i = 0; i < 256; i++) { 7301 entry = &ctl_cmd_table[i]; 7302 if (entry->flags & CTL_CMD_FLAG_SA5) { 7303 for (j = 0; j < 32; j++) { 7304 sentry = &((const struct ctl_cmd_entry *) 7305 entry->execute)[j]; 7306 if (!ctl_cmd_applicable( 7307 lun->be_lun->lun_type, sentry)) 7308 continue; 7309 descr = &all->descr[num++]; 7310 descr->opcode = i; 7311 scsi_ulto2b(j, descr->service_action); 7312 descr->flags = RSO_SERVACTV; 7313 scsi_ulto2b(sentry->length, 7314 descr->cdb_length); 7315 } 7316 } else { 7317 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7318 entry)) 7319 continue; 7320 descr = &all->descr[num++]; 7321 descr->opcode = i; 7322 scsi_ulto2b(0, descr->service_action); 7323 descr->flags = 0; 7324 scsi_ulto2b(entry->length, descr->cdb_length); 7325 } 7326 } 7327 scsi_ulto4b( 7328 num * sizeof(struct scsi_report_supported_opcodes_descr), 7329 all->length); 7330 break; 7331 case RSO_OPTIONS_OC: 7332 one = (struct scsi_report_supported_opcodes_one *) 7333 ctsio->kern_data_ptr; 7334 entry = &ctl_cmd_table[opcode]; 7335 goto fill_one; 7336 case RSO_OPTIONS_OC_SA: 7337 one = (struct scsi_report_supported_opcodes_one *) 7338 ctsio->kern_data_ptr; 7339 entry = &ctl_cmd_table[opcode]; 7340 entry = &((const struct ctl_cmd_entry *) 7341 entry->execute)[service_action]; 7342 fill_one: 7343 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7344 one->support = 3; 7345 scsi_ulto2b(entry->length, one->cdb_length); 7346 one->cdb_usage[0] = opcode; 7347 memcpy(&one->cdb_usage[1], entry->usage, 7348 entry->length - 1); 7349 } else 7350 one->support = 1; 7351 break; 7352 } 7353 7354 ctl_set_success(ctsio); 7355 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7356 ctsio->be_move_done = ctl_config_move_done; 7357 ctl_datamove((union ctl_io *)ctsio); 7358 return(retval); 7359 } 7360 7361 int 7362 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7363 { 7364 struct scsi_report_supported_tmf *cdb; 7365 struct scsi_report_supported_tmf_data *data; 7366 int retval; 7367 int alloc_len, total_len; 7368 7369 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7370 7371 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7372 7373 retval = CTL_RETVAL_COMPLETE; 7374 7375 total_len = sizeof(struct scsi_report_supported_tmf_data); 7376 alloc_len = scsi_4btoul(cdb->length); 7377 7378 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7379 7380 ctsio->kern_sg_entries = 0; 7381 7382 if (total_len < alloc_len) { 7383 ctsio->residual = alloc_len - total_len; 7384 ctsio->kern_data_len = total_len; 7385 ctsio->kern_total_len = total_len; 7386 } else { 7387 ctsio->residual = 0; 7388 ctsio->kern_data_len = alloc_len; 7389 ctsio->kern_total_len = alloc_len; 7390 } 7391 ctsio->kern_data_resid = 0; 7392 ctsio->kern_rel_offset = 0; 7393 7394 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7395 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7396 RST_TRS; 7397 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7398 7399 ctl_set_success(ctsio); 7400 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7401 ctsio->be_move_done = ctl_config_move_done; 7402 ctl_datamove((union ctl_io *)ctsio); 7403 return (retval); 7404 } 7405 7406 int 7407 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7408 { 7409 struct scsi_report_timestamp *cdb; 7410 struct scsi_report_timestamp_data *data; 7411 struct timeval tv; 7412 int64_t timestamp; 7413 int retval; 7414 int alloc_len, total_len; 7415 7416 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7417 7418 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7419 7420 retval = CTL_RETVAL_COMPLETE; 7421 7422 total_len = sizeof(struct scsi_report_timestamp_data); 7423 alloc_len = scsi_4btoul(cdb->length); 7424 7425 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7426 7427 ctsio->kern_sg_entries = 0; 7428 7429 if (total_len < alloc_len) { 7430 ctsio->residual = alloc_len - total_len; 7431 ctsio->kern_data_len = total_len; 7432 ctsio->kern_total_len = total_len; 7433 } else { 7434 ctsio->residual = 0; 7435 ctsio->kern_data_len = alloc_len; 7436 ctsio->kern_total_len = alloc_len; 7437 } 7438 ctsio->kern_data_resid = 0; 7439 ctsio->kern_rel_offset = 0; 7440 7441 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7442 scsi_ulto2b(sizeof(*data) - 2, data->length); 7443 data->origin = RTS_ORIG_OUTSIDE; 7444 getmicrotime(&tv); 7445 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7446 scsi_ulto4b(timestamp >> 16, data->timestamp); 7447 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7448 7449 ctl_set_success(ctsio); 7450 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7451 ctsio->be_move_done = ctl_config_move_done; 7452 ctl_datamove((union ctl_io *)ctsio); 7453 return (retval); 7454 } 7455 7456 int 7457 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7458 { 7459 struct scsi_per_res_in *cdb; 7460 int alloc_len, total_len = 0; 7461 /* struct scsi_per_res_in_rsrv in_data; */ 7462 struct ctl_lun *lun; 7463 struct ctl_softc *softc; 7464 uint64_t key; 7465 7466 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7467 7468 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7469 7470 alloc_len = scsi_2btoul(cdb->length); 7471 7472 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7473 softc = lun->ctl_softc; 7474 7475 retry: 7476 mtx_lock(&lun->lun_lock); 7477 switch (cdb->action) { 7478 case SPRI_RK: /* read keys */ 7479 total_len = sizeof(struct scsi_per_res_in_keys) + 7480 lun->pr_key_count * 7481 sizeof(struct scsi_per_res_key); 7482 break; 7483 case SPRI_RR: /* read reservation */ 7484 if (lun->flags & CTL_LUN_PR_RESERVED) 7485 total_len = sizeof(struct scsi_per_res_in_rsrv); 7486 else 7487 total_len = sizeof(struct scsi_per_res_in_header); 7488 break; 7489 case SPRI_RC: /* report capabilities */ 7490 total_len = sizeof(struct scsi_per_res_cap); 7491 break; 7492 case SPRI_RS: /* read full status */ 7493 total_len = sizeof(struct scsi_per_res_in_header) + 7494 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7495 lun->pr_key_count; 7496 break; 7497 default: 7498 panic("Invalid PR type %x", cdb->action); 7499 } 7500 mtx_unlock(&lun->lun_lock); 7501 7502 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7503 7504 if (total_len < alloc_len) { 7505 ctsio->residual = alloc_len - total_len; 7506 ctsio->kern_data_len = total_len; 7507 ctsio->kern_total_len = total_len; 7508 } else { 7509 ctsio->residual = 0; 7510 ctsio->kern_data_len = alloc_len; 7511 ctsio->kern_total_len = alloc_len; 7512 } 7513 7514 ctsio->kern_data_resid = 0; 7515 ctsio->kern_rel_offset = 0; 7516 ctsio->kern_sg_entries = 0; 7517 7518 mtx_lock(&lun->lun_lock); 7519 switch (cdb->action) { 7520 case SPRI_RK: { // read keys 7521 struct scsi_per_res_in_keys *res_keys; 7522 int i, key_count; 7523 7524 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7525 7526 /* 7527 * We had to drop the lock to allocate our buffer, which 7528 * leaves time for someone to come in with another 7529 * persistent reservation. (That is unlikely, though, 7530 * since this should be the only persistent reservation 7531 * command active right now.) 7532 */ 7533 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7534 (lun->pr_key_count * 7535 sizeof(struct scsi_per_res_key)))){ 7536 mtx_unlock(&lun->lun_lock); 7537 free(ctsio->kern_data_ptr, M_CTL); 7538 printf("%s: reservation length changed, retrying\n", 7539 __func__); 7540 goto retry; 7541 } 7542 7543 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7544 7545 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7546 lun->pr_key_count, res_keys->header.length); 7547 7548 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7549 if ((key = ctl_get_prkey(lun, i)) == 0) 7550 continue; 7551 7552 /* 7553 * We used lun->pr_key_count to calculate the 7554 * size to allocate. If it turns out the number of 7555 * initiators with the registered flag set is 7556 * larger than that (i.e. they haven't been kept in 7557 * sync), we've got a problem. 7558 */ 7559 if (key_count >= lun->pr_key_count) { 7560 key_count++; 7561 continue; 7562 } 7563 scsi_u64to8b(key, res_keys->keys[key_count].key); 7564 key_count++; 7565 } 7566 break; 7567 } 7568 case SPRI_RR: { // read reservation 7569 struct scsi_per_res_in_rsrv *res; 7570 int tmp_len, header_only; 7571 7572 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7573 7574 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7575 7576 if (lun->flags & CTL_LUN_PR_RESERVED) 7577 { 7578 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7579 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7580 res->header.length); 7581 header_only = 0; 7582 } else { 7583 tmp_len = sizeof(struct scsi_per_res_in_header); 7584 scsi_ulto4b(0, res->header.length); 7585 header_only = 1; 7586 } 7587 7588 /* 7589 * We had to drop the lock to allocate our buffer, which 7590 * leaves time for someone to come in with another 7591 * persistent reservation. (That is unlikely, though, 7592 * since this should be the only persistent reservation 7593 * command active right now.) 7594 */ 7595 if (tmp_len != total_len) { 7596 mtx_unlock(&lun->lun_lock); 7597 free(ctsio->kern_data_ptr, M_CTL); 7598 printf("%s: reservation status changed, retrying\n", 7599 __func__); 7600 goto retry; 7601 } 7602 7603 /* 7604 * No reservation held, so we're done. 7605 */ 7606 if (header_only != 0) 7607 break; 7608 7609 /* 7610 * If the registration is an All Registrants type, the key 7611 * is 0, since it doesn't really matter. 7612 */ 7613 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7614 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7615 res->data.reservation); 7616 } 7617 res->data.scopetype = lun->res_type; 7618 break; 7619 } 7620 case SPRI_RC: //report capabilities 7621 { 7622 struct scsi_per_res_cap *res_cap; 7623 uint16_t type_mask; 7624 7625 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7626 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7627 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7628 type_mask = SPRI_TM_WR_EX_AR | 7629 SPRI_TM_EX_AC_RO | 7630 SPRI_TM_WR_EX_RO | 7631 SPRI_TM_EX_AC | 7632 SPRI_TM_WR_EX | 7633 SPRI_TM_EX_AC_AR; 7634 scsi_ulto2b(type_mask, res_cap->type_mask); 7635 break; 7636 } 7637 case SPRI_RS: { // read full status 7638 struct scsi_per_res_in_full *res_status; 7639 struct scsi_per_res_in_full_desc *res_desc; 7640 struct ctl_port *port; 7641 int i, len; 7642 7643 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7644 7645 /* 7646 * We had to drop the lock to allocate our buffer, which 7647 * leaves time for someone to come in with another 7648 * persistent reservation. (That is unlikely, though, 7649 * since this should be the only persistent reservation 7650 * command active right now.) 7651 */ 7652 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7653 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7654 lun->pr_key_count)){ 7655 mtx_unlock(&lun->lun_lock); 7656 free(ctsio->kern_data_ptr, M_CTL); 7657 printf("%s: reservation length changed, retrying\n", 7658 __func__); 7659 goto retry; 7660 } 7661 7662 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7663 7664 res_desc = &res_status->desc[0]; 7665 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7666 if ((key = ctl_get_prkey(lun, i)) == 0) 7667 continue; 7668 7669 scsi_u64to8b(key, res_desc->res_key.key); 7670 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7671 (lun->pr_res_idx == i || 7672 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7673 res_desc->flags = SPRI_FULL_R_HOLDER; 7674 res_desc->scopetype = lun->res_type; 7675 } 7676 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7677 res_desc->rel_trgt_port_id); 7678 len = 0; 7679 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7680 if (port != NULL) 7681 len = ctl_create_iid(port, 7682 i % CTL_MAX_INIT_PER_PORT, 7683 res_desc->transport_id); 7684 scsi_ulto4b(len, res_desc->additional_length); 7685 res_desc = (struct scsi_per_res_in_full_desc *) 7686 &res_desc->transport_id[len]; 7687 } 7688 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7689 res_status->header.length); 7690 break; 7691 } 7692 default: 7693 /* 7694 * This is a bug, because we just checked for this above, 7695 * and should have returned an error. 7696 */ 7697 panic("Invalid PR type %x", cdb->action); 7698 break; /* NOTREACHED */ 7699 } 7700 mtx_unlock(&lun->lun_lock); 7701 7702 ctl_set_success(ctsio); 7703 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7704 ctsio->be_move_done = ctl_config_move_done; 7705 ctl_datamove((union ctl_io *)ctsio); 7706 return (CTL_RETVAL_COMPLETE); 7707 } 7708 7709 /* 7710 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7711 * it should return. 7712 */ 7713 static int 7714 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7715 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7716 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7717 struct scsi_per_res_out_parms* param) 7718 { 7719 union ctl_ha_msg persis_io; 7720 int i; 7721 7722 mtx_lock(&lun->lun_lock); 7723 if (sa_res_key == 0) { 7724 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7725 /* validate scope and type */ 7726 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7727 SPR_LU_SCOPE) { 7728 mtx_unlock(&lun->lun_lock); 7729 ctl_set_invalid_field(/*ctsio*/ ctsio, 7730 /*sks_valid*/ 1, 7731 /*command*/ 1, 7732 /*field*/ 2, 7733 /*bit_valid*/ 1, 7734 /*bit*/ 4); 7735 ctl_done((union ctl_io *)ctsio); 7736 return (1); 7737 } 7738 7739 if (type>8 || type==2 || type==4 || type==0) { 7740 mtx_unlock(&lun->lun_lock); 7741 ctl_set_invalid_field(/*ctsio*/ ctsio, 7742 /*sks_valid*/ 1, 7743 /*command*/ 1, 7744 /*field*/ 2, 7745 /*bit_valid*/ 1, 7746 /*bit*/ 0); 7747 ctl_done((union ctl_io *)ctsio); 7748 return (1); 7749 } 7750 7751 /* 7752 * Unregister everybody else and build UA for 7753 * them 7754 */ 7755 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7756 if (i == residx || ctl_get_prkey(lun, i) == 0) 7757 continue; 7758 7759 ctl_clr_prkey(lun, i); 7760 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7761 } 7762 lun->pr_key_count = 1; 7763 lun->res_type = type; 7764 if (lun->res_type != SPR_TYPE_WR_EX_AR 7765 && lun->res_type != SPR_TYPE_EX_AC_AR) 7766 lun->pr_res_idx = residx; 7767 lun->PRGeneration++; 7768 mtx_unlock(&lun->lun_lock); 7769 7770 /* send msg to other side */ 7771 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7772 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7773 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7774 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7775 persis_io.pr.pr_info.res_type = type; 7776 memcpy(persis_io.pr.pr_info.sa_res_key, 7777 param->serv_act_res_key, 7778 sizeof(param->serv_act_res_key)); 7779 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7780 sizeof(persis_io.pr), M_WAITOK); 7781 } else { 7782 /* not all registrants */ 7783 mtx_unlock(&lun->lun_lock); 7784 free(ctsio->kern_data_ptr, M_CTL); 7785 ctl_set_invalid_field(ctsio, 7786 /*sks_valid*/ 1, 7787 /*command*/ 0, 7788 /*field*/ 8, 7789 /*bit_valid*/ 0, 7790 /*bit*/ 0); 7791 ctl_done((union ctl_io *)ctsio); 7792 return (1); 7793 } 7794 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7795 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7796 int found = 0; 7797 7798 if (res_key == sa_res_key) { 7799 /* special case */ 7800 /* 7801 * The spec implies this is not good but doesn't 7802 * say what to do. There are two choices either 7803 * generate a res conflict or check condition 7804 * with illegal field in parameter data. Since 7805 * that is what is done when the sa_res_key is 7806 * zero I'll take that approach since this has 7807 * to do with the sa_res_key. 7808 */ 7809 mtx_unlock(&lun->lun_lock); 7810 free(ctsio->kern_data_ptr, M_CTL); 7811 ctl_set_invalid_field(ctsio, 7812 /*sks_valid*/ 1, 7813 /*command*/ 0, 7814 /*field*/ 8, 7815 /*bit_valid*/ 0, 7816 /*bit*/ 0); 7817 ctl_done((union ctl_io *)ctsio); 7818 return (1); 7819 } 7820 7821 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7822 if (ctl_get_prkey(lun, i) != sa_res_key) 7823 continue; 7824 7825 found = 1; 7826 ctl_clr_prkey(lun, i); 7827 lun->pr_key_count--; 7828 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7829 } 7830 if (!found) { 7831 mtx_unlock(&lun->lun_lock); 7832 free(ctsio->kern_data_ptr, M_CTL); 7833 ctl_set_reservation_conflict(ctsio); 7834 ctl_done((union ctl_io *)ctsio); 7835 return (CTL_RETVAL_COMPLETE); 7836 } 7837 lun->PRGeneration++; 7838 mtx_unlock(&lun->lun_lock); 7839 7840 /* send msg to other side */ 7841 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7842 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7843 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7844 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7845 persis_io.pr.pr_info.res_type = type; 7846 memcpy(persis_io.pr.pr_info.sa_res_key, 7847 param->serv_act_res_key, 7848 sizeof(param->serv_act_res_key)); 7849 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7850 sizeof(persis_io.pr), M_WAITOK); 7851 } else { 7852 /* Reserved but not all registrants */ 7853 /* sa_res_key is res holder */ 7854 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7855 /* validate scope and type */ 7856 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7857 SPR_LU_SCOPE) { 7858 mtx_unlock(&lun->lun_lock); 7859 ctl_set_invalid_field(/*ctsio*/ ctsio, 7860 /*sks_valid*/ 1, 7861 /*command*/ 1, 7862 /*field*/ 2, 7863 /*bit_valid*/ 1, 7864 /*bit*/ 4); 7865 ctl_done((union ctl_io *)ctsio); 7866 return (1); 7867 } 7868 7869 if (type>8 || type==2 || type==4 || type==0) { 7870 mtx_unlock(&lun->lun_lock); 7871 ctl_set_invalid_field(/*ctsio*/ ctsio, 7872 /*sks_valid*/ 1, 7873 /*command*/ 1, 7874 /*field*/ 2, 7875 /*bit_valid*/ 1, 7876 /*bit*/ 0); 7877 ctl_done((union ctl_io *)ctsio); 7878 return (1); 7879 } 7880 7881 /* 7882 * Do the following: 7883 * if sa_res_key != res_key remove all 7884 * registrants w/sa_res_key and generate UA 7885 * for these registrants(Registrations 7886 * Preempted) if it wasn't an exclusive 7887 * reservation generate UA(Reservations 7888 * Preempted) for all other registered nexuses 7889 * if the type has changed. Establish the new 7890 * reservation and holder. If res_key and 7891 * sa_res_key are the same do the above 7892 * except don't unregister the res holder. 7893 */ 7894 7895 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7896 if (i == residx || ctl_get_prkey(lun, i) == 0) 7897 continue; 7898 7899 if (sa_res_key == ctl_get_prkey(lun, i)) { 7900 ctl_clr_prkey(lun, i); 7901 lun->pr_key_count--; 7902 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7903 } else if (type != lun->res_type 7904 && (lun->res_type == SPR_TYPE_WR_EX_RO 7905 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 7906 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 7907 } 7908 } 7909 lun->res_type = type; 7910 if (lun->res_type != SPR_TYPE_WR_EX_AR 7911 && lun->res_type != SPR_TYPE_EX_AC_AR) 7912 lun->pr_res_idx = residx; 7913 else 7914 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7915 lun->PRGeneration++; 7916 mtx_unlock(&lun->lun_lock); 7917 7918 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7919 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7920 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7921 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7922 persis_io.pr.pr_info.res_type = type; 7923 memcpy(persis_io.pr.pr_info.sa_res_key, 7924 param->serv_act_res_key, 7925 sizeof(param->serv_act_res_key)); 7926 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7927 sizeof(persis_io.pr), M_WAITOK); 7928 } else { 7929 /* 7930 * sa_res_key is not the res holder just 7931 * remove registrants 7932 */ 7933 int found=0; 7934 7935 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7936 if (sa_res_key != ctl_get_prkey(lun, i)) 7937 continue; 7938 7939 found = 1; 7940 ctl_clr_prkey(lun, i); 7941 lun->pr_key_count--; 7942 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7943 } 7944 7945 if (!found) { 7946 mtx_unlock(&lun->lun_lock); 7947 free(ctsio->kern_data_ptr, M_CTL); 7948 ctl_set_reservation_conflict(ctsio); 7949 ctl_done((union ctl_io *)ctsio); 7950 return (1); 7951 } 7952 lun->PRGeneration++; 7953 mtx_unlock(&lun->lun_lock); 7954 7955 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7956 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7957 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7958 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7959 persis_io.pr.pr_info.res_type = type; 7960 memcpy(persis_io.pr.pr_info.sa_res_key, 7961 param->serv_act_res_key, 7962 sizeof(param->serv_act_res_key)); 7963 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7964 sizeof(persis_io.pr), M_WAITOK); 7965 } 7966 } 7967 return (0); 7968 } 7969 7970 static void 7971 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 7972 { 7973 uint64_t sa_res_key; 7974 int i; 7975 7976 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 7977 7978 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7979 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 7980 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 7981 if (sa_res_key == 0) { 7982 /* 7983 * Unregister everybody else and build UA for 7984 * them 7985 */ 7986 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7987 if (i == msg->pr.pr_info.residx || 7988 ctl_get_prkey(lun, i) == 0) 7989 continue; 7990 7991 ctl_clr_prkey(lun, i); 7992 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7993 } 7994 7995 lun->pr_key_count = 1; 7996 lun->res_type = msg->pr.pr_info.res_type; 7997 if (lun->res_type != SPR_TYPE_WR_EX_AR 7998 && lun->res_type != SPR_TYPE_EX_AC_AR) 7999 lun->pr_res_idx = msg->pr.pr_info.residx; 8000 } else { 8001 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8002 if (sa_res_key == ctl_get_prkey(lun, i)) 8003 continue; 8004 8005 ctl_clr_prkey(lun, i); 8006 lun->pr_key_count--; 8007 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8008 } 8009 } 8010 } else { 8011 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8012 if (i == msg->pr.pr_info.residx || 8013 ctl_get_prkey(lun, i) == 0) 8014 continue; 8015 8016 if (sa_res_key == ctl_get_prkey(lun, i)) { 8017 ctl_clr_prkey(lun, i); 8018 lun->pr_key_count--; 8019 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8020 } else if (msg->pr.pr_info.res_type != lun->res_type 8021 && (lun->res_type == SPR_TYPE_WR_EX_RO 8022 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8023 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8024 } 8025 } 8026 lun->res_type = msg->pr.pr_info.res_type; 8027 if (lun->res_type != SPR_TYPE_WR_EX_AR 8028 && lun->res_type != SPR_TYPE_EX_AC_AR) 8029 lun->pr_res_idx = msg->pr.pr_info.residx; 8030 else 8031 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8032 } 8033 lun->PRGeneration++; 8034 8035 } 8036 8037 8038 int 8039 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8040 { 8041 int retval; 8042 u_int32_t param_len; 8043 struct scsi_per_res_out *cdb; 8044 struct ctl_lun *lun; 8045 struct scsi_per_res_out_parms* param; 8046 struct ctl_softc *softc; 8047 uint32_t residx; 8048 uint64_t res_key, sa_res_key, key; 8049 uint8_t type; 8050 union ctl_ha_msg persis_io; 8051 int i; 8052 8053 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8054 8055 retval = CTL_RETVAL_COMPLETE; 8056 8057 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8058 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8059 softc = lun->ctl_softc; 8060 8061 /* 8062 * We only support whole-LUN scope. The scope & type are ignored for 8063 * register, register and ignore existing key and clear. 8064 * We sometimes ignore scope and type on preempts too!! 8065 * Verify reservation type here as well. 8066 */ 8067 type = cdb->scope_type & SPR_TYPE_MASK; 8068 if ((cdb->action == SPRO_RESERVE) 8069 || (cdb->action == SPRO_RELEASE)) { 8070 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8071 ctl_set_invalid_field(/*ctsio*/ ctsio, 8072 /*sks_valid*/ 1, 8073 /*command*/ 1, 8074 /*field*/ 2, 8075 /*bit_valid*/ 1, 8076 /*bit*/ 4); 8077 ctl_done((union ctl_io *)ctsio); 8078 return (CTL_RETVAL_COMPLETE); 8079 } 8080 8081 if (type>8 || type==2 || type==4 || type==0) { 8082 ctl_set_invalid_field(/*ctsio*/ ctsio, 8083 /*sks_valid*/ 1, 8084 /*command*/ 1, 8085 /*field*/ 2, 8086 /*bit_valid*/ 1, 8087 /*bit*/ 0); 8088 ctl_done((union ctl_io *)ctsio); 8089 return (CTL_RETVAL_COMPLETE); 8090 } 8091 } 8092 8093 param_len = scsi_4btoul(cdb->length); 8094 8095 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8096 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8097 ctsio->kern_data_len = param_len; 8098 ctsio->kern_total_len = param_len; 8099 ctsio->kern_data_resid = 0; 8100 ctsio->kern_rel_offset = 0; 8101 ctsio->kern_sg_entries = 0; 8102 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8103 ctsio->be_move_done = ctl_config_move_done; 8104 ctl_datamove((union ctl_io *)ctsio); 8105 8106 return (CTL_RETVAL_COMPLETE); 8107 } 8108 8109 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8110 8111 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8112 res_key = scsi_8btou64(param->res_key.key); 8113 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8114 8115 /* 8116 * Validate the reservation key here except for SPRO_REG_IGNO 8117 * This must be done for all other service actions 8118 */ 8119 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8120 mtx_lock(&lun->lun_lock); 8121 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8122 if (res_key != key) { 8123 /* 8124 * The current key passed in doesn't match 8125 * the one the initiator previously 8126 * registered. 8127 */ 8128 mtx_unlock(&lun->lun_lock); 8129 free(ctsio->kern_data_ptr, M_CTL); 8130 ctl_set_reservation_conflict(ctsio); 8131 ctl_done((union ctl_io *)ctsio); 8132 return (CTL_RETVAL_COMPLETE); 8133 } 8134 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8135 /* 8136 * We are not registered 8137 */ 8138 mtx_unlock(&lun->lun_lock); 8139 free(ctsio->kern_data_ptr, M_CTL); 8140 ctl_set_reservation_conflict(ctsio); 8141 ctl_done((union ctl_io *)ctsio); 8142 return (CTL_RETVAL_COMPLETE); 8143 } else if (res_key != 0) { 8144 /* 8145 * We are not registered and trying to register but 8146 * the register key isn't zero. 8147 */ 8148 mtx_unlock(&lun->lun_lock); 8149 free(ctsio->kern_data_ptr, M_CTL); 8150 ctl_set_reservation_conflict(ctsio); 8151 ctl_done((union ctl_io *)ctsio); 8152 return (CTL_RETVAL_COMPLETE); 8153 } 8154 mtx_unlock(&lun->lun_lock); 8155 } 8156 8157 switch (cdb->action & SPRO_ACTION_MASK) { 8158 case SPRO_REGISTER: 8159 case SPRO_REG_IGNO: { 8160 8161 #if 0 8162 printf("Registration received\n"); 8163 #endif 8164 8165 /* 8166 * We don't support any of these options, as we report in 8167 * the read capabilities request (see 8168 * ctl_persistent_reserve_in(), above). 8169 */ 8170 if ((param->flags & SPR_SPEC_I_PT) 8171 || (param->flags & SPR_ALL_TG_PT) 8172 || (param->flags & SPR_APTPL)) { 8173 int bit_ptr; 8174 8175 if (param->flags & SPR_APTPL) 8176 bit_ptr = 0; 8177 else if (param->flags & SPR_ALL_TG_PT) 8178 bit_ptr = 2; 8179 else /* SPR_SPEC_I_PT */ 8180 bit_ptr = 3; 8181 8182 free(ctsio->kern_data_ptr, M_CTL); 8183 ctl_set_invalid_field(ctsio, 8184 /*sks_valid*/ 1, 8185 /*command*/ 0, 8186 /*field*/ 20, 8187 /*bit_valid*/ 1, 8188 /*bit*/ bit_ptr); 8189 ctl_done((union ctl_io *)ctsio); 8190 return (CTL_RETVAL_COMPLETE); 8191 } 8192 8193 mtx_lock(&lun->lun_lock); 8194 8195 /* 8196 * The initiator wants to clear the 8197 * key/unregister. 8198 */ 8199 if (sa_res_key == 0) { 8200 if ((res_key == 0 8201 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8202 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8203 && ctl_get_prkey(lun, residx) == 0)) { 8204 mtx_unlock(&lun->lun_lock); 8205 goto done; 8206 } 8207 8208 ctl_clr_prkey(lun, residx); 8209 lun->pr_key_count--; 8210 8211 if (residx == lun->pr_res_idx) { 8212 lun->flags &= ~CTL_LUN_PR_RESERVED; 8213 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8214 8215 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8216 || lun->res_type == SPR_TYPE_EX_AC_RO) 8217 && lun->pr_key_count) { 8218 /* 8219 * If the reservation is a registrants 8220 * only type we need to generate a UA 8221 * for other registered inits. The 8222 * sense code should be RESERVATIONS 8223 * RELEASED 8224 */ 8225 8226 for (i = softc->init_min; i < softc->init_max; i++){ 8227 if (ctl_get_prkey(lun, i) == 0) 8228 continue; 8229 ctl_est_ua(lun, i, 8230 CTL_UA_RES_RELEASE); 8231 } 8232 } 8233 lun->res_type = 0; 8234 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8235 if (lun->pr_key_count==0) { 8236 lun->flags &= ~CTL_LUN_PR_RESERVED; 8237 lun->res_type = 0; 8238 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8239 } 8240 } 8241 lun->PRGeneration++; 8242 mtx_unlock(&lun->lun_lock); 8243 8244 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8245 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8246 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8247 persis_io.pr.pr_info.residx = residx; 8248 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8249 sizeof(persis_io.pr), M_WAITOK); 8250 } else /* sa_res_key != 0 */ { 8251 8252 /* 8253 * If we aren't registered currently then increment 8254 * the key count and set the registered flag. 8255 */ 8256 ctl_alloc_prkey(lun, residx); 8257 if (ctl_get_prkey(lun, residx) == 0) 8258 lun->pr_key_count++; 8259 ctl_set_prkey(lun, residx, sa_res_key); 8260 lun->PRGeneration++; 8261 mtx_unlock(&lun->lun_lock); 8262 8263 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8264 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8265 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8266 persis_io.pr.pr_info.residx = residx; 8267 memcpy(persis_io.pr.pr_info.sa_res_key, 8268 param->serv_act_res_key, 8269 sizeof(param->serv_act_res_key)); 8270 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8271 sizeof(persis_io.pr), M_WAITOK); 8272 } 8273 8274 break; 8275 } 8276 case SPRO_RESERVE: 8277 #if 0 8278 printf("Reserve executed type %d\n", type); 8279 #endif 8280 mtx_lock(&lun->lun_lock); 8281 if (lun->flags & CTL_LUN_PR_RESERVED) { 8282 /* 8283 * if this isn't the reservation holder and it's 8284 * not a "all registrants" type or if the type is 8285 * different then we have a conflict 8286 */ 8287 if ((lun->pr_res_idx != residx 8288 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8289 || lun->res_type != type) { 8290 mtx_unlock(&lun->lun_lock); 8291 free(ctsio->kern_data_ptr, M_CTL); 8292 ctl_set_reservation_conflict(ctsio); 8293 ctl_done((union ctl_io *)ctsio); 8294 return (CTL_RETVAL_COMPLETE); 8295 } 8296 mtx_unlock(&lun->lun_lock); 8297 } else /* create a reservation */ { 8298 /* 8299 * If it's not an "all registrants" type record 8300 * reservation holder 8301 */ 8302 if (type != SPR_TYPE_WR_EX_AR 8303 && type != SPR_TYPE_EX_AC_AR) 8304 lun->pr_res_idx = residx; /* Res holder */ 8305 else 8306 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8307 8308 lun->flags |= CTL_LUN_PR_RESERVED; 8309 lun->res_type = type; 8310 8311 mtx_unlock(&lun->lun_lock); 8312 8313 /* send msg to other side */ 8314 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8315 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8316 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8317 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8318 persis_io.pr.pr_info.res_type = type; 8319 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8320 sizeof(persis_io.pr), M_WAITOK); 8321 } 8322 break; 8323 8324 case SPRO_RELEASE: 8325 mtx_lock(&lun->lun_lock); 8326 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8327 /* No reservation exists return good status */ 8328 mtx_unlock(&lun->lun_lock); 8329 goto done; 8330 } 8331 /* 8332 * Is this nexus a reservation holder? 8333 */ 8334 if (lun->pr_res_idx != residx 8335 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8336 /* 8337 * not a res holder return good status but 8338 * do nothing 8339 */ 8340 mtx_unlock(&lun->lun_lock); 8341 goto done; 8342 } 8343 8344 if (lun->res_type != type) { 8345 mtx_unlock(&lun->lun_lock); 8346 free(ctsio->kern_data_ptr, M_CTL); 8347 ctl_set_illegal_pr_release(ctsio); 8348 ctl_done((union ctl_io *)ctsio); 8349 return (CTL_RETVAL_COMPLETE); 8350 } 8351 8352 /* okay to release */ 8353 lun->flags &= ~CTL_LUN_PR_RESERVED; 8354 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8355 lun->res_type = 0; 8356 8357 /* 8358 * if this isn't an exclusive access 8359 * res generate UA for all other 8360 * registrants. 8361 */ 8362 if (type != SPR_TYPE_EX_AC 8363 && type != SPR_TYPE_WR_EX) { 8364 for (i = softc->init_min; i < softc->init_max; i++) { 8365 if (i == residx || ctl_get_prkey(lun, i) == 0) 8366 continue; 8367 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8368 } 8369 } 8370 mtx_unlock(&lun->lun_lock); 8371 8372 /* Send msg to other side */ 8373 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8374 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8375 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8376 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8377 sizeof(persis_io.pr), M_WAITOK); 8378 break; 8379 8380 case SPRO_CLEAR: 8381 /* send msg to other side */ 8382 8383 mtx_lock(&lun->lun_lock); 8384 lun->flags &= ~CTL_LUN_PR_RESERVED; 8385 lun->res_type = 0; 8386 lun->pr_key_count = 0; 8387 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8388 8389 ctl_clr_prkey(lun, residx); 8390 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8391 if (ctl_get_prkey(lun, i) != 0) { 8392 ctl_clr_prkey(lun, i); 8393 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8394 } 8395 lun->PRGeneration++; 8396 mtx_unlock(&lun->lun_lock); 8397 8398 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8399 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8400 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8401 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8402 sizeof(persis_io.pr), M_WAITOK); 8403 break; 8404 8405 case SPRO_PREEMPT: 8406 case SPRO_PRE_ABO: { 8407 int nretval; 8408 8409 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8410 residx, ctsio, cdb, param); 8411 if (nretval != 0) 8412 return (CTL_RETVAL_COMPLETE); 8413 break; 8414 } 8415 default: 8416 panic("Invalid PR type %x", cdb->action); 8417 } 8418 8419 done: 8420 free(ctsio->kern_data_ptr, M_CTL); 8421 ctl_set_success(ctsio); 8422 ctl_done((union ctl_io *)ctsio); 8423 8424 return (retval); 8425 } 8426 8427 /* 8428 * This routine is for handling a message from the other SC pertaining to 8429 * persistent reserve out. All the error checking will have been done 8430 * so only perorming the action need be done here to keep the two 8431 * in sync. 8432 */ 8433 static void 8434 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8435 { 8436 struct ctl_softc *softc = control_softc; 8437 struct ctl_lun *lun; 8438 int i; 8439 uint32_t residx, targ_lun; 8440 8441 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8442 mtx_lock(&softc->ctl_lock); 8443 if ((targ_lun >= CTL_MAX_LUNS) || 8444 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 8445 mtx_unlock(&softc->ctl_lock); 8446 return; 8447 } 8448 mtx_lock(&lun->lun_lock); 8449 mtx_unlock(&softc->ctl_lock); 8450 if (lun->flags & CTL_LUN_DISABLED) { 8451 mtx_unlock(&lun->lun_lock); 8452 return; 8453 } 8454 residx = ctl_get_initindex(&msg->hdr.nexus); 8455 switch(msg->pr.pr_info.action) { 8456 case CTL_PR_REG_KEY: 8457 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8458 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8459 lun->pr_key_count++; 8460 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8461 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8462 lun->PRGeneration++; 8463 break; 8464 8465 case CTL_PR_UNREG_KEY: 8466 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8467 lun->pr_key_count--; 8468 8469 /* XXX Need to see if the reservation has been released */ 8470 /* if so do we need to generate UA? */ 8471 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8472 lun->flags &= ~CTL_LUN_PR_RESERVED; 8473 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8474 8475 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8476 || lun->res_type == SPR_TYPE_EX_AC_RO) 8477 && lun->pr_key_count) { 8478 /* 8479 * If the reservation is a registrants 8480 * only type we need to generate a UA 8481 * for other registered inits. The 8482 * sense code should be RESERVATIONS 8483 * RELEASED 8484 */ 8485 8486 for (i = softc->init_min; i < softc->init_max; i++) { 8487 if (ctl_get_prkey(lun, i) == 0) 8488 continue; 8489 8490 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8491 } 8492 } 8493 lun->res_type = 0; 8494 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8495 if (lun->pr_key_count==0) { 8496 lun->flags &= ~CTL_LUN_PR_RESERVED; 8497 lun->res_type = 0; 8498 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8499 } 8500 } 8501 lun->PRGeneration++; 8502 break; 8503 8504 case CTL_PR_RESERVE: 8505 lun->flags |= CTL_LUN_PR_RESERVED; 8506 lun->res_type = msg->pr.pr_info.res_type; 8507 lun->pr_res_idx = msg->pr.pr_info.residx; 8508 8509 break; 8510 8511 case CTL_PR_RELEASE: 8512 /* 8513 * if this isn't an exclusive access res generate UA for all 8514 * other registrants. 8515 */ 8516 if (lun->res_type != SPR_TYPE_EX_AC 8517 && lun->res_type != SPR_TYPE_WR_EX) { 8518 for (i = softc->init_min; i < softc->init_max; i++) 8519 if (i == residx || ctl_get_prkey(lun, i) == 0) 8520 continue; 8521 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8522 } 8523 8524 lun->flags &= ~CTL_LUN_PR_RESERVED; 8525 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8526 lun->res_type = 0; 8527 break; 8528 8529 case CTL_PR_PREEMPT: 8530 ctl_pro_preempt_other(lun, msg); 8531 break; 8532 case CTL_PR_CLEAR: 8533 lun->flags &= ~CTL_LUN_PR_RESERVED; 8534 lun->res_type = 0; 8535 lun->pr_key_count = 0; 8536 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8537 8538 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8539 if (ctl_get_prkey(lun, i) == 0) 8540 continue; 8541 ctl_clr_prkey(lun, i); 8542 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8543 } 8544 lun->PRGeneration++; 8545 break; 8546 } 8547 8548 mtx_unlock(&lun->lun_lock); 8549 } 8550 8551 int 8552 ctl_read_write(struct ctl_scsiio *ctsio) 8553 { 8554 struct ctl_lun *lun; 8555 struct ctl_lba_len_flags *lbalen; 8556 uint64_t lba; 8557 uint32_t num_blocks; 8558 int flags, retval; 8559 int isread; 8560 8561 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8562 8563 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8564 8565 flags = 0; 8566 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8567 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8568 switch (ctsio->cdb[0]) { 8569 case READ_6: 8570 case WRITE_6: { 8571 struct scsi_rw_6 *cdb; 8572 8573 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8574 8575 lba = scsi_3btoul(cdb->addr); 8576 /* only 5 bits are valid in the most significant address byte */ 8577 lba &= 0x1fffff; 8578 num_blocks = cdb->length; 8579 /* 8580 * This is correct according to SBC-2. 8581 */ 8582 if (num_blocks == 0) 8583 num_blocks = 256; 8584 break; 8585 } 8586 case READ_10: 8587 case WRITE_10: { 8588 struct scsi_rw_10 *cdb; 8589 8590 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8591 if (cdb->byte2 & SRW10_FUA) 8592 flags |= CTL_LLF_FUA; 8593 if (cdb->byte2 & SRW10_DPO) 8594 flags |= CTL_LLF_DPO; 8595 lba = scsi_4btoul(cdb->addr); 8596 num_blocks = scsi_2btoul(cdb->length); 8597 break; 8598 } 8599 case WRITE_VERIFY_10: { 8600 struct scsi_write_verify_10 *cdb; 8601 8602 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8603 flags |= CTL_LLF_FUA; 8604 if (cdb->byte2 & SWV_DPO) 8605 flags |= CTL_LLF_DPO; 8606 lba = scsi_4btoul(cdb->addr); 8607 num_blocks = scsi_2btoul(cdb->length); 8608 break; 8609 } 8610 case READ_12: 8611 case WRITE_12: { 8612 struct scsi_rw_12 *cdb; 8613 8614 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8615 if (cdb->byte2 & SRW12_FUA) 8616 flags |= CTL_LLF_FUA; 8617 if (cdb->byte2 & SRW12_DPO) 8618 flags |= CTL_LLF_DPO; 8619 lba = scsi_4btoul(cdb->addr); 8620 num_blocks = scsi_4btoul(cdb->length); 8621 break; 8622 } 8623 case WRITE_VERIFY_12: { 8624 struct scsi_write_verify_12 *cdb; 8625 8626 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8627 flags |= CTL_LLF_FUA; 8628 if (cdb->byte2 & SWV_DPO) 8629 flags |= CTL_LLF_DPO; 8630 lba = scsi_4btoul(cdb->addr); 8631 num_blocks = scsi_4btoul(cdb->length); 8632 break; 8633 } 8634 case READ_16: 8635 case WRITE_16: { 8636 struct scsi_rw_16 *cdb; 8637 8638 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8639 if (cdb->byte2 & SRW12_FUA) 8640 flags |= CTL_LLF_FUA; 8641 if (cdb->byte2 & SRW12_DPO) 8642 flags |= CTL_LLF_DPO; 8643 lba = scsi_8btou64(cdb->addr); 8644 num_blocks = scsi_4btoul(cdb->length); 8645 break; 8646 } 8647 case WRITE_ATOMIC_16: { 8648 struct scsi_write_atomic_16 *cdb; 8649 8650 if (lun->be_lun->atomicblock == 0) { 8651 ctl_set_invalid_opcode(ctsio); 8652 ctl_done((union ctl_io *)ctsio); 8653 return (CTL_RETVAL_COMPLETE); 8654 } 8655 8656 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8657 if (cdb->byte2 & SRW12_FUA) 8658 flags |= CTL_LLF_FUA; 8659 if (cdb->byte2 & SRW12_DPO) 8660 flags |= CTL_LLF_DPO; 8661 lba = scsi_8btou64(cdb->addr); 8662 num_blocks = scsi_2btoul(cdb->length); 8663 if (num_blocks > lun->be_lun->atomicblock) { 8664 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8665 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8666 /*bit*/ 0); 8667 ctl_done((union ctl_io *)ctsio); 8668 return (CTL_RETVAL_COMPLETE); 8669 } 8670 break; 8671 } 8672 case WRITE_VERIFY_16: { 8673 struct scsi_write_verify_16 *cdb; 8674 8675 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8676 flags |= CTL_LLF_FUA; 8677 if (cdb->byte2 & SWV_DPO) 8678 flags |= CTL_LLF_DPO; 8679 lba = scsi_8btou64(cdb->addr); 8680 num_blocks = scsi_4btoul(cdb->length); 8681 break; 8682 } 8683 default: 8684 /* 8685 * We got a command we don't support. This shouldn't 8686 * happen, commands should be filtered out above us. 8687 */ 8688 ctl_set_invalid_opcode(ctsio); 8689 ctl_done((union ctl_io *)ctsio); 8690 8691 return (CTL_RETVAL_COMPLETE); 8692 break; /* NOTREACHED */ 8693 } 8694 8695 /* 8696 * The first check is to make sure we're in bounds, the second 8697 * check is to catch wrap-around problems. If the lba + num blocks 8698 * is less than the lba, then we've wrapped around and the block 8699 * range is invalid anyway. 8700 */ 8701 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8702 || ((lba + num_blocks) < lba)) { 8703 ctl_set_lba_out_of_range(ctsio); 8704 ctl_done((union ctl_io *)ctsio); 8705 return (CTL_RETVAL_COMPLETE); 8706 } 8707 8708 /* 8709 * According to SBC-3, a transfer length of 0 is not an error. 8710 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8711 * translates to 256 blocks for those commands. 8712 */ 8713 if (num_blocks == 0) { 8714 ctl_set_success(ctsio); 8715 ctl_done((union ctl_io *)ctsio); 8716 return (CTL_RETVAL_COMPLETE); 8717 } 8718 8719 /* Set FUA and/or DPO if caches are disabled. */ 8720 if (isread) { 8721 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8722 SCP_RCD) != 0) 8723 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8724 } else { 8725 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8726 SCP_WCE) == 0) 8727 flags |= CTL_LLF_FUA; 8728 } 8729 8730 lbalen = (struct ctl_lba_len_flags *) 8731 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8732 lbalen->lba = lba; 8733 lbalen->len = num_blocks; 8734 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8735 8736 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8737 ctsio->kern_rel_offset = 0; 8738 8739 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8740 8741 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8742 return (retval); 8743 } 8744 8745 static int 8746 ctl_cnw_cont(union ctl_io *io) 8747 { 8748 struct ctl_scsiio *ctsio; 8749 struct ctl_lun *lun; 8750 struct ctl_lba_len_flags *lbalen; 8751 int retval; 8752 8753 ctsio = &io->scsiio; 8754 ctsio->io_hdr.status = CTL_STATUS_NONE; 8755 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8756 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8757 lbalen = (struct ctl_lba_len_flags *) 8758 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8759 lbalen->flags &= ~CTL_LLF_COMPARE; 8760 lbalen->flags |= CTL_LLF_WRITE; 8761 8762 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8763 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8764 return (retval); 8765 } 8766 8767 int 8768 ctl_cnw(struct ctl_scsiio *ctsio) 8769 { 8770 struct ctl_lun *lun; 8771 struct ctl_lba_len_flags *lbalen; 8772 uint64_t lba; 8773 uint32_t num_blocks; 8774 int flags, retval; 8775 8776 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8777 8778 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8779 8780 flags = 0; 8781 switch (ctsio->cdb[0]) { 8782 case COMPARE_AND_WRITE: { 8783 struct scsi_compare_and_write *cdb; 8784 8785 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8786 if (cdb->byte2 & SRW10_FUA) 8787 flags |= CTL_LLF_FUA; 8788 if (cdb->byte2 & SRW10_DPO) 8789 flags |= CTL_LLF_DPO; 8790 lba = scsi_8btou64(cdb->addr); 8791 num_blocks = cdb->length; 8792 break; 8793 } 8794 default: 8795 /* 8796 * We got a command we don't support. This shouldn't 8797 * happen, commands should be filtered out above us. 8798 */ 8799 ctl_set_invalid_opcode(ctsio); 8800 ctl_done((union ctl_io *)ctsio); 8801 8802 return (CTL_RETVAL_COMPLETE); 8803 break; /* NOTREACHED */ 8804 } 8805 8806 /* 8807 * The first check is to make sure we're in bounds, the second 8808 * check is to catch wrap-around problems. If the lba + num blocks 8809 * is less than the lba, then we've wrapped around and the block 8810 * range is invalid anyway. 8811 */ 8812 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8813 || ((lba + num_blocks) < lba)) { 8814 ctl_set_lba_out_of_range(ctsio); 8815 ctl_done((union ctl_io *)ctsio); 8816 return (CTL_RETVAL_COMPLETE); 8817 } 8818 8819 /* 8820 * According to SBC-3, a transfer length of 0 is not an error. 8821 */ 8822 if (num_blocks == 0) { 8823 ctl_set_success(ctsio); 8824 ctl_done((union ctl_io *)ctsio); 8825 return (CTL_RETVAL_COMPLETE); 8826 } 8827 8828 /* Set FUA if write cache is disabled. */ 8829 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8830 SCP_WCE) == 0) 8831 flags |= CTL_LLF_FUA; 8832 8833 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8834 ctsio->kern_rel_offset = 0; 8835 8836 /* 8837 * Set the IO_CONT flag, so that if this I/O gets passed to 8838 * ctl_data_submit_done(), it'll get passed back to 8839 * ctl_ctl_cnw_cont() for further processing. 8840 */ 8841 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8842 ctsio->io_cont = ctl_cnw_cont; 8843 8844 lbalen = (struct ctl_lba_len_flags *) 8845 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8846 lbalen->lba = lba; 8847 lbalen->len = num_blocks; 8848 lbalen->flags = CTL_LLF_COMPARE | flags; 8849 8850 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8851 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8852 return (retval); 8853 } 8854 8855 int 8856 ctl_verify(struct ctl_scsiio *ctsio) 8857 { 8858 struct ctl_lun *lun; 8859 struct ctl_lba_len_flags *lbalen; 8860 uint64_t lba; 8861 uint32_t num_blocks; 8862 int bytchk, flags; 8863 int retval; 8864 8865 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8866 8867 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8868 8869 bytchk = 0; 8870 flags = CTL_LLF_FUA; 8871 switch (ctsio->cdb[0]) { 8872 case VERIFY_10: { 8873 struct scsi_verify_10 *cdb; 8874 8875 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8876 if (cdb->byte2 & SVFY_BYTCHK) 8877 bytchk = 1; 8878 if (cdb->byte2 & SVFY_DPO) 8879 flags |= CTL_LLF_DPO; 8880 lba = scsi_4btoul(cdb->addr); 8881 num_blocks = scsi_2btoul(cdb->length); 8882 break; 8883 } 8884 case VERIFY_12: { 8885 struct scsi_verify_12 *cdb; 8886 8887 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8888 if (cdb->byte2 & SVFY_BYTCHK) 8889 bytchk = 1; 8890 if (cdb->byte2 & SVFY_DPO) 8891 flags |= CTL_LLF_DPO; 8892 lba = scsi_4btoul(cdb->addr); 8893 num_blocks = scsi_4btoul(cdb->length); 8894 break; 8895 } 8896 case VERIFY_16: { 8897 struct scsi_rw_16 *cdb; 8898 8899 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8900 if (cdb->byte2 & SVFY_BYTCHK) 8901 bytchk = 1; 8902 if (cdb->byte2 & SVFY_DPO) 8903 flags |= CTL_LLF_DPO; 8904 lba = scsi_8btou64(cdb->addr); 8905 num_blocks = scsi_4btoul(cdb->length); 8906 break; 8907 } 8908 default: 8909 /* 8910 * We got a command we don't support. This shouldn't 8911 * happen, commands should be filtered out above us. 8912 */ 8913 ctl_set_invalid_opcode(ctsio); 8914 ctl_done((union ctl_io *)ctsio); 8915 return (CTL_RETVAL_COMPLETE); 8916 } 8917 8918 /* 8919 * The first check is to make sure we're in bounds, the second 8920 * check is to catch wrap-around problems. If the lba + num blocks 8921 * is less than the lba, then we've wrapped around and the block 8922 * range is invalid anyway. 8923 */ 8924 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8925 || ((lba + num_blocks) < lba)) { 8926 ctl_set_lba_out_of_range(ctsio); 8927 ctl_done((union ctl_io *)ctsio); 8928 return (CTL_RETVAL_COMPLETE); 8929 } 8930 8931 /* 8932 * According to SBC-3, a transfer length of 0 is not an error. 8933 */ 8934 if (num_blocks == 0) { 8935 ctl_set_success(ctsio); 8936 ctl_done((union ctl_io *)ctsio); 8937 return (CTL_RETVAL_COMPLETE); 8938 } 8939 8940 lbalen = (struct ctl_lba_len_flags *) 8941 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8942 lbalen->lba = lba; 8943 lbalen->len = num_blocks; 8944 if (bytchk) { 8945 lbalen->flags = CTL_LLF_COMPARE | flags; 8946 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8947 } else { 8948 lbalen->flags = CTL_LLF_VERIFY | flags; 8949 ctsio->kern_total_len = 0; 8950 } 8951 ctsio->kern_rel_offset = 0; 8952 8953 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 8954 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8955 return (retval); 8956 } 8957 8958 int 8959 ctl_report_luns(struct ctl_scsiio *ctsio) 8960 { 8961 struct ctl_softc *softc; 8962 struct scsi_report_luns *cdb; 8963 struct scsi_report_luns_data *lun_data; 8964 struct ctl_lun *lun, *request_lun; 8965 struct ctl_port *port; 8966 int num_luns, retval; 8967 uint32_t alloc_len, lun_datalen; 8968 int num_filled; 8969 uint32_t initidx, targ_lun_id, lun_id; 8970 8971 retval = CTL_RETVAL_COMPLETE; 8972 cdb = (struct scsi_report_luns *)ctsio->cdb; 8973 port = ctl_io_port(&ctsio->io_hdr); 8974 softc = port->ctl_softc; 8975 8976 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 8977 8978 mtx_lock(&softc->ctl_lock); 8979 num_luns = 0; 8980 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 8981 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 8982 num_luns++; 8983 } 8984 mtx_unlock(&softc->ctl_lock); 8985 8986 switch (cdb->select_report) { 8987 case RPL_REPORT_DEFAULT: 8988 case RPL_REPORT_ALL: 8989 case RPL_REPORT_NONSUBSID: 8990 break; 8991 case RPL_REPORT_WELLKNOWN: 8992 case RPL_REPORT_ADMIN: 8993 case RPL_REPORT_CONGLOM: 8994 num_luns = 0; 8995 break; 8996 default: 8997 ctl_set_invalid_field(ctsio, 8998 /*sks_valid*/ 1, 8999 /*command*/ 1, 9000 /*field*/ 2, 9001 /*bit_valid*/ 0, 9002 /*bit*/ 0); 9003 ctl_done((union ctl_io *)ctsio); 9004 return (retval); 9005 break; /* NOTREACHED */ 9006 } 9007 9008 alloc_len = scsi_4btoul(cdb->length); 9009 /* 9010 * The initiator has to allocate at least 16 bytes for this request, 9011 * so he can at least get the header and the first LUN. Otherwise 9012 * we reject the request (per SPC-3 rev 14, section 6.21). 9013 */ 9014 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9015 sizeof(struct scsi_report_luns_lundata))) { 9016 ctl_set_invalid_field(ctsio, 9017 /*sks_valid*/ 1, 9018 /*command*/ 1, 9019 /*field*/ 6, 9020 /*bit_valid*/ 0, 9021 /*bit*/ 0); 9022 ctl_done((union ctl_io *)ctsio); 9023 return (retval); 9024 } 9025 9026 request_lun = (struct ctl_lun *) 9027 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9028 9029 lun_datalen = sizeof(*lun_data) + 9030 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9031 9032 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9033 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9034 ctsio->kern_sg_entries = 0; 9035 9036 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9037 9038 mtx_lock(&softc->ctl_lock); 9039 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9040 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9041 if (lun_id >= CTL_MAX_LUNS) 9042 continue; 9043 lun = softc->ctl_luns[lun_id]; 9044 if (lun == NULL) 9045 continue; 9046 9047 if (targ_lun_id <= 0xff) { 9048 /* 9049 * Peripheral addressing method, bus number 0. 9050 */ 9051 lun_data->luns[num_filled].lundata[0] = 9052 RPL_LUNDATA_ATYP_PERIPH; 9053 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9054 num_filled++; 9055 } else if (targ_lun_id <= 0x3fff) { 9056 /* 9057 * Flat addressing method. 9058 */ 9059 lun_data->luns[num_filled].lundata[0] = 9060 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9061 lun_data->luns[num_filled].lundata[1] = 9062 (targ_lun_id & 0xff); 9063 num_filled++; 9064 } else if (targ_lun_id <= 0xffffff) { 9065 /* 9066 * Extended flat addressing method. 9067 */ 9068 lun_data->luns[num_filled].lundata[0] = 9069 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9070 scsi_ulto3b(targ_lun_id, 9071 &lun_data->luns[num_filled].lundata[1]); 9072 num_filled++; 9073 } else { 9074 printf("ctl_report_luns: bogus LUN number %jd, " 9075 "skipping\n", (intmax_t)targ_lun_id); 9076 } 9077 /* 9078 * According to SPC-3, rev 14 section 6.21: 9079 * 9080 * "The execution of a REPORT LUNS command to any valid and 9081 * installed logical unit shall clear the REPORTED LUNS DATA 9082 * HAS CHANGED unit attention condition for all logical 9083 * units of that target with respect to the requesting 9084 * initiator. A valid and installed logical unit is one 9085 * having a PERIPHERAL QUALIFIER of 000b in the standard 9086 * INQUIRY data (see 6.4.2)." 9087 * 9088 * If request_lun is NULL, the LUN this report luns command 9089 * was issued to is either disabled or doesn't exist. In that 9090 * case, we shouldn't clear any pending lun change unit 9091 * attention. 9092 */ 9093 if (request_lun != NULL) { 9094 mtx_lock(&lun->lun_lock); 9095 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9096 mtx_unlock(&lun->lun_lock); 9097 } 9098 } 9099 mtx_unlock(&softc->ctl_lock); 9100 9101 /* 9102 * It's quite possible that we've returned fewer LUNs than we allocated 9103 * space for. Trim it. 9104 */ 9105 lun_datalen = sizeof(*lun_data) + 9106 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9107 9108 if (lun_datalen < alloc_len) { 9109 ctsio->residual = alloc_len - lun_datalen; 9110 ctsio->kern_data_len = lun_datalen; 9111 ctsio->kern_total_len = lun_datalen; 9112 } else { 9113 ctsio->residual = 0; 9114 ctsio->kern_data_len = alloc_len; 9115 ctsio->kern_total_len = alloc_len; 9116 } 9117 ctsio->kern_data_resid = 0; 9118 ctsio->kern_rel_offset = 0; 9119 ctsio->kern_sg_entries = 0; 9120 9121 /* 9122 * We set this to the actual data length, regardless of how much 9123 * space we actually have to return results. If the user looks at 9124 * this value, he'll know whether or not he allocated enough space 9125 * and reissue the command if necessary. We don't support well 9126 * known logical units, so if the user asks for that, return none. 9127 */ 9128 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9129 9130 /* 9131 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9132 * this request. 9133 */ 9134 ctl_set_success(ctsio); 9135 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9136 ctsio->be_move_done = ctl_config_move_done; 9137 ctl_datamove((union ctl_io *)ctsio); 9138 return (retval); 9139 } 9140 9141 int 9142 ctl_request_sense(struct ctl_scsiio *ctsio) 9143 { 9144 struct scsi_request_sense *cdb; 9145 struct scsi_sense_data *sense_ptr; 9146 struct ctl_softc *ctl_softc; 9147 struct ctl_lun *lun; 9148 uint32_t initidx; 9149 int have_error; 9150 scsi_sense_data_type sense_format; 9151 ctl_ua_type ua_type; 9152 9153 cdb = (struct scsi_request_sense *)ctsio->cdb; 9154 9155 ctl_softc = control_softc; 9156 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9157 9158 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9159 9160 /* 9161 * Determine which sense format the user wants. 9162 */ 9163 if (cdb->byte2 & SRS_DESC) 9164 sense_format = SSD_TYPE_DESC; 9165 else 9166 sense_format = SSD_TYPE_FIXED; 9167 9168 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9169 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9170 ctsio->kern_sg_entries = 0; 9171 9172 /* 9173 * struct scsi_sense_data, which is currently set to 256 bytes, is 9174 * larger than the largest allowed value for the length field in the 9175 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9176 */ 9177 ctsio->residual = 0; 9178 ctsio->kern_data_len = cdb->length; 9179 ctsio->kern_total_len = cdb->length; 9180 9181 ctsio->kern_data_resid = 0; 9182 ctsio->kern_rel_offset = 0; 9183 ctsio->kern_sg_entries = 0; 9184 9185 /* 9186 * If we don't have a LUN, we don't have any pending sense. 9187 */ 9188 if (lun == NULL) 9189 goto no_sense; 9190 9191 have_error = 0; 9192 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9193 /* 9194 * Check for pending sense, and then for pending unit attentions. 9195 * Pending sense gets returned first, then pending unit attentions. 9196 */ 9197 mtx_lock(&lun->lun_lock); 9198 #ifdef CTL_WITH_CA 9199 if (ctl_is_set(lun->have_ca, initidx)) { 9200 scsi_sense_data_type stored_format; 9201 9202 /* 9203 * Check to see which sense format was used for the stored 9204 * sense data. 9205 */ 9206 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9207 9208 /* 9209 * If the user requested a different sense format than the 9210 * one we stored, then we need to convert it to the other 9211 * format. If we're going from descriptor to fixed format 9212 * sense data, we may lose things in translation, depending 9213 * on what options were used. 9214 * 9215 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9216 * for some reason we'll just copy it out as-is. 9217 */ 9218 if ((stored_format == SSD_TYPE_FIXED) 9219 && (sense_format == SSD_TYPE_DESC)) 9220 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9221 &lun->pending_sense[initidx], 9222 (struct scsi_sense_data_desc *)sense_ptr); 9223 else if ((stored_format == SSD_TYPE_DESC) 9224 && (sense_format == SSD_TYPE_FIXED)) 9225 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9226 &lun->pending_sense[initidx], 9227 (struct scsi_sense_data_fixed *)sense_ptr); 9228 else 9229 memcpy(sense_ptr, &lun->pending_sense[initidx], 9230 MIN(sizeof(*sense_ptr), 9231 sizeof(lun->pending_sense[initidx]))); 9232 9233 ctl_clear_mask(lun->have_ca, initidx); 9234 have_error = 1; 9235 } else 9236 #endif 9237 { 9238 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9239 if (ua_type != CTL_UA_NONE) 9240 have_error = 1; 9241 if (ua_type == CTL_UA_LUN_CHANGE) { 9242 mtx_unlock(&lun->lun_lock); 9243 mtx_lock(&ctl_softc->ctl_lock); 9244 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type); 9245 mtx_unlock(&ctl_softc->ctl_lock); 9246 mtx_lock(&lun->lun_lock); 9247 } 9248 9249 } 9250 mtx_unlock(&lun->lun_lock); 9251 9252 /* 9253 * We already have a pending error, return it. 9254 */ 9255 if (have_error != 0) { 9256 /* 9257 * We report the SCSI status as OK, since the status of the 9258 * request sense command itself is OK. 9259 * We report 0 for the sense length, because we aren't doing 9260 * autosense in this case. We're reporting sense as 9261 * parameter data. 9262 */ 9263 ctl_set_success(ctsio); 9264 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9265 ctsio->be_move_done = ctl_config_move_done; 9266 ctl_datamove((union ctl_io *)ctsio); 9267 return (CTL_RETVAL_COMPLETE); 9268 } 9269 9270 no_sense: 9271 9272 /* 9273 * No sense information to report, so we report that everything is 9274 * okay. 9275 */ 9276 ctl_set_sense_data(sense_ptr, 9277 lun, 9278 sense_format, 9279 /*current_error*/ 1, 9280 /*sense_key*/ SSD_KEY_NO_SENSE, 9281 /*asc*/ 0x00, 9282 /*ascq*/ 0x00, 9283 SSD_ELEM_NONE); 9284 9285 /* 9286 * We report 0 for the sense length, because we aren't doing 9287 * autosense in this case. We're reporting sense as parameter data. 9288 */ 9289 ctl_set_success(ctsio); 9290 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9291 ctsio->be_move_done = ctl_config_move_done; 9292 ctl_datamove((union ctl_io *)ctsio); 9293 return (CTL_RETVAL_COMPLETE); 9294 } 9295 9296 int 9297 ctl_tur(struct ctl_scsiio *ctsio) 9298 { 9299 9300 CTL_DEBUG_PRINT(("ctl_tur\n")); 9301 9302 ctl_set_success(ctsio); 9303 ctl_done((union ctl_io *)ctsio); 9304 9305 return (CTL_RETVAL_COMPLETE); 9306 } 9307 9308 /* 9309 * SCSI VPD page 0x00, the Supported VPD Pages page. 9310 */ 9311 static int 9312 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9313 { 9314 struct scsi_vpd_supported_pages *pages; 9315 int sup_page_size; 9316 struct ctl_lun *lun; 9317 int p; 9318 9319 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9320 9321 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9322 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9323 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9324 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9325 ctsio->kern_sg_entries = 0; 9326 9327 if (sup_page_size < alloc_len) { 9328 ctsio->residual = alloc_len - sup_page_size; 9329 ctsio->kern_data_len = sup_page_size; 9330 ctsio->kern_total_len = sup_page_size; 9331 } else { 9332 ctsio->residual = 0; 9333 ctsio->kern_data_len = alloc_len; 9334 ctsio->kern_total_len = alloc_len; 9335 } 9336 ctsio->kern_data_resid = 0; 9337 ctsio->kern_rel_offset = 0; 9338 ctsio->kern_sg_entries = 0; 9339 9340 /* 9341 * The control device is always connected. The disk device, on the 9342 * other hand, may not be online all the time. Need to change this 9343 * to figure out whether the disk device is actually online or not. 9344 */ 9345 if (lun != NULL) 9346 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9347 lun->be_lun->lun_type; 9348 else 9349 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9350 9351 p = 0; 9352 /* Supported VPD pages */ 9353 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9354 /* Serial Number */ 9355 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9356 /* Device Identification */ 9357 pages->page_list[p++] = SVPD_DEVICE_ID; 9358 /* Extended INQUIRY Data */ 9359 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9360 /* Mode Page Policy */ 9361 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9362 /* SCSI Ports */ 9363 pages->page_list[p++] = SVPD_SCSI_PORTS; 9364 /* Third-party Copy */ 9365 pages->page_list[p++] = SVPD_SCSI_TPC; 9366 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9367 /* Block limits */ 9368 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9369 /* Block Device Characteristics */ 9370 pages->page_list[p++] = SVPD_BDC; 9371 /* Logical Block Provisioning */ 9372 pages->page_list[p++] = SVPD_LBP; 9373 } 9374 pages->length = p; 9375 9376 ctl_set_success(ctsio); 9377 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9378 ctsio->be_move_done = ctl_config_move_done; 9379 ctl_datamove((union ctl_io *)ctsio); 9380 return (CTL_RETVAL_COMPLETE); 9381 } 9382 9383 /* 9384 * SCSI VPD page 0x80, the Unit Serial Number page. 9385 */ 9386 static int 9387 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9388 { 9389 struct scsi_vpd_unit_serial_number *sn_ptr; 9390 struct ctl_lun *lun; 9391 int data_len; 9392 9393 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9394 9395 data_len = 4 + CTL_SN_LEN; 9396 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9397 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9398 if (data_len < alloc_len) { 9399 ctsio->residual = alloc_len - data_len; 9400 ctsio->kern_data_len = data_len; 9401 ctsio->kern_total_len = data_len; 9402 } else { 9403 ctsio->residual = 0; 9404 ctsio->kern_data_len = alloc_len; 9405 ctsio->kern_total_len = alloc_len; 9406 } 9407 ctsio->kern_data_resid = 0; 9408 ctsio->kern_rel_offset = 0; 9409 ctsio->kern_sg_entries = 0; 9410 9411 /* 9412 * The control device is always connected. The disk device, on the 9413 * other hand, may not be online all the time. Need to change this 9414 * to figure out whether the disk device is actually online or not. 9415 */ 9416 if (lun != NULL) 9417 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9418 lun->be_lun->lun_type; 9419 else 9420 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9421 9422 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9423 sn_ptr->length = CTL_SN_LEN; 9424 /* 9425 * If we don't have a LUN, we just leave the serial number as 9426 * all spaces. 9427 */ 9428 if (lun != NULL) { 9429 strncpy((char *)sn_ptr->serial_num, 9430 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9431 } else 9432 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9433 9434 ctl_set_success(ctsio); 9435 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9436 ctsio->be_move_done = ctl_config_move_done; 9437 ctl_datamove((union ctl_io *)ctsio); 9438 return (CTL_RETVAL_COMPLETE); 9439 } 9440 9441 9442 /* 9443 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9444 */ 9445 static int 9446 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9447 { 9448 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9449 struct ctl_lun *lun; 9450 int data_len; 9451 9452 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9453 9454 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9455 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9456 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9457 ctsio->kern_sg_entries = 0; 9458 9459 if (data_len < alloc_len) { 9460 ctsio->residual = alloc_len - data_len; 9461 ctsio->kern_data_len = data_len; 9462 ctsio->kern_total_len = data_len; 9463 } else { 9464 ctsio->residual = 0; 9465 ctsio->kern_data_len = alloc_len; 9466 ctsio->kern_total_len = alloc_len; 9467 } 9468 ctsio->kern_data_resid = 0; 9469 ctsio->kern_rel_offset = 0; 9470 ctsio->kern_sg_entries = 0; 9471 9472 /* 9473 * The control device is always connected. The disk device, on the 9474 * other hand, may not be online all the time. 9475 */ 9476 if (lun != NULL) 9477 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9478 lun->be_lun->lun_type; 9479 else 9480 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9481 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9482 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9483 /* 9484 * We support head of queue, ordered and simple tags. 9485 */ 9486 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9487 /* 9488 * Volatile cache supported. 9489 */ 9490 eid_ptr->flags3 = SVPD_EID_V_SUP; 9491 9492 /* 9493 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9494 * attention for a particular IT nexus on all LUNs once we report 9495 * it to that nexus once. This bit is required as of SPC-4. 9496 */ 9497 eid_ptr->flags4 = SVPD_EID_LUICLT; 9498 9499 /* 9500 * XXX KDM in order to correctly answer this, we would need 9501 * information from the SIM to determine how much sense data it 9502 * can send. So this would really be a path inquiry field, most 9503 * likely. This can be set to a maximum of 252 according to SPC-4, 9504 * but the hardware may or may not be able to support that much. 9505 * 0 just means that the maximum sense data length is not reported. 9506 */ 9507 eid_ptr->max_sense_length = 0; 9508 9509 ctl_set_success(ctsio); 9510 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9511 ctsio->be_move_done = ctl_config_move_done; 9512 ctl_datamove((union ctl_io *)ctsio); 9513 return (CTL_RETVAL_COMPLETE); 9514 } 9515 9516 static int 9517 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9518 { 9519 struct scsi_vpd_mode_page_policy *mpp_ptr; 9520 struct ctl_lun *lun; 9521 int data_len; 9522 9523 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9524 9525 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9526 sizeof(struct scsi_vpd_mode_page_policy_descr); 9527 9528 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9529 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9530 ctsio->kern_sg_entries = 0; 9531 9532 if (data_len < alloc_len) { 9533 ctsio->residual = alloc_len - data_len; 9534 ctsio->kern_data_len = data_len; 9535 ctsio->kern_total_len = data_len; 9536 } else { 9537 ctsio->residual = 0; 9538 ctsio->kern_data_len = alloc_len; 9539 ctsio->kern_total_len = alloc_len; 9540 } 9541 ctsio->kern_data_resid = 0; 9542 ctsio->kern_rel_offset = 0; 9543 ctsio->kern_sg_entries = 0; 9544 9545 /* 9546 * The control device is always connected. The disk device, on the 9547 * other hand, may not be online all the time. 9548 */ 9549 if (lun != NULL) 9550 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9551 lun->be_lun->lun_type; 9552 else 9553 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9554 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9555 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9556 mpp_ptr->descr[0].page_code = 0x3f; 9557 mpp_ptr->descr[0].subpage_code = 0xff; 9558 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9559 9560 ctl_set_success(ctsio); 9561 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9562 ctsio->be_move_done = ctl_config_move_done; 9563 ctl_datamove((union ctl_io *)ctsio); 9564 return (CTL_RETVAL_COMPLETE); 9565 } 9566 9567 /* 9568 * SCSI VPD page 0x83, the Device Identification page. 9569 */ 9570 static int 9571 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9572 { 9573 struct scsi_vpd_device_id *devid_ptr; 9574 struct scsi_vpd_id_descriptor *desc; 9575 struct ctl_softc *softc; 9576 struct ctl_lun *lun; 9577 struct ctl_port *port; 9578 int data_len; 9579 uint8_t proto; 9580 9581 softc = control_softc; 9582 9583 port = ctl_io_port(&ctsio->io_hdr); 9584 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9585 9586 data_len = sizeof(struct scsi_vpd_device_id) + 9587 sizeof(struct scsi_vpd_id_descriptor) + 9588 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9589 sizeof(struct scsi_vpd_id_descriptor) + 9590 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9591 if (lun && lun->lun_devid) 9592 data_len += lun->lun_devid->len; 9593 if (port && port->port_devid) 9594 data_len += port->port_devid->len; 9595 if (port && port->target_devid) 9596 data_len += port->target_devid->len; 9597 9598 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9599 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9600 ctsio->kern_sg_entries = 0; 9601 9602 if (data_len < alloc_len) { 9603 ctsio->residual = alloc_len - data_len; 9604 ctsio->kern_data_len = data_len; 9605 ctsio->kern_total_len = data_len; 9606 } else { 9607 ctsio->residual = 0; 9608 ctsio->kern_data_len = alloc_len; 9609 ctsio->kern_total_len = alloc_len; 9610 } 9611 ctsio->kern_data_resid = 0; 9612 ctsio->kern_rel_offset = 0; 9613 ctsio->kern_sg_entries = 0; 9614 9615 /* 9616 * The control device is always connected. The disk device, on the 9617 * other hand, may not be online all the time. 9618 */ 9619 if (lun != NULL) 9620 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9621 lun->be_lun->lun_type; 9622 else 9623 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9624 devid_ptr->page_code = SVPD_DEVICE_ID; 9625 scsi_ulto2b(data_len - 4, devid_ptr->length); 9626 9627 if (port && port->port_type == CTL_PORT_FC) 9628 proto = SCSI_PROTO_FC << 4; 9629 else if (port && port->port_type == CTL_PORT_ISCSI) 9630 proto = SCSI_PROTO_ISCSI << 4; 9631 else 9632 proto = SCSI_PROTO_SPI << 4; 9633 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9634 9635 /* 9636 * We're using a LUN association here. i.e., this device ID is a 9637 * per-LUN identifier. 9638 */ 9639 if (lun && lun->lun_devid) { 9640 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9641 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9642 lun->lun_devid->len); 9643 } 9644 9645 /* 9646 * This is for the WWPN which is a port association. 9647 */ 9648 if (port && port->port_devid) { 9649 memcpy(desc, port->port_devid->data, port->port_devid->len); 9650 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9651 port->port_devid->len); 9652 } 9653 9654 /* 9655 * This is for the Relative Target Port(type 4h) identifier 9656 */ 9657 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9658 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9659 SVPD_ID_TYPE_RELTARG; 9660 desc->length = 4; 9661 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9662 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9663 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9664 9665 /* 9666 * This is for the Target Port Group(type 5h) identifier 9667 */ 9668 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9669 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9670 SVPD_ID_TYPE_TPORTGRP; 9671 desc->length = 4; 9672 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1, 9673 &desc->identifier[2]); 9674 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9675 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9676 9677 /* 9678 * This is for the Target identifier 9679 */ 9680 if (port && port->target_devid) { 9681 memcpy(desc, port->target_devid->data, port->target_devid->len); 9682 } 9683 9684 ctl_set_success(ctsio); 9685 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9686 ctsio->be_move_done = ctl_config_move_done; 9687 ctl_datamove((union ctl_io *)ctsio); 9688 return (CTL_RETVAL_COMPLETE); 9689 } 9690 9691 static int 9692 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9693 { 9694 struct ctl_softc *softc = control_softc; 9695 struct scsi_vpd_scsi_ports *sp; 9696 struct scsi_vpd_port_designation *pd; 9697 struct scsi_vpd_port_designation_cont *pdc; 9698 struct ctl_lun *lun; 9699 struct ctl_port *port; 9700 int data_len, num_target_ports, iid_len, id_len; 9701 9702 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9703 9704 num_target_ports = 0; 9705 iid_len = 0; 9706 id_len = 0; 9707 mtx_lock(&softc->ctl_lock); 9708 STAILQ_FOREACH(port, &softc->port_list, links) { 9709 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9710 continue; 9711 if (lun != NULL && 9712 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9713 continue; 9714 num_target_ports++; 9715 if (port->init_devid) 9716 iid_len += port->init_devid->len; 9717 if (port->port_devid) 9718 id_len += port->port_devid->len; 9719 } 9720 mtx_unlock(&softc->ctl_lock); 9721 9722 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9723 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9724 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9725 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9726 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9727 ctsio->kern_sg_entries = 0; 9728 9729 if (data_len < alloc_len) { 9730 ctsio->residual = alloc_len - data_len; 9731 ctsio->kern_data_len = data_len; 9732 ctsio->kern_total_len = data_len; 9733 } else { 9734 ctsio->residual = 0; 9735 ctsio->kern_data_len = alloc_len; 9736 ctsio->kern_total_len = alloc_len; 9737 } 9738 ctsio->kern_data_resid = 0; 9739 ctsio->kern_rel_offset = 0; 9740 ctsio->kern_sg_entries = 0; 9741 9742 /* 9743 * The control device is always connected. The disk device, on the 9744 * other hand, may not be online all the time. Need to change this 9745 * to figure out whether the disk device is actually online or not. 9746 */ 9747 if (lun != NULL) 9748 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9749 lun->be_lun->lun_type; 9750 else 9751 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9752 9753 sp->page_code = SVPD_SCSI_PORTS; 9754 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9755 sp->page_length); 9756 pd = &sp->design[0]; 9757 9758 mtx_lock(&softc->ctl_lock); 9759 STAILQ_FOREACH(port, &softc->port_list, links) { 9760 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9761 continue; 9762 if (lun != NULL && 9763 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9764 continue; 9765 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9766 if (port->init_devid) { 9767 iid_len = port->init_devid->len; 9768 memcpy(pd->initiator_transportid, 9769 port->init_devid->data, port->init_devid->len); 9770 } else 9771 iid_len = 0; 9772 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9773 pdc = (struct scsi_vpd_port_designation_cont *) 9774 (&pd->initiator_transportid[iid_len]); 9775 if (port->port_devid) { 9776 id_len = port->port_devid->len; 9777 memcpy(pdc->target_port_descriptors, 9778 port->port_devid->data, port->port_devid->len); 9779 } else 9780 id_len = 0; 9781 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9782 pd = (struct scsi_vpd_port_designation *) 9783 ((uint8_t *)pdc->target_port_descriptors + id_len); 9784 } 9785 mtx_unlock(&softc->ctl_lock); 9786 9787 ctl_set_success(ctsio); 9788 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9789 ctsio->be_move_done = ctl_config_move_done; 9790 ctl_datamove((union ctl_io *)ctsio); 9791 return (CTL_RETVAL_COMPLETE); 9792 } 9793 9794 static int 9795 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9796 { 9797 struct scsi_vpd_block_limits *bl_ptr; 9798 struct ctl_lun *lun; 9799 9800 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9801 9802 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9803 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9804 ctsio->kern_sg_entries = 0; 9805 9806 if (sizeof(*bl_ptr) < alloc_len) { 9807 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9808 ctsio->kern_data_len = sizeof(*bl_ptr); 9809 ctsio->kern_total_len = sizeof(*bl_ptr); 9810 } else { 9811 ctsio->residual = 0; 9812 ctsio->kern_data_len = alloc_len; 9813 ctsio->kern_total_len = alloc_len; 9814 } 9815 ctsio->kern_data_resid = 0; 9816 ctsio->kern_rel_offset = 0; 9817 ctsio->kern_sg_entries = 0; 9818 9819 /* 9820 * The control device is always connected. The disk device, on the 9821 * other hand, may not be online all the time. Need to change this 9822 * to figure out whether the disk device is actually online or not. 9823 */ 9824 if (lun != NULL) 9825 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9826 lun->be_lun->lun_type; 9827 else 9828 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9829 9830 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9831 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9832 bl_ptr->max_cmp_write_len = 0xff; 9833 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9834 if (lun != NULL) { 9835 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9836 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9837 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 9838 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 9839 if (lun->be_lun->ublockexp != 0) { 9840 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9841 bl_ptr->opt_unmap_grain); 9842 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9843 bl_ptr->unmap_grain_align); 9844 } 9845 } 9846 scsi_ulto4b(lun->be_lun->atomicblock, 9847 bl_ptr->max_atomic_transfer_length); 9848 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9849 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9850 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9851 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9852 } 9853 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 9854 9855 ctl_set_success(ctsio); 9856 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9857 ctsio->be_move_done = ctl_config_move_done; 9858 ctl_datamove((union ctl_io *)ctsio); 9859 return (CTL_RETVAL_COMPLETE); 9860 } 9861 9862 static int 9863 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9864 { 9865 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9866 struct ctl_lun *lun; 9867 const char *value; 9868 u_int i; 9869 9870 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9871 9872 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9873 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9874 ctsio->kern_sg_entries = 0; 9875 9876 if (sizeof(*bdc_ptr) < alloc_len) { 9877 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9878 ctsio->kern_data_len = sizeof(*bdc_ptr); 9879 ctsio->kern_total_len = sizeof(*bdc_ptr); 9880 } else { 9881 ctsio->residual = 0; 9882 ctsio->kern_data_len = alloc_len; 9883 ctsio->kern_total_len = alloc_len; 9884 } 9885 ctsio->kern_data_resid = 0; 9886 ctsio->kern_rel_offset = 0; 9887 ctsio->kern_sg_entries = 0; 9888 9889 /* 9890 * The control device is always connected. The disk device, on the 9891 * other hand, may not be online all the time. Need to change this 9892 * to figure out whether the disk device is actually online or not. 9893 */ 9894 if (lun != NULL) 9895 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9896 lun->be_lun->lun_type; 9897 else 9898 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9899 bdc_ptr->page_code = SVPD_BDC; 9900 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9901 if (lun != NULL && 9902 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9903 i = strtol(value, NULL, 0); 9904 else 9905 i = CTL_DEFAULT_ROTATION_RATE; 9906 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9907 if (lun != NULL && 9908 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9909 i = strtol(value, NULL, 0); 9910 else 9911 i = 0; 9912 bdc_ptr->wab_wac_ff = (i & 0x0f); 9913 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9914 9915 ctl_set_success(ctsio); 9916 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9917 ctsio->be_move_done = ctl_config_move_done; 9918 ctl_datamove((union ctl_io *)ctsio); 9919 return (CTL_RETVAL_COMPLETE); 9920 } 9921 9922 static int 9923 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9924 { 9925 struct scsi_vpd_logical_block_prov *lbp_ptr; 9926 struct ctl_lun *lun; 9927 9928 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9929 9930 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9931 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9932 ctsio->kern_sg_entries = 0; 9933 9934 if (sizeof(*lbp_ptr) < alloc_len) { 9935 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9936 ctsio->kern_data_len = sizeof(*lbp_ptr); 9937 ctsio->kern_total_len = sizeof(*lbp_ptr); 9938 } else { 9939 ctsio->residual = 0; 9940 ctsio->kern_data_len = alloc_len; 9941 ctsio->kern_total_len = alloc_len; 9942 } 9943 ctsio->kern_data_resid = 0; 9944 ctsio->kern_rel_offset = 0; 9945 ctsio->kern_sg_entries = 0; 9946 9947 /* 9948 * The control device is always connected. The disk device, on the 9949 * other hand, may not be online all the time. Need to change this 9950 * to figure out whether the disk device is actually online or not. 9951 */ 9952 if (lun != NULL) 9953 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9954 lun->be_lun->lun_type; 9955 else 9956 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9957 9958 lbp_ptr->page_code = SVPD_LBP; 9959 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9960 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9961 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9962 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9963 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9964 lbp_ptr->prov_type = SVPD_LBP_THIN; 9965 } 9966 9967 ctl_set_success(ctsio); 9968 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9969 ctsio->be_move_done = ctl_config_move_done; 9970 ctl_datamove((union ctl_io *)ctsio); 9971 return (CTL_RETVAL_COMPLETE); 9972 } 9973 9974 /* 9975 * INQUIRY with the EVPD bit set. 9976 */ 9977 static int 9978 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9979 { 9980 struct ctl_lun *lun; 9981 struct scsi_inquiry *cdb; 9982 int alloc_len, retval; 9983 9984 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9985 cdb = (struct scsi_inquiry *)ctsio->cdb; 9986 alloc_len = scsi_2btoul(cdb->length); 9987 9988 switch (cdb->page_code) { 9989 case SVPD_SUPPORTED_PAGES: 9990 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9991 break; 9992 case SVPD_UNIT_SERIAL_NUMBER: 9993 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9994 break; 9995 case SVPD_DEVICE_ID: 9996 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9997 break; 9998 case SVPD_EXTENDED_INQUIRY_DATA: 9999 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10000 break; 10001 case SVPD_MODE_PAGE_POLICY: 10002 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10003 break; 10004 case SVPD_SCSI_PORTS: 10005 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10006 break; 10007 case SVPD_SCSI_TPC: 10008 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10009 break; 10010 case SVPD_BLOCK_LIMITS: 10011 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10012 goto err; 10013 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10014 break; 10015 case SVPD_BDC: 10016 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10017 goto err; 10018 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10019 break; 10020 case SVPD_LBP: 10021 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10022 goto err; 10023 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10024 break; 10025 default: 10026 err: 10027 ctl_set_invalid_field(ctsio, 10028 /*sks_valid*/ 1, 10029 /*command*/ 1, 10030 /*field*/ 2, 10031 /*bit_valid*/ 0, 10032 /*bit*/ 0); 10033 ctl_done((union ctl_io *)ctsio); 10034 retval = CTL_RETVAL_COMPLETE; 10035 break; 10036 } 10037 10038 return (retval); 10039 } 10040 10041 /* 10042 * Standard INQUIRY data. 10043 */ 10044 static int 10045 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10046 { 10047 struct scsi_inquiry_data *inq_ptr; 10048 struct scsi_inquiry *cdb; 10049 struct ctl_softc *softc = control_softc; 10050 struct ctl_port *port; 10051 struct ctl_lun *lun; 10052 char *val; 10053 uint32_t alloc_len, data_len; 10054 ctl_port_type port_type; 10055 10056 port = ctl_io_port(&ctsio->io_hdr); 10057 port_type = port->port_type; 10058 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10059 port_type = CTL_PORT_SCSI; 10060 10061 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10062 cdb = (struct scsi_inquiry *)ctsio->cdb; 10063 alloc_len = scsi_2btoul(cdb->length); 10064 10065 /* 10066 * We malloc the full inquiry data size here and fill it 10067 * in. If the user only asks for less, we'll give him 10068 * that much. 10069 */ 10070 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10071 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10072 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10073 ctsio->kern_sg_entries = 0; 10074 ctsio->kern_data_resid = 0; 10075 ctsio->kern_rel_offset = 0; 10076 10077 if (data_len < alloc_len) { 10078 ctsio->residual = alloc_len - data_len; 10079 ctsio->kern_data_len = data_len; 10080 ctsio->kern_total_len = data_len; 10081 } else { 10082 ctsio->residual = 0; 10083 ctsio->kern_data_len = alloc_len; 10084 ctsio->kern_total_len = alloc_len; 10085 } 10086 10087 if (lun != NULL) { 10088 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10089 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10090 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10091 lun->be_lun->lun_type; 10092 } else { 10093 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10094 lun->be_lun->lun_type; 10095 } 10096 if (lun->flags & CTL_LUN_REMOVABLE) 10097 inq_ptr->dev_qual2 |= SID_RMB; 10098 } else 10099 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10100 10101 /* RMB in byte 2 is 0 */ 10102 inq_ptr->version = SCSI_REV_SPC4; 10103 10104 /* 10105 * According to SAM-3, even if a device only supports a single 10106 * level of LUN addressing, it should still set the HISUP bit: 10107 * 10108 * 4.9.1 Logical unit numbers overview 10109 * 10110 * All logical unit number formats described in this standard are 10111 * hierarchical in structure even when only a single level in that 10112 * hierarchy is used. The HISUP bit shall be set to one in the 10113 * standard INQUIRY data (see SPC-2) when any logical unit number 10114 * format described in this standard is used. Non-hierarchical 10115 * formats are outside the scope of this standard. 10116 * 10117 * Therefore we set the HiSup bit here. 10118 * 10119 * The reponse format is 2, per SPC-3. 10120 */ 10121 inq_ptr->response_format = SID_HiSup | 2; 10122 10123 inq_ptr->additional_length = data_len - 10124 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10125 CTL_DEBUG_PRINT(("additional_length = %d\n", 10126 inq_ptr->additional_length)); 10127 10128 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10129 if (port_type == CTL_PORT_SCSI) 10130 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10131 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10132 inq_ptr->flags = SID_CmdQue; 10133 if (port_type == CTL_PORT_SCSI) 10134 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10135 10136 /* 10137 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10138 * We have 8 bytes for the vendor name, and 16 bytes for the device 10139 * name and 4 bytes for the revision. 10140 */ 10141 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10142 "vendor")) == NULL) { 10143 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10144 } else { 10145 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10146 strncpy(inq_ptr->vendor, val, 10147 min(sizeof(inq_ptr->vendor), strlen(val))); 10148 } 10149 if (lun == NULL) { 10150 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10151 sizeof(inq_ptr->product)); 10152 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10153 switch (lun->be_lun->lun_type) { 10154 case T_DIRECT: 10155 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10156 sizeof(inq_ptr->product)); 10157 break; 10158 case T_PROCESSOR: 10159 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10160 sizeof(inq_ptr->product)); 10161 break; 10162 case T_CDROM: 10163 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10164 sizeof(inq_ptr->product)); 10165 break; 10166 default: 10167 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10168 sizeof(inq_ptr->product)); 10169 break; 10170 } 10171 } else { 10172 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10173 strncpy(inq_ptr->product, val, 10174 min(sizeof(inq_ptr->product), strlen(val))); 10175 } 10176 10177 /* 10178 * XXX make this a macro somewhere so it automatically gets 10179 * incremented when we make changes. 10180 */ 10181 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10182 "revision")) == NULL) { 10183 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10184 } else { 10185 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10186 strncpy(inq_ptr->revision, val, 10187 min(sizeof(inq_ptr->revision), strlen(val))); 10188 } 10189 10190 /* 10191 * For parallel SCSI, we support double transition and single 10192 * transition clocking. We also support QAS (Quick Arbitration 10193 * and Selection) and Information Unit transfers on both the 10194 * control and array devices. 10195 */ 10196 if (port_type == CTL_PORT_SCSI) 10197 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10198 SID_SPI_IUS; 10199 10200 /* SAM-5 (no version claimed) */ 10201 scsi_ulto2b(0x00A0, inq_ptr->version1); 10202 /* SPC-4 (no version claimed) */ 10203 scsi_ulto2b(0x0460, inq_ptr->version2); 10204 if (port_type == CTL_PORT_FC) { 10205 /* FCP-2 ANSI INCITS.350:2003 */ 10206 scsi_ulto2b(0x0917, inq_ptr->version3); 10207 } else if (port_type == CTL_PORT_SCSI) { 10208 /* SPI-4 ANSI INCITS.362:200x */ 10209 scsi_ulto2b(0x0B56, inq_ptr->version3); 10210 } else if (port_type == CTL_PORT_ISCSI) { 10211 /* iSCSI (no version claimed) */ 10212 scsi_ulto2b(0x0960, inq_ptr->version3); 10213 } else if (port_type == CTL_PORT_SAS) { 10214 /* SAS (no version claimed) */ 10215 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10216 } 10217 10218 if (lun == NULL) { 10219 /* SBC-4 (no version claimed) */ 10220 scsi_ulto2b(0x0600, inq_ptr->version4); 10221 } else { 10222 switch (lun->be_lun->lun_type) { 10223 case T_DIRECT: 10224 /* SBC-4 (no version claimed) */ 10225 scsi_ulto2b(0x0600, inq_ptr->version4); 10226 break; 10227 case T_PROCESSOR: 10228 break; 10229 case T_CDROM: 10230 /* MMC-6 (no version claimed) */ 10231 scsi_ulto2b(0x04E0, inq_ptr->version4); 10232 break; 10233 default: 10234 break; 10235 } 10236 } 10237 10238 ctl_set_success(ctsio); 10239 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10240 ctsio->be_move_done = ctl_config_move_done; 10241 ctl_datamove((union ctl_io *)ctsio); 10242 return (CTL_RETVAL_COMPLETE); 10243 } 10244 10245 int 10246 ctl_inquiry(struct ctl_scsiio *ctsio) 10247 { 10248 struct scsi_inquiry *cdb; 10249 int retval; 10250 10251 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10252 10253 cdb = (struct scsi_inquiry *)ctsio->cdb; 10254 if (cdb->byte2 & SI_EVPD) 10255 retval = ctl_inquiry_evpd(ctsio); 10256 else if (cdb->page_code == 0) 10257 retval = ctl_inquiry_std(ctsio); 10258 else { 10259 ctl_set_invalid_field(ctsio, 10260 /*sks_valid*/ 1, 10261 /*command*/ 1, 10262 /*field*/ 2, 10263 /*bit_valid*/ 0, 10264 /*bit*/ 0); 10265 ctl_done((union ctl_io *)ctsio); 10266 return (CTL_RETVAL_COMPLETE); 10267 } 10268 10269 return (retval); 10270 } 10271 10272 int 10273 ctl_get_config(struct ctl_scsiio *ctsio) 10274 { 10275 struct scsi_get_config_header *hdr; 10276 struct scsi_get_config_feature *feature; 10277 struct scsi_get_config *cdb; 10278 struct ctl_lun *lun; 10279 uint32_t alloc_len, data_len; 10280 int rt, starting; 10281 10282 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10283 cdb = (struct scsi_get_config *)ctsio->cdb; 10284 rt = (cdb->rt & SGC_RT_MASK); 10285 starting = scsi_2btoul(cdb->starting_feature); 10286 alloc_len = scsi_2btoul(cdb->length); 10287 10288 data_len = sizeof(struct scsi_get_config_header) + 10289 sizeof(struct scsi_get_config_feature) + 8 + 10290 sizeof(struct scsi_get_config_feature) + 8 + 10291 sizeof(struct scsi_get_config_feature) + 4 + 10292 sizeof(struct scsi_get_config_feature) + 4 + 10293 sizeof(struct scsi_get_config_feature) + 8 + 10294 sizeof(struct scsi_get_config_feature) + 10295 sizeof(struct scsi_get_config_feature) + 4 + 10296 sizeof(struct scsi_get_config_feature) + 4 + 10297 sizeof(struct scsi_get_config_feature) + 4 + 10298 sizeof(struct scsi_get_config_feature) + 4 + 10299 sizeof(struct scsi_get_config_feature) + 4 + 10300 sizeof(struct scsi_get_config_feature) + 4; 10301 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10302 ctsio->kern_sg_entries = 0; 10303 ctsio->kern_data_resid = 0; 10304 ctsio->kern_rel_offset = 0; 10305 10306 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10307 if (lun->flags & CTL_LUN_NO_MEDIA) 10308 scsi_ulto2b(0x0000, hdr->current_profile); 10309 else 10310 scsi_ulto2b(0x0010, hdr->current_profile); 10311 feature = (struct scsi_get_config_feature *)(hdr + 1); 10312 10313 if (starting > 0x003b) 10314 goto done; 10315 if (starting > 0x003a) 10316 goto f3b; 10317 if (starting > 0x002b) 10318 goto f3a; 10319 if (starting > 0x002a) 10320 goto f2b; 10321 if (starting > 0x001f) 10322 goto f2a; 10323 if (starting > 0x001e) 10324 goto f1f; 10325 if (starting > 0x001d) 10326 goto f1e; 10327 if (starting > 0x0010) 10328 goto f1d; 10329 if (starting > 0x0003) 10330 goto f10; 10331 if (starting > 0x0002) 10332 goto f3; 10333 if (starting > 0x0001) 10334 goto f2; 10335 if (starting > 0x0000) 10336 goto f1; 10337 10338 /* Profile List */ 10339 scsi_ulto2b(0x0000, feature->feature_code); 10340 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10341 feature->add_length = 8; 10342 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10343 feature->feature_data[2] = 0x00; 10344 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10345 feature->feature_data[6] = 0x01; 10346 feature = (struct scsi_get_config_feature *) 10347 &feature->feature_data[feature->add_length]; 10348 10349 f1: /* Core */ 10350 scsi_ulto2b(0x0001, feature->feature_code); 10351 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10352 feature->add_length = 8; 10353 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10354 feature->feature_data[4] = 0x03; 10355 feature = (struct scsi_get_config_feature *) 10356 &feature->feature_data[feature->add_length]; 10357 10358 f2: /* Morphing */ 10359 scsi_ulto2b(0x0002, feature->feature_code); 10360 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10361 feature->add_length = 4; 10362 feature->feature_data[0] = 0x02; 10363 feature = (struct scsi_get_config_feature *) 10364 &feature->feature_data[feature->add_length]; 10365 10366 f3: /* Removable Medium */ 10367 scsi_ulto2b(0x0003, feature->feature_code); 10368 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10369 feature->add_length = 4; 10370 feature->feature_data[0] = 0x39; 10371 feature = (struct scsi_get_config_feature *) 10372 &feature->feature_data[feature->add_length]; 10373 10374 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10375 goto done; 10376 10377 f10: /* Random Read */ 10378 scsi_ulto2b(0x0010, feature->feature_code); 10379 feature->flags = 0x00; 10380 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10381 feature->flags |= SGC_F_CURRENT; 10382 feature->add_length = 8; 10383 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10384 scsi_ulto2b(1, &feature->feature_data[4]); 10385 feature->feature_data[6] = 0x00; 10386 feature = (struct scsi_get_config_feature *) 10387 &feature->feature_data[feature->add_length]; 10388 10389 f1d: /* Multi-Read */ 10390 scsi_ulto2b(0x001D, feature->feature_code); 10391 feature->flags = 0x00; 10392 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10393 feature->flags |= SGC_F_CURRENT; 10394 feature->add_length = 0; 10395 feature = (struct scsi_get_config_feature *) 10396 &feature->feature_data[feature->add_length]; 10397 10398 f1e: /* CD Read */ 10399 scsi_ulto2b(0x001E, feature->feature_code); 10400 feature->flags = 0x00; 10401 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10402 feature->flags |= SGC_F_CURRENT; 10403 feature->add_length = 4; 10404 feature->feature_data[0] = 0x00; 10405 feature = (struct scsi_get_config_feature *) 10406 &feature->feature_data[feature->add_length]; 10407 10408 f1f: /* DVD Read */ 10409 scsi_ulto2b(0x001F, feature->feature_code); 10410 feature->flags = 0x08; 10411 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10412 feature->flags |= SGC_F_CURRENT; 10413 feature->add_length = 4; 10414 feature->feature_data[0] = 0x01; 10415 feature->feature_data[2] = 0x03; 10416 feature = (struct scsi_get_config_feature *) 10417 &feature->feature_data[feature->add_length]; 10418 10419 f2a: /* DVD+RW */ 10420 scsi_ulto2b(0x002A, feature->feature_code); 10421 feature->flags = 0x04; 10422 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10423 feature->flags |= SGC_F_CURRENT; 10424 feature->add_length = 4; 10425 feature->feature_data[0] = 0x00; 10426 feature->feature_data[1] = 0x00; 10427 feature = (struct scsi_get_config_feature *) 10428 &feature->feature_data[feature->add_length]; 10429 10430 f2b: /* DVD+R */ 10431 scsi_ulto2b(0x002B, feature->feature_code); 10432 feature->flags = 0x00; 10433 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10434 feature->flags |= SGC_F_CURRENT; 10435 feature->add_length = 4; 10436 feature->feature_data[0] = 0x00; 10437 feature = (struct scsi_get_config_feature *) 10438 &feature->feature_data[feature->add_length]; 10439 10440 f3a: /* DVD+RW Dual Layer */ 10441 scsi_ulto2b(0x003A, feature->feature_code); 10442 feature->flags = 0x00; 10443 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10444 feature->flags |= SGC_F_CURRENT; 10445 feature->add_length = 4; 10446 feature->feature_data[0] = 0x00; 10447 feature->feature_data[1] = 0x00; 10448 feature = (struct scsi_get_config_feature *) 10449 &feature->feature_data[feature->add_length]; 10450 10451 f3b: /* DVD+R Dual Layer */ 10452 scsi_ulto2b(0x003B, feature->feature_code); 10453 feature->flags = 0x00; 10454 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10455 feature->flags |= SGC_F_CURRENT; 10456 feature->add_length = 4; 10457 feature->feature_data[0] = 0x00; 10458 feature = (struct scsi_get_config_feature *) 10459 &feature->feature_data[feature->add_length]; 10460 10461 done: 10462 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10463 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10464 feature = (struct scsi_get_config_feature *)(hdr + 1); 10465 if (scsi_2btoul(feature->feature_code) == starting) 10466 feature = (struct scsi_get_config_feature *) 10467 &feature->feature_data[feature->add_length]; 10468 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10469 } 10470 scsi_ulto4b(data_len - 4, hdr->data_length); 10471 if (data_len < alloc_len) { 10472 ctsio->residual = alloc_len - data_len; 10473 ctsio->kern_data_len = data_len; 10474 ctsio->kern_total_len = data_len; 10475 } else { 10476 ctsio->residual = 0; 10477 ctsio->kern_data_len = alloc_len; 10478 ctsio->kern_total_len = alloc_len; 10479 } 10480 10481 ctl_set_success(ctsio); 10482 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10483 ctsio->be_move_done = ctl_config_move_done; 10484 ctl_datamove((union ctl_io *)ctsio); 10485 return (CTL_RETVAL_COMPLETE); 10486 } 10487 10488 int 10489 ctl_get_event_status(struct ctl_scsiio *ctsio) 10490 { 10491 struct scsi_get_event_status_header *hdr; 10492 struct scsi_get_event_status *cdb; 10493 struct ctl_lun *lun; 10494 uint32_t alloc_len, data_len; 10495 int notif_class; 10496 10497 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10498 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10499 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10500 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10501 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10502 ctl_done((union ctl_io *)ctsio); 10503 return (CTL_RETVAL_COMPLETE); 10504 } 10505 notif_class = cdb->notif_class; 10506 alloc_len = scsi_2btoul(cdb->length); 10507 10508 data_len = sizeof(struct scsi_get_event_status_header); 10509 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10510 ctsio->kern_sg_entries = 0; 10511 ctsio->kern_data_resid = 0; 10512 ctsio->kern_rel_offset = 0; 10513 10514 if (data_len < alloc_len) { 10515 ctsio->residual = alloc_len - data_len; 10516 ctsio->kern_data_len = data_len; 10517 ctsio->kern_total_len = data_len; 10518 } else { 10519 ctsio->residual = 0; 10520 ctsio->kern_data_len = alloc_len; 10521 ctsio->kern_total_len = alloc_len; 10522 } 10523 10524 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10525 scsi_ulto2b(0, hdr->descr_length); 10526 hdr->nea_class = SGESN_NEA; 10527 hdr->supported_class = 0; 10528 10529 ctl_set_success(ctsio); 10530 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10531 ctsio->be_move_done = ctl_config_move_done; 10532 ctl_datamove((union ctl_io *)ctsio); 10533 return (CTL_RETVAL_COMPLETE); 10534 } 10535 10536 int 10537 ctl_mechanism_status(struct ctl_scsiio *ctsio) 10538 { 10539 struct scsi_mechanism_status_header *hdr; 10540 struct scsi_mechanism_status *cdb; 10541 struct ctl_lun *lun; 10542 uint32_t alloc_len, data_len; 10543 10544 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10545 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10546 alloc_len = scsi_2btoul(cdb->length); 10547 10548 data_len = sizeof(struct scsi_mechanism_status_header); 10549 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10550 ctsio->kern_sg_entries = 0; 10551 ctsio->kern_data_resid = 0; 10552 ctsio->kern_rel_offset = 0; 10553 10554 if (data_len < alloc_len) { 10555 ctsio->residual = alloc_len - data_len; 10556 ctsio->kern_data_len = data_len; 10557 ctsio->kern_total_len = data_len; 10558 } else { 10559 ctsio->residual = 0; 10560 ctsio->kern_data_len = alloc_len; 10561 ctsio->kern_total_len = alloc_len; 10562 } 10563 10564 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10565 hdr->state1 = 0x00; 10566 hdr->state2 = 0xe0; 10567 scsi_ulto3b(0, hdr->lba); 10568 hdr->slots_num = 0; 10569 scsi_ulto2b(0, hdr->slots_length); 10570 10571 ctl_set_success(ctsio); 10572 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10573 ctsio->be_move_done = ctl_config_move_done; 10574 ctl_datamove((union ctl_io *)ctsio); 10575 return (CTL_RETVAL_COMPLETE); 10576 } 10577 10578 static void 10579 ctl_ultomsf(uint32_t lba, uint8_t *buf) 10580 { 10581 10582 lba += 150; 10583 buf[0] = 0; 10584 buf[1] = bin2bcd((lba / 75) / 60); 10585 buf[2] = bin2bcd((lba / 75) % 60); 10586 buf[3] = bin2bcd(lba % 75); 10587 } 10588 10589 int 10590 ctl_read_toc(struct ctl_scsiio *ctsio) 10591 { 10592 struct scsi_read_toc_hdr *hdr; 10593 struct scsi_read_toc_type01_descr *descr; 10594 struct scsi_read_toc *cdb; 10595 struct ctl_lun *lun; 10596 uint32_t alloc_len, data_len; 10597 int format, msf; 10598 10599 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10600 cdb = (struct scsi_read_toc *)ctsio->cdb; 10601 msf = (cdb->byte2 & CD_MSF) != 0; 10602 format = cdb->format; 10603 alloc_len = scsi_2btoul(cdb->data_len); 10604 10605 data_len = sizeof(struct scsi_read_toc_hdr); 10606 if (format == 0) 10607 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10608 else 10609 data_len += sizeof(struct scsi_read_toc_type01_descr); 10610 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10611 ctsio->kern_sg_entries = 0; 10612 ctsio->kern_data_resid = 0; 10613 ctsio->kern_rel_offset = 0; 10614 10615 if (data_len < alloc_len) { 10616 ctsio->residual = alloc_len - data_len; 10617 ctsio->kern_data_len = data_len; 10618 ctsio->kern_total_len = data_len; 10619 } else { 10620 ctsio->residual = 0; 10621 ctsio->kern_data_len = alloc_len; 10622 ctsio->kern_total_len = alloc_len; 10623 } 10624 10625 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10626 if (format == 0) { 10627 scsi_ulto2b(0x12, hdr->data_length); 10628 hdr->first = 1; 10629 hdr->last = 1; 10630 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10631 descr->addr_ctl = 0x14; 10632 descr->track_number = 1; 10633 if (msf) 10634 ctl_ultomsf(0, descr->track_start); 10635 else 10636 scsi_ulto4b(0, descr->track_start); 10637 descr++; 10638 descr->addr_ctl = 0x14; 10639 descr->track_number = 0xaa; 10640 if (msf) 10641 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10642 else 10643 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10644 } else { 10645 scsi_ulto2b(0x0a, hdr->data_length); 10646 hdr->first = 1; 10647 hdr->last = 1; 10648 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10649 descr->addr_ctl = 0x14; 10650 descr->track_number = 1; 10651 if (msf) 10652 ctl_ultomsf(0, descr->track_start); 10653 else 10654 scsi_ulto4b(0, descr->track_start); 10655 } 10656 10657 ctl_set_success(ctsio); 10658 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10659 ctsio->be_move_done = ctl_config_move_done; 10660 ctl_datamove((union ctl_io *)ctsio); 10661 return (CTL_RETVAL_COMPLETE); 10662 } 10663 10664 /* 10665 * For known CDB types, parse the LBA and length. 10666 */ 10667 static int 10668 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10669 { 10670 if (io->io_hdr.io_type != CTL_IO_SCSI) 10671 return (1); 10672 10673 switch (io->scsiio.cdb[0]) { 10674 case COMPARE_AND_WRITE: { 10675 struct scsi_compare_and_write *cdb; 10676 10677 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10678 10679 *lba = scsi_8btou64(cdb->addr); 10680 *len = cdb->length; 10681 break; 10682 } 10683 case READ_6: 10684 case WRITE_6: { 10685 struct scsi_rw_6 *cdb; 10686 10687 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10688 10689 *lba = scsi_3btoul(cdb->addr); 10690 /* only 5 bits are valid in the most significant address byte */ 10691 *lba &= 0x1fffff; 10692 *len = cdb->length; 10693 break; 10694 } 10695 case READ_10: 10696 case WRITE_10: { 10697 struct scsi_rw_10 *cdb; 10698 10699 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10700 10701 *lba = scsi_4btoul(cdb->addr); 10702 *len = scsi_2btoul(cdb->length); 10703 break; 10704 } 10705 case WRITE_VERIFY_10: { 10706 struct scsi_write_verify_10 *cdb; 10707 10708 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10709 10710 *lba = scsi_4btoul(cdb->addr); 10711 *len = scsi_2btoul(cdb->length); 10712 break; 10713 } 10714 case READ_12: 10715 case WRITE_12: { 10716 struct scsi_rw_12 *cdb; 10717 10718 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10719 10720 *lba = scsi_4btoul(cdb->addr); 10721 *len = scsi_4btoul(cdb->length); 10722 break; 10723 } 10724 case WRITE_VERIFY_12: { 10725 struct scsi_write_verify_12 *cdb; 10726 10727 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10728 10729 *lba = scsi_4btoul(cdb->addr); 10730 *len = scsi_4btoul(cdb->length); 10731 break; 10732 } 10733 case READ_16: 10734 case WRITE_16: { 10735 struct scsi_rw_16 *cdb; 10736 10737 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10738 10739 *lba = scsi_8btou64(cdb->addr); 10740 *len = scsi_4btoul(cdb->length); 10741 break; 10742 } 10743 case WRITE_ATOMIC_16: { 10744 struct scsi_write_atomic_16 *cdb; 10745 10746 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10747 10748 *lba = scsi_8btou64(cdb->addr); 10749 *len = scsi_2btoul(cdb->length); 10750 break; 10751 } 10752 case WRITE_VERIFY_16: { 10753 struct scsi_write_verify_16 *cdb; 10754 10755 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10756 10757 *lba = scsi_8btou64(cdb->addr); 10758 *len = scsi_4btoul(cdb->length); 10759 break; 10760 } 10761 case WRITE_SAME_10: { 10762 struct scsi_write_same_10 *cdb; 10763 10764 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10765 10766 *lba = scsi_4btoul(cdb->addr); 10767 *len = scsi_2btoul(cdb->length); 10768 break; 10769 } 10770 case WRITE_SAME_16: { 10771 struct scsi_write_same_16 *cdb; 10772 10773 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10774 10775 *lba = scsi_8btou64(cdb->addr); 10776 *len = scsi_4btoul(cdb->length); 10777 break; 10778 } 10779 case VERIFY_10: { 10780 struct scsi_verify_10 *cdb; 10781 10782 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10783 10784 *lba = scsi_4btoul(cdb->addr); 10785 *len = scsi_2btoul(cdb->length); 10786 break; 10787 } 10788 case VERIFY_12: { 10789 struct scsi_verify_12 *cdb; 10790 10791 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10792 10793 *lba = scsi_4btoul(cdb->addr); 10794 *len = scsi_4btoul(cdb->length); 10795 break; 10796 } 10797 case VERIFY_16: { 10798 struct scsi_verify_16 *cdb; 10799 10800 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10801 10802 *lba = scsi_8btou64(cdb->addr); 10803 *len = scsi_4btoul(cdb->length); 10804 break; 10805 } 10806 case UNMAP: { 10807 *lba = 0; 10808 *len = UINT64_MAX; 10809 break; 10810 } 10811 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10812 struct scsi_get_lba_status *cdb; 10813 10814 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10815 *lba = scsi_8btou64(cdb->addr); 10816 *len = UINT32_MAX; 10817 break; 10818 } 10819 default: 10820 return (1); 10821 break; /* NOTREACHED */ 10822 } 10823 10824 return (0); 10825 } 10826 10827 static ctl_action 10828 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10829 bool seq) 10830 { 10831 uint64_t endlba1, endlba2; 10832 10833 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10834 endlba2 = lba2 + len2 - 1; 10835 10836 if ((endlba1 < lba2) || (endlba2 < lba1)) 10837 return (CTL_ACTION_PASS); 10838 else 10839 return (CTL_ACTION_BLOCK); 10840 } 10841 10842 static int 10843 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10844 { 10845 struct ctl_ptr_len_flags *ptrlen; 10846 struct scsi_unmap_desc *buf, *end, *range; 10847 uint64_t lba; 10848 uint32_t len; 10849 10850 /* If not UNMAP -- go other way. */ 10851 if (io->io_hdr.io_type != CTL_IO_SCSI || 10852 io->scsiio.cdb[0] != UNMAP) 10853 return (CTL_ACTION_ERROR); 10854 10855 /* If UNMAP without data -- block and wait for data. */ 10856 ptrlen = (struct ctl_ptr_len_flags *) 10857 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10858 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10859 ptrlen->ptr == NULL) 10860 return (CTL_ACTION_BLOCK); 10861 10862 /* UNMAP with data -- check for collision. */ 10863 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10864 end = buf + ptrlen->len / sizeof(*buf); 10865 for (range = buf; range < end; range++) { 10866 lba = scsi_8btou64(range->lba); 10867 len = scsi_4btoul(range->length); 10868 if ((lba < lba2 + len2) && (lba + len > lba2)) 10869 return (CTL_ACTION_BLOCK); 10870 } 10871 return (CTL_ACTION_PASS); 10872 } 10873 10874 static ctl_action 10875 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10876 { 10877 uint64_t lba1, lba2; 10878 uint64_t len1, len2; 10879 int retval; 10880 10881 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10882 return (CTL_ACTION_ERROR); 10883 10884 retval = ctl_extent_check_unmap(io1, lba2, len2); 10885 if (retval != CTL_ACTION_ERROR) 10886 return (retval); 10887 10888 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10889 return (CTL_ACTION_ERROR); 10890 10891 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10892 seq = FALSE; 10893 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10894 } 10895 10896 static ctl_action 10897 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10898 { 10899 uint64_t lba1, lba2; 10900 uint64_t len1, len2; 10901 10902 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10903 return (CTL_ACTION_PASS); 10904 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10905 return (CTL_ACTION_ERROR); 10906 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10907 return (CTL_ACTION_ERROR); 10908 10909 if (lba1 + len1 == lba2) 10910 return (CTL_ACTION_BLOCK); 10911 return (CTL_ACTION_PASS); 10912 } 10913 10914 static ctl_action 10915 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10916 union ctl_io *ooa_io) 10917 { 10918 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10919 const ctl_serialize_action *serialize_row; 10920 10921 /* 10922 * The initiator attempted multiple untagged commands at the same 10923 * time. Can't do that. 10924 */ 10925 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10926 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10927 && ((pending_io->io_hdr.nexus.targ_port == 10928 ooa_io->io_hdr.nexus.targ_port) 10929 && (pending_io->io_hdr.nexus.initid == 10930 ooa_io->io_hdr.nexus.initid)) 10931 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10932 CTL_FLAG_STATUS_SENT)) == 0)) 10933 return (CTL_ACTION_OVERLAP); 10934 10935 /* 10936 * The initiator attempted to send multiple tagged commands with 10937 * the same ID. (It's fine if different initiators have the same 10938 * tag ID.) 10939 * 10940 * Even if all of those conditions are true, we don't kill the I/O 10941 * if the command ahead of us has been aborted. We won't end up 10942 * sending it to the FETD, and it's perfectly legal to resend a 10943 * command with the same tag number as long as the previous 10944 * instance of this tag number has been aborted somehow. 10945 */ 10946 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10947 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10948 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10949 && ((pending_io->io_hdr.nexus.targ_port == 10950 ooa_io->io_hdr.nexus.targ_port) 10951 && (pending_io->io_hdr.nexus.initid == 10952 ooa_io->io_hdr.nexus.initid)) 10953 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10954 CTL_FLAG_STATUS_SENT)) == 0)) 10955 return (CTL_ACTION_OVERLAP_TAG); 10956 10957 /* 10958 * If we get a head of queue tag, SAM-3 says that we should 10959 * immediately execute it. 10960 * 10961 * What happens if this command would normally block for some other 10962 * reason? e.g. a request sense with a head of queue tag 10963 * immediately after a write. Normally that would block, but this 10964 * will result in its getting executed immediately... 10965 * 10966 * We currently return "pass" instead of "skip", so we'll end up 10967 * going through the rest of the queue to check for overlapped tags. 10968 * 10969 * XXX KDM check for other types of blockage first?? 10970 */ 10971 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10972 return (CTL_ACTION_PASS); 10973 10974 /* 10975 * Ordered tags have to block until all items ahead of them 10976 * have completed. If we get called with an ordered tag, we always 10977 * block, if something else is ahead of us in the queue. 10978 */ 10979 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10980 return (CTL_ACTION_BLOCK); 10981 10982 /* 10983 * Simple tags get blocked until all head of queue and ordered tags 10984 * ahead of them have completed. I'm lumping untagged commands in 10985 * with simple tags here. XXX KDM is that the right thing to do? 10986 */ 10987 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10988 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10989 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10990 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10991 return (CTL_ACTION_BLOCK); 10992 10993 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10994 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10995 10996 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10997 10998 switch (serialize_row[pending_entry->seridx]) { 10999 case CTL_SER_BLOCK: 11000 return (CTL_ACTION_BLOCK); 11001 case CTL_SER_EXTENT: 11002 return (ctl_extent_check(ooa_io, pending_io, 11003 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11004 case CTL_SER_EXTENTOPT: 11005 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11006 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11007 return (ctl_extent_check(ooa_io, pending_io, 11008 (lun->be_lun && 11009 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11010 return (CTL_ACTION_PASS); 11011 case CTL_SER_EXTENTSEQ: 11012 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11013 return (ctl_extent_check_seq(ooa_io, pending_io)); 11014 return (CTL_ACTION_PASS); 11015 case CTL_SER_PASS: 11016 return (CTL_ACTION_PASS); 11017 case CTL_SER_BLOCKOPT: 11018 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11019 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11020 return (CTL_ACTION_BLOCK); 11021 return (CTL_ACTION_PASS); 11022 case CTL_SER_SKIP: 11023 return (CTL_ACTION_SKIP); 11024 default: 11025 panic("invalid serialization value %d", 11026 serialize_row[pending_entry->seridx]); 11027 } 11028 11029 return (CTL_ACTION_ERROR); 11030 } 11031 11032 /* 11033 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11034 * Assumptions: 11035 * - pending_io is generally either incoming, or on the blocked queue 11036 * - starting I/O is the I/O we want to start the check with. 11037 */ 11038 static ctl_action 11039 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11040 union ctl_io *starting_io) 11041 { 11042 union ctl_io *ooa_io; 11043 ctl_action action; 11044 11045 mtx_assert(&lun->lun_lock, MA_OWNED); 11046 11047 /* 11048 * Run back along the OOA queue, starting with the current 11049 * blocked I/O and going through every I/O before it on the 11050 * queue. If starting_io is NULL, we'll just end up returning 11051 * CTL_ACTION_PASS. 11052 */ 11053 for (ooa_io = starting_io; ooa_io != NULL; 11054 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11055 ooa_links)){ 11056 11057 /* 11058 * This routine just checks to see whether 11059 * cur_blocked is blocked by ooa_io, which is ahead 11060 * of it in the queue. It doesn't queue/dequeue 11061 * cur_blocked. 11062 */ 11063 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11064 switch (action) { 11065 case CTL_ACTION_BLOCK: 11066 case CTL_ACTION_OVERLAP: 11067 case CTL_ACTION_OVERLAP_TAG: 11068 case CTL_ACTION_SKIP: 11069 case CTL_ACTION_ERROR: 11070 return (action); 11071 break; /* NOTREACHED */ 11072 case CTL_ACTION_PASS: 11073 break; 11074 default: 11075 panic("invalid action %d", action); 11076 break; /* NOTREACHED */ 11077 } 11078 } 11079 11080 return (CTL_ACTION_PASS); 11081 } 11082 11083 /* 11084 * Assumptions: 11085 * - An I/O has just completed, and has been removed from the per-LUN OOA 11086 * queue, so some items on the blocked queue may now be unblocked. 11087 */ 11088 static int 11089 ctl_check_blocked(struct ctl_lun *lun) 11090 { 11091 struct ctl_softc *softc = lun->ctl_softc; 11092 union ctl_io *cur_blocked, *next_blocked; 11093 11094 mtx_assert(&lun->lun_lock, MA_OWNED); 11095 11096 /* 11097 * Run forward from the head of the blocked queue, checking each 11098 * entry against the I/Os prior to it on the OOA queue to see if 11099 * there is still any blockage. 11100 * 11101 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11102 * with our removing a variable on it while it is traversing the 11103 * list. 11104 */ 11105 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11106 cur_blocked != NULL; cur_blocked = next_blocked) { 11107 union ctl_io *prev_ooa; 11108 ctl_action action; 11109 11110 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11111 blocked_links); 11112 11113 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11114 ctl_ooaq, ooa_links); 11115 11116 /* 11117 * If cur_blocked happens to be the first item in the OOA 11118 * queue now, prev_ooa will be NULL, and the action 11119 * returned will just be CTL_ACTION_PASS. 11120 */ 11121 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11122 11123 switch (action) { 11124 case CTL_ACTION_BLOCK: 11125 /* Nothing to do here, still blocked */ 11126 break; 11127 case CTL_ACTION_OVERLAP: 11128 case CTL_ACTION_OVERLAP_TAG: 11129 /* 11130 * This shouldn't happen! In theory we've already 11131 * checked this command for overlap... 11132 */ 11133 break; 11134 case CTL_ACTION_PASS: 11135 case CTL_ACTION_SKIP: { 11136 const struct ctl_cmd_entry *entry; 11137 11138 /* 11139 * The skip case shouldn't happen, this transaction 11140 * should have never made it onto the blocked queue. 11141 */ 11142 /* 11143 * This I/O is no longer blocked, we can remove it 11144 * from the blocked queue. Since this is a TAILQ 11145 * (doubly linked list), we can do O(1) removals 11146 * from any place on the list. 11147 */ 11148 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11149 blocked_links); 11150 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11151 11152 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11153 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11154 /* 11155 * Need to send IO back to original side to 11156 * run 11157 */ 11158 union ctl_ha_msg msg_info; 11159 11160 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11161 msg_info.hdr.original_sc = 11162 cur_blocked->io_hdr.original_sc; 11163 msg_info.hdr.serializing_sc = cur_blocked; 11164 msg_info.hdr.msg_type = CTL_MSG_R2R; 11165 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11166 sizeof(msg_info.hdr), M_NOWAIT); 11167 break; 11168 } 11169 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11170 11171 /* 11172 * Check this I/O for LUN state changes that may 11173 * have happened while this command was blocked. 11174 * The LUN state may have been changed by a command 11175 * ahead of us in the queue, so we need to re-check 11176 * for any states that can be caused by SCSI 11177 * commands. 11178 */ 11179 if (ctl_scsiio_lun_check(lun, entry, 11180 &cur_blocked->scsiio) == 0) { 11181 cur_blocked->io_hdr.flags |= 11182 CTL_FLAG_IS_WAS_ON_RTR; 11183 ctl_enqueue_rtr(cur_blocked); 11184 } else 11185 ctl_done(cur_blocked); 11186 break; 11187 } 11188 default: 11189 /* 11190 * This probably shouldn't happen -- we shouldn't 11191 * get CTL_ACTION_ERROR, or anything else. 11192 */ 11193 break; 11194 } 11195 } 11196 11197 return (CTL_RETVAL_COMPLETE); 11198 } 11199 11200 /* 11201 * This routine (with one exception) checks LUN flags that can be set by 11202 * commands ahead of us in the OOA queue. These flags have to be checked 11203 * when a command initially comes in, and when we pull a command off the 11204 * blocked queue and are preparing to execute it. The reason we have to 11205 * check these flags for commands on the blocked queue is that the LUN 11206 * state may have been changed by a command ahead of us while we're on the 11207 * blocked queue. 11208 * 11209 * Ordering is somewhat important with these checks, so please pay 11210 * careful attention to the placement of any new checks. 11211 */ 11212 static int 11213 ctl_scsiio_lun_check(struct ctl_lun *lun, 11214 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11215 { 11216 struct ctl_softc *softc = lun->ctl_softc; 11217 int retval; 11218 uint32_t residx; 11219 11220 retval = 0; 11221 11222 mtx_assert(&lun->lun_lock, MA_OWNED); 11223 11224 /* 11225 * If this shelf is a secondary shelf controller, we may have to 11226 * reject some commands disallowed by HA mode and link state. 11227 */ 11228 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11229 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11230 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11231 ctl_set_lun_unavail(ctsio); 11232 retval = 1; 11233 goto bailout; 11234 } 11235 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11236 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11237 ctl_set_lun_transit(ctsio); 11238 retval = 1; 11239 goto bailout; 11240 } 11241 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11242 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11243 ctl_set_lun_standby(ctsio); 11244 retval = 1; 11245 goto bailout; 11246 } 11247 11248 /* The rest of checks are only done on executing side */ 11249 if (softc->ha_mode == CTL_HA_MODE_XFER) 11250 goto bailout; 11251 } 11252 11253 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11254 if (lun->be_lun && 11255 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11256 ctl_set_hw_write_protected(ctsio); 11257 retval = 1; 11258 goto bailout; 11259 } 11260 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11261 .eca_and_aen & SCP_SWP) != 0) { 11262 ctl_set_sense(ctsio, /*current_error*/ 1, 11263 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11264 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11265 retval = 1; 11266 goto bailout; 11267 } 11268 } 11269 11270 /* 11271 * Check for a reservation conflict. If this command isn't allowed 11272 * even on reserved LUNs, and if this initiator isn't the one who 11273 * reserved us, reject the command with a reservation conflict. 11274 */ 11275 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11276 if ((lun->flags & CTL_LUN_RESERVED) 11277 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11278 if (lun->res_idx != residx) { 11279 ctl_set_reservation_conflict(ctsio); 11280 retval = 1; 11281 goto bailout; 11282 } 11283 } 11284 11285 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11286 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11287 /* No reservation or command is allowed. */; 11288 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11289 (lun->res_type == SPR_TYPE_WR_EX || 11290 lun->res_type == SPR_TYPE_WR_EX_RO || 11291 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11292 /* The command is allowed for Write Exclusive resv. */; 11293 } else { 11294 /* 11295 * if we aren't registered or it's a res holder type 11296 * reservation and this isn't the res holder then set a 11297 * conflict. 11298 */ 11299 if (ctl_get_prkey(lun, residx) == 0 11300 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11301 ctl_set_reservation_conflict(ctsio); 11302 retval = 1; 11303 goto bailout; 11304 } 11305 } 11306 11307 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11308 if (lun->flags & CTL_LUN_EJECTED) 11309 ctl_set_lun_ejected(ctsio); 11310 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11311 if (lun->flags & CTL_LUN_REMOVABLE) 11312 ctl_set_lun_no_media(ctsio); 11313 else 11314 ctl_set_lun_int_reqd(ctsio); 11315 } else if (lun->flags & CTL_LUN_STOPPED) 11316 ctl_set_lun_stopped(ctsio); 11317 else 11318 goto bailout; 11319 retval = 1; 11320 goto bailout; 11321 } 11322 11323 bailout: 11324 return (retval); 11325 } 11326 11327 static void 11328 ctl_failover_io(union ctl_io *io, int have_lock) 11329 { 11330 ctl_set_busy(&io->scsiio); 11331 ctl_done(io); 11332 } 11333 11334 static void 11335 ctl_failover_lun(union ctl_io *rio) 11336 { 11337 struct ctl_softc *softc = control_softc; 11338 struct ctl_lun *lun; 11339 struct ctl_io_hdr *io, *next_io; 11340 uint32_t targ_lun; 11341 11342 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11343 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun)); 11344 11345 /* Find and lock the LUN. */ 11346 mtx_lock(&softc->ctl_lock); 11347 if ((targ_lun < CTL_MAX_LUNS) && 11348 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11349 mtx_lock(&lun->lun_lock); 11350 mtx_unlock(&softc->ctl_lock); 11351 if (lun->flags & CTL_LUN_DISABLED) { 11352 mtx_unlock(&lun->lun_lock); 11353 return; 11354 } 11355 } else { 11356 mtx_unlock(&softc->ctl_lock); 11357 return; 11358 } 11359 11360 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11361 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11362 /* We are master */ 11363 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11364 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11365 io->flags |= CTL_FLAG_ABORT; 11366 io->flags |= CTL_FLAG_FAILOVER; 11367 } else { /* This can be only due to DATAMOVE */ 11368 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11369 io->flags &= ~CTL_FLAG_DMA_INPROG; 11370 io->flags |= CTL_FLAG_IO_ACTIVE; 11371 io->port_status = 31340; 11372 ctl_enqueue_isc((union ctl_io *)io); 11373 } 11374 } 11375 /* We are slave */ 11376 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11377 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11378 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11379 io->flags |= CTL_FLAG_FAILOVER; 11380 } else { 11381 ctl_set_busy(&((union ctl_io *)io)-> 11382 scsiio); 11383 ctl_done((union ctl_io *)io); 11384 } 11385 } 11386 } 11387 } else { /* SERIALIZE modes */ 11388 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11389 next_io) { 11390 /* We are master */ 11391 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11392 TAILQ_REMOVE(&lun->blocked_queue, io, 11393 blocked_links); 11394 io->flags &= ~CTL_FLAG_BLOCKED; 11395 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11396 ctl_free_io((union ctl_io *)io); 11397 } 11398 } 11399 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11400 /* We are master */ 11401 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11402 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11403 ctl_free_io((union ctl_io *)io); 11404 } 11405 /* We are slave */ 11406 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11407 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11408 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11409 ctl_set_busy(&((union ctl_io *)io)-> 11410 scsiio); 11411 ctl_done((union ctl_io *)io); 11412 } 11413 } 11414 } 11415 ctl_check_blocked(lun); 11416 } 11417 mtx_unlock(&lun->lun_lock); 11418 } 11419 11420 static int 11421 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11422 { 11423 struct ctl_lun *lun; 11424 const struct ctl_cmd_entry *entry; 11425 uint32_t initidx, targ_lun; 11426 int retval; 11427 11428 retval = 0; 11429 11430 lun = NULL; 11431 11432 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11433 if ((targ_lun < CTL_MAX_LUNS) 11434 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11435 /* 11436 * If the LUN is invalid, pretend that it doesn't exist. 11437 * It will go away as soon as all pending I/O has been 11438 * completed. 11439 */ 11440 mtx_lock(&lun->lun_lock); 11441 if (lun->flags & CTL_LUN_DISABLED) { 11442 mtx_unlock(&lun->lun_lock); 11443 lun = NULL; 11444 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11445 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11446 } else { 11447 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11448 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11449 lun->be_lun; 11450 11451 /* 11452 * Every I/O goes into the OOA queue for a 11453 * particular LUN, and stays there until completion. 11454 */ 11455 #ifdef CTL_TIME_IO 11456 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11457 lun->idle_time += getsbinuptime() - 11458 lun->last_busy; 11459 } 11460 #endif 11461 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11462 ooa_links); 11463 } 11464 } else { 11465 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11466 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11467 } 11468 11469 /* Get command entry and return error if it is unsuppotyed. */ 11470 entry = ctl_validate_command(ctsio); 11471 if (entry == NULL) { 11472 if (lun) 11473 mtx_unlock(&lun->lun_lock); 11474 return (retval); 11475 } 11476 11477 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11478 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11479 11480 /* 11481 * Check to see whether we can send this command to LUNs that don't 11482 * exist. This should pretty much only be the case for inquiry 11483 * and request sense. Further checks, below, really require having 11484 * a LUN, so we can't really check the command anymore. Just put 11485 * it on the rtr queue. 11486 */ 11487 if (lun == NULL) { 11488 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11489 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11490 ctl_enqueue_rtr((union ctl_io *)ctsio); 11491 return (retval); 11492 } 11493 11494 ctl_set_unsupported_lun(ctsio); 11495 ctl_done((union ctl_io *)ctsio); 11496 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11497 return (retval); 11498 } else { 11499 /* 11500 * Make sure we support this particular command on this LUN. 11501 * e.g., we don't support writes to the control LUN. 11502 */ 11503 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11504 mtx_unlock(&lun->lun_lock); 11505 ctl_set_invalid_opcode(ctsio); 11506 ctl_done((union ctl_io *)ctsio); 11507 return (retval); 11508 } 11509 } 11510 11511 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11512 11513 #ifdef CTL_WITH_CA 11514 /* 11515 * If we've got a request sense, it'll clear the contingent 11516 * allegiance condition. Otherwise, if we have a CA condition for 11517 * this initiator, clear it, because it sent down a command other 11518 * than request sense. 11519 */ 11520 if ((ctsio->cdb[0] != REQUEST_SENSE) 11521 && (ctl_is_set(lun->have_ca, initidx))) 11522 ctl_clear_mask(lun->have_ca, initidx); 11523 #endif 11524 11525 /* 11526 * If the command has this flag set, it handles its own unit 11527 * attention reporting, we shouldn't do anything. Otherwise we 11528 * check for any pending unit attentions, and send them back to the 11529 * initiator. We only do this when a command initially comes in, 11530 * not when we pull it off the blocked queue. 11531 * 11532 * According to SAM-3, section 5.3.2, the order that things get 11533 * presented back to the host is basically unit attentions caused 11534 * by some sort of reset event, busy status, reservation conflicts 11535 * or task set full, and finally any other status. 11536 * 11537 * One issue here is that some of the unit attentions we report 11538 * don't fall into the "reset" category (e.g. "reported luns data 11539 * has changed"). So reporting it here, before the reservation 11540 * check, may be technically wrong. I guess the only thing to do 11541 * would be to check for and report the reset events here, and then 11542 * check for the other unit attention types after we check for a 11543 * reservation conflict. 11544 * 11545 * XXX KDM need to fix this 11546 */ 11547 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11548 ctl_ua_type ua_type; 11549 11550 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11551 SSD_TYPE_NONE); 11552 if (ua_type != CTL_UA_NONE) { 11553 mtx_unlock(&lun->lun_lock); 11554 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11555 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11556 ctsio->sense_len = SSD_FULL_SIZE; 11557 ctl_done((union ctl_io *)ctsio); 11558 return (retval); 11559 } 11560 } 11561 11562 11563 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11564 mtx_unlock(&lun->lun_lock); 11565 ctl_done((union ctl_io *)ctsio); 11566 return (retval); 11567 } 11568 11569 /* 11570 * XXX CHD this is where we want to send IO to other side if 11571 * this LUN is secondary on this SC. We will need to make a copy 11572 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11573 * the copy we send as FROM_OTHER. 11574 * We also need to stuff the address of the original IO so we can 11575 * find it easily. Something similar will need be done on the other 11576 * side so when we are done we can find the copy. 11577 */ 11578 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11579 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11580 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11581 union ctl_ha_msg msg_info; 11582 int isc_retval; 11583 11584 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11585 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11586 mtx_unlock(&lun->lun_lock); 11587 11588 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11589 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11590 msg_info.hdr.serializing_sc = NULL; 11591 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11592 msg_info.scsi.tag_num = ctsio->tag_num; 11593 msg_info.scsi.tag_type = ctsio->tag_type; 11594 msg_info.scsi.cdb_len = ctsio->cdb_len; 11595 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11596 11597 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11598 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11599 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11600 ctl_set_busy(ctsio); 11601 ctl_done((union ctl_io *)ctsio); 11602 return (retval); 11603 } 11604 return (retval); 11605 } 11606 11607 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11608 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11609 ctl_ooaq, ooa_links))) { 11610 case CTL_ACTION_BLOCK: 11611 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11612 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11613 blocked_links); 11614 mtx_unlock(&lun->lun_lock); 11615 return (retval); 11616 case CTL_ACTION_PASS: 11617 case CTL_ACTION_SKIP: 11618 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11619 mtx_unlock(&lun->lun_lock); 11620 ctl_enqueue_rtr((union ctl_io *)ctsio); 11621 break; 11622 case CTL_ACTION_OVERLAP: 11623 mtx_unlock(&lun->lun_lock); 11624 ctl_set_overlapped_cmd(ctsio); 11625 ctl_done((union ctl_io *)ctsio); 11626 break; 11627 case CTL_ACTION_OVERLAP_TAG: 11628 mtx_unlock(&lun->lun_lock); 11629 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11630 ctl_done((union ctl_io *)ctsio); 11631 break; 11632 case CTL_ACTION_ERROR: 11633 default: 11634 mtx_unlock(&lun->lun_lock); 11635 ctl_set_internal_failure(ctsio, 11636 /*sks_valid*/ 0, 11637 /*retry_count*/ 0); 11638 ctl_done((union ctl_io *)ctsio); 11639 break; 11640 } 11641 return (retval); 11642 } 11643 11644 const struct ctl_cmd_entry * 11645 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11646 { 11647 const struct ctl_cmd_entry *entry; 11648 int service_action; 11649 11650 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11651 if (sa) 11652 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11653 if (entry->flags & CTL_CMD_FLAG_SA5) { 11654 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11655 entry = &((const struct ctl_cmd_entry *) 11656 entry->execute)[service_action]; 11657 } 11658 return (entry); 11659 } 11660 11661 const struct ctl_cmd_entry * 11662 ctl_validate_command(struct ctl_scsiio *ctsio) 11663 { 11664 const struct ctl_cmd_entry *entry; 11665 int i, sa; 11666 uint8_t diff; 11667 11668 entry = ctl_get_cmd_entry(ctsio, &sa); 11669 if (entry->execute == NULL) { 11670 if (sa) 11671 ctl_set_invalid_field(ctsio, 11672 /*sks_valid*/ 1, 11673 /*command*/ 1, 11674 /*field*/ 1, 11675 /*bit_valid*/ 1, 11676 /*bit*/ 4); 11677 else 11678 ctl_set_invalid_opcode(ctsio); 11679 ctl_done((union ctl_io *)ctsio); 11680 return (NULL); 11681 } 11682 KASSERT(entry->length > 0, 11683 ("Not defined length for command 0x%02x/0x%02x", 11684 ctsio->cdb[0], ctsio->cdb[1])); 11685 for (i = 1; i < entry->length; i++) { 11686 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11687 if (diff == 0) 11688 continue; 11689 ctl_set_invalid_field(ctsio, 11690 /*sks_valid*/ 1, 11691 /*command*/ 1, 11692 /*field*/ i, 11693 /*bit_valid*/ 1, 11694 /*bit*/ fls(diff) - 1); 11695 ctl_done((union ctl_io *)ctsio); 11696 return (NULL); 11697 } 11698 return (entry); 11699 } 11700 11701 static int 11702 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11703 { 11704 11705 switch (lun_type) { 11706 case T_DIRECT: 11707 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11708 return (0); 11709 break; 11710 case T_PROCESSOR: 11711 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11712 return (0); 11713 break; 11714 case T_CDROM: 11715 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11716 return (0); 11717 break; 11718 default: 11719 return (0); 11720 } 11721 return (1); 11722 } 11723 11724 static int 11725 ctl_scsiio(struct ctl_scsiio *ctsio) 11726 { 11727 int retval; 11728 const struct ctl_cmd_entry *entry; 11729 11730 retval = CTL_RETVAL_COMPLETE; 11731 11732 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11733 11734 entry = ctl_get_cmd_entry(ctsio, NULL); 11735 11736 /* 11737 * If this I/O has been aborted, just send it straight to 11738 * ctl_done() without executing it. 11739 */ 11740 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11741 ctl_done((union ctl_io *)ctsio); 11742 goto bailout; 11743 } 11744 11745 /* 11746 * All the checks should have been handled by ctl_scsiio_precheck(). 11747 * We should be clear now to just execute the I/O. 11748 */ 11749 retval = entry->execute(ctsio); 11750 11751 bailout: 11752 return (retval); 11753 } 11754 11755 /* 11756 * Since we only implement one target right now, a bus reset simply resets 11757 * our single target. 11758 */ 11759 static int 11760 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11761 { 11762 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11763 } 11764 11765 static int 11766 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11767 ctl_ua_type ua_type) 11768 { 11769 struct ctl_port *port; 11770 struct ctl_lun *lun; 11771 int retval; 11772 11773 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11774 union ctl_ha_msg msg_info; 11775 11776 msg_info.hdr.nexus = io->io_hdr.nexus; 11777 if (ua_type==CTL_UA_TARG_RESET) 11778 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11779 else 11780 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11781 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11782 msg_info.hdr.original_sc = NULL; 11783 msg_info.hdr.serializing_sc = NULL; 11784 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11785 sizeof(msg_info.task), M_WAITOK); 11786 } 11787 retval = 0; 11788 11789 mtx_lock(&softc->ctl_lock); 11790 port = ctl_io_port(&io->io_hdr); 11791 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11792 if (port != NULL && 11793 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 11794 continue; 11795 retval += ctl_do_lun_reset(lun, io, ua_type); 11796 } 11797 mtx_unlock(&softc->ctl_lock); 11798 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11799 return (retval); 11800 } 11801 11802 /* 11803 * The LUN should always be set. The I/O is optional, and is used to 11804 * distinguish between I/Os sent by this initiator, and by other 11805 * initiators. We set unit attention for initiators other than this one. 11806 * SAM-3 is vague on this point. It does say that a unit attention should 11807 * be established for other initiators when a LUN is reset (see section 11808 * 5.7.3), but it doesn't specifically say that the unit attention should 11809 * be established for this particular initiator when a LUN is reset. Here 11810 * is the relevant text, from SAM-3 rev 8: 11811 * 11812 * 5.7.2 When a SCSI initiator port aborts its own tasks 11813 * 11814 * When a SCSI initiator port causes its own task(s) to be aborted, no 11815 * notification that the task(s) have been aborted shall be returned to 11816 * the SCSI initiator port other than the completion response for the 11817 * command or task management function action that caused the task(s) to 11818 * be aborted and notification(s) associated with related effects of the 11819 * action (e.g., a reset unit attention condition). 11820 * 11821 * XXX KDM for now, we're setting unit attention for all initiators. 11822 */ 11823 static int 11824 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11825 { 11826 union ctl_io *xio; 11827 #if 0 11828 uint32_t initidx; 11829 #endif 11830 int i; 11831 11832 mtx_lock(&lun->lun_lock); 11833 /* 11834 * Run through the OOA queue and abort each I/O. 11835 */ 11836 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11837 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11838 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11839 } 11840 11841 /* 11842 * This version sets unit attention for every 11843 */ 11844 #if 0 11845 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11846 ctl_est_ua_all(lun, initidx, ua_type); 11847 #else 11848 ctl_est_ua_all(lun, -1, ua_type); 11849 #endif 11850 11851 /* 11852 * A reset (any kind, really) clears reservations established with 11853 * RESERVE/RELEASE. It does not clear reservations established 11854 * with PERSISTENT RESERVE OUT, but we don't support that at the 11855 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11856 * reservations made with the RESERVE/RELEASE commands, because 11857 * those commands are obsolete in SPC-3. 11858 */ 11859 lun->flags &= ~CTL_LUN_RESERVED; 11860 11861 #ifdef CTL_WITH_CA 11862 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11863 ctl_clear_mask(lun->have_ca, i); 11864 #endif 11865 lun->prevent_count = 0; 11866 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11867 ctl_clear_mask(lun->prevent, i); 11868 mtx_unlock(&lun->lun_lock); 11869 11870 return (0); 11871 } 11872 11873 static int 11874 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11875 { 11876 struct ctl_lun *lun; 11877 uint32_t targ_lun; 11878 int retval; 11879 11880 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11881 mtx_lock(&softc->ctl_lock); 11882 if ((targ_lun >= CTL_MAX_LUNS) || 11883 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11884 mtx_unlock(&softc->ctl_lock); 11885 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11886 return (1); 11887 } 11888 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 11889 mtx_unlock(&softc->ctl_lock); 11890 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11891 11892 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11893 union ctl_ha_msg msg_info; 11894 11895 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11896 msg_info.hdr.nexus = io->io_hdr.nexus; 11897 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11898 msg_info.hdr.original_sc = NULL; 11899 msg_info.hdr.serializing_sc = NULL; 11900 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11901 sizeof(msg_info.task), M_WAITOK); 11902 } 11903 return (retval); 11904 } 11905 11906 static void 11907 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11908 int other_sc) 11909 { 11910 union ctl_io *xio; 11911 11912 mtx_assert(&lun->lun_lock, MA_OWNED); 11913 11914 /* 11915 * Run through the OOA queue and attempt to find the given I/O. 11916 * The target port, initiator ID, tag type and tag number have to 11917 * match the values that we got from the initiator. If we have an 11918 * untagged command to abort, simply abort the first untagged command 11919 * we come to. We only allow one untagged command at a time of course. 11920 */ 11921 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11922 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11923 11924 if ((targ_port == UINT32_MAX || 11925 targ_port == xio->io_hdr.nexus.targ_port) && 11926 (init_id == UINT32_MAX || 11927 init_id == xio->io_hdr.nexus.initid)) { 11928 if (targ_port != xio->io_hdr.nexus.targ_port || 11929 init_id != xio->io_hdr.nexus.initid) 11930 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11931 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11932 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11933 union ctl_ha_msg msg_info; 11934 11935 msg_info.hdr.nexus = xio->io_hdr.nexus; 11936 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11937 msg_info.task.tag_num = xio->scsiio.tag_num; 11938 msg_info.task.tag_type = xio->scsiio.tag_type; 11939 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11940 msg_info.hdr.original_sc = NULL; 11941 msg_info.hdr.serializing_sc = NULL; 11942 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11943 sizeof(msg_info.task), M_NOWAIT); 11944 } 11945 } 11946 } 11947 } 11948 11949 static int 11950 ctl_abort_task_set(union ctl_io *io) 11951 { 11952 struct ctl_softc *softc = control_softc; 11953 struct ctl_lun *lun; 11954 uint32_t targ_lun; 11955 11956 /* 11957 * Look up the LUN. 11958 */ 11959 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11960 mtx_lock(&softc->ctl_lock); 11961 if ((targ_lun >= CTL_MAX_LUNS) || 11962 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11963 mtx_unlock(&softc->ctl_lock); 11964 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11965 return (1); 11966 } 11967 11968 mtx_lock(&lun->lun_lock); 11969 mtx_unlock(&softc->ctl_lock); 11970 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11971 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11972 io->io_hdr.nexus.initid, 11973 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11974 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11975 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11976 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11977 } 11978 mtx_unlock(&lun->lun_lock); 11979 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11980 return (0); 11981 } 11982 11983 static int 11984 ctl_i_t_nexus_reset(union ctl_io *io) 11985 { 11986 struct ctl_softc *softc = control_softc; 11987 struct ctl_lun *lun; 11988 uint32_t initidx; 11989 11990 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11991 union ctl_ha_msg msg_info; 11992 11993 msg_info.hdr.nexus = io->io_hdr.nexus; 11994 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 11995 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11996 msg_info.hdr.original_sc = NULL; 11997 msg_info.hdr.serializing_sc = NULL; 11998 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11999 sizeof(msg_info.task), M_WAITOK); 12000 } 12001 12002 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12003 mtx_lock(&softc->ctl_lock); 12004 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12005 mtx_lock(&lun->lun_lock); 12006 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12007 io->io_hdr.nexus.initid, 1); 12008 #ifdef CTL_WITH_CA 12009 ctl_clear_mask(lun->have_ca, initidx); 12010 #endif 12011 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 12012 lun->flags &= ~CTL_LUN_RESERVED; 12013 if (ctl_is_set(lun->prevent, initidx)) { 12014 ctl_clear_mask(lun->prevent, initidx); 12015 lun->prevent_count--; 12016 } 12017 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12018 mtx_unlock(&lun->lun_lock); 12019 } 12020 mtx_unlock(&softc->ctl_lock); 12021 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12022 return (0); 12023 } 12024 12025 static int 12026 ctl_abort_task(union ctl_io *io) 12027 { 12028 union ctl_io *xio; 12029 struct ctl_lun *lun; 12030 struct ctl_softc *softc; 12031 #if 0 12032 struct sbuf sb; 12033 char printbuf[128]; 12034 #endif 12035 int found; 12036 uint32_t targ_lun; 12037 12038 softc = control_softc; 12039 found = 0; 12040 12041 /* 12042 * Look up the LUN. 12043 */ 12044 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12045 mtx_lock(&softc->ctl_lock); 12046 if ((targ_lun >= CTL_MAX_LUNS) || 12047 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12048 mtx_unlock(&softc->ctl_lock); 12049 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12050 return (1); 12051 } 12052 12053 #if 0 12054 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12055 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12056 #endif 12057 12058 mtx_lock(&lun->lun_lock); 12059 mtx_unlock(&softc->ctl_lock); 12060 /* 12061 * Run through the OOA queue and attempt to find the given I/O. 12062 * The target port, initiator ID, tag type and tag number have to 12063 * match the values that we got from the initiator. If we have an 12064 * untagged command to abort, simply abort the first untagged command 12065 * we come to. We only allow one untagged command at a time of course. 12066 */ 12067 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12068 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12069 #if 0 12070 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12071 12072 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12073 lun->lun, xio->scsiio.tag_num, 12074 xio->scsiio.tag_type, 12075 (xio->io_hdr.blocked_links.tqe_prev 12076 == NULL) ? "" : " BLOCKED", 12077 (xio->io_hdr.flags & 12078 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12079 (xio->io_hdr.flags & 12080 CTL_FLAG_ABORT) ? " ABORT" : "", 12081 (xio->io_hdr.flags & 12082 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12083 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12084 sbuf_finish(&sb); 12085 printf("%s\n", sbuf_data(&sb)); 12086 #endif 12087 12088 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12089 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12090 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12091 continue; 12092 12093 /* 12094 * If the abort says that the task is untagged, the 12095 * task in the queue must be untagged. Otherwise, 12096 * we just check to see whether the tag numbers 12097 * match. This is because the QLogic firmware 12098 * doesn't pass back the tag type in an abort 12099 * request. 12100 */ 12101 #if 0 12102 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12103 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12104 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12105 #endif 12106 /* 12107 * XXX KDM we've got problems with FC, because it 12108 * doesn't send down a tag type with aborts. So we 12109 * can only really go by the tag number... 12110 * This may cause problems with parallel SCSI. 12111 * Need to figure that out!! 12112 */ 12113 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12114 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12115 found = 1; 12116 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12117 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12118 union ctl_ha_msg msg_info; 12119 12120 msg_info.hdr.nexus = io->io_hdr.nexus; 12121 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12122 msg_info.task.tag_num = io->taskio.tag_num; 12123 msg_info.task.tag_type = io->taskio.tag_type; 12124 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12125 msg_info.hdr.original_sc = NULL; 12126 msg_info.hdr.serializing_sc = NULL; 12127 #if 0 12128 printf("Sent Abort to other side\n"); 12129 #endif 12130 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12131 sizeof(msg_info.task), M_NOWAIT); 12132 } 12133 #if 0 12134 printf("ctl_abort_task: found I/O to abort\n"); 12135 #endif 12136 } 12137 } 12138 mtx_unlock(&lun->lun_lock); 12139 12140 if (found == 0) { 12141 /* 12142 * This isn't really an error. It's entirely possible for 12143 * the abort and command completion to cross on the wire. 12144 * This is more of an informative/diagnostic error. 12145 */ 12146 #if 0 12147 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12148 "%u:%u:%u tag %d type %d\n", 12149 io->io_hdr.nexus.initid, 12150 io->io_hdr.nexus.targ_port, 12151 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12152 io->taskio.tag_type); 12153 #endif 12154 } 12155 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12156 return (0); 12157 } 12158 12159 static int 12160 ctl_query_task(union ctl_io *io, int task_set) 12161 { 12162 union ctl_io *xio; 12163 struct ctl_lun *lun; 12164 struct ctl_softc *softc; 12165 int found = 0; 12166 uint32_t targ_lun; 12167 12168 softc = control_softc; 12169 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12170 mtx_lock(&softc->ctl_lock); 12171 if ((targ_lun >= CTL_MAX_LUNS) || 12172 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12173 mtx_unlock(&softc->ctl_lock); 12174 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12175 return (1); 12176 } 12177 mtx_lock(&lun->lun_lock); 12178 mtx_unlock(&softc->ctl_lock); 12179 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12180 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12181 12182 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12183 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12184 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12185 continue; 12186 12187 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12188 found = 1; 12189 break; 12190 } 12191 } 12192 mtx_unlock(&lun->lun_lock); 12193 if (found) 12194 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12195 else 12196 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12197 return (0); 12198 } 12199 12200 static int 12201 ctl_query_async_event(union ctl_io *io) 12202 { 12203 struct ctl_lun *lun; 12204 struct ctl_softc *softc; 12205 ctl_ua_type ua; 12206 uint32_t targ_lun, initidx; 12207 12208 softc = control_softc; 12209 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12210 mtx_lock(&softc->ctl_lock); 12211 if ((targ_lun >= CTL_MAX_LUNS) || 12212 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12213 mtx_unlock(&softc->ctl_lock); 12214 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12215 return (1); 12216 } 12217 mtx_lock(&lun->lun_lock); 12218 mtx_unlock(&softc->ctl_lock); 12219 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12220 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12221 mtx_unlock(&lun->lun_lock); 12222 if (ua != CTL_UA_NONE) 12223 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12224 else 12225 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12226 return (0); 12227 } 12228 12229 static void 12230 ctl_run_task(union ctl_io *io) 12231 { 12232 struct ctl_softc *softc = control_softc; 12233 int retval = 1; 12234 12235 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12236 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12237 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12238 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12239 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12240 switch (io->taskio.task_action) { 12241 case CTL_TASK_ABORT_TASK: 12242 retval = ctl_abort_task(io); 12243 break; 12244 case CTL_TASK_ABORT_TASK_SET: 12245 case CTL_TASK_CLEAR_TASK_SET: 12246 retval = ctl_abort_task_set(io); 12247 break; 12248 case CTL_TASK_CLEAR_ACA: 12249 break; 12250 case CTL_TASK_I_T_NEXUS_RESET: 12251 retval = ctl_i_t_nexus_reset(io); 12252 break; 12253 case CTL_TASK_LUN_RESET: 12254 retval = ctl_lun_reset(softc, io); 12255 break; 12256 case CTL_TASK_TARGET_RESET: 12257 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12258 break; 12259 case CTL_TASK_BUS_RESET: 12260 retval = ctl_bus_reset(softc, io); 12261 break; 12262 case CTL_TASK_PORT_LOGIN: 12263 break; 12264 case CTL_TASK_PORT_LOGOUT: 12265 break; 12266 case CTL_TASK_QUERY_TASK: 12267 retval = ctl_query_task(io, 0); 12268 break; 12269 case CTL_TASK_QUERY_TASK_SET: 12270 retval = ctl_query_task(io, 1); 12271 break; 12272 case CTL_TASK_QUERY_ASYNC_EVENT: 12273 retval = ctl_query_async_event(io); 12274 break; 12275 default: 12276 printf("%s: got unknown task management event %d\n", 12277 __func__, io->taskio.task_action); 12278 break; 12279 } 12280 if (retval == 0) 12281 io->io_hdr.status = CTL_SUCCESS; 12282 else 12283 io->io_hdr.status = CTL_ERROR; 12284 ctl_done(io); 12285 } 12286 12287 /* 12288 * For HA operation. Handle commands that come in from the other 12289 * controller. 12290 */ 12291 static void 12292 ctl_handle_isc(union ctl_io *io) 12293 { 12294 int free_io; 12295 struct ctl_lun *lun; 12296 struct ctl_softc *softc = control_softc; 12297 uint32_t targ_lun; 12298 12299 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12300 lun = softc->ctl_luns[targ_lun]; 12301 12302 switch (io->io_hdr.msg_type) { 12303 case CTL_MSG_SERIALIZE: 12304 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12305 break; 12306 case CTL_MSG_R2R: { 12307 const struct ctl_cmd_entry *entry; 12308 12309 /* 12310 * This is only used in SER_ONLY mode. 12311 */ 12312 free_io = 0; 12313 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12314 mtx_lock(&lun->lun_lock); 12315 if (ctl_scsiio_lun_check(lun, 12316 entry, (struct ctl_scsiio *)io) != 0) { 12317 mtx_unlock(&lun->lun_lock); 12318 ctl_done(io); 12319 break; 12320 } 12321 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12322 mtx_unlock(&lun->lun_lock); 12323 ctl_enqueue_rtr(io); 12324 break; 12325 } 12326 case CTL_MSG_FINISH_IO: 12327 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12328 free_io = 0; 12329 ctl_done(io); 12330 } else { 12331 free_io = 1; 12332 mtx_lock(&lun->lun_lock); 12333 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12334 ooa_links); 12335 ctl_check_blocked(lun); 12336 mtx_unlock(&lun->lun_lock); 12337 } 12338 break; 12339 case CTL_MSG_PERS_ACTION: 12340 ctl_hndl_per_res_out_on_other_sc( 12341 (union ctl_ha_msg *)&io->presio.pr_msg); 12342 free_io = 1; 12343 break; 12344 case CTL_MSG_BAD_JUJU: 12345 free_io = 0; 12346 ctl_done(io); 12347 break; 12348 case CTL_MSG_DATAMOVE: 12349 /* Only used in XFER mode */ 12350 free_io = 0; 12351 ctl_datamove_remote(io); 12352 break; 12353 case CTL_MSG_DATAMOVE_DONE: 12354 /* Only used in XFER mode */ 12355 free_io = 0; 12356 io->scsiio.be_move_done(io); 12357 break; 12358 case CTL_MSG_FAILOVER: 12359 ctl_failover_lun(io); 12360 free_io = 1; 12361 break; 12362 default: 12363 free_io = 1; 12364 printf("%s: Invalid message type %d\n", 12365 __func__, io->io_hdr.msg_type); 12366 break; 12367 } 12368 if (free_io) 12369 ctl_free_io(io); 12370 12371 } 12372 12373 12374 /* 12375 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12376 * there is no match. 12377 */ 12378 static ctl_lun_error_pattern 12379 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12380 { 12381 const struct ctl_cmd_entry *entry; 12382 ctl_lun_error_pattern filtered_pattern, pattern; 12383 12384 pattern = desc->error_pattern; 12385 12386 /* 12387 * XXX KDM we need more data passed into this function to match a 12388 * custom pattern, and we actually need to implement custom pattern 12389 * matching. 12390 */ 12391 if (pattern & CTL_LUN_PAT_CMD) 12392 return (CTL_LUN_PAT_CMD); 12393 12394 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12395 return (CTL_LUN_PAT_ANY); 12396 12397 entry = ctl_get_cmd_entry(ctsio, NULL); 12398 12399 filtered_pattern = entry->pattern & pattern; 12400 12401 /* 12402 * If the user requested specific flags in the pattern (e.g. 12403 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12404 * flags. 12405 * 12406 * If the user did not specify any flags, it doesn't matter whether 12407 * or not the command supports the flags. 12408 */ 12409 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12410 (pattern & ~CTL_LUN_PAT_MASK)) 12411 return (CTL_LUN_PAT_NONE); 12412 12413 /* 12414 * If the user asked for a range check, see if the requested LBA 12415 * range overlaps with this command's LBA range. 12416 */ 12417 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12418 uint64_t lba1; 12419 uint64_t len1; 12420 ctl_action action; 12421 int retval; 12422 12423 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12424 if (retval != 0) 12425 return (CTL_LUN_PAT_NONE); 12426 12427 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12428 desc->lba_range.len, FALSE); 12429 /* 12430 * A "pass" means that the LBA ranges don't overlap, so 12431 * this doesn't match the user's range criteria. 12432 */ 12433 if (action == CTL_ACTION_PASS) 12434 return (CTL_LUN_PAT_NONE); 12435 } 12436 12437 return (filtered_pattern); 12438 } 12439 12440 static void 12441 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12442 { 12443 struct ctl_error_desc *desc, *desc2; 12444 12445 mtx_assert(&lun->lun_lock, MA_OWNED); 12446 12447 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12448 ctl_lun_error_pattern pattern; 12449 /* 12450 * Check to see whether this particular command matches 12451 * the pattern in the descriptor. 12452 */ 12453 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12454 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12455 continue; 12456 12457 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12458 case CTL_LUN_INJ_ABORTED: 12459 ctl_set_aborted(&io->scsiio); 12460 break; 12461 case CTL_LUN_INJ_MEDIUM_ERR: 12462 ctl_set_medium_error(&io->scsiio, 12463 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12464 CTL_FLAG_DATA_OUT); 12465 break; 12466 case CTL_LUN_INJ_UA: 12467 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12468 * OCCURRED */ 12469 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12470 break; 12471 case CTL_LUN_INJ_CUSTOM: 12472 /* 12473 * We're assuming the user knows what he is doing. 12474 * Just copy the sense information without doing 12475 * checks. 12476 */ 12477 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12478 MIN(sizeof(desc->custom_sense), 12479 sizeof(io->scsiio.sense_data))); 12480 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12481 io->scsiio.sense_len = SSD_FULL_SIZE; 12482 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12483 break; 12484 case CTL_LUN_INJ_NONE: 12485 default: 12486 /* 12487 * If this is an error injection type we don't know 12488 * about, clear the continuous flag (if it is set) 12489 * so it will get deleted below. 12490 */ 12491 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12492 break; 12493 } 12494 /* 12495 * By default, each error injection action is a one-shot 12496 */ 12497 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12498 continue; 12499 12500 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12501 12502 free(desc, M_CTL); 12503 } 12504 } 12505 12506 #ifdef CTL_IO_DELAY 12507 static void 12508 ctl_datamove_timer_wakeup(void *arg) 12509 { 12510 union ctl_io *io; 12511 12512 io = (union ctl_io *)arg; 12513 12514 ctl_datamove(io); 12515 } 12516 #endif /* CTL_IO_DELAY */ 12517 12518 void 12519 ctl_datamove(union ctl_io *io) 12520 { 12521 struct ctl_lun *lun; 12522 void (*fe_datamove)(union ctl_io *io); 12523 12524 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12525 12526 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12527 12528 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12529 #ifdef CTL_TIME_IO 12530 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12531 char str[256]; 12532 char path_str[64]; 12533 struct sbuf sb; 12534 12535 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12536 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12537 12538 sbuf_cat(&sb, path_str); 12539 switch (io->io_hdr.io_type) { 12540 case CTL_IO_SCSI: 12541 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12542 sbuf_printf(&sb, "\n"); 12543 sbuf_cat(&sb, path_str); 12544 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12545 io->scsiio.tag_num, io->scsiio.tag_type); 12546 break; 12547 case CTL_IO_TASK: 12548 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12549 "Tag Type: %d\n", io->taskio.task_action, 12550 io->taskio.tag_num, io->taskio.tag_type); 12551 break; 12552 default: 12553 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12554 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12555 break; 12556 } 12557 sbuf_cat(&sb, path_str); 12558 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12559 (intmax_t)time_uptime - io->io_hdr.start_time); 12560 sbuf_finish(&sb); 12561 printf("%s", sbuf_data(&sb)); 12562 } 12563 #endif /* CTL_TIME_IO */ 12564 12565 #ifdef CTL_IO_DELAY 12566 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12567 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12568 } else { 12569 if ((lun != NULL) 12570 && (lun->delay_info.datamove_delay > 0)) { 12571 12572 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12573 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12574 callout_reset(&io->io_hdr.delay_callout, 12575 lun->delay_info.datamove_delay * hz, 12576 ctl_datamove_timer_wakeup, io); 12577 if (lun->delay_info.datamove_type == 12578 CTL_DELAY_TYPE_ONESHOT) 12579 lun->delay_info.datamove_delay = 0; 12580 return; 12581 } 12582 } 12583 #endif 12584 12585 /* 12586 * This command has been aborted. Set the port status, so we fail 12587 * the data move. 12588 */ 12589 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12590 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12591 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12592 io->io_hdr.nexus.targ_port, 12593 io->io_hdr.nexus.targ_lun); 12594 io->io_hdr.port_status = 31337; 12595 /* 12596 * Note that the backend, in this case, will get the 12597 * callback in its context. In other cases it may get 12598 * called in the frontend's interrupt thread context. 12599 */ 12600 io->scsiio.be_move_done(io); 12601 return; 12602 } 12603 12604 /* Don't confuse frontend with zero length data move. */ 12605 if (io->scsiio.kern_data_len == 0) { 12606 io->scsiio.be_move_done(io); 12607 return; 12608 } 12609 12610 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12611 fe_datamove(io); 12612 } 12613 12614 static void 12615 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12616 { 12617 union ctl_ha_msg msg; 12618 #ifdef CTL_TIME_IO 12619 struct bintime cur_bt; 12620 #endif 12621 12622 memset(&msg, 0, sizeof(msg)); 12623 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12624 msg.hdr.original_sc = io; 12625 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12626 msg.hdr.nexus = io->io_hdr.nexus; 12627 msg.hdr.status = io->io_hdr.status; 12628 msg.scsi.tag_num = io->scsiio.tag_num; 12629 msg.scsi.tag_type = io->scsiio.tag_type; 12630 msg.scsi.scsi_status = io->scsiio.scsi_status; 12631 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12632 io->scsiio.sense_len); 12633 msg.scsi.sense_len = io->scsiio.sense_len; 12634 msg.scsi.sense_residual = io->scsiio.sense_residual; 12635 msg.scsi.fetd_status = io->io_hdr.port_status; 12636 msg.scsi.residual = io->scsiio.residual; 12637 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12638 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12639 ctl_failover_io(io, /*have_lock*/ have_lock); 12640 return; 12641 } 12642 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12643 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12644 msg.scsi.sense_len, M_WAITOK); 12645 12646 #ifdef CTL_TIME_IO 12647 getbinuptime(&cur_bt); 12648 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12649 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12650 #endif 12651 io->io_hdr.num_dmas++; 12652 } 12653 12654 /* 12655 * The DMA to the remote side is done, now we need to tell the other side 12656 * we're done so it can continue with its data movement. 12657 */ 12658 static void 12659 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12660 { 12661 union ctl_io *io; 12662 int i; 12663 12664 io = rq->context; 12665 12666 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12667 printf("%s: ISC DMA write failed with error %d", __func__, 12668 rq->ret); 12669 ctl_set_internal_failure(&io->scsiio, 12670 /*sks_valid*/ 1, 12671 /*retry_count*/ rq->ret); 12672 } 12673 12674 ctl_dt_req_free(rq); 12675 12676 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12677 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12678 free(io->io_hdr.remote_sglist, M_CTL); 12679 io->io_hdr.remote_sglist = NULL; 12680 io->io_hdr.local_sglist = NULL; 12681 12682 /* 12683 * The data is in local and remote memory, so now we need to send 12684 * status (good or back) back to the other side. 12685 */ 12686 ctl_send_datamove_done(io, /*have_lock*/ 0); 12687 } 12688 12689 /* 12690 * We've moved the data from the host/controller into local memory. Now we 12691 * need to push it over to the remote controller's memory. 12692 */ 12693 static int 12694 ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12695 { 12696 int retval; 12697 12698 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12699 ctl_datamove_remote_write_cb); 12700 return (retval); 12701 } 12702 12703 static void 12704 ctl_datamove_remote_write(union ctl_io *io) 12705 { 12706 int retval; 12707 void (*fe_datamove)(union ctl_io *io); 12708 12709 /* 12710 * - Get the data from the host/HBA into local memory. 12711 * - DMA memory from the local controller to the remote controller. 12712 * - Send status back to the remote controller. 12713 */ 12714 12715 retval = ctl_datamove_remote_sgl_setup(io); 12716 if (retval != 0) 12717 return; 12718 12719 /* Switch the pointer over so the FETD knows what to do */ 12720 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12721 12722 /* 12723 * Use a custom move done callback, since we need to send completion 12724 * back to the other controller, not to the backend on this side. 12725 */ 12726 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12727 12728 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12729 fe_datamove(io); 12730 } 12731 12732 static int 12733 ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12734 { 12735 #if 0 12736 char str[256]; 12737 char path_str[64]; 12738 struct sbuf sb; 12739 #endif 12740 int i; 12741 12742 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12743 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12744 free(io->io_hdr.remote_sglist, M_CTL); 12745 io->io_hdr.remote_sglist = NULL; 12746 io->io_hdr.local_sglist = NULL; 12747 12748 #if 0 12749 scsi_path_string(io, path_str, sizeof(path_str)); 12750 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12751 sbuf_cat(&sb, path_str); 12752 scsi_command_string(&io->scsiio, NULL, &sb); 12753 sbuf_printf(&sb, "\n"); 12754 sbuf_cat(&sb, path_str); 12755 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12756 io->scsiio.tag_num, io->scsiio.tag_type); 12757 sbuf_cat(&sb, path_str); 12758 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12759 io->io_hdr.flags, io->io_hdr.status); 12760 sbuf_finish(&sb); 12761 printk("%s", sbuf_data(&sb)); 12762 #endif 12763 12764 12765 /* 12766 * The read is done, now we need to send status (good or bad) back 12767 * to the other side. 12768 */ 12769 ctl_send_datamove_done(io, /*have_lock*/ 0); 12770 12771 return (0); 12772 } 12773 12774 static void 12775 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12776 { 12777 union ctl_io *io; 12778 void (*fe_datamove)(union ctl_io *io); 12779 12780 io = rq->context; 12781 12782 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12783 printf("%s: ISC DMA read failed with error %d\n", __func__, 12784 rq->ret); 12785 ctl_set_internal_failure(&io->scsiio, 12786 /*sks_valid*/ 1, 12787 /*retry_count*/ rq->ret); 12788 } 12789 12790 ctl_dt_req_free(rq); 12791 12792 /* Switch the pointer over so the FETD knows what to do */ 12793 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12794 12795 /* 12796 * Use a custom move done callback, since we need to send completion 12797 * back to the other controller, not to the backend on this side. 12798 */ 12799 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12800 12801 /* XXX KDM add checks like the ones in ctl_datamove? */ 12802 12803 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12804 fe_datamove(io); 12805 } 12806 12807 static int 12808 ctl_datamove_remote_sgl_setup(union ctl_io *io) 12809 { 12810 struct ctl_sg_entry *local_sglist; 12811 uint32_t len_to_go; 12812 int retval; 12813 int i; 12814 12815 retval = 0; 12816 local_sglist = io->io_hdr.local_sglist; 12817 len_to_go = io->scsiio.kern_data_len; 12818 12819 /* 12820 * The difficult thing here is that the size of the various 12821 * S/G segments may be different than the size from the 12822 * remote controller. That'll make it harder when DMAing 12823 * the data back to the other side. 12824 */ 12825 for (i = 0; len_to_go > 0; i++) { 12826 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12827 local_sglist[i].addr = 12828 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12829 12830 len_to_go -= local_sglist[i].len; 12831 } 12832 /* 12833 * Reset the number of S/G entries accordingly. The original 12834 * number of S/G entries is available in rem_sg_entries. 12835 */ 12836 io->scsiio.kern_sg_entries = i; 12837 12838 #if 0 12839 printf("%s: kern_sg_entries = %d\n", __func__, 12840 io->scsiio.kern_sg_entries); 12841 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12842 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12843 local_sglist[i].addr, local_sglist[i].len); 12844 #endif 12845 12846 return (retval); 12847 } 12848 12849 static int 12850 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12851 ctl_ha_dt_cb callback) 12852 { 12853 struct ctl_ha_dt_req *rq; 12854 struct ctl_sg_entry *remote_sglist, *local_sglist; 12855 uint32_t local_used, remote_used, total_used; 12856 int i, j, isc_ret; 12857 12858 rq = ctl_dt_req_alloc(); 12859 12860 /* 12861 * If we failed to allocate the request, and if the DMA didn't fail 12862 * anyway, set busy status. This is just a resource allocation 12863 * failure. 12864 */ 12865 if ((rq == NULL) 12866 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12867 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12868 ctl_set_busy(&io->scsiio); 12869 12870 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12871 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12872 12873 if (rq != NULL) 12874 ctl_dt_req_free(rq); 12875 12876 /* 12877 * The data move failed. We need to return status back 12878 * to the other controller. No point in trying to DMA 12879 * data to the remote controller. 12880 */ 12881 12882 ctl_send_datamove_done(io, /*have_lock*/ 0); 12883 12884 return (1); 12885 } 12886 12887 local_sglist = io->io_hdr.local_sglist; 12888 remote_sglist = io->io_hdr.remote_sglist; 12889 local_used = 0; 12890 remote_used = 0; 12891 total_used = 0; 12892 12893 /* 12894 * Pull/push the data over the wire from/to the other controller. 12895 * This takes into account the possibility that the local and 12896 * remote sglists may not be identical in terms of the size of 12897 * the elements and the number of elements. 12898 * 12899 * One fundamental assumption here is that the length allocated for 12900 * both the local and remote sglists is identical. Otherwise, we've 12901 * essentially got a coding error of some sort. 12902 */ 12903 isc_ret = CTL_HA_STATUS_SUCCESS; 12904 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12905 uint32_t cur_len; 12906 uint8_t *tmp_ptr; 12907 12908 rq->command = command; 12909 rq->context = io; 12910 12911 /* 12912 * Both pointers should be aligned. But it is possible 12913 * that the allocation length is not. They should both 12914 * also have enough slack left over at the end, though, 12915 * to round up to the next 8 byte boundary. 12916 */ 12917 cur_len = MIN(local_sglist[i].len - local_used, 12918 remote_sglist[j].len - remote_used); 12919 rq->size = cur_len; 12920 12921 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12922 tmp_ptr += local_used; 12923 12924 #if 0 12925 /* Use physical addresses when talking to ISC hardware */ 12926 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12927 /* XXX KDM use busdma */ 12928 rq->local = vtophys(tmp_ptr); 12929 } else 12930 rq->local = tmp_ptr; 12931 #else 12932 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12933 ("HA does not support BUS_ADDR")); 12934 rq->local = tmp_ptr; 12935 #endif 12936 12937 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12938 tmp_ptr += remote_used; 12939 rq->remote = tmp_ptr; 12940 12941 rq->callback = NULL; 12942 12943 local_used += cur_len; 12944 if (local_used >= local_sglist[i].len) { 12945 i++; 12946 local_used = 0; 12947 } 12948 12949 remote_used += cur_len; 12950 if (remote_used >= remote_sglist[j].len) { 12951 j++; 12952 remote_used = 0; 12953 } 12954 total_used += cur_len; 12955 12956 if (total_used >= io->scsiio.kern_data_len) 12957 rq->callback = callback; 12958 12959 #if 0 12960 printf("%s: %s: local %p remote %p size %d\n", __func__, 12961 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12962 rq->local, rq->remote, rq->size); 12963 #endif 12964 12965 isc_ret = ctl_dt_single(rq); 12966 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12967 break; 12968 } 12969 if (isc_ret != CTL_HA_STATUS_WAIT) { 12970 rq->ret = isc_ret; 12971 callback(rq); 12972 } 12973 12974 return (0); 12975 } 12976 12977 static void 12978 ctl_datamove_remote_read(union ctl_io *io) 12979 { 12980 int retval; 12981 int i; 12982 12983 /* 12984 * This will send an error to the other controller in the case of a 12985 * failure. 12986 */ 12987 retval = ctl_datamove_remote_sgl_setup(io); 12988 if (retval != 0) 12989 return; 12990 12991 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12992 ctl_datamove_remote_read_cb); 12993 if (retval != 0) { 12994 /* 12995 * Make sure we free memory if there was an error.. The 12996 * ctl_datamove_remote_xfer() function will send the 12997 * datamove done message, or call the callback with an 12998 * error if there is a problem. 12999 */ 13000 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13001 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13002 free(io->io_hdr.remote_sglist, M_CTL); 13003 io->io_hdr.remote_sglist = NULL; 13004 io->io_hdr.local_sglist = NULL; 13005 } 13006 } 13007 13008 /* 13009 * Process a datamove request from the other controller. This is used for 13010 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13011 * first. Once that is complete, the data gets DMAed into the remote 13012 * controller's memory. For reads, we DMA from the remote controller's 13013 * memory into our memory first, and then move it out to the FETD. 13014 */ 13015 static void 13016 ctl_datamove_remote(union ctl_io *io) 13017 { 13018 13019 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 13020 13021 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13022 ctl_failover_io(io, /*have_lock*/ 0); 13023 return; 13024 } 13025 13026 /* 13027 * Note that we look for an aborted I/O here, but don't do some of 13028 * the other checks that ctl_datamove() normally does. 13029 * We don't need to run the datamove delay code, since that should 13030 * have been done if need be on the other controller. 13031 */ 13032 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13033 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 13034 io->scsiio.tag_num, io->io_hdr.nexus.initid, 13035 io->io_hdr.nexus.targ_port, 13036 io->io_hdr.nexus.targ_lun); 13037 io->io_hdr.port_status = 31338; 13038 ctl_send_datamove_done(io, /*have_lock*/ 0); 13039 return; 13040 } 13041 13042 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 13043 ctl_datamove_remote_write(io); 13044 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 13045 ctl_datamove_remote_read(io); 13046 else { 13047 io->io_hdr.port_status = 31339; 13048 ctl_send_datamove_done(io, /*have_lock*/ 0); 13049 } 13050 } 13051 13052 static void 13053 ctl_process_done(union ctl_io *io) 13054 { 13055 struct ctl_lun *lun; 13056 struct ctl_softc *softc = control_softc; 13057 void (*fe_done)(union ctl_io *io); 13058 union ctl_ha_msg msg; 13059 uint32_t targ_port = io->io_hdr.nexus.targ_port; 13060 13061 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13062 fe_done = softc->ctl_ports[targ_port]->fe_done; 13063 13064 #ifdef CTL_TIME_IO 13065 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13066 char str[256]; 13067 char path_str[64]; 13068 struct sbuf sb; 13069 13070 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13071 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13072 13073 sbuf_cat(&sb, path_str); 13074 switch (io->io_hdr.io_type) { 13075 case CTL_IO_SCSI: 13076 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13077 sbuf_printf(&sb, "\n"); 13078 sbuf_cat(&sb, path_str); 13079 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13080 io->scsiio.tag_num, io->scsiio.tag_type); 13081 break; 13082 case CTL_IO_TASK: 13083 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13084 "Tag Type: %d\n", io->taskio.task_action, 13085 io->taskio.tag_num, io->taskio.tag_type); 13086 break; 13087 default: 13088 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13089 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13090 break; 13091 } 13092 sbuf_cat(&sb, path_str); 13093 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13094 (intmax_t)time_uptime - io->io_hdr.start_time); 13095 sbuf_finish(&sb); 13096 printf("%s", sbuf_data(&sb)); 13097 } 13098 #endif /* CTL_TIME_IO */ 13099 13100 switch (io->io_hdr.io_type) { 13101 case CTL_IO_SCSI: 13102 break; 13103 case CTL_IO_TASK: 13104 if (ctl_debug & CTL_DEBUG_INFO) 13105 ctl_io_error_print(io, NULL); 13106 fe_done(io); 13107 return; 13108 default: 13109 panic("ctl_process_done: invalid io type %d\n", 13110 io->io_hdr.io_type); 13111 break; /* NOTREACHED */ 13112 } 13113 13114 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13115 if (lun == NULL) { 13116 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13117 io->io_hdr.nexus.targ_mapped_lun)); 13118 goto bailout; 13119 } 13120 13121 mtx_lock(&lun->lun_lock); 13122 13123 /* 13124 * Check to see if we have any errors to inject here. We only 13125 * inject errors for commands that don't already have errors set. 13126 */ 13127 if (!STAILQ_EMPTY(&lun->error_list) && 13128 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13129 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13130 ctl_inject_error(lun, io); 13131 13132 /* 13133 * XXX KDM how do we treat commands that aren't completed 13134 * successfully? 13135 * 13136 * XXX KDM should we also track I/O latency? 13137 */ 13138 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13139 io->io_hdr.io_type == CTL_IO_SCSI) { 13140 #ifdef CTL_TIME_IO 13141 struct bintime cur_bt; 13142 #endif 13143 int type; 13144 13145 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13146 CTL_FLAG_DATA_IN) 13147 type = CTL_STATS_READ; 13148 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13149 CTL_FLAG_DATA_OUT) 13150 type = CTL_STATS_WRITE; 13151 else 13152 type = CTL_STATS_NO_IO; 13153 13154 lun->stats.ports[targ_port].bytes[type] += 13155 io->scsiio.kern_total_len; 13156 lun->stats.ports[targ_port].operations[type]++; 13157 #ifdef CTL_TIME_IO 13158 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13159 &io->io_hdr.dma_bt); 13160 getbinuptime(&cur_bt); 13161 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13162 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13163 #endif 13164 lun->stats.ports[targ_port].num_dmas[type] += 13165 io->io_hdr.num_dmas; 13166 } 13167 13168 /* 13169 * Remove this from the OOA queue. 13170 */ 13171 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13172 #ifdef CTL_TIME_IO 13173 if (TAILQ_EMPTY(&lun->ooa_queue)) 13174 lun->last_busy = getsbinuptime(); 13175 #endif 13176 13177 /* 13178 * Run through the blocked queue on this LUN and see if anything 13179 * has become unblocked, now that this transaction is done. 13180 */ 13181 ctl_check_blocked(lun); 13182 13183 /* 13184 * If the LUN has been invalidated, free it if there is nothing 13185 * left on its OOA queue. 13186 */ 13187 if ((lun->flags & CTL_LUN_INVALID) 13188 && TAILQ_EMPTY(&lun->ooa_queue)) { 13189 mtx_unlock(&lun->lun_lock); 13190 mtx_lock(&softc->ctl_lock); 13191 ctl_free_lun(lun); 13192 mtx_unlock(&softc->ctl_lock); 13193 } else 13194 mtx_unlock(&lun->lun_lock); 13195 13196 bailout: 13197 13198 /* 13199 * If this command has been aborted, make sure we set the status 13200 * properly. The FETD is responsible for freeing the I/O and doing 13201 * whatever it needs to do to clean up its state. 13202 */ 13203 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13204 ctl_set_task_aborted(&io->scsiio); 13205 13206 /* 13207 * If enabled, print command error status. 13208 */ 13209 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13210 (ctl_debug & CTL_DEBUG_INFO) != 0) 13211 ctl_io_error_print(io, NULL); 13212 13213 /* 13214 * Tell the FETD or the other shelf controller we're done with this 13215 * command. Note that only SCSI commands get to this point. Task 13216 * management commands are completed above. 13217 */ 13218 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13219 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13220 memset(&msg, 0, sizeof(msg)); 13221 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13222 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13223 msg.hdr.nexus = io->io_hdr.nexus; 13224 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13225 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13226 M_WAITOK); 13227 } 13228 13229 fe_done(io); 13230 } 13231 13232 #ifdef CTL_WITH_CA 13233 /* 13234 * Front end should call this if it doesn't do autosense. When the request 13235 * sense comes back in from the initiator, we'll dequeue this and send it. 13236 */ 13237 int 13238 ctl_queue_sense(union ctl_io *io) 13239 { 13240 struct ctl_lun *lun; 13241 struct ctl_port *port; 13242 struct ctl_softc *softc; 13243 uint32_t initidx, targ_lun; 13244 13245 softc = control_softc; 13246 13247 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13248 13249 /* 13250 * LUN lookup will likely move to the ctl_work_thread() once we 13251 * have our new queueing infrastructure (that doesn't put things on 13252 * a per-LUN queue initially). That is so that we can handle 13253 * things like an INQUIRY to a LUN that we don't have enabled. We 13254 * can't deal with that right now. 13255 */ 13256 mtx_lock(&softc->ctl_lock); 13257 13258 /* 13259 * If we don't have a LUN for this, just toss the sense 13260 * information. 13261 */ 13262 port = ctl_io_port(&ctsio->io_hdr); 13263 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13264 if ((targ_lun < CTL_MAX_LUNS) 13265 && (softc->ctl_luns[targ_lun] != NULL)) 13266 lun = softc->ctl_luns[targ_lun]; 13267 else 13268 goto bailout; 13269 13270 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13271 13272 mtx_lock(&lun->lun_lock); 13273 /* 13274 * Already have CA set for this LUN...toss the sense information. 13275 */ 13276 if (ctl_is_set(lun->have_ca, initidx)) { 13277 mtx_unlock(&lun->lun_lock); 13278 goto bailout; 13279 } 13280 13281 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13282 MIN(sizeof(lun->pending_sense[initidx]), 13283 sizeof(io->scsiio.sense_data))); 13284 ctl_set_mask(lun->have_ca, initidx); 13285 mtx_unlock(&lun->lun_lock); 13286 13287 bailout: 13288 mtx_unlock(&softc->ctl_lock); 13289 13290 ctl_free_io(io); 13291 13292 return (CTL_RETVAL_COMPLETE); 13293 } 13294 #endif 13295 13296 /* 13297 * Primary command inlet from frontend ports. All SCSI and task I/O 13298 * requests must go through this function. 13299 */ 13300 int 13301 ctl_queue(union ctl_io *io) 13302 { 13303 struct ctl_port *port; 13304 13305 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13306 13307 #ifdef CTL_TIME_IO 13308 io->io_hdr.start_time = time_uptime; 13309 getbinuptime(&io->io_hdr.start_bt); 13310 #endif /* CTL_TIME_IO */ 13311 13312 /* Map FE-specific LUN ID into global one. */ 13313 port = ctl_io_port(&io->io_hdr); 13314 io->io_hdr.nexus.targ_mapped_lun = 13315 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13316 13317 switch (io->io_hdr.io_type) { 13318 case CTL_IO_SCSI: 13319 case CTL_IO_TASK: 13320 if (ctl_debug & CTL_DEBUG_CDB) 13321 ctl_io_print(io); 13322 ctl_enqueue_incoming(io); 13323 break; 13324 default: 13325 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13326 return (EINVAL); 13327 } 13328 13329 return (CTL_RETVAL_COMPLETE); 13330 } 13331 13332 #ifdef CTL_IO_DELAY 13333 static void 13334 ctl_done_timer_wakeup(void *arg) 13335 { 13336 union ctl_io *io; 13337 13338 io = (union ctl_io *)arg; 13339 ctl_done(io); 13340 } 13341 #endif /* CTL_IO_DELAY */ 13342 13343 void 13344 ctl_serseq_done(union ctl_io *io) 13345 { 13346 struct ctl_lun *lun; 13347 13348 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13349 if (lun->be_lun == NULL || 13350 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13351 return; 13352 mtx_lock(&lun->lun_lock); 13353 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13354 ctl_check_blocked(lun); 13355 mtx_unlock(&lun->lun_lock); 13356 } 13357 13358 void 13359 ctl_done(union ctl_io *io) 13360 { 13361 13362 /* 13363 * Enable this to catch duplicate completion issues. 13364 */ 13365 #if 0 13366 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13367 printf("%s: type %d msg %d cdb %x iptl: " 13368 "%u:%u:%u tag 0x%04x " 13369 "flag %#x status %x\n", 13370 __func__, 13371 io->io_hdr.io_type, 13372 io->io_hdr.msg_type, 13373 io->scsiio.cdb[0], 13374 io->io_hdr.nexus.initid, 13375 io->io_hdr.nexus.targ_port, 13376 io->io_hdr.nexus.targ_lun, 13377 (io->io_hdr.io_type == 13378 CTL_IO_TASK) ? 13379 io->taskio.tag_num : 13380 io->scsiio.tag_num, 13381 io->io_hdr.flags, 13382 io->io_hdr.status); 13383 } else 13384 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13385 #endif 13386 13387 /* 13388 * This is an internal copy of an I/O, and should not go through 13389 * the normal done processing logic. 13390 */ 13391 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13392 return; 13393 13394 #ifdef CTL_IO_DELAY 13395 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13396 struct ctl_lun *lun; 13397 13398 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13399 13400 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13401 } else { 13402 struct ctl_lun *lun; 13403 13404 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13405 13406 if ((lun != NULL) 13407 && (lun->delay_info.done_delay > 0)) { 13408 13409 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13410 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13411 callout_reset(&io->io_hdr.delay_callout, 13412 lun->delay_info.done_delay * hz, 13413 ctl_done_timer_wakeup, io); 13414 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13415 lun->delay_info.done_delay = 0; 13416 return; 13417 } 13418 } 13419 #endif /* CTL_IO_DELAY */ 13420 13421 ctl_enqueue_done(io); 13422 } 13423 13424 static void 13425 ctl_work_thread(void *arg) 13426 { 13427 struct ctl_thread *thr = (struct ctl_thread *)arg; 13428 struct ctl_softc *softc = thr->ctl_softc; 13429 union ctl_io *io; 13430 int retval; 13431 13432 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13433 13434 for (;;) { 13435 /* 13436 * We handle the queues in this order: 13437 * - ISC 13438 * - done queue (to free up resources, unblock other commands) 13439 * - RtR queue 13440 * - incoming queue 13441 * 13442 * If those queues are empty, we break out of the loop and 13443 * go to sleep. 13444 */ 13445 mtx_lock(&thr->queue_lock); 13446 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13447 if (io != NULL) { 13448 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13449 mtx_unlock(&thr->queue_lock); 13450 ctl_handle_isc(io); 13451 continue; 13452 } 13453 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13454 if (io != NULL) { 13455 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13456 /* clear any blocked commands, call fe_done */ 13457 mtx_unlock(&thr->queue_lock); 13458 ctl_process_done(io); 13459 continue; 13460 } 13461 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13462 if (io != NULL) { 13463 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13464 mtx_unlock(&thr->queue_lock); 13465 if (io->io_hdr.io_type == CTL_IO_TASK) 13466 ctl_run_task(io); 13467 else 13468 ctl_scsiio_precheck(softc, &io->scsiio); 13469 continue; 13470 } 13471 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13472 if (io != NULL) { 13473 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13474 mtx_unlock(&thr->queue_lock); 13475 retval = ctl_scsiio(&io->scsiio); 13476 if (retval != CTL_RETVAL_COMPLETE) 13477 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13478 continue; 13479 } 13480 13481 /* Sleep until we have something to do. */ 13482 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13483 } 13484 } 13485 13486 static void 13487 ctl_lun_thread(void *arg) 13488 { 13489 struct ctl_softc *softc = (struct ctl_softc *)arg; 13490 struct ctl_be_lun *be_lun; 13491 13492 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13493 13494 for (;;) { 13495 mtx_lock(&softc->ctl_lock); 13496 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13497 if (be_lun != NULL) { 13498 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13499 mtx_unlock(&softc->ctl_lock); 13500 ctl_create_lun(be_lun); 13501 continue; 13502 } 13503 13504 /* Sleep until we have something to do. */ 13505 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13506 PDROP | PRIBIO, "-", 0); 13507 } 13508 } 13509 13510 static void 13511 ctl_thresh_thread(void *arg) 13512 { 13513 struct ctl_softc *softc = (struct ctl_softc *)arg; 13514 struct ctl_lun *lun; 13515 struct scsi_da_rw_recovery_page *rwpage; 13516 struct ctl_logical_block_provisioning_page *page; 13517 const char *attr; 13518 union ctl_ha_msg msg; 13519 uint64_t thres, val; 13520 int i, e, set; 13521 13522 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13523 13524 for (;;) { 13525 mtx_lock(&softc->ctl_lock); 13526 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13527 if ((lun->flags & CTL_LUN_DISABLED) || 13528 (lun->flags & CTL_LUN_NO_MEDIA) || 13529 lun->backend->lun_attr == NULL) 13530 continue; 13531 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13532 softc->ha_mode == CTL_HA_MODE_XFER) 13533 continue; 13534 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13535 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13536 continue; 13537 e = 0; 13538 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13539 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13540 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13541 continue; 13542 thres = scsi_4btoul(page->descr[i].count); 13543 thres <<= CTL_LBP_EXPONENT; 13544 switch (page->descr[i].resource) { 13545 case 0x01: 13546 attr = "blocksavail"; 13547 break; 13548 case 0x02: 13549 attr = "blocksused"; 13550 break; 13551 case 0xf1: 13552 attr = "poolblocksavail"; 13553 break; 13554 case 0xf2: 13555 attr = "poolblocksused"; 13556 break; 13557 default: 13558 continue; 13559 } 13560 mtx_unlock(&softc->ctl_lock); // XXX 13561 val = lun->backend->lun_attr( 13562 lun->be_lun->be_lun, attr); 13563 mtx_lock(&softc->ctl_lock); 13564 if (val == UINT64_MAX) 13565 continue; 13566 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13567 == SLBPPD_ARMING_INC) 13568 e = (val >= thres); 13569 else 13570 e = (val <= thres); 13571 if (e) 13572 break; 13573 } 13574 mtx_lock(&lun->lun_lock); 13575 if (e) { 13576 scsi_u64to8b((uint8_t *)&page->descr[i] - 13577 (uint8_t *)page, lun->ua_tpt_info); 13578 if (lun->lasttpt == 0 || 13579 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13580 lun->lasttpt = time_uptime; 13581 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13582 set = 1; 13583 } else 13584 set = 0; 13585 } else { 13586 lun->lasttpt = 0; 13587 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13588 set = -1; 13589 } 13590 mtx_unlock(&lun->lun_lock); 13591 if (set != 0 && 13592 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13593 /* Send msg to other side. */ 13594 bzero(&msg.ua, sizeof(msg.ua)); 13595 msg.hdr.msg_type = CTL_MSG_UA; 13596 msg.hdr.nexus.initid = -1; 13597 msg.hdr.nexus.targ_port = -1; 13598 msg.hdr.nexus.targ_lun = lun->lun; 13599 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13600 msg.ua.ua_all = 1; 13601 msg.ua.ua_set = (set > 0); 13602 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13603 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13604 mtx_unlock(&softc->ctl_lock); // XXX 13605 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13606 sizeof(msg.ua), M_WAITOK); 13607 mtx_lock(&softc->ctl_lock); 13608 } 13609 } 13610 mtx_unlock(&softc->ctl_lock); 13611 pause("-", CTL_LBP_PERIOD * hz); 13612 } 13613 } 13614 13615 static void 13616 ctl_enqueue_incoming(union ctl_io *io) 13617 { 13618 struct ctl_softc *softc = control_softc; 13619 struct ctl_thread *thr; 13620 u_int idx; 13621 13622 idx = (io->io_hdr.nexus.targ_port * 127 + 13623 io->io_hdr.nexus.initid) % worker_threads; 13624 thr = &softc->threads[idx]; 13625 mtx_lock(&thr->queue_lock); 13626 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13627 mtx_unlock(&thr->queue_lock); 13628 wakeup(thr); 13629 } 13630 13631 static void 13632 ctl_enqueue_rtr(union ctl_io *io) 13633 { 13634 struct ctl_softc *softc = control_softc; 13635 struct ctl_thread *thr; 13636 13637 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13638 mtx_lock(&thr->queue_lock); 13639 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13640 mtx_unlock(&thr->queue_lock); 13641 wakeup(thr); 13642 } 13643 13644 static void 13645 ctl_enqueue_done(union ctl_io *io) 13646 { 13647 struct ctl_softc *softc = control_softc; 13648 struct ctl_thread *thr; 13649 13650 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13651 mtx_lock(&thr->queue_lock); 13652 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13653 mtx_unlock(&thr->queue_lock); 13654 wakeup(thr); 13655 } 13656 13657 static void 13658 ctl_enqueue_isc(union ctl_io *io) 13659 { 13660 struct ctl_softc *softc = control_softc; 13661 struct ctl_thread *thr; 13662 13663 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13664 mtx_lock(&thr->queue_lock); 13665 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13666 mtx_unlock(&thr->queue_lock); 13667 wakeup(thr); 13668 } 13669 13670 /* 13671 * vim: ts=8 13672 */ 13673