1 /*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36 /* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42 #define _CTL_C 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/types.h> 51 #include <sys/kthread.h> 52 #include <sys/bio.h> 53 #include <sys/fcntl.h> 54 #include <sys/lock.h> 55 #include <sys/module.h> 56 #include <sys/mutex.h> 57 #include <sys/condvar.h> 58 #include <sys/malloc.h> 59 #include <sys/conf.h> 60 #include <sys/ioccom.h> 61 #include <sys/queue.h> 62 #include <sys/sbuf.h> 63 #include <sys/smp.h> 64 #include <sys/endian.h> 65 #include <sys/sysctl.h> 66 67 #include <cam/cam.h> 68 #include <cam/scsi/scsi_all.h> 69 #include <cam/scsi/scsi_da.h> 70 #include <cam/ctl/ctl_io.h> 71 #include <cam/ctl/ctl.h> 72 #include <cam/ctl/ctl_frontend.h> 73 #include <cam/ctl/ctl_frontend_internal.h> 74 #include <cam/ctl/ctl_util.h> 75 #include <cam/ctl/ctl_backend.h> 76 #include <cam/ctl/ctl_ioctl.h> 77 #include <cam/ctl/ctl_ha.h> 78 #include <cam/ctl/ctl_private.h> 79 #include <cam/ctl/ctl_debug.h> 80 #include <cam/ctl/ctl_scsi_all.h> 81 #include <cam/ctl/ctl_error.h> 82 83 struct ctl_softc *control_softc = NULL; 84 85 /* 86 * Size and alignment macros needed for Copan-specific HA hardware. These 87 * can go away when the HA code is re-written, and uses busdma for any 88 * hardware. 89 */ 90 #define CTL_ALIGN_8B(target, source, type) \ 91 if (((uint32_t)source & 0x7) != 0) \ 92 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 93 else \ 94 target = (type)source; 95 96 #define CTL_SIZE_8B(target, size) \ 97 if ((size & 0x7) != 0) \ 98 target = size + (0x8 - (size & 0x7)); \ 99 else \ 100 target = size; 101 102 #define CTL_ALIGN_8B_MARGIN 16 103 104 /* 105 * Template mode pages. 106 */ 107 108 /* 109 * Note that these are default values only. The actual values will be 110 * filled in when the user does a mode sense. 111 */ 112 static struct copan_power_subpage power_page_default = { 113 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 114 /*subpage*/ PWR_SUBPAGE_CODE, 115 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 116 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 117 /*page_version*/ PWR_VERSION, 118 /* total_luns */ 26, 119 /* max_active_luns*/ PWR_DFLT_MAX_LUNS, 120 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 122 0, 0, 0, 0, 0, 0} 123 }; 124 125 static struct copan_power_subpage power_page_changeable = { 126 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 127 /*subpage*/ PWR_SUBPAGE_CODE, 128 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 129 (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 130 /*page_version*/ 0, 131 /* total_luns */ 0, 132 /* max_active_luns*/ 0, 133 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135 0, 0, 0, 0, 0, 0} 136 }; 137 138 static struct copan_aps_subpage aps_page_default = { 139 APS_PAGE_CODE | SMPH_SPF, //page_code 140 APS_SUBPAGE_CODE, //subpage 141 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 142 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 143 APS_VERSION, //page_version 144 0, //lock_active 145 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 147 0, 0, 0, 0, 0} //reserved 148 }; 149 150 static struct copan_aps_subpage aps_page_changeable = { 151 APS_PAGE_CODE | SMPH_SPF, //page_code 152 APS_SUBPAGE_CODE, //subpage 153 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 154 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 155 0, //page_version 156 0, //lock_active 157 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 159 0, 0, 0, 0, 0} //reserved 160 }; 161 162 static struct copan_debugconf_subpage debugconf_page_default = { 163 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 164 DBGCNF_SUBPAGE_CODE, /* subpage */ 165 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 166 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 167 DBGCNF_VERSION, /* page_version */ 168 {CTL_TIME_IO_DEFAULT_SECS>>8, 169 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 170 }; 171 172 static struct copan_debugconf_subpage debugconf_page_changeable = { 173 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 174 DBGCNF_SUBPAGE_CODE, /* subpage */ 175 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 176 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 177 0, /* page_version */ 178 {0xff,0xff}, /* ctl_time_io_secs */ 179 }; 180 181 static struct scsi_format_page format_page_default = { 182 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 183 /*page_length*/sizeof(struct scsi_format_page) - 2, 184 /*tracks_per_zone*/ {0, 0}, 185 /*alt_sectors_per_zone*/ {0, 0}, 186 /*alt_tracks_per_zone*/ {0, 0}, 187 /*alt_tracks_per_lun*/ {0, 0}, 188 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 189 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 190 /*bytes_per_sector*/ {0, 0}, 191 /*interleave*/ {0, 0}, 192 /*track_skew*/ {0, 0}, 193 /*cylinder_skew*/ {0, 0}, 194 /*flags*/ SFP_HSEC, 195 /*reserved*/ {0, 0, 0} 196 }; 197 198 static struct scsi_format_page format_page_changeable = { 199 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 200 /*page_length*/sizeof(struct scsi_format_page) - 2, 201 /*tracks_per_zone*/ {0, 0}, 202 /*alt_sectors_per_zone*/ {0, 0}, 203 /*alt_tracks_per_zone*/ {0, 0}, 204 /*alt_tracks_per_lun*/ {0, 0}, 205 /*sectors_per_track*/ {0, 0}, 206 /*bytes_per_sector*/ {0, 0}, 207 /*interleave*/ {0, 0}, 208 /*track_skew*/ {0, 0}, 209 /*cylinder_skew*/ {0, 0}, 210 /*flags*/ 0, 211 /*reserved*/ {0, 0, 0} 212 }; 213 214 static struct scsi_rigid_disk_page rigid_disk_page_default = { 215 /*page_code*/SMS_RIGID_DISK_PAGE, 216 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 217 /*cylinders*/ {0, 0, 0}, 218 /*heads*/ CTL_DEFAULT_HEADS, 219 /*start_write_precomp*/ {0, 0, 0}, 220 /*start_reduced_current*/ {0, 0, 0}, 221 /*step_rate*/ {0, 0}, 222 /*landing_zone_cylinder*/ {0, 0, 0}, 223 /*rpl*/ SRDP_RPL_DISABLED, 224 /*rotational_offset*/ 0, 225 /*reserved1*/ 0, 226 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 227 CTL_DEFAULT_ROTATION_RATE & 0xff}, 228 /*reserved2*/ {0, 0} 229 }; 230 231 static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 232 /*page_code*/SMS_RIGID_DISK_PAGE, 233 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 234 /*cylinders*/ {0, 0, 0}, 235 /*heads*/ 0, 236 /*start_write_precomp*/ {0, 0, 0}, 237 /*start_reduced_current*/ {0, 0, 0}, 238 /*step_rate*/ {0, 0}, 239 /*landing_zone_cylinder*/ {0, 0, 0}, 240 /*rpl*/ 0, 241 /*rotational_offset*/ 0, 242 /*reserved1*/ 0, 243 /*rotation_rate*/ {0, 0}, 244 /*reserved2*/ {0, 0} 245 }; 246 247 static struct scsi_caching_page caching_page_default = { 248 /*page_code*/SMS_CACHING_PAGE, 249 /*page_length*/sizeof(struct scsi_caching_page) - 2, 250 /*flags1*/ SCP_DISC | SCP_WCE, 251 /*ret_priority*/ 0, 252 /*disable_pf_transfer_len*/ {0xff, 0xff}, 253 /*min_prefetch*/ {0, 0}, 254 /*max_prefetch*/ {0xff, 0xff}, 255 /*max_pf_ceiling*/ {0xff, 0xff}, 256 /*flags2*/ 0, 257 /*cache_segments*/ 0, 258 /*cache_seg_size*/ {0, 0}, 259 /*reserved*/ 0, 260 /*non_cache_seg_size*/ {0, 0, 0} 261 }; 262 263 static struct scsi_caching_page caching_page_changeable = { 264 /*page_code*/SMS_CACHING_PAGE, 265 /*page_length*/sizeof(struct scsi_caching_page) - 2, 266 /*flags1*/ SCP_WCE | SCP_RCD, 267 /*ret_priority*/ 0, 268 /*disable_pf_transfer_len*/ {0, 0}, 269 /*min_prefetch*/ {0, 0}, 270 /*max_prefetch*/ {0, 0}, 271 /*max_pf_ceiling*/ {0, 0}, 272 /*flags2*/ 0, 273 /*cache_segments*/ 0, 274 /*cache_seg_size*/ {0, 0}, 275 /*reserved*/ 0, 276 /*non_cache_seg_size*/ {0, 0, 0} 277 }; 278 279 static struct scsi_control_page control_page_default = { 280 /*page_code*/SMS_CONTROL_MODE_PAGE, 281 /*page_length*/sizeof(struct scsi_control_page) - 2, 282 /*rlec*/0, 283 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 284 /*eca_and_aen*/0, 285 /*flags4*/SCP_TAS, 286 /*aen_holdoff_period*/{0, 0}, 287 /*busy_timeout_period*/{0, 0}, 288 /*extended_selftest_completion_time*/{0, 0} 289 }; 290 291 static struct scsi_control_page control_page_changeable = { 292 /*page_code*/SMS_CONTROL_MODE_PAGE, 293 /*page_length*/sizeof(struct scsi_control_page) - 2, 294 /*rlec*/SCP_DSENSE, 295 /*queue_flags*/SCP_QUEUE_ALG_MASK, 296 /*eca_and_aen*/0, 297 /*flags4*/0, 298 /*aen_holdoff_period*/{0, 0}, 299 /*busy_timeout_period*/{0, 0}, 300 /*extended_selftest_completion_time*/{0, 0} 301 }; 302 303 304 /* 305 * XXX KDM move these into the softc. 306 */ 307 static int rcv_sync_msg; 308 static int persis_offset; 309 static uint8_t ctl_pause_rtr; 310 static int ctl_is_single = 1; 311 static int index_to_aps_page; 312 313 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 314 static int worker_threads = -1; 315 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 316 &worker_threads, 1, "Number of worker threads"); 317 static int verbose = 0; 318 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 319 &verbose, 0, "Show SCSI errors returned to initiator"); 320 321 /* 322 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 323 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 324 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 325 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 326 */ 327 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 328 329 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 330 int param); 331 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 332 static int ctl_init(void); 333 void ctl_shutdown(void); 334 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 335 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 336 static void ctl_ioctl_online(void *arg); 337 static void ctl_ioctl_offline(void *arg); 338 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 339 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 340 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 341 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 342 static int ctl_ioctl_submit_wait(union ctl_io *io); 343 static void ctl_ioctl_datamove(union ctl_io *io); 344 static void ctl_ioctl_done(union ctl_io *io); 345 static void ctl_ioctl_hard_startstop_callback(void *arg, 346 struct cfi_metatask *metatask); 347 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 348 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 349 struct ctl_ooa *ooa_hdr, 350 struct ctl_ooa_entry *kern_entries); 351 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 352 struct thread *td); 353 static uint32_t ctl_map_lun(int port_num, uint32_t lun); 354 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 355 #ifdef unused 356 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 357 uint32_t targ_target, uint32_t targ_lun, 358 int can_wait); 359 static void ctl_kfree_io(union ctl_io *io); 360 #endif /* unused */ 361 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 362 struct ctl_be_lun *be_lun, struct ctl_id target_id); 363 static int ctl_free_lun(struct ctl_lun *lun); 364 static void ctl_create_lun(struct ctl_be_lun *be_lun); 365 /** 366 static void ctl_failover_change_pages(struct ctl_softc *softc, 367 struct ctl_scsiio *ctsio, int master); 368 **/ 369 370 static int ctl_do_mode_select(union ctl_io *io); 371 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 372 uint64_t res_key, uint64_t sa_res_key, 373 uint8_t type, uint32_t residx, 374 struct ctl_scsiio *ctsio, 375 struct scsi_per_res_out *cdb, 376 struct scsi_per_res_out_parms* param); 377 static void ctl_pro_preempt_other(struct ctl_lun *lun, 378 union ctl_ha_msg *msg); 379 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 380 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 381 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 382 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 383 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 384 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 385 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 386 int alloc_len); 387 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 388 int alloc_len); 389 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 390 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 391 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 392 static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 393 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 394 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 395 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 396 union ctl_io *pending_io, union ctl_io *ooa_io); 397 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 398 union ctl_io *starting_io); 399 static int ctl_check_blocked(struct ctl_lun *lun); 400 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 401 struct ctl_lun *lun, 402 const struct ctl_cmd_entry *entry, 403 struct ctl_scsiio *ctsio); 404 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 405 static void ctl_failover(void); 406 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 407 struct ctl_scsiio *ctsio); 408 static int ctl_scsiio(struct ctl_scsiio *ctsio); 409 410 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 411 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 412 ctl_ua_type ua_type); 413 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 414 ctl_ua_type ua_type); 415 static int ctl_abort_task(union ctl_io *io); 416 static int ctl_abort_task_set(union ctl_io *io); 417 static int ctl_i_t_nexus_reset(union ctl_io *io); 418 static void ctl_run_task(union ctl_io *io); 419 #ifdef CTL_IO_DELAY 420 static void ctl_datamove_timer_wakeup(void *arg); 421 static void ctl_done_timer_wakeup(void *arg); 422 #endif /* CTL_IO_DELAY */ 423 424 static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 425 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 426 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 427 static void ctl_datamove_remote_write(union ctl_io *io); 428 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 429 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 430 static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 431 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 432 ctl_ha_dt_cb callback); 433 static void ctl_datamove_remote_read(union ctl_io *io); 434 static void ctl_datamove_remote(union ctl_io *io); 435 static int ctl_process_done(union ctl_io *io); 436 static void ctl_lun_thread(void *arg); 437 static void ctl_work_thread(void *arg); 438 static void ctl_enqueue_incoming(union ctl_io *io); 439 static void ctl_enqueue_rtr(union ctl_io *io); 440 static void ctl_enqueue_done(union ctl_io *io); 441 static void ctl_enqueue_isc(union ctl_io *io); 442 static const struct ctl_cmd_entry * 443 ctl_get_cmd_entry(struct ctl_scsiio *ctsio); 444 static const struct ctl_cmd_entry * 445 ctl_validate_command(struct ctl_scsiio *ctsio); 446 static int ctl_cmd_applicable(uint8_t lun_type, 447 const struct ctl_cmd_entry *entry); 448 449 /* 450 * Load the serialization table. This isn't very pretty, but is probably 451 * the easiest way to do it. 452 */ 453 #include "ctl_ser_table.c" 454 455 /* 456 * We only need to define open, close and ioctl routines for this driver. 457 */ 458 static struct cdevsw ctl_cdevsw = { 459 .d_version = D_VERSION, 460 .d_flags = 0, 461 .d_open = ctl_open, 462 .d_close = ctl_close, 463 .d_ioctl = ctl_ioctl, 464 .d_name = "ctl", 465 }; 466 467 468 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 469 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 470 471 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 472 473 static moduledata_t ctl_moduledata = { 474 "ctl", 475 ctl_module_event_handler, 476 NULL 477 }; 478 479 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 480 MODULE_VERSION(ctl, 1); 481 482 static struct ctl_frontend ioctl_frontend = 483 { 484 .name = "ioctl", 485 }; 486 487 static void 488 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 489 union ctl_ha_msg *msg_info) 490 { 491 struct ctl_scsiio *ctsio; 492 493 if (msg_info->hdr.original_sc == NULL) { 494 printf("%s: original_sc == NULL!\n", __func__); 495 /* XXX KDM now what? */ 496 return; 497 } 498 499 ctsio = &msg_info->hdr.original_sc->scsiio; 500 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 501 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 502 ctsio->io_hdr.status = msg_info->hdr.status; 503 ctsio->scsi_status = msg_info->scsi.scsi_status; 504 ctsio->sense_len = msg_info->scsi.sense_len; 505 ctsio->sense_residual = msg_info->scsi.sense_residual; 506 ctsio->residual = msg_info->scsi.residual; 507 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 508 sizeof(ctsio->sense_data)); 509 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 510 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 511 ctl_enqueue_isc((union ctl_io *)ctsio); 512 } 513 514 static void 515 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 516 union ctl_ha_msg *msg_info) 517 { 518 struct ctl_scsiio *ctsio; 519 520 if (msg_info->hdr.serializing_sc == NULL) { 521 printf("%s: serializing_sc == NULL!\n", __func__); 522 /* XXX KDM now what? */ 523 return; 524 } 525 526 ctsio = &msg_info->hdr.serializing_sc->scsiio; 527 #if 0 528 /* 529 * Attempt to catch the situation where an I/O has 530 * been freed, and we're using it again. 531 */ 532 if (ctsio->io_hdr.io_type == 0xff) { 533 union ctl_io *tmp_io; 534 tmp_io = (union ctl_io *)ctsio; 535 printf("%s: %p use after free!\n", __func__, 536 ctsio); 537 printf("%s: type %d msg %d cdb %x iptl: " 538 "%d:%d:%d:%d tag 0x%04x " 539 "flag %#x status %x\n", 540 __func__, 541 tmp_io->io_hdr.io_type, 542 tmp_io->io_hdr.msg_type, 543 tmp_io->scsiio.cdb[0], 544 tmp_io->io_hdr.nexus.initid.id, 545 tmp_io->io_hdr.nexus.targ_port, 546 tmp_io->io_hdr.nexus.targ_target.id, 547 tmp_io->io_hdr.nexus.targ_lun, 548 (tmp_io->io_hdr.io_type == 549 CTL_IO_TASK) ? 550 tmp_io->taskio.tag_num : 551 tmp_io->scsiio.tag_num, 552 tmp_io->io_hdr.flags, 553 tmp_io->io_hdr.status); 554 } 555 #endif 556 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 557 ctl_enqueue_isc((union ctl_io *)ctsio); 558 } 559 560 /* 561 * ISC (Inter Shelf Communication) event handler. Events from the HA 562 * subsystem come in here. 563 */ 564 static void 565 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 566 { 567 struct ctl_softc *ctl_softc; 568 union ctl_io *io; 569 struct ctl_prio *presio; 570 ctl_ha_status isc_status; 571 572 ctl_softc = control_softc; 573 io = NULL; 574 575 576 #if 0 577 printf("CTL: Isc Msg event %d\n", event); 578 #endif 579 if (event == CTL_HA_EVT_MSG_RECV) { 580 union ctl_ha_msg msg_info; 581 582 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 583 sizeof(msg_info), /*wait*/ 0); 584 #if 0 585 printf("CTL: msg_type %d\n", msg_info.msg_type); 586 #endif 587 if (isc_status != 0) { 588 printf("Error receiving message, status = %d\n", 589 isc_status); 590 return; 591 } 592 593 switch (msg_info.hdr.msg_type) { 594 case CTL_MSG_SERIALIZE: 595 #if 0 596 printf("Serialize\n"); 597 #endif 598 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 599 if (io == NULL) { 600 printf("ctl_isc_event_handler: can't allocate " 601 "ctl_io!\n"); 602 /* Bad Juju */ 603 /* Need to set busy and send msg back */ 604 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 605 msg_info.hdr.status = CTL_SCSI_ERROR; 606 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 607 msg_info.scsi.sense_len = 0; 608 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 609 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 610 } 611 goto bailout; 612 } 613 ctl_zero_io(io); 614 // populate ctsio from msg_info 615 io->io_hdr.io_type = CTL_IO_SCSI; 616 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 617 io->io_hdr.original_sc = msg_info.hdr.original_sc; 618 #if 0 619 printf("pOrig %x\n", (int)msg_info.original_sc); 620 #endif 621 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 622 CTL_FLAG_IO_ACTIVE; 623 /* 624 * If we're in serialization-only mode, we don't 625 * want to go through full done processing. Thus 626 * the COPY flag. 627 * 628 * XXX KDM add another flag that is more specific. 629 */ 630 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 631 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 632 io->io_hdr.nexus = msg_info.hdr.nexus; 633 #if 0 634 printf("targ %d, port %d, iid %d, lun %d\n", 635 io->io_hdr.nexus.targ_target.id, 636 io->io_hdr.nexus.targ_port, 637 io->io_hdr.nexus.initid.id, 638 io->io_hdr.nexus.targ_lun); 639 #endif 640 io->scsiio.tag_num = msg_info.scsi.tag_num; 641 io->scsiio.tag_type = msg_info.scsi.tag_type; 642 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 643 CTL_MAX_CDBLEN); 644 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 645 const struct ctl_cmd_entry *entry; 646 647 entry = ctl_get_cmd_entry(&io->scsiio); 648 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 649 io->io_hdr.flags |= 650 entry->flags & CTL_FLAG_DATA_MASK; 651 } 652 ctl_enqueue_isc(io); 653 break; 654 655 /* Performed on the Originating SC, XFER mode only */ 656 case CTL_MSG_DATAMOVE: { 657 struct ctl_sg_entry *sgl; 658 int i, j; 659 660 io = msg_info.hdr.original_sc; 661 if (io == NULL) { 662 printf("%s: original_sc == NULL!\n", __func__); 663 /* XXX KDM do something here */ 664 break; 665 } 666 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 667 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 668 /* 669 * Keep track of this, we need to send it back over 670 * when the datamove is complete. 671 */ 672 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 673 674 if (msg_info.dt.sg_sequence == 0) { 675 /* 676 * XXX KDM we use the preallocated S/G list 677 * here, but we'll need to change this to 678 * dynamic allocation if we need larger S/G 679 * lists. 680 */ 681 if (msg_info.dt.kern_sg_entries > 682 sizeof(io->io_hdr.remote_sglist) / 683 sizeof(io->io_hdr.remote_sglist[0])) { 684 printf("%s: number of S/G entries " 685 "needed %u > allocated num %zd\n", 686 __func__, 687 msg_info.dt.kern_sg_entries, 688 sizeof(io->io_hdr.remote_sglist)/ 689 sizeof(io->io_hdr.remote_sglist[0])); 690 691 /* 692 * XXX KDM send a message back to 693 * the other side to shut down the 694 * DMA. The error will come back 695 * through via the normal channel. 696 */ 697 break; 698 } 699 sgl = io->io_hdr.remote_sglist; 700 memset(sgl, 0, 701 sizeof(io->io_hdr.remote_sglist)); 702 703 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 704 705 io->scsiio.kern_sg_entries = 706 msg_info.dt.kern_sg_entries; 707 io->scsiio.rem_sg_entries = 708 msg_info.dt.kern_sg_entries; 709 io->scsiio.kern_data_len = 710 msg_info.dt.kern_data_len; 711 io->scsiio.kern_total_len = 712 msg_info.dt.kern_total_len; 713 io->scsiio.kern_data_resid = 714 msg_info.dt.kern_data_resid; 715 io->scsiio.kern_rel_offset = 716 msg_info.dt.kern_rel_offset; 717 /* 718 * Clear out per-DMA flags. 719 */ 720 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 721 /* 722 * Add per-DMA flags that are set for this 723 * particular DMA request. 724 */ 725 io->io_hdr.flags |= msg_info.dt.flags & 726 CTL_FLAG_RDMA_MASK; 727 } else 728 sgl = (struct ctl_sg_entry *) 729 io->scsiio.kern_data_ptr; 730 731 for (i = msg_info.dt.sent_sg_entries, j = 0; 732 i < (msg_info.dt.sent_sg_entries + 733 msg_info.dt.cur_sg_entries); i++, j++) { 734 sgl[i].addr = msg_info.dt.sg_list[j].addr; 735 sgl[i].len = msg_info.dt.sg_list[j].len; 736 737 #if 0 738 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 739 __func__, 740 msg_info.dt.sg_list[j].addr, 741 msg_info.dt.sg_list[j].len, 742 sgl[i].addr, sgl[i].len, j, i); 743 #endif 744 } 745 #if 0 746 memcpy(&sgl[msg_info.dt.sent_sg_entries], 747 msg_info.dt.sg_list, 748 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 749 #endif 750 751 /* 752 * If this is the last piece of the I/O, we've got 753 * the full S/G list. Queue processing in the thread. 754 * Otherwise wait for the next piece. 755 */ 756 if (msg_info.dt.sg_last != 0) 757 ctl_enqueue_isc(io); 758 break; 759 } 760 /* Performed on the Serializing (primary) SC, XFER mode only */ 761 case CTL_MSG_DATAMOVE_DONE: { 762 if (msg_info.hdr.serializing_sc == NULL) { 763 printf("%s: serializing_sc == NULL!\n", 764 __func__); 765 /* XXX KDM now what? */ 766 break; 767 } 768 /* 769 * We grab the sense information here in case 770 * there was a failure, so we can return status 771 * back to the initiator. 772 */ 773 io = msg_info.hdr.serializing_sc; 774 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 775 io->io_hdr.status = msg_info.hdr.status; 776 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 777 io->scsiio.sense_len = msg_info.scsi.sense_len; 778 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 779 io->io_hdr.port_status = msg_info.scsi.fetd_status; 780 io->scsiio.residual = msg_info.scsi.residual; 781 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 782 sizeof(io->scsiio.sense_data)); 783 ctl_enqueue_isc(io); 784 break; 785 } 786 787 /* Preformed on Originating SC, SER_ONLY mode */ 788 case CTL_MSG_R2R: 789 io = msg_info.hdr.original_sc; 790 if (io == NULL) { 791 printf("%s: Major Bummer\n", __func__); 792 return; 793 } else { 794 #if 0 795 printf("pOrig %x\n",(int) ctsio); 796 #endif 797 } 798 io->io_hdr.msg_type = CTL_MSG_R2R; 799 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 800 ctl_enqueue_isc(io); 801 break; 802 803 /* 804 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 805 * mode. 806 * Performed on the Originating (i.e. secondary) SC in XFER 807 * mode 808 */ 809 case CTL_MSG_FINISH_IO: 810 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 811 ctl_isc_handler_finish_xfer(ctl_softc, 812 &msg_info); 813 else 814 ctl_isc_handler_finish_ser_only(ctl_softc, 815 &msg_info); 816 break; 817 818 /* Preformed on Originating SC */ 819 case CTL_MSG_BAD_JUJU: 820 io = msg_info.hdr.original_sc; 821 if (io == NULL) { 822 printf("%s: Bad JUJU!, original_sc is NULL!\n", 823 __func__); 824 break; 825 } 826 ctl_copy_sense_data(&msg_info, io); 827 /* 828 * IO should have already been cleaned up on other 829 * SC so clear this flag so we won't send a message 830 * back to finish the IO there. 831 */ 832 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 833 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 834 835 /* io = msg_info.hdr.serializing_sc; */ 836 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 837 ctl_enqueue_isc(io); 838 break; 839 840 /* Handle resets sent from the other side */ 841 case CTL_MSG_MANAGE_TASKS: { 842 struct ctl_taskio *taskio; 843 taskio = (struct ctl_taskio *)ctl_alloc_io( 844 (void *)ctl_softc->othersc_pool); 845 if (taskio == NULL) { 846 printf("ctl_isc_event_handler: can't allocate " 847 "ctl_io!\n"); 848 /* Bad Juju */ 849 /* should I just call the proper reset func 850 here??? */ 851 goto bailout; 852 } 853 ctl_zero_io((union ctl_io *)taskio); 854 taskio->io_hdr.io_type = CTL_IO_TASK; 855 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 856 taskio->io_hdr.nexus = msg_info.hdr.nexus; 857 taskio->task_action = msg_info.task.task_action; 858 taskio->tag_num = msg_info.task.tag_num; 859 taskio->tag_type = msg_info.task.tag_type; 860 #ifdef CTL_TIME_IO 861 taskio->io_hdr.start_time = time_uptime; 862 getbintime(&taskio->io_hdr.start_bt); 863 #if 0 864 cs_prof_gettime(&taskio->io_hdr.start_ticks); 865 #endif 866 #endif /* CTL_TIME_IO */ 867 ctl_run_task((union ctl_io *)taskio); 868 break; 869 } 870 /* Persistent Reserve action which needs attention */ 871 case CTL_MSG_PERS_ACTION: 872 presio = (struct ctl_prio *)ctl_alloc_io( 873 (void *)ctl_softc->othersc_pool); 874 if (presio == NULL) { 875 printf("ctl_isc_event_handler: can't allocate " 876 "ctl_io!\n"); 877 /* Bad Juju */ 878 /* Need to set busy and send msg back */ 879 goto bailout; 880 } 881 ctl_zero_io((union ctl_io *)presio); 882 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 883 presio->pr_msg = msg_info.pr; 884 ctl_enqueue_isc((union ctl_io *)presio); 885 break; 886 case CTL_MSG_SYNC_FE: 887 rcv_sync_msg = 1; 888 break; 889 case CTL_MSG_APS_LOCK: { 890 // It's quicker to execute this then to 891 // queue it. 892 struct ctl_lun *lun; 893 struct ctl_page_index *page_index; 894 struct copan_aps_subpage *current_sp; 895 uint32_t targ_lun; 896 897 targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 898 lun = ctl_softc->ctl_luns[targ_lun]; 899 mtx_lock(&lun->lun_lock); 900 page_index = &lun->mode_pages.index[index_to_aps_page]; 901 current_sp = (struct copan_aps_subpage *) 902 (page_index->page_data + 903 (page_index->page_len * CTL_PAGE_CURRENT)); 904 905 current_sp->lock_active = msg_info.aps.lock_flag; 906 mtx_unlock(&lun->lun_lock); 907 break; 908 } 909 default: 910 printf("How did I get here?\n"); 911 } 912 } else if (event == CTL_HA_EVT_MSG_SENT) { 913 if (param != CTL_HA_STATUS_SUCCESS) { 914 printf("Bad status from ctl_ha_msg_send status %d\n", 915 param); 916 } 917 return; 918 } else if (event == CTL_HA_EVT_DISCONNECT) { 919 printf("CTL: Got a disconnect from Isc\n"); 920 return; 921 } else { 922 printf("ctl_isc_event_handler: Unknown event %d\n", event); 923 return; 924 } 925 926 bailout: 927 return; 928 } 929 930 static void 931 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 932 { 933 struct scsi_sense_data *sense; 934 935 sense = &dest->scsiio.sense_data; 936 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 937 dest->scsiio.scsi_status = src->scsi.scsi_status; 938 dest->scsiio.sense_len = src->scsi.sense_len; 939 dest->io_hdr.status = src->hdr.status; 940 } 941 942 static int 943 ctl_init(void) 944 { 945 struct ctl_softc *softc; 946 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 947 struct ctl_port *port; 948 uint8_t sc_id =0; 949 int i, error, retval; 950 //int isc_retval; 951 952 retval = 0; 953 ctl_pause_rtr = 0; 954 rcv_sync_msg = 0; 955 956 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 957 M_WAITOK | M_ZERO); 958 softc = control_softc; 959 960 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 961 "cam/ctl"); 962 963 softc->dev->si_drv1 = softc; 964 965 /* 966 * By default, return a "bad LUN" peripheral qualifier for unknown 967 * LUNs. The user can override this default using the tunable or 968 * sysctl. See the comment in ctl_inquiry_std() for more details. 969 */ 970 softc->inquiry_pq_no_lun = 1; 971 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 972 &softc->inquiry_pq_no_lun); 973 sysctl_ctx_init(&softc->sysctl_ctx); 974 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 975 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 976 CTLFLAG_RD, 0, "CAM Target Layer"); 977 978 if (softc->sysctl_tree == NULL) { 979 printf("%s: unable to allocate sysctl tree\n", __func__); 980 destroy_dev(softc->dev); 981 free(control_softc, M_DEVBUF); 982 control_softc = NULL; 983 return (ENOMEM); 984 } 985 986 SYSCTL_ADD_INT(&softc->sysctl_ctx, 987 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 988 "inquiry_pq_no_lun", CTLFLAG_RW, 989 &softc->inquiry_pq_no_lun, 0, 990 "Report no lun possible for invalid LUNs"); 991 992 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 993 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 994 softc->open_count = 0; 995 996 /* 997 * Default to actually sending a SYNCHRONIZE CACHE command down to 998 * the drive. 999 */ 1000 softc->flags = CTL_FLAG_REAL_SYNC; 1001 1002 /* 1003 * In Copan's HA scheme, the "master" and "slave" roles are 1004 * figured out through the slot the controller is in. Although it 1005 * is an active/active system, someone has to be in charge. 1006 */ 1007 #ifdef NEEDTOPORT 1008 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1009 #endif 1010 1011 if (sc_id == 0) { 1012 softc->flags |= CTL_FLAG_MASTER_SHELF; 1013 persis_offset = 0; 1014 } else 1015 persis_offset = CTL_MAX_INITIATORS; 1016 1017 /* 1018 * XXX KDM need to figure out where we want to get our target ID 1019 * and WWID. Is it different on each port? 1020 */ 1021 softc->target.id = 0; 1022 softc->target.wwid[0] = 0x12345678; 1023 softc->target.wwid[1] = 0x87654321; 1024 STAILQ_INIT(&softc->lun_list); 1025 STAILQ_INIT(&softc->pending_lun_queue); 1026 STAILQ_INIT(&softc->fe_list); 1027 STAILQ_INIT(&softc->port_list); 1028 STAILQ_INIT(&softc->be_list); 1029 STAILQ_INIT(&softc->io_pools); 1030 ctl_tpc_init(softc); 1031 1032 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1033 &internal_pool)!= 0){ 1034 printf("ctl: can't allocate %d entry internal pool, " 1035 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1036 return (ENOMEM); 1037 } 1038 1039 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1040 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1041 printf("ctl: can't allocate %d entry emergency pool, " 1042 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1043 ctl_pool_free(internal_pool); 1044 return (ENOMEM); 1045 } 1046 1047 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1048 &other_pool) != 0) 1049 { 1050 printf("ctl: can't allocate %d entry other SC pool, " 1051 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1052 ctl_pool_free(internal_pool); 1053 ctl_pool_free(emergency_pool); 1054 return (ENOMEM); 1055 } 1056 1057 softc->internal_pool = internal_pool; 1058 softc->emergency_pool = emergency_pool; 1059 softc->othersc_pool = other_pool; 1060 1061 if (worker_threads <= 0) 1062 worker_threads = max(1, mp_ncpus / 4); 1063 if (worker_threads > CTL_MAX_THREADS) 1064 worker_threads = CTL_MAX_THREADS; 1065 1066 for (i = 0; i < worker_threads; i++) { 1067 struct ctl_thread *thr = &softc->threads[i]; 1068 1069 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1070 thr->ctl_softc = softc; 1071 STAILQ_INIT(&thr->incoming_queue); 1072 STAILQ_INIT(&thr->rtr_queue); 1073 STAILQ_INIT(&thr->done_queue); 1074 STAILQ_INIT(&thr->isc_queue); 1075 1076 error = kproc_kthread_add(ctl_work_thread, thr, 1077 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1078 if (error != 0) { 1079 printf("error creating CTL work thread!\n"); 1080 ctl_pool_free(internal_pool); 1081 ctl_pool_free(emergency_pool); 1082 ctl_pool_free(other_pool); 1083 return (error); 1084 } 1085 } 1086 error = kproc_kthread_add(ctl_lun_thread, softc, 1087 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1088 if (error != 0) { 1089 printf("error creating CTL lun thread!\n"); 1090 ctl_pool_free(internal_pool); 1091 ctl_pool_free(emergency_pool); 1092 ctl_pool_free(other_pool); 1093 return (error); 1094 } 1095 if (bootverbose) 1096 printf("ctl: CAM Target Layer loaded\n"); 1097 1098 /* 1099 * Initialize the ioctl front end. 1100 */ 1101 ctl_frontend_register(&ioctl_frontend); 1102 port = &softc->ioctl_info.port; 1103 port->frontend = &ioctl_frontend; 1104 sprintf(softc->ioctl_info.port_name, "ioctl"); 1105 port->port_type = CTL_PORT_IOCTL; 1106 port->num_requested_ctl_io = 100; 1107 port->port_name = softc->ioctl_info.port_name; 1108 port->port_online = ctl_ioctl_online; 1109 port->port_offline = ctl_ioctl_offline; 1110 port->onoff_arg = &softc->ioctl_info; 1111 port->lun_enable = ctl_ioctl_lun_enable; 1112 port->lun_disable = ctl_ioctl_lun_disable; 1113 port->targ_lun_arg = &softc->ioctl_info; 1114 port->fe_datamove = ctl_ioctl_datamove; 1115 port->fe_done = ctl_ioctl_done; 1116 port->max_targets = 15; 1117 port->max_target_id = 15; 1118 1119 if (ctl_port_register(&softc->ioctl_info.port, 1120 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1121 printf("ctl: ioctl front end registration failed, will " 1122 "continue anyway\n"); 1123 } 1124 1125 #ifdef CTL_IO_DELAY 1126 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1127 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1128 sizeof(struct callout), CTL_TIMER_BYTES); 1129 return (EINVAL); 1130 } 1131 #endif /* CTL_IO_DELAY */ 1132 1133 return (0); 1134 } 1135 1136 void 1137 ctl_shutdown(void) 1138 { 1139 struct ctl_softc *softc; 1140 struct ctl_lun *lun, *next_lun; 1141 struct ctl_io_pool *pool; 1142 1143 softc = (struct ctl_softc *)control_softc; 1144 1145 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1146 printf("ctl: ioctl front end deregistration failed\n"); 1147 1148 mtx_lock(&softc->ctl_lock); 1149 1150 /* 1151 * Free up each LUN. 1152 */ 1153 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1154 next_lun = STAILQ_NEXT(lun, links); 1155 ctl_free_lun(lun); 1156 } 1157 1158 mtx_unlock(&softc->ctl_lock); 1159 1160 ctl_frontend_deregister(&ioctl_frontend); 1161 1162 /* 1163 * This will rip the rug out from under any FETDs or anyone else 1164 * that has a pool allocated. Since we increment our module 1165 * refcount any time someone outside the main CTL module allocates 1166 * a pool, we shouldn't have any problems here. The user won't be 1167 * able to unload the CTL module until client modules have 1168 * successfully unloaded. 1169 */ 1170 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1171 ctl_pool_free(pool); 1172 1173 #if 0 1174 ctl_shutdown_thread(softc->work_thread); 1175 mtx_destroy(&softc->queue_lock); 1176 #endif 1177 1178 ctl_tpc_shutdown(softc); 1179 mtx_destroy(&softc->pool_lock); 1180 mtx_destroy(&softc->ctl_lock); 1181 1182 destroy_dev(softc->dev); 1183 1184 sysctl_ctx_free(&softc->sysctl_ctx); 1185 1186 free(control_softc, M_DEVBUF); 1187 control_softc = NULL; 1188 1189 if (bootverbose) 1190 printf("ctl: CAM Target Layer unloaded\n"); 1191 } 1192 1193 static int 1194 ctl_module_event_handler(module_t mod, int what, void *arg) 1195 { 1196 1197 switch (what) { 1198 case MOD_LOAD: 1199 return (ctl_init()); 1200 case MOD_UNLOAD: 1201 return (EBUSY); 1202 default: 1203 return (EOPNOTSUPP); 1204 } 1205 } 1206 1207 /* 1208 * XXX KDM should we do some access checks here? Bump a reference count to 1209 * prevent a CTL module from being unloaded while someone has it open? 1210 */ 1211 static int 1212 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1213 { 1214 return (0); 1215 } 1216 1217 static int 1218 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1219 { 1220 return (0); 1221 } 1222 1223 int 1224 ctl_port_enable(ctl_port_type port_type) 1225 { 1226 struct ctl_softc *softc; 1227 struct ctl_port *port; 1228 1229 if (ctl_is_single == 0) { 1230 union ctl_ha_msg msg_info; 1231 int isc_retval; 1232 1233 #if 0 1234 printf("%s: HA mode, synchronizing frontend enable\n", 1235 __func__); 1236 #endif 1237 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1238 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1239 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1240 printf("Sync msg send error retval %d\n", isc_retval); 1241 } 1242 if (!rcv_sync_msg) { 1243 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1244 sizeof(msg_info), 1); 1245 } 1246 #if 0 1247 printf("CTL:Frontend Enable\n"); 1248 } else { 1249 printf("%s: single mode, skipping frontend synchronization\n", 1250 __func__); 1251 #endif 1252 } 1253 1254 softc = control_softc; 1255 1256 STAILQ_FOREACH(port, &softc->port_list, links) { 1257 if (port_type & port->port_type) 1258 { 1259 #if 0 1260 printf("port %d\n", port->targ_port); 1261 #endif 1262 ctl_port_online(port); 1263 } 1264 } 1265 1266 return (0); 1267 } 1268 1269 int 1270 ctl_port_disable(ctl_port_type port_type) 1271 { 1272 struct ctl_softc *softc; 1273 struct ctl_port *port; 1274 1275 softc = control_softc; 1276 1277 STAILQ_FOREACH(port, &softc->port_list, links) { 1278 if (port_type & port->port_type) 1279 ctl_port_offline(port); 1280 } 1281 1282 return (0); 1283 } 1284 1285 /* 1286 * Returns 0 for success, 1 for failure. 1287 * Currently the only failure mode is if there aren't enough entries 1288 * allocated. So, in case of a failure, look at num_entries_dropped, 1289 * reallocate and try again. 1290 */ 1291 int 1292 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1293 int *num_entries_filled, int *num_entries_dropped, 1294 ctl_port_type port_type, int no_virtual) 1295 { 1296 struct ctl_softc *softc; 1297 struct ctl_port *port; 1298 int entries_dropped, entries_filled; 1299 int retval; 1300 int i; 1301 1302 softc = control_softc; 1303 1304 retval = 0; 1305 entries_filled = 0; 1306 entries_dropped = 0; 1307 1308 i = 0; 1309 mtx_lock(&softc->ctl_lock); 1310 STAILQ_FOREACH(port, &softc->port_list, links) { 1311 struct ctl_port_entry *entry; 1312 1313 if ((port->port_type & port_type) == 0) 1314 continue; 1315 1316 if ((no_virtual != 0) 1317 && (port->virtual_port != 0)) 1318 continue; 1319 1320 if (entries_filled >= num_entries_alloced) { 1321 entries_dropped++; 1322 continue; 1323 } 1324 entry = &entries[i]; 1325 1326 entry->port_type = port->port_type; 1327 strlcpy(entry->port_name, port->port_name, 1328 sizeof(entry->port_name)); 1329 entry->physical_port = port->physical_port; 1330 entry->virtual_port = port->virtual_port; 1331 entry->wwnn = port->wwnn; 1332 entry->wwpn = port->wwpn; 1333 1334 i++; 1335 entries_filled++; 1336 } 1337 1338 mtx_unlock(&softc->ctl_lock); 1339 1340 if (entries_dropped > 0) 1341 retval = 1; 1342 1343 *num_entries_dropped = entries_dropped; 1344 *num_entries_filled = entries_filled; 1345 1346 return (retval); 1347 } 1348 1349 static void 1350 ctl_ioctl_online(void *arg) 1351 { 1352 struct ctl_ioctl_info *ioctl_info; 1353 1354 ioctl_info = (struct ctl_ioctl_info *)arg; 1355 1356 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1357 } 1358 1359 static void 1360 ctl_ioctl_offline(void *arg) 1361 { 1362 struct ctl_ioctl_info *ioctl_info; 1363 1364 ioctl_info = (struct ctl_ioctl_info *)arg; 1365 1366 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1367 } 1368 1369 /* 1370 * Remove an initiator by port number and initiator ID. 1371 * Returns 0 for success, -1 for failure. 1372 */ 1373 int 1374 ctl_remove_initiator(struct ctl_port *port, int iid) 1375 { 1376 struct ctl_softc *softc = control_softc; 1377 1378 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1379 1380 if (iid > CTL_MAX_INIT_PER_PORT) { 1381 printf("%s: initiator ID %u > maximun %u!\n", 1382 __func__, iid, CTL_MAX_INIT_PER_PORT); 1383 return (-1); 1384 } 1385 1386 mtx_lock(&softc->ctl_lock); 1387 port->wwpn_iid[iid].in_use--; 1388 port->wwpn_iid[iid].last_use = time_uptime; 1389 mtx_unlock(&softc->ctl_lock); 1390 1391 return (0); 1392 } 1393 1394 /* 1395 * Add an initiator to the initiator map. 1396 * Returns iid for success, < 0 for failure. 1397 */ 1398 int 1399 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1400 { 1401 struct ctl_softc *softc = control_softc; 1402 time_t best_time; 1403 int i, best; 1404 1405 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1406 1407 if (iid >= CTL_MAX_INIT_PER_PORT) { 1408 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1409 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1410 free(name, M_CTL); 1411 return (-1); 1412 } 1413 1414 mtx_lock(&softc->ctl_lock); 1415 1416 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1417 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1418 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1419 iid = i; 1420 break; 1421 } 1422 if (name != NULL && port->wwpn_iid[i].name != NULL && 1423 strcmp(name, port->wwpn_iid[i].name) == 0) { 1424 iid = i; 1425 break; 1426 } 1427 } 1428 } 1429 1430 if (iid < 0) { 1431 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1432 if (port->wwpn_iid[i].in_use == 0 && 1433 port->wwpn_iid[i].wwpn == 0 && 1434 port->wwpn_iid[i].name == NULL) { 1435 iid = i; 1436 break; 1437 } 1438 } 1439 } 1440 1441 if (iid < 0) { 1442 best = -1; 1443 best_time = INT32_MAX; 1444 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1445 if (port->wwpn_iid[i].in_use == 0) { 1446 if (port->wwpn_iid[i].last_use < best_time) { 1447 best = i; 1448 best_time = port->wwpn_iid[i].last_use; 1449 } 1450 } 1451 } 1452 iid = best; 1453 } 1454 1455 if (iid < 0) { 1456 mtx_unlock(&softc->ctl_lock); 1457 free(name, M_CTL); 1458 return (-2); 1459 } 1460 1461 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1462 /* 1463 * This is not an error yet. 1464 */ 1465 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1466 #if 0 1467 printf("%s: port %d iid %u WWPN %#jx arrived" 1468 " again\n", __func__, port->targ_port, 1469 iid, (uintmax_t)wwpn); 1470 #endif 1471 goto take; 1472 } 1473 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1474 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1475 #if 0 1476 printf("%s: port %d iid %u name '%s' arrived" 1477 " again\n", __func__, port->targ_port, 1478 iid, name); 1479 #endif 1480 goto take; 1481 } 1482 1483 /* 1484 * This is an error, but what do we do about it? The 1485 * driver is telling us we have a new WWPN for this 1486 * initiator ID, so we pretty much need to use it. 1487 */ 1488 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1489 " but WWPN %#jx '%s' is still at that address\n", 1490 __func__, port->targ_port, iid, wwpn, name, 1491 (uintmax_t)port->wwpn_iid[iid].wwpn, 1492 port->wwpn_iid[iid].name); 1493 1494 /* 1495 * XXX KDM clear have_ca and ua_pending on each LUN for 1496 * this initiator. 1497 */ 1498 } 1499 take: 1500 free(port->wwpn_iid[iid].name, M_CTL); 1501 port->wwpn_iid[iid].name = name; 1502 port->wwpn_iid[iid].wwpn = wwpn; 1503 port->wwpn_iid[iid].in_use++; 1504 mtx_unlock(&softc->ctl_lock); 1505 1506 return (iid); 1507 } 1508 1509 static int 1510 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1511 { 1512 int len; 1513 1514 switch (port->port_type) { 1515 case CTL_PORT_FC: 1516 { 1517 struct scsi_transportid_fcp *id = 1518 (struct scsi_transportid_fcp *)buf; 1519 if (port->wwpn_iid[iid].wwpn == 0) 1520 return (0); 1521 memset(id, 0, sizeof(*id)); 1522 id->format_protocol = SCSI_PROTO_FC; 1523 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1524 return (sizeof(*id)); 1525 } 1526 case CTL_PORT_ISCSI: 1527 { 1528 struct scsi_transportid_iscsi_port *id = 1529 (struct scsi_transportid_iscsi_port *)buf; 1530 if (port->wwpn_iid[iid].name == NULL) 1531 return (0); 1532 memset(id, 0, 256); 1533 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1534 SCSI_PROTO_ISCSI; 1535 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1536 len = roundup2(min(len, 252), 4); 1537 scsi_ulto2b(len, id->additional_length); 1538 return (sizeof(*id) + len); 1539 } 1540 case CTL_PORT_SAS: 1541 { 1542 struct scsi_transportid_sas *id = 1543 (struct scsi_transportid_sas *)buf; 1544 if (port->wwpn_iid[iid].wwpn == 0) 1545 return (0); 1546 memset(id, 0, sizeof(*id)); 1547 id->format_protocol = SCSI_PROTO_SAS; 1548 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1549 return (sizeof(*id)); 1550 } 1551 default: 1552 { 1553 struct scsi_transportid_spi *id = 1554 (struct scsi_transportid_spi *)buf; 1555 memset(id, 0, sizeof(*id)); 1556 id->format_protocol = SCSI_PROTO_SPI; 1557 scsi_ulto2b(iid, id->scsi_addr); 1558 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1559 return (sizeof(*id)); 1560 } 1561 } 1562 } 1563 1564 static int 1565 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1566 { 1567 return (0); 1568 } 1569 1570 static int 1571 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1572 { 1573 return (0); 1574 } 1575 1576 /* 1577 * Data movement routine for the CTL ioctl frontend port. 1578 */ 1579 static int 1580 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1581 { 1582 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1583 struct ctl_sg_entry ext_entry, kern_entry; 1584 int ext_sglen, ext_sg_entries, kern_sg_entries; 1585 int ext_sg_start, ext_offset; 1586 int len_to_copy, len_copied; 1587 int kern_watermark, ext_watermark; 1588 int ext_sglist_malloced; 1589 int i, j; 1590 1591 ext_sglist_malloced = 0; 1592 ext_sg_start = 0; 1593 ext_offset = 0; 1594 1595 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1596 1597 /* 1598 * If this flag is set, fake the data transfer. 1599 */ 1600 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1601 ctsio->ext_data_filled = ctsio->ext_data_len; 1602 goto bailout; 1603 } 1604 1605 /* 1606 * To simplify things here, if we have a single buffer, stick it in 1607 * a S/G entry and just make it a single entry S/G list. 1608 */ 1609 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1610 int len_seen; 1611 1612 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1613 1614 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1615 M_WAITOK); 1616 ext_sglist_malloced = 1; 1617 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1618 ext_sglen) != 0) { 1619 ctl_set_internal_failure(ctsio, 1620 /*sks_valid*/ 0, 1621 /*retry_count*/ 0); 1622 goto bailout; 1623 } 1624 ext_sg_entries = ctsio->ext_sg_entries; 1625 len_seen = 0; 1626 for (i = 0; i < ext_sg_entries; i++) { 1627 if ((len_seen + ext_sglist[i].len) >= 1628 ctsio->ext_data_filled) { 1629 ext_sg_start = i; 1630 ext_offset = ctsio->ext_data_filled - len_seen; 1631 break; 1632 } 1633 len_seen += ext_sglist[i].len; 1634 } 1635 } else { 1636 ext_sglist = &ext_entry; 1637 ext_sglist->addr = ctsio->ext_data_ptr; 1638 ext_sglist->len = ctsio->ext_data_len; 1639 ext_sg_entries = 1; 1640 ext_sg_start = 0; 1641 ext_offset = ctsio->ext_data_filled; 1642 } 1643 1644 if (ctsio->kern_sg_entries > 0) { 1645 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1646 kern_sg_entries = ctsio->kern_sg_entries; 1647 } else { 1648 kern_sglist = &kern_entry; 1649 kern_sglist->addr = ctsio->kern_data_ptr; 1650 kern_sglist->len = ctsio->kern_data_len; 1651 kern_sg_entries = 1; 1652 } 1653 1654 1655 kern_watermark = 0; 1656 ext_watermark = ext_offset; 1657 len_copied = 0; 1658 for (i = ext_sg_start, j = 0; 1659 i < ext_sg_entries && j < kern_sg_entries;) { 1660 uint8_t *ext_ptr, *kern_ptr; 1661 1662 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1663 kern_sglist[j].len - kern_watermark); 1664 1665 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1666 ext_ptr = ext_ptr + ext_watermark; 1667 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1668 /* 1669 * XXX KDM fix this! 1670 */ 1671 panic("need to implement bus address support"); 1672 #if 0 1673 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1674 #endif 1675 } else 1676 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1677 kern_ptr = kern_ptr + kern_watermark; 1678 1679 kern_watermark += len_to_copy; 1680 ext_watermark += len_to_copy; 1681 1682 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1683 CTL_FLAG_DATA_IN) { 1684 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1685 "bytes to user\n", len_to_copy)); 1686 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1687 "to %p\n", kern_ptr, ext_ptr)); 1688 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1689 ctl_set_internal_failure(ctsio, 1690 /*sks_valid*/ 0, 1691 /*retry_count*/ 0); 1692 goto bailout; 1693 } 1694 } else { 1695 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1696 "bytes from user\n", len_to_copy)); 1697 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1698 "to %p\n", ext_ptr, kern_ptr)); 1699 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1700 ctl_set_internal_failure(ctsio, 1701 /*sks_valid*/ 0, 1702 /*retry_count*/0); 1703 goto bailout; 1704 } 1705 } 1706 1707 len_copied += len_to_copy; 1708 1709 if (ext_sglist[i].len == ext_watermark) { 1710 i++; 1711 ext_watermark = 0; 1712 } 1713 1714 if (kern_sglist[j].len == kern_watermark) { 1715 j++; 1716 kern_watermark = 0; 1717 } 1718 } 1719 1720 ctsio->ext_data_filled += len_copied; 1721 1722 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1723 "kern_sg_entries: %d\n", ext_sg_entries, 1724 kern_sg_entries)); 1725 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1726 "kern_data_len = %d\n", ctsio->ext_data_len, 1727 ctsio->kern_data_len)); 1728 1729 1730 /* XXX KDM set residual?? */ 1731 bailout: 1732 1733 if (ext_sglist_malloced != 0) 1734 free(ext_sglist, M_CTL); 1735 1736 return (CTL_RETVAL_COMPLETE); 1737 } 1738 1739 /* 1740 * Serialize a command that went down the "wrong" side, and so was sent to 1741 * this controller for execution. The logic is a little different than the 1742 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1743 * sent back to the other side, but in the success case, we execute the 1744 * command on this side (XFER mode) or tell the other side to execute it 1745 * (SER_ONLY mode). 1746 */ 1747 static int 1748 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1749 { 1750 struct ctl_softc *ctl_softc; 1751 union ctl_ha_msg msg_info; 1752 struct ctl_lun *lun; 1753 int retval = 0; 1754 uint32_t targ_lun; 1755 1756 ctl_softc = control_softc; 1757 1758 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1759 lun = ctl_softc->ctl_luns[targ_lun]; 1760 if (lun==NULL) 1761 { 1762 /* 1763 * Why isn't LUN defined? The other side wouldn't 1764 * send a cmd if the LUN is undefined. 1765 */ 1766 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1767 1768 /* "Logical unit not supported" */ 1769 ctl_set_sense_data(&msg_info.scsi.sense_data, 1770 lun, 1771 /*sense_format*/SSD_TYPE_NONE, 1772 /*current_error*/ 1, 1773 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1774 /*asc*/ 0x25, 1775 /*ascq*/ 0x00, 1776 SSD_ELEM_NONE); 1777 1778 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1779 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1780 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1781 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1782 msg_info.hdr.serializing_sc = NULL; 1783 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1784 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1785 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1786 } 1787 return(1); 1788 1789 } 1790 1791 mtx_lock(&lun->lun_lock); 1792 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1793 1794 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1795 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1796 ooa_links))) { 1797 case CTL_ACTION_BLOCK: 1798 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1799 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1800 blocked_links); 1801 break; 1802 case CTL_ACTION_PASS: 1803 case CTL_ACTION_SKIP: 1804 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1805 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1806 ctl_enqueue_rtr((union ctl_io *)ctsio); 1807 } else { 1808 1809 /* send msg back to other side */ 1810 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1811 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1812 msg_info.hdr.msg_type = CTL_MSG_R2R; 1813 #if 0 1814 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1815 #endif 1816 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1817 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1818 } 1819 } 1820 break; 1821 case CTL_ACTION_OVERLAP: 1822 /* OVERLAPPED COMMANDS ATTEMPTED */ 1823 ctl_set_sense_data(&msg_info.scsi.sense_data, 1824 lun, 1825 /*sense_format*/SSD_TYPE_NONE, 1826 /*current_error*/ 1, 1827 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1828 /*asc*/ 0x4E, 1829 /*ascq*/ 0x00, 1830 SSD_ELEM_NONE); 1831 1832 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1833 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1834 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1835 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1836 msg_info.hdr.serializing_sc = NULL; 1837 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1838 #if 0 1839 printf("BAD JUJU:Major Bummer Overlap\n"); 1840 #endif 1841 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1842 retval = 1; 1843 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1844 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1845 } 1846 break; 1847 case CTL_ACTION_OVERLAP_TAG: 1848 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1849 ctl_set_sense_data(&msg_info.scsi.sense_data, 1850 lun, 1851 /*sense_format*/SSD_TYPE_NONE, 1852 /*current_error*/ 1, 1853 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1854 /*asc*/ 0x4D, 1855 /*ascq*/ ctsio->tag_num & 0xff, 1856 SSD_ELEM_NONE); 1857 1858 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1859 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1860 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1861 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1862 msg_info.hdr.serializing_sc = NULL; 1863 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1864 #if 0 1865 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1866 #endif 1867 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1868 retval = 1; 1869 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1870 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1871 } 1872 break; 1873 case CTL_ACTION_ERROR: 1874 default: 1875 /* "Internal target failure" */ 1876 ctl_set_sense_data(&msg_info.scsi.sense_data, 1877 lun, 1878 /*sense_format*/SSD_TYPE_NONE, 1879 /*current_error*/ 1, 1880 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1881 /*asc*/ 0x44, 1882 /*ascq*/ 0x00, 1883 SSD_ELEM_NONE); 1884 1885 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1886 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1887 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1888 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1889 msg_info.hdr.serializing_sc = NULL; 1890 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1891 #if 0 1892 printf("BAD JUJU:Major Bummer HW Error\n"); 1893 #endif 1894 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1895 retval = 1; 1896 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1897 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1898 } 1899 break; 1900 } 1901 mtx_unlock(&lun->lun_lock); 1902 return (retval); 1903 } 1904 1905 static int 1906 ctl_ioctl_submit_wait(union ctl_io *io) 1907 { 1908 struct ctl_fe_ioctl_params params; 1909 ctl_fe_ioctl_state last_state; 1910 int done, retval; 1911 1912 retval = 0; 1913 1914 bzero(¶ms, sizeof(params)); 1915 1916 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1917 cv_init(¶ms.sem, "ctlioccv"); 1918 params.state = CTL_IOCTL_INPROG; 1919 last_state = params.state; 1920 1921 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1922 1923 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1924 1925 /* This shouldn't happen */ 1926 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1927 return (retval); 1928 1929 done = 0; 1930 1931 do { 1932 mtx_lock(¶ms.ioctl_mtx); 1933 /* 1934 * Check the state here, and don't sleep if the state has 1935 * already changed (i.e. wakeup has already occured, but we 1936 * weren't waiting yet). 1937 */ 1938 if (params.state == last_state) { 1939 /* XXX KDM cv_wait_sig instead? */ 1940 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1941 } 1942 last_state = params.state; 1943 1944 switch (params.state) { 1945 case CTL_IOCTL_INPROG: 1946 /* Why did we wake up? */ 1947 /* XXX KDM error here? */ 1948 mtx_unlock(¶ms.ioctl_mtx); 1949 break; 1950 case CTL_IOCTL_DATAMOVE: 1951 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1952 1953 /* 1954 * change last_state back to INPROG to avoid 1955 * deadlock on subsequent data moves. 1956 */ 1957 params.state = last_state = CTL_IOCTL_INPROG; 1958 1959 mtx_unlock(¶ms.ioctl_mtx); 1960 ctl_ioctl_do_datamove(&io->scsiio); 1961 /* 1962 * Note that in some cases, most notably writes, 1963 * this will queue the I/O and call us back later. 1964 * In other cases, generally reads, this routine 1965 * will immediately call back and wake us up, 1966 * probably using our own context. 1967 */ 1968 io->scsiio.be_move_done(io); 1969 break; 1970 case CTL_IOCTL_DONE: 1971 mtx_unlock(¶ms.ioctl_mtx); 1972 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1973 done = 1; 1974 break; 1975 default: 1976 mtx_unlock(¶ms.ioctl_mtx); 1977 /* XXX KDM error here? */ 1978 break; 1979 } 1980 } while (done == 0); 1981 1982 mtx_destroy(¶ms.ioctl_mtx); 1983 cv_destroy(¶ms.sem); 1984 1985 return (CTL_RETVAL_COMPLETE); 1986 } 1987 1988 static void 1989 ctl_ioctl_datamove(union ctl_io *io) 1990 { 1991 struct ctl_fe_ioctl_params *params; 1992 1993 params = (struct ctl_fe_ioctl_params *) 1994 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1995 1996 mtx_lock(¶ms->ioctl_mtx); 1997 params->state = CTL_IOCTL_DATAMOVE; 1998 cv_broadcast(¶ms->sem); 1999 mtx_unlock(¶ms->ioctl_mtx); 2000 } 2001 2002 static void 2003 ctl_ioctl_done(union ctl_io *io) 2004 { 2005 struct ctl_fe_ioctl_params *params; 2006 2007 params = (struct ctl_fe_ioctl_params *) 2008 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2009 2010 mtx_lock(¶ms->ioctl_mtx); 2011 params->state = CTL_IOCTL_DONE; 2012 cv_broadcast(¶ms->sem); 2013 mtx_unlock(¶ms->ioctl_mtx); 2014 } 2015 2016 static void 2017 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2018 { 2019 struct ctl_fe_ioctl_startstop_info *sd_info; 2020 2021 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2022 2023 sd_info->hs_info.status = metatask->status; 2024 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2025 sd_info->hs_info.luns_complete = 2026 metatask->taskinfo.startstop.luns_complete; 2027 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2028 2029 cv_broadcast(&sd_info->sem); 2030 } 2031 2032 static void 2033 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2034 { 2035 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2036 2037 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2038 2039 mtx_lock(fe_bbr_info->lock); 2040 fe_bbr_info->bbr_info->status = metatask->status; 2041 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2042 fe_bbr_info->wakeup_done = 1; 2043 mtx_unlock(fe_bbr_info->lock); 2044 2045 cv_broadcast(&fe_bbr_info->sem); 2046 } 2047 2048 /* 2049 * Returns 0 for success, errno for failure. 2050 */ 2051 static int 2052 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2053 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2054 { 2055 union ctl_io *io; 2056 int retval; 2057 2058 retval = 0; 2059 2060 mtx_lock(&lun->lun_lock); 2061 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2062 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2063 ooa_links)) { 2064 struct ctl_ooa_entry *entry; 2065 2066 /* 2067 * If we've got more than we can fit, just count the 2068 * remaining entries. 2069 */ 2070 if (*cur_fill_num >= ooa_hdr->alloc_num) 2071 continue; 2072 2073 entry = &kern_entries[*cur_fill_num]; 2074 2075 entry->tag_num = io->scsiio.tag_num; 2076 entry->lun_num = lun->lun; 2077 #ifdef CTL_TIME_IO 2078 entry->start_bt = io->io_hdr.start_bt; 2079 #endif 2080 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2081 entry->cdb_len = io->scsiio.cdb_len; 2082 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2083 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2084 2085 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2086 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2087 2088 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2089 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2090 2091 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2092 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2093 2094 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2095 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2096 } 2097 mtx_unlock(&lun->lun_lock); 2098 2099 return (retval); 2100 } 2101 2102 static void * 2103 ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2104 size_t error_str_len) 2105 { 2106 void *kptr; 2107 2108 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2109 2110 if (copyin(user_addr, kptr, len) != 0) { 2111 snprintf(error_str, error_str_len, "Error copying %d bytes " 2112 "from user address %p to kernel address %p", len, 2113 user_addr, kptr); 2114 free(kptr, M_CTL); 2115 return (NULL); 2116 } 2117 2118 return (kptr); 2119 } 2120 2121 static void 2122 ctl_free_args(int num_args, struct ctl_be_arg *args) 2123 { 2124 int i; 2125 2126 if (args == NULL) 2127 return; 2128 2129 for (i = 0; i < num_args; i++) { 2130 free(args[i].kname, M_CTL); 2131 free(args[i].kvalue, M_CTL); 2132 } 2133 2134 free(args, M_CTL); 2135 } 2136 2137 static struct ctl_be_arg * 2138 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2139 char *error_str, size_t error_str_len) 2140 { 2141 struct ctl_be_arg *args; 2142 int i; 2143 2144 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2145 error_str, error_str_len); 2146 2147 if (args == NULL) 2148 goto bailout; 2149 2150 for (i = 0; i < num_args; i++) { 2151 args[i].kname = NULL; 2152 args[i].kvalue = NULL; 2153 } 2154 2155 for (i = 0; i < num_args; i++) { 2156 uint8_t *tmpptr; 2157 2158 args[i].kname = ctl_copyin_alloc(args[i].name, 2159 args[i].namelen, error_str, error_str_len); 2160 if (args[i].kname == NULL) 2161 goto bailout; 2162 2163 if (args[i].kname[args[i].namelen - 1] != '\0') { 2164 snprintf(error_str, error_str_len, "Argument %d " 2165 "name is not NUL-terminated", i); 2166 goto bailout; 2167 } 2168 2169 if (args[i].flags & CTL_BEARG_RD) { 2170 tmpptr = ctl_copyin_alloc(args[i].value, 2171 args[i].vallen, error_str, error_str_len); 2172 if (tmpptr == NULL) 2173 goto bailout; 2174 if ((args[i].flags & CTL_BEARG_ASCII) 2175 && (tmpptr[args[i].vallen - 1] != '\0')) { 2176 snprintf(error_str, error_str_len, "Argument " 2177 "%d value is not NUL-terminated", i); 2178 goto bailout; 2179 } 2180 args[i].kvalue = tmpptr; 2181 } else { 2182 args[i].kvalue = malloc(args[i].vallen, 2183 M_CTL, M_WAITOK | M_ZERO); 2184 } 2185 } 2186 2187 return (args); 2188 bailout: 2189 2190 ctl_free_args(num_args, args); 2191 2192 return (NULL); 2193 } 2194 2195 static void 2196 ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2197 { 2198 int i; 2199 2200 for (i = 0; i < num_args; i++) { 2201 if (args[i].flags & CTL_BEARG_WR) 2202 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2203 } 2204 } 2205 2206 /* 2207 * Escape characters that are illegal or not recommended in XML. 2208 */ 2209 int 2210 ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2211 { 2212 int retval; 2213 2214 retval = 0; 2215 2216 for (; *str; str++) { 2217 switch (*str) { 2218 case '&': 2219 retval = sbuf_printf(sb, "&"); 2220 break; 2221 case '>': 2222 retval = sbuf_printf(sb, ">"); 2223 break; 2224 case '<': 2225 retval = sbuf_printf(sb, "<"); 2226 break; 2227 default: 2228 retval = sbuf_putc(sb, *str); 2229 break; 2230 } 2231 2232 if (retval != 0) 2233 break; 2234 2235 } 2236 2237 return (retval); 2238 } 2239 2240 static int 2241 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2242 struct thread *td) 2243 { 2244 struct ctl_softc *softc; 2245 int retval; 2246 2247 softc = control_softc; 2248 2249 retval = 0; 2250 2251 switch (cmd) { 2252 case CTL_IO: { 2253 union ctl_io *io; 2254 void *pool_tmp; 2255 2256 /* 2257 * If we haven't been "enabled", don't allow any SCSI I/O 2258 * to this FETD. 2259 */ 2260 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2261 retval = EPERM; 2262 break; 2263 } 2264 2265 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2266 if (io == NULL) { 2267 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2268 retval = ENOSPC; 2269 break; 2270 } 2271 2272 /* 2273 * Need to save the pool reference so it doesn't get 2274 * spammed by the user's ctl_io. 2275 */ 2276 pool_tmp = io->io_hdr.pool; 2277 2278 memcpy(io, (void *)addr, sizeof(*io)); 2279 2280 io->io_hdr.pool = pool_tmp; 2281 /* 2282 * No status yet, so make sure the status is set properly. 2283 */ 2284 io->io_hdr.status = CTL_STATUS_NONE; 2285 2286 /* 2287 * The user sets the initiator ID, target and LUN IDs. 2288 */ 2289 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2290 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2291 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2292 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2293 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2294 2295 retval = ctl_ioctl_submit_wait(io); 2296 2297 if (retval != 0) { 2298 ctl_free_io(io); 2299 break; 2300 } 2301 2302 memcpy((void *)addr, io, sizeof(*io)); 2303 2304 /* return this to our pool */ 2305 ctl_free_io(io); 2306 2307 break; 2308 } 2309 case CTL_ENABLE_PORT: 2310 case CTL_DISABLE_PORT: 2311 case CTL_SET_PORT_WWNS: { 2312 struct ctl_port *port; 2313 struct ctl_port_entry *entry; 2314 2315 entry = (struct ctl_port_entry *)addr; 2316 2317 mtx_lock(&softc->ctl_lock); 2318 STAILQ_FOREACH(port, &softc->port_list, links) { 2319 int action, done; 2320 2321 action = 0; 2322 done = 0; 2323 2324 if ((entry->port_type == CTL_PORT_NONE) 2325 && (entry->targ_port == port->targ_port)) { 2326 /* 2327 * If the user only wants to enable or 2328 * disable or set WWNs on a specific port, 2329 * do the operation and we're done. 2330 */ 2331 action = 1; 2332 done = 1; 2333 } else if (entry->port_type & port->port_type) { 2334 /* 2335 * Compare the user's type mask with the 2336 * particular frontend type to see if we 2337 * have a match. 2338 */ 2339 action = 1; 2340 done = 0; 2341 2342 /* 2343 * Make sure the user isn't trying to set 2344 * WWNs on multiple ports at the same time. 2345 */ 2346 if (cmd == CTL_SET_PORT_WWNS) { 2347 printf("%s: Can't set WWNs on " 2348 "multiple ports\n", __func__); 2349 retval = EINVAL; 2350 break; 2351 } 2352 } 2353 if (action != 0) { 2354 /* 2355 * XXX KDM we have to drop the lock here, 2356 * because the online/offline operations 2357 * can potentially block. We need to 2358 * reference count the frontends so they 2359 * can't go away, 2360 */ 2361 mtx_unlock(&softc->ctl_lock); 2362 2363 if (cmd == CTL_ENABLE_PORT) { 2364 struct ctl_lun *lun; 2365 2366 STAILQ_FOREACH(lun, &softc->lun_list, 2367 links) { 2368 port->lun_enable(port->targ_lun_arg, 2369 lun->target, 2370 lun->lun); 2371 } 2372 2373 ctl_port_online(port); 2374 } else if (cmd == CTL_DISABLE_PORT) { 2375 struct ctl_lun *lun; 2376 2377 ctl_port_offline(port); 2378 2379 STAILQ_FOREACH(lun, &softc->lun_list, 2380 links) { 2381 port->lun_disable( 2382 port->targ_lun_arg, 2383 lun->target, 2384 lun->lun); 2385 } 2386 } 2387 2388 mtx_lock(&softc->ctl_lock); 2389 2390 if (cmd == CTL_SET_PORT_WWNS) 2391 ctl_port_set_wwns(port, 2392 (entry->flags & CTL_PORT_WWNN_VALID) ? 2393 1 : 0, entry->wwnn, 2394 (entry->flags & CTL_PORT_WWPN_VALID) ? 2395 1 : 0, entry->wwpn); 2396 } 2397 if (done != 0) 2398 break; 2399 } 2400 mtx_unlock(&softc->ctl_lock); 2401 break; 2402 } 2403 case CTL_GET_PORT_LIST: { 2404 struct ctl_port *port; 2405 struct ctl_port_list *list; 2406 int i; 2407 2408 list = (struct ctl_port_list *)addr; 2409 2410 if (list->alloc_len != (list->alloc_num * 2411 sizeof(struct ctl_port_entry))) { 2412 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2413 "alloc_num %u * sizeof(struct ctl_port_entry) " 2414 "%zu\n", __func__, list->alloc_len, 2415 list->alloc_num, sizeof(struct ctl_port_entry)); 2416 retval = EINVAL; 2417 break; 2418 } 2419 list->fill_len = 0; 2420 list->fill_num = 0; 2421 list->dropped_num = 0; 2422 i = 0; 2423 mtx_lock(&softc->ctl_lock); 2424 STAILQ_FOREACH(port, &softc->port_list, links) { 2425 struct ctl_port_entry entry, *list_entry; 2426 2427 if (list->fill_num >= list->alloc_num) { 2428 list->dropped_num++; 2429 continue; 2430 } 2431 2432 entry.port_type = port->port_type; 2433 strlcpy(entry.port_name, port->port_name, 2434 sizeof(entry.port_name)); 2435 entry.targ_port = port->targ_port; 2436 entry.physical_port = port->physical_port; 2437 entry.virtual_port = port->virtual_port; 2438 entry.wwnn = port->wwnn; 2439 entry.wwpn = port->wwpn; 2440 if (port->status & CTL_PORT_STATUS_ONLINE) 2441 entry.online = 1; 2442 else 2443 entry.online = 0; 2444 2445 list_entry = &list->entries[i]; 2446 2447 retval = copyout(&entry, list_entry, sizeof(entry)); 2448 if (retval != 0) { 2449 printf("%s: CTL_GET_PORT_LIST: copyout " 2450 "returned %d\n", __func__, retval); 2451 break; 2452 } 2453 i++; 2454 list->fill_num++; 2455 list->fill_len += sizeof(entry); 2456 } 2457 mtx_unlock(&softc->ctl_lock); 2458 2459 /* 2460 * If this is non-zero, we had a copyout fault, so there's 2461 * probably no point in attempting to set the status inside 2462 * the structure. 2463 */ 2464 if (retval != 0) 2465 break; 2466 2467 if (list->dropped_num > 0) 2468 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2469 else 2470 list->status = CTL_PORT_LIST_OK; 2471 break; 2472 } 2473 case CTL_DUMP_OOA: { 2474 struct ctl_lun *lun; 2475 union ctl_io *io; 2476 char printbuf[128]; 2477 struct sbuf sb; 2478 2479 mtx_lock(&softc->ctl_lock); 2480 printf("Dumping OOA queues:\n"); 2481 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2482 mtx_lock(&lun->lun_lock); 2483 for (io = (union ctl_io *)TAILQ_FIRST( 2484 &lun->ooa_queue); io != NULL; 2485 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2486 ooa_links)) { 2487 sbuf_new(&sb, printbuf, sizeof(printbuf), 2488 SBUF_FIXEDLEN); 2489 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2490 (intmax_t)lun->lun, 2491 io->scsiio.tag_num, 2492 (io->io_hdr.flags & 2493 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2494 (io->io_hdr.flags & 2495 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2496 (io->io_hdr.flags & 2497 CTL_FLAG_ABORT) ? " ABORT" : "", 2498 (io->io_hdr.flags & 2499 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2500 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2501 sbuf_finish(&sb); 2502 printf("%s\n", sbuf_data(&sb)); 2503 } 2504 mtx_unlock(&lun->lun_lock); 2505 } 2506 printf("OOA queues dump done\n"); 2507 mtx_unlock(&softc->ctl_lock); 2508 break; 2509 } 2510 case CTL_GET_OOA: { 2511 struct ctl_lun *lun; 2512 struct ctl_ooa *ooa_hdr; 2513 struct ctl_ooa_entry *entries; 2514 uint32_t cur_fill_num; 2515 2516 ooa_hdr = (struct ctl_ooa *)addr; 2517 2518 if ((ooa_hdr->alloc_len == 0) 2519 || (ooa_hdr->alloc_num == 0)) { 2520 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2521 "must be non-zero\n", __func__, 2522 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2523 retval = EINVAL; 2524 break; 2525 } 2526 2527 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2528 sizeof(struct ctl_ooa_entry))) { 2529 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2530 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2531 __func__, ooa_hdr->alloc_len, 2532 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2533 retval = EINVAL; 2534 break; 2535 } 2536 2537 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2538 if (entries == NULL) { 2539 printf("%s: could not allocate %d bytes for OOA " 2540 "dump\n", __func__, ooa_hdr->alloc_len); 2541 retval = ENOMEM; 2542 break; 2543 } 2544 2545 mtx_lock(&softc->ctl_lock); 2546 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2547 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2548 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2549 mtx_unlock(&softc->ctl_lock); 2550 free(entries, M_CTL); 2551 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2552 __func__, (uintmax_t)ooa_hdr->lun_num); 2553 retval = EINVAL; 2554 break; 2555 } 2556 2557 cur_fill_num = 0; 2558 2559 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2560 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2561 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2562 ooa_hdr, entries); 2563 if (retval != 0) 2564 break; 2565 } 2566 if (retval != 0) { 2567 mtx_unlock(&softc->ctl_lock); 2568 free(entries, M_CTL); 2569 break; 2570 } 2571 } else { 2572 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2573 2574 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2575 entries); 2576 } 2577 mtx_unlock(&softc->ctl_lock); 2578 2579 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2580 ooa_hdr->fill_len = ooa_hdr->fill_num * 2581 sizeof(struct ctl_ooa_entry); 2582 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2583 if (retval != 0) { 2584 printf("%s: error copying out %d bytes for OOA dump\n", 2585 __func__, ooa_hdr->fill_len); 2586 } 2587 2588 getbintime(&ooa_hdr->cur_bt); 2589 2590 if (cur_fill_num > ooa_hdr->alloc_num) { 2591 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2592 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2593 } else { 2594 ooa_hdr->dropped_num = 0; 2595 ooa_hdr->status = CTL_OOA_OK; 2596 } 2597 2598 free(entries, M_CTL); 2599 break; 2600 } 2601 case CTL_CHECK_OOA: { 2602 union ctl_io *io; 2603 struct ctl_lun *lun; 2604 struct ctl_ooa_info *ooa_info; 2605 2606 2607 ooa_info = (struct ctl_ooa_info *)addr; 2608 2609 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2610 ooa_info->status = CTL_OOA_INVALID_LUN; 2611 break; 2612 } 2613 mtx_lock(&softc->ctl_lock); 2614 lun = softc->ctl_luns[ooa_info->lun_id]; 2615 if (lun == NULL) { 2616 mtx_unlock(&softc->ctl_lock); 2617 ooa_info->status = CTL_OOA_INVALID_LUN; 2618 break; 2619 } 2620 mtx_lock(&lun->lun_lock); 2621 mtx_unlock(&softc->ctl_lock); 2622 ooa_info->num_entries = 0; 2623 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2624 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2625 &io->io_hdr, ooa_links)) { 2626 ooa_info->num_entries++; 2627 } 2628 mtx_unlock(&lun->lun_lock); 2629 2630 ooa_info->status = CTL_OOA_SUCCESS; 2631 2632 break; 2633 } 2634 case CTL_HARD_START: 2635 case CTL_HARD_STOP: { 2636 struct ctl_fe_ioctl_startstop_info ss_info; 2637 struct cfi_metatask *metatask; 2638 struct mtx hs_mtx; 2639 2640 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2641 2642 cv_init(&ss_info.sem, "hard start/stop cv" ); 2643 2644 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2645 if (metatask == NULL) { 2646 retval = ENOMEM; 2647 mtx_destroy(&hs_mtx); 2648 break; 2649 } 2650 2651 if (cmd == CTL_HARD_START) 2652 metatask->tasktype = CFI_TASK_STARTUP; 2653 else 2654 metatask->tasktype = CFI_TASK_SHUTDOWN; 2655 2656 metatask->callback = ctl_ioctl_hard_startstop_callback; 2657 metatask->callback_arg = &ss_info; 2658 2659 cfi_action(metatask); 2660 2661 /* Wait for the callback */ 2662 mtx_lock(&hs_mtx); 2663 cv_wait_sig(&ss_info.sem, &hs_mtx); 2664 mtx_unlock(&hs_mtx); 2665 2666 /* 2667 * All information has been copied from the metatask by the 2668 * time cv_broadcast() is called, so we free the metatask here. 2669 */ 2670 cfi_free_metatask(metatask); 2671 2672 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2673 2674 mtx_destroy(&hs_mtx); 2675 break; 2676 } 2677 case CTL_BBRREAD: { 2678 struct ctl_bbrread_info *bbr_info; 2679 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2680 struct mtx bbr_mtx; 2681 struct cfi_metatask *metatask; 2682 2683 bbr_info = (struct ctl_bbrread_info *)addr; 2684 2685 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2686 2687 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2688 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2689 2690 fe_bbr_info.bbr_info = bbr_info; 2691 fe_bbr_info.lock = &bbr_mtx; 2692 2693 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2694 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2695 2696 if (metatask == NULL) { 2697 mtx_destroy(&bbr_mtx); 2698 cv_destroy(&fe_bbr_info.sem); 2699 retval = ENOMEM; 2700 break; 2701 } 2702 metatask->tasktype = CFI_TASK_BBRREAD; 2703 metatask->callback = ctl_ioctl_bbrread_callback; 2704 metatask->callback_arg = &fe_bbr_info; 2705 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2706 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2707 metatask->taskinfo.bbrread.len = bbr_info->len; 2708 2709 cfi_action(metatask); 2710 2711 mtx_lock(&bbr_mtx); 2712 while (fe_bbr_info.wakeup_done == 0) 2713 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2714 mtx_unlock(&bbr_mtx); 2715 2716 bbr_info->status = metatask->status; 2717 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2718 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2719 memcpy(&bbr_info->sense_data, 2720 &metatask->taskinfo.bbrread.sense_data, 2721 ctl_min(sizeof(bbr_info->sense_data), 2722 sizeof(metatask->taskinfo.bbrread.sense_data))); 2723 2724 cfi_free_metatask(metatask); 2725 2726 mtx_destroy(&bbr_mtx); 2727 cv_destroy(&fe_bbr_info.sem); 2728 2729 break; 2730 } 2731 case CTL_DELAY_IO: { 2732 struct ctl_io_delay_info *delay_info; 2733 #ifdef CTL_IO_DELAY 2734 struct ctl_lun *lun; 2735 #endif /* CTL_IO_DELAY */ 2736 2737 delay_info = (struct ctl_io_delay_info *)addr; 2738 2739 #ifdef CTL_IO_DELAY 2740 mtx_lock(&softc->ctl_lock); 2741 2742 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2743 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2744 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2745 } else { 2746 lun = softc->ctl_luns[delay_info->lun_id]; 2747 mtx_lock(&lun->lun_lock); 2748 2749 delay_info->status = CTL_DELAY_STATUS_OK; 2750 2751 switch (delay_info->delay_type) { 2752 case CTL_DELAY_TYPE_CONT: 2753 break; 2754 case CTL_DELAY_TYPE_ONESHOT: 2755 break; 2756 default: 2757 delay_info->status = 2758 CTL_DELAY_STATUS_INVALID_TYPE; 2759 break; 2760 } 2761 2762 switch (delay_info->delay_loc) { 2763 case CTL_DELAY_LOC_DATAMOVE: 2764 lun->delay_info.datamove_type = 2765 delay_info->delay_type; 2766 lun->delay_info.datamove_delay = 2767 delay_info->delay_secs; 2768 break; 2769 case CTL_DELAY_LOC_DONE: 2770 lun->delay_info.done_type = 2771 delay_info->delay_type; 2772 lun->delay_info.done_delay = 2773 delay_info->delay_secs; 2774 break; 2775 default: 2776 delay_info->status = 2777 CTL_DELAY_STATUS_INVALID_LOC; 2778 break; 2779 } 2780 mtx_unlock(&lun->lun_lock); 2781 } 2782 2783 mtx_unlock(&softc->ctl_lock); 2784 #else 2785 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2786 #endif /* CTL_IO_DELAY */ 2787 break; 2788 } 2789 case CTL_REALSYNC_SET: { 2790 int *syncstate; 2791 2792 syncstate = (int *)addr; 2793 2794 mtx_lock(&softc->ctl_lock); 2795 switch (*syncstate) { 2796 case 0: 2797 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2798 break; 2799 case 1: 2800 softc->flags |= CTL_FLAG_REAL_SYNC; 2801 break; 2802 default: 2803 retval = EINVAL; 2804 break; 2805 } 2806 mtx_unlock(&softc->ctl_lock); 2807 break; 2808 } 2809 case CTL_REALSYNC_GET: { 2810 int *syncstate; 2811 2812 syncstate = (int*)addr; 2813 2814 mtx_lock(&softc->ctl_lock); 2815 if (softc->flags & CTL_FLAG_REAL_SYNC) 2816 *syncstate = 1; 2817 else 2818 *syncstate = 0; 2819 mtx_unlock(&softc->ctl_lock); 2820 2821 break; 2822 } 2823 case CTL_SETSYNC: 2824 case CTL_GETSYNC: { 2825 struct ctl_sync_info *sync_info; 2826 struct ctl_lun *lun; 2827 2828 sync_info = (struct ctl_sync_info *)addr; 2829 2830 mtx_lock(&softc->ctl_lock); 2831 lun = softc->ctl_luns[sync_info->lun_id]; 2832 if (lun == NULL) { 2833 mtx_unlock(&softc->ctl_lock); 2834 sync_info->status = CTL_GS_SYNC_NO_LUN; 2835 } 2836 /* 2837 * Get or set the sync interval. We're not bounds checking 2838 * in the set case, hopefully the user won't do something 2839 * silly. 2840 */ 2841 mtx_lock(&lun->lun_lock); 2842 mtx_unlock(&softc->ctl_lock); 2843 if (cmd == CTL_GETSYNC) 2844 sync_info->sync_interval = lun->sync_interval; 2845 else 2846 lun->sync_interval = sync_info->sync_interval; 2847 mtx_unlock(&lun->lun_lock); 2848 2849 sync_info->status = CTL_GS_SYNC_OK; 2850 2851 break; 2852 } 2853 case CTL_GETSTATS: { 2854 struct ctl_stats *stats; 2855 struct ctl_lun *lun; 2856 int i; 2857 2858 stats = (struct ctl_stats *)addr; 2859 2860 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2861 stats->alloc_len) { 2862 stats->status = CTL_SS_NEED_MORE_SPACE; 2863 stats->num_luns = softc->num_luns; 2864 break; 2865 } 2866 /* 2867 * XXX KDM no locking here. If the LUN list changes, 2868 * things can blow up. 2869 */ 2870 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2871 i++, lun = STAILQ_NEXT(lun, links)) { 2872 retval = copyout(&lun->stats, &stats->lun_stats[i], 2873 sizeof(lun->stats)); 2874 if (retval != 0) 2875 break; 2876 } 2877 stats->num_luns = softc->num_luns; 2878 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2879 softc->num_luns; 2880 stats->status = CTL_SS_OK; 2881 #ifdef CTL_TIME_IO 2882 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2883 #else 2884 stats->flags = CTL_STATS_FLAG_NONE; 2885 #endif 2886 getnanouptime(&stats->timestamp); 2887 break; 2888 } 2889 case CTL_ERROR_INJECT: { 2890 struct ctl_error_desc *err_desc, *new_err_desc; 2891 struct ctl_lun *lun; 2892 2893 err_desc = (struct ctl_error_desc *)addr; 2894 2895 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2896 M_WAITOK | M_ZERO); 2897 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2898 2899 mtx_lock(&softc->ctl_lock); 2900 lun = softc->ctl_luns[err_desc->lun_id]; 2901 if (lun == NULL) { 2902 mtx_unlock(&softc->ctl_lock); 2903 free(new_err_desc, M_CTL); 2904 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2905 __func__, (uintmax_t)err_desc->lun_id); 2906 retval = EINVAL; 2907 break; 2908 } 2909 mtx_lock(&lun->lun_lock); 2910 mtx_unlock(&softc->ctl_lock); 2911 2912 /* 2913 * We could do some checking here to verify the validity 2914 * of the request, but given the complexity of error 2915 * injection requests, the checking logic would be fairly 2916 * complex. 2917 * 2918 * For now, if the request is invalid, it just won't get 2919 * executed and might get deleted. 2920 */ 2921 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2922 2923 /* 2924 * XXX KDM check to make sure the serial number is unique, 2925 * in case we somehow manage to wrap. That shouldn't 2926 * happen for a very long time, but it's the right thing to 2927 * do. 2928 */ 2929 new_err_desc->serial = lun->error_serial; 2930 err_desc->serial = lun->error_serial; 2931 lun->error_serial++; 2932 2933 mtx_unlock(&lun->lun_lock); 2934 break; 2935 } 2936 case CTL_ERROR_INJECT_DELETE: { 2937 struct ctl_error_desc *delete_desc, *desc, *desc2; 2938 struct ctl_lun *lun; 2939 int delete_done; 2940 2941 delete_desc = (struct ctl_error_desc *)addr; 2942 delete_done = 0; 2943 2944 mtx_lock(&softc->ctl_lock); 2945 lun = softc->ctl_luns[delete_desc->lun_id]; 2946 if (lun == NULL) { 2947 mtx_unlock(&softc->ctl_lock); 2948 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2949 __func__, (uintmax_t)delete_desc->lun_id); 2950 retval = EINVAL; 2951 break; 2952 } 2953 mtx_lock(&lun->lun_lock); 2954 mtx_unlock(&softc->ctl_lock); 2955 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2956 if (desc->serial != delete_desc->serial) 2957 continue; 2958 2959 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2960 links); 2961 free(desc, M_CTL); 2962 delete_done = 1; 2963 } 2964 mtx_unlock(&lun->lun_lock); 2965 if (delete_done == 0) { 2966 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2967 "error serial %ju on LUN %u\n", __func__, 2968 delete_desc->serial, delete_desc->lun_id); 2969 retval = EINVAL; 2970 break; 2971 } 2972 break; 2973 } 2974 case CTL_DUMP_STRUCTS: { 2975 int i, j, k, idx; 2976 struct ctl_port *port; 2977 struct ctl_frontend *fe; 2978 2979 mtx_lock(&softc->ctl_lock); 2980 printf("CTL Persistent Reservation information start:\n"); 2981 for (i = 0; i < CTL_MAX_LUNS; i++) { 2982 struct ctl_lun *lun; 2983 2984 lun = softc->ctl_luns[i]; 2985 2986 if ((lun == NULL) 2987 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2988 continue; 2989 2990 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2991 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2992 idx = j * CTL_MAX_INIT_PER_PORT + k; 2993 if (lun->per_res[idx].registered == 0) 2994 continue; 2995 printf(" LUN %d port %d iid %d key " 2996 "%#jx\n", i, j, k, 2997 (uintmax_t)scsi_8btou64( 2998 lun->per_res[idx].res_key.key)); 2999 } 3000 } 3001 } 3002 printf("CTL Persistent Reservation information end\n"); 3003 printf("CTL Ports:\n"); 3004 STAILQ_FOREACH(port, &softc->port_list, links) { 3005 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3006 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3007 port->frontend->name, port->port_type, 3008 port->physical_port, port->virtual_port, 3009 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3010 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3011 if (port->wwpn_iid[j].in_use == 0 && 3012 port->wwpn_iid[j].wwpn == 0 && 3013 port->wwpn_iid[j].name == NULL) 3014 continue; 3015 3016 printf(" iid %u use %d WWPN %#jx '%s'\n", 3017 j, port->wwpn_iid[j].in_use, 3018 (uintmax_t)port->wwpn_iid[j].wwpn, 3019 port->wwpn_iid[j].name); 3020 } 3021 } 3022 printf("CTL Port information end\n"); 3023 mtx_unlock(&softc->ctl_lock); 3024 /* 3025 * XXX KDM calling this without a lock. We'd likely want 3026 * to drop the lock before calling the frontend's dump 3027 * routine anyway. 3028 */ 3029 printf("CTL Frontends:\n"); 3030 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3031 printf(" Frontend '%s'\n", fe->name); 3032 if (fe->fe_dump != NULL) 3033 fe->fe_dump(); 3034 } 3035 printf("CTL Frontend information end\n"); 3036 break; 3037 } 3038 case CTL_LUN_REQ: { 3039 struct ctl_lun_req *lun_req; 3040 struct ctl_backend_driver *backend; 3041 3042 lun_req = (struct ctl_lun_req *)addr; 3043 3044 backend = ctl_backend_find(lun_req->backend); 3045 if (backend == NULL) { 3046 lun_req->status = CTL_LUN_ERROR; 3047 snprintf(lun_req->error_str, 3048 sizeof(lun_req->error_str), 3049 "Backend \"%s\" not found.", 3050 lun_req->backend); 3051 break; 3052 } 3053 if (lun_req->num_be_args > 0) { 3054 lun_req->kern_be_args = ctl_copyin_args( 3055 lun_req->num_be_args, 3056 lun_req->be_args, 3057 lun_req->error_str, 3058 sizeof(lun_req->error_str)); 3059 if (lun_req->kern_be_args == NULL) { 3060 lun_req->status = CTL_LUN_ERROR; 3061 break; 3062 } 3063 } 3064 3065 retval = backend->ioctl(dev, cmd, addr, flag, td); 3066 3067 if (lun_req->num_be_args > 0) { 3068 ctl_copyout_args(lun_req->num_be_args, 3069 lun_req->kern_be_args); 3070 ctl_free_args(lun_req->num_be_args, 3071 lun_req->kern_be_args); 3072 } 3073 break; 3074 } 3075 case CTL_LUN_LIST: { 3076 struct sbuf *sb; 3077 struct ctl_lun *lun; 3078 struct ctl_lun_list *list; 3079 struct ctl_option *opt; 3080 3081 list = (struct ctl_lun_list *)addr; 3082 3083 /* 3084 * Allocate a fixed length sbuf here, based on the length 3085 * of the user's buffer. We could allocate an auto-extending 3086 * buffer, and then tell the user how much larger our 3087 * amount of data is than his buffer, but that presents 3088 * some problems: 3089 * 3090 * 1. The sbuf(9) routines use a blocking malloc, and so 3091 * we can't hold a lock while calling them with an 3092 * auto-extending buffer. 3093 * 3094 * 2. There is not currently a LUN reference counting 3095 * mechanism, outside of outstanding transactions on 3096 * the LUN's OOA queue. So a LUN could go away on us 3097 * while we're getting the LUN number, backend-specific 3098 * information, etc. Thus, given the way things 3099 * currently work, we need to hold the CTL lock while 3100 * grabbing LUN information. 3101 * 3102 * So, from the user's standpoint, the best thing to do is 3103 * allocate what he thinks is a reasonable buffer length, 3104 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3105 * double the buffer length and try again. (And repeat 3106 * that until he succeeds.) 3107 */ 3108 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3109 if (sb == NULL) { 3110 list->status = CTL_LUN_LIST_ERROR; 3111 snprintf(list->error_str, sizeof(list->error_str), 3112 "Unable to allocate %d bytes for LUN list", 3113 list->alloc_len); 3114 break; 3115 } 3116 3117 sbuf_printf(sb, "<ctllunlist>\n"); 3118 3119 mtx_lock(&softc->ctl_lock); 3120 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3121 mtx_lock(&lun->lun_lock); 3122 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3123 (uintmax_t)lun->lun); 3124 3125 /* 3126 * Bail out as soon as we see that we've overfilled 3127 * the buffer. 3128 */ 3129 if (retval != 0) 3130 break; 3131 3132 retval = sbuf_printf(sb, "\t<backend_type>%s" 3133 "</backend_type>\n", 3134 (lun->backend == NULL) ? "none" : 3135 lun->backend->name); 3136 3137 if (retval != 0) 3138 break; 3139 3140 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3141 lun->be_lun->lun_type); 3142 3143 if (retval != 0) 3144 break; 3145 3146 if (lun->backend == NULL) { 3147 retval = sbuf_printf(sb, "</lun>\n"); 3148 if (retval != 0) 3149 break; 3150 continue; 3151 } 3152 3153 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3154 (lun->be_lun->maxlba > 0) ? 3155 lun->be_lun->maxlba + 1 : 0); 3156 3157 if (retval != 0) 3158 break; 3159 3160 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3161 lun->be_lun->blocksize); 3162 3163 if (retval != 0) 3164 break; 3165 3166 retval = sbuf_printf(sb, "\t<serial_number>"); 3167 3168 if (retval != 0) 3169 break; 3170 3171 retval = ctl_sbuf_printf_esc(sb, 3172 lun->be_lun->serial_num); 3173 3174 if (retval != 0) 3175 break; 3176 3177 retval = sbuf_printf(sb, "</serial_number>\n"); 3178 3179 if (retval != 0) 3180 break; 3181 3182 retval = sbuf_printf(sb, "\t<device_id>"); 3183 3184 if (retval != 0) 3185 break; 3186 3187 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3188 3189 if (retval != 0) 3190 break; 3191 3192 retval = sbuf_printf(sb, "</device_id>\n"); 3193 3194 if (retval != 0) 3195 break; 3196 3197 if (lun->backend->lun_info != NULL) { 3198 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3199 if (retval != 0) 3200 break; 3201 } 3202 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3203 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3204 opt->name, opt->value, opt->name); 3205 if (retval != 0) 3206 break; 3207 } 3208 3209 retval = sbuf_printf(sb, "</lun>\n"); 3210 3211 if (retval != 0) 3212 break; 3213 mtx_unlock(&lun->lun_lock); 3214 } 3215 if (lun != NULL) 3216 mtx_unlock(&lun->lun_lock); 3217 mtx_unlock(&softc->ctl_lock); 3218 3219 if ((retval != 0) 3220 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3221 retval = 0; 3222 sbuf_delete(sb); 3223 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3224 snprintf(list->error_str, sizeof(list->error_str), 3225 "Out of space, %d bytes is too small", 3226 list->alloc_len); 3227 break; 3228 } 3229 3230 sbuf_finish(sb); 3231 3232 retval = copyout(sbuf_data(sb), list->lun_xml, 3233 sbuf_len(sb) + 1); 3234 3235 list->fill_len = sbuf_len(sb) + 1; 3236 list->status = CTL_LUN_LIST_OK; 3237 sbuf_delete(sb); 3238 break; 3239 } 3240 case CTL_ISCSI: { 3241 struct ctl_iscsi *ci; 3242 struct ctl_frontend *fe; 3243 3244 ci = (struct ctl_iscsi *)addr; 3245 3246 fe = ctl_frontend_find("iscsi"); 3247 if (fe == NULL) { 3248 ci->status = CTL_ISCSI_ERROR; 3249 snprintf(ci->error_str, sizeof(ci->error_str), 3250 "Frontend \"iscsi\" not found."); 3251 break; 3252 } 3253 3254 retval = fe->ioctl(dev, cmd, addr, flag, td); 3255 break; 3256 } 3257 case CTL_PORT_REQ: { 3258 struct ctl_req *req; 3259 struct ctl_frontend *fe; 3260 3261 req = (struct ctl_req *)addr; 3262 3263 fe = ctl_frontend_find(req->driver); 3264 if (fe == NULL) { 3265 req->status = CTL_LUN_ERROR; 3266 snprintf(req->error_str, sizeof(req->error_str), 3267 "Frontend \"%s\" not found.", req->driver); 3268 break; 3269 } 3270 if (req->num_args > 0) { 3271 req->kern_args = ctl_copyin_args(req->num_args, 3272 req->args, req->error_str, sizeof(req->error_str)); 3273 if (req->kern_args == NULL) { 3274 req->status = CTL_LUN_ERROR; 3275 break; 3276 } 3277 } 3278 3279 retval = fe->ioctl(dev, cmd, addr, flag, td); 3280 3281 if (req->num_args > 0) { 3282 ctl_copyout_args(req->num_args, req->kern_args); 3283 ctl_free_args(req->num_args, req->kern_args); 3284 } 3285 break; 3286 } 3287 case CTL_PORT_LIST: { 3288 struct sbuf *sb; 3289 struct ctl_port *port; 3290 struct ctl_lun_list *list; 3291 struct ctl_option *opt; 3292 3293 list = (struct ctl_lun_list *)addr; 3294 3295 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3296 if (sb == NULL) { 3297 list->status = CTL_LUN_LIST_ERROR; 3298 snprintf(list->error_str, sizeof(list->error_str), 3299 "Unable to allocate %d bytes for LUN list", 3300 list->alloc_len); 3301 break; 3302 } 3303 3304 sbuf_printf(sb, "<ctlportlist>\n"); 3305 3306 mtx_lock(&softc->ctl_lock); 3307 STAILQ_FOREACH(port, &softc->port_list, links) { 3308 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3309 (uintmax_t)port->targ_port); 3310 3311 /* 3312 * Bail out as soon as we see that we've overfilled 3313 * the buffer. 3314 */ 3315 if (retval != 0) 3316 break; 3317 3318 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3319 "</frontend_type>\n", port->frontend->name); 3320 if (retval != 0) 3321 break; 3322 3323 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3324 port->port_type); 3325 if (retval != 0) 3326 break; 3327 3328 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3329 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3330 if (retval != 0) 3331 break; 3332 3333 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3334 port->port_name); 3335 if (retval != 0) 3336 break; 3337 3338 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3339 port->physical_port); 3340 if (retval != 0) 3341 break; 3342 3343 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3344 port->virtual_port); 3345 if (retval != 0) 3346 break; 3347 3348 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n", 3349 (uintmax_t)port->wwnn); 3350 if (retval != 0) 3351 break; 3352 3353 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n", 3354 (uintmax_t)port->wwpn); 3355 if (retval != 0) 3356 break; 3357 3358 if (port->port_info != NULL) { 3359 retval = port->port_info(port->onoff_arg, sb); 3360 if (retval != 0) 3361 break; 3362 } 3363 STAILQ_FOREACH(opt, &port->options, links) { 3364 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3365 opt->name, opt->value, opt->name); 3366 if (retval != 0) 3367 break; 3368 } 3369 3370 retval = sbuf_printf(sb, "</targ_port>\n"); 3371 if (retval != 0) 3372 break; 3373 } 3374 mtx_unlock(&softc->ctl_lock); 3375 3376 if ((retval != 0) 3377 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3378 retval = 0; 3379 sbuf_delete(sb); 3380 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3381 snprintf(list->error_str, sizeof(list->error_str), 3382 "Out of space, %d bytes is too small", 3383 list->alloc_len); 3384 break; 3385 } 3386 3387 sbuf_finish(sb); 3388 3389 retval = copyout(sbuf_data(sb), list->lun_xml, 3390 sbuf_len(sb) + 1); 3391 3392 list->fill_len = sbuf_len(sb) + 1; 3393 list->status = CTL_LUN_LIST_OK; 3394 sbuf_delete(sb); 3395 break; 3396 } 3397 default: { 3398 /* XXX KDM should we fix this? */ 3399 #if 0 3400 struct ctl_backend_driver *backend; 3401 unsigned int type; 3402 int found; 3403 3404 found = 0; 3405 3406 /* 3407 * We encode the backend type as the ioctl type for backend 3408 * ioctls. So parse it out here, and then search for a 3409 * backend of this type. 3410 */ 3411 type = _IOC_TYPE(cmd); 3412 3413 STAILQ_FOREACH(backend, &softc->be_list, links) { 3414 if (backend->type == type) { 3415 found = 1; 3416 break; 3417 } 3418 } 3419 if (found == 0) { 3420 printf("ctl: unknown ioctl command %#lx or backend " 3421 "%d\n", cmd, type); 3422 retval = EINVAL; 3423 break; 3424 } 3425 retval = backend->ioctl(dev, cmd, addr, flag, td); 3426 #endif 3427 retval = ENOTTY; 3428 break; 3429 } 3430 } 3431 return (retval); 3432 } 3433 3434 uint32_t 3435 ctl_get_initindex(struct ctl_nexus *nexus) 3436 { 3437 if (nexus->targ_port < CTL_MAX_PORTS) 3438 return (nexus->initid.id + 3439 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3440 else 3441 return (nexus->initid.id + 3442 ((nexus->targ_port - CTL_MAX_PORTS) * 3443 CTL_MAX_INIT_PER_PORT)); 3444 } 3445 3446 uint32_t 3447 ctl_get_resindex(struct ctl_nexus *nexus) 3448 { 3449 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3450 } 3451 3452 uint32_t 3453 ctl_port_idx(int port_num) 3454 { 3455 if (port_num < CTL_MAX_PORTS) 3456 return(port_num); 3457 else 3458 return(port_num - CTL_MAX_PORTS); 3459 } 3460 3461 static uint32_t 3462 ctl_map_lun(int port_num, uint32_t lun_id) 3463 { 3464 struct ctl_port *port; 3465 3466 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3467 if (port == NULL) 3468 return (UINT32_MAX); 3469 if (port->lun_map == NULL) 3470 return (lun_id); 3471 return (port->lun_map(port->targ_lun_arg, lun_id)); 3472 } 3473 3474 static uint32_t 3475 ctl_map_lun_back(int port_num, uint32_t lun_id) 3476 { 3477 struct ctl_port *port; 3478 uint32_t i; 3479 3480 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3481 if (port->lun_map == NULL) 3482 return (lun_id); 3483 for (i = 0; i < CTL_MAX_LUNS; i++) { 3484 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3485 return (i); 3486 } 3487 return (UINT32_MAX); 3488 } 3489 3490 /* 3491 * Note: This only works for bitmask sizes that are at least 32 bits, and 3492 * that are a power of 2. 3493 */ 3494 int 3495 ctl_ffz(uint32_t *mask, uint32_t size) 3496 { 3497 uint32_t num_chunks, num_pieces; 3498 int i, j; 3499 3500 num_chunks = (size >> 5); 3501 if (num_chunks == 0) 3502 num_chunks++; 3503 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3504 3505 for (i = 0; i < num_chunks; i++) { 3506 for (j = 0; j < num_pieces; j++) { 3507 if ((mask[i] & (1 << j)) == 0) 3508 return ((i << 5) + j); 3509 } 3510 } 3511 3512 return (-1); 3513 } 3514 3515 int 3516 ctl_set_mask(uint32_t *mask, uint32_t bit) 3517 { 3518 uint32_t chunk, piece; 3519 3520 chunk = bit >> 5; 3521 piece = bit % (sizeof(uint32_t) * 8); 3522 3523 if ((mask[chunk] & (1 << piece)) != 0) 3524 return (-1); 3525 else 3526 mask[chunk] |= (1 << piece); 3527 3528 return (0); 3529 } 3530 3531 int 3532 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3533 { 3534 uint32_t chunk, piece; 3535 3536 chunk = bit >> 5; 3537 piece = bit % (sizeof(uint32_t) * 8); 3538 3539 if ((mask[chunk] & (1 << piece)) == 0) 3540 return (-1); 3541 else 3542 mask[chunk] &= ~(1 << piece); 3543 3544 return (0); 3545 } 3546 3547 int 3548 ctl_is_set(uint32_t *mask, uint32_t bit) 3549 { 3550 uint32_t chunk, piece; 3551 3552 chunk = bit >> 5; 3553 piece = bit % (sizeof(uint32_t) * 8); 3554 3555 if ((mask[chunk] & (1 << piece)) == 0) 3556 return (0); 3557 else 3558 return (1); 3559 } 3560 3561 #ifdef unused 3562 /* 3563 * The bus, target and lun are optional, they can be filled in later. 3564 * can_wait is used to determine whether we can wait on the malloc or not. 3565 */ 3566 union ctl_io* 3567 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3568 uint32_t targ_lun, int can_wait) 3569 { 3570 union ctl_io *io; 3571 3572 if (can_wait) 3573 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3574 else 3575 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3576 3577 if (io != NULL) { 3578 io->io_hdr.io_type = io_type; 3579 io->io_hdr.targ_port = targ_port; 3580 /* 3581 * XXX KDM this needs to change/go away. We need to move 3582 * to a preallocated pool of ctl_scsiio structures. 3583 */ 3584 io->io_hdr.nexus.targ_target.id = targ_target; 3585 io->io_hdr.nexus.targ_lun = targ_lun; 3586 } 3587 3588 return (io); 3589 } 3590 3591 void 3592 ctl_kfree_io(union ctl_io *io) 3593 { 3594 free(io, M_CTL); 3595 } 3596 #endif /* unused */ 3597 3598 /* 3599 * ctl_softc, pool_type, total_ctl_io are passed in. 3600 * npool is passed out. 3601 */ 3602 int 3603 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3604 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3605 { 3606 uint32_t i; 3607 union ctl_io *cur_io, *next_io; 3608 struct ctl_io_pool *pool; 3609 int retval; 3610 3611 retval = 0; 3612 3613 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3614 M_NOWAIT | M_ZERO); 3615 if (pool == NULL) { 3616 retval = ENOMEM; 3617 goto bailout; 3618 } 3619 3620 pool->type = pool_type; 3621 pool->ctl_softc = ctl_softc; 3622 3623 mtx_lock(&ctl_softc->pool_lock); 3624 pool->id = ctl_softc->cur_pool_id++; 3625 mtx_unlock(&ctl_softc->pool_lock); 3626 3627 pool->flags = CTL_POOL_FLAG_NONE; 3628 pool->refcount = 1; /* Reference for validity. */ 3629 STAILQ_INIT(&pool->free_queue); 3630 3631 /* 3632 * XXX KDM other options here: 3633 * - allocate a page at a time 3634 * - allocate one big chunk of memory. 3635 * Page allocation might work well, but would take a little more 3636 * tracking. 3637 */ 3638 for (i = 0; i < total_ctl_io; i++) { 3639 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3640 M_NOWAIT); 3641 if (cur_io == NULL) { 3642 retval = ENOMEM; 3643 break; 3644 } 3645 cur_io->io_hdr.pool = pool; 3646 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3647 pool->total_ctl_io++; 3648 pool->free_ctl_io++; 3649 } 3650 3651 if (retval != 0) { 3652 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3653 cur_io != NULL; cur_io = next_io) { 3654 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3655 links); 3656 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3657 ctl_io_hdr, links); 3658 free(cur_io, M_CTLIO); 3659 } 3660 3661 free(pool, M_CTL); 3662 goto bailout; 3663 } 3664 mtx_lock(&ctl_softc->pool_lock); 3665 ctl_softc->num_pools++; 3666 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3667 /* 3668 * Increment our usage count if this is an external consumer, so we 3669 * can't get unloaded until the external consumer (most likely a 3670 * FETD) unloads and frees his pool. 3671 * 3672 * XXX KDM will this increment the caller's module use count, or 3673 * mine? 3674 */ 3675 #if 0 3676 if ((pool_type != CTL_POOL_EMERGENCY) 3677 && (pool_type != CTL_POOL_INTERNAL) 3678 && (pool_type != CTL_POOL_4OTHERSC)) 3679 MOD_INC_USE_COUNT; 3680 #endif 3681 3682 mtx_unlock(&ctl_softc->pool_lock); 3683 3684 *npool = pool; 3685 3686 bailout: 3687 3688 return (retval); 3689 } 3690 3691 static int 3692 ctl_pool_acquire(struct ctl_io_pool *pool) 3693 { 3694 3695 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3696 3697 if (pool->flags & CTL_POOL_FLAG_INVALID) 3698 return (EINVAL); 3699 3700 pool->refcount++; 3701 3702 return (0); 3703 } 3704 3705 static void 3706 ctl_pool_release(struct ctl_io_pool *pool) 3707 { 3708 struct ctl_softc *ctl_softc = pool->ctl_softc; 3709 union ctl_io *io; 3710 3711 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3712 3713 if (--pool->refcount != 0) 3714 return; 3715 3716 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3717 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3718 links); 3719 free(io, M_CTLIO); 3720 } 3721 3722 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3723 ctl_softc->num_pools--; 3724 3725 /* 3726 * XXX KDM will this decrement the caller's usage count or mine? 3727 */ 3728 #if 0 3729 if ((pool->type != CTL_POOL_EMERGENCY) 3730 && (pool->type != CTL_POOL_INTERNAL) 3731 && (pool->type != CTL_POOL_4OTHERSC)) 3732 MOD_DEC_USE_COUNT; 3733 #endif 3734 3735 free(pool, M_CTL); 3736 } 3737 3738 void 3739 ctl_pool_free(struct ctl_io_pool *pool) 3740 { 3741 struct ctl_softc *ctl_softc; 3742 3743 if (pool == NULL) 3744 return; 3745 3746 ctl_softc = pool->ctl_softc; 3747 mtx_lock(&ctl_softc->pool_lock); 3748 pool->flags |= CTL_POOL_FLAG_INVALID; 3749 ctl_pool_release(pool); 3750 mtx_unlock(&ctl_softc->pool_lock); 3751 } 3752 3753 /* 3754 * This routine does not block (except for spinlocks of course). 3755 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3756 * possible. 3757 */ 3758 union ctl_io * 3759 ctl_alloc_io(void *pool_ref) 3760 { 3761 union ctl_io *io; 3762 struct ctl_softc *ctl_softc; 3763 struct ctl_io_pool *pool, *npool; 3764 struct ctl_io_pool *emergency_pool; 3765 3766 pool = (struct ctl_io_pool *)pool_ref; 3767 3768 if (pool == NULL) { 3769 printf("%s: pool is NULL\n", __func__); 3770 return (NULL); 3771 } 3772 3773 emergency_pool = NULL; 3774 3775 ctl_softc = pool->ctl_softc; 3776 3777 mtx_lock(&ctl_softc->pool_lock); 3778 /* 3779 * First, try to get the io structure from the user's pool. 3780 */ 3781 if (ctl_pool_acquire(pool) == 0) { 3782 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3783 if (io != NULL) { 3784 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3785 pool->total_allocated++; 3786 pool->free_ctl_io--; 3787 mtx_unlock(&ctl_softc->pool_lock); 3788 return (io); 3789 } else 3790 ctl_pool_release(pool); 3791 } 3792 /* 3793 * If he doesn't have any io structures left, search for an 3794 * emergency pool and grab one from there. 3795 */ 3796 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3797 if (npool->type != CTL_POOL_EMERGENCY) 3798 continue; 3799 3800 if (ctl_pool_acquire(npool) != 0) 3801 continue; 3802 3803 emergency_pool = npool; 3804 3805 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3806 if (io != NULL) { 3807 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3808 npool->total_allocated++; 3809 npool->free_ctl_io--; 3810 mtx_unlock(&ctl_softc->pool_lock); 3811 return (io); 3812 } else 3813 ctl_pool_release(npool); 3814 } 3815 3816 /* Drop the spinlock before we malloc */ 3817 mtx_unlock(&ctl_softc->pool_lock); 3818 3819 /* 3820 * The emergency pool (if it exists) didn't have one, so try an 3821 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3822 */ 3823 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3824 if (io != NULL) { 3825 /* 3826 * If the emergency pool exists but is empty, add this 3827 * ctl_io to its list when it gets freed. 3828 */ 3829 if (emergency_pool != NULL) { 3830 mtx_lock(&ctl_softc->pool_lock); 3831 if (ctl_pool_acquire(emergency_pool) == 0) { 3832 io->io_hdr.pool = emergency_pool; 3833 emergency_pool->total_ctl_io++; 3834 /* 3835 * Need to bump this, otherwise 3836 * total_allocated and total_freed won't 3837 * match when we no longer have anything 3838 * outstanding. 3839 */ 3840 emergency_pool->total_allocated++; 3841 } 3842 mtx_unlock(&ctl_softc->pool_lock); 3843 } else 3844 io->io_hdr.pool = NULL; 3845 } 3846 3847 return (io); 3848 } 3849 3850 void 3851 ctl_free_io(union ctl_io *io) 3852 { 3853 if (io == NULL) 3854 return; 3855 3856 /* 3857 * If this ctl_io has a pool, return it to that pool. 3858 */ 3859 if (io->io_hdr.pool != NULL) { 3860 struct ctl_io_pool *pool; 3861 3862 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3863 mtx_lock(&pool->ctl_softc->pool_lock); 3864 io->io_hdr.io_type = 0xff; 3865 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3866 pool->total_freed++; 3867 pool->free_ctl_io++; 3868 ctl_pool_release(pool); 3869 mtx_unlock(&pool->ctl_softc->pool_lock); 3870 } else { 3871 /* 3872 * Otherwise, just free it. We probably malloced it and 3873 * the emergency pool wasn't available. 3874 */ 3875 free(io, M_CTLIO); 3876 } 3877 3878 } 3879 3880 void 3881 ctl_zero_io(union ctl_io *io) 3882 { 3883 void *pool_ref; 3884 3885 if (io == NULL) 3886 return; 3887 3888 /* 3889 * May need to preserve linked list pointers at some point too. 3890 */ 3891 pool_ref = io->io_hdr.pool; 3892 3893 memset(io, 0, sizeof(*io)); 3894 3895 io->io_hdr.pool = pool_ref; 3896 } 3897 3898 /* 3899 * This routine is currently used for internal copies of ctl_ios that need 3900 * to persist for some reason after we've already returned status to the 3901 * FETD. (Thus the flag set.) 3902 * 3903 * XXX XXX 3904 * Note that this makes a blind copy of all fields in the ctl_io, except 3905 * for the pool reference. This includes any memory that has been 3906 * allocated! That memory will no longer be valid after done has been 3907 * called, so this would be VERY DANGEROUS for command that actually does 3908 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3909 * start and stop commands, which don't transfer any data, so this is not a 3910 * problem. If it is used for anything else, the caller would also need to 3911 * allocate data buffer space and this routine would need to be modified to 3912 * copy the data buffer(s) as well. 3913 */ 3914 void 3915 ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3916 { 3917 void *pool_ref; 3918 3919 if ((src == NULL) 3920 || (dest == NULL)) 3921 return; 3922 3923 /* 3924 * May need to preserve linked list pointers at some point too. 3925 */ 3926 pool_ref = dest->io_hdr.pool; 3927 3928 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3929 3930 dest->io_hdr.pool = pool_ref; 3931 /* 3932 * We need to know that this is an internal copy, and doesn't need 3933 * to get passed back to the FETD that allocated it. 3934 */ 3935 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3936 } 3937 3938 #ifdef NEEDTOPORT 3939 static void 3940 ctl_update_power_subpage(struct copan_power_subpage *page) 3941 { 3942 int num_luns, num_partitions, config_type; 3943 struct ctl_softc *softc; 3944 cs_BOOL_t aor_present, shelf_50pct_power; 3945 cs_raidset_personality_t rs_type; 3946 int max_active_luns; 3947 3948 softc = control_softc; 3949 3950 /* subtract out the processor LUN */ 3951 num_luns = softc->num_luns - 1; 3952 /* 3953 * Default to 7 LUNs active, which was the only number we allowed 3954 * in the past. 3955 */ 3956 max_active_luns = 7; 3957 3958 num_partitions = config_GetRsPartitionInfo(); 3959 config_type = config_GetConfigType(); 3960 shelf_50pct_power = config_GetShelfPowerMode(); 3961 aor_present = config_IsAorRsPresent(); 3962 3963 rs_type = ddb_GetRsRaidType(1); 3964 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3965 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3966 EPRINT(0, "Unsupported RS type %d!", rs_type); 3967 } 3968 3969 3970 page->total_luns = num_luns; 3971 3972 switch (config_type) { 3973 case 40: 3974 /* 3975 * In a 40 drive configuration, it doesn't matter what DC 3976 * cards we have, whether we have AOR enabled or not, 3977 * partitioning or not, or what type of RAIDset we have. 3978 * In that scenario, we can power up every LUN we present 3979 * to the user. 3980 */ 3981 max_active_luns = num_luns; 3982 3983 break; 3984 case 64: 3985 if (shelf_50pct_power == CS_FALSE) { 3986 /* 25% power */ 3987 if (aor_present == CS_TRUE) { 3988 if (rs_type == 3989 CS_RAIDSET_PERSONALITY_RAID5) { 3990 max_active_luns = 7; 3991 } else if (rs_type == 3992 CS_RAIDSET_PERSONALITY_RAID1){ 3993 max_active_luns = 14; 3994 } else { 3995 /* XXX KDM now what?? */ 3996 } 3997 } else { 3998 if (rs_type == 3999 CS_RAIDSET_PERSONALITY_RAID5) { 4000 max_active_luns = 8; 4001 } else if (rs_type == 4002 CS_RAIDSET_PERSONALITY_RAID1){ 4003 max_active_luns = 16; 4004 } else { 4005 /* XXX KDM now what?? */ 4006 } 4007 } 4008 } else { 4009 /* 50% power */ 4010 /* 4011 * With 50% power in a 64 drive configuration, we 4012 * can power all LUNs we present. 4013 */ 4014 max_active_luns = num_luns; 4015 } 4016 break; 4017 case 112: 4018 if (shelf_50pct_power == CS_FALSE) { 4019 /* 25% power */ 4020 if (aor_present == CS_TRUE) { 4021 if (rs_type == 4022 CS_RAIDSET_PERSONALITY_RAID5) { 4023 max_active_luns = 7; 4024 } else if (rs_type == 4025 CS_RAIDSET_PERSONALITY_RAID1){ 4026 max_active_luns = 14; 4027 } else { 4028 /* XXX KDM now what?? */ 4029 } 4030 } else { 4031 if (rs_type == 4032 CS_RAIDSET_PERSONALITY_RAID5) { 4033 max_active_luns = 8; 4034 } else if (rs_type == 4035 CS_RAIDSET_PERSONALITY_RAID1){ 4036 max_active_luns = 16; 4037 } else { 4038 /* XXX KDM now what?? */ 4039 } 4040 } 4041 } else { 4042 /* 50% power */ 4043 if (aor_present == CS_TRUE) { 4044 if (rs_type == 4045 CS_RAIDSET_PERSONALITY_RAID5) { 4046 max_active_luns = 14; 4047 } else if (rs_type == 4048 CS_RAIDSET_PERSONALITY_RAID1){ 4049 /* 4050 * We're assuming here that disk 4051 * caching is enabled, and so we're 4052 * able to power up half of each 4053 * LUN, and cache all writes. 4054 */ 4055 max_active_luns = num_luns; 4056 } else { 4057 /* XXX KDM now what?? */ 4058 } 4059 } else { 4060 if (rs_type == 4061 CS_RAIDSET_PERSONALITY_RAID5) { 4062 max_active_luns = 15; 4063 } else if (rs_type == 4064 CS_RAIDSET_PERSONALITY_RAID1){ 4065 max_active_luns = 30; 4066 } else { 4067 /* XXX KDM now what?? */ 4068 } 4069 } 4070 } 4071 break; 4072 default: 4073 /* 4074 * In this case, we have an unknown configuration, so we 4075 * just use the default from above. 4076 */ 4077 break; 4078 } 4079 4080 page->max_active_luns = max_active_luns; 4081 #if 0 4082 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 4083 page->total_luns, page->max_active_luns); 4084 #endif 4085 } 4086 #endif /* NEEDTOPORT */ 4087 4088 /* 4089 * This routine could be used in the future to load default and/or saved 4090 * mode page parameters for a particuar lun. 4091 */ 4092 static int 4093 ctl_init_page_index(struct ctl_lun *lun) 4094 { 4095 int i; 4096 struct ctl_page_index *page_index; 4097 struct ctl_softc *softc; 4098 const char *value; 4099 4100 memcpy(&lun->mode_pages.index, page_index_template, 4101 sizeof(page_index_template)); 4102 4103 softc = lun->ctl_softc; 4104 4105 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4106 4107 page_index = &lun->mode_pages.index[i]; 4108 /* 4109 * If this is a disk-only mode page, there's no point in 4110 * setting it up. For some pages, we have to have some 4111 * basic information about the disk in order to calculate the 4112 * mode page data. 4113 */ 4114 if ((lun->be_lun->lun_type != T_DIRECT) 4115 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4116 continue; 4117 4118 switch (page_index->page_code & SMPH_PC_MASK) { 4119 case SMS_FORMAT_DEVICE_PAGE: { 4120 struct scsi_format_page *format_page; 4121 4122 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4123 panic("subpage is incorrect!"); 4124 4125 /* 4126 * Sectors per track are set above. Bytes per 4127 * sector need to be set here on a per-LUN basis. 4128 */ 4129 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4130 &format_page_default, 4131 sizeof(format_page_default)); 4132 memcpy(&lun->mode_pages.format_page[ 4133 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4134 sizeof(format_page_changeable)); 4135 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4136 &format_page_default, 4137 sizeof(format_page_default)); 4138 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4139 &format_page_default, 4140 sizeof(format_page_default)); 4141 4142 format_page = &lun->mode_pages.format_page[ 4143 CTL_PAGE_CURRENT]; 4144 scsi_ulto2b(lun->be_lun->blocksize, 4145 format_page->bytes_per_sector); 4146 4147 format_page = &lun->mode_pages.format_page[ 4148 CTL_PAGE_DEFAULT]; 4149 scsi_ulto2b(lun->be_lun->blocksize, 4150 format_page->bytes_per_sector); 4151 4152 format_page = &lun->mode_pages.format_page[ 4153 CTL_PAGE_SAVED]; 4154 scsi_ulto2b(lun->be_lun->blocksize, 4155 format_page->bytes_per_sector); 4156 4157 page_index->page_data = 4158 (uint8_t *)lun->mode_pages.format_page; 4159 break; 4160 } 4161 case SMS_RIGID_DISK_PAGE: { 4162 struct scsi_rigid_disk_page *rigid_disk_page; 4163 uint32_t sectors_per_cylinder; 4164 uint64_t cylinders; 4165 #ifndef __XSCALE__ 4166 int shift; 4167 #endif /* !__XSCALE__ */ 4168 4169 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4170 panic("invalid subpage value %d", 4171 page_index->subpage); 4172 4173 /* 4174 * Rotation rate and sectors per track are set 4175 * above. We calculate the cylinders here based on 4176 * capacity. Due to the number of heads and 4177 * sectors per track we're using, smaller arrays 4178 * may turn out to have 0 cylinders. Linux and 4179 * FreeBSD don't pay attention to these mode pages 4180 * to figure out capacity, but Solaris does. It 4181 * seems to deal with 0 cylinders just fine, and 4182 * works out a fake geometry based on the capacity. 4183 */ 4184 memcpy(&lun->mode_pages.rigid_disk_page[ 4185 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4186 sizeof(rigid_disk_page_default)); 4187 memcpy(&lun->mode_pages.rigid_disk_page[ 4188 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4189 sizeof(rigid_disk_page_changeable)); 4190 memcpy(&lun->mode_pages.rigid_disk_page[ 4191 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4192 sizeof(rigid_disk_page_default)); 4193 memcpy(&lun->mode_pages.rigid_disk_page[ 4194 CTL_PAGE_SAVED], &rigid_disk_page_default, 4195 sizeof(rigid_disk_page_default)); 4196 4197 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4198 CTL_DEFAULT_HEADS; 4199 4200 /* 4201 * The divide method here will be more accurate, 4202 * probably, but results in floating point being 4203 * used in the kernel on i386 (__udivdi3()). On the 4204 * XScale, though, __udivdi3() is implemented in 4205 * software. 4206 * 4207 * The shift method for cylinder calculation is 4208 * accurate if sectors_per_cylinder is a power of 4209 * 2. Otherwise it might be slightly off -- you 4210 * might have a bit of a truncation problem. 4211 */ 4212 #ifdef __XSCALE__ 4213 cylinders = (lun->be_lun->maxlba + 1) / 4214 sectors_per_cylinder; 4215 #else 4216 for (shift = 31; shift > 0; shift--) { 4217 if (sectors_per_cylinder & (1 << shift)) 4218 break; 4219 } 4220 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4221 #endif 4222 4223 /* 4224 * We've basically got 3 bytes, or 24 bits for the 4225 * cylinder size in the mode page. If we're over, 4226 * just round down to 2^24. 4227 */ 4228 if (cylinders > 0xffffff) 4229 cylinders = 0xffffff; 4230 4231 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4232 CTL_PAGE_CURRENT]; 4233 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4234 4235 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4236 CTL_PAGE_DEFAULT]; 4237 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4238 4239 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4240 CTL_PAGE_SAVED]; 4241 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4242 4243 page_index->page_data = 4244 (uint8_t *)lun->mode_pages.rigid_disk_page; 4245 break; 4246 } 4247 case SMS_CACHING_PAGE: { 4248 struct scsi_caching_page *caching_page; 4249 4250 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4251 panic("invalid subpage value %d", 4252 page_index->subpage); 4253 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4254 &caching_page_default, 4255 sizeof(caching_page_default)); 4256 memcpy(&lun->mode_pages.caching_page[ 4257 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4258 sizeof(caching_page_changeable)); 4259 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4260 &caching_page_default, 4261 sizeof(caching_page_default)); 4262 caching_page = &lun->mode_pages.caching_page[ 4263 CTL_PAGE_SAVED]; 4264 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4265 if (value != NULL && strcmp(value, "off") == 0) 4266 caching_page->flags1 &= ~SCP_WCE; 4267 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4268 if (value != NULL && strcmp(value, "off") == 0) 4269 caching_page->flags1 |= SCP_RCD; 4270 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4271 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4272 sizeof(caching_page_default)); 4273 page_index->page_data = 4274 (uint8_t *)lun->mode_pages.caching_page; 4275 break; 4276 } 4277 case SMS_CONTROL_MODE_PAGE: { 4278 struct scsi_control_page *control_page; 4279 4280 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4281 panic("invalid subpage value %d", 4282 page_index->subpage); 4283 4284 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4285 &control_page_default, 4286 sizeof(control_page_default)); 4287 memcpy(&lun->mode_pages.control_page[ 4288 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4289 sizeof(control_page_changeable)); 4290 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4291 &control_page_default, 4292 sizeof(control_page_default)); 4293 control_page = &lun->mode_pages.control_page[ 4294 CTL_PAGE_SAVED]; 4295 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4296 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4297 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4298 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4299 } 4300 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4301 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4302 sizeof(control_page_default)); 4303 page_index->page_data = 4304 (uint8_t *)lun->mode_pages.control_page; 4305 break; 4306 4307 } 4308 case SMS_VENDOR_SPECIFIC_PAGE:{ 4309 switch (page_index->subpage) { 4310 case PWR_SUBPAGE_CODE: { 4311 struct copan_power_subpage *current_page, 4312 *saved_page; 4313 4314 memcpy(&lun->mode_pages.power_subpage[ 4315 CTL_PAGE_CURRENT], 4316 &power_page_default, 4317 sizeof(power_page_default)); 4318 memcpy(&lun->mode_pages.power_subpage[ 4319 CTL_PAGE_CHANGEABLE], 4320 &power_page_changeable, 4321 sizeof(power_page_changeable)); 4322 memcpy(&lun->mode_pages.power_subpage[ 4323 CTL_PAGE_DEFAULT], 4324 &power_page_default, 4325 sizeof(power_page_default)); 4326 memcpy(&lun->mode_pages.power_subpage[ 4327 CTL_PAGE_SAVED], 4328 &power_page_default, 4329 sizeof(power_page_default)); 4330 page_index->page_data = 4331 (uint8_t *)lun->mode_pages.power_subpage; 4332 4333 current_page = (struct copan_power_subpage *) 4334 (page_index->page_data + 4335 (page_index->page_len * 4336 CTL_PAGE_CURRENT)); 4337 saved_page = (struct copan_power_subpage *) 4338 (page_index->page_data + 4339 (page_index->page_len * 4340 CTL_PAGE_SAVED)); 4341 break; 4342 } 4343 case APS_SUBPAGE_CODE: { 4344 struct copan_aps_subpage *current_page, 4345 *saved_page; 4346 4347 // This gets set multiple times but 4348 // it should always be the same. It's 4349 // only done during init so who cares. 4350 index_to_aps_page = i; 4351 4352 memcpy(&lun->mode_pages.aps_subpage[ 4353 CTL_PAGE_CURRENT], 4354 &aps_page_default, 4355 sizeof(aps_page_default)); 4356 memcpy(&lun->mode_pages.aps_subpage[ 4357 CTL_PAGE_CHANGEABLE], 4358 &aps_page_changeable, 4359 sizeof(aps_page_changeable)); 4360 memcpy(&lun->mode_pages.aps_subpage[ 4361 CTL_PAGE_DEFAULT], 4362 &aps_page_default, 4363 sizeof(aps_page_default)); 4364 memcpy(&lun->mode_pages.aps_subpage[ 4365 CTL_PAGE_SAVED], 4366 &aps_page_default, 4367 sizeof(aps_page_default)); 4368 page_index->page_data = 4369 (uint8_t *)lun->mode_pages.aps_subpage; 4370 4371 current_page = (struct copan_aps_subpage *) 4372 (page_index->page_data + 4373 (page_index->page_len * 4374 CTL_PAGE_CURRENT)); 4375 saved_page = (struct copan_aps_subpage *) 4376 (page_index->page_data + 4377 (page_index->page_len * 4378 CTL_PAGE_SAVED)); 4379 break; 4380 } 4381 case DBGCNF_SUBPAGE_CODE: { 4382 struct copan_debugconf_subpage *current_page, 4383 *saved_page; 4384 4385 memcpy(&lun->mode_pages.debugconf_subpage[ 4386 CTL_PAGE_CURRENT], 4387 &debugconf_page_default, 4388 sizeof(debugconf_page_default)); 4389 memcpy(&lun->mode_pages.debugconf_subpage[ 4390 CTL_PAGE_CHANGEABLE], 4391 &debugconf_page_changeable, 4392 sizeof(debugconf_page_changeable)); 4393 memcpy(&lun->mode_pages.debugconf_subpage[ 4394 CTL_PAGE_DEFAULT], 4395 &debugconf_page_default, 4396 sizeof(debugconf_page_default)); 4397 memcpy(&lun->mode_pages.debugconf_subpage[ 4398 CTL_PAGE_SAVED], 4399 &debugconf_page_default, 4400 sizeof(debugconf_page_default)); 4401 page_index->page_data = 4402 (uint8_t *)lun->mode_pages.debugconf_subpage; 4403 4404 current_page = (struct copan_debugconf_subpage *) 4405 (page_index->page_data + 4406 (page_index->page_len * 4407 CTL_PAGE_CURRENT)); 4408 saved_page = (struct copan_debugconf_subpage *) 4409 (page_index->page_data + 4410 (page_index->page_len * 4411 CTL_PAGE_SAVED)); 4412 break; 4413 } 4414 default: 4415 panic("invalid subpage value %d", 4416 page_index->subpage); 4417 break; 4418 } 4419 break; 4420 } 4421 default: 4422 panic("invalid page value %d", 4423 page_index->page_code & SMPH_PC_MASK); 4424 break; 4425 } 4426 } 4427 4428 return (CTL_RETVAL_COMPLETE); 4429 } 4430 4431 /* 4432 * LUN allocation. 4433 * 4434 * Requirements: 4435 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4436 * wants us to allocate the LUN and he can block. 4437 * - ctl_softc is always set 4438 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4439 * 4440 * Returns 0 for success, non-zero (errno) for failure. 4441 */ 4442 static int 4443 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4444 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4445 { 4446 struct ctl_lun *nlun, *lun; 4447 struct ctl_port *port; 4448 struct scsi_vpd_id_descriptor *desc; 4449 struct scsi_vpd_id_t10 *t10id; 4450 const char *eui, *naa, *scsiname, *vendor; 4451 int lun_number, i, lun_malloced; 4452 int devidlen, idlen1, idlen2 = 0, len; 4453 4454 if (be_lun == NULL) 4455 return (EINVAL); 4456 4457 /* 4458 * We currently only support Direct Access or Processor LUN types. 4459 */ 4460 switch (be_lun->lun_type) { 4461 case T_DIRECT: 4462 break; 4463 case T_PROCESSOR: 4464 break; 4465 case T_SEQUENTIAL: 4466 case T_CHANGER: 4467 default: 4468 be_lun->lun_config_status(be_lun->be_lun, 4469 CTL_LUN_CONFIG_FAILURE); 4470 break; 4471 } 4472 if (ctl_lun == NULL) { 4473 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4474 lun_malloced = 1; 4475 } else { 4476 lun_malloced = 0; 4477 lun = ctl_lun; 4478 } 4479 4480 memset(lun, 0, sizeof(*lun)); 4481 if (lun_malloced) 4482 lun->flags = CTL_LUN_MALLOCED; 4483 4484 /* Generate LUN ID. */ 4485 devidlen = max(CTL_DEVID_MIN_LEN, 4486 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4487 idlen1 = sizeof(*t10id) + devidlen; 4488 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4489 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4490 if (scsiname != NULL) { 4491 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4492 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4493 } 4494 eui = ctl_get_opt(&be_lun->options, "eui"); 4495 if (eui != NULL) { 4496 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4497 } 4498 naa = ctl_get_opt(&be_lun->options, "naa"); 4499 if (naa != NULL) { 4500 len += sizeof(struct scsi_vpd_id_descriptor) + 8; 4501 } 4502 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4503 M_CTL, M_WAITOK | M_ZERO); 4504 lun->lun_devid->len = len; 4505 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4506 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4507 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4508 desc->length = idlen1; 4509 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4510 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4511 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4512 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4513 } else { 4514 strncpy(t10id->vendor, vendor, 4515 min(sizeof(t10id->vendor), strlen(vendor))); 4516 } 4517 strncpy((char *)t10id->vendor_spec_id, 4518 (char *)be_lun->device_id, devidlen); 4519 if (scsiname != NULL) { 4520 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4521 desc->length); 4522 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4523 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4524 SVPD_ID_TYPE_SCSI_NAME; 4525 desc->length = idlen2; 4526 strlcpy(desc->identifier, scsiname, idlen2); 4527 } 4528 if (eui != NULL) { 4529 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4530 desc->length); 4531 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4532 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4533 SVPD_ID_TYPE_EUI64; 4534 desc->length = 8; 4535 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier); 4536 } 4537 if (naa != NULL) { 4538 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4539 desc->length); 4540 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4541 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4542 SVPD_ID_TYPE_NAA; 4543 desc->length = 8; 4544 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier); 4545 } 4546 4547 mtx_lock(&ctl_softc->ctl_lock); 4548 /* 4549 * See if the caller requested a particular LUN number. If so, see 4550 * if it is available. Otherwise, allocate the first available LUN. 4551 */ 4552 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4553 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4554 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4555 mtx_unlock(&ctl_softc->ctl_lock); 4556 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4557 printf("ctl: requested LUN ID %d is higher " 4558 "than CTL_MAX_LUNS - 1 (%d)\n", 4559 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4560 } else { 4561 /* 4562 * XXX KDM return an error, or just assign 4563 * another LUN ID in this case?? 4564 */ 4565 printf("ctl: requested LUN ID %d is already " 4566 "in use\n", be_lun->req_lun_id); 4567 } 4568 if (lun->flags & CTL_LUN_MALLOCED) 4569 free(lun, M_CTL); 4570 be_lun->lun_config_status(be_lun->be_lun, 4571 CTL_LUN_CONFIG_FAILURE); 4572 return (ENOSPC); 4573 } 4574 lun_number = be_lun->req_lun_id; 4575 } else { 4576 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4577 if (lun_number == -1) { 4578 mtx_unlock(&ctl_softc->ctl_lock); 4579 printf("ctl: can't allocate LUN on target %ju, out of " 4580 "LUNs\n", (uintmax_t)target_id.id); 4581 if (lun->flags & CTL_LUN_MALLOCED) 4582 free(lun, M_CTL); 4583 be_lun->lun_config_status(be_lun->be_lun, 4584 CTL_LUN_CONFIG_FAILURE); 4585 return (ENOSPC); 4586 } 4587 } 4588 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4589 4590 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4591 lun->target = target_id; 4592 lun->lun = lun_number; 4593 lun->be_lun = be_lun; 4594 /* 4595 * The processor LUN is always enabled. Disk LUNs come on line 4596 * disabled, and must be enabled by the backend. 4597 */ 4598 lun->flags |= CTL_LUN_DISABLED; 4599 lun->backend = be_lun->be; 4600 be_lun->ctl_lun = lun; 4601 be_lun->lun_id = lun_number; 4602 atomic_add_int(&be_lun->be->num_luns, 1); 4603 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4604 lun->flags |= CTL_LUN_STOPPED; 4605 4606 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4607 lun->flags |= CTL_LUN_INOPERABLE; 4608 4609 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4610 lun->flags |= CTL_LUN_PRIMARY_SC; 4611 4612 lun->ctl_softc = ctl_softc; 4613 TAILQ_INIT(&lun->ooa_queue); 4614 TAILQ_INIT(&lun->blocked_queue); 4615 STAILQ_INIT(&lun->error_list); 4616 ctl_tpc_lun_init(lun); 4617 4618 /* 4619 * Initialize the mode page index. 4620 */ 4621 ctl_init_page_index(lun); 4622 4623 /* 4624 * Set the poweron UA for all initiators on this LUN only. 4625 */ 4626 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4627 lun->pending_ua[i] = CTL_UA_POWERON; 4628 4629 /* 4630 * Now, before we insert this lun on the lun list, set the lun 4631 * inventory changed UA for all other luns. 4632 */ 4633 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4634 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4635 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4636 } 4637 } 4638 4639 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4640 4641 ctl_softc->ctl_luns[lun_number] = lun; 4642 4643 ctl_softc->num_luns++; 4644 4645 /* Setup statistics gathering */ 4646 lun->stats.device_type = be_lun->lun_type; 4647 lun->stats.lun_number = lun_number; 4648 if (lun->stats.device_type == T_DIRECT) 4649 lun->stats.blocksize = be_lun->blocksize; 4650 else 4651 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4652 for (i = 0;i < CTL_MAX_PORTS;i++) 4653 lun->stats.ports[i].targ_port = i; 4654 4655 mtx_unlock(&ctl_softc->ctl_lock); 4656 4657 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4658 4659 /* 4660 * Run through each registered FETD and bring it online if it isn't 4661 * already. Enable the target ID if it hasn't been enabled, and 4662 * enable this particular LUN. 4663 */ 4664 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4665 int retval; 4666 4667 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4668 if (retval != 0) { 4669 printf("ctl_alloc_lun: FETD %s port %d returned error " 4670 "%d for lun_enable on target %ju lun %d\n", 4671 port->port_name, port->targ_port, retval, 4672 (uintmax_t)target_id.id, lun_number); 4673 } else 4674 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4675 } 4676 return (0); 4677 } 4678 4679 /* 4680 * Delete a LUN. 4681 * Assumptions: 4682 * - LUN has already been marked invalid and any pending I/O has been taken 4683 * care of. 4684 */ 4685 static int 4686 ctl_free_lun(struct ctl_lun *lun) 4687 { 4688 struct ctl_softc *softc; 4689 #if 0 4690 struct ctl_port *port; 4691 #endif 4692 struct ctl_lun *nlun; 4693 int i; 4694 4695 softc = lun->ctl_softc; 4696 4697 mtx_assert(&softc->ctl_lock, MA_OWNED); 4698 4699 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4700 4701 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4702 4703 softc->ctl_luns[lun->lun] = NULL; 4704 4705 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4706 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4707 4708 softc->num_luns--; 4709 4710 /* 4711 * XXX KDM this scheme only works for a single target/multiple LUN 4712 * setup. It needs to be revamped for a multiple target scheme. 4713 * 4714 * XXX KDM this results in port->lun_disable() getting called twice, 4715 * once when ctl_disable_lun() is called, and a second time here. 4716 * We really need to re-think the LUN disable semantics. There 4717 * should probably be several steps/levels to LUN removal: 4718 * - disable 4719 * - invalidate 4720 * - free 4721 * 4722 * Right now we only have a disable method when communicating to 4723 * the front end ports, at least for individual LUNs. 4724 */ 4725 #if 0 4726 STAILQ_FOREACH(port, &softc->port_list, links) { 4727 int retval; 4728 4729 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4730 lun->lun); 4731 if (retval != 0) { 4732 printf("ctl_free_lun: FETD %s port %d returned error " 4733 "%d for lun_disable on target %ju lun %jd\n", 4734 port->port_name, port->targ_port, retval, 4735 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4736 } 4737 4738 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4739 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4740 4741 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4742 if (retval != 0) { 4743 printf("ctl_free_lun: FETD %s port %d " 4744 "returned error %d for targ_disable on " 4745 "target %ju\n", port->port_name, 4746 port->targ_port, retval, 4747 (uintmax_t)lun->target.id); 4748 } else 4749 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4750 4751 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4752 continue; 4753 4754 #if 0 4755 port->port_offline(port->onoff_arg); 4756 port->status &= ~CTL_PORT_STATUS_ONLINE; 4757 #endif 4758 } 4759 } 4760 #endif 4761 4762 /* 4763 * Tell the backend to free resources, if this LUN has a backend. 4764 */ 4765 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4766 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4767 4768 ctl_tpc_lun_shutdown(lun); 4769 mtx_destroy(&lun->lun_lock); 4770 free(lun->lun_devid, M_CTL); 4771 if (lun->flags & CTL_LUN_MALLOCED) 4772 free(lun, M_CTL); 4773 4774 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4775 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4776 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4777 } 4778 } 4779 4780 return (0); 4781 } 4782 4783 static void 4784 ctl_create_lun(struct ctl_be_lun *be_lun) 4785 { 4786 struct ctl_softc *ctl_softc; 4787 4788 ctl_softc = control_softc; 4789 4790 /* 4791 * ctl_alloc_lun() should handle all potential failure cases. 4792 */ 4793 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4794 } 4795 4796 int 4797 ctl_add_lun(struct ctl_be_lun *be_lun) 4798 { 4799 struct ctl_softc *ctl_softc = control_softc; 4800 4801 mtx_lock(&ctl_softc->ctl_lock); 4802 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4803 mtx_unlock(&ctl_softc->ctl_lock); 4804 wakeup(&ctl_softc->pending_lun_queue); 4805 4806 return (0); 4807 } 4808 4809 int 4810 ctl_enable_lun(struct ctl_be_lun *be_lun) 4811 { 4812 struct ctl_softc *ctl_softc; 4813 struct ctl_port *port, *nport; 4814 struct ctl_lun *lun; 4815 int retval; 4816 4817 ctl_softc = control_softc; 4818 4819 lun = (struct ctl_lun *)be_lun->ctl_lun; 4820 4821 mtx_lock(&ctl_softc->ctl_lock); 4822 mtx_lock(&lun->lun_lock); 4823 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4824 /* 4825 * eh? Why did we get called if the LUN is already 4826 * enabled? 4827 */ 4828 mtx_unlock(&lun->lun_lock); 4829 mtx_unlock(&ctl_softc->ctl_lock); 4830 return (0); 4831 } 4832 lun->flags &= ~CTL_LUN_DISABLED; 4833 mtx_unlock(&lun->lun_lock); 4834 4835 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4836 nport = STAILQ_NEXT(port, links); 4837 4838 /* 4839 * Drop the lock while we call the FETD's enable routine. 4840 * This can lead to a callback into CTL (at least in the 4841 * case of the internal initiator frontend. 4842 */ 4843 mtx_unlock(&ctl_softc->ctl_lock); 4844 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4845 mtx_lock(&ctl_softc->ctl_lock); 4846 if (retval != 0) { 4847 printf("%s: FETD %s port %d returned error " 4848 "%d for lun_enable on target %ju lun %jd\n", 4849 __func__, port->port_name, port->targ_port, retval, 4850 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4851 } 4852 #if 0 4853 else { 4854 /* NOTE: TODO: why does lun enable affect port status? */ 4855 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4856 } 4857 #endif 4858 } 4859 4860 mtx_unlock(&ctl_softc->ctl_lock); 4861 4862 return (0); 4863 } 4864 4865 int 4866 ctl_disable_lun(struct ctl_be_lun *be_lun) 4867 { 4868 struct ctl_softc *ctl_softc; 4869 struct ctl_port *port; 4870 struct ctl_lun *lun; 4871 int retval; 4872 4873 ctl_softc = control_softc; 4874 4875 lun = (struct ctl_lun *)be_lun->ctl_lun; 4876 4877 mtx_lock(&ctl_softc->ctl_lock); 4878 mtx_lock(&lun->lun_lock); 4879 if (lun->flags & CTL_LUN_DISABLED) { 4880 mtx_unlock(&lun->lun_lock); 4881 mtx_unlock(&ctl_softc->ctl_lock); 4882 return (0); 4883 } 4884 lun->flags |= CTL_LUN_DISABLED; 4885 mtx_unlock(&lun->lun_lock); 4886 4887 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4888 mtx_unlock(&ctl_softc->ctl_lock); 4889 /* 4890 * Drop the lock before we call the frontend's disable 4891 * routine, to avoid lock order reversals. 4892 * 4893 * XXX KDM what happens if the frontend list changes while 4894 * we're traversing it? It's unlikely, but should be handled. 4895 */ 4896 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4897 lun->lun); 4898 mtx_lock(&ctl_softc->ctl_lock); 4899 if (retval != 0) { 4900 printf("ctl_alloc_lun: FETD %s port %d returned error " 4901 "%d for lun_disable on target %ju lun %jd\n", 4902 port->port_name, port->targ_port, retval, 4903 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4904 } 4905 } 4906 4907 mtx_unlock(&ctl_softc->ctl_lock); 4908 4909 return (0); 4910 } 4911 4912 int 4913 ctl_start_lun(struct ctl_be_lun *be_lun) 4914 { 4915 struct ctl_softc *ctl_softc; 4916 struct ctl_lun *lun; 4917 4918 ctl_softc = control_softc; 4919 4920 lun = (struct ctl_lun *)be_lun->ctl_lun; 4921 4922 mtx_lock(&lun->lun_lock); 4923 lun->flags &= ~CTL_LUN_STOPPED; 4924 mtx_unlock(&lun->lun_lock); 4925 4926 return (0); 4927 } 4928 4929 int 4930 ctl_stop_lun(struct ctl_be_lun *be_lun) 4931 { 4932 struct ctl_softc *ctl_softc; 4933 struct ctl_lun *lun; 4934 4935 ctl_softc = control_softc; 4936 4937 lun = (struct ctl_lun *)be_lun->ctl_lun; 4938 4939 mtx_lock(&lun->lun_lock); 4940 lun->flags |= CTL_LUN_STOPPED; 4941 mtx_unlock(&lun->lun_lock); 4942 4943 return (0); 4944 } 4945 4946 int 4947 ctl_lun_offline(struct ctl_be_lun *be_lun) 4948 { 4949 struct ctl_softc *ctl_softc; 4950 struct ctl_lun *lun; 4951 4952 ctl_softc = control_softc; 4953 4954 lun = (struct ctl_lun *)be_lun->ctl_lun; 4955 4956 mtx_lock(&lun->lun_lock); 4957 lun->flags |= CTL_LUN_OFFLINE; 4958 mtx_unlock(&lun->lun_lock); 4959 4960 return (0); 4961 } 4962 4963 int 4964 ctl_lun_online(struct ctl_be_lun *be_lun) 4965 { 4966 struct ctl_softc *ctl_softc; 4967 struct ctl_lun *lun; 4968 4969 ctl_softc = control_softc; 4970 4971 lun = (struct ctl_lun *)be_lun->ctl_lun; 4972 4973 mtx_lock(&lun->lun_lock); 4974 lun->flags &= ~CTL_LUN_OFFLINE; 4975 mtx_unlock(&lun->lun_lock); 4976 4977 return (0); 4978 } 4979 4980 int 4981 ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4982 { 4983 struct ctl_softc *ctl_softc; 4984 struct ctl_lun *lun; 4985 4986 ctl_softc = control_softc; 4987 4988 lun = (struct ctl_lun *)be_lun->ctl_lun; 4989 4990 mtx_lock(&lun->lun_lock); 4991 4992 /* 4993 * The LUN needs to be disabled before it can be marked invalid. 4994 */ 4995 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4996 mtx_unlock(&lun->lun_lock); 4997 return (-1); 4998 } 4999 /* 5000 * Mark the LUN invalid. 5001 */ 5002 lun->flags |= CTL_LUN_INVALID; 5003 5004 /* 5005 * If there is nothing in the OOA queue, go ahead and free the LUN. 5006 * If we have something in the OOA queue, we'll free it when the 5007 * last I/O completes. 5008 */ 5009 if (TAILQ_EMPTY(&lun->ooa_queue)) { 5010 mtx_unlock(&lun->lun_lock); 5011 mtx_lock(&ctl_softc->ctl_lock); 5012 ctl_free_lun(lun); 5013 mtx_unlock(&ctl_softc->ctl_lock); 5014 } else 5015 mtx_unlock(&lun->lun_lock); 5016 5017 return (0); 5018 } 5019 5020 int 5021 ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5022 { 5023 struct ctl_softc *ctl_softc; 5024 struct ctl_lun *lun; 5025 5026 ctl_softc = control_softc; 5027 lun = (struct ctl_lun *)be_lun->ctl_lun; 5028 5029 mtx_lock(&lun->lun_lock); 5030 lun->flags |= CTL_LUN_INOPERABLE; 5031 mtx_unlock(&lun->lun_lock); 5032 5033 return (0); 5034 } 5035 5036 int 5037 ctl_lun_operable(struct ctl_be_lun *be_lun) 5038 { 5039 struct ctl_softc *ctl_softc; 5040 struct ctl_lun *lun; 5041 5042 ctl_softc = control_softc; 5043 lun = (struct ctl_lun *)be_lun->ctl_lun; 5044 5045 mtx_lock(&lun->lun_lock); 5046 lun->flags &= ~CTL_LUN_INOPERABLE; 5047 mtx_unlock(&lun->lun_lock); 5048 5049 return (0); 5050 } 5051 5052 int 5053 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 5054 int lock) 5055 { 5056 struct ctl_softc *softc; 5057 struct ctl_lun *lun; 5058 struct copan_aps_subpage *current_sp; 5059 struct ctl_page_index *page_index; 5060 int i; 5061 5062 softc = control_softc; 5063 5064 mtx_lock(&softc->ctl_lock); 5065 5066 lun = (struct ctl_lun *)be_lun->ctl_lun; 5067 mtx_lock(&lun->lun_lock); 5068 5069 page_index = NULL; 5070 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5071 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 5072 APS_PAGE_CODE) 5073 continue; 5074 5075 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 5076 continue; 5077 page_index = &lun->mode_pages.index[i]; 5078 } 5079 5080 if (page_index == NULL) { 5081 mtx_unlock(&lun->lun_lock); 5082 mtx_unlock(&softc->ctl_lock); 5083 printf("%s: APS subpage not found for lun %ju!\n", __func__, 5084 (uintmax_t)lun->lun); 5085 return (1); 5086 } 5087 #if 0 5088 if ((softc->aps_locked_lun != 0) 5089 && (softc->aps_locked_lun != lun->lun)) { 5090 printf("%s: attempt to lock LUN %llu when %llu is already " 5091 "locked\n"); 5092 mtx_unlock(&lun->lun_lock); 5093 mtx_unlock(&softc->ctl_lock); 5094 return (1); 5095 } 5096 #endif 5097 5098 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5099 (page_index->page_len * CTL_PAGE_CURRENT)); 5100 5101 if (lock != 0) { 5102 current_sp->lock_active = APS_LOCK_ACTIVE; 5103 softc->aps_locked_lun = lun->lun; 5104 } else { 5105 current_sp->lock_active = 0; 5106 softc->aps_locked_lun = 0; 5107 } 5108 5109 5110 /* 5111 * If we're in HA mode, try to send the lock message to the other 5112 * side. 5113 */ 5114 if (ctl_is_single == 0) { 5115 int isc_retval; 5116 union ctl_ha_msg lock_msg; 5117 5118 lock_msg.hdr.nexus = *nexus; 5119 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 5120 if (lock != 0) 5121 lock_msg.aps.lock_flag = 1; 5122 else 5123 lock_msg.aps.lock_flag = 0; 5124 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 5125 sizeof(lock_msg), 0); 5126 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 5127 printf("%s: APS (lock=%d) error returned from " 5128 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 5129 mtx_unlock(&lun->lun_lock); 5130 mtx_unlock(&softc->ctl_lock); 5131 return (1); 5132 } 5133 } 5134 5135 mtx_unlock(&lun->lun_lock); 5136 mtx_unlock(&softc->ctl_lock); 5137 5138 return (0); 5139 } 5140 5141 void 5142 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5143 { 5144 struct ctl_lun *lun; 5145 struct ctl_softc *softc; 5146 int i; 5147 5148 softc = control_softc; 5149 5150 lun = (struct ctl_lun *)be_lun->ctl_lun; 5151 5152 mtx_lock(&lun->lun_lock); 5153 5154 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5155 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5156 5157 mtx_unlock(&lun->lun_lock); 5158 } 5159 5160 /* 5161 * Backend "memory move is complete" callback for requests that never 5162 * make it down to say RAIDCore's configuration code. 5163 */ 5164 int 5165 ctl_config_move_done(union ctl_io *io) 5166 { 5167 int retval; 5168 5169 retval = CTL_RETVAL_COMPLETE; 5170 5171 5172 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5173 /* 5174 * XXX KDM this shouldn't happen, but what if it does? 5175 */ 5176 if (io->io_hdr.io_type != CTL_IO_SCSI) 5177 panic("I/O type isn't CTL_IO_SCSI!"); 5178 5179 if ((io->io_hdr.port_status == 0) 5180 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5181 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5182 io->io_hdr.status = CTL_SUCCESS; 5183 else if ((io->io_hdr.port_status != 0) 5184 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5185 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5186 /* 5187 * For hardware error sense keys, the sense key 5188 * specific value is defined to be a retry count, 5189 * but we use it to pass back an internal FETD 5190 * error code. XXX KDM Hopefully the FETD is only 5191 * using 16 bits for an error code, since that's 5192 * all the space we have in the sks field. 5193 */ 5194 ctl_set_internal_failure(&io->scsiio, 5195 /*sks_valid*/ 1, 5196 /*retry_count*/ 5197 io->io_hdr.port_status); 5198 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5199 free(io->scsiio.kern_data_ptr, M_CTL); 5200 ctl_done(io); 5201 goto bailout; 5202 } 5203 5204 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5205 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5206 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5207 /* 5208 * XXX KDM just assuming a single pointer here, and not a 5209 * S/G list. If we start using S/G lists for config data, 5210 * we'll need to know how to clean them up here as well. 5211 */ 5212 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5213 free(io->scsiio.kern_data_ptr, M_CTL); 5214 /* Hopefully the user has already set the status... */ 5215 ctl_done(io); 5216 } else { 5217 /* 5218 * XXX KDM now we need to continue data movement. Some 5219 * options: 5220 * - call ctl_scsiio() again? We don't do this for data 5221 * writes, because for those at least we know ahead of 5222 * time where the write will go and how long it is. For 5223 * config writes, though, that information is largely 5224 * contained within the write itself, thus we need to 5225 * parse out the data again. 5226 * 5227 * - Call some other function once the data is in? 5228 */ 5229 5230 /* 5231 * XXX KDM call ctl_scsiio() again for now, and check flag 5232 * bits to see whether we're allocated or not. 5233 */ 5234 retval = ctl_scsiio(&io->scsiio); 5235 } 5236 bailout: 5237 return (retval); 5238 } 5239 5240 /* 5241 * This gets called by a backend driver when it is done with a 5242 * data_submit method. 5243 */ 5244 void 5245 ctl_data_submit_done(union ctl_io *io) 5246 { 5247 /* 5248 * If the IO_CONT flag is set, we need to call the supplied 5249 * function to continue processing the I/O, instead of completing 5250 * the I/O just yet. 5251 * 5252 * If there is an error, though, we don't want to keep processing. 5253 * Instead, just send status back to the initiator. 5254 */ 5255 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5256 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5257 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5258 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5259 io->scsiio.io_cont(io); 5260 return; 5261 } 5262 ctl_done(io); 5263 } 5264 5265 /* 5266 * This gets called by a backend driver when it is done with a 5267 * configuration write. 5268 */ 5269 void 5270 ctl_config_write_done(union ctl_io *io) 5271 { 5272 uint8_t *buf; 5273 5274 /* 5275 * If the IO_CONT flag is set, we need to call the supplied 5276 * function to continue processing the I/O, instead of completing 5277 * the I/O just yet. 5278 * 5279 * If there is an error, though, we don't want to keep processing. 5280 * Instead, just send status back to the initiator. 5281 */ 5282 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5283 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5284 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5285 io->scsiio.io_cont(io); 5286 return; 5287 } 5288 /* 5289 * Since a configuration write can be done for commands that actually 5290 * have data allocated, like write buffer, and commands that have 5291 * no data, like start/stop unit, we need to check here. 5292 */ 5293 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5294 buf = io->scsiio.kern_data_ptr; 5295 else 5296 buf = NULL; 5297 ctl_done(io); 5298 if (buf) 5299 free(buf, M_CTL); 5300 } 5301 5302 /* 5303 * SCSI release command. 5304 */ 5305 int 5306 ctl_scsi_release(struct ctl_scsiio *ctsio) 5307 { 5308 int length, longid, thirdparty_id, resv_id; 5309 struct ctl_softc *ctl_softc; 5310 struct ctl_lun *lun; 5311 5312 length = 0; 5313 resv_id = 0; 5314 5315 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5316 5317 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5318 ctl_softc = control_softc; 5319 5320 switch (ctsio->cdb[0]) { 5321 case RELEASE_10: { 5322 struct scsi_release_10 *cdb; 5323 5324 cdb = (struct scsi_release_10 *)ctsio->cdb; 5325 5326 if (cdb->byte2 & SR10_LONGID) 5327 longid = 1; 5328 else 5329 thirdparty_id = cdb->thirdparty_id; 5330 5331 resv_id = cdb->resv_id; 5332 length = scsi_2btoul(cdb->length); 5333 break; 5334 } 5335 } 5336 5337 5338 /* 5339 * XXX KDM right now, we only support LUN reservation. We don't 5340 * support 3rd party reservations, or extent reservations, which 5341 * might actually need the parameter list. If we've gotten this 5342 * far, we've got a LUN reservation. Anything else got kicked out 5343 * above. So, according to SPC, ignore the length. 5344 */ 5345 length = 0; 5346 5347 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5348 && (length > 0)) { 5349 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5350 ctsio->kern_data_len = length; 5351 ctsio->kern_total_len = length; 5352 ctsio->kern_data_resid = 0; 5353 ctsio->kern_rel_offset = 0; 5354 ctsio->kern_sg_entries = 0; 5355 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5356 ctsio->be_move_done = ctl_config_move_done; 5357 ctl_datamove((union ctl_io *)ctsio); 5358 5359 return (CTL_RETVAL_COMPLETE); 5360 } 5361 5362 if (length > 0) 5363 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5364 5365 mtx_lock(&lun->lun_lock); 5366 5367 /* 5368 * According to SPC, it is not an error for an intiator to attempt 5369 * to release a reservation on a LUN that isn't reserved, or that 5370 * is reserved by another initiator. The reservation can only be 5371 * released, though, by the initiator who made it or by one of 5372 * several reset type events. 5373 */ 5374 if (lun->flags & CTL_LUN_RESERVED) { 5375 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5376 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5377 && (ctsio->io_hdr.nexus.targ_target.id == 5378 lun->rsv_nexus.targ_target.id)) { 5379 lun->flags &= ~CTL_LUN_RESERVED; 5380 } 5381 } 5382 5383 mtx_unlock(&lun->lun_lock); 5384 5385 ctsio->scsi_status = SCSI_STATUS_OK; 5386 ctsio->io_hdr.status = CTL_SUCCESS; 5387 5388 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5389 free(ctsio->kern_data_ptr, M_CTL); 5390 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5391 } 5392 5393 ctl_done((union ctl_io *)ctsio); 5394 return (CTL_RETVAL_COMPLETE); 5395 } 5396 5397 int 5398 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5399 { 5400 int extent, thirdparty, longid; 5401 int resv_id, length; 5402 uint64_t thirdparty_id; 5403 struct ctl_softc *ctl_softc; 5404 struct ctl_lun *lun; 5405 5406 extent = 0; 5407 thirdparty = 0; 5408 longid = 0; 5409 resv_id = 0; 5410 length = 0; 5411 thirdparty_id = 0; 5412 5413 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5414 5415 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5416 ctl_softc = control_softc; 5417 5418 switch (ctsio->cdb[0]) { 5419 case RESERVE_10: { 5420 struct scsi_reserve_10 *cdb; 5421 5422 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5423 5424 if (cdb->byte2 & SR10_LONGID) 5425 longid = 1; 5426 else 5427 thirdparty_id = cdb->thirdparty_id; 5428 5429 resv_id = cdb->resv_id; 5430 length = scsi_2btoul(cdb->length); 5431 break; 5432 } 5433 } 5434 5435 /* 5436 * XXX KDM right now, we only support LUN reservation. We don't 5437 * support 3rd party reservations, or extent reservations, which 5438 * might actually need the parameter list. If we've gotten this 5439 * far, we've got a LUN reservation. Anything else got kicked out 5440 * above. So, according to SPC, ignore the length. 5441 */ 5442 length = 0; 5443 5444 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5445 && (length > 0)) { 5446 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5447 ctsio->kern_data_len = length; 5448 ctsio->kern_total_len = length; 5449 ctsio->kern_data_resid = 0; 5450 ctsio->kern_rel_offset = 0; 5451 ctsio->kern_sg_entries = 0; 5452 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5453 ctsio->be_move_done = ctl_config_move_done; 5454 ctl_datamove((union ctl_io *)ctsio); 5455 5456 return (CTL_RETVAL_COMPLETE); 5457 } 5458 5459 if (length > 0) 5460 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5461 5462 mtx_lock(&lun->lun_lock); 5463 if (lun->flags & CTL_LUN_RESERVED) { 5464 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5465 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5466 || (ctsio->io_hdr.nexus.targ_target.id != 5467 lun->rsv_nexus.targ_target.id)) { 5468 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5469 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5470 goto bailout; 5471 } 5472 } 5473 5474 lun->flags |= CTL_LUN_RESERVED; 5475 lun->rsv_nexus = ctsio->io_hdr.nexus; 5476 5477 ctsio->scsi_status = SCSI_STATUS_OK; 5478 ctsio->io_hdr.status = CTL_SUCCESS; 5479 5480 bailout: 5481 mtx_unlock(&lun->lun_lock); 5482 5483 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5484 free(ctsio->kern_data_ptr, M_CTL); 5485 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5486 } 5487 5488 ctl_done((union ctl_io *)ctsio); 5489 return (CTL_RETVAL_COMPLETE); 5490 } 5491 5492 int 5493 ctl_start_stop(struct ctl_scsiio *ctsio) 5494 { 5495 struct scsi_start_stop_unit *cdb; 5496 struct ctl_lun *lun; 5497 struct ctl_softc *ctl_softc; 5498 int retval; 5499 5500 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5501 5502 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5503 ctl_softc = control_softc; 5504 retval = 0; 5505 5506 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5507 5508 /* 5509 * XXX KDM 5510 * We don't support the immediate bit on a stop unit. In order to 5511 * do that, we would need to code up a way to know that a stop is 5512 * pending, and hold off any new commands until it completes, one 5513 * way or another. Then we could accept or reject those commands 5514 * depending on its status. We would almost need to do the reverse 5515 * of what we do below for an immediate start -- return the copy of 5516 * the ctl_io to the FETD with status to send to the host (and to 5517 * free the copy!) and then free the original I/O once the stop 5518 * actually completes. That way, the OOA queue mechanism can work 5519 * to block commands that shouldn't proceed. Another alternative 5520 * would be to put the copy in the queue in place of the original, 5521 * and return the original back to the caller. That could be 5522 * slightly safer.. 5523 */ 5524 if ((cdb->byte2 & SSS_IMMED) 5525 && ((cdb->how & SSS_START) == 0)) { 5526 ctl_set_invalid_field(ctsio, 5527 /*sks_valid*/ 1, 5528 /*command*/ 1, 5529 /*field*/ 1, 5530 /*bit_valid*/ 1, 5531 /*bit*/ 0); 5532 ctl_done((union ctl_io *)ctsio); 5533 return (CTL_RETVAL_COMPLETE); 5534 } 5535 5536 if ((lun->flags & CTL_LUN_PR_RESERVED) 5537 && ((cdb->how & SSS_START)==0)) { 5538 uint32_t residx; 5539 5540 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5541 if (!lun->per_res[residx].registered 5542 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5543 5544 ctl_set_reservation_conflict(ctsio); 5545 ctl_done((union ctl_io *)ctsio); 5546 return (CTL_RETVAL_COMPLETE); 5547 } 5548 } 5549 5550 /* 5551 * If there is no backend on this device, we can't start or stop 5552 * it. In theory we shouldn't get any start/stop commands in the 5553 * first place at this level if the LUN doesn't have a backend. 5554 * That should get stopped by the command decode code. 5555 */ 5556 if (lun->backend == NULL) { 5557 ctl_set_invalid_opcode(ctsio); 5558 ctl_done((union ctl_io *)ctsio); 5559 return (CTL_RETVAL_COMPLETE); 5560 } 5561 5562 /* 5563 * XXX KDM Copan-specific offline behavior. 5564 * Figure out a reasonable way to port this? 5565 */ 5566 #ifdef NEEDTOPORT 5567 mtx_lock(&lun->lun_lock); 5568 5569 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5570 && (lun->flags & CTL_LUN_OFFLINE)) { 5571 /* 5572 * If the LUN is offline, and the on/offline bit isn't set, 5573 * reject the start or stop. Otherwise, let it through. 5574 */ 5575 mtx_unlock(&lun->lun_lock); 5576 ctl_set_lun_not_ready(ctsio); 5577 ctl_done((union ctl_io *)ctsio); 5578 } else { 5579 mtx_unlock(&lun->lun_lock); 5580 #endif /* NEEDTOPORT */ 5581 /* 5582 * This could be a start or a stop when we're online, 5583 * or a stop/offline or start/online. A start or stop when 5584 * we're offline is covered in the case above. 5585 */ 5586 /* 5587 * In the non-immediate case, we send the request to 5588 * the backend and return status to the user when 5589 * it is done. 5590 * 5591 * In the immediate case, we allocate a new ctl_io 5592 * to hold a copy of the request, and send that to 5593 * the backend. We then set good status on the 5594 * user's request and return it immediately. 5595 */ 5596 if (cdb->byte2 & SSS_IMMED) { 5597 union ctl_io *new_io; 5598 5599 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5600 if (new_io == NULL) { 5601 ctl_set_busy(ctsio); 5602 ctl_done((union ctl_io *)ctsio); 5603 } else { 5604 ctl_copy_io((union ctl_io *)ctsio, 5605 new_io); 5606 retval = lun->backend->config_write(new_io); 5607 ctl_set_success(ctsio); 5608 ctl_done((union ctl_io *)ctsio); 5609 } 5610 } else { 5611 retval = lun->backend->config_write( 5612 (union ctl_io *)ctsio); 5613 } 5614 #ifdef NEEDTOPORT 5615 } 5616 #endif 5617 return (retval); 5618 } 5619 5620 /* 5621 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5622 * we don't really do anything with the LBA and length fields if the user 5623 * passes them in. Instead we'll just flush out the cache for the entire 5624 * LUN. 5625 */ 5626 int 5627 ctl_sync_cache(struct ctl_scsiio *ctsio) 5628 { 5629 struct ctl_lun *lun; 5630 struct ctl_softc *ctl_softc; 5631 uint64_t starting_lba; 5632 uint32_t block_count; 5633 int retval; 5634 5635 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5636 5637 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5638 ctl_softc = control_softc; 5639 retval = 0; 5640 5641 switch (ctsio->cdb[0]) { 5642 case SYNCHRONIZE_CACHE: { 5643 struct scsi_sync_cache *cdb; 5644 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5645 5646 starting_lba = scsi_4btoul(cdb->begin_lba); 5647 block_count = scsi_2btoul(cdb->lb_count); 5648 break; 5649 } 5650 case SYNCHRONIZE_CACHE_16: { 5651 struct scsi_sync_cache_16 *cdb; 5652 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5653 5654 starting_lba = scsi_8btou64(cdb->begin_lba); 5655 block_count = scsi_4btoul(cdb->lb_count); 5656 break; 5657 } 5658 default: 5659 ctl_set_invalid_opcode(ctsio); 5660 ctl_done((union ctl_io *)ctsio); 5661 goto bailout; 5662 break; /* NOTREACHED */ 5663 } 5664 5665 /* 5666 * We check the LBA and length, but don't do anything with them. 5667 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5668 * get flushed. This check will just help satisfy anyone who wants 5669 * to see an error for an out of range LBA. 5670 */ 5671 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5672 ctl_set_lba_out_of_range(ctsio); 5673 ctl_done((union ctl_io *)ctsio); 5674 goto bailout; 5675 } 5676 5677 /* 5678 * If this LUN has no backend, we can't flush the cache anyway. 5679 */ 5680 if (lun->backend == NULL) { 5681 ctl_set_invalid_opcode(ctsio); 5682 ctl_done((union ctl_io *)ctsio); 5683 goto bailout; 5684 } 5685 5686 /* 5687 * Check to see whether we're configured to send the SYNCHRONIZE 5688 * CACHE command directly to the back end. 5689 */ 5690 mtx_lock(&lun->lun_lock); 5691 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5692 && (++(lun->sync_count) >= lun->sync_interval)) { 5693 lun->sync_count = 0; 5694 mtx_unlock(&lun->lun_lock); 5695 retval = lun->backend->config_write((union ctl_io *)ctsio); 5696 } else { 5697 mtx_unlock(&lun->lun_lock); 5698 ctl_set_success(ctsio); 5699 ctl_done((union ctl_io *)ctsio); 5700 } 5701 5702 bailout: 5703 5704 return (retval); 5705 } 5706 5707 int 5708 ctl_format(struct ctl_scsiio *ctsio) 5709 { 5710 struct scsi_format *cdb; 5711 struct ctl_lun *lun; 5712 struct ctl_softc *ctl_softc; 5713 int length, defect_list_len; 5714 5715 CTL_DEBUG_PRINT(("ctl_format\n")); 5716 5717 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5718 ctl_softc = control_softc; 5719 5720 cdb = (struct scsi_format *)ctsio->cdb; 5721 5722 length = 0; 5723 if (cdb->byte2 & SF_FMTDATA) { 5724 if (cdb->byte2 & SF_LONGLIST) 5725 length = sizeof(struct scsi_format_header_long); 5726 else 5727 length = sizeof(struct scsi_format_header_short); 5728 } 5729 5730 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5731 && (length > 0)) { 5732 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5733 ctsio->kern_data_len = length; 5734 ctsio->kern_total_len = length; 5735 ctsio->kern_data_resid = 0; 5736 ctsio->kern_rel_offset = 0; 5737 ctsio->kern_sg_entries = 0; 5738 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5739 ctsio->be_move_done = ctl_config_move_done; 5740 ctl_datamove((union ctl_io *)ctsio); 5741 5742 return (CTL_RETVAL_COMPLETE); 5743 } 5744 5745 defect_list_len = 0; 5746 5747 if (cdb->byte2 & SF_FMTDATA) { 5748 if (cdb->byte2 & SF_LONGLIST) { 5749 struct scsi_format_header_long *header; 5750 5751 header = (struct scsi_format_header_long *) 5752 ctsio->kern_data_ptr; 5753 5754 defect_list_len = scsi_4btoul(header->defect_list_len); 5755 if (defect_list_len != 0) { 5756 ctl_set_invalid_field(ctsio, 5757 /*sks_valid*/ 1, 5758 /*command*/ 0, 5759 /*field*/ 2, 5760 /*bit_valid*/ 0, 5761 /*bit*/ 0); 5762 goto bailout; 5763 } 5764 } else { 5765 struct scsi_format_header_short *header; 5766 5767 header = (struct scsi_format_header_short *) 5768 ctsio->kern_data_ptr; 5769 5770 defect_list_len = scsi_2btoul(header->defect_list_len); 5771 if (defect_list_len != 0) { 5772 ctl_set_invalid_field(ctsio, 5773 /*sks_valid*/ 1, 5774 /*command*/ 0, 5775 /*field*/ 2, 5776 /*bit_valid*/ 0, 5777 /*bit*/ 0); 5778 goto bailout; 5779 } 5780 } 5781 } 5782 5783 /* 5784 * The format command will clear out the "Medium format corrupted" 5785 * status if set by the configuration code. That status is really 5786 * just a way to notify the host that we have lost the media, and 5787 * get them to issue a command that will basically make them think 5788 * they're blowing away the media. 5789 */ 5790 mtx_lock(&lun->lun_lock); 5791 lun->flags &= ~CTL_LUN_INOPERABLE; 5792 mtx_unlock(&lun->lun_lock); 5793 5794 ctsio->scsi_status = SCSI_STATUS_OK; 5795 ctsio->io_hdr.status = CTL_SUCCESS; 5796 bailout: 5797 5798 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5799 free(ctsio->kern_data_ptr, M_CTL); 5800 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5801 } 5802 5803 ctl_done((union ctl_io *)ctsio); 5804 return (CTL_RETVAL_COMPLETE); 5805 } 5806 5807 int 5808 ctl_read_buffer(struct ctl_scsiio *ctsio) 5809 { 5810 struct scsi_read_buffer *cdb; 5811 struct ctl_lun *lun; 5812 int buffer_offset, len; 5813 static uint8_t descr[4]; 5814 static uint8_t echo_descr[4] = { 0 }; 5815 5816 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5817 5818 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5819 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5820 5821 if (lun->flags & CTL_LUN_PR_RESERVED) { 5822 uint32_t residx; 5823 5824 /* 5825 * XXX KDM need a lock here. 5826 */ 5827 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5828 if ((lun->res_type == SPR_TYPE_EX_AC 5829 && residx != lun->pr_res_idx) 5830 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5831 || lun->res_type == SPR_TYPE_EX_AC_AR) 5832 && !lun->per_res[residx].registered)) { 5833 ctl_set_reservation_conflict(ctsio); 5834 ctl_done((union ctl_io *)ctsio); 5835 return (CTL_RETVAL_COMPLETE); 5836 } 5837 } 5838 5839 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5840 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5841 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5842 ctl_set_invalid_field(ctsio, 5843 /*sks_valid*/ 1, 5844 /*command*/ 1, 5845 /*field*/ 1, 5846 /*bit_valid*/ 1, 5847 /*bit*/ 4); 5848 ctl_done((union ctl_io *)ctsio); 5849 return (CTL_RETVAL_COMPLETE); 5850 } 5851 5852 len = scsi_3btoul(cdb->length); 5853 buffer_offset = scsi_3btoul(cdb->offset); 5854 5855 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5856 ctl_set_invalid_field(ctsio, 5857 /*sks_valid*/ 1, 5858 /*command*/ 1, 5859 /*field*/ 6, 5860 /*bit_valid*/ 0, 5861 /*bit*/ 0); 5862 ctl_done((union ctl_io *)ctsio); 5863 return (CTL_RETVAL_COMPLETE); 5864 } 5865 5866 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5867 descr[0] = 0; 5868 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5869 ctsio->kern_data_ptr = descr; 5870 len = min(len, sizeof(descr)); 5871 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5872 ctsio->kern_data_ptr = echo_descr; 5873 len = min(len, sizeof(echo_descr)); 5874 } else 5875 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5876 ctsio->kern_data_len = len; 5877 ctsio->kern_total_len = len; 5878 ctsio->kern_data_resid = 0; 5879 ctsio->kern_rel_offset = 0; 5880 ctsio->kern_sg_entries = 0; 5881 ctsio->be_move_done = ctl_config_move_done; 5882 ctl_datamove((union ctl_io *)ctsio); 5883 5884 return (CTL_RETVAL_COMPLETE); 5885 } 5886 5887 int 5888 ctl_write_buffer(struct ctl_scsiio *ctsio) 5889 { 5890 struct scsi_write_buffer *cdb; 5891 struct ctl_lun *lun; 5892 int buffer_offset, len; 5893 5894 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5895 5896 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5897 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5898 5899 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5900 ctl_set_invalid_field(ctsio, 5901 /*sks_valid*/ 1, 5902 /*command*/ 1, 5903 /*field*/ 1, 5904 /*bit_valid*/ 1, 5905 /*bit*/ 4); 5906 ctl_done((union ctl_io *)ctsio); 5907 return (CTL_RETVAL_COMPLETE); 5908 } 5909 5910 len = scsi_3btoul(cdb->length); 5911 buffer_offset = scsi_3btoul(cdb->offset); 5912 5913 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5914 ctl_set_invalid_field(ctsio, 5915 /*sks_valid*/ 1, 5916 /*command*/ 1, 5917 /*field*/ 6, 5918 /*bit_valid*/ 0, 5919 /*bit*/ 0); 5920 ctl_done((union ctl_io *)ctsio); 5921 return (CTL_RETVAL_COMPLETE); 5922 } 5923 5924 /* 5925 * If we've got a kernel request that hasn't been malloced yet, 5926 * malloc it and tell the caller the data buffer is here. 5927 */ 5928 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5929 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5930 ctsio->kern_data_len = len; 5931 ctsio->kern_total_len = len; 5932 ctsio->kern_data_resid = 0; 5933 ctsio->kern_rel_offset = 0; 5934 ctsio->kern_sg_entries = 0; 5935 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5936 ctsio->be_move_done = ctl_config_move_done; 5937 ctl_datamove((union ctl_io *)ctsio); 5938 5939 return (CTL_RETVAL_COMPLETE); 5940 } 5941 5942 ctl_done((union ctl_io *)ctsio); 5943 5944 return (CTL_RETVAL_COMPLETE); 5945 } 5946 5947 int 5948 ctl_write_same(struct ctl_scsiio *ctsio) 5949 { 5950 struct ctl_lun *lun; 5951 struct ctl_lba_len_flags *lbalen; 5952 uint64_t lba; 5953 uint32_t num_blocks; 5954 int len, retval; 5955 uint8_t byte2; 5956 5957 retval = CTL_RETVAL_COMPLETE; 5958 5959 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5960 5961 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5962 5963 switch (ctsio->cdb[0]) { 5964 case WRITE_SAME_10: { 5965 struct scsi_write_same_10 *cdb; 5966 5967 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5968 5969 lba = scsi_4btoul(cdb->addr); 5970 num_blocks = scsi_2btoul(cdb->length); 5971 byte2 = cdb->byte2; 5972 break; 5973 } 5974 case WRITE_SAME_16: { 5975 struct scsi_write_same_16 *cdb; 5976 5977 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5978 5979 lba = scsi_8btou64(cdb->addr); 5980 num_blocks = scsi_4btoul(cdb->length); 5981 byte2 = cdb->byte2; 5982 break; 5983 } 5984 default: 5985 /* 5986 * We got a command we don't support. This shouldn't 5987 * happen, commands should be filtered out above us. 5988 */ 5989 ctl_set_invalid_opcode(ctsio); 5990 ctl_done((union ctl_io *)ctsio); 5991 5992 return (CTL_RETVAL_COMPLETE); 5993 break; /* NOTREACHED */ 5994 } 5995 5996 /* 5997 * The first check is to make sure we're in bounds, the second 5998 * check is to catch wrap-around problems. If the lba + num blocks 5999 * is less than the lba, then we've wrapped around and the block 6000 * range is invalid anyway. 6001 */ 6002 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6003 || ((lba + num_blocks) < lba)) { 6004 ctl_set_lba_out_of_range(ctsio); 6005 ctl_done((union ctl_io *)ctsio); 6006 return (CTL_RETVAL_COMPLETE); 6007 } 6008 6009 /* Zero number of blocks means "to the last logical block" */ 6010 if (num_blocks == 0) { 6011 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 6012 ctl_set_invalid_field(ctsio, 6013 /*sks_valid*/ 0, 6014 /*command*/ 1, 6015 /*field*/ 0, 6016 /*bit_valid*/ 0, 6017 /*bit*/ 0); 6018 ctl_done((union ctl_io *)ctsio); 6019 return (CTL_RETVAL_COMPLETE); 6020 } 6021 num_blocks = (lun->be_lun->maxlba + 1) - lba; 6022 } 6023 6024 len = lun->be_lun->blocksize; 6025 6026 /* 6027 * If we've got a kernel request that hasn't been malloced yet, 6028 * malloc it and tell the caller the data buffer is here. 6029 */ 6030 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6031 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6032 ctsio->kern_data_len = len; 6033 ctsio->kern_total_len = len; 6034 ctsio->kern_data_resid = 0; 6035 ctsio->kern_rel_offset = 0; 6036 ctsio->kern_sg_entries = 0; 6037 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6038 ctsio->be_move_done = ctl_config_move_done; 6039 ctl_datamove((union ctl_io *)ctsio); 6040 6041 return (CTL_RETVAL_COMPLETE); 6042 } 6043 6044 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6045 lbalen->lba = lba; 6046 lbalen->len = num_blocks; 6047 lbalen->flags = byte2; 6048 retval = lun->backend->config_write((union ctl_io *)ctsio); 6049 6050 return (retval); 6051 } 6052 6053 int 6054 ctl_unmap(struct ctl_scsiio *ctsio) 6055 { 6056 struct ctl_lun *lun; 6057 struct scsi_unmap *cdb; 6058 struct ctl_ptr_len_flags *ptrlen; 6059 struct scsi_unmap_header *hdr; 6060 struct scsi_unmap_desc *buf, *end, *range; 6061 uint64_t lba; 6062 uint32_t num_blocks; 6063 int len, retval; 6064 uint8_t byte2; 6065 6066 retval = CTL_RETVAL_COMPLETE; 6067 6068 CTL_DEBUG_PRINT(("ctl_unmap\n")); 6069 6070 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6071 cdb = (struct scsi_unmap *)ctsio->cdb; 6072 6073 len = scsi_2btoul(cdb->length); 6074 byte2 = cdb->byte2; 6075 6076 /* 6077 * If we've got a kernel request that hasn't been malloced yet, 6078 * malloc it and tell the caller the data buffer is here. 6079 */ 6080 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6081 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 6082 ctsio->kern_data_len = len; 6083 ctsio->kern_total_len = len; 6084 ctsio->kern_data_resid = 0; 6085 ctsio->kern_rel_offset = 0; 6086 ctsio->kern_sg_entries = 0; 6087 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6088 ctsio->be_move_done = ctl_config_move_done; 6089 ctl_datamove((union ctl_io *)ctsio); 6090 6091 return (CTL_RETVAL_COMPLETE); 6092 } 6093 6094 len = ctsio->kern_total_len - ctsio->kern_data_resid; 6095 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 6096 if (len < sizeof (*hdr) || 6097 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 6098 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 6099 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 6100 ctl_set_invalid_field(ctsio, 6101 /*sks_valid*/ 0, 6102 /*command*/ 0, 6103 /*field*/ 0, 6104 /*bit_valid*/ 0, 6105 /*bit*/ 0); 6106 ctl_done((union ctl_io *)ctsio); 6107 return (CTL_RETVAL_COMPLETE); 6108 } 6109 len = scsi_2btoul(hdr->desc_length); 6110 buf = (struct scsi_unmap_desc *)(hdr + 1); 6111 end = buf + len / sizeof(*buf); 6112 6113 for (range = buf; range < end; range++) { 6114 lba = scsi_8btou64(range->lba); 6115 num_blocks = scsi_4btoul(range->length); 6116 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6117 || ((lba + num_blocks) < lba)) { 6118 ctl_set_lba_out_of_range(ctsio); 6119 ctl_done((union ctl_io *)ctsio); 6120 return (CTL_RETVAL_COMPLETE); 6121 } 6122 } 6123 6124 mtx_lock(&lun->lun_lock); 6125 ptrlen = (struct ctl_ptr_len_flags *) 6126 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6127 ptrlen->ptr = (void *)buf; 6128 ptrlen->len = len; 6129 ptrlen->flags = byte2; 6130 ctl_check_blocked(lun); 6131 mtx_unlock(&lun->lun_lock); 6132 6133 retval = lun->backend->config_write((union ctl_io *)ctsio); 6134 return (retval); 6135 } 6136 6137 /* 6138 * Note that this function currently doesn't actually do anything inside 6139 * CTL to enforce things if the DQue bit is turned on. 6140 * 6141 * Also note that this function can't be used in the default case, because 6142 * the DQue bit isn't set in the changeable mask for the control mode page 6143 * anyway. This is just here as an example for how to implement a page 6144 * handler, and a placeholder in case we want to allow the user to turn 6145 * tagged queueing on and off. 6146 * 6147 * The D_SENSE bit handling is functional, however, and will turn 6148 * descriptor sense on and off for a given LUN. 6149 */ 6150 int 6151 ctl_control_page_handler(struct ctl_scsiio *ctsio, 6152 struct ctl_page_index *page_index, uint8_t *page_ptr) 6153 { 6154 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6155 struct ctl_lun *lun; 6156 struct ctl_softc *softc; 6157 int set_ua; 6158 uint32_t initidx; 6159 6160 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6161 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6162 set_ua = 0; 6163 6164 user_cp = (struct scsi_control_page *)page_ptr; 6165 current_cp = (struct scsi_control_page *) 6166 (page_index->page_data + (page_index->page_len * 6167 CTL_PAGE_CURRENT)); 6168 saved_cp = (struct scsi_control_page *) 6169 (page_index->page_data + (page_index->page_len * 6170 CTL_PAGE_SAVED)); 6171 6172 softc = control_softc; 6173 6174 mtx_lock(&lun->lun_lock); 6175 if (((current_cp->rlec & SCP_DSENSE) == 0) 6176 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6177 /* 6178 * Descriptor sense is currently turned off and the user 6179 * wants to turn it on. 6180 */ 6181 current_cp->rlec |= SCP_DSENSE; 6182 saved_cp->rlec |= SCP_DSENSE; 6183 lun->flags |= CTL_LUN_SENSE_DESC; 6184 set_ua = 1; 6185 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6186 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6187 /* 6188 * Descriptor sense is currently turned on, and the user 6189 * wants to turn it off. 6190 */ 6191 current_cp->rlec &= ~SCP_DSENSE; 6192 saved_cp->rlec &= ~SCP_DSENSE; 6193 lun->flags &= ~CTL_LUN_SENSE_DESC; 6194 set_ua = 1; 6195 } 6196 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6197 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6198 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6199 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6200 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6201 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6202 set_ua = 1; 6203 } 6204 if (set_ua != 0) { 6205 int i; 6206 /* 6207 * Let other initiators know that the mode 6208 * parameters for this LUN have changed. 6209 */ 6210 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6211 if (i == initidx) 6212 continue; 6213 6214 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6215 } 6216 } 6217 mtx_unlock(&lun->lun_lock); 6218 6219 return (0); 6220 } 6221 6222 int 6223 ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6224 struct ctl_page_index *page_index, uint8_t *page_ptr) 6225 { 6226 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6227 struct ctl_lun *lun; 6228 int set_ua; 6229 uint32_t initidx; 6230 6231 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6232 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6233 set_ua = 0; 6234 6235 user_cp = (struct scsi_caching_page *)page_ptr; 6236 current_cp = (struct scsi_caching_page *) 6237 (page_index->page_data + (page_index->page_len * 6238 CTL_PAGE_CURRENT)); 6239 saved_cp = (struct scsi_caching_page *) 6240 (page_index->page_data + (page_index->page_len * 6241 CTL_PAGE_SAVED)); 6242 6243 mtx_lock(&lun->lun_lock); 6244 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6245 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6246 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6247 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6248 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6249 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6250 set_ua = 1; 6251 } 6252 if (set_ua != 0) { 6253 int i; 6254 /* 6255 * Let other initiators know that the mode 6256 * parameters for this LUN have changed. 6257 */ 6258 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6259 if (i == initidx) 6260 continue; 6261 6262 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6263 } 6264 } 6265 mtx_unlock(&lun->lun_lock); 6266 6267 return (0); 6268 } 6269 6270 int 6271 ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6272 struct ctl_page_index *page_index, uint8_t *page_ptr) 6273 { 6274 return (0); 6275 } 6276 6277 int 6278 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6279 struct ctl_page_index *page_index, int pc) 6280 { 6281 struct copan_power_subpage *page; 6282 6283 page = (struct copan_power_subpage *)page_index->page_data + 6284 (page_index->page_len * pc); 6285 6286 switch (pc) { 6287 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6288 /* 6289 * We don't update the changable bits for this page. 6290 */ 6291 break; 6292 case SMS_PAGE_CTRL_CURRENT >> 6: 6293 case SMS_PAGE_CTRL_DEFAULT >> 6: 6294 case SMS_PAGE_CTRL_SAVED >> 6: 6295 #ifdef NEEDTOPORT 6296 ctl_update_power_subpage(page); 6297 #endif 6298 break; 6299 default: 6300 #ifdef NEEDTOPORT 6301 EPRINT(0, "Invalid PC %d!!", pc); 6302 #endif 6303 break; 6304 } 6305 return (0); 6306 } 6307 6308 6309 int 6310 ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6311 struct ctl_page_index *page_index, uint8_t *page_ptr) 6312 { 6313 struct copan_aps_subpage *user_sp; 6314 struct copan_aps_subpage *current_sp; 6315 union ctl_modepage_info *modepage_info; 6316 struct ctl_softc *softc; 6317 struct ctl_lun *lun; 6318 int retval; 6319 6320 retval = CTL_RETVAL_COMPLETE; 6321 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6322 (page_index->page_len * CTL_PAGE_CURRENT)); 6323 softc = control_softc; 6324 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6325 6326 user_sp = (struct copan_aps_subpage *)page_ptr; 6327 6328 modepage_info = (union ctl_modepage_info *) 6329 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6330 6331 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6332 modepage_info->header.subpage = page_index->subpage; 6333 modepage_info->aps.lock_active = user_sp->lock_active; 6334 6335 mtx_lock(&softc->ctl_lock); 6336 6337 /* 6338 * If there is a request to lock the LUN and another LUN is locked 6339 * this is an error. If the requested LUN is already locked ignore 6340 * the request. If no LUN is locked attempt to lock it. 6341 * if there is a request to unlock the LUN and the LUN is currently 6342 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6343 * if another LUN is locked or no LUN is locked. 6344 */ 6345 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6346 if (softc->aps_locked_lun == lun->lun) { 6347 /* 6348 * This LUN is already locked, so we're done. 6349 */ 6350 retval = CTL_RETVAL_COMPLETE; 6351 } else if (softc->aps_locked_lun == 0) { 6352 /* 6353 * No one has the lock, pass the request to the 6354 * backend. 6355 */ 6356 retval = lun->backend->config_write( 6357 (union ctl_io *)ctsio); 6358 } else { 6359 /* 6360 * Someone else has the lock, throw out the request. 6361 */ 6362 ctl_set_already_locked(ctsio); 6363 free(ctsio->kern_data_ptr, M_CTL); 6364 ctl_done((union ctl_io *)ctsio); 6365 6366 /* 6367 * Set the return value so that ctl_do_mode_select() 6368 * won't try to complete the command. We already 6369 * completed it here. 6370 */ 6371 retval = CTL_RETVAL_ERROR; 6372 } 6373 } else if (softc->aps_locked_lun == lun->lun) { 6374 /* 6375 * This LUN is locked, so pass the unlock request to the 6376 * backend. 6377 */ 6378 retval = lun->backend->config_write((union ctl_io *)ctsio); 6379 } 6380 mtx_unlock(&softc->ctl_lock); 6381 6382 return (retval); 6383 } 6384 6385 int 6386 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6387 struct ctl_page_index *page_index, 6388 uint8_t *page_ptr) 6389 { 6390 uint8_t *c; 6391 int i; 6392 6393 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6394 ctl_time_io_secs = 6395 (c[0] << 8) | 6396 (c[1] << 0) | 6397 0; 6398 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6399 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6400 printf("page data:"); 6401 for (i=0; i<8; i++) 6402 printf(" %.2x",page_ptr[i]); 6403 printf("\n"); 6404 return (0); 6405 } 6406 6407 int 6408 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6409 struct ctl_page_index *page_index, 6410 int pc) 6411 { 6412 struct copan_debugconf_subpage *page; 6413 6414 page = (struct copan_debugconf_subpage *)page_index->page_data + 6415 (page_index->page_len * pc); 6416 6417 switch (pc) { 6418 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6419 case SMS_PAGE_CTRL_DEFAULT >> 6: 6420 case SMS_PAGE_CTRL_SAVED >> 6: 6421 /* 6422 * We don't update the changable or default bits for this page. 6423 */ 6424 break; 6425 case SMS_PAGE_CTRL_CURRENT >> 6: 6426 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6427 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6428 break; 6429 default: 6430 #ifdef NEEDTOPORT 6431 EPRINT(0, "Invalid PC %d!!", pc); 6432 #endif /* NEEDTOPORT */ 6433 break; 6434 } 6435 return (0); 6436 } 6437 6438 6439 static int 6440 ctl_do_mode_select(union ctl_io *io) 6441 { 6442 struct scsi_mode_page_header *page_header; 6443 struct ctl_page_index *page_index; 6444 struct ctl_scsiio *ctsio; 6445 int control_dev, page_len; 6446 int page_len_offset, page_len_size; 6447 union ctl_modepage_info *modepage_info; 6448 struct ctl_lun *lun; 6449 int *len_left, *len_used; 6450 int retval, i; 6451 6452 ctsio = &io->scsiio; 6453 page_index = NULL; 6454 page_len = 0; 6455 retval = CTL_RETVAL_COMPLETE; 6456 6457 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6458 6459 if (lun->be_lun->lun_type != T_DIRECT) 6460 control_dev = 1; 6461 else 6462 control_dev = 0; 6463 6464 modepage_info = (union ctl_modepage_info *) 6465 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6466 len_left = &modepage_info->header.len_left; 6467 len_used = &modepage_info->header.len_used; 6468 6469 do_next_page: 6470 6471 page_header = (struct scsi_mode_page_header *) 6472 (ctsio->kern_data_ptr + *len_used); 6473 6474 if (*len_left == 0) { 6475 free(ctsio->kern_data_ptr, M_CTL); 6476 ctl_set_success(ctsio); 6477 ctl_done((union ctl_io *)ctsio); 6478 return (CTL_RETVAL_COMPLETE); 6479 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6480 6481 free(ctsio->kern_data_ptr, M_CTL); 6482 ctl_set_param_len_error(ctsio); 6483 ctl_done((union ctl_io *)ctsio); 6484 return (CTL_RETVAL_COMPLETE); 6485 6486 } else if ((page_header->page_code & SMPH_SPF) 6487 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6488 6489 free(ctsio->kern_data_ptr, M_CTL); 6490 ctl_set_param_len_error(ctsio); 6491 ctl_done((union ctl_io *)ctsio); 6492 return (CTL_RETVAL_COMPLETE); 6493 } 6494 6495 6496 /* 6497 * XXX KDM should we do something with the block descriptor? 6498 */ 6499 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6500 6501 if ((control_dev != 0) 6502 && (lun->mode_pages.index[i].page_flags & 6503 CTL_PAGE_FLAG_DISK_ONLY)) 6504 continue; 6505 6506 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6507 (page_header->page_code & SMPH_PC_MASK)) 6508 continue; 6509 6510 /* 6511 * If neither page has a subpage code, then we've got a 6512 * match. 6513 */ 6514 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6515 && ((page_header->page_code & SMPH_SPF) == 0)) { 6516 page_index = &lun->mode_pages.index[i]; 6517 page_len = page_header->page_length; 6518 break; 6519 } 6520 6521 /* 6522 * If both pages have subpages, then the subpage numbers 6523 * have to match. 6524 */ 6525 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6526 && (page_header->page_code & SMPH_SPF)) { 6527 struct scsi_mode_page_header_sp *sph; 6528 6529 sph = (struct scsi_mode_page_header_sp *)page_header; 6530 6531 if (lun->mode_pages.index[i].subpage == 6532 sph->subpage) { 6533 page_index = &lun->mode_pages.index[i]; 6534 page_len = scsi_2btoul(sph->page_length); 6535 break; 6536 } 6537 } 6538 } 6539 6540 /* 6541 * If we couldn't find the page, or if we don't have a mode select 6542 * handler for it, send back an error to the user. 6543 */ 6544 if ((page_index == NULL) 6545 || (page_index->select_handler == NULL)) { 6546 ctl_set_invalid_field(ctsio, 6547 /*sks_valid*/ 1, 6548 /*command*/ 0, 6549 /*field*/ *len_used, 6550 /*bit_valid*/ 0, 6551 /*bit*/ 0); 6552 free(ctsio->kern_data_ptr, M_CTL); 6553 ctl_done((union ctl_io *)ctsio); 6554 return (CTL_RETVAL_COMPLETE); 6555 } 6556 6557 if (page_index->page_code & SMPH_SPF) { 6558 page_len_offset = 2; 6559 page_len_size = 2; 6560 } else { 6561 page_len_size = 1; 6562 page_len_offset = 1; 6563 } 6564 6565 /* 6566 * If the length the initiator gives us isn't the one we specify in 6567 * the mode page header, or if they didn't specify enough data in 6568 * the CDB to avoid truncating this page, kick out the request. 6569 */ 6570 if ((page_len != (page_index->page_len - page_len_offset - 6571 page_len_size)) 6572 || (*len_left < page_index->page_len)) { 6573 6574 6575 ctl_set_invalid_field(ctsio, 6576 /*sks_valid*/ 1, 6577 /*command*/ 0, 6578 /*field*/ *len_used + page_len_offset, 6579 /*bit_valid*/ 0, 6580 /*bit*/ 0); 6581 free(ctsio->kern_data_ptr, M_CTL); 6582 ctl_done((union ctl_io *)ctsio); 6583 return (CTL_RETVAL_COMPLETE); 6584 } 6585 6586 /* 6587 * Run through the mode page, checking to make sure that the bits 6588 * the user changed are actually legal for him to change. 6589 */ 6590 for (i = 0; i < page_index->page_len; i++) { 6591 uint8_t *user_byte, *change_mask, *current_byte; 6592 int bad_bit; 6593 int j; 6594 6595 user_byte = (uint8_t *)page_header + i; 6596 change_mask = page_index->page_data + 6597 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6598 current_byte = page_index->page_data + 6599 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6600 6601 /* 6602 * Check to see whether the user set any bits in this byte 6603 * that he is not allowed to set. 6604 */ 6605 if ((*user_byte & ~(*change_mask)) == 6606 (*current_byte & ~(*change_mask))) 6607 continue; 6608 6609 /* 6610 * Go through bit by bit to determine which one is illegal. 6611 */ 6612 bad_bit = 0; 6613 for (j = 7; j >= 0; j--) { 6614 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6615 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6616 bad_bit = i; 6617 break; 6618 } 6619 } 6620 ctl_set_invalid_field(ctsio, 6621 /*sks_valid*/ 1, 6622 /*command*/ 0, 6623 /*field*/ *len_used + i, 6624 /*bit_valid*/ 1, 6625 /*bit*/ bad_bit); 6626 free(ctsio->kern_data_ptr, M_CTL); 6627 ctl_done((union ctl_io *)ctsio); 6628 return (CTL_RETVAL_COMPLETE); 6629 } 6630 6631 /* 6632 * Decrement these before we call the page handler, since we may 6633 * end up getting called back one way or another before the handler 6634 * returns to this context. 6635 */ 6636 *len_left -= page_index->page_len; 6637 *len_used += page_index->page_len; 6638 6639 retval = page_index->select_handler(ctsio, page_index, 6640 (uint8_t *)page_header); 6641 6642 /* 6643 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6644 * wait until this queued command completes to finish processing 6645 * the mode page. If it returns anything other than 6646 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6647 * already set the sense information, freed the data pointer, and 6648 * completed the io for us. 6649 */ 6650 if (retval != CTL_RETVAL_COMPLETE) 6651 goto bailout_no_done; 6652 6653 /* 6654 * If the initiator sent us more than one page, parse the next one. 6655 */ 6656 if (*len_left > 0) 6657 goto do_next_page; 6658 6659 ctl_set_success(ctsio); 6660 free(ctsio->kern_data_ptr, M_CTL); 6661 ctl_done((union ctl_io *)ctsio); 6662 6663 bailout_no_done: 6664 6665 return (CTL_RETVAL_COMPLETE); 6666 6667 } 6668 6669 int 6670 ctl_mode_select(struct ctl_scsiio *ctsio) 6671 { 6672 int param_len, pf, sp; 6673 int header_size, bd_len; 6674 int len_left, len_used; 6675 struct ctl_page_index *page_index; 6676 struct ctl_lun *lun; 6677 int control_dev, page_len; 6678 union ctl_modepage_info *modepage_info; 6679 int retval; 6680 6681 pf = 0; 6682 sp = 0; 6683 page_len = 0; 6684 len_used = 0; 6685 len_left = 0; 6686 retval = 0; 6687 bd_len = 0; 6688 page_index = NULL; 6689 6690 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6691 6692 if (lun->be_lun->lun_type != T_DIRECT) 6693 control_dev = 1; 6694 else 6695 control_dev = 0; 6696 6697 switch (ctsio->cdb[0]) { 6698 case MODE_SELECT_6: { 6699 struct scsi_mode_select_6 *cdb; 6700 6701 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6702 6703 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6704 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6705 6706 param_len = cdb->length; 6707 header_size = sizeof(struct scsi_mode_header_6); 6708 break; 6709 } 6710 case MODE_SELECT_10: { 6711 struct scsi_mode_select_10 *cdb; 6712 6713 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6714 6715 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6716 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6717 6718 param_len = scsi_2btoul(cdb->length); 6719 header_size = sizeof(struct scsi_mode_header_10); 6720 break; 6721 } 6722 default: 6723 ctl_set_invalid_opcode(ctsio); 6724 ctl_done((union ctl_io *)ctsio); 6725 return (CTL_RETVAL_COMPLETE); 6726 break; /* NOTREACHED */ 6727 } 6728 6729 /* 6730 * From SPC-3: 6731 * "A parameter list length of zero indicates that the Data-Out Buffer 6732 * shall be empty. This condition shall not be considered as an error." 6733 */ 6734 if (param_len == 0) { 6735 ctl_set_success(ctsio); 6736 ctl_done((union ctl_io *)ctsio); 6737 return (CTL_RETVAL_COMPLETE); 6738 } 6739 6740 /* 6741 * Since we'll hit this the first time through, prior to 6742 * allocation, we don't need to free a data buffer here. 6743 */ 6744 if (param_len < header_size) { 6745 ctl_set_param_len_error(ctsio); 6746 ctl_done((union ctl_io *)ctsio); 6747 return (CTL_RETVAL_COMPLETE); 6748 } 6749 6750 /* 6751 * Allocate the data buffer and grab the user's data. In theory, 6752 * we shouldn't have to sanity check the parameter list length here 6753 * because the maximum size is 64K. We should be able to malloc 6754 * that much without too many problems. 6755 */ 6756 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6757 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6758 ctsio->kern_data_len = param_len; 6759 ctsio->kern_total_len = param_len; 6760 ctsio->kern_data_resid = 0; 6761 ctsio->kern_rel_offset = 0; 6762 ctsio->kern_sg_entries = 0; 6763 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6764 ctsio->be_move_done = ctl_config_move_done; 6765 ctl_datamove((union ctl_io *)ctsio); 6766 6767 return (CTL_RETVAL_COMPLETE); 6768 } 6769 6770 switch (ctsio->cdb[0]) { 6771 case MODE_SELECT_6: { 6772 struct scsi_mode_header_6 *mh6; 6773 6774 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6775 bd_len = mh6->blk_desc_len; 6776 break; 6777 } 6778 case MODE_SELECT_10: { 6779 struct scsi_mode_header_10 *mh10; 6780 6781 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6782 bd_len = scsi_2btoul(mh10->blk_desc_len); 6783 break; 6784 } 6785 default: 6786 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6787 break; 6788 } 6789 6790 if (param_len < (header_size + bd_len)) { 6791 free(ctsio->kern_data_ptr, M_CTL); 6792 ctl_set_param_len_error(ctsio); 6793 ctl_done((union ctl_io *)ctsio); 6794 return (CTL_RETVAL_COMPLETE); 6795 } 6796 6797 /* 6798 * Set the IO_CONT flag, so that if this I/O gets passed to 6799 * ctl_config_write_done(), it'll get passed back to 6800 * ctl_do_mode_select() for further processing, or completion if 6801 * we're all done. 6802 */ 6803 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6804 ctsio->io_cont = ctl_do_mode_select; 6805 6806 modepage_info = (union ctl_modepage_info *) 6807 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6808 6809 memset(modepage_info, 0, sizeof(*modepage_info)); 6810 6811 len_left = param_len - header_size - bd_len; 6812 len_used = header_size + bd_len; 6813 6814 modepage_info->header.len_left = len_left; 6815 modepage_info->header.len_used = len_used; 6816 6817 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6818 } 6819 6820 int 6821 ctl_mode_sense(struct ctl_scsiio *ctsio) 6822 { 6823 struct ctl_lun *lun; 6824 int pc, page_code, dbd, llba, subpage; 6825 int alloc_len, page_len, header_len, total_len; 6826 struct scsi_mode_block_descr *block_desc; 6827 struct ctl_page_index *page_index; 6828 int control_dev; 6829 6830 dbd = 0; 6831 llba = 0; 6832 block_desc = NULL; 6833 page_index = NULL; 6834 6835 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6836 6837 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6838 6839 if (lun->be_lun->lun_type != T_DIRECT) 6840 control_dev = 1; 6841 else 6842 control_dev = 0; 6843 6844 if (lun->flags & CTL_LUN_PR_RESERVED) { 6845 uint32_t residx; 6846 6847 /* 6848 * XXX KDM need a lock here. 6849 */ 6850 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6851 if ((lun->res_type == SPR_TYPE_EX_AC 6852 && residx != lun->pr_res_idx) 6853 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6854 || lun->res_type == SPR_TYPE_EX_AC_AR) 6855 && !lun->per_res[residx].registered)) { 6856 ctl_set_reservation_conflict(ctsio); 6857 ctl_done((union ctl_io *)ctsio); 6858 return (CTL_RETVAL_COMPLETE); 6859 } 6860 } 6861 6862 switch (ctsio->cdb[0]) { 6863 case MODE_SENSE_6: { 6864 struct scsi_mode_sense_6 *cdb; 6865 6866 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6867 6868 header_len = sizeof(struct scsi_mode_hdr_6); 6869 if (cdb->byte2 & SMS_DBD) 6870 dbd = 1; 6871 else 6872 header_len += sizeof(struct scsi_mode_block_descr); 6873 6874 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6875 page_code = cdb->page & SMS_PAGE_CODE; 6876 subpage = cdb->subpage; 6877 alloc_len = cdb->length; 6878 break; 6879 } 6880 case MODE_SENSE_10: { 6881 struct scsi_mode_sense_10 *cdb; 6882 6883 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6884 6885 header_len = sizeof(struct scsi_mode_hdr_10); 6886 6887 if (cdb->byte2 & SMS_DBD) 6888 dbd = 1; 6889 else 6890 header_len += sizeof(struct scsi_mode_block_descr); 6891 if (cdb->byte2 & SMS10_LLBAA) 6892 llba = 1; 6893 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6894 page_code = cdb->page & SMS_PAGE_CODE; 6895 subpage = cdb->subpage; 6896 alloc_len = scsi_2btoul(cdb->length); 6897 break; 6898 } 6899 default: 6900 ctl_set_invalid_opcode(ctsio); 6901 ctl_done((union ctl_io *)ctsio); 6902 return (CTL_RETVAL_COMPLETE); 6903 break; /* NOTREACHED */ 6904 } 6905 6906 /* 6907 * We have to make a first pass through to calculate the size of 6908 * the pages that match the user's query. Then we allocate enough 6909 * memory to hold it, and actually copy the data into the buffer. 6910 */ 6911 switch (page_code) { 6912 case SMS_ALL_PAGES_PAGE: { 6913 int i; 6914 6915 page_len = 0; 6916 6917 /* 6918 * At the moment, values other than 0 and 0xff here are 6919 * reserved according to SPC-3. 6920 */ 6921 if ((subpage != SMS_SUBPAGE_PAGE_0) 6922 && (subpage != SMS_SUBPAGE_ALL)) { 6923 ctl_set_invalid_field(ctsio, 6924 /*sks_valid*/ 1, 6925 /*command*/ 1, 6926 /*field*/ 3, 6927 /*bit_valid*/ 0, 6928 /*bit*/ 0); 6929 ctl_done((union ctl_io *)ctsio); 6930 return (CTL_RETVAL_COMPLETE); 6931 } 6932 6933 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6934 if ((control_dev != 0) 6935 && (lun->mode_pages.index[i].page_flags & 6936 CTL_PAGE_FLAG_DISK_ONLY)) 6937 continue; 6938 6939 /* 6940 * We don't use this subpage if the user didn't 6941 * request all subpages. 6942 */ 6943 if ((lun->mode_pages.index[i].subpage != 0) 6944 && (subpage == SMS_SUBPAGE_PAGE_0)) 6945 continue; 6946 6947 #if 0 6948 printf("found page %#x len %d\n", 6949 lun->mode_pages.index[i].page_code & 6950 SMPH_PC_MASK, 6951 lun->mode_pages.index[i].page_len); 6952 #endif 6953 page_len += lun->mode_pages.index[i].page_len; 6954 } 6955 break; 6956 } 6957 default: { 6958 int i; 6959 6960 page_len = 0; 6961 6962 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6963 /* Look for the right page code */ 6964 if ((lun->mode_pages.index[i].page_code & 6965 SMPH_PC_MASK) != page_code) 6966 continue; 6967 6968 /* Look for the right subpage or the subpage wildcard*/ 6969 if ((lun->mode_pages.index[i].subpage != subpage) 6970 && (subpage != SMS_SUBPAGE_ALL)) 6971 continue; 6972 6973 /* Make sure the page is supported for this dev type */ 6974 if ((control_dev != 0) 6975 && (lun->mode_pages.index[i].page_flags & 6976 CTL_PAGE_FLAG_DISK_ONLY)) 6977 continue; 6978 6979 #if 0 6980 printf("found page %#x len %d\n", 6981 lun->mode_pages.index[i].page_code & 6982 SMPH_PC_MASK, 6983 lun->mode_pages.index[i].page_len); 6984 #endif 6985 6986 page_len += lun->mode_pages.index[i].page_len; 6987 } 6988 6989 if (page_len == 0) { 6990 ctl_set_invalid_field(ctsio, 6991 /*sks_valid*/ 1, 6992 /*command*/ 1, 6993 /*field*/ 2, 6994 /*bit_valid*/ 1, 6995 /*bit*/ 5); 6996 ctl_done((union ctl_io *)ctsio); 6997 return (CTL_RETVAL_COMPLETE); 6998 } 6999 break; 7000 } 7001 } 7002 7003 total_len = header_len + page_len; 7004 #if 0 7005 printf("header_len = %d, page_len = %d, total_len = %d\n", 7006 header_len, page_len, total_len); 7007 #endif 7008 7009 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7010 ctsio->kern_sg_entries = 0; 7011 ctsio->kern_data_resid = 0; 7012 ctsio->kern_rel_offset = 0; 7013 if (total_len < alloc_len) { 7014 ctsio->residual = alloc_len - total_len; 7015 ctsio->kern_data_len = total_len; 7016 ctsio->kern_total_len = total_len; 7017 } else { 7018 ctsio->residual = 0; 7019 ctsio->kern_data_len = alloc_len; 7020 ctsio->kern_total_len = alloc_len; 7021 } 7022 7023 switch (ctsio->cdb[0]) { 7024 case MODE_SENSE_6: { 7025 struct scsi_mode_hdr_6 *header; 7026 7027 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 7028 7029 header->datalen = ctl_min(total_len - 1, 254); 7030 if (control_dev == 0) 7031 header->dev_specific = 0x10; /* DPOFUA */ 7032 if (dbd) 7033 header->block_descr_len = 0; 7034 else 7035 header->block_descr_len = 7036 sizeof(struct scsi_mode_block_descr); 7037 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7038 break; 7039 } 7040 case MODE_SENSE_10: { 7041 struct scsi_mode_hdr_10 *header; 7042 int datalen; 7043 7044 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 7045 7046 datalen = ctl_min(total_len - 2, 65533); 7047 scsi_ulto2b(datalen, header->datalen); 7048 if (control_dev == 0) 7049 header->dev_specific = 0x10; /* DPOFUA */ 7050 if (dbd) 7051 scsi_ulto2b(0, header->block_descr_len); 7052 else 7053 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 7054 header->block_descr_len); 7055 block_desc = (struct scsi_mode_block_descr *)&header[1]; 7056 break; 7057 } 7058 default: 7059 panic("invalid CDB type %#x", ctsio->cdb[0]); 7060 break; /* NOTREACHED */ 7061 } 7062 7063 /* 7064 * If we've got a disk, use its blocksize in the block 7065 * descriptor. Otherwise, just set it to 0. 7066 */ 7067 if (dbd == 0) { 7068 if (control_dev != 0) 7069 scsi_ulto3b(lun->be_lun->blocksize, 7070 block_desc->block_len); 7071 else 7072 scsi_ulto3b(0, block_desc->block_len); 7073 } 7074 7075 switch (page_code) { 7076 case SMS_ALL_PAGES_PAGE: { 7077 int i, data_used; 7078 7079 data_used = header_len; 7080 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7081 struct ctl_page_index *page_index; 7082 7083 page_index = &lun->mode_pages.index[i]; 7084 7085 if ((control_dev != 0) 7086 && (page_index->page_flags & 7087 CTL_PAGE_FLAG_DISK_ONLY)) 7088 continue; 7089 7090 /* 7091 * We don't use this subpage if the user didn't 7092 * request all subpages. We already checked (above) 7093 * to make sure the user only specified a subpage 7094 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 7095 */ 7096 if ((page_index->subpage != 0) 7097 && (subpage == SMS_SUBPAGE_PAGE_0)) 7098 continue; 7099 7100 /* 7101 * Call the handler, if it exists, to update the 7102 * page to the latest values. 7103 */ 7104 if (page_index->sense_handler != NULL) 7105 page_index->sense_handler(ctsio, page_index,pc); 7106 7107 memcpy(ctsio->kern_data_ptr + data_used, 7108 page_index->page_data + 7109 (page_index->page_len * pc), 7110 page_index->page_len); 7111 data_used += page_index->page_len; 7112 } 7113 break; 7114 } 7115 default: { 7116 int i, data_used; 7117 7118 data_used = header_len; 7119 7120 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 7121 struct ctl_page_index *page_index; 7122 7123 page_index = &lun->mode_pages.index[i]; 7124 7125 /* Look for the right page code */ 7126 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 7127 continue; 7128 7129 /* Look for the right subpage or the subpage wildcard*/ 7130 if ((page_index->subpage != subpage) 7131 && (subpage != SMS_SUBPAGE_ALL)) 7132 continue; 7133 7134 /* Make sure the page is supported for this dev type */ 7135 if ((control_dev != 0) 7136 && (page_index->page_flags & 7137 CTL_PAGE_FLAG_DISK_ONLY)) 7138 continue; 7139 7140 /* 7141 * Call the handler, if it exists, to update the 7142 * page to the latest values. 7143 */ 7144 if (page_index->sense_handler != NULL) 7145 page_index->sense_handler(ctsio, page_index,pc); 7146 7147 memcpy(ctsio->kern_data_ptr + data_used, 7148 page_index->page_data + 7149 (page_index->page_len * pc), 7150 page_index->page_len); 7151 data_used += page_index->page_len; 7152 } 7153 break; 7154 } 7155 } 7156 7157 ctsio->scsi_status = SCSI_STATUS_OK; 7158 7159 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7160 ctsio->be_move_done = ctl_config_move_done; 7161 ctl_datamove((union ctl_io *)ctsio); 7162 7163 return (CTL_RETVAL_COMPLETE); 7164 } 7165 7166 int 7167 ctl_read_capacity(struct ctl_scsiio *ctsio) 7168 { 7169 struct scsi_read_capacity *cdb; 7170 struct scsi_read_capacity_data *data; 7171 struct ctl_lun *lun; 7172 uint32_t lba; 7173 7174 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7175 7176 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7177 7178 lba = scsi_4btoul(cdb->addr); 7179 if (((cdb->pmi & SRC_PMI) == 0) 7180 && (lba != 0)) { 7181 ctl_set_invalid_field(/*ctsio*/ ctsio, 7182 /*sks_valid*/ 1, 7183 /*command*/ 1, 7184 /*field*/ 2, 7185 /*bit_valid*/ 0, 7186 /*bit*/ 0); 7187 ctl_done((union ctl_io *)ctsio); 7188 return (CTL_RETVAL_COMPLETE); 7189 } 7190 7191 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7192 7193 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7194 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7195 ctsio->residual = 0; 7196 ctsio->kern_data_len = sizeof(*data); 7197 ctsio->kern_total_len = sizeof(*data); 7198 ctsio->kern_data_resid = 0; 7199 ctsio->kern_rel_offset = 0; 7200 ctsio->kern_sg_entries = 0; 7201 7202 /* 7203 * If the maximum LBA is greater than 0xfffffffe, the user must 7204 * issue a SERVICE ACTION IN (16) command, with the read capacity 7205 * serivce action set. 7206 */ 7207 if (lun->be_lun->maxlba > 0xfffffffe) 7208 scsi_ulto4b(0xffffffff, data->addr); 7209 else 7210 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7211 7212 /* 7213 * XXX KDM this may not be 512 bytes... 7214 */ 7215 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7216 7217 ctsio->scsi_status = SCSI_STATUS_OK; 7218 7219 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7220 ctsio->be_move_done = ctl_config_move_done; 7221 ctl_datamove((union ctl_io *)ctsio); 7222 7223 return (CTL_RETVAL_COMPLETE); 7224 } 7225 7226 int 7227 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7228 { 7229 struct scsi_read_capacity_16 *cdb; 7230 struct scsi_read_capacity_data_long *data; 7231 struct ctl_lun *lun; 7232 uint64_t lba; 7233 uint32_t alloc_len; 7234 7235 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7236 7237 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7238 7239 alloc_len = scsi_4btoul(cdb->alloc_len); 7240 lba = scsi_8btou64(cdb->addr); 7241 7242 if ((cdb->reladr & SRC16_PMI) 7243 && (lba != 0)) { 7244 ctl_set_invalid_field(/*ctsio*/ ctsio, 7245 /*sks_valid*/ 1, 7246 /*command*/ 1, 7247 /*field*/ 2, 7248 /*bit_valid*/ 0, 7249 /*bit*/ 0); 7250 ctl_done((union ctl_io *)ctsio); 7251 return (CTL_RETVAL_COMPLETE); 7252 } 7253 7254 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7255 7256 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7257 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7258 7259 if (sizeof(*data) < alloc_len) { 7260 ctsio->residual = alloc_len - sizeof(*data); 7261 ctsio->kern_data_len = sizeof(*data); 7262 ctsio->kern_total_len = sizeof(*data); 7263 } else { 7264 ctsio->residual = 0; 7265 ctsio->kern_data_len = alloc_len; 7266 ctsio->kern_total_len = alloc_len; 7267 } 7268 ctsio->kern_data_resid = 0; 7269 ctsio->kern_rel_offset = 0; 7270 ctsio->kern_sg_entries = 0; 7271 7272 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7273 /* XXX KDM this may not be 512 bytes... */ 7274 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7275 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7276 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7277 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7278 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7279 7280 ctsio->scsi_status = SCSI_STATUS_OK; 7281 7282 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7283 ctsio->be_move_done = ctl_config_move_done; 7284 ctl_datamove((union ctl_io *)ctsio); 7285 7286 return (CTL_RETVAL_COMPLETE); 7287 } 7288 7289 int 7290 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7291 { 7292 struct scsi_maintenance_in *cdb; 7293 int retval; 7294 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7295 int num_target_port_groups, num_target_ports, single; 7296 struct ctl_lun *lun; 7297 struct ctl_softc *softc; 7298 struct ctl_port *port; 7299 struct scsi_target_group_data *rtg_ptr; 7300 struct scsi_target_group_data_extended *rtg_ext_ptr; 7301 struct scsi_target_port_group_descriptor *tpg_desc; 7302 7303 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7304 7305 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7306 softc = control_softc; 7307 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7308 7309 retval = CTL_RETVAL_COMPLETE; 7310 7311 switch (cdb->byte2 & STG_PDF_MASK) { 7312 case STG_PDF_LENGTH: 7313 ext = 0; 7314 break; 7315 case STG_PDF_EXTENDED: 7316 ext = 1; 7317 break; 7318 default: 7319 ctl_set_invalid_field(/*ctsio*/ ctsio, 7320 /*sks_valid*/ 1, 7321 /*command*/ 1, 7322 /*field*/ 2, 7323 /*bit_valid*/ 1, 7324 /*bit*/ 5); 7325 ctl_done((union ctl_io *)ctsio); 7326 return(retval); 7327 } 7328 7329 single = ctl_is_single; 7330 if (single) 7331 num_target_port_groups = 1; 7332 else 7333 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7334 num_target_ports = 0; 7335 mtx_lock(&softc->ctl_lock); 7336 STAILQ_FOREACH(port, &softc->port_list, links) { 7337 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7338 continue; 7339 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7340 continue; 7341 num_target_ports++; 7342 } 7343 mtx_unlock(&softc->ctl_lock); 7344 7345 if (ext) 7346 total_len = sizeof(struct scsi_target_group_data_extended); 7347 else 7348 total_len = sizeof(struct scsi_target_group_data); 7349 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7350 num_target_port_groups + 7351 sizeof(struct scsi_target_port_descriptor) * 7352 num_target_ports * num_target_port_groups; 7353 7354 alloc_len = scsi_4btoul(cdb->length); 7355 7356 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7357 7358 ctsio->kern_sg_entries = 0; 7359 7360 if (total_len < alloc_len) { 7361 ctsio->residual = alloc_len - total_len; 7362 ctsio->kern_data_len = total_len; 7363 ctsio->kern_total_len = total_len; 7364 } else { 7365 ctsio->residual = 0; 7366 ctsio->kern_data_len = alloc_len; 7367 ctsio->kern_total_len = alloc_len; 7368 } 7369 ctsio->kern_data_resid = 0; 7370 ctsio->kern_rel_offset = 0; 7371 7372 if (ext) { 7373 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7374 ctsio->kern_data_ptr; 7375 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7376 rtg_ext_ptr->format_type = 0x10; 7377 rtg_ext_ptr->implicit_transition_time = 0; 7378 tpg_desc = &rtg_ext_ptr->groups[0]; 7379 } else { 7380 rtg_ptr = (struct scsi_target_group_data *) 7381 ctsio->kern_data_ptr; 7382 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7383 tpg_desc = &rtg_ptr->groups[0]; 7384 } 7385 7386 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7387 mtx_lock(&softc->ctl_lock); 7388 for (g = 0; g < num_target_port_groups; g++) { 7389 if (g == pg) 7390 tpg_desc->pref_state = TPG_PRIMARY | 7391 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7392 else 7393 tpg_desc->pref_state = 7394 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7395 tpg_desc->support = TPG_AO_SUP; 7396 if (!single) 7397 tpg_desc->support |= TPG_AN_SUP; 7398 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7399 tpg_desc->status = TPG_IMPLICIT; 7400 pc = 0; 7401 STAILQ_FOREACH(port, &softc->port_list, links) { 7402 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7403 continue; 7404 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7405 CTL_MAX_LUNS) 7406 continue; 7407 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7408 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7409 relative_target_port_identifier); 7410 pc++; 7411 } 7412 tpg_desc->target_port_count = pc; 7413 tpg_desc = (struct scsi_target_port_group_descriptor *) 7414 &tpg_desc->descriptors[pc]; 7415 } 7416 mtx_unlock(&softc->ctl_lock); 7417 7418 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7419 ctsio->be_move_done = ctl_config_move_done; 7420 7421 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7422 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7423 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7424 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7425 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7426 7427 ctl_datamove((union ctl_io *)ctsio); 7428 return(retval); 7429 } 7430 7431 int 7432 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7433 { 7434 struct ctl_lun *lun; 7435 struct scsi_report_supported_opcodes *cdb; 7436 const struct ctl_cmd_entry *entry, *sentry; 7437 struct scsi_report_supported_opcodes_all *all; 7438 struct scsi_report_supported_opcodes_descr *descr; 7439 struct scsi_report_supported_opcodes_one *one; 7440 int retval; 7441 int alloc_len, total_len; 7442 int opcode, service_action, i, j, num; 7443 7444 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7445 7446 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7447 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7448 7449 retval = CTL_RETVAL_COMPLETE; 7450 7451 opcode = cdb->requested_opcode; 7452 service_action = scsi_2btoul(cdb->requested_service_action); 7453 switch (cdb->options & RSO_OPTIONS_MASK) { 7454 case RSO_OPTIONS_ALL: 7455 num = 0; 7456 for (i = 0; i < 256; i++) { 7457 entry = &ctl_cmd_table[i]; 7458 if (entry->flags & CTL_CMD_FLAG_SA5) { 7459 for (j = 0; j < 32; j++) { 7460 sentry = &((const struct ctl_cmd_entry *) 7461 entry->execute)[j]; 7462 if (ctl_cmd_applicable( 7463 lun->be_lun->lun_type, sentry)) 7464 num++; 7465 } 7466 } else { 7467 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7468 entry)) 7469 num++; 7470 } 7471 } 7472 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7473 num * sizeof(struct scsi_report_supported_opcodes_descr); 7474 break; 7475 case RSO_OPTIONS_OC: 7476 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7477 ctl_set_invalid_field(/*ctsio*/ ctsio, 7478 /*sks_valid*/ 1, 7479 /*command*/ 1, 7480 /*field*/ 2, 7481 /*bit_valid*/ 1, 7482 /*bit*/ 2); 7483 ctl_done((union ctl_io *)ctsio); 7484 return (CTL_RETVAL_COMPLETE); 7485 } 7486 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7487 break; 7488 case RSO_OPTIONS_OC_SA: 7489 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7490 service_action >= 32) { 7491 ctl_set_invalid_field(/*ctsio*/ ctsio, 7492 /*sks_valid*/ 1, 7493 /*command*/ 1, 7494 /*field*/ 2, 7495 /*bit_valid*/ 1, 7496 /*bit*/ 2); 7497 ctl_done((union ctl_io *)ctsio); 7498 return (CTL_RETVAL_COMPLETE); 7499 } 7500 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7501 break; 7502 default: 7503 ctl_set_invalid_field(/*ctsio*/ ctsio, 7504 /*sks_valid*/ 1, 7505 /*command*/ 1, 7506 /*field*/ 2, 7507 /*bit_valid*/ 1, 7508 /*bit*/ 2); 7509 ctl_done((union ctl_io *)ctsio); 7510 return (CTL_RETVAL_COMPLETE); 7511 } 7512 7513 alloc_len = scsi_4btoul(cdb->length); 7514 7515 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7516 7517 ctsio->kern_sg_entries = 0; 7518 7519 if (total_len < alloc_len) { 7520 ctsio->residual = alloc_len - total_len; 7521 ctsio->kern_data_len = total_len; 7522 ctsio->kern_total_len = total_len; 7523 } else { 7524 ctsio->residual = 0; 7525 ctsio->kern_data_len = alloc_len; 7526 ctsio->kern_total_len = alloc_len; 7527 } 7528 ctsio->kern_data_resid = 0; 7529 ctsio->kern_rel_offset = 0; 7530 7531 switch (cdb->options & RSO_OPTIONS_MASK) { 7532 case RSO_OPTIONS_ALL: 7533 all = (struct scsi_report_supported_opcodes_all *) 7534 ctsio->kern_data_ptr; 7535 num = 0; 7536 for (i = 0; i < 256; i++) { 7537 entry = &ctl_cmd_table[i]; 7538 if (entry->flags & CTL_CMD_FLAG_SA5) { 7539 for (j = 0; j < 32; j++) { 7540 sentry = &((const struct ctl_cmd_entry *) 7541 entry->execute)[j]; 7542 if (!ctl_cmd_applicable( 7543 lun->be_lun->lun_type, sentry)) 7544 continue; 7545 descr = &all->descr[num++]; 7546 descr->opcode = i; 7547 scsi_ulto2b(j, descr->service_action); 7548 descr->flags = RSO_SERVACTV; 7549 scsi_ulto2b(sentry->length, 7550 descr->cdb_length); 7551 } 7552 } else { 7553 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7554 entry)) 7555 continue; 7556 descr = &all->descr[num++]; 7557 descr->opcode = i; 7558 scsi_ulto2b(0, descr->service_action); 7559 descr->flags = 0; 7560 scsi_ulto2b(entry->length, descr->cdb_length); 7561 } 7562 } 7563 scsi_ulto4b( 7564 num * sizeof(struct scsi_report_supported_opcodes_descr), 7565 all->length); 7566 break; 7567 case RSO_OPTIONS_OC: 7568 one = (struct scsi_report_supported_opcodes_one *) 7569 ctsio->kern_data_ptr; 7570 entry = &ctl_cmd_table[opcode]; 7571 goto fill_one; 7572 case RSO_OPTIONS_OC_SA: 7573 one = (struct scsi_report_supported_opcodes_one *) 7574 ctsio->kern_data_ptr; 7575 entry = &ctl_cmd_table[opcode]; 7576 entry = &((const struct ctl_cmd_entry *) 7577 entry->execute)[service_action]; 7578 fill_one: 7579 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7580 one->support = 3; 7581 scsi_ulto2b(entry->length, one->cdb_length); 7582 one->cdb_usage[0] = opcode; 7583 memcpy(&one->cdb_usage[1], entry->usage, 7584 entry->length - 1); 7585 } else 7586 one->support = 1; 7587 break; 7588 } 7589 7590 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7591 ctsio->be_move_done = ctl_config_move_done; 7592 7593 ctl_datamove((union ctl_io *)ctsio); 7594 return(retval); 7595 } 7596 7597 int 7598 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7599 { 7600 struct ctl_lun *lun; 7601 struct scsi_report_supported_tmf *cdb; 7602 struct scsi_report_supported_tmf_data *data; 7603 int retval; 7604 int alloc_len, total_len; 7605 7606 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7607 7608 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7609 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7610 7611 retval = CTL_RETVAL_COMPLETE; 7612 7613 total_len = sizeof(struct scsi_report_supported_tmf_data); 7614 alloc_len = scsi_4btoul(cdb->length); 7615 7616 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7617 7618 ctsio->kern_sg_entries = 0; 7619 7620 if (total_len < alloc_len) { 7621 ctsio->residual = alloc_len - total_len; 7622 ctsio->kern_data_len = total_len; 7623 ctsio->kern_total_len = total_len; 7624 } else { 7625 ctsio->residual = 0; 7626 ctsio->kern_data_len = alloc_len; 7627 ctsio->kern_total_len = alloc_len; 7628 } 7629 ctsio->kern_data_resid = 0; 7630 ctsio->kern_rel_offset = 0; 7631 7632 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7633 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7634 data->byte2 |= RST_ITNRS; 7635 7636 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7637 ctsio->be_move_done = ctl_config_move_done; 7638 7639 ctl_datamove((union ctl_io *)ctsio); 7640 return (retval); 7641 } 7642 7643 int 7644 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7645 { 7646 struct ctl_lun *lun; 7647 struct scsi_report_timestamp *cdb; 7648 struct scsi_report_timestamp_data *data; 7649 struct timeval tv; 7650 int64_t timestamp; 7651 int retval; 7652 int alloc_len, total_len; 7653 7654 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7655 7656 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7657 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7658 7659 retval = CTL_RETVAL_COMPLETE; 7660 7661 total_len = sizeof(struct scsi_report_timestamp_data); 7662 alloc_len = scsi_4btoul(cdb->length); 7663 7664 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7665 7666 ctsio->kern_sg_entries = 0; 7667 7668 if (total_len < alloc_len) { 7669 ctsio->residual = alloc_len - total_len; 7670 ctsio->kern_data_len = total_len; 7671 ctsio->kern_total_len = total_len; 7672 } else { 7673 ctsio->residual = 0; 7674 ctsio->kern_data_len = alloc_len; 7675 ctsio->kern_total_len = alloc_len; 7676 } 7677 ctsio->kern_data_resid = 0; 7678 ctsio->kern_rel_offset = 0; 7679 7680 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7681 scsi_ulto2b(sizeof(*data) - 2, data->length); 7682 data->origin = RTS_ORIG_OUTSIDE; 7683 getmicrotime(&tv); 7684 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7685 scsi_ulto4b(timestamp >> 16, data->timestamp); 7686 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7687 7688 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7689 ctsio->be_move_done = ctl_config_move_done; 7690 7691 ctl_datamove((union ctl_io *)ctsio); 7692 return (retval); 7693 } 7694 7695 int 7696 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7697 { 7698 struct scsi_per_res_in *cdb; 7699 int alloc_len, total_len = 0; 7700 /* struct scsi_per_res_in_rsrv in_data; */ 7701 struct ctl_lun *lun; 7702 struct ctl_softc *softc; 7703 7704 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7705 7706 softc = control_softc; 7707 7708 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7709 7710 alloc_len = scsi_2btoul(cdb->length); 7711 7712 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7713 7714 retry: 7715 mtx_lock(&lun->lun_lock); 7716 switch (cdb->action) { 7717 case SPRI_RK: /* read keys */ 7718 total_len = sizeof(struct scsi_per_res_in_keys) + 7719 lun->pr_key_count * 7720 sizeof(struct scsi_per_res_key); 7721 break; 7722 case SPRI_RR: /* read reservation */ 7723 if (lun->flags & CTL_LUN_PR_RESERVED) 7724 total_len = sizeof(struct scsi_per_res_in_rsrv); 7725 else 7726 total_len = sizeof(struct scsi_per_res_in_header); 7727 break; 7728 case SPRI_RC: /* report capabilities */ 7729 total_len = sizeof(struct scsi_per_res_cap); 7730 break; 7731 case SPRI_RS: /* read full status */ 7732 total_len = sizeof(struct scsi_per_res_in_header) + 7733 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7734 lun->pr_key_count; 7735 break; 7736 default: 7737 panic("Invalid PR type %x", cdb->action); 7738 } 7739 mtx_unlock(&lun->lun_lock); 7740 7741 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7742 7743 if (total_len < alloc_len) { 7744 ctsio->residual = alloc_len - total_len; 7745 ctsio->kern_data_len = total_len; 7746 ctsio->kern_total_len = total_len; 7747 } else { 7748 ctsio->residual = 0; 7749 ctsio->kern_data_len = alloc_len; 7750 ctsio->kern_total_len = alloc_len; 7751 } 7752 7753 ctsio->kern_data_resid = 0; 7754 ctsio->kern_rel_offset = 0; 7755 ctsio->kern_sg_entries = 0; 7756 7757 mtx_lock(&lun->lun_lock); 7758 switch (cdb->action) { 7759 case SPRI_RK: { // read keys 7760 struct scsi_per_res_in_keys *res_keys; 7761 int i, key_count; 7762 7763 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7764 7765 /* 7766 * We had to drop the lock to allocate our buffer, which 7767 * leaves time for someone to come in with another 7768 * persistent reservation. (That is unlikely, though, 7769 * since this should be the only persistent reservation 7770 * command active right now.) 7771 */ 7772 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7773 (lun->pr_key_count * 7774 sizeof(struct scsi_per_res_key)))){ 7775 mtx_unlock(&lun->lun_lock); 7776 free(ctsio->kern_data_ptr, M_CTL); 7777 printf("%s: reservation length changed, retrying\n", 7778 __func__); 7779 goto retry; 7780 } 7781 7782 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7783 7784 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7785 lun->pr_key_count, res_keys->header.length); 7786 7787 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7788 if (!lun->per_res[i].registered) 7789 continue; 7790 7791 /* 7792 * We used lun->pr_key_count to calculate the 7793 * size to allocate. If it turns out the number of 7794 * initiators with the registered flag set is 7795 * larger than that (i.e. they haven't been kept in 7796 * sync), we've got a problem. 7797 */ 7798 if (key_count >= lun->pr_key_count) { 7799 #ifdef NEEDTOPORT 7800 csevent_log(CSC_CTL | CSC_SHELF_SW | 7801 CTL_PR_ERROR, 7802 csevent_LogType_Fault, 7803 csevent_AlertLevel_Yellow, 7804 csevent_FRU_ShelfController, 7805 csevent_FRU_Firmware, 7806 csevent_FRU_Unknown, 7807 "registered keys %d >= key " 7808 "count %d", key_count, 7809 lun->pr_key_count); 7810 #endif 7811 key_count++; 7812 continue; 7813 } 7814 memcpy(res_keys->keys[key_count].key, 7815 lun->per_res[i].res_key.key, 7816 ctl_min(sizeof(res_keys->keys[key_count].key), 7817 sizeof(lun->per_res[i].res_key))); 7818 key_count++; 7819 } 7820 break; 7821 } 7822 case SPRI_RR: { // read reservation 7823 struct scsi_per_res_in_rsrv *res; 7824 int tmp_len, header_only; 7825 7826 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7827 7828 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7829 7830 if (lun->flags & CTL_LUN_PR_RESERVED) 7831 { 7832 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7833 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7834 res->header.length); 7835 header_only = 0; 7836 } else { 7837 tmp_len = sizeof(struct scsi_per_res_in_header); 7838 scsi_ulto4b(0, res->header.length); 7839 header_only = 1; 7840 } 7841 7842 /* 7843 * We had to drop the lock to allocate our buffer, which 7844 * leaves time for someone to come in with another 7845 * persistent reservation. (That is unlikely, though, 7846 * since this should be the only persistent reservation 7847 * command active right now.) 7848 */ 7849 if (tmp_len != total_len) { 7850 mtx_unlock(&lun->lun_lock); 7851 free(ctsio->kern_data_ptr, M_CTL); 7852 printf("%s: reservation status changed, retrying\n", 7853 __func__); 7854 goto retry; 7855 } 7856 7857 /* 7858 * No reservation held, so we're done. 7859 */ 7860 if (header_only != 0) 7861 break; 7862 7863 /* 7864 * If the registration is an All Registrants type, the key 7865 * is 0, since it doesn't really matter. 7866 */ 7867 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7868 memcpy(res->data.reservation, 7869 &lun->per_res[lun->pr_res_idx].res_key, 7870 sizeof(struct scsi_per_res_key)); 7871 } 7872 res->data.scopetype = lun->res_type; 7873 break; 7874 } 7875 case SPRI_RC: //report capabilities 7876 { 7877 struct scsi_per_res_cap *res_cap; 7878 uint16_t type_mask; 7879 7880 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7881 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7882 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3; 7883 type_mask = SPRI_TM_WR_EX_AR | 7884 SPRI_TM_EX_AC_RO | 7885 SPRI_TM_WR_EX_RO | 7886 SPRI_TM_EX_AC | 7887 SPRI_TM_WR_EX | 7888 SPRI_TM_EX_AC_AR; 7889 scsi_ulto2b(type_mask, res_cap->type_mask); 7890 break; 7891 } 7892 case SPRI_RS: { // read full status 7893 struct scsi_per_res_in_full *res_status; 7894 struct scsi_per_res_in_full_desc *res_desc; 7895 struct ctl_port *port; 7896 int i, len; 7897 7898 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7899 7900 /* 7901 * We had to drop the lock to allocate our buffer, which 7902 * leaves time for someone to come in with another 7903 * persistent reservation. (That is unlikely, though, 7904 * since this should be the only persistent reservation 7905 * command active right now.) 7906 */ 7907 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7908 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7909 lun->pr_key_count)){ 7910 mtx_unlock(&lun->lun_lock); 7911 free(ctsio->kern_data_ptr, M_CTL); 7912 printf("%s: reservation length changed, retrying\n", 7913 __func__); 7914 goto retry; 7915 } 7916 7917 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7918 7919 res_desc = &res_status->desc[0]; 7920 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7921 if (!lun->per_res[i].registered) 7922 continue; 7923 7924 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key, 7925 sizeof(res_desc->res_key)); 7926 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7927 (lun->pr_res_idx == i || 7928 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7929 res_desc->flags = SPRI_FULL_R_HOLDER; 7930 res_desc->scopetype = lun->res_type; 7931 } 7932 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7933 res_desc->rel_trgt_port_id); 7934 len = 0; 7935 port = softc->ctl_ports[ 7936 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7937 if (port != NULL) 7938 len = ctl_create_iid(port, 7939 i % CTL_MAX_INIT_PER_PORT, 7940 res_desc->transport_id); 7941 scsi_ulto4b(len, res_desc->additional_length); 7942 res_desc = (struct scsi_per_res_in_full_desc *) 7943 &res_desc->transport_id[len]; 7944 } 7945 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7946 res_status->header.length); 7947 break; 7948 } 7949 default: 7950 /* 7951 * This is a bug, because we just checked for this above, 7952 * and should have returned an error. 7953 */ 7954 panic("Invalid PR type %x", cdb->action); 7955 break; /* NOTREACHED */ 7956 } 7957 mtx_unlock(&lun->lun_lock); 7958 7959 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7960 ctsio->be_move_done = ctl_config_move_done; 7961 7962 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7963 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7964 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7965 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7966 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7967 7968 ctl_datamove((union ctl_io *)ctsio); 7969 7970 return (CTL_RETVAL_COMPLETE); 7971 } 7972 7973 /* 7974 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7975 * it should return. 7976 */ 7977 static int 7978 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7979 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7980 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7981 struct scsi_per_res_out_parms* param) 7982 { 7983 union ctl_ha_msg persis_io; 7984 int retval, i; 7985 int isc_retval; 7986 7987 retval = 0; 7988 7989 mtx_lock(&lun->lun_lock); 7990 if (sa_res_key == 0) { 7991 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7992 /* validate scope and type */ 7993 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7994 SPR_LU_SCOPE) { 7995 mtx_unlock(&lun->lun_lock); 7996 ctl_set_invalid_field(/*ctsio*/ ctsio, 7997 /*sks_valid*/ 1, 7998 /*command*/ 1, 7999 /*field*/ 2, 8000 /*bit_valid*/ 1, 8001 /*bit*/ 4); 8002 ctl_done((union ctl_io *)ctsio); 8003 return (1); 8004 } 8005 8006 if (type>8 || type==2 || type==4 || type==0) { 8007 mtx_unlock(&lun->lun_lock); 8008 ctl_set_invalid_field(/*ctsio*/ ctsio, 8009 /*sks_valid*/ 1, 8010 /*command*/ 1, 8011 /*field*/ 2, 8012 /*bit_valid*/ 1, 8013 /*bit*/ 0); 8014 ctl_done((union ctl_io *)ctsio); 8015 return (1); 8016 } 8017 8018 /* temporarily unregister this nexus */ 8019 lun->per_res[residx].registered = 0; 8020 8021 /* 8022 * Unregister everybody else and build UA for 8023 * them 8024 */ 8025 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8026 if (lun->per_res[i].registered == 0) 8027 continue; 8028 8029 if (!persis_offset 8030 && i <CTL_MAX_INITIATORS) 8031 lun->pending_ua[i] |= 8032 CTL_UA_REG_PREEMPT; 8033 else if (persis_offset 8034 && i >= persis_offset) 8035 lun->pending_ua[i-persis_offset] |= 8036 CTL_UA_REG_PREEMPT; 8037 lun->per_res[i].registered = 0; 8038 memset(&lun->per_res[i].res_key, 0, 8039 sizeof(struct scsi_per_res_key)); 8040 } 8041 lun->per_res[residx].registered = 1; 8042 lun->pr_key_count = 1; 8043 lun->res_type = type; 8044 if (lun->res_type != SPR_TYPE_WR_EX_AR 8045 && lun->res_type != SPR_TYPE_EX_AC_AR) 8046 lun->pr_res_idx = residx; 8047 8048 /* send msg to other side */ 8049 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8050 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8051 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8052 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8053 persis_io.pr.pr_info.res_type = type; 8054 memcpy(persis_io.pr.pr_info.sa_res_key, 8055 param->serv_act_res_key, 8056 sizeof(param->serv_act_res_key)); 8057 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8058 &persis_io, sizeof(persis_io), 0)) > 8059 CTL_HA_STATUS_SUCCESS) { 8060 printf("CTL:Persis Out error returned " 8061 "from ctl_ha_msg_send %d\n", 8062 isc_retval); 8063 } 8064 } else { 8065 /* not all registrants */ 8066 mtx_unlock(&lun->lun_lock); 8067 free(ctsio->kern_data_ptr, M_CTL); 8068 ctl_set_invalid_field(ctsio, 8069 /*sks_valid*/ 1, 8070 /*command*/ 0, 8071 /*field*/ 8, 8072 /*bit_valid*/ 0, 8073 /*bit*/ 0); 8074 ctl_done((union ctl_io *)ctsio); 8075 return (1); 8076 } 8077 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8078 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8079 int found = 0; 8080 8081 if (res_key == sa_res_key) { 8082 /* special case */ 8083 /* 8084 * The spec implies this is not good but doesn't 8085 * say what to do. There are two choices either 8086 * generate a res conflict or check condition 8087 * with illegal field in parameter data. Since 8088 * that is what is done when the sa_res_key is 8089 * zero I'll take that approach since this has 8090 * to do with the sa_res_key. 8091 */ 8092 mtx_unlock(&lun->lun_lock); 8093 free(ctsio->kern_data_ptr, M_CTL); 8094 ctl_set_invalid_field(ctsio, 8095 /*sks_valid*/ 1, 8096 /*command*/ 0, 8097 /*field*/ 8, 8098 /*bit_valid*/ 0, 8099 /*bit*/ 0); 8100 ctl_done((union ctl_io *)ctsio); 8101 return (1); 8102 } 8103 8104 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8105 if (lun->per_res[i].registered 8106 && memcmp(param->serv_act_res_key, 8107 lun->per_res[i].res_key.key, 8108 sizeof(struct scsi_per_res_key)) != 0) 8109 continue; 8110 8111 found = 1; 8112 lun->per_res[i].registered = 0; 8113 memset(&lun->per_res[i].res_key, 0, 8114 sizeof(struct scsi_per_res_key)); 8115 lun->pr_key_count--; 8116 8117 if (!persis_offset && i < CTL_MAX_INITIATORS) 8118 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8119 else if (persis_offset && i >= persis_offset) 8120 lun->pending_ua[i-persis_offset] |= 8121 CTL_UA_REG_PREEMPT; 8122 } 8123 if (!found) { 8124 mtx_unlock(&lun->lun_lock); 8125 free(ctsio->kern_data_ptr, M_CTL); 8126 ctl_set_reservation_conflict(ctsio); 8127 ctl_done((union ctl_io *)ctsio); 8128 return (CTL_RETVAL_COMPLETE); 8129 } 8130 /* send msg to other side */ 8131 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8132 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8133 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8134 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8135 persis_io.pr.pr_info.res_type = type; 8136 memcpy(persis_io.pr.pr_info.sa_res_key, 8137 param->serv_act_res_key, 8138 sizeof(param->serv_act_res_key)); 8139 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8140 &persis_io, sizeof(persis_io), 0)) > 8141 CTL_HA_STATUS_SUCCESS) { 8142 printf("CTL:Persis Out error returned from " 8143 "ctl_ha_msg_send %d\n", isc_retval); 8144 } 8145 } else { 8146 /* Reserved but not all registrants */ 8147 /* sa_res_key is res holder */ 8148 if (memcmp(param->serv_act_res_key, 8149 lun->per_res[lun->pr_res_idx].res_key.key, 8150 sizeof(struct scsi_per_res_key)) == 0) { 8151 /* validate scope and type */ 8152 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8153 SPR_LU_SCOPE) { 8154 mtx_unlock(&lun->lun_lock); 8155 ctl_set_invalid_field(/*ctsio*/ ctsio, 8156 /*sks_valid*/ 1, 8157 /*command*/ 1, 8158 /*field*/ 2, 8159 /*bit_valid*/ 1, 8160 /*bit*/ 4); 8161 ctl_done((union ctl_io *)ctsio); 8162 return (1); 8163 } 8164 8165 if (type>8 || type==2 || type==4 || type==0) { 8166 mtx_unlock(&lun->lun_lock); 8167 ctl_set_invalid_field(/*ctsio*/ ctsio, 8168 /*sks_valid*/ 1, 8169 /*command*/ 1, 8170 /*field*/ 2, 8171 /*bit_valid*/ 1, 8172 /*bit*/ 0); 8173 ctl_done((union ctl_io *)ctsio); 8174 return (1); 8175 } 8176 8177 /* 8178 * Do the following: 8179 * if sa_res_key != res_key remove all 8180 * registrants w/sa_res_key and generate UA 8181 * for these registrants(Registrations 8182 * Preempted) if it wasn't an exclusive 8183 * reservation generate UA(Reservations 8184 * Preempted) for all other registered nexuses 8185 * if the type has changed. Establish the new 8186 * reservation and holder. If res_key and 8187 * sa_res_key are the same do the above 8188 * except don't unregister the res holder. 8189 */ 8190 8191 /* 8192 * Temporarily unregister so it won't get 8193 * removed or UA generated 8194 */ 8195 lun->per_res[residx].registered = 0; 8196 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8197 if (lun->per_res[i].registered == 0) 8198 continue; 8199 8200 if (memcmp(param->serv_act_res_key, 8201 lun->per_res[i].res_key.key, 8202 sizeof(struct scsi_per_res_key)) == 0) { 8203 lun->per_res[i].registered = 0; 8204 memset(&lun->per_res[i].res_key, 8205 0, 8206 sizeof(struct scsi_per_res_key)); 8207 lun->pr_key_count--; 8208 8209 if (!persis_offset 8210 && i < CTL_MAX_INITIATORS) 8211 lun->pending_ua[i] |= 8212 CTL_UA_REG_PREEMPT; 8213 else if (persis_offset 8214 && i >= persis_offset) 8215 lun->pending_ua[i-persis_offset] |= 8216 CTL_UA_REG_PREEMPT; 8217 } else if (type != lun->res_type 8218 && (lun->res_type == SPR_TYPE_WR_EX_RO 8219 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8220 if (!persis_offset 8221 && i < CTL_MAX_INITIATORS) 8222 lun->pending_ua[i] |= 8223 CTL_UA_RES_RELEASE; 8224 else if (persis_offset 8225 && i >= persis_offset) 8226 lun->pending_ua[ 8227 i-persis_offset] |= 8228 CTL_UA_RES_RELEASE; 8229 } 8230 } 8231 lun->per_res[residx].registered = 1; 8232 lun->res_type = type; 8233 if (lun->res_type != SPR_TYPE_WR_EX_AR 8234 && lun->res_type != SPR_TYPE_EX_AC_AR) 8235 lun->pr_res_idx = residx; 8236 else 8237 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8238 8239 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8240 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8241 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8242 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8243 persis_io.pr.pr_info.res_type = type; 8244 memcpy(persis_io.pr.pr_info.sa_res_key, 8245 param->serv_act_res_key, 8246 sizeof(param->serv_act_res_key)); 8247 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8248 &persis_io, sizeof(persis_io), 0)) > 8249 CTL_HA_STATUS_SUCCESS) { 8250 printf("CTL:Persis Out error returned " 8251 "from ctl_ha_msg_send %d\n", 8252 isc_retval); 8253 } 8254 } else { 8255 /* 8256 * sa_res_key is not the res holder just 8257 * remove registrants 8258 */ 8259 int found=0; 8260 8261 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8262 if (memcmp(param->serv_act_res_key, 8263 lun->per_res[i].res_key.key, 8264 sizeof(struct scsi_per_res_key)) != 0) 8265 continue; 8266 8267 found = 1; 8268 lun->per_res[i].registered = 0; 8269 memset(&lun->per_res[i].res_key, 0, 8270 sizeof(struct scsi_per_res_key)); 8271 lun->pr_key_count--; 8272 8273 if (!persis_offset 8274 && i < CTL_MAX_INITIATORS) 8275 lun->pending_ua[i] |= 8276 CTL_UA_REG_PREEMPT; 8277 else if (persis_offset 8278 && i >= persis_offset) 8279 lun->pending_ua[i-persis_offset] |= 8280 CTL_UA_REG_PREEMPT; 8281 } 8282 8283 if (!found) { 8284 mtx_unlock(&lun->lun_lock); 8285 free(ctsio->kern_data_ptr, M_CTL); 8286 ctl_set_reservation_conflict(ctsio); 8287 ctl_done((union ctl_io *)ctsio); 8288 return (1); 8289 } 8290 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8291 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8292 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8293 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8294 persis_io.pr.pr_info.res_type = type; 8295 memcpy(persis_io.pr.pr_info.sa_res_key, 8296 param->serv_act_res_key, 8297 sizeof(param->serv_act_res_key)); 8298 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8299 &persis_io, sizeof(persis_io), 0)) > 8300 CTL_HA_STATUS_SUCCESS) { 8301 printf("CTL:Persis Out error returned " 8302 "from ctl_ha_msg_send %d\n", 8303 isc_retval); 8304 } 8305 } 8306 } 8307 8308 lun->PRGeneration++; 8309 mtx_unlock(&lun->lun_lock); 8310 8311 return (retval); 8312 } 8313 8314 static void 8315 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8316 { 8317 int i; 8318 8319 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8320 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8321 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 8322 msg->pr.pr_info.sa_res_key, 8323 sizeof(struct scsi_per_res_key)) != 0) { 8324 uint64_t sa_res_key; 8325 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8326 8327 if (sa_res_key == 0) { 8328 /* temporarily unregister this nexus */ 8329 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8330 8331 /* 8332 * Unregister everybody else and build UA for 8333 * them 8334 */ 8335 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8336 if (lun->per_res[i].registered == 0) 8337 continue; 8338 8339 if (!persis_offset 8340 && i < CTL_MAX_INITIATORS) 8341 lun->pending_ua[i] |= 8342 CTL_UA_REG_PREEMPT; 8343 else if (persis_offset && i >= persis_offset) 8344 lun->pending_ua[i - persis_offset] |= 8345 CTL_UA_REG_PREEMPT; 8346 lun->per_res[i].registered = 0; 8347 memset(&lun->per_res[i].res_key, 0, 8348 sizeof(struct scsi_per_res_key)); 8349 } 8350 8351 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8352 lun->pr_key_count = 1; 8353 lun->res_type = msg->pr.pr_info.res_type; 8354 if (lun->res_type != SPR_TYPE_WR_EX_AR 8355 && lun->res_type != SPR_TYPE_EX_AC_AR) 8356 lun->pr_res_idx = msg->pr.pr_info.residx; 8357 } else { 8358 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8359 if (memcmp(msg->pr.pr_info.sa_res_key, 8360 lun->per_res[i].res_key.key, 8361 sizeof(struct scsi_per_res_key)) != 0) 8362 continue; 8363 8364 lun->per_res[i].registered = 0; 8365 memset(&lun->per_res[i].res_key, 0, 8366 sizeof(struct scsi_per_res_key)); 8367 lun->pr_key_count--; 8368 8369 if (!persis_offset 8370 && i < persis_offset) 8371 lun->pending_ua[i] |= 8372 CTL_UA_REG_PREEMPT; 8373 else if (persis_offset 8374 && i >= persis_offset) 8375 lun->pending_ua[i - persis_offset] |= 8376 CTL_UA_REG_PREEMPT; 8377 } 8378 } 8379 } else { 8380 /* 8381 * Temporarily unregister so it won't get removed 8382 * or UA generated 8383 */ 8384 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8385 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8386 if (lun->per_res[i].registered == 0) 8387 continue; 8388 8389 if (memcmp(msg->pr.pr_info.sa_res_key, 8390 lun->per_res[i].res_key.key, 8391 sizeof(struct scsi_per_res_key)) == 0) { 8392 lun->per_res[i].registered = 0; 8393 memset(&lun->per_res[i].res_key, 0, 8394 sizeof(struct scsi_per_res_key)); 8395 lun->pr_key_count--; 8396 if (!persis_offset 8397 && i < CTL_MAX_INITIATORS) 8398 lun->pending_ua[i] |= 8399 CTL_UA_REG_PREEMPT; 8400 else if (persis_offset 8401 && i >= persis_offset) 8402 lun->pending_ua[i - persis_offset] |= 8403 CTL_UA_REG_PREEMPT; 8404 } else if (msg->pr.pr_info.res_type != lun->res_type 8405 && (lun->res_type == SPR_TYPE_WR_EX_RO 8406 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8407 if (!persis_offset 8408 && i < persis_offset) 8409 lun->pending_ua[i] |= 8410 CTL_UA_RES_RELEASE; 8411 else if (persis_offset 8412 && i >= persis_offset) 8413 lun->pending_ua[i - persis_offset] |= 8414 CTL_UA_RES_RELEASE; 8415 } 8416 } 8417 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8418 lun->res_type = msg->pr.pr_info.res_type; 8419 if (lun->res_type != SPR_TYPE_WR_EX_AR 8420 && lun->res_type != SPR_TYPE_EX_AC_AR) 8421 lun->pr_res_idx = msg->pr.pr_info.residx; 8422 else 8423 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8424 } 8425 lun->PRGeneration++; 8426 8427 } 8428 8429 8430 int 8431 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8432 { 8433 int retval; 8434 int isc_retval; 8435 u_int32_t param_len; 8436 struct scsi_per_res_out *cdb; 8437 struct ctl_lun *lun; 8438 struct scsi_per_res_out_parms* param; 8439 struct ctl_softc *softc; 8440 uint32_t residx; 8441 uint64_t res_key, sa_res_key; 8442 uint8_t type; 8443 union ctl_ha_msg persis_io; 8444 int i; 8445 8446 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8447 8448 retval = CTL_RETVAL_COMPLETE; 8449 8450 softc = control_softc; 8451 8452 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8453 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8454 8455 /* 8456 * We only support whole-LUN scope. The scope & type are ignored for 8457 * register, register and ignore existing key and clear. 8458 * We sometimes ignore scope and type on preempts too!! 8459 * Verify reservation type here as well. 8460 */ 8461 type = cdb->scope_type & SPR_TYPE_MASK; 8462 if ((cdb->action == SPRO_RESERVE) 8463 || (cdb->action == SPRO_RELEASE)) { 8464 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8465 ctl_set_invalid_field(/*ctsio*/ ctsio, 8466 /*sks_valid*/ 1, 8467 /*command*/ 1, 8468 /*field*/ 2, 8469 /*bit_valid*/ 1, 8470 /*bit*/ 4); 8471 ctl_done((union ctl_io *)ctsio); 8472 return (CTL_RETVAL_COMPLETE); 8473 } 8474 8475 if (type>8 || type==2 || type==4 || type==0) { 8476 ctl_set_invalid_field(/*ctsio*/ ctsio, 8477 /*sks_valid*/ 1, 8478 /*command*/ 1, 8479 /*field*/ 2, 8480 /*bit_valid*/ 1, 8481 /*bit*/ 0); 8482 ctl_done((union ctl_io *)ctsio); 8483 return (CTL_RETVAL_COMPLETE); 8484 } 8485 } 8486 8487 param_len = scsi_4btoul(cdb->length); 8488 8489 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8490 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8491 ctsio->kern_data_len = param_len; 8492 ctsio->kern_total_len = param_len; 8493 ctsio->kern_data_resid = 0; 8494 ctsio->kern_rel_offset = 0; 8495 ctsio->kern_sg_entries = 0; 8496 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8497 ctsio->be_move_done = ctl_config_move_done; 8498 ctl_datamove((union ctl_io *)ctsio); 8499 8500 return (CTL_RETVAL_COMPLETE); 8501 } 8502 8503 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8504 8505 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8506 res_key = scsi_8btou64(param->res_key.key); 8507 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8508 8509 /* 8510 * Validate the reservation key here except for SPRO_REG_IGNO 8511 * This must be done for all other service actions 8512 */ 8513 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8514 mtx_lock(&lun->lun_lock); 8515 if (lun->per_res[residx].registered) { 8516 if (memcmp(param->res_key.key, 8517 lun->per_res[residx].res_key.key, 8518 ctl_min(sizeof(param->res_key), 8519 sizeof(lun->per_res[residx].res_key))) != 0) { 8520 /* 8521 * The current key passed in doesn't match 8522 * the one the initiator previously 8523 * registered. 8524 */ 8525 mtx_unlock(&lun->lun_lock); 8526 free(ctsio->kern_data_ptr, M_CTL); 8527 ctl_set_reservation_conflict(ctsio); 8528 ctl_done((union ctl_io *)ctsio); 8529 return (CTL_RETVAL_COMPLETE); 8530 } 8531 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8532 /* 8533 * We are not registered 8534 */ 8535 mtx_unlock(&lun->lun_lock); 8536 free(ctsio->kern_data_ptr, M_CTL); 8537 ctl_set_reservation_conflict(ctsio); 8538 ctl_done((union ctl_io *)ctsio); 8539 return (CTL_RETVAL_COMPLETE); 8540 } else if (res_key != 0) { 8541 /* 8542 * We are not registered and trying to register but 8543 * the register key isn't zero. 8544 */ 8545 mtx_unlock(&lun->lun_lock); 8546 free(ctsio->kern_data_ptr, M_CTL); 8547 ctl_set_reservation_conflict(ctsio); 8548 ctl_done((union ctl_io *)ctsio); 8549 return (CTL_RETVAL_COMPLETE); 8550 } 8551 mtx_unlock(&lun->lun_lock); 8552 } 8553 8554 switch (cdb->action & SPRO_ACTION_MASK) { 8555 case SPRO_REGISTER: 8556 case SPRO_REG_IGNO: { 8557 8558 #if 0 8559 printf("Registration received\n"); 8560 #endif 8561 8562 /* 8563 * We don't support any of these options, as we report in 8564 * the read capabilities request (see 8565 * ctl_persistent_reserve_in(), above). 8566 */ 8567 if ((param->flags & SPR_SPEC_I_PT) 8568 || (param->flags & SPR_ALL_TG_PT) 8569 || (param->flags & SPR_APTPL)) { 8570 int bit_ptr; 8571 8572 if (param->flags & SPR_APTPL) 8573 bit_ptr = 0; 8574 else if (param->flags & SPR_ALL_TG_PT) 8575 bit_ptr = 2; 8576 else /* SPR_SPEC_I_PT */ 8577 bit_ptr = 3; 8578 8579 free(ctsio->kern_data_ptr, M_CTL); 8580 ctl_set_invalid_field(ctsio, 8581 /*sks_valid*/ 1, 8582 /*command*/ 0, 8583 /*field*/ 20, 8584 /*bit_valid*/ 1, 8585 /*bit*/ bit_ptr); 8586 ctl_done((union ctl_io *)ctsio); 8587 return (CTL_RETVAL_COMPLETE); 8588 } 8589 8590 mtx_lock(&lun->lun_lock); 8591 8592 /* 8593 * The initiator wants to clear the 8594 * key/unregister. 8595 */ 8596 if (sa_res_key == 0) { 8597 if ((res_key == 0 8598 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8599 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8600 && !lun->per_res[residx].registered)) { 8601 mtx_unlock(&lun->lun_lock); 8602 goto done; 8603 } 8604 8605 lun->per_res[residx].registered = 0; 8606 memset(&lun->per_res[residx].res_key, 8607 0, sizeof(lun->per_res[residx].res_key)); 8608 lun->pr_key_count--; 8609 8610 if (residx == lun->pr_res_idx) { 8611 lun->flags &= ~CTL_LUN_PR_RESERVED; 8612 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8613 8614 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8615 || lun->res_type == SPR_TYPE_EX_AC_RO) 8616 && lun->pr_key_count) { 8617 /* 8618 * If the reservation is a registrants 8619 * only type we need to generate a UA 8620 * for other registered inits. The 8621 * sense code should be RESERVATIONS 8622 * RELEASED 8623 */ 8624 8625 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8626 if (lun->per_res[ 8627 i+persis_offset].registered 8628 == 0) 8629 continue; 8630 lun->pending_ua[i] |= 8631 CTL_UA_RES_RELEASE; 8632 } 8633 } 8634 lun->res_type = 0; 8635 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8636 if (lun->pr_key_count==0) { 8637 lun->flags &= ~CTL_LUN_PR_RESERVED; 8638 lun->res_type = 0; 8639 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8640 } 8641 } 8642 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8643 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8644 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8645 persis_io.pr.pr_info.residx = residx; 8646 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8647 &persis_io, sizeof(persis_io), 0 )) > 8648 CTL_HA_STATUS_SUCCESS) { 8649 printf("CTL:Persis Out error returned from " 8650 "ctl_ha_msg_send %d\n", isc_retval); 8651 } 8652 } else /* sa_res_key != 0 */ { 8653 8654 /* 8655 * If we aren't registered currently then increment 8656 * the key count and set the registered flag. 8657 */ 8658 if (!lun->per_res[residx].registered) { 8659 lun->pr_key_count++; 8660 lun->per_res[residx].registered = 1; 8661 } 8662 8663 memcpy(&lun->per_res[residx].res_key, 8664 param->serv_act_res_key, 8665 ctl_min(sizeof(param->serv_act_res_key), 8666 sizeof(lun->per_res[residx].res_key))); 8667 8668 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8669 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8670 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8671 persis_io.pr.pr_info.residx = residx; 8672 memcpy(persis_io.pr.pr_info.sa_res_key, 8673 param->serv_act_res_key, 8674 sizeof(param->serv_act_res_key)); 8675 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8676 &persis_io, sizeof(persis_io), 0)) > 8677 CTL_HA_STATUS_SUCCESS) { 8678 printf("CTL:Persis Out error returned from " 8679 "ctl_ha_msg_send %d\n", isc_retval); 8680 } 8681 } 8682 lun->PRGeneration++; 8683 mtx_unlock(&lun->lun_lock); 8684 8685 break; 8686 } 8687 case SPRO_RESERVE: 8688 #if 0 8689 printf("Reserve executed type %d\n", type); 8690 #endif 8691 mtx_lock(&lun->lun_lock); 8692 if (lun->flags & CTL_LUN_PR_RESERVED) { 8693 /* 8694 * if this isn't the reservation holder and it's 8695 * not a "all registrants" type or if the type is 8696 * different then we have a conflict 8697 */ 8698 if ((lun->pr_res_idx != residx 8699 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8700 || lun->res_type != type) { 8701 mtx_unlock(&lun->lun_lock); 8702 free(ctsio->kern_data_ptr, M_CTL); 8703 ctl_set_reservation_conflict(ctsio); 8704 ctl_done((union ctl_io *)ctsio); 8705 return (CTL_RETVAL_COMPLETE); 8706 } 8707 mtx_unlock(&lun->lun_lock); 8708 } else /* create a reservation */ { 8709 /* 8710 * If it's not an "all registrants" type record 8711 * reservation holder 8712 */ 8713 if (type != SPR_TYPE_WR_EX_AR 8714 && type != SPR_TYPE_EX_AC_AR) 8715 lun->pr_res_idx = residx; /* Res holder */ 8716 else 8717 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8718 8719 lun->flags |= CTL_LUN_PR_RESERVED; 8720 lun->res_type = type; 8721 8722 mtx_unlock(&lun->lun_lock); 8723 8724 /* send msg to other side */ 8725 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8726 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8727 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8728 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8729 persis_io.pr.pr_info.res_type = type; 8730 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8731 &persis_io, sizeof(persis_io), 0)) > 8732 CTL_HA_STATUS_SUCCESS) { 8733 printf("CTL:Persis Out error returned from " 8734 "ctl_ha_msg_send %d\n", isc_retval); 8735 } 8736 } 8737 break; 8738 8739 case SPRO_RELEASE: 8740 mtx_lock(&lun->lun_lock); 8741 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8742 /* No reservation exists return good status */ 8743 mtx_unlock(&lun->lun_lock); 8744 goto done; 8745 } 8746 /* 8747 * Is this nexus a reservation holder? 8748 */ 8749 if (lun->pr_res_idx != residx 8750 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8751 /* 8752 * not a res holder return good status but 8753 * do nothing 8754 */ 8755 mtx_unlock(&lun->lun_lock); 8756 goto done; 8757 } 8758 8759 if (lun->res_type != type) { 8760 mtx_unlock(&lun->lun_lock); 8761 free(ctsio->kern_data_ptr, M_CTL); 8762 ctl_set_illegal_pr_release(ctsio); 8763 ctl_done((union ctl_io *)ctsio); 8764 return (CTL_RETVAL_COMPLETE); 8765 } 8766 8767 /* okay to release */ 8768 lun->flags &= ~CTL_LUN_PR_RESERVED; 8769 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8770 lun->res_type = 0; 8771 8772 /* 8773 * if this isn't an exclusive access 8774 * res generate UA for all other 8775 * registrants. 8776 */ 8777 if (type != SPR_TYPE_EX_AC 8778 && type != SPR_TYPE_WR_EX) { 8779 /* 8780 * temporarily unregister so we don't generate UA 8781 */ 8782 lun->per_res[residx].registered = 0; 8783 8784 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8785 if (lun->per_res[i+persis_offset].registered 8786 == 0) 8787 continue; 8788 lun->pending_ua[i] |= 8789 CTL_UA_RES_RELEASE; 8790 } 8791 8792 lun->per_res[residx].registered = 1; 8793 } 8794 mtx_unlock(&lun->lun_lock); 8795 /* Send msg to other side */ 8796 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8797 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8798 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8799 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8800 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8801 printf("CTL:Persis Out error returned from " 8802 "ctl_ha_msg_send %d\n", isc_retval); 8803 } 8804 break; 8805 8806 case SPRO_CLEAR: 8807 /* send msg to other side */ 8808 8809 mtx_lock(&lun->lun_lock); 8810 lun->flags &= ~CTL_LUN_PR_RESERVED; 8811 lun->res_type = 0; 8812 lun->pr_key_count = 0; 8813 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8814 8815 8816 memset(&lun->per_res[residx].res_key, 8817 0, sizeof(lun->per_res[residx].res_key)); 8818 lun->per_res[residx].registered = 0; 8819 8820 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8821 if (lun->per_res[i].registered) { 8822 if (!persis_offset && i < CTL_MAX_INITIATORS) 8823 lun->pending_ua[i] |= 8824 CTL_UA_RES_PREEMPT; 8825 else if (persis_offset && i >= persis_offset) 8826 lun->pending_ua[i-persis_offset] |= 8827 CTL_UA_RES_PREEMPT; 8828 8829 memset(&lun->per_res[i].res_key, 8830 0, sizeof(struct scsi_per_res_key)); 8831 lun->per_res[i].registered = 0; 8832 } 8833 lun->PRGeneration++; 8834 mtx_unlock(&lun->lun_lock); 8835 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8836 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8837 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8838 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8839 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8840 printf("CTL:Persis Out error returned from " 8841 "ctl_ha_msg_send %d\n", isc_retval); 8842 } 8843 break; 8844 8845 case SPRO_PREEMPT: { 8846 int nretval; 8847 8848 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8849 residx, ctsio, cdb, param); 8850 if (nretval != 0) 8851 return (CTL_RETVAL_COMPLETE); 8852 break; 8853 } 8854 default: 8855 panic("Invalid PR type %x", cdb->action); 8856 } 8857 8858 done: 8859 free(ctsio->kern_data_ptr, M_CTL); 8860 ctl_set_success(ctsio); 8861 ctl_done((union ctl_io *)ctsio); 8862 8863 return (retval); 8864 } 8865 8866 /* 8867 * This routine is for handling a message from the other SC pertaining to 8868 * persistent reserve out. All the error checking will have been done 8869 * so only perorming the action need be done here to keep the two 8870 * in sync. 8871 */ 8872 static void 8873 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8874 { 8875 struct ctl_lun *lun; 8876 struct ctl_softc *softc; 8877 int i; 8878 uint32_t targ_lun; 8879 8880 softc = control_softc; 8881 8882 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8883 lun = softc->ctl_luns[targ_lun]; 8884 mtx_lock(&lun->lun_lock); 8885 switch(msg->pr.pr_info.action) { 8886 case CTL_PR_REG_KEY: 8887 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8888 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8889 lun->pr_key_count++; 8890 } 8891 lun->PRGeneration++; 8892 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8893 msg->pr.pr_info.sa_res_key, 8894 sizeof(struct scsi_per_res_key)); 8895 break; 8896 8897 case CTL_PR_UNREG_KEY: 8898 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8899 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8900 0, sizeof(struct scsi_per_res_key)); 8901 lun->pr_key_count--; 8902 8903 /* XXX Need to see if the reservation has been released */ 8904 /* if so do we need to generate UA? */ 8905 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8906 lun->flags &= ~CTL_LUN_PR_RESERVED; 8907 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8908 8909 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8910 || lun->res_type == SPR_TYPE_EX_AC_RO) 8911 && lun->pr_key_count) { 8912 /* 8913 * If the reservation is a registrants 8914 * only type we need to generate a UA 8915 * for other registered inits. The 8916 * sense code should be RESERVATIONS 8917 * RELEASED 8918 */ 8919 8920 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8921 if (lun->per_res[i+ 8922 persis_offset].registered == 0) 8923 continue; 8924 8925 lun->pending_ua[i] |= 8926 CTL_UA_RES_RELEASE; 8927 } 8928 } 8929 lun->res_type = 0; 8930 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8931 if (lun->pr_key_count==0) { 8932 lun->flags &= ~CTL_LUN_PR_RESERVED; 8933 lun->res_type = 0; 8934 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8935 } 8936 } 8937 lun->PRGeneration++; 8938 break; 8939 8940 case CTL_PR_RESERVE: 8941 lun->flags |= CTL_LUN_PR_RESERVED; 8942 lun->res_type = msg->pr.pr_info.res_type; 8943 lun->pr_res_idx = msg->pr.pr_info.residx; 8944 8945 break; 8946 8947 case CTL_PR_RELEASE: 8948 /* 8949 * if this isn't an exclusive access res generate UA for all 8950 * other registrants. 8951 */ 8952 if (lun->res_type != SPR_TYPE_EX_AC 8953 && lun->res_type != SPR_TYPE_WR_EX) { 8954 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8955 if (lun->per_res[i+persis_offset].registered) 8956 lun->pending_ua[i] |= 8957 CTL_UA_RES_RELEASE; 8958 } 8959 8960 lun->flags &= ~CTL_LUN_PR_RESERVED; 8961 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8962 lun->res_type = 0; 8963 break; 8964 8965 case CTL_PR_PREEMPT: 8966 ctl_pro_preempt_other(lun, msg); 8967 break; 8968 case CTL_PR_CLEAR: 8969 lun->flags &= ~CTL_LUN_PR_RESERVED; 8970 lun->res_type = 0; 8971 lun->pr_key_count = 0; 8972 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8973 8974 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8975 if (lun->per_res[i].registered == 0) 8976 continue; 8977 if (!persis_offset 8978 && i < CTL_MAX_INITIATORS) 8979 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 8980 else if (persis_offset 8981 && i >= persis_offset) 8982 lun->pending_ua[i-persis_offset] |= 8983 CTL_UA_RES_PREEMPT; 8984 memset(&lun->per_res[i].res_key, 0, 8985 sizeof(struct scsi_per_res_key)); 8986 lun->per_res[i].registered = 0; 8987 } 8988 lun->PRGeneration++; 8989 break; 8990 } 8991 8992 mtx_unlock(&lun->lun_lock); 8993 } 8994 8995 int 8996 ctl_read_write(struct ctl_scsiio *ctsio) 8997 { 8998 struct ctl_lun *lun; 8999 struct ctl_lba_len_flags *lbalen; 9000 uint64_t lba; 9001 uint32_t num_blocks; 9002 int flags, retval; 9003 int isread; 9004 9005 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9006 9007 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 9008 9009 flags = 0; 9010 retval = CTL_RETVAL_COMPLETE; 9011 9012 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 9013 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 9014 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 9015 uint32_t residx; 9016 9017 /* 9018 * XXX KDM need a lock here. 9019 */ 9020 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 9021 if ((lun->res_type == SPR_TYPE_EX_AC 9022 && residx != lun->pr_res_idx) 9023 || ((lun->res_type == SPR_TYPE_EX_AC_RO 9024 || lun->res_type == SPR_TYPE_EX_AC_AR) 9025 && !lun->per_res[residx].registered)) { 9026 ctl_set_reservation_conflict(ctsio); 9027 ctl_done((union ctl_io *)ctsio); 9028 return (CTL_RETVAL_COMPLETE); 9029 } 9030 } 9031 9032 switch (ctsio->cdb[0]) { 9033 case READ_6: 9034 case WRITE_6: { 9035 struct scsi_rw_6 *cdb; 9036 9037 cdb = (struct scsi_rw_6 *)ctsio->cdb; 9038 9039 lba = scsi_3btoul(cdb->addr); 9040 /* only 5 bits are valid in the most significant address byte */ 9041 lba &= 0x1fffff; 9042 num_blocks = cdb->length; 9043 /* 9044 * This is correct according to SBC-2. 9045 */ 9046 if (num_blocks == 0) 9047 num_blocks = 256; 9048 break; 9049 } 9050 case READ_10: 9051 case WRITE_10: { 9052 struct scsi_rw_10 *cdb; 9053 9054 cdb = (struct scsi_rw_10 *)ctsio->cdb; 9055 if (cdb->byte2 & SRW10_FUA) 9056 flags |= CTL_LLF_FUA; 9057 if (cdb->byte2 & SRW10_DPO) 9058 flags |= CTL_LLF_DPO; 9059 lba = scsi_4btoul(cdb->addr); 9060 num_blocks = scsi_2btoul(cdb->length); 9061 break; 9062 } 9063 case WRITE_VERIFY_10: { 9064 struct scsi_write_verify_10 *cdb; 9065 9066 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 9067 flags |= CTL_LLF_FUA; 9068 if (cdb->byte2 & SWV_DPO) 9069 flags |= CTL_LLF_DPO; 9070 lba = scsi_4btoul(cdb->addr); 9071 num_blocks = scsi_2btoul(cdb->length); 9072 break; 9073 } 9074 case READ_12: 9075 case WRITE_12: { 9076 struct scsi_rw_12 *cdb; 9077 9078 cdb = (struct scsi_rw_12 *)ctsio->cdb; 9079 if (cdb->byte2 & SRW12_FUA) 9080 flags |= CTL_LLF_FUA; 9081 if (cdb->byte2 & SRW12_DPO) 9082 flags |= CTL_LLF_DPO; 9083 lba = scsi_4btoul(cdb->addr); 9084 num_blocks = scsi_4btoul(cdb->length); 9085 break; 9086 } 9087 case WRITE_VERIFY_12: { 9088 struct scsi_write_verify_12 *cdb; 9089 9090 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9091 flags |= CTL_LLF_FUA; 9092 if (cdb->byte2 & SWV_DPO) 9093 flags |= CTL_LLF_DPO; 9094 lba = scsi_4btoul(cdb->addr); 9095 num_blocks = scsi_4btoul(cdb->length); 9096 break; 9097 } 9098 case READ_16: 9099 case WRITE_16: { 9100 struct scsi_rw_16 *cdb; 9101 9102 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9103 if (cdb->byte2 & SRW12_FUA) 9104 flags |= CTL_LLF_FUA; 9105 if (cdb->byte2 & SRW12_DPO) 9106 flags |= CTL_LLF_DPO; 9107 lba = scsi_8btou64(cdb->addr); 9108 num_blocks = scsi_4btoul(cdb->length); 9109 break; 9110 } 9111 case WRITE_VERIFY_16: { 9112 struct scsi_write_verify_16 *cdb; 9113 9114 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9115 flags |= CTL_LLF_FUA; 9116 if (cdb->byte2 & SWV_DPO) 9117 flags |= CTL_LLF_DPO; 9118 lba = scsi_8btou64(cdb->addr); 9119 num_blocks = scsi_4btoul(cdb->length); 9120 break; 9121 } 9122 default: 9123 /* 9124 * We got a command we don't support. This shouldn't 9125 * happen, commands should be filtered out above us. 9126 */ 9127 ctl_set_invalid_opcode(ctsio); 9128 ctl_done((union ctl_io *)ctsio); 9129 9130 return (CTL_RETVAL_COMPLETE); 9131 break; /* NOTREACHED */ 9132 } 9133 9134 /* 9135 * The first check is to make sure we're in bounds, the second 9136 * check is to catch wrap-around problems. If the lba + num blocks 9137 * is less than the lba, then we've wrapped around and the block 9138 * range is invalid anyway. 9139 */ 9140 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9141 || ((lba + num_blocks) < lba)) { 9142 ctl_set_lba_out_of_range(ctsio); 9143 ctl_done((union ctl_io *)ctsio); 9144 return (CTL_RETVAL_COMPLETE); 9145 } 9146 9147 /* 9148 * According to SBC-3, a transfer length of 0 is not an error. 9149 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9150 * translates to 256 blocks for those commands. 9151 */ 9152 if (num_blocks == 0) { 9153 ctl_set_success(ctsio); 9154 ctl_done((union ctl_io *)ctsio); 9155 return (CTL_RETVAL_COMPLETE); 9156 } 9157 9158 /* Set FUA and/or DPO if caches are disabled. */ 9159 if (isread) { 9160 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9161 SCP_RCD) != 0) 9162 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9163 } else { 9164 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9165 SCP_WCE) == 0) 9166 flags |= CTL_LLF_FUA; 9167 } 9168 9169 lbalen = (struct ctl_lba_len_flags *) 9170 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9171 lbalen->lba = lba; 9172 lbalen->len = num_blocks; 9173 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9174 9175 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9176 ctsio->kern_rel_offset = 0; 9177 9178 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9179 9180 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9181 9182 return (retval); 9183 } 9184 9185 static int 9186 ctl_cnw_cont(union ctl_io *io) 9187 { 9188 struct ctl_scsiio *ctsio; 9189 struct ctl_lun *lun; 9190 struct ctl_lba_len_flags *lbalen; 9191 int retval; 9192 9193 ctsio = &io->scsiio; 9194 ctsio->io_hdr.status = CTL_STATUS_NONE; 9195 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9196 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9197 lbalen = (struct ctl_lba_len_flags *) 9198 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9199 lbalen->flags &= ~CTL_LLF_COMPARE; 9200 lbalen->flags |= CTL_LLF_WRITE; 9201 9202 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9203 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9204 return (retval); 9205 } 9206 9207 int 9208 ctl_cnw(struct ctl_scsiio *ctsio) 9209 { 9210 struct ctl_lun *lun; 9211 struct ctl_lba_len_flags *lbalen; 9212 uint64_t lba; 9213 uint32_t num_blocks; 9214 int flags, retval; 9215 9216 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9217 9218 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9219 9220 flags = 0; 9221 retval = CTL_RETVAL_COMPLETE; 9222 9223 switch (ctsio->cdb[0]) { 9224 case COMPARE_AND_WRITE: { 9225 struct scsi_compare_and_write *cdb; 9226 9227 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9228 if (cdb->byte2 & SRW10_FUA) 9229 flags |= CTL_LLF_FUA; 9230 if (cdb->byte2 & SRW10_DPO) 9231 flags |= CTL_LLF_DPO; 9232 lba = scsi_8btou64(cdb->addr); 9233 num_blocks = cdb->length; 9234 break; 9235 } 9236 default: 9237 /* 9238 * We got a command we don't support. This shouldn't 9239 * happen, commands should be filtered out above us. 9240 */ 9241 ctl_set_invalid_opcode(ctsio); 9242 ctl_done((union ctl_io *)ctsio); 9243 9244 return (CTL_RETVAL_COMPLETE); 9245 break; /* NOTREACHED */ 9246 } 9247 9248 /* 9249 * The first check is to make sure we're in bounds, the second 9250 * check is to catch wrap-around problems. If the lba + num blocks 9251 * is less than the lba, then we've wrapped around and the block 9252 * range is invalid anyway. 9253 */ 9254 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9255 || ((lba + num_blocks) < lba)) { 9256 ctl_set_lba_out_of_range(ctsio); 9257 ctl_done((union ctl_io *)ctsio); 9258 return (CTL_RETVAL_COMPLETE); 9259 } 9260 9261 /* 9262 * According to SBC-3, a transfer length of 0 is not an error. 9263 */ 9264 if (num_blocks == 0) { 9265 ctl_set_success(ctsio); 9266 ctl_done((union ctl_io *)ctsio); 9267 return (CTL_RETVAL_COMPLETE); 9268 } 9269 9270 /* Set FUA if write cache is disabled. */ 9271 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9272 SCP_WCE) == 0) 9273 flags |= CTL_LLF_FUA; 9274 9275 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9276 ctsio->kern_rel_offset = 0; 9277 9278 /* 9279 * Set the IO_CONT flag, so that if this I/O gets passed to 9280 * ctl_data_submit_done(), it'll get passed back to 9281 * ctl_ctl_cnw_cont() for further processing. 9282 */ 9283 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9284 ctsio->io_cont = ctl_cnw_cont; 9285 9286 lbalen = (struct ctl_lba_len_flags *) 9287 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9288 lbalen->lba = lba; 9289 lbalen->len = num_blocks; 9290 lbalen->flags = CTL_LLF_COMPARE | flags; 9291 9292 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9293 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9294 return (retval); 9295 } 9296 9297 int 9298 ctl_verify(struct ctl_scsiio *ctsio) 9299 { 9300 struct ctl_lun *lun; 9301 struct ctl_lba_len_flags *lbalen; 9302 uint64_t lba; 9303 uint32_t num_blocks; 9304 int bytchk, flags; 9305 int retval; 9306 9307 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9308 9309 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9310 9311 bytchk = 0; 9312 flags = CTL_LLF_FUA; 9313 retval = CTL_RETVAL_COMPLETE; 9314 9315 switch (ctsio->cdb[0]) { 9316 case VERIFY_10: { 9317 struct scsi_verify_10 *cdb; 9318 9319 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9320 if (cdb->byte2 & SVFY_BYTCHK) 9321 bytchk = 1; 9322 if (cdb->byte2 & SVFY_DPO) 9323 flags |= CTL_LLF_DPO; 9324 lba = scsi_4btoul(cdb->addr); 9325 num_blocks = scsi_2btoul(cdb->length); 9326 break; 9327 } 9328 case VERIFY_12: { 9329 struct scsi_verify_12 *cdb; 9330 9331 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9332 if (cdb->byte2 & SVFY_BYTCHK) 9333 bytchk = 1; 9334 if (cdb->byte2 & SVFY_DPO) 9335 flags |= CTL_LLF_DPO; 9336 lba = scsi_4btoul(cdb->addr); 9337 num_blocks = scsi_4btoul(cdb->length); 9338 break; 9339 } 9340 case VERIFY_16: { 9341 struct scsi_rw_16 *cdb; 9342 9343 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9344 if (cdb->byte2 & SVFY_BYTCHK) 9345 bytchk = 1; 9346 if (cdb->byte2 & SVFY_DPO) 9347 flags |= CTL_LLF_DPO; 9348 lba = scsi_8btou64(cdb->addr); 9349 num_blocks = scsi_4btoul(cdb->length); 9350 break; 9351 } 9352 default: 9353 /* 9354 * We got a command we don't support. This shouldn't 9355 * happen, commands should be filtered out above us. 9356 */ 9357 ctl_set_invalid_opcode(ctsio); 9358 ctl_done((union ctl_io *)ctsio); 9359 return (CTL_RETVAL_COMPLETE); 9360 } 9361 9362 /* 9363 * The first check is to make sure we're in bounds, the second 9364 * check is to catch wrap-around problems. If the lba + num blocks 9365 * is less than the lba, then we've wrapped around and the block 9366 * range is invalid anyway. 9367 */ 9368 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9369 || ((lba + num_blocks) < lba)) { 9370 ctl_set_lba_out_of_range(ctsio); 9371 ctl_done((union ctl_io *)ctsio); 9372 return (CTL_RETVAL_COMPLETE); 9373 } 9374 9375 /* 9376 * According to SBC-3, a transfer length of 0 is not an error. 9377 */ 9378 if (num_blocks == 0) { 9379 ctl_set_success(ctsio); 9380 ctl_done((union ctl_io *)ctsio); 9381 return (CTL_RETVAL_COMPLETE); 9382 } 9383 9384 lbalen = (struct ctl_lba_len_flags *) 9385 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9386 lbalen->lba = lba; 9387 lbalen->len = num_blocks; 9388 if (bytchk) { 9389 lbalen->flags = CTL_LLF_COMPARE | flags; 9390 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9391 } else { 9392 lbalen->flags = CTL_LLF_VERIFY | flags; 9393 ctsio->kern_total_len = 0; 9394 } 9395 ctsio->kern_rel_offset = 0; 9396 9397 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9398 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9399 return (retval); 9400 } 9401 9402 int 9403 ctl_report_luns(struct ctl_scsiio *ctsio) 9404 { 9405 struct scsi_report_luns *cdb; 9406 struct scsi_report_luns_data *lun_data; 9407 struct ctl_lun *lun, *request_lun; 9408 int num_luns, retval; 9409 uint32_t alloc_len, lun_datalen; 9410 int num_filled, well_known; 9411 uint32_t initidx, targ_lun_id, lun_id; 9412 9413 retval = CTL_RETVAL_COMPLETE; 9414 well_known = 0; 9415 9416 cdb = (struct scsi_report_luns *)ctsio->cdb; 9417 9418 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9419 9420 mtx_lock(&control_softc->ctl_lock); 9421 num_luns = control_softc->num_luns; 9422 mtx_unlock(&control_softc->ctl_lock); 9423 9424 switch (cdb->select_report) { 9425 case RPL_REPORT_DEFAULT: 9426 case RPL_REPORT_ALL: 9427 break; 9428 case RPL_REPORT_WELLKNOWN: 9429 well_known = 1; 9430 num_luns = 0; 9431 break; 9432 default: 9433 ctl_set_invalid_field(ctsio, 9434 /*sks_valid*/ 1, 9435 /*command*/ 1, 9436 /*field*/ 2, 9437 /*bit_valid*/ 0, 9438 /*bit*/ 0); 9439 ctl_done((union ctl_io *)ctsio); 9440 return (retval); 9441 break; /* NOTREACHED */ 9442 } 9443 9444 alloc_len = scsi_4btoul(cdb->length); 9445 /* 9446 * The initiator has to allocate at least 16 bytes for this request, 9447 * so he can at least get the header and the first LUN. Otherwise 9448 * we reject the request (per SPC-3 rev 14, section 6.21). 9449 */ 9450 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9451 sizeof(struct scsi_report_luns_lundata))) { 9452 ctl_set_invalid_field(ctsio, 9453 /*sks_valid*/ 1, 9454 /*command*/ 1, 9455 /*field*/ 6, 9456 /*bit_valid*/ 0, 9457 /*bit*/ 0); 9458 ctl_done((union ctl_io *)ctsio); 9459 return (retval); 9460 } 9461 9462 request_lun = (struct ctl_lun *) 9463 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9464 9465 lun_datalen = sizeof(*lun_data) + 9466 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9467 9468 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9469 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9470 ctsio->kern_sg_entries = 0; 9471 9472 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9473 9474 mtx_lock(&control_softc->ctl_lock); 9475 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9476 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9477 if (lun_id >= CTL_MAX_LUNS) 9478 continue; 9479 lun = control_softc->ctl_luns[lun_id]; 9480 if (lun == NULL) 9481 continue; 9482 9483 if (targ_lun_id <= 0xff) { 9484 /* 9485 * Peripheral addressing method, bus number 0. 9486 */ 9487 lun_data->luns[num_filled].lundata[0] = 9488 RPL_LUNDATA_ATYP_PERIPH; 9489 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9490 num_filled++; 9491 } else if (targ_lun_id <= 0x3fff) { 9492 /* 9493 * Flat addressing method. 9494 */ 9495 lun_data->luns[num_filled].lundata[0] = 9496 RPL_LUNDATA_ATYP_FLAT | 9497 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9498 #ifdef OLDCTLHEADERS 9499 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9500 (targ_lun_id & SRLD_BUS_LUN_MASK); 9501 #endif 9502 lun_data->luns[num_filled].lundata[1] = 9503 #ifdef OLDCTLHEADERS 9504 targ_lun_id >> SRLD_BUS_LUN_BITS; 9505 #endif 9506 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9507 num_filled++; 9508 } else { 9509 printf("ctl_report_luns: bogus LUN number %jd, " 9510 "skipping\n", (intmax_t)targ_lun_id); 9511 } 9512 /* 9513 * According to SPC-3, rev 14 section 6.21: 9514 * 9515 * "The execution of a REPORT LUNS command to any valid and 9516 * installed logical unit shall clear the REPORTED LUNS DATA 9517 * HAS CHANGED unit attention condition for all logical 9518 * units of that target with respect to the requesting 9519 * initiator. A valid and installed logical unit is one 9520 * having a PERIPHERAL QUALIFIER of 000b in the standard 9521 * INQUIRY data (see 6.4.2)." 9522 * 9523 * If request_lun is NULL, the LUN this report luns command 9524 * was issued to is either disabled or doesn't exist. In that 9525 * case, we shouldn't clear any pending lun change unit 9526 * attention. 9527 */ 9528 if (request_lun != NULL) { 9529 mtx_lock(&lun->lun_lock); 9530 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9531 mtx_unlock(&lun->lun_lock); 9532 } 9533 } 9534 mtx_unlock(&control_softc->ctl_lock); 9535 9536 /* 9537 * It's quite possible that we've returned fewer LUNs than we allocated 9538 * space for. Trim it. 9539 */ 9540 lun_datalen = sizeof(*lun_data) + 9541 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9542 9543 if (lun_datalen < alloc_len) { 9544 ctsio->residual = alloc_len - lun_datalen; 9545 ctsio->kern_data_len = lun_datalen; 9546 ctsio->kern_total_len = lun_datalen; 9547 } else { 9548 ctsio->residual = 0; 9549 ctsio->kern_data_len = alloc_len; 9550 ctsio->kern_total_len = alloc_len; 9551 } 9552 ctsio->kern_data_resid = 0; 9553 ctsio->kern_rel_offset = 0; 9554 ctsio->kern_sg_entries = 0; 9555 9556 /* 9557 * We set this to the actual data length, regardless of how much 9558 * space we actually have to return results. If the user looks at 9559 * this value, he'll know whether or not he allocated enough space 9560 * and reissue the command if necessary. We don't support well 9561 * known logical units, so if the user asks for that, return none. 9562 */ 9563 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9564 9565 /* 9566 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9567 * this request. 9568 */ 9569 ctsio->scsi_status = SCSI_STATUS_OK; 9570 9571 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9572 ctsio->be_move_done = ctl_config_move_done; 9573 ctl_datamove((union ctl_io *)ctsio); 9574 9575 return (retval); 9576 } 9577 9578 int 9579 ctl_request_sense(struct ctl_scsiio *ctsio) 9580 { 9581 struct scsi_request_sense *cdb; 9582 struct scsi_sense_data *sense_ptr; 9583 struct ctl_lun *lun; 9584 uint32_t initidx; 9585 int have_error; 9586 scsi_sense_data_type sense_format; 9587 9588 cdb = (struct scsi_request_sense *)ctsio->cdb; 9589 9590 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9591 9592 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9593 9594 /* 9595 * Determine which sense format the user wants. 9596 */ 9597 if (cdb->byte2 & SRS_DESC) 9598 sense_format = SSD_TYPE_DESC; 9599 else 9600 sense_format = SSD_TYPE_FIXED; 9601 9602 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9603 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9604 ctsio->kern_sg_entries = 0; 9605 9606 /* 9607 * struct scsi_sense_data, which is currently set to 256 bytes, is 9608 * larger than the largest allowed value for the length field in the 9609 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9610 */ 9611 ctsio->residual = 0; 9612 ctsio->kern_data_len = cdb->length; 9613 ctsio->kern_total_len = cdb->length; 9614 9615 ctsio->kern_data_resid = 0; 9616 ctsio->kern_rel_offset = 0; 9617 ctsio->kern_sg_entries = 0; 9618 9619 /* 9620 * If we don't have a LUN, we don't have any pending sense. 9621 */ 9622 if (lun == NULL) 9623 goto no_sense; 9624 9625 have_error = 0; 9626 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9627 /* 9628 * Check for pending sense, and then for pending unit attentions. 9629 * Pending sense gets returned first, then pending unit attentions. 9630 */ 9631 mtx_lock(&lun->lun_lock); 9632 #ifdef CTL_WITH_CA 9633 if (ctl_is_set(lun->have_ca, initidx)) { 9634 scsi_sense_data_type stored_format; 9635 9636 /* 9637 * Check to see which sense format was used for the stored 9638 * sense data. 9639 */ 9640 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9641 9642 /* 9643 * If the user requested a different sense format than the 9644 * one we stored, then we need to convert it to the other 9645 * format. If we're going from descriptor to fixed format 9646 * sense data, we may lose things in translation, depending 9647 * on what options were used. 9648 * 9649 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9650 * for some reason we'll just copy it out as-is. 9651 */ 9652 if ((stored_format == SSD_TYPE_FIXED) 9653 && (sense_format == SSD_TYPE_DESC)) 9654 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9655 &lun->pending_sense[initidx], 9656 (struct scsi_sense_data_desc *)sense_ptr); 9657 else if ((stored_format == SSD_TYPE_DESC) 9658 && (sense_format == SSD_TYPE_FIXED)) 9659 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9660 &lun->pending_sense[initidx], 9661 (struct scsi_sense_data_fixed *)sense_ptr); 9662 else 9663 memcpy(sense_ptr, &lun->pending_sense[initidx], 9664 ctl_min(sizeof(*sense_ptr), 9665 sizeof(lun->pending_sense[initidx]))); 9666 9667 ctl_clear_mask(lun->have_ca, initidx); 9668 have_error = 1; 9669 } else 9670 #endif 9671 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9672 ctl_ua_type ua_type; 9673 9674 ua_type = ctl_build_ua(lun->pending_ua[initidx], 9675 sense_ptr, sense_format); 9676 if (ua_type != CTL_UA_NONE) { 9677 have_error = 1; 9678 /* We're reporting this UA, so clear it */ 9679 lun->pending_ua[initidx] &= ~ua_type; 9680 } 9681 } 9682 mtx_unlock(&lun->lun_lock); 9683 9684 /* 9685 * We already have a pending error, return it. 9686 */ 9687 if (have_error != 0) { 9688 /* 9689 * We report the SCSI status as OK, since the status of the 9690 * request sense command itself is OK. 9691 */ 9692 ctsio->scsi_status = SCSI_STATUS_OK; 9693 9694 /* 9695 * We report 0 for the sense length, because we aren't doing 9696 * autosense in this case. We're reporting sense as 9697 * parameter data. 9698 */ 9699 ctsio->sense_len = 0; 9700 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9701 ctsio->be_move_done = ctl_config_move_done; 9702 ctl_datamove((union ctl_io *)ctsio); 9703 9704 return (CTL_RETVAL_COMPLETE); 9705 } 9706 9707 no_sense: 9708 9709 /* 9710 * No sense information to report, so we report that everything is 9711 * okay. 9712 */ 9713 ctl_set_sense_data(sense_ptr, 9714 lun, 9715 sense_format, 9716 /*current_error*/ 1, 9717 /*sense_key*/ SSD_KEY_NO_SENSE, 9718 /*asc*/ 0x00, 9719 /*ascq*/ 0x00, 9720 SSD_ELEM_NONE); 9721 9722 ctsio->scsi_status = SCSI_STATUS_OK; 9723 9724 /* 9725 * We report 0 for the sense length, because we aren't doing 9726 * autosense in this case. We're reporting sense as parameter data. 9727 */ 9728 ctsio->sense_len = 0; 9729 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9730 ctsio->be_move_done = ctl_config_move_done; 9731 ctl_datamove((union ctl_io *)ctsio); 9732 9733 return (CTL_RETVAL_COMPLETE); 9734 } 9735 9736 int 9737 ctl_tur(struct ctl_scsiio *ctsio) 9738 { 9739 struct ctl_lun *lun; 9740 9741 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9742 9743 CTL_DEBUG_PRINT(("ctl_tur\n")); 9744 9745 if (lun == NULL) 9746 return (EINVAL); 9747 9748 ctsio->scsi_status = SCSI_STATUS_OK; 9749 ctsio->io_hdr.status = CTL_SUCCESS; 9750 9751 ctl_done((union ctl_io *)ctsio); 9752 9753 return (CTL_RETVAL_COMPLETE); 9754 } 9755 9756 #ifdef notyet 9757 static int 9758 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9759 { 9760 9761 } 9762 #endif 9763 9764 static int 9765 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9766 { 9767 struct scsi_vpd_supported_pages *pages; 9768 int sup_page_size; 9769 struct ctl_lun *lun; 9770 9771 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9772 9773 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9774 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9775 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9776 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9777 ctsio->kern_sg_entries = 0; 9778 9779 if (sup_page_size < alloc_len) { 9780 ctsio->residual = alloc_len - sup_page_size; 9781 ctsio->kern_data_len = sup_page_size; 9782 ctsio->kern_total_len = sup_page_size; 9783 } else { 9784 ctsio->residual = 0; 9785 ctsio->kern_data_len = alloc_len; 9786 ctsio->kern_total_len = alloc_len; 9787 } 9788 ctsio->kern_data_resid = 0; 9789 ctsio->kern_rel_offset = 0; 9790 ctsio->kern_sg_entries = 0; 9791 9792 /* 9793 * The control device is always connected. The disk device, on the 9794 * other hand, may not be online all the time. Need to change this 9795 * to figure out whether the disk device is actually online or not. 9796 */ 9797 if (lun != NULL) 9798 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9799 lun->be_lun->lun_type; 9800 else 9801 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9802 9803 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9804 /* Supported VPD pages */ 9805 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9806 /* Serial Number */ 9807 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9808 /* Device Identification */ 9809 pages->page_list[2] = SVPD_DEVICE_ID; 9810 /* Extended INQUIRY Data */ 9811 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9812 /* Mode Page Policy */ 9813 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9814 /* SCSI Ports */ 9815 pages->page_list[5] = SVPD_SCSI_PORTS; 9816 /* Third-party Copy */ 9817 pages->page_list[6] = SVPD_SCSI_TPC; 9818 /* Block limits */ 9819 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9820 /* Block Device Characteristics */ 9821 pages->page_list[8] = SVPD_BDC; 9822 /* Logical Block Provisioning */ 9823 pages->page_list[9] = SVPD_LBP; 9824 9825 ctsio->scsi_status = SCSI_STATUS_OK; 9826 9827 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9828 ctsio->be_move_done = ctl_config_move_done; 9829 ctl_datamove((union ctl_io *)ctsio); 9830 9831 return (CTL_RETVAL_COMPLETE); 9832 } 9833 9834 static int 9835 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9836 { 9837 struct scsi_vpd_unit_serial_number *sn_ptr; 9838 struct ctl_lun *lun; 9839 9840 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9841 9842 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9843 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9844 ctsio->kern_sg_entries = 0; 9845 9846 if (sizeof(*sn_ptr) < alloc_len) { 9847 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9848 ctsio->kern_data_len = sizeof(*sn_ptr); 9849 ctsio->kern_total_len = sizeof(*sn_ptr); 9850 } else { 9851 ctsio->residual = 0; 9852 ctsio->kern_data_len = alloc_len; 9853 ctsio->kern_total_len = alloc_len; 9854 } 9855 ctsio->kern_data_resid = 0; 9856 ctsio->kern_rel_offset = 0; 9857 ctsio->kern_sg_entries = 0; 9858 9859 /* 9860 * The control device is always connected. The disk device, on the 9861 * other hand, may not be online all the time. Need to change this 9862 * to figure out whether the disk device is actually online or not. 9863 */ 9864 if (lun != NULL) 9865 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9866 lun->be_lun->lun_type; 9867 else 9868 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9869 9870 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9871 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9872 /* 9873 * If we don't have a LUN, we just leave the serial number as 9874 * all spaces. 9875 */ 9876 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9877 if (lun != NULL) { 9878 strncpy((char *)sn_ptr->serial_num, 9879 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9880 } 9881 ctsio->scsi_status = SCSI_STATUS_OK; 9882 9883 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9884 ctsio->be_move_done = ctl_config_move_done; 9885 ctl_datamove((union ctl_io *)ctsio); 9886 9887 return (CTL_RETVAL_COMPLETE); 9888 } 9889 9890 9891 static int 9892 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9893 { 9894 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9895 struct ctl_lun *lun; 9896 int data_len; 9897 9898 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9899 9900 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9901 sizeof(struct scsi_vpd_mode_page_policy_descr); 9902 9903 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9904 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9905 ctsio->kern_sg_entries = 0; 9906 9907 if (data_len < alloc_len) { 9908 ctsio->residual = alloc_len - data_len; 9909 ctsio->kern_data_len = data_len; 9910 ctsio->kern_total_len = data_len; 9911 } else { 9912 ctsio->residual = 0; 9913 ctsio->kern_data_len = alloc_len; 9914 ctsio->kern_total_len = alloc_len; 9915 } 9916 ctsio->kern_data_resid = 0; 9917 ctsio->kern_rel_offset = 0; 9918 ctsio->kern_sg_entries = 0; 9919 9920 /* 9921 * The control device is always connected. The disk device, on the 9922 * other hand, may not be online all the time. 9923 */ 9924 if (lun != NULL) 9925 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9926 lun->be_lun->lun_type; 9927 else 9928 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9929 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9930 eid_ptr->page_length = data_len - 4; 9931 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9932 eid_ptr->flags3 = SVPD_EID_V_SUP; 9933 9934 ctsio->scsi_status = SCSI_STATUS_OK; 9935 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9936 ctsio->be_move_done = ctl_config_move_done; 9937 ctl_datamove((union ctl_io *)ctsio); 9938 9939 return (CTL_RETVAL_COMPLETE); 9940 } 9941 9942 static int 9943 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9944 { 9945 struct scsi_vpd_mode_page_policy *mpp_ptr; 9946 struct ctl_lun *lun; 9947 int data_len; 9948 9949 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9950 9951 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9952 sizeof(struct scsi_vpd_mode_page_policy_descr); 9953 9954 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9955 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9956 ctsio->kern_sg_entries = 0; 9957 9958 if (data_len < alloc_len) { 9959 ctsio->residual = alloc_len - data_len; 9960 ctsio->kern_data_len = data_len; 9961 ctsio->kern_total_len = data_len; 9962 } else { 9963 ctsio->residual = 0; 9964 ctsio->kern_data_len = alloc_len; 9965 ctsio->kern_total_len = alloc_len; 9966 } 9967 ctsio->kern_data_resid = 0; 9968 ctsio->kern_rel_offset = 0; 9969 ctsio->kern_sg_entries = 0; 9970 9971 /* 9972 * The control device is always connected. The disk device, on the 9973 * other hand, may not be online all the time. 9974 */ 9975 if (lun != NULL) 9976 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9977 lun->be_lun->lun_type; 9978 else 9979 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9980 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9981 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9982 mpp_ptr->descr[0].page_code = 0x3f; 9983 mpp_ptr->descr[0].subpage_code = 0xff; 9984 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9985 9986 ctsio->scsi_status = SCSI_STATUS_OK; 9987 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9988 ctsio->be_move_done = ctl_config_move_done; 9989 ctl_datamove((union ctl_io *)ctsio); 9990 9991 return (CTL_RETVAL_COMPLETE); 9992 } 9993 9994 static int 9995 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9996 { 9997 struct scsi_vpd_device_id *devid_ptr; 9998 struct scsi_vpd_id_descriptor *desc; 9999 struct ctl_softc *ctl_softc; 10000 struct ctl_lun *lun; 10001 struct ctl_port *port; 10002 int data_len; 10003 uint8_t proto; 10004 10005 ctl_softc = control_softc; 10006 10007 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 10008 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10009 10010 data_len = sizeof(struct scsi_vpd_device_id) + 10011 sizeof(struct scsi_vpd_id_descriptor) + 10012 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 10013 sizeof(struct scsi_vpd_id_descriptor) + 10014 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 10015 if (lun && lun->lun_devid) 10016 data_len += lun->lun_devid->len; 10017 if (port->port_devid) 10018 data_len += port->port_devid->len; 10019 if (port->target_devid) 10020 data_len += port->target_devid->len; 10021 10022 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10023 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 10024 ctsio->kern_sg_entries = 0; 10025 10026 if (data_len < alloc_len) { 10027 ctsio->residual = alloc_len - data_len; 10028 ctsio->kern_data_len = data_len; 10029 ctsio->kern_total_len = data_len; 10030 } else { 10031 ctsio->residual = 0; 10032 ctsio->kern_data_len = alloc_len; 10033 ctsio->kern_total_len = alloc_len; 10034 } 10035 ctsio->kern_data_resid = 0; 10036 ctsio->kern_rel_offset = 0; 10037 ctsio->kern_sg_entries = 0; 10038 10039 /* 10040 * The control device is always connected. The disk device, on the 10041 * other hand, may not be online all the time. 10042 */ 10043 if (lun != NULL) 10044 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10045 lun->be_lun->lun_type; 10046 else 10047 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10048 devid_ptr->page_code = SVPD_DEVICE_ID; 10049 scsi_ulto2b(data_len - 4, devid_ptr->length); 10050 10051 if (port->port_type == CTL_PORT_FC) 10052 proto = SCSI_PROTO_FC << 4; 10053 else if (port->port_type == CTL_PORT_ISCSI) 10054 proto = SCSI_PROTO_ISCSI << 4; 10055 else 10056 proto = SCSI_PROTO_SPI << 4; 10057 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 10058 10059 /* 10060 * We're using a LUN association here. i.e., this device ID is a 10061 * per-LUN identifier. 10062 */ 10063 if (lun && lun->lun_devid) { 10064 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 10065 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10066 lun->lun_devid->len); 10067 } 10068 10069 /* 10070 * This is for the WWPN which is a port association. 10071 */ 10072 if (port->port_devid) { 10073 memcpy(desc, port->port_devid->data, port->port_devid->len); 10074 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10075 port->port_devid->len); 10076 } 10077 10078 /* 10079 * This is for the Relative Target Port(type 4h) identifier 10080 */ 10081 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10082 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10083 SVPD_ID_TYPE_RELTARG; 10084 desc->length = 4; 10085 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 10086 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10087 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 10088 10089 /* 10090 * This is for the Target Port Group(type 5h) identifier 10091 */ 10092 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10093 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10094 SVPD_ID_TYPE_TPORTGRP; 10095 desc->length = 4; 10096 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10097 &desc->identifier[2]); 10098 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10099 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10100 10101 /* 10102 * This is for the Target identifier 10103 */ 10104 if (port->target_devid) { 10105 memcpy(desc, port->target_devid->data, port->target_devid->len); 10106 } 10107 10108 ctsio->scsi_status = SCSI_STATUS_OK; 10109 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10110 ctsio->be_move_done = ctl_config_move_done; 10111 ctl_datamove((union ctl_io *)ctsio); 10112 10113 return (CTL_RETVAL_COMPLETE); 10114 } 10115 10116 static int 10117 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10118 { 10119 struct ctl_softc *softc = control_softc; 10120 struct scsi_vpd_scsi_ports *sp; 10121 struct scsi_vpd_port_designation *pd; 10122 struct scsi_vpd_port_designation_cont *pdc; 10123 struct ctl_lun *lun; 10124 struct ctl_port *port; 10125 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10126 int num_target_port_groups, single; 10127 10128 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10129 10130 single = ctl_is_single; 10131 if (single) 10132 num_target_port_groups = 1; 10133 else 10134 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10135 num_target_ports = 0; 10136 iid_len = 0; 10137 id_len = 0; 10138 mtx_lock(&softc->ctl_lock); 10139 STAILQ_FOREACH(port, &softc->port_list, links) { 10140 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10141 continue; 10142 if (lun != NULL && 10143 ctl_map_lun_back(port->targ_port, lun->lun) >= 10144 CTL_MAX_LUNS) 10145 continue; 10146 num_target_ports++; 10147 if (port->init_devid) 10148 iid_len += port->init_devid->len; 10149 if (port->port_devid) 10150 id_len += port->port_devid->len; 10151 } 10152 mtx_unlock(&softc->ctl_lock); 10153 10154 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10155 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10156 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10157 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10158 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10159 ctsio->kern_sg_entries = 0; 10160 10161 if (data_len < alloc_len) { 10162 ctsio->residual = alloc_len - data_len; 10163 ctsio->kern_data_len = data_len; 10164 ctsio->kern_total_len = data_len; 10165 } else { 10166 ctsio->residual = 0; 10167 ctsio->kern_data_len = alloc_len; 10168 ctsio->kern_total_len = alloc_len; 10169 } 10170 ctsio->kern_data_resid = 0; 10171 ctsio->kern_rel_offset = 0; 10172 ctsio->kern_sg_entries = 0; 10173 10174 /* 10175 * The control device is always connected. The disk device, on the 10176 * other hand, may not be online all the time. Need to change this 10177 * to figure out whether the disk device is actually online or not. 10178 */ 10179 if (lun != NULL) 10180 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10181 lun->be_lun->lun_type; 10182 else 10183 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10184 10185 sp->page_code = SVPD_SCSI_PORTS; 10186 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10187 sp->page_length); 10188 pd = &sp->design[0]; 10189 10190 mtx_lock(&softc->ctl_lock); 10191 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10192 pg = 0; 10193 else 10194 pg = 1; 10195 for (g = 0; g < num_target_port_groups; g++) { 10196 STAILQ_FOREACH(port, &softc->port_list, links) { 10197 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10198 continue; 10199 if (lun != NULL && 10200 ctl_map_lun_back(port->targ_port, lun->lun) >= 10201 CTL_MAX_LUNS) 10202 continue; 10203 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10204 scsi_ulto2b(p, pd->relative_port_id); 10205 if (port->init_devid && g == pg) { 10206 iid_len = port->init_devid->len; 10207 memcpy(pd->initiator_transportid, 10208 port->init_devid->data, port->init_devid->len); 10209 } else 10210 iid_len = 0; 10211 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10212 pdc = (struct scsi_vpd_port_designation_cont *) 10213 (&pd->initiator_transportid[iid_len]); 10214 if (port->port_devid && g == pg) { 10215 id_len = port->port_devid->len; 10216 memcpy(pdc->target_port_descriptors, 10217 port->port_devid->data, port->port_devid->len); 10218 } else 10219 id_len = 0; 10220 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10221 pd = (struct scsi_vpd_port_designation *) 10222 ((uint8_t *)pdc->target_port_descriptors + id_len); 10223 } 10224 } 10225 mtx_unlock(&softc->ctl_lock); 10226 10227 ctsio->scsi_status = SCSI_STATUS_OK; 10228 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10229 ctsio->be_move_done = ctl_config_move_done; 10230 ctl_datamove((union ctl_io *)ctsio); 10231 10232 return (CTL_RETVAL_COMPLETE); 10233 } 10234 10235 static int 10236 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10237 { 10238 struct scsi_vpd_block_limits *bl_ptr; 10239 struct ctl_lun *lun; 10240 int bs; 10241 10242 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10243 10244 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10245 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10246 ctsio->kern_sg_entries = 0; 10247 10248 if (sizeof(*bl_ptr) < alloc_len) { 10249 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10250 ctsio->kern_data_len = sizeof(*bl_ptr); 10251 ctsio->kern_total_len = sizeof(*bl_ptr); 10252 } else { 10253 ctsio->residual = 0; 10254 ctsio->kern_data_len = alloc_len; 10255 ctsio->kern_total_len = alloc_len; 10256 } 10257 ctsio->kern_data_resid = 0; 10258 ctsio->kern_rel_offset = 0; 10259 ctsio->kern_sg_entries = 0; 10260 10261 /* 10262 * The control device is always connected. The disk device, on the 10263 * other hand, may not be online all the time. Need to change this 10264 * to figure out whether the disk device is actually online or not. 10265 */ 10266 if (lun != NULL) 10267 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10268 lun->be_lun->lun_type; 10269 else 10270 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10271 10272 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10273 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 10274 bl_ptr->max_cmp_write_len = 0xff; 10275 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10276 if (lun != NULL) { 10277 bs = lun->be_lun->blocksize; 10278 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10279 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10280 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10281 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10282 if (lun->be_lun->pblockexp != 0) { 10283 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10284 bl_ptr->opt_unmap_grain); 10285 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10286 bl_ptr->unmap_grain_align); 10287 } 10288 } 10289 } 10290 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10291 10292 ctsio->scsi_status = SCSI_STATUS_OK; 10293 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10294 ctsio->be_move_done = ctl_config_move_done; 10295 ctl_datamove((union ctl_io *)ctsio); 10296 10297 return (CTL_RETVAL_COMPLETE); 10298 } 10299 10300 static int 10301 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10302 { 10303 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10304 struct ctl_lun *lun; 10305 10306 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10307 10308 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10309 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10310 ctsio->kern_sg_entries = 0; 10311 10312 if (sizeof(*bdc_ptr) < alloc_len) { 10313 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10314 ctsio->kern_data_len = sizeof(*bdc_ptr); 10315 ctsio->kern_total_len = sizeof(*bdc_ptr); 10316 } else { 10317 ctsio->residual = 0; 10318 ctsio->kern_data_len = alloc_len; 10319 ctsio->kern_total_len = alloc_len; 10320 } 10321 ctsio->kern_data_resid = 0; 10322 ctsio->kern_rel_offset = 0; 10323 ctsio->kern_sg_entries = 0; 10324 10325 /* 10326 * The control device is always connected. The disk device, on the 10327 * other hand, may not be online all the time. Need to change this 10328 * to figure out whether the disk device is actually online or not. 10329 */ 10330 if (lun != NULL) 10331 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10332 lun->be_lun->lun_type; 10333 else 10334 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10335 bdc_ptr->page_code = SVPD_BDC; 10336 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10337 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate); 10338 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10339 10340 ctsio->scsi_status = SCSI_STATUS_OK; 10341 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10342 ctsio->be_move_done = ctl_config_move_done; 10343 ctl_datamove((union ctl_io *)ctsio); 10344 10345 return (CTL_RETVAL_COMPLETE); 10346 } 10347 10348 static int 10349 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10350 { 10351 struct scsi_vpd_logical_block_prov *lbp_ptr; 10352 struct ctl_lun *lun; 10353 10354 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10355 10356 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10357 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10358 ctsio->kern_sg_entries = 0; 10359 10360 if (sizeof(*lbp_ptr) < alloc_len) { 10361 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10362 ctsio->kern_data_len = sizeof(*lbp_ptr); 10363 ctsio->kern_total_len = sizeof(*lbp_ptr); 10364 } else { 10365 ctsio->residual = 0; 10366 ctsio->kern_data_len = alloc_len; 10367 ctsio->kern_total_len = alloc_len; 10368 } 10369 ctsio->kern_data_resid = 0; 10370 ctsio->kern_rel_offset = 0; 10371 ctsio->kern_sg_entries = 0; 10372 10373 /* 10374 * The control device is always connected. The disk device, on the 10375 * other hand, may not be online all the time. Need to change this 10376 * to figure out whether the disk device is actually online or not. 10377 */ 10378 if (lun != NULL) 10379 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10380 lun->be_lun->lun_type; 10381 else 10382 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10383 10384 lbp_ptr->page_code = SVPD_LBP; 10385 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10386 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10387 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10388 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10389 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10390 } 10391 10392 ctsio->scsi_status = SCSI_STATUS_OK; 10393 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10394 ctsio->be_move_done = ctl_config_move_done; 10395 ctl_datamove((union ctl_io *)ctsio); 10396 10397 return (CTL_RETVAL_COMPLETE); 10398 } 10399 10400 static int 10401 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10402 { 10403 struct scsi_inquiry *cdb; 10404 struct ctl_lun *lun; 10405 int alloc_len, retval; 10406 10407 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10408 cdb = (struct scsi_inquiry *)ctsio->cdb; 10409 10410 retval = CTL_RETVAL_COMPLETE; 10411 10412 alloc_len = scsi_2btoul(cdb->length); 10413 10414 switch (cdb->page_code) { 10415 case SVPD_SUPPORTED_PAGES: 10416 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10417 break; 10418 case SVPD_UNIT_SERIAL_NUMBER: 10419 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10420 break; 10421 case SVPD_DEVICE_ID: 10422 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10423 break; 10424 case SVPD_EXTENDED_INQUIRY_DATA: 10425 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10426 break; 10427 case SVPD_MODE_PAGE_POLICY: 10428 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10429 break; 10430 case SVPD_SCSI_PORTS: 10431 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10432 break; 10433 case SVPD_SCSI_TPC: 10434 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10435 break; 10436 case SVPD_BLOCK_LIMITS: 10437 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10438 break; 10439 case SVPD_BDC: 10440 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10441 break; 10442 case SVPD_LBP: 10443 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10444 break; 10445 default: 10446 ctl_set_invalid_field(ctsio, 10447 /*sks_valid*/ 1, 10448 /*command*/ 1, 10449 /*field*/ 2, 10450 /*bit_valid*/ 0, 10451 /*bit*/ 0); 10452 ctl_done((union ctl_io *)ctsio); 10453 retval = CTL_RETVAL_COMPLETE; 10454 break; 10455 } 10456 10457 return (retval); 10458 } 10459 10460 static int 10461 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10462 { 10463 struct scsi_inquiry_data *inq_ptr; 10464 struct scsi_inquiry *cdb; 10465 struct ctl_softc *ctl_softc; 10466 struct ctl_lun *lun; 10467 char *val; 10468 uint32_t alloc_len; 10469 ctl_port_type port_type; 10470 10471 ctl_softc = control_softc; 10472 10473 /* 10474 * Figure out whether we're talking to a Fibre Channel port or not. 10475 * We treat the ioctl front end, and any SCSI adapters, as packetized 10476 * SCSI front ends. 10477 */ 10478 port_type = ctl_softc->ctl_ports[ 10479 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10480 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10481 port_type = CTL_PORT_SCSI; 10482 10483 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10484 cdb = (struct scsi_inquiry *)ctsio->cdb; 10485 alloc_len = scsi_2btoul(cdb->length); 10486 10487 /* 10488 * We malloc the full inquiry data size here and fill it 10489 * in. If the user only asks for less, we'll give him 10490 * that much. 10491 */ 10492 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 10493 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10494 ctsio->kern_sg_entries = 0; 10495 ctsio->kern_data_resid = 0; 10496 ctsio->kern_rel_offset = 0; 10497 10498 if (sizeof(*inq_ptr) < alloc_len) { 10499 ctsio->residual = alloc_len - sizeof(*inq_ptr); 10500 ctsio->kern_data_len = sizeof(*inq_ptr); 10501 ctsio->kern_total_len = sizeof(*inq_ptr); 10502 } else { 10503 ctsio->residual = 0; 10504 ctsio->kern_data_len = alloc_len; 10505 ctsio->kern_total_len = alloc_len; 10506 } 10507 10508 /* 10509 * If we have a LUN configured, report it as connected. Otherwise, 10510 * report that it is offline or no device is supported, depending 10511 * on the value of inquiry_pq_no_lun. 10512 * 10513 * According to the spec (SPC-4 r34), the peripheral qualifier 10514 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10515 * 10516 * "A peripheral device having the specified peripheral device type 10517 * is not connected to this logical unit. However, the device 10518 * server is capable of supporting the specified peripheral device 10519 * type on this logical unit." 10520 * 10521 * According to the same spec, the peripheral qualifier 10522 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10523 * 10524 * "The device server is not capable of supporting a peripheral 10525 * device on this logical unit. For this peripheral qualifier the 10526 * peripheral device type shall be set to 1Fh. All other peripheral 10527 * device type values are reserved for this peripheral qualifier." 10528 * 10529 * Given the text, it would seem that we probably want to report that 10530 * the LUN is offline here. There is no LUN connected, but we can 10531 * support a LUN at the given LUN number. 10532 * 10533 * In the real world, though, it sounds like things are a little 10534 * different: 10535 * 10536 * - Linux, when presented with a LUN with the offline peripheral 10537 * qualifier, will create an sg driver instance for it. So when 10538 * you attach it to CTL, you wind up with a ton of sg driver 10539 * instances. (One for every LUN that Linux bothered to probe.) 10540 * Linux does this despite the fact that it issues a REPORT LUNs 10541 * to LUN 0 to get the inventory of supported LUNs. 10542 * 10543 * - There is other anecdotal evidence (from Emulex folks) about 10544 * arrays that use the offline peripheral qualifier for LUNs that 10545 * are on the "passive" path in an active/passive array. 10546 * 10547 * So the solution is provide a hopefully reasonable default 10548 * (return bad/no LUN) and allow the user to change the behavior 10549 * with a tunable/sysctl variable. 10550 */ 10551 if (lun != NULL) 10552 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10553 lun->be_lun->lun_type; 10554 else if (ctl_softc->inquiry_pq_no_lun == 0) 10555 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10556 else 10557 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10558 10559 /* RMB in byte 2 is 0 */ 10560 inq_ptr->version = SCSI_REV_SPC4; 10561 10562 /* 10563 * According to SAM-3, even if a device only supports a single 10564 * level of LUN addressing, it should still set the HISUP bit: 10565 * 10566 * 4.9.1 Logical unit numbers overview 10567 * 10568 * All logical unit number formats described in this standard are 10569 * hierarchical in structure even when only a single level in that 10570 * hierarchy is used. The HISUP bit shall be set to one in the 10571 * standard INQUIRY data (see SPC-2) when any logical unit number 10572 * format described in this standard is used. Non-hierarchical 10573 * formats are outside the scope of this standard. 10574 * 10575 * Therefore we set the HiSup bit here. 10576 * 10577 * The reponse format is 2, per SPC-3. 10578 */ 10579 inq_ptr->response_format = SID_HiSup | 2; 10580 10581 inq_ptr->additional_length = 10582 offsetof(struct scsi_inquiry_data, vendor_specific1) - 10583 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10584 CTL_DEBUG_PRINT(("additional_length = %d\n", 10585 inq_ptr->additional_length)); 10586 10587 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10588 /* 16 bit addressing */ 10589 if (port_type == CTL_PORT_SCSI) 10590 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10591 /* XXX set the SID_MultiP bit here if we're actually going to 10592 respond on multiple ports */ 10593 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10594 10595 /* 16 bit data bus, synchronous transfers */ 10596 if (port_type == CTL_PORT_SCSI) 10597 inq_ptr->flags = SID_WBus16 | SID_Sync; 10598 /* 10599 * XXX KDM do we want to support tagged queueing on the control 10600 * device at all? 10601 */ 10602 if ((lun == NULL) 10603 || (lun->be_lun->lun_type != T_PROCESSOR)) 10604 inq_ptr->flags |= SID_CmdQue; 10605 /* 10606 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10607 * We have 8 bytes for the vendor name, and 16 bytes for the device 10608 * name and 4 bytes for the revision. 10609 */ 10610 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10611 "vendor")) == NULL) { 10612 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10613 } else { 10614 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10615 strncpy(inq_ptr->vendor, val, 10616 min(sizeof(inq_ptr->vendor), strlen(val))); 10617 } 10618 if (lun == NULL) { 10619 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10620 sizeof(inq_ptr->product)); 10621 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10622 switch (lun->be_lun->lun_type) { 10623 case T_DIRECT: 10624 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10625 sizeof(inq_ptr->product)); 10626 break; 10627 case T_PROCESSOR: 10628 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10629 sizeof(inq_ptr->product)); 10630 break; 10631 default: 10632 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10633 sizeof(inq_ptr->product)); 10634 break; 10635 } 10636 } else { 10637 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10638 strncpy(inq_ptr->product, val, 10639 min(sizeof(inq_ptr->product), strlen(val))); 10640 } 10641 10642 /* 10643 * XXX make this a macro somewhere so it automatically gets 10644 * incremented when we make changes. 10645 */ 10646 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10647 "revision")) == NULL) { 10648 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10649 } else { 10650 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10651 strncpy(inq_ptr->revision, val, 10652 min(sizeof(inq_ptr->revision), strlen(val))); 10653 } 10654 10655 /* 10656 * For parallel SCSI, we support double transition and single 10657 * transition clocking. We also support QAS (Quick Arbitration 10658 * and Selection) and Information Unit transfers on both the 10659 * control and array devices. 10660 */ 10661 if (port_type == CTL_PORT_SCSI) 10662 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10663 SID_SPI_IUS; 10664 10665 /* SAM-5 (no version claimed) */ 10666 scsi_ulto2b(0x00A0, inq_ptr->version1); 10667 /* SPC-4 (no version claimed) */ 10668 scsi_ulto2b(0x0460, inq_ptr->version2); 10669 if (port_type == CTL_PORT_FC) { 10670 /* FCP-2 ANSI INCITS.350:2003 */ 10671 scsi_ulto2b(0x0917, inq_ptr->version3); 10672 } else if (port_type == CTL_PORT_SCSI) { 10673 /* SPI-4 ANSI INCITS.362:200x */ 10674 scsi_ulto2b(0x0B56, inq_ptr->version3); 10675 } else if (port_type == CTL_PORT_ISCSI) { 10676 /* iSCSI (no version claimed) */ 10677 scsi_ulto2b(0x0960, inq_ptr->version3); 10678 } else if (port_type == CTL_PORT_SAS) { 10679 /* SAS (no version claimed) */ 10680 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10681 } 10682 10683 if (lun == NULL) { 10684 /* SBC-3 (no version claimed) */ 10685 scsi_ulto2b(0x04C0, inq_ptr->version4); 10686 } else { 10687 switch (lun->be_lun->lun_type) { 10688 case T_DIRECT: 10689 /* SBC-3 (no version claimed) */ 10690 scsi_ulto2b(0x04C0, inq_ptr->version4); 10691 break; 10692 case T_PROCESSOR: 10693 default: 10694 break; 10695 } 10696 } 10697 10698 ctsio->scsi_status = SCSI_STATUS_OK; 10699 if (ctsio->kern_data_len > 0) { 10700 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10701 ctsio->be_move_done = ctl_config_move_done; 10702 ctl_datamove((union ctl_io *)ctsio); 10703 } else { 10704 ctsio->io_hdr.status = CTL_SUCCESS; 10705 ctl_done((union ctl_io *)ctsio); 10706 } 10707 10708 return (CTL_RETVAL_COMPLETE); 10709 } 10710 10711 int 10712 ctl_inquiry(struct ctl_scsiio *ctsio) 10713 { 10714 struct scsi_inquiry *cdb; 10715 int retval; 10716 10717 cdb = (struct scsi_inquiry *)ctsio->cdb; 10718 10719 retval = 0; 10720 10721 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10722 10723 /* 10724 * Right now, we don't support the CmdDt inquiry information. 10725 * This would be nice to support in the future. When we do 10726 * support it, we should change this test so that it checks to make 10727 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10728 */ 10729 #ifdef notyet 10730 if (((cdb->byte2 & SI_EVPD) 10731 && (cdb->byte2 & SI_CMDDT))) 10732 #endif 10733 if (cdb->byte2 & SI_CMDDT) { 10734 /* 10735 * Point to the SI_CMDDT bit. We might change this 10736 * when we support SI_CMDDT, but since both bits would be 10737 * "wrong", this should probably just stay as-is then. 10738 */ 10739 ctl_set_invalid_field(ctsio, 10740 /*sks_valid*/ 1, 10741 /*command*/ 1, 10742 /*field*/ 1, 10743 /*bit_valid*/ 1, 10744 /*bit*/ 1); 10745 ctl_done((union ctl_io *)ctsio); 10746 return (CTL_RETVAL_COMPLETE); 10747 } 10748 if (cdb->byte2 & SI_EVPD) 10749 retval = ctl_inquiry_evpd(ctsio); 10750 #ifdef notyet 10751 else if (cdb->byte2 & SI_CMDDT) 10752 retval = ctl_inquiry_cmddt(ctsio); 10753 #endif 10754 else 10755 retval = ctl_inquiry_std(ctsio); 10756 10757 return (retval); 10758 } 10759 10760 /* 10761 * For known CDB types, parse the LBA and length. 10762 */ 10763 static int 10764 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10765 { 10766 if (io->io_hdr.io_type != CTL_IO_SCSI) 10767 return (1); 10768 10769 switch (io->scsiio.cdb[0]) { 10770 case COMPARE_AND_WRITE: { 10771 struct scsi_compare_and_write *cdb; 10772 10773 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10774 10775 *lba = scsi_8btou64(cdb->addr); 10776 *len = cdb->length; 10777 break; 10778 } 10779 case READ_6: 10780 case WRITE_6: { 10781 struct scsi_rw_6 *cdb; 10782 10783 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10784 10785 *lba = scsi_3btoul(cdb->addr); 10786 /* only 5 bits are valid in the most significant address byte */ 10787 *lba &= 0x1fffff; 10788 *len = cdb->length; 10789 break; 10790 } 10791 case READ_10: 10792 case WRITE_10: { 10793 struct scsi_rw_10 *cdb; 10794 10795 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10796 10797 *lba = scsi_4btoul(cdb->addr); 10798 *len = scsi_2btoul(cdb->length); 10799 break; 10800 } 10801 case WRITE_VERIFY_10: { 10802 struct scsi_write_verify_10 *cdb; 10803 10804 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10805 10806 *lba = scsi_4btoul(cdb->addr); 10807 *len = scsi_2btoul(cdb->length); 10808 break; 10809 } 10810 case READ_12: 10811 case WRITE_12: { 10812 struct scsi_rw_12 *cdb; 10813 10814 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10815 10816 *lba = scsi_4btoul(cdb->addr); 10817 *len = scsi_4btoul(cdb->length); 10818 break; 10819 } 10820 case WRITE_VERIFY_12: { 10821 struct scsi_write_verify_12 *cdb; 10822 10823 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10824 10825 *lba = scsi_4btoul(cdb->addr); 10826 *len = scsi_4btoul(cdb->length); 10827 break; 10828 } 10829 case READ_16: 10830 case WRITE_16: { 10831 struct scsi_rw_16 *cdb; 10832 10833 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10834 10835 *lba = scsi_8btou64(cdb->addr); 10836 *len = scsi_4btoul(cdb->length); 10837 break; 10838 } 10839 case WRITE_VERIFY_16: { 10840 struct scsi_write_verify_16 *cdb; 10841 10842 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10843 10844 10845 *lba = scsi_8btou64(cdb->addr); 10846 *len = scsi_4btoul(cdb->length); 10847 break; 10848 } 10849 case WRITE_SAME_10: { 10850 struct scsi_write_same_10 *cdb; 10851 10852 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10853 10854 *lba = scsi_4btoul(cdb->addr); 10855 *len = scsi_2btoul(cdb->length); 10856 break; 10857 } 10858 case WRITE_SAME_16: { 10859 struct scsi_write_same_16 *cdb; 10860 10861 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10862 10863 *lba = scsi_8btou64(cdb->addr); 10864 *len = scsi_4btoul(cdb->length); 10865 break; 10866 } 10867 case VERIFY_10: { 10868 struct scsi_verify_10 *cdb; 10869 10870 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10871 10872 *lba = scsi_4btoul(cdb->addr); 10873 *len = scsi_2btoul(cdb->length); 10874 break; 10875 } 10876 case VERIFY_12: { 10877 struct scsi_verify_12 *cdb; 10878 10879 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10880 10881 *lba = scsi_4btoul(cdb->addr); 10882 *len = scsi_4btoul(cdb->length); 10883 break; 10884 } 10885 case VERIFY_16: { 10886 struct scsi_verify_16 *cdb; 10887 10888 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10889 10890 *lba = scsi_8btou64(cdb->addr); 10891 *len = scsi_4btoul(cdb->length); 10892 break; 10893 } 10894 case UNMAP: { 10895 *lba = 0; 10896 *len = UINT64_MAX; 10897 break; 10898 } 10899 default: 10900 return (1); 10901 break; /* NOTREACHED */ 10902 } 10903 10904 return (0); 10905 } 10906 10907 static ctl_action 10908 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10909 { 10910 uint64_t endlba1, endlba2; 10911 10912 endlba1 = lba1 + len1 - 1; 10913 endlba2 = lba2 + len2 - 1; 10914 10915 if ((endlba1 < lba2) 10916 || (endlba2 < lba1)) 10917 return (CTL_ACTION_PASS); 10918 else 10919 return (CTL_ACTION_BLOCK); 10920 } 10921 10922 static int 10923 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10924 { 10925 struct ctl_ptr_len_flags *ptrlen; 10926 struct scsi_unmap_desc *buf, *end, *range; 10927 uint64_t lba; 10928 uint32_t len; 10929 10930 /* If not UNMAP -- go other way. */ 10931 if (io->io_hdr.io_type != CTL_IO_SCSI || 10932 io->scsiio.cdb[0] != UNMAP) 10933 return (CTL_ACTION_ERROR); 10934 10935 /* If UNMAP without data -- block and wait for data. */ 10936 ptrlen = (struct ctl_ptr_len_flags *) 10937 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10938 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10939 ptrlen->ptr == NULL) 10940 return (CTL_ACTION_BLOCK); 10941 10942 /* UNMAP with data -- check for collision. */ 10943 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10944 end = buf + ptrlen->len / sizeof(*buf); 10945 for (range = buf; range < end; range++) { 10946 lba = scsi_8btou64(range->lba); 10947 len = scsi_4btoul(range->length); 10948 if ((lba < lba2 + len2) && (lba + len > lba2)) 10949 return (CTL_ACTION_BLOCK); 10950 } 10951 return (CTL_ACTION_PASS); 10952 } 10953 10954 static ctl_action 10955 ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10956 { 10957 uint64_t lba1, lba2; 10958 uint64_t len1, len2; 10959 int retval; 10960 10961 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10962 return (CTL_ACTION_ERROR); 10963 10964 retval = ctl_extent_check_unmap(io2, lba1, len1); 10965 if (retval != CTL_ACTION_ERROR) 10966 return (retval); 10967 10968 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10969 return (CTL_ACTION_ERROR); 10970 10971 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10972 } 10973 10974 static ctl_action 10975 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10976 union ctl_io *ooa_io) 10977 { 10978 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10979 ctl_serialize_action *serialize_row; 10980 10981 /* 10982 * The initiator attempted multiple untagged commands at the same 10983 * time. Can't do that. 10984 */ 10985 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10986 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10987 && ((pending_io->io_hdr.nexus.targ_port == 10988 ooa_io->io_hdr.nexus.targ_port) 10989 && (pending_io->io_hdr.nexus.initid.id == 10990 ooa_io->io_hdr.nexus.initid.id)) 10991 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10992 return (CTL_ACTION_OVERLAP); 10993 10994 /* 10995 * The initiator attempted to send multiple tagged commands with 10996 * the same ID. (It's fine if different initiators have the same 10997 * tag ID.) 10998 * 10999 * Even if all of those conditions are true, we don't kill the I/O 11000 * if the command ahead of us has been aborted. We won't end up 11001 * sending it to the FETD, and it's perfectly legal to resend a 11002 * command with the same tag number as long as the previous 11003 * instance of this tag number has been aborted somehow. 11004 */ 11005 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11006 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11007 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 11008 && ((pending_io->io_hdr.nexus.targ_port == 11009 ooa_io->io_hdr.nexus.targ_port) 11010 && (pending_io->io_hdr.nexus.initid.id == 11011 ooa_io->io_hdr.nexus.initid.id)) 11012 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 11013 return (CTL_ACTION_OVERLAP_TAG); 11014 11015 /* 11016 * If we get a head of queue tag, SAM-3 says that we should 11017 * immediately execute it. 11018 * 11019 * What happens if this command would normally block for some other 11020 * reason? e.g. a request sense with a head of queue tag 11021 * immediately after a write. Normally that would block, but this 11022 * will result in its getting executed immediately... 11023 * 11024 * We currently return "pass" instead of "skip", so we'll end up 11025 * going through the rest of the queue to check for overlapped tags. 11026 * 11027 * XXX KDM check for other types of blockage first?? 11028 */ 11029 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11030 return (CTL_ACTION_PASS); 11031 11032 /* 11033 * Ordered tags have to block until all items ahead of them 11034 * have completed. If we get called with an ordered tag, we always 11035 * block, if something else is ahead of us in the queue. 11036 */ 11037 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11038 return (CTL_ACTION_BLOCK); 11039 11040 /* 11041 * Simple tags get blocked until all head of queue and ordered tags 11042 * ahead of them have completed. I'm lumping untagged commands in 11043 * with simple tags here. XXX KDM is that the right thing to do? 11044 */ 11045 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11046 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11047 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11048 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11049 return (CTL_ACTION_BLOCK); 11050 11051 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio); 11052 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio); 11053 11054 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11055 11056 switch (serialize_row[pending_entry->seridx]) { 11057 case CTL_SER_BLOCK: 11058 return (CTL_ACTION_BLOCK); 11059 case CTL_SER_EXTENT: 11060 return (ctl_extent_check(pending_io, ooa_io)); 11061 case CTL_SER_EXTENTOPT: 11062 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11063 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11064 return (ctl_extent_check(pending_io, ooa_io)); 11065 /* FALLTHROUGH */ 11066 case CTL_SER_PASS: 11067 return (CTL_ACTION_PASS); 11068 case CTL_SER_BLOCKOPT: 11069 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11070 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11071 return (CTL_ACTION_BLOCK); 11072 return (CTL_ACTION_PASS); 11073 case CTL_SER_SKIP: 11074 return (CTL_ACTION_SKIP); 11075 default: 11076 panic("invalid serialization value %d", 11077 serialize_row[pending_entry->seridx]); 11078 } 11079 11080 return (CTL_ACTION_ERROR); 11081 } 11082 11083 /* 11084 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11085 * Assumptions: 11086 * - pending_io is generally either incoming, or on the blocked queue 11087 * - starting I/O is the I/O we want to start the check with. 11088 */ 11089 static ctl_action 11090 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11091 union ctl_io *starting_io) 11092 { 11093 union ctl_io *ooa_io; 11094 ctl_action action; 11095 11096 mtx_assert(&lun->lun_lock, MA_OWNED); 11097 11098 /* 11099 * Run back along the OOA queue, starting with the current 11100 * blocked I/O and going through every I/O before it on the 11101 * queue. If starting_io is NULL, we'll just end up returning 11102 * CTL_ACTION_PASS. 11103 */ 11104 for (ooa_io = starting_io; ooa_io != NULL; 11105 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11106 ooa_links)){ 11107 11108 /* 11109 * This routine just checks to see whether 11110 * cur_blocked is blocked by ooa_io, which is ahead 11111 * of it in the queue. It doesn't queue/dequeue 11112 * cur_blocked. 11113 */ 11114 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11115 switch (action) { 11116 case CTL_ACTION_BLOCK: 11117 case CTL_ACTION_OVERLAP: 11118 case CTL_ACTION_OVERLAP_TAG: 11119 case CTL_ACTION_SKIP: 11120 case CTL_ACTION_ERROR: 11121 return (action); 11122 break; /* NOTREACHED */ 11123 case CTL_ACTION_PASS: 11124 break; 11125 default: 11126 panic("invalid action %d", action); 11127 break; /* NOTREACHED */ 11128 } 11129 } 11130 11131 return (CTL_ACTION_PASS); 11132 } 11133 11134 /* 11135 * Assumptions: 11136 * - An I/O has just completed, and has been removed from the per-LUN OOA 11137 * queue, so some items on the blocked queue may now be unblocked. 11138 */ 11139 static int 11140 ctl_check_blocked(struct ctl_lun *lun) 11141 { 11142 union ctl_io *cur_blocked, *next_blocked; 11143 11144 mtx_assert(&lun->lun_lock, MA_OWNED); 11145 11146 /* 11147 * Run forward from the head of the blocked queue, checking each 11148 * entry against the I/Os prior to it on the OOA queue to see if 11149 * there is still any blockage. 11150 * 11151 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11152 * with our removing a variable on it while it is traversing the 11153 * list. 11154 */ 11155 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11156 cur_blocked != NULL; cur_blocked = next_blocked) { 11157 union ctl_io *prev_ooa; 11158 ctl_action action; 11159 11160 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11161 blocked_links); 11162 11163 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11164 ctl_ooaq, ooa_links); 11165 11166 /* 11167 * If cur_blocked happens to be the first item in the OOA 11168 * queue now, prev_ooa will be NULL, and the action 11169 * returned will just be CTL_ACTION_PASS. 11170 */ 11171 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11172 11173 switch (action) { 11174 case CTL_ACTION_BLOCK: 11175 /* Nothing to do here, still blocked */ 11176 break; 11177 case CTL_ACTION_OVERLAP: 11178 case CTL_ACTION_OVERLAP_TAG: 11179 /* 11180 * This shouldn't happen! In theory we've already 11181 * checked this command for overlap... 11182 */ 11183 break; 11184 case CTL_ACTION_PASS: 11185 case CTL_ACTION_SKIP: { 11186 struct ctl_softc *softc; 11187 const struct ctl_cmd_entry *entry; 11188 uint32_t initidx; 11189 int isc_retval; 11190 11191 /* 11192 * The skip case shouldn't happen, this transaction 11193 * should have never made it onto the blocked queue. 11194 */ 11195 /* 11196 * This I/O is no longer blocked, we can remove it 11197 * from the blocked queue. Since this is a TAILQ 11198 * (doubly linked list), we can do O(1) removals 11199 * from any place on the list. 11200 */ 11201 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11202 blocked_links); 11203 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11204 11205 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11206 /* 11207 * Need to send IO back to original side to 11208 * run 11209 */ 11210 union ctl_ha_msg msg_info; 11211 11212 msg_info.hdr.original_sc = 11213 cur_blocked->io_hdr.original_sc; 11214 msg_info.hdr.serializing_sc = cur_blocked; 11215 msg_info.hdr.msg_type = CTL_MSG_R2R; 11216 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11217 &msg_info, sizeof(msg_info), 0)) > 11218 CTL_HA_STATUS_SUCCESS) { 11219 printf("CTL:Check Blocked error from " 11220 "ctl_ha_msg_send %d\n", 11221 isc_retval); 11222 } 11223 break; 11224 } 11225 entry = ctl_get_cmd_entry(&cur_blocked->scsiio); 11226 softc = control_softc; 11227 11228 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11229 11230 /* 11231 * Check this I/O for LUN state changes that may 11232 * have happened while this command was blocked. 11233 * The LUN state may have been changed by a command 11234 * ahead of us in the queue, so we need to re-check 11235 * for any states that can be caused by SCSI 11236 * commands. 11237 */ 11238 if (ctl_scsiio_lun_check(softc, lun, entry, 11239 &cur_blocked->scsiio) == 0) { 11240 cur_blocked->io_hdr.flags |= 11241 CTL_FLAG_IS_WAS_ON_RTR; 11242 ctl_enqueue_rtr(cur_blocked); 11243 } else 11244 ctl_done(cur_blocked); 11245 break; 11246 } 11247 default: 11248 /* 11249 * This probably shouldn't happen -- we shouldn't 11250 * get CTL_ACTION_ERROR, or anything else. 11251 */ 11252 break; 11253 } 11254 } 11255 11256 return (CTL_RETVAL_COMPLETE); 11257 } 11258 11259 /* 11260 * This routine (with one exception) checks LUN flags that can be set by 11261 * commands ahead of us in the OOA queue. These flags have to be checked 11262 * when a command initially comes in, and when we pull a command off the 11263 * blocked queue and are preparing to execute it. The reason we have to 11264 * check these flags for commands on the blocked queue is that the LUN 11265 * state may have been changed by a command ahead of us while we're on the 11266 * blocked queue. 11267 * 11268 * Ordering is somewhat important with these checks, so please pay 11269 * careful attention to the placement of any new checks. 11270 */ 11271 static int 11272 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11273 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11274 { 11275 int retval; 11276 11277 retval = 0; 11278 11279 mtx_assert(&lun->lun_lock, MA_OWNED); 11280 11281 /* 11282 * If this shelf is a secondary shelf controller, we have to reject 11283 * any media access commands. 11284 */ 11285 #if 0 11286 /* No longer needed for HA */ 11287 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11288 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11289 ctl_set_lun_standby(ctsio); 11290 retval = 1; 11291 goto bailout; 11292 } 11293 #endif 11294 11295 /* 11296 * Check for a reservation conflict. If this command isn't allowed 11297 * even on reserved LUNs, and if this initiator isn't the one who 11298 * reserved us, reject the command with a reservation conflict. 11299 */ 11300 if ((lun->flags & CTL_LUN_RESERVED) 11301 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11302 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 11303 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 11304 || (ctsio->io_hdr.nexus.targ_target.id != 11305 lun->rsv_nexus.targ_target.id)) { 11306 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11307 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11308 retval = 1; 11309 goto bailout; 11310 } 11311 } 11312 11313 if ( (lun->flags & CTL_LUN_PR_RESERVED) 11314 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11315 uint32_t residx; 11316 11317 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11318 /* 11319 * if we aren't registered or it's a res holder type 11320 * reservation and this isn't the res holder then set a 11321 * conflict. 11322 * NOTE: Commands which might be allowed on write exclusive 11323 * type reservations are checked in the particular command 11324 * for a conflict. Read and SSU are the only ones. 11325 */ 11326 if (!lun->per_res[residx].registered 11327 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11328 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11329 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11330 retval = 1; 11331 goto bailout; 11332 } 11333 11334 } 11335 11336 if ((lun->flags & CTL_LUN_OFFLINE) 11337 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11338 ctl_set_lun_not_ready(ctsio); 11339 retval = 1; 11340 goto bailout; 11341 } 11342 11343 /* 11344 * If the LUN is stopped, see if this particular command is allowed 11345 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11346 */ 11347 if ((lun->flags & CTL_LUN_STOPPED) 11348 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11349 /* "Logical unit not ready, initializing cmd. required" */ 11350 ctl_set_lun_stopped(ctsio); 11351 retval = 1; 11352 goto bailout; 11353 } 11354 11355 if ((lun->flags & CTL_LUN_INOPERABLE) 11356 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11357 /* "Medium format corrupted" */ 11358 ctl_set_medium_format_corrupted(ctsio); 11359 retval = 1; 11360 goto bailout; 11361 } 11362 11363 bailout: 11364 return (retval); 11365 11366 } 11367 11368 static void 11369 ctl_failover_io(union ctl_io *io, int have_lock) 11370 { 11371 ctl_set_busy(&io->scsiio); 11372 ctl_done(io); 11373 } 11374 11375 static void 11376 ctl_failover(void) 11377 { 11378 struct ctl_lun *lun; 11379 struct ctl_softc *ctl_softc; 11380 union ctl_io *next_io, *pending_io; 11381 union ctl_io *io; 11382 int lun_idx; 11383 int i; 11384 11385 ctl_softc = control_softc; 11386 11387 mtx_lock(&ctl_softc->ctl_lock); 11388 /* 11389 * Remove any cmds from the other SC from the rtr queue. These 11390 * will obviously only be for LUNs for which we're the primary. 11391 * We can't send status or get/send data for these commands. 11392 * Since they haven't been executed yet, we can just remove them. 11393 * We'll either abort them or delete them below, depending on 11394 * which HA mode we're in. 11395 */ 11396 #ifdef notyet 11397 mtx_lock(&ctl_softc->queue_lock); 11398 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11399 io != NULL; io = next_io) { 11400 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11401 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11402 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11403 ctl_io_hdr, links); 11404 } 11405 mtx_unlock(&ctl_softc->queue_lock); 11406 #endif 11407 11408 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11409 lun = ctl_softc->ctl_luns[lun_idx]; 11410 if (lun==NULL) 11411 continue; 11412 11413 /* 11414 * Processor LUNs are primary on both sides. 11415 * XXX will this always be true? 11416 */ 11417 if (lun->be_lun->lun_type == T_PROCESSOR) 11418 continue; 11419 11420 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11421 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11422 printf("FAILOVER: primary lun %d\n", lun_idx); 11423 /* 11424 * Remove all commands from the other SC. First from the 11425 * blocked queue then from the ooa queue. Once we have 11426 * removed them. Call ctl_check_blocked to see if there 11427 * is anything that can run. 11428 */ 11429 for (io = (union ctl_io *)TAILQ_FIRST( 11430 &lun->blocked_queue); io != NULL; io = next_io) { 11431 11432 next_io = (union ctl_io *)TAILQ_NEXT( 11433 &io->io_hdr, blocked_links); 11434 11435 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11436 TAILQ_REMOVE(&lun->blocked_queue, 11437 &io->io_hdr,blocked_links); 11438 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11439 TAILQ_REMOVE(&lun->ooa_queue, 11440 &io->io_hdr, ooa_links); 11441 11442 ctl_free_io(io); 11443 } 11444 } 11445 11446 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11447 io != NULL; io = next_io) { 11448 11449 next_io = (union ctl_io *)TAILQ_NEXT( 11450 &io->io_hdr, ooa_links); 11451 11452 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11453 11454 TAILQ_REMOVE(&lun->ooa_queue, 11455 &io->io_hdr, 11456 ooa_links); 11457 11458 ctl_free_io(io); 11459 } 11460 } 11461 ctl_check_blocked(lun); 11462 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11463 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11464 11465 printf("FAILOVER: primary lun %d\n", lun_idx); 11466 /* 11467 * Abort all commands from the other SC. We can't 11468 * send status back for them now. These should get 11469 * cleaned up when they are completed or come out 11470 * for a datamove operation. 11471 */ 11472 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11473 io != NULL; io = next_io) { 11474 next_io = (union ctl_io *)TAILQ_NEXT( 11475 &io->io_hdr, ooa_links); 11476 11477 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11478 io->io_hdr.flags |= CTL_FLAG_ABORT; 11479 } 11480 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11481 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11482 11483 printf("FAILOVER: secondary lun %d\n", lun_idx); 11484 11485 lun->flags |= CTL_LUN_PRIMARY_SC; 11486 11487 /* 11488 * We send all I/O that was sent to this controller 11489 * and redirected to the other side back with 11490 * busy status, and have the initiator retry it. 11491 * Figuring out how much data has been transferred, 11492 * etc. and picking up where we left off would be 11493 * very tricky. 11494 * 11495 * XXX KDM need to remove I/O from the blocked 11496 * queue as well! 11497 */ 11498 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11499 &lun->ooa_queue); pending_io != NULL; 11500 pending_io = next_io) { 11501 11502 next_io = (union ctl_io *)TAILQ_NEXT( 11503 &pending_io->io_hdr, ooa_links); 11504 11505 pending_io->io_hdr.flags &= 11506 ~CTL_FLAG_SENT_2OTHER_SC; 11507 11508 if (pending_io->io_hdr.flags & 11509 CTL_FLAG_IO_ACTIVE) { 11510 pending_io->io_hdr.flags |= 11511 CTL_FLAG_FAILOVER; 11512 } else { 11513 ctl_set_busy(&pending_io->scsiio); 11514 ctl_done(pending_io); 11515 } 11516 } 11517 11518 /* 11519 * Build Unit Attention 11520 */ 11521 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11522 lun->pending_ua[i] |= 11523 CTL_UA_ASYM_ACC_CHANGE; 11524 } 11525 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11526 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11527 printf("FAILOVER: secondary lun %d\n", lun_idx); 11528 /* 11529 * if the first io on the OOA is not on the RtR queue 11530 * add it. 11531 */ 11532 lun->flags |= CTL_LUN_PRIMARY_SC; 11533 11534 pending_io = (union ctl_io *)TAILQ_FIRST( 11535 &lun->ooa_queue); 11536 if (pending_io==NULL) { 11537 printf("Nothing on OOA queue\n"); 11538 continue; 11539 } 11540 11541 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11542 if ((pending_io->io_hdr.flags & 11543 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11544 pending_io->io_hdr.flags |= 11545 CTL_FLAG_IS_WAS_ON_RTR; 11546 ctl_enqueue_rtr(pending_io); 11547 } 11548 #if 0 11549 else 11550 { 11551 printf("Tag 0x%04x is running\n", 11552 pending_io->scsiio.tag_num); 11553 } 11554 #endif 11555 11556 next_io = (union ctl_io *)TAILQ_NEXT( 11557 &pending_io->io_hdr, ooa_links); 11558 for (pending_io=next_io; pending_io != NULL; 11559 pending_io = next_io) { 11560 pending_io->io_hdr.flags &= 11561 ~CTL_FLAG_SENT_2OTHER_SC; 11562 next_io = (union ctl_io *)TAILQ_NEXT( 11563 &pending_io->io_hdr, ooa_links); 11564 if (pending_io->io_hdr.flags & 11565 CTL_FLAG_IS_WAS_ON_RTR) { 11566 #if 0 11567 printf("Tag 0x%04x is running\n", 11568 pending_io->scsiio.tag_num); 11569 #endif 11570 continue; 11571 } 11572 11573 switch (ctl_check_ooa(lun, pending_io, 11574 (union ctl_io *)TAILQ_PREV( 11575 &pending_io->io_hdr, ctl_ooaq, 11576 ooa_links))) { 11577 11578 case CTL_ACTION_BLOCK: 11579 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11580 &pending_io->io_hdr, 11581 blocked_links); 11582 pending_io->io_hdr.flags |= 11583 CTL_FLAG_BLOCKED; 11584 break; 11585 case CTL_ACTION_PASS: 11586 case CTL_ACTION_SKIP: 11587 pending_io->io_hdr.flags |= 11588 CTL_FLAG_IS_WAS_ON_RTR; 11589 ctl_enqueue_rtr(pending_io); 11590 break; 11591 case CTL_ACTION_OVERLAP: 11592 ctl_set_overlapped_cmd( 11593 (struct ctl_scsiio *)pending_io); 11594 ctl_done(pending_io); 11595 break; 11596 case CTL_ACTION_OVERLAP_TAG: 11597 ctl_set_overlapped_tag( 11598 (struct ctl_scsiio *)pending_io, 11599 pending_io->scsiio.tag_num & 0xff); 11600 ctl_done(pending_io); 11601 break; 11602 case CTL_ACTION_ERROR: 11603 default: 11604 ctl_set_internal_failure( 11605 (struct ctl_scsiio *)pending_io, 11606 0, // sks_valid 11607 0); //retry count 11608 ctl_done(pending_io); 11609 break; 11610 } 11611 } 11612 11613 /* 11614 * Build Unit Attention 11615 */ 11616 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11617 lun->pending_ua[i] |= 11618 CTL_UA_ASYM_ACC_CHANGE; 11619 } 11620 } else { 11621 panic("Unhandled HA mode failover, LUN flags = %#x, " 11622 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11623 } 11624 } 11625 ctl_pause_rtr = 0; 11626 mtx_unlock(&ctl_softc->ctl_lock); 11627 } 11628 11629 static int 11630 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11631 { 11632 struct ctl_lun *lun; 11633 const struct ctl_cmd_entry *entry; 11634 uint32_t initidx, targ_lun; 11635 int retval; 11636 11637 retval = 0; 11638 11639 lun = NULL; 11640 11641 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11642 if ((targ_lun < CTL_MAX_LUNS) 11643 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11644 lun = ctl_softc->ctl_luns[targ_lun]; 11645 /* 11646 * If the LUN is invalid, pretend that it doesn't exist. 11647 * It will go away as soon as all pending I/O has been 11648 * completed. 11649 */ 11650 if (lun->flags & CTL_LUN_DISABLED) { 11651 lun = NULL; 11652 } else { 11653 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11654 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11655 lun->be_lun; 11656 if (lun->be_lun->lun_type == T_PROCESSOR) { 11657 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11658 } 11659 11660 /* 11661 * Every I/O goes into the OOA queue for a 11662 * particular LUN, and stays there until completion. 11663 */ 11664 mtx_lock(&lun->lun_lock); 11665 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11666 ooa_links); 11667 } 11668 } else { 11669 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11670 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11671 } 11672 11673 /* Get command entry and return error if it is unsuppotyed. */ 11674 entry = ctl_validate_command(ctsio); 11675 if (entry == NULL) { 11676 if (lun) 11677 mtx_unlock(&lun->lun_lock); 11678 return (retval); 11679 } 11680 11681 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11682 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11683 11684 /* 11685 * Check to see whether we can send this command to LUNs that don't 11686 * exist. This should pretty much only be the case for inquiry 11687 * and request sense. Further checks, below, really require having 11688 * a LUN, so we can't really check the command anymore. Just put 11689 * it on the rtr queue. 11690 */ 11691 if (lun == NULL) { 11692 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11693 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11694 ctl_enqueue_rtr((union ctl_io *)ctsio); 11695 return (retval); 11696 } 11697 11698 ctl_set_unsupported_lun(ctsio); 11699 ctl_done((union ctl_io *)ctsio); 11700 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11701 return (retval); 11702 } else { 11703 /* 11704 * Make sure we support this particular command on this LUN. 11705 * e.g., we don't support writes to the control LUN. 11706 */ 11707 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11708 mtx_unlock(&lun->lun_lock); 11709 ctl_set_invalid_opcode(ctsio); 11710 ctl_done((union ctl_io *)ctsio); 11711 return (retval); 11712 } 11713 } 11714 11715 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11716 11717 #ifdef CTL_WITH_CA 11718 /* 11719 * If we've got a request sense, it'll clear the contingent 11720 * allegiance condition. Otherwise, if we have a CA condition for 11721 * this initiator, clear it, because it sent down a command other 11722 * than request sense. 11723 */ 11724 if ((ctsio->cdb[0] != REQUEST_SENSE) 11725 && (ctl_is_set(lun->have_ca, initidx))) 11726 ctl_clear_mask(lun->have_ca, initidx); 11727 #endif 11728 11729 /* 11730 * If the command has this flag set, it handles its own unit 11731 * attention reporting, we shouldn't do anything. Otherwise we 11732 * check for any pending unit attentions, and send them back to the 11733 * initiator. We only do this when a command initially comes in, 11734 * not when we pull it off the blocked queue. 11735 * 11736 * According to SAM-3, section 5.3.2, the order that things get 11737 * presented back to the host is basically unit attentions caused 11738 * by some sort of reset event, busy status, reservation conflicts 11739 * or task set full, and finally any other status. 11740 * 11741 * One issue here is that some of the unit attentions we report 11742 * don't fall into the "reset" category (e.g. "reported luns data 11743 * has changed"). So reporting it here, before the reservation 11744 * check, may be technically wrong. I guess the only thing to do 11745 * would be to check for and report the reset events here, and then 11746 * check for the other unit attention types after we check for a 11747 * reservation conflict. 11748 * 11749 * XXX KDM need to fix this 11750 */ 11751 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11752 ctl_ua_type ua_type; 11753 11754 ua_type = lun->pending_ua[initidx]; 11755 if (ua_type != CTL_UA_NONE) { 11756 scsi_sense_data_type sense_format; 11757 11758 if (lun != NULL) 11759 sense_format = (lun->flags & 11760 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11761 SSD_TYPE_FIXED; 11762 else 11763 sense_format = SSD_TYPE_FIXED; 11764 11765 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data, 11766 sense_format); 11767 if (ua_type != CTL_UA_NONE) { 11768 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11769 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11770 CTL_AUTOSENSE; 11771 ctsio->sense_len = SSD_FULL_SIZE; 11772 lun->pending_ua[initidx] &= ~ua_type; 11773 mtx_unlock(&lun->lun_lock); 11774 ctl_done((union ctl_io *)ctsio); 11775 return (retval); 11776 } 11777 } 11778 } 11779 11780 11781 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11782 mtx_unlock(&lun->lun_lock); 11783 ctl_done((union ctl_io *)ctsio); 11784 return (retval); 11785 } 11786 11787 /* 11788 * XXX CHD this is where we want to send IO to other side if 11789 * this LUN is secondary on this SC. We will need to make a copy 11790 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11791 * the copy we send as FROM_OTHER. 11792 * We also need to stuff the address of the original IO so we can 11793 * find it easily. Something similar will need be done on the other 11794 * side so when we are done we can find the copy. 11795 */ 11796 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11797 union ctl_ha_msg msg_info; 11798 int isc_retval; 11799 11800 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11801 11802 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11803 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11804 #if 0 11805 printf("1. ctsio %p\n", ctsio); 11806 #endif 11807 msg_info.hdr.serializing_sc = NULL; 11808 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11809 msg_info.scsi.tag_num = ctsio->tag_num; 11810 msg_info.scsi.tag_type = ctsio->tag_type; 11811 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11812 11813 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11814 11815 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11816 (void *)&msg_info, sizeof(msg_info), 0)) > 11817 CTL_HA_STATUS_SUCCESS) { 11818 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11819 isc_retval); 11820 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11821 } else { 11822 #if 0 11823 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11824 #endif 11825 } 11826 11827 /* 11828 * XXX KDM this I/O is off the incoming queue, but hasn't 11829 * been inserted on any other queue. We may need to come 11830 * up with a holding queue while we wait for serialization 11831 * so that we have an idea of what we're waiting for from 11832 * the other side. 11833 */ 11834 mtx_unlock(&lun->lun_lock); 11835 return (retval); 11836 } 11837 11838 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11839 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11840 ctl_ooaq, ooa_links))) { 11841 case CTL_ACTION_BLOCK: 11842 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11843 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11844 blocked_links); 11845 mtx_unlock(&lun->lun_lock); 11846 return (retval); 11847 case CTL_ACTION_PASS: 11848 case CTL_ACTION_SKIP: 11849 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11850 mtx_unlock(&lun->lun_lock); 11851 ctl_enqueue_rtr((union ctl_io *)ctsio); 11852 break; 11853 case CTL_ACTION_OVERLAP: 11854 mtx_unlock(&lun->lun_lock); 11855 ctl_set_overlapped_cmd(ctsio); 11856 ctl_done((union ctl_io *)ctsio); 11857 break; 11858 case CTL_ACTION_OVERLAP_TAG: 11859 mtx_unlock(&lun->lun_lock); 11860 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11861 ctl_done((union ctl_io *)ctsio); 11862 break; 11863 case CTL_ACTION_ERROR: 11864 default: 11865 mtx_unlock(&lun->lun_lock); 11866 ctl_set_internal_failure(ctsio, 11867 /*sks_valid*/ 0, 11868 /*retry_count*/ 0); 11869 ctl_done((union ctl_io *)ctsio); 11870 break; 11871 } 11872 return (retval); 11873 } 11874 11875 const struct ctl_cmd_entry * 11876 ctl_get_cmd_entry(struct ctl_scsiio *ctsio) 11877 { 11878 const struct ctl_cmd_entry *entry; 11879 int service_action; 11880 11881 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11882 if (entry->flags & CTL_CMD_FLAG_SA5) { 11883 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11884 entry = &((const struct ctl_cmd_entry *) 11885 entry->execute)[service_action]; 11886 } 11887 return (entry); 11888 } 11889 11890 const struct ctl_cmd_entry * 11891 ctl_validate_command(struct ctl_scsiio *ctsio) 11892 { 11893 const struct ctl_cmd_entry *entry; 11894 int i; 11895 uint8_t diff; 11896 11897 entry = ctl_get_cmd_entry(ctsio); 11898 if (entry->execute == NULL) { 11899 ctl_set_invalid_opcode(ctsio); 11900 ctl_done((union ctl_io *)ctsio); 11901 return (NULL); 11902 } 11903 KASSERT(entry->length > 0, 11904 ("Not defined length for command 0x%02x/0x%02x", 11905 ctsio->cdb[0], ctsio->cdb[1])); 11906 for (i = 1; i < entry->length; i++) { 11907 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11908 if (diff == 0) 11909 continue; 11910 ctl_set_invalid_field(ctsio, 11911 /*sks_valid*/ 1, 11912 /*command*/ 1, 11913 /*field*/ i, 11914 /*bit_valid*/ 1, 11915 /*bit*/ fls(diff) - 1); 11916 ctl_done((union ctl_io *)ctsio); 11917 return (NULL); 11918 } 11919 return (entry); 11920 } 11921 11922 static int 11923 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11924 { 11925 11926 switch (lun_type) { 11927 case T_PROCESSOR: 11928 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11929 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11930 return (0); 11931 break; 11932 case T_DIRECT: 11933 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11934 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11935 return (0); 11936 break; 11937 default: 11938 return (0); 11939 } 11940 return (1); 11941 } 11942 11943 static int 11944 ctl_scsiio(struct ctl_scsiio *ctsio) 11945 { 11946 int retval; 11947 const struct ctl_cmd_entry *entry; 11948 11949 retval = CTL_RETVAL_COMPLETE; 11950 11951 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11952 11953 entry = ctl_get_cmd_entry(ctsio); 11954 11955 /* 11956 * If this I/O has been aborted, just send it straight to 11957 * ctl_done() without executing it. 11958 */ 11959 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11960 ctl_done((union ctl_io *)ctsio); 11961 goto bailout; 11962 } 11963 11964 /* 11965 * All the checks should have been handled by ctl_scsiio_precheck(). 11966 * We should be clear now to just execute the I/O. 11967 */ 11968 retval = entry->execute(ctsio); 11969 11970 bailout: 11971 return (retval); 11972 } 11973 11974 /* 11975 * Since we only implement one target right now, a bus reset simply resets 11976 * our single target. 11977 */ 11978 static int 11979 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11980 { 11981 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11982 } 11983 11984 static int 11985 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11986 ctl_ua_type ua_type) 11987 { 11988 struct ctl_lun *lun; 11989 int retval; 11990 11991 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11992 union ctl_ha_msg msg_info; 11993 11994 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11995 msg_info.hdr.nexus = io->io_hdr.nexus; 11996 if (ua_type==CTL_UA_TARG_RESET) 11997 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11998 else 11999 msg_info.task.task_action = CTL_TASK_BUS_RESET; 12000 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12001 msg_info.hdr.original_sc = NULL; 12002 msg_info.hdr.serializing_sc = NULL; 12003 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12004 (void *)&msg_info, sizeof(msg_info), 0)) { 12005 } 12006 } 12007 retval = 0; 12008 12009 mtx_lock(&ctl_softc->ctl_lock); 12010 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 12011 retval += ctl_lun_reset(lun, io, ua_type); 12012 mtx_unlock(&ctl_softc->ctl_lock); 12013 12014 return (retval); 12015 } 12016 12017 /* 12018 * The LUN should always be set. The I/O is optional, and is used to 12019 * distinguish between I/Os sent by this initiator, and by other 12020 * initiators. We set unit attention for initiators other than this one. 12021 * SAM-3 is vague on this point. It does say that a unit attention should 12022 * be established for other initiators when a LUN is reset (see section 12023 * 5.7.3), but it doesn't specifically say that the unit attention should 12024 * be established for this particular initiator when a LUN is reset. Here 12025 * is the relevant text, from SAM-3 rev 8: 12026 * 12027 * 5.7.2 When a SCSI initiator port aborts its own tasks 12028 * 12029 * When a SCSI initiator port causes its own task(s) to be aborted, no 12030 * notification that the task(s) have been aborted shall be returned to 12031 * the SCSI initiator port other than the completion response for the 12032 * command or task management function action that caused the task(s) to 12033 * be aborted and notification(s) associated with related effects of the 12034 * action (e.g., a reset unit attention condition). 12035 * 12036 * XXX KDM for now, we're setting unit attention for all initiators. 12037 */ 12038 static int 12039 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 12040 { 12041 union ctl_io *xio; 12042 #if 0 12043 uint32_t initindex; 12044 #endif 12045 int i; 12046 12047 mtx_lock(&lun->lun_lock); 12048 /* 12049 * Run through the OOA queue and abort each I/O. 12050 */ 12051 #if 0 12052 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12053 #endif 12054 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12055 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12056 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 12057 } 12058 12059 /* 12060 * This version sets unit attention for every 12061 */ 12062 #if 0 12063 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12064 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12065 if (initindex == i) 12066 continue; 12067 lun->pending_ua[i] |= ua_type; 12068 } 12069 #endif 12070 12071 /* 12072 * A reset (any kind, really) clears reservations established with 12073 * RESERVE/RELEASE. It does not clear reservations established 12074 * with PERSISTENT RESERVE OUT, but we don't support that at the 12075 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 12076 * reservations made with the RESERVE/RELEASE commands, because 12077 * those commands are obsolete in SPC-3. 12078 */ 12079 lun->flags &= ~CTL_LUN_RESERVED; 12080 12081 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 12082 #ifdef CTL_WITH_CA 12083 ctl_clear_mask(lun->have_ca, i); 12084 #endif 12085 lun->pending_ua[i] |= ua_type; 12086 } 12087 mtx_unlock(&lun->lun_lock); 12088 12089 return (0); 12090 } 12091 12092 static void 12093 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12094 int other_sc) 12095 { 12096 union ctl_io *xio; 12097 12098 mtx_assert(&lun->lun_lock, MA_OWNED); 12099 12100 /* 12101 * Run through the OOA queue and attempt to find the given I/O. 12102 * The target port, initiator ID, tag type and tag number have to 12103 * match the values that we got from the initiator. If we have an 12104 * untagged command to abort, simply abort the first untagged command 12105 * we come to. We only allow one untagged command at a time of course. 12106 */ 12107 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12108 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12109 12110 if ((targ_port == UINT32_MAX || 12111 targ_port == xio->io_hdr.nexus.targ_port) && 12112 (init_id == UINT32_MAX || 12113 init_id == xio->io_hdr.nexus.initid.id)) { 12114 if (targ_port != xio->io_hdr.nexus.targ_port || 12115 init_id != xio->io_hdr.nexus.initid.id) 12116 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12117 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12118 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12119 union ctl_ha_msg msg_info; 12120 12121 msg_info.hdr.nexus = xio->io_hdr.nexus; 12122 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12123 msg_info.task.tag_num = xio->scsiio.tag_num; 12124 msg_info.task.tag_type = xio->scsiio.tag_type; 12125 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12126 msg_info.hdr.original_sc = NULL; 12127 msg_info.hdr.serializing_sc = NULL; 12128 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12129 (void *)&msg_info, sizeof(msg_info), 0); 12130 } 12131 } 12132 } 12133 } 12134 12135 static int 12136 ctl_abort_task_set(union ctl_io *io) 12137 { 12138 struct ctl_softc *softc = control_softc; 12139 struct ctl_lun *lun; 12140 uint32_t targ_lun; 12141 12142 /* 12143 * Look up the LUN. 12144 */ 12145 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12146 mtx_lock(&softc->ctl_lock); 12147 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12148 lun = softc->ctl_luns[targ_lun]; 12149 else { 12150 mtx_unlock(&softc->ctl_lock); 12151 return (1); 12152 } 12153 12154 mtx_lock(&lun->lun_lock); 12155 mtx_unlock(&softc->ctl_lock); 12156 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12157 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12158 io->io_hdr.nexus.initid.id, 12159 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12160 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12161 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12162 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12163 } 12164 mtx_unlock(&lun->lun_lock); 12165 return (0); 12166 } 12167 12168 static int 12169 ctl_i_t_nexus_reset(union ctl_io *io) 12170 { 12171 struct ctl_softc *softc = control_softc; 12172 struct ctl_lun *lun; 12173 uint32_t initindex; 12174 12175 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12176 mtx_lock(&softc->ctl_lock); 12177 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12178 mtx_lock(&lun->lun_lock); 12179 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12180 io->io_hdr.nexus.initid.id, 12181 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12182 #ifdef CTL_WITH_CA 12183 ctl_clear_mask(lun->have_ca, initindex); 12184 #endif 12185 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12186 mtx_unlock(&lun->lun_lock); 12187 } 12188 mtx_unlock(&softc->ctl_lock); 12189 return (0); 12190 } 12191 12192 static int 12193 ctl_abort_task(union ctl_io *io) 12194 { 12195 union ctl_io *xio; 12196 struct ctl_lun *lun; 12197 struct ctl_softc *ctl_softc; 12198 #if 0 12199 struct sbuf sb; 12200 char printbuf[128]; 12201 #endif 12202 int found; 12203 uint32_t targ_lun; 12204 12205 ctl_softc = control_softc; 12206 found = 0; 12207 12208 /* 12209 * Look up the LUN. 12210 */ 12211 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12212 mtx_lock(&ctl_softc->ctl_lock); 12213 if ((targ_lun < CTL_MAX_LUNS) 12214 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12215 lun = ctl_softc->ctl_luns[targ_lun]; 12216 else { 12217 mtx_unlock(&ctl_softc->ctl_lock); 12218 return (1); 12219 } 12220 12221 #if 0 12222 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12223 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12224 #endif 12225 12226 mtx_lock(&lun->lun_lock); 12227 mtx_unlock(&ctl_softc->ctl_lock); 12228 /* 12229 * Run through the OOA queue and attempt to find the given I/O. 12230 * The target port, initiator ID, tag type and tag number have to 12231 * match the values that we got from the initiator. If we have an 12232 * untagged command to abort, simply abort the first untagged command 12233 * we come to. We only allow one untagged command at a time of course. 12234 */ 12235 #if 0 12236 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12237 #endif 12238 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12239 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12240 #if 0 12241 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12242 12243 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12244 lun->lun, xio->scsiio.tag_num, 12245 xio->scsiio.tag_type, 12246 (xio->io_hdr.blocked_links.tqe_prev 12247 == NULL) ? "" : " BLOCKED", 12248 (xio->io_hdr.flags & 12249 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12250 (xio->io_hdr.flags & 12251 CTL_FLAG_ABORT) ? " ABORT" : "", 12252 (xio->io_hdr.flags & 12253 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12254 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12255 sbuf_finish(&sb); 12256 printf("%s\n", sbuf_data(&sb)); 12257 #endif 12258 12259 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12260 && (xio->io_hdr.nexus.initid.id == 12261 io->io_hdr.nexus.initid.id)) { 12262 /* 12263 * If the abort says that the task is untagged, the 12264 * task in the queue must be untagged. Otherwise, 12265 * we just check to see whether the tag numbers 12266 * match. This is because the QLogic firmware 12267 * doesn't pass back the tag type in an abort 12268 * request. 12269 */ 12270 #if 0 12271 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12272 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12273 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12274 #endif 12275 /* 12276 * XXX KDM we've got problems with FC, because it 12277 * doesn't send down a tag type with aborts. So we 12278 * can only really go by the tag number... 12279 * This may cause problems with parallel SCSI. 12280 * Need to figure that out!! 12281 */ 12282 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12283 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12284 found = 1; 12285 if ((io->io_hdr.flags & 12286 CTL_FLAG_FROM_OTHER_SC) == 0 && 12287 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12288 union ctl_ha_msg msg_info; 12289 12290 io->io_hdr.flags |= 12291 CTL_FLAG_SENT_2OTHER_SC; 12292 msg_info.hdr.nexus = io->io_hdr.nexus; 12293 msg_info.task.task_action = 12294 CTL_TASK_ABORT_TASK; 12295 msg_info.task.tag_num = 12296 io->taskio.tag_num; 12297 msg_info.task.tag_type = 12298 io->taskio.tag_type; 12299 msg_info.hdr.msg_type = 12300 CTL_MSG_MANAGE_TASKS; 12301 msg_info.hdr.original_sc = NULL; 12302 msg_info.hdr.serializing_sc = NULL; 12303 #if 0 12304 printf("Sent Abort to other side\n"); 12305 #endif 12306 if (CTL_HA_STATUS_SUCCESS != 12307 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12308 (void *)&msg_info, 12309 sizeof(msg_info), 0)) { 12310 } 12311 } 12312 #if 0 12313 printf("ctl_abort_task: found I/O to abort\n"); 12314 #endif 12315 break; 12316 } 12317 } 12318 } 12319 mtx_unlock(&lun->lun_lock); 12320 12321 if (found == 0) { 12322 /* 12323 * This isn't really an error. It's entirely possible for 12324 * the abort and command completion to cross on the wire. 12325 * This is more of an informative/diagnostic error. 12326 */ 12327 #if 0 12328 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12329 "%d:%d:%d:%d tag %d type %d\n", 12330 io->io_hdr.nexus.initid.id, 12331 io->io_hdr.nexus.targ_port, 12332 io->io_hdr.nexus.targ_target.id, 12333 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12334 io->taskio.tag_type); 12335 #endif 12336 } 12337 return (0); 12338 } 12339 12340 static void 12341 ctl_run_task(union ctl_io *io) 12342 { 12343 struct ctl_softc *ctl_softc = control_softc; 12344 int retval = 1; 12345 const char *task_desc; 12346 12347 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12348 12349 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12350 ("ctl_run_task: Unextected io_type %d\n", 12351 io->io_hdr.io_type)); 12352 12353 task_desc = ctl_scsi_task_string(&io->taskio); 12354 if (task_desc != NULL) { 12355 #ifdef NEEDTOPORT 12356 csevent_log(CSC_CTL | CSC_SHELF_SW | 12357 CTL_TASK_REPORT, 12358 csevent_LogType_Trace, 12359 csevent_Severity_Information, 12360 csevent_AlertLevel_Green, 12361 csevent_FRU_Firmware, 12362 csevent_FRU_Unknown, 12363 "CTL: received task: %s",task_desc); 12364 #endif 12365 } else { 12366 #ifdef NEEDTOPORT 12367 csevent_log(CSC_CTL | CSC_SHELF_SW | 12368 CTL_TASK_REPORT, 12369 csevent_LogType_Trace, 12370 csevent_Severity_Information, 12371 csevent_AlertLevel_Green, 12372 csevent_FRU_Firmware, 12373 csevent_FRU_Unknown, 12374 "CTL: received unknown task " 12375 "type: %d (%#x)", 12376 io->taskio.task_action, 12377 io->taskio.task_action); 12378 #endif 12379 } 12380 switch (io->taskio.task_action) { 12381 case CTL_TASK_ABORT_TASK: 12382 retval = ctl_abort_task(io); 12383 break; 12384 case CTL_TASK_ABORT_TASK_SET: 12385 case CTL_TASK_CLEAR_TASK_SET: 12386 retval = ctl_abort_task_set(io); 12387 break; 12388 case CTL_TASK_CLEAR_ACA: 12389 break; 12390 case CTL_TASK_I_T_NEXUS_RESET: 12391 retval = ctl_i_t_nexus_reset(io); 12392 break; 12393 case CTL_TASK_LUN_RESET: { 12394 struct ctl_lun *lun; 12395 uint32_t targ_lun; 12396 12397 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12398 mtx_lock(&ctl_softc->ctl_lock); 12399 if ((targ_lun < CTL_MAX_LUNS) 12400 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12401 lun = ctl_softc->ctl_luns[targ_lun]; 12402 else { 12403 mtx_unlock(&ctl_softc->ctl_lock); 12404 retval = 1; 12405 break; 12406 } 12407 12408 if (!(io->io_hdr.flags & 12409 CTL_FLAG_FROM_OTHER_SC)) { 12410 union ctl_ha_msg msg_info; 12411 12412 io->io_hdr.flags |= 12413 CTL_FLAG_SENT_2OTHER_SC; 12414 msg_info.hdr.msg_type = 12415 CTL_MSG_MANAGE_TASKS; 12416 msg_info.hdr.nexus = io->io_hdr.nexus; 12417 msg_info.task.task_action = 12418 CTL_TASK_LUN_RESET; 12419 msg_info.hdr.original_sc = NULL; 12420 msg_info.hdr.serializing_sc = NULL; 12421 if (CTL_HA_STATUS_SUCCESS != 12422 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12423 (void *)&msg_info, 12424 sizeof(msg_info), 0)) { 12425 } 12426 } 12427 12428 retval = ctl_lun_reset(lun, io, 12429 CTL_UA_LUN_RESET); 12430 mtx_unlock(&ctl_softc->ctl_lock); 12431 break; 12432 } 12433 case CTL_TASK_TARGET_RESET: 12434 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12435 break; 12436 case CTL_TASK_BUS_RESET: 12437 retval = ctl_bus_reset(ctl_softc, io); 12438 break; 12439 case CTL_TASK_PORT_LOGIN: 12440 break; 12441 case CTL_TASK_PORT_LOGOUT: 12442 break; 12443 default: 12444 printf("ctl_run_task: got unknown task management event %d\n", 12445 io->taskio.task_action); 12446 break; 12447 } 12448 if (retval == 0) 12449 io->io_hdr.status = CTL_SUCCESS; 12450 else 12451 io->io_hdr.status = CTL_ERROR; 12452 ctl_done(io); 12453 } 12454 12455 /* 12456 * For HA operation. Handle commands that come in from the other 12457 * controller. 12458 */ 12459 static void 12460 ctl_handle_isc(union ctl_io *io) 12461 { 12462 int free_io; 12463 struct ctl_lun *lun; 12464 struct ctl_softc *ctl_softc; 12465 uint32_t targ_lun; 12466 12467 ctl_softc = control_softc; 12468 12469 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12470 lun = ctl_softc->ctl_luns[targ_lun]; 12471 12472 switch (io->io_hdr.msg_type) { 12473 case CTL_MSG_SERIALIZE: 12474 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12475 break; 12476 case CTL_MSG_R2R: { 12477 const struct ctl_cmd_entry *entry; 12478 12479 /* 12480 * This is only used in SER_ONLY mode. 12481 */ 12482 free_io = 0; 12483 entry = ctl_get_cmd_entry(&io->scsiio); 12484 mtx_lock(&lun->lun_lock); 12485 if (ctl_scsiio_lun_check(ctl_softc, lun, 12486 entry, (struct ctl_scsiio *)io) != 0) { 12487 mtx_unlock(&lun->lun_lock); 12488 ctl_done(io); 12489 break; 12490 } 12491 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12492 mtx_unlock(&lun->lun_lock); 12493 ctl_enqueue_rtr(io); 12494 break; 12495 } 12496 case CTL_MSG_FINISH_IO: 12497 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12498 free_io = 0; 12499 ctl_done(io); 12500 } else { 12501 free_io = 1; 12502 mtx_lock(&lun->lun_lock); 12503 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12504 ooa_links); 12505 ctl_check_blocked(lun); 12506 mtx_unlock(&lun->lun_lock); 12507 } 12508 break; 12509 case CTL_MSG_PERS_ACTION: 12510 ctl_hndl_per_res_out_on_other_sc( 12511 (union ctl_ha_msg *)&io->presio.pr_msg); 12512 free_io = 1; 12513 break; 12514 case CTL_MSG_BAD_JUJU: 12515 free_io = 0; 12516 ctl_done(io); 12517 break; 12518 case CTL_MSG_DATAMOVE: 12519 /* Only used in XFER mode */ 12520 free_io = 0; 12521 ctl_datamove_remote(io); 12522 break; 12523 case CTL_MSG_DATAMOVE_DONE: 12524 /* Only used in XFER mode */ 12525 free_io = 0; 12526 io->scsiio.be_move_done(io); 12527 break; 12528 default: 12529 free_io = 1; 12530 printf("%s: Invalid message type %d\n", 12531 __func__, io->io_hdr.msg_type); 12532 break; 12533 } 12534 if (free_io) 12535 ctl_free_io(io); 12536 12537 } 12538 12539 12540 /* 12541 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12542 * there is no match. 12543 */ 12544 static ctl_lun_error_pattern 12545 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12546 { 12547 const struct ctl_cmd_entry *entry; 12548 ctl_lun_error_pattern filtered_pattern, pattern; 12549 12550 pattern = desc->error_pattern; 12551 12552 /* 12553 * XXX KDM we need more data passed into this function to match a 12554 * custom pattern, and we actually need to implement custom pattern 12555 * matching. 12556 */ 12557 if (pattern & CTL_LUN_PAT_CMD) 12558 return (CTL_LUN_PAT_CMD); 12559 12560 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12561 return (CTL_LUN_PAT_ANY); 12562 12563 entry = ctl_get_cmd_entry(ctsio); 12564 12565 filtered_pattern = entry->pattern & pattern; 12566 12567 /* 12568 * If the user requested specific flags in the pattern (e.g. 12569 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12570 * flags. 12571 * 12572 * If the user did not specify any flags, it doesn't matter whether 12573 * or not the command supports the flags. 12574 */ 12575 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12576 (pattern & ~CTL_LUN_PAT_MASK)) 12577 return (CTL_LUN_PAT_NONE); 12578 12579 /* 12580 * If the user asked for a range check, see if the requested LBA 12581 * range overlaps with this command's LBA range. 12582 */ 12583 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12584 uint64_t lba1; 12585 uint64_t len1; 12586 ctl_action action; 12587 int retval; 12588 12589 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12590 if (retval != 0) 12591 return (CTL_LUN_PAT_NONE); 12592 12593 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12594 desc->lba_range.len); 12595 /* 12596 * A "pass" means that the LBA ranges don't overlap, so 12597 * this doesn't match the user's range criteria. 12598 */ 12599 if (action == CTL_ACTION_PASS) 12600 return (CTL_LUN_PAT_NONE); 12601 } 12602 12603 return (filtered_pattern); 12604 } 12605 12606 static void 12607 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12608 { 12609 struct ctl_error_desc *desc, *desc2; 12610 12611 mtx_assert(&lun->lun_lock, MA_OWNED); 12612 12613 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12614 ctl_lun_error_pattern pattern; 12615 /* 12616 * Check to see whether this particular command matches 12617 * the pattern in the descriptor. 12618 */ 12619 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12620 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12621 continue; 12622 12623 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12624 case CTL_LUN_INJ_ABORTED: 12625 ctl_set_aborted(&io->scsiio); 12626 break; 12627 case CTL_LUN_INJ_MEDIUM_ERR: 12628 ctl_set_medium_error(&io->scsiio); 12629 break; 12630 case CTL_LUN_INJ_UA: 12631 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12632 * OCCURRED */ 12633 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12634 break; 12635 case CTL_LUN_INJ_CUSTOM: 12636 /* 12637 * We're assuming the user knows what he is doing. 12638 * Just copy the sense information without doing 12639 * checks. 12640 */ 12641 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12642 ctl_min(sizeof(desc->custom_sense), 12643 sizeof(io->scsiio.sense_data))); 12644 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12645 io->scsiio.sense_len = SSD_FULL_SIZE; 12646 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12647 break; 12648 case CTL_LUN_INJ_NONE: 12649 default: 12650 /* 12651 * If this is an error injection type we don't know 12652 * about, clear the continuous flag (if it is set) 12653 * so it will get deleted below. 12654 */ 12655 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12656 break; 12657 } 12658 /* 12659 * By default, each error injection action is a one-shot 12660 */ 12661 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12662 continue; 12663 12664 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12665 12666 free(desc, M_CTL); 12667 } 12668 } 12669 12670 #ifdef CTL_IO_DELAY 12671 static void 12672 ctl_datamove_timer_wakeup(void *arg) 12673 { 12674 union ctl_io *io; 12675 12676 io = (union ctl_io *)arg; 12677 12678 ctl_datamove(io); 12679 } 12680 #endif /* CTL_IO_DELAY */ 12681 12682 void 12683 ctl_datamove(union ctl_io *io) 12684 { 12685 void (*fe_datamove)(union ctl_io *io); 12686 12687 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12688 12689 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12690 12691 #ifdef CTL_TIME_IO 12692 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12693 char str[256]; 12694 char path_str[64]; 12695 struct sbuf sb; 12696 12697 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12698 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12699 12700 sbuf_cat(&sb, path_str); 12701 switch (io->io_hdr.io_type) { 12702 case CTL_IO_SCSI: 12703 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12704 sbuf_printf(&sb, "\n"); 12705 sbuf_cat(&sb, path_str); 12706 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12707 io->scsiio.tag_num, io->scsiio.tag_type); 12708 break; 12709 case CTL_IO_TASK: 12710 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12711 "Tag Type: %d\n", io->taskio.task_action, 12712 io->taskio.tag_num, io->taskio.tag_type); 12713 break; 12714 default: 12715 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12716 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12717 break; 12718 } 12719 sbuf_cat(&sb, path_str); 12720 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12721 (intmax_t)time_uptime - io->io_hdr.start_time); 12722 sbuf_finish(&sb); 12723 printf("%s", sbuf_data(&sb)); 12724 } 12725 #endif /* CTL_TIME_IO */ 12726 12727 #ifdef CTL_IO_DELAY 12728 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12729 struct ctl_lun *lun; 12730 12731 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12732 12733 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12734 } else { 12735 struct ctl_lun *lun; 12736 12737 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12738 if ((lun != NULL) 12739 && (lun->delay_info.datamove_delay > 0)) { 12740 struct callout *callout; 12741 12742 callout = (struct callout *)&io->io_hdr.timer_bytes; 12743 callout_init(callout, /*mpsafe*/ 1); 12744 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12745 callout_reset(callout, 12746 lun->delay_info.datamove_delay * hz, 12747 ctl_datamove_timer_wakeup, io); 12748 if (lun->delay_info.datamove_type == 12749 CTL_DELAY_TYPE_ONESHOT) 12750 lun->delay_info.datamove_delay = 0; 12751 return; 12752 } 12753 } 12754 #endif 12755 12756 /* 12757 * This command has been aborted. Set the port status, so we fail 12758 * the data move. 12759 */ 12760 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12761 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12762 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12763 io->io_hdr.nexus.targ_port, 12764 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12765 io->io_hdr.nexus.targ_lun); 12766 io->io_hdr.port_status = 31337; 12767 /* 12768 * Note that the backend, in this case, will get the 12769 * callback in its context. In other cases it may get 12770 * called in the frontend's interrupt thread context. 12771 */ 12772 io->scsiio.be_move_done(io); 12773 return; 12774 } 12775 12776 /* 12777 * If we're in XFER mode and this I/O is from the other shelf 12778 * controller, we need to send the DMA to the other side to 12779 * actually transfer the data to/from the host. In serialize only 12780 * mode the transfer happens below CTL and ctl_datamove() is only 12781 * called on the machine that originally received the I/O. 12782 */ 12783 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12784 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12785 union ctl_ha_msg msg; 12786 uint32_t sg_entries_sent; 12787 int do_sg_copy; 12788 int i; 12789 12790 memset(&msg, 0, sizeof(msg)); 12791 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12792 msg.hdr.original_sc = io->io_hdr.original_sc; 12793 msg.hdr.serializing_sc = io; 12794 msg.hdr.nexus = io->io_hdr.nexus; 12795 msg.dt.flags = io->io_hdr.flags; 12796 /* 12797 * We convert everything into a S/G list here. We can't 12798 * pass by reference, only by value between controllers. 12799 * So we can't pass a pointer to the S/G list, only as many 12800 * S/G entries as we can fit in here. If it's possible for 12801 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12802 * then we need to break this up into multiple transfers. 12803 */ 12804 if (io->scsiio.kern_sg_entries == 0) { 12805 msg.dt.kern_sg_entries = 1; 12806 /* 12807 * If this is in cached memory, flush the cache 12808 * before we send the DMA request to the other 12809 * controller. We want to do this in either the 12810 * read or the write case. The read case is 12811 * straightforward. In the write case, we want to 12812 * make sure nothing is in the local cache that 12813 * could overwrite the DMAed data. 12814 */ 12815 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12816 /* 12817 * XXX KDM use bus_dmamap_sync() here. 12818 */ 12819 } 12820 12821 /* 12822 * Convert to a physical address if this is a 12823 * virtual address. 12824 */ 12825 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12826 msg.dt.sg_list[0].addr = 12827 io->scsiio.kern_data_ptr; 12828 } else { 12829 /* 12830 * XXX KDM use busdma here! 12831 */ 12832 #if 0 12833 msg.dt.sg_list[0].addr = (void *) 12834 vtophys(io->scsiio.kern_data_ptr); 12835 #endif 12836 } 12837 12838 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12839 do_sg_copy = 0; 12840 } else { 12841 struct ctl_sg_entry *sgl; 12842 12843 do_sg_copy = 1; 12844 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12845 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12846 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12847 /* 12848 * XXX KDM use bus_dmamap_sync() here. 12849 */ 12850 } 12851 } 12852 12853 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12854 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12855 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12856 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12857 msg.dt.sg_sequence = 0; 12858 12859 /* 12860 * Loop until we've sent all of the S/G entries. On the 12861 * other end, we'll recompose these S/G entries into one 12862 * contiguous list before passing it to the 12863 */ 12864 for (sg_entries_sent = 0; sg_entries_sent < 12865 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12866 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12867 sizeof(msg.dt.sg_list[0])), 12868 msg.dt.kern_sg_entries - sg_entries_sent); 12869 12870 if (do_sg_copy != 0) { 12871 struct ctl_sg_entry *sgl; 12872 int j; 12873 12874 sgl = (struct ctl_sg_entry *) 12875 io->scsiio.kern_data_ptr; 12876 /* 12877 * If this is in cached memory, flush the cache 12878 * before we send the DMA request to the other 12879 * controller. We want to do this in either 12880 * the * read or the write case. The read 12881 * case is straightforward. In the write 12882 * case, we want to make sure nothing is 12883 * in the local cache that could overwrite 12884 * the DMAed data. 12885 */ 12886 12887 for (i = sg_entries_sent, j = 0; 12888 i < msg.dt.cur_sg_entries; i++, j++) { 12889 if ((io->io_hdr.flags & 12890 CTL_FLAG_NO_DATASYNC) == 0) { 12891 /* 12892 * XXX KDM use bus_dmamap_sync() 12893 */ 12894 } 12895 if ((io->io_hdr.flags & 12896 CTL_FLAG_BUS_ADDR) == 0) { 12897 /* 12898 * XXX KDM use busdma. 12899 */ 12900 #if 0 12901 msg.dt.sg_list[j].addr =(void *) 12902 vtophys(sgl[i].addr); 12903 #endif 12904 } else { 12905 msg.dt.sg_list[j].addr = 12906 sgl[i].addr; 12907 } 12908 msg.dt.sg_list[j].len = sgl[i].len; 12909 } 12910 } 12911 12912 sg_entries_sent += msg.dt.cur_sg_entries; 12913 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12914 msg.dt.sg_last = 1; 12915 else 12916 msg.dt.sg_last = 0; 12917 12918 /* 12919 * XXX KDM drop and reacquire the lock here? 12920 */ 12921 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12922 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12923 /* 12924 * XXX do something here. 12925 */ 12926 } 12927 12928 msg.dt.sent_sg_entries = sg_entries_sent; 12929 } 12930 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12931 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12932 ctl_failover_io(io, /*have_lock*/ 0); 12933 12934 } else { 12935 12936 /* 12937 * Lookup the fe_datamove() function for this particular 12938 * front end. 12939 */ 12940 fe_datamove = 12941 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12942 12943 fe_datamove(io); 12944 } 12945 } 12946 12947 static void 12948 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12949 { 12950 union ctl_ha_msg msg; 12951 int isc_status; 12952 12953 memset(&msg, 0, sizeof(msg)); 12954 12955 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12956 msg.hdr.original_sc = io; 12957 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12958 msg.hdr.nexus = io->io_hdr.nexus; 12959 msg.hdr.status = io->io_hdr.status; 12960 msg.scsi.tag_num = io->scsiio.tag_num; 12961 msg.scsi.tag_type = io->scsiio.tag_type; 12962 msg.scsi.scsi_status = io->scsiio.scsi_status; 12963 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12964 sizeof(io->scsiio.sense_data)); 12965 msg.scsi.sense_len = io->scsiio.sense_len; 12966 msg.scsi.sense_residual = io->scsiio.sense_residual; 12967 msg.scsi.fetd_status = io->io_hdr.port_status; 12968 msg.scsi.residual = io->scsiio.residual; 12969 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12970 12971 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12972 ctl_failover_io(io, /*have_lock*/ have_lock); 12973 return; 12974 } 12975 12976 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12977 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12978 /* XXX do something if this fails */ 12979 } 12980 12981 } 12982 12983 /* 12984 * The DMA to the remote side is done, now we need to tell the other side 12985 * we're done so it can continue with its data movement. 12986 */ 12987 static void 12988 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12989 { 12990 union ctl_io *io; 12991 12992 io = rq->context; 12993 12994 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12995 printf("%s: ISC DMA write failed with error %d", __func__, 12996 rq->ret); 12997 ctl_set_internal_failure(&io->scsiio, 12998 /*sks_valid*/ 1, 12999 /*retry_count*/ rq->ret); 13000 } 13001 13002 ctl_dt_req_free(rq); 13003 13004 /* 13005 * In this case, we had to malloc the memory locally. Free it. 13006 */ 13007 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13008 int i; 13009 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13010 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13011 } 13012 /* 13013 * The data is in local and remote memory, so now we need to send 13014 * status (good or back) back to the other side. 13015 */ 13016 ctl_send_datamove_done(io, /*have_lock*/ 0); 13017 } 13018 13019 /* 13020 * We've moved the data from the host/controller into local memory. Now we 13021 * need to push it over to the remote controller's memory. 13022 */ 13023 static int 13024 ctl_datamove_remote_dm_write_cb(union ctl_io *io) 13025 { 13026 int retval; 13027 13028 retval = 0; 13029 13030 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 13031 ctl_datamove_remote_write_cb); 13032 13033 return (retval); 13034 } 13035 13036 static void 13037 ctl_datamove_remote_write(union ctl_io *io) 13038 { 13039 int retval; 13040 void (*fe_datamove)(union ctl_io *io); 13041 13042 /* 13043 * - Get the data from the host/HBA into local memory. 13044 * - DMA memory from the local controller to the remote controller. 13045 * - Send status back to the remote controller. 13046 */ 13047 13048 retval = ctl_datamove_remote_sgl_setup(io); 13049 if (retval != 0) 13050 return; 13051 13052 /* Switch the pointer over so the FETD knows what to do */ 13053 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13054 13055 /* 13056 * Use a custom move done callback, since we need to send completion 13057 * back to the other controller, not to the backend on this side. 13058 */ 13059 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 13060 13061 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13062 13063 fe_datamove(io); 13064 13065 return; 13066 13067 } 13068 13069 static int 13070 ctl_datamove_remote_dm_read_cb(union ctl_io *io) 13071 { 13072 #if 0 13073 char str[256]; 13074 char path_str[64]; 13075 struct sbuf sb; 13076 #endif 13077 13078 /* 13079 * In this case, we had to malloc the memory locally. Free it. 13080 */ 13081 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13082 int i; 13083 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13084 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13085 } 13086 13087 #if 0 13088 scsi_path_string(io, path_str, sizeof(path_str)); 13089 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13090 sbuf_cat(&sb, path_str); 13091 scsi_command_string(&io->scsiio, NULL, &sb); 13092 sbuf_printf(&sb, "\n"); 13093 sbuf_cat(&sb, path_str); 13094 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13095 io->scsiio.tag_num, io->scsiio.tag_type); 13096 sbuf_cat(&sb, path_str); 13097 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 13098 io->io_hdr.flags, io->io_hdr.status); 13099 sbuf_finish(&sb); 13100 printk("%s", sbuf_data(&sb)); 13101 #endif 13102 13103 13104 /* 13105 * The read is done, now we need to send status (good or bad) back 13106 * to the other side. 13107 */ 13108 ctl_send_datamove_done(io, /*have_lock*/ 0); 13109 13110 return (0); 13111 } 13112 13113 static void 13114 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13115 { 13116 union ctl_io *io; 13117 void (*fe_datamove)(union ctl_io *io); 13118 13119 io = rq->context; 13120 13121 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13122 printf("%s: ISC DMA read failed with error %d", __func__, 13123 rq->ret); 13124 ctl_set_internal_failure(&io->scsiio, 13125 /*sks_valid*/ 1, 13126 /*retry_count*/ rq->ret); 13127 } 13128 13129 ctl_dt_req_free(rq); 13130 13131 /* Switch the pointer over so the FETD knows what to do */ 13132 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13133 13134 /* 13135 * Use a custom move done callback, since we need to send completion 13136 * back to the other controller, not to the backend on this side. 13137 */ 13138 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13139 13140 /* XXX KDM add checks like the ones in ctl_datamove? */ 13141 13142 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13143 13144 fe_datamove(io); 13145 } 13146 13147 static int 13148 ctl_datamove_remote_sgl_setup(union ctl_io *io) 13149 { 13150 struct ctl_sg_entry *local_sglist, *remote_sglist; 13151 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13152 struct ctl_softc *softc; 13153 int retval; 13154 int i; 13155 13156 retval = 0; 13157 softc = control_softc; 13158 13159 local_sglist = io->io_hdr.local_sglist; 13160 local_dma_sglist = io->io_hdr.local_dma_sglist; 13161 remote_sglist = io->io_hdr.remote_sglist; 13162 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13163 13164 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13165 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13166 local_sglist[i].len = remote_sglist[i].len; 13167 13168 /* 13169 * XXX Detect the situation where the RS-level I/O 13170 * redirector on the other side has already read the 13171 * data off of the AOR RS on this side, and 13172 * transferred it to remote (mirror) memory on the 13173 * other side. Since we already have the data in 13174 * memory here, we just need to use it. 13175 * 13176 * XXX KDM this can probably be removed once we 13177 * get the cache device code in and take the 13178 * current AOR implementation out. 13179 */ 13180 #ifdef NEEDTOPORT 13181 if ((remote_sglist[i].addr >= 13182 (void *)vtophys(softc->mirr->addr)) 13183 && (remote_sglist[i].addr < 13184 ((void *)vtophys(softc->mirr->addr) + 13185 CacheMirrorOffset))) { 13186 local_sglist[i].addr = remote_sglist[i].addr - 13187 CacheMirrorOffset; 13188 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13189 CTL_FLAG_DATA_IN) 13190 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13191 } else { 13192 local_sglist[i].addr = remote_sglist[i].addr + 13193 CacheMirrorOffset; 13194 } 13195 #endif 13196 #if 0 13197 printf("%s: local %p, remote %p, len %d\n", 13198 __func__, local_sglist[i].addr, 13199 remote_sglist[i].addr, local_sglist[i].len); 13200 #endif 13201 } 13202 } else { 13203 uint32_t len_to_go; 13204 13205 /* 13206 * In this case, we don't have automatically allocated 13207 * memory for this I/O on this controller. This typically 13208 * happens with internal CTL I/O -- e.g. inquiry, mode 13209 * sense, etc. Anything coming from RAIDCore will have 13210 * a mirror area available. 13211 */ 13212 len_to_go = io->scsiio.kern_data_len; 13213 13214 /* 13215 * Clear the no datasync flag, we have to use malloced 13216 * buffers. 13217 */ 13218 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13219 13220 /* 13221 * The difficult thing here is that the size of the various 13222 * S/G segments may be different than the size from the 13223 * remote controller. That'll make it harder when DMAing 13224 * the data back to the other side. 13225 */ 13226 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13227 sizeof(io->io_hdr.remote_sglist[0])) && 13228 (len_to_go > 0); i++) { 13229 local_sglist[i].len = ctl_min(len_to_go, 131072); 13230 CTL_SIZE_8B(local_dma_sglist[i].len, 13231 local_sglist[i].len); 13232 local_sglist[i].addr = 13233 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13234 13235 local_dma_sglist[i].addr = local_sglist[i].addr; 13236 13237 if (local_sglist[i].addr == NULL) { 13238 int j; 13239 13240 printf("malloc failed for %zd bytes!", 13241 local_dma_sglist[i].len); 13242 for (j = 0; j < i; j++) { 13243 free(local_sglist[j].addr, M_CTL); 13244 } 13245 ctl_set_internal_failure(&io->scsiio, 13246 /*sks_valid*/ 1, 13247 /*retry_count*/ 4857); 13248 retval = 1; 13249 goto bailout_error; 13250 13251 } 13252 /* XXX KDM do we need a sync here? */ 13253 13254 len_to_go -= local_sglist[i].len; 13255 } 13256 /* 13257 * Reset the number of S/G entries accordingly. The 13258 * original number of S/G entries is available in 13259 * rem_sg_entries. 13260 */ 13261 io->scsiio.kern_sg_entries = i; 13262 13263 #if 0 13264 printf("%s: kern_sg_entries = %d\n", __func__, 13265 io->scsiio.kern_sg_entries); 13266 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13267 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13268 local_sglist[i].addr, local_sglist[i].len, 13269 local_dma_sglist[i].len); 13270 #endif 13271 } 13272 13273 13274 return (retval); 13275 13276 bailout_error: 13277 13278 ctl_send_datamove_done(io, /*have_lock*/ 0); 13279 13280 return (retval); 13281 } 13282 13283 static int 13284 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13285 ctl_ha_dt_cb callback) 13286 { 13287 struct ctl_ha_dt_req *rq; 13288 struct ctl_sg_entry *remote_sglist, *local_sglist; 13289 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13290 uint32_t local_used, remote_used, total_used; 13291 int retval; 13292 int i, j; 13293 13294 retval = 0; 13295 13296 rq = ctl_dt_req_alloc(); 13297 13298 /* 13299 * If we failed to allocate the request, and if the DMA didn't fail 13300 * anyway, set busy status. This is just a resource allocation 13301 * failure. 13302 */ 13303 if ((rq == NULL) 13304 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13305 ctl_set_busy(&io->scsiio); 13306 13307 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13308 13309 if (rq != NULL) 13310 ctl_dt_req_free(rq); 13311 13312 /* 13313 * The data move failed. We need to return status back 13314 * to the other controller. No point in trying to DMA 13315 * data to the remote controller. 13316 */ 13317 13318 ctl_send_datamove_done(io, /*have_lock*/ 0); 13319 13320 retval = 1; 13321 13322 goto bailout; 13323 } 13324 13325 local_sglist = io->io_hdr.local_sglist; 13326 local_dma_sglist = io->io_hdr.local_dma_sglist; 13327 remote_sglist = io->io_hdr.remote_sglist; 13328 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13329 local_used = 0; 13330 remote_used = 0; 13331 total_used = 0; 13332 13333 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13334 rq->ret = CTL_HA_STATUS_SUCCESS; 13335 rq->context = io; 13336 callback(rq); 13337 goto bailout; 13338 } 13339 13340 /* 13341 * Pull/push the data over the wire from/to the other controller. 13342 * This takes into account the possibility that the local and 13343 * remote sglists may not be identical in terms of the size of 13344 * the elements and the number of elements. 13345 * 13346 * One fundamental assumption here is that the length allocated for 13347 * both the local and remote sglists is identical. Otherwise, we've 13348 * essentially got a coding error of some sort. 13349 */ 13350 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13351 int isc_ret; 13352 uint32_t cur_len, dma_length; 13353 uint8_t *tmp_ptr; 13354 13355 rq->id = CTL_HA_DATA_CTL; 13356 rq->command = command; 13357 rq->context = io; 13358 13359 /* 13360 * Both pointers should be aligned. But it is possible 13361 * that the allocation length is not. They should both 13362 * also have enough slack left over at the end, though, 13363 * to round up to the next 8 byte boundary. 13364 */ 13365 cur_len = ctl_min(local_sglist[i].len - local_used, 13366 remote_sglist[j].len - remote_used); 13367 13368 /* 13369 * In this case, we have a size issue and need to decrease 13370 * the size, except in the case where we actually have less 13371 * than 8 bytes left. In that case, we need to increase 13372 * the DMA length to get the last bit. 13373 */ 13374 if ((cur_len & 0x7) != 0) { 13375 if (cur_len > 0x7) { 13376 cur_len = cur_len - (cur_len & 0x7); 13377 dma_length = cur_len; 13378 } else { 13379 CTL_SIZE_8B(dma_length, cur_len); 13380 } 13381 13382 } else 13383 dma_length = cur_len; 13384 13385 /* 13386 * If we had to allocate memory for this I/O, instead of using 13387 * the non-cached mirror memory, we'll need to flush the cache 13388 * before trying to DMA to the other controller. 13389 * 13390 * We could end up doing this multiple times for the same 13391 * segment if we have a larger local segment than remote 13392 * segment. That shouldn't be an issue. 13393 */ 13394 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13395 /* 13396 * XXX KDM use bus_dmamap_sync() here. 13397 */ 13398 } 13399 13400 rq->size = dma_length; 13401 13402 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13403 tmp_ptr += local_used; 13404 13405 /* Use physical addresses when talking to ISC hardware */ 13406 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13407 /* XXX KDM use busdma */ 13408 #if 0 13409 rq->local = vtophys(tmp_ptr); 13410 #endif 13411 } else 13412 rq->local = tmp_ptr; 13413 13414 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13415 tmp_ptr += remote_used; 13416 rq->remote = tmp_ptr; 13417 13418 rq->callback = NULL; 13419 13420 local_used += cur_len; 13421 if (local_used >= local_sglist[i].len) { 13422 i++; 13423 local_used = 0; 13424 } 13425 13426 remote_used += cur_len; 13427 if (remote_used >= remote_sglist[j].len) { 13428 j++; 13429 remote_used = 0; 13430 } 13431 total_used += cur_len; 13432 13433 if (total_used >= io->scsiio.kern_data_len) 13434 rq->callback = callback; 13435 13436 if ((rq->size & 0x7) != 0) { 13437 printf("%s: warning: size %d is not on 8b boundary\n", 13438 __func__, rq->size); 13439 } 13440 if (((uintptr_t)rq->local & 0x7) != 0) { 13441 printf("%s: warning: local %p not on 8b boundary\n", 13442 __func__, rq->local); 13443 } 13444 if (((uintptr_t)rq->remote & 0x7) != 0) { 13445 printf("%s: warning: remote %p not on 8b boundary\n", 13446 __func__, rq->local); 13447 } 13448 #if 0 13449 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13450 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13451 rq->local, rq->remote, rq->size); 13452 #endif 13453 13454 isc_ret = ctl_dt_single(rq); 13455 if (isc_ret == CTL_HA_STATUS_WAIT) 13456 continue; 13457 13458 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13459 rq->ret = CTL_HA_STATUS_SUCCESS; 13460 } else { 13461 rq->ret = isc_ret; 13462 } 13463 callback(rq); 13464 goto bailout; 13465 } 13466 13467 bailout: 13468 return (retval); 13469 13470 } 13471 13472 static void 13473 ctl_datamove_remote_read(union ctl_io *io) 13474 { 13475 int retval; 13476 int i; 13477 13478 /* 13479 * This will send an error to the other controller in the case of a 13480 * failure. 13481 */ 13482 retval = ctl_datamove_remote_sgl_setup(io); 13483 if (retval != 0) 13484 return; 13485 13486 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13487 ctl_datamove_remote_read_cb); 13488 if ((retval != 0) 13489 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13490 /* 13491 * Make sure we free memory if there was an error.. The 13492 * ctl_datamove_remote_xfer() function will send the 13493 * datamove done message, or call the callback with an 13494 * error if there is a problem. 13495 */ 13496 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13497 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13498 } 13499 13500 return; 13501 } 13502 13503 /* 13504 * Process a datamove request from the other controller. This is used for 13505 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13506 * first. Once that is complete, the data gets DMAed into the remote 13507 * controller's memory. For reads, we DMA from the remote controller's 13508 * memory into our memory first, and then move it out to the FETD. 13509 */ 13510 static void 13511 ctl_datamove_remote(union ctl_io *io) 13512 { 13513 struct ctl_softc *softc; 13514 13515 softc = control_softc; 13516 13517 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13518 13519 /* 13520 * Note that we look for an aborted I/O here, but don't do some of 13521 * the other checks that ctl_datamove() normally does. 13522 * We don't need to run the datamove delay code, since that should 13523 * have been done if need be on the other controller. 13524 */ 13525 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13526 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13527 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13528 io->io_hdr.nexus.targ_port, 13529 io->io_hdr.nexus.targ_target.id, 13530 io->io_hdr.nexus.targ_lun); 13531 io->io_hdr.port_status = 31338; 13532 ctl_send_datamove_done(io, /*have_lock*/ 0); 13533 return; 13534 } 13535 13536 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13537 ctl_datamove_remote_write(io); 13538 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13539 ctl_datamove_remote_read(io); 13540 } else { 13541 union ctl_ha_msg msg; 13542 struct scsi_sense_data *sense; 13543 uint8_t sks[3]; 13544 int retry_count; 13545 13546 memset(&msg, 0, sizeof(msg)); 13547 13548 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13549 msg.hdr.status = CTL_SCSI_ERROR; 13550 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13551 13552 retry_count = 4243; 13553 13554 sense = &msg.scsi.sense_data; 13555 sks[0] = SSD_SCS_VALID; 13556 sks[1] = (retry_count >> 8) & 0xff; 13557 sks[2] = retry_count & 0xff; 13558 13559 /* "Internal target failure" */ 13560 scsi_set_sense_data(sense, 13561 /*sense_format*/ SSD_TYPE_NONE, 13562 /*current_error*/ 1, 13563 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13564 /*asc*/ 0x44, 13565 /*ascq*/ 0x00, 13566 /*type*/ SSD_ELEM_SKS, 13567 /*size*/ sizeof(sks), 13568 /*data*/ sks, 13569 SSD_ELEM_NONE); 13570 13571 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13572 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13573 ctl_failover_io(io, /*have_lock*/ 1); 13574 return; 13575 } 13576 13577 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13578 CTL_HA_STATUS_SUCCESS) { 13579 /* XXX KDM what to do if this fails? */ 13580 } 13581 return; 13582 } 13583 13584 } 13585 13586 static int 13587 ctl_process_done(union ctl_io *io) 13588 { 13589 struct ctl_lun *lun; 13590 struct ctl_softc *ctl_softc; 13591 void (*fe_done)(union ctl_io *io); 13592 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13593 13594 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13595 13596 fe_done = 13597 control_softc->ctl_ports[targ_port]->fe_done; 13598 13599 #ifdef CTL_TIME_IO 13600 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13601 char str[256]; 13602 char path_str[64]; 13603 struct sbuf sb; 13604 13605 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13606 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13607 13608 sbuf_cat(&sb, path_str); 13609 switch (io->io_hdr.io_type) { 13610 case CTL_IO_SCSI: 13611 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13612 sbuf_printf(&sb, "\n"); 13613 sbuf_cat(&sb, path_str); 13614 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13615 io->scsiio.tag_num, io->scsiio.tag_type); 13616 break; 13617 case CTL_IO_TASK: 13618 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13619 "Tag Type: %d\n", io->taskio.task_action, 13620 io->taskio.tag_num, io->taskio.tag_type); 13621 break; 13622 default: 13623 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13624 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13625 break; 13626 } 13627 sbuf_cat(&sb, path_str); 13628 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13629 (intmax_t)time_uptime - io->io_hdr.start_time); 13630 sbuf_finish(&sb); 13631 printf("%s", sbuf_data(&sb)); 13632 } 13633 #endif /* CTL_TIME_IO */ 13634 13635 switch (io->io_hdr.io_type) { 13636 case CTL_IO_SCSI: 13637 break; 13638 case CTL_IO_TASK: 13639 if (bootverbose || verbose > 0) 13640 ctl_io_error_print(io, NULL); 13641 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13642 ctl_free_io(io); 13643 else 13644 fe_done(io); 13645 return (CTL_RETVAL_COMPLETE); 13646 break; 13647 default: 13648 printf("ctl_process_done: invalid io type %d\n", 13649 io->io_hdr.io_type); 13650 panic("ctl_process_done: invalid io type %d\n", 13651 io->io_hdr.io_type); 13652 break; /* NOTREACHED */ 13653 } 13654 13655 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13656 if (lun == NULL) { 13657 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13658 io->io_hdr.nexus.targ_mapped_lun)); 13659 fe_done(io); 13660 goto bailout; 13661 } 13662 ctl_softc = lun->ctl_softc; 13663 13664 mtx_lock(&lun->lun_lock); 13665 13666 /* 13667 * Check to see if we have any errors to inject here. We only 13668 * inject errors for commands that don't already have errors set. 13669 */ 13670 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13671 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13672 ctl_inject_error(lun, io); 13673 13674 /* 13675 * XXX KDM how do we treat commands that aren't completed 13676 * successfully? 13677 * 13678 * XXX KDM should we also track I/O latency? 13679 */ 13680 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13681 io->io_hdr.io_type == CTL_IO_SCSI) { 13682 #ifdef CTL_TIME_IO 13683 struct bintime cur_bt; 13684 #endif 13685 int type; 13686 13687 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13688 CTL_FLAG_DATA_IN) 13689 type = CTL_STATS_READ; 13690 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13691 CTL_FLAG_DATA_OUT) 13692 type = CTL_STATS_WRITE; 13693 else 13694 type = CTL_STATS_NO_IO; 13695 13696 lun->stats.ports[targ_port].bytes[type] += 13697 io->scsiio.kern_total_len; 13698 lun->stats.ports[targ_port].operations[type]++; 13699 #ifdef CTL_TIME_IO 13700 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13701 &io->io_hdr.dma_bt); 13702 lun->stats.ports[targ_port].num_dmas[type] += 13703 io->io_hdr.num_dmas; 13704 getbintime(&cur_bt); 13705 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13706 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13707 #endif 13708 } 13709 13710 /* 13711 * Remove this from the OOA queue. 13712 */ 13713 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13714 13715 /* 13716 * Run through the blocked queue on this LUN and see if anything 13717 * has become unblocked, now that this transaction is done. 13718 */ 13719 ctl_check_blocked(lun); 13720 13721 /* 13722 * If the LUN has been invalidated, free it if there is nothing 13723 * left on its OOA queue. 13724 */ 13725 if ((lun->flags & CTL_LUN_INVALID) 13726 && TAILQ_EMPTY(&lun->ooa_queue)) { 13727 mtx_unlock(&lun->lun_lock); 13728 mtx_lock(&ctl_softc->ctl_lock); 13729 ctl_free_lun(lun); 13730 mtx_unlock(&ctl_softc->ctl_lock); 13731 } else 13732 mtx_unlock(&lun->lun_lock); 13733 13734 /* 13735 * If this command has been aborted, make sure we set the status 13736 * properly. The FETD is responsible for freeing the I/O and doing 13737 * whatever it needs to do to clean up its state. 13738 */ 13739 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13740 ctl_set_task_aborted(&io->scsiio); 13741 13742 /* 13743 * We print out status for every task management command. For SCSI 13744 * commands, we filter out any unit attention errors; they happen 13745 * on every boot, and would clutter up the log. Note: task 13746 * management commands aren't printed here, they are printed above, 13747 * since they should never even make it down here. 13748 */ 13749 switch (io->io_hdr.io_type) { 13750 case CTL_IO_SCSI: { 13751 int error_code, sense_key, asc, ascq; 13752 13753 sense_key = 0; 13754 13755 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13756 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13757 /* 13758 * Since this is just for printing, no need to 13759 * show errors here. 13760 */ 13761 scsi_extract_sense_len(&io->scsiio.sense_data, 13762 io->scsiio.sense_len, 13763 &error_code, 13764 &sense_key, 13765 &asc, 13766 &ascq, 13767 /*show_errors*/ 0); 13768 } 13769 13770 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13771 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13772 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13773 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13774 13775 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13776 ctl_softc->skipped_prints++; 13777 } else { 13778 uint32_t skipped_prints; 13779 13780 skipped_prints = ctl_softc->skipped_prints; 13781 13782 ctl_softc->skipped_prints = 0; 13783 ctl_softc->last_print_jiffies = time_uptime; 13784 13785 if (skipped_prints > 0) { 13786 #ifdef NEEDTOPORT 13787 csevent_log(CSC_CTL | CSC_SHELF_SW | 13788 CTL_ERROR_REPORT, 13789 csevent_LogType_Trace, 13790 csevent_Severity_Information, 13791 csevent_AlertLevel_Green, 13792 csevent_FRU_Firmware, 13793 csevent_FRU_Unknown, 13794 "High CTL error volume, %d prints " 13795 "skipped", skipped_prints); 13796 #endif 13797 } 13798 if (bootverbose || verbose > 0) 13799 ctl_io_error_print(io, NULL); 13800 } 13801 } 13802 break; 13803 } 13804 case CTL_IO_TASK: 13805 if (bootverbose || verbose > 0) 13806 ctl_io_error_print(io, NULL); 13807 break; 13808 default: 13809 break; 13810 } 13811 13812 /* 13813 * Tell the FETD or the other shelf controller we're done with this 13814 * command. Note that only SCSI commands get to this point. Task 13815 * management commands are completed above. 13816 * 13817 * We only send status to the other controller if we're in XFER 13818 * mode. In SER_ONLY mode, the I/O is done on the controller that 13819 * received the I/O (from CTL's perspective), and so the status is 13820 * generated there. 13821 * 13822 * XXX KDM if we hold the lock here, we could cause a deadlock 13823 * if the frontend comes back in in this context to queue 13824 * something. 13825 */ 13826 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13827 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13828 union ctl_ha_msg msg; 13829 13830 memset(&msg, 0, sizeof(msg)); 13831 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13832 msg.hdr.original_sc = io->io_hdr.original_sc; 13833 msg.hdr.nexus = io->io_hdr.nexus; 13834 msg.hdr.status = io->io_hdr.status; 13835 msg.scsi.scsi_status = io->scsiio.scsi_status; 13836 msg.scsi.tag_num = io->scsiio.tag_num; 13837 msg.scsi.tag_type = io->scsiio.tag_type; 13838 msg.scsi.sense_len = io->scsiio.sense_len; 13839 msg.scsi.sense_residual = io->scsiio.sense_residual; 13840 msg.scsi.residual = io->scsiio.residual; 13841 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13842 sizeof(io->scsiio.sense_data)); 13843 /* 13844 * We copy this whether or not this is an I/O-related 13845 * command. Otherwise, we'd have to go and check to see 13846 * whether it's a read/write command, and it really isn't 13847 * worth it. 13848 */ 13849 memcpy(&msg.scsi.lbalen, 13850 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13851 sizeof(msg.scsi.lbalen)); 13852 13853 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13854 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13855 /* XXX do something here */ 13856 } 13857 13858 ctl_free_io(io); 13859 } else 13860 fe_done(io); 13861 13862 bailout: 13863 13864 return (CTL_RETVAL_COMPLETE); 13865 } 13866 13867 #ifdef CTL_WITH_CA 13868 /* 13869 * Front end should call this if it doesn't do autosense. When the request 13870 * sense comes back in from the initiator, we'll dequeue this and send it. 13871 */ 13872 int 13873 ctl_queue_sense(union ctl_io *io) 13874 { 13875 struct ctl_lun *lun; 13876 struct ctl_softc *ctl_softc; 13877 uint32_t initidx, targ_lun; 13878 13879 ctl_softc = control_softc; 13880 13881 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13882 13883 /* 13884 * LUN lookup will likely move to the ctl_work_thread() once we 13885 * have our new queueing infrastructure (that doesn't put things on 13886 * a per-LUN queue initially). That is so that we can handle 13887 * things like an INQUIRY to a LUN that we don't have enabled. We 13888 * can't deal with that right now. 13889 */ 13890 mtx_lock(&ctl_softc->ctl_lock); 13891 13892 /* 13893 * If we don't have a LUN for this, just toss the sense 13894 * information. 13895 */ 13896 targ_lun = io->io_hdr.nexus.targ_lun; 13897 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13898 if ((targ_lun < CTL_MAX_LUNS) 13899 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13900 lun = ctl_softc->ctl_luns[targ_lun]; 13901 else 13902 goto bailout; 13903 13904 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13905 13906 mtx_lock(&lun->lun_lock); 13907 /* 13908 * Already have CA set for this LUN...toss the sense information. 13909 */ 13910 if (ctl_is_set(lun->have_ca, initidx)) { 13911 mtx_unlock(&lun->lun_lock); 13912 goto bailout; 13913 } 13914 13915 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13916 ctl_min(sizeof(lun->pending_sense[initidx]), 13917 sizeof(io->scsiio.sense_data))); 13918 ctl_set_mask(lun->have_ca, initidx); 13919 mtx_unlock(&lun->lun_lock); 13920 13921 bailout: 13922 mtx_unlock(&ctl_softc->ctl_lock); 13923 13924 ctl_free_io(io); 13925 13926 return (CTL_RETVAL_COMPLETE); 13927 } 13928 #endif 13929 13930 /* 13931 * Primary command inlet from frontend ports. All SCSI and task I/O 13932 * requests must go through this function. 13933 */ 13934 int 13935 ctl_queue(union ctl_io *io) 13936 { 13937 struct ctl_softc *ctl_softc; 13938 13939 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13940 13941 ctl_softc = control_softc; 13942 13943 #ifdef CTL_TIME_IO 13944 io->io_hdr.start_time = time_uptime; 13945 getbintime(&io->io_hdr.start_bt); 13946 #endif /* CTL_TIME_IO */ 13947 13948 /* Map FE-specific LUN ID into global one. */ 13949 io->io_hdr.nexus.targ_mapped_lun = 13950 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13951 13952 switch (io->io_hdr.io_type) { 13953 case CTL_IO_SCSI: 13954 case CTL_IO_TASK: 13955 ctl_enqueue_incoming(io); 13956 break; 13957 default: 13958 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13959 return (EINVAL); 13960 } 13961 13962 return (CTL_RETVAL_COMPLETE); 13963 } 13964 13965 #ifdef CTL_IO_DELAY 13966 static void 13967 ctl_done_timer_wakeup(void *arg) 13968 { 13969 union ctl_io *io; 13970 13971 io = (union ctl_io *)arg; 13972 ctl_done(io); 13973 } 13974 #endif /* CTL_IO_DELAY */ 13975 13976 void 13977 ctl_done(union ctl_io *io) 13978 { 13979 struct ctl_softc *ctl_softc; 13980 13981 ctl_softc = control_softc; 13982 13983 /* 13984 * Enable this to catch duplicate completion issues. 13985 */ 13986 #if 0 13987 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13988 printf("%s: type %d msg %d cdb %x iptl: " 13989 "%d:%d:%d:%d tag 0x%04x " 13990 "flag %#x status %x\n", 13991 __func__, 13992 io->io_hdr.io_type, 13993 io->io_hdr.msg_type, 13994 io->scsiio.cdb[0], 13995 io->io_hdr.nexus.initid.id, 13996 io->io_hdr.nexus.targ_port, 13997 io->io_hdr.nexus.targ_target.id, 13998 io->io_hdr.nexus.targ_lun, 13999 (io->io_hdr.io_type == 14000 CTL_IO_TASK) ? 14001 io->taskio.tag_num : 14002 io->scsiio.tag_num, 14003 io->io_hdr.flags, 14004 io->io_hdr.status); 14005 } else 14006 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 14007 #endif 14008 14009 /* 14010 * This is an internal copy of an I/O, and should not go through 14011 * the normal done processing logic. 14012 */ 14013 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 14014 return; 14015 14016 /* 14017 * We need to send a msg to the serializing shelf to finish the IO 14018 * as well. We don't send a finish message to the other shelf if 14019 * this is a task management command. Task management commands 14020 * aren't serialized in the OOA queue, but rather just executed on 14021 * both shelf controllers for commands that originated on that 14022 * controller. 14023 */ 14024 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 14025 && (io->io_hdr.io_type != CTL_IO_TASK)) { 14026 union ctl_ha_msg msg_io; 14027 14028 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 14029 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 14030 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 14031 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 14032 } 14033 /* continue on to finish IO */ 14034 } 14035 #ifdef CTL_IO_DELAY 14036 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 14037 struct ctl_lun *lun; 14038 14039 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14040 14041 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 14042 } else { 14043 struct ctl_lun *lun; 14044 14045 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14046 14047 if ((lun != NULL) 14048 && (lun->delay_info.done_delay > 0)) { 14049 struct callout *callout; 14050 14051 callout = (struct callout *)&io->io_hdr.timer_bytes; 14052 callout_init(callout, /*mpsafe*/ 1); 14053 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 14054 callout_reset(callout, 14055 lun->delay_info.done_delay * hz, 14056 ctl_done_timer_wakeup, io); 14057 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 14058 lun->delay_info.done_delay = 0; 14059 return; 14060 } 14061 } 14062 #endif /* CTL_IO_DELAY */ 14063 14064 ctl_enqueue_done(io); 14065 } 14066 14067 int 14068 ctl_isc(struct ctl_scsiio *ctsio) 14069 { 14070 struct ctl_lun *lun; 14071 int retval; 14072 14073 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14074 14075 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 14076 14077 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 14078 14079 retval = lun->backend->data_submit((union ctl_io *)ctsio); 14080 14081 return (retval); 14082 } 14083 14084 14085 static void 14086 ctl_work_thread(void *arg) 14087 { 14088 struct ctl_thread *thr = (struct ctl_thread *)arg; 14089 struct ctl_softc *softc = thr->ctl_softc; 14090 union ctl_io *io; 14091 int retval; 14092 14093 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 14094 14095 for (;;) { 14096 retval = 0; 14097 14098 /* 14099 * We handle the queues in this order: 14100 * - ISC 14101 * - done queue (to free up resources, unblock other commands) 14102 * - RtR queue 14103 * - incoming queue 14104 * 14105 * If those queues are empty, we break out of the loop and 14106 * go to sleep. 14107 */ 14108 mtx_lock(&thr->queue_lock); 14109 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 14110 if (io != NULL) { 14111 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 14112 mtx_unlock(&thr->queue_lock); 14113 ctl_handle_isc(io); 14114 continue; 14115 } 14116 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 14117 if (io != NULL) { 14118 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 14119 /* clear any blocked commands, call fe_done */ 14120 mtx_unlock(&thr->queue_lock); 14121 retval = ctl_process_done(io); 14122 continue; 14123 } 14124 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 14125 if (io != NULL) { 14126 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 14127 mtx_unlock(&thr->queue_lock); 14128 if (io->io_hdr.io_type == CTL_IO_TASK) 14129 ctl_run_task(io); 14130 else 14131 ctl_scsiio_precheck(softc, &io->scsiio); 14132 continue; 14133 } 14134 if (!ctl_pause_rtr) { 14135 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 14136 if (io != NULL) { 14137 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14138 mtx_unlock(&thr->queue_lock); 14139 retval = ctl_scsiio(&io->scsiio); 14140 if (retval != CTL_RETVAL_COMPLETE) 14141 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14142 continue; 14143 } 14144 } 14145 14146 /* Sleep until we have something to do. */ 14147 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 14148 } 14149 } 14150 14151 static void 14152 ctl_lun_thread(void *arg) 14153 { 14154 struct ctl_softc *softc = (struct ctl_softc *)arg; 14155 struct ctl_be_lun *be_lun; 14156 int retval; 14157 14158 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14159 14160 for (;;) { 14161 retval = 0; 14162 mtx_lock(&softc->ctl_lock); 14163 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14164 if (be_lun != NULL) { 14165 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14166 mtx_unlock(&softc->ctl_lock); 14167 ctl_create_lun(be_lun); 14168 continue; 14169 } 14170 14171 /* Sleep until we have something to do. */ 14172 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14173 PDROP | PRIBIO, "-", 0); 14174 } 14175 } 14176 14177 static void 14178 ctl_enqueue_incoming(union ctl_io *io) 14179 { 14180 struct ctl_softc *softc = control_softc; 14181 struct ctl_thread *thr; 14182 u_int idx; 14183 14184 idx = (io->io_hdr.nexus.targ_port * 127 + 14185 io->io_hdr.nexus.initid.id) % worker_threads; 14186 thr = &softc->threads[idx]; 14187 mtx_lock(&thr->queue_lock); 14188 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14189 mtx_unlock(&thr->queue_lock); 14190 wakeup(thr); 14191 } 14192 14193 static void 14194 ctl_enqueue_rtr(union ctl_io *io) 14195 { 14196 struct ctl_softc *softc = control_softc; 14197 struct ctl_thread *thr; 14198 14199 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14200 mtx_lock(&thr->queue_lock); 14201 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14202 mtx_unlock(&thr->queue_lock); 14203 wakeup(thr); 14204 } 14205 14206 static void 14207 ctl_enqueue_done(union ctl_io *io) 14208 { 14209 struct ctl_softc *softc = control_softc; 14210 struct ctl_thread *thr; 14211 14212 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14213 mtx_lock(&thr->queue_lock); 14214 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14215 mtx_unlock(&thr->queue_lock); 14216 wakeup(thr); 14217 } 14218 14219 static void 14220 ctl_enqueue_isc(union ctl_io *io) 14221 { 14222 struct ctl_softc *softc = control_softc; 14223 struct ctl_thread *thr; 14224 14225 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14226 mtx_lock(&thr->queue_lock); 14227 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14228 mtx_unlock(&thr->queue_lock); 14229 wakeup(thr); 14230 } 14231 14232 /* Initialization and failover */ 14233 14234 void 14235 ctl_init_isc_msg(void) 14236 { 14237 printf("CTL: Still calling this thing\n"); 14238 } 14239 14240 /* 14241 * Init component 14242 * Initializes component into configuration defined by bootMode 14243 * (see hasc-sv.c) 14244 * returns hasc_Status: 14245 * OK 14246 * ERROR - fatal error 14247 */ 14248 static ctl_ha_comp_status 14249 ctl_isc_init(struct ctl_ha_component *c) 14250 { 14251 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14252 14253 c->status = ret; 14254 return ret; 14255 } 14256 14257 /* Start component 14258 * Starts component in state requested. If component starts successfully, 14259 * it must set its own state to the requestrd state 14260 * When requested state is HASC_STATE_HA, the component may refine it 14261 * by adding _SLAVE or _MASTER flags. 14262 * Currently allowed state transitions are: 14263 * UNKNOWN->HA - initial startup 14264 * UNKNOWN->SINGLE - initial startup when no parter detected 14265 * HA->SINGLE - failover 14266 * returns ctl_ha_comp_status: 14267 * OK - component successfully started in requested state 14268 * FAILED - could not start the requested state, failover may 14269 * be possible 14270 * ERROR - fatal error detected, no future startup possible 14271 */ 14272 static ctl_ha_comp_status 14273 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14274 { 14275 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14276 14277 printf("%s: go\n", __func__); 14278 14279 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14280 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14281 ctl_is_single = 0; 14282 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14283 != CTL_HA_STATUS_SUCCESS) { 14284 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14285 ret = CTL_HA_COMP_STATUS_ERROR; 14286 } 14287 } else if (CTL_HA_STATE_IS_HA(c->state) 14288 && CTL_HA_STATE_IS_SINGLE(state)){ 14289 // HA->SINGLE transition 14290 ctl_failover(); 14291 ctl_is_single = 1; 14292 } else { 14293 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14294 c->state, state); 14295 ret = CTL_HA_COMP_STATUS_ERROR; 14296 } 14297 if (CTL_HA_STATE_IS_SINGLE(state)) 14298 ctl_is_single = 1; 14299 14300 c->state = state; 14301 c->status = ret; 14302 return ret; 14303 } 14304 14305 /* 14306 * Quiesce component 14307 * The component must clear any error conditions (set status to OK) and 14308 * prepare itself to another Start call 14309 * returns ctl_ha_comp_status: 14310 * OK 14311 * ERROR 14312 */ 14313 static ctl_ha_comp_status 14314 ctl_isc_quiesce(struct ctl_ha_component *c) 14315 { 14316 int ret = CTL_HA_COMP_STATUS_OK; 14317 14318 ctl_pause_rtr = 1; 14319 c->status = ret; 14320 return ret; 14321 } 14322 14323 struct ctl_ha_component ctl_ha_component_ctlisc = 14324 { 14325 .name = "CTL ISC", 14326 .state = CTL_HA_STATE_UNKNOWN, 14327 .init = ctl_isc_init, 14328 .start = ctl_isc_start, 14329 .quiesce = ctl_isc_quiesce 14330 }; 14331 14332 /* 14333 * vim: ts=8 14334 */ 14335